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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
26 $FUNCTION bind_builtin
27 $SHORT_DOC bind [-lvd] [-m keymap] [-f filename] [-q name] [keyseq:readline-function]
28 Bind a key sequence to a Readline function, or to a macro. The
29 syntax is equivalent to that found in ~/.inputrc, but must be
30 passed as a single argument: bind '"\C-x\C-r": re-read-init-file'.
32 -m keymap Use `keymap' as the keymap for the duration of this
33 command. Acceptable keymap names are emacs,
34 emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move,
35 vi-command, and vi-insert.
36 -l List names of functions.
37 -v List function names and bindings.
38 -d Dump functions and bindings such that they
40 -f filename Read key bindings from FILENAME.
41 -q function-name Query about which keys invoke the named function.
46 #if defined (READLINE)
51 #include <readline/readline.h>
52 #include <readline/history.h>
53 #include "bashgetopt.h"
55 static int query_bindings ();
57 extern int bash_readline_initialized;
58 extern int no_line_editing;
60 #define BIND_RETURN(x) do { return_code = x; goto bind_exit; } while (0)
62 #define USAGE "usage: bind [-lvd] [-m keymap] [-f filename] [-q name] [keyseq:readline_func]"
68 int return_code = EXECUTION_SUCCESS;
69 FILE *old_rl_outstream;
70 Keymap kmap, saved_keymap;
71 int lflag, dflag, fflag, vflag, qflag, mflag, opt;
72 char *initfile, *map_name, *fun_name;
75 return (EXECUTION_FAILURE);
77 kmap = saved_keymap = (Keymap) NULL;
78 lflag = dflag = vflag = fflag = qflag = mflag = 0;
79 initfile = map_name = fun_name = (char *)NULL;
81 if (!bash_readline_initialized)
82 initialize_readline ();
84 /* Cannot use unwind_protect_pointer () on "FILE *", it is only
85 guaranteed to work for strings. */
86 /* XXX -- see if we can use unwind_protect here */
87 old_rl_outstream = rl_outstream;
88 rl_outstream = stdout;
90 reset_internal_getopt ();
91 while ((opt = internal_getopt (list, "lvdf:q:m:")) != EOF)
109 initfile = list_optarg;
114 map_name = list_optarg;
119 fun_name = list_optarg;
123 builtin_error (USAGE);
124 BIND_RETURN (EX_USAGE);
130 /* First, see if we need to install a special keymap for this
131 command. Then start on the arguments. */
133 if (mflag && map_name)
135 kmap = rl_get_keymap_by_name (map_name);
138 builtin_error ("`%s': illegal keymap name", map_name);
139 BIND_RETURN (EXECUTION_FAILURE);
145 saved_keymap = rl_get_keymap ();
146 rl_set_keymap (kmap);
149 /* XXX - we need to add exclusive use tests here. It doesn't make sense
150 to use some of these options together. */
151 /* Now hack the option arguments */
153 rl_list_funmap_names (0);
156 rl_function_dumper (0);
159 rl_function_dumper (1);
161 if (fflag && initfile)
163 if (rl_read_init_file (initfile) != 0)
165 builtin_error ("cannot read %s: %s", initfile, strerror (errno));
166 BIND_RETURN (EXECUTION_FAILURE);
170 if (qflag && fun_name)
171 return_code = query_bindings (fun_name);
173 /* Process the rest of the arguments as binding specifications. */
176 rl_parse_and_bind (list->word->word);
182 rl_set_keymap (saved_keymap);
184 rl_outstream = old_rl_outstream;
185 return (return_code);
189 query_bindings (name)
196 function = rl_named_function (name);
199 builtin_error ("unknown function name `%s'", name);
200 return EXECUTION_FAILURE;
203 keyseqs = rl_invoking_keyseqs (function);
207 printf ("%s is not bound to any keys.\n", name);
208 return EXECUTION_FAILURE;
211 printf ("%s can be invoked via ", name);
212 for (j = 0; j < 5 && keyseqs[j]; j++)
213 printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n");
216 free_array (keyseqs);
217 return EXECUTION_SUCCESS;
219 #endif /* READLINE */