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
25 #include <sys/types.h>
27 #if !defined (NO_SYS_FILE)
28 # include <sys/file.h>
29 #endif /* !NO_SYS_FILE */
32 #if defined (HAVE_UNISTD_H)
34 #endif /* HAVE_UNISTD_H */
36 #if defined (HAVE_STDLIB_H)
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
43 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
48 #include "posixstat.h"
50 /* System-specific feature definitions and include files. */
53 /* Some standard library routines. */
57 #if !defined (strchr) && !defined (__STDC__)
58 extern char *strchr (), *strrchr ();
59 #endif /* !strchr && !__STDC__ */
61 extern int _rl_horizontal_scroll_mode;
62 extern int _rl_mark_modified_lines;
63 extern int _rl_bell_preference;
64 extern int _rl_meta_flag;
65 extern int _rl_convert_meta_chars_to_ascii;
66 extern int _rl_output_meta_chars;
67 extern int _rl_complete_show_all;
68 #if defined (PAREN_MATCHING)
69 extern int rl_blink_matching_paren;
70 #endif /* PAREN_MATCHING */
71 #if defined (VISIBLE_STATS)
72 extern int rl_visible_stats;
73 #endif /* VISIBLE_STATS */
74 extern int rl_complete_with_tilde_expansion;
75 extern int rl_completion_query_items;
77 extern char *rl_vi_comment_begin;
80 extern int rl_explicit_arg;
81 extern int rl_editing_mode;
82 extern unsigned short _rl_parsing_conditionalized_out;
83 extern Keymap _rl_keymap;
85 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
87 extern char **rl_funmap_names ();
89 /* Forward declarations */
90 void rl_set_keymap_from_edit_mode ();
92 static int glean_key_from_name ();
94 #if defined (HAVE_STRCASECMP)
95 #define stricmp strcasecmp
96 #define strnicmp strncasecmp
98 static int stricmp (), strnicmp ();
101 #if defined (STATIC_MALLOC)
102 static char *xmalloc (), *xrealloc ();
104 extern char *xmalloc (), *xrealloc ();
105 #endif /* STATIC_MALLOC */
107 /* **************************************************************** */
111 /* **************************************************************** */
113 /* rl_add_defun (char *name, Function *function, int key)
114 Add NAME to the list of named functions. Make FUNCTION be the function
115 that gets called. If KEY is not -1, then bind it. */
116 rl_add_defun (name, function, key)
122 rl_bind_key (key, function);
123 rl_add_funmap_entry (name, function);
127 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
129 rl_bind_key (key, function)
136 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
138 if (_rl_keymap[ESC].type == ISKMAP)
142 escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
144 escmap[key].type = ISFUNC;
145 escmap[key].function = function;
151 _rl_keymap[key].type = ISFUNC;
152 _rl_keymap[key].function = function;
156 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
159 rl_bind_key_in_map (key, function, map)
165 Keymap oldmap = _rl_keymap;
168 result = rl_bind_key (key, function);
173 /* Make KEY do nothing in the currently selected keymap.
174 Returns non-zero in case of error. */
179 return (rl_bind_key (key, (Function *)NULL));
182 /* Make KEY do nothing in MAP.
183 Returns non-zero in case of error. */
185 rl_unbind_key_in_map (key, map)
189 return (rl_bind_key_in_map (key, (Function *)NULL, map));
192 /* Bind the key sequence represented by the string KEYSEQ to
193 FUNCTION. This makes new keymaps as necessary. The initial
194 place to do bindings is in MAP. */
195 rl_set_key (keyseq, function, map)
200 return (rl_generic_bind (ISFUNC, keyseq, function, map));
203 /* Bind the key sequence represented by the string KEYSEQ to
204 the string of characters MACRO. This makes new keymaps as
205 necessary. The initial place to do bindings is in MAP. */
206 rl_macro_bind (keyseq, macro, map)
207 char *keyseq, *macro;
213 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
215 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
220 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
224 /* Bind the key sequence represented by the string KEYSEQ to
225 the arbitrary pointer DATA. TYPE says what kind of data is
226 pointed to by DATA, right now this can be a function (ISFUNC),
227 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
228 as necessary. The initial place to do bindings is in MAP. */
229 rl_generic_bind (type, keyseq, data, map)
238 /* If no keys to bind to, exit right away. */
239 if (!keyseq || !*keyseq)
246 keys = xmalloc (1 + (2 * strlen (keyseq)));
248 /* Translate the ASCII representation of KEYSEQ into an array of
249 characters. Stuff the characters into KEYS, and the length of
250 KEYS into KEYS_LEN. */
251 if (rl_translate_keyseq (keyseq, keys, &keys_len))
257 /* Bind keys, making new keymaps as necessary. */
258 for (i = 0; i < keys_len; i++)
260 int ic = (int) ((unsigned char)keys[i]);
262 if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
265 if (map[ESC].type == ISKMAP)
266 map = FUNCTION_TO_KEYMAP (map, ESC);
269 if ((i + 1) < keys_len)
271 if (map[ic].type != ISKMAP)
273 if (map[ic].type == ISMACR)
274 free ((char *)map[ic].function);
276 map[ic].type = ISKMAP;
277 map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
279 map = FUNCTION_TO_KEYMAP (map, ic);
283 if (map[ic].type == ISMACR)
284 free ((char *)map[ic].function);
286 map[ic].function = KEYMAP_TO_FUNCTION (data);
294 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
295 an array of characters. LEN gets the final length of ARRAY. Return
296 non-zero if there was an error parsing SEQ. */
297 rl_translate_keyseq (seq, array, len)
301 register int i, c, l = 0;
303 for (i = 0; c = seq[i]; i++)
312 if (((c == 'C' || c == 'M') && seq[i + 1] == '-') ||
315 /* Handle special case of backwards define. */
316 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
320 array[l++] = CTRL (to_upper (seq[i]));
335 /* Special hack for C-?... */
339 array[l++] = CTRL (to_upper (seq[i]));
357 /* Return a pointer to the function that STRING represents.
358 If STRING doesn't have a matching function, then a NULL pointer
361 rl_named_function (string)
366 rl_initialize_funmap ();
368 for (i = 0; funmap[i]; i++)
369 if (stricmp (funmap[i]->name, string) == 0)
370 return (funmap[i]->function);
371 return ((Function *)NULL);
374 /* Return the function (or macro) definition which would be invoked via
375 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
376 used. TYPE, if non-NULL, is a pointer to an int which will receive the
377 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
378 or ISMACR (macro). */
380 rl_function_of_keyseq (keyseq, map, type)
390 for (i = 0; keyseq && keyseq[i]; i++)
394 if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
396 if (map[ESC].type != ISKMAP)
399 *type = map[ESC].type;
401 return (map[ESC].function);
405 map = FUNCTION_TO_KEYMAP (map, ESC);
410 if (map[ic].type == ISKMAP)
412 /* If this is the last key in the key sequence, return the
419 return (map[ic].function);
422 map = FUNCTION_TO_KEYMAP (map, ic);
427 *type = map[ic].type;
429 return (map[ic].function);
432 return ((Function *) NULL);
435 /* The last key bindings file read. */
436 static char *last_readline_init_file = (char *)NULL;
438 /* Re-read the current keybindings file. */
439 rl_re_read_init_file (count, ignore)
443 r = rl_read_init_file ((char *)NULL);
444 rl_set_keymap_from_edit_mode ();
448 /* Do key bindings from a file. If FILENAME is NULL it defaults
449 to the first non-null filename from this list:
450 1. the filename used for the previous call
451 2. the value of the shell variable `INPUTRC'
453 If the file existed and could be opened and read, 0 is returned,
454 otherwise errno is returned. */
456 rl_read_init_file (filename)
460 char *buffer, *openname, *line, *end;
464 /* Default the filename. */
467 filename = last_readline_init_file;
469 filename = getenv ("INPUTRC");
471 filename = DEFAULT_INPUTRC;
475 filename = DEFAULT_INPUTRC;
477 openname = tilde_expand (filename);
479 if ((stat (openname, &finfo) < 0) ||
480 (file = open (openname, O_RDONLY, 0666)) < 0)
488 if (filename != last_readline_init_file)
490 if (last_readline_init_file)
491 free (last_readline_init_file);
493 last_readline_init_file = savestring (filename);
496 /* Read the file into BUFFER. */
497 buffer = (char *)xmalloc ((int)finfo.st_size + 1);
498 i = read (file, buffer, finfo.st_size);
501 if (i != finfo.st_size)
504 /* Loop over the lines in the file. Lines that start with `#' are
505 comments; all other lines are commands for readline initialization. */
507 end = buffer + finfo.st_size;
510 /* Find the end of this line. */
511 for (i = 0; line + i != end && line[i] != '\n'; i++);
513 /* Mark end of line. */
516 /* Skip leading whitespace. */
517 while (*line && whitespace (*line))
523 /* If the line is not a comment, then parse it. */
524 if (*line && *line != '#')
525 rl_parse_and_bind (line);
527 /* Move to the next line. */
534 /* **************************************************************** */
536 /* Parser Directives */
538 /* **************************************************************** */
542 /* Calling programs set this to have their argv[0]. */
543 char *rl_readline_name = "other";
545 /* Stack of previous values of parsing_conditionalized_out. */
546 static unsigned char *if_stack = (unsigned char *)NULL;
547 static int if_stack_depth = 0;
548 static int if_stack_size = 0;
550 /* Push _rl_parsing_conditionalized_out, and set parser state based
558 /* Push parser state. */
559 if (if_stack_depth + 1 >= if_stack_size)
562 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
564 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
566 if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
568 /* If parsing is turned off, then nothing can turn it back on except
569 for finding the matching endif. In that case, return right now. */
570 if (_rl_parsing_conditionalized_out)
573 /* Isolate first argument. */
574 for (i = 0; args[i] && !whitespace (args[i]); i++);
579 /* Handle "if term=foo" and "if mode=emacs" constructs. If this
580 isn't term=foo, or mode=emacs, then check to see if the first
581 word in ARGS is the same as the value stored in rl_readline_name. */
582 if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
586 /* Terminals like "aaa-60" are equivalent to "aaa". */
587 tname = savestring (rl_terminal_name);
588 tem = strchr (tname, '-');
592 /* Test the `long' and `short' forms of the terminal name so that
593 if someone has a `sun-cmd' and does not want to have bindings
594 that will be executed if the terminal is a `sun', they can put
595 `$if term=sun-cmd' into their .inputrc. */
596 if ((stricmp (args + 5, tname) == 0) ||
597 (stricmp (args + 5, rl_terminal_name) == 0))
598 _rl_parsing_conditionalized_out = 0;
600 _rl_parsing_conditionalized_out = 1;
604 #if defined (VI_MODE)
605 else if (strnicmp (args, "mode=", 5) == 0)
609 if (stricmp (args + 5, "emacs") == 0)
611 else if (stricmp (args + 5, "vi") == 0)
616 if (mode == rl_editing_mode)
617 _rl_parsing_conditionalized_out = 0;
619 _rl_parsing_conditionalized_out = 1;
622 /* Check to see if the first word in ARGS is the same as the
623 value stored in rl_readline_name. */
624 else if (stricmp (args, rl_readline_name) == 0)
625 _rl_parsing_conditionalized_out = 0;
627 _rl_parsing_conditionalized_out = 1;
631 /* Invert the current parser state if there is anything on the stack. */
644 /* Check the previous (n - 1) levels of the stack to make sure that
645 we haven't previously turned off parsing. */
646 for (i = 0; i < if_stack_depth - 1; i++)
647 if (if_stack[i] == 1)
650 /* Invert the state of parsing if at top level. */
651 _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
655 /* Terminate a conditional, popping the value of
656 _rl_parsing_conditionalized_out from the stack. */
662 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
665 /* *** What, no error message? *** */
670 /* Associate textual names with actual functions. */
674 } parser_directives [] = {
676 { "endif", parser_endif },
677 { "else", parser_else },
678 { (char *)0x0, (Function *)0x0 }
681 /* Handle a parser directive. STATEMENT is the line of the directive
682 without any leading `$'. */
684 handle_parser_directive (statement)
688 char *directive, *args;
690 /* Isolate the actual directive. */
692 /* Skip whitespace. */
693 for (i = 0; whitespace (statement[i]); i++);
695 directive = &statement[i];
697 for (; statement[i] && !whitespace (statement[i]); i++);
700 statement[i++] = '\0';
702 for (; statement[i] && whitespace (statement[i]); i++);
704 args = &statement[i];
706 /* Lookup the command, and act on it. */
707 for (i = 0; parser_directives[i].name; i++)
708 if (stricmp (directive, parser_directives[i].name) == 0)
710 (*parser_directives[i].function) (args);
714 /* *** Should an error message be output? */
718 static int substring_member_of_array ();
720 /* Read the binding command from STRING and perform it.
721 A key binding command looks like: Keyname: function-name\0,
722 a variable binding command looks like: set variable value.
723 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
724 rl_parse_and_bind (string)
727 char *funname, *kname;
729 int key, equivalency;
731 while (string && whitespace (*string))
734 if (!string || !*string || *string == '#')
737 /* If this is a parser directive, act on it. */
740 handle_parser_directive (&string[1]);
744 /* If we aren't supposed to be parsing right now, then we're done. */
745 if (_rl_parsing_conditionalized_out)
749 /* If this keyname is a complex key expression surrounded by quotes,
750 advance to after the matching close quote. This code allows the
751 backslash to quote characters in the key expression. */
756 for (i = 1; c = string[i]; i++)
775 /* Advance to the colon (:) or whitespace which separates the two objects. */
776 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
778 equivalency = (c == ':' && string[i + 1] == '=');
780 /* Mark the end of the command (or keyname). */
784 /* If doing assignment, skip the '=' sign as well. */
788 /* If this is a command to set a variable, then do that. */
789 if (stricmp (string, "set") == 0)
791 char *var = string + i;
794 /* Make VAR point to start of variable name. */
795 while (*var && whitespace (*var)) var++;
797 /* Make value point to start of value string. */
799 while (*value && !whitespace (*value)) value++;
802 while (*value && whitespace (*value)) value++;
804 rl_variable_bind (var, value);
808 /* Skip any whitespace between keyname and funname. */
809 for (; string[i] && whitespace (string[i]); i++);
810 funname = &string[i];
812 /* Now isolate funname.
813 For straight function names just look for whitespace, since
814 that will signify the end of the string. But this could be a
815 macro definition. In that case, the string is quoted, so skip
816 to the matching delimiter. We allow the backslash to quote the
817 delimiter characters in the macro body. */
818 /* This code exists to allow whitespace in macro expansions, which
819 would otherwise be gobbled up by the next `for' loop.*/
820 /* XXX - it may be desirable to allow backslash quoting only if " is
821 the quoted string delimiter, like the shell. */
822 if (*funname == '\'' || *funname == '"')
824 int delimiter = string[i++];
827 for (; c = string[i]; i++)
848 /* Advance to the end of the string. */
849 for (; string[i] && !whitespace (string[i]); i++);
851 /* No extra whitespace at the end of the string. */
854 /* Handle equivalency bindings here. Make the left-hand side be exactly
855 whatever the right-hand evaluates to, including keymaps. */
861 /* If this is a new-style key-binding, then do the binding with
862 rl_set_key (). Otherwise, let the older code deal with it. */
865 char *seq = xmalloc (1 + strlen (string));
866 register int j, k = 0;
869 for (j = 1; string[j]; j++)
871 /* Allow backslash to quote characters, but leave them in place.
872 This allows a string to end with a backslash quoting another
873 backslash, or with a backslash quoting a double quote. The
874 backslashes are left in place for rl_translate_keyseq (). */
875 if (passc || (string[j] == '\\'))
877 seq[k++] = string[j];
882 if (string[j] == '"')
885 seq[k++] = string[j];
890 if (*funname == '\'' || *funname == '"')
892 j = strlen (funname);
894 /* Remove the delimiting quotes from each end of FUNNAME. */
895 if (j && funname[j - 1] == *funname)
896 funname[j - 1] = '\0';
898 rl_macro_bind (seq, &funname[1], _rl_keymap);
901 rl_set_key (seq, rl_named_function (funname), _rl_keymap);
907 /* Get the actual character we want to deal with. */
908 kname = strrchr (string, '-');
914 key = glean_key_from_name (kname);
916 /* Add in control and meta bits. */
917 if (substring_member_of_array (string, possible_control_prefixes))
918 key = CTRL (to_upper (key));
920 if (substring_member_of_array (string, possible_meta_prefixes))
923 /* Temporary. Handle old-style keyname with macro-binding. */
924 if (*funname == '\'' || *funname == '"')
927 int fl = strlen (funname);
929 seq[0] = key; seq[1] = '\0';
930 if (fl && funname[fl - 1] == *funname)
931 funname[fl - 1] = '\0';
933 rl_macro_bind (seq, &funname[1], _rl_keymap);
935 #if defined (PREFIX_META_HACK)
936 /* Ugly, but working hack to keep prefix-meta around. */
937 else if (stricmp (funname, "prefix-meta") == 0)
943 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
945 #endif /* PREFIX_META_HACK */
947 rl_bind_key (key, rl_named_function (funname));
951 /* Simple structure for boolean readline variables (i.e., those that can
952 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
958 } boolean_varlist [] = {
959 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode },
960 { "mark-modified-lines", &_rl_mark_modified_lines },
961 { "meta-flag", &_rl_meta_flag },
962 #if defined (PAREN_MATCHING)
963 { "blink-matching-paren", &rl_blink_matching_paren },
965 { "convert-meta", &_rl_convert_meta_chars_to_ascii },
966 { "show-all-if-ambiguous", &_rl_complete_show_all },
967 { "output-meta", &_rl_output_meta_chars },
968 #if defined (VISIBLE_STATS)
969 { "visible-stats", &rl_visible_stats },
970 #endif /* VISIBLE_STATS */
971 { "expand-tilde", &rl_complete_with_tilde_expansion },
972 { (char *)NULL, (int *)NULL }
975 rl_variable_bind (name, value)
980 /* Check for simple variables first. */
981 for (i = 0; boolean_varlist[i].name; i++)
983 if (stricmp (name, boolean_varlist[i].name) == 0)
985 /* A variable is TRUE if the "value" is "on", "1" or "". */
987 (stricmp (value, "On") == 0) ||
988 (value[0] == '1' && value[1] == '\0'))
989 *boolean_varlist[i].value = 1;
991 *boolean_varlist[i].value = 0;
996 /* Not a boolean variable, so check for specials. */
998 /* Editing mode change? */
999 if (stricmp (name, "editing-mode") == 0)
1001 if (strnicmp (value, "vi", 2) == 0)
1003 #if defined (VI_MODE)
1004 _rl_keymap = vi_insertion_keymap;
1005 rl_editing_mode = vi_mode;
1006 #endif /* VI_MODE */
1008 else if (strnicmp (value, "emacs", 5) == 0)
1010 _rl_keymap = emacs_standard_keymap;
1011 rl_editing_mode = emacs_mode;
1015 /* Comment string change? */
1016 else if (stricmp (name, "comment-begin") == 0)
1018 #if defined (VI_MODE)
1021 if (rl_vi_comment_begin)
1022 free (rl_vi_comment_begin);
1024 rl_vi_comment_begin = savestring (value);
1026 #endif /* VI_MODE */
1028 else if (stricmp (name, "completion-query-items") == 0)
1033 nval = atoi (value);
1037 rl_completion_query_items = nval;
1039 else if (stricmp (name, "keymap") == 0)
1042 kmap = rl_get_keymap_by_name (value);
1044 rl_set_keymap (kmap);
1046 else if (stricmp (name, "bell-style") == 0)
1049 _rl_bell_preference = AUDIBLE_BELL;
1052 if (stricmp (value, "none") == 0 || stricmp (value, "off") == 0)
1053 _rl_bell_preference = NO_BELL;
1054 else if (stricmp (value, "audible") == 0 || stricmp (value, "on") == 0)
1055 _rl_bell_preference = AUDIBLE_BELL;
1056 else if (stricmp (value, "visible") == 0)
1057 _rl_bell_preference = VISIBLE_BELL;
1060 else if (stricmp (name, "prefer-visible-bell") == 0)
1062 /* Backwards compatibility. */
1063 if (*value && (stricmp (value, "on") == 0 ||
1064 (*value == '1' && !value[1])))
1065 _rl_bell_preference = VISIBLE_BELL;
1067 _rl_bell_preference = AUDIBLE_BELL;
1073 /* Return the character which matches NAME.
1074 For example, `Space' returns ' '. */
1081 static assoc_list name_key_alist[] = {
1084 { "Escape", '\033' },
1086 { "Newline", '\n' },
1097 glean_key_from_name (name)
1102 for (i = 0; name_key_alist[i].name; i++)
1103 if (stricmp (name, name_key_alist[i].name) == 0)
1104 return (name_key_alist[i].value);
1106 return (*(unsigned char *)name); /* XXX was return (*name) */
1109 /* Auxiliary functions to manage keymaps. */
1113 } keymap_names[] = {
1114 { "emacs", emacs_standard_keymap },
1115 { "emacs-standard", emacs_standard_keymap },
1116 { "emacs-meta", emacs_meta_keymap },
1117 { "emacs-ctlx", emacs_ctlx_keymap },
1118 #if defined (VI_MODE)
1119 { "vi", vi_movement_keymap },
1120 { "vi-move", vi_movement_keymap },
1121 { "vi-command", vi_movement_keymap },
1122 { "vi-insert", vi_insertion_keymap },
1123 #endif /* VI_MODE */
1124 { (char *)0x0, (Keymap)0x0 }
1128 rl_get_keymap_by_name (name)
1133 for (i = 0; keymap_names[i].name; i++)
1134 if (strcmp (name, keymap_names[i].name) == 0)
1135 return (keymap_names[i].map);
1136 return ((Keymap) NULL);
1150 return (_rl_keymap);
1154 rl_set_keymap_from_edit_mode ()
1156 if (rl_editing_mode == emacs_mode)
1157 _rl_keymap = emacs_standard_keymap;
1158 #if defined (VI_MODE)
1159 else if (rl_editing_mode == vi_mode)
1160 _rl_keymap = vi_insertion_keymap;
1161 #endif /* VI_MODE */
1164 /* **************************************************************** */
1166 /* Key Binding and Function Information */
1168 /* **************************************************************** */
1170 /* Each of the following functions produces information about the
1171 state of keybindings and functions known to Readline. The info
1172 is always printed to rl_outstream, and in such a way that it can
1173 be read back in (i.e., passed to rl_parse_and_bind (). */
1175 /* Print the names of functions known to Readline. */
1177 rl_list_funmap_names (count, ignore)
1181 char **funmap_names;
1183 funmap_names = rl_funmap_names ();
1188 for (i = 0; funmap_names[i]; i++)
1189 fprintf (rl_outstream, "%s\n", funmap_names[i]);
1191 free (funmap_names);
1194 /* Return a NULL terminated array of strings which represent the key
1195 sequences that are used to invoke FUNCTION in MAP. */
1197 rl_invoking_keyseqs_in_map (function, map)
1203 int result_index, result_size;
1205 result = (char **)NULL;
1206 result_index = result_size = 0;
1208 for (key = 0; key < 128; key++)
1210 switch (map[key].type)
1213 /* Macros match, if, and only if, the pointers are identical.
1214 Thus, they are treated exactly like functions in here. */
1216 /* If the function in the keymap is the one we are looking for,
1217 then add the current KEY to the list of invoking keys. */
1218 if (map[key].function == function)
1220 char *keyname = (char *)xmalloc (5);
1222 if (CTRL_CHAR (key))
1223 sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
1224 else if (key == RUBOUT)
1225 sprintf (keyname, "\\C-?");
1226 else if (key == '\\' || key == '"')
1229 keyname[1] = (char) key;
1234 keyname[0] = (char) key;
1238 if (result_index + 2 > result_size)
1239 result = (char **) xrealloc
1240 (result, (result_size += 10) * sizeof (char *));
1242 result[result_index++] = keyname;
1243 result[result_index] = (char *)NULL;
1249 char **seqs = (char **)NULL;
1251 /* Find the list of keyseqs in this map which have FUNCTION as
1252 their target. Add the key sequences found to RESULT. */
1253 if (map[key].function)
1255 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1261 for (i = 0; seqs[i]; i++)
1263 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1266 sprintf (keyname, "\\e");
1267 else if (CTRL_CHAR (key))
1268 sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
1269 else if (key == RUBOUT)
1270 sprintf (keyname, "\\C-?");
1271 else if (key == '\\' || key == '"')
1274 keyname[1] = (char) key;
1279 keyname[0] = (char) key;
1283 strcat (keyname, seqs[i]);
1286 if (result_index + 2 > result_size)
1287 result = (char **) xrealloc
1288 (result, (result_size += 10) * sizeof (char *));
1290 result[result_index++] = keyname;
1291 result[result_index] = (char *)NULL;
1303 /* Return a NULL terminated array of strings which represent the key
1304 sequences that can be used to invoke FUNCTION using the current keymap. */
1306 rl_invoking_keyseqs (function)
1309 return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1312 /* Print all of the current functions and their bindings to
1313 rl_outstream. If an explicit argument is given, then print
1314 the output in such a way that it can be read back in. */
1316 rl_dump_functions (count, key)
1319 rl_function_dumper (rl_explicit_arg);
1324 /* Print all of the functions and their bindings to rl_outstream. If
1325 PRINT_READABLY is non-zero, then print the output in such a way
1326 that it can be read back in. */
1328 rl_function_dumper (print_readably)
1335 names = rl_funmap_names ();
1337 fprintf (rl_outstream, "\n");
1339 for (i = 0; name = names[i]; i++)
1344 function = rl_named_function (name);
1345 invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1350 fprintf (rl_outstream, "# %s (not bound)\n", name);
1355 for (j = 0; invokers[j]; j++)
1357 fprintf (rl_outstream, "\"%s\": %s\n",
1368 fprintf (rl_outstream, "%s is not bound to any keys\n",
1374 fprintf (rl_outstream, "%s can be found on ", name);
1376 for (j = 0; invokers[j] && j < 5; j++)
1378 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1379 invokers[j + 1] ? ", " : ".\n");
1382 if (j == 5 && invokers[j])
1383 fprintf (rl_outstream, "...\n");
1385 for (j = 0; invokers[j]; j++)
1394 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
1396 _rl_bind_if_unbound (keyseq, default_func)
1398 Function *default_func;
1404 func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
1405 if (!func || func == rl_do_lowercase_version)
1406 rl_set_key (keyseq, default_func, _rl_keymap);
1410 /* **************************************************************** */
1412 /* String Utility Functions */
1414 /* **************************************************************** */
1416 static char *strindex ();
1418 /* Return non-zero if any members of ARRAY are a substring in STRING. */
1420 substring_member_of_array (string, array)
1421 char *string, **array;
1425 if (strindex (string, *array))
1432 #if !defined (HAVE_STRCASECMP)
1433 /* Whoops, Unix doesn't have strnicmp. */
1435 /* Compare at most COUNT characters from string1 to string2. Case
1438 strnicmp (string1, string2, count)
1439 char *string1, *string2;
1442 register char ch1, ch2;
1448 if (to_upper(ch1) == to_upper(ch2))
1456 /* strcmp (), but caseless. */
1458 stricmp (string1, string2)
1459 char *string1, *string2;
1461 register char ch1, ch2;
1463 while (*string1 && *string2)
1467 if (to_upper(ch1) != to_upper(ch2))
1470 return (*string1 - *string2);
1472 #endif /* !HAVE_STRCASECMP */
1474 /* Determine if s2 occurs in s1. If so, return a pointer to the
1475 match in s1. The compare is case insensitive. */
1478 register char *s1, *s2;
1480 register int i, l = strlen (s2);
1481 register int len = strlen (s1);
1483 for (i = 0; (len - i) >= l; i++)
1484 if (strnicmp (s1 + i, s2, l) == 0)
1486 return ((char *)NULL);