summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-04-09 04:14:41 +0200
committerhut <hut@lavabit.com>2010-04-09 04:14:41 +0200
commitfeaf9e0a81cf2506b18ad9dbb9732e0be45c10fc (patch)
treeb88f3b66f31b1c73b696218dd2c04d7417a5930e
parent0fcbf6dcc044709ec240edfcb5a72147b7bbb4c4 (diff)
downloadranger-feaf9e0a81cf2506b18ad9dbb9732e0be45c10fc.tar.gz
tc_newkeys: fixed all but one test
-rw-r--r--ranger/api/keys.py2
-rw-r--r--ranger/container/keymap.py32
-rw-r--r--ranger/defaults/keys.py101
-rw-r--r--test/tc_newkeys.py30
4 files changed, 107 insertions, 58 deletions
diff --git a/ranger/api/keys.py b/ranger/api/keys.py
index d1011d22..97e07ae5 100644
--- a/ranger/api/keys.py
+++ b/ranger/api/keys.py
@@ -21,7 +21,7 @@ from inspect import getargspec, ismethod
 from ranger import RANGERDIR
 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
+from ranger.container.keymap import KeyMap, Direction, KeyMapWithDirections
 
 class Wrapper(object):
 	def __init__(self, firstattr):
diff --git a/ranger/container/keymap.py b/ranger/container/keymap.py
index 29d6e629..d39df381 100644
--- a/ranger/container/keymap.py
+++ b/ranger/container/keymap.py
@@ -89,6 +89,23 @@ class KeyMap(Tree):
 		return self.traverse(translate_keys(key))
 
 
+class KeyMapWithDirections(KeyMap):
+	def __init__(self, *args, **keywords):
+		Tree.__init__(self, *args, **keywords)
+		self.directions = KeyMap()
+
+	def merge(self, other):
+		assert hasattr(other, 'directions'), 'Merging with wrong type?'
+		Tree.merge(self, other)
+		Tree.merge(self.directions, other.directions)
+
+	def dir(self, *args, **keywords):
+		if ALIASARG in keywords:
+			self.directions.map(*args, **keywords)
+		else:
+			self.directions.map(*args, dir=Direction(**keywords))
+
+
 class KeyManager(object):
 	def __init__(self, keybuffer, contexts):
 		self._keybuffer = keybuffer
@@ -96,28 +113,29 @@ class KeyManager(object):
 		self.clear()
 
 	def clear(self):
-		self._contexts = {
-			'directions': KeyMap(),
-		}
+		self._contexts = dict()
 		for context in self._list_of_contexts:
-			self._contexts[context] = KeyMap()
+			self._contexts[context] = KeyMapWithDirections()
 
 	def map(self, context, *args, **keywords):
 		self.get_context(context).map(*args, **keywords)
 
+	def dir(self, context, *args, **keywords):
+		self.get_context(context).dir(*args, **keywords)
+
 	def get_context(self, context):
 		assert isinstance(context, str)
 		assert context in self._contexts, "no such context!"
 		return self._contexts[context]
 	__getitem__ = get_context
 
-	def use_context(self, context, directions='directions'):
+	def use_context(self, context):
 		context = self.get_context(context)
 		if self._keybuffer.keymap is not context:
-			directions = self.get_context(directions)
-			self._keybuffer.assign(context, directions)
+			self._keybuffer.assign(context, context.directions)
 			self._keybuffer.clear()
 
+
 class Binding(object):
 	"""The keybinding object"""
 	def __init__(self, keys, actions):
diff --git a/ranger/defaults/keys.py b/ranger/defaults/keys.py
index 57a0d415..061e9091 100644
--- a/ranger/defaults/keys.py
+++ b/ranger/defaults/keys.py
@@ -27,6 +27,25 @@ arg.wdg: the widget or ui instance
 arg.n: the number typed before the key combination (if allowed)
 arg.keys: the string representation of the used key combination
 arg.keybuffer: the keybuffer instance
+
+Direction keys are special.  They must be mapped with: map.dir(*keys, **args)
+where args is a dict of values such as up, left, down, right, absolute,
+relative, pages, etc...
+Example: map.dir('gg', to=0)
+Direction keys can be accessed in a mapping that contians "<dir>".
+
+Example scenario
+----------------
+If this keys are defined:
+map("dd", "d<dir>", fm.cut(foo=bar))
+map.dir("gg", to=0)
+
+Type in the keys on the left and the function on the right will be executed:
+dd        => fm.cut(foo=bar)
+5dd       => fm.cut(foo=bar, narg=5)
+dgg       => fm.cut(foo=bar, dirarg=Direction(to=0))
+5dgg      => fm.cut(foo=bar, narg=5, dirarg=Direction(to=0))
+5d3gg     => fm.cut(foo=bar, narg=5, dirarg=Direction(to=3))
 """
 
 from ranger.api.keys import *
@@ -35,36 +54,48 @@ from ranger import log
 # ===================================================================
 # == Define keys for everywhere:
 # ===================================================================
-map = global_keys = KeyMap()
+map = global_keys = KeyMapWithDirections()
 map('Q', fm.exit())
 map('<C-L>', fm.redraw_window())
 map('<backspace2>', alias='<backspace>')  # Backspace is bugged sometimes
 
+#map('<dir>', wdg.move())
 @map('<dir>') # move around with direction keys
 def move(arg):
 	arg.wdg.move(narg=arg.n, **arg.direction)
 
+# -------------------------------------------------- direction keys
+map.dir('<down>', down=1)
+map.dir('<up>', down=-1)
+map.dir('<left>', right=-1)
+map.dir('<right>', right=1)
+map.dir('<home>', down=0, absolute=True)
+map.dir('<end>', down=-1, absolute=True)
+map.dir('<pagedown>', down=1, pages=True)
+map.dir('<pageup>', down=-1, pages=True)
+map.dir('%', down=1, percentage=True, absolute=True)
+
 
 # ===================================================================
 # == Define aliases
 # ===================================================================
-map = vim_aliases = KeyMap()
-map('j', alias='<down>')
-map('k', alias='<up>')
-map('h', alias='<left>')
-map('l', alias='<right>')
-map('gg', alias='<home>')
-map('G', alias='<end>')
-map('<C-F>', alias='<pagedown>')
-map('<C-B>', alias='<pageup>')
-
-map = readline_aliases = KeyMap()
-map('<C-B>', alias='<left>')
-map('<C-F>', alias='<right>')
-map('<C-A>', alias='<home>')
-map('<C-E>', alias='<end>')
-map('<C-D>', alias='<delete>')
-map('<C-H>', alias='<backspace>')
+map = vim_aliases = KeyMapWithDirections()
+map.dir('j', alias='<down>')
+map.dir('k', alias='<up>')
+map.dir('h', alias='<left>')
+map.dir('l', alias='<right>')
+map.dir('gg', alias='<home>')
+map.dir('G', alias='<end>')
+map.dir('<C-F>', alias='<pagedown>')
+map.dir('<C-B>', alias='<pageup>')
+
+map = readline_aliases = KeyMapWithDirections()
+map.dir('<C-B>', alias='<left>')
+map.dir('<C-F>', alias='<right>')
+map.dir('<C-A>', alias='<home>')
+map.dir('<C-E>', alias='<end>')
+map.dir('<C-D>', alias='<delete>')
+map.dir('<C-H>', alias='<backspace>')
 
 
 # ===================================================================
@@ -74,6 +105,8 @@ map = keymanager['general']
 map.merge(global_keys)
 map.merge(vim_aliases)
 
+map('gg', fm.move(to=0))
+
 # --------------------------------------------------------- history
 map('H', fm.history_go(-1))
 map('L', fm.history_go(1))
@@ -87,8 +120,8 @@ map('v', fm.mark(all=True, toggle=True))
 map('V', fm.mark(all=True, val=False))
 
 # ------------------------------------------ file system operations
-map('yy', fm.copy())
-map('dd', fm.cut())
+map('yy', 'y<dir>', fm.copy())
+map('dd', 'd<dir>', fm.cut())
 map('pp', fm.paste())
 map('po', fm.paste(overwrite=True))
 map('pl', fm.paste_symlink())
@@ -215,7 +248,7 @@ map('r', fm.open_console(cmode.OPEN_QUICK))
 # ===================================================================
 # == Define keys for the pager
 # ===================================================================
-map = pager_keys = KeyMap()
+map = pager_keys = KeyMapWithDirections()
 map.merge(global_keys)
 map.merge(vim_aliases)
 
@@ -288,26 +321,6 @@ map('<C-Y>', wdg.paste())
 def type_key(arg):
 	arg.wdg.type_key(arg.match)
 
-# Override some global keys so we can type them:
-override = ('Q', '%')
-for key in override:
-	map(key, wdg.type_key(key))
-
-
-# ===================================================================
-# == Define direction keys
-# ===================================================================
-# Note that direction keys point to no functions, but Direction objects.
-# Direction keys are completely independent and can not be merged into
-# other keymaps.  You can't define or unmap direction keys on
-# a per-context-basis, instead use aliases.
-map = keymanager.get_context('directions')
-map('<down>', dir=Direction(down=1))
-map('<up>', dir=Direction(down=-1))
-map('<left>', dir=Direction(right=-1))
-map('<right>', dir=Direction(right=1))
-map('<home>', dir=Direction(down=0, absolute=True))
-map('<end>', dir=Direction(down=-1, absolute=True))
-map('<pagedown>', dir=Direction(down=1, pages=True))
-map('<pageup>', dir=Direction(down=-1, pages=True))
-map('%', dir=Direction(down=1, percentage=True, absolute=True))
+# Unmap some global keys so we can type them:
+map.unmap('Q')
+map.directions.unmap('%')
diff --git a/test/tc_newkeys.py b/test/tc_newkeys.py
index 9a7f10c7..c06c2894 100644
--- a/test/tc_newkeys.py
+++ b/test/tc_newkeys.py
@@ -484,7 +484,8 @@ class Test(PressTestCase):
 		map('c', func)
 		map('<dir>', getdown)
 
-		keymanager.map('directions', 'j', dir=Direction(down=1))
+		keymanager.dir('foo', 'j', down=1)
+		keymanager.dir('bar', 'j', down=1)
 
 		keymanager.use_context('foo')
 		self.assertEqual(5, press('a'))
@@ -509,19 +510,24 @@ class Test(PressTestCase):
 		def func(arg):
 			return arg.direction.down()
 
-		km = KeyMap()
-		directions = KeyMap()
-		kb = KeyBuffer(km, directions)
+		km = KeyMapWithDirections()
+		kb = KeyBuffer(km, km.directions)
 		press = self._mkpress(kb)
 
 		km.map('<dir>', func)
 		km.map('d<dir>', func)
-		directions.map('j', dir=Direction(down=42))
+		km.dir('j', down=42)
+		km.dir('k', alias='j')
 		self.assertEqual(42, press('j'))
 
-		km.map('o', alias='j')
+		km.dir('o', alias='j')
+		km.dir('ick', alias='j')
 		self.assertEqual(42, press('o'))
+		self.assertEqual(42, press('dj'))
+		self.assertEqual(42, press('dk'))
 		self.assertEqual(42, press('do'))
+		self.assertEqual(42, press('dick'))
+		self.assertPressFails(kb, 'dioo')
 
 	def test_both_directory_and_any_key(self):
 		def func(arg):
@@ -572,5 +578,17 @@ class Test(PressTestCase):
 #		self.assertPressFails(kb, 'agh')
 		self.assertEqual(1, press('agg'))
 
+	def test_keymap_with_dir(self):
+		def func(arg):
+			return arg.direction.down()
+
+		km = KeyMapWithDirections()
+		kb = KeyBuffer(km, km.directions)
+
+		press = self._mkpress(kb)
+
+		km.map('abc<dir>', func)
+		km.dir('j', down=42)
+		self.assertEqual(42, press('abcj'))
 
 if __name__ == '__main__': main()
Thomas E. Dickey <dickey@invisible-island.net> 1997-04-16 01:40:22 -0400 snapshot of project "lynx", label v2-7-1ac_0-6' href='/ingrix/lynx-snapshots/commit/makefile.in?id=e4409c408eedf320b8845cafdd62b664bec1afd8'>e4409c40 ^
e4409c40 ^


d2e46bbf ^
e4409c40 ^



d2e46bbf ^
e4409c40 ^



d2e46bbf ^
e4409c40 ^






55ebd12c ^



d3f9d547 ^



55ebd12c ^
d3f9d547 ^
55ebd12c ^

d3f9d547 ^
55ebd12c ^
02f28514 ^
55ebd12c ^
d3f9d547 ^


55ebd12c ^







d3f9d547 ^

55ebd12c ^

d3f9d547 ^

55ebd12c ^

d3f9d547 ^
55ebd12c ^
e4409c40 ^
e4409c40 ^
55ebd12c ^
c4e90d70 ^
d3f9d547 ^
c5fef0d4 ^


c4e90d70 ^
e38e34bf ^
b6c832d0 ^
c4e90d70 ^

d2e46bbf ^
b6c832d0 ^
d3f9d547 ^
e4409c40 ^



10f6c5df ^












050803d4 ^




1a2ac66b ^
050803d4 ^




e4409c40 ^



1a2ac66b ^
c5fef0d4 ^




e4409c40 ^
d3f9d547 ^
1a2ac66b ^


e4409c40 ^
e4409c40 ^


b63d287c ^

c5fef0d4 ^



57bfc74f ^
b63d287c ^
c5fef0d4 ^



e4409c40 ^
86b4d41a ^
e4409c40 ^


57bfc74f ^
e4409c40 ^

86b4d41a ^
e4409c40 ^








86b4d41a ^

e4409c40 ^









02f28514 ^
ab8b1f12 ^

d3f9d547 ^
ab8b1f12 ^
b6c832d0 ^
e87f21b4 ^


b6c832d0 ^
c5fef0d4 ^
86b4d41a ^
d3f9d547 ^
b6c832d0 ^
d3f9d547 ^

6b6388d0 ^
d3f9d547 ^

18024037 ^
d3f9d547 ^
d3f9d547 ^








152e034b ^


d3f9d547 ^









08fc6e5c ^
d3f9d547 ^










10f6c5df ^
08fc6e5c ^
88a84876 ^



d3f9d547 ^

b6c832d0 ^

87434eaa ^
d3f9d547 ^

88a84876 ^



d3f9d547 ^













08fc6e5c ^
d3f9d547 ^








a2e94617 ^
d3f9d547 ^
87434eaa ^
d3f9d547 ^
88a84876 ^


8f8c57cc ^
ab8b1f12 ^
55ebd12c ^


8f8c57cc ^
c5fef0d4 ^
86b4d41a ^
8f8c57cc ^

55ebd12c ^
d3f9d547 ^

b63d287c ^
d3f9d547 ^
b63d287c ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396