pychecker inspired clean-up, bugfixes

--HG--
branch : pmacs2
This commit is contained in:
Erik Osheim 2009-11-11 00:54:08 -05:00
parent abc6898fd1
commit 1fb326369c
4 changed files with 32 additions and 28 deletions

View File

@ -29,6 +29,11 @@ from point import Point
import util import util
from window import Window from window import Window
class PmcError(Exception): pass
class RunError(PmcError): pass
class FileError(PmcError): pass
class DrawError(PmcError): pass
class Application(object): class Application(object):
def __init__(self, stdscr, buffers=[], **kwargs): def __init__(self, stdscr, buffers=[], **kwargs):
# initalize curses primitives # initalize curses primitives
@ -195,19 +200,20 @@ class Application(object):
# initialize our buffers # initialize our buffers
# note that only the first buffer will be initially visible # note that only the first buffer will be initially visible
buffers.append(buffer.about.AboutBuffer()) buffers_ = list(buffers)
buffers.append(self.log) buffers_.append(buffer.about.AboutBuffer())
buffers_.append(self.log)
if self.rcerror: 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 # build windows for our buffers
for b in buffers: for b in buffers_:
if b.modename: if b.modename:
Window(b, self) Window(b, self)
else: else:
Window(b, self, mode_name=kwargs.get('init_mode')) Window(b, self, mode_name=kwargs.get('init_mode'))
self.bufferlist.add_buffer(b) 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 # see if the user has requested that we go to a particular line
jump_to_line = kwargs.get('jump_to_line') jump_to_line = kwargs.get('jump_to_line')
@ -367,7 +373,7 @@ class Application(object):
def get_window_height_width(self, i): def get_window_height_width(self, i):
slots = self.bufferlist.slots 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] slot = slots[i]
return (slot.height, slot.width) return (slot.height, slot.width)
@ -418,14 +424,14 @@ class Application(object):
b = buffer.fs.DirBuffer(path, name=name) b = buffer.fs.DirBuffer(path, name=name)
mode_name = 'dir' mode_name = 'dir'
else: else:
raise Exception, "not a file or dir: %r" % path raise FileError("not a file or dir: %r" % path)
elif cipher == 'aes': elif cipher == 'aes':
if not password: if not password:
raise Exception, "password is required" raise RunError("password is required")
if not os.path.exists(path) or os.path.isfile(path): if not os.path.exists(path) or os.path.isfile(path):
b = buffer.aes.AesBuffer(path, password, name=name) b = buffer.aes.AesBuffer(path, password, name=name)
else: else:
raise Exception, "not a file or dir: %r" % path raise FileError("not a file or dir: %r" % path)
try: try:
b.open() b.open()
except buffer.BinaryDataException: except buffer.BinaryDataException:
@ -648,7 +654,7 @@ class Application(object):
f = open(path, 'r') f = open(path, 'r')
exec(f) exec(f)
f.close() f.close()
except Exception, e: except:
s = traceback.format_exc() s = traceback.format_exc()
self.rcerror = 'There was an error during startup:\n\n' + s self.rcerror = 'There was an error during startup:\n\n' + s
@ -759,8 +765,8 @@ class Application(object):
self.draw_minibuffer() self.draw_minibuffer()
self.draw_cursor() self.draw_cursor()
self.win.refresh() self.win.refresh()
except Exception, e: except:
raise raise #XYZ
# ok, so there was a problem... # ok, so there was a problem...
# let's see if the screen changed sizes and if so, resize our slots # let's see if the screen changed sizes and if so, resize our slots
self.resize_event() self.resize_event()
@ -815,7 +821,7 @@ class Application(object):
else: else:
slot = self.bufferlist.slots[self.active_slot] slot = self.bufferlist.slots[self.active_slot]
w = slot.window 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): if w.active_point is not None and w.point_is_visible(w.active_point):
p = w.active_point p = w.active_point
else: else:
@ -827,13 +833,13 @@ class Application(object):
try: try:
self.win.move(vy, vx) self.win.move(vy, vx)
except: 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 # sub-drawing methods
def draw_slots(self): def draw_slots(self):
self.win.erase() self.win.erase()
for i in range(0, len(self.bufferlist.slots)): 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_slot(i)
self.draw_status_bar(i) self.draw_status_bar(i)
@ -843,10 +849,10 @@ class Application(object):
attr = color.build(fg, bg) attr = color.build(fg, bg)
try: try:
#self.win.addstr(sy, sx, char, attr) #self.win.addstr(sy, sx, char, attr)
self.addstr(sy, sx, char, attr) self.addstr(sy + 1000, sx, char, attr)
except Exception, e: except curses.error:
raise Exception, "(%d, %d, %r, %r) v. (%d, %d)" % \ raise DrawError("(%d, %d, %r, %r) v. (%d, %d)" %
(sy, sx, fg, bg, self.y, self.x) (sy, sx, fg, bg, self.y, self.x))
def highlight_chars(self, sy, sx1, sx2, fg='default', bg='default'): def highlight_chars(self, sy, sx1, sx2, fg='default', bg='default'):
assert sx2 < self.x, "%d < %d" % (sx2, self.x) assert sx2 < self.x, "%d < %d" % (sx2, self.x)
@ -975,10 +981,8 @@ class Application(object):
def _draw_slot(self, i): def _draw_slot(self, i):
slot = self.bufferlist.slots[i] slot = self.bufferlist.slots[i]
w = slot.window w = slot.window
redattr = color.build('red', 'default')
x, y = w.first.xy() x, y = w.first.xy()
lm, rm = w.mode.lmargin, w.mode.rmargin lm, rm = w.mode.lmargin, w.mode.rmargin
lines = w.buffer.lines
count = w.mode.header count = w.mode.header
swidth = slot.width - lm - rm swidth = slot.width - lm - rm
lit = w.mode.name in w.buffer.highlights lit = w.mode.name in w.buffer.highlights

View File

@ -130,13 +130,13 @@ class BufferList(object):
def close_buffer(self, b): def close_buffer(self, b):
for i in range(0, len(self.slots)): for i in range(0, len(self.slots)):
slot = self.slots[i] 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.unset_slot(i)
def remove_buffer(self, b): def remove_buffer(self, b):
assert b in self.buffers, "buffer %s does not exist" % (b.name()) assert b in self.buffers, "buffer %s does not exist" % (b.name())
for i in range(0, len(self.slots)): for i in range(0, len(self.slots)):
slot = self.slots[i] 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.unset_slot(i)
self.buffers.remove(b) self.buffers.remove(b)
del self.buffer_names[b.name()] del self.buffer_names[b.name()]

View File

@ -439,7 +439,7 @@ class ToggleMargins(Method):
class CenterView(Method): class CenterView(Method):
'''Move view to center on cursor''' '''Move view to center on cursor'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.center_view(force=True) w.center_view()
class SetMark(Method): class SetMark(Method):
'''Set the mark to the current cursor location''' '''Set the mark to the current cursor location'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
@ -922,7 +922,7 @@ class SplitWindow(Method):
a = w.application a = w.application
a.add_slot() a.add_slot()
if not w.cursor_is_visible(): if not w.cursor_is_visible():
w.center_view(force=True) w.center_view()
n = len(a.bufferlist.slots) n = len(a.bufferlist.slots)
a.set_error('Window has been split into %d windows!' % n) a.set_error('Window has been split into %d windows!' % n)

View File

@ -225,7 +225,7 @@ class Window(object):
def last_is_visible(self): def last_is_visible(self):
return self.point_is_visible(self.buffer.get_buffer_end()) 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() x, y = self.logical_cursor().xy()
counter = 0 counter = 0
while counter < self.height / 2: while counter < self.height / 2:
@ -613,7 +613,7 @@ class Window(object):
def copy_line(self): def copy_line(self):
(p1, p2) = self.get_line_right() (p1, p2) = self.get_line_right()
if p1 is not None: return self.copy(p1, p2) 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() (p1, p2) = self.get_region()
if p1 is not None: return self.copy(p1, p2) if p1 is not None: return self.copy(p1, p2)
@ -684,7 +684,7 @@ class Window(object):
self.goto(p) self.goto(p)
# highlighting tokens # highlighting tokens
def get_token_list(self, y): def get_token_list(self):
return self.get_token_list_at_point(self.logical_cursor()) return self.get_token_list_at_point(self.logical_cursor())
def get_token_list_at_point(self, p): def get_token_list_at_point(self, p):
return self.get_highlighter().tokens[p.y] return self.get_highlighter().tokens[p.y]