line-wrapping should be improved

--HG--
branch : pmacs2
This commit is contained in:
moculus 2007-10-26 18:36:42 +00:00
parent 6c03924d91
commit 58979d7825
2 changed files with 47 additions and 25 deletions

View File

@ -289,6 +289,14 @@ class Buffer(object):
def overwrite_char(self, p, c, act=ACT_NORM, force=False): def overwrite_char(self, p, c, act=ACT_NORM, force=False):
self.delete_char(p, act=act, force=force) self.delete_char(p, act=act, force=force)
self.insert_string(p, c, act=act, force=force) self.insert_string(p, c, act=act, force=force)
def delete_line(self, y, act=ACT_NORM, force=False):
line = self.lines[y]
p1 = Point(0, y)
if y < len(self.lines) - 1:
p2 = Point(0, y + 1)
else:
p2 = Point(len(self.lines[-1]), y)
self.delete(p1, p2, act, force)
# random # random
def count_leading_whitespace(self, y): def count_leading_whitespace(self, y):

View File

@ -644,36 +644,50 @@ class UncommentRegion(Method):
# wrapping/justifying/etc # wrapping/justifying/etc
class WrapLine(Method): class WrapLine(Method):
'''Wrap the current line at 80 characters by word'''
limit = 80 limit = 80
prefix_re = None def _token_len(self, tokens):
l = 0
for t in tokens:
l += len(t)
return l
def _find_line_bounds(self, tokens):
if len(tokens[0]) > self.limit:
i = 1
else:
i = 0
while i < len(tokens) and self._token_len(tokens[:i+1]) <= self.limit:
i += 1
while i > 1 and tokens and tokens[i-1].isspace():
del tokens[i-1]
i -= 1
return i
def _clear_preceeding_spaces(self, tokens):
while tokens and tokens[0].isspace():
del tokens[0]
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
cursor = w.logical_cursor() cursor = w.logical_cursor()
old_cursor = cursor x, y = cursor.xy()
i = cursor.y
move_old_cursor = old_cursor.x > self.limit
while len(w.buffer.lines[i]) > self.limit: tokens = re.findall('[^ ]+| +', w.buffer.lines[y])
if ' ' in w.buffer.lines[i][:self.limit]: if self._token_len(tokens) <= self.limit:
j = w.buffer.lines[i][:self.limit].rindex(' ') return
elif ' ' in w.buffer.lines[i][self.limit:]:
j = w.buffer.lines[i][self.limit:].index(' ')
else:
break
if move_old_cursor:
move_old_cursor = False
old_cursor.x -= j + 1
old_cursor.y += 1
w.goto(Point(j, i))
w.right_delete()
w.insert_string_at_cursor('\n')
i += 1
l = len(w.buffer.lines[old_cursor.y]) lines = []
if l > old_cursor.x: while tokens and self._token_len(tokens) > self.limit:
w.goto(old_cursor) i = self._find_line_bounds(tokens)
else: s = ''.join(tokens[:i])
w.goto(Point(l, old_cursor.y)) lines.append(s)
if x > len(s):
y += 1
x -= len(s)
del tokens[:i]
self._clear_preceeding_spaces(tokens)
if tokens:
lines.append(''.join(tokens))
w.buffer.delete_line(cursor.y)
w.buffer.insert_lines(Point(0, cursor.y), lines)
w.goto(Point(x, y))
class WrapParagraph(Method): class WrapParagraph(Method):
limit = 80 limit = 80
wrapper = WrapLine wrapper = WrapLine