From 630077cbad27f5c9e861d4aaf82745426f2a55a4 Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Wed, 18 Nov 2009 19:56:21 -0500 Subject: [PATCH] improved support for octal/hex --HG-- branch : pmacs2 --- method/numbers.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/method/numbers.py b/method/numbers.py index 14d8c75..ca9364a 100644 --- a/method/numbers.py +++ b/method/numbers.py @@ -3,7 +3,7 @@ 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)') +num_re = re.compile(r'(?:-?[1-9][0-9]*|0[xX][0-9A-Fa-f]+|0[0-7]+|0)') def find_next_number(w, offset=0): b = w.buffer @@ -12,10 +12,16 @@ def find_next_number(w, offset=0): while y < len(b.lines): m = num_re.search(b.lines[y], x) if m: - return (Point(m.start(), y), Point(m.end(), y)) + if m.group(0).lower().startswith('0x'): + base = 16 + elif m.group(0).startswith('0'): + base = 8 + else: + base = 10 + return (Point(m.start(), y), Point(m.end(), y), base) x = 0 y += 1 - return (None, None) + return (None, None, None) def find_prev_number(w, offset=0): b = w.buffer x, y = w.logical_cursor().xy() @@ -32,15 +38,14 @@ def find_prev_number(w, offset=0): return (None, None) def get_converter(s): - if s.startswith('0x'): - return hex + if s.startswith('0x') or s.startswith('0X'): + return lambda x: '0x%02x' % x elif s.startswith('0') and len(s) > 1: return oct else: return str class GotoPrevNumber(Method): - '''Move the cursor to the start of the word to the right''' def _execute(self, w, **vargs): (p1, p2) = find_prev_number(w, -1) if p1 is not None: @@ -48,18 +53,18 @@ class GotoPrevNumber(Method): class GotoNextNumber(Method): def _execute(self, w, **vargs): - (p1, p2) = find_next_number(w, 1) + (p1, p2, base) = find_next_number(w, 1) if p1 is not None: w.goto(p2.add(-1, 0)) class Increment(Method): '''Increment the next number''' def _execute(self, w, **vargs): - (p1, p2) = find_next_number(w) + (p1, p2, base) = find_next_number(w) if p1 is None: return s = w.buffer.lines[p1.y][p1.x:p2.x] - num = int(s) + num = int(s, base) conv = get_converter(s) w.set_error('found %r' % num) w.delete(p1, p2) @@ -70,11 +75,11 @@ class Increment(Method): class Decrement(Method): '''Decrement the next number''' def _execute(self, w, **vargs): - (p1, p2) = find_next_number(w) + (p1, p2, base) = find_next_number(w) if p1 is None: return s = w.buffer.lines[p1.y][p1.x:p2.x] - num = int(s) + num = int(s, base) conv = get_converter(s) w.set_error('found %r' % num) w.delete(p1, p2)