parent
277cd82b68
commit
f8680694b3
|
@ -519,13 +519,13 @@ class WrapLine(Method):
|
|||
for t in tokens:
|
||||
l += len(t)
|
||||
return l
|
||||
def _find_line_bounds(self, tokens, x, y):
|
||||
if len(tokens[0]) > self.limit:
|
||||
def _find_line_bounds(self, limit, tokens, x, y):
|
||||
if len(tokens[0]) > limit:
|
||||
i = 1
|
||||
else:
|
||||
i = 0
|
||||
l = self._token_len(tokens[:i+1])
|
||||
while i < len(tokens) and l <= self.limit:
|
||||
while i < len(tokens) and l <= limit:
|
||||
i += 1
|
||||
l = self._token_len(tokens[:i+1])
|
||||
while i > 1 and tokens and tokens[i-1].isspace():
|
||||
|
@ -542,14 +542,14 @@ class WrapLine(Method):
|
|||
del tokens[0]
|
||||
return x, y
|
||||
|
||||
def _wrap_line(self, line, x, y):
|
||||
def _wrap_line(self, limit, line, x, y):
|
||||
tokens = re.findall('[^ ]+| +', line)
|
||||
if self._token_len(tokens) <= self.limit:
|
||||
if self._token_len(tokens) <= limit:
|
||||
return None, None, None
|
||||
|
||||
lines = []
|
||||
while tokens and self._token_len(tokens) > self.limit:
|
||||
i, x, y = self._find_line_bounds(tokens, x, y)
|
||||
while tokens and self._token_len(tokens) > limit:
|
||||
i, x, y = self._find_line_bounds(limit, tokens, x, y)
|
||||
s = ''.join(tokens[:i])
|
||||
lines.append(s)
|
||||
if x > len(s):
|
||||
|
@ -562,9 +562,10 @@ class WrapLine(Method):
|
|||
|
||||
return lines, x, y
|
||||
def _execute(self, w, **vargs):
|
||||
limit = util.get_margin_limit(w, self.limit)
|
||||
cursor = w.logical_cursor()
|
||||
x, y = cursor.xy()
|
||||
lines, x, y = self._wrap_line(w.buffer.lines[y], x, y)
|
||||
lines, x, y = self._wrap_line(limit, w.buffer.lines[y], x, y)
|
||||
if lines is None:
|
||||
return
|
||||
p1 = Point(0, cursor.y)
|
||||
|
@ -575,10 +576,12 @@ class WrapLine(Method):
|
|||
w.goto(Point(x, y))
|
||||
|
||||
class WrapParagraph(Method):
|
||||
limit = 80
|
||||
limit = 80
|
||||
valid_re = re.compile('^( *)([^ ].*)$')
|
||||
empty_re = regex.whitespace
|
||||
def _execute(self, w, **vargs):
|
||||
limit = util.get_margin_limit(w, self.limit)
|
||||
|
||||
# we will store the start of our paragaph in p1, and also the original
|
||||
# cursor position.
|
||||
p1 = oldc = w.logical_cursor()
|
||||
|
@ -631,12 +634,12 @@ class WrapParagraph(Method):
|
|||
newlines = []
|
||||
while s:
|
||||
# if we have less than the limit left, add it and we're done!
|
||||
if len(s) < self.limit:
|
||||
if len(s) < limit:
|
||||
newlines.append(s)
|
||||
break
|
||||
|
||||
# look for the rightmost space within our bounds
|
||||
j = s.rfind(' ', 0, self.limit)
|
||||
j = s.rfind(' ', 0, limit)
|
||||
# if we failed to find one, look for the leftmost space
|
||||
if j == -1:
|
||||
j = s.find(' ')
|
||||
|
|
7
util.py
7
util.py
|
@ -40,3 +40,10 @@ def dump(x):
|
|||
for name in dir(x):
|
||||
d[name] = getattr(x, name)
|
||||
return '%s: %r' % (x, d)
|
||||
|
||||
def get_margin_limit(w, def_limit=80):
|
||||
lname = '%s.margin' % w.mode.name()
|
||||
if lname in w.application.config:
|
||||
return w.application.config[lname]
|
||||
else:
|
||||
return w.application.config.get('margin', def_limit)
|
||||
|
|
Loading…
Reference in New Issue