diff options
author | Guido van Rossum <guido@python.org> | 1999-05-03 15:49:52 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1999-05-03 15:49:52 (GMT) |
commit | 318a70d976fbf225de3485bbbc61b7fe1f24f4a2 (patch) | |
tree | 358d6940133c029256a0b4d94bde7ed61fba03e4 /Tools/idle/AutoIndent.py | |
parent | 2d6a568a0f6c65c76797556bf949b97ede9049e1 (diff) | |
download | cpython-318a70d976fbf225de3485bbbc61b7fe1f24f4a2.zip cpython-318a70d976fbf225de3485bbbc61b7fe1f24f4a2.tar.gz cpython-318a70d976fbf225de3485bbbc61b7fe1f24f4a2.tar.bz2 |
Tim Peters writes:
I'm still unsure, but couldn't stand the virtual event trickery so tried a
different sin (adding undo_block_start/stop methods to the Text instance in
EditorWindow.py). Like it or not, it's efficient and works <wink>. Better
idea?
Give the attached a whirl. Even if you hate the implementation, I think
you'll like the results. Think I caught all the "block edit" cmds,
including Format Paragraph, plus subtler ones involving smart indents and
backspacing.
Diffstat (limited to 'Tools/idle/AutoIndent.py')
-rw-r--r-- | Tools/idle/AutoIndent.py | 82 |
1 files changed, 46 insertions, 36 deletions
diff --git a/Tools/idle/AutoIndent.py b/Tools/idle/AutoIndent.py index 4de7ad0..386490d 100644 --- a/Tools/idle/AutoIndent.py +++ b/Tools/idle/AutoIndent.py @@ -142,20 +142,24 @@ class AutoIndent: last = text.index("sel.last") except TclError: first = last = None - if first and last: - if index2line(first) != index2line(last): - return self.indent_region_event(event) - text.delete(first, last) - text.mark_set("insert", first) - if self.prefertabs: - pad = '\t' - else: - n = len(self.spaceindent) - prefix = text.get("insert linestart", "insert") - pad = ' ' * (n - len(prefix) % n) - text.insert("insert", pad) - text.see("insert") - return "break" + text.undo_block_start() + try: + if first and last: + if index2line(first) != index2line(last): + return self.indent_region_event(event) + text.delete(first, last) + text.mark_set("insert", first) + if self.prefertabs: + pad = '\t' + else: + n = len(self.spaceindent) + prefix = text.get("insert linestart", "insert") + pad = ' ' * (n - len(prefix) % n) + text.insert("insert", pad) + text.see("insert") + return "break" + finally: + text.undo_block_stop() def newline_and_indent_event(self, event): text = self.text @@ -164,28 +168,32 @@ class AutoIndent: last = text.index("sel.last") except TclError: first = last = None - if first and last: - text.delete(first, last) - text.mark_set("insert", first) - line = text.get("insert linestart", "insert") - i, n = 0, len(line) - while i < n and line[i] in " \t": - i = i+1 - indent = line[:i] - # strip trailing whitespace - i = 0 - while line and line[-1] in " \t": - line = line[:-1] - i = i + 1 - if i: - text.delete("insert - %d chars" % i, "insert") - text.insert("insert", "\n" + indent) - if _is_block_opener(line): - self.smart_indent_event(event) - elif indent and _is_block_closer(line) and line[-1:] != "\\": - self.smart_backspace_event(event) - text.see("insert") - return "break" + text.undo_block_start() + try: + if first and last: + text.delete(first, last) + text.mark_set("insert", first) + line = text.get("insert linestart", "insert") + i, n = 0, len(line) + while i < n and line[i] in " \t": + i = i+1 + indent = line[:i] + # strip trailing whitespace + i = 0 + while line and line[-1] in " \t": + line = line[:-1] + i = i + 1 + if i: + text.delete("insert - %d chars" % i, "insert") + text.insert("insert", "\n" + indent) + if _is_block_opener(line): + self.smart_indent_event(event) + elif indent and _is_block_closer(line) and line[-1:] != "\\": + self.smart_backspace_event(event) + text.see("insert") + return "break" + finally: + text.undo_block_stop() auto_indent = newline_and_indent_event @@ -275,8 +283,10 @@ class AutoIndent: return text.tag_remove("sel", "1.0", "end") text.mark_set("insert", head) + text.undo_block_start() text.delete(head, tail) text.insert(head, newchars) + text.undo_block_stop() text.tag_add("sel", head, "insert") def tabify(line, tabsize=8): |