Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rc tools menu: replace menu builtin with a prompt-based implementation #5038

Merged
merged 2 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -879,15 +879,6 @@ progressively displaying their result in Kakoune.

See <<doc/pages/buffers#fifo-buffers,`:doc buffers fifo-buffers`>>.

Menus
~~~~~

When a menu is displayed, you can use `j`, `<c-n>` or `<tab>` to select the next
entry, and `k`, `<c-p>` or `<shift-tab>` to select the previous one.

Using the `/` key, you can enter some regex in order to restrict available choices
to the matching ones.

Credits
-------

Expand Down
13 changes: 0 additions & 13 deletions doc/pages/commands.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -342,19 +342,6 @@ but not really useful in that context.
so inside a draft context like `evaluate-commands -draft`, it only
responds to an `execute-keys` command in the same context.

*menu* [<switches>] <label1> <commands1> <label2> <commands2> ...::
display a menu using labels, the selected label’s commands are
executed. The *menu* command can take an *-auto-single* argument, to automatically
run commands when only one choice is provided, and a *-select-cmds*
argument, in which case menu takes three argument per item, the
last one being a command to execute when the item is selected (but
not validated)

NOTE: The menu is displayed in and receives input from the
current client context, so inside a draft context like
`evaluate-commands -draft`, it is invisible and only responds to
an `execute-keys` command in the same context.

*info* [<switches>] <text>::
display text in an information box with the following *switches*:

Expand Down
3 changes: 0 additions & 3 deletions doc/pages/mapping.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ The *map* command makes *key* behave as if the *keys* sequence was typed.
*prompt*::
prompts, such as when entering a command through *:*, or a regex through */*

*menu*::
mode entered when a menu is displayed with the 'menu' command

*user*::
mode entered when the user prefix is hit (default: '<space>')

Expand Down
5 changes: 0 additions & 5 deletions doc/pages/modes.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ as scrolling or centering the main selection cursor.

See view commands <<keys#view-commands,`:doc keys view-commands`>>.

=== Menu mode

Menu mode is entered when a menu is displayed with the `menu` command.
Mappings are used to filter and select intended items.

=== Prompt mode

Mode entered with `:`, `/` or the `prompt` command. During prompt mode a
Expand Down
4 changes: 2 additions & 2 deletions rc/filetype/kakrc.kak
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ evaluate-commands %sh{
keywords="add-highlighter alias arrange-buffers buffer buffer-next buffer-previous catch
change-directory colorscheme debug declare-option declare-user-mode define-command complete-command
delete-buffer delete-buffer! echo edit edit! enter-user-mode evaluate-commands execute-keys
fail hook info kill kill! map menu nop on-key prompt provide-module quit quit!
fail hook info kill kill! map nop on-key prompt provide-module quit quit!
remove-highlighter remove-hooks rename-buffer rename-client rename-session require-module
select set-face set-option set-register source trigger-user-hook try
unalias unmap unset-face unset-option update-option
write write! write-all write-all-quit write-quit write-quit!"
attributes="global buffer window current
normal insert menu prompt goto view user object
normal insert prompt goto view user object
number-lines show-matching show-whitespaces fill regex dynregex group flag-lines
ranges line column wrap ref regions region default-region replace-ranges"
types="int bool str regex int-list str-list completions line-specs range-specs str-to-str-map"
Expand Down
6 changes: 3 additions & 3 deletions rc/tools/ctags.kak
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ define-command -params ..1 \
ctags-search [<symbol>]: jump to a symbol's definition
If no symbol is passed then the current selection is used as symbol name
} \
ctags-search %[ evaluate-commands %sh[
ctags-search %[ require-module menu; evaluate-commands %sh[
realpath() { ( cd "$(dirname "$1")"; printf "%s/%s\n" "$(pwd -P)" "$(basename "$1")" ) }
export tagname="${1:-${kak_selection}}"
eval "set -- $kak_quoted_opt_ctagsfiles"
Expand All @@ -49,15 +49,15 @@ define-command -params ..1 \
menu_item = $2; gsub("!", "!!", menu_item);
edit_path = path($2); gsub("&", "&&", edit_path); gsub("#", "##", edit_path); gsub("\\|", "||", edit_path);
select = $1; gsub(/</, "<lt>", select); gsub(/\t/, "<c-v><c-i>", select); gsub("!", "!!", select); gsub("&", "&&", select); gsub("#", "##", select); gsub("\\|", "||", select);
out = out "%!" menu_item ": {MenuInfo}{\\}" menu_info "! %!evaluate-commands %# try %& edit -existing %|" edit_path "|; execute-keys %|/\\Q" keys "<ret>vc| & catch %& fail unable to find tag &; try %& execute-keys %|s\\Q" select "<ret>| & # !"
out = out "%!" menu_item ": " menu_info "! %!evaluate-commands %# try %& edit -existing %|" edit_path "|; execute-keys %|/\\Q" keys "<ret>vc| & catch %& fail unable to find tag &; try %& execute-keys %|s\\Q" select "<ret>| & # !"
}
/[^\t]+\t[^\t]+\t[0-9]+/ {
menu_item = $2; gsub("!", "!!", menu_item);
select = $1; gsub(/</, "<lt>", select); gsub(/\t/, "<c-v><c-i>", select); gsub("!", "!!", select); gsub("&", "&&", select); gsub("#", "##", select); gsub("\\|", "||", select);
menu_info = $3; gsub("!", "!!", menu_info);
edit_path = path($2); gsub("!", "!!", edit_path); gsub("#", "##", edit_path); gsub("&", "&&", edit_path); gsub("\\|", "||", edit_path);
line_number = $3;
out = out "%!" menu_item ": {MenuInfo}{\\}" menu_info "! %!evaluate-commands %# try %& edit -existing %|" edit_path "|; execute-keys %|" line_number "gx| & catch %& fail unable to find tag &; try %& execute-keys %|s\\Q" select "<ret>| & # !"
out = out "%!" menu_item ": " menu_info "! %!evaluate-commands %# try %& edit -existing %|" edit_path "|; execute-keys %|" line_number "gx| & catch %& fail unable to find tag &; try %& execute-keys %|s\\Q" select "<ret>| & # !"
}
END { print ( length(out) == 0 ? "fail no such tag " ENVIRON["tagname"] : "menu -markup -auto-single " out ) }
# Ensure x is an absolute file path, by prepending with tagroot
Expand Down
85 changes: 85 additions & 0 deletions rc/tools/menu.kak
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
provide-module menu %§§

define-command menu -params 1.. -docstring %{
menu [<switches>] <name1> <commands1> <name2> <commands2>...: display a
menu and execute commands for the selected item

-auto-single instantly validate if only one item is available
-select-cmds each item specify an additional command to run when selected
} %{
evaluate-commands %sh{
auto_single=false
select_cmds=false
stride=2
on_abort=
while true
do
case "$1" in
(-auto-single) auto_single=true ;;
(-select-cmds) select_cmds=true; stride=3 ;;
(-on-abort) on_abort="$2"; shift ;;
(-markup) ;; # no longer supported
(*) break ;;
esac
shift
done
if [ $(( $# % $stride )) -ne 0 ]; then
echo fail "wrong argument count"
exit
fi
if $auto_single && [ $# -eq $stride ]; then
printf %s "$2"
exit
fi
shellquote() {
printf "'%s'" "$(printf %s "$1" | sed "s/'/'\\\\''/g; s/§/§§/g; $2")"
}
cases=
select_cases=
completion=
nl=$(printf '\n.'); nl=${nl%.}
while [ $# -gt 0 ]; do
title=$1
command=$2
completion="${completion}${title}${nl}"
cases="${cases}
($(shellquote "$title" s/¶/¶¶/g))
printf '%s\\n' $(shellquote "$command" s/¶/¶¶/g)
;;"
if $select_cmds; then
select_command=$3
select_cases="${select_cases}
($(shellquote "$title" s/¶/¶¶/g))
printf '%s\\n' $(shellquote "$select_command" s/¶/¶¶/g)
;;"
fi
shift $stride
done
printf "\
prompt '' %%§
evaluate-commands %%sh¶
case \"\$kak_text\" in \
%s
(*) echo fail -- no such item: \"'\$(printf %%s \"\$kak_text\" | sed \"s/'/''/g\")'\" ;;
esac
§" "$cases"
if $select_cmds; then
printf " \
-on-change %%§
evaluate-commands %%sh¶
case \"\$kak_text\" in \
%s
(*) : ;;
esac
§" "$select_cases"
fi
if [ -n "$on_abort" ]; then
printf " -on-abort '%s'" "$(printf %s "$on_abort" | sed "s/'/''/g")"
fi
printf ' -menu -shell-script-candidates %%§
printf %%s %s
§\n' "$(shellquote "$completion")"
}
}
60 changes: 0 additions & 60 deletions src/commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2274,65 +2274,6 @@ const CommandDesc prompt_cmd = {
}
};

const CommandDesc menu_cmd = {
"menu",
nullptr,
"menu [<switches>] <name1> <commands1> <name2> <commands2>...: display a "
"menu and execute commands for the selected item",
ParameterDesc{
{ { "auto-single", { {}, "instantly validate if only one item is available" } },
{ "select-cmds", { {}, "each item specify an additional command to run when selected" } },
{ "markup", { {}, "parse menu entries as markup text" } } }
},
CommandFlags::None,
CommandHelper{},
CommandCompleter{},
[](const ParametersParser& parser, Context& context, const ShellContext& shell_context)
{
const bool with_select_cmds = (bool)parser.get_switch("select-cmds");
const bool markup = (bool)parser.get_switch("markup");
const size_t modulo = with_select_cmds ? 3 : 2;

const size_t count = parser.positional_count();
if (count == 0 or (count % modulo) != 0)
throw wrong_argument_count();

if (count == modulo and parser.get_switch("auto-single"))
{
ScopedSetBool noninteractive{context.noninteractive()};

CommandManager::instance().execute(parser[1], context);
return;
}

Vector<DisplayLine> choices;
Vector<String> commands;
Vector<String> select_cmds;
for (int i = 0; i < count; i += modulo)
{
if (parser[i].empty())
throw runtime_error(format("entry #{} is empty", i+1));

choices.push_back(markup ? parse_display_line(parser[i], context.faces())
: DisplayLine{ parser[i], {} });
commands.push_back(parser[i+1]);
if (with_select_cmds)
select_cmds.push_back(parser[i+2]);
}

CapturedShellContext sc{shell_context};
context.input_handler().menu(std::move(choices),
[=](int choice, MenuEvent event, Context& context) {
ScopedSetBool noninteractive{context.noninteractive()};

if (event == MenuEvent::Validate and choice >= 0 and choice < commands.size())
CommandManager::instance().execute(commands[choice], context, sc);
if (event == MenuEvent::Select and choice >= 0 and choice < select_cmds.size())
CommandManager::instance().execute(select_cmds[choice], context, sc);
});
}
};

const CommandDesc on_key_cmd = {
"on-key",
nullptr,
Expand Down Expand Up @@ -2820,7 +2761,6 @@ void register_commands()
register_command(execute_keys_cmd);
register_command(evaluate_commands_cmd);
register_command(prompt_cmd);
register_command(menu_cmd);
register_command(on_key_cmd);
register_command(info_cmd);
register_command(try_catch_cmd);
Expand Down
Loading