From fb73a4b2e2e4841ea11552a8376ed6168cf03479 Mon Sep 17 00:00:00 2001 From: Andrey Mishchenko Date: Wed, 8 Dec 2021 11:16:26 -0500 Subject: [PATCH] Implement nextd-or-forward-word and prevd-or-backward-word in C++ --- doc_src/tutorial.rst | 3 ++- share/functions/nextd-or-forward-word.fish | 9 ------- share/functions/prevd-or-backward-word.fish | 9 ------- src/input.cpp | 2 ++ src/input_common.h | 2 ++ src/reader.cpp | 26 +++++++++++++++++++-- 6 files changed, 30 insertions(+), 21 deletions(-) delete mode 100644 share/functions/nextd-or-forward-word.fish delete mode 100644 share/functions/prevd-or-backward-word.fish diff --git a/doc_src/tutorial.rst b/doc_src/tutorial.rst index f66c6ccac..001c3e0b2 100644 --- a/doc_src/tutorial.rst +++ b/doc_src/tutorial.rst @@ -583,7 +583,8 @@ Unlike other shells, fish does not have aliases or special prompt syntax. Functi You can list the names of all functions with the :ref:`functions ` builtin (note the plural!). fish starts out with a number of functions:: > functions - N_, abbr, alias, bg, cd, cdh, contains_seq, delete-or-exit, dirh, dirs, disown, down-or-search, edit_command_buffer, export, fg, fish_add_path, fish_breakpoint_prompt, fish_clipboard_copy, fish_clipboard_paste, fish_config, fish_default_key_bindings, fish_default_mode_prompt, fish_git_prompt, fish_hg_prompt, fish_hybrid_key_bindings, fish_indent, fish_is_root_user, fish_job_summary, fish_key_reader, fish_md5, fish_mode_prompt, fish_npm_helper, fish_opt, fish_print_git_action, fish_print_hg_root, fish_prompt, fish_sigtrap_handler, fish_svn_prompt, fish_title, fish_update_completions, fish_vcs_prompt, fish_vi_cursor, fish_vi_key_bindings, funced, funcsave, grep, help, history, hostname, isatty, kill, la, ll, ls, man, nextd, nextd-or-forward-word, open, popd, prevd, prevd-or-backward-word, prompt_hostname, prompt_pwd, psub, pushd, realpath, seq, setenv, suspend, trap, type, umask, up-or-search, vared, wait + N_, abbr, alias, bg, cd, cdh, contains_seq, delete-or-exit, dirh, dirs, disown, down-or-search, edit_command_buffer, export, fg, fish_add_path, fish_breakpoint_prompt, fish_clipboard_copy, fish_clipboard_paste, fish_config, fish_default_key_bindings, fish_default_mode_prompt, fish_git_prompt, fish_hg_prompt, fish_hybrid_key_bindings, fish_indent, fish_is_root_user, fish_job_summary, fish_key_reader, fish_md5, fish_mode_prompt, fish_npm_helper, fish_opt, fish_print_git_action, fish_print_hg_root, fish_prompt, fish_sigtrap_handler, fish_svn_prompt, fish_title, fish_update_completions, fish_vcs_prompt, fish_vi_cursor, fish_vi_key_bindings, funced, funcsave, grep, help, history, hostname, isatty, kill, la, ll, ls, man, nextd, open, popd, prevd, prompt_hostname, prompt_pwd, psub, pushd, realpath, seq, setenv, suspend, trap, type, umask, up-or-search, vared, wait + You can see the source for any function by passing its name to ``functions``:: diff --git a/share/functions/nextd-or-forward-word.fish b/share/functions/nextd-or-forward-word.fish deleted file mode 100644 index 9428b89e2..000000000 --- a/share/functions/nextd-or-forward-word.fish +++ /dev/null @@ -1,9 +0,0 @@ -function nextd-or-forward-word - set -l cmd (commandline) - if test -z "$cmd" - nextd - commandline -f repaint - else - commandline -f forward-word - end -end diff --git a/share/functions/prevd-or-backward-word.fish b/share/functions/prevd-or-backward-word.fish deleted file mode 100644 index 2aaee3b51..000000000 --- a/share/functions/prevd-or-backward-word.fish +++ /dev/null @@ -1,9 +0,0 @@ -function prevd-or-backward-word - set -l cmd (commandline) - if test -z "$cmd" - prevd - commandline -f repaint - else - commandline -f backward-word - end -end diff --git a/src/input.cpp b/src/input.cpp index 58e98cdf8..75a9522ba 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -143,8 +143,10 @@ static constexpr const input_function_metadata_t input_function_metadata[] = { {L"kill-selection", readline_cmd_t::kill_selection}, {L"kill-whole-line", readline_cmd_t::kill_whole_line}, {L"kill-word", readline_cmd_t::kill_word}, + {L"nextd-or-forward-word", readline_cmd_t::nextd_or_forward_word}, {L"or", readline_cmd_t::func_or}, {L"pager-toggle-search", readline_cmd_t::pager_toggle_search}, + {L"prevd-or-backward-word", readline_cmd_t::prevd_or_backward_word}, {L"redo", readline_cmd_t::redo}, {L"repaint", readline_cmd_t::repaint}, {L"repaint-mode", readline_cmd_t::repaint_mode}, diff --git a/src/input_common.h b/src/input_common.h index 07b1539fd..ea517cd6e 100644 --- a/src/input_common.h +++ b/src/input_common.h @@ -19,6 +19,8 @@ enum class readline_cmd_t { backward_word, forward_bigword, backward_bigword, + nextd_or_forward_word, + prevd_or_backward_word, history_search_backward, history_search_forward, history_prefix_search_backward, diff --git a/src/reader.cpp b/src/reader.cpp index 1714040d9..644349e4c 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -1462,6 +1462,8 @@ static bool command_ends_paging(readline_cmd_t c, bool focused_on_search_field) case rl::backward_word: case rl::forward_bigword: case rl::backward_bigword: + case rl::nextd_or_forward_word: + case rl::prevd_or_backward_word: case rl::delete_char: case rl::backward_delete_char: case rl::kill_line: @@ -3510,7 +3512,17 @@ void reader_data_t::handle_readline_command(readline_cmd_t c, readline_loop_stat break; } case rl::backward_word: - case rl::backward_bigword: { + case rl::backward_bigword: + case rl::prevd_or_backward_word: { + if (c == rl::prevd_or_backward_word && command_line.empty()) { + auto last_statuses = parser().get_last_statuses(); + (void)parser().eval(L"prevd", io_chain_t{}); + parser().set_last_statuses(std::move(last_statuses)); + force_exec_prompt_and_repaint = true; + inputter.queue_char(readline_cmd_t::repaint); + break; + } + auto move_style = (c == rl::backward_word) ? move_word_style_punctuation : move_word_style_whitespace; move_word(active_edit_line(), MOVE_DIR_LEFT, false /* do not erase */, move_style, @@ -3518,7 +3530,17 @@ void reader_data_t::handle_readline_command(readline_cmd_t c, readline_loop_stat break; } case rl::forward_word: - case rl::forward_bigword: { + case rl::forward_bigword: + case rl::nextd_or_forward_word: { + if (c == rl::nextd_or_forward_word && command_line.empty()) { + auto last_statuses = parser().get_last_statuses(); + (void)parser().eval(L"nextd", io_chain_t{}); + parser().set_last_statuses(std::move(last_statuses)); + force_exec_prompt_and_repaint = true; + inputter.queue_char(readline_cmd_t::repaint); + break; + } + auto move_style = (c == rl::forward_word) ? move_word_style_punctuation : move_word_style_whitespace; editable_line_t *el = active_edit_line();