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;
87 extern int rl_explicit_arg;
88 extern int rl_editing_mode;
89 extern unsigned char _rl_parsing_conditionalized_out;
90 extern Keymap _rl_keymap;
92 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
94 /* Functions imported from funmap.c */
95 extern char **rl_funmap_names ();
96 extern int rl_add_funmap_entry ();
98 /* Functions imported from util.c */
99 extern char *_rl_strindex ();
101 /* Functions imported from shell.c */
102 extern char *get_env_value ();
104 /* Variables exported by this file. */
105 Keymap rl_binding_keymap;
107 /* Forward declarations */
108 void rl_set_keymap_from_edit_mode ();
110 static int _rl_read_init_file ();
111 static int glean_key_from_name ();
112 static int substring_member_of_array ();
114 extern char *xmalloc (), *xrealloc ();
116 /* **************************************************************** */
120 /* **************************************************************** */
122 /* rl_add_defun (char *name, Function *function, int key)
123 Add NAME to the list of named functions. Make FUNCTION be the function
124 that gets called. If KEY is not -1, then bind it. */
126 rl_add_defun (name, function, key)
132 rl_bind_key (key, function);
133 rl_add_funmap_entry (name, function);
137 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
139 rl_bind_key (key, function)
146 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
148 if (_rl_keymap[ESC].type == ISKMAP)
152 escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
154 escmap[key].type = ISFUNC;
155 escmap[key].function = function;
161 _rl_keymap[key].type = ISFUNC;
162 _rl_keymap[key].function = function;
163 rl_binding_keymap = _rl_keymap;
167 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
170 rl_bind_key_in_map (key, function, map)
180 result = rl_bind_key (key, function);
185 /* Make KEY do nothing in the currently selected keymap.
186 Returns non-zero in case of error. */
191 return (rl_bind_key (key, (Function *)NULL));
194 /* Make KEY do nothing in MAP.
195 Returns non-zero in case of error. */
197 rl_unbind_key_in_map (key, map)
201 return (rl_bind_key_in_map (key, (Function *)NULL, map));
204 /* Unbind all keys bound to FUNCTION in MAP. */
206 rl_unbind_function_in_map (func, map)
212 for (i = 0; i < KEYMAP_SIZE; i++)
214 if (map[i].type == ISFUNC && map[i].function == func)
215 map[i].function = (Function *)NULL;
220 rl_unbind_command_in_map (command, map)
227 func = rl_named_function (command);
230 return (rl_unbind_function_in_map (func, map));
233 /* Bind the key sequence represented by the string KEYSEQ to
234 FUNCTION. This makes new keymaps as necessary. The initial
235 place to do bindings is in MAP. */
237 rl_set_key (keyseq, function, map)
242 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
245 /* Bind the key sequence represented by the string KEYSEQ to
246 the string of characters MACRO. This makes new keymaps as
247 necessary. The initial place to do bindings is in MAP. */
249 rl_macro_bind (keyseq, macro, map)
250 char *keyseq, *macro;
256 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
258 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
263 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
267 /* Bind the key sequence represented by the string KEYSEQ to
268 the arbitrary pointer DATA. TYPE says what kind of data is
269 pointed to by DATA, right now this can be a function (ISFUNC),
270 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
271 as necessary. The initial place to do bindings is in MAP. */
273 rl_generic_bind (type, keyseq, data, map)
282 /* If no keys to bind to, exit right away. */
283 if (!keyseq || !*keyseq)
290 keys = xmalloc (1 + (2 * strlen (keyseq)));
292 /* Translate the ASCII representation of KEYSEQ into an array of
293 characters. Stuff the characters into KEYS, and the length of
294 KEYS into KEYS_LEN. */
295 if (rl_translate_keyseq (keyseq, keys, &keys_len))
301 /* Bind keys, making new keymaps as necessary. */
302 for (i = 0; i < keys_len; i++)
304 int ic = (int) ((unsigned char)keys[i]);
306 if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
309 if (map[ESC].type == ISKMAP)
310 map = FUNCTION_TO_KEYMAP (map, ESC);
313 if ((i + 1) < keys_len)
315 if (map[ic].type != ISKMAP)
317 if (map[ic].type == ISMACR)
318 free ((char *)map[ic].function);
320 map[ic].type = ISKMAP;
321 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
323 map = FUNCTION_TO_KEYMAP (map, ic);
327 if (map[ic].type == ISMACR)
328 free ((char *)map[ic].function);
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)
348 register int i, c, l, temp;
350 for (i = l = 0; c = seq[i]; i++)
359 /* Handle \C- and \M- prefixes. */
360 if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
362 /* Handle special case of backwards define. */
363 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
367 array[l++] = CTRL (_rl_to_upper (seq[i]));
374 array[l++] = ESC; /* XXX */
379 /* Special hack for C-?... */
380 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
385 /* Translate other backslash-escaped characters. These are the
386 same escape sequences that bash's `echo' and `printf' builtins
387 handle, with the addition of \d -> RUBOUT. A backslash
388 preceding a character that is not special is stripped. */
398 array[l++] = RUBOUT; /* readline-specific */
407 array[l++] = NEWLINE;
421 case '0': case '1': case '2': case '3':
422 case '4': case '5': case '6': case '7':
424 for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++)
425 c = (c * 8) + OCTVALUE (seq[i]);
426 i--; /* auto-increment in for loop */
427 array[l++] = c % (largest_char + 1);
431 for (temp = 3, c = 0; isxdigit (seq[i]) && temp--; i++)
432 c = (c * 16) + HEXVALUE (seq[i]);
435 i--; /* auto-increment in for loop */
436 array[l++] = c % (largest_char + 1);
438 default: /* backslashes before non-special chars just add the char */
440 break; /* the backslash is stripped */
454 rl_untranslate_keyseq (seq)
457 static char kseq[16];
469 else if (CTRL_CHAR (c))
474 c = _rl_to_lower (UNCTRL (c));
476 else if (c == RUBOUT)
489 else if (c == '\\' || c == '"')
494 kseq[i++] = (unsigned char) c;
500 _rl_untranslate_macro_value (seq)
506 r = ret = xmalloc (7 * strlen (seq) + 1);
507 for (s = seq; *s; s++)
517 else if (CTRL_CHAR (c) && c != ESC)
522 c = _rl_to_lower (UNCTRL (c));
524 else if (c == RUBOUT)
537 else if (c == '\\' || c == '"')
540 *r++ = (unsigned char)c;
546 /* Return a pointer to the function that STRING represents.
547 If STRING doesn't have a matching function, then a NULL pointer
550 rl_named_function (string)
555 rl_initialize_funmap ();
557 for (i = 0; funmap[i]; i++)
558 if (_rl_stricmp (funmap[i]->name, string) == 0)
559 return (funmap[i]->function);
560 return ((Function *)NULL);
563 /* Return the function (or macro) definition which would be invoked via
564 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
565 used. TYPE, if non-NULL, is a pointer to an int which will receive the
566 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
567 or ISMACR (macro). */
569 rl_function_of_keyseq (keyseq, map, type)
579 for (i = 0; keyseq && keyseq[i]; i++)
583 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
585 if (map[ESC].type != ISKMAP)
588 *type = map[ESC].type;
590 return (map[ESC].function);
594 map = FUNCTION_TO_KEYMAP (map, ESC);
599 if (map[ic].type == ISKMAP)
601 /* If this is the last key in the key sequence, return the
608 return (map[ic].function);
611 map = FUNCTION_TO_KEYMAP (map, ic);
616 *type = map[ic].type;
618 return (map[ic].function);
621 return ((Function *) NULL);
624 /* The last key bindings file read. */
625 static char *last_readline_init_file = (char *)NULL;
627 /* The file we're currently reading key bindings from. */
628 static char *current_readline_init_file;
629 static int current_readline_init_include_level;
630 static int current_readline_init_lineno;
632 /* Read FILENAME into a locally-allocated buffer and return the buffer.
633 The size of the buffer is returned in *SIZEP. Returns NULL if any
634 errors were encountered. */
636 _rl_read_file (filename, sizep)
645 if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0)
646 return ((char *)NULL);
648 file_size = (size_t)finfo.st_size;
650 /* check for overflow on very large files */
651 if (file_size != finfo.st_size || file_size + 1 < file_size)
658 return ((char *)NULL);
661 /* Read the file into BUFFER. */
662 buffer = (char *)xmalloc (file_size + 1);
663 i = read (file, buffer, file_size);
669 return ((char *)NULL);
672 buffer[file_size] = '\0';
678 /* Re-read the current keybindings file. */
680 rl_re_read_init_file (count, ignore)
684 r = rl_read_init_file ((char *)NULL);
685 rl_set_keymap_from_edit_mode ();
689 /* Do key bindings from a file. If FILENAME is NULL it defaults
690 to the first non-null filename from this list:
691 1. the filename used for the previous call
692 2. the value of the shell variable `INPUTRC'
694 If the file existed and could be opened and read, 0 is returned,
695 otherwise errno is returned. */
697 rl_read_init_file (filename)
700 /* Default the filename. */
703 filename = last_readline_init_file;
705 filename = get_env_value ("INPUTRC");
707 filename = DEFAULT_INPUTRC;
711 filename = DEFAULT_INPUTRC;
713 return (_rl_read_init_file (filename, 0));
717 _rl_read_init_file (filename, include_level)
722 char *buffer, *openname, *line, *end;
725 current_readline_init_file = filename;
726 current_readline_init_include_level = include_level;
728 openname = tilde_expand (filename);
729 buffer = _rl_read_file (openname, &file_size);
733 if (include_level == 0 && filename != last_readline_init_file)
735 FREE (last_readline_init_file);
736 last_readline_init_file = savestring (filename);
739 /* Loop over the lines in the file. Lines that start with `#' are
740 comments; all other lines are commands for readline initialization. */
741 current_readline_init_lineno = 1;
743 end = buffer + file_size;
746 /* Find the end of this line. */
747 for (i = 0; line + i != end && line[i] != '\n'; i++);
749 /* Mark end of line. */
752 /* Skip leading whitespace. */
753 while (*line && whitespace (*line))
759 /* If the line is not a comment, then parse it. */
760 if (*line && *line != '#')
761 rl_parse_and_bind (line);
763 /* Move to the next line. */
765 current_readline_init_lineno++;
773 _rl_init_file_error (msg)
776 fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
777 current_readline_init_lineno,
781 /* **************************************************************** */
783 /* Parser Directives */
785 /* **************************************************************** */
789 /* Calling programs set this to have their argv[0]. */
790 char *rl_readline_name = "other";
792 /* Stack of previous values of parsing_conditionalized_out. */
793 static unsigned char *if_stack = (unsigned char *)NULL;
794 static int if_stack_depth;
795 static int if_stack_size;
797 /* Push _rl_parsing_conditionalized_out, and set parser state based
805 /* Push parser state. */
806 if (if_stack_depth + 1 >= if_stack_size)
809 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
811 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
813 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
815 /* If parsing is turned off, then nothing can turn it back on except
816 for finding the matching endif. In that case, return right now. */
817 if (_rl_parsing_conditionalized_out)
820 /* Isolate first argument. */
821 for (i = 0; args[i] && !whitespace (args[i]); i++);
826 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
827 isn't term=foo, or mode=emacs, then check to see if the first
828 word in ARGS is the same as the value stored in rl_readline_name. */
829 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
833 /* Terminals like "aaa-60" are equivalent to "aaa". */
834 tname = savestring (rl_terminal_name);
835 tem = strchr (tname, '-');
839 /* Test the `long' and `short' forms of the terminal name so that
840 if someone has a `sun-cmd' and does not want to have bindings
841 that will be executed if the terminal is a `sun', they can put
842 `$if term=sun-cmd' into their .inputrc. */
843 _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
844 _rl_stricmp (args + 5, rl_terminal_name);
847 #if defined (VI_MODE)
848 else if (_rl_strnicmp (args, "mode=", 5) == 0)
852 if (_rl_stricmp (args + 5, "emacs") == 0)
854 else if (_rl_stricmp (args + 5, "vi") == 0)
859 _rl_parsing_conditionalized_out = mode != rl_editing_mode;
862 /* Check to see if the first word in ARGS is the same as the
863 value stored in rl_readline_name. */
864 else if (_rl_stricmp (args, rl_readline_name) == 0)
865 _rl_parsing_conditionalized_out = 0;
867 _rl_parsing_conditionalized_out = 1;
871 /* Invert the current parser state if there is anything on the stack. */
878 if (if_stack_depth == 0)
880 _rl_init_file_error ("$else found without matching $if");
884 /* Check the previous (n - 1) levels of the stack to make sure that
885 we haven't previously turned off parsing. */
886 for (i = 0; i < if_stack_depth - 1; i++)
887 if (if_stack[i] == 1)
890 /* Invert the state of parsing if at top level. */
891 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
895 /* Terminate a conditional, popping the value of
896 _rl_parsing_conditionalized_out from the stack. */
902 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
904 _rl_init_file_error ("$endif without matching $if");
909 parser_include (args)
912 char *old_init_file, *e;
913 int old_line_number, old_include_level, r;
915 if (_rl_parsing_conditionalized_out)
918 old_init_file = current_readline_init_file;
919 old_line_number = current_readline_init_lineno;
920 old_include_level = current_readline_init_include_level;
922 e = strchr (args, '\n');
925 r = _rl_read_init_file (args, old_include_level + 1);
927 current_readline_init_file = old_init_file;
928 current_readline_init_lineno = old_line_number;
929 current_readline_init_include_level = old_include_level;
934 /* Associate textual names with actual functions. */
938 } parser_directives [] = {
940 { "endif", parser_endif },
941 { "else", parser_else },
942 { "include", parser_include },
943 { (char *)0x0, (Function *)0x0 }
946 /* Handle a parser directive. STATEMENT is the line of the directive
947 without any leading `$'. */
949 handle_parser_directive (statement)
953 char *directive, *args;
955 /* Isolate the actual directive. */
957 /* Skip whitespace. */
958 for (i = 0; whitespace (statement[i]); i++);
960 directive = &statement[i];
962 for (; statement[i] && !whitespace (statement[i]); i++);
965 statement[i++] = '\0';
967 for (; statement[i] && whitespace (statement[i]); i++);
969 args = &statement[i];
971 /* Lookup the command, and act on it. */
972 for (i = 0; parser_directives[i].name; i++)
973 if (_rl_stricmp (directive, parser_directives[i].name) == 0)
975 (*parser_directives[i].function) (args);
979 /* display an error message about the unknown parser directive */
980 _rl_init_file_error ("unknown parser directive");
984 /* Read the binding command from STRING and perform it.
985 A key binding command looks like: Keyname: function-name\0,
986 a variable binding command looks like: set variable value.
987 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
989 rl_parse_and_bind (string)
992 char *funname, *kname;
994 int key, equivalency;
996 while (string && whitespace (*string))
999 if (!string || !*string || *string == '#')
1002 /* If this is a parser directive, act on it. */
1005 handle_parser_directive (&string[1]);
1009 /* If we aren't supposed to be parsing right now, then we're done. */
1010 if (_rl_parsing_conditionalized_out)
1014 /* If this keyname is a complex key expression surrounded by quotes,
1015 advance to after the matching close quote. This code allows the
1016 backslash to quote characters in the key expression. */
1021 for (i = 1; c = string[i]; i++)
1038 /* If we didn't find a closing quote, abort the line. */
1039 if (string[i] == '\0')
1041 _rl_init_file_error ("no closing `\"' in key binding");
1046 /* Advance to the colon (:) or whitespace which separates the two objects. */
1047 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
1049 equivalency = (c == ':' && string[i + 1] == '=');
1051 /* Mark the end of the command (or keyname). */
1055 /* If doing assignment, skip the '=' sign as well. */
1059 /* If this is a command to set a variable, then do that. */
1060 if (_rl_stricmp (string, "set") == 0)
1062 char *var = string + i;
1065 /* Make VAR point to start of variable name. */
1066 while (*var && whitespace (*var)) var++;
1068 /* Make value point to start of value string. */
1070 while (*value && !whitespace (*value)) value++;
1073 while (*value && whitespace (*value)) value++;
1075 rl_variable_bind (var, value);
1079 /* Skip any whitespace between keyname and funname. */
1080 for (; string[i] && whitespace (string[i]); i++);
1081 funname = &string[i];
1083 /* Now isolate funname.
1084 For straight function names just look for whitespace, since
1085 that will signify the end of the string. But this could be a
1086 macro definition. In that case, the string is quoted, so skip
1087 to the matching delimiter. We allow the backslash to quote the
1088 delimiter characters in the macro body. */
1089 /* This code exists to allow whitespace in macro expansions, which
1090 would otherwise be gobbled up by the next `for' loop.*/
1091 /* XXX - it may be desirable to allow backslash quoting only if " is
1092 the quoted string delimiter, like the shell. */
1093 if (*funname == '\'' || *funname == '"')
1095 int delimiter = string[i++], passc;
1097 for (passc = 0; c = string[i]; i++)
1118 /* Advance to the end of the string. */
1119 for (; string[i] && !whitespace (string[i]); i++);
1121 /* No extra whitespace at the end of the string. */
1124 /* Handle equivalency bindings here. Make the left-hand side be exactly
1125 whatever the right-hand evaluates to, including keymaps. */
1131 /* If this is a new-style key-binding, then do the binding with
1132 rl_set_key (). Otherwise, let the older code deal with it. */
1136 register int j, k, passc;
1138 seq = xmalloc (1 + strlen (string));
1139 for (j = 1, k = passc = 0; string[j]; j++)
1141 /* Allow backslash to quote characters, but leave them in place.
1142 This allows a string to end with a backslash quoting another
1143 backslash, or with a backslash quoting a double quote. The
1144 backslashes are left in place for rl_translate_keyseq (). */
1145 if (passc || (string[j] == '\\'))
1147 seq[k++] = string[j];
1152 if (string[j] == '"')
1155 seq[k++] = string[j];
1159 /* Binding macro? */
1160 if (*funname == '\'' || *funname == '"')
1162 j = strlen (funname);
1164 /* Remove the delimiting quotes from each end of FUNNAME. */
1165 if (j && funname[j - 1] == *funname)
1166 funname[j - 1] = '\0';
1168 rl_macro_bind (seq, &funname[1], _rl_keymap);
1171 rl_set_key (seq, rl_named_function (funname), _rl_keymap);
1177 /* Get the actual character we want to deal with. */
1178 kname = strrchr (string, '-');
1184 key = glean_key_from_name (kname);
1186 /* Add in control and meta bits. */
1187 if (substring_member_of_array (string, possible_control_prefixes))
1188 key = CTRL (_rl_to_upper (key));
1190 if (substring_member_of_array (string, possible_meta_prefixes))
1193 /* Temporary. Handle old-style keyname with macro-binding. */
1194 if (*funname == '\'' || *funname == '"')
1196 unsigned char useq[2];
1197 int fl = strlen (funname);
1199 useq[0] = key; useq[1] = '\0';
1200 if (fl && funname[fl - 1] == *funname)
1201 funname[fl - 1] = '\0';
1203 rl_macro_bind (useq, &funname[1], _rl_keymap);
1205 #if defined (PREFIX_META_HACK)
1206 /* Ugly, but working hack to keep prefix-meta around. */
1207 else if (_rl_stricmp (funname, "prefix-meta") == 0)
1213 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1215 #endif /* PREFIX_META_HACK */
1217 rl_bind_key (key, rl_named_function (funname));
1221 /* Simple structure for boolean readline variables (i.e., those that can
1222 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1228 } boolean_varlist [] = {
1229 #if defined (PAREN_MATCHING)
1230 { "blink-matching-paren", &rl_blink_matching_paren },
1232 { "completion-ignore-case", &_rl_completion_case_fold },
1233 { "convert-meta", &_rl_convert_meta_chars_to_ascii },
1234 { "disable-completion", &rl_inhibit_completion },
1235 { "enable-keypad", &_rl_enable_keypad },
1236 { "expand-tilde", &rl_complete_with_tilde_expansion },
1237 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode },
1238 { "input-meta", &_rl_meta_flag },
1239 { "mark-directories", &_rl_complete_mark_directories },
1240 { "mark-modified-lines", &_rl_mark_modified_lines },
1241 { "meta-flag", &_rl_meta_flag },
1242 { "output-meta", &_rl_output_meta_chars },
1243 { "print-completions-horizontally", &_rl_print_completions_horizontally },
1244 { "show-all-if-ambiguous", &_rl_complete_show_all },
1245 #if defined (VISIBLE_STATS)
1246 { "visible-stats", &rl_visible_stats },
1247 #endif /* VISIBLE_STATS */
1248 { (char *)NULL, (int *)NULL }
1252 rl_variable_bind (name, value)
1257 /* Check for simple variables first. */
1258 for (i = 0; boolean_varlist[i].name; i++)
1260 if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1262 /* A variable is TRUE if the "value" is "on", "1" or "". */
1263 *boolean_varlist[i].value = *value == 0 ||
1264 _rl_stricmp (value, "on") == 0 ||
1265 (value[0] == '1' && value[1] == '\0');
1270 /* Not a boolean variable, so check for specials. */
1272 /* Editing mode change? */
1273 if (_rl_stricmp (name, "editing-mode") == 0)
1275 if (_rl_strnicmp (value, "vi", 2) == 0)
1277 #if defined (VI_MODE)
1278 _rl_keymap = vi_insertion_keymap;
1279 rl_editing_mode = vi_mode;
1280 #endif /* VI_MODE */
1282 else if (_rl_strnicmp (value, "emacs", 5) == 0)
1284 _rl_keymap = emacs_standard_keymap;
1285 rl_editing_mode = emacs_mode;
1289 /* Comment string change? */
1290 else if (_rl_stricmp (name, "comment-begin") == 0)
1294 if (_rl_comment_begin)
1295 free (_rl_comment_begin);
1297 _rl_comment_begin = savestring (value);
1300 else if (_rl_stricmp (name, "completion-query-items") == 0)
1305 nval = atoi (value);
1309 rl_completion_query_items = nval;
1311 else if (_rl_stricmp (name, "keymap") == 0)
1314 kmap = rl_get_keymap_by_name (value);
1316 rl_set_keymap (kmap);
1318 else if (_rl_stricmp (name, "bell-style") == 0)
1321 _rl_bell_preference = AUDIBLE_BELL;
1324 if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1325 _rl_bell_preference = NO_BELL;
1326 else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1327 _rl_bell_preference = AUDIBLE_BELL;
1328 else if (_rl_stricmp (value, "visible") == 0)
1329 _rl_bell_preference = VISIBLE_BELL;
1332 else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1334 /* Backwards compatibility. */
1335 if (*value && (_rl_stricmp (value, "on") == 0 ||
1336 (*value == '1' && !value[1])))
1337 _rl_bell_preference = VISIBLE_BELL;
1339 _rl_bell_preference = AUDIBLE_BELL;
1342 /* For the time being, unknown variable names are simply ignored. */
1346 /* Return the character which matches NAME.
1347 For example, `Space' returns ' '. */
1354 static assoc_list name_key_alist[] = {
1357 { "Escape", '\033' },
1359 { "Newline", '\n' },
1370 glean_key_from_name (name)
1375 for (i = 0; name_key_alist[i].name; i++)
1376 if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1377 return (name_key_alist[i].value);
1379 return (*(unsigned char *)name); /* XXX was return (*name) */
1382 /* Auxiliary functions to manage keymaps. */
1386 } keymap_names[] = {
1387 { "emacs", emacs_standard_keymap },
1388 { "emacs-standard", emacs_standard_keymap },
1389 { "emacs-meta", emacs_meta_keymap },
1390 { "emacs-ctlx", emacs_ctlx_keymap },
1391 #if defined (VI_MODE)
1392 { "vi", vi_movement_keymap },
1393 { "vi-move", vi_movement_keymap },
1394 { "vi-command", vi_movement_keymap },
1395 { "vi-insert", vi_insertion_keymap },
1396 #endif /* VI_MODE */
1397 { (char *)0x0, (Keymap)0x0 }
1401 rl_get_keymap_by_name (name)
1406 for (i = 0; keymap_names[i].name; i++)
1407 if (strcmp (name, keymap_names[i].name) == 0)
1408 return (keymap_names[i].map);
1409 return ((Keymap) NULL);
1413 rl_get_keymap_name (map)
1417 for (i = 0; keymap_names[i].name; i++)
1418 if (map == keymap_names[i].map)
1419 return (keymap_names[i].name);
1420 return ((char *)NULL);
1434 return (_rl_keymap);
1438 rl_set_keymap_from_edit_mode ()
1440 if (rl_editing_mode == emacs_mode)
1441 _rl_keymap = emacs_standard_keymap;
1442 #if defined (VI_MODE)
1443 else if (rl_editing_mode == vi_mode)
1444 _rl_keymap = vi_insertion_keymap;
1445 #endif /* VI_MODE */
1449 rl_get_keymap_name_from_edit_mode ()
1451 if (rl_editing_mode == emacs_mode)
1453 #if defined (VI_MODE)
1454 else if (rl_editing_mode == vi_mode)
1456 #endif /* VI_MODE */
1461 /* **************************************************************** */
1463 /* Key Binding and Function Information */
1465 /* **************************************************************** */
1467 /* Each of the following functions produces information about the
1468 state of keybindings and functions known to Readline. The info
1469 is always printed to rl_outstream, and in such a way that it can
1470 be read back in (i.e., passed to rl_parse_and_bind (). */
1472 /* Print the names of functions known to Readline. */
1474 rl_list_funmap_names ()
1477 char **funmap_names;
1479 funmap_names = rl_funmap_names ();
1484 for (i = 0; funmap_names[i]; i++)
1485 fprintf (rl_outstream, "%s\n", funmap_names[i]);
1487 free (funmap_names);
1491 _rl_get_keyname (key)
1497 keyname = (char *)xmalloc (8);
1500 /* Since this is going to be used to write out keysequence-function
1501 pairs for possible inclusion in an inputrc file, we don't want to
1502 do any special meta processing on KEY. */
1505 /* We might want to do this, but the old version of the code did not. */
1507 /* If this is an escape character, we don't want to do any more processing.
1508 Just add the special ESC key sequence and return. */
1518 /* RUBOUT is translated directly into \C-? */
1530 /* Now add special prefixes needed for control characters. This can
1531 potentially change C. */
1534 keyname[i++] = '\\';
1537 c = _rl_to_lower (UNCTRL (c));
1540 /* XXX experimental code. Turn the characters that are not ASCII or
1541 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
1543 if (c >= 128 && c <= 159)
1545 keyname[i++] = '\\';
1548 keyname[i++] = (c / 8) + '0';
1552 /* Now, if the character needs to be quoted with a backslash, do that. */
1553 if (c == '\\' || c == '"')
1554 keyname[i++] = '\\';
1556 /* Now add the key, terminate the string, and return it. */
1557 keyname[i++] = (char) c;
1563 /* Return a NULL terminated array of strings which represent the key
1564 sequences that are used to invoke FUNCTION in MAP. */
1566 rl_invoking_keyseqs_in_map (function, map)
1572 int result_index, result_size;
1574 result = (char **)NULL;
1575 result_index = result_size = 0;
1577 for (key = 0; key < KEYMAP_SIZE; key++)
1579 switch (map[key].type)
1582 /* Macros match, if, and only if, the pointers are identical.
1583 Thus, they are treated exactly like functions in here. */
1585 /* If the function in the keymap is the one we are looking for,
1586 then add the current KEY to the list of invoking keys. */
1587 if (map[key].function == function)
1591 keyname = _rl_get_keyname (key);
1593 if (result_index + 2 > result_size)
1596 result = (char **) xrealloc (result, result_size * sizeof (char *));
1599 result[result_index++] = keyname;
1600 result[result_index] = (char *)NULL;
1609 /* Find the list of keyseqs in this map which have FUNCTION as
1610 their target. Add the key sequences found to RESULT. */
1611 if (map[key].function)
1613 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1620 for (i = 0; seqs[i]; i++)
1622 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1625 sprintf (keyname, "\\e");
1626 else if (CTRL_CHAR (key))
1627 sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1628 else if (key == RUBOUT)
1629 sprintf (keyname, "\\C-?");
1630 else if (key == '\\' || key == '"')
1633 keyname[1] = (char) key;
1638 keyname[0] = (char) key;
1642 strcat (keyname, seqs[i]);
1645 if (result_index + 2 > result_size)
1648 result = (char **) xrealloc (result, result_size * sizeof (char *));
1651 result[result_index++] = keyname;
1652 result[result_index] = (char *)NULL;
1663 /* Return a NULL terminated array of strings which represent the key
1664 sequences that can be used to invoke FUNCTION using the current keymap. */
1666 rl_invoking_keyseqs (function)
1669 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1672 /* Print all of the functions and their bindings to rl_outstream. If
1673 PRINT_READABLY is non-zero, then print the output in such a way
1674 that it can be read back in. */
1676 rl_function_dumper (print_readably)
1683 names = rl_funmap_names ();
1685 fprintf (rl_outstream, "\n");
1687 for (i = 0; name = names[i]; i++)
1692 function = rl_named_function (name);
1693 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1698 fprintf (rl_outstream, "# %s (not bound)\n", name);
1703 for (j = 0; invokers[j]; j++)
1705 fprintf (rl_outstream, "\"%s\": %s\n",
1716 fprintf (rl_outstream, "%s is not bound to any keys\n",
1722 fprintf (rl_outstream, "%s can be found on ", name);
1724 for (j = 0; invokers[j] && j < 5; j++)
1726 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1727 invokers[j + 1] ? ", " : ".\n");
1730 if (j == 5 && invokers[j])
1731 fprintf (rl_outstream, "...\n");
1733 for (j = 0; invokers[j]; j++)
1742 /* Print all of the current functions and their bindings to
1743 rl_outstream. If an explicit argument is given, then print
1744 the output in such a way that it can be read back in. */
1746 rl_dump_functions (count, key)
1750 fprintf (rl_outstream, "\r\n");
1751 rl_function_dumper (rl_explicit_arg);
1757 _rl_macro_dumper_internal (print_readably, map, prefix)
1763 char *keyname, *out;
1766 for (key = 0; key < KEYMAP_SIZE; key++)
1768 switch (map[key].type)
1771 keyname = _rl_get_keyname (key);
1773 out = (char *)map[key].function;
1775 out = _rl_untranslate_macro_value ((char *)map[key].function);
1778 fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
1782 fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
1793 prefix_len = prefix ? strlen (prefix) : 0;
1796 keyname = xmalloc (3 + prefix_len);
1798 strcpy (keyname, prefix);
1799 keyname[prefix_len] = '\\';
1800 keyname[prefix_len + 1] = 'e';
1801 keyname[prefix_len + 2] = '\0';
1805 keyname = _rl_get_keyname (key);
1808 out = xmalloc (strlen (keyname) + prefix_len + 1);
1809 strcpy (out, prefix);
1810 strcpy (out + prefix_len, keyname);
1816 _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
1824 rl_macro_dumper (print_readably)
1827 _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
1831 rl_dump_macros (count, key)
1835 fprintf (rl_outstream, "\r\n");
1836 rl_macro_dumper (rl_explicit_arg);
1842 rl_variable_dumper (print_readably)
1848 for (i = 0; boolean_varlist[i].name; i++)
1851 fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
1852 *boolean_varlist[i].value ? "on" : "off");
1854 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
1855 *boolean_varlist[i].value ? "on" : "off");
1859 switch (_rl_bell_preference)
1862 kname = "none"; break;
1864 kname = "visible"; break;
1867 kname = "audible"; break;
1870 fprintf (rl_outstream, "set bell-style %s\n", kname);
1872 fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
1876 fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
1878 fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : "");
1880 /* completion-query-items */
1882 fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items);
1884 fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items);
1888 fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1890 fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1893 kname = rl_get_keymap_name (_rl_keymap);
1895 kname = rl_get_keymap_name_from_edit_mode ();
1897 fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none");
1899 fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none");
1902 /* Print all of the current variables and their values to
1903 rl_outstream. If an explicit argument is given, then print
1904 the output in such a way that it can be read back in. */
1906 rl_dump_variables (count, key)
1910 fprintf (rl_outstream, "\r\n");
1911 rl_variable_dumper (rl_explicit_arg);
1916 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
1918 _rl_bind_if_unbound (keyseq, default_func)
1920 Function *default_func;
1926 func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
1927 if (!func || func == rl_do_lowercase_version)
1928 rl_set_key (keyseq, default_func, _rl_keymap);
1932 /* Return non-zero if any members of ARRAY are a substring in STRING. */
1934 substring_member_of_array (string, array)
1935 char *string, **array;
1939 if (_rl_strindex (string, *array))