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 /* Functions imported from funmap.c */
93 extern char **rl_funmap_names ();
94 extern int rl_add_funmap_entry ();
96 /* Functions imported from util.c */
97 extern char *_rl_strindex ();
99 /* Functions imported from shell.c */
100 extern char *get_env_value ();
102 /* Variables exported by this file. */
103 Keymap rl_binding_keymap;
105 /* Forward declarations */
106 void rl_set_keymap_from_edit_mode ();
108 static int glean_key_from_name ();
109 static int substring_member_of_array ();
111 extern char *xmalloc (), *xrealloc ();
113 /* **************************************************************** */
117 /* **************************************************************** */
119 /* rl_add_defun (char *name, Function *function, int key)
120 Add NAME to the list of named functions. Make FUNCTION be the function
121 that gets called. If KEY is not -1, then bind it. */
123 rl_add_defun (name, function, key)
129 rl_bind_key (key, function);
130 rl_add_funmap_entry (name, function);
134 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
136 rl_bind_key (key, function)
143 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
145 if (_rl_keymap[ESC].type == ISKMAP)
149 escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
151 escmap[key].type = ISFUNC;
152 escmap[key].function = function;
158 _rl_keymap[key].type = ISFUNC;
159 _rl_keymap[key].function = function;
160 rl_binding_keymap = _rl_keymap;
164 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
167 rl_bind_key_in_map (key, function, map)
177 result = rl_bind_key (key, function);
182 /* Make KEY do nothing in the currently selected keymap.
183 Returns non-zero in case of error. */
188 return (rl_bind_key (key, (Function *)NULL));
191 /* Make KEY do nothing in MAP.
192 Returns non-zero in case of error. */
194 rl_unbind_key_in_map (key, map)
198 return (rl_bind_key_in_map (key, (Function *)NULL, map));
201 /* Bind the key sequence represented by the string KEYSEQ to
202 FUNCTION. This makes new keymaps as necessary. The initial
203 place to do bindings is in MAP. */
205 rl_set_key (keyseq, function, map)
210 return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
213 /* Bind the key sequence represented by the string KEYSEQ to
214 the string of characters MACRO. This makes new keymaps as
215 necessary. The initial place to do bindings is in MAP. */
217 rl_macro_bind (keyseq, macro, map)
218 char *keyseq, *macro;
224 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
226 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
231 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
235 /* Bind the key sequence represented by the string KEYSEQ to
236 the arbitrary pointer DATA. TYPE says what kind of data is
237 pointed to by DATA, right now this can be a function (ISFUNC),
238 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
239 as necessary. The initial place to do bindings is in MAP. */
241 rl_generic_bind (type, keyseq, data, map)
250 /* If no keys to bind to, exit right away. */
251 if (!keyseq || !*keyseq)
258 keys = xmalloc (1 + (2 * strlen (keyseq)));
260 /* Translate the ASCII representation of KEYSEQ into an array of
261 characters. Stuff the characters into KEYS, and the length of
262 KEYS into KEYS_LEN. */
263 if (rl_translate_keyseq (keyseq, keys, &keys_len))
269 /* Bind keys, making new keymaps as necessary. */
270 for (i = 0; i < keys_len; i++)
272 int ic = (int) ((unsigned char)keys[i]);
274 if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
277 if (map[ESC].type == ISKMAP)
278 map = FUNCTION_TO_KEYMAP (map, ESC);
281 if ((i + 1) < keys_len)
283 if (map[ic].type != ISKMAP)
285 if (map[ic].type == ISMACR)
286 free ((char *)map[ic].function);
288 map[ic].type = ISKMAP;
289 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
291 map = FUNCTION_TO_KEYMAP (map, ic);
295 if (map[ic].type == ISMACR)
296 free ((char *)map[ic].function);
298 map[ic].function = KEYMAP_TO_FUNCTION (data);
302 rl_binding_keymap = map;
308 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
309 an array of characters. LEN gets the final length of ARRAY. Return
310 non-zero if there was an error parsing SEQ. */
312 rl_translate_keyseq (seq, array, len)
316 register int i, c, l;
318 for (i = l = 0; c = seq[i]; i++)
327 if (((c == 'C' || c == 'M') && seq[i + 1] == '-') || (c == 'e'))
329 /* Handle special case of backwards define. */
330 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
334 array[l++] = CTRL (_rl_to_upper (seq[i]));
344 array[l++] = ESC; /* XXX */
349 /* Special hack for C-?... */
350 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
369 rl_untranslate_keyseq (seq)
372 static char kseq[16];
384 else if (CTRL_CHAR (c))
389 c = _rl_to_lower (UNCTRL (c));
391 else if (c == RUBOUT)
404 else if (c == '\\' || c == '"')
409 kseq[i++] = (unsigned char) c;
415 _rl_untranslate_macro_value (seq)
421 r = ret = xmalloc (7 * strlen (seq) + 1);
422 for (s = seq; *s; s++)
432 else if (CTRL_CHAR (c) && c != ESC)
437 c = _rl_to_lower (UNCTRL (c));
439 else if (c == RUBOUT)
452 else if (c == '\\' || c == '"')
455 *r++ = (unsigned char)c;
461 /* Return a pointer to the function that STRING represents.
462 If STRING doesn't have a matching function, then a NULL pointer
465 rl_named_function (string)
470 rl_initialize_funmap ();
472 for (i = 0; funmap[i]; i++)
473 if (_rl_stricmp (funmap[i]->name, string) == 0)
474 return (funmap[i]->function);
475 return ((Function *)NULL);
478 /* Return the function (or macro) definition which would be invoked via
479 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
480 used. TYPE, if non-NULL, is a pointer to an int which will receive the
481 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
482 or ISMACR (macro). */
484 rl_function_of_keyseq (keyseq, map, type)
494 for (i = 0; keyseq && keyseq[i]; i++)
498 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
500 if (map[ESC].type != ISKMAP)
503 *type = map[ESC].type;
505 return (map[ESC].function);
509 map = FUNCTION_TO_KEYMAP (map, ESC);
514 if (map[ic].type == ISKMAP)
516 /* If this is the last key in the key sequence, return the
523 return (map[ic].function);
526 map = FUNCTION_TO_KEYMAP (map, ic);
531 *type = map[ic].type;
533 return (map[ic].function);
536 return ((Function *) NULL);
539 /* The last key bindings file read. */
540 static char *last_readline_init_file = (char *)NULL;
542 /* The file we're currently reading key bindings from. */
543 static char *current_readline_init_file;
544 static int current_readline_init_lineno;
546 /* Re-read the current keybindings file. */
548 rl_re_read_init_file (count, ignore)
552 r = rl_read_init_file ((char *)NULL);
553 rl_set_keymap_from_edit_mode ();
557 /* Do key bindings from a file. If FILENAME is NULL it defaults
558 to the first non-null filename from this list:
559 1. the filename used for the previous call
560 2. the value of the shell variable `INPUTRC'
562 If the file existed and could be opened and read, 0 is returned,
563 otherwise errno is returned. */
565 rl_read_init_file (filename)
569 char *buffer, *openname, *line, *end;
573 /* Default the filename. */
576 filename = last_readline_init_file;
578 filename = get_env_value ("INPUTRC");
580 filename = DEFAULT_INPUTRC;
584 filename = DEFAULT_INPUTRC;
586 current_readline_init_file = filename;
587 openname = tilde_expand (filename);
589 if ((stat (openname, &finfo) < 0) ||
590 (file = open (openname, O_RDONLY, 0666)) < 0)
598 if (filename != last_readline_init_file)
600 if (last_readline_init_file)
601 free (last_readline_init_file);
603 last_readline_init_file = savestring (filename);
606 /* Read the file into BUFFER. */
607 buffer = (char *)xmalloc ((int)finfo.st_size + 1);
608 i = read (file, buffer, finfo.st_size);
611 if (i != finfo.st_size)
614 /* Loop over the lines in the file. Lines that start with `#' are
615 comments; all other lines are commands for readline initialization. */
616 current_readline_init_lineno = 1;
618 end = buffer + finfo.st_size;
621 /* Find the end of this line. */
622 for (i = 0; line + i != end && line[i] != '\n'; i++);
624 /* Mark end of line. */
627 /* Skip leading whitespace. */
628 while (*line && whitespace (*line))
634 /* If the line is not a comment, then parse it. */
635 if (*line && *line != '#')
636 rl_parse_and_bind (line);
638 /* Move to the next line. */
640 current_readline_init_lineno++;
647 _rl_init_file_error (msg)
650 fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
651 current_readline_init_lineno,
655 /* **************************************************************** */
657 /* Parser Directives */
659 /* **************************************************************** */
663 /* Calling programs set this to have their argv[0]. */
664 char *rl_readline_name = "other";
666 /* Stack of previous values of parsing_conditionalized_out. */
667 static unsigned char *if_stack = (unsigned char *)NULL;
668 static int if_stack_depth;
669 static int if_stack_size;
671 /* Push _rl_parsing_conditionalized_out, and set parser state based
679 /* Push parser state. */
680 if (if_stack_depth + 1 >= if_stack_size)
683 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
685 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
687 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
689 /* If parsing is turned off, then nothing can turn it back on except
690 for finding the matching endif. In that case, return right now. */
691 if (_rl_parsing_conditionalized_out)
694 /* Isolate first argument. */
695 for (i = 0; args[i] && !whitespace (args[i]); i++);
700 /* Handle "if term=foo" and "if mode=emacs" constructs. If this
701 isn't term=foo, or mode=emacs, then check to see if the first
702 word in ARGS is the same as the value stored in rl_readline_name. */
703 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
707 /* Terminals like "aaa-60" are equivalent to "aaa". */
708 tname = savestring (rl_terminal_name);
709 tem = strchr (tname, '-');
713 /* Test the `long' and `short' forms of the terminal name so that
714 if someone has a `sun-cmd' and does not want to have bindings
715 that will be executed if the terminal is a `sun', they can put
716 `$if term=sun-cmd' into their .inputrc. */
717 _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
718 _rl_stricmp (args + 5, rl_terminal_name);
721 #if defined (VI_MODE)
722 else if (_rl_strnicmp (args, "mode=", 5) == 0)
726 if (_rl_stricmp (args + 5, "emacs") == 0)
728 else if (_rl_stricmp (args + 5, "vi") == 0)
733 _rl_parsing_conditionalized_out = mode != rl_editing_mode;
736 /* Check to see if the first word in ARGS is the same as the
737 value stored in rl_readline_name. */
738 else if (_rl_stricmp (args, rl_readline_name) == 0)
739 _rl_parsing_conditionalized_out = 0;
741 _rl_parsing_conditionalized_out = 1;
745 /* Invert the current parser state if there is anything on the stack. */
758 /* Check the previous (n - 1) levels of the stack to make sure that
759 we haven't previously turned off parsing. */
760 for (i = 0; i < if_stack_depth - 1; i++)
761 if (if_stack[i] == 1)
764 /* Invert the state of parsing if at top level. */
765 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
769 /* Terminate a conditional, popping the value of
770 _rl_parsing_conditionalized_out from the stack. */
776 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
779 /* *** What, no error message? *** */
784 /* Associate textual names with actual functions. */
788 } parser_directives [] = {
790 { "endif", parser_endif },
791 { "else", parser_else },
792 { (char *)0x0, (Function *)0x0 }
795 /* Handle a parser directive. STATEMENT is the line of the directive
796 without any leading `$'. */
798 handle_parser_directive (statement)
802 char *directive, *args;
804 /* Isolate the actual directive. */
806 /* Skip whitespace. */
807 for (i = 0; whitespace (statement[i]); i++);
809 directive = &statement[i];
811 for (; statement[i] && !whitespace (statement[i]); i++);
814 statement[i++] = '\0';
816 for (; statement[i] && whitespace (statement[i]); i++);
818 args = &statement[i];
820 /* Lookup the command, and act on it. */
821 for (i = 0; parser_directives[i].name; i++)
822 if (_rl_stricmp (directive, parser_directives[i].name) == 0)
824 (*parser_directives[i].function) (args);
828 /* *** Should an error message be output? */
832 /* Read the binding command from STRING and perform it.
833 A key binding command looks like: Keyname: function-name\0,
834 a variable binding command looks like: set variable value.
835 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
837 rl_parse_and_bind (string)
840 char *funname, *kname;
842 int key, equivalency;
844 while (string && whitespace (*string))
847 if (!string || !*string || *string == '#')
850 /* If this is a parser directive, act on it. */
853 handle_parser_directive (&string[1]);
857 /* If we aren't supposed to be parsing right now, then we're done. */
858 if (_rl_parsing_conditionalized_out)
862 /* If this keyname is a complex key expression surrounded by quotes,
863 advance to after the matching close quote. This code allows the
864 backslash to quote characters in the key expression. */
869 for (i = 1; c = string[i]; i++)
886 /* If we didn't find a closing quote, abort the line. */
887 if (string[i] == '\0')
889 _rl_init_file_error ("no closing `\"' in key binding");
894 /* Advance to the colon (:) or whitespace which separates the two objects. */
895 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
897 equivalency = (c == ':' && string[i + 1] == '=');
899 /* Mark the end of the command (or keyname). */
903 /* If doing assignment, skip the '=' sign as well. */
907 /* If this is a command to set a variable, then do that. */
908 if (_rl_stricmp (string, "set") == 0)
910 char *var = string + i;
913 /* Make VAR point to start of variable name. */
914 while (*var && whitespace (*var)) var++;
916 /* Make value point to start of value string. */
918 while (*value && !whitespace (*value)) value++;
921 while (*value && whitespace (*value)) value++;
923 rl_variable_bind (var, value);
927 /* Skip any whitespace between keyname and funname. */
928 for (; string[i] && whitespace (string[i]); i++);
929 funname = &string[i];
931 /* Now isolate funname.
932 For straight function names just look for whitespace, since
933 that will signify the end of the string. But this could be a
934 macro definition. In that case, the string is quoted, so skip
935 to the matching delimiter. We allow the backslash to quote the
936 delimiter characters in the macro body. */
937 /* This code exists to allow whitespace in macro expansions, which
938 would otherwise be gobbled up by the next `for' loop.*/
939 /* XXX - it may be desirable to allow backslash quoting only if " is
940 the quoted string delimiter, like the shell. */
941 if (*funname == '\'' || *funname == '"')
943 int delimiter = string[i++];
946 for (; c = string[i]; i++)
967 /* Advance to the end of the string. */
968 for (; string[i] && !whitespace (string[i]); i++);
970 /* No extra whitespace at the end of the string. */
973 /* Handle equivalency bindings here. Make the left-hand side be exactly
974 whatever the right-hand evaluates to, including keymaps. */
980 /* If this is a new-style key-binding, then do the binding with
981 rl_set_key (). Otherwise, let the older code deal with it. */
984 char *seq = xmalloc (1 + strlen (string));
985 register int j, k = 0;
988 for (j = 1; string[j]; j++)
990 /* Allow backslash to quote characters, but leave them in place.
991 This allows a string to end with a backslash quoting another
992 backslash, or with a backslash quoting a double quote. The
993 backslashes are left in place for rl_translate_keyseq (). */
994 if (passc || (string[j] == '\\'))
996 seq[k++] = string[j];
1001 if (string[j] == '"')
1004 seq[k++] = string[j];
1008 /* Binding macro? */
1009 if (*funname == '\'' || *funname == '"')
1011 j = strlen (funname);
1013 /* Remove the delimiting quotes from each end of FUNNAME. */
1014 if (j && funname[j - 1] == *funname)
1015 funname[j - 1] = '\0';
1017 rl_macro_bind (seq, &funname[1], _rl_keymap);
1020 rl_set_key (seq, rl_named_function (funname), _rl_keymap);
1026 /* Get the actual character we want to deal with. */
1027 kname = strrchr (string, '-');
1033 key = glean_key_from_name (kname);
1035 /* Add in control and meta bits. */
1036 if (substring_member_of_array (string, possible_control_prefixes))
1037 key = CTRL (_rl_to_upper (key));
1039 if (substring_member_of_array (string, possible_meta_prefixes))
1042 /* Temporary. Handle old-style keyname with macro-binding. */
1043 if (*funname == '\'' || *funname == '"')
1045 unsigned char useq[2];
1046 int fl = strlen (funname);
1048 useq[0] = key; useq[1] = '\0';
1049 if (fl && funname[fl - 1] == *funname)
1050 funname[fl - 1] = '\0';
1052 rl_macro_bind (useq, &funname[1], _rl_keymap);
1054 #if defined (PREFIX_META_HACK)
1055 /* Ugly, but working hack to keep prefix-meta around. */
1056 else if (_rl_stricmp (funname, "prefix-meta") == 0)
1062 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1064 #endif /* PREFIX_META_HACK */
1066 rl_bind_key (key, rl_named_function (funname));
1070 /* Simple structure for boolean readline variables (i.e., those that can
1071 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1077 } boolean_varlist [] = {
1078 #if defined (PAREN_MATCHING)
1079 { "blink-matching-paren", &rl_blink_matching_paren },
1081 { "convert-meta", &_rl_convert_meta_chars_to_ascii },
1082 { "disable-completion", &rl_inhibit_completion },
1083 { "enable-keypad", &_rl_enable_keypad },
1084 { "expand-tilde", &rl_complete_with_tilde_expansion },
1085 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode },
1086 { "input-meta", &_rl_meta_flag },
1087 { "mark-directories", &_rl_complete_mark_directories },
1088 { "mark-modified-lines", &_rl_mark_modified_lines },
1089 { "meta-flag", &_rl_meta_flag },
1090 { "output-meta", &_rl_output_meta_chars },
1091 { "show-all-if-ambiguous", &_rl_complete_show_all },
1092 #if defined (VISIBLE_STATS)
1093 { "visible-stats", &rl_visible_stats },
1094 #endif /* VISIBLE_STATS */
1095 { (char *)NULL, (int *)NULL }
1099 rl_variable_bind (name, value)
1104 /* Check for simple variables first. */
1105 for (i = 0; boolean_varlist[i].name; i++)
1107 if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1109 /* A variable is TRUE if the "value" is "on", "1" or "". */
1110 *boolean_varlist[i].value = *value == 0 ||
1111 _rl_stricmp (value, "on") == 0 ||
1112 (value[0] == '1' && value[1] == '\0');
1117 /* Not a boolean variable, so check for specials. */
1119 /* Editing mode change? */
1120 if (_rl_stricmp (name, "editing-mode") == 0)
1122 if (_rl_strnicmp (value, "vi", 2) == 0)
1124 #if defined (VI_MODE)
1125 _rl_keymap = vi_insertion_keymap;
1126 rl_editing_mode = vi_mode;
1127 #endif /* VI_MODE */
1129 else if (_rl_strnicmp (value, "emacs", 5) == 0)
1131 _rl_keymap = emacs_standard_keymap;
1132 rl_editing_mode = emacs_mode;
1136 /* Comment string change? */
1137 else if (_rl_stricmp (name, "comment-begin") == 0)
1141 if (_rl_comment_begin)
1142 free (_rl_comment_begin);
1144 _rl_comment_begin = savestring (value);
1147 else if (_rl_stricmp (name, "completion-query-items") == 0)
1152 nval = atoi (value);
1156 rl_completion_query_items = nval;
1158 else if (_rl_stricmp (name, "keymap") == 0)
1161 kmap = rl_get_keymap_by_name (value);
1163 rl_set_keymap (kmap);
1165 else if (_rl_stricmp (name, "bell-style") == 0)
1168 _rl_bell_preference = AUDIBLE_BELL;
1171 if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1172 _rl_bell_preference = NO_BELL;
1173 else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1174 _rl_bell_preference = AUDIBLE_BELL;
1175 else if (_rl_stricmp (value, "visible") == 0)
1176 _rl_bell_preference = VISIBLE_BELL;
1179 else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1181 /* Backwards compatibility. */
1182 if (*value && (_rl_stricmp (value, "on") == 0 ||
1183 (*value == '1' && !value[1])))
1184 _rl_bell_preference = VISIBLE_BELL;
1186 _rl_bell_preference = AUDIBLE_BELL;
1192 /* Return the character which matches NAME.
1193 For example, `Space' returns ' '. */
1200 static assoc_list name_key_alist[] = {
1203 { "Escape", '\033' },
1205 { "Newline", '\n' },
1216 glean_key_from_name (name)
1221 for (i = 0; name_key_alist[i].name; i++)
1222 if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1223 return (name_key_alist[i].value);
1225 return (*(unsigned char *)name); /* XXX was return (*name) */
1228 /* Auxiliary functions to manage keymaps. */
1232 } keymap_names[] = {
1233 { "emacs", emacs_standard_keymap },
1234 { "emacs-standard", emacs_standard_keymap },
1235 { "emacs-meta", emacs_meta_keymap },
1236 { "emacs-ctlx", emacs_ctlx_keymap },
1237 #if defined (VI_MODE)
1238 { "vi", vi_movement_keymap },
1239 { "vi-move", vi_movement_keymap },
1240 { "vi-command", vi_movement_keymap },
1241 { "vi-insert", vi_insertion_keymap },
1242 #endif /* VI_MODE */
1243 { (char *)0x0, (Keymap)0x0 }
1247 rl_get_keymap_by_name (name)
1252 for (i = 0; keymap_names[i].name; i++)
1253 if (strcmp (name, keymap_names[i].name) == 0)
1254 return (keymap_names[i].map);
1255 return ((Keymap) NULL);
1259 rl_get_keymap_name (map)
1263 for (i = 0; keymap_names[i].name; i++)
1264 if (map == keymap_names[i].map)
1265 return (keymap_names[i].name);
1266 return ((char *)NULL);
1280 return (_rl_keymap);
1284 rl_set_keymap_from_edit_mode ()
1286 if (rl_editing_mode == emacs_mode)
1287 _rl_keymap = emacs_standard_keymap;
1288 #if defined (VI_MODE)
1289 else if (rl_editing_mode == vi_mode)
1290 _rl_keymap = vi_insertion_keymap;
1291 #endif /* VI_MODE */
1295 rl_get_keymap_name_from_edit_mode ()
1297 if (rl_editing_mode == emacs_mode)
1299 #if defined (VI_MODE)
1300 else if (rl_editing_mode == vi_mode)
1302 #endif /* VI_MODE */
1307 /* **************************************************************** */
1309 /* Key Binding and Function Information */
1311 /* **************************************************************** */
1313 /* Each of the following functions produces information about the
1314 state of keybindings and functions known to Readline. The info
1315 is always printed to rl_outstream, and in such a way that it can
1316 be read back in (i.e., passed to rl_parse_and_bind (). */
1318 /* Print the names of functions known to Readline. */
1320 rl_list_funmap_names ()
1323 char **funmap_names;
1325 funmap_names = rl_funmap_names ();
1330 for (i = 0; funmap_names[i]; i++)
1331 fprintf (rl_outstream, "%s\n", funmap_names[i]);
1333 free (funmap_names);
1337 _rl_get_keyname (key)
1343 keyname = (char *)xmalloc (8);
1346 /* Since this is going to be used to write out keysequence-function
1347 pairs for possible inclusion in an inputrc file, we don't want to
1348 do any special meta processing on KEY. */
1351 /* We might want to do this, but the old version of the code did not. */
1353 /* If this is an escape character, we don't want to do any more processing.
1354 Just add the special ESC key sequence and return. */
1364 /* RUBOUT is translated directly into \C-? */
1376 /* Now add special prefixes needed for control characters. This can
1377 potentially change C. */
1380 keyname[i++] = '\\';
1383 c = _rl_to_lower (UNCTRL (c));
1386 /* Now, if the character needs to be quoted with a backslash, do that. */
1387 if (c == '\\' || c == '"')
1388 keyname[i++] = '\\';
1390 /* Now add the key, terminate the string, and return it. */
1391 keyname[i++] = (char) c;
1397 /* Return a NULL terminated array of strings which represent the key
1398 sequences that are used to invoke FUNCTION in MAP. */
1400 rl_invoking_keyseqs_in_map (function, map)
1406 int result_index, result_size;
1408 result = (char **)NULL;
1409 result_index = result_size = 0;
1411 for (key = 0; key < KEYMAP_SIZE; key++)
1413 switch (map[key].type)
1416 /* Macros match, if, and only if, the pointers are identical.
1417 Thus, they are treated exactly like functions in here. */
1419 /* If the function in the keymap is the one we are looking for,
1420 then add the current KEY to the list of invoking keys. */
1421 if (map[key].function == function)
1425 keyname = _rl_get_keyname (key);
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;
1443 /* Find the list of keyseqs in this map which have FUNCTION as
1444 their target. Add the key sequences found to RESULT. */
1445 if (map[key].function)
1447 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1454 for (i = 0; seqs[i]; i++)
1456 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1459 sprintf (keyname, "\\e");
1460 else if (CTRL_CHAR (key))
1461 sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1462 else if (key == RUBOUT)
1463 sprintf (keyname, "\\C-?");
1464 else if (key == '\\' || key == '"')
1467 keyname[1] = (char) key;
1472 keyname[0] = (char) key;
1476 strcat (keyname, seqs[i]);
1479 if (result_index + 2 > result_size)
1482 result = (char **) xrealloc (result, result_size * sizeof (char *));
1485 result[result_index++] = keyname;
1486 result[result_index] = (char *)NULL;
1497 /* Return a NULL terminated array of strings which represent the key
1498 sequences that can be used to invoke FUNCTION using the current keymap. */
1500 rl_invoking_keyseqs (function)
1503 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1506 /* Print all of the functions and their bindings to rl_outstream. If
1507 PRINT_READABLY is non-zero, then print the output in such a way
1508 that it can be read back in. */
1510 rl_function_dumper (print_readably)
1517 names = rl_funmap_names ();
1519 fprintf (rl_outstream, "\n");
1521 for (i = 0; name = names[i]; i++)
1526 function = rl_named_function (name);
1527 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1532 fprintf (rl_outstream, "# %s (not bound)\n", name);
1537 for (j = 0; invokers[j]; j++)
1539 fprintf (rl_outstream, "\"%s\": %s\n",
1550 fprintf (rl_outstream, "%s is not bound to any keys\n",
1556 fprintf (rl_outstream, "%s can be found on ", name);
1558 for (j = 0; invokers[j] && j < 5; j++)
1560 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1561 invokers[j + 1] ? ", " : ".\n");
1564 if (j == 5 && invokers[j])
1565 fprintf (rl_outstream, "...\n");
1567 for (j = 0; invokers[j]; j++)
1576 /* Print all of the current functions and their bindings to
1577 rl_outstream. If an explicit argument is given, then print
1578 the output in such a way that it can be read back in. */
1580 rl_dump_functions (count, key)
1584 fprintf (rl_outstream, "\r\n");
1585 rl_function_dumper (rl_explicit_arg);
1591 _rl_macro_dumper_internal (print_readably, map, prefix)
1597 char *keyname, *out;
1600 for (key = 0; key < KEYMAP_SIZE; key++)
1602 switch (map[key].type)
1605 keyname = _rl_get_keyname (key);
1607 out = (char *)map[key].function;
1609 out = _rl_untranslate_macro_value ((char *)map[key].function);
1612 fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
1616 fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
1627 prefix_len = prefix ? strlen (prefix) : 0;
1630 keyname = xmalloc (3 + prefix_len);
1632 strcpy (keyname, prefix);
1633 keyname[prefix_len] = '\\';
1634 keyname[prefix_len + 1] = 'e';
1635 keyname[prefix_len + 2] = '\0';
1639 keyname = _rl_get_keyname (key);
1642 out = xmalloc (strlen (keyname) + prefix_len + 1);
1643 strcpy (out, prefix);
1644 strcpy (out + prefix_len, keyname);
1650 _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
1658 rl_macro_dumper (print_readably)
1661 _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
1665 rl_dump_macros (count, key)
1669 fprintf (rl_outstream, "\r\n");
1670 rl_macro_dumper (rl_explicit_arg);
1676 rl_variable_dumper (print_readably)
1682 for (i = 0; boolean_varlist[i].name; i++)
1685 fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
1686 *boolean_varlist[i].value ? "on" : "off");
1688 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
1689 *boolean_varlist[i].value ? "on" : "off");
1693 switch (_rl_bell_preference)
1695 case NO_BELL: kname = "none"; break;
1696 case VISIBLE_BELL: kname = "visible"; break;
1698 default: kname = "audible"; break;
1701 fprintf (rl_outstream, "set bell-style %s\n", kname);
1703 fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
1707 fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
1709 fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : "");
1711 /* completion-query-items */
1713 fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items);
1715 fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items);
1719 fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1721 fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1724 kname = rl_get_keymap_name (_rl_keymap);
1726 kname = rl_get_keymap_name_from_edit_mode ();
1728 fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none");
1730 fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none");
1733 /* Print all of the current variables and their values to
1734 rl_outstream. If an explicit argument is given, then print
1735 the output in such a way that it can be read back in. */
1737 rl_dump_variables (count, key)
1741 fprintf (rl_outstream, "\r\n");
1742 rl_variable_dumper (rl_explicit_arg);
1747 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
1749 _rl_bind_if_unbound (keyseq, default_func)
1751 Function *default_func;
1757 func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
1758 if (!func || func == rl_do_lowercase_version)
1759 rl_set_key (keyseq, default_func, _rl_keymap);
1763 /* Return non-zero if any members of ARRAY are a substring in STRING. */
1765 substring_member_of_array (string, array)
1766 char *string, **array;
1770 if (_rl_strindex (string, *array))