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__ */
69 extern int _rl_horizontal_scroll_mode;
70 extern int _rl_mark_modified_lines;
71 extern int _rl_bell_preference;
72 extern int _rl_meta_flag;
73 extern int _rl_convert_meta_chars_to_ascii;
74 extern int _rl_output_meta_chars;
75 extern int _rl_complete_show_all;
76 extern int _rl_complete_mark_directories;
77 extern int _rl_print_completions_horizontally;
78 extern int _rl_completion_case_fold;
79 extern int _rl_enable_keypad;
80 #if defined (PAREN_MATCHING)
81 extern int rl_blink_matching_paren;
82 #endif /* PAREN_MATCHING */
83 #if defined (VISIBLE_STATS)
84 extern int rl_visible_stats;
85 #endif /* VISIBLE_STATS */
86 extern int rl_complete_with_tilde_expansion;
87 extern int rl_completion_query_items;
88 extern int rl_inhibit_completion;
89 extern char *_rl_comment_begin;
90 extern unsigned char *_rl_isearch_terminators;
92 extern int rl_explicit_arg;
93 extern int rl_editing_mode;
94 extern unsigned char _rl_parsing_conditionalized_out;
95 extern Keymap _rl_keymap;
97 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
99 /* Functions imported from funmap.c */
100 extern char **rl_funmap_names ();
101 extern int rl_add_funmap_entry ();
103 /* Functions imported from util.c */
104 extern char *_rl_strindex ();
106 /* Functions imported from shell.c */
107 extern char *get_env_value ();
109 /* Variables exported by this file. */
110 Keymap rl_binding_keymap;
112 /* Forward declarations */
113 void rl_set_keymap_from_edit_mode ();
115 static int _rl_read_init_file ();
116 static int glean_key_from_name ();
117 static int substring_member_of_array ();
119 extern char *xmalloc (), *xrealloc ();
121 /* **************************************************************** */
125 /* **************************************************************** */
127 /* rl_add_defun (char *name, Function *function, int key)
128 Add NAME to the list of named functions. Make FUNCTION be the function
129 that gets called. If KEY is not -1, then bind it. */
131 rl_add_defun (name, function, key)
137 rl_bind_key (key, function);
138 rl_add_funmap_entry (name, function);
142 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
144 rl_bind_key (key, function)
151 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
153 if (_rl_keymap[ESC].type == ISKMAP)
157 escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
159 escmap[key].type = ISFUNC;
160 escmap[key].function = function;
166 _rl_keymap[key].type = ISFUNC;
167 _rl_keymap[key].function = function;
168 rl_binding_keymap = _rl_keymap;
172 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
175 rl_bind_key_in_map (key, function, map)
185 result = rl_bind_key (key, function);
190 /* Make KEY do nothing in the currently selected keymap.
191 Returns non-zero in case of error. */
196 return (rl_bind_key (key, (Function *)NULL));
199 /* Make KEY do nothing in MAP.
200 Returns non-zero in case of error. */
202 rl_unbind_key_in_map (key, map)
206 return (rl_bind_key_in_map (key, (Function *)NULL, map));
209 /* Unbind all keys bound to FUNCTION in MAP. */
211 rl_unbind_function_in_map (func, map)
215 register int i, rval;
217 for (i = rval = 0; i < KEYMAP_SIZE; i++)
219 if (map[i].type == ISFUNC && map[i].function == func)
221 map[i].function = (Function *)NULL;
229 rl_unbind_command_in_map (command, map)
235 func = rl_named_function (command);
238 return (rl_unbind_function_in_map (func, map));
241 /* Bind the key sequence represented by the string KEYSEQ to
242 FUNCTION. This makes new keymaps as necessary. The initial
243 place to do bindings is in MAP. */
245 rl_set_key (keyseq, function, map)
250 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
253 /* Bind the key sequence represented by the string KEYSEQ to
254 the string of characters MACRO. This makes new keymaps as
255 necessary. The initial place to do bindings is in MAP. */
257 rl_macro_bind (keyseq, macro, map)
258 char *keyseq, *macro;
264 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
266 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
271 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
275 /* Bind the key sequence represented by the string KEYSEQ to
276 the arbitrary pointer DATA. TYPE says what kind of data is
277 pointed to by DATA, right now this can be a function (ISFUNC),
278 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
279 as necessary. The initial place to do bindings is in MAP. */
281 rl_generic_bind (type, keyseq, data, map)
290 /* If no keys to bind to, exit right away. */
291 if (!keyseq || !*keyseq)
298 keys = xmalloc (1 + (2 * strlen (keyseq)));
300 /* Translate the ASCII representation of KEYSEQ into an array of
301 characters. Stuff the characters into KEYS, and the length of
302 KEYS into KEYS_LEN. */
303 if (rl_translate_keyseq (keyseq, keys, &keys_len))
309 /* Bind keys, making new keymaps as necessary. */
310 for (i = 0; i < keys_len; i++)
312 int ic = (int) ((unsigned char)keys[i]);
314 if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
317 if (map[ESC].type == ISKMAP)
318 map = FUNCTION_TO_KEYMAP (map, ESC);
321 if ((i + 1) < keys_len)
323 if (map[ic].type != ISKMAP)
325 if (map[ic].type == ISMACR)
326 free ((char *)map[ic].function);
328 map[ic].type = ISKMAP;
329 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
331 map = FUNCTION_TO_KEYMAP (map, ic);
335 if (map[ic].type == ISMACR)
336 free ((char *)map[ic].function);
338 map[ic].function = KEYMAP_TO_FUNCTION (data);
342 rl_binding_keymap = map;
348 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
349 an array of characters. LEN gets the final length of ARRAY. Return
350 non-zero if there was an error parsing SEQ. */
352 rl_translate_keyseq (seq, array, len)
356 register int i, c, l, temp;
358 for (i = l = 0; c = seq[i]; i++)
367 /* Handle \C- and \M- prefixes. */
368 if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
370 /* Handle special case of backwards define. */
371 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
375 array[l++] = CTRL (_rl_to_upper (seq[i]));
382 array[l++] = ESC; /* XXX */
387 /* Special hack for C-?... */
388 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
393 /* Translate other backslash-escaped characters. These are the
394 same escape sequences that bash's `echo' and `printf' builtins
395 handle, with the addition of \d -> RUBOUT. A backslash
396 preceding a character that is not special is stripped. */
406 array[l++] = RUBOUT; /* readline-specific */
415 array[l++] = NEWLINE;
429 case '0': case '1': case '2': case '3':
430 case '4': case '5': case '6': case '7':
432 for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++)
433 c = (c * 8) + OCTVALUE (seq[i]);
434 i--; /* auto-increment in for loop */
435 array[l++] = c % (largest_char + 1);
439 for (temp = 3, c = 0; isxdigit (seq[i]) && temp--; i++)
440 c = (c * 16) + HEXVALUE (seq[i]);
443 i--; /* auto-increment in for loop */
444 array[l++] = c % (largest_char + 1);
446 default: /* backslashes before non-special chars just add the char */
448 break; /* the backslash is stripped */
462 rl_untranslate_keyseq (seq)
465 static char kseq[16];
477 else if (CTRL_CHAR (c))
482 c = _rl_to_lower (UNCTRL (c));
484 else if (c == RUBOUT)
497 else if (c == '\\' || c == '"')
502 kseq[i++] = (unsigned char) c;
508 _rl_untranslate_macro_value (seq)
514 r = ret = xmalloc (7 * strlen (seq) + 1);
515 for (s = seq; *s; s++)
525 else if (CTRL_CHAR (c) && c != ESC)
530 c = _rl_to_lower (UNCTRL (c));
532 else if (c == RUBOUT)
545 else if (c == '\\' || c == '"')
548 *r++ = (unsigned char)c;
554 /* Return a pointer to the function that STRING represents.
555 If STRING doesn't have a matching function, then a NULL pointer
558 rl_named_function (string)
563 rl_initialize_funmap ();
565 for (i = 0; funmap[i]; i++)
566 if (_rl_stricmp (funmap[i]->name, string) == 0)
567 return (funmap[i]->function);
568 return ((Function *)NULL);
571 /* Return the function (or macro) definition which would be invoked via
572 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
573 used. TYPE, if non-NULL, is a pointer to an int which will receive the
574 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
575 or ISMACR (macro). */
577 rl_function_of_keyseq (keyseq, map, type)
587 for (i = 0; keyseq && keyseq[i]; i++)
591 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
593 if (map[ESC].type != ISKMAP)
596 *type = map[ESC].type;
598 return (map[ESC].function);
602 map = FUNCTION_TO_KEYMAP (map, ESC);
607 if (map[ic].type == ISKMAP)
609 /* If this is the last key in the key sequence, return the
616 return (map[ic].function);
619 map = FUNCTION_TO_KEYMAP (map, ic);
624 *type = map[ic].type;
626 return (map[ic].function);
629 return ((Function *) NULL);
632 /* The last key bindings file read. */
633 static char *last_readline_init_file = (char *)NULL;
635 /* The file we're currently reading key bindings from. */
636 static char *current_readline_init_file;
637 static int current_readline_init_include_level;
638 static int current_readline_init_lineno;
640 /* Read FILENAME into a locally-allocated buffer and return the buffer.
641 The size of the buffer is returned in *SIZEP. Returns NULL if any
642 errors were encountered. */
644 _rl_read_file (filename, sizep)
653 if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY | O_BINARY, 0666)) < 0)
654 return ((char *)NULL);
656 file_size = (size_t)finfo.st_size;
658 /* check for overflow on very large files */
659 if (file_size != finfo.st_size || file_size + 1 < file_size)
666 return ((char *)NULL);
669 /* Read the file into BUFFER. */
670 buffer = (char *)xmalloc (file_size + 1);
671 i = read (file, buffer, file_size);
681 return ((char *)NULL);
684 buffer[file_size] = '\0';
688 /* Systems which distinguish between text and binary files need
689 to strip the CR characters before each Newline, otherwise the
690 parsing functions won't work. */
694 for (s = buffer, d = buffer; s < buffer + file_size; s++)
698 if (*s != '\r' || s[1] != '\n')
704 file_size -= removed;
705 buffer[file_size] = '\0';
714 /* Re-read the current keybindings file. */
716 rl_re_read_init_file (count, ignore)
720 r = rl_read_init_file ((char *)NULL);
721 rl_set_keymap_from_edit_mode ();
725 /* Do key bindings from a file. If FILENAME is NULL it defaults
726 to the first non-null filename from this list:
727 1. the filename used for the previous call
728 2. the value of the shell variable `INPUTRC'
730 4. (for __MSDOS__ only) ~/_inputrc
731 If the file existed and could be opened and read, 0 is returned,
732 otherwise errno is returned. */
734 rl_read_init_file (filename)
737 /* Default the filename. */
740 filename = last_readline_init_file;
742 filename = get_env_value ("INPUTRC");
744 filename = DEFAULT_INPUTRC;
748 filename = DEFAULT_INPUTRC;
752 /* DOS doesn't allow leading dots in file names. If the original
753 name fails (it could work if we are on Windows), fall back to
755 int retval = _rl_read_init_file (filename, 0);
759 else if (strcmp (filename, "~/.inputrc") == 0)
760 filename = "~/_inputrc";
764 return (_rl_read_init_file (filename, 0));
768 _rl_read_init_file (filename, include_level)
773 char *buffer, *openname, *line, *end;
776 current_readline_init_file = filename;
777 current_readline_init_include_level = include_level;
779 openname = tilde_expand (filename);
780 buffer = _rl_read_file (openname, &file_size);
786 if (include_level == 0 && filename != last_readline_init_file)
788 FREE (last_readline_init_file);
789 last_readline_init_file = savestring (filename);
792 /* Loop over the lines in the file. Lines that start with `#' are
793 comments; all other lines are commands for readline initialization. */
794 current_readline_init_lineno = 1;
796 end = buffer + file_size;
799 /* Find the end of this line. */
800 for (i = 0; line + i != end && line[i] != '\n'; i++);
802 /* Mark end of line. */
805 /* Skip leading whitespace. */
806 while (*line && whitespace (*line))
812 /* If the line is not a comment, then parse it. */
813 if (*line && *line != '#')
814 rl_parse_and_bind (line);
816 /* Move to the next line. */
818 current_readline_init_lineno++;
826 _rl_init_file_error (msg)
829 fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
830 current_readline_init_lineno,
834 /* **************************************************************** */
836 /* Parser Directives */
838 /* **************************************************************** */
842 /* Calling programs set this to have their argv[0]. */
843 char *rl_readline_name = "other";
845 /* Stack of previous values of parsing_conditionalized_out. */
846 static unsigned char *if_stack = (unsigned char *)NULL;
847 static int if_stack_depth;
848 static int if_stack_size;
850 /* Push _rl_parsing_conditionalized_out, and set parser state based
858 /* Push parser state. */
859 if (if_stack_depth + 1 >= if_stack_size)
862 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
864 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
866 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
868 /* If parsing is turned off, then nothing can turn it back on except
869 for finding the matching endif. In that case, return right now. */
870 if (_rl_parsing_conditionalized_out)
873 /* Isolate first argument. */
874 for (i = 0; args[i] && !whitespace (args[i]); i++);
879 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
880 isn't term=foo, or mode=emacs, then check to see if the first
881 word in ARGS is the same as the value stored in rl_readline_name. */
882 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
886 /* Terminals like "aaa-60" are equivalent to "aaa". */
887 tname = savestring (rl_terminal_name);
888 tem = strchr (tname, '-');
892 /* Test the `long' and `short' forms of the terminal name so that
893 if someone has a `sun-cmd' and does not want to have bindings
894 that will be executed if the terminal is a `sun', they can put
895 `$if term=sun-cmd' into their .inputrc. */
896 _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
897 _rl_stricmp (args + 5, rl_terminal_name);
900 #if defined (VI_MODE)
901 else if (_rl_strnicmp (args, "mode=", 5) == 0)
905 if (_rl_stricmp (args + 5, "emacs") == 0)
907 else if (_rl_stricmp (args + 5, "vi") == 0)
912 _rl_parsing_conditionalized_out = mode != rl_editing_mode;
915 /* Check to see if the first word in ARGS is the same as the
916 value stored in rl_readline_name. */
917 else if (_rl_stricmp (args, rl_readline_name) == 0)
918 _rl_parsing_conditionalized_out = 0;
920 _rl_parsing_conditionalized_out = 1;
924 /* Invert the current parser state if there is anything on the stack. */
931 if (if_stack_depth == 0)
933 _rl_init_file_error ("$else found without matching $if");
937 /* Check the previous (n - 1) levels of the stack to make sure that
938 we haven't previously turned off parsing. */
939 for (i = 0; i < if_stack_depth - 1; i++)
940 if (if_stack[i] == 1)
943 /* Invert the state of parsing if at top level. */
944 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
948 /* Terminate a conditional, popping the value of
949 _rl_parsing_conditionalized_out from the stack. */
955 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
957 _rl_init_file_error ("$endif without matching $if");
962 parser_include (args)
965 char *old_init_file, *e;
966 int old_line_number, old_include_level, r;
968 if (_rl_parsing_conditionalized_out)
971 old_init_file = current_readline_init_file;
972 old_line_number = current_readline_init_lineno;
973 old_include_level = current_readline_init_include_level;
975 e = strchr (args, '\n');
978 r = _rl_read_init_file (args, old_include_level + 1);
980 current_readline_init_file = old_init_file;
981 current_readline_init_lineno = old_line_number;
982 current_readline_init_include_level = old_include_level;
987 /* Associate textual names with actual functions. */
991 } parser_directives [] = {
993 { "endif", parser_endif },
994 { "else", parser_else },
995 { "include", parser_include },
996 { (char *)0x0, (Function *)0x0 }
999 /* Handle a parser directive. STATEMENT is the line of the directive
1000 without any leading `$'. */
1002 handle_parser_directive (statement)
1006 char *directive, *args;
1008 /* Isolate the actual directive. */
1010 /* Skip whitespace. */
1011 for (i = 0; whitespace (statement[i]); i++);
1013 directive = &statement[i];
1015 for (; statement[i] && !whitespace (statement[i]); i++);
1018 statement[i++] = '\0';
1020 for (; statement[i] && whitespace (statement[i]); i++);
1022 args = &statement[i];
1024 /* Lookup the command, and act on it. */
1025 for (i = 0; parser_directives[i].name; i++)
1026 if (_rl_stricmp (directive, parser_directives[i].name) == 0)
1028 (*parser_directives[i].function) (args);
1032 /* display an error message about the unknown parser directive */
1033 _rl_init_file_error ("unknown parser directive");
1037 /* Read the binding command from STRING and perform it.
1038 A key binding command looks like: Keyname: function-name\0,
1039 a variable binding command looks like: set variable value.
1040 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
1042 rl_parse_and_bind (string)
1045 char *funname, *kname;
1047 int key, equivalency;
1049 while (string && whitespace (*string))
1052 if (!string || !*string || *string == '#')
1055 /* If this is a parser directive, act on it. */
1058 handle_parser_directive (&string[1]);
1062 /* If we aren't supposed to be parsing right now, then we're done. */
1063 if (_rl_parsing_conditionalized_out)
1067 /* If this keyname is a complex key expression surrounded by quotes,
1068 advance to after the matching close quote. This code allows the
1069 backslash to quote characters in the key expression. */
1074 for (i = 1; c = string[i]; i++)
1091 /* If we didn't find a closing quote, abort the line. */
1092 if (string[i] == '\0')
1094 _rl_init_file_error ("no closing `\"' in key binding");
1099 /* Advance to the colon (:) or whitespace which separates the two objects. */
1100 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
1102 equivalency = (c == ':' && string[i + 1] == '=');
1104 /* Mark the end of the command (or keyname). */
1108 /* If doing assignment, skip the '=' sign as well. */
1112 /* If this is a command to set a variable, then do that. */
1113 if (_rl_stricmp (string, "set") == 0)
1115 char *var = string + i;
1118 /* Make VAR point to start of variable name. */
1119 while (*var && whitespace (*var)) var++;
1121 /* Make value point to start of value string. */
1123 while (*value && !whitespace (*value)) value++;
1126 while (*value && whitespace (*value)) value++;
1128 rl_variable_bind (var, value);
1132 /* Skip any whitespace between keyname and funname. */
1133 for (; string[i] && whitespace (string[i]); i++);
1134 funname = &string[i];
1136 /* Now isolate funname.
1137 For straight function names just look for whitespace, since
1138 that will signify the end of the string. But this could be a
1139 macro definition. In that case, the string is quoted, so skip
1140 to the matching delimiter. We allow the backslash to quote the
1141 delimiter characters in the macro body. */
1142 /* This code exists to allow whitespace in macro expansions, which
1143 would otherwise be gobbled up by the next `for' loop.*/
1144 /* XXX - it may be desirable to allow backslash quoting only if " is
1145 the quoted string delimiter, like the shell. */
1146 if (*funname == '\'' || *funname == '"')
1148 int delimiter = string[i++], passc;
1150 for (passc = 0; c = string[i]; i++)
1171 /* Advance to the end of the string. */
1172 for (; string[i] && !whitespace (string[i]); i++);
1174 /* No extra whitespace at the end of the string. */
1177 /* Handle equivalency bindings here. Make the left-hand side be exactly
1178 whatever the right-hand evaluates to, including keymaps. */
1184 /* If this is a new-style key-binding, then do the binding with
1185 rl_set_key (). Otherwise, let the older code deal with it. */
1189 register int j, k, passc;
1191 seq = xmalloc (1 + strlen (string));
1192 for (j = 1, k = passc = 0; string[j]; j++)
1194 /* Allow backslash to quote characters, but leave them in place.
1195 This allows a string to end with a backslash quoting another
1196 backslash, or with a backslash quoting a double quote. The
1197 backslashes are left in place for rl_translate_keyseq (). */
1198 if (passc || (string[j] == '\\'))
1200 seq[k++] = string[j];
1205 if (string[j] == '"')
1208 seq[k++] = string[j];
1212 /* Binding macro? */
1213 if (*funname == '\'' || *funname == '"')
1215 j = strlen (funname);
1217 /* Remove the delimiting quotes from each end of FUNNAME. */
1218 if (j && funname[j - 1] == *funname)
1219 funname[j - 1] = '\0';
1221 rl_macro_bind (seq, &funname[1], _rl_keymap);
1224 rl_set_key (seq, rl_named_function (funname), _rl_keymap);
1230 /* Get the actual character we want to deal with. */
1231 kname = strrchr (string, '-');
1237 key = glean_key_from_name (kname);
1239 /* Add in control and meta bits. */
1240 if (substring_member_of_array (string, possible_control_prefixes))
1241 key = CTRL (_rl_to_upper (key));
1243 if (substring_member_of_array (string, possible_meta_prefixes))
1246 /* Temporary. Handle old-style keyname with macro-binding. */
1247 if (*funname == '\'' || *funname == '"')
1249 unsigned char useq[2];
1250 int fl = strlen (funname);
1252 useq[0] = key; useq[1] = '\0';
1253 if (fl && funname[fl - 1] == *funname)
1254 funname[fl - 1] = '\0';
1256 rl_macro_bind (useq, &funname[1], _rl_keymap);
1258 #if defined (PREFIX_META_HACK)
1259 /* Ugly, but working hack to keep prefix-meta around. */
1260 else if (_rl_stricmp (funname, "prefix-meta") == 0)
1266 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1268 #endif /* PREFIX_META_HACK */
1270 rl_bind_key (key, rl_named_function (funname));
1274 /* Simple structure for boolean readline variables (i.e., those that can
1275 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1281 } boolean_varlist [] = {
1282 #if defined (PAREN_MATCHING)
1283 { "blink-matching-paren", &rl_blink_matching_paren },
1285 { "completion-ignore-case", &_rl_completion_case_fold },
1286 { "convert-meta", &_rl_convert_meta_chars_to_ascii },
1287 { "disable-completion", &rl_inhibit_completion },
1288 { "enable-keypad", &_rl_enable_keypad },
1289 { "expand-tilde", &rl_complete_with_tilde_expansion },
1290 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode },
1291 { "input-meta", &_rl_meta_flag },
1292 { "mark-directories", &_rl_complete_mark_directories },
1293 { "mark-modified-lines", &_rl_mark_modified_lines },
1294 { "meta-flag", &_rl_meta_flag },
1295 { "output-meta", &_rl_output_meta_chars },
1296 { "print-completions-horizontally", &_rl_print_completions_horizontally },
1297 { "show-all-if-ambiguous", &_rl_complete_show_all },
1298 #if defined (VISIBLE_STATS)
1299 { "visible-stats", &rl_visible_stats },
1300 #endif /* VISIBLE_STATS */
1301 { (char *)NULL, (int *)NULL }
1305 rl_variable_bind (name, value)
1310 /* Check for simple variables first. */
1311 for (i = 0; boolean_varlist[i].name; i++)
1313 if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1315 /* A variable is TRUE if the "value" is "on", "1" or "". */
1316 *boolean_varlist[i].value = *value == 0 ||
1317 _rl_stricmp (value, "on") == 0 ||
1318 (value[0] == '1' && value[1] == '\0');
1323 /* Not a boolean variable, so check for specials. */
1325 /* Editing mode change? */
1326 if (_rl_stricmp (name, "editing-mode") == 0)
1328 if (_rl_strnicmp (value, "vi", 2) == 0)
1330 #if defined (VI_MODE)
1331 _rl_keymap = vi_insertion_keymap;
1332 rl_editing_mode = vi_mode;
1333 #endif /* VI_MODE */
1335 else if (_rl_strnicmp (value, "emacs", 5) == 0)
1337 _rl_keymap = emacs_standard_keymap;
1338 rl_editing_mode = emacs_mode;
1342 /* Comment string change? */
1343 else if (_rl_stricmp (name, "comment-begin") == 0)
1347 if (_rl_comment_begin)
1348 free (_rl_comment_begin);
1350 _rl_comment_begin = savestring (value);
1353 else if (_rl_stricmp (name, "completion-query-items") == 0)
1358 nval = atoi (value);
1362 rl_completion_query_items = nval;
1364 else if (_rl_stricmp (name, "keymap") == 0)
1367 kmap = rl_get_keymap_by_name (value);
1369 rl_set_keymap (kmap);
1371 else if (_rl_stricmp (name, "bell-style") == 0)
1374 _rl_bell_preference = AUDIBLE_BELL;
1377 if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1378 _rl_bell_preference = NO_BELL;
1379 else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1380 _rl_bell_preference = AUDIBLE_BELL;
1381 else if (_rl_stricmp (value, "visible") == 0)
1382 _rl_bell_preference = VISIBLE_BELL;
1385 else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1387 /* Backwards compatibility. */
1388 if (*value && (_rl_stricmp (value, "on") == 0 ||
1389 (*value == '1' && !value[1])))
1390 _rl_bell_preference = VISIBLE_BELL;
1392 _rl_bell_preference = AUDIBLE_BELL;
1394 else if (_rl_stricmp (name, "isearch-terminators") == 0)
1396 /* Isolate the value and translate it into a character string. */
1400 v = savestring (value);
1401 FREE (_rl_isearch_terminators);
1402 if (v[0] == '"' || v[0] == '\'')
1405 for (beg = end = 1; v[end] && v[end] != delim; end++)
1410 for (beg = end = 0; whitespace (v[end]) == 0; end++)
1415 /* The value starts at v + beg. Translate it into a character string. */
1416 _rl_isearch_terminators = (unsigned char *)xmalloc (2 * strlen (v) + 1);
1417 rl_translate_keyseq (v + beg, _rl_isearch_terminators, &end);
1418 _rl_isearch_terminators[end] = '\0';
1422 /* For the time being, unknown variable names are simply ignored. */
1426 /* Return the character which matches NAME.
1427 For example, `Space' returns ' '. */
1434 static assoc_list name_key_alist[] = {
1437 { "Escape", '\033' },
1439 { "Newline", '\n' },
1450 glean_key_from_name (name)
1455 for (i = 0; name_key_alist[i].name; i++)
1456 if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1457 return (name_key_alist[i].value);
1459 return (*(unsigned char *)name); /* XXX was return (*name) */
1462 /* Auxiliary functions to manage keymaps. */
1466 } keymap_names[] = {
1467 { "emacs", emacs_standard_keymap },
1468 { "emacs-standard", emacs_standard_keymap },
1469 { "emacs-meta", emacs_meta_keymap },
1470 { "emacs-ctlx", emacs_ctlx_keymap },
1471 #if defined (VI_MODE)
1472 { "vi", vi_movement_keymap },
1473 { "vi-move", vi_movement_keymap },
1474 { "vi-command", vi_movement_keymap },
1475 { "vi-insert", vi_insertion_keymap },
1476 #endif /* VI_MODE */
1477 { (char *)0x0, (Keymap)0x0 }
1481 rl_get_keymap_by_name (name)
1486 for (i = 0; keymap_names[i].name; i++)
1487 if (strcmp (name, keymap_names[i].name) == 0)
1488 return (keymap_names[i].map);
1489 return ((Keymap) NULL);
1493 rl_get_keymap_name (map)
1497 for (i = 0; keymap_names[i].name; i++)
1498 if (map == keymap_names[i].map)
1499 return (keymap_names[i].name);
1500 return ((char *)NULL);
1514 return (_rl_keymap);
1518 rl_set_keymap_from_edit_mode ()
1520 if (rl_editing_mode == emacs_mode)
1521 _rl_keymap = emacs_standard_keymap;
1522 #if defined (VI_MODE)
1523 else if (rl_editing_mode == vi_mode)
1524 _rl_keymap = vi_insertion_keymap;
1525 #endif /* VI_MODE */
1529 rl_get_keymap_name_from_edit_mode ()
1531 if (rl_editing_mode == emacs_mode)
1533 #if defined (VI_MODE)
1534 else if (rl_editing_mode == vi_mode)
1536 #endif /* VI_MODE */
1541 /* **************************************************************** */
1543 /* Key Binding and Function Information */
1545 /* **************************************************************** */
1547 /* Each of the following functions produces information about the
1548 state of keybindings and functions known to Readline. The info
1549 is always printed to rl_outstream, and in such a way that it can
1550 be read back in (i.e., passed to rl_parse_and_bind (). */
1552 /* Print the names of functions known to Readline. */
1554 rl_list_funmap_names ()
1557 char **funmap_names;
1559 funmap_names = rl_funmap_names ();
1564 for (i = 0; funmap_names[i]; i++)
1565 fprintf (rl_outstream, "%s\n", funmap_names[i]);
1567 free (funmap_names);
1571 _rl_get_keyname (key)
1577 keyname = (char *)xmalloc (8);
1580 /* Since this is going to be used to write out keysequence-function
1581 pairs for possible inclusion in an inputrc file, we don't want to
1582 do any special meta processing on KEY. */
1585 /* We might want to do this, but the old version of the code did not. */
1587 /* If this is an escape character, we don't want to do any more processing.
1588 Just add the special ESC key sequence and return. */
1598 /* RUBOUT is translated directly into \C-? */
1610 /* Now add special prefixes needed for control characters. This can
1611 potentially change C. */
1614 keyname[i++] = '\\';
1617 c = _rl_to_lower (UNCTRL (c));
1620 /* XXX experimental code. Turn the characters that are not ASCII or
1621 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
1623 if (c >= 128 && c <= 159)
1625 keyname[i++] = '\\';
1628 keyname[i++] = (c / 8) + '0';
1632 /* Now, if the character needs to be quoted with a backslash, do that. */
1633 if (c == '\\' || c == '"')
1634 keyname[i++] = '\\';
1636 /* Now add the key, terminate the string, and return it. */
1637 keyname[i++] = (char) c;
1643 /* Return a NULL terminated array of strings which represent the key
1644 sequences that are used to invoke FUNCTION in MAP. */
1646 rl_invoking_keyseqs_in_map (function, map)
1652 int result_index, result_size;
1654 result = (char **)NULL;
1655 result_index = result_size = 0;
1657 for (key = 0; key < KEYMAP_SIZE; key++)
1659 switch (map[key].type)
1662 /* Macros match, if, and only if, the pointers are identical.
1663 Thus, they are treated exactly like functions in here. */
1665 /* If the function in the keymap is the one we are looking for,
1666 then add the current KEY to the list of invoking keys. */
1667 if (map[key].function == function)
1671 keyname = _rl_get_keyname (key);
1673 if (result_index + 2 > result_size)
1676 result = (char **) xrealloc (result, result_size * sizeof (char *));
1679 result[result_index++] = keyname;
1680 result[result_index] = (char *)NULL;
1689 /* Find the list of keyseqs in this map which have FUNCTION as
1690 their target. Add the key sequences found to RESULT. */
1691 if (map[key].function)
1693 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1700 for (i = 0; seqs[i]; i++)
1702 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1705 sprintf (keyname, "\\e");
1706 else if (CTRL_CHAR (key))
1707 sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1708 else if (key == RUBOUT)
1709 sprintf (keyname, "\\C-?");
1710 else if (key == '\\' || key == '"')
1713 keyname[1] = (char) key;
1718 keyname[0] = (char) key;
1722 strcat (keyname, seqs[i]);
1725 if (result_index + 2 > result_size)
1728 result = (char **) xrealloc (result, result_size * sizeof (char *));
1731 result[result_index++] = keyname;
1732 result[result_index] = (char *)NULL;
1743 /* Return a NULL terminated array of strings which represent the key
1744 sequences that can be used to invoke FUNCTION using the current keymap. */
1746 rl_invoking_keyseqs (function)
1749 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1752 /* Print all of the functions and their bindings to rl_outstream. If
1753 PRINT_READABLY is non-zero, then print the output in such a way
1754 that it can be read back in. */
1756 rl_function_dumper (print_readably)
1763 names = rl_funmap_names ();
1765 fprintf (rl_outstream, "\n");
1767 for (i = 0; name = names[i]; i++)
1772 function = rl_named_function (name);
1773 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1778 fprintf (rl_outstream, "# %s (not bound)\n", name);
1783 for (j = 0; invokers[j]; j++)
1785 fprintf (rl_outstream, "\"%s\": %s\n",
1796 fprintf (rl_outstream, "%s is not bound to any keys\n",
1802 fprintf (rl_outstream, "%s can be found on ", name);
1804 for (j = 0; invokers[j] && j < 5; j++)
1806 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1807 invokers[j + 1] ? ", " : ".\n");
1810 if (j == 5 && invokers[j])
1811 fprintf (rl_outstream, "...\n");
1813 for (j = 0; invokers[j]; j++)
1822 /* Print all of the current functions and their bindings to
1823 rl_outstream. If an explicit argument is given, then print
1824 the output in such a way that it can be read back in. */
1826 rl_dump_functions (count, key)
1830 fprintf (rl_outstream, "\r\n");
1831 rl_function_dumper (rl_explicit_arg);
1837 _rl_macro_dumper_internal (print_readably, map, prefix)
1843 char *keyname, *out;
1846 for (key = 0; key < KEYMAP_SIZE; key++)
1848 switch (map[key].type)
1851 keyname = _rl_get_keyname (key);
1853 out = (char *)map[key].function;
1855 out = _rl_untranslate_macro_value ((char *)map[key].function);
1858 fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
1862 fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
1873 prefix_len = prefix ? strlen (prefix) : 0;
1876 keyname = xmalloc (3 + prefix_len);
1878 strcpy (keyname, prefix);
1879 keyname[prefix_len] = '\\';
1880 keyname[prefix_len + 1] = 'e';
1881 keyname[prefix_len + 2] = '\0';
1885 keyname = _rl_get_keyname (key);
1888 out = xmalloc (strlen (keyname) + prefix_len + 1);
1889 strcpy (out, prefix);
1890 strcpy (out + prefix_len, keyname);
1896 _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
1904 rl_macro_dumper (print_readably)
1907 _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
1911 rl_dump_macros (count, key)
1915 fprintf (rl_outstream, "\r\n");
1916 rl_macro_dumper (rl_explicit_arg);
1922 rl_variable_dumper (print_readably)
1928 for (i = 0; boolean_varlist[i].name; i++)
1931 fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
1932 *boolean_varlist[i].value ? "on" : "off");
1934 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
1935 *boolean_varlist[i].value ? "on" : "off");
1939 switch (_rl_bell_preference)
1942 kname = "none"; break;
1944 kname = "visible"; break;
1947 kname = "audible"; break;
1950 fprintf (rl_outstream, "set bell-style %s\n", kname);
1952 fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
1956 fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
1958 fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : "");
1960 /* completion-query-items */
1962 fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items);
1964 fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items);
1968 fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1970 fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1973 kname = rl_get_keymap_name (_rl_keymap);
1975 kname = rl_get_keymap_name_from_edit_mode ();
1977 fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none");
1979 fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none");
1981 /* isearch-terminators */
1982 if (_rl_isearch_terminators)
1986 disp = _rl_untranslate_macro_value (_rl_isearch_terminators);
1989 fprintf (rl_outstream, "set isearch-terminators \"%s\"\n", disp);
1991 fprintf (rl_outstream, "isearch-terminators is set to \"%s\"\n", disp);
1997 /* Print all of the current variables and their values to
1998 rl_outstream. If an explicit argument is given, then print
1999 the output in such a way that it can be read back in. */
2001 rl_dump_variables (count, key)
2005 fprintf (rl_outstream, "\r\n");
2006 rl_variable_dumper (rl_explicit_arg);
2011 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
2013 _rl_bind_if_unbound (keyseq, default_func)
2015 Function *default_func;
2021 func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
2022 if (!func || func == rl_do_lowercase_version)
2023 rl_set_key (keyseq, default_func, _rl_keymap);
2027 /* Return non-zero if any members of ARRAY are a substring in STRING. */
2029 substring_member_of_array (string, array)
2030 char *string, **array;
2034 if (_rl_strindex (string, *array))