Skip to content

Commit

Permalink
[core] allow custom accept-then keybindings from input
Browse files Browse the repository at this point in the history
Allow a mapping in the form "keystroke -> iterable-of-vd-functions" to
define custom keybindings that accept input and then queue one or more
vd functions. With these changes, an option like the following can
define custom "accept/move/re-edit" flows:

vd.options.edit_accept_keybindings = {
    "Ctrl+N": ("go-down", "edit-cell"),
    "Ctrl+P": ("go-up", "edit-cell"),
}
  • Loading branch information
ajkerrigan committed Dec 27, 2023
1 parent c9c018a commit 2d464ee
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions visidata/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
vd.theme_option('disp_edit_fill', '_', 'edit field fill character')
vd.theme_option('disp_unprintable', '·', 'substitute character for unprintables')
vd.theme_option('mouse_interval', 1, 'max time between press/release for click (ms)', sheettype=None)
vd.option('edit_accept_keybindings', {}, 'mapping of keystrokes to vd functions to run after accepting edit input')

vd.disp_help = 1 # current level of help shown (up to vd.options.disp_help as maximum)

Expand Down Expand Up @@ -285,6 +286,7 @@ def find_nonword(s, a, b, incr):
ch = vd.getkeystroke(scr)
if ch == '': continue
elif ch in bindings: v, i = bindings[ch](v, i)
elif vd.prettykeys(ch) in bindings: v, i = bindings[vd.prettykeys(ch)](v, i)
elif ch == 'KEY_IC': insert_mode = not insert_mode
elif ch == '^A' or ch == 'KEY_HOME': i = 0
elif ch == '^B' or ch == 'KEY_LEFT': i -= 1
Expand Down Expand Up @@ -623,6 +625,7 @@ def editCell(self, vcolidx=None, rowidx=None, value=None, **kwargs):

# update local bindings with kwargs.bindings instead of the inverse, to preserve kwargs.bindings for caller
bindings.update(kwargs.get('bindings', {}))
bindings.update({k: acceptThenFunc(*v) for k, v in options.edit_accept_keybindings.items()})
kwargs['bindings'] = bindings

editargs = dict(value=value,
Expand Down

0 comments on commit 2d464ee

Please sign in to comment.