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)