summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--test/tc_newkeys.py112
1 files changed, 61 insertions, 51 deletions
diff --git a/test/tc_newkeys.py b/test/tc_newkeys.py
index 39d6b23a..2db9b160 100644
--- a/test/tc_newkeys.py
+++ b/test/tc_newkeys.py
@@ -47,8 +47,9 @@ class CommandArgs(object):
 		self.fm = fm
 		self.wdg = widget
 		self.keybuffer = keybuffer
-		self.n = keybuffer.quant1
-		self.direction = keybuffer.direction
+		self.n = keybuffer.quant
+		self.direction = keybuffer.directions and keybuffer.directions[0] or None
+		self.directions = keybuffer.directions
 		self.keys = str(keybuffer)
 		self.moo = keybuffer.moo
 
@@ -65,32 +66,24 @@ class KeyBuffer(object):
 		assert isinstance(key, int)
 		assert key >= 0
 
-		# evaluate first quantifier
-		if self.level == 0:
-			if is_ascii_digit(key) and ANYKEY not in self.tree_pointer:
-				if self.quant1 is None:
-					self.quant1 = 0
-				self.quant1 = self.quant1 * 10 + key - 48
-			else:
-				self.level = 1
+		# evaluate quantifiers
+		if self.eval_quantifier and self._do_eval_quantifier(key):
+			return
 
-		# evaluate the command and the second quantifier.
-		# it's possible to jump between them. "x3xj" is equivalent to "xx3j"
-		if self.level == 1:
+		# evaluate the command
+		if self.eval_command:
 			try:
 				self.tree_pointer = self.tree_pointer[key]
 			except TypeError:
+				print(self.tree_pointer)
 				self.failure = True
 				return None
 			except KeyError:
-				if is_ascii_digit(key) and ANYKEY not in self.tree_pointer:
-					if self.quant2 is None:
-						self.quant2 = 0
-					self.quant2 = self.quant2 * 10 + key - 48
-				elif DIRKEY in self.tree_pointer:
-					self.level = 2
-					self.command = self.tree_pointer[DIRKEY]
-					self.tree_pointer = self.direction_keys._tree
+				if DIRKEY in self.tree_pointer:
+					self.eval_command = False
+					self.eval_quantifier = True
+					self.tree_pointer = self.tree_pointer[DIRKEY]
+					self.dir_tree_pointer = self.direction_keys._tree
 				elif ANYKEY in self.tree_pointer:
 					self.moo.append(key)
 					self.tree_pointer = self.tree_pointer[ANYKEY]
@@ -101,42 +94,58 @@ class KeyBuffer(object):
 			else:
 				self._try_to_finish()
 
+		if self.eval_quantifier and self._do_eval_quantifier(key):
+			return
+
 		# evaluate direction keys {j,k,gg,pagedown,...}
-		if self.level == 2:
+		if not self.eval_command:
 			try:
-				self.tree_pointer = self.tree_pointer[key]
+				self.dir_tree_pointer = self.dir_tree_pointer[key]
 			except KeyError:
 				self.failure = True
 			else:
-				if not isinstance(self.tree_pointer, dict):
-					match = self.tree_pointer
-					self.direction = match.actions['dir'] * self.quant2
-					self.done = True
+				if not isinstance(self.dir_tree_pointer, dict):
+					match = self.dir_tree_pointer
+					direction = match.actions['dir'] * self.direction_quant
+					self.directions.append(direction)
+					self.direction_quant = None
+					self._try_to_finish()
+
+	def _do_eval_quantifier(self, key):
+		if self.eval_command:
+			tree = self.tree_pointer
+		else:
+			tree = self.dir_tree_pointer
+		if is_ascii_digit(key) and ANYKEY not in tree:
+			attr = self.eval_command and 'quant' or 'direction_quant'
+			if getattr(self, attr) is None:
+				setattr(self, attr, 0)
+			setattr(self, attr, getattr(self, attr) * 10 + key - 48)
+		else:
+			self.eval_quantifier = False
+			return False
+		return True
 
 	def _try_to_finish(self):
 		if not isinstance(self.tree_pointer, dict):
-			match = self.tree_pointer
-			self.command = match
-			if not match.has_direction:
-				if self.quant2 is not None:
-					self.direction = self.direction * self.quant2
-				self.done = True
+			self.command = self.tree_pointer
+			self.done = True
 
 	def clear(self):
 		self.failure = False
 		self.done = False
-		self.quant1 = None
+		self.quant = None
 		self.moo = []
 		self.quant2 = None
 		self.command = None
-		self.direction = Direction(down=1)
+		self.direction_quant = None
+		self.directions = []
 		self.all_keys = []
 		self.tree_pointer = self.keymap._tree
-		self.direction_tree_pointer = self.direction_keys._tree
-		self.level = 0
-		# level 0 = parsing quantifier 1
-		#       1 = parsing command or quantifier 2
-		#       2 = parsing direction
+		self.dir_tree_pointer = self.direction_keys._tree
+
+		self.eval_quantifier = True
+		self.eval_command = True
 
 	def __str__(self):
 		"""returns a concatenation of all characters"""
@@ -254,10 +263,10 @@ class binding(object):
 
 def n(value):
 	""" return n or value """
-	def fnc(n=None):
-		if n is None:
+	def fnc(arg=None):
+		if arg is None or arg.n is None:
 			return value
-		return n
+		return arg.n
 	return fnc
 
 def nd(arg):
@@ -283,6 +292,7 @@ class Test(TestCase):
 			self.assertTrue(keybuffer.done,
 					"parsing keys '"+keys+"' did not complete!")
 			arg = CommandArgs(None, None, keybuffer)
+			self.assert_(match.function, match.__dict__)
 			return match.function(arg)
 		return press
 
@@ -301,13 +311,10 @@ class Test(TestCase):
 		km = Keymap()
 		directions = Keymap()
 		kb = KeyBuffer(km, directions)
-		km.add(n(5), 'd')
-		match = kb.simulate_press('3d')
-		self.assertEqual(3, match.function(kb.quant1))
-		kb.clear()
-		match = kb.simulate_press('6223d')
-		self.assertEqual(6223, match.function(kb.quant1))
-		kb.clear()
+		km.add(n(5), 'p')
+		press = self._mkpress(kb, km)
+		self.assertEqual(3, press('3p'))
+		self.assertEqual(6223, press('6223p'))
 
 	def test_direction(self):
 		km = Keymap()
@@ -320,11 +327,14 @@ class Test(TestCase):
 
 		press = self._mkpress(kb, km)
 
+		self.assertEqual(  1, press('dj'))
 		self.assertEqual(  3, press('3ddj'))
 		self.assertEqual( 15, press('3d5j'))
 		self.assertEqual(-15, press('3d5k'))
-		self.assertEqual( 15, press('3d5d'))
+		# supporting this kind of key combination would be too confusing:
+		# self.assertEqual( 15, press('3d5d'))
 		self.assertEqual(  3, press('3dd'))
+		self.assertEqual(  33, press('33dd'))
 		self.assertEqual(  1, press('dd'))
 
 		km.add(nd, 'x}')
/div>
cbcd3325 ^
d3bcb234 ^

cbcd3325 ^
81f5ac9e ^











cbcd3325 ^
90018274 ^




cbcd3325 ^
95e021ae ^

c928a9eb ^
95e021ae ^
cbcd3325 ^
95e021ae ^















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