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 ();
80 static int unbind_command ();
82 extern int no_line_editing;
84 #define BIND_RETURN(x) do { return_code = x; goto bind_exit; } while (0)
105 FILE *old_rl_outstream;
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 /* Cannot use unwind_protect_pointer () on "FILE *", it is only
122 guaranteed to work for strings. */
123 /* XXX -- see if we can use unwind_protect here */
124 old_rl_outstream = rl_outstream;
125 rl_outstream = stdout;
127 reset_internal_getopt ();
128 while ((opt = internal_getopt (list, "lvpVPsSf:q:u:m:r:x:")) != EOF)
143 initfile = list_optarg;
147 map_name = list_optarg;
151 fun_name = list_optarg;
155 unbind_name = list_optarg;
159 remove_seq = list_optarg;
175 cmd_seq = list_optarg;
179 BIND_RETURN (EX_USAGE);
185 /* First, see if we need to install a special keymap for this
186 command. Then start on the arguments. */
188 if ((flags & MFLAG) && map_name)
190 kmap = rl_get_keymap_by_name (map_name);
193 builtin_error ("`%s': invalid keymap name", map_name);
194 BIND_RETURN (EXECUTION_FAILURE);
200 saved_keymap = rl_get_keymap ();
201 rl_set_keymap (kmap);
204 /* XXX - we need to add exclusive use tests here. It doesn't make sense
205 to use some of these options together. */
206 /* Now hack the option arguments */
208 rl_list_funmap_names ();
211 rl_function_dumper (1);
214 rl_function_dumper (0);
223 rl_variable_dumper (1);
226 rl_variable_dumper (0);
228 if ((flags & FFLAG) && initfile)
230 if (rl_read_init_file (initfile) != 0)
232 builtin_error ("cannot read %s: %s", initfile, strerror (errno));
233 BIND_RETURN (EXECUTION_FAILURE);
237 if ((flags & QFLAG) && fun_name)
238 return_code = query_bindings (fun_name);
240 if ((flags & UFLAG) && unbind_name)
241 return_code = unbind_command (unbind_name);
243 if ((flags & RFLAG) && remove_seq)
245 if (rl_set_key (remove_seq, (Function *)NULL, rl_get_keymap ()) != 0)
247 builtin_error ("cannot unbind %s", remove_seq);
248 BIND_RETURN (EXECUTION_FAILURE);
253 return_code = bind_keyseq_to_unix_command (cmd_seq);
255 /* Process the rest of the arguments as binding specifications. */
258 rl_parse_and_bind (list->word->word);
264 rl_set_keymap (saved_keymap);
266 rl_outstream = old_rl_outstream;
267 return (return_code);
271 query_bindings (name)
278 function = rl_named_function (name);
281 builtin_error ("unknown function name `%s'", 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 free_array (keyseqs);
299 return EXECUTION_SUCCESS;
303 unbind_command (name)
308 function = rl_named_function (name);
311 builtin_error ("unknown function name `%s'", name);
312 return EXECUTION_FAILURE;
315 rl_unbind_function_in_map (function, rl_get_keymap ());
316 return EXECUTION_SUCCESS;
318 #endif /* READLINE */