1 This file is bind.def, from which is created bind.c.
2 It implements the builtin "bind" in Bash.
4 Copyright (C) 1987-2020 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
27 $FUNCTION bind_builtin
28 $SHORT_DOC bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
29 Set Readline key bindings and variables.
31 Bind a key sequence to a Readline function or a macro, or set a
32 Readline variable. The non-option argument syntax is equivalent to
33 that found in ~/.inputrc, but must be passed as a single argument:
34 e.g., bind '"\C-x\C-r": re-read-init-file'.
37 -m keymap Use KEYMAP as the keymap for the duration of this
38 command. Acceptable keymap names are emacs,
39 emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move,
40 vi-command, and vi-insert.
41 -l List names of functions.
42 -P List function names and bindings.
43 -p List functions and bindings in a form that can be
45 -S List key sequences that invoke macros and their values
46 -s List key sequences that invoke macros and their values
47 in a form that can be reused as input.
48 -V List variable names and values
49 -v List variable names and values in a form that can
51 -q function-name Query about which keys invoke the named function.
52 -u function-name Unbind all keys which are bound to the named function.
53 -r keyseq Remove the binding for KEYSEQ.
54 -f filename Read key bindings from FILENAME.
55 -x keyseq:shell-command Cause SHELL-COMMAND to be executed when
57 -X List key sequences bound with -x and associated commands
58 in a form that can be reused as input.
61 bind returns 0 unless an unrecognized option is given or an error occurs.
64 #if defined (READLINE)
66 #if defined (HAVE_UNISTD_H)
68 # include <sys/types.h>
79 #include <readline/readline.h>
80 #include <readline/history.h>
82 #include "../bashintl.h"
85 #include "../bashline.h"
86 #include "bashgetopt.h"
89 static int query_bindings PARAMS((char *));
90 static int unbind_command PARAMS((char *));
91 static int unbind_keyseq PARAMS((char *));
93 #define BIND_RETURN(x) do { return_code = x; goto bind_exit; } while (0)
102 #define PPFLAG 0x0080
103 #define VVFLAG 0x0100
105 #define SSFLAG 0x0400
108 #define XXFLAG 0x2000
115 Keymap kmap, saved_keymap;
117 char *initfile, *map_name, *fun_name, *unbind_name, *remove_seq, *cmd_seq, *t;
122 builtin_error (_("line editing not enabled"));
123 return (EXECUTION_FAILURE);
125 builtin_warning (_("line editing not enabled"));
129 kmap = saved_keymap = (Keymap) NULL;
131 initfile = map_name = fun_name = unbind_name = remove_seq = cmd_seq = (char *)NULL;
132 return_code = EXECUTION_SUCCESS;
134 if (bash_readline_initialized == 0)
135 initialize_readline ();
137 begin_unwind_frame ("bind_builtin");
138 unwind_protect_var (rl_outstream);
140 rl_outstream = stdout;
142 reset_internal_getopt ();
143 while ((opt = internal_getopt (list, "lvpVPsSXf:q:u:m:r:x:")) != -1)
158 initfile = list_optarg;
162 map_name = list_optarg;
166 fun_name = list_optarg;
170 unbind_name = list_optarg;
174 remove_seq = list_optarg;
190 cmd_seq = list_optarg;
198 BIND_RETURN (EX_USAGE);
204 /* First, see if we need to install a special keymap for this
205 command. Then start on the arguments. */
207 if ((flags & MFLAG) && map_name)
209 kmap = rl_get_keymap_by_name (map_name);
212 builtin_error (_("`%s': invalid keymap name"), map_name);
213 BIND_RETURN (EXECUTION_FAILURE);
219 saved_keymap = rl_get_keymap ();
220 rl_set_keymap (kmap);
223 /* XXX - we need to add exclusive use tests here. It doesn't make sense
224 to use some of these options together. */
225 /* Now hack the option arguments */
227 rl_list_funmap_names ();
230 rl_function_dumper (1);
233 rl_function_dumper (0);
242 rl_variable_dumper (1);
245 rl_variable_dumper (0);
247 if ((flags & FFLAG) && initfile)
249 if (rl_read_init_file (initfile) != 0)
251 t = printable_filename (initfile, 0);
252 builtin_error (_("%s: cannot read: %s"), t, strerror (errno));
255 BIND_RETURN (EXECUTION_FAILURE);
259 if ((flags & QFLAG) && fun_name)
260 return_code = query_bindings (fun_name);
262 if ((flags & UFLAG) && unbind_name)
263 return_code = unbind_command (unbind_name);
265 if ((flags & RFLAG) && remove_seq)
267 opt = unbind_keyseq (remove_seq);
272 return_code = bind_keyseq_to_unix_command (cmd_seq);
275 return_code = print_unix_command_map ();
277 /* Process the rest of the arguments as binding specifications. */
280 int olen, nlen, d, i;
281 char **obindings, **nbindings;
283 obindings = rl_invoking_keyseqs (bash_execute_unix_command);
284 olen = obindings ? strvec_len (obindings) : 0;
286 rl_parse_and_bind (list->word->word);
288 nbindings = rl_invoking_keyseqs (bash_execute_unix_command);
289 nlen = nbindings ? strvec_len (nbindings) : 0;
291 if (nlen < olen) /* fewer bind -x bindings */
292 for (d = olen - nlen, i = 0; i < olen && d > 0; i++)
293 if (nlen == 0 || strvec_search (nbindings, obindings[i]) >= 0)
295 unbind_unix_command (obindings[i]);
299 strvec_dispose (obindings);
300 strvec_dispose (nbindings);
307 rl_set_keymap (saved_keymap);
309 run_unwind_frame ("bind_builtin");
312 return_code = EXECUTION_FAILURE;
314 return (sh_chkwrite (return_code));
318 query_bindings (name)
321 rl_command_func_t *function;
325 function = rl_named_function (name);
328 builtin_error (_("`%s': unknown function name"), name);
329 return EXECUTION_FAILURE;
332 keyseqs = rl_invoking_keyseqs (function);
336 printf (_("%s is not bound to any keys.\n"), name);
337 return EXECUTION_FAILURE;
340 printf (_("%s can be invoked via "), name);
341 for (j = 0; j < 5 && keyseqs[j]; j++)
342 printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n");
345 strvec_dispose (keyseqs);
346 return EXECUTION_SUCCESS;
350 unbind_command (name)
353 rl_command_func_t *function;
355 function = rl_named_function (name);
358 builtin_error (_("`%s': unknown function name"), name);
359 return EXECUTION_FAILURE;
362 rl_unbind_function_in_map (function, rl_get_keymap ());
363 return EXECUTION_SUCCESS;
372 rl_command_func_t *f;
374 kseq = (char *)xmalloc ((2 * strlen (seq)) + 1);
375 if (rl_translate_keyseq (seq, kseq, &kslen))
378 builtin_error (_("`%s': cannot unbind"), seq);
379 return EXECUTION_FAILURE;
381 if ((f = rl_function_of_keyseq_len (kseq, kslen, (Keymap)0, &type)) == 0)
384 return (EXECUTION_SUCCESS);
387 f = ((Keymap) f)[ANYOTHERKEY].function;
389 /* I wish this didn't have to translate the key sequence again, but readline
390 doesn't have a binding function that takes a translated key sequence as
392 if (rl_bind_keyseq (seq, (rl_command_func_t *)NULL) != 0)
395 builtin_error (_("`%s': cannot unbind"), seq);
396 return (EXECUTION_FAILURE);
399 if (f == bash_execute_unix_command)
400 unbind_unix_command (seq);
403 return (EXECUTION_SUCCESS);
405 #endif /* READLINE */