From 9f22f9dbc12d02a999fc1a1c06ebbcf245486b1b Mon Sep 17 00:00:00 2001 From: moculus Date: Sat, 13 Oct 2007 02:58:17 +0000 Subject: [PATCH] hex mode rules! --HG-- branch : pmacs2 --- application.py | 2 ++ buffer2.py | 5 ++++- method.py | 11 ++++++++++- mode/hex.py | 9 +++++++++ window2.py | 15 +++++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/application.py b/application.py index 90a040b..dc86f8b 100755 --- a/application.py +++ b/application.py @@ -177,6 +177,8 @@ class Application(object): for c in string.letters + string.digits + string.punctuation: obj = method.InsertString(c) self.methods[obj.name] = obj + obj = method.OverwriteChar(c) + self.methods[obj.name] = obj # window/slot height/width height = self.y - 2 diff --git a/buffer2.py b/buffer2.py index 11d437e..2c575da 100644 --- a/buffer2.py +++ b/buffer2.py @@ -285,7 +285,10 @@ class Buffer(object): p2 = Point(0, p.y + 1) else: p2 = Point(p.x + 1, p.y) - self.delete(p, p2, act, force) + self.delete(p, p2, act=act, force=force) + def overwrite_char(self, p, c, act=ACT_NORM, force=False): + self.delete_char(p, act=act, force=force) + self.insert_string(p, c, act=act, force=force) # random def count_leading_whitespace(self, y): diff --git a/method.py b/method.py index 706a682..1a58f0d 100644 --- a/method.py +++ b/method.py @@ -352,12 +352,21 @@ class Exit(Method): class InsertString(Method): _is_method = False def __init__(self, s): - self.name = "insert-string-%s" % (s) + self.name = "insert-string-%s" % s self.args = [] self.help = "Insert %r into the current buffer." % s self.string = s def _execute(self, w, **vargs): w.insert_string_at_cursor(self.string) +class OverwriteChar(Method): + _is_method = False + def __init__(self, c): + self.name = 'overwrite-char-%s' % c + self.args = [] + self.help = "Overwrite %r into the current buffer." % c + self.char = c + def _execute(self, w, **vargs): + w.overwrite_char_at_cursor(self.char) # killing/copying/etc. class Kill(Method): diff --git a/mode/hex.py b/mode/hex.py index 454b512..9448c14 100644 --- a/mode/hex.py +++ b/mode/hex.py @@ -1,3 +1,4 @@ +import string import color, mode2 from lex3 import Grammar, PatternRule, RegionRule @@ -17,5 +18,13 @@ class Hex(mode2.Fundamental): } def __init__(self, w): mode2.Fundamental.__init__(self, w) + + # create all the insert actions for the basic text input + for c in string.letters + string.digits + string.punctuation: + if c in string.hexdigits: + self.add_binding('overwrite-char-%s' % c.lower(), c) + else: + self.del_binding(c) + def name(self): return "Hex" diff --git a/window2.py b/window2.py index 43f37b7..8987f17 100644 --- a/window2.py +++ b/window2.py @@ -534,6 +534,21 @@ class Window(object): self.application.push_kill(copied) return copied + # overwriting + def overwrite_char_at_cursor(self, c): + self.overwrite_char(self.logical_cursor(), c) + def overwrite_char(self, p, c): + line = self.buffer.lines[p.y] + if p.x >= len(line): + self.insert_string(p, c) + elif p.x == len(line) - 1: + self.buffer.overwrite_char(p, c) + if p.y < len(self.buffer.lines): + self.cursor = Point(0, p.y + 1) + else: + self.buffer.overwrite_char(p, c) + self.cursor = Point(p.x + 1, p.y) + # insertion def insert_string_at_cursor(self, s): self.insert_string(self.logical_cursor(), s)