From 1fb326369cec1fa119f017134e134b3ae2ce47fe Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Wed, 11 Nov 2009 00:54:08 -0500 Subject: [PATCH] pychecker inspired clean-up, bugfixes --HG-- branch : pmacs2 --- application.py | 46 +++++++++++++++++++++++++--------------------- bufferlist.py | 4 ++-- method/__init__.py | 4 ++-- window.py | 6 +++--- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/application.py b/application.py index da85421..3391a5f 100755 --- a/application.py +++ b/application.py @@ -29,6 +29,11 @@ from point import Point import util from window import Window +class PmcError(Exception): pass +class RunError(PmcError): pass +class FileError(PmcError): pass +class DrawError(PmcError): pass + class Application(object): def __init__(self, stdscr, buffers=[], **kwargs): # initalize curses primitives @@ -195,19 +200,20 @@ class Application(object): # initialize our buffers # note that only the first buffer will be initially visible - buffers.append(buffer.about.AboutBuffer()) - buffers.append(self.log) + buffers_ = list(buffers) + buffers_.append(buffer.about.AboutBuffer()) + buffers_.append(self.log) if self.rcerror: - buffers.insert(0, buffer.data.DataBuffer('*RcError*', self.rcerror)) + buffers_.insert(0, buffer.data.DataBuffer('*RcError*', self.rcerror)) # build windows for our buffers - for b in buffers: + for b in buffers_: if b.modename: Window(b, self) else: Window(b, self, mode_name=kwargs.get('init_mode')) self.bufferlist.add_buffer(b) - self.bufferlist.set_slot(self.active_slot, buffers[0]) + self.bufferlist.set_slot(self.active_slot, buffers_[0]) # see if the user has requested that we go to a particular line jump_to_line = kwargs.get('jump_to_line') @@ -367,7 +373,7 @@ class Application(object): def get_window_height_width(self, i): slots = self.bufferlist.slots - assert i >= 0 and i < len(slots), "invalid slot: %r" % slotname + assert i >= 0 and i < len(slots), "invalid slot: %d" % i slot = slots[i] return (slot.height, slot.width) @@ -418,14 +424,14 @@ class Application(object): b = buffer.fs.DirBuffer(path, name=name) mode_name = 'dir' else: - raise Exception, "not a file or dir: %r" % path + raise FileError("not a file or dir: %r" % path) elif cipher == 'aes': if not password: - raise Exception, "password is required" + raise RunError("password is required") if not os.path.exists(path) or os.path.isfile(path): b = buffer.aes.AesBuffer(path, password, name=name) else: - raise Exception, "not a file or dir: %r" % path + raise FileError("not a file or dir: %r" % path) try: b.open() except buffer.BinaryDataException: @@ -648,7 +654,7 @@ class Application(object): f = open(path, 'r') exec(f) f.close() - except Exception, e: + except: s = traceback.format_exc() self.rcerror = 'There was an error during startup:\n\n' + s @@ -759,8 +765,8 @@ class Application(object): self.draw_minibuffer() self.draw_cursor() self.win.refresh() - except Exception, e: - raise + except: + raise #XYZ # ok, so there was a problem... # let's see if the screen changed sizes and if so, resize our slots self.resize_event() @@ -815,7 +821,7 @@ class Application(object): else: slot = self.bufferlist.slots[self.active_slot] w = slot.window - swidth = slot.width - w.mode.lmargin - w.mode.rmargin + #swidth = slot.width - w.mode.lmargin - w.mode.rmargin if w.active_point is not None and w.point_is_visible(w.active_point): p = w.active_point else: @@ -827,13 +833,13 @@ class Application(object): try: self.win.move(vy, vx) except: - raise Exception, "(%r=%r) no (%r)" % ((vx, vy), p, (self.x, self.y)) + raise DrawError("(%r=%r) no (%r)" % ((vx, vy), p, (self.x, self.y))) # sub-drawing methods def draw_slots(self): self.win.erase() for i in range(0, len(self.bufferlist.slots)): - slot = self.bufferlist.slots[i] + #slot = self.bufferlist.slots[i] self.draw_slot(i) self.draw_status_bar(i) @@ -843,10 +849,10 @@ class Application(object): attr = color.build(fg, bg) try: #self.win.addstr(sy, sx, char, attr) - self.addstr(sy, sx, char, attr) - except Exception, e: - raise Exception, "(%d, %d, %r, %r) v. (%d, %d)" % \ - (sy, sx, fg, bg, self.y, self.x) + self.addstr(sy + 1000, sx, char, attr) + except curses.error: + raise DrawError("(%d, %d, %r, %r) v. (%d, %d)" % + (sy, sx, fg, bg, self.y, self.x)) def highlight_chars(self, sy, sx1, sx2, fg='default', bg='default'): assert sx2 < self.x, "%d < %d" % (sx2, self.x) @@ -975,10 +981,8 @@ class Application(object): def _draw_slot(self, i): slot = self.bufferlist.slots[i] w = slot.window - redattr = color.build('red', 'default') x, y = w.first.xy() lm, rm = w.mode.lmargin, w.mode.rmargin - lines = w.buffer.lines count = w.mode.header swidth = slot.width - lm - rm lit = w.mode.name in w.buffer.highlights diff --git a/bufferlist.py b/bufferlist.py index 24ec0de..6238767 100644 --- a/bufferlist.py +++ b/bufferlist.py @@ -130,13 +130,13 @@ class BufferList(object): def close_buffer(self, b): for i in range(0, len(self.slots)): slot = self.slots[i] - if self.slots[i].window is not None and self.slots[i].window.buffer is b: + if slot.window is not None and slot.window.buffer is b: self.unset_slot(i) def remove_buffer(self, b): assert b in self.buffers, "buffer %s does not exist" % (b.name()) for i in range(0, len(self.slots)): slot = self.slots[i] - if self.slots[i].window is not None and self.slots[i].window.buffer is b: + if slot.window is not None and slot.window.buffer is b: self.unset_slot(i) self.buffers.remove(b) del self.buffer_names[b.name()] diff --git a/method/__init__.py b/method/__init__.py index c8c4685..6670b09 100644 --- a/method/__init__.py +++ b/method/__init__.py @@ -439,7 +439,7 @@ class ToggleMargins(Method): class CenterView(Method): '''Move view to center on cursor''' def _execute(self, w, **vargs): - w.center_view(force=True) + w.center_view() class SetMark(Method): '''Set the mark to the current cursor location''' def _execute(self, w, **vargs): @@ -922,7 +922,7 @@ class SplitWindow(Method): a = w.application a.add_slot() if not w.cursor_is_visible(): - w.center_view(force=True) + w.center_view() n = len(a.bufferlist.slots) a.set_error('Window has been split into %d windows!' % n) diff --git a/window.py b/window.py index 7a23fa7..e2e0c3b 100644 --- a/window.py +++ b/window.py @@ -225,7 +225,7 @@ class Window(object): def last_is_visible(self): return self.point_is_visible(self.buffer.get_buffer_end()) - def center_view(self, force=False): + def center_view(self): x, y = self.logical_cursor().xy() counter = 0 while counter < self.height / 2: @@ -613,7 +613,7 @@ class Window(object): def copy_line(self): (p1, p2) = self.get_line_right() if p1 is not None: return self.copy(p1, p2) - def copy_region(self, kill=False): + def copy_region(self): (p1, p2) = self.get_region() if p1 is not None: return self.copy(p1, p2) @@ -684,7 +684,7 @@ class Window(object): self.goto(p) # highlighting tokens - def get_token_list(self, y): + def get_token_list(self): return self.get_token_list_at_point(self.logical_cursor()) def get_token_list_at_point(self, p): return self.get_highlighter().tokens[p.y]