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 1, 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 675 Mass Ave, Cambridge, MA 02139, 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 */
52 #include "posixstat.h"
54 /* System-specific feature definitions and include files. */
57 /* Some standard library routines. */
61 #if !defined (strchr) && !defined (__STDC__)
62 extern char *strchr (), *strrchr ();
63 #endif /* !strchr && !__STDC__ */
65 extern int _rl_horizontal_scroll_mode;
66 extern int _rl_mark_modified_lines;
67 extern int _rl_bell_preference;
68 extern int _rl_meta_flag;
69 extern int _rl_convert_meta_chars_to_ascii;
70 extern int _rl_output_meta_chars;
71 extern int _rl_complete_show_all;
72 extern int _rl_complete_mark_directories;
73 extern int _rl_print_completions_horizontally;
74 extern int _rl_completion_case_fold;
75 extern int _rl_enable_keypad;
76 #if defined (PAREN_MATCHING)
77 extern int rl_blink_matching_paren;
78 #endif /* PAREN_MATCHING */
79 #if defined (VISIBLE_STATS)
80 extern int rl_visible_stats;
81 #endif /* VISIBLE_STATS */
82 extern int rl_complete_with_tilde_expansion;
83 extern int rl_completion_query_items;
84 extern int rl_inhibit_completion;
85 extern char *_rl_comment_begin;
86 extern unsigned char *_rl_isearch_terminators;
88 extern int rl_explicit_arg;
89 extern int rl_editing_mode;
90 extern unsigned char _rl_parsing_conditionalized_out;
91 extern Keymap _rl_keymap;
93 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
95 /* Functions imported from funmap.c */
96 extern char **rl_funmap_names ();
97 extern int rl_add_funmap_entry ();
99 /* Functions imported from util.c */
100 extern char *_rl_strindex ();
102 /* Functions imported from shell.c */
103 extern char *get_env_value ();
105 /* Variables exported by this file. */
106 Keymap rl_binding_keymap;
108 /* Forward declarations */
109 void rl_set_keymap_from_edit_mode ();
111 static int _rl_read_init_file ();
112 static int glean_key_from_name ();
113 static int substring_member_of_array ();
115 extern char *xmalloc (), *xrealloc ();
117 /* **************************************************************** */
121 /* **************************************************************** */
123 /* rl_add_defun (char *name, Function *function, int key)
124 Add NAME to the list of named functions. Make FUNCTION be the function
125 that gets called. If KEY is not -1, then bind it. */
127 rl_add_defun (name, function, key)
133 rl_bind_key (key, function);
134 rl_add_funmap_entry (name, function);
138 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
140 rl_bind_key (key, function)
147 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
149 if (_rl_keymap[ESC].type == ISKMAP)
153 escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
155 escmap[key].type = ISFUNC;
156 escmap[key].function = function;
162 _rl_keymap[key].type = ISFUNC;
163 _rl_keymap[key].function = function;
164 rl_binding_keymap = _rl_keymap;
168 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
171 rl_bind_key_in_map (key, function, map)
181 result = rl_bind_key (key, function);
186 /* Make KEY do nothing in the currently selected keymap.
187 Returns non-zero in case of error. */
192 return (rl_bind_key (key, (Function *)NULL));
195 /* Make KEY do nothing in MAP.
196 Returns non-zero in case of error. */
198 rl_unbind_key_in_map (key, map)
202 return (rl_bind_key_in_map (key, (Function *)NULL, map));
205 /* Unbind all keys bound to FUNCTION in MAP. */
207 rl_unbind_function_in_map (func, map)
211 register int i, rval;
213 for (i = rval = 0; i < KEYMAP_SIZE; i++)
215 if (map[i].type == ISFUNC && map[i].function == func)
217 map[i].function = (Function *)NULL;
225 rl_unbind_command_in_map (command, map)
231 func = rl_named_function (command);
234 return (rl_unbind_function_in_map (func, map));
237 /* Bind the key sequence represented by the string KEYSEQ to
238 FUNCTION. This makes new keymaps as necessary. The initial
239 place to do bindings is in MAP. */
241 rl_set_key (keyseq, function, map)
246 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
249 /* Bind the key sequence represented by the string KEYSEQ to
250 the string of characters MACRO. This makes new keymaps as
251 necessary. The initial place to do bindings is in MAP. */
253 rl_macro_bind (keyseq, macro, map)
254 char *keyseq, *macro;
260 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
262 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
267 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
271 /* Bind the key sequence represented by the string KEYSEQ to
272 the arbitrary pointer DATA. TYPE says what kind of data is
273 pointed to by DATA, right now this can be a function (ISFUNC),
274 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
275 as necessary. The initial place to do bindings is in MAP. */
277 rl_generic_bind (type, keyseq, data, map)
286 /* If no keys to bind to, exit right away. */
287 if (!keyseq || !*keyseq)
294 keys = xmalloc (1 + (2 * strlen (keyseq)));
296 /* Translate the ASCII representation of KEYSEQ into an array of
297 characters. Stuff the characters into KEYS, and the length of
298 KEYS into KEYS_LEN. */
299 if (rl_translate_keyseq (keyseq, keys, &keys_len))
305 /* Bind keys, making new keymaps as necessary. */
306 for (i = 0; i < keys_len; i++)
308 int ic = (int) ((unsigned char)keys[i]);
310 if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
313 if (map[ESC].type == ISKMAP)
314 map = FUNCTION_TO_KEYMAP (map, ESC);
317 if ((i + 1) < keys_len)
319 if (map[ic].type != ISKMAP)
321 if (map[ic].type == ISMACR)
322 free ((char *)map[ic].function);
324 map[ic].type = ISKMAP;
325 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
327 map = FUNCTION_TO_KEYMAP (map, ic);
331 if (map[ic].type == ISMACR)
332 free ((char *)map[ic].function);
334 map[ic].function = KEYMAP_TO_FUNCTION (data);
338 rl_binding_keymap = map;
344 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
345 an array of characters. LEN gets the final length of ARRAY. Return
346 non-zero if there was an error parsing SEQ. */
348 rl_translate_keyseq (seq, array, len)
352 register int i, c, l, temp;
354 for (i = l = 0; c = seq[i]; i++)
363 /* Handle \C- and \M- prefixes. */
364 if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
366 /* Handle special case of backwards define. */
367 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
371 array[l++] = CTRL (_rl_to_upper (seq[i]));
378 array[l++] = ESC; /* XXX */
383 /* Special hack for C-?... */
384 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
389 /* Translate other backslash-escaped characters. These are the
390 same escape sequences that bash's `echo' and `printf' builtins
391 handle, with the addition of \d -> RUBOUT. A backslash
392 preceding a character that is not special is stripped. */
402 array[l++] = RUBOUT; /* readline-specific */
411 array[l++] = NEWLINE;
425 case '0': case '1': case '2': case '3':
426 case '4': case '5': case '6': case '7':
428 for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++)
429 c = (c * 8) + OCTVALUE (seq[i]);
430 i--; /* auto-increment in for loop */
431 array[l++] = c % (largest_char + 1);
435 for (temp = 3, c = 0; isxdigit (seq[i]) && temp--; i++)
436 c = (c * 16) + HEXVALUE (seq[i]);
439 i--; /* auto-increment in for loop */
440 array[l++] = c % (largest_char + 1);
442 default: /* backslashes before non-special chars just add the char */
444 break; /* the backslash is stripped */
458 rl_untranslate_keyseq (seq)
461 static char kseq[16];
473 else if (CTRL_CHAR (c))
478 c = _rl_to_lower (UNCTRL (c));
480 else if (c == RUBOUT)
493 else if (c == '\\' || c == '"')
498 kseq[i++] = (unsigned char) c;
504 _rl_untranslate_macro_value (seq)
510 r = ret = xmalloc (7 * strlen (seq) + 1);
511 for (s = seq; *s; s++)
521 else if (CTRL_CHAR (c) && c != ESC)
526 c = _rl_to_lower (UNCTRL (c));
528 else if (c == RUBOUT)
541 else if (c == '\\' || c == '"')
544 *r++ = (unsigned char)c;
550 /* Return a pointer to the function that STRING represents.
551 If STRING doesn't have a matching function, then a NULL pointer
554 rl_named_function (string)
559 rl_initialize_funmap ();
561 for (i = 0; funmap[i]; i++)
562 if (_rl_stricmp (funmap[i]->name, string) == 0)
563 return (funmap[i]->function);
564 return ((Function *)NULL);
567 /* Return the function (or macro) definition which would be invoked via
568 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
569 used. TYPE, if non-NULL, is a pointer to an int which will receive the
570 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
571 or ISMACR (macro). */
573 rl_function_of_keyseq (keyseq, map, type)
583 for (i = 0; keyseq && keyseq[i]; i++)
587 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
589 if (map[ESC].type != ISKMAP)
592 *type = map[ESC].type;
594 return (map[ESC].function);
598 map = FUNCTION_TO_KEYMAP (map, ESC);
603 if (map[ic].type == ISKMAP)
605 /* If this is the last key in the key sequence, return the
612 return (map[ic].function);
615 map = FUNCTION_TO_KEYMAP (map, ic);
620 *type = map[ic].type;
622 return (map[ic].function);
625 return ((Function *) NULL);
628 /* The last key bindings file read. */
629 static char *last_readline_init_file = (char *)NULL;
631 /* The file we're currently reading key bindings from. */
632 static char *current_readline_init_file;
633 static int current_readline_init_include_level;
634 static int current_readline_init_lineno;
636 /* Read FILENAME into a locally-allocated buffer and return the buffer.
637 The size of the buffer is returned in *SIZEP. Returns NULL if any
638 errors were encountered. */
640 _rl_read_file (filename, sizep)
649 if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0)
650 return ((char *)NULL);
652 file_size = (size_t)finfo.st_size;
654 /* check for overflow on very large files */
655 if (file_size != finfo.st_size || file_size + 1 < file_size)
662 return ((char *)NULL);
665 /* Read the file into BUFFER. */
666 buffer = (char *)xmalloc (file_size + 1);
667 i = read (file, buffer, file_size);
677 return ((char *)NULL);
680 buffer[file_size] = '\0';
686 /* Re-read the current keybindings file. */
688 rl_re_read_init_file (count, ignore)
692 r = rl_read_init_file ((char *)NULL);
693 rl_set_keymap_from_edit_mode ();
697 /* Do key bindings from a file. If FILENAME is NULL it defaults
698 to the first non-null filename from this list:
699 1. the filename used for the previous call
700 2. the value of the shell variable `INPUTRC'
702 If the file existed and could be opened and read, 0 is returned,
703 otherwise errno is returned. */
705 rl_read_init_file (filename)
708 /* Default the filename. */
711 filename = last_readline_init_file;
713 filename = get_env_value ("INPUTRC");
715 filename = DEFAULT_INPUTRC;
719 filename = DEFAULT_INPUTRC;
721 return (_rl_read_init_file (filename, 0));
725 _rl_read_init_file (filename, include_level)
730 char *buffer, *openname, *line, *end;
733 current_readline_init_file = filename;
734 current_readline_init_include_level = include_level;
736 openname = tilde_expand (filename);
737 buffer = _rl_read_file (openname, &file_size);
743 if (include_level == 0 && filename != last_readline_init_file)
745 FREE (last_readline_init_file);
746 last_readline_init_file = savestring (filename);
749 /* Loop over the lines in the file. Lines that start with `#' are
750 comments; all other lines are commands for readline initialization. */
751 current_readline_init_lineno = 1;
753 end = buffer + file_size;
756 /* Find the end of this line. */
757 for (i = 0; line + i != end && line[i] != '\n'; i++);
759 /* Mark end of line. */
762 /* Skip leading whitespace. */
763 while (*line && whitespace (*line))
769 /* If the line is not a comment, then parse it. */
770 if (*line && *line != '#')
771 rl_parse_and_bind (line);
773 /* Move to the next line. */
775 current_readline_init_lineno++;
783 _rl_init_file_error (msg)
786 fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
787 current_readline_init_lineno,
791 /* **************************************************************** */
793 /* Parser Directives */
795 /* **************************************************************** */
799 /* Calling programs set this to have their argv[0]. */
800 char *rl_readline_name = "other";
802 /* Stack of previous values of parsing_conditionalized_out. */
803 static unsigned char *if_stack = (unsigned char *)NULL;
804 static int if_stack_depth;
805 static int if_stack_size;
807 /* Push _rl_parsing_conditionalized_out, and set parser state based
815 /* Push parser state. */
816 if (if_stack_depth + 1 >= if_stack_size)
819 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
821 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
823 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
825 /* If parsing is turned off, then nothing can turn it back on except
826 for finding the matching endif. In that case, return right now. */
827 if (_rl_parsing_conditionalized_out)
830 /* Isolate first argument. */
831 for (i = 0; args[i] && !whitespace (args[i]); i++);
836 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
837 isn't term=foo, or mode=emacs, then check to see if the first
838 word in ARGS is the same as the value stored in rl_readline_name. */
839 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
843 /* Terminals like "aaa-60" are equivalent to "aaa". */
844 tname = savestring (rl_terminal_name);
845 tem = strchr (tname, '-');
849 /* Test the `long' and `short' forms of the terminal name so that
850 if someone has a `sun-cmd' and does not want to have bindings
851 that will be executed if the terminal is a `sun', they can put
852 `$if term=sun-cmd' into their .inputrc. */
853 _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
854 _rl_stricmp (args + 5, rl_terminal_name);
857 #if defined (VI_MODE)
858 else if (_rl_strnicmp (args, "mode=", 5) == 0)
862 if (_rl_stricmp (args + 5, "emacs") == 0)
864 else if (_rl_stricmp (args + 5, "vi") == 0)
869 _rl_parsing_conditionalized_out = mode != rl_editing_mode;
872 /* Check to see if the first word in ARGS is the same as the
873 value stored in rl_readline_name. */
874 else if (_rl_stricmp (args, rl_readline_name) == 0)
875 _rl_parsing_conditionalized_out = 0;
877 _rl_parsing_conditionalized_out = 1;
881 /* Invert the current parser state if there is anything on the stack. */
888 if (if_stack_depth == 0)
890 _rl_init_file_error ("$else found without matching $if");
894 /* Check the previous (n - 1) levels of the stack to make sure that
895 we haven't previously turned off parsing. */
896 for (i = 0; i < if_stack_depth - 1; i++)
897 if (if_stack[i] == 1)
900 /* Invert the state of parsing if at top level. */
901 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
905 /* Terminate a conditional, popping the value of
906 _rl_parsing_conditionalized_out from the stack. */
912 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
914 _rl_init_file_error ("$endif without matching $if");
919 parser_include (args)
922 char *old_init_file, *e;
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 (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. */
948 } parser_directives [] = {
950 { "endif", parser_endif },
951 { "else", parser_else },
952 { "include", parser_include },
953 { (char *)0x0, (Function *)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, possible_control_prefixes))
1198 key = CTRL (_rl_to_upper (key));
1200 if (substring_member_of_array (string, 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
1238 } boolean_varlist [] = {
1239 #if defined (PAREN_MATCHING)
1240 { "blink-matching-paren", &rl_blink_matching_paren },
1242 { "completion-ignore-case", &_rl_completion_case_fold },
1243 { "convert-meta", &_rl_convert_meta_chars_to_ascii },
1244 { "disable-completion", &rl_inhibit_completion },
1245 { "enable-keypad", &_rl_enable_keypad },
1246 { "expand-tilde", &rl_complete_with_tilde_expansion },
1247 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode },
1248 { "input-meta", &_rl_meta_flag },
1249 { "mark-directories", &_rl_complete_mark_directories },
1250 { "mark-modified-lines", &_rl_mark_modified_lines },
1251 { "meta-flag", &_rl_meta_flag },
1252 { "output-meta", &_rl_output_meta_chars },
1253 { "print-completions-horizontally", &_rl_print_completions_horizontally },
1254 { "show-all-if-ambiguous", &_rl_complete_show_all },
1255 #if defined (VISIBLE_STATS)
1256 { "visible-stats", &rl_visible_stats },
1257 #endif /* VISIBLE_STATS */
1258 { (char *)NULL, (int *)NULL }
1262 rl_variable_bind (name, value)
1267 /* Check for simple variables first. */
1268 for (i = 0; boolean_varlist[i].name; i++)
1270 if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1272 /* A variable is TRUE if the "value" is "on", "1" or "". */
1273 *boolean_varlist[i].value = *value == 0 ||
1274 _rl_stricmp (value, "on") == 0 ||
1275 (value[0] == '1' && value[1] == '\0');
1280 /* Not a boolean variable, so check for specials. */
1282 /* Editing mode change? */
1283 if (_rl_stricmp (name, "editing-mode") == 0)
1285 if (_rl_strnicmp (value, "vi", 2) == 0)
1287 #if defined (VI_MODE)
1288 _rl_keymap = vi_insertion_keymap;
1289 rl_editing_mode = vi_mode;
1290 #endif /* VI_MODE */
1292 else if (_rl_strnicmp (value, "emacs", 5) == 0)
1294 _rl_keymap = emacs_standard_keymap;
1295 rl_editing_mode = emacs_mode;
1299 /* Comment string change? */
1300 else if (_rl_stricmp (name, "comment-begin") == 0)
1304 if (_rl_comment_begin)
1305 free (_rl_comment_begin);
1307 _rl_comment_begin = savestring (value);
1310 else if (_rl_stricmp (name, "completion-query-items") == 0)
1315 nval = atoi (value);
1319 rl_completion_query_items = nval;
1321 else if (_rl_stricmp (name, "keymap") == 0)
1324 kmap = rl_get_keymap_by_name (value);
1326 rl_set_keymap (kmap);
1328 else if (_rl_stricmp (name, "bell-style") == 0)
1331 _rl_bell_preference = AUDIBLE_BELL;
1334 if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1335 _rl_bell_preference = NO_BELL;
1336 else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1337 _rl_bell_preference = AUDIBLE_BELL;
1338 else if (_rl_stricmp (value, "visible") == 0)
1339 _rl_bell_preference = VISIBLE_BELL;
1342 else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1344 /* Backwards compatibility. */
1345 if (*value && (_rl_stricmp (value, "on") == 0 ||
1346 (*value == '1' && !value[1])))
1347 _rl_bell_preference = VISIBLE_BELL;
1349 _rl_bell_preference = AUDIBLE_BELL;
1351 else if (_rl_stricmp (name, "isearch-terminators") == 0)
1353 /* Isolate the value and translate it into a character string. */
1357 v = savestring (value);
1358 FREE (_rl_isearch_terminators);
1359 if (v[0] == '"' || v[0] == '\'')
1362 for (beg = end = 1; v[end] && v[end] != delim; end++)
1367 for (beg = end = 0; whitespace (v[end]) == 0; end++)
1372 /* The value starts at v + beg. Translate it into a character string. */
1373 _rl_isearch_terminators = (unsigned char *)xmalloc (2 * strlen (v) + 1);
1374 rl_translate_keyseq (v + beg, _rl_isearch_terminators, &end);
1375 _rl_isearch_terminators[end] = '\0';
1379 /* For the time being, unknown variable names are simply ignored. */
1383 /* Return the character which matches NAME.
1384 For example, `Space' returns ' '. */
1391 static assoc_list name_key_alist[] = {
1394 { "Escape", '\033' },
1396 { "Newline", '\n' },
1407 glean_key_from_name (name)
1412 for (i = 0; name_key_alist[i].name; i++)
1413 if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1414 return (name_key_alist[i].value);
1416 return (*(unsigned char *)name); /* XXX was return (*name) */
1419 /* Auxiliary functions to manage keymaps. */
1423 } keymap_names[] = {
1424 { "emacs", emacs_standard_keymap },
1425 { "emacs-standard", emacs_standard_keymap },
1426 { "emacs-meta", emacs_meta_keymap },
1427 { "emacs-ctlx", emacs_ctlx_keymap },
1428 #if defined (VI_MODE)
1429 { "vi", vi_movement_keymap },
1430 { "vi-move", vi_movement_keymap },
1431 { "vi-command", vi_movement_keymap },
1432 { "vi-insert", vi_insertion_keymap },
1433 #endif /* VI_MODE */
1434 { (char *)0x0, (Keymap)0x0 }
1438 rl_get_keymap_by_name (name)
1443 for (i = 0; keymap_names[i].name; i++)
1444 if (strcmp (name, keymap_names[i].name) == 0)
1445 return (keymap_names[i].map);
1446 return ((Keymap) NULL);
1450 rl_get_keymap_name (map)
1454 for (i = 0; keymap_names[i].name; i++)
1455 if (map == keymap_names[i].map)
1456 return (keymap_names[i].name);
1457 return ((char *)NULL);
1471 return (_rl_keymap);
1475 rl_set_keymap_from_edit_mode ()
1477 if (rl_editing_mode == emacs_mode)
1478 _rl_keymap = emacs_standard_keymap;
1479 #if defined (VI_MODE)
1480 else if (rl_editing_mode == vi_mode)
1481 _rl_keymap = vi_insertion_keymap;
1482 #endif /* VI_MODE */
1486 rl_get_keymap_name_from_edit_mode ()
1488 if (rl_editing_mode == emacs_mode)
1490 #if defined (VI_MODE)
1491 else if (rl_editing_mode == vi_mode)
1493 #endif /* VI_MODE */
1498 /* **************************************************************** */
1500 /* Key Binding and Function Information */
1502 /* **************************************************************** */
1504 /* Each of the following functions produces information about the
1505 state of keybindings and functions known to Readline. The info
1506 is always printed to rl_outstream, and in such a way that it can
1507 be read back in (i.e., passed to rl_parse_and_bind (). */
1509 /* Print the names of functions known to Readline. */
1511 rl_list_funmap_names ()
1514 char **funmap_names;
1516 funmap_names = rl_funmap_names ();
1521 for (i = 0; funmap_names[i]; i++)
1522 fprintf (rl_outstream, "%s\n", funmap_names[i]);
1524 free (funmap_names);
1528 _rl_get_keyname (key)
1534 keyname = (char *)xmalloc (8);
1537 /* Since this is going to be used to write out keysequence-function
1538 pairs for possible inclusion in an inputrc file, we don't want to
1539 do any special meta processing on KEY. */
1542 /* We might want to do this, but the old version of the code did not. */
1544 /* If this is an escape character, we don't want to do any more processing.
1545 Just add the special ESC key sequence and return. */
1555 /* RUBOUT is translated directly into \C-? */
1567 /* Now add special prefixes needed for control characters. This can
1568 potentially change C. */
1571 keyname[i++] = '\\';
1574 c = _rl_to_lower (UNCTRL (c));
1577 /* XXX experimental code. Turn the characters that are not ASCII or
1578 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
1580 if (c >= 128 && c <= 159)
1582 keyname[i++] = '\\';
1585 keyname[i++] = (c / 8) + '0';
1589 /* Now, if the character needs to be quoted with a backslash, do that. */
1590 if (c == '\\' || c == '"')
1591 keyname[i++] = '\\';
1593 /* Now add the key, terminate the string, and return it. */
1594 keyname[i++] = (char) c;
1600 /* Return a NULL terminated array of strings which represent the key
1601 sequences that are used to invoke FUNCTION in MAP. */
1603 rl_invoking_keyseqs_in_map (function, map)
1609 int result_index, result_size;
1611 result = (char **)NULL;
1612 result_index = result_size = 0;
1614 for (key = 0; key < KEYMAP_SIZE; key++)
1616 switch (map[key].type)
1619 /* Macros match, if, and only if, the pointers are identical.
1620 Thus, they are treated exactly like functions in here. */
1622 /* If the function in the keymap is the one we are looking for,
1623 then add the current KEY to the list of invoking keys. */
1624 if (map[key].function == function)
1628 keyname = _rl_get_keyname (key);
1630 if (result_index + 2 > result_size)
1633 result = (char **) xrealloc (result, result_size * sizeof (char *));
1636 result[result_index++] = keyname;
1637 result[result_index] = (char *)NULL;
1646 /* Find the list of keyseqs in this map which have FUNCTION as
1647 their target. Add the key sequences found to RESULT. */
1648 if (map[key].function)
1650 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1657 for (i = 0; seqs[i]; i++)
1659 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1662 sprintf (keyname, "\\e");
1663 else if (CTRL_CHAR (key))
1664 sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1665 else if (key == RUBOUT)
1666 sprintf (keyname, "\\C-?");
1667 else if (key == '\\' || key == '"')
1670 keyname[1] = (char) key;
1675 keyname[0] = (char) key;
1679 strcat (keyname, seqs[i]);
1682 if (result_index + 2 > result_size)
1685 result = (char **) xrealloc (result, result_size * sizeof (char *));
1688 result[result_index++] = keyname;
1689 result[result_index] = (char *)NULL;
1700 /* Return a NULL terminated array of strings which represent the key
1701 sequences that can be used to invoke FUNCTION using the current keymap. */
1703 rl_invoking_keyseqs (function)
1706 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1709 /* Print all of the functions and their bindings to rl_outstream. If
1710 PRINT_READABLY is non-zero, then print the output in such a way
1711 that it can be read back in. */
1713 rl_function_dumper (print_readably)
1720 names = rl_funmap_names ();
1722 fprintf (rl_outstream, "\n");
1724 for (i = 0; name = names[i]; i++)
1729 function = rl_named_function (name);
1730 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1735 fprintf (rl_outstream, "# %s (not bound)\n", name);
1740 for (j = 0; invokers[j]; j++)
1742 fprintf (rl_outstream, "\"%s\": %s\n",
1753 fprintf (rl_outstream, "%s is not bound to any keys\n",
1759 fprintf (rl_outstream, "%s can be found on ", name);
1761 for (j = 0; invokers[j] && j < 5; j++)
1763 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1764 invokers[j + 1] ? ", " : ".\n");
1767 if (j == 5 && invokers[j])
1768 fprintf (rl_outstream, "...\n");
1770 for (j = 0; invokers[j]; j++)
1779 /* Print all of the current functions and their bindings to
1780 rl_outstream. If an explicit argument is given, then print
1781 the output in such a way that it can be read back in. */
1783 rl_dump_functions (count, key)
1787 fprintf (rl_outstream, "\r\n");
1788 rl_function_dumper (rl_explicit_arg);
1794 _rl_macro_dumper_internal (print_readably, map, prefix)
1800 char *keyname, *out;
1803 for (key = 0; key < KEYMAP_SIZE; key++)
1805 switch (map[key].type)
1808 keyname = _rl_get_keyname (key);
1810 out = (char *)map[key].function;
1812 out = _rl_untranslate_macro_value ((char *)map[key].function);
1815 fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
1819 fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
1830 prefix_len = prefix ? strlen (prefix) : 0;
1833 keyname = xmalloc (3 + prefix_len);
1835 strcpy (keyname, prefix);
1836 keyname[prefix_len] = '\\';
1837 keyname[prefix_len + 1] = 'e';
1838 keyname[prefix_len + 2] = '\0';
1842 keyname = _rl_get_keyname (key);
1845 out = xmalloc (strlen (keyname) + prefix_len + 1);
1846 strcpy (out, prefix);
1847 strcpy (out + prefix_len, keyname);
1853 _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
1861 rl_macro_dumper (print_readably)
1864 _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
1868 rl_dump_macros (count, key)
1872 fprintf (rl_outstream, "\r\n");
1873 rl_macro_dumper (rl_explicit_arg);
1879 rl_variable_dumper (print_readably)
1885 for (i = 0; boolean_varlist[i].name; i++)
1888 fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
1889 *boolean_varlist[i].value ? "on" : "off");
1891 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
1892 *boolean_varlist[i].value ? "on" : "off");
1896 switch (_rl_bell_preference)
1899 kname = "none"; break;
1901 kname = "visible"; break;
1904 kname = "audible"; break;
1907 fprintf (rl_outstream, "set bell-style %s\n", kname);
1909 fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
1913 fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
1915 fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : "");
1917 /* completion-query-items */
1919 fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items);
1921 fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items);
1925 fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1927 fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1930 kname = rl_get_keymap_name (_rl_keymap);
1932 kname = rl_get_keymap_name_from_edit_mode ();
1934 fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none");
1936 fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none");
1938 /* isearch-terminators */
1939 if (_rl_isearch_terminators)
1943 disp = _rl_untranslate_macro_value (_rl_isearch_terminators);
1946 fprintf (rl_outstream, "set isearch-terminators \"%s\"\n", disp);
1948 fprintf (rl_outstream, "isearch-terminators is set to \"%s\"\n", disp);
1954 /* Print all of the current variables and their values to
1955 rl_outstream. If an explicit argument is given, then print
1956 the output in such a way that it can be read back in. */
1958 rl_dump_variables (count, key)
1962 fprintf (rl_outstream, "\r\n");
1963 rl_variable_dumper (rl_explicit_arg);
1968 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
1970 _rl_bind_if_unbound (keyseq, default_func)
1972 Function *default_func;
1978 func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
1979 if (!func || func == rl_do_lowercase_version)
1980 rl_set_key (keyseq, default_func, _rl_keymap);
1984 /* Return non-zero if any members of ARRAY are a substring in STRING. */
1986 substring_member_of_array (string, array)
1987 char *string, **array;
1991 if (_rl_strindex (string, *array))