1 /* bind.c -- key binding and startup file support for the readline library. */
3 /* Copyright (C) 1987-2016 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
22 #define READLINE_LIBRARY
24 #if defined (__TANDEM)
28 #if defined (HAVE_CONFIG_H)
33 #include <sys/types.h>
35 #if defined (HAVE_SYS_FILE_H)
36 # include <sys/file.h>
37 #endif /* HAVE_SYS_FILE_H */
39 #if defined (HAVE_UNISTD_H)
41 #endif /* HAVE_UNISTD_H */
43 #if defined (HAVE_STDLIB_H)
46 # include "ansi_stdlib.h"
47 #endif /* HAVE_STDLIB_H */
55 #include "posixstat.h"
57 /* System-specific feature definitions and include files. */
60 /* Some standard library routines. */
64 #include "rlprivate.h"
68 #if !defined (strchr) && !defined (__STDC__)
69 extern char *strchr (), *strrchr ();
70 #endif /* !strchr && !__STDC__ */
72 /* Variables exported by this file. */
73 Keymap rl_binding_keymap
;
75 static int _rl_skip_to_delim
PARAMS((char *, int, int));
77 #if defined (USE_VARARGS) && defined (PREFER_STDARG)
78 static void _rl_init_file_error (const char *, ...) __attribute__((__format__ (printf
, 1, 2)));
80 static void _rl_init_file_error ();
83 static char *_rl_read_file
PARAMS((char *, size_t *));
84 static int _rl_read_init_file
PARAMS((const char *, int));
85 static int glean_key_from_name
PARAMS((char *));
87 static int find_boolean_var
PARAMS((const char *));
88 static int find_string_var
PARAMS((const char *));
90 static char *_rl_get_string_variable_value
PARAMS((const char *));
91 static int substring_member_of_array
PARAMS((const char *, const char * const *));
93 static int currently_reading_init_file
;
95 /* used only in this file */
96 static int _rl_prefer_visible_bell
= 1;
98 /* **************************************************************** */
102 /* **************************************************************** */
104 /* rl_add_defun (char *name, rl_command_func_t *function, int key)
105 Add NAME to the list of named functions. Make FUNCTION be the function
106 that gets called. If KEY is not -1, then bind it. */
108 rl_add_defun (name
, function
, key
)
110 rl_command_func_t
*function
;
114 rl_bind_key (key
, function
);
115 rl_add_funmap_entry (name
, function
);
119 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
121 rl_bind_key (key
, function
)
123 rl_command_func_t
*function
;
131 if (META_CHAR (key
) && _rl_convert_meta_chars_to_ascii
)
133 if (_rl_keymap
[ESC
].type
== ISKMAP
)
137 escmap
= FUNCTION_TO_KEYMAP (_rl_keymap
, ESC
);
139 escmap
[key
].type
= ISFUNC
;
140 escmap
[key
].function
= function
;
146 /* If it's bound to a function or macro, just overwrite. Otherwise we have
147 to treat it as a key sequence so rl_generic_bind handles shadow keymaps
148 for us. If we are binding '\' make sure to escape it so it makes it
149 through the call to rl_translate_keyseq. */
150 if (_rl_keymap
[key
].type
!= ISKMAP
)
152 _rl_keymap
[key
].type
= ISFUNC
;
153 _rl_keymap
[key
].function
= function
;
162 rl_bind_keyseq (keyseq
, function
);
164 rl_binding_keymap
= _rl_keymap
;
168 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
171 rl_bind_key_in_map (key
, function
, map
)
173 rl_command_func_t
*function
;
181 result
= rl_bind_key (key
, function
);
186 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right
187 now, this is always used to attempt to bind the arrow keys, hence the
188 check for rl_vi_movement_mode. */
190 rl_bind_key_if_unbound_in_map (key
, default_func
, kmap
)
192 rl_command_func_t
*default_func
;
197 keyseq
[0] = (unsigned char)key
;
199 return (rl_bind_keyseq_if_unbound_in_map (keyseq
, default_func
, kmap
));
203 rl_bind_key_if_unbound (key
, default_func
)
205 rl_command_func_t
*default_func
;
209 keyseq
[0] = (unsigned char)key
;
211 return (rl_bind_keyseq_if_unbound_in_map (keyseq
, default_func
, _rl_keymap
));
214 /* Make KEY do nothing in the currently selected keymap.
215 Returns non-zero in case of error. */
220 return (rl_bind_key (key
, (rl_command_func_t
*)NULL
));
223 /* Make KEY do nothing in MAP.
224 Returns non-zero in case of error. */
226 rl_unbind_key_in_map (key
, map
)
230 return (rl_bind_key_in_map (key
, (rl_command_func_t
*)NULL
, map
));
233 /* Unbind all keys bound to FUNCTION in MAP. */
235 rl_unbind_function_in_map (func
, map
)
236 rl_command_func_t
*func
;
239 register int i
, rval
;
241 for (i
= rval
= 0; i
< KEYMAP_SIZE
; i
++)
243 if (map
[i
].type
== ISFUNC
&& map
[i
].function
== func
)
245 map
[i
].function
= (rl_command_func_t
*)NULL
;
253 rl_unbind_command_in_map (command
, map
)
257 rl_command_func_t
*func
;
259 func
= rl_named_function (command
);
262 return (rl_unbind_function_in_map (func
, map
));
265 /* Bind the key sequence represented by the string KEYSEQ to
266 FUNCTION, starting in the current keymap. This makes new
267 keymaps as necessary. */
269 rl_bind_keyseq (keyseq
, function
)
271 rl_command_func_t
*function
;
273 return (rl_generic_bind (ISFUNC
, keyseq
, (char *)function
, _rl_keymap
));
276 /* Bind the key sequence represented by the string KEYSEQ to
277 FUNCTION. This makes new keymaps as necessary. The initial
278 place to do bindings is in MAP. */
280 rl_bind_keyseq_in_map (keyseq
, function
, map
)
282 rl_command_func_t
*function
;
285 return (rl_generic_bind (ISFUNC
, keyseq
, (char *)function
, map
));
288 /* Backwards compatibility; equivalent to rl_bind_keyseq_in_map() */
290 rl_set_key (keyseq
, function
, map
)
292 rl_command_func_t
*function
;
295 return (rl_generic_bind (ISFUNC
, keyseq
, (char *)function
, map
));
298 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right
299 now, this is always used to attempt to bind the arrow keys, hence the
300 check for rl_vi_movement_mode. */
302 rl_bind_keyseq_if_unbound_in_map (keyseq
, default_func
, kmap
)
304 rl_command_func_t
*default_func
;
307 rl_command_func_t
*func
;
311 func
= rl_function_of_keyseq (keyseq
, kmap
, (int *)NULL
);
312 #if defined (VI_MODE)
313 if (!func
|| func
== rl_do_lowercase_version
|| func
== rl_vi_movement_mode
)
315 if (!func
|| func
== rl_do_lowercase_version
)
317 return (rl_bind_keyseq_in_map (keyseq
, default_func
, kmap
));
325 rl_bind_keyseq_if_unbound (keyseq
, default_func
)
327 rl_command_func_t
*default_func
;
329 return (rl_bind_keyseq_if_unbound_in_map (keyseq
, default_func
, _rl_keymap
));
332 /* Bind the key sequence represented by the string KEYSEQ to
333 the string of characters MACRO. This makes new keymaps as
334 necessary. The initial place to do bindings is in MAP. */
336 rl_macro_bind (keyseq
, macro
, map
)
337 const char *keyseq
, *macro
;
343 macro_keys
= (char *)xmalloc ((2 * strlen (macro
)) + 1);
345 if (rl_translate_keyseq (macro
, macro_keys
, ¯o_keys_len
))
350 rl_generic_bind (ISMACR
, keyseq
, macro_keys
, map
);
354 /* Bind the key sequence represented by the string KEYSEQ to
355 the arbitrary pointer DATA. TYPE says what kind of data is
356 pointed to by DATA, right now this can be a function (ISFUNC),
357 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
358 as necessary. The initial place to do bindings is in MAP. */
360 rl_generic_bind (type
, keyseq
, data
, map
)
373 /* If no keys to bind to, exit right away. */
374 if (keyseq
== 0 || *keyseq
== 0)
381 keys
= (char *)xmalloc (1 + (2 * strlen (keyseq
)));
383 /* Translate the ASCII representation of KEYSEQ into an array of
384 characters. Stuff the characters into KEYS, and the length of
385 KEYS into KEYS_LEN. */
386 if (rl_translate_keyseq (keyseq
, keys
, &keys_len
))
392 /* Bind keys, making new keymaps as necessary. */
393 for (i
= 0; i
< keys_len
; i
++)
395 unsigned char uc
= keys
[i
];
399 if (ic
< 0 || ic
>= KEYMAP_SIZE
)
405 if (META_CHAR (ic
) && _rl_convert_meta_chars_to_ascii
)
408 if (map
[ESC
].type
== ISKMAP
)
409 map
= FUNCTION_TO_KEYMAP (map
, ESC
);
412 if ((i
+ 1) < keys_len
)
414 if (map
[ic
].type
!= ISKMAP
)
416 /* We allow subsequences of keys. If a keymap is being
417 created that will `shadow' an existing function or macro
418 key binding, we save that keybinding into the ANYOTHERKEY
419 index in the new map. The dispatch code will look there
420 to find the function to execute if the subsequence is not
421 matched. ANYOTHERKEY was chosen to be greater than
425 map
[ic
].type
= ISKMAP
;
426 map
[ic
].function
= KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
428 map
= FUNCTION_TO_KEYMAP (map
, ic
);
429 /* The dispatch code will return this function if no matching
430 key sequence is found in the keymap. This (with a little
431 help from the dispatch code in readline.c) allows `a' to be
432 mapped to something, `abc' to be mapped to something else,
433 and the function bound to `a' to be executed when the user
434 types `abx', leaving `bx' in the input queue. */
435 if (k
.function
&& ((k
.type
== ISFUNC
&& k
.function
!= rl_do_lowercase_version
) || k
.type
== ISMACR
))
437 map
[ANYOTHERKEY
] = k
;
443 if (map
[ic
].type
== ISMACR
)
444 xfree ((char *)map
[ic
].function
);
445 else if (map
[ic
].type
== ISKMAP
)
447 map
= FUNCTION_TO_KEYMAP (map
, ic
);
449 /* If we're trying to override a keymap with a null function
450 (e.g., trying to unbind it), we can't use a null pointer
451 here because that's indistinguishable from having not been
452 overridden. We use a special bindable function that does
454 if (type
== ISFUNC
&& data
== 0)
455 data
= (char *)_rl_null_function
;
458 map
[ic
].function
= KEYMAP_TO_FUNCTION (data
);
462 rl_binding_keymap
= map
;
468 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
469 an array of characters. LEN gets the final length of ARRAY. Return
470 non-zero if there was an error parsing SEQ. */
472 rl_translate_keyseq (seq
, array
, len
)
477 register int i
, c
, l
, temp
;
479 for (i
= l
= 0; c
= seq
[i
]; i
++)
488 /* Handle \C- and \M- prefixes. */
489 if ((c
== 'C' || c
== 'M') && seq
[i
+ 1] == '-')
491 /* Handle special case of backwards define. */
492 if (strncmp (&seq
[i
], "C-\\M-", 5) == 0)
494 array
[l
++] = ESC
; /* ESC is meta-prefix */
496 array
[l
++] = CTRL (_rl_to_upper (seq
[i
]));
502 i
++; /* seq[i] == '-' */
503 /* XXX - obey convert-meta setting */
504 if (_rl_convert_meta_chars_to_ascii
&& _rl_keymap
[ESC
].type
== ISKMAP
)
505 array
[l
++] = ESC
; /* ESC is meta-prefix */
506 else if (seq
[i
+1] == '\\' && seq
[i
+2] == 'C' && seq
[i
+3] == '-')
509 temp
= (seq
[i
] == '?') ? RUBOUT
: CTRL (_rl_to_upper (seq
[i
]));
510 array
[l
++] = META (temp
);
514 /* This doesn't yet handle things like \M-\a, which may
515 or may not have any reasonable meaning. You're
516 probably better off using straight octal or hex. */
518 array
[l
++] = META (seq
[i
]);
524 /* Special hack for C-?... */
525 array
[l
++] = (seq
[i
] == '?') ? RUBOUT
: CTRL (_rl_to_upper (seq
[i
]));
530 /* Translate other backslash-escaped characters. These are the
531 same escape sequences that bash's `echo' and `printf' builtins
532 handle, with the addition of \d -> RUBOUT. A backslash
533 preceding a character that is not special is stripped. */
543 array
[l
++] = RUBOUT
; /* readline-specific */
552 array
[l
++] = NEWLINE
;
566 case '0': case '1': case '2': case '3':
567 case '4': case '5': case '6': case '7':
569 for (temp
= 2, c
-= '0'; ISOCTAL ((unsigned char)seq
[i
]) && temp
--; i
++)
570 c
= (c
* 8) + OCTVALUE (seq
[i
]);
571 i
--; /* auto-increment in for loop */
572 array
[l
++] = c
& largest_char
;
576 for (temp
= 2, c
= 0; ISXDIGIT ((unsigned char)seq
[i
]) && temp
--; i
++)
577 c
= (c
* 16) + HEXVALUE (seq
[i
]);
580 i
--; /* auto-increment in for loop */
581 array
[l
++] = c
& largest_char
;
583 default: /* backslashes before non-special chars just add the char */
585 break; /* the backslash is stripped */
610 case 0x0b: return (1);
621 case '\007': return ('a');
622 case '\b': return ('b');
623 case '\f': return ('f');
624 case '\n': return ('n');
625 case '\r': return ('r');
626 case TAB
: return ('t');
627 case 0x0b: return ('v');
633 rl_untranslate_keyseq (seq
)
636 static char kseq
[16];
653 else if (CTRL_CHAR (c
))
658 c
= _rl_to_lower (UNCTRL (c
));
660 else if (c
== RUBOUT
)
673 else if (c
== '\\' || c
== '"')
678 kseq
[i
++] = (unsigned char) c
;
684 _rl_untranslate_macro_value (seq
, use_escapes
)
691 r
= ret
= (char *)xmalloc (7 * strlen (seq
) + 1);
692 for (s
= seq
; *s
; s
++)
707 else if (CTRL_CHAR (c
))
710 if (use_escapes
&& _rl_isescape (c
))
716 c
= _rl_to_lower (UNCTRL (c
));
719 else if (c
== RUBOUT
)
732 else if (c
== '\\' || c
== '"')
735 *r
++ = (unsigned char)c
;
741 /* Return a pointer to the function that STRING represents.
742 If STRING doesn't have a matching function, then a NULL pointer
745 rl_named_function (string
)
750 rl_initialize_funmap ();
752 for (i
= 0; funmap
[i
]; i
++)
753 if (_rl_stricmp (funmap
[i
]->name
, string
) == 0)
754 return (funmap
[i
]->function
);
755 return ((rl_command_func_t
*)NULL
);
758 /* Return the function (or macro) definition which would be invoked via
759 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
760 used. TYPE, if non-NULL, is a pointer to an int which will receive the
761 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
762 or ISMACR (macro). */
764 rl_function_of_keyseq (keyseq
, map
, type
)
774 for (i
= 0; keyseq
&& keyseq
[i
]; i
++)
776 unsigned char ic
= keyseq
[i
];
778 if (META_CHAR (ic
) && _rl_convert_meta_chars_to_ascii
)
780 if (map
[ESC
].type
== ISKMAP
)
782 map
= FUNCTION_TO_KEYMAP (map
, ESC
);
785 /* XXX - should we just return NULL here, since this obviously
790 *type
= map
[ESC
].type
;
792 return (map
[ESC
].function
);
796 if (map
[ic
].type
== ISKMAP
)
798 /* If this is the last key in the key sequence, return the
800 if (keyseq
[i
+ 1] == '\0')
805 return (map
[ic
].function
);
808 map
= FUNCTION_TO_KEYMAP (map
, ic
);
810 /* If we're not at the end of the key sequence, and the current key
811 is bound to something other than a keymap, then the entire key
812 sequence is not bound. */
813 else if (map
[ic
].type
!= ISKMAP
&& keyseq
[i
+1])
814 return ((rl_command_func_t
*)NULL
);
815 else /* map[ic].type != ISKMAP && keyseq[i+1] == 0 */
818 *type
= map
[ic
].type
;
820 return (map
[ic
].function
);
823 return ((rl_command_func_t
*) NULL
);
826 /* The last key bindings file read. */
827 static char *last_readline_init_file
= (char *)NULL
;
829 /* The file we're currently reading key bindings from. */
830 static const char *current_readline_init_file
;
831 static int current_readline_init_include_level
;
832 static int current_readline_init_lineno
;
834 /* Read FILENAME into a locally-allocated buffer and return the buffer.
835 The size of the buffer is returned in *SIZEP. Returns NULL if any
836 errors were encountered. */
838 _rl_read_file (filename
, sizep
)
847 if ((stat (filename
, &finfo
) < 0) || (file
= open (filename
, O_RDONLY
, 0666)) < 0)
848 return ((char *)NULL
);
850 file_size
= (size_t)finfo
.st_size
;
852 /* check for overflow on very large files */
853 if (file_size
!= finfo
.st_size
|| file_size
+ 1 < file_size
)
860 return ((char *)NULL
);
863 /* Read the file into BUFFER. */
864 buffer
= (char *)xmalloc (file_size
+ 1);
865 i
= read (file
, buffer
, file_size
);
871 return ((char *)NULL
);
883 /* Re-read the current keybindings file. */
885 rl_re_read_init_file (count
, ignore
)
889 r
= rl_read_init_file ((const char *)NULL
);
890 rl_set_keymap_from_edit_mode ();
894 /* Do key bindings from a file. If FILENAME is NULL it defaults
895 to the first non-null filename from this list:
896 1. the filename used for the previous call
897 2. the value of the shell variable `INPUTRC'
900 If the file existed and could be opened and read, 0 is returned,
901 otherwise errno is returned. */
903 rl_read_init_file (filename
)
904 const char *filename
;
906 /* Default the filename. */
908 filename
= last_readline_init_file
;
910 filename
= sh_get_env_value ("INPUTRC");
911 if (filename
== 0 || *filename
== 0)
913 filename
= DEFAULT_INPUTRC
;
914 /* Try to read DEFAULT_INPUTRC; fall back to SYS_INPUTRC on failure */
915 if (_rl_read_init_file (filename
, 0) == 0)
917 filename
= SYS_INPUTRC
;
920 #if defined (__MSDOS__)
921 if (_rl_read_init_file (filename
, 0) == 0)
923 filename
= "~/_inputrc";
925 return (_rl_read_init_file (filename
, 0));
929 _rl_read_init_file (filename
, include_level
)
930 const char *filename
;
934 char *buffer
, *openname
, *line
, *end
;
937 current_readline_init_file
= filename
;
938 current_readline_init_include_level
= include_level
;
940 openname
= tilde_expand (filename
);
941 buffer
= _rl_read_file (openname
, &file_size
);
948 if (include_level
== 0 && filename
!= last_readline_init_file
)
950 FREE (last_readline_init_file
);
951 last_readline_init_file
= savestring (filename
);
954 currently_reading_init_file
= 1;
956 /* Loop over the lines in the file. Lines that start with `#' are
957 comments; all other lines are commands for readline initialization. */
958 current_readline_init_lineno
= 1;
960 end
= buffer
+ file_size
;
963 /* Find the end of this line. */
964 for (i
= 0; line
+ i
!= end
&& line
[i
] != '\n'; i
++);
966 #if defined (__CYGWIN__)
967 /* ``Be liberal in what you accept.'' */
968 if (line
[i
] == '\n' && line
[i
-1] == '\r')
972 /* Mark end of line. */
975 /* Skip leading whitespace. */
976 while (*line
&& whitespace (*line
))
982 /* If the line is not a comment, then parse it. */
983 if (*line
&& *line
!= '#')
984 rl_parse_and_bind (line
);
986 /* Move to the next line. */
988 current_readline_init_lineno
++;
992 currently_reading_init_file
= 0;
997 #if defined (PREFER_STDARG)
998 _rl_init_file_error (const char *format
, ...)
1000 _rl_init_file_error (va_alist
)
1005 #if defined (PREFER_VARARGS)
1009 #if defined (PREFER_STDARG)
1010 va_start (args
, format
);
1013 format
= va_arg (args
, char *);
1016 fprintf (stderr
, "readline: ");
1017 if (currently_reading_init_file
)
1018 fprintf (stderr
, "%s: line %d: ", current_readline_init_file
,
1019 current_readline_init_lineno
);
1021 vfprintf (stderr
, format
, args
);
1022 fprintf (stderr
, "\n");
1028 /* **************************************************************** */
1030 /* Parser Directives */
1032 /* **************************************************************** */
1034 typedef int _rl_parser_func_t
PARAMS((char *));
1036 /* Things that mean `Control'. */
1037 const char * const _rl_possible_control_prefixes
[] = {
1038 "Control-", "C-", "CTRL-", (const char *)NULL
1041 const char * const _rl_possible_meta_prefixes
[] = {
1042 "Meta", "M-", (const char *)NULL
1047 /* Calling programs set this to have their argv[0]. */
1048 const char *rl_readline_name
= "other";
1050 /* Stack of previous values of parsing_conditionalized_out. */
1051 static unsigned char *if_stack
= (unsigned char *)NULL
;
1052 static int if_stack_depth
;
1053 static int if_stack_size
;
1055 /* Push _rl_parsing_conditionalized_out, and set parser state based
1063 /* Push parser state. */
1064 if (if_stack_depth
+ 1 >= if_stack_size
)
1067 if_stack
= (unsigned char *)xmalloc (if_stack_size
= 20);
1069 if_stack
= (unsigned char *)xrealloc (if_stack
, if_stack_size
+= 20);
1071 if_stack
[if_stack_depth
++] = _rl_parsing_conditionalized_out
;
1073 /* If parsing is turned off, then nothing can turn it back on except
1074 for finding the matching endif. In that case, return right now. */
1075 if (_rl_parsing_conditionalized_out
)
1078 /* Isolate first argument. */
1079 for (i
= 0; args
[i
] && !whitespace (args
[i
]); i
++);
1084 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
1085 isn't term=foo, or mode=emacs, then check to see if the first
1086 word in ARGS is the same as the value stored in rl_readline_name. */
1087 if (rl_terminal_name
&& _rl_strnicmp (args
, "term=", 5) == 0)
1091 /* Terminals like "aaa-60" are equivalent to "aaa". */
1092 tname
= savestring (rl_terminal_name
);
1093 tem
= strchr (tname
, '-');
1097 /* Test the `long' and `short' forms of the terminal name so that
1098 if someone has a `sun-cmd' and does not want to have bindings
1099 that will be executed if the terminal is a `sun', they can put
1100 `$if term=sun-cmd' into their .inputrc. */
1101 _rl_parsing_conditionalized_out
= _rl_stricmp (args
+ 5, tname
) &&
1102 _rl_stricmp (args
+ 5, rl_terminal_name
);
1105 #if defined (VI_MODE)
1106 else if (_rl_strnicmp (args
, "mode=", 5) == 0)
1110 if (_rl_stricmp (args
+ 5, "emacs") == 0)
1112 else if (_rl_stricmp (args
+ 5, "vi") == 0)
1117 _rl_parsing_conditionalized_out
= mode
!= rl_editing_mode
;
1119 #endif /* VI_MODE */
1120 /* Check to see if the first word in ARGS is the same as the
1121 value stored in rl_readline_name. */
1122 else if (_rl_stricmp (args
, rl_readline_name
) == 0)
1123 _rl_parsing_conditionalized_out
= 0;
1125 _rl_parsing_conditionalized_out
= 1;
1129 /* Invert the current parser state if there is anything on the stack. */
1136 if (if_stack_depth
== 0)
1138 _rl_init_file_error ("$else found without matching $if");
1143 /* Check the previous (n - 1) levels of the stack to make sure that
1144 we haven't previously turned off parsing. */
1145 for (i
= 0; i
< if_stack_depth
- 1; i
++)
1147 /* Check the previous (n) levels of the stack to make sure that
1148 we haven't previously turned off parsing. */
1149 for (i
= 0; i
< if_stack_depth
; i
++)
1151 if (if_stack
[i
] == 1)
1154 /* Invert the state of parsing if at top level. */
1155 _rl_parsing_conditionalized_out
= !_rl_parsing_conditionalized_out
;
1159 /* Terminate a conditional, popping the value of
1160 _rl_parsing_conditionalized_out from the stack. */
1166 _rl_parsing_conditionalized_out
= if_stack
[--if_stack_depth
];
1168 _rl_init_file_error ("$endif without matching $if");
1173 parser_include (args
)
1176 const char *old_init_file
;
1178 int old_line_number
, old_include_level
, r
;
1180 if (_rl_parsing_conditionalized_out
)
1183 old_init_file
= current_readline_init_file
;
1184 old_line_number
= current_readline_init_lineno
;
1185 old_include_level
= current_readline_init_include_level
;
1187 e
= strchr (args
, '\n');
1190 r
= _rl_read_init_file ((const char *)args
, old_include_level
+ 1);
1192 current_readline_init_file
= old_init_file
;
1193 current_readline_init_lineno
= old_line_number
;
1194 current_readline_init_include_level
= old_include_level
;
1199 /* Associate textual names with actual functions. */
1200 static const struct {
1201 const char * const name
;
1202 _rl_parser_func_t
*function
;
1203 } parser_directives
[] = {
1204 { "if", parser_if
},
1205 { "endif", parser_endif
},
1206 { "else", parser_else
},
1207 { "include", parser_include
},
1208 { (char *)0x0, (_rl_parser_func_t
*)0x0 }
1211 /* Handle a parser directive. STATEMENT is the line of the directive
1212 without any leading `$'. */
1214 handle_parser_directive (statement
)
1218 char *directive
, *args
;
1220 /* Isolate the actual directive. */
1222 /* Skip whitespace. */
1223 for (i
= 0; whitespace (statement
[i
]); i
++);
1225 directive
= &statement
[i
];
1227 for (; statement
[i
] && !whitespace (statement
[i
]); i
++);
1230 statement
[i
++] = '\0';
1232 for (; statement
[i
] && whitespace (statement
[i
]); i
++);
1234 args
= &statement
[i
];
1236 /* Lookup the command, and act on it. */
1237 for (i
= 0; parser_directives
[i
].name
; i
++)
1238 if (_rl_stricmp (directive
, parser_directives
[i
].name
) == 0)
1240 (*parser_directives
[i
].function
) (args
);
1244 /* display an error message about the unknown parser directive */
1245 _rl_init_file_error ("%s: unknown parser directive", directive
);
1249 /* Start at STRING[START] and look for DELIM. Return I where STRING[I] ==
1250 DELIM or STRING[I] == 0. DELIM is usually a double quote. */
1252 _rl_skip_to_delim (string
, start
, delim
)
1258 for (i
= start
,passc
= 0; c
= string
[i
]; i
++)
1281 /* Read the binding command from STRING and perform it.
1282 A key binding command looks like: Keyname: function-name\0,
1283 a variable binding command looks like: set variable value.
1284 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
1286 rl_parse_and_bind (string
)
1289 char *funname
, *kname
;
1291 int key
, equivalency
, foundmod
, foundsep
;
1293 while (string
&& whitespace (*string
))
1296 if (string
== 0 || *string
== 0 || *string
== '#')
1299 /* If this is a parser directive, act on it. */
1302 handle_parser_directive (&string
[1]);
1306 /* If we aren't supposed to be parsing right now, then we're done. */
1307 if (_rl_parsing_conditionalized_out
)
1311 /* If this keyname is a complex key expression surrounded by quotes,
1312 advance to after the matching close quote. This code allows the
1313 backslash to quote characters in the key expression. */
1316 i
= _rl_skip_to_delim (string
, 1, '"');
1318 /* If we didn't find a closing quote, abort the line. */
1319 if (string
[i
] == '\0')
1321 _rl_init_file_error ("%s: no closing `\"' in key binding", string
);
1325 i
++; /* skip past closing double quote */
1328 /* Advance to the colon (:) or whitespace which separates the two objects. */
1329 for (; (c
= string
[i
]) && c
!= ':' && c
!= ' ' && c
!= '\t'; i
++ );
1331 equivalency
= (c
== ':' && string
[i
+ 1] == '=');
1335 /* Mark the end of the command (or keyname). */
1339 /* If doing assignment, skip the '=' sign as well. */
1343 /* If this is a command to set a variable, then do that. */
1344 if (_rl_stricmp (string
, "set") == 0)
1346 char *var
, *value
, *e
;
1350 /* Make VAR point to start of variable name. */
1351 while (*var
&& whitespace (*var
)) var
++;
1353 /* Make VALUE point to start of value string. */
1355 while (*value
&& whitespace (*value
) == 0) value
++;
1358 while (*value
&& whitespace (*value
)) value
++;
1360 /* Strip trailing whitespace from values of boolean variables. */
1361 if (find_boolean_var (var
) >= 0)
1363 /* remove trailing whitespace */
1365 e
= value
+ strlen (value
) - 1;
1366 while (e
>= value
&& whitespace (*e
))
1368 e
++; /* skip back to whitespace or EOS */
1370 if (*e
&& e
>= value
)
1373 else if ((i
= find_string_var (var
)) >= 0)
1375 /* Allow quoted strings in variable values */
1378 i
= _rl_skip_to_delim (value
, 1, *value
);
1380 value
++; /* skip past the quote */
1383 goto remove_trailing
;
1386 rl_variable_bind (var
, value
);
1390 /* Skip any whitespace between keyname and funname. */
1391 for (; string
[i
] && whitespace (string
[i
]); i
++);
1392 funname
= &string
[i
];
1394 /* Now isolate funname.
1395 For straight function names just look for whitespace, since
1396 that will signify the end of the string. But this could be a
1397 macro definition. In that case, the string is quoted, so skip
1398 to the matching delimiter. We allow the backslash to quote the
1399 delimiter characters in the macro body. */
1400 /* This code exists to allow whitespace in macro expansions, which
1401 would otherwise be gobbled up by the next `for' loop.*/
1402 /* XXX - it may be desirable to allow backslash quoting only if " is
1403 the quoted string delimiter, like the shell. */
1404 if (*funname
== '\'' || *funname
== '"')
1406 i
= _rl_skip_to_delim (string
, i
+1, *funname
);
1411 /* Advance to the end of the string. */
1412 for (; string
[i
] && whitespace (string
[i
]) == 0; i
++);
1414 /* No extra whitespace at the end of the string. */
1417 /* Handle equivalency bindings here. Make the left-hand side be exactly
1418 whatever the right-hand evaluates to, including keymaps. */
1426 _rl_init_file_error ("%s: no key sequence terminator", string
);
1430 /* If this is a new-style key-binding, then do the binding with
1431 rl_bind_keyseq (). Otherwise, let the older code deal with it. */
1435 register int j
, k
, passc
;
1437 seq
= (char *)xmalloc (1 + strlen (string
));
1438 for (j
= 1, k
= passc
= 0; string
[j
]; j
++)
1440 /* Allow backslash to quote characters, but leave them in place.
1441 This allows a string to end with a backslash quoting another
1442 backslash, or with a backslash quoting a double quote. The
1443 backslashes are left in place for rl_translate_keyseq (). */
1444 if (passc
|| (string
[j
] == '\\'))
1446 seq
[k
++] = string
[j
];
1451 if (string
[j
] == '"')
1454 seq
[k
++] = string
[j
];
1458 /* Binding macro? */
1459 if (*funname
== '\'' || *funname
== '"')
1461 j
= strlen (funname
);
1463 /* Remove the delimiting quotes from each end of FUNNAME. */
1464 if (j
&& funname
[j
- 1] == *funname
)
1465 funname
[j
- 1] = '\0';
1467 rl_macro_bind (seq
, &funname
[1], _rl_keymap
);
1470 rl_bind_keyseq (seq
, rl_named_function (funname
));
1476 /* Get the actual character we want to deal with. */
1477 kname
= strrchr (string
, '-');
1483 key
= glean_key_from_name (kname
);
1485 /* Add in control and meta bits. */
1487 if (substring_member_of_array (string
, _rl_possible_control_prefixes
))
1489 key
= CTRL (_rl_to_upper (key
));
1493 if (substring_member_of_array (string
, _rl_possible_meta_prefixes
))
1499 if (foundmod
== 0 && kname
!= string
)
1501 _rl_init_file_error ("%s: unknown key modifier", string
);
1505 /* Temporary. Handle old-style keyname with macro-binding. */
1506 if (*funname
== '\'' || *funname
== '"')
1509 int fl
= strlen (funname
);
1511 useq
[0] = key
; useq
[1] = '\0';
1512 if (fl
&& funname
[fl
- 1] == *funname
)
1513 funname
[fl
- 1] = '\0';
1515 rl_macro_bind (useq
, &funname
[1], _rl_keymap
);
1517 #if defined (PREFIX_META_HACK)
1518 /* Ugly, but working hack to keep prefix-meta around. */
1519 else if (_rl_stricmp (funname
, "prefix-meta") == 0)
1525 rl_generic_bind (ISKMAP
, seq
, (char *)emacs_meta_keymap
, _rl_keymap
);
1527 #endif /* PREFIX_META_HACK */
1529 rl_bind_key (key
, rl_named_function (funname
));
1534 /* Simple structure for boolean readline variables (i.e., those that can
1535 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1538 #define V_SPECIAL 0x1
1540 static const struct {
1541 const char * const name
;
1544 } boolean_varlist
[] = {
1545 { "bind-tty-special-chars", &_rl_bind_stty_chars
, 0 },
1546 { "blink-matching-paren", &rl_blink_matching_paren
, V_SPECIAL
},
1547 { "byte-oriented", &rl_byte_oriented
, 0 },
1548 #if defined (COLOR_SUPPORT)
1549 { "colored-completion-prefix",&_rl_colored_completion_prefix
, 0 },
1550 { "colored-stats", &_rl_colored_stats
, 0 },
1552 { "completion-ignore-case", &_rl_completion_case_fold
, 0 },
1553 { "completion-map-case", &_rl_completion_case_map
, 0 },
1554 { "convert-meta", &_rl_convert_meta_chars_to_ascii
, 0 },
1555 { "disable-completion", &rl_inhibit_completion
, 0 },
1556 { "echo-control-characters", &_rl_echo_control_chars
, 0 },
1557 { "enable-bracketed-paste", &_rl_enable_bracketed_paste
, 0 },
1558 { "enable-keypad", &_rl_enable_keypad
, 0 },
1559 { "enable-meta-key", &_rl_enable_meta
, 0 },
1560 { "expand-tilde", &rl_complete_with_tilde_expansion
, 0 },
1561 { "history-preserve-point", &_rl_history_preserve_point
, 0 },
1562 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode
, 0 },
1563 { "input-meta", &_rl_meta_flag
, 0 },
1564 { "mark-directories", &_rl_complete_mark_directories
, 0 },
1565 { "mark-modified-lines", &_rl_mark_modified_lines
, 0 },
1566 { "mark-symlinked-directories", &_rl_complete_mark_symlink_dirs
, 0 },
1567 { "match-hidden-files", &_rl_match_hidden_files
, 0 },
1568 { "menu-complete-display-prefix", &_rl_menu_complete_prefix_first
, 0 },
1569 { "meta-flag", &_rl_meta_flag
, 0 },
1570 { "output-meta", &_rl_output_meta_chars
, 0 },
1571 { "page-completions", &_rl_page_completions
, 0 },
1572 { "prefer-visible-bell", &_rl_prefer_visible_bell
, V_SPECIAL
},
1573 { "print-completions-horizontally", &_rl_print_completions_horizontally
, 0 },
1574 { "revert-all-at-newline", &_rl_revert_all_at_newline
, 0 },
1575 { "show-all-if-ambiguous", &_rl_complete_show_all
, 0 },
1576 { "show-all-if-unmodified", &_rl_complete_show_unmodified
, 0 },
1577 { "show-mode-in-prompt", &_rl_show_mode_in_prompt
, 0 },
1578 { "skip-completed-text", &_rl_skip_completed_text
, 0 },
1579 #if defined (VISIBLE_STATS)
1580 { "visible-stats", &rl_visible_stats
, 0 },
1581 #endif /* VISIBLE_STATS */
1582 { (char *)NULL
, (int *)NULL
, 0 }
1586 find_boolean_var (name
)
1591 for (i
= 0; boolean_varlist
[i
].name
; i
++)
1592 if (_rl_stricmp (name
, boolean_varlist
[i
].name
) == 0)
1597 /* Hooks for handling special boolean variables, where a
1598 function needs to be called or another variable needs
1599 to be changed when they're changed. */
1601 hack_special_boolean_var (i
)
1606 name
= boolean_varlist
[i
].name
;
1608 if (_rl_stricmp (name
, "blink-matching-paren") == 0)
1609 _rl_enable_paren_matching (rl_blink_matching_paren
);
1610 else if (_rl_stricmp (name
, "prefer-visible-bell") == 0)
1612 if (_rl_prefer_visible_bell
)
1613 _rl_bell_preference
= VISIBLE_BELL
;
1615 _rl_bell_preference
= AUDIBLE_BELL
;
1617 else if (_rl_stricmp (name
, "show-mode-in-prompt") == 0)
1618 _rl_reset_prompt ();
1621 typedef int _rl_sv_func_t
PARAMS((const char *));
1623 /* These *must* correspond to the array indices for the appropriate
1624 string variable. (Though they're not used right now.) */
1625 #define V_BELLSTYLE 0
1626 #define V_COMBEGIN 1
1627 #define V_EDITMODE 2
1628 #define V_ISRCHTERM 3
1634 /* Forward declarations */
1635 static int sv_bell_style
PARAMS((const char *));
1636 static int sv_combegin
PARAMS((const char *));
1637 static int sv_dispprefix
PARAMS((const char *));
1638 static int sv_compquery
PARAMS((const char *));
1639 static int sv_compwidth
PARAMS((const char *));
1640 static int sv_editmode
PARAMS((const char *));
1641 static int sv_emacs_modestr
PARAMS((const char *));
1642 static int sv_histsize
PARAMS((const char *));
1643 static int sv_isrchterm
PARAMS((const char *));
1644 static int sv_keymap
PARAMS((const char *));
1645 static int sv_seqtimeout
PARAMS((const char *));
1646 static int sv_viins_modestr
PARAMS((const char *));
1647 static int sv_vicmd_modestr
PARAMS((const char *));
1649 static const struct {
1650 const char * const name
;
1652 _rl_sv_func_t
*set_func
;
1653 } string_varlist
[] = {
1654 { "bell-style", V_STRING
, sv_bell_style
},
1655 { "comment-begin", V_STRING
, sv_combegin
},
1656 { "completion-display-width", V_INT
, sv_compwidth
},
1657 { "completion-prefix-display-length", V_INT
, sv_dispprefix
},
1658 { "completion-query-items", V_INT
, sv_compquery
},
1659 { "editing-mode", V_STRING
, sv_editmode
},
1660 { "emacs-mode-string", V_STRING
, sv_emacs_modestr
},
1661 { "history-size", V_INT
, sv_histsize
},
1662 { "isearch-terminators", V_STRING
, sv_isrchterm
},
1663 { "keymap", V_STRING
, sv_keymap
},
1664 { "keyseq-timeout", V_INT
, sv_seqtimeout
},
1665 { "vi-cmd-mode-string", V_STRING
, sv_vicmd_modestr
},
1666 { "vi-ins-mode-string", V_STRING
, sv_viins_modestr
},
1667 { (char *)NULL
, 0, (_rl_sv_func_t
*)0 }
1671 find_string_var (name
)
1676 for (i
= 0; string_varlist
[i
].name
; i
++)
1677 if (_rl_stricmp (name
, string_varlist
[i
].name
) == 0)
1682 /* A boolean value that can appear in a `set variable' command is true if
1683 the value is null or empty, `on' (case-insensitive), or "1". Any other
1684 values result in 0 (false). */
1689 return (value
== 0 || *value
== '\0' ||
1690 (_rl_stricmp (value
, "on") == 0) ||
1691 (value
[0] == '1' && value
[1] == '\0'));
1695 rl_variable_value (name
)
1700 /* Check for simple variables first. */
1701 i
= find_boolean_var (name
);
1703 return (*boolean_varlist
[i
].value
? "on" : "off");
1705 i
= find_string_var (name
);
1707 return (_rl_get_string_variable_value (string_varlist
[i
].name
));
1709 /* Unknown variable names return NULL. */
1714 rl_variable_bind (name
, value
)
1715 const char *name
, *value
;
1720 /* Check for simple variables first. */
1721 i
= find_boolean_var (name
);
1724 *boolean_varlist
[i
].value
= bool_to_int (value
);
1725 if (boolean_varlist
[i
].flags
& V_SPECIAL
)
1726 hack_special_boolean_var (i
);
1730 i
= find_string_var (name
);
1732 /* For the time being, string names without a handler function are simply
1734 if (i
< 0 || string_varlist
[i
].set_func
== 0)
1737 _rl_init_file_error ("%s: unknown variable name", name
);
1741 v
= (*string_varlist
[i
].set_func
) (value
);
1749 if (_rl_strnicmp (value
, "vi", 2) == 0)
1751 #if defined (VI_MODE)
1752 _rl_keymap
= vi_insertion_keymap
;
1753 rl_editing_mode
= vi_mode
;
1754 #endif /* VI_MODE */
1757 else if (_rl_strnicmp (value
, "emacs", 5) == 0)
1759 _rl_keymap
= emacs_standard_keymap
;
1760 rl_editing_mode
= emacs_mode
;
1770 if (value
&& *value
)
1772 FREE (_rl_comment_begin
);
1773 _rl_comment_begin
= savestring (value
);
1780 sv_dispprefix (value
)
1785 if (value
&& *value
)
1787 nval
= atoi (value
);
1791 _rl_completion_prefix_display_length
= nval
;
1796 sv_compquery (value
)
1801 if (value
&& *value
)
1803 nval
= atoi (value
);
1807 rl_completion_query_items
= nval
;
1812 sv_compwidth (value
)
1817 if (value
&& *value
)
1818 nval
= atoi (value
);
1820 _rl_completion_columns
= nval
;
1831 if (value
&& *value
)
1833 nval
= atoi (value
);
1836 unstifle_history ();
1840 stifle_history (nval
);
1850 kmap
= rl_get_keymap_by_name (value
);
1853 rl_set_keymap (kmap
);
1860 sv_seqtimeout (value
)
1866 if (value
&& *value
)
1868 nval
= atoi (value
);
1872 _rl_keyseq_timeout
= nval
;
1877 sv_bell_style (value
)
1880 if (value
== 0 || *value
== '\0')
1881 _rl_bell_preference
= AUDIBLE_BELL
;
1882 else if (_rl_stricmp (value
, "none") == 0 || _rl_stricmp (value
, "off") == 0)
1883 _rl_bell_preference
= NO_BELL
;
1884 else if (_rl_stricmp (value
, "audible") == 0 || _rl_stricmp (value
, "on") == 0)
1885 _rl_bell_preference
= AUDIBLE_BELL
;
1886 else if (_rl_stricmp (value
, "visible") == 0)
1887 _rl_bell_preference
= VISIBLE_BELL
;
1894 sv_isrchterm (value
)
1897 int beg
, end
, delim
;
1903 /* Isolate the value and translate it into a character string. */
1904 v
= savestring (value
);
1905 FREE (_rl_isearch_terminators
);
1906 if (v
[0] == '"' || v
[0] == '\'')
1909 for (beg
= end
= 1; v
[end
] && v
[end
] != delim
; end
++)
1914 for (beg
= end
= 0; v
[end
] && whitespace (v
[end
]) == 0; end
++)
1920 /* The value starts at v + beg. Translate it into a character string. */
1921 _rl_isearch_terminators
= (char *)xmalloc (2 * strlen (v
) + 1);
1922 rl_translate_keyseq (v
+ beg
, _rl_isearch_terminators
, &end
);
1923 _rl_isearch_terminators
[end
] = '\0';
1929 extern char *_rl_emacs_mode_str
;
1932 sv_emacs_modestr (value
)
1935 if (value
&& *value
)
1937 FREE (_rl_emacs_mode_str
);
1938 _rl_emacs_mode_str
= (char *)xmalloc (2 * strlen (value
) + 1);
1939 rl_translate_keyseq (value
, _rl_emacs_mode_str
, &_rl_emacs_modestr_len
);
1940 _rl_emacs_mode_str
[_rl_emacs_modestr_len
] = '\0';
1945 FREE (_rl_emacs_mode_str
);
1946 _rl_emacs_mode_str
= (char *)xmalloc (1);
1947 _rl_emacs_mode_str
[_rl_emacs_modestr_len
= 0] = '\0';
1950 else if (value
== 0)
1952 FREE (_rl_emacs_mode_str
);
1953 _rl_emacs_mode_str
= 0; /* prompt_modestr does the right thing */
1954 _rl_emacs_modestr_len
= 0;
1961 sv_viins_modestr (value
)
1964 if (value
&& *value
)
1966 FREE (_rl_vi_ins_mode_str
);
1967 _rl_vi_ins_mode_str
= (char *)xmalloc (2 * strlen (value
) + 1);
1968 rl_translate_keyseq (value
, _rl_vi_ins_mode_str
, &_rl_vi_ins_modestr_len
);
1969 _rl_vi_ins_mode_str
[_rl_vi_ins_modestr_len
] = '\0';
1974 FREE (_rl_vi_ins_mode_str
);
1975 _rl_vi_ins_mode_str
= (char *)xmalloc (1);
1976 _rl_vi_ins_mode_str
[_rl_vi_ins_modestr_len
= 0] = '\0';
1979 else if (value
== 0)
1981 FREE (_rl_vi_ins_mode_str
);
1982 _rl_vi_ins_mode_str
= 0; /* prompt_modestr does the right thing */
1983 _rl_vi_ins_modestr_len
= 0;
1990 sv_vicmd_modestr (value
)
1993 if (value
&& *value
)
1995 FREE (_rl_vi_cmd_mode_str
);
1996 _rl_vi_cmd_mode_str
= (char *)xmalloc (2 * strlen (value
) + 1);
1997 rl_translate_keyseq (value
, _rl_vi_cmd_mode_str
, &_rl_vi_cmd_modestr_len
);
1998 _rl_vi_cmd_mode_str
[_rl_vi_cmd_modestr_len
] = '\0';
2003 FREE (_rl_vi_cmd_mode_str
);
2004 _rl_vi_cmd_mode_str
= (char *)xmalloc (1);
2005 _rl_vi_cmd_mode_str
[_rl_vi_cmd_modestr_len
= 0] = '\0';
2008 else if (value
== 0)
2010 FREE (_rl_vi_cmd_mode_str
);
2011 _rl_vi_cmd_mode_str
= 0; /* prompt_modestr does the right thing */
2012 _rl_vi_cmd_modestr_len
= 0;
2018 /* Return the character which matches NAME.
2019 For example, `Space' returns ' '. */
2022 const char * const name
;
2026 static const assoc_list name_key_alist
[] = {
2029 { "Escape", '\033' },
2031 { "Newline", '\n' },
2042 glean_key_from_name (name
)
2047 for (i
= 0; name_key_alist
[i
].name
; i
++)
2048 if (_rl_stricmp (name
, name_key_alist
[i
].name
) == 0)
2049 return (name_key_alist
[i
].value
);
2051 return (*(unsigned char *)name
); /* XXX was return (*name) */
2054 /* Auxiliary functions to manage keymaps. */
2055 static const struct {
2056 const char * const name
;
2058 } keymap_names
[] = {
2059 { "emacs", emacs_standard_keymap
},
2060 { "emacs-standard", emacs_standard_keymap
},
2061 { "emacs-meta", emacs_meta_keymap
},
2062 { "emacs-ctlx", emacs_ctlx_keymap
},
2063 #if defined (VI_MODE)
2064 { "vi", vi_movement_keymap
},
2065 { "vi-move", vi_movement_keymap
},
2066 { "vi-command", vi_movement_keymap
},
2067 { "vi-insert", vi_insertion_keymap
},
2068 #endif /* VI_MODE */
2069 { (char *)0x0, (Keymap
)0x0 }
2073 rl_get_keymap_by_name (name
)
2078 for (i
= 0; keymap_names
[i
].name
; i
++)
2079 if (_rl_stricmp (name
, keymap_names
[i
].name
) == 0)
2080 return (keymap_names
[i
].map
);
2081 return ((Keymap
) NULL
);
2085 rl_get_keymap_name (map
)
2089 for (i
= 0; keymap_names
[i
].name
; i
++)
2090 if (map
== keymap_names
[i
].map
)
2091 return ((char *)keymap_names
[i
].name
);
2092 return ((char *)NULL
);
2106 return (_rl_keymap
);
2110 rl_set_keymap_from_edit_mode ()
2112 if (rl_editing_mode
== emacs_mode
)
2113 _rl_keymap
= emacs_standard_keymap
;
2114 #if defined (VI_MODE)
2115 else if (rl_editing_mode
== vi_mode
)
2116 _rl_keymap
= vi_insertion_keymap
;
2117 #endif /* VI_MODE */
2121 rl_get_keymap_name_from_edit_mode ()
2123 if (rl_editing_mode
== emacs_mode
)
2125 #if defined (VI_MODE)
2126 else if (rl_editing_mode
== vi_mode
)
2128 #endif /* VI_MODE */
2133 /* **************************************************************** */
2135 /* Key Binding and Function Information */
2137 /* **************************************************************** */
2139 /* Each of the following functions produces information about the
2140 state of keybindings and functions known to Readline. The info
2141 is always printed to rl_outstream, and in such a way that it can
2142 be read back in (i.e., passed to rl_parse_and_bind ()). */
2144 /* Print the names of functions known to Readline. */
2146 rl_list_funmap_names ()
2149 const char **funmap_names
;
2151 funmap_names
= rl_funmap_names ();
2156 for (i
= 0; funmap_names
[i
]; i
++)
2157 fprintf (rl_outstream
, "%s\n", funmap_names
[i
]);
2159 xfree (funmap_names
);
2163 _rl_get_keyname (key
)
2169 keyname
= (char *)xmalloc (8);
2172 /* Since this is going to be used to write out keysequence-function
2173 pairs for possible inclusion in an inputrc file, we don't want to
2174 do any special meta processing on KEY. */
2177 /* XXX - Experimental */
2178 /* We might want to do this, but the old version of the code did not. */
2180 /* If this is an escape character, we don't want to do any more processing.
2181 Just add the special ESC key sequence and return. */
2191 /* RUBOUT is translated directly into \C-? */
2203 /* Now add special prefixes needed for control characters. This can
2204 potentially change C. */
2207 keyname
[i
++] = '\\';
2210 c
= _rl_to_lower (UNCTRL (c
));
2213 /* XXX experimental code. Turn the characters that are not ASCII or
2214 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
2216 if (c
>= 128 && c
<= 159)
2218 keyname
[i
++] = '\\';
2221 keyname
[i
++] = (c
/ 8) + '0';
2225 /* Now, if the character needs to be quoted with a backslash, do that. */
2226 if (c
== '\\' || c
== '"')
2227 keyname
[i
++] = '\\';
2229 /* Now add the key, terminate the string, and return it. */
2230 keyname
[i
++] = (char) c
;
2236 /* Return a NULL terminated array of strings which represent the key
2237 sequences that are used to invoke FUNCTION in MAP. */
2239 rl_invoking_keyseqs_in_map (function
, map
)
2240 rl_command_func_t
*function
;
2245 int result_index
, result_size
;
2247 result
= (char **)NULL
;
2248 result_index
= result_size
= 0;
2250 for (key
= 0; key
< KEYMAP_SIZE
; key
++)
2252 switch (map
[key
].type
)
2255 /* Macros match, if, and only if, the pointers are identical.
2256 Thus, they are treated exactly like functions in here. */
2258 /* If the function in the keymap is the one we are looking for,
2259 then add the current KEY to the list of invoking keys. */
2260 if (map
[key
].function
== function
)
2264 keyname
= _rl_get_keyname (key
);
2266 if (result_index
+ 2 > result_size
)
2269 result
= (char **)xrealloc (result
, result_size
* sizeof (char *));
2272 result
[result_index
++] = keyname
;
2273 result
[result_index
] = (char *)NULL
;
2282 /* Find the list of keyseqs in this map which have FUNCTION as
2283 their target. Add the key sequences found to RESULT. */
2284 if (map
[key
].function
)
2286 rl_invoking_keyseqs_in_map (function
, FUNCTION_TO_KEYMAP (map
, key
));
2293 for (i
= 0; seqs
[i
]; i
++)
2295 char *keyname
= (char *)xmalloc (6 + strlen (seqs
[i
]));
2299 /* If ESC is the meta prefix and we're converting chars
2300 with the eighth bit set to ESC-prefixed sequences, then
2301 we can use \M-. Otherwise we need to use the sequence
2303 if (_rl_convert_meta_chars_to_ascii
&& map
[ESC
].type
== ISKMAP
)
2304 sprintf (keyname
, "\\M-");
2306 sprintf (keyname
, "\\e");
2308 else if (CTRL_CHAR (key
))
2309 sprintf (keyname
, "\\C-%c", _rl_to_lower (UNCTRL (key
)));
2310 else if (key
== RUBOUT
)
2311 sprintf (keyname
, "\\C-?");
2312 else if (key
== '\\' || key
== '"')
2315 keyname
[1] = (char) key
;
2320 keyname
[0] = (char) key
;
2324 strcat (keyname
, seqs
[i
]);
2327 if (result_index
+ 2 > result_size
)
2330 result
= (char **)xrealloc (result
, result_size
* sizeof (char *));
2333 result
[result_index
++] = keyname
;
2334 result
[result_index
] = (char *)NULL
;
2345 /* Return a NULL terminated array of strings which represent the key
2346 sequences that can be used to invoke FUNCTION using the current keymap. */
2348 rl_invoking_keyseqs (function
)
2349 rl_command_func_t
*function
;
2351 return (rl_invoking_keyseqs_in_map (function
, _rl_keymap
));
2354 /* Print all of the functions and their bindings to rl_outstream. If
2355 PRINT_READABLY is non-zero, then print the output in such a way
2356 that it can be read back in. */
2358 rl_function_dumper (print_readably
)
2365 names
= rl_funmap_names ();
2367 fprintf (rl_outstream
, "\n");
2369 for (i
= 0; name
= names
[i
]; i
++)
2371 rl_command_func_t
*function
;
2374 function
= rl_named_function (name
);
2375 invokers
= rl_invoking_keyseqs_in_map (function
, _rl_keymap
);
2380 fprintf (rl_outstream
, "# %s (not bound)\n", name
);
2385 for (j
= 0; invokers
[j
]; j
++)
2387 fprintf (rl_outstream
, "\"%s\": %s\n",
2389 xfree (invokers
[j
]);
2398 fprintf (rl_outstream
, "%s is not bound to any keys\n",
2404 fprintf (rl_outstream
, "%s can be found on ", name
);
2406 for (j
= 0; invokers
[j
] && j
< 5; j
++)
2408 fprintf (rl_outstream
, "\"%s\"%s", invokers
[j
],
2409 invokers
[j
+ 1] ? ", " : ".\n");
2412 if (j
== 5 && invokers
[j
])
2413 fprintf (rl_outstream
, "...\n");
2415 for (j
= 0; invokers
[j
]; j
++)
2416 xfree (invokers
[j
]);
2426 /* Print all of the current functions and their bindings to
2427 rl_outstream. If an explicit argument is given, then print
2428 the output in such a way that it can be read back in. */
2430 rl_dump_functions (count
, key
)
2434 fprintf (rl_outstream
, "\r\n");
2435 rl_function_dumper (rl_explicit_arg
);
2441 _rl_macro_dumper_internal (print_readably
, map
, prefix
)
2447 char *keyname
, *out
;
2450 for (key
= 0; key
< KEYMAP_SIZE
; key
++)
2452 switch (map
[key
].type
)
2455 keyname
= _rl_get_keyname (key
);
2456 out
= _rl_untranslate_macro_value ((char *)map
[key
].function
, 0);
2459 fprintf (rl_outstream
, "\"%s%s\": \"%s\"\n", prefix
? prefix
: "",
2463 fprintf (rl_outstream
, "%s%s outputs %s\n", prefix
? prefix
: "",
2472 prefix_len
= prefix
? strlen (prefix
) : 0;
2475 keyname
= (char *)xmalloc (3 + prefix_len
);
2477 strcpy (keyname
, prefix
);
2478 keyname
[prefix_len
] = '\\';
2479 keyname
[prefix_len
+ 1] = 'e';
2480 keyname
[prefix_len
+ 2] = '\0';
2484 keyname
= _rl_get_keyname (key
);
2487 out
= (char *)xmalloc (strlen (keyname
) + prefix_len
+ 1);
2488 strcpy (out
, prefix
);
2489 strcpy (out
+ prefix_len
, keyname
);
2495 _rl_macro_dumper_internal (print_readably
, FUNCTION_TO_KEYMAP (map
, key
), keyname
);
2503 rl_macro_dumper (print_readably
)
2506 _rl_macro_dumper_internal (print_readably
, _rl_keymap
, (char *)NULL
);
2510 rl_dump_macros (count
, key
)
2514 fprintf (rl_outstream
, "\r\n");
2515 rl_macro_dumper (rl_explicit_arg
);
2521 _rl_get_string_variable_value (name
)
2524 static char numbuf
[32];
2527 if (_rl_stricmp (name
, "bell-style") == 0)
2529 switch (_rl_bell_preference
)
2540 else if (_rl_stricmp (name
, "comment-begin") == 0)
2541 return (_rl_comment_begin
? _rl_comment_begin
: RL_COMMENT_BEGIN_DEFAULT
);
2542 else if (_rl_stricmp (name
, "completion-display-width") == 0)
2544 sprintf (numbuf
, "%d", _rl_completion_columns
);
2547 else if (_rl_stricmp (name
, "completion-prefix-display-length") == 0)
2549 sprintf (numbuf
, "%d", _rl_completion_prefix_display_length
);
2552 else if (_rl_stricmp (name
, "completion-query-items") == 0)
2554 sprintf (numbuf
, "%d", rl_completion_query_items
);
2557 else if (_rl_stricmp (name
, "editing-mode") == 0)
2558 return (rl_get_keymap_name_from_edit_mode ());
2559 else if (_rl_stricmp (name
, "history-size") == 0)
2561 sprintf (numbuf
, "%d", history_is_stifled() ? history_max_entries
: 0);
2564 else if (_rl_stricmp (name
, "isearch-terminators") == 0)
2566 if (_rl_isearch_terminators
== 0)
2568 ret
= _rl_untranslate_macro_value (_rl_isearch_terminators
, 0);
2571 strncpy (numbuf
, ret
, sizeof (numbuf
) - 1);
2573 numbuf
[sizeof(numbuf
) - 1] = '\0';
2579 else if (_rl_stricmp (name
, "keymap") == 0)
2581 ret
= rl_get_keymap_name (_rl_keymap
);
2583 ret
= rl_get_keymap_name_from_edit_mode ();
2584 return (ret
? ret
: "none");
2586 else if (_rl_stricmp (name
, "keyseq-timeout") == 0)
2588 sprintf (numbuf
, "%d", _rl_keyseq_timeout
);
2591 else if (_rl_stricmp (name
, "emacs-mode-string") == 0)
2592 return (_rl_emacs_mode_str
? _rl_emacs_mode_str
: RL_EMACS_MODESTR_DEFAULT
);
2593 else if (_rl_stricmp (name
, "vi-cmd-mode-string") == 0)
2594 return (_rl_vi_cmd_mode_str
? _rl_vi_cmd_mode_str
: RL_VI_CMD_MODESTR_DEFAULT
);
2595 else if (_rl_stricmp (name
, "vi-ins-mode-string") == 0)
2596 return (_rl_vi_ins_mode_str
? _rl_vi_ins_mode_str
: RL_VI_INS_MODESTR_DEFAULT
);
2602 rl_variable_dumper (print_readably
)
2608 for (i
= 0; boolean_varlist
[i
].name
; i
++)
2611 fprintf (rl_outstream
, "set %s %s\n", boolean_varlist
[i
].name
,
2612 *boolean_varlist
[i
].value
? "on" : "off");
2614 fprintf (rl_outstream
, "%s is set to `%s'\n", boolean_varlist
[i
].name
,
2615 *boolean_varlist
[i
].value
? "on" : "off");
2618 for (i
= 0; string_varlist
[i
].name
; i
++)
2620 v
= _rl_get_string_variable_value (string_varlist
[i
].name
);
2621 if (v
== 0) /* _rl_isearch_terminators can be NULL */
2624 fprintf (rl_outstream
, "set %s %s\n", string_varlist
[i
].name
, v
);
2626 fprintf (rl_outstream
, "%s is set to `%s'\n", string_varlist
[i
].name
, v
);
2630 /* Print all of the current variables and their values to
2631 rl_outstream. If an explicit argument is given, then print
2632 the output in such a way that it can be read back in. */
2634 rl_dump_variables (count
, key
)
2638 fprintf (rl_outstream
, "\r\n");
2639 rl_variable_dumper (rl_explicit_arg
);
2644 /* Return non-zero if any members of ARRAY are a substring in STRING. */
2646 substring_member_of_array (string
, array
)
2648 const char * const *array
;
2652 if (_rl_strindex (string
, *array
))