From 064ad7b9812b85192a5377923084cadee4da03d9 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Thu, 4 Jun 2015 13:31:48 -0700 Subject: [PATCH] Rework how the mode is reported in fish_vi_mode Add a new function fish_mode_prompt which (if it is defined) has its output prepended to the left prompt. Rather than replacing the prompt wholesale, make fish_vi_mode enable this function by setting a variable __fish_vi_mode. This enables vi mode to interoperate nicely with custom prompts. Users who want to change how the mode is reported can either redefine this function or erase it entirely. Fixes #1988. --- reader.cpp | 16 +++++++++ share/functions/fish_mode_prompt.fish | 19 ++++++++++ share/functions/fish_vi_mode.fish | 8 +++-- share/functions/fish_vi_prompt.fish | 51 --------------------------- 4 files changed, 40 insertions(+), 54 deletions(-) create mode 100644 share/functions/fish_mode_prompt.fish delete mode 100644 share/functions/fish_vi_prompt.fish diff --git a/reader.cpp b/reader.cpp index 4fdd6cebd..23f51a08b 100644 --- a/reader.cpp +++ b/reader.cpp @@ -128,6 +128,10 @@ commence. #define RIGHT_PROMPT_FUNCTION_NAME L"fish_right_prompt" +/* The name of the function for getting the input mode indicator */ +#define MODE_PROMPT_FUNCTION_NAME L"fish_mode_prompt" + + /** The default title for the reader. This is used by reader_readline. */ @@ -997,6 +1001,18 @@ static void exec_prompt() { proc_push_interactive(0); + // Prepend any mode indicator to the left prompt (#1988) + if (function_exists(MODE_PROMPT_FUNCTION_NAME)) + { + wcstring_list_t mode_indicator_list; + exec_subshell(MODE_PROMPT_FUNCTION_NAME, mode_indicator_list, apply_exit_status); + // We do not support multiple lines in the mode indicator, so just concatenate all of them + for (size_t i = 0; i < mode_indicator_list.size(); i++) + { + data->left_prompt_buff += mode_indicator_list.at(i); + } + } + if (! data->left_prompt.empty()) { wcstring_list_t prompt_list; diff --git a/share/functions/fish_mode_prompt.fish b/share/functions/fish_mode_prompt.fish new file mode 100644 index 000000000..305216796 --- /dev/null +++ b/share/functions/fish_mode_prompt.fish @@ -0,0 +1,19 @@ +# The fish_mode_prompt function is prepended to the prompt +function fish_mode_prompt --description "Displays the current mode" + # Do nothing if not in vi mode + if set -q __fish_vi_mode + switch $fish_bind_mode + case default + set_color --bold --background red white + echo '[N]' + case insert + set_color --bold --background green white + echo '[I]' + case visual + set_color --bold --background magenta white + echo '[V]' + end + set_color normal + echo -n ' ' + end +end diff --git a/share/functions/fish_vi_mode.fish b/share/functions/fish_vi_mode.fish index e431d0e4a..b4fbe63a7 100644 --- a/share/functions/fish_vi_mode.fish +++ b/share/functions/fish_vi_mode.fish @@ -1,6 +1,8 @@ function fish_vi_mode - function fish_prompt - fish_vi_prompt - end + # Set the __fish_vi_mode variable + # This triggers fish_mode_prompt to output the mode indicator + set -g __fish_vi_mode 1 + + # Turn on vi keybindings set -g fish_key_bindings fish_vi_key_bindings end diff --git a/share/functions/fish_vi_prompt.fish b/share/functions/fish_vi_prompt.fish deleted file mode 100644 index 420ea3cf0..000000000 --- a/share/functions/fish_vi_prompt.fish +++ /dev/null @@ -1,51 +0,0 @@ -function fish_vi_prompt_cm --description "Displays the current mode" - echo -n " " - switch $fish_bind_mode - case default - set_color --bold --background red white - echo "[N]" - case insert - set_color --bold --background green white - echo "[I]" - case visual - set_color --bold --background magenta white - echo "[V]" - end - set_color normal -end - -function fish_vi_prompt --description "Simple vi prompt" - - # Just calculate these once, to save a few cycles when displaying the prompt - if not set -q __fish_prompt_hostname - set -g __fish_prompt_hostname (hostname|cut -d . -f 1) - end - - if not set -q __fish_prompt_normal - set -g __fish_prompt_normal (set_color normal) - end - - switch $USER - - case root toor - - if not set -q __fish_prompt_cwd - if set -q fish_color_cwd_root - set -g __fish_prompt_cwd (set_color $fish_color_cwd_root) - else - set -g __fish_prompt_cwd (set_color $fish_color_cwd) - end - end - - echo -n -s "$USER" @ "$__fish_prompt_hostname" ' ' "$__fish_prompt_cwd" (prompt_pwd) "$__fish_prompt_normal" (fish_vi_prompt_cm) '# ' - - case '*' - - if not set -q __fish_prompt_cwd - set -g __fish_prompt_cwd (set_color $fish_color_cwd) - end - - echo -n -s "$USER" @ "$__fish_prompt_hostname" ' ' "$__fish_prompt_cwd" (prompt_pwd) "$__fish_prompt_normal" (fish_vi_prompt_cm) '> ' - - end -end