From 8eaa16542ac845ae9e8a343f15f2242678704512 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Fri, 25 Oct 2024 22:47:20 +0200 Subject: [PATCH] Extract function for changing key bindings --- .../functions/__fish_change_key_bindings.fish | 17 +++++++++++++++ .../functions/fish_default_key_bindings.fish | 21 ++----------------- share/functions/fish_vi_key_bindings.fish | 17 +-------------- 3 files changed, 20 insertions(+), 35 deletions(-) create mode 100644 share/functions/__fish_change_key_bindings.fish diff --git a/share/functions/__fish_change_key_bindings.fish b/share/functions/__fish_change_key_bindings.fish new file mode 100644 index 000000000..5997f4c3b --- /dev/null +++ b/share/functions/__fish_change_key_bindings.fish @@ -0,0 +1,17 @@ +function __fish_change_key_bindings --argument-names bindings + # Allow the user to set the variable universally + set -l scope + set -q fish_key_bindings + or set scope -g + # We try to use `set --no-event`, but to avoid leaving the user without bindings + # if they run this with an older version we fall back on setting the variable + # with an event. + if ! set --no-event $scope fish_key_bindings $bindings 2>/dev/null + # This triggers the handler, which calls us again + set $scope fish_key_bindings $bindings + # unless the handler somehow doesn't exist, which would leave us without bindings. + # this happens in no-config mode. + functions -q __fish_reload_key_bindings + and return 1 + end +end diff --git a/share/functions/fish_default_key_bindings.fish b/share/functions/fish_default_key_bindings.fish index 3eb8f7afb..f79ad7676 100644 --- a/share/functions/fish_default_key_bindings.fish +++ b/share/functions/fish_default_key_bindings.fish @@ -9,25 +9,8 @@ function fish_default_key_bindings -d "emacs-like key binds" if not set -q argv[1] bind --erase --all --preset # clear earlier bindings, if any if test "$fish_key_bindings" != fish_default_key_bindings - # Allow the user to set the variable universally - set -l scope - set -q fish_key_bindings - or set scope -g - true - # We try to use `set --no-event`, but to avoid leaving the user without bindings - # if they run this with an older version we fall back on setting the variable - # with an event. - if ! set --no-event $scope fish_key_bindings fish_default_key_bindings 2>/dev/null - # This triggers the handler, which calls us again - set $scope fish_key_bindings fish_default_key_bindings - # unless the handler somehow doesn't exist, which would leave us without bindings. - # this happens in no-config mode. - functions -q __fish_reload_key_bindings - and return - else - # (we need to set the bind mode to default) - set --no-event fish_bind_mode default - end + __fish_change_key_bindings fish_default_key_bindings || return + set fish_bind_mode default end end diff --git a/share/functions/fish_vi_key_bindings.fish b/share/functions/fish_vi_key_bindings.fish index fdcf56848..00c279cdf 100644 --- a/share/functions/fish_vi_key_bindings.fish +++ b/share/functions/fish_vi_key_bindings.fish @@ -23,22 +23,7 @@ function fish_vi_key_bindings --description 'vi-like key bindings for fish' # and without this would then have subtly broken bindings. if test "$fish_key_bindings" != fish_vi_key_bindings and test "$rebind" = true - # Allow the user to set the variable universally - set -l scope - set -q fish_key_bindings - or set scope -g - true - # We try to use `set --no-event`, but to avoid leaving the user without bindings - # if they run this with an older version we fall back on setting the variable - # with an event. - if ! set --no-event $scope fish_key_bindings fish_vi_key_bindings 2>/dev/null - # This triggers the handler, which calls us again - set $scope fish_key_bindings fish_vi_key_bindings - # unless the handler somehow doesn't exist, which would leave us without bindings. - # this happens in no-config mode. - functions -q __fish_reload_key_bindings - and return - end + __fish_change_key_bindings fish_vi_key_bindings || return end set -l init_mode insert