improved (but not perfect) wrap-paragraph

--HG--
branch : pmacs2
This commit is contained in:
Erik Osheim 2009-06-11 01:59:31 -04:00
parent cc4108d107
commit 2f23872896
3 changed files with 20 additions and 26 deletions

View File

@ -80,22 +80,14 @@ def path_fields(path, name):
# - regular, b block, c character, d directory, l symlink, p fifo
# s socket, ? unknown
perm = [' '] * 10
if stat.S_ISREG(info.st_mode):
perm[0] = '-'
elif stat.S_ISBLK(info.st_mode):
perm[0] = 'b'
elif stat.S_ISCHR(info.st_mode):
perm[0] = 'c'
elif stat.S_ISDIR(info.st_mode):
perm[0] = 'd'
elif stat.S_ISLNK(info.st_mode):
perm[0] = 'l'
elif stat.S_ISFIFO(info.st_mode):
perm[0] = 'p'
elif stat.S_ISSOCK(info.st_mode):
perm[0] = 's'
else:
perm[0] = '?'
if stat.S_ISREG(info.st_mode): perm[0] = '-'
elif stat.S_ISBLK(info.st_mode): perm[0] = 'b'
elif stat.S_ISCHR(info.st_mode): perm[0] = 'c'
elif stat.S_ISDIR(info.st_mode): perm[0] = 'd'
elif stat.S_ISLNK(info.st_mode): perm[0] = 'l'
elif stat.S_ISFIFO(info.st_mode): perm[0] = 'p'
elif stat.S_ISSOCK(info.st_mode): perm[0] = 's'
else: perm[0] = '?'
i = 1
symbols = ('r', 'w', 'x')

View File

@ -644,8 +644,8 @@ class WrapLine(Method):
class WrapParagraph(Method):
'''Wrap contiguous lines of text based on a predefined margin'''
limit = 80
valid_re = re.compile('^( *)([^ ].*)$')
empty_re = regex.whitespace
valid_re = re.compile('^()( *)([^ ].*)$')
empty_re = re.compile('^ ')
def _execute(self, w, **vargs):
limit = util.get_margin_limit(w, self.limit)
@ -654,11 +654,14 @@ class WrapParagraph(Method):
p1 = oldc = w.logical_cursor()
cur_offset = 0
if self.empty_re.match(w.buffer.lines[p1.y]):
return
m = self.valid_re.match(w.buffer.lines[p1.y])
if not m:
# the line was empty
return
elif m.group(1):
elif not m.group(1) and m.group(2):
# the line had leading whitespace
return
@ -666,7 +669,8 @@ class WrapParagraph(Method):
# let's find the actual begining, and update p1 accordingly.
i = p1.y
if i > 1 and w.buffer.lines[i] and not w.buffer.lines[i].startswith(' '):
while i > 1 and w.buffer.lines[i - 1] and not w.buffer.lines[i - 1].startswith(' '):
while (i > 1 and w.buffer.lines[i - 1] and
not self.empty_re.match(w.buffer.lines[i - 1])):
i -= 1
p1 = Point(0, i)
@ -679,7 +683,8 @@ class WrapParagraph(Method):
# ok, so now let's move forward and find the end of the paragraph.
i = p1.y + 1
while i < len(w.buffer.lines) and w.buffer.lines[i] and not w.buffer.lines[i].startswith(' '):
while (i < len(w.buffer.lines) and w.buffer.lines[i] and
not self.empty_re.match(w.buffer.lines[i])):
s1 = w.buffer.lines[i]
s2 = s1.rstrip()
if oldc.y == i:
@ -697,8 +702,6 @@ class WrapParagraph(Method):
# stringify our paragraph
s = " ".join(lines)
#raise Exception, '%r %r' % (limit, s)
# ok, so now we need to find the line breaks
newlines = []
while s:
@ -727,8 +730,6 @@ class WrapParagraph(Method):
y += 1
k += 1
#assert len(newlines), 'fooga: %r %r' % (limit, s)
# kill the old paragraph region, insert the new, and goto the new cursor
w.delete(p1, Point(len(w.buffer.lines[i-1]), i-1))
w.insert_lines(p1, newlines)

View File

@ -24,7 +24,8 @@ class MuttGrammar(Grammar):
class MuttWrapParagraph(WrapParagraph):
limit = 72
empty_re = re.compile('^(?: *>.*| *)$')
valid_re = re.compile('^((?:>[> ]*)?)( *)([^> ].*)$')
empty_re = re.compile('^[> ]*$')
class MuttInsertSpace(TextInsertSpace):
wrapper = MuttWrapParagraph