Skip to content

Commit 51dbf41

Browse files
committed
Refactor and fix keybinding issue (fixes bpython#447)
This changes the logic for the function get_key_no_doublebind() and removes the mutable list which was previously used to keep track of keybindings which were already used. Although this fix passes the test cases and the specific case mentioned in bpython#447, the behavior is such that specifying the same custom keybinding for two commands will result in only one command being bound.
1 parent 305b445 commit 51dbf41

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

bpython/config.py

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,39 @@ def loadini(struct, configfile):
104104
'list_above' : False,
105105
'right_arrow_completion' : True,
106106
}}
107+
108+
default_keys_to_commands = {
109+
'': 'exit',
110+
'C-n': 'down_one_line',
111+
'C-l': 'clear_screen',
112+
'C-k': 'kill_line',
113+
'C-o': 'search',
114+
'C-h': 'backspace',
115+
'C-f': 'right',
116+
'C-e': 'end_of_line',
117+
'C-d': 'delete',
118+
'C-b':'left',
119+
'C-a': 'beginning_of_line',
120+
'C-z': 'suspend',
121+
'C-y': 'yank_from_buffer',
122+
'C-x': 'edit_current_block',
123+
'C-w': 'clear_word',
124+
'C-u': 'clear_line',
125+
'C-t': 'transpose_chars',
126+
'C-s': 'save',
127+
'C-r': 'undo',
128+
'C-p': 'up_one_line',
129+
'F10': 'copy_clipboard',
130+
'F1': 'help',
131+
'F2': 'show_source',
132+
'F3': 'edit_config',
133+
'F5': 'toggle_file_watch',
134+
'F6': 'reimport',
135+
'F7': 'external_editor',
136+
'F8': 'pastebin',
137+
'F9': 'last_output'
138+
}
139+
107140
fill_config_with_default_values(config, defaults)
108141
if not config.read(config_path):
109142
# No config file. If the user has it in the old place then complain
@@ -113,17 +146,17 @@ def loadini(struct, configfile):
113146
"%s\n" % default_config_path())
114147
sys.exit(1)
115148

116-
def get_key_no_doublebind(attr, already_used={}):
117-
"""Clears any other configured keybindings using this key"""
118-
key = config.get('keyboard', attr)
119-
if key in already_used:
120-
default = defaults['keyboard'][already_used[key]]
121-
if default in already_used:
122-
setattr(struct, '%s_key' % already_used[key], '')
123-
else:
124-
setattr(struct, '%s_key' % already_used[key], default)
125-
already_used[key] = attr
126-
return key
149+
150+
def get_key_no_doublebind(command):
151+
default_commands_to_keys = defaults['keyboard']
152+
requested_key = config.get('keyboard', command)
153+
default_command = default_keys_to_commands[requested_key]
154+
155+
if default_commands_to_keys[default_command] == \
156+
config.get('keyboard', default_command):
157+
setattr(struct, '%s_key' % default_command, '')
158+
159+
return requested_key
127160

128161
struct.config_path = config_path
129162

0 commit comments

Comments
 (0)