From abc6898fd13c4487e6b7ce7d65c6a68967717f9a Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Wed, 11 Nov 2009 00:27:40 -0500 Subject: [PATCH] less cursor jumping on auto-ident; close #28 --HG-- branch : pmacs2 --- method/__init__.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/method/__init__.py b/method/__init__.py index 9edda6e..c8c4685 100644 --- a/method/__init__.py +++ b/method/__init__.py @@ -478,20 +478,28 @@ class InsertTab(Method): '''Insert tab into buffer, or tabbify line, depending on mode''' def _execute(self, w, **vargs): cursor = w.logical_cursor() - if w.mode.tabber: - i = w.mode.tabber.get_level(cursor.y) - else: - i = None + x = cursor.x + y = cursor.y - if i is None: - w.insert_string_at_cursor(' ' * w.mode.tabwidth) + # get the correct indentation lvl, if applicable + if w.mode.tabber: + lvl = w.mode.tabber.get_level(y) else: - j = w.buffer.count_leading_whitespace(cursor.y) - if i != j: - KillWhitespace().execute(w) - w.insert_string(Point(0, cursor.y), ' ' * i) - else: - w.goto(Point(j, cursor.y)) + lvl = None + + # if no lvl, insert a literal tab + if lvl is None: + w.insert_string_at_cursor(' ' * w.mode.tabwidth) + return + + # insert the correct amount of whitespace + ws = w.buffer.count_leading_whitespace(y) + if lvl != ws: + w.delete(Point(0, y), Point(ws, y)) + w.insert_string(Point(0, y), ' ' * lvl) + x2 = max(x, lvl) + if w.logical_cursor().x < x2: + w.goto(Point(x2, y)) class KillWhitespace(Method): '''Delete leading whitespace on current line'''