summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2011-10-03 05:30:53 +0200
committerhut <hut@lavabit.com>2011-10-03 05:30:53 +0200
commitd07c8c124a084d3e4e2c2c34b86d199fc0387f7f (patch)
treeb8e16798ee104654579057b4423fcf482a89037b
parentc1f2a152d0ed514bb44ee61dd0c2c52da57cd7ee (diff)
downloadranger-d07c8c124a084d3e4e2c2c34b86d199fc0387f7f.tar.gz
defaults.commands: Added copymap command
-rw-r--r--ranger/defaults/commands.py15
-rw-r--r--ranger/ext/keybindings.py26
2 files changed, 36 insertions, 5 deletions
diff --git a/ranger/defaults/commands.py b/ranger/defaults/commands.py
index a909cee9..870fd127 100644
--- a/ranger/defaults/commands.py
+++ b/ranger/defaults/commands.py
@@ -810,6 +810,21 @@ class help_(Command):
 		self.fm.display_help()
 
 
+class copymap(Command):
+	"""
+	:copymap <keys> <newkeys1> [<newkeys2>...]
+	Copies a keybinding from <keys> to <newkeys>
+	"""
+	context = 'browser'
+
+	def execute(self):
+		if not self.arg(1) or not self.arg(2):
+			return self.notify("Not enough arguments", bad=True)
+
+		for arg in self.args[1:]:
+			self.fm.env.keymaps.copy(self.context, self.arg(1), arg)
+
+
 class map_(Command):
 	"""
 	:map <keysequence> <command>
diff --git a/ranger/ext/keybindings.py b/ranger/ext/keybindings.py
index 133da83b..fba025f8 100644
--- a/ranger/ext/keybindings.py
+++ b/ranger/ext/keybindings.py
@@ -16,6 +16,7 @@
 from ranger.ext.keybinding_parser import (parse_keybinding,
 	ANYKEY, PASSIVE_ACTION, QUANT_KEY)
 import sys
+import copy
 
 PY3 = sys.version > '3'
 
@@ -27,6 +28,12 @@ class KeyMaps(dict):
 		self.keybuffer = keybuffer
 		self.used_keymap = None
 
+	def use_keymap(self, keymap_name):
+		self.keybuffer.keymap = self.get(keymap_name, dict())
+		if self.used_keymap != keymap_name:
+			self.used_keymap = keymap_name
+			self.keybuffer.clear()
+
 	def bind(self, context, keys, leaf):
 		try:
 			pointer = self[context]
@@ -45,11 +52,20 @@ class KeyMaps(dict):
 				pointer[key] = pointer = dict()
 		pointer[last_key] = leaf
 
-	def use_keymap(self, keymap_name):
-		self.keybuffer.keymap = self.get(keymap_name, dict())
-		if self.used_keymap != keymap_name:
-			self.used_keymap = keymap_name
-			self.keybuffer.clear()
+	def copy(self, context, source, target):
+		try:
+			pointer = self[context]
+		except:
+			self[context] = pointer = dict()
+		if PY3:
+			source = source.encode('utf-8').decode('latin-1')
+		source = list(parse_keybinding(source))
+		if not source:
+			return
+
+		for key in source:
+			pointer = pointer[key]
+		self.bind(context, target, copy.deepcopy(pointer))
 
 
 class KeyBuffer(object):