Skip to content

Commit

Permalink
Clear keybinding from all keymaps (unbind) with `bind generic <key> n…
Browse files Browse the repository at this point in the history
…one`
  • Loading branch information
koutcher committed Mar 27, 2024
1 parent 683fdc8 commit e314418
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Improvements:
(hidden in default config). (#1318)
- Show the selected commit in the blame view title window.
- Improve the blob view experience.
- Clear keybinding from all keymaps (unbind) with `bind generic <key> none`.

Bug fixes:

Expand Down
1 change: 0 additions & 1 deletion contrib/git-flow.tigrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

# Get rid of default bindings for F, as that will be the entry point for all
# git-flow related commands with this binding.
bind main F none
bind generic F none

# General
Expand Down
3 changes: 1 addition & 2 deletions contrib/vim.tigrc
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ bind generic @k :?^@@
bind generic @- :toggle diff-context -1
bind generic @+ :toggle diff-context +1

bind status u none
bind stage u none
bind generic u none
bind generic uu status-update
bind generic ur status-revert
bind generic um status-merge
Expand Down
7 changes: 5 additions & 2 deletions doc/tigrc.5.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -613,16 +613,19 @@ Or in the Git configuration files:
--------------------------------------------------------------------------
[tig "bind"]
# 'unbind' the default quit key binding
main = Q none
generic = Q none
# Cherry-pick current commit onto current branch
generic = C !git cherry-pick %(commit)
main = C !git cherry-pick %(commit)
--------------------------------------------------------------------------

Keys are mapped by first searching the keybindings for the current view, then
the keybindings for the *generic* keymap, and last the default keybindings.
Thus, the view keybindings override the generic keybindings which override the
built-in keybindings.

Clear keybinding (unbind) from all keymaps at once with bind *generic* 'key'
*none*.

Keybindings at the line-entry prompt are typically governed by the readline
library, and are configured separately in `~/.inputrc`. See 'readline(1)'.
Tig respects but does not require an `$if tig` section in `~/.inputrc`.
Expand Down
27 changes: 27 additions & 0 deletions src/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ keybinding_equals(const struct keybinding *keybinding, const struct key key[],
return keybinding_matches(keybinding, key, keys, conflict_ptr);
}

static enum status_code
del_keybinding(const struct key key[], size_t keys)
{
bool found = false;
size_t i;

for (i = 0; i < ARRAY_SIZE(keymaps); i++) {
struct keymap *table = &keymaps[i];
size_t j;

for (j = 0; j < table->size; j++)
if (keybinding_equals(table->data[j], key, keys, NULL)) {
found = true;
free(table->data[j]);
table->size--;
for (; j < table->size; j++)
table->data[j] = table->data[j + 1];
table->data = realloc(table->data, table->size * sizeof(*table->data));
}
}

return found ? SUCCESS : error("No keybinding found for %s", get_key_name(key, keys, false));
}

enum status_code
add_keybinding(struct keymap *table, enum request request,
const struct key key[], size_t keys)
Expand All @@ -103,6 +127,9 @@ add_keybinding(struct keymap *table, enum request request,
bool conflict = false;
size_t i;

if (is_generic_keymap(table) && request == REQ_NONE)
return del_keybinding(key, keys);

for (i = 0; i < table->size; i++) {
if (keybinding_equals(table->data[i], key, keys, &conflict)) {
enum request old_request = table->data[i]->request;
Expand Down
2 changes: 1 addition & 1 deletion test/help/user-command-test
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ Searching
[-] main bindings
Cursor navigation
G move-last-line Move cursor to last line
[help] - line 81 of 155 69%
[help] - line 81 of 148 72%
EOF

0 comments on commit e314418

Please sign in to comment.