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((char *, int));
72 static int glean_key_from_name __P((char *));
73 static int substring_member_of_array __P((char *, 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, Function *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)
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)
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)
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, (Function *)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, (Function *)NULL, map));
168 /* Unbind all keys bound to FUNCTION in MAP. */
170 rl_unbind_function_in_map (func, map)
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 = (Function *)NULL;
188 rl_unbind_command_in_map (command, map)
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)
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 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)
249 /* If no keys to bind to, exit right away. */
250 if (!keyseq || !*keyseq)
257 keys = xmalloc (1 + (2 * strlen (keyseq)));
259 /* Translate the ASCII representation of KEYSEQ into an array of
260 characters. Stuff the characters into KEYS, and the length of
261 KEYS into KEYS_LEN. */
262 if (rl_translate_keyseq (keyseq, keys, &keys_len))
268 /* Bind keys, making new keymaps as necessary. */
269 for (i = 0; i < keys_len; i++)
271 int ic = (int) ((unsigned char)keys[i]);
273 if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
276 if (map[ESC].type == ISKMAP)
277 map = FUNCTION_TO_KEYMAP (map, ESC);
280 if ((i + 1) < keys_len)
282 if (map[ic].type != ISKMAP)
284 if (map[ic].type == ISMACR)
285 free ((char *)map[ic].function);
287 map[ic].type = ISKMAP;
288 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
290 map = FUNCTION_TO_KEYMAP (map, ic);
294 if (map[ic].type == ISMACR)
295 free ((char *)map[ic].function);
297 map[ic].function = KEYMAP_TO_FUNCTION (data);
301 rl_binding_keymap = map;
307 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
308 an array of characters. LEN gets the final length of ARRAY. Return
309 non-zero if there was an error parsing SEQ. */
311 rl_translate_keyseq (seq, array, len)
315 register int i, c, l, temp;
317 for (i = l = 0; c = seq[i]; i++)
326 /* Handle \C- and \M- prefixes. */
327 if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
329 /* Handle special case of backwards define. */
330 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
334 array[l++] = CTRL (_rl_to_upper (seq[i]));
341 array[l++] = ESC; /* XXX */
346 /* Special hack for C-?... */
347 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
352 /* Translate other backslash-escaped characters. These are the
353 same escape sequences that bash's `echo' and `printf' builtins
354 handle, with the addition of \d -> RUBOUT. A backslash
355 preceding a character that is not special is stripped. */
365 array[l++] = RUBOUT; /* readline-specific */
374 array[l++] = NEWLINE;
388 case '0': case '1': case '2': case '3':
389 case '4': case '5': case '6': case '7':
391 for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++)
392 c = (c * 8) + OCTVALUE (seq[i]);
393 i--; /* auto-increment in for loop */
394 array[l++] = c % (largest_char + 1);
398 for (temp = 3, c = 0; isxdigit (seq[i]) && temp--; i++)
399 c = (c * 16) + HEXVALUE (seq[i]);
402 i--; /* auto-increment in for loop */
403 array[l++] = c % (largest_char + 1);
405 default: /* backslashes before non-special chars just add the char */
407 break; /* the backslash is stripped */
421 rl_untranslate_keyseq (seq)
424 static char kseq[16];
436 else if (CTRL_CHAR (c))
441 c = _rl_to_lower (UNCTRL (c));
443 else if (c == RUBOUT)
456 else if (c == '\\' || c == '"')
461 kseq[i++] = (unsigned char) c;
467 _rl_untranslate_macro_value (seq)
473 r = ret = xmalloc (7 * strlen (seq) + 1);
474 for (s = seq; *s; s++)
484 else if (CTRL_CHAR (c) && c != ESC)
489 c = _rl_to_lower (UNCTRL (c));
491 else if (c == RUBOUT)
504 else if (c == '\\' || c == '"')
507 *r++ = (unsigned char)c;
513 /* Return a pointer to the function that STRING represents.
514 If STRING doesn't have a matching function, then a NULL pointer
517 rl_named_function (string)
522 rl_initialize_funmap ();
524 for (i = 0; funmap[i]; i++)
525 if (_rl_stricmp (funmap[i]->name, string) == 0)
526 return (funmap[i]->function);
527 return ((Function *)NULL);
530 /* Return the function (or macro) definition which would be invoked via
531 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
532 used. TYPE, if non-NULL, is a pointer to an int which will receive the
533 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
534 or ISMACR (macro). */
536 rl_function_of_keyseq (keyseq, map, type)
546 for (i = 0; keyseq && keyseq[i]; i++)
550 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
552 if (map[ESC].type != ISKMAP)
555 *type = map[ESC].type;
557 return (map[ESC].function);
561 map = FUNCTION_TO_KEYMAP (map, ESC);
566 if (map[ic].type == ISKMAP)
568 /* If this is the last key in the key sequence, return the
575 return (map[ic].function);
578 map = FUNCTION_TO_KEYMAP (map, ic);
583 *type = map[ic].type;
585 return (map[ic].function);
588 return ((Function *) NULL);
591 /* The last key bindings file read. */
592 static char *last_readline_init_file = (char *)NULL;
594 /* The file we're currently reading key bindings from. */
595 static char *current_readline_init_file;
596 static int current_readline_init_include_level;
597 static int current_readline_init_lineno;
599 /* Read FILENAME into a locally-allocated buffer and return the buffer.
600 The size of the buffer is returned in *SIZEP. Returns NULL if any
601 errors were encountered. */
603 _rl_read_file (filename, sizep)
612 if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0)
613 return ((char *)NULL);
615 file_size = (size_t)finfo.st_size;
617 /* check for overflow on very large files */
618 if (file_size != finfo.st_size || file_size + 1 < file_size)
625 return ((char *)NULL);
628 /* Read the file into BUFFER. */
629 buffer = (char *)xmalloc (file_size + 1);
630 i = read (file, buffer, file_size);
640 return ((char *)NULL);
644 buffer[file_size] = '\0';
656 /* Re-read the current keybindings file. */
658 rl_re_read_init_file (count, ignore)
662 r = rl_read_init_file ((char *)NULL);
663 rl_set_keymap_from_edit_mode ();
667 /* Do key bindings from a file. If FILENAME is NULL it defaults
668 to the first non-null filename from this list:
669 1. the filename used for the previous call
670 2. the value of the shell variable `INPUTRC'
672 If the file existed and could be opened and read, 0 is returned,
673 otherwise errno is returned. */
675 rl_read_init_file (filename)
678 /* Default the filename. */
681 filename = last_readline_init_file;
683 filename = get_env_value ("INPUTRC");
685 filename = DEFAULT_INPUTRC;
689 filename = DEFAULT_INPUTRC;
691 #if defined (__MSDOS__)
692 if (_rl_read_init_file (filename, 0) == 0)
694 filename = "~/_inputrc";
696 return (_rl_read_init_file (filename, 0));
700 _rl_read_init_file (filename, include_level)
705 char *buffer, *openname, *line, *end;
708 current_readline_init_file = filename;
709 current_readline_init_include_level = include_level;
711 openname = tilde_expand (filename);
712 buffer = _rl_read_file (openname, &file_size);
718 if (include_level == 0 && filename != last_readline_init_file)
720 FREE (last_readline_init_file);
721 last_readline_init_file = savestring (filename);
724 currently_reading_init_file = 1;
726 /* Loop over the lines in the file. Lines that start with `#' are
727 comments; all other lines are commands for readline initialization. */
728 current_readline_init_lineno = 1;
730 end = buffer + file_size;
733 /* Find the end of this line. */
734 for (i = 0; line + i != end && line[i] != '\n'; i++);
736 #if defined (__CYGWIN32__)
737 /* ``Be liberal in what you accept.'' */
738 if (line[i] == '\n' && line[i-1] == '\r')
742 /* Mark end of line. */
745 /* Skip leading whitespace. */
746 while (*line && whitespace (*line))
752 /* If the line is not a comment, then parse it. */
753 if (*line && *line != '#')
754 rl_parse_and_bind (line);
756 /* Move to the next line. */
758 current_readline_init_lineno++;
762 currently_reading_init_file = 0;
767 _rl_init_file_error (msg)
770 if (currently_reading_init_file)
771 fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
772 current_readline_init_lineno, msg);
774 fprintf (stderr, "readline: %s\n", msg);
777 /* **************************************************************** */
779 /* Parser Directives */
781 /* **************************************************************** */
785 /* Calling programs set this to have their argv[0]. */
786 char *rl_readline_name = "other";
788 /* Stack of previous values of parsing_conditionalized_out. */
789 static unsigned char *if_stack = (unsigned char *)NULL;
790 static int if_stack_depth;
791 static int if_stack_size;
793 /* Push _rl_parsing_conditionalized_out, and set parser state based
801 /* Push parser state. */
802 if (if_stack_depth + 1 >= if_stack_size)
805 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
807 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
809 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
811 /* If parsing is turned off, then nothing can turn it back on except
812 for finding the matching endif. In that case, return right now. */
813 if (_rl_parsing_conditionalized_out)
816 /* Isolate first argument. */
817 for (i = 0; args[i] && !whitespace (args[i]); i++);
822 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
823 isn't term=foo, or mode=emacs, then check to see if the first
824 word in ARGS is the same as the value stored in rl_readline_name. */
825 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
829 /* Terminals like "aaa-60" are equivalent to "aaa". */
830 tname = savestring (rl_terminal_name);
831 tem = strchr (tname, '-');
835 /* Test the `long' and `short' forms of the terminal name so that
836 if someone has a `sun-cmd' and does not want to have bindings
837 that will be executed if the terminal is a `sun', they can put
838 `$if term=sun-cmd' into their .inputrc. */
839 _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
840 _rl_stricmp (args + 5, rl_terminal_name);
843 #if defined (VI_MODE)
844 else if (_rl_strnicmp (args, "mode=", 5) == 0)
848 if (_rl_stricmp (args + 5, "emacs") == 0)
850 else if (_rl_stricmp (args + 5, "vi") == 0)
855 _rl_parsing_conditionalized_out = mode != rl_editing_mode;
858 /* Check to see if the first word in ARGS is the same as the
859 value stored in rl_readline_name. */
860 else if (_rl_stricmp (args, rl_readline_name) == 0)
861 _rl_parsing_conditionalized_out = 0;
863 _rl_parsing_conditionalized_out = 1;
867 /* Invert the current parser state if there is anything on the stack. */
874 if (if_stack_depth == 0)
876 _rl_init_file_error ("$else found without matching $if");
880 /* Check the previous (n - 1) levels of the stack to make sure that
881 we haven't previously turned off parsing. */
882 for (i = 0; i < if_stack_depth - 1; i++)
883 if (if_stack[i] == 1)
886 /* Invert the state of parsing if at top level. */
887 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
891 /* Terminate a conditional, popping the value of
892 _rl_parsing_conditionalized_out from the stack. */
898 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
900 _rl_init_file_error ("$endif without matching $if");
905 parser_include (args)
908 char *old_init_file, *e;
909 int old_line_number, old_include_level, r;
911 if (_rl_parsing_conditionalized_out)
914 old_init_file = current_readline_init_file;
915 old_line_number = current_readline_init_lineno;
916 old_include_level = current_readline_init_include_level;
918 e = strchr (args, '\n');
921 r = _rl_read_init_file (args, old_include_level + 1);
923 current_readline_init_file = old_init_file;
924 current_readline_init_lineno = old_line_number;
925 current_readline_init_include_level = old_include_level;
930 /* Associate textual names with actual functions. */
934 } parser_directives [] = {
936 { "endif", parser_endif },
937 { "else", parser_else },
938 { "include", parser_include },
939 { (char *)0x0, (Function *)0x0 }
942 /* Handle a parser directive. STATEMENT is the line of the directive
943 without any leading `$'. */
945 handle_parser_directive (statement)
949 char *directive, *args;
951 /* Isolate the actual directive. */
953 /* Skip whitespace. */
954 for (i = 0; whitespace (statement[i]); i++);
956 directive = &statement[i];
958 for (; statement[i] && !whitespace (statement[i]); i++);
961 statement[i++] = '\0';
963 for (; statement[i] && whitespace (statement[i]); i++);
965 args = &statement[i];
967 /* Lookup the command, and act on it. */
968 for (i = 0; parser_directives[i].name; i++)
969 if (_rl_stricmp (directive, parser_directives[i].name) == 0)
971 (*parser_directives[i].function) (args);
975 /* display an error message about the unknown parser directive */
976 _rl_init_file_error ("unknown parser directive");
980 /* Read the binding command from STRING and perform it.
981 A key binding command looks like: Keyname: function-name\0,
982 a variable binding command looks like: set variable value.
983 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
985 rl_parse_and_bind (string)
988 char *funname, *kname;
990 int key, equivalency;
992 while (string && whitespace (*string))
995 if (!string || !*string || *string == '#')
998 /* If this is a parser directive, act on it. */
1001 handle_parser_directive (&string[1]);
1005 /* If we aren't supposed to be parsing right now, then we're done. */
1006 if (_rl_parsing_conditionalized_out)
1010 /* If this keyname is a complex key expression surrounded by quotes,
1011 advance to after the matching close quote. This code allows the
1012 backslash to quote characters in the key expression. */
1017 for (i = 1; c = string[i]; i++)
1034 /* If we didn't find a closing quote, abort the line. */
1035 if (string[i] == '\0')
1037 _rl_init_file_error ("no closing `\"' in key binding");
1042 /* Advance to the colon (:) or whitespace which separates the two objects. */
1043 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
1045 equivalency = (c == ':' && string[i + 1] == '=');
1047 /* Mark the end of the command (or keyname). */
1051 /* If doing assignment, skip the '=' sign as well. */
1055 /* If this is a command to set a variable, then do that. */
1056 if (_rl_stricmp (string, "set") == 0)
1058 char *var = string + i;
1061 /* Make VAR point to start of variable name. */
1062 while (*var && whitespace (*var)) var++;
1064 /* Make value point to start of value string. */
1066 while (*value && !whitespace (*value)) value++;
1069 while (*value && whitespace (*value)) value++;
1071 rl_variable_bind (var, value);
1075 /* Skip any whitespace between keyname and funname. */
1076 for (; string[i] && whitespace (string[i]); i++);
1077 funname = &string[i];
1079 /* Now isolate funname.
1080 For straight function names just look for whitespace, since
1081 that will signify the end of the string. But this could be a
1082 macro definition. In that case, the string is quoted, so skip
1083 to the matching delimiter. We allow the backslash to quote the
1084 delimiter characters in the macro body. */
1085 /* This code exists to allow whitespace in macro expansions, which
1086 would otherwise be gobbled up by the next `for' loop.*/
1087 /* XXX - it may be desirable to allow backslash quoting only if " is
1088 the quoted string delimiter, like the shell. */
1089 if (*funname == '\'' || *funname == '"')
1091 int delimiter = string[i++], passc;
1093 for (passc = 0; c = string[i]; i++)
1114 /* Advance to the end of the string. */
1115 for (; string[i] && !whitespace (string[i]); i++);
1117 /* No extra whitespace at the end of the string. */
1120 /* Handle equivalency bindings here. Make the left-hand side be exactly
1121 whatever the right-hand evaluates to, including keymaps. */
1127 /* If this is a new-style key-binding, then do the binding with
1128 rl_set_key (). Otherwise, let the older code deal with it. */
1132 register int j, k, passc;
1134 seq = xmalloc (1 + strlen (string));
1135 for (j = 1, k = passc = 0; string[j]; j++)
1137 /* Allow backslash to quote characters, but leave them in place.
1138 This allows a string to end with a backslash quoting another
1139 backslash, or with a backslash quoting a double quote. The
1140 backslashes are left in place for rl_translate_keyseq (). */
1141 if (passc || (string[j] == '\\'))
1143 seq[k++] = string[j];
1148 if (string[j] == '"')
1151 seq[k++] = string[j];
1155 /* Binding macro? */
1156 if (*funname == '\'' || *funname == '"')
1158 j = strlen (funname);
1160 /* Remove the delimiting quotes from each end of FUNNAME. */
1161 if (j && funname[j - 1] == *funname)
1162 funname[j - 1] = '\0';
1164 rl_macro_bind (seq, &funname[1], _rl_keymap);
1167 rl_set_key (seq, rl_named_function (funname), _rl_keymap);
1173 /* Get the actual character we want to deal with. */
1174 kname = strrchr (string, '-');
1180 key = glean_key_from_name (kname);
1182 /* Add in control and meta bits. */
1183 if (substring_member_of_array (string, possible_control_prefixes))
1184 key = CTRL (_rl_to_upper (key));
1186 if (substring_member_of_array (string, possible_meta_prefixes))
1189 /* Temporary. Handle old-style keyname with macro-binding. */
1190 if (*funname == '\'' || *funname == '"')
1192 unsigned char useq[2];
1193 int fl = strlen (funname);
1195 useq[0] = key; useq[1] = '\0';
1196 if (fl && funname[fl - 1] == *funname)
1197 funname[fl - 1] = '\0';
1199 rl_macro_bind (useq, &funname[1], _rl_keymap);
1201 #if defined (PREFIX_META_HACK)
1202 /* Ugly, but working hack to keep prefix-meta around. */
1203 else if (_rl_stricmp (funname, "prefix-meta") == 0)
1209 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1211 #endif /* PREFIX_META_HACK */
1213 rl_bind_key (key, rl_named_function (funname));
1217 /* Simple structure for boolean readline variables (i.e., those that can
1218 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1221 #define V_SPECIAL 0x1
1227 } boolean_varlist [] = {
1228 { "blink-matching-paren", &rl_blink_matching_paren, V_SPECIAL },
1229 { "completion-ignore-case", &_rl_completion_case_fold, 0 },
1230 { "convert-meta", &_rl_convert_meta_chars_to_ascii, 0 },
1231 { "disable-completion", &rl_inhibit_completion, 0 },
1232 { "enable-keypad", &_rl_enable_keypad, 0 },
1233 { "expand-tilde", &rl_complete_with_tilde_expansion, 0 },
1234 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode, 0 },
1235 { "input-meta", &_rl_meta_flag, 0 },
1236 { "mark-directories", &_rl_complete_mark_directories, 0 },
1237 { "mark-modified-lines", &_rl_mark_modified_lines, 0 },
1238 { "meta-flag", &_rl_meta_flag, 0 },
1239 { "output-meta", &_rl_output_meta_chars, 0 },
1240 { "prefer-visible-bell", &_rl_prefer_visible_bell, V_SPECIAL },
1241 { "print-completions-horizontally", &_rl_print_completions_horizontally, 0 },
1242 { "show-all-if-ambiguous", &_rl_complete_show_all, 0 },
1243 #if defined (VISIBLE_STATS)
1244 { "visible-stats", &rl_visible_stats, 0 },
1245 #endif /* VISIBLE_STATS */
1246 { (char *)NULL, (int *)NULL }
1250 find_boolean_var (name)
1255 for (i = 0; boolean_varlist[i].name; i++)
1256 if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1261 /* Hooks for handling special boolean variables, where a
1262 function needs to be called or another variable needs
1263 to be changed when they're changed. */
1265 hack_special_boolean_var (i)
1270 name = boolean_varlist[i].name;
1272 if (_rl_stricmp (name, "blink-matching-paren") == 0)
1273 _rl_enable_paren_matching (rl_blink_matching_paren);
1274 else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1276 if (_rl_prefer_visible_bell)
1277 _rl_bell_preference = VISIBLE_BELL;
1279 _rl_bell_preference = AUDIBLE_BELL;
1283 /* These *must* correspond to the array indices for the appropriate
1284 string variable. (Though they're not used right now.) */
1285 #define V_BELLSTYLE 0
1286 #define V_COMBEGIN 1
1287 #define V_EDITMODE 2
1288 #define V_ISRCHTERM 3
1294 /* Forward declarations */
1295 static int sv_bell_style __P((char *));
1296 static int sv_combegin __P((char *));
1297 static int sv_compquery __P((char *));
1298 static int sv_editmode __P((char *));
1299 static int sv_isrchterm __P((char *));
1300 static int sv_keymap __P((char *));
1306 } string_varlist[] = {
1307 { "bell-style", V_STRING, sv_bell_style },
1308 { "comment-begin", V_STRING, sv_combegin },
1309 { "completion-query-items", V_INT, sv_compquery },
1310 { "editing-mode", V_STRING, sv_editmode },
1311 { "isearch-terminators", V_STRING, sv_isrchterm },
1312 { "keymap", V_STRING, sv_keymap },
1317 find_string_var (name)
1322 for (i = 0; string_varlist[i].name; i++)
1323 if (_rl_stricmp (name, string_varlist[i].name) == 0)
1328 /* A boolean value that can appear in a `set variable' command is true if
1329 the value is null or empty, `on' (case-insenstive), or "1". Any other
1330 values result in 0 (false). */
1335 return (value == 0 || *value == '\0' ||
1336 (_rl_stricmp (value, "on") == 0) ||
1337 (value[0] == '1' && value[1] == '\0'));
1341 rl_variable_bind (name, value)
1347 /* Check for simple variables first. */
1348 i = find_boolean_var (name);
1351 *boolean_varlist[i].value = bool_to_int (value);
1352 if (boolean_varlist[i].flags & V_SPECIAL)
1353 hack_special_boolean_var (i);
1357 i = find_string_var (name);
1359 /* For the time being, unknown variable names or string names without a
1360 handler function are simply ignored. */
1361 if (i < 0 || string_varlist[i].set_func == 0)
1364 v = (*string_varlist[i].set_func) (value);
1372 if (_rl_strnicmp (value, "vi", 2) == 0)
1374 #if defined (VI_MODE)
1375 _rl_keymap = vi_insertion_keymap;
1376 rl_editing_mode = vi_mode;
1377 #endif /* VI_MODE */
1380 else if (_rl_strnicmp (value, "emacs", 5) == 0)
1382 _rl_keymap = emacs_standard_keymap;
1383 rl_editing_mode = emacs_mode;
1393 if (value && *value)
1395 FREE (_rl_comment_begin);
1396 _rl_comment_begin = savestring (value);
1403 sv_compquery (value)
1408 if (value && *value)
1410 nval = atoi (value);
1414 rl_completion_query_items = nval;
1424 kmap = rl_get_keymap_by_name (value);
1427 rl_set_keymap (kmap);
1433 #define _SET_BELL(v) do { _rl_bell_preference = v; return 0; } while (0)
1436 sv_bell_style (value)
1439 if (value == 0 || *value == '\0')
1440 _SET_BELL (AUDIBLE_BELL);
1441 else if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1442 _SET_BELL (NO_BELL);
1443 else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1444 _SET_BELL (AUDIBLE_BELL);
1445 else if (_rl_stricmp (value, "visible") == 0)
1446 _SET_BELL (VISIBLE_BELL);
1453 sv_isrchterm (value)
1456 int beg, end, delim;
1462 /* Isolate the value and translate it into a character string. */
1463 v = savestring (value);
1464 FREE (_rl_isearch_terminators);
1465 if (v[0] == '"' || v[0] == '\'')
1468 for (beg = end = 1; v[end] && v[end] != delim; end++)
1473 for (beg = end = 0; whitespace (v[end]) == 0; end++)
1479 /* The value starts at v + beg. Translate it into a character string. */
1480 _rl_isearch_terminators = (unsigned char *)xmalloc (2 * strlen (v) + 1);
1481 rl_translate_keyseq (v + beg, _rl_isearch_terminators, &end);
1482 _rl_isearch_terminators[end] = '\0';
1488 /* Return the character which matches NAME.
1489 For example, `Space' returns ' '. */
1496 static assoc_list name_key_alist[] = {
1499 { "Escape", '\033' },
1501 { "Newline", '\n' },
1512 glean_key_from_name (name)
1517 for (i = 0; name_key_alist[i].name; i++)
1518 if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1519 return (name_key_alist[i].value);
1521 return (*(unsigned char *)name); /* XXX was return (*name) */
1524 /* Auxiliary functions to manage keymaps. */
1528 } keymap_names[] = {
1529 { "emacs", emacs_standard_keymap },
1530 { "emacs-standard", emacs_standard_keymap },
1531 { "emacs-meta", emacs_meta_keymap },
1532 { "emacs-ctlx", emacs_ctlx_keymap },
1533 #if defined (VI_MODE)
1534 { "vi", vi_movement_keymap },
1535 { "vi-move", vi_movement_keymap },
1536 { "vi-command", vi_movement_keymap },
1537 { "vi-insert", vi_insertion_keymap },
1538 #endif /* VI_MODE */
1539 { (char *)0x0, (Keymap)0x0 }
1543 rl_get_keymap_by_name (name)
1548 for (i = 0; keymap_names[i].name; i++)
1549 if (strcmp (name, keymap_names[i].name) == 0)
1550 return (keymap_names[i].map);
1551 return ((Keymap) NULL);
1555 rl_get_keymap_name (map)
1559 for (i = 0; keymap_names[i].name; i++)
1560 if (map == keymap_names[i].map)
1561 return (keymap_names[i].name);
1562 return ((char *)NULL);
1576 return (_rl_keymap);
1580 rl_set_keymap_from_edit_mode ()
1582 if (rl_editing_mode == emacs_mode)
1583 _rl_keymap = emacs_standard_keymap;
1584 #if defined (VI_MODE)
1585 else if (rl_editing_mode == vi_mode)
1586 _rl_keymap = vi_insertion_keymap;
1587 #endif /* VI_MODE */
1591 rl_get_keymap_name_from_edit_mode ()
1593 if (rl_editing_mode == emacs_mode)
1595 #if defined (VI_MODE)
1596 else if (rl_editing_mode == vi_mode)
1598 #endif /* VI_MODE */
1603 /* **************************************************************** */
1605 /* Key Binding and Function Information */
1607 /* **************************************************************** */
1609 /* Each of the following functions produces information about the
1610 state of keybindings and functions known to Readline. The info
1611 is always printed to rl_outstream, and in such a way that it can
1612 be read back in (i.e., passed to rl_parse_and_bind (). */
1614 /* Print the names of functions known to Readline. */
1616 rl_list_funmap_names ()
1619 char **funmap_names;
1621 funmap_names = rl_funmap_names ();
1626 for (i = 0; funmap_names[i]; i++)
1627 fprintf (rl_outstream, "%s\n", funmap_names[i]);
1629 free (funmap_names);
1633 _rl_get_keyname (key)
1639 keyname = (char *)xmalloc (8);
1642 /* Since this is going to be used to write out keysequence-function
1643 pairs for possible inclusion in an inputrc file, we don't want to
1644 do any special meta processing on KEY. */
1647 /* We might want to do this, but the old version of the code did not. */
1649 /* If this is an escape character, we don't want to do any more processing.
1650 Just add the special ESC key sequence and return. */
1660 /* RUBOUT is translated directly into \C-? */
1672 /* Now add special prefixes needed for control characters. This can
1673 potentially change C. */
1676 keyname[i++] = '\\';
1679 c = _rl_to_lower (UNCTRL (c));
1682 /* XXX experimental code. Turn the characters that are not ASCII or
1683 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
1685 if (c >= 128 && c <= 159)
1687 keyname[i++] = '\\';
1690 keyname[i++] = (c / 8) + '0';
1694 /* Now, if the character needs to be quoted with a backslash, do that. */
1695 if (c == '\\' || c == '"')
1696 keyname[i++] = '\\';
1698 /* Now add the key, terminate the string, and return it. */
1699 keyname[i++] = (char) c;
1705 /* Return a NULL terminated array of strings which represent the key
1706 sequences that are used to invoke FUNCTION in MAP. */
1708 rl_invoking_keyseqs_in_map (function, map)
1714 int result_index, result_size;
1716 result = (char **)NULL;
1717 result_index = result_size = 0;
1719 for (key = 0; key < KEYMAP_SIZE; key++)
1721 switch (map[key].type)
1724 /* Macros match, if, and only if, the pointers are identical.
1725 Thus, they are treated exactly like functions in here. */
1727 /* If the function in the keymap is the one we are looking for,
1728 then add the current KEY to the list of invoking keys. */
1729 if (map[key].function == function)
1733 keyname = _rl_get_keyname (key);
1735 if (result_index + 2 > result_size)
1738 result = (char **) xrealloc (result, result_size * sizeof (char *));
1741 result[result_index++] = keyname;
1742 result[result_index] = (char *)NULL;
1751 /* Find the list of keyseqs in this map which have FUNCTION as
1752 their target. Add the key sequences found to RESULT. */
1753 if (map[key].function)
1755 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1762 for (i = 0; seqs[i]; i++)
1764 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1767 sprintf (keyname, "\\e");
1768 else if (CTRL_CHAR (key))
1769 sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1770 else if (key == RUBOUT)
1771 sprintf (keyname, "\\C-?");
1772 else if (key == '\\' || key == '"')
1775 keyname[1] = (char) key;
1780 keyname[0] = (char) key;
1784 strcat (keyname, seqs[i]);
1787 if (result_index + 2 > result_size)
1790 result = (char **) xrealloc (result, result_size * sizeof (char *));
1793 result[result_index++] = keyname;
1794 result[result_index] = (char *)NULL;
1805 /* Return a NULL terminated array of strings which represent the key
1806 sequences that can be used to invoke FUNCTION using the current keymap. */
1808 rl_invoking_keyseqs (function)
1811 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1814 /* Print all of the functions and their bindings to rl_outstream. If
1815 PRINT_READABLY is non-zero, then print the output in such a way
1816 that it can be read back in. */
1818 rl_function_dumper (print_readably)
1825 names = rl_funmap_names ();
1827 fprintf (rl_outstream, "\n");
1829 for (i = 0; name = names[i]; i++)
1834 function = rl_named_function (name);
1835 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1840 fprintf (rl_outstream, "# %s (not bound)\n", name);
1845 for (j = 0; invokers[j]; j++)
1847 fprintf (rl_outstream, "\"%s\": %s\n",
1858 fprintf (rl_outstream, "%s is not bound to any keys\n",
1864 fprintf (rl_outstream, "%s can be found on ", name);
1866 for (j = 0; invokers[j] && j < 5; j++)
1868 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1869 invokers[j + 1] ? ", " : ".\n");
1872 if (j == 5 && invokers[j])
1873 fprintf (rl_outstream, "...\n");
1875 for (j = 0; invokers[j]; j++)
1884 /* Print all of the current functions and their bindings to
1885 rl_outstream. If an explicit argument is given, then print
1886 the output in such a way that it can be read back in. */
1888 rl_dump_functions (count, key)
1892 fprintf (rl_outstream, "\r\n");
1893 rl_function_dumper (rl_explicit_arg);
1899 _rl_macro_dumper_internal (print_readably, map, prefix)
1905 char *keyname, *out;
1908 for (key = 0; key < KEYMAP_SIZE; key++)
1910 switch (map[key].type)
1913 keyname = _rl_get_keyname (key);
1915 out = (char *)map[key].function;
1917 out = _rl_untranslate_macro_value ((char *)map[key].function);
1920 fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
1924 fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
1935 prefix_len = prefix ? strlen (prefix) : 0;
1938 keyname = xmalloc (3 + prefix_len);
1940 strcpy (keyname, prefix);
1941 keyname[prefix_len] = '\\';
1942 keyname[prefix_len + 1] = 'e';
1943 keyname[prefix_len + 2] = '\0';
1947 keyname = _rl_get_keyname (key);
1950 out = xmalloc (strlen (keyname) + prefix_len + 1);
1951 strcpy (out, prefix);
1952 strcpy (out + prefix_len, keyname);
1958 _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
1966 rl_macro_dumper (print_readably)
1969 _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
1973 rl_dump_macros (count, key)
1977 fprintf (rl_outstream, "\r\n");
1978 rl_macro_dumper (rl_explicit_arg);
1984 rl_variable_dumper (print_readably)
1990 for (i = 0; boolean_varlist[i].name; i++)
1993 fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
1994 *boolean_varlist[i].value ? "on" : "off");
1996 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
1997 *boolean_varlist[i].value ? "on" : "off");
2001 switch (_rl_bell_preference)
2004 kname = "none"; break;
2006 kname = "visible"; break;
2009 kname = "audible"; break;
2012 fprintf (rl_outstream, "set bell-style %s\n", kname);
2014 fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
2018 fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
2020 fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : "");
2022 /* completion-query-items */
2024 fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items);
2026 fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items);
2030 fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
2032 fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
2035 kname = rl_get_keymap_name (_rl_keymap);
2037 kname = rl_get_keymap_name_from_edit_mode ();
2039 fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none");
2041 fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none");
2043 /* isearch-terminators */
2044 if (_rl_isearch_terminators)
2048 disp = _rl_untranslate_macro_value (_rl_isearch_terminators);
2051 fprintf (rl_outstream, "set isearch-terminators \"%s\"\n", disp);
2053 fprintf (rl_outstream, "isearch-terminators is set to \"%s\"\n", disp);
2059 /* Print all of the current variables and their values to
2060 rl_outstream. If an explicit argument is given, then print
2061 the output in such a way that it can be read back in. */
2063 rl_dump_variables (count, key)
2067 fprintf (rl_outstream, "\r\n");
2068 rl_variable_dumper (rl_explicit_arg);
2073 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
2075 _rl_bind_if_unbound (keyseq, default_func)
2077 Function *default_func;
2083 func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
2084 if (!func || func == rl_do_lowercase_version)
2085 rl_set_key (keyseq, default_func, _rl_keymap);
2089 /* Return non-zero if any members of ARRAY are a substring in STRING. */
2091 substring_member_of_array (string, array)
2092 char *string, **array;
2096 if (_rl_strindex (string, *array))