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_enable_keypad;
74 #if defined (PAREN_MATCHING)
75 extern int rl_blink_matching_paren;
76 #endif /* PAREN_MATCHING */
77 #if defined (VISIBLE_STATS)
78 extern int rl_visible_stats;
79 #endif /* VISIBLE_STATS */
80 extern int rl_complete_with_tilde_expansion;
81 extern int rl_completion_query_items;
82 extern int rl_inhibit_completion;
83 extern char *_rl_comment_begin;
85 extern int rl_explicit_arg;
86 extern int rl_editing_mode;
87 extern unsigned char _rl_parsing_conditionalized_out;
88 extern Keymap _rl_keymap;
90 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
92 extern char **rl_funmap_names ();
93 extern int rl_add_funmap_entry ();
95 extern char *_rl_strindex ();
97 /* Variables exported by this file. */
98 Keymap rl_binding_keymap;
100 /* Forward declarations */
101 void rl_set_keymap_from_edit_mode ();
103 static int glean_key_from_name ();
104 static int substring_member_of_array ();
106 extern char *xmalloc (), *xrealloc ();
108 /* **************************************************************** */
112 /* **************************************************************** */
114 /* rl_add_defun (char *name, Function *function, int key)
115 Add NAME to the list of named functions. Make FUNCTION be the function
116 that gets called. If KEY is not -1, then bind it. */
118 rl_add_defun (name, function, key)
124 rl_bind_key (key, function);
125 rl_add_funmap_entry (name, function);
129 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
131 rl_bind_key (key, function)
138 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
140 if (_rl_keymap[ESC].type == ISKMAP)
144 escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
146 escmap[key].type = ISFUNC;
147 escmap[key].function = function;
153 _rl_keymap[key].type = ISFUNC;
154 _rl_keymap[key].function = function;
155 rl_binding_keymap = _rl_keymap;
159 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
162 rl_bind_key_in_map (key, function, map)
172 result = rl_bind_key (key, function);
177 /* Make KEY do nothing in the currently selected keymap.
178 Returns non-zero in case of error. */
183 return (rl_bind_key (key, (Function *)NULL));
186 /* Make KEY do nothing in MAP.
187 Returns non-zero in case of error. */
189 rl_unbind_key_in_map (key, map)
193 return (rl_bind_key_in_map (key, (Function *)NULL, map));
196 /* Bind the key sequence represented by the string KEYSEQ to
197 FUNCTION. This makes new keymaps as necessary. The initial
198 place to do bindings is in MAP. */
200 rl_set_key (keyseq, function, map)
205 return (rl_generic_bind (ISFUNC, keyseq, function, map));
208 /* Bind the key sequence represented by the string KEYSEQ to
209 the string of characters MACRO. This makes new keymaps as
210 necessary. The initial place to do bindings is in MAP. */
212 rl_macro_bind (keyseq, macro, map)
213 char *keyseq, *macro;
219 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
221 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
226 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
230 /* Bind the key sequence represented by the string KEYSEQ to
231 the arbitrary pointer DATA. TYPE says what kind of data is
232 pointed to by DATA, right now this can be a function (ISFUNC),
233 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
234 as necessary. The initial place to do bindings is in MAP. */
236 rl_generic_bind (type, keyseq, data, map)
245 /* If no keys to bind to, exit right away. */
246 if (!keyseq || !*keyseq)
253 keys = xmalloc (1 + (2 * strlen (keyseq)));
255 /* Translate the ASCII representation of KEYSEQ into an array of
256 characters. Stuff the characters into KEYS, and the length of
257 KEYS into KEYS_LEN. */
258 if (rl_translate_keyseq (keyseq, keys, &keys_len))
264 /* Bind keys, making new keymaps as necessary. */
265 for (i = 0; i < keys_len; i++)
267 int ic = (int) ((unsigned char)keys[i]);
269 if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
272 if (map[ESC].type == ISKMAP)
273 map = FUNCTION_TO_KEYMAP (map, ESC);
276 if ((i + 1) < keys_len)
278 if (map[ic].type != ISKMAP)
280 if (map[ic].type == ISMACR)
281 free ((char *)map[ic].function);
283 map[ic].type = ISKMAP;
284 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
286 map = FUNCTION_TO_KEYMAP (map, ic);
290 if (map[ic].type == ISMACR)
291 free ((char *)map[ic].function);
293 map[ic].function = KEYMAP_TO_FUNCTION (data);
297 rl_binding_keymap = map;
303 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
304 an array of characters. LEN gets the final length of ARRAY. Return
305 non-zero if there was an error parsing SEQ. */
307 rl_translate_keyseq (seq, array, len)
311 register int i, c, l;
313 for (i = l = 0; c = seq[i]; i++)
322 if (((c == 'C' || c == 'M') && seq[i + 1] == '-') || (c == 'e'))
324 /* Handle special case of backwards define. */
325 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
329 array[l++] = CTRL (_rl_to_upper (seq[i]));
339 array[l++] = ESC; /* XXX */
344 /* Special hack for C-?... */
345 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
364 rl_untranslate_keyseq (seq)
367 static char kseq[16];
379 else if (CTRL_CHAR (c))
386 else if (c == RUBOUT)
399 else if (c == '\\' || c == '"')
404 kseq[i++] = (unsigned char) c;
409 /* Return a pointer to the function that STRING represents.
410 If STRING doesn't have a matching function, then a NULL pointer
413 rl_named_function (string)
418 rl_initialize_funmap ();
420 for (i = 0; funmap[i]; i++)
421 if (_rl_stricmp (funmap[i]->name, string) == 0)
422 return (funmap[i]->function);
423 return ((Function *)NULL);
426 /* Return the function (or macro) definition which would be invoked via
427 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
428 used. TYPE, if non-NULL, is a pointer to an int which will receive the
429 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
430 or ISMACR (macro). */
432 rl_function_of_keyseq (keyseq, map, type)
442 for (i = 0; keyseq && keyseq[i]; i++)
446 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
448 if (map[ESC].type != ISKMAP)
451 *type = map[ESC].type;
453 return (map[ESC].function);
457 map = FUNCTION_TO_KEYMAP (map, ESC);
462 if (map[ic].type == ISKMAP)
464 /* If this is the last key in the key sequence, return the
471 return (map[ic].function);
474 map = FUNCTION_TO_KEYMAP (map, ic);
479 *type = map[ic].type;
481 return (map[ic].function);
484 return ((Function *) NULL);
487 /* The last key bindings file read. */
488 static char *last_readline_init_file = (char *)NULL;
490 /* The file we're currently reading key bindings from. */
491 static char *current_readline_init_file;
492 static int current_readline_init_lineno;
494 /* Re-read the current keybindings file. */
496 rl_re_read_init_file (count, ignore)
500 r = rl_read_init_file ((char *)NULL);
501 rl_set_keymap_from_edit_mode ();
505 /* Do key bindings from a file. If FILENAME is NULL it defaults
506 to the first non-null filename from this list:
507 1. the filename used for the previous call
508 2. the value of the shell variable `INPUTRC'
510 If the file existed and could be opened and read, 0 is returned,
511 otherwise errno is returned. */
513 rl_read_init_file (filename)
517 char *buffer, *openname, *line, *end;
521 /* Default the filename. */
524 filename = last_readline_init_file;
526 filename = getenv ("INPUTRC");
528 filename = DEFAULT_INPUTRC;
532 filename = DEFAULT_INPUTRC;
534 current_readline_init_file = filename;
535 openname = tilde_expand (filename);
537 if ((stat (openname, &finfo) < 0) ||
538 (file = open (openname, O_RDONLY, 0666)) < 0)
546 if (filename != last_readline_init_file)
548 if (last_readline_init_file)
549 free (last_readline_init_file);
551 last_readline_init_file = savestring (filename);
554 /* Read the file into BUFFER. */
555 buffer = (char *)xmalloc ((int)finfo.st_size + 1);
556 i = read (file, buffer, finfo.st_size);
559 if (i != finfo.st_size)
562 /* Loop over the lines in the file. Lines that start with `#' are
563 comments; all other lines are commands for readline initialization. */
564 current_readline_init_lineno = 1;
566 end = buffer + finfo.st_size;
569 /* Find the end of this line. */
570 for (i = 0; line + i != end && line[i] != '\n'; i++);
572 /* Mark end of line. */
575 /* Skip leading whitespace. */
576 while (*line && whitespace (*line))
582 /* If the line is not a comment, then parse it. */
583 if (*line && *line != '#')
584 rl_parse_and_bind (line);
586 /* Move to the next line. */
588 current_readline_init_lineno++;
595 _rl_init_file_error (msg)
598 fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
599 current_readline_init_lineno,
603 /* **************************************************************** */
605 /* Parser Directives */
607 /* **************************************************************** */
611 /* Calling programs set this to have their argv[0]. */
612 char *rl_readline_name = "other";
614 /* Stack of previous values of parsing_conditionalized_out. */
615 static unsigned char *if_stack = (unsigned char *)NULL;
616 static int if_stack_depth;
617 static int if_stack_size;
619 /* Push _rl_parsing_conditionalized_out, and set parser state based
627 /* Push parser state. */
628 if (if_stack_depth + 1 >= if_stack_size)
631 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
633 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
635 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
637 /* If parsing is turned off, then nothing can turn it back on except
638 for finding the matching endif. In that case, return right now. */
639 if (_rl_parsing_conditionalized_out)
642 /* Isolate first argument. */
643 for (i = 0; args[i] && !whitespace (args[i]); i++);
648 /* Handle "if term=foo" and "if mode=emacs" constructs. If this
649 isn't term=foo, or mode=emacs, then check to see if the first
650 word in ARGS is the same as the value stored in rl_readline_name. */
651 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
655 /* Terminals like "aaa-60" are equivalent to "aaa". */
656 tname = savestring (rl_terminal_name);
657 tem = strchr (tname, '-');
661 /* Test the `long' and `short' forms of the terminal name so that
662 if someone has a `sun-cmd' and does not want to have bindings
663 that will be executed if the terminal is a `sun', they can put
664 `$if term=sun-cmd' into their .inputrc. */
665 _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
666 _rl_stricmp (args + 5, rl_terminal_name);
669 #if defined (VI_MODE)
670 else if (_rl_strnicmp (args, "mode=", 5) == 0)
674 if (_rl_stricmp (args + 5, "emacs") == 0)
676 else if (_rl_stricmp (args + 5, "vi") == 0)
681 _rl_parsing_conditionalized_out = mode != rl_editing_mode;
684 /* Check to see if the first word in ARGS is the same as the
685 value stored in rl_readline_name. */
686 else if (_rl_stricmp (args, rl_readline_name) == 0)
687 _rl_parsing_conditionalized_out = 0;
689 _rl_parsing_conditionalized_out = 1;
693 /* Invert the current parser state if there is anything on the stack. */
706 /* Check the previous (n - 1) levels of the stack to make sure that
707 we haven't previously turned off parsing. */
708 for (i = 0; i < if_stack_depth - 1; i++)
709 if (if_stack[i] == 1)
712 /* Invert the state of parsing if at top level. */
713 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
717 /* Terminate a conditional, popping the value of
718 _rl_parsing_conditionalized_out from the stack. */
724 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
727 /* *** What, no error message? *** */
732 /* Associate textual names with actual functions. */
736 } parser_directives [] = {
738 { "endif", parser_endif },
739 { "else", parser_else },
740 { (char *)0x0, (Function *)0x0 }
743 /* Handle a parser directive. STATEMENT is the line of the directive
744 without any leading `$'. */
746 handle_parser_directive (statement)
750 char *directive, *args;
752 /* Isolate the actual directive. */
754 /* Skip whitespace. */
755 for (i = 0; whitespace (statement[i]); i++);
757 directive = &statement[i];
759 for (; statement[i] && !whitespace (statement[i]); i++);
762 statement[i++] = '\0';
764 for (; statement[i] && whitespace (statement[i]); i++);
766 args = &statement[i];
768 /* Lookup the command, and act on it. */
769 for (i = 0; parser_directives[i].name; i++)
770 if (_rl_stricmp (directive, parser_directives[i].name) == 0)
772 (*parser_directives[i].function) (args);
776 /* *** Should an error message be output? */
780 /* Read the binding command from STRING and perform it.
781 A key binding command looks like: Keyname: function-name\0,
782 a variable binding command looks like: set variable value.
783 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
785 rl_parse_and_bind (string)
788 char *funname, *kname;
790 int key, equivalency;
792 while (string && whitespace (*string))
795 if (!string || !*string || *string == '#')
798 /* If this is a parser directive, act on it. */
801 handle_parser_directive (&string[1]);
805 /* If we aren't supposed to be parsing right now, then we're done. */
806 if (_rl_parsing_conditionalized_out)
810 /* If this keyname is a complex key expression surrounded by quotes,
811 advance to after the matching close quote. This code allows the
812 backslash to quote characters in the key expression. */
817 for (i = 1; c = string[i]; i++)
834 /* If we didn't find a closing quote, abort the line. */
835 if (string[i] == '\0')
837 _rl_init_file_error ("no closing `\"' in key binding");
842 /* Advance to the colon (:) or whitespace which separates the two objects. */
843 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
845 equivalency = (c == ':' && string[i + 1] == '=');
847 /* Mark the end of the command (or keyname). */
851 /* If doing assignment, skip the '=' sign as well. */
855 /* If this is a command to set a variable, then do that. */
856 if (_rl_stricmp (string, "set") == 0)
858 char *var = string + i;
861 /* Make VAR point to start of variable name. */
862 while (*var && whitespace (*var)) var++;
864 /* Make value point to start of value string. */
866 while (*value && !whitespace (*value)) value++;
869 while (*value && whitespace (*value)) value++;
871 rl_variable_bind (var, value);
875 /* Skip any whitespace between keyname and funname. */
876 for (; string[i] && whitespace (string[i]); i++);
877 funname = &string[i];
879 /* Now isolate funname.
880 For straight function names just look for whitespace, since
881 that will signify the end of the string. But this could be a
882 macro definition. In that case, the string is quoted, so skip
883 to the matching delimiter. We allow the backslash to quote the
884 delimiter characters in the macro body. */
885 /* This code exists to allow whitespace in macro expansions, which
886 would otherwise be gobbled up by the next `for' loop.*/
887 /* XXX - it may be desirable to allow backslash quoting only if " is
888 the quoted string delimiter, like the shell. */
889 if (*funname == '\'' || *funname == '"')
891 int delimiter = string[i++];
894 for (; c = string[i]; i++)
915 /* Advance to the end of the string. */
916 for (; string[i] && !whitespace (string[i]); i++);
918 /* No extra whitespace at the end of the string. */
921 /* Handle equivalency bindings here. Make the left-hand side be exactly
922 whatever the right-hand evaluates to, including keymaps. */
928 /* If this is a new-style key-binding, then do the binding with
929 rl_set_key (). Otherwise, let the older code deal with it. */
932 char *seq = xmalloc (1 + strlen (string));
933 register int j, k = 0;
936 for (j = 1; string[j]; j++)
938 /* Allow backslash to quote characters, but leave them in place.
939 This allows a string to end with a backslash quoting another
940 backslash, or with a backslash quoting a double quote. The
941 backslashes are left in place for rl_translate_keyseq (). */
942 if (passc || (string[j] == '\\'))
944 seq[k++] = string[j];
949 if (string[j] == '"')
952 seq[k++] = string[j];
957 if (*funname == '\'' || *funname == '"')
959 j = strlen (funname);
961 /* Remove the delimiting quotes from each end of FUNNAME. */
962 if (j && funname[j - 1] == *funname)
963 funname[j - 1] = '\0';
965 rl_macro_bind (seq, &funname[1], _rl_keymap);
968 rl_set_key (seq, rl_named_function (funname), _rl_keymap);
974 /* Get the actual character we want to deal with. */
975 kname = strrchr (string, '-');
981 key = glean_key_from_name (kname);
983 /* Add in control and meta bits. */
984 if (substring_member_of_array (string, possible_control_prefixes))
985 key = CTRL (_rl_to_upper (key));
987 if (substring_member_of_array (string, possible_meta_prefixes))
990 /* Temporary. Handle old-style keyname with macro-binding. */
991 if (*funname == '\'' || *funname == '"')
994 int fl = strlen (funname);
996 seq[0] = key; seq[1] = '\0';
997 if (fl && funname[fl - 1] == *funname)
998 funname[fl - 1] = '\0';
1000 rl_macro_bind (seq, &funname[1], _rl_keymap);
1002 #if defined (PREFIX_META_HACK)
1003 /* Ugly, but working hack to keep prefix-meta around. */
1004 else if (_rl_stricmp (funname, "prefix-meta") == 0)
1010 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1012 #endif /* PREFIX_META_HACK */
1014 rl_bind_key (key, rl_named_function (funname));
1018 /* Simple structure for boolean readline variables (i.e., those that can
1019 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1025 } boolean_varlist [] = {
1026 #if defined (PAREN_MATCHING)
1027 { "blink-matching-paren", &rl_blink_matching_paren },
1029 { "convert-meta", &_rl_convert_meta_chars_to_ascii },
1030 { "disable-completion", &rl_inhibit_completion },
1031 { "enable-keypad", &_rl_enable_keypad },
1032 { "expand-tilde", &rl_complete_with_tilde_expansion },
1033 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode },
1034 { "input-meta", &_rl_meta_flag },
1035 { "mark-directories", &_rl_complete_mark_directories },
1036 { "mark-modified-lines", &_rl_mark_modified_lines },
1037 { "meta-flag", &_rl_meta_flag },
1038 { "output-meta", &_rl_output_meta_chars },
1039 { "show-all-if-ambiguous", &_rl_complete_show_all },
1040 #if defined (VISIBLE_STATS)
1041 { "visible-stats", &rl_visible_stats },
1042 #endif /* VISIBLE_STATS */
1043 { (char *)NULL, (int *)NULL }
1047 rl_variable_bind (name, value)
1052 /* Check for simple variables first. */
1053 for (i = 0; boolean_varlist[i].name; i++)
1055 if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1057 /* A variable is TRUE if the "value" is "on", "1" or "". */
1058 *boolean_varlist[i].value = *value == 0 ||
1059 _rl_stricmp (value, "on") == 0 ||
1060 (value[0] == '1' && value[1] == '\0');
1065 /* Not a boolean variable, so check for specials. */
1067 /* Editing mode change? */
1068 if (_rl_stricmp (name, "editing-mode") == 0)
1070 if (_rl_strnicmp (value, "vi", 2) == 0)
1072 #if defined (VI_MODE)
1073 _rl_keymap = vi_insertion_keymap;
1074 rl_editing_mode = vi_mode;
1075 #endif /* VI_MODE */
1077 else if (_rl_strnicmp (value, "emacs", 5) == 0)
1079 _rl_keymap = emacs_standard_keymap;
1080 rl_editing_mode = emacs_mode;
1084 /* Comment string change? */
1085 else if (_rl_stricmp (name, "comment-begin") == 0)
1089 if (_rl_comment_begin)
1090 free (_rl_comment_begin);
1092 _rl_comment_begin = savestring (value);
1095 else if (_rl_stricmp (name, "completion-query-items") == 0)
1100 nval = atoi (value);
1104 rl_completion_query_items = nval;
1106 else if (_rl_stricmp (name, "keymap") == 0)
1109 kmap = rl_get_keymap_by_name (value);
1111 rl_set_keymap (kmap);
1113 else if (_rl_stricmp (name, "bell-style") == 0)
1116 _rl_bell_preference = AUDIBLE_BELL;
1119 if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1120 _rl_bell_preference = NO_BELL;
1121 else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1122 _rl_bell_preference = AUDIBLE_BELL;
1123 else if (_rl_stricmp (value, "visible") == 0)
1124 _rl_bell_preference = VISIBLE_BELL;
1127 else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1129 /* Backwards compatibility. */
1130 if (*value && (_rl_stricmp (value, "on") == 0 ||
1131 (*value == '1' && !value[1])))
1132 _rl_bell_preference = VISIBLE_BELL;
1134 _rl_bell_preference = AUDIBLE_BELL;
1140 /* Return the character which matches NAME.
1141 For example, `Space' returns ' '. */
1148 static assoc_list name_key_alist[] = {
1151 { "Escape", '\033' },
1153 { "Newline", '\n' },
1164 glean_key_from_name (name)
1169 for (i = 0; name_key_alist[i].name; i++)
1170 if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1171 return (name_key_alist[i].value);
1173 return (*(unsigned char *)name); /* XXX was return (*name) */
1176 /* Auxiliary functions to manage keymaps. */
1180 } keymap_names[] = {
1181 { "emacs", emacs_standard_keymap },
1182 { "emacs-standard", emacs_standard_keymap },
1183 { "emacs-meta", emacs_meta_keymap },
1184 { "emacs-ctlx", emacs_ctlx_keymap },
1185 #if defined (VI_MODE)
1186 { "vi", vi_movement_keymap },
1187 { "vi-move", vi_movement_keymap },
1188 { "vi-command", vi_movement_keymap },
1189 { "vi-insert", vi_insertion_keymap },
1190 #endif /* VI_MODE */
1191 { (char *)0x0, (Keymap)0x0 }
1195 rl_get_keymap_by_name (name)
1200 for (i = 0; keymap_names[i].name; i++)
1201 if (strcmp (name, keymap_names[i].name) == 0)
1202 return (keymap_names[i].map);
1203 return ((Keymap) NULL);
1207 rl_get_keymap_name (map)
1211 for (i = 0; keymap_names[i].name; i++)
1212 if (map == keymap_names[i].map)
1213 return (keymap_names[i].name);
1214 return ((char *)NULL);
1228 return (_rl_keymap);
1232 rl_set_keymap_from_edit_mode ()
1234 if (rl_editing_mode == emacs_mode)
1235 _rl_keymap = emacs_standard_keymap;
1236 #if defined (VI_MODE)
1237 else if (rl_editing_mode == vi_mode)
1238 _rl_keymap = vi_insertion_keymap;
1239 #endif /* VI_MODE */
1243 rl_get_keymap_name_from_edit_mode ()
1245 if (rl_editing_mode == emacs_mode)
1247 #if defined (VI_MODE)
1248 else if (rl_editing_mode == vi_mode)
1250 #endif /* VI_MODE */
1255 /* **************************************************************** */
1257 /* Key Binding and Function Information */
1259 /* **************************************************************** */
1261 /* Each of the following functions produces information about the
1262 state of keybindings and functions known to Readline. The info
1263 is always printed to rl_outstream, and in such a way that it can
1264 be read back in (i.e., passed to rl_parse_and_bind (). */
1266 /* Print the names of functions known to Readline. */
1268 rl_list_funmap_names ()
1271 char **funmap_names;
1273 funmap_names = rl_funmap_names ();
1278 for (i = 0; funmap_names[i]; i++)
1279 fprintf (rl_outstream, "%s\n", funmap_names[i]);
1281 free (funmap_names);
1285 _rl_get_keyname (key)
1291 keyname = (char *)xmalloc (8);
1294 /* Since this is going to be used to write out keysequence-function
1295 pairs for possible inclusion in an inputrc file, we don't want to
1296 do any special meta processing on KEY. */
1299 /* We might want to do this, but the old version of the code did not. */
1301 /* If this is an escape character, we don't want to do any more processing.
1302 Just add the special ESC key sequence and return. */
1312 /* RUBOUT is translated directly into \C-? */
1324 /* Now add special prefixes needed for control characters. This can
1325 potentially change C. */
1328 keyname[i++] = '\\';
1331 c = _rl_to_lower (UNCTRL (c));
1334 /* Now, if the character needs to be quoted with a backslash, do that. */
1335 if (c == '\\' || c == '"')
1336 keyname[i++] = '\\';
1338 /* Now add the key, terminate the string, and return it. */
1339 keyname[i++] = (char) c;
1345 /* Return a NULL terminated array of strings which represent the key
1346 sequences that are used to invoke FUNCTION in MAP. */
1348 rl_invoking_keyseqs_in_map (function, map)
1354 int result_index, result_size;
1356 result = (char **)NULL;
1357 result_index = result_size = 0;
1359 for (key = 0; key < KEYMAP_SIZE; key++)
1361 switch (map[key].type)
1364 /* Macros match, if, and only if, the pointers are identical.
1365 Thus, they are treated exactly like functions in here. */
1367 /* If the function in the keymap is the one we are looking for,
1368 then add the current KEY to the list of invoking keys. */
1369 if (map[key].function == function)
1373 keyname = _rl_get_keyname (key);
1375 if (result_index + 2 > result_size)
1378 result = (char **) xrealloc (result, result_size * sizeof (char *));
1381 result[result_index++] = keyname;
1382 result[result_index] = (char *)NULL;
1391 /* Find the list of keyseqs in this map which have FUNCTION as
1392 their target. Add the key sequences found to RESULT. */
1393 if (map[key].function)
1395 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1402 for (i = 0; seqs[i]; i++)
1404 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1407 sprintf (keyname, "\\e");
1408 else if (CTRL_CHAR (key))
1409 sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1410 else if (key == RUBOUT)
1411 sprintf (keyname, "\\C-?");
1412 else if (key == '\\' || key == '"')
1415 keyname[1] = (char) key;
1420 keyname[0] = (char) key;
1424 strcat (keyname, seqs[i]);
1427 if (result_index + 2 > result_size)
1430 result = (char **) xrealloc (result, result_size * sizeof (char *));
1433 result[result_index++] = keyname;
1434 result[result_index] = (char *)NULL;
1445 /* Return a NULL terminated array of strings which represent the key
1446 sequences that can be used to invoke FUNCTION using the current keymap. */
1448 rl_invoking_keyseqs (function)
1451 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1454 /* Print all of the functions and their bindings to rl_outstream. If
1455 PRINT_READABLY is non-zero, then print the output in such a way
1456 that it can be read back in. */
1458 rl_function_dumper (print_readably)
1465 names = rl_funmap_names ();
1467 fprintf (rl_outstream, "\n");
1469 for (i = 0; name = names[i]; i++)
1474 function = rl_named_function (name);
1475 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1480 fprintf (rl_outstream, "# %s (not bound)\n", name);
1485 for (j = 0; invokers[j]; j++)
1487 fprintf (rl_outstream, "\"%s\": %s\n",
1498 fprintf (rl_outstream, "%s is not bound to any keys\n",
1504 fprintf (rl_outstream, "%s can be found on ", name);
1506 for (j = 0; invokers[j] && j < 5; j++)
1508 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1509 invokers[j + 1] ? ", " : ".\n");
1512 if (j == 5 && invokers[j])
1513 fprintf (rl_outstream, "...\n");
1515 for (j = 0; invokers[j]; j++)
1524 /* Print all of the current functions and their bindings to
1525 rl_outstream. If an explicit argument is given, then print
1526 the output in such a way that it can be read back in. */
1528 rl_dump_functions (count, key)
1531 rl_function_dumper (rl_explicit_arg);
1537 _rl_macro_dumper_internal (print_readably, map, prefix)
1543 char *keyname, *out;
1546 for (key = 0; key < KEYMAP_SIZE; key++)
1548 switch (map[key].type)
1551 keyname = _rl_get_keyname (key);
1552 out = (char *)map[key].function;
1554 fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
1558 fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
1566 prefix_len = prefix ? strlen (prefix) : 0;
1569 keyname = xmalloc (3 + prefix_len);
1571 strcpy (keyname, prefix);
1572 keyname[prefix_len] = '\\';
1573 keyname[prefix_len + 1] = 'e';
1574 keyname[prefix_len + 2] = '\0';
1578 keyname = _rl_get_keyname (key);
1581 out = xmalloc (strlen (keyname) + prefix_len + 1);
1582 strcpy (out, prefix);
1583 strcpy (out + prefix_len, keyname);
1589 _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
1597 rl_macro_dumper (print_readably)
1600 _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
1604 rl_dump_macros (count, key)
1607 rl_macro_dumper (rl_explicit_arg);
1613 rl_variable_dumper (print_readably)
1619 for (i = 0; boolean_varlist[i].name; i++)
1622 fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
1623 *boolean_varlist[i].value ? "on" : "off");
1625 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
1626 *boolean_varlist[i].value ? "on" : "off");
1630 switch (_rl_bell_preference)
1632 case NO_BELL: kname = "none"; break;
1633 case VISIBLE_BELL: kname = "visible"; break;
1635 default: kname = "audible"; break;
1638 fprintf (rl_outstream, "set bell-style %s\n", kname);
1640 fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
1644 fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
1646 fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : "");
1648 /* completion-query-items */
1650 fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items);
1652 fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items);
1656 fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1658 fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1661 kname = rl_get_keymap_name (_rl_keymap);
1663 kname = rl_get_keymap_name_from_edit_mode ();
1665 fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none");
1667 fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none");
1670 /* Print all of the current variables and their values to
1671 rl_outstream. If an explicit argument is given, then print
1672 the output in such a way that it can be read back in. */
1674 rl_dump_variables (count, key)
1677 rl_variable_dumper (rl_explicit_arg);
1682 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
1684 _rl_bind_if_unbound (keyseq, default_func)
1686 Function *default_func;
1692 func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
1693 if (!func || func == rl_do_lowercase_version)
1694 rl_set_key (keyseq, default_func, _rl_keymap);
1698 /* Return non-zero if any members of ARRAY are a substring in STRING. */
1700 substring_member_of_array (string, array)
1701 char *string, **array;
1705 if (_rl_strindex (string, *array))