From 5b33e6075283a4f0def2c87ccf789da824980f8a Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Mon, 22 Sep 2014 21:30:44 -0700 Subject: [PATCH] Support `bind SEQ` to print a binding for `SEQ` --- builtin.cpp | 36 +++++++++++++++++++++++++++++++----- doc_src/bind.txt | 8 ++++++-- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/builtin.cpp b/builtin.cpp index ab258acd6..a8478c4fd 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -516,23 +516,24 @@ static int get_terminfo_sequence(const wchar_t *seq, wcstring *out_seq) { return 1; } + wcstring eseq = escape_string(seq, 0); switch (errno) { case ENOENT: { - append_format(stderr_buffer, _(L"%ls: No key with name '%ls' found\n"), L"bind", seq); + append_format(stderr_buffer, _(L"%ls: No key with name '%ls' found\n"), L"bind", eseq.c_str()); break; } case EILSEQ: { - append_format(stderr_buffer, _(L"%ls: Key with name '%ls' does not have any mapping\n"), L"bind", seq); + append_format(stderr_buffer, _(L"%ls: Key with name '%ls' does not have any mapping\n"), L"bind", eseq.c_str()); break; } default: { - append_format(stderr_buffer, _(L"%ls: Unknown error trying to bind to key named '%ls'\n"), L"bind", seq); + append_format(stderr_buffer, _(L"%ls: Unknown error trying to bind to key named '%ls'\n"), L"bind", eseq.c_str()); break; } } @@ -763,8 +764,33 @@ static int builtin_bind(parser_t &parser, wchar_t **argv) case 1: { - res = STATUS_BUILTIN_ERROR; - append_format(stderr_buffer, _(L"%ls: Expected zero or at least two parameters, got %d\n"), argv[0], argc-woptind); + wcstring seq; + if (use_terminfo) + { + if (!get_terminfo_sequence(argv[woptind], &seq)) + { + res = STATUS_BUILTIN_ERROR; + // get_terminfo_sequence already printed the error + break; + } + } + else + { + seq = argv[woptind]; + } + if (!builtin_bind_list_one(seq, bind_mode)) + { + res = STATUS_BUILTIN_ERROR; + wcstring eseq = escape_string(argv[woptind], 0); + if (use_terminfo) + { + append_format(stderr_buffer, _(L"%ls: No binding found for key '%ls'\n"), argv[0], eseq.c_str()); + } + else + { + append_format(stderr_buffer, _(L"%ls: No binding found for sequence '%ls'\n"), argv[0], eseq.c_str()); + } + } break; } diff --git a/doc_src/bind.txt b/doc_src/bind.txt index 93e1f80dd..599a1ec3a 100644 --- a/doc_src/bind.txt +++ b/doc_src/bind.txt @@ -4,6 +4,7 @@ \fish{synopsis} bind [(-M | --mode) MODE] [(-m | --sets-mode) NEW_MODE] [(-k | --key)] SEQUENCE COMMAND [COMMAND...] +bind [(-M | --mode) MODE] [(-k | --key)] SEQUENCE bind (-K | --key-names) [(-a | --all)] bind (-f | --function-names) bind (-e | --erase) [(-M | --mode) MODE] @@ -12,8 +13,7 @@ bind (-e | --erase) [(-M | --mode) MODE] \subsection bind-description Description -`bind` adds a binding for the specified key sequence to the -specified command. +`bind` adds a binding for the specified key sequence to the specified command. SEQUENCE is the character sequence to bind to. These should be written as fish escape sequences. For example, because pressing the Alt key and another character sends that character prefixed with an escape character, Alt-based key bindings can be written using the `\e` escape. For example, @key{Alt,w} can be written as `\ew`. The control character can be written in much the same way using the `\c` escape, for example @key{Control,X} (^X) can be written as `\cx`. Note that Alt-based key bindings are case sensitive and Control-based key bindings are not. This is a constraint of text-based terminals, not `fish`. @@ -27,6 +27,10 @@ When `COMMAND` is a shellscript command, it is a good practice to put the actual If such a script produces output, the script needs to finish by calling `commandline -f repaint` in order to tell fish that a repaint is in order. +When multiple `COMMAND`s are provided, they are all run in the specified order when the key is pressed. + +If no `SEQUENCE` is provided, all bindings (or just the bindings in the specified `MODE`) are printed. If `SEQUENCE` is provided without `COMMAND`, just the binding matching that sequence is printed. + Key bindings are not saved between sessions by default. To save custom keybindings, edit the `fish_user_key_bindings` function and insert the appropriate `bind` statements. Key bindings may use "modes", which mimics Vi's modal input behavior. The default mode is "default", and every bind applies to a single mode. The mode can be viewed/changed with the `$fish_bind_mode` variable.