added increment and decrement functions

--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-11-09 05:43:34 +00:00
parent dc5c58a09f
commit 714705ec7c
4 changed files with 59 additions and 4 deletions

View File

@ -89,7 +89,7 @@ class Application(object):
self.methods = {}
names = ('method', 'method.svn', 'method.cvs', 'method.search',
'method.buffers', 'method.move', 'method.shell',
'method.introspect', 'method.help')
'method.introspect', 'method.help', 'method.numbers')
for name in names:
exec("import %s" % name)
mod = eval(name)

55
method/numbers.py Normal file
View File

@ -0,0 +1,55 @@
import re
import buffer, default, dirutil, regex, util, window
from point import Point
from method import Method, Argument
num_re = re.compile(r'(?:-?[1-9][0-9]*|0x[0-9A-Fa-f]+|0[0-7]+|0)')
def find_next_number(w):
b = w.buffer
x, y = w.logical_cursor().xy()
while y < len(b.lines):
m = num_re.search(b.lines[y], x)
if m:
return (Point(m.start(), y), Point(m.end(), y))
x = 0
y += 1
return (None, None)
def get_converter(s):
if s.startswith('0x'):
return hex
elif s.startswith('0') and len(s) > 1:
return oct
else:
return str
class Increment(Method):
'''Increment the next number'''
def _execute(self, w, **vargs):
(p1, p2) = find_next_number(w)
if p1 is None:
return
s = w.buffer.lines[p1.y][p1.x:p2.x]
num = int(s)
conv = get_converter(s)
w.set_error('found %r' % num)
w.delete(p1, p2)
s2 = conv(num + 1)
w.insert_string(p1, s2)
w.goto(p1)
class Decrement(Method):
'''Decrement the next number'''
def _execute(self, w, **vargs):
(p1, p2) = find_next_number(w)
if p1 is None:
return
s = w.buffer.lines[p1.y][p1.x:p2.x]
num = int(s)
conv = get_converter(s)
w.set_error('found %r' % num)
w.delete(p1, p2)
s2 = conv(num - 1)
w.insert_string(p1, s2)
w.goto(p1)

View File

@ -231,6 +231,8 @@ class Fundamental(Handler):
self.add_bindings('insert-text', ('C-c i',))
self.add_bindings('insert-text2', ('C-c M-i',))
self.add_bindings('insert-multiline-text', ('C-c m',))
self.add_bindings('increment', ('M-+',))
self.add_bindings('decrement', ('M--',))
# used for all word operations
if not self.word_letters:

View File

@ -16,7 +16,7 @@ class PodGrammar(Grammar):
]
def _make_string_rules(forbidden):
rule1 = PatternRule(r'scalar', r"\$[^\[\]\(\){}<>A-Za-z0-9 \\%s](?![A-Za-z0-9_])" % forbidden)
#rule1 = PatternRule(r'scalar', r"\$[^\[\]\(\){}<>A-Za-z0-9 \\%s](?![A-Za-z0-9_])" % forbidden)
#rule2 = PatternRule(r'data', r"[^%s\\\@\$%%\&]+" % forbidden)
rules = [
@ -97,7 +97,6 @@ class PerlGrammar(Grammar):
PatternRule(r'length', r"\$#[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*"),
PatternRule(r'cast', r'[\$\@\%\^\&](?= *{)'),
PatternRule(r'scalar', r"\$[\[\]<>ab/'\"_@\?#\$!%^|&*()](?![A-Za-z0-9_])"),
#PatternRule(r'scalar', r"\$(?:_|a|b|\d+|&|`|'|\+|\*|\.|/|\||,|\\|\"|;|#|%|=|-|~|:|\?|!|@|\$|<|>|\(|\)|0|\[|\]|\^[ACDEFHILMNOPRSTVWX]|\^|ARGV)"),
PatternRule(r'array', r"@_"),
PatternRule(r'perl_function', r"\$\$*[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*(?=-> *\()"),
PatternRule(r'scalar', r"\$\$*[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*"),
@ -608,7 +607,6 @@ class Perl(mode.Fundamental):
modename = 'Perl'
extensions = ['.pl', '.pm']
detection = ['perl']
#tabbercls = PerlTabber
tabbercls = PerlTabber2
grammar = PerlGrammar
opentokens = ('delimiter',)