summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/api/keys.py28
-rw-r--r--ranger/container/commandlist.py17
-rw-r--r--ranger/defaults/keys.py324
3 files changed, 173 insertions, 196 deletions
diff --git a/ranger/api/keys.py b/ranger/api/keys.py
index 4ac2e18e..87186378 100644
--- a/ranger/api/keys.py
+++ b/ranger/api/keys.py
@@ -22,34 +22,6 @@ from ranger import RANGERDIR
 from ranger.gui.widgets import console_mode as cmode
 from ranger.container.bookmarks import ALLOWED_KEYS as ALLOWED_BOOKMARK_KEYS
 
-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/commandlist.py b/ranger/container/commandlist.py
index f50603f2..eb289c46 100644
--- a/ranger/container/commandlist.py
+++ b/ranger/container/commandlist.py
@@ -94,6 +94,23 @@ class CommandList(object):
 			if self.paths[k] == self.dummy_object: del self.paths[k]
 		self.dummies_in_paths = False
 
+	def __call__(self, *args, **keywords):
+		if keywords:
+			self.show(*args, **keywords)
+		else:
+			lastarg = args[-1]
+			if hasattr(lastarg, '__call__'):
+				# do the binding
+				self.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: self.bind(fnc, *args)
 
 	def _str_to_tuple(self, obj):
 		"""splits a string into a tuple of integers"""
diff --git a/ranger/defaults/keys.py b/ranger/defaults/keys.py
index ee6a7802..a573279c 100644
--- a/ranger/defaults/keys.py
+++ b/ranger/defaults/keys.py
@@ -36,8 +36,8 @@ Check ranger.keyapi for more information
 
 from ranger.api.keys import *
 
-def _vimlike_aliases(command_list):
-	bind, alias = make_abbreviations(command_list)
+def _vimlike_aliases(map):
+	alias = map.alias
 
 	# the key 'k' will always do the same as KEY_UP, etc.
 	alias(KEY_UP, 'k')
@@ -50,72 +50,70 @@ def _vimlike_aliases(command_list):
 	alias(KEY_HOME, 'gg')
 	alias(KEY_END, 'G')
 
-def initialize_commands(command_list):
+def initialize_commands(map):
 	"""Initialize the commands for the main user interface"""
 
-	bind, alias = make_abbreviations(command_list)
-
 	# -------------------------------------------------------- movement
-	_vimlike_aliases(command_list)
-	command_list.alias(KEY_LEFT, KEY_BACKSPACE, DEL)
+	_vimlike_aliases(map)
+	map.alias(KEY_LEFT, KEY_BACKSPACE, DEL)
 
-	bind(KEY_DOWN, fm.move_pointer(relative=1))
-	bind(KEY_UP, fm.move_pointer(relative=-1))
-	bind(KEY_RIGHT, KEY_ENTER, ctrl('j'), fm.move_right())
-	bind(KEY_LEFT, KEY_BACKSPACE, DEL, fm.move_left(1))
-	bind(KEY_HOME, fm.move_pointer(absolute=0))
-	bind(KEY_END, fm.move_pointer(absolute=-1))
+	map(KEY_DOWN, fm.move_pointer(relative=1))
+	map(KEY_UP, fm.move_pointer(relative=-1))
+	map(KEY_RIGHT, KEY_ENTER, ctrl('j'), fm.move_right())
+	map(KEY_LEFT, KEY_BACKSPACE, DEL, fm.move_left(1))
+	map(KEY_HOME, fm.move_pointer(absolute=0))
+	map(KEY_END, fm.move_pointer(absolute=-1))
 
-	bind(KEY_HOME, fm.move_pointer(absolute=0))
-	bind(KEY_END, fm.move_pointer(absolute=-1))
+	map(KEY_HOME, fm.move_pointer(absolute=0))
+	map(KEY_END, fm.move_pointer(absolute=-1))
 
-	bind('%', fm.move_pointer_by_percentage(absolute=50))
-	bind(KEY_NPAGE, fm.move_pointer_by_pages(1))
-	bind(KEY_PPAGE, fm.move_pointer_by_pages(-1))
-	bind(ctrl('d'), fm.move_pointer_by_pages(0.5))
-	bind(ctrl('u'), fm.move_pointer_by_pages(-0.5))
+	map('%', fm.move_pointer_by_percentage(absolute=50))
+	map(KEY_NPAGE, fm.move_pointer_by_pages(1))
+	map(KEY_PPAGE, fm.move_pointer_by_pages(-1))
+	map(ctrl('d'), fm.move_pointer_by_pages(0.5))
+	map(ctrl('u'), fm.move_pointer_by_pages(-0.5))
 
-	bind(']', fm.traverse())
-	bind('[', fm.history_go(-1))
+	map(']', fm.traverse())
+	map('[', fm.history_go(-1))
 
 	# --------------------------------------------------------- history
-	bind('H', fm.history_go(-1))
-	bind('L', fm.history_go(1))
+	map('H', fm.history_go(-1))
+	map('L', fm.history_go(1))
 
 	# ----------------------------------------------- tagging / marking
-	bind('t', fm.tag_toggle())
-	bind('T', fm.tag_remove())
+	map('t', fm.tag_toggle())
+	map('T', fm.tag_remove())
 
-	bind(' ', fm.mark(toggle=True))
-	bind('v', fm.mark(all=True, toggle=True))
-	bind('V', 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
-	bind('yy', fm.copy())
-	bind('dd', fm.cut())
-	bind('pp', fm.paste())
-	bind('po', fm.paste(overwrite=True))
-	bind('pl', fm.paste_symlink())
-	bind('p', hint='press //p// once again to confirm pasting' \
+	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', hint='press //p// once again to confirm pasting' \
 			', or //l// to create symlinks')
 
 	# ---------------------------------------------------- run programs
-	bind('s', fm.execute_command(os.environ['SHELL']))
-	bind('E', fm.edit_file())
-	bind(',term', fm.execute_command('x-terminal-emulator', flags='d'))
-	bind('du', 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'))
 
 	# -------------------------------------------------- toggle options
-	bind('b', hint="bind_//h//idden //p//review_files //d//irectories_first " \
-			"//c//ollapse_preview flush//i//nput")
-	bind('bh', fm.toggle_boolean_option('show_hidden'))
-	bind('bp', fm.toggle_boolean_option('preview_files'))
-	bind('bi', fm.toggle_boolean_option('flushinput'))
-	bind('bd', fm.toggle_boolean_option('directories_first'))
-	bind('bc', fm.toggle_boolean_option('collapse_preview'))
+	map('b', hint="bind_//h//idden //p//review_files //d//irectories_first " \
+		"//c//ollapse_preview flush//i//nput")
+	map('bh', fm.toggle_boolean_option('show_hidden'))
+	map('bp', fm.toggle_boolean_option('preview_files'))
+	map('bi', fm.toggle_boolean_option('flushinput'))
+	map('bd', fm.toggle_boolean_option('directories_first'))
+	map('bc', fm.toggle_boolean_option('collapse_preview'))
 
 	# ------------------------------------------------------------ sort
-	bind('o', 'O', hint="//s//ize //b//ase//n//ame //m//time //t//ype //r//everse")
+	map('o', 'O', hint="//s//ize //b//ase//n//ame //m//time //t//ype //r//everse")
 	sort_dict = {
 		's': 'size',
 		'b': 'basename',
@@ -127,190 +125,180 @@ def initialize_commands(command_list):
 	for key, val in sort_dict.items():
 		for key, is_capital in ((key, False), (key.upper(), True)):
 			# reverse if any of the two letters is capital
-			bind('o' + key, fm.sort(func=val, reverse=is_capital))
-			bind('O' + key, fm.sort(func=val, reverse=True))
+			map('o' + key, fm.sort(func=val, reverse=is_capital))
+			map('O' + key, fm.sort(func=val, reverse=True))
 
-	bind('or', 'Or', 'oR', 'OR', lambda arg: \
+	map('or', 'Or', 'oR', 'OR', lambda arg: \
 			arg.fm.sort(reverse=not arg.fm.settings.reverse))
 
 	# ----------------------------------------------- console shortcuts
-	@bind("A")
+	@map("A")
 	def append_to_filename(arg):
 		command = 'rename ' + arg.fm.env.cf.basename
 		arg.fm.open_console(cmode.COMMAND, command)
 
-	bind('cw', fm.open_console(cmode.COMMAND, 'rename '))
-	bind('cd', fm.open_console(cmode.COMMAND, 'cd '))
-	bind('f', fm.open_console(cmode.COMMAND_QUICK, 'find '))
-	bind('tf', fm.open_console(cmode.COMMAND, 'filter '))
-	bind('d', hint='d//u// (disk usage) d//d// (cut)')
+	map('cw', fm.open_console(cmode.COMMAND, 'rename '))
+	map('cd', fm.open_console(cmode.COMMAND, 'cd '))
+	map('f', fm.open_console(cmode.COMMAND_QUICK, 'find '))
+	map('tf', fm.open_console(cmode.COMMAND, 'filter '))
+	map('d', hint='d//u// (disk usage) d//d// (cut)')
 
 	# --------------------------------------------- jump to directories
-	bind('gh', fm.cd('~'))
-	bind('ge', fm.cd('/etc'))
-	bind('gu', fm.cd('/usr'))
-	bind('gd', fm.cd('/dev'))
-	bind('gl', fm.cd('/lib'))
-	bind('go', fm.cd('/opt'))
-	bind('gv', fm.cd('/var'))
-	bind('gr', 'g/', fm.cd('/'))
-	bind('gm', fm.cd('/media'))
-	bind('gn', fm.cd('/mnt'))
-	bind('gt', fm.cd('/tmp'))
-	bind('gs', fm.cd('/srv'))
-	bind('gR', fm.cd(RANGERDIR))
+	map('gh', fm.cd('~'))
+	map('ge', fm.cd('/etc'))
+	map('gu', fm.cd('/usr'))
+	map('gd', fm.cd('/dev'))
+	map('gl', fm.cd('/lib'))
+	map('go', fm.cd('/opt'))
+	map('gv', fm.cd('/var'))
+	map('gr', 'g/', fm.cd('/'))
+	map('gm', fm.cd('/media'))
+	map('gn', fm.cd('/mnt'))
+	map('gt', fm.cd('/tmp'))
+	map('gs', fm.cd('/srv'))
+	map('gR', fm.cd(RANGERDIR))
 
 	# ------------------------------------------------------- searching
-	bind('/', fm.open_console(cmode.SEARCH))
+	map('/', fm.open_console(cmode.SEARCH))
 
-	bind('n', fm.search())
-	bind('N', fm.search(forward=False))
+	map('n', fm.search())
+	map('N', fm.search(forward=False))
 
-	bind(TAB, fm.search(order='tag'))
-	bind('cc', fm.search(order='ctime'))
-	bind('cm', fm.search(order='mimetype'))
-	bind('cs', fm.search(order='size'))
-	bind('c', hint='//c//time //m//imetype //s//ize')
+	map(TAB, fm.search(order='tag'))
+	map('cc', fm.search(order='ctime'))
+	map('cm', fm.search(order='mimetype'))
+	map('cs', fm.search(order='size'))
+	map('c', hint='//c//time //m//imetype //s//ize')
 
 	# ------------------------------------------------------- bookmarks
 	for key in ALLOWED_BOOKMARK_KEYS:
-		bind("`" + key, "'" + key, fm.enter_bookmark(key))
-		bind("m" + key, fm.set_bookmark(key))
-		bind("um" + key, fm.unset_bookmark(key))
-	bind("`", "'", "m", draw_bookmarks=True)
+		map("`" + key, "'" + key, fm.enter_bookmark(key))
+		map("m" + key, fm.set_bookmark(key))
+		map("um" + key, fm.unset_bookmark(key))
+	map("`", "'", "m", draw_bookmarks=True)
 
 	# ---------------------------------------------------- change views
-	bind('i', fm.display_file())
-	bind(ctrl('p'), fm.display_log())
-	bind('?', KEY_F1, fm.display_help())
-	bind('w', lambda arg: arg.fm.ui.open_taskview())
+	map('i', fm.display_file())
+	map(ctrl('p'), fm.display_log())
+	map('?', KEY_F1, fm.display_help())
+	map('w', lambda arg: arg.fm.ui.open_taskview())
 
 	# ------------------------------------------------ system functions
-	_system_functions(command_list)
-	bind('ZZ', fm.exit())
-	bind(ctrl('R'), fm.reset())
-	bind('R', fm.reload_cwd())
-	bind(ctrl('C'), fm.exit())
+	_system_functions(map)
+	map('ZZ', fm.exit())
+	map(ctrl('R'), fm.reset())
+	map('R', fm.reload_cwd())
+	map(ctrl('C'), fm.exit())
 
-	bind(':', ';', fm.open_console(cmode.COMMAND))
-	bind('>', fm.open_console(cmode.COMMAND_QUICK))
-	bind('!', fm.open_console(cmode.OPEN))
-	bind('r', fm.open_console(cmode.OPEN_QUICK))
+	map(':', ';', fm.open_console(cmode.COMMAND))
+	map('>', fm.open_console(cmode.COMMAND_QUICK))
+	map('!', fm.open_console(cmode.OPEN))
+	map('r', fm.open_console(cmode.OPEN_QUICK))
 
-	command_list.rebuild_paths()
+	map.rebuild_paths()
 
 
-def initialize_console_commands(command_list):
+def initialize_console_commands(map):
 	"""Initialize the commands for the console widget only"""
-	bind, alias = make_abbreviations(command_list)
 
 	# -------------------------------------------------------- movement
-	bind(KEY_UP, wdg.history_move(-1))
-	bind(KEY_DOWN, wdg.history_move(1))
+	map(KEY_UP, wdg.history_move(-1))
+	map(KEY_DOWN, wdg.history_move(1))
 
-	bind(ctrl('b'), KEY_LEFT, wdg.move(relative = -1))
-	bind(ctrl('f'), KEY_RIGHT, wdg.move(relative = 1))
-	bind(ctrl('a'), KEY_HOME, wdg.move(absolute = 0))
-	bind(ctrl('e'), KEY_END, wdg.move(absolute = -1))
+	map(ctrl('b'), KEY_LEFT, wdg.move(relative = -1))
+	map(ctrl('f'), KEY_RIGHT, wdg.move(relative = 1))
+	map(ctrl('a'), KEY_HOME, wdg.move(absolute = 0))
+	map(ctrl('e'), KEY_END, wdg.move(absolute = -1))
 
 	# ----------------------------------------- deleting / pasting text
-	bind(ctrl('d'), KEY_DC, wdg.delete(0))
-	bind(ctrl('h'), KEY_BACKSPACE, DEL, wdg.delete(-1))
-	bind(ctrl('w'), wdg.delete_word())
-	bind(ctrl('k'), wdg.delete_rest(1))
-	bind(ctrl('u'), wdg.delete_rest(-1))
-	bind(ctrl('y'), wdg.paste())
+	map(ctrl('d'), KEY_DC, wdg.delete(0))
+	map(ctrl('h'), KEY_BACKSPACE, DEL, wdg.delete(-1))
+	map(ctrl('w'), wdg.delete_word())
+	map(ctrl('k'), wdg.delete_rest(1))
+	map(ctrl('u'), wdg.delete_rest(-1))
+	map(ctrl('y'), wdg.paste())
 
 	# ----------------------------------------------------- typing keys
 	def type_key(arg):
 		arg.wdg.type_key(arg.keys)
 
 	for i in range(ord(' '), ord('~')+1):
-		bind(i, type_key)
+		map(i, type_key)
 
 	# ------------------------------------------------ system functions
-	_system_functions(command_list)
+	_system_functions(map)
 
-	bind(KEY_F1, lambda arg: arg.fm.display_command_help(arg.wdg))
-	bind(ctrl('c'), ESC, wdg.close())
-	bind(ctrl('j'), KEY_ENTER, wdg.execute())
-	bind(TAB, wdg.tab())
-	bind(KEY_BTAB, wdg.tab(-1))
+	map(KEY_F1, lambda arg: arg.fm.display_command_help(arg.wdg))
+	map(ctrl('c'), ESC, wdg.close())
+	map(ctrl('j'), KEY_ENTER, wdg.execute())
+	map(TAB, wdg.tab())
+	map(KEY_BTAB, wdg.tab(-1))
 
-	command_list.rebuild_paths()
+	map.rebuild_paths()
 
 
-def initialize_taskview_commands(command_list):
+def initialize_taskview_commands(map):
 	"""Initialize the commands for the TaskView widget"""
-	bind, alias = make_abbreviations(command_list)
-	_basic_movement(command_list)
-	_vimlike_aliases(command_list)
-	_system_functions(command_list)
+	_basic_movement(map)
+	_vimlike_aliases(map)
+	_system_functions(map)
 
 	# -------------------------------------------------- (re)move tasks
-	bind('K', wdg.task_move(0))
-	bind('J', wdg.task_move(-1))
-	bind('dd', wdg.task_remove())
+	map('K', wdg.task_move(0))
+	map('J', wdg.task_move(-1))
+	map('dd', wdg.task_remove())
 
 	# ------------------------------------------------ system functions
-	bind('?', fm.display_help())
-	bind('w', 'q', ESC, ctrl('d'), ctrl('c'),
+	map('?', fm.display_help())
+	map('w', 'q', ESC, ctrl('d'), ctrl('c'),
 			lambda arg: arg.fm.ui.close_taskview())
 
-	command_list.rebuild_paths()
-
+	map.rebuild_paths()
 
-def initialize_pager_commands(command_list):
-	bind, alias = make_abbreviations(command_list)
-	_base_pager_commands(command_list)
-	bind('q', 'i', ESC, KEY_F1, lambda arg: arg.fm.ui.close_pager())
-	command_list.rebuild_paths()
 
+def initialize_pager_commands(map):
+	_base_pager_commands(map)
+	map('q', 'i', ESC, KEY_F1, lambda arg: arg.fm.ui.close_pager())
+	map.rebuild_paths()
 
-def initialize_embedded_pager_commands(command_list):
-	bind, alias = make_abbreviations(command_list)
-	_base_pager_commands(command_list)
-	bind('q', 'i', ESC, lambda arg: arg.fm.ui.close_embedded_pager())
-	command_list.rebuild_paths()
 
+def initialize_embedded_pager_commands(map):
+	_base_pager_commands(map)
+	map('q', 'i', ESC, lambda arg: arg.fm.ui.close_embedded_pager())
+	map.rebuild_paths()
 
-def _base_pager_commands(command_list):
-	bind, alias = make_abbreviations(command_list)
-	_basic_movement(command_list)
-	_vimlike_aliases(command_list)
-	_system_functions(command_list)
+def _base_pager_commands(map):
+	_basic_movement(map)
+	_vimlike_aliases(map)
+	_system_functions(map)
 
 	# -------------------------------------------------------- movement
-	bind(KEY_LEFT, wdg.move_horizontal(relative=-4))
-	bind(KEY_RIGHT, wdg.move_horizontal(relative=4))
-	bind(KEY_NPAGE, wdg.move(relative=1, pages=True))
-	bind(KEY_PPAGE, wdg.move(relative=-1, pages=True))
-	bind(ctrl('d'), wdg.move(relative=0.5, pages=True))
-	bind(ctrl('u'), wdg.move(relative=-0.5, pages=True))
+	map(KEY_LEFT, wdg.move_horizontal(relative=-4))
+	map(KEY_RIGHT, wdg.move_horizontal(relative=4))
+	map(KEY_NPAGE, wdg.move(relative=1, pages=True))
+	map(KEY_PPAGE, wdg.move(relative=-1, pages=True))
+	map(ctrl('d'), wdg.move(relative=0.5, pages=True))
+	map(ctrl('u'), wdg.move(relative=-0.5, pages=True))
 
 	# ---------------------------------------------------------- others
-	bind('E', fm.edit_file())
-	bind('?', fm.display_help())
+	map('E', fm.edit_file())
+	map('?', fm.display_help())
 
 	# --------------------------------------------- less-like shortcuts
-	alias(KEY_NPAGE, 'd')
-	alias(KEY_PPAGE, 'u')
+	map.alias(KEY_NPAGE, 'd')
+	map.alias(KEY_PPAGE, 'u')
 
 
-def _system_functions(command_list):
+def _system_functions(map):
 	# Each commandlist should have this bindings
-	bind, alias = make_abbreviations(command_list)
-
-	bind(KEY_RESIZE, fm.resize())
-	bind(KEY_MOUSE, fm.handle_mouse())
-	bind('Q', fm.exit())
-	bind(ctrl('L'), fm.redraw_window())
-
+	map(KEY_RESIZE, fm.resize())
+	map(KEY_MOUSE, fm.handle_mouse())
+	map('Q', fm.exit())
+	map(ctrl('L'), fm.redraw_window())
 
-def _basic_movement(command_list):
-	bind, alias = make_abbreviations(command_list)
 
-	bind(KEY_DOWN, wdg.move(relative=1))
-	bind(KEY_UP, wdg.move(relative=-1))
-	bind(KEY_HOME, wdg.move(absolute=0))
-	bind(KEY_END, wdg.move(absolute=-1))
+def _basic_movement(map):
+	map(KEY_DOWN, wdg.move(relative=1))
+	map(KEY_UP, wdg.move(relative=-1))
+	map(KEY_HOME, wdg.move(absolute=0))
+	map(KEY_END, wdg.move(absolute=-1))