diff --git a/application.py b/application.py index 0a7d8b7..ad9b591 100755 --- a/application.py +++ b/application.py @@ -180,12 +180,13 @@ class Application(object): method.GotoLine().execute(w, lineno=jump_to_line) # initialize our kill ring and last action - self.kill_ring = [] - self.kill_commands = ['kill', 'kill-region'] - self.last_action = None - self.last_search = None + self.kill_ring = [] + self.kill_commands = ['kill', 'kill-region'] + self.last_action = None + self.last_search = None self.last_replace_before = None - self.last_replace_after = None + self.last_replace_after = None + self.registers = {} # initialize tab handlers method.DATATYPES['path'] = completer.FileCompleter() @@ -364,7 +365,7 @@ class Application(object): self.kill_ring[-1] = self.kill_ring[-1] + s else: self.kill_ring.append(s) - if len(self.kill_ring) > KILL_RING_LIMIT: + if KILL_RING_LIMIT and len(self.kill_ring) > KILL_RING_LIMIT: self.kill_ring.pop(0) def pop_kill(self): return self.kill_ring.pop(-1) diff --git a/method.py b/method.py index 2a5db00..07720db 100644 --- a/method.py +++ b/method.py @@ -13,6 +13,9 @@ DATATYPES = { "shellcommand": None, } +class MethodError(Exception): + pass + class Argument: def __init__(self, name, type=type(""), datatype=None, prompt=None, help="", default=default.none, load_default=False): @@ -89,11 +92,15 @@ class Method: def _args(self): return [] - def pre_execute(self, w, **vargs): + def _pre_execute(self, w, **vargs): pass def execute(self, w, **vargs): - self.pre_execute(w, **vargs) + try: + self._pre_execute(w, **vargs) + except MethodError, e: + w.application.set_error(str(e)) + return for arg in self.args: if arg.name not in vargs: self.old_window = w @@ -219,9 +226,10 @@ class SwitchBuffer(Method): return [Argument('buffername', datatype="buffer", prompt="Switch To Buffer: ", default=default.last_buffer)] - def pre_execute(self, w, **vargs): + def _pre_execute(self, w, **vargs): a = w.application - assert len(a.bufferlist.buffers) > 1, "No other buffers" + if len(a.bufferlist.buffers) < 1: + raise Exception, "No other buffers" def _execute(self, w, **vargs): name = vargs['buffername'] buf = None @@ -1381,3 +1389,41 @@ class GetToken(Method): w.application.set_error('No Token') else: w.application.set_error('Token: %r' % token.string) + +class RegisterSave(Method): + MAX_TXT = 30 + MAX_REG = 20 + '''help here''' + def _args(self): + return [Argument('name', datatype="str", prompt="Register name: ")] + def _pre_execute(self, w, **vargs): + if not w.has_kill(): + raise MethodError, "No text on the kill stack" + def _execute(self, w, **vargs): + name = vargs['name'] + text = w.get_kill() + w.application.registers[name] = text + if len(name) > self.MAX_REG: + name = name[:self.MAX_REG] + '...' + if len(text) > self.MAX_TXT: + text = text[:self.MAX_TXT] + '...' + w.set_error('Saved %r into register %r' % (text, name)) + +class RegisterRestore(Method): + MAX_TXT = 30 + MAX_REG = 18 + '''help here''' + def _args(self): + return [Argument('name', datatype="str", prompt="Register name: ")] + def _execute(self, w, **vargs): + name = vargs['name'] + if name not in w.application.registers: + w.set_error('Register %r does not exist' % name) + return + text = app.registers[name] + w.push_kill(text) + if len(text) > self.MAX_TXT: + text = text[0:self.MAX_TXT] + '...' + if len(name) > self.MAX_REG: + name = name[0:self.MAX_REG] + '...' + w.set_error('Restored %r from register %r' % (text2, name2)) diff --git a/window2.py b/window2.py index 84ef247..4339a0d 100644 --- a/window2.py +++ b/window2.py @@ -69,6 +69,10 @@ class Window(object): else: return os.path.splitext(path)[1].lower() + # some useful pass-through to application + def set_error(self, s): + self.application.set_error(s) + # mode stuff def set_mode(self, m): self.mode = m @@ -514,8 +518,14 @@ class Window(object): # yank/pop def yank(self): self.insert_string_at_cursor(self.application.get_kill()) + def get_kill(self): + return self.application.get_kill() + def has_kill(self, i=-1): + return self.application.has_kill(i) def pop_kill(self): return self.application.pop_kill() + def push_kill(self, s): + return self.application.push_kill(s) # querying def cursor_char(self):