better centering support

--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-11-18 16:38:43 +00:00
parent 155044696f
commit 3b10120e58
2 changed files with 25 additions and 12 deletions

View File

@ -396,7 +396,7 @@ class ToggleMargins(Method):
class CenterView(Method):
'''Move view to center on cursor'''
def _execute(self, w, **vargs):
w.center_view()
w.center_view(force=True)
class SetMark(Method):
'''Set the mark to the current cursor location'''
def _execute(self, w, **vargs):

View File

@ -221,7 +221,7 @@ class Window(object):
def last_is_visible(self):
return self.point_is_visible(self.buffer.get_buffer_end())
def center_view(self):
def center_view(self, force=False):
(x, y) = self.logical_cursor().xy()
counter = 0
while counter < self.height / 2:
@ -234,6 +234,14 @@ class Window(object):
(x, y) = (0, 0)
break
counter += 1
# make sure we aren't "centering" on the end of the file (where half the
# screen is empty). that is, unless that's what the user wants
# (indicated by force=True).
if not force:
(x2, y2) = self.last_visible_coords()
if y2 < y or x2 < x:
x, y = x2, y2
self.first = Point(x - (x % self.width), y)
self.redraw()
def lower_view(self):
@ -267,11 +275,13 @@ class Window(object):
self.first = Point(x - (x % self.width), y)
self.redraw()
def assure_visible_cursor(self):
p = self.logical_cursor()
if self.first > p:
self.upper_view()
elif p > self.last:
self.lower_view()
if not self.cursor_is_visible():
self.center_view()
#p = self.logical_cursor()
#if self.first > p:
# self.upper_view()
#elif p > self.last:
# self.lower_view()
# moving in buffer
def forward(self):
@ -457,7 +467,13 @@ class Window(object):
self.assure_visible_cursor()
def goto_end(self, force=False):
self.cursor = self.buffer.get_buffer_end()
(x, y) = self.logical_cursor().xy()
(x, y) = self.last_visible_coords()
if force or not self.cursor_is_visible():
self.first = Point(x - (x % self.width), y)
self.redraw()
def last_visible_coords(self):
(x, y) = self.buffer.get_buffer_end().xy()
if x == 0:
y -= 1
x = len(self.buffer.lines[y])
@ -474,10 +490,7 @@ class Window(object):
(x, y) = (0, 0)
break
counter += 1
if force or not self.cursor_is_visible():
self.first = Point(x - (x % self.width), y)
self.redraw()
return (x, y)
# mark manipulation
def set_mark_point(self, p):