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