1 /* bind.c -- key binding and startup file support for the readline library. */
3 /* Copyright (C) 1987-2012 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 static char *_rl_read_file
PARAMS((char *, size_t *));
78 static void _rl_init_file_error
PARAMS((const char *));
79 static int _rl_read_init_file
PARAMS((const char *, int));
80 static int glean_key_from_name
PARAMS((char *));
82 static int find_boolean_var
PARAMS((const char *));
83 static int find_string_var
PARAMS((const char *));
85 static char *_rl_get_string_variable_value
PARAMS((const char *));
86 static int substring_member_of_array
PARAMS((const char *, const char * const *));
88 static int currently_reading_init_file
;
90 /* used only in this file */
91 static int _rl_prefer_visible_bell
= 1;
93 /* **************************************************************** */
97 /* **************************************************************** */
99 /* rl_add_defun (char *name, rl_command_func_t *function, int key)
100 Add NAME to the list of named functions. Make FUNCTION be the function
101 that gets called. If KEY is not -1, then bind it. */
103 rl_add_defun (name
, function
, key
)
105 rl_command_func_t
*function
;
109 rl_bind_key (key
, function
);
110 rl_add_funmap_entry (name
, function
);
114 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
116 rl_bind_key (key
, function
)
118 rl_command_func_t
*function
;
126 if (META_CHAR (key
) && _rl_convert_meta_chars_to_ascii
)
128 if (_rl_keymap
[ESC
].type
== ISKMAP
)
132 escmap
= FUNCTION_TO_KEYMAP (_rl_keymap
, ESC
);
134 escmap
[key
].type
= ISFUNC
;
135 escmap
[key
].function
= function
;
141 /* If it's bound to a function or macro, just overwrite. Otherwise we have
142 to treat it as a key sequence so rl_generic_bind handles shadow keymaps
143 for us. If we are binding '\' make sure to escape it so it makes it
144 through the call to rl_translate_keyseq. */
145 if (_rl_keymap
[key
].type
!= ISKMAP
)
147 _rl_keymap
[key
].type
= ISFUNC
;
148 _rl_keymap
[key
].function
= function
;
157 rl_bind_keyseq (keyseq
, function
);
159 rl_binding_keymap
= _rl_keymap
;
163 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
166 rl_bind_key_in_map (key
, function
, map
)
168 rl_command_func_t
*function
;
176 result
= rl_bind_key (key
, function
);
181 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right
182 now, this is always used to attempt to bind the arrow keys, hence the
183 check for rl_vi_movement_mode. */
185 rl_bind_key_if_unbound_in_map (key
, default_func
, kmap
)
187 rl_command_func_t
*default_func
;
192 keyseq
[0] = (unsigned char)key
;
194 return (rl_bind_keyseq_if_unbound_in_map (keyseq
, default_func
, kmap
));
198 rl_bind_key_if_unbound (key
, default_func
)
200 rl_command_func_t
*default_func
;
204 keyseq
[0] = (unsigned char)key
;
206 return (rl_bind_keyseq_if_unbound_in_map (keyseq
, default_func
, _rl_keymap
));
209 /* Make KEY do nothing in the currently selected keymap.
210 Returns non-zero in case of error. */
215 return (rl_bind_key (key
, (rl_command_func_t
*)NULL
));
218 /* Make KEY do nothing in MAP.
219 Returns non-zero in case of error. */
221 rl_unbind_key_in_map (key
, map
)
225 return (rl_bind_key_in_map (key
, (rl_command_func_t
*)NULL
, map
));
228 /* Unbind all keys bound to FUNCTION in MAP. */
230 rl_unbind_function_in_map (func
, map
)
231 rl_command_func_t
*func
;
234 register int i
, rval
;
236 for (i
= rval
= 0; i
< KEYMAP_SIZE
; i
++)
238 if (map
[i
].type
== ISFUNC
&& map
[i
].function
== func
)
240 map
[i
].function
= (rl_command_func_t
*)NULL
;
248 rl_unbind_command_in_map (command
, map
)
252 rl_command_func_t
*func
;
254 func
= rl_named_function (command
);
257 return (rl_unbind_function_in_map (func
, map
));
260 /* Bind the key sequence represented by the string KEYSEQ to
261 FUNCTION, starting in the current keymap. This makes new
262 keymaps as necessary. */
264 rl_bind_keyseq (keyseq
, function
)
266 rl_command_func_t
*function
;
268 return (rl_generic_bind (ISFUNC
, keyseq
, (char *)function
, _rl_keymap
));
271 /* Bind the key sequence represented by the string KEYSEQ to
272 FUNCTION. This makes new keymaps as necessary. The initial
273 place to do bindings is in MAP. */
275 rl_bind_keyseq_in_map (keyseq
, function
, map
)
277 rl_command_func_t
*function
;
280 return (rl_generic_bind (ISFUNC
, keyseq
, (char *)function
, map
));
283 /* Backwards compatibility; equivalent to rl_bind_keyseq_in_map() */
285 rl_set_key (keyseq
, function
, map
)
287 rl_command_func_t
*function
;
290 return (rl_generic_bind (ISFUNC
, keyseq
, (char *)function
, map
));
293 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right
294 now, this is always used to attempt to bind the arrow keys, hence the
295 check for rl_vi_movement_mode. */
297 rl_bind_keyseq_if_unbound_in_map (keyseq
, default_func
, kmap
)
299 rl_command_func_t
*default_func
;
302 rl_command_func_t
*func
;
306 func
= rl_function_of_keyseq (keyseq
, kmap
, (int *)NULL
);
307 #if defined (VI_MODE)
308 if (!func
|| func
== rl_do_lowercase_version
|| func
== rl_vi_movement_mode
)
310 if (!func
|| func
== rl_do_lowercase_version
)
312 return (rl_bind_keyseq_in_map (keyseq
, default_func
, kmap
));
320 rl_bind_keyseq_if_unbound (keyseq
, default_func
)
322 rl_command_func_t
*default_func
;
324 return (rl_bind_keyseq_if_unbound_in_map (keyseq
, default_func
, _rl_keymap
));
327 /* Bind the key sequence represented by the string KEYSEQ to
328 the string of characters MACRO. This makes new keymaps as
329 necessary. The initial place to do bindings is in MAP. */
331 rl_macro_bind (keyseq
, macro
, map
)
332 const char *keyseq
, *macro
;
338 macro_keys
= (char *)xmalloc ((2 * strlen (macro
)) + 1);
340 if (rl_translate_keyseq (macro
, macro_keys
, ¯o_keys_len
))
345 rl_generic_bind (ISMACR
, keyseq
, macro_keys
, map
);
349 /* Bind the key sequence represented by the string KEYSEQ to
350 the arbitrary pointer DATA. TYPE says what kind of data is
351 pointed to by DATA, right now this can be a function (ISFUNC),
352 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
353 as necessary. The initial place to do bindings is in MAP. */
355 rl_generic_bind (type
, keyseq
, data
, map
)
368 /* If no keys to bind to, exit right away. */
369 if (keyseq
== 0 || *keyseq
== 0)
376 keys
= (char *)xmalloc (1 + (2 * strlen (keyseq
)));
378 /* Translate the ASCII representation of KEYSEQ into an array of
379 characters. Stuff the characters into KEYS, and the length of
380 KEYS into KEYS_LEN. */
381 if (rl_translate_keyseq (keyseq
, keys
, &keys_len
))
387 /* Bind keys, making new keymaps as necessary. */
388 for (i
= 0; i
< keys_len
; i
++)
390 unsigned char uc
= keys
[i
];
394 if (ic
< 0 || ic
>= KEYMAP_SIZE
)
400 if (META_CHAR (ic
) && _rl_convert_meta_chars_to_ascii
)
403 if (map
[ESC
].type
== ISKMAP
)
404 map
= FUNCTION_TO_KEYMAP (map
, ESC
);
407 if ((i
+ 1) < keys_len
)
409 if (map
[ic
].type
!= ISKMAP
)
411 /* We allow subsequences of keys. If a keymap is being
412 created that will `shadow' an existing function or macro
413 key binding, we save that keybinding into the ANYOTHERKEY
414 index in the new map. The dispatch code will look there
415 to find the function to execute if the subsequence is not
416 matched. ANYOTHERKEY was chosen to be greater than
420 map
[ic
].type
= ISKMAP
;
421 map
[ic
].function
= KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
423 map
= FUNCTION_TO_KEYMAP (map
, ic
);
424 /* The dispatch code will return this function if no matching
425 key sequence is found in the keymap. This (with a little
426 help from the dispatch code in readline.c) allows `a' to be
427 mapped to something, `abc' to be mapped to something else,
428 and the function bound to `a' to be executed when the user
429 types `abx', leaving `bx' in the input queue. */
430 if (k
.function
&& ((k
.type
== ISFUNC
&& k
.function
!= rl_do_lowercase_version
) || k
.type
== ISMACR
))
432 map
[ANYOTHERKEY
] = k
;
438 if (map
[ic
].type
== ISMACR
)
439 xfree ((char *)map
[ic
].function
);
440 else if (map
[ic
].type
== ISKMAP
)
442 map
= FUNCTION_TO_KEYMAP (map
, ic
);
444 /* If we're trying to override a keymap with a null function
445 (e.g., trying to unbind it), we can't use a null pointer
446 here because that's indistinguishable from having not been
447 overridden. We use a special bindable function that does
449 if (type
== ISFUNC
&& data
== 0)
450 data
= (char *)_rl_null_function
;
453 map
[ic
].function
= KEYMAP_TO_FUNCTION (data
);
457 rl_binding_keymap
= map
;
463 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
464 an array of characters. LEN gets the final length of ARRAY. Return
465 non-zero if there was an error parsing SEQ. */
467 rl_translate_keyseq (seq
, array
, len
)
472 register int i
, c
, l
, temp
;
474 for (i
= l
= 0; c
= seq
[i
]; i
++)
483 /* Handle \C- and \M- prefixes. */
484 if ((c
== 'C' || c
== 'M') && seq
[i
+ 1] == '-')
486 /* Handle special case of backwards define. */
487 if (strncmp (&seq
[i
], "C-\\M-", 5) == 0)
489 array
[l
++] = ESC
; /* ESC is meta-prefix */
491 array
[l
++] = CTRL (_rl_to_upper (seq
[i
]));
497 i
++; /* seq[i] == '-' */
498 /* XXX - obey convert-meta setting */
499 if (_rl_convert_meta_chars_to_ascii
&& _rl_keymap
[ESC
].type
== ISKMAP
)
500 array
[l
++] = ESC
; /* ESC is meta-prefix */
501 else if (seq
[i
+1] == '\\' && seq
[i
+2] == 'C' && seq
[i
+3] == '-')
504 temp
= (seq
[i
] == '?') ? RUBOUT
: CTRL (_rl_to_upper (seq
[i
]));
505 array
[l
++] = META (temp
);
509 /* This doesn't yet handle things like \M-\a, which may
510 or may not have any reasonable meaning. You're
511 probably better off using straight octal or hex. */
513 array
[l
++] = META (seq
[i
]);
519 /* Special hack for C-?... */
520 array
[l
++] = (seq
[i
] == '?') ? RUBOUT
: CTRL (_rl_to_upper (seq
[i
]));
525 /* Translate other backslash-escaped characters. These are the
526 same escape sequences that bash's `echo' and `printf' builtins
527 handle, with the addition of \d -> RUBOUT. A backslash
528 preceding a character that is not special is stripped. */
538 array
[l
++] = RUBOUT
; /* readline-specific */
547 array
[l
++] = NEWLINE
;
561 case '0': case '1': case '2': case '3':
562 case '4': case '5': case '6': case '7':
564 for (temp
= 2, c
-= '0'; ISOCTAL ((unsigned char)seq
[i
]) && temp
--; i
++)
565 c
= (c
* 8) + OCTVALUE (seq
[i
]);
566 i
--; /* auto-increment in for loop */
567 array
[l
++] = c
& largest_char
;
571 for (temp
= 2, c
= 0; ISXDIGIT ((unsigned char)seq
[i
]) && temp
--; i
++)
572 c
= (c
* 16) + HEXVALUE (seq
[i
]);
575 i
--; /* auto-increment in for loop */
576 array
[l
++] = c
& largest_char
;
578 default: /* backslashes before non-special chars just add the char */
580 break; /* the backslash is stripped */
605 case 0x0b: return (1);
616 case '\007': return ('a');
617 case '\b': return ('b');
618 case '\f': return ('f');
619 case '\n': return ('n');
620 case '\r': return ('r');
621 case TAB
: return ('t');
622 case 0x0b: return ('v');
628 rl_untranslate_keyseq (seq
)
631 static char kseq
[16];
648 else if (CTRL_CHAR (c
))
653 c
= _rl_to_lower (UNCTRL (c
));
655 else if (c
== RUBOUT
)
668 else if (c
== '\\' || c
== '"')
673 kseq
[i
++] = (unsigned char) c
;
679 _rl_untranslate_macro_value (seq
, use_escapes
)
686 r
= ret
= (char *)xmalloc (7 * strlen (seq
) + 1);
687 for (s
= seq
; *s
; s
++)
702 else if (CTRL_CHAR (c
))
705 if (use_escapes
&& _rl_isescape (c
))
711 c
= _rl_to_lower (UNCTRL (c
));
714 else if (c
== RUBOUT
)
727 else if (c
== '\\' || c
== '"')
730 *r
++ = (unsigned char)c
;
736 /* Return a pointer to the function that STRING represents.
737 If STRING doesn't have a matching function, then a NULL pointer
740 rl_named_function (string
)
745 rl_initialize_funmap ();
747 for (i
= 0; funmap
[i
]; i
++)
748 if (_rl_stricmp (funmap
[i
]->name
, string
) == 0)
749 return (funmap
[i
]->function
);
750 return ((rl_command_func_t
*)NULL
);
753 /* Return the function (or macro) definition which would be invoked via
754 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
755 used. TYPE, if non-NULL, is a pointer to an int which will receive the
756 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
757 or ISMACR (macro). */
759 rl_function_of_keyseq (keyseq
, map
, type
)
769 for (i
= 0; keyseq
&& keyseq
[i
]; i
++)
771 unsigned char ic
= keyseq
[i
];
773 if (META_CHAR (ic
) && _rl_convert_meta_chars_to_ascii
)
775 if (map
[ESC
].type
== ISKMAP
)
777 map
= FUNCTION_TO_KEYMAP (map
, ESC
);
780 /* XXX - should we just return NULL here, since this obviously
785 *type
= map
[ESC
].type
;
787 return (map
[ESC
].function
);
791 if (map
[ic
].type
== ISKMAP
)
793 /* If this is the last key in the key sequence, return the
795 if (keyseq
[i
+ 1] == '\0')
800 return (map
[ic
].function
);
803 map
= FUNCTION_TO_KEYMAP (map
, ic
);
805 /* If we're not at the end of the key sequence, and the current key
806 is bound to something other than a keymap, then the entire key
807 sequence is not bound. */
808 else if (map
[ic
].type
!= ISKMAP
&& keyseq
[i
+1])
809 return ((rl_command_func_t
*)NULL
);
810 else /* map[ic].type != ISKMAP && keyseq[i+1] == 0 */
813 *type
= map
[ic
].type
;
815 return (map
[ic
].function
);
818 return ((rl_command_func_t
*) NULL
);
821 /* The last key bindings file read. */
822 static char *last_readline_init_file
= (char *)NULL
;
824 /* The file we're currently reading key bindings from. */
825 static const char *current_readline_init_file
;
826 static int current_readline_init_include_level
;
827 static int current_readline_init_lineno
;
829 /* Read FILENAME into a locally-allocated buffer and return the buffer.
830 The size of the buffer is returned in *SIZEP. Returns NULL if any
831 errors were encountered. */
833 _rl_read_file (filename
, sizep
)
842 if ((stat (filename
, &finfo
) < 0) || (file
= open (filename
, O_RDONLY
, 0666)) < 0)
843 return ((char *)NULL
);
845 file_size
= (size_t)finfo
.st_size
;
847 /* check for overflow on very large files */
848 if (file_size
!= finfo
.st_size
|| file_size
+ 1 < file_size
)
855 return ((char *)NULL
);
858 /* Read the file into BUFFER. */
859 buffer
= (char *)xmalloc (file_size
+ 1);
860 i
= read (file
, buffer
, file_size
);
866 return ((char *)NULL
);
878 /* Re-read the current keybindings file. */
880 rl_re_read_init_file (count
, ignore
)
884 r
= rl_read_init_file ((const char *)NULL
);
885 rl_set_keymap_from_edit_mode ();
889 /* Do key bindings from a file. If FILENAME is NULL it defaults
890 to the first non-null filename from this list:
891 1. the filename used for the previous call
892 2. the value of the shell variable `INPUTRC'
895 If the file existed and could be opened and read, 0 is returned,
896 otherwise errno is returned. */
898 rl_read_init_file (filename
)
899 const char *filename
;
901 /* Default the filename. */
903 filename
= last_readline_init_file
;
905 filename
= sh_get_env_value ("INPUTRC");
906 if (filename
== 0 || *filename
== 0)
908 filename
= DEFAULT_INPUTRC
;
909 /* Try to read DEFAULT_INPUTRC; fall back to SYS_INPUTRC on failure */
910 if (_rl_read_init_file (filename
, 0) == 0)
912 filename
= SYS_INPUTRC
;
915 #if defined (__MSDOS__)
916 if (_rl_read_init_file (filename
, 0) == 0)
918 filename
= "~/_inputrc";
920 return (_rl_read_init_file (filename
, 0));
924 _rl_read_init_file (filename
, include_level
)
925 const char *filename
;
929 char *buffer
, *openname
, *line
, *end
;
932 current_readline_init_file
= filename
;
933 current_readline_init_include_level
= include_level
;
935 openname
= tilde_expand (filename
);
936 buffer
= _rl_read_file (openname
, &file_size
);
943 if (include_level
== 0 && filename
!= last_readline_init_file
)
945 FREE (last_readline_init_file
);
946 last_readline_init_file
= savestring (filename
);
949 currently_reading_init_file
= 1;
951 /* Loop over the lines in the file. Lines that start with `#' are
952 comments; all other lines are commands for readline initialization. */
953 current_readline_init_lineno
= 1;
955 end
= buffer
+ file_size
;
958 /* Find the end of this line. */
959 for (i
= 0; line
+ i
!= end
&& line
[i
] != '\n'; i
++);
961 #if defined (__CYGWIN__)
962 /* ``Be liberal in what you accept.'' */
963 if (line
[i
] == '\n' && line
[i
-1] == '\r')
967 /* Mark end of line. */
970 /* Skip leading whitespace. */
971 while (*line
&& whitespace (*line
))
977 /* If the line is not a comment, then parse it. */
978 if (*line
&& *line
!= '#')
979 rl_parse_and_bind (line
);
981 /* Move to the next line. */
983 current_readline_init_lineno
++;
987 currently_reading_init_file
= 0;
992 _rl_init_file_error (msg
)
995 if (currently_reading_init_file
)
996 _rl_errmsg ("%s: line %d: %s\n", current_readline_init_file
,
997 current_readline_init_lineno
, msg
);
999 _rl_errmsg ("%s", msg
);
1002 /* **************************************************************** */
1004 /* Parser Directives */
1006 /* **************************************************************** */
1008 typedef int _rl_parser_func_t
PARAMS((char *));
1010 /* Things that mean `Control'. */
1011 const char * const _rl_possible_control_prefixes
[] = {
1012 "Control-", "C-", "CTRL-", (const char *)NULL
1015 const char * const _rl_possible_meta_prefixes
[] = {
1016 "Meta", "M-", (const char *)NULL
1021 /* Calling programs set this to have their argv[0]. */
1022 const char *rl_readline_name
= "other";
1024 /* Stack of previous values of parsing_conditionalized_out. */
1025 static unsigned char *if_stack
= (unsigned char *)NULL
;
1026 static int if_stack_depth
;
1027 static int if_stack_size
;
1029 /* Push _rl_parsing_conditionalized_out, and set parser state based
1037 /* Push parser state. */
1038 if (if_stack_depth
+ 1 >= if_stack_size
)
1041 if_stack
= (unsigned char *)xmalloc (if_stack_size
= 20);
1043 if_stack
= (unsigned char *)xrealloc (if_stack
, if_stack_size
+= 20);
1045 if_stack
[if_stack_depth
++] = _rl_parsing_conditionalized_out
;
1047 /* If parsing is turned off, then nothing can turn it back on except
1048 for finding the matching endif. In that case, return right now. */
1049 if (_rl_parsing_conditionalized_out
)
1052 /* Isolate first argument. */
1053 for (i
= 0; args
[i
] && !whitespace (args
[i
]); i
++);
1058 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
1059 isn't term=foo, or mode=emacs, then check to see if the first
1060 word in ARGS is the same as the value stored in rl_readline_name. */
1061 if (rl_terminal_name
&& _rl_strnicmp (args
, "term=", 5) == 0)
1065 /* Terminals like "aaa-60" are equivalent to "aaa". */
1066 tname
= savestring (rl_terminal_name
);
1067 tem
= strchr (tname
, '-');
1071 /* Test the `long' and `short' forms of the terminal name so that
1072 if someone has a `sun-cmd' and does not want to have bindings
1073 that will be executed if the terminal is a `sun', they can put
1074 `$if term=sun-cmd' into their .inputrc. */
1075 _rl_parsing_conditionalized_out
= _rl_stricmp (args
+ 5, tname
) &&
1076 _rl_stricmp (args
+ 5, rl_terminal_name
);
1079 #if defined (VI_MODE)
1080 else if (_rl_strnicmp (args
, "mode=", 5) == 0)
1084 if (_rl_stricmp (args
+ 5, "emacs") == 0)
1086 else if (_rl_stricmp (args
+ 5, "vi") == 0)
1091 _rl_parsing_conditionalized_out
= mode
!= rl_editing_mode
;
1093 #endif /* VI_MODE */
1094 /* Check to see if the first word in ARGS is the same as the
1095 value stored in rl_readline_name. */
1096 else if (_rl_stricmp (args
, rl_readline_name
) == 0)
1097 _rl_parsing_conditionalized_out
= 0;
1099 _rl_parsing_conditionalized_out
= 1;
1103 /* Invert the current parser state if there is anything on the stack. */
1110 if (if_stack_depth
== 0)
1112 _rl_init_file_error ("$else found without matching $if");
1117 /* Check the previous (n - 1) levels of the stack to make sure that
1118 we haven't previously turned off parsing. */
1119 for (i
= 0; i
< if_stack_depth
- 1; i
++)
1121 /* Check the previous (n) levels of the stack to make sure that
1122 we haven't previously turned off parsing. */
1123 for (i
= 0; i
< if_stack_depth
; i
++)
1125 if (if_stack
[i
] == 1)
1128 /* Invert the state of parsing if at top level. */
1129 _rl_parsing_conditionalized_out
= !_rl_parsing_conditionalized_out
;
1133 /* Terminate a conditional, popping the value of
1134 _rl_parsing_conditionalized_out from the stack. */
1140 _rl_parsing_conditionalized_out
= if_stack
[--if_stack_depth
];
1142 _rl_init_file_error ("$endif without matching $if");
1147 parser_include (args
)
1150 const char *old_init_file
;
1152 int old_line_number
, old_include_level
, r
;
1154 if (_rl_parsing_conditionalized_out
)
1157 old_init_file
= current_readline_init_file
;
1158 old_line_number
= current_readline_init_lineno
;
1159 old_include_level
= current_readline_init_include_level
;
1161 e
= strchr (args
, '\n');
1164 r
= _rl_read_init_file ((const char *)args
, old_include_level
+ 1);
1166 current_readline_init_file
= old_init_file
;
1167 current_readline_init_lineno
= old_line_number
;
1168 current_readline_init_include_level
= old_include_level
;
1173 /* Associate textual names with actual functions. */
1174 static const struct {
1175 const char * const name
;
1176 _rl_parser_func_t
*function
;
1177 } parser_directives
[] = {
1178 { "if", parser_if
},
1179 { "endif", parser_endif
},
1180 { "else", parser_else
},
1181 { "include", parser_include
},
1182 { (char *)0x0, (_rl_parser_func_t
*)0x0 }
1185 /* Handle a parser directive. STATEMENT is the line of the directive
1186 without any leading `$'. */
1188 handle_parser_directive (statement
)
1192 char *directive
, *args
;
1194 /* Isolate the actual directive. */
1196 /* Skip whitespace. */
1197 for (i
= 0; whitespace (statement
[i
]); i
++);
1199 directive
= &statement
[i
];
1201 for (; statement
[i
] && !whitespace (statement
[i
]); i
++);
1204 statement
[i
++] = '\0';
1206 for (; statement
[i
] && whitespace (statement
[i
]); i
++);
1208 args
= &statement
[i
];
1210 /* Lookup the command, and act on it. */
1211 for (i
= 0; parser_directives
[i
].name
; i
++)
1212 if (_rl_stricmp (directive
, parser_directives
[i
].name
) == 0)
1214 (*parser_directives
[i
].function
) (args
);
1218 /* display an error message about the unknown parser directive */
1219 _rl_init_file_error ("unknown parser directive");
1223 /* Start at STRING[START] and look for DELIM. Return I where STRING[I] ==
1224 DELIM or STRING[I] == 0. DELIM is usually a double quote. */
1226 _rl_skip_to_delim (string
, start
, delim
)
1232 for (i
= start
,passc
= 0; c
= string
[i
]; i
++)
1255 /* Read the binding command from STRING and perform it.
1256 A key binding command looks like: Keyname: function-name\0,
1257 a variable binding command looks like: set variable value.
1258 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
1260 rl_parse_and_bind (string
)
1263 char *funname
, *kname
;
1265 int key
, equivalency
;
1267 while (string
&& whitespace (*string
))
1270 if (string
== 0 || *string
== 0 || *string
== '#')
1273 /* If this is a parser directive, act on it. */
1276 handle_parser_directive (&string
[1]);
1280 /* If we aren't supposed to be parsing right now, then we're done. */
1281 if (_rl_parsing_conditionalized_out
)
1285 /* If this keyname is a complex key expression surrounded by quotes,
1286 advance to after the matching close quote. This code allows the
1287 backslash to quote characters in the key expression. */
1290 i
= _rl_skip_to_delim (string
, 1, '"');
1292 /* If we didn't find a closing quote, abort the line. */
1293 if (string
[i
] == '\0')
1295 _rl_init_file_error ("no closing `\"' in key binding");
1299 i
++; /* skip past closing double quote */
1302 /* Advance to the colon (:) or whitespace which separates the two objects. */
1303 for (; (c
= string
[i
]) && c
!= ':' && c
!= ' ' && c
!= '\t'; i
++ );
1305 equivalency
= (c
== ':' && string
[i
+ 1] == '=');
1307 /* Mark the end of the command (or keyname). */
1311 /* If doing assignment, skip the '=' sign as well. */
1315 /* If this is a command to set a variable, then do that. */
1316 if (_rl_stricmp (string
, "set") == 0)
1318 char *var
, *value
, *e
;
1322 /* Make VAR point to start of variable name. */
1323 while (*var
&& whitespace (*var
)) var
++;
1325 /* Make VALUE point to start of value string. */
1327 while (*value
&& whitespace (*value
) == 0) value
++;
1330 while (*value
&& whitespace (*value
)) value
++;
1332 /* Strip trailing whitespace from values of boolean variables. */
1333 if (find_boolean_var (var
) >= 0)
1335 /* remove trailing whitespace */
1337 e
= value
+ strlen (value
) - 1;
1338 while (e
>= value
&& whitespace (*e
))
1340 e
++; /* skip back to whitespace or EOS */
1342 if (*e
&& e
>= value
)
1345 else if ((i
= find_string_var (var
)) >= 0)
1347 /* Allow quoted strings in variable values */
1350 i
= _rl_skip_to_delim (value
, 1, *value
);
1352 value
++; /* skip past the quote */
1355 goto remove_trailing
;
1358 rl_variable_bind (var
, value
);
1362 /* Skip any whitespace between keyname and funname. */
1363 for (; string
[i
] && whitespace (string
[i
]); i
++);
1364 funname
= &string
[i
];
1366 /* Now isolate funname.
1367 For straight function names just look for whitespace, since
1368 that will signify the end of the string. But this could be a
1369 macro definition. In that case, the string is quoted, so skip
1370 to the matching delimiter. We allow the backslash to quote the
1371 delimiter characters in the macro body. */
1372 /* This code exists to allow whitespace in macro expansions, which
1373 would otherwise be gobbled up by the next `for' loop.*/
1374 /* XXX - it may be desirable to allow backslash quoting only if " is
1375 the quoted string delimiter, like the shell. */
1376 if (*funname
== '\'' || *funname
== '"')
1378 i
= _rl_skip_to_delim (string
, i
+1, *funname
);
1383 /* Advance to the end of the string. */
1384 for (; string
[i
] && whitespace (string
[i
]) == 0; i
++);
1386 /* No extra whitespace at the end of the string. */
1389 /* Handle equivalency bindings here. Make the left-hand side be exactly
1390 whatever the right-hand evaluates to, including keymaps. */
1396 /* If this is a new-style key-binding, then do the binding with
1397 rl_bind_keyseq (). Otherwise, let the older code deal with it. */
1401 register int j
, k
, passc
;
1403 seq
= (char *)xmalloc (1 + strlen (string
));
1404 for (j
= 1, k
= passc
= 0; string
[j
]; j
++)
1406 /* Allow backslash to quote characters, but leave them in place.
1407 This allows a string to end with a backslash quoting another
1408 backslash, or with a backslash quoting a double quote. The
1409 backslashes are left in place for rl_translate_keyseq (). */
1410 if (passc
|| (string
[j
] == '\\'))
1412 seq
[k
++] = string
[j
];
1417 if (string
[j
] == '"')
1420 seq
[k
++] = string
[j
];
1424 /* Binding macro? */
1425 if (*funname
== '\'' || *funname
== '"')
1427 j
= strlen (funname
);
1429 /* Remove the delimiting quotes from each end of FUNNAME. */
1430 if (j
&& funname
[j
- 1] == *funname
)
1431 funname
[j
- 1] = '\0';
1433 rl_macro_bind (seq
, &funname
[1], _rl_keymap
);
1436 rl_bind_keyseq (seq
, rl_named_function (funname
));
1442 /* Get the actual character we want to deal with. */
1443 kname
= strrchr (string
, '-');
1449 key
= glean_key_from_name (kname
);
1451 /* Add in control and meta bits. */
1452 if (substring_member_of_array (string
, _rl_possible_control_prefixes
))
1453 key
= CTRL (_rl_to_upper (key
));
1455 if (substring_member_of_array (string
, _rl_possible_meta_prefixes
))
1458 /* Temporary. Handle old-style keyname with macro-binding. */
1459 if (*funname
== '\'' || *funname
== '"')
1462 int fl
= strlen (funname
);
1464 useq
[0] = key
; useq
[1] = '\0';
1465 if (fl
&& funname
[fl
- 1] == *funname
)
1466 funname
[fl
- 1] = '\0';
1468 rl_macro_bind (useq
, &funname
[1], _rl_keymap
);
1470 #if defined (PREFIX_META_HACK)
1471 /* Ugly, but working hack to keep prefix-meta around. */
1472 else if (_rl_stricmp (funname
, "prefix-meta") == 0)
1478 rl_generic_bind (ISKMAP
, seq
, (char *)emacs_meta_keymap
, _rl_keymap
);
1480 #endif /* PREFIX_META_HACK */
1482 rl_bind_key (key
, rl_named_function (funname
));
1486 /* Simple structure for boolean readline variables (i.e., those that can
1487 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1490 #define V_SPECIAL 0x1
1492 static const struct {
1493 const char * const name
;
1496 } boolean_varlist
[] = {
1497 { "bind-tty-special-chars", &_rl_bind_stty_chars
, 0 },
1498 { "blink-matching-paren", &rl_blink_matching_paren
, V_SPECIAL
},
1499 { "byte-oriented", &rl_byte_oriented
, 0 },
1500 #if defined (COLOR_SUPPORT)
1501 { "colored-completion-prefix",&_rl_colored_completion_prefix
, 0 },
1502 { "colored-stats", &_rl_colored_stats
, 0 },
1504 { "completion-ignore-case", &_rl_completion_case_fold
, 0 },
1505 { "completion-map-case", &_rl_completion_case_map
, 0 },
1506 { "convert-meta", &_rl_convert_meta_chars_to_ascii
, 0 },
1507 { "disable-completion", &rl_inhibit_completion
, 0 },
1508 { "echo-control-characters", &_rl_echo_control_chars
, 0 },
1509 { "enable-bracketed-paste", &_rl_enable_bracketed_paste
, 0 },
1510 { "enable-keypad", &_rl_enable_keypad
, 0 },
1511 { "enable-meta-key", &_rl_enable_meta
, 0 },
1512 { "expand-tilde", &rl_complete_with_tilde_expansion
, 0 },
1513 { "history-preserve-point", &_rl_history_preserve_point
, 0 },
1514 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode
, 0 },
1515 { "input-meta", &_rl_meta_flag
, 0 },
1516 { "mark-directories", &_rl_complete_mark_directories
, 0 },
1517 { "mark-modified-lines", &_rl_mark_modified_lines
, 0 },
1518 { "mark-symlinked-directories", &_rl_complete_mark_symlink_dirs
, 0 },
1519 { "match-hidden-files", &_rl_match_hidden_files
, 0 },
1520 { "menu-complete-display-prefix", &_rl_menu_complete_prefix_first
, 0 },
1521 { "meta-flag", &_rl_meta_flag
, 0 },
1522 { "output-meta", &_rl_output_meta_chars
, 0 },
1523 { "page-completions", &_rl_page_completions
, 0 },
1524 { "prefer-visible-bell", &_rl_prefer_visible_bell
, V_SPECIAL
},
1525 { "print-completions-horizontally", &_rl_print_completions_horizontally
, 0 },
1526 { "revert-all-at-newline", &_rl_revert_all_at_newline
, 0 },
1527 { "show-all-if-ambiguous", &_rl_complete_show_all
, 0 },
1528 { "show-all-if-unmodified", &_rl_complete_show_unmodified
, 0 },
1529 { "show-mode-in-prompt", &_rl_show_mode_in_prompt
, 0 },
1530 { "skip-completed-text", &_rl_skip_completed_text
, 0 },
1531 #if defined (VISIBLE_STATS)
1532 { "visible-stats", &rl_visible_stats
, 0 },
1533 #endif /* VISIBLE_STATS */
1534 { (char *)NULL
, (int *)NULL
, 0 }
1538 find_boolean_var (name
)
1543 for (i
= 0; boolean_varlist
[i
].name
; i
++)
1544 if (_rl_stricmp (name
, boolean_varlist
[i
].name
) == 0)
1549 /* Hooks for handling special boolean variables, where a
1550 function needs to be called or another variable needs
1551 to be changed when they're changed. */
1553 hack_special_boolean_var (i
)
1558 name
= boolean_varlist
[i
].name
;
1560 if (_rl_stricmp (name
, "blink-matching-paren") == 0)
1561 _rl_enable_paren_matching (rl_blink_matching_paren
);
1562 else if (_rl_stricmp (name
, "prefer-visible-bell") == 0)
1564 if (_rl_prefer_visible_bell
)
1565 _rl_bell_preference
= VISIBLE_BELL
;
1567 _rl_bell_preference
= AUDIBLE_BELL
;
1569 else if (_rl_stricmp (name
, "show-mode-in-prompt") == 0)
1570 _rl_reset_prompt ();
1573 typedef int _rl_sv_func_t
PARAMS((const char *));
1575 /* These *must* correspond to the array indices for the appropriate
1576 string variable. (Though they're not used right now.) */
1577 #define V_BELLSTYLE 0
1578 #define V_COMBEGIN 1
1579 #define V_EDITMODE 2
1580 #define V_ISRCHTERM 3
1586 /* Forward declarations */
1587 static int sv_bell_style
PARAMS((const char *));
1588 static int sv_combegin
PARAMS((const char *));
1589 static int sv_dispprefix
PARAMS((const char *));
1590 static int sv_compquery
PARAMS((const char *));
1591 static int sv_compwidth
PARAMS((const char *));
1592 static int sv_editmode
PARAMS((const char *));
1593 static int sv_emacs_modestr
PARAMS((const char *));
1594 static int sv_histsize
PARAMS((const char *));
1595 static int sv_isrchterm
PARAMS((const char *));
1596 static int sv_keymap
PARAMS((const char *));
1597 static int sv_seqtimeout
PARAMS((const char *));
1598 static int sv_viins_modestr
PARAMS((const char *));
1599 static int sv_vicmd_modestr
PARAMS((const char *));
1601 static const struct {
1602 const char * const name
;
1604 _rl_sv_func_t
*set_func
;
1605 } string_varlist
[] = {
1606 { "bell-style", V_STRING
, sv_bell_style
},
1607 { "comment-begin", V_STRING
, sv_combegin
},
1608 { "completion-display-width", V_INT
, sv_compwidth
},
1609 { "completion-prefix-display-length", V_INT
, sv_dispprefix
},
1610 { "completion-query-items", V_INT
, sv_compquery
},
1611 { "editing-mode", V_STRING
, sv_editmode
},
1612 { "emacs-mode-string", V_STRING
, sv_emacs_modestr
},
1613 { "history-size", V_INT
, sv_histsize
},
1614 { "isearch-terminators", V_STRING
, sv_isrchterm
},
1615 { "keymap", V_STRING
, sv_keymap
},
1616 { "keyseq-timeout", V_INT
, sv_seqtimeout
},
1617 { "vi-cmd-mode-string", V_STRING
, sv_vicmd_modestr
},
1618 { "vi-ins-mode-string", V_STRING
, sv_viins_modestr
},
1619 { (char *)NULL
, 0, (_rl_sv_func_t
*)0 }
1623 find_string_var (name
)
1628 for (i
= 0; string_varlist
[i
].name
; i
++)
1629 if (_rl_stricmp (name
, string_varlist
[i
].name
) == 0)
1634 /* A boolean value that can appear in a `set variable' command is true if
1635 the value is null or empty, `on' (case-insensitive), or "1". Any other
1636 values result in 0 (false). */
1641 return (value
== 0 || *value
== '\0' ||
1642 (_rl_stricmp (value
, "on") == 0) ||
1643 (value
[0] == '1' && value
[1] == '\0'));
1647 rl_variable_value (name
)
1652 /* Check for simple variables first. */
1653 i
= find_boolean_var (name
);
1655 return (*boolean_varlist
[i
].value
? "on" : "off");
1657 i
= find_string_var (name
);
1659 return (_rl_get_string_variable_value (string_varlist
[i
].name
));
1661 /* Unknown variable names return NULL. */
1666 rl_variable_bind (name
, value
)
1667 const char *name
, *value
;
1672 /* Check for simple variables first. */
1673 i
= find_boolean_var (name
);
1676 *boolean_varlist
[i
].value
= bool_to_int (value
);
1677 if (boolean_varlist
[i
].flags
& V_SPECIAL
)
1678 hack_special_boolean_var (i
);
1682 i
= find_string_var (name
);
1684 /* For the time being, unknown variable names or string names without a
1685 handler function are simply ignored. */
1686 if (i
< 0 || string_varlist
[i
].set_func
== 0)
1689 v
= (*string_varlist
[i
].set_func
) (value
);
1697 if (_rl_strnicmp (value
, "vi", 2) == 0)
1699 #if defined (VI_MODE)
1700 _rl_keymap
= vi_insertion_keymap
;
1701 rl_editing_mode
= vi_mode
;
1702 #endif /* VI_MODE */
1705 else if (_rl_strnicmp (value
, "emacs", 5) == 0)
1707 _rl_keymap
= emacs_standard_keymap
;
1708 rl_editing_mode
= emacs_mode
;
1718 if (value
&& *value
)
1720 FREE (_rl_comment_begin
);
1721 _rl_comment_begin
= savestring (value
);
1728 sv_dispprefix (value
)
1733 if (value
&& *value
)
1735 nval
= atoi (value
);
1739 _rl_completion_prefix_display_length
= nval
;
1744 sv_compquery (value
)
1749 if (value
&& *value
)
1751 nval
= atoi (value
);
1755 rl_completion_query_items
= nval
;
1760 sv_compwidth (value
)
1765 if (value
&& *value
)
1766 nval
= atoi (value
);
1768 _rl_completion_columns
= nval
;
1779 if (value
&& *value
)
1781 nval
= atoi (value
);
1784 unstifle_history ();
1788 stifle_history (nval
);
1798 kmap
= rl_get_keymap_by_name (value
);
1801 rl_set_keymap (kmap
);
1808 sv_seqtimeout (value
)
1814 if (value
&& *value
)
1816 nval
= atoi (value
);
1820 _rl_keyseq_timeout
= nval
;
1825 sv_bell_style (value
)
1828 if (value
== 0 || *value
== '\0')
1829 _rl_bell_preference
= AUDIBLE_BELL
;
1830 else if (_rl_stricmp (value
, "none") == 0 || _rl_stricmp (value
, "off") == 0)
1831 _rl_bell_preference
= NO_BELL
;
1832 else if (_rl_stricmp (value
, "audible") == 0 || _rl_stricmp (value
, "on") == 0)
1833 _rl_bell_preference
= AUDIBLE_BELL
;
1834 else if (_rl_stricmp (value
, "visible") == 0)
1835 _rl_bell_preference
= VISIBLE_BELL
;
1842 sv_isrchterm (value
)
1845 int beg
, end
, delim
;
1851 /* Isolate the value and translate it into a character string. */
1852 v
= savestring (value
);
1853 FREE (_rl_isearch_terminators
);
1854 if (v
[0] == '"' || v
[0] == '\'')
1857 for (beg
= end
= 1; v
[end
] && v
[end
] != delim
; end
++)
1862 for (beg
= end
= 0; v
[end
] && whitespace (v
[end
]) == 0; end
++)
1868 /* The value starts at v + beg. Translate it into a character string. */
1869 _rl_isearch_terminators
= (char *)xmalloc (2 * strlen (v
) + 1);
1870 rl_translate_keyseq (v
+ beg
, _rl_isearch_terminators
, &end
);
1871 _rl_isearch_terminators
[end
] = '\0';
1877 extern char *_rl_emacs_mode_str
;
1880 sv_emacs_modestr (value
)
1883 if (value
&& *value
)
1885 FREE (_rl_emacs_mode_str
);
1886 _rl_emacs_mode_str
= (char *)xmalloc (2 * strlen (value
) + 1);
1887 rl_translate_keyseq (value
, _rl_emacs_mode_str
, &_rl_emacs_modestr_len
);
1888 _rl_emacs_mode_str
[_rl_emacs_modestr_len
] = '\0';
1893 FREE (_rl_emacs_mode_str
);
1894 _rl_emacs_mode_str
= (char *)xmalloc (1);
1895 _rl_emacs_mode_str
[_rl_emacs_modestr_len
= 0] = '\0';
1898 else if (value
== 0)
1900 FREE (_rl_emacs_mode_str
);
1901 _rl_emacs_mode_str
= 0; /* prompt_modestr does the right thing */
1902 _rl_emacs_modestr_len
= 0;
1909 sv_viins_modestr (value
)
1912 if (value
&& *value
)
1914 FREE (_rl_vi_ins_mode_str
);
1915 _rl_vi_ins_mode_str
= (char *)xmalloc (2 * strlen (value
) + 1);
1916 rl_translate_keyseq (value
, _rl_vi_ins_mode_str
, &_rl_vi_ins_modestr_len
);
1917 _rl_vi_ins_mode_str
[_rl_vi_ins_modestr_len
] = '\0';
1922 FREE (_rl_vi_ins_mode_str
);
1923 _rl_vi_ins_mode_str
= (char *)xmalloc (1);
1924 _rl_vi_ins_mode_str
[_rl_vi_ins_modestr_len
= 0] = '\0';
1927 else if (value
== 0)
1929 FREE (_rl_vi_ins_mode_str
);
1930 _rl_vi_ins_mode_str
= 0; /* prompt_modestr does the right thing */
1931 _rl_vi_ins_modestr_len
= 0;
1938 sv_vicmd_modestr (value
)
1941 if (value
&& *value
)
1943 FREE (_rl_vi_cmd_mode_str
);
1944 _rl_vi_cmd_mode_str
= (char *)xmalloc (2 * strlen (value
) + 1);
1945 rl_translate_keyseq (value
, _rl_vi_cmd_mode_str
, &_rl_vi_cmd_modestr_len
);
1946 _rl_vi_cmd_mode_str
[_rl_vi_cmd_modestr_len
] = '\0';
1951 FREE (_rl_vi_cmd_mode_str
);
1952 _rl_vi_cmd_mode_str
= (char *)xmalloc (1);
1953 _rl_vi_cmd_mode_str
[_rl_vi_cmd_modestr_len
= 0] = '\0';
1956 else if (value
== 0)
1958 FREE (_rl_vi_cmd_mode_str
);
1959 _rl_vi_cmd_mode_str
= 0; /* prompt_modestr does the right thing */
1960 _rl_vi_cmd_modestr_len
= 0;
1966 /* Return the character which matches NAME.
1967 For example, `Space' returns ' '. */
1970 const char * const name
;
1974 static const assoc_list name_key_alist
[] = {
1977 { "Escape", '\033' },
1979 { "Newline", '\n' },
1990 glean_key_from_name (name
)
1995 for (i
= 0; name_key_alist
[i
].name
; i
++)
1996 if (_rl_stricmp (name
, name_key_alist
[i
].name
) == 0)
1997 return (name_key_alist
[i
].value
);
1999 return (*(unsigned char *)name
); /* XXX was return (*name) */
2002 /* Auxiliary functions to manage keymaps. */
2003 static const struct {
2004 const char * const name
;
2006 } keymap_names
[] = {
2007 { "emacs", emacs_standard_keymap
},
2008 { "emacs-standard", emacs_standard_keymap
},
2009 { "emacs-meta", emacs_meta_keymap
},
2010 { "emacs-ctlx", emacs_ctlx_keymap
},
2011 #if defined (VI_MODE)
2012 { "vi", vi_movement_keymap
},
2013 { "vi-move", vi_movement_keymap
},
2014 { "vi-command", vi_movement_keymap
},
2015 { "vi-insert", vi_insertion_keymap
},
2016 #endif /* VI_MODE */
2017 { (char *)0x0, (Keymap
)0x0 }
2021 rl_get_keymap_by_name (name
)
2026 for (i
= 0; keymap_names
[i
].name
; i
++)
2027 if (_rl_stricmp (name
, keymap_names
[i
].name
) == 0)
2028 return (keymap_names
[i
].map
);
2029 return ((Keymap
) NULL
);
2033 rl_get_keymap_name (map
)
2037 for (i
= 0; keymap_names
[i
].name
; i
++)
2038 if (map
== keymap_names
[i
].map
)
2039 return ((char *)keymap_names
[i
].name
);
2040 return ((char *)NULL
);
2054 return (_rl_keymap
);
2058 rl_set_keymap_from_edit_mode ()
2060 if (rl_editing_mode
== emacs_mode
)
2061 _rl_keymap
= emacs_standard_keymap
;
2062 #if defined (VI_MODE)
2063 else if (rl_editing_mode
== vi_mode
)
2064 _rl_keymap
= vi_insertion_keymap
;
2065 #endif /* VI_MODE */
2069 rl_get_keymap_name_from_edit_mode ()
2071 if (rl_editing_mode
== emacs_mode
)
2073 #if defined (VI_MODE)
2074 else if (rl_editing_mode
== vi_mode
)
2076 #endif /* VI_MODE */
2081 /* **************************************************************** */
2083 /* Key Binding and Function Information */
2085 /* **************************************************************** */
2087 /* Each of the following functions produces information about the
2088 state of keybindings and functions known to Readline. The info
2089 is always printed to rl_outstream, and in such a way that it can
2090 be read back in (i.e., passed to rl_parse_and_bind ()). */
2092 /* Print the names of functions known to Readline. */
2094 rl_list_funmap_names ()
2097 const char **funmap_names
;
2099 funmap_names
= rl_funmap_names ();
2104 for (i
= 0; funmap_names
[i
]; i
++)
2105 fprintf (rl_outstream
, "%s\n", funmap_names
[i
]);
2107 xfree (funmap_names
);
2111 _rl_get_keyname (key
)
2117 keyname
= (char *)xmalloc (8);
2120 /* Since this is going to be used to write out keysequence-function
2121 pairs for possible inclusion in an inputrc file, we don't want to
2122 do any special meta processing on KEY. */
2125 /* XXX - Experimental */
2126 /* We might want to do this, but the old version of the code did not. */
2128 /* If this is an escape character, we don't want to do any more processing.
2129 Just add the special ESC key sequence and return. */
2139 /* RUBOUT is translated directly into \C-? */
2151 /* Now add special prefixes needed for control characters. This can
2152 potentially change C. */
2155 keyname
[i
++] = '\\';
2158 c
= _rl_to_lower (UNCTRL (c
));
2161 /* XXX experimental code. Turn the characters that are not ASCII or
2162 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
2164 if (c
>= 128 && c
<= 159)
2166 keyname
[i
++] = '\\';
2169 keyname
[i
++] = (c
/ 8) + '0';
2173 /* Now, if the character needs to be quoted with a backslash, do that. */
2174 if (c
== '\\' || c
== '"')
2175 keyname
[i
++] = '\\';
2177 /* Now add the key, terminate the string, and return it. */
2178 keyname
[i
++] = (char) c
;
2184 /* Return a NULL terminated array of strings which represent the key
2185 sequences that are used to invoke FUNCTION in MAP. */
2187 rl_invoking_keyseqs_in_map (function
, map
)
2188 rl_command_func_t
*function
;
2193 int result_index
, result_size
;
2195 result
= (char **)NULL
;
2196 result_index
= result_size
= 0;
2198 for (key
= 0; key
< KEYMAP_SIZE
; key
++)
2200 switch (map
[key
].type
)
2203 /* Macros match, if, and only if, the pointers are identical.
2204 Thus, they are treated exactly like functions in here. */
2206 /* If the function in the keymap is the one we are looking for,
2207 then add the current KEY to the list of invoking keys. */
2208 if (map
[key
].function
== function
)
2212 keyname
= _rl_get_keyname (key
);
2214 if (result_index
+ 2 > result_size
)
2217 result
= (char **)xrealloc (result
, result_size
* sizeof (char *));
2220 result
[result_index
++] = keyname
;
2221 result
[result_index
] = (char *)NULL
;
2230 /* Find the list of keyseqs in this map which have FUNCTION as
2231 their target. Add the key sequences found to RESULT. */
2232 if (map
[key
].function
)
2234 rl_invoking_keyseqs_in_map (function
, FUNCTION_TO_KEYMAP (map
, key
));
2241 for (i
= 0; seqs
[i
]; i
++)
2243 char *keyname
= (char *)xmalloc (6 + strlen (seqs
[i
]));
2247 /* If ESC is the meta prefix and we're converting chars
2248 with the eighth bit set to ESC-prefixed sequences, then
2249 we can use \M-. Otherwise we need to use the sequence
2251 if (_rl_convert_meta_chars_to_ascii
&& map
[ESC
].type
== ISKMAP
)
2252 sprintf (keyname
, "\\M-");
2254 sprintf (keyname
, "\\e");
2256 else if (CTRL_CHAR (key
))
2257 sprintf (keyname
, "\\C-%c", _rl_to_lower (UNCTRL (key
)));
2258 else if (key
== RUBOUT
)
2259 sprintf (keyname
, "\\C-?");
2260 else if (key
== '\\' || key
== '"')
2263 keyname
[1] = (char) key
;
2268 keyname
[0] = (char) key
;
2272 strcat (keyname
, seqs
[i
]);
2275 if (result_index
+ 2 > result_size
)
2278 result
= (char **)xrealloc (result
, result_size
* sizeof (char *));
2281 result
[result_index
++] = keyname
;
2282 result
[result_index
] = (char *)NULL
;
2293 /* Return a NULL terminated array of strings which represent the key
2294 sequences that can be used to invoke FUNCTION using the current keymap. */
2296 rl_invoking_keyseqs (function
)
2297 rl_command_func_t
*function
;
2299 return (rl_invoking_keyseqs_in_map (function
, _rl_keymap
));
2302 /* Print all of the functions and their bindings to rl_outstream. If
2303 PRINT_READABLY is non-zero, then print the output in such a way
2304 that it can be read back in. */
2306 rl_function_dumper (print_readably
)
2313 names
= rl_funmap_names ();
2315 fprintf (rl_outstream
, "\n");
2317 for (i
= 0; name
= names
[i
]; i
++)
2319 rl_command_func_t
*function
;
2322 function
= rl_named_function (name
);
2323 invokers
= rl_invoking_keyseqs_in_map (function
, _rl_keymap
);
2328 fprintf (rl_outstream
, "# %s (not bound)\n", name
);
2333 for (j
= 0; invokers
[j
]; j
++)
2335 fprintf (rl_outstream
, "\"%s\": %s\n",
2337 xfree (invokers
[j
]);
2346 fprintf (rl_outstream
, "%s is not bound to any keys\n",
2352 fprintf (rl_outstream
, "%s can be found on ", name
);
2354 for (j
= 0; invokers
[j
] && j
< 5; j
++)
2356 fprintf (rl_outstream
, "\"%s\"%s", invokers
[j
],
2357 invokers
[j
+ 1] ? ", " : ".\n");
2360 if (j
== 5 && invokers
[j
])
2361 fprintf (rl_outstream
, "...\n");
2363 for (j
= 0; invokers
[j
]; j
++)
2364 xfree (invokers
[j
]);
2374 /* Print all of the current functions and their bindings to
2375 rl_outstream. If an explicit argument is given, then print
2376 the output in such a way that it can be read back in. */
2378 rl_dump_functions (count
, key
)
2382 fprintf (rl_outstream
, "\r\n");
2383 rl_function_dumper (rl_explicit_arg
);
2389 _rl_macro_dumper_internal (print_readably
, map
, prefix
)
2395 char *keyname
, *out
;
2398 for (key
= 0; key
< KEYMAP_SIZE
; key
++)
2400 switch (map
[key
].type
)
2403 keyname
= _rl_get_keyname (key
);
2404 out
= _rl_untranslate_macro_value ((char *)map
[key
].function
, 0);
2407 fprintf (rl_outstream
, "\"%s%s\": \"%s\"\n", prefix
? prefix
: "",
2411 fprintf (rl_outstream
, "%s%s outputs %s\n", prefix
? prefix
: "",
2420 prefix_len
= prefix
? strlen (prefix
) : 0;
2423 keyname
= (char *)xmalloc (3 + prefix_len
);
2425 strcpy (keyname
, prefix
);
2426 keyname
[prefix_len
] = '\\';
2427 keyname
[prefix_len
+ 1] = 'e';
2428 keyname
[prefix_len
+ 2] = '\0';
2432 keyname
= _rl_get_keyname (key
);
2435 out
= (char *)xmalloc (strlen (keyname
) + prefix_len
+ 1);
2436 strcpy (out
, prefix
);
2437 strcpy (out
+ prefix_len
, keyname
);
2443 _rl_macro_dumper_internal (print_readably
, FUNCTION_TO_KEYMAP (map
, key
), keyname
);
2451 rl_macro_dumper (print_readably
)
2454 _rl_macro_dumper_internal (print_readably
, _rl_keymap
, (char *)NULL
);
2458 rl_dump_macros (count
, key
)
2462 fprintf (rl_outstream
, "\r\n");
2463 rl_macro_dumper (rl_explicit_arg
);
2469 _rl_get_string_variable_value (name
)
2472 static char numbuf
[32];
2475 if (_rl_stricmp (name
, "bell-style") == 0)
2477 switch (_rl_bell_preference
)
2488 else if (_rl_stricmp (name
, "comment-begin") == 0)
2489 return (_rl_comment_begin
? _rl_comment_begin
: RL_COMMENT_BEGIN_DEFAULT
);
2490 else if (_rl_stricmp (name
, "completion-display-width") == 0)
2492 sprintf (numbuf
, "%d", _rl_completion_columns
);
2495 else if (_rl_stricmp (name
, "completion-prefix-display-length") == 0)
2497 sprintf (numbuf
, "%d", _rl_completion_prefix_display_length
);
2500 else if (_rl_stricmp (name
, "completion-query-items") == 0)
2502 sprintf (numbuf
, "%d", rl_completion_query_items
);
2505 else if (_rl_stricmp (name
, "editing-mode") == 0)
2506 return (rl_get_keymap_name_from_edit_mode ());
2507 else if (_rl_stricmp (name
, "history-size") == 0)
2509 sprintf (numbuf
, "%d", history_is_stifled() ? history_max_entries
: 0);
2512 else if (_rl_stricmp (name
, "isearch-terminators") == 0)
2514 if (_rl_isearch_terminators
== 0)
2516 ret
= _rl_untranslate_macro_value (_rl_isearch_terminators
, 0);
2519 strncpy (numbuf
, ret
, sizeof (numbuf
) - 1);
2521 numbuf
[sizeof(numbuf
) - 1] = '\0';
2527 else if (_rl_stricmp (name
, "keymap") == 0)
2529 ret
= rl_get_keymap_name (_rl_keymap
);
2531 ret
= rl_get_keymap_name_from_edit_mode ();
2532 return (ret
? ret
: "none");
2534 else if (_rl_stricmp (name
, "keyseq-timeout") == 0)
2536 sprintf (numbuf
, "%d", _rl_keyseq_timeout
);
2539 else if (_rl_stricmp (name
, "emacs-mode-string") == 0)
2540 return (_rl_emacs_mode_str
? _rl_emacs_mode_str
: RL_EMACS_MODESTR_DEFAULT
);
2541 else if (_rl_stricmp (name
, "vi-cmd-mode-string") == 0)
2542 return (_rl_emacs_mode_str
? _rl_emacs_mode_str
: RL_VI_CMD_MODESTR_DEFAULT
);
2543 else if (_rl_stricmp (name
, "vi-ins-mode-string") == 0)
2544 return (_rl_emacs_mode_str
? _rl_emacs_mode_str
: RL_VI_INS_MODESTR_DEFAULT
);
2550 rl_variable_dumper (print_readably
)
2556 for (i
= 0; boolean_varlist
[i
].name
; i
++)
2559 fprintf (rl_outstream
, "set %s %s\n", boolean_varlist
[i
].name
,
2560 *boolean_varlist
[i
].value
? "on" : "off");
2562 fprintf (rl_outstream
, "%s is set to `%s'\n", boolean_varlist
[i
].name
,
2563 *boolean_varlist
[i
].value
? "on" : "off");
2566 for (i
= 0; string_varlist
[i
].name
; i
++)
2568 v
= _rl_get_string_variable_value (string_varlist
[i
].name
);
2569 if (v
== 0) /* _rl_isearch_terminators can be NULL */
2572 fprintf (rl_outstream
, "set %s %s\n", string_varlist
[i
].name
, v
);
2574 fprintf (rl_outstream
, "%s is set to `%s'\n", string_varlist
[i
].name
, v
);
2578 /* Print all of the current variables and their values to
2579 rl_outstream. If an explicit argument is given, then print
2580 the output in such a way that it can be read back in. */
2582 rl_dump_variables (count
, key
)
2586 fprintf (rl_outstream
, "\r\n");
2587 rl_variable_dumper (rl_explicit_arg
);
2592 /* Return non-zero if any members of ARRAY are a substring in STRING. */
2594 substring_member_of_array (string
, array
)
2596 const char * const *array
;
2600 if (_rl_strindex (string
, *array
))