1 This file is bind.def, from which is created bind.c.
2 It implements the builtin "bind" in Bash.
4 Copyright (C) 1987-2002 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 it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
28 $FUNCTION bind_builtin
29 $SHORT_DOC bind [-lpvsPVS] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
30 Bind a key sequence to a Readline function or a macro, or set
31 a Readline variable. The non-option argument syntax is equivalent
32 to that found in ~/.inputrc, but must be passed as a single argument:
33 bind '"\C-x\C-r": re-read-init-file'.
34 bind accepts the following options:
35 -m keymap Use `keymap' as the keymap for the duration of this
36 command. Acceptable keymap names are emacs,
37 emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move,
38 vi-command, and vi-insert.
39 -l List names of functions.
40 -P List function names and bindings.
41 -p List functions and bindings in a form that can be
43 -r keyseq Remove the binding for KEYSEQ.
44 -x keyseq:shell-command Cause SHELL-COMMAND to be executed when
46 -f filename Read key bindings from FILENAME.
47 -q function-name Query about which keys invoke the named function.
48 -u function-name Unbind all keys which are bound to the named function.
49 -V List variable names and values
50 -v List variable names and values in a form that can
52 -S List key sequences that invoke macros and their values
53 -s List key sequences that invoke macros and their values
54 in a form that can be reused as input.
57 #if defined (READLINE)
59 #if defined (HAVE_UNISTD_H)
61 # include <sys/types.h>
72 #include <readline/readline.h>
73 #include <readline/history.h>
76 #include "../bashline.h"
77 #include "bashgetopt.h"
80 static int query_bindings __P((char *));
81 static int unbind_command __P((char *));
83 extern int no_line_editing;
85 #define BIND_RETURN(x) do { return_code = x; goto bind_exit; } while (0)
106 Keymap kmap, saved_keymap;
108 char *initfile, *map_name, *fun_name, *unbind_name, *remove_seq, *cmd_seq;
111 return (EXECUTION_FAILURE);
113 kmap = saved_keymap = (Keymap) NULL;
115 initfile = map_name = fun_name = unbind_name = remove_seq = (char *)NULL;
116 return_code = EXECUTION_SUCCESS;
118 if (!bash_readline_initialized)
119 initialize_readline ();
121 begin_unwind_frame ("bind_builtin");
122 unwind_protect_var (rl_outstream);
124 rl_outstream = stdout;
126 reset_internal_getopt ();
127 while ((opt = internal_getopt (list, "lvpVPsSf:q:u:m:r:x:")) != EOF)
142 initfile = list_optarg;
146 map_name = list_optarg;
150 fun_name = list_optarg;
154 unbind_name = list_optarg;
158 remove_seq = list_optarg;
174 cmd_seq = list_optarg;
178 BIND_RETURN (EX_USAGE);
184 /* First, see if we need to install a special keymap for this
185 command. Then start on the arguments. */
187 if ((flags & MFLAG) && map_name)
189 kmap = rl_get_keymap_by_name (map_name);
192 builtin_error ("`%s': invalid keymap name", map_name);
193 BIND_RETURN (EXECUTION_FAILURE);
199 saved_keymap = rl_get_keymap ();
200 rl_set_keymap (kmap);
203 /* XXX - we need to add exclusive use tests here. It doesn't make sense
204 to use some of these options together. */
205 /* Now hack the option arguments */
207 rl_list_funmap_names ();
210 rl_function_dumper (1);
213 rl_function_dumper (0);
222 rl_variable_dumper (1);
225 rl_variable_dumper (0);
227 if ((flags & FFLAG) && initfile)
229 if (rl_read_init_file (initfile) != 0)
231 builtin_error ("%s: cannot read: %s", initfile, strerror (errno));
232 BIND_RETURN (EXECUTION_FAILURE);
236 if ((flags & QFLAG) && fun_name)
237 return_code = query_bindings (fun_name);
239 if ((flags & UFLAG) && unbind_name)
240 return_code = unbind_command (unbind_name);
242 if ((flags & RFLAG) && remove_seq)
244 if (rl_set_key (remove_seq, (rl_command_func_t *)NULL, rl_get_keymap ()) != 0)
246 builtin_error ("`%s': cannot unbind", remove_seq);
247 BIND_RETURN (EXECUTION_FAILURE);
252 return_code = bind_keyseq_to_unix_command (cmd_seq);
254 /* Process the rest of the arguments as binding specifications. */
257 rl_parse_and_bind (list->word->word);
263 rl_set_keymap (saved_keymap);
265 run_unwind_frame ("bind_builtin");
267 return (return_code);
271 query_bindings (name)
274 rl_command_func_t *function;
278 function = rl_named_function (name);
281 builtin_error ("`%s': unknown function name", name);
282 return EXECUTION_FAILURE;
285 keyseqs = rl_invoking_keyseqs (function);
289 printf ("%s is not bound to any keys.\n", name);
290 return EXECUTION_FAILURE;
293 printf ("%s can be invoked via ", name);
294 for (j = 0; j < 5 && keyseqs[j]; j++)
295 printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n");
298 strvec_dispose (keyseqs);
299 return EXECUTION_SUCCESS;
303 unbind_command (name)
306 rl_command_func_t *function;
308 function = rl_named_function (name);
311 builtin_error ("`%s': unknown function name", name);
312 return EXECUTION_FAILURE;
315 rl_unbind_function_in_map (function, rl_get_keymap ());
316 return EXECUTION_SUCCESS;
318 #endif /* READLINE */