1 This file is bind.def, from which is created bind.c.
2 It implements the builtin "bind" in Bash.
4 Copyright (C) 1987-2009 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 [-lpvsPVS] [-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
59 bind returns 0 unless an unrecognized option is given or an error occurs.
62 #if defined (READLINE)
64 #if defined (HAVE_UNISTD_H)
66 # include <sys/types.h>
77 #include <readline/readline.h>
78 #include <readline/history.h>
80 #include "../bashintl.h"
83 #include "../bashline.h"
84 #include "bashgetopt.h"
87 static int query_bindings __P((char *));
88 static int unbind_command __P((char *));
90 extern int no_line_editing;
92 #define BIND_RETURN(x) do { return_code = x; goto bind_exit; } while (0)
101 #define PPFLAG 0x0080
102 #define VVFLAG 0x0100
104 #define SSFLAG 0x0400
113 Keymap kmap, saved_keymap;
115 char *initfile, *map_name, *fun_name, *unbind_name, *remove_seq, *cmd_seq;
120 builtin_error (_("line editing not enabled"));
121 return (EXECUTION_FAILURE);
123 builtin_warning (_("line editing not enabled"));
127 kmap = saved_keymap = (Keymap) NULL;
129 initfile = map_name = fun_name = unbind_name = remove_seq = (char *)NULL;
130 return_code = EXECUTION_SUCCESS;
132 if (bash_readline_initialized == 0)
133 initialize_readline ();
135 begin_unwind_frame ("bind_builtin");
136 unwind_protect_var (rl_outstream);
138 rl_outstream = stdout;
140 reset_internal_getopt ();
141 while ((opt = internal_getopt (list, "lvpVPsSf:q:u:m:r:x:")) != EOF)
156 initfile = list_optarg;
160 map_name = list_optarg;
164 fun_name = list_optarg;
168 unbind_name = list_optarg;
172 remove_seq = list_optarg;
188 cmd_seq = list_optarg;
192 BIND_RETURN (EX_USAGE);
198 /* First, see if we need to install a special keymap for this
199 command. Then start on the arguments. */
201 if ((flags & MFLAG) && map_name)
203 kmap = rl_get_keymap_by_name (map_name);
206 builtin_error (_("`%s': invalid keymap name"), map_name);
207 BIND_RETURN (EXECUTION_FAILURE);
213 saved_keymap = rl_get_keymap ();
214 rl_set_keymap (kmap);
217 /* XXX - we need to add exclusive use tests here. It doesn't make sense
218 to use some of these options together. */
219 /* Now hack the option arguments */
221 rl_list_funmap_names ();
224 rl_function_dumper (1);
227 rl_function_dumper (0);
236 rl_variable_dumper (1);
239 rl_variable_dumper (0);
241 if ((flags & FFLAG) && initfile)
243 if (rl_read_init_file (initfile) != 0)
245 builtin_error (_("%s: cannot read: %s"), initfile, strerror (errno));
246 BIND_RETURN (EXECUTION_FAILURE);
250 if ((flags & QFLAG) && fun_name)
251 return_code = query_bindings (fun_name);
253 if ((flags & UFLAG) && unbind_name)
254 return_code = unbind_command (unbind_name);
256 if ((flags & RFLAG) && remove_seq)
258 if (rl_bind_keyseq (remove_seq, (rl_command_func_t *)NULL) != 0)
260 builtin_error (_("`%s': cannot unbind"), remove_seq);
261 BIND_RETURN (EXECUTION_FAILURE);
266 return_code = bind_keyseq_to_unix_command (cmd_seq);
268 /* Process the rest of the arguments as binding specifications. */
271 rl_parse_and_bind (list->word->word);
277 rl_set_keymap (saved_keymap);
279 run_unwind_frame ("bind_builtin");
281 return (sh_chkwrite (return_code));
285 query_bindings (name)
288 rl_command_func_t *function;
292 function = rl_named_function (name);
295 builtin_error (_("`%s': unknown function name"), name);
296 return EXECUTION_FAILURE;
299 keyseqs = rl_invoking_keyseqs (function);
303 printf (_("%s is not bound to any keys.\n"), name);
304 return EXECUTION_FAILURE;
307 printf (_("%s can be invoked via "), name);
308 for (j = 0; j < 5 && keyseqs[j]; j++)
309 printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n");
312 strvec_dispose (keyseqs);
313 return EXECUTION_SUCCESS;
317 unbind_command (name)
320 rl_command_func_t *function;
322 function = rl_named_function (name);
325 builtin_error (_("`%s': unknown function name"), name);
326 return EXECUTION_FAILURE;
329 rl_unbind_function_in_map (function, rl_get_keymap ());
330 return EXECUTION_SUCCESS;
332 #endif /* READLINE */