1 This file is bind.def, from which is created bind.c.
2 It implements the builtin "bind" in Bash.
4 Copyright (C) 1987, 1989, 1991 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]
30 Bind a key sequence to a Readline function, or to a macro. The
31 syntax is equivalent to that found in ~/.inputrc, but must be
32 passed as a single argument: bind '"\C-x\C-r": re-read-init-file'.
34 -m keymap Use `keymap' as the keymap for the duration of this
35 command. Acceptable keymap names are emacs,
36 emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move,
37 vi-command, and vi-insert.
38 -l List names of functions.
39 -P List function names and bindings.
40 -p List functions and bindings in a form that can be
42 -r keyseq Remove the binding for KEYSEQ.
43 -x keyseq:shell-command Cause SHELL-COMMAND to be executed when KEYSEQ
45 -f filename Read key bindings from FILENAME.
46 -q function-name Query about which keys invoke the named function.
47 -u function-name Unbind all keys which are bound to the named function.
48 -V List variable names and values
49 -v List variable names and values in a form that can
51 -S List key sequences that invoke macros and their values
52 -s List key sequences that invoke macros and their values in
53 a form that can be reused as input.
56 #if defined (READLINE)
58 #if defined (HAVE_UNISTD_H)
60 # include <sys/types.h>
71 #include <readline/readline.h>
72 #include <readline/history.h>
75 #include "../bashline.h"
76 #include "bashgetopt.h"
79 static int query_bindings __P((char *));
80 static int unbind_command __P((char *));
82 extern int no_line_editing;
84 #define BIND_RETURN(x) do { return_code = x; goto bind_exit; } while (0)
105 Keymap kmap, saved_keymap;
107 char *initfile, *map_name, *fun_name, *unbind_name, *remove_seq, *cmd_seq;
110 return (EXECUTION_FAILURE);
112 kmap = saved_keymap = (Keymap) NULL;
114 initfile = map_name = fun_name = unbind_name = remove_seq = (char *)NULL;
115 return_code = EXECUTION_SUCCESS;
117 if (!bash_readline_initialized)
118 initialize_readline ();
120 begin_unwind_frame ("bind_builtin");
121 unwind_protect_var (rl_outstream);
123 rl_outstream = stdout;
125 reset_internal_getopt ();
126 while ((opt = internal_getopt (list, "lvpVPsSf:q:u:m:r:x:")) != EOF)
141 initfile = list_optarg;
145 map_name = list_optarg;
149 fun_name = list_optarg;
153 unbind_name = list_optarg;
157 remove_seq = list_optarg;
173 cmd_seq = list_optarg;
177 BIND_RETURN (EX_USAGE);
183 /* First, see if we need to install a special keymap for this
184 command. Then start on the arguments. */
186 if ((flags & MFLAG) && map_name)
188 kmap = rl_get_keymap_by_name (map_name);
191 builtin_error ("`%s': invalid keymap name", map_name);
192 BIND_RETURN (EXECUTION_FAILURE);
198 saved_keymap = rl_get_keymap ();
199 rl_set_keymap (kmap);
202 /* XXX - we need to add exclusive use tests here. It doesn't make sense
203 to use some of these options together. */
204 /* Now hack the option arguments */
206 rl_list_funmap_names ();
209 rl_function_dumper (1);
212 rl_function_dumper (0);
221 rl_variable_dumper (1);
224 rl_variable_dumper (0);
226 if ((flags & FFLAG) && initfile)
228 if (rl_read_init_file (initfile) != 0)
230 builtin_error ("cannot read %s: %s", initfile, strerror (errno));
231 BIND_RETURN (EXECUTION_FAILURE);
235 if ((flags & QFLAG) && fun_name)
236 return_code = query_bindings (fun_name);
238 if ((flags & UFLAG) && unbind_name)
239 return_code = unbind_command (unbind_name);
241 if ((flags & RFLAG) && remove_seq)
243 if (rl_set_key (remove_seq, (rl_command_func_t *)NULL, rl_get_keymap ()) != 0)
245 builtin_error ("cannot unbind %s", remove_seq);
246 BIND_RETURN (EXECUTION_FAILURE);
251 return_code = bind_keyseq_to_unix_command (cmd_seq);
253 /* Process the rest of the arguments as binding specifications. */
256 rl_parse_and_bind (list->word->word);
262 rl_set_keymap (saved_keymap);
264 run_unwind_frame ("bind_builtin");
266 return (return_code);
270 query_bindings (name)
273 rl_command_func_t *function;
277 function = rl_named_function (name);
280 builtin_error ("unknown function name `%s'", name);
281 return EXECUTION_FAILURE;
284 keyseqs = rl_invoking_keyseqs (function);
288 printf ("%s is not bound to any keys.\n", name);
289 return EXECUTION_FAILURE;
292 printf ("%s can be invoked via ", name);
293 for (j = 0; j < 5 && keyseqs[j]; j++)
294 printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n");
297 free_array (keyseqs);
298 return EXECUTION_SUCCESS;
302 unbind_command (name)
305 rl_command_func_t *function;
307 function = rl_named_function (name);
310 builtin_error ("unknown function name `%s'", name);
311 return EXECUTION_FAILURE;
314 rl_unbind_function_in_map (function, rl_get_keymap ());
315 return EXECUTION_SUCCESS;
317 #endif /* READLINE */