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
|
# Copyright (C) 2009, 2010 Roman Zimbelmann <romanz@lavabit.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ranger.ext.keybinding_parser import (parse_keybinding,
ANYKEY, PASSIVE_ACTION, QUANT_KEY)
import sys
import copy
PY3 = sys.version > '3'
digits = set(range(ord('0'), ord('9')+1))
def _unbind_traverse(pointer, keys, pos=0):
if keys[pos] not in pointer:
return
if len(keys) > pos+1 and isinstance(pointer, dict):
_unbind_traverse(pointer[keys[pos]], keys, pos=pos+1)
if not pointer[keys[pos]]:
del pointer[keys[pos]]
elif len(keys) == pos+1:
try:
del pointer[keys[pos]]
keys.pop()
except:
pass
class KeyMaps(dict):
def __init__(self, keybuffer=None):
dict.__init__(self)
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 _clean_input(self, context, keys):
try:
pointer = self[context]
except:
self[context] = pointer = dict()
if PY3:
keys = keys.encode('utf-8').decode('latin-1')
return list(parse_keybinding(keys)), pointer
def bind(self, context, keys, leaf):
keys, pointer = self._clean_input(context, keys)
if not keys:
return
last_key = keys[-1]
for key in keys[:-1]:
try:
if isinstance(pointer[key], dict):
pointer = pointer[key]
else:
pointer[key] = pointer = dict()
except:
pointer[key] = pointer = dict()
pointer[last_key] = leaf
def copy(self, context, source, target):
clean_source, pointer = self._clean_input(context, source)
if not source:
return
for key in clean_source:
try:
pointer = pointer[key]
except:
raise KeyError("Tried to copy the keybinding `%s',"
" but it was not found." % source)
self.bind(context, target, copy.deepcopy(pointer))
def unbind(self, context, keys):
keys, pointer = self._clean_input(context, keys)
if not keys:
return
_unbind_traverse(pointer, keys)
class KeyBuffer(object):
any_key = ANYKEY
passive_key = PASSIVE_ACTION
quantifier_key = QUANT_KEY
exclude_from_anykey = [27]
def __init__(self, keymap=None):
self.keymap = keymap
self.clear()
def clear(self):
self.keys = []
self.wildcards = []
self.pointer = self.keymap
self.result = None
self.quantifier = None
self.finished_parsing_quantifier = False
self.finished_parsing = False
self.parse_error = False
if self.keymap and self.quantifier_key in self.keymap:
if self.keymap[self.quantifier_key] == 'false':
self.finished_parsing_quantifier = True
def add(self, key):
self.keys.append(key)
self.result = None
if not self.finished_parsing_quantifier and key in digits:
if self.quantifier is None:
self.quantifier = 0
self.quantifier = self.quantifier * 10 + key - 48 # (48 = ord(0))
else:
self.finished_parsing_quantifier = True
moved = True
if key in self.pointer:
self.pointer = self.pointer[key]
elif self.any_key in self.pointer and \
key not in self.exclude_from_anykey:
self.wildcards.append(key)
self.pointer = self.pointer[self.any_key]
else:
moved = False
if moved:
if isinstance(self.pointer, dict):
if self.passive_key in self.pointer:
self.result = self.pointer[self.passive_key]
else:
self.result = self.pointer
self.finished_parsing = True
else:
self.finished_parsing = True
self.parse_error = True
def __str__(self):
return "".join("{0:c}".format(c) for c in self.keys)
|