1 This file is bind.def, from which is created bind.c.
2 It implements the builtin "bind" in Bash.
4 Copyright (C) 1987-2003 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>
75 #include "../bashintl.h"
78 #include "../bashline.h"
79 #include "bashgetopt.h"
82 static int query_bindings __P((char *));
83 static int unbind_command __P((char *));
85 extern int no_line_editing;
87 #define BIND_RETURN(x) do { return_code = x; goto bind_exit; } while (0)
108 Keymap kmap, saved_keymap;
110 char *initfile, *map_name, *fun_name, *unbind_name, *remove_seq, *cmd_seq;
113 return (EXECUTION_FAILURE);
115 kmap = saved_keymap = (Keymap) NULL;
117 initfile = map_name = fun_name = unbind_name = remove_seq = (char *)NULL;
118 return_code = EXECUTION_SUCCESS;
120 if (!bash_readline_initialized)
121 initialize_readline ();
123 begin_unwind_frame ("bind_builtin");
124 unwind_protect_var (rl_outstream);
126 rl_outstream = stdout;
128 reset_internal_getopt ();
129 while ((opt = internal_getopt (list, "lvpVPsSf:q:u:m:r:x:")) != EOF)
144 initfile = list_optarg;
148 map_name = list_optarg;
152 fun_name = list_optarg;
156 unbind_name = list_optarg;
160 remove_seq = list_optarg;
176 cmd_seq = list_optarg;
180 BIND_RETURN (EX_USAGE);
186 /* First, see if we need to install a special keymap for this
187 command. Then start on the arguments. */
189 if ((flags & MFLAG) && map_name)
191 kmap = rl_get_keymap_by_name (map_name);
194 builtin_error (_("`%s': invalid keymap name"), map_name);
195 BIND_RETURN (EXECUTION_FAILURE);
201 saved_keymap = rl_get_keymap ();
202 rl_set_keymap (kmap);
205 /* XXX - we need to add exclusive use tests here. It doesn't make sense
206 to use some of these options together. */
207 /* Now hack the option arguments */
209 rl_list_funmap_names ();
212 rl_function_dumper (1);
215 rl_function_dumper (0);
224 rl_variable_dumper (1);
227 rl_variable_dumper (0);
229 if ((flags & FFLAG) && initfile)
231 if (rl_read_init_file (initfile) != 0)
233 builtin_error (_("%s: cannot read: %s"), initfile, strerror (errno));
234 BIND_RETURN (EXECUTION_FAILURE);
238 if ((flags & QFLAG) && fun_name)
239 return_code = query_bindings (fun_name);
241 if ((flags & UFLAG) && unbind_name)
242 return_code = unbind_command (unbind_name);
244 if ((flags & RFLAG) && remove_seq)
246 if (rl_set_key (remove_seq, (rl_command_func_t *)NULL, rl_get_keymap ()) != 0)
248 builtin_error (_("`%s': cannot unbind"), remove_seq);
249 BIND_RETURN (EXECUTION_FAILURE);
254 return_code = bind_keyseq_to_unix_command (cmd_seq);
256 /* Process the rest of the arguments as binding specifications. */
259 rl_parse_and_bind (list->word->word);
265 rl_set_keymap (saved_keymap);
267 run_unwind_frame ("bind_builtin");
269 return (return_code);
273 query_bindings (name)
276 rl_command_func_t *function;
280 function = rl_named_function (name);
283 builtin_error (_("`%s': unknown function name"), name);
284 return EXECUTION_FAILURE;
287 keyseqs = rl_invoking_keyseqs (function);
291 printf (_("%s is not bound to any keys.\n"), name);
292 return EXECUTION_FAILURE;
295 printf (_("%s can be invoked via "), name);
296 for (j = 0; j < 5 && keyseqs[j]; j++)
297 printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n");
300 strvec_dispose (keyseqs);
301 return EXECUTION_SUCCESS;
305 unbind_command (name)
308 rl_command_func_t *function;
310 function = rl_named_function (name);
313 builtin_error ("`%s': unknown function name", name);
314 return EXECUTION_FAILURE;
317 rl_unbind_function_in_map (function, rl_get_keymap ());
318 return EXECUTION_SUCCESS;
320 #endif /* READLINE */