1 /* bind.c -- key binding and startup file support for the readline library. */
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
31 #if defined (HAVE_SYS_FILE_H)
32 # include <sys/file.h>
33 #endif /* HAVE_SYS_FILE_H */
35 #if defined (HAVE_UNISTD_H)
37 #endif /* HAVE_UNISTD_H */
39 #if defined (HAVE_STDLIB_H)
42 # include "ansi_stdlib.h"
43 #endif /* HAVE_STDLIB_H */
51 #include "posixstat.h"
53 /* System-specific feature definitions and include files. */
56 /* Some standard library routines. */
60 #include "rlprivate.h"
64 #if !defined (strchr) && !defined (__STDC__)
65 extern char *strchr (), *strrchr ();
66 #endif /* !strchr && !__STDC__ */
68 /* Variables exported by this file. */
69 Keymap rl_binding_keymap;
71 static int _rl_read_init_file __P((const char *, int));
72 static int glean_key_from_name __P((char *));
73 static int substring_member_of_array __P((char *, const char **));
75 static int currently_reading_init_file;
77 /* used only in this file */
78 static int _rl_prefer_visible_bell = 1;
80 /* **************************************************************** */
84 /* **************************************************************** */
86 /* rl_add_defun (char *name, rl_command_func_t *function, int key)
87 Add NAME to the list of named functions. Make FUNCTION be the function
88 that gets called. If KEY is not -1, then bind it. */
90 rl_add_defun (name, function, key)
92 rl_command_func_t *function;
96 rl_bind_key (key, function);
97 rl_add_funmap_entry (name, function);
101 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
103 rl_bind_key (key, function)
105 rl_command_func_t *function;
110 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
112 if (_rl_keymap[ESC].type == ISKMAP)
116 escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
118 escmap[key].type = ISFUNC;
119 escmap[key].function = function;
125 _rl_keymap[key].type = ISFUNC;
126 _rl_keymap[key].function = function;
127 rl_binding_keymap = _rl_keymap;
131 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
134 rl_bind_key_in_map (key, function, map)
136 rl_command_func_t *function;
144 result = rl_bind_key (key, function);
149 /* Make KEY do nothing in the currently selected keymap.
150 Returns non-zero in case of error. */
155 return (rl_bind_key (key, (rl_command_func_t *)NULL));
158 /* Make KEY do nothing in MAP.
159 Returns non-zero in case of error. */
161 rl_unbind_key_in_map (key, map)
165 return (rl_bind_key_in_map (key, (rl_command_func_t *)NULL, map));
168 /* Unbind all keys bound to FUNCTION in MAP. */
170 rl_unbind_function_in_map (func, map)
171 rl_command_func_t *func;
174 register int i, rval;
176 for (i = rval = 0; i < KEYMAP_SIZE; i++)
178 if (map[i].type == ISFUNC && map[i].function == func)
180 map[i].function = (rl_command_func_t *)NULL;
188 rl_unbind_command_in_map (command, map)
192 rl_command_func_t *func;
194 func = rl_named_function (command);
197 return (rl_unbind_function_in_map (func, map));
200 /* Bind the key sequence represented by the string KEYSEQ to
201 FUNCTION. This makes new keymaps as necessary. The initial
202 place to do bindings is in MAP. */
204 rl_set_key (keyseq, function, map)
206 rl_command_func_t *function;
209 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
212 /* Bind the key sequence represented by the string KEYSEQ to
213 the string of characters MACRO. This makes new keymaps as
214 necessary. The initial place to do bindings is in MAP. */
216 rl_macro_bind (keyseq, macro, map)
217 const char *keyseq, *macro;
223 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
225 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
230 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
234 /* Bind the key sequence represented by the string KEYSEQ to
235 the arbitrary pointer DATA. TYPE says what kind of data is
236 pointed to by DATA, right now this can be a function (ISFUNC),
237 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
238 as necessary. The initial place to do bindings is in MAP. */
240 rl_generic_bind (type, keyseq, data, map)
250 /* If no keys to bind to, exit right away. */
251 if (!keyseq || !*keyseq)
258 keys = xmalloc (1 + (2 * strlen (keyseq)));
260 /* Translate the ASCII representation of KEYSEQ into an array of
261 characters. Stuff the characters into KEYS, and the length of
262 KEYS into KEYS_LEN. */
263 if (rl_translate_keyseq (keyseq, keys, &keys_len))
269 /* Bind keys, making new keymaps as necessary. */
270 for (i = 0; i < keys_len; i++)
272 int ic = (int) ((unsigned char)keys[i]);
274 if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
277 if (map[ESC].type == ISKMAP)
278 map = FUNCTION_TO_KEYMAP (map, ESC);
281 if ((i + 1) < keys_len)
283 if (map[ic].type != ISKMAP)
285 if (map[ic].type == ISMACR)
286 free ((char *)map[ic].function);
288 map[ic].type = ISKMAP;
289 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
291 map = FUNCTION_TO_KEYMAP (map, ic);
295 if (map[ic].type == ISMACR)
296 free ((char *)map[ic].function);
298 map[ic].function = KEYMAP_TO_FUNCTION (data);
302 rl_binding_keymap = map;
308 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
309 an array of characters. LEN gets the final length of ARRAY. Return
310 non-zero if there was an error parsing SEQ. */
312 rl_translate_keyseq (seq, array, len)
317 register int i, c, l, temp;
319 for (i = l = 0; c = seq[i]; i++)
328 /* Handle \C- and \M- prefixes. */
329 if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
331 /* Handle special case of backwards define. */
332 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
336 array[l++] = CTRL (_rl_to_upper (seq[i]));
343 array[l++] = ESC; /* XXX */
348 /* Special hack for C-?... */
349 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
354 /* Translate other backslash-escaped characters. These are the
355 same escape sequences that bash's `echo' and `printf' builtins
356 handle, with the addition of \d -> RUBOUT. A backslash
357 preceding a character that is not special is stripped. */
367 array[l++] = RUBOUT; /* readline-specific */
376 array[l++] = NEWLINE;
390 case '0': case '1': case '2': case '3':
391 case '4': case '5': case '6': case '7':
393 for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++)
394 c = (c * 8) + OCTVALUE (seq[i]);
395 i--; /* auto-increment in for loop */
396 array[l++] = c % (largest_char + 1);
400 for (temp = 3, c = 0; isxdigit (seq[i]) && temp--; i++)
401 c = (c * 16) + HEXVALUE (seq[i]);
404 i--; /* auto-increment in for loop */
405 array[l++] = c % (largest_char + 1);
407 default: /* backslashes before non-special chars just add the char */
409 break; /* the backslash is stripped */
423 rl_untranslate_keyseq (seq)
426 static char kseq[16];
438 else if (CTRL_CHAR (c))
443 c = _rl_to_lower (UNCTRL (c));
445 else if (c == RUBOUT)
458 else if (c == '\\' || c == '"')
463 kseq[i++] = (unsigned char) c;
469 _rl_untranslate_macro_value (seq)
475 r = ret = xmalloc (7 * strlen (seq) + 1);
476 for (s = seq; *s; s++)
486 else if (CTRL_CHAR (c) && c != ESC)
491 c = _rl_to_lower (UNCTRL (c));
493 else if (c == RUBOUT)
506 else if (c == '\\' || c == '"')
509 *r++ = (unsigned char)c;
515 /* Return a pointer to the function that STRING represents.
516 If STRING doesn't have a matching function, then a NULL pointer
519 rl_named_function (string)
524 rl_initialize_funmap ();
526 for (i = 0; funmap[i]; i++)
527 if (_rl_stricmp (funmap[i]->name, string) == 0)
528 return (funmap[i]->function);
529 return ((rl_command_func_t *)NULL);
532 /* Return the function (or macro) definition which would be invoked via
533 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
534 used. TYPE, if non-NULL, is a pointer to an int which will receive the
535 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
536 or ISMACR (macro). */
538 rl_function_of_keyseq (keyseq, map, type)
548 for (i = 0; keyseq && keyseq[i]; i++)
552 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
554 if (map[ESC].type != ISKMAP)
557 *type = map[ESC].type;
559 return (map[ESC].function);
563 map = FUNCTION_TO_KEYMAP (map, ESC);
568 if (map[ic].type == ISKMAP)
570 /* If this is the last key in the key sequence, return the
577 return (map[ic].function);
580 map = FUNCTION_TO_KEYMAP (map, ic);
585 *type = map[ic].type;
587 return (map[ic].function);
590 return ((rl_command_func_t *) NULL);
593 /* The last key bindings file read. */
594 static char *last_readline_init_file = (char *)NULL;
596 /* The file we're currently reading key bindings from. */
597 static const char *current_readline_init_file;
598 static int current_readline_init_include_level;
599 static int current_readline_init_lineno;
601 /* Read FILENAME into a locally-allocated buffer and return the buffer.
602 The size of the buffer is returned in *SIZEP. Returns NULL if any
603 errors were encountered. */
605 _rl_read_file (filename, sizep)
614 if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0)
615 return ((char *)NULL);
617 file_size = (size_t)finfo.st_size;
619 /* check for overflow on very large files */
620 if (file_size != finfo.st_size || file_size + 1 < file_size)
627 return ((char *)NULL);
630 /* Read the file into BUFFER. */
631 buffer = (char *)xmalloc (file_size + 1);
632 i = read (file, buffer, file_size);
642 return ((char *)NULL);
646 buffer[file_size] = '\0';
658 /* Re-read the current keybindings file. */
660 rl_re_read_init_file (count, ignore)
664 r = rl_read_init_file ((const char *)NULL);
665 rl_set_keymap_from_edit_mode ();
669 /* Do key bindings from a file. If FILENAME is NULL it defaults
670 to the first non-null filename from this list:
671 1. the filename used for the previous call
672 2. the value of the shell variable `INPUTRC'
674 If the file existed and could be opened and read, 0 is returned,
675 otherwise errno is returned. */
677 rl_read_init_file (filename)
678 const char *filename;
680 /* Default the filename. */
683 filename = last_readline_init_file;
685 filename = sh_get_env_value ("INPUTRC");
687 filename = DEFAULT_INPUTRC;
691 filename = DEFAULT_INPUTRC;
693 #if defined (__MSDOS__)
694 if (_rl_read_init_file (filename, 0) == 0)
696 filename = "~/_inputrc";
698 return (_rl_read_init_file (filename, 0));
702 _rl_read_init_file (filename, include_level)
703 const char *filename;
707 char *buffer, *openname, *line, *end;
710 current_readline_init_file = filename;
711 current_readline_init_include_level = include_level;
713 openname = tilde_expand (filename);
714 buffer = _rl_read_file (openname, &file_size);
720 if (include_level == 0 && filename != last_readline_init_file)
722 FREE (last_readline_init_file);
723 last_readline_init_file = savestring (filename);
726 currently_reading_init_file = 1;
728 /* Loop over the lines in the file. Lines that start with `#' are
729 comments; all other lines are commands for readline initialization. */
730 current_readline_init_lineno = 1;
732 end = buffer + file_size;
735 /* Find the end of this line. */
736 for (i = 0; line + i != end && line[i] != '\n'; i++);
738 #if defined (__CYGWIN__)
739 /* ``Be liberal in what you accept.'' */
740 if (line[i] == '\n' && line[i-1] == '\r')
744 /* Mark end of line. */
747 /* Skip leading whitespace. */
748 while (*line && whitespace (*line))
754 /* If the line is not a comment, then parse it. */
755 if (*line && *line != '#')
756 rl_parse_and_bind (line);
758 /* Move to the next line. */
760 current_readline_init_lineno++;
764 currently_reading_init_file = 0;
769 _rl_init_file_error (msg)
772 if (currently_reading_init_file)
773 fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
774 current_readline_init_lineno, msg);
776 fprintf (stderr, "readline: %s\n", msg);
779 /* **************************************************************** */
781 /* Parser Directives */
783 /* **************************************************************** */
785 typedef int _rl_parser_func_t __P((char *));
787 /* Things that mean `Control'. */
788 const char *_rl_possible_control_prefixes[] = {
789 "Control-", "C-", "CTRL-", (const char *)NULL
792 const char *_rl_possible_meta_prefixes[] = {
793 "Meta", "M-", (const char *)NULL
798 /* Calling programs set this to have their argv[0]. */
799 const char *rl_readline_name = "other";
801 /* Stack of previous values of parsing_conditionalized_out. */
802 static unsigned char *if_stack = (unsigned char *)NULL;
803 static int if_stack_depth;
804 static int if_stack_size;
806 /* Push _rl_parsing_conditionalized_out, and set parser state based
814 /* Push parser state. */
815 if (if_stack_depth + 1 >= if_stack_size)
818 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
820 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
822 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
824 /* If parsing is turned off, then nothing can turn it back on except
825 for finding the matching endif. In that case, return right now. */
826 if (_rl_parsing_conditionalized_out)
829 /* Isolate first argument. */
830 for (i = 0; args[i] && !whitespace (args[i]); i++);
835 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
836 isn't term=foo, or mode=emacs, then check to see if the first
837 word in ARGS is the same as the value stored in rl_readline_name. */
838 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
842 /* Terminals like "aaa-60" are equivalent to "aaa". */
843 tname = savestring (rl_terminal_name);
844 tem = strchr (tname, '-');
848 /* Test the `long' and `short' forms of the terminal name so that
849 if someone has a `sun-cmd' and does not want to have bindings
850 that will be executed if the terminal is a `sun', they can put
851 `$if term=sun-cmd' into their .inputrc. */
852 _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
853 _rl_stricmp (args + 5, rl_terminal_name);
856 #if defined (VI_MODE)
857 else if (_rl_strnicmp (args, "mode=", 5) == 0)
861 if (_rl_stricmp (args + 5, "emacs") == 0)
863 else if (_rl_stricmp (args + 5, "vi") == 0)
868 _rl_parsing_conditionalized_out = mode != rl_editing_mode;
871 /* Check to see if the first word in ARGS is the same as the
872 value stored in rl_readline_name. */
873 else if (_rl_stricmp (args, rl_readline_name) == 0)
874 _rl_parsing_conditionalized_out = 0;
876 _rl_parsing_conditionalized_out = 1;
880 /* Invert the current parser state if there is anything on the stack. */
887 if (if_stack_depth == 0)
889 _rl_init_file_error ("$else found without matching $if");
893 /* Check the previous (n - 1) levels of the stack to make sure that
894 we haven't previously turned off parsing. */
895 for (i = 0; i < if_stack_depth - 1; i++)
896 if (if_stack[i] == 1)
899 /* Invert the state of parsing if at top level. */
900 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
904 /* Terminate a conditional, popping the value of
905 _rl_parsing_conditionalized_out from the stack. */
911 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
913 _rl_init_file_error ("$endif without matching $if");
918 parser_include (args)
921 const char *old_init_file;
923 int old_line_number, old_include_level, r;
925 if (_rl_parsing_conditionalized_out)
928 old_init_file = current_readline_init_file;
929 old_line_number = current_readline_init_lineno;
930 old_include_level = current_readline_init_include_level;
932 e = strchr (args, '\n');
935 r = _rl_read_init_file ((const char *)args, old_include_level + 1);
937 current_readline_init_file = old_init_file;
938 current_readline_init_lineno = old_line_number;
939 current_readline_init_include_level = old_include_level;
944 /* Associate textual names with actual functions. */
947 _rl_parser_func_t *function;
948 } parser_directives [] = {
950 { "endif", parser_endif },
951 { "else", parser_else },
952 { "include", parser_include },
953 { (char *)0x0, (_rl_parser_func_t *)0x0 }
956 /* Handle a parser directive. STATEMENT is the line of the directive
957 without any leading `$'. */
959 handle_parser_directive (statement)
963 char *directive, *args;
965 /* Isolate the actual directive. */
967 /* Skip whitespace. */
968 for (i = 0; whitespace (statement[i]); i++);
970 directive = &statement[i];
972 for (; statement[i] && !whitespace (statement[i]); i++);
975 statement[i++] = '\0';
977 for (; statement[i] && whitespace (statement[i]); i++);
979 args = &statement[i];
981 /* Lookup the command, and act on it. */
982 for (i = 0; parser_directives[i].name; i++)
983 if (_rl_stricmp (directive, parser_directives[i].name) == 0)
985 (*parser_directives[i].function) (args);
989 /* display an error message about the unknown parser directive */
990 _rl_init_file_error ("unknown parser directive");
994 /* Read the binding command from STRING and perform it.
995 A key binding command looks like: Keyname: function-name\0,
996 a variable binding command looks like: set variable value.
997 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
999 rl_parse_and_bind (string)
1002 char *funname, *kname;
1004 int key, equivalency;
1006 while (string && whitespace (*string))
1009 if (!string || !*string || *string == '#')
1012 /* If this is a parser directive, act on it. */
1015 handle_parser_directive (&string[1]);
1019 /* If we aren't supposed to be parsing right now, then we're done. */
1020 if (_rl_parsing_conditionalized_out)
1024 /* If this keyname is a complex key expression surrounded by quotes,
1025 advance to after the matching close quote. This code allows the
1026 backslash to quote characters in the key expression. */
1031 for (i = 1; c = string[i]; i++)
1048 /* If we didn't find a closing quote, abort the line. */
1049 if (string[i] == '\0')
1051 _rl_init_file_error ("no closing `\"' in key binding");
1056 /* Advance to the colon (:) or whitespace which separates the two objects. */
1057 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
1059 equivalency = (c == ':' && string[i + 1] == '=');
1061 /* Mark the end of the command (or keyname). */
1065 /* If doing assignment, skip the '=' sign as well. */
1069 /* If this is a command to set a variable, then do that. */
1070 if (_rl_stricmp (string, "set") == 0)
1072 char *var = string + i;
1075 /* Make VAR point to start of variable name. */
1076 while (*var && whitespace (*var)) var++;
1078 /* Make value point to start of value string. */
1080 while (*value && !whitespace (*value)) value++;
1083 while (*value && whitespace (*value)) value++;
1085 rl_variable_bind (var, value);
1089 /* Skip any whitespace between keyname and funname. */
1090 for (; string[i] && whitespace (string[i]); i++);
1091 funname = &string[i];
1093 /* Now isolate funname.
1094 For straight function names just look for whitespace, since
1095 that will signify the end of the string. But this could be a
1096 macro definition. In that case, the string is quoted, so skip
1097 to the matching delimiter. We allow the backslash to quote the
1098 delimiter characters in the macro body. */
1099 /* This code exists to allow whitespace in macro expansions, which
1100 would otherwise be gobbled up by the next `for' loop.*/
1101 /* XXX - it may be desirable to allow backslash quoting only if " is
1102 the quoted string delimiter, like the shell. */
1103 if (*funname == '\'' || *funname == '"')
1105 int delimiter = string[i++], passc;
1107 for (passc = 0; c = string[i]; i++)
1128 /* Advance to the end of the string. */
1129 for (; string[i] && !whitespace (string[i]); i++);
1131 /* No extra whitespace at the end of the string. */
1134 /* Handle equivalency bindings here. Make the left-hand side be exactly
1135 whatever the right-hand evaluates to, including keymaps. */
1141 /* If this is a new-style key-binding, then do the binding with
1142 rl_set_key (). Otherwise, let the older code deal with it. */
1146 register int j, k, passc;
1148 seq = xmalloc (1 + strlen (string));
1149 for (j = 1, k = passc = 0; string[j]; j++)
1151 /* Allow backslash to quote characters, but leave them in place.
1152 This allows a string to end with a backslash quoting another
1153 backslash, or with a backslash quoting a double quote. The
1154 backslashes are left in place for rl_translate_keyseq (). */
1155 if (passc || (string[j] == '\\'))
1157 seq[k++] = string[j];
1162 if (string[j] == '"')
1165 seq[k++] = string[j];
1169 /* Binding macro? */
1170 if (*funname == '\'' || *funname == '"')
1172 j = strlen (funname);
1174 /* Remove the delimiting quotes from each end of FUNNAME. */
1175 if (j && funname[j - 1] == *funname)
1176 funname[j - 1] = '\0';
1178 rl_macro_bind (seq, &funname[1], _rl_keymap);
1181 rl_set_key (seq, rl_named_function (funname), _rl_keymap);
1187 /* Get the actual character we want to deal with. */
1188 kname = strrchr (string, '-');
1194 key = glean_key_from_name (kname);
1196 /* Add in control and meta bits. */
1197 if (substring_member_of_array (string, _rl_possible_control_prefixes))
1198 key = CTRL (_rl_to_upper (key));
1200 if (substring_member_of_array (string, _rl_possible_meta_prefixes))
1203 /* Temporary. Handle old-style keyname with macro-binding. */
1204 if (*funname == '\'' || *funname == '"')
1206 unsigned char useq[2];
1207 int fl = strlen (funname);
1209 useq[0] = key; useq[1] = '\0';
1210 if (fl && funname[fl - 1] == *funname)
1211 funname[fl - 1] = '\0';
1213 rl_macro_bind (useq, &funname[1], _rl_keymap);
1215 #if defined (PREFIX_META_HACK)
1216 /* Ugly, but working hack to keep prefix-meta around. */
1217 else if (_rl_stricmp (funname, "prefix-meta") == 0)
1223 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1225 #endif /* PREFIX_META_HACK */
1227 rl_bind_key (key, rl_named_function (funname));
1231 /* Simple structure for boolean readline variables (i.e., those that can
1232 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1235 #define V_SPECIAL 0x1
1241 } boolean_varlist [] = {
1242 { "blink-matching-paren", &rl_blink_matching_paren, V_SPECIAL },
1243 { "completion-ignore-case", &_rl_completion_case_fold, 0 },
1244 { "convert-meta", &_rl_convert_meta_chars_to_ascii, 0 },
1245 { "disable-completion", &rl_inhibit_completion, 0 },
1246 { "enable-keypad", &_rl_enable_keypad, 0 },
1247 { "expand-tilde", &rl_complete_with_tilde_expansion, 0 },
1248 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode, 0 },
1249 { "input-meta", &_rl_meta_flag, 0 },
1250 { "mark-directories", &_rl_complete_mark_directories, 0 },
1251 { "mark-modified-lines", &_rl_mark_modified_lines, 0 },
1252 { "meta-flag", &_rl_meta_flag, 0 },
1253 { "output-meta", &_rl_output_meta_chars, 0 },
1254 { "prefer-visible-bell", &_rl_prefer_visible_bell, V_SPECIAL },
1255 { "print-completions-horizontally", &_rl_print_completions_horizontally, 0 },
1256 { "show-all-if-ambiguous", &_rl_complete_show_all, 0 },
1257 #if defined (VISIBLE_STATS)
1258 { "visible-stats", &rl_visible_stats, 0 },
1259 #endif /* VISIBLE_STATS */
1260 { (char *)NULL, (int *)NULL }
1264 find_boolean_var (name)
1269 for (i = 0; boolean_varlist[i].name; i++)
1270 if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1275 /* Hooks for handling special boolean variables, where a
1276 function needs to be called or another variable needs
1277 to be changed when they're changed. */
1279 hack_special_boolean_var (i)
1284 name = boolean_varlist[i].name;
1286 if (_rl_stricmp (name, "blink-matching-paren") == 0)
1287 _rl_enable_paren_matching (rl_blink_matching_paren);
1288 else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1290 if (_rl_prefer_visible_bell)
1291 _rl_bell_preference = VISIBLE_BELL;
1293 _rl_bell_preference = AUDIBLE_BELL;
1297 typedef int _rl_sv_func_t __P((const char *));
1299 /* These *must* correspond to the array indices for the appropriate
1300 string variable. (Though they're not used right now.) */
1301 #define V_BELLSTYLE 0
1302 #define V_COMBEGIN 1
1303 #define V_EDITMODE 2
1304 #define V_ISRCHTERM 3
1310 /* Forward declarations */
1311 static int sv_bell_style __P((const char *));
1312 static int sv_combegin __P((const char *));
1313 static int sv_compquery __P((const char *));
1314 static int sv_editmode __P((const char *));
1315 static int sv_isrchterm __P((const char *));
1316 static int sv_keymap __P((const char *));
1321 _rl_sv_func_t *set_func;
1322 } string_varlist[] = {
1323 { "bell-style", V_STRING, sv_bell_style },
1324 { "comment-begin", V_STRING, sv_combegin },
1325 { "completion-query-items", V_INT, sv_compquery },
1326 { "editing-mode", V_STRING, sv_editmode },
1327 { "isearch-terminators", V_STRING, sv_isrchterm },
1328 { "keymap", V_STRING, sv_keymap },
1333 find_string_var (name)
1338 for (i = 0; string_varlist[i].name; i++)
1339 if (_rl_stricmp (name, string_varlist[i].name) == 0)
1344 /* A boolean value that can appear in a `set variable' command is true if
1345 the value is null or empty, `on' (case-insenstive), or "1". Any other
1346 values result in 0 (false). */
1351 return (value == 0 || *value == '\0' ||
1352 (_rl_stricmp (value, "on") == 0) ||
1353 (value[0] == '1' && value[1] == '\0'));
1357 rl_variable_bind (name, value)
1358 const char *name, *value;
1363 /* Check for simple variables first. */
1364 i = find_boolean_var (name);
1367 *boolean_varlist[i].value = bool_to_int (value);
1368 if (boolean_varlist[i].flags & V_SPECIAL)
1369 hack_special_boolean_var (i);
1373 i = find_string_var (name);
1375 /* For the time being, unknown variable names or string names without a
1376 handler function are simply ignored. */
1377 if (i < 0 || string_varlist[i].set_func == 0)
1380 v = (*string_varlist[i].set_func) (value);
1388 if (_rl_strnicmp (value, "vi", 2) == 0)
1390 #if defined (VI_MODE)
1391 _rl_keymap = vi_insertion_keymap;
1392 rl_editing_mode = vi_mode;
1393 #endif /* VI_MODE */
1396 else if (_rl_strnicmp (value, "emacs", 5) == 0)
1398 _rl_keymap = emacs_standard_keymap;
1399 rl_editing_mode = emacs_mode;
1409 if (value && *value)
1411 FREE (_rl_comment_begin);
1412 _rl_comment_begin = savestring (value);
1419 sv_compquery (value)
1424 if (value && *value)
1426 nval = atoi (value);
1430 rl_completion_query_items = nval;
1440 kmap = rl_get_keymap_by_name (value);
1443 rl_set_keymap (kmap);
1449 #define _SET_BELL(v) do { _rl_bell_preference = v; return 0; } while (0)
1452 sv_bell_style (value)
1455 if (value == 0 || *value == '\0')
1456 _SET_BELL (AUDIBLE_BELL);
1457 else if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1458 _SET_BELL (NO_BELL);
1459 else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1460 _SET_BELL (AUDIBLE_BELL);
1461 else if (_rl_stricmp (value, "visible") == 0)
1462 _SET_BELL (VISIBLE_BELL);
1469 sv_isrchterm (value)
1472 int beg, end, delim;
1478 /* Isolate the value and translate it into a character string. */
1479 v = savestring (value);
1480 FREE (_rl_isearch_terminators);
1481 if (v[0] == '"' || v[0] == '\'')
1484 for (beg = end = 1; v[end] && v[end] != delim; end++)
1489 for (beg = end = 0; whitespace (v[end]) == 0; end++)
1495 /* The value starts at v + beg. Translate it into a character string. */
1496 _rl_isearch_terminators = (unsigned char *)xmalloc (2 * strlen (v) + 1);
1497 rl_translate_keyseq (v + beg, _rl_isearch_terminators, &end);
1498 _rl_isearch_terminators[end] = '\0';
1504 /* Return the character which matches NAME.
1505 For example, `Space' returns ' '. */
1512 static assoc_list name_key_alist[] = {
1515 { "Escape", '\033' },
1517 { "Newline", '\n' },
1528 glean_key_from_name (name)
1533 for (i = 0; name_key_alist[i].name; i++)
1534 if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1535 return (name_key_alist[i].value);
1537 return (*(unsigned char *)name); /* XXX was return (*name) */
1540 /* Auxiliary functions to manage keymaps. */
1544 } keymap_names[] = {
1545 { "emacs", emacs_standard_keymap },
1546 { "emacs-standard", emacs_standard_keymap },
1547 { "emacs-meta", emacs_meta_keymap },
1548 { "emacs-ctlx", emacs_ctlx_keymap },
1549 #if defined (VI_MODE)
1550 { "vi", vi_movement_keymap },
1551 { "vi-move", vi_movement_keymap },
1552 { "vi-command", vi_movement_keymap },
1553 { "vi-insert", vi_insertion_keymap },
1554 #endif /* VI_MODE */
1555 { (char *)0x0, (Keymap)0x0 }
1559 rl_get_keymap_by_name (name)
1564 for (i = 0; keymap_names[i].name; i++)
1565 if (_rl_stricmp (name, keymap_names[i].name) == 0)
1566 return (keymap_names[i].map);
1567 return ((Keymap) NULL);
1571 rl_get_keymap_name (map)
1575 for (i = 0; keymap_names[i].name; i++)
1576 if (map == keymap_names[i].map)
1577 return ((char *)keymap_names[i].name);
1578 return ((char *)NULL);
1592 return (_rl_keymap);
1596 rl_set_keymap_from_edit_mode ()
1598 if (rl_editing_mode == emacs_mode)
1599 _rl_keymap = emacs_standard_keymap;
1600 #if defined (VI_MODE)
1601 else if (rl_editing_mode == vi_mode)
1602 _rl_keymap = vi_insertion_keymap;
1603 #endif /* VI_MODE */
1607 rl_get_keymap_name_from_edit_mode ()
1609 if (rl_editing_mode == emacs_mode)
1611 #if defined (VI_MODE)
1612 else if (rl_editing_mode == vi_mode)
1614 #endif /* VI_MODE */
1619 /* **************************************************************** */
1621 /* Key Binding and Function Information */
1623 /* **************************************************************** */
1625 /* Each of the following functions produces information about the
1626 state of keybindings and functions known to Readline. The info
1627 is always printed to rl_outstream, and in such a way that it can
1628 be read back in (i.e., passed to rl_parse_and_bind (). */
1630 /* Print the names of functions known to Readline. */
1632 rl_list_funmap_names ()
1635 const char **funmap_names;
1637 funmap_names = rl_funmap_names ();
1642 for (i = 0; funmap_names[i]; i++)
1643 fprintf (rl_outstream, "%s\n", funmap_names[i]);
1645 free (funmap_names);
1649 _rl_get_keyname (key)
1655 keyname = (char *)xmalloc (8);
1658 /* Since this is going to be used to write out keysequence-function
1659 pairs for possible inclusion in an inputrc file, we don't want to
1660 do any special meta processing on KEY. */
1663 /* We might want to do this, but the old version of the code did not. */
1665 /* If this is an escape character, we don't want to do any more processing.
1666 Just add the special ESC key sequence and return. */
1676 /* RUBOUT is translated directly into \C-? */
1688 /* Now add special prefixes needed for control characters. This can
1689 potentially change C. */
1692 keyname[i++] = '\\';
1695 c = _rl_to_lower (UNCTRL (c));
1698 /* XXX experimental code. Turn the characters that are not ASCII or
1699 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
1701 if (c >= 128 && c <= 159)
1703 keyname[i++] = '\\';
1706 keyname[i++] = (c / 8) + '0';
1710 /* Now, if the character needs to be quoted with a backslash, do that. */
1711 if (c == '\\' || c == '"')
1712 keyname[i++] = '\\';
1714 /* Now add the key, terminate the string, and return it. */
1715 keyname[i++] = (char) c;
1721 /* Return a NULL terminated array of strings which represent the key
1722 sequences that are used to invoke FUNCTION in MAP. */
1724 rl_invoking_keyseqs_in_map (function, map)
1725 rl_command_func_t *function;
1730 int result_index, result_size;
1732 result = (char **)NULL;
1733 result_index = result_size = 0;
1735 for (key = 0; key < KEYMAP_SIZE; key++)
1737 switch (map[key].type)
1740 /* Macros match, if, and only if, the pointers are identical.
1741 Thus, they are treated exactly like functions in here. */
1743 /* If the function in the keymap is the one we are looking for,
1744 then add the current KEY to the list of invoking keys. */
1745 if (map[key].function == function)
1749 keyname = _rl_get_keyname (key);
1751 if (result_index + 2 > result_size)
1754 result = (char **) xrealloc (result, result_size * sizeof (char *));
1757 result[result_index++] = keyname;
1758 result[result_index] = (char *)NULL;
1767 /* Find the list of keyseqs in this map which have FUNCTION as
1768 their target. Add the key sequences found to RESULT. */
1769 if (map[key].function)
1771 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1778 for (i = 0; seqs[i]; i++)
1780 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1783 sprintf (keyname, "\\e");
1784 else if (CTRL_CHAR (key))
1785 sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1786 else if (key == RUBOUT)
1787 sprintf (keyname, "\\C-?");
1788 else if (key == '\\' || key == '"')
1791 keyname[1] = (char) key;
1796 keyname[0] = (char) key;
1800 strcat (keyname, seqs[i]);
1803 if (result_index + 2 > result_size)
1806 result = (char **) xrealloc (result, result_size * sizeof (char *));
1809 result[result_index++] = keyname;
1810 result[result_index] = (char *)NULL;
1821 /* Return a NULL terminated array of strings which represent the key
1822 sequences that can be used to invoke FUNCTION using the current keymap. */
1824 rl_invoking_keyseqs (function)
1825 rl_command_func_t *function;
1827 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1830 /* Print all of the functions and their bindings to rl_outstream. If
1831 PRINT_READABLY is non-zero, then print the output in such a way
1832 that it can be read back in. */
1834 rl_function_dumper (print_readably)
1841 names = rl_funmap_names ();
1843 fprintf (rl_outstream, "\n");
1845 for (i = 0; name = names[i]; i++)
1847 rl_command_func_t *function;
1850 function = rl_named_function (name);
1851 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1856 fprintf (rl_outstream, "# %s (not bound)\n", name);
1861 for (j = 0; invokers[j]; j++)
1863 fprintf (rl_outstream, "\"%s\": %s\n",
1874 fprintf (rl_outstream, "%s is not bound to any keys\n",
1880 fprintf (rl_outstream, "%s can be found on ", name);
1882 for (j = 0; invokers[j] && j < 5; j++)
1884 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1885 invokers[j + 1] ? ", " : ".\n");
1888 if (j == 5 && invokers[j])
1889 fprintf (rl_outstream, "...\n");
1891 for (j = 0; invokers[j]; j++)
1900 /* Print all of the current functions and their bindings to
1901 rl_outstream. If an explicit argument is given, then print
1902 the output in such a way that it can be read back in. */
1904 rl_dump_functions (count, key)
1908 fprintf (rl_outstream, "\r\n");
1909 rl_function_dumper (rl_explicit_arg);
1915 _rl_macro_dumper_internal (print_readably, map, prefix)
1921 char *keyname, *out;
1924 for (key = 0; key < KEYMAP_SIZE; key++)
1926 switch (map[key].type)
1929 keyname = _rl_get_keyname (key);
1931 out = (char *)map[key].function;
1933 out = _rl_untranslate_macro_value ((char *)map[key].function);
1936 fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
1940 fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
1951 prefix_len = prefix ? strlen (prefix) : 0;
1954 keyname = xmalloc (3 + prefix_len);
1956 strcpy (keyname, prefix);
1957 keyname[prefix_len] = '\\';
1958 keyname[prefix_len + 1] = 'e';
1959 keyname[prefix_len + 2] = '\0';
1963 keyname = _rl_get_keyname (key);
1966 out = xmalloc (strlen (keyname) + prefix_len + 1);
1967 strcpy (out, prefix);
1968 strcpy (out + prefix_len, keyname);
1974 _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
1982 rl_macro_dumper (print_readably)
1985 _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
1989 rl_dump_macros (count, key)
1993 fprintf (rl_outstream, "\r\n");
1994 rl_macro_dumper (rl_explicit_arg);
2000 rl_variable_dumper (print_readably)
2006 for (i = 0; boolean_varlist[i].name; i++)
2009 fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
2010 *boolean_varlist[i].value ? "on" : "off");
2012 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
2013 *boolean_varlist[i].value ? "on" : "off");
2017 switch (_rl_bell_preference)
2020 kname = "none"; break;
2022 kname = "visible"; break;
2025 kname = "audible"; break;
2028 fprintf (rl_outstream, "set bell-style %s\n", kname);
2030 fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
2034 fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
2036 fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : "");
2038 /* completion-query-items */
2040 fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items);
2042 fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items);
2046 fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
2048 fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
2051 kname = rl_get_keymap_name (_rl_keymap);
2053 kname = rl_get_keymap_name_from_edit_mode ();
2055 fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none");
2057 fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none");
2059 /* isearch-terminators */
2060 if (_rl_isearch_terminators)
2064 disp = _rl_untranslate_macro_value (_rl_isearch_terminators);
2067 fprintf (rl_outstream, "set isearch-terminators \"%s\"\n", disp);
2069 fprintf (rl_outstream, "isearch-terminators is set to \"%s\"\n", disp);
2075 /* Print all of the current variables and their values to
2076 rl_outstream. If an explicit argument is given, then print
2077 the output in such a way that it can be read back in. */
2079 rl_dump_variables (count, key)
2083 fprintf (rl_outstream, "\r\n");
2084 rl_variable_dumper (rl_explicit_arg);
2089 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
2091 _rl_bind_if_unbound (keyseq, default_func)
2093 rl_command_func_t *default_func;
2095 rl_command_func_t *func;
2099 func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
2100 if (!func || func == rl_do_lowercase_version)
2101 rl_set_key (keyseq, default_func, _rl_keymap);
2105 /* Return non-zero if any members of ARRAY are a substring in STRING. */
2107 substring_member_of_array (string, array)
2113 if (_rl_strindex (string, *array))