diff options
author | hut <hut@lavabit.com> | 2010-02-18 22:41:15 +0100 |
---|---|---|
committer | hut <hut@lavabit.com> | 2010-03-09 14:40:23 +0100 |
commit | ee4687c32e105a69d49c023e8f3a695b4b407ce2 (patch) | |
tree | 0b9cc7a28abd4244454be26d0678c3b739cb3d5c | |
parent | 6ae8cb053b78effdeda1d9ba5966086e02befc9a (diff) | |
download | ranger-ee4687c32e105a69d49c023e8f3a695b4b407ce2.tar.gz |
keyparser: renamed KeyMap.add to KeyMap.map
-rw-r--r-- | ranger/api/keys.py | 28 | ||||
-rw-r--r-- | ranger/container/keymap.py | 4 | ||||
-rw-r--r-- | ranger/defaults/keys.py | 44 | ||||
-rw-r--r-- | ranger/gui/ui.py | 10 | ||||
-rw-r--r-- | test/tc_newkeys.py | 90 |
5 files changed, 74 insertions, 102 deletions
diff --git a/ranger/api/keys.py b/ranger/api/keys.py index a08c57b3..86911569 100644 --- a/ranger/api/keys.py +++ b/ranger/api/keys.py @@ -23,34 +23,6 @@ from ranger.gui.widgets import console_mode as cmode from ranger.container.bookmarks import ALLOWED_KEYS as ALLOWED_BOOKMARK_KEYS from ranger.container.keymap import KeyMap, Direction -def make_abbreviations(command_list): - def bind(*args, **keywords): - if keywords: - command_list.show(*args, **keywords) - else: - lastarg = args[-1] - if hasattr(lastarg, '__call__'): - # do the binding - command_list.bind(lastarg, *args[:-1]) - else: - # act as a decorator. eg: - # @bind('a') - # def do_stuff(arg): - # arg.fm.ui.do_stuff() - # - # is equivalent to: - # bind('a', lambda arg: arg.fm.ui.do_stuff()) - return lambda fnc: command_list.bind(fnc, *args) - - def show(*args, **keywords): - command_list.show(*args, **keywords) - - def alias(*args): - command_list.alias(*args) - - return bind, alias - - class Wrapper(object): def __init__(self, firstattr): self.__firstattr__ = firstattr diff --git a/ranger/container/keymap.py b/ranger/container/keymap.py index abc73d4d..dae8955a 100644 --- a/ranger/container/keymap.py +++ b/ranger/container/keymap.py @@ -75,7 +75,7 @@ class CommandArgs(object): class KeyMap(Tree): """Contains a tree with all the keybindings""" - def add(self, *args, **keywords): + def map(self, *args, **keywords): if keywords: return self.add_binding(*args, **keywords) firstarg = args[-1] @@ -84,7 +84,7 @@ class KeyMap(Tree): return self.add_binding(*args[:-1], **keywords) def decorator_function(func): keywords = {FUNC:func} - self.add(*args, **keywords) + self.map(*args, **keywords) return func return decorator_function diff --git a/ranger/defaults/keys.py b/ranger/defaults/keys.py index e6d2b0cc..aaa332b9 100644 --- a/ranger/defaults/keys.py +++ b/ranger/defaults/keys.py @@ -336,7 +336,7 @@ def _basic_movement(command_list): def get_directions(): k = KeyMap() - map = k.add + map = k.map map('<down>', dir=Direction(down=1)) map('<up>', dir=Direction(down=-1)) @@ -355,37 +355,37 @@ def move(arg): def get_ui_keys(): k = KeyMap() k.merge(system_keys()) - map = k.add + map = k.map - map('<dir>', func=move) - map('<C-c>', 'Q', func=fm.exit()) + map('<dir>', move) + map('<C-c>', 'Q', fm.exit()) # --------------------------------------------------------- history - map('H', func=fm.history_go(-1)) - map('L', func=fm.history_go(1)) + map('H', fm.history_go(-1)) + map('L', fm.history_go(1)) # ----------------------------------------------- tagging / marking - map('t', func=fm.tag_toggle()) - map('T', func=fm.tag_remove()) + map('t', fm.tag_toggle()) + map('T', fm.tag_remove()) - map(' ', func=fm.mark(toggle=True)) - map('v', func=fm.mark(all=True, toggle=True)) - map('V', func=fm.mark(all=True, val=False)) + map(' ', fm.mark(toggle=True)) + map('v', fm.mark(all=True, toggle=True)) + map('V', fm.mark(all=True, val=False)) # ------------------------------------------ file system operations - map('yy', func=fm.copy()) - map('dd', func=fm.cut()) - map('pp', func=fm.paste()) - map('po', func=fm.paste(overwrite=True)) - map('pl', func=fm.paste_symlink()) - map('p', hint='press //p// once again to confirm pasting' \ - ', func=or //l// to create symlinks') + map('yy', fm.copy()) + map('dd', fm.cut()) + map('pp', fm.paste()) + map('po', fm.paste(overwrite=True)) + map('pl', fm.paste_symlink()) + map('p<psv>', fm.notify('press //p// once again to confirm pasting' \ + ', or //l// to create symlinks')) # ---------------------------------------------------- run programs - map('s', func=fm.execute_command(os.environ['SHELL'])) - map('E', func=fm.edit_file()) - map('term', func=fm.execute_command('x-terminal-emulator', flags='d')) - map('du', func=fm.execute_command('du --max-depth=1 -h | less')) + map('s', fm.execute_command(os.environ['SHELL'])) + map('E', fm.edit_file()) + map('term', fm.execute_command('x-terminal-emulator', flags='d')) + map('du', fm.execute_command('du --max-depth=1 -h | less')) return k diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py index 79552bf2..eb7c26fa 100644 --- a/ranger/gui/ui.py +++ b/ranger/gui/ui.py @@ -134,13 +134,12 @@ class UI(DisplayableContainer): return kbuf = self.env.keybuffer + cmd = kbuf.command - if kbuf.done: - cmd = kbuf.command - elif kbuf.failure: + if kbuf.failure: kbuf.clear() return - else: + elif not cmd: return self.env.cmd = cmd @@ -153,7 +152,8 @@ class UI(DisplayableContainer): cmd.function(CommandArgs.from_widget(self)) except Exception as error: self.fm.notify(error) - kbuf.clear() + if kbuf.done: + kbuf.clear() def get_next_key(self): """Waits for key input and returns the pressed key""" diff --git a/test/tc_newkeys.py b/test/tc_newkeys.py index 0bc99de1..45ac4e33 100644 --- a/test/tc_newkeys.py +++ b/test/tc_newkeys.py @@ -50,10 +50,10 @@ class Test(PressTestCase): return arg.n return fnc - km.add('ppp', n(5)) - km.add('pp<psv>', n(8)) - km.add('pp<dir>', n(2)) - directions.add('j', dir=Direction(down=1)) + km.map('ppp', n(5)) + km.map('pp<psv>', n(8)) + km.map('pp<dir>', n(2)) + directions.map('j', dir=Direction(down=1)) press = self._mkpress(kb, km) self.assertEqual(5, press('ppp')) @@ -113,22 +113,22 @@ class Test(PressTestCase): return 5 directions = KeyMap() - directions.add('j', dir=Direction(down=1)) - directions.add('k', dir=Direction(down=-1)) - directions.add('<CR>', alias='j') + directions.map('j', dir=Direction(down=1)) + directions.map('k', dir=Direction(down=-1)) + directions.map('<CR>', alias='j') base = KeyMap() - base.add('a<dir>', add_dirs) - base.add('b<dir>', add_dirs) - base.add('x<dir>x<dir>', add_dirs) - base.add('f', return5) - base.add('yy', alias='y') - base.add('!', alias='!') + base.map('a<dir>', add_dirs) + base.map('b<dir>', add_dirs) + base.map('x<dir>x<dir>', add_dirs) + base.map('f', return5) + base.map('yy', alias='y') + base.map('!', alias='!') other = KeyMap() - other.add('b<dir>b<dir>', alias='x<dir>x<dir>') - other.add('c<dir>', add_dirs) - other.add('g', alias='f') + other.map('b<dir>b<dir>', alias='x<dir>x<dir>') + other.map('c<dir>', add_dirs) + other.map('g', alias='f') km = base.merge(other) kb = KeyBuffer(km, directions) @@ -248,9 +248,9 @@ class Test(PressTestCase): def test_add(self): c = KeyMap() - c.add('aa', 'b', lambda *_: 'lolz') + c.map('aa', 'b', lambda *_: 'lolz') self.assert_(c['aa'].function(), 'lolz') - @c.add('a', 'c') + @c.map('a', 'c') def test(): return 5 self.assert_(c['b'].function(), 'lolz') @@ -268,7 +268,7 @@ class Test(PressTestCase): return value return arg.n return fnc - km.add('p', n(5)) + km.map('p', n(5)) press = self._mkpress(kb, km) self.assertEqual(5, press('p')) self.assertEqual(3, press('3p')) @@ -278,16 +278,16 @@ class Test(PressTestCase): km = KeyMap() directions = KeyMap() kb = KeyBuffer(km, directions) - directions.add('j', dir=Direction(down=1)) - directions.add('k', dir=Direction(down=-1)) + directions.map('j', dir=Direction(down=1)) + directions.map('k', dir=Direction(down=-1)) def nd(arg): """ n * direction """ n = arg.n is None and 1 or arg.n dir = arg.direction is None and Direction(down=1) \ or arg.direction return n * dir.down - km.add('d<dir>', nd) - km.add('dd', func=nd) + km.map('d<dir>', nd) + km.map('dd', func=nd) press = self._mkpress(kb, km) @@ -302,8 +302,8 @@ class Test(PressTestCase): self.assertEqual( 33, press('33dd')) self.assertEqual( 1, press('dd')) - km.add('x<dir>', nd) - km.add('xxxx', func=nd) + km.map('x<dir>', nd) + km.map('xxxx', func=nd) self.assertEqual(1, press('xxxxj')) self.assertEqual(1, press('xxxxjsomeinvalitchars')) @@ -319,18 +319,18 @@ class Test(PressTestCase): km = KeyMap() directions = KeyMap() kb = KeyBuffer(km, directions) - directions.add('j', dir=Direction(down=1)) - directions.add('k', dir=Direction(down=-1)) + directions.map('j', dir=Direction(down=1)) + directions.map('k', dir=Direction(down=-1)) - directions.add('g<any>', dir=Direction(down=-1)) + directions.map('g<any>', dir=Direction(down=-1)) def cat(arg): n = arg.n is None and 1 or arg.n return ''.join(chr(c) for c in arg.matches) * n - km.add('return<any>', cat) - km.add('cat4<any><any><any><any>', cat) - km.add('foo<dir><any>', cat) + km.map('return<any>', cat) + km.map('cat4<any><any><any><any>', cat) + km.map('foo<dir><any>', cat) press = self._mkpress(kb, km) @@ -342,7 +342,7 @@ class Test(PressTestCase): self.assertEqual('x', press('foojx')) self.assertPressFails(kb, 'fooggx') # ANYKEY forbidden in DIRECTION - km.add('<any>', lambda _: Ellipsis) + km.map('<any>', lambda _: Ellipsis) self.assertEqual('x', press('returnx')) self.assertEqual('abcd', press('cat4abcd')) self.assertEqual(Ellipsis, press('2cat4abcd')) @@ -356,8 +356,8 @@ class Test(PressTestCase): km = KeyMap() directions = KeyMap() kb = KeyBuffer(km, directions) - directions.add('j', dir=Direction(down=1)) - directions.add('k', dir=Direction(down=-1)) + directions.map('j', dir=Direction(down=1)) + directions.map('k', dir=Direction(down=-1)) def add_dirs(arg): n = 0 @@ -365,8 +365,8 @@ class Test(PressTestCase): n += dir.down return n - km.add('x<dir>y<dir>', add_dirs) - km.add('four<dir><dir><dir><dir>', add_dirs) + km.map('x<dir>y<dir>', add_dirs) + km.map('four<dir><dir><dir><dir>', add_dirs) press = self._mkpress(kb, km) @@ -381,9 +381,9 @@ class Test(PressTestCase): directions = KeyMap() kb = KeyBuffer(km, directions) press = self._mkpress(kb, km) - directions.add('j', dir=Direction(down=1)) - directions.add('k', dir=Direction(down=-1)) - km.add('xxx', lambda _: 1) + directions.map('j', dir=Direction(down=1)) + directions.map('k', dir=Direction(down=-1)) + km.map('xxx', lambda _: 1) self.assertEqual(1, press('xxx')) @@ -409,10 +409,10 @@ class Test(PressTestCase): def move(arg): return arg.direction.down - directions.add('j', dir=Direction(down=1)) - directions.add('s', alias='j') - directions.add('k', dir=Direction(down=-1)) - km.add('<dir>', func=move) + directions.map('j', dir=Direction(down=1)) + directions.map('s', alias='j') + directions.map('k', dir=Direction(down=-1)) + km.map('<dir>', func=move) self.assertEqual(1, press('j')) self.assertEqual(1, press('j')) @@ -428,14 +428,14 @@ class Test(PressTestCase): self.assertEqual(-1, press('k')) self.assertEqual(-1, press('k')) - km.add('k', func=lambda _: 'love') + km.map('k', func=lambda _: 'love') self.assertEqual(1, press('j')) self.assertEqual('love', press('k')) self.assertEqual(40, press('40j')) - km.add('<dir><dir><any><any>', func=move) + km.map('<dir><dir><any><any>', func=move) self.assertEqual(40, press('40jkhl')) |