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 char *_rl_read_file PARAMS((char *, size_t *));
72 static void _rl_init_file_error PARAMS((const char *));
73 static int _rl_read_init_file PARAMS((const char *, int));
74 static int glean_key_from_name PARAMS((char *));
75 static int substring_member_of_array PARAMS((char *, const char **));
77 static int currently_reading_init_file;
79 /* used only in this file */
80 static int _rl_prefer_visible_bell = 1;
82 /* **************************************************************** */
86 /* **************************************************************** */
88 /* rl_add_defun (char *name, rl_command_func_t *function, int key)
89 Add NAME to the list of named functions. Make FUNCTION be the function
90 that gets called. If KEY is not -1, then bind it. */
92 rl_add_defun (name, function, key)
94 rl_command_func_t *function;
98 rl_bind_key (key, function);
99 rl_add_funmap_entry (name, function);
103 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
105 rl_bind_key (key, function)
107 rl_command_func_t *function;
112 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
114 if (_rl_keymap[ESC].type == ISKMAP)
118 escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
120 escmap[key].type = ISFUNC;
121 escmap[key].function = function;
127 _rl_keymap[key].type = ISFUNC;
128 _rl_keymap[key].function = function;
129 rl_binding_keymap = _rl_keymap;
133 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
136 rl_bind_key_in_map (key, function, map)
138 rl_command_func_t *function;
146 result = rl_bind_key (key, function);
151 /* Make KEY do nothing in the currently selected keymap.
152 Returns non-zero in case of error. */
157 return (rl_bind_key (key, (rl_command_func_t *)NULL));
160 /* Make KEY do nothing in MAP.
161 Returns non-zero in case of error. */
163 rl_unbind_key_in_map (key, map)
167 return (rl_bind_key_in_map (key, (rl_command_func_t *)NULL, map));
170 /* Unbind all keys bound to FUNCTION in MAP. */
172 rl_unbind_function_in_map (func, map)
173 rl_command_func_t *func;
176 register int i, rval;
178 for (i = rval = 0; i < KEYMAP_SIZE; i++)
180 if (map[i].type == ISFUNC && map[i].function == func)
182 map[i].function = (rl_command_func_t *)NULL;
190 rl_unbind_command_in_map (command, map)
194 rl_command_func_t *func;
196 func = rl_named_function (command);
199 return (rl_unbind_function_in_map (func, map));
202 /* Bind the key sequence represented by the string KEYSEQ to
203 FUNCTION. This makes new keymaps as necessary. The initial
204 place to do bindings is in MAP. */
206 rl_set_key (keyseq, function, map)
208 rl_command_func_t *function;
211 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
214 /* Bind the key sequence represented by the string KEYSEQ to
215 the string of characters MACRO. This makes new keymaps as
216 necessary. The initial place to do bindings is in MAP. */
218 rl_macro_bind (keyseq, macro, map)
219 const char *keyseq, *macro;
225 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
227 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
232 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
236 /* Bind the key sequence represented by the string KEYSEQ to
237 the arbitrary pointer DATA. TYPE says what kind of data is
238 pointed to by DATA, right now this can be a function (ISFUNC),
239 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
240 as necessary. The initial place to do bindings is in MAP. */
242 rl_generic_bind (type, keyseq, data, map)
255 /* If no keys to bind to, exit right away. */
256 if (!keyseq || !*keyseq)
263 keys = (char *)xmalloc (1 + (2 * strlen (keyseq)));
265 /* Translate the ASCII representation of KEYSEQ into an array of
266 characters. Stuff the characters into KEYS, and the length of
267 KEYS into KEYS_LEN. */
268 if (rl_translate_keyseq (keyseq, keys, &keys_len))
274 /* Bind keys, making new keymaps as necessary. */
275 for (i = 0; i < keys_len; i++)
277 unsigned char uc = keys[i];
281 if (ic < 0 || ic >= KEYMAP_SIZE)
284 if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
287 if (map[ESC].type == ISKMAP)
288 map = FUNCTION_TO_KEYMAP (map, ESC);
291 if ((i + 1) < keys_len)
293 if (map[ic].type != ISKMAP)
295 /* We allow subsequences of keys. If a keymap is being
296 created that will `shadow' an existing function or macro
297 key binding, we save that keybinding into the ANYOTHERKEY
298 index in the new map. The dispatch code will look there
299 to find the function to execute if the subsequence is not
300 matched. ANYOTHERKEY was chosen to be greater than
304 map[ic].type = ISKMAP;
305 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
307 map = FUNCTION_TO_KEYMAP (map, ic);
308 /* The dispatch code will return this function if no matching
309 key sequence is found in the keymap. This (with a little
310 help from the dispatch code in readline.c) allows `a' to be
311 mapped to something, `abc' to be mapped to something else,
312 and the function bound to `a' to be executed when the user
313 types `abx', leaving `bx' in the input queue. */
314 if (k.function /* && k.type == ISFUNC */)
316 map[ANYOTHERKEY] = k;
322 if (map[ic].type == ISMACR)
323 free ((char *)map[ic].function);
324 else if (map[ic].type == ISKMAP)
326 map = FUNCTION_TO_KEYMAP (map, ic);
330 map[ic].function = KEYMAP_TO_FUNCTION (data);
334 rl_binding_keymap = map;
340 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
341 an array of characters. LEN gets the final length of ARRAY. Return
342 non-zero if there was an error parsing SEQ. */
344 rl_translate_keyseq (seq, array, len)
349 register int i, c, l, temp;
351 for (i = l = 0; c = seq[i]; i++)
360 /* Handle \C- and \M- prefixes. */
361 if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
363 /* Handle special case of backwards define. */
364 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
366 array[l++] = ESC; /* ESC is meta-prefix */
368 array[l++] = CTRL (_rl_to_upper (seq[i]));
375 array[l++] = ESC; /* ESC is meta-prefix */
380 /* Special hack for C-?... */
381 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
386 /* Translate other backslash-escaped characters. These are the
387 same escape sequences that bash's `echo' and `printf' builtins
388 handle, with the addition of \d -> RUBOUT. A backslash
389 preceding a character that is not special is stripped. */
399 array[l++] = RUBOUT; /* readline-specific */
408 array[l++] = NEWLINE;
422 case '0': case '1': case '2': case '3':
423 case '4': case '5': case '6': case '7':
425 for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++)
426 c = (c * 8) + OCTVALUE (seq[i]);
427 i--; /* auto-increment in for loop */
428 array[l++] = c & largest_char;
432 for (temp = 2, c = 0; ISXDIGIT ((unsigned char)seq[i]) && temp--; i++)
433 c = (c * 16) + HEXVALUE (seq[i]);
436 i--; /* auto-increment in for loop */
437 array[l++] = c & largest_char;
439 default: /* backslashes before non-special chars just add the char */
441 break; /* the backslash is stripped */
455 rl_untranslate_keyseq (seq)
458 static char kseq[16];
470 else if (CTRL_CHAR (c))
475 c = _rl_to_lower (UNCTRL (c));
477 else if (c == RUBOUT)
490 else if (c == '\\' || c == '"')
495 kseq[i++] = (unsigned char) c;
501 _rl_untranslate_macro_value (seq)
507 r = ret = (char *)xmalloc (7 * strlen (seq) + 1);
508 for (s = seq; *s; s++)
518 else if (CTRL_CHAR (c) && c != ESC)
523 c = _rl_to_lower (UNCTRL (c));
525 else if (c == RUBOUT)
538 else if (c == '\\' || c == '"')
541 *r++ = (unsigned char)c;
547 /* Return a pointer to the function that STRING represents.
548 If STRING doesn't have a matching function, then a NULL pointer
551 rl_named_function (string)
556 rl_initialize_funmap ();
558 for (i = 0; funmap[i]; i++)
559 if (_rl_stricmp (funmap[i]->name, string) == 0)
560 return (funmap[i]->function);
561 return ((rl_command_func_t *)NULL);
564 /* Return the function (or macro) definition which would be invoked via
565 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
566 used. TYPE, if non-NULL, is a pointer to an int which will receive the
567 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
568 or ISMACR (macro). */
570 rl_function_of_keyseq (keyseq, map, type)
580 for (i = 0; keyseq && keyseq[i]; i++)
582 unsigned char ic = keyseq[i];
584 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
586 if (map[ESC].type != ISKMAP)
589 *type = map[ESC].type;
591 return (map[ESC].function);
595 map = FUNCTION_TO_KEYMAP (map, ESC);
600 if (map[ic].type == ISKMAP)
602 /* If this is the last key in the key sequence, return the
609 return (map[ic].function);
612 map = FUNCTION_TO_KEYMAP (map, ic);
617 *type = map[ic].type;
619 return (map[ic].function);
622 return ((rl_command_func_t *) NULL);
625 /* The last key bindings file read. */
626 static char *last_readline_init_file = (char *)NULL;
628 /* The file we're currently reading key bindings from. */
629 static const char *current_readline_init_file;
630 static int current_readline_init_include_level;
631 static int current_readline_init_lineno;
633 /* Read FILENAME into a locally-allocated buffer and return the buffer.
634 The size of the buffer is returned in *SIZEP. Returns NULL if any
635 errors were encountered. */
637 _rl_read_file (filename, sizep)
646 if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0)
647 return ((char *)NULL);
649 file_size = (size_t)finfo.st_size;
651 /* check for overflow on very large files */
652 if (file_size != finfo.st_size || file_size + 1 < file_size)
659 return ((char *)NULL);
662 /* Read the file into BUFFER. */
663 buffer = (char *)xmalloc (file_size + 1);
664 i = read (file, buffer, file_size);
670 return ((char *)NULL);
680 /* Re-read the current keybindings file. */
682 rl_re_read_init_file (count, ignore)
686 r = rl_read_init_file ((const char *)NULL);
687 rl_set_keymap_from_edit_mode ();
691 /* Do key bindings from a file. If FILENAME is NULL it defaults
692 to the first non-null filename from this list:
693 1. the filename used for the previous call
694 2. the value of the shell variable `INPUTRC'
696 If the file existed and could be opened and read, 0 is returned,
697 otherwise errno is returned. */
699 rl_read_init_file (filename)
700 const char *filename;
702 /* Default the filename. */
705 filename = last_readline_init_file;
707 filename = sh_get_env_value ("INPUTRC");
709 filename = DEFAULT_INPUTRC;
713 filename = DEFAULT_INPUTRC;
715 #if defined (__MSDOS__)
716 if (_rl_read_init_file (filename, 0) == 0)
718 filename = "~/_inputrc";
720 return (_rl_read_init_file (filename, 0));
724 _rl_read_init_file (filename, include_level)
725 const char *filename;
729 char *buffer, *openname, *line, *end;
732 current_readline_init_file = filename;
733 current_readline_init_include_level = include_level;
735 openname = tilde_expand (filename);
736 buffer = _rl_read_file (openname, &file_size);
742 if (include_level == 0 && filename != last_readline_init_file)
744 FREE (last_readline_init_file);
745 last_readline_init_file = savestring (filename);
748 currently_reading_init_file = 1;
750 /* Loop over the lines in the file. Lines that start with `#' are
751 comments; all other lines are commands for readline initialization. */
752 current_readline_init_lineno = 1;
754 end = buffer + file_size;
757 /* Find the end of this line. */
758 for (i = 0; line + i != end && line[i] != '\n'; i++);
760 #if defined (__CYGWIN__)
761 /* ``Be liberal in what you accept.'' */
762 if (line[i] == '\n' && line[i-1] == '\r')
766 /* Mark end of line. */
769 /* Skip leading whitespace. */
770 while (*line && whitespace (*line))
776 /* If the line is not a comment, then parse it. */
777 if (*line && *line != '#')
778 rl_parse_and_bind (line);
780 /* Move to the next line. */
782 current_readline_init_lineno++;
786 currently_reading_init_file = 0;
791 _rl_init_file_error (msg)
794 if (currently_reading_init_file)
795 fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
796 current_readline_init_lineno, msg);
798 fprintf (stderr, "readline: %s\n", msg);
801 /* **************************************************************** */
803 /* Parser Directives */
805 /* **************************************************************** */
807 typedef int _rl_parser_func_t PARAMS((char *));
809 /* Things that mean `Control'. */
810 const char *_rl_possible_control_prefixes[] = {
811 "Control-", "C-", "CTRL-", (const char *)NULL
814 const char *_rl_possible_meta_prefixes[] = {
815 "Meta", "M-", (const char *)NULL
820 /* Calling programs set this to have their argv[0]. */
821 const char *rl_readline_name = "other";
823 /* Stack of previous values of parsing_conditionalized_out. */
824 static unsigned char *if_stack = (unsigned char *)NULL;
825 static int if_stack_depth;
826 static int if_stack_size;
828 /* Push _rl_parsing_conditionalized_out, and set parser state based
836 /* Push parser state. */
837 if (if_stack_depth + 1 >= if_stack_size)
840 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
842 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
844 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
846 /* If parsing is turned off, then nothing can turn it back on except
847 for finding the matching endif. In that case, return right now. */
848 if (_rl_parsing_conditionalized_out)
851 /* Isolate first argument. */
852 for (i = 0; args[i] && !whitespace (args[i]); i++);
857 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
858 isn't term=foo, or mode=emacs, then check to see if the first
859 word in ARGS is the same as the value stored in rl_readline_name. */
860 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
864 /* Terminals like "aaa-60" are equivalent to "aaa". */
865 tname = savestring (rl_terminal_name);
866 tem = strchr (tname, '-');
870 /* Test the `long' and `short' forms of the terminal name so that
871 if someone has a `sun-cmd' and does not want to have bindings
872 that will be executed if the terminal is a `sun', they can put
873 `$if term=sun-cmd' into their .inputrc. */
874 _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
875 _rl_stricmp (args + 5, rl_terminal_name);
878 #if defined (VI_MODE)
879 else if (_rl_strnicmp (args, "mode=", 5) == 0)
883 if (_rl_stricmp (args + 5, "emacs") == 0)
885 else if (_rl_stricmp (args + 5, "vi") == 0)
890 _rl_parsing_conditionalized_out = mode != rl_editing_mode;
893 /* Check to see if the first word in ARGS is the same as the
894 value stored in rl_readline_name. */
895 else if (_rl_stricmp (args, rl_readline_name) == 0)
896 _rl_parsing_conditionalized_out = 0;
898 _rl_parsing_conditionalized_out = 1;
902 /* Invert the current parser state if there is anything on the stack. */
909 if (if_stack_depth == 0)
911 _rl_init_file_error ("$else found without matching $if");
915 /* Check the previous (n - 1) levels of the stack to make sure that
916 we haven't previously turned off parsing. */
917 for (i = 0; i < if_stack_depth - 1; i++)
918 if (if_stack[i] == 1)
921 /* Invert the state of parsing if at top level. */
922 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
926 /* Terminate a conditional, popping the value of
927 _rl_parsing_conditionalized_out from the stack. */
933 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
935 _rl_init_file_error ("$endif without matching $if");
940 parser_include (args)
943 const char *old_init_file;
945 int old_line_number, old_include_level, r;
947 if (_rl_parsing_conditionalized_out)
950 old_init_file = current_readline_init_file;
951 old_line_number = current_readline_init_lineno;
952 old_include_level = current_readline_init_include_level;
954 e = strchr (args, '\n');
957 r = _rl_read_init_file ((const char *)args, old_include_level + 1);
959 current_readline_init_file = old_init_file;
960 current_readline_init_lineno = old_line_number;
961 current_readline_init_include_level = old_include_level;
966 /* Associate textual names with actual functions. */
969 _rl_parser_func_t *function;
970 } parser_directives [] = {
972 { "endif", parser_endif },
973 { "else", parser_else },
974 { "include", parser_include },
975 { (char *)0x0, (_rl_parser_func_t *)0x0 }
978 /* Handle a parser directive. STATEMENT is the line of the directive
979 without any leading `$'. */
981 handle_parser_directive (statement)
985 char *directive, *args;
987 /* Isolate the actual directive. */
989 /* Skip whitespace. */
990 for (i = 0; whitespace (statement[i]); i++);
992 directive = &statement[i];
994 for (; statement[i] && !whitespace (statement[i]); i++);
997 statement[i++] = '\0';
999 for (; statement[i] && whitespace (statement[i]); i++);
1001 args = &statement[i];
1003 /* Lookup the command, and act on it. */
1004 for (i = 0; parser_directives[i].name; i++)
1005 if (_rl_stricmp (directive, parser_directives[i].name) == 0)
1007 (*parser_directives[i].function) (args);
1011 /* display an error message about the unknown parser directive */
1012 _rl_init_file_error ("unknown parser directive");
1016 /* Read the binding command from STRING and perform it.
1017 A key binding command looks like: Keyname: function-name\0,
1018 a variable binding command looks like: set variable value.
1019 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
1021 rl_parse_and_bind (string)
1024 char *funname, *kname;
1026 int key, equivalency;
1028 while (string && whitespace (*string))
1031 if (!string || !*string || *string == '#')
1034 /* If this is a parser directive, act on it. */
1037 handle_parser_directive (&string[1]);
1041 /* If we aren't supposed to be parsing right now, then we're done. */
1042 if (_rl_parsing_conditionalized_out)
1046 /* If this keyname is a complex key expression surrounded by quotes,
1047 advance to after the matching close quote. This code allows the
1048 backslash to quote characters in the key expression. */
1053 for (i = 1; c = string[i]; i++)
1070 /* If we didn't find a closing quote, abort the line. */
1071 if (string[i] == '\0')
1073 _rl_init_file_error ("no closing `\"' in key binding");
1078 /* Advance to the colon (:) or whitespace which separates the two objects. */
1079 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
1081 equivalency = (c == ':' && string[i + 1] == '=');
1083 /* Mark the end of the command (or keyname). */
1087 /* If doing assignment, skip the '=' sign as well. */
1091 /* If this is a command to set a variable, then do that. */
1092 if (_rl_stricmp (string, "set") == 0)
1094 char *var = string + i;
1097 /* Make VAR point to start of variable name. */
1098 while (*var && whitespace (*var)) var++;
1100 /* Make VALUE point to start of value string. */
1102 while (*value && !whitespace (*value)) value++;
1105 while (*value && whitespace (*value)) value++;
1107 rl_variable_bind (var, value);
1111 /* Skip any whitespace between keyname and funname. */
1112 for (; string[i] && whitespace (string[i]); i++);
1113 funname = &string[i];
1115 /* Now isolate funname.
1116 For straight function names just look for whitespace, since
1117 that will signify the end of the string. But this could be a
1118 macro definition. In that case, the string is quoted, so skip
1119 to the matching delimiter. We allow the backslash to quote the
1120 delimiter characters in the macro body. */
1121 /* This code exists to allow whitespace in macro expansions, which
1122 would otherwise be gobbled up by the next `for' loop.*/
1123 /* XXX - it may be desirable to allow backslash quoting only if " is
1124 the quoted string delimiter, like the shell. */
1125 if (*funname == '\'' || *funname == '"')
1127 int delimiter = string[i++], passc;
1129 for (passc = 0; c = string[i]; i++)
1150 /* Advance to the end of the string. */
1151 for (; string[i] && !whitespace (string[i]); i++);
1153 /* No extra whitespace at the end of the string. */
1156 /* Handle equivalency bindings here. Make the left-hand side be exactly
1157 whatever the right-hand evaluates to, including keymaps. */
1163 /* If this is a new-style key-binding, then do the binding with
1164 rl_set_key (). Otherwise, let the older code deal with it. */
1168 register int j, k, passc;
1170 seq = (char *)xmalloc (1 + strlen (string));
1171 for (j = 1, k = passc = 0; string[j]; j++)
1173 /* Allow backslash to quote characters, but leave them in place.
1174 This allows a string to end with a backslash quoting another
1175 backslash, or with a backslash quoting a double quote. The
1176 backslashes are left in place for rl_translate_keyseq (). */
1177 if (passc || (string[j] == '\\'))
1179 seq[k++] = string[j];
1184 if (string[j] == '"')
1187 seq[k++] = string[j];
1191 /* Binding macro? */
1192 if (*funname == '\'' || *funname == '"')
1194 j = strlen (funname);
1196 /* Remove the delimiting quotes from each end of FUNNAME. */
1197 if (j && funname[j - 1] == *funname)
1198 funname[j - 1] = '\0';
1200 rl_macro_bind (seq, &funname[1], _rl_keymap);
1203 rl_set_key (seq, rl_named_function (funname), _rl_keymap);
1209 /* Get the actual character we want to deal with. */
1210 kname = strrchr (string, '-');
1216 key = glean_key_from_name (kname);
1218 /* Add in control and meta bits. */
1219 if (substring_member_of_array (string, _rl_possible_control_prefixes))
1220 key = CTRL (_rl_to_upper (key));
1222 if (substring_member_of_array (string, _rl_possible_meta_prefixes))
1225 /* Temporary. Handle old-style keyname with macro-binding. */
1226 if (*funname == '\'' || *funname == '"')
1229 int fl = strlen (funname);
1231 useq[0] = key; useq[1] = '\0';
1232 if (fl && funname[fl - 1] == *funname)
1233 funname[fl - 1] = '\0';
1235 rl_macro_bind (useq, &funname[1], _rl_keymap);
1237 #if defined (PREFIX_META_HACK)
1238 /* Ugly, but working hack to keep prefix-meta around. */
1239 else if (_rl_stricmp (funname, "prefix-meta") == 0)
1245 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1247 #endif /* PREFIX_META_HACK */
1249 rl_bind_key (key, rl_named_function (funname));
1253 /* Simple structure for boolean readline variables (i.e., those that can
1254 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1257 #define V_SPECIAL 0x1
1263 } boolean_varlist [] = {
1264 { "blink-matching-paren", &rl_blink_matching_paren, V_SPECIAL },
1265 { "byte-oriented", &rl_byte_oriented, 0 },
1266 { "completion-ignore-case", &_rl_completion_case_fold, 0 },
1267 { "convert-meta", &_rl_convert_meta_chars_to_ascii, 0 },
1268 { "disable-completion", &rl_inhibit_completion, 0 },
1269 { "enable-keypad", &_rl_enable_keypad, 0 },
1270 { "expand-tilde", &rl_complete_with_tilde_expansion, 0 },
1271 { "history-preserve-point", &_rl_history_preserve_point, 0 },
1272 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode, 0 },
1273 { "input-meta", &_rl_meta_flag, 0 },
1274 { "mark-directories", &_rl_complete_mark_directories, 0 },
1275 { "mark-modified-lines", &_rl_mark_modified_lines, 0 },
1276 { "mark-symlinked-directories", &_rl_complete_mark_symlink_dirs, 0 },
1277 { "match-hidden-files", &_rl_match_hidden_files, 0 },
1278 { "meta-flag", &_rl_meta_flag, 0 },
1279 { "output-meta", &_rl_output_meta_chars, 0 },
1280 { "page-completions", &_rl_page_completions, 0 },
1281 { "prefer-visible-bell", &_rl_prefer_visible_bell, V_SPECIAL },
1282 { "print-completions-horizontally", &_rl_print_completions_horizontally, 0 },
1283 { "show-all-if-ambiguous", &_rl_complete_show_all, 0 },
1284 #if defined (VISIBLE_STATS)
1285 { "visible-stats", &rl_visible_stats, 0 },
1286 #endif /* VISIBLE_STATS */
1287 { (char *)NULL, (int *)NULL }
1291 find_boolean_var (name)
1296 for (i = 0; boolean_varlist[i].name; i++)
1297 if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1302 /* Hooks for handling special boolean variables, where a
1303 function needs to be called or another variable needs
1304 to be changed when they're changed. */
1306 hack_special_boolean_var (i)
1311 name = boolean_varlist[i].name;
1313 if (_rl_stricmp (name, "blink-matching-paren") == 0)
1314 _rl_enable_paren_matching (rl_blink_matching_paren);
1315 else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1317 if (_rl_prefer_visible_bell)
1318 _rl_bell_preference = VISIBLE_BELL;
1320 _rl_bell_preference = AUDIBLE_BELL;
1324 typedef int _rl_sv_func_t PARAMS((const char *));
1326 /* These *must* correspond to the array indices for the appropriate
1327 string variable. (Though they're not used right now.) */
1328 #define V_BELLSTYLE 0
1329 #define V_COMBEGIN 1
1330 #define V_EDITMODE 2
1331 #define V_ISRCHTERM 3
1337 /* Forward declarations */
1338 static int sv_bell_style PARAMS((const char *));
1339 static int sv_combegin PARAMS((const char *));
1340 static int sv_compquery PARAMS((const char *));
1341 static int sv_editmode PARAMS((const char *));
1342 static int sv_isrchterm PARAMS((const char *));
1343 static int sv_keymap PARAMS((const char *));
1348 _rl_sv_func_t *set_func;
1349 } string_varlist[] = {
1350 { "bell-style", V_STRING, sv_bell_style },
1351 { "comment-begin", V_STRING, sv_combegin },
1352 { "completion-query-items", V_INT, sv_compquery },
1353 { "editing-mode", V_STRING, sv_editmode },
1354 { "isearch-terminators", V_STRING, sv_isrchterm },
1355 { "keymap", V_STRING, sv_keymap },
1360 find_string_var (name)
1365 for (i = 0; string_varlist[i].name; i++)
1366 if (_rl_stricmp (name, string_varlist[i].name) == 0)
1371 /* A boolean value that can appear in a `set variable' command is true if
1372 the value is null or empty, `on' (case-insenstive), or "1". Any other
1373 values result in 0 (false). */
1378 return (value == 0 || *value == '\0' ||
1379 (_rl_stricmp (value, "on") == 0) ||
1380 (value[0] == '1' && value[1] == '\0'));
1384 rl_variable_bind (name, value)
1385 const char *name, *value;
1390 /* Check for simple variables first. */
1391 i = find_boolean_var (name);
1394 *boolean_varlist[i].value = bool_to_int (value);
1395 if (boolean_varlist[i].flags & V_SPECIAL)
1396 hack_special_boolean_var (i);
1400 i = find_string_var (name);
1402 /* For the time being, unknown variable names or string names without a
1403 handler function are simply ignored. */
1404 if (i < 0 || string_varlist[i].set_func == 0)
1407 v = (*string_varlist[i].set_func) (value);
1415 if (_rl_strnicmp (value, "vi", 2) == 0)
1417 #if defined (VI_MODE)
1418 _rl_keymap = vi_insertion_keymap;
1419 rl_editing_mode = vi_mode;
1420 #endif /* VI_MODE */
1423 else if (_rl_strnicmp (value, "emacs", 5) == 0)
1425 _rl_keymap = emacs_standard_keymap;
1426 rl_editing_mode = emacs_mode;
1436 if (value && *value)
1438 FREE (_rl_comment_begin);
1439 _rl_comment_begin = savestring (value);
1446 sv_compquery (value)
1451 if (value && *value)
1453 nval = atoi (value);
1457 rl_completion_query_items = nval;
1467 kmap = rl_get_keymap_by_name (value);
1470 rl_set_keymap (kmap);
1477 sv_bell_style (value)
1480 if (value == 0 || *value == '\0')
1481 _rl_bell_preference = AUDIBLE_BELL;
1482 else if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1483 _rl_bell_preference = NO_BELL;
1484 else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1485 _rl_bell_preference = AUDIBLE_BELL;
1486 else if (_rl_stricmp (value, "visible") == 0)
1487 _rl_bell_preference = VISIBLE_BELL;
1494 sv_isrchterm (value)
1497 int beg, end, delim;
1503 /* Isolate the value and translate it into a character string. */
1504 v = savestring (value);
1505 FREE (_rl_isearch_terminators);
1506 if (v[0] == '"' || v[0] == '\'')
1509 for (beg = end = 1; v[end] && v[end] != delim; end++)
1514 for (beg = end = 0; whitespace (v[end]) == 0; end++)
1520 /* The value starts at v + beg. Translate it into a character string. */
1521 _rl_isearch_terminators = (char *)xmalloc (2 * strlen (v) + 1);
1522 rl_translate_keyseq (v + beg, _rl_isearch_terminators, &end);
1523 _rl_isearch_terminators[end] = '\0';
1529 /* Return the character which matches NAME.
1530 For example, `Space' returns ' '. */
1537 static assoc_list name_key_alist[] = {
1540 { "Escape", '\033' },
1542 { "Newline", '\n' },
1553 glean_key_from_name (name)
1558 for (i = 0; name_key_alist[i].name; i++)
1559 if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1560 return (name_key_alist[i].value);
1562 return (*(unsigned char *)name); /* XXX was return (*name) */
1565 /* Auxiliary functions to manage keymaps. */
1569 } keymap_names[] = {
1570 { "emacs", emacs_standard_keymap },
1571 { "emacs-standard", emacs_standard_keymap },
1572 { "emacs-meta", emacs_meta_keymap },
1573 { "emacs-ctlx", emacs_ctlx_keymap },
1574 #if defined (VI_MODE)
1575 { "vi", vi_movement_keymap },
1576 { "vi-move", vi_movement_keymap },
1577 { "vi-command", vi_movement_keymap },
1578 { "vi-insert", vi_insertion_keymap },
1579 #endif /* VI_MODE */
1580 { (char *)0x0, (Keymap)0x0 }
1584 rl_get_keymap_by_name (name)
1589 for (i = 0; keymap_names[i].name; i++)
1590 if (_rl_stricmp (name, keymap_names[i].name) == 0)
1591 return (keymap_names[i].map);
1592 return ((Keymap) NULL);
1596 rl_get_keymap_name (map)
1600 for (i = 0; keymap_names[i].name; i++)
1601 if (map == keymap_names[i].map)
1602 return ((char *)keymap_names[i].name);
1603 return ((char *)NULL);
1617 return (_rl_keymap);
1621 rl_set_keymap_from_edit_mode ()
1623 if (rl_editing_mode == emacs_mode)
1624 _rl_keymap = emacs_standard_keymap;
1625 #if defined (VI_MODE)
1626 else if (rl_editing_mode == vi_mode)
1627 _rl_keymap = vi_insertion_keymap;
1628 #endif /* VI_MODE */
1632 rl_get_keymap_name_from_edit_mode ()
1634 if (rl_editing_mode == emacs_mode)
1636 #if defined (VI_MODE)
1637 else if (rl_editing_mode == vi_mode)
1639 #endif /* VI_MODE */
1644 /* **************************************************************** */
1646 /* Key Binding and Function Information */
1648 /* **************************************************************** */
1650 /* Each of the following functions produces information about the
1651 state of keybindings and functions known to Readline. The info
1652 is always printed to rl_outstream, and in such a way that it can
1653 be read back in (i.e., passed to rl_parse_and_bind (). */
1655 /* Print the names of functions known to Readline. */
1657 rl_list_funmap_names ()
1660 const char **funmap_names;
1662 funmap_names = rl_funmap_names ();
1667 for (i = 0; funmap_names[i]; i++)
1668 fprintf (rl_outstream, "%s\n", funmap_names[i]);
1670 free (funmap_names);
1674 _rl_get_keyname (key)
1680 keyname = (char *)xmalloc (8);
1683 /* Since this is going to be used to write out keysequence-function
1684 pairs for possible inclusion in an inputrc file, we don't want to
1685 do any special meta processing on KEY. */
1688 /* XXX - Experimental */
1689 /* We might want to do this, but the old version of the code did not. */
1691 /* If this is an escape character, we don't want to do any more processing.
1692 Just add the special ESC key sequence and return. */
1702 /* RUBOUT is translated directly into \C-? */
1714 /* Now add special prefixes needed for control characters. This can
1715 potentially change C. */
1718 keyname[i++] = '\\';
1721 c = _rl_to_lower (UNCTRL (c));
1724 /* XXX experimental code. Turn the characters that are not ASCII or
1725 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
1727 if (c >= 128 && c <= 159)
1729 keyname[i++] = '\\';
1732 keyname[i++] = (c / 8) + '0';
1736 /* Now, if the character needs to be quoted with a backslash, do that. */
1737 if (c == '\\' || c == '"')
1738 keyname[i++] = '\\';
1740 /* Now add the key, terminate the string, and return it. */
1741 keyname[i++] = (char) c;
1747 /* Return a NULL terminated array of strings which represent the key
1748 sequences that are used to invoke FUNCTION in MAP. */
1750 rl_invoking_keyseqs_in_map (function, map)
1751 rl_command_func_t *function;
1756 int result_index, result_size;
1758 result = (char **)NULL;
1759 result_index = result_size = 0;
1761 for (key = 0; key < KEYMAP_SIZE; key++)
1763 switch (map[key].type)
1766 /* Macros match, if, and only if, the pointers are identical.
1767 Thus, they are treated exactly like functions in here. */
1769 /* If the function in the keymap is the one we are looking for,
1770 then add the current KEY to the list of invoking keys. */
1771 if (map[key].function == function)
1775 keyname = _rl_get_keyname (key);
1777 if (result_index + 2 > result_size)
1780 result = (char **)xrealloc (result, result_size * sizeof (char *));
1783 result[result_index++] = keyname;
1784 result[result_index] = (char *)NULL;
1793 /* Find the list of keyseqs in this map which have FUNCTION as
1794 their target. Add the key sequences found to RESULT. */
1795 if (map[key].function)
1797 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1804 for (i = 0; seqs[i]; i++)
1806 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1810 sprintf (keyname, "\\e");
1812 /* XXX - experimental */
1813 sprintf (keyname, "\\M-");
1815 else if (CTRL_CHAR (key))
1816 sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1817 else if (key == RUBOUT)
1818 sprintf (keyname, "\\C-?");
1819 else if (key == '\\' || key == '"')
1822 keyname[1] = (char) key;
1827 keyname[0] = (char) key;
1831 strcat (keyname, seqs[i]);
1834 if (result_index + 2 > result_size)
1837 result = (char **)xrealloc (result, result_size * sizeof (char *));
1840 result[result_index++] = keyname;
1841 result[result_index] = (char *)NULL;
1852 /* Return a NULL terminated array of strings which represent the key
1853 sequences that can be used to invoke FUNCTION using the current keymap. */
1855 rl_invoking_keyseqs (function)
1856 rl_command_func_t *function;
1858 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1861 /* Print all of the functions and their bindings to rl_outstream. If
1862 PRINT_READABLY is non-zero, then print the output in such a way
1863 that it can be read back in. */
1865 rl_function_dumper (print_readably)
1872 names = rl_funmap_names ();
1874 fprintf (rl_outstream, "\n");
1876 for (i = 0; name = names[i]; i++)
1878 rl_command_func_t *function;
1881 function = rl_named_function (name);
1882 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1887 fprintf (rl_outstream, "# %s (not bound)\n", name);
1892 for (j = 0; invokers[j]; j++)
1894 fprintf (rl_outstream, "\"%s\": %s\n",
1905 fprintf (rl_outstream, "%s is not bound to any keys\n",
1911 fprintf (rl_outstream, "%s can be found on ", name);
1913 for (j = 0; invokers[j] && j < 5; j++)
1915 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1916 invokers[j + 1] ? ", " : ".\n");
1919 if (j == 5 && invokers[j])
1920 fprintf (rl_outstream, "...\n");
1922 for (j = 0; invokers[j]; j++)
1931 /* Print all of the current functions and their bindings to
1932 rl_outstream. If an explicit argument is given, then print
1933 the output in such a way that it can be read back in. */
1935 rl_dump_functions (count, key)
1939 fprintf (rl_outstream, "\r\n");
1940 rl_function_dumper (rl_explicit_arg);
1946 _rl_macro_dumper_internal (print_readably, map, prefix)
1952 char *keyname, *out;
1955 for (key = 0; key < KEYMAP_SIZE; key++)
1957 switch (map[key].type)
1960 keyname = _rl_get_keyname (key);
1961 out = _rl_untranslate_macro_value ((char *)map[key].function);
1964 fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
1968 fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
1977 prefix_len = prefix ? strlen (prefix) : 0;
1980 keyname = (char *)xmalloc (3 + prefix_len);
1982 strcpy (keyname, prefix);
1983 keyname[prefix_len] = '\\';
1984 keyname[prefix_len + 1] = 'e';
1985 keyname[prefix_len + 2] = '\0';
1989 keyname = _rl_get_keyname (key);
1992 out = (char *)xmalloc (strlen (keyname) + prefix_len + 1);
1993 strcpy (out, prefix);
1994 strcpy (out + prefix_len, keyname);
2000 _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
2008 rl_macro_dumper (print_readably)
2011 _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
2015 rl_dump_macros (count, key)
2019 fprintf (rl_outstream, "\r\n");
2020 rl_macro_dumper (rl_explicit_arg);
2026 rl_variable_dumper (print_readably)
2032 for (i = 0; boolean_varlist[i].name; i++)
2035 fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
2036 *boolean_varlist[i].value ? "on" : "off");
2038 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
2039 *boolean_varlist[i].value ? "on" : "off");
2043 switch (_rl_bell_preference)
2046 kname = "none"; break;
2048 kname = "visible"; break;
2051 kname = "audible"; break;
2054 fprintf (rl_outstream, "set bell-style %s\n", kname);
2056 fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
2060 fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
2062 fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
2064 /* completion-query-items */
2066 fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items);
2068 fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items);
2072 fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
2074 fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
2076 /* isearch-terminators */
2077 if (_rl_isearch_terminators)
2081 disp = _rl_untranslate_macro_value (_rl_isearch_terminators);
2084 fprintf (rl_outstream, "set isearch-terminators \"%s\"\n", disp);
2086 fprintf (rl_outstream, "isearch-terminators is set to \"%s\"\n", disp);
2092 kname = rl_get_keymap_name (_rl_keymap);
2094 kname = rl_get_keymap_name_from_edit_mode ();
2096 fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none");
2098 fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none");
2101 /* Print all of the current variables and their values to
2102 rl_outstream. If an explicit argument is given, then print
2103 the output in such a way that it can be read back in. */
2105 rl_dump_variables (count, key)
2109 fprintf (rl_outstream, "\r\n");
2110 rl_variable_dumper (rl_explicit_arg);
2115 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right
2116 now, this is always used to attempt to bind the arrow keys, hence the
2117 check for rl_vi_movement_mode. */
2119 _rl_bind_if_unbound (keyseq, default_func)
2121 rl_command_func_t *default_func;
2123 rl_command_func_t *func;
2127 func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
2128 #if defined (VI_MODE)
2129 if (!func || func == rl_do_lowercase_version || func == rl_vi_movement_mode)
2131 if (!func || func == rl_do_lowercase_version)
2133 rl_set_key (keyseq, default_func, _rl_keymap);
2137 /* Return non-zero if any members of ARRAY are a substring in STRING. */
2139 substring_member_of_array (string, array)
2145 if (_rl_strindex (string, *array))