bd899ca9f88379cabdb7655d9738ee16e3583112
[platform/upstream/bash.git] / lib / readline / bind.c
1 /* bind.c -- key binding and startup file support for the readline library. */
2
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library, a library for
6    reading lines of text with interactive input and history editing.
7
8    The GNU Readline Library is free software; you can redistribute it
9    and/or modify it under the terms of the GNU General Public License
10    as published by the Free Software Foundation; either version 1, or
11    (at your option) any later version.
12
13    The GNU Readline Library is distributed in the hope that it will be
14    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    The GNU General Public License is often shipped with GNU software, and
19    is generally kept in a file called COPYING or LICENSE.  If you do not
20    have a copy of the license, write to the Free Software Foundation,
21    675 Mass Ave, Cambridge, MA 02139, USA. */
22 #define READLINE_LIBRARY
23
24 #if defined (HAVE_CONFIG_H)
25 #  include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
31 #if defined (HAVE_SYS_FILE_H)
32 #  include <sys/file.h>
33 #endif /* HAVE_SYS_FILE_H */
34
35 #if defined (HAVE_UNISTD_H)
36 #  include <unistd.h>
37 #endif /* HAVE_UNISTD_H */
38
39 #if defined (HAVE_STDLIB_H)
40 #  include <stdlib.h>
41 #else
42 #  include "ansi_stdlib.h"
43 #endif /* HAVE_STDLIB_H */
44
45 #include <signal.h>
46 #include <errno.h>
47
48 #if !defined (errno)
49 extern int errno;
50 #endif /* !errno */
51
52 #include "posixstat.h"
53
54 /* System-specific feature definitions and include files. */
55 #include "rldefs.h"
56
57 /* Some standard library routines. */
58 #include "readline.h"
59 #include "history.h"
60
61 #if !defined (strchr) && !defined (__STDC__)
62 extern char *strchr (), *strrchr ();
63 #endif /* !strchr && !__STDC__ */
64
65 extern int _rl_horizontal_scroll_mode;
66 extern int _rl_mark_modified_lines;
67 extern int _rl_bell_preference;
68 extern int _rl_meta_flag;
69 extern int _rl_convert_meta_chars_to_ascii;
70 extern int _rl_output_meta_chars;
71 extern int _rl_complete_show_all;
72 extern int _rl_complete_mark_directories;
73 extern int _rl_enable_keypad;
74 #if defined (PAREN_MATCHING)
75 extern int rl_blink_matching_paren;
76 #endif /* PAREN_MATCHING */
77 #if defined (VISIBLE_STATS)
78 extern int rl_visible_stats;
79 #endif /* VISIBLE_STATS */
80 extern int rl_complete_with_tilde_expansion;
81 extern int rl_completion_query_items;
82 extern int rl_inhibit_completion;
83 extern char *_rl_comment_begin;
84
85 extern int rl_explicit_arg;
86 extern int rl_editing_mode;
87 extern unsigned char _rl_parsing_conditionalized_out;
88 extern Keymap _rl_keymap;
89
90 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
91
92 extern char **rl_funmap_names ();
93 extern int rl_add_funmap_entry ();
94
95 extern char *_rl_strindex ();
96
97 /* Variables exported by this file. */
98 Keymap rl_binding_keymap;
99
100 /* Forward declarations */
101 void rl_set_keymap_from_edit_mode ();
102
103 static int glean_key_from_name ();
104 static int substring_member_of_array ();
105
106 extern char *xmalloc (), *xrealloc ();
107
108 /* **************************************************************** */
109 /*                                                                  */
110 /*                      Binding keys                                */
111 /*                                                                  */
112 /* **************************************************************** */
113
114 /* rl_add_defun (char *name, Function *function, int key)
115    Add NAME to the list of named functions.  Make FUNCTION be the function
116    that gets called.  If KEY is not -1, then bind it. */
117 int
118 rl_add_defun (name, function, key)
119      char *name;
120      Function *function;
121      int key;
122 {
123   if (key != -1)
124     rl_bind_key (key, function);
125   rl_add_funmap_entry (name, function);
126   return 0;
127 }
128
129 /* Bind KEY to FUNCTION.  Returns non-zero if KEY is out of range. */
130 int
131 rl_bind_key (key, function)
132      int key;
133      Function *function;
134 {
135   if (key < 0)
136     return (key);
137
138   if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
139     {
140       if (_rl_keymap[ESC].type == ISKMAP)
141         {
142           Keymap escmap;
143
144           escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
145           key = UNMETA (key);
146           escmap[key].type = ISFUNC;
147           escmap[key].function = function;
148           return (0);
149         }
150       return (key);
151     }
152
153   _rl_keymap[key].type = ISFUNC;
154   _rl_keymap[key].function = function;
155   rl_binding_keymap = _rl_keymap;
156   return (0);
157 }
158
159 /* Bind KEY to FUNCTION in MAP.  Returns non-zero in case of invalid
160    KEY. */
161 int
162 rl_bind_key_in_map (key, function, map)
163      int key;
164      Function *function;
165      Keymap map;
166 {
167   int result;
168   Keymap oldmap;
169
170   oldmap = _rl_keymap;
171   _rl_keymap = map;
172   result = rl_bind_key (key, function);
173   _rl_keymap = oldmap;
174   return (result);
175 }
176
177 /* Make KEY do nothing in the currently selected keymap.
178    Returns non-zero in case of error. */
179 int
180 rl_unbind_key (key)
181      int key;
182 {
183   return (rl_bind_key (key, (Function *)NULL));
184 }
185
186 /* Make KEY do nothing in MAP.
187    Returns non-zero in case of error. */
188 int
189 rl_unbind_key_in_map (key, map)
190      int key;
191      Keymap map;
192 {
193   return (rl_bind_key_in_map (key, (Function *)NULL, map));
194 }
195
196 /* Bind the key sequence represented by the string KEYSEQ to
197    FUNCTION.  This makes new keymaps as necessary.  The initial
198    place to do bindings is in MAP. */
199 int
200 rl_set_key (keyseq, function, map)
201      char *keyseq;
202      Function *function;
203      Keymap map;
204 {
205   return (rl_generic_bind (ISFUNC, keyseq, function, map));
206 }
207
208 /* Bind the key sequence represented by the string KEYSEQ to
209    the string of characters MACRO.  This makes new keymaps as
210    necessary.  The initial place to do bindings is in MAP. */
211 int
212 rl_macro_bind (keyseq, macro, map)
213      char *keyseq, *macro;
214      Keymap map;
215 {
216   char *macro_keys;
217   int macro_keys_len;
218
219   macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
220
221   if (rl_translate_keyseq (macro, macro_keys, &macro_keys_len))
222     {
223       free (macro_keys);
224       return -1;
225     }
226   rl_generic_bind (ISMACR, keyseq, macro_keys, map);
227   return 0;
228 }
229
230 /* Bind the key sequence represented by the string KEYSEQ to
231    the arbitrary pointer DATA.  TYPE says what kind of data is
232    pointed to by DATA, right now this can be a function (ISFUNC),
233    a macro (ISMACR), or a keymap (ISKMAP).  This makes new keymaps
234    as necessary.  The initial place to do bindings is in MAP. */
235 int
236 rl_generic_bind (type, keyseq, data, map)
237      int type;
238      char *keyseq, *data;
239      Keymap map;
240 {
241   char *keys;
242   int keys_len;
243   register int i;
244
245   /* If no keys to bind to, exit right away. */
246   if (!keyseq || !*keyseq)
247     {
248       if (type == ISMACR)
249         free (data);
250       return -1;
251     }
252
253   keys = xmalloc (1 + (2 * strlen (keyseq)));
254
255   /* Translate the ASCII representation of KEYSEQ into an array of
256      characters.  Stuff the characters into KEYS, and the length of
257      KEYS into KEYS_LEN. */
258   if (rl_translate_keyseq (keyseq, keys, &keys_len))
259     {
260       free (keys);
261       return -1;
262     }
263
264   /* Bind keys, making new keymaps as necessary. */
265   for (i = 0; i < keys_len; i++)
266     {
267       int ic = (int) ((unsigned char)keys[i]);
268
269       if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
270         {
271           ic = UNMETA (ic);
272           if (map[ESC].type == ISKMAP)
273             map = FUNCTION_TO_KEYMAP (map, ESC);
274         }
275
276       if ((i + 1) < keys_len)
277         {
278           if (map[ic].type != ISKMAP)
279             {
280               if (map[ic].type == ISMACR)
281                 free ((char *)map[ic].function);
282
283               map[ic].type = ISKMAP;
284               map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
285             }
286           map = FUNCTION_TO_KEYMAP (map, ic);
287         }
288       else
289         {
290           if (map[ic].type == ISMACR)
291             free ((char *)map[ic].function);
292
293           map[ic].function = KEYMAP_TO_FUNCTION (data);
294           map[ic].type = type;
295         }
296
297       rl_binding_keymap = map;
298     }
299   free (keys);
300   return 0;
301 }
302
303 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
304    an array of characters.  LEN gets the final length of ARRAY.  Return
305    non-zero if there was an error parsing SEQ. */
306 int
307 rl_translate_keyseq (seq, array, len)
308      char *seq, *array;
309      int *len;
310 {
311   register int i, c, l;
312
313   for (i = l = 0; c = seq[i]; i++)
314     {
315       if (c == '\\')
316         {
317           c = seq[++i];
318
319           if (c == 0)
320             break;
321
322           if (((c == 'C' || c == 'M') && seq[i + 1] == '-') || (c == 'e'))
323             {
324               /* Handle special case of backwards define. */
325               if (strncmp (&seq[i], "C-\\M-", 5) == 0)
326                 {
327                   array[l++] = ESC;
328                   i += 5;
329                   array[l++] = CTRL (_rl_to_upper (seq[i]));
330                   if (!seq[i])
331                     i--;
332                   continue;
333                 }
334
335               switch (c)
336                 {
337                 case 'M':
338                   i++;
339                   array[l++] = ESC;     /* XXX */
340                   break;
341
342                 case 'C':
343                   i += 2;
344                   /* Special hack for C-?... */
345                   array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
346                   break;
347
348                 case 'e':
349                   array[l++] = ESC;
350                 }
351
352               continue;
353             }
354         }
355       array[l++] = c;
356     }
357
358   *len = l;
359   array[l] = '\0';
360   return (0);
361 }
362
363 char *
364 rl_untranslate_keyseq (seq)
365      int seq;
366 {
367   static char kseq[16];
368   int i, c;
369
370   i = 0;
371   c = seq;
372   if (META_CHAR (c))
373     {
374       kseq[i++] = '\\';
375       kseq[i++] = 'M';
376       kseq[i++] = '-';
377       c = UNMETA (c);
378     }
379   else if (CTRL_CHAR (c))
380     {
381       kseq[i++] = '\\';
382       kseq[i++] = 'C';
383       kseq[i++] = '-';
384       c = UNCTRL (c);
385     }
386   else if (c == RUBOUT)
387     {
388       kseq[i++] = '\\';
389       kseq[i++] = 'C';
390       kseq[i++] = '-';
391       c = '?';
392     }
393
394   if (c == ESC)
395     {
396       kseq[i++] = '\\';
397       kseq[i++] = 'e';
398     }
399   else if (c == '\\' || c == '"')
400     {
401       kseq[i++] = '\\';
402     }
403
404   kseq[i++] = (unsigned char) c;
405   kseq[i] = '\0';
406   return kseq;
407 }
408
409 /* Return a pointer to the function that STRING represents.
410    If STRING doesn't have a matching function, then a NULL pointer
411    is returned. */
412 Function *
413 rl_named_function (string)
414      char *string;
415 {
416   register int i;
417
418   rl_initialize_funmap ();
419
420   for (i = 0; funmap[i]; i++)
421     if (_rl_stricmp (funmap[i]->name, string) == 0)
422       return (funmap[i]->function);
423   return ((Function *)NULL);
424 }
425
426 /* Return the function (or macro) definition which would be invoked via
427    KEYSEQ if executed in MAP.  If MAP is NULL, then the current keymap is
428    used.  TYPE, if non-NULL, is a pointer to an int which will receive the
429    type of the object pointed to.  One of ISFUNC (function), ISKMAP (keymap),
430    or ISMACR (macro). */
431 Function *
432 rl_function_of_keyseq (keyseq, map, type)
433      char *keyseq;
434      Keymap map;
435      int *type;
436 {
437   register int i;
438
439   if (!map)
440     map = _rl_keymap;
441
442   for (i = 0; keyseq && keyseq[i]; i++)
443     {
444       int ic = keyseq[i];
445
446       if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
447         {
448           if (map[ESC].type != ISKMAP)
449             {
450               if (type)
451                 *type = map[ESC].type;
452
453               return (map[ESC].function);
454             }
455           else
456             {
457               map = FUNCTION_TO_KEYMAP (map, ESC);
458               ic = UNMETA (ic);
459             }
460         }
461
462       if (map[ic].type == ISKMAP)
463         {
464           /* If this is the last key in the key sequence, return the
465              map. */
466           if (!keyseq[i + 1])
467             {
468               if (type)
469                 *type = ISKMAP;
470
471               return (map[ic].function);
472             }
473           else
474             map = FUNCTION_TO_KEYMAP (map, ic);
475         }
476       else
477         {
478           if (type)
479             *type = map[ic].type;
480
481           return (map[ic].function);
482         }
483     }
484   return ((Function *) NULL);
485 }
486
487 /* The last key bindings file read. */
488 static char *last_readline_init_file = (char *)NULL;
489
490 /* The file we're currently reading key bindings from. */
491 static char *current_readline_init_file;
492 static int current_readline_init_lineno;
493
494 /* Re-read the current keybindings file. */
495 int
496 rl_re_read_init_file (count, ignore)
497      int count, ignore;
498 {
499   int r;
500   r = rl_read_init_file ((char *)NULL);
501   rl_set_keymap_from_edit_mode ();
502   return r;
503 }
504
505 /* Do key bindings from a file.  If FILENAME is NULL it defaults
506    to the first non-null filename from this list:
507      1. the filename used for the previous call
508      2. the value of the shell variable `INPUTRC'
509      3. ~/.inputrc
510    If the file existed and could be opened and read, 0 is returned,
511    otherwise errno is returned. */
512 int
513 rl_read_init_file (filename)
514      char *filename;
515 {
516   register int i;
517   char *buffer, *openname, *line, *end;
518   struct stat finfo;
519   int file;
520
521   /* Default the filename. */
522   if (filename == 0)
523     {
524       filename = last_readline_init_file;
525       if (filename == 0)
526         filename = getenv ("INPUTRC");
527       if (filename == 0)
528         filename = DEFAULT_INPUTRC;
529     }
530
531   if (*filename == 0)
532     filename = DEFAULT_INPUTRC;
533
534   current_readline_init_file = filename;
535   openname = tilde_expand (filename);
536
537   if ((stat (openname, &finfo) < 0) ||
538       (file = open (openname, O_RDONLY, 0666)) < 0)
539     {
540       free (openname);
541       return (errno);
542     }
543   else
544     free (openname);
545
546   if (filename != last_readline_init_file)
547     {
548       if (last_readline_init_file)
549         free (last_readline_init_file);
550
551       last_readline_init_file = savestring (filename);
552     }
553
554   /* Read the file into BUFFER. */
555   buffer = (char *)xmalloc ((int)finfo.st_size + 1);
556   i = read (file, buffer, finfo.st_size);
557   close (file);
558
559   if (i != finfo.st_size)
560     return (errno);
561
562   /* Loop over the lines in the file.  Lines that start with `#' are
563      comments; all other lines are commands for readline initialization. */
564   current_readline_init_lineno = 1;
565   line = buffer;
566   end = buffer + finfo.st_size;
567   while (line < end)
568     {
569       /* Find the end of this line. */
570       for (i = 0; line + i != end && line[i] != '\n'; i++);
571
572       /* Mark end of line. */
573       line[i] = '\0';
574
575       /* Skip leading whitespace. */
576       while (*line && whitespace (*line))
577         {
578           line++;
579           i--;
580         }
581
582       /* If the line is not a comment, then parse it. */
583       if (*line && *line != '#')
584         rl_parse_and_bind (line);
585
586       /* Move to the next line. */
587       line += i + 1;
588       current_readline_init_lineno++;
589     }
590   free (buffer);
591   return (0);
592 }
593
594 static void
595 _rl_init_file_error (msg)
596      char *msg;
597 {
598   fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
599                    current_readline_init_lineno,
600                    msg);
601 }
602
603 /* **************************************************************** */
604 /*                                                                  */
605 /*                      Parser Directives                           */
606 /*                                                                  */
607 /* **************************************************************** */
608
609 /* Conditionals. */
610
611 /* Calling programs set this to have their argv[0]. */
612 char *rl_readline_name = "other";
613
614 /* Stack of previous values of parsing_conditionalized_out. */
615 static unsigned char *if_stack = (unsigned char *)NULL;
616 static int if_stack_depth;
617 static int if_stack_size;
618
619 /* Push _rl_parsing_conditionalized_out, and set parser state based
620    on ARGS. */
621 static int
622 parser_if (args)
623      char *args;
624 {
625   register int i;
626
627   /* Push parser state. */
628   if (if_stack_depth + 1 >= if_stack_size)
629     {
630       if (!if_stack)
631         if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
632       else
633         if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
634     }
635   if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
636
637   /* If parsing is turned off, then nothing can turn it back on except
638      for finding the matching endif.  In that case, return right now. */
639   if (_rl_parsing_conditionalized_out)
640     return 0;
641
642   /* Isolate first argument. */
643   for (i = 0; args[i] && !whitespace (args[i]); i++);
644
645   if (args[i])
646     args[i++] = '\0';
647
648   /* Handle "if term=foo" and "if mode=emacs" constructs.  If this
649      isn't term=foo, or mode=emacs, then check to see if the first
650      word in ARGS is the same as the value stored in rl_readline_name. */
651   if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
652     {
653       char *tem, *tname;
654
655       /* Terminals like "aaa-60" are equivalent to "aaa". */
656       tname = savestring (rl_terminal_name);
657       tem = strchr (tname, '-');
658       if (tem)
659         *tem = '\0';
660
661       /* Test the `long' and `short' forms of the terminal name so that
662          if someone has a `sun-cmd' and does not want to have bindings
663          that will be executed if the terminal is a `sun', they can put
664          `$if term=sun-cmd' into their .inputrc. */
665       _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
666                                         _rl_stricmp (args + 5, rl_terminal_name);
667       free (tname);
668     }
669 #if defined (VI_MODE)
670   else if (_rl_strnicmp (args, "mode=", 5) == 0)
671     {
672       int mode;
673
674       if (_rl_stricmp (args + 5, "emacs") == 0)
675         mode = emacs_mode;
676       else if (_rl_stricmp (args + 5, "vi") == 0)
677         mode = vi_mode;
678       else
679         mode = no_mode;
680
681       _rl_parsing_conditionalized_out = mode != rl_editing_mode;
682     }
683 #endif /* VI_MODE */
684   /* Check to see if the first word in ARGS is the same as the
685      value stored in rl_readline_name. */
686   else if (_rl_stricmp (args, rl_readline_name) == 0)
687     _rl_parsing_conditionalized_out = 0;
688   else
689     _rl_parsing_conditionalized_out = 1;
690   return 0;
691 }
692
693 /* Invert the current parser state if there is anything on the stack. */
694 static int
695 parser_else (args)
696      char *args;
697 {
698   register int i;
699
700   if (!if_stack_depth)
701     {
702       /* Error message? */
703       return 0;
704     }
705
706   /* Check the previous (n - 1) levels of the stack to make sure that
707      we haven't previously turned off parsing. */
708   for (i = 0; i < if_stack_depth - 1; i++)
709     if (if_stack[i] == 1)
710       return 0;
711
712   /* Invert the state of parsing if at top level. */
713   _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
714   return 0;
715 }
716
717 /* Terminate a conditional, popping the value of
718    _rl_parsing_conditionalized_out from the stack. */
719 static int
720 parser_endif (args)
721      char *args;
722 {
723   if (if_stack_depth)
724     _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
725   else
726     {
727       /* *** What, no error message? *** */
728     }
729   return 0;
730 }
731
732 /* Associate textual names with actual functions. */
733 static struct {
734   char *name;
735   Function *function;
736 } parser_directives [] = {
737   { "if", parser_if },
738   { "endif", parser_endif },
739   { "else", parser_else },
740   { (char *)0x0, (Function *)0x0 }
741 };
742
743 /* Handle a parser directive.  STATEMENT is the line of the directive
744    without any leading `$'. */
745 static int
746 handle_parser_directive (statement)
747      char *statement;
748 {
749   register int i;
750   char *directive, *args;
751
752   /* Isolate the actual directive. */
753
754   /* Skip whitespace. */
755   for (i = 0; whitespace (statement[i]); i++);
756
757   directive = &statement[i];
758
759   for (; statement[i] && !whitespace (statement[i]); i++);
760
761   if (statement[i])
762     statement[i++] = '\0';
763
764   for (; statement[i] && whitespace (statement[i]); i++);
765
766   args = &statement[i];
767
768   /* Lookup the command, and act on it. */
769   for (i = 0; parser_directives[i].name; i++)
770     if (_rl_stricmp (directive, parser_directives[i].name) == 0)
771       {
772         (*parser_directives[i].function) (args);
773         return (0);
774       }
775
776   /* *** Should an error message be output? */
777   return (1);
778 }
779
780 /* Read the binding command from STRING and perform it.
781    A key binding command looks like: Keyname: function-name\0,
782    a variable binding command looks like: set variable value.
783    A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
784 int
785 rl_parse_and_bind (string)
786      char *string;
787 {
788   char *funname, *kname;
789   register int c, i;
790   int key, equivalency;
791
792   while (string && whitespace (*string))
793     string++;
794
795   if (!string || !*string || *string == '#')
796     return 0;
797
798   /* If this is a parser directive, act on it. */
799   if (*string == '$')
800     {
801       handle_parser_directive (&string[1]);
802       return 0;
803     }
804
805   /* If we aren't supposed to be parsing right now, then we're done. */
806   if (_rl_parsing_conditionalized_out)
807     return 0;
808
809   i = 0;
810   /* If this keyname is a complex key expression surrounded by quotes,
811      advance to after the matching close quote.  This code allows the
812      backslash to quote characters in the key expression. */
813   if (*string == '"')
814     {
815       int passc = 0;
816
817       for (i = 1; c = string[i]; i++)
818         {
819           if (passc)
820             {
821               passc = 0;
822               continue;
823             }
824
825           if (c == '\\')
826             {
827               passc++;
828               continue;
829             }
830
831           if (c == '"')
832             break;
833         }
834       /* If we didn't find a closing quote, abort the line. */
835       if (string[i] == '\0')
836         {
837           _rl_init_file_error ("no closing `\"' in key binding");
838           return 1;
839         }
840     }
841
842   /* Advance to the colon (:) or whitespace which separates the two objects. */
843   for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
844
845   equivalency = (c == ':' && string[i + 1] == '=');
846
847   /* Mark the end of the command (or keyname). */
848   if (string[i])
849     string[i++] = '\0';
850
851   /* If doing assignment, skip the '=' sign as well. */
852   if (equivalency)
853     string[i++] = '\0';
854
855   /* If this is a command to set a variable, then do that. */
856   if (_rl_stricmp (string, "set") == 0)
857     {
858       char *var = string + i;
859       char *value;
860
861       /* Make VAR point to start of variable name. */
862       while (*var && whitespace (*var)) var++;
863
864       /* Make value point to start of value string. */
865       value = var;
866       while (*value && !whitespace (*value)) value++;
867       if (*value)
868         *value++ = '\0';
869       while (*value && whitespace (*value)) value++;
870
871       rl_variable_bind (var, value);
872       return 0;
873     }
874
875   /* Skip any whitespace between keyname and funname. */
876   for (; string[i] && whitespace (string[i]); i++);
877   funname = &string[i];
878
879   /* Now isolate funname.
880      For straight function names just look for whitespace, since
881      that will signify the end of the string.  But this could be a
882      macro definition.  In that case, the string is quoted, so skip
883      to the matching delimiter.  We allow the backslash to quote the
884      delimiter characters in the macro body. */
885   /* This code exists to allow whitespace in macro expansions, which
886      would otherwise be gobbled up by the next `for' loop.*/
887   /* XXX - it may be desirable to allow backslash quoting only if " is
888      the quoted string delimiter, like the shell. */
889   if (*funname == '\'' || *funname == '"')
890     {
891       int delimiter = string[i++];
892       int passc = 0;
893
894       for (; c = string[i]; i++)
895         {
896           if (passc)
897             {
898               passc = 0;
899               continue;
900             }
901
902           if (c == '\\')
903             {
904               passc = 1;
905               continue;
906             }
907
908           if (c == delimiter)
909             break;
910         }
911       if (c)
912         i++;
913     }
914
915   /* Advance to the end of the string.  */
916   for (; string[i] && !whitespace (string[i]); i++);
917
918   /* No extra whitespace at the end of the string. */
919   string[i] = '\0';
920
921   /* Handle equivalency bindings here.  Make the left-hand side be exactly
922      whatever the right-hand evaluates to, including keymaps. */
923   if (equivalency)
924     {
925       return 0;
926     }
927
928   /* If this is a new-style key-binding, then do the binding with
929      rl_set_key ().  Otherwise, let the older code deal with it. */
930   if (*string == '"')
931     {
932       char *seq = xmalloc (1 + strlen (string));
933       register int j, k = 0;
934       int passc = 0;
935
936       for (j = 1; string[j]; j++)
937         {
938           /* Allow backslash to quote characters, but leave them in place.
939              This allows a string to end with a backslash quoting another
940              backslash, or with a backslash quoting a double quote.  The
941              backslashes are left in place for rl_translate_keyseq (). */
942           if (passc || (string[j] == '\\'))
943             {
944               seq[k++] = string[j];
945               passc = !passc;
946               continue;
947             }
948
949           if (string[j] == '"')
950             break;
951
952           seq[k++] = string[j];
953         }
954       seq[k] = '\0';
955
956       /* Binding macro? */
957       if (*funname == '\'' || *funname == '"')
958         {
959           j = strlen (funname);
960
961           /* Remove the delimiting quotes from each end of FUNNAME. */
962           if (j && funname[j - 1] == *funname)
963             funname[j - 1] = '\0';
964
965           rl_macro_bind (seq, &funname[1], _rl_keymap);
966         }
967       else
968         rl_set_key (seq, rl_named_function (funname), _rl_keymap);
969
970       free (seq);
971       return 0;
972     }
973
974   /* Get the actual character we want to deal with. */
975   kname = strrchr (string, '-');
976   if (!kname)
977     kname = string;
978   else
979     kname++;
980
981   key = glean_key_from_name (kname);
982
983   /* Add in control and meta bits. */
984   if (substring_member_of_array (string, possible_control_prefixes))
985     key = CTRL (_rl_to_upper (key));
986
987   if (substring_member_of_array (string, possible_meta_prefixes))
988     key = META (key);
989
990   /* Temporary.  Handle old-style keyname with macro-binding. */
991   if (*funname == '\'' || *funname == '"')
992     {
993       char seq[2];
994       int fl = strlen (funname);
995
996       seq[0] = key; seq[1] = '\0';
997       if (fl && funname[fl - 1] == *funname)
998         funname[fl - 1] = '\0';
999
1000       rl_macro_bind (seq, &funname[1], _rl_keymap);
1001     }
1002 #if defined (PREFIX_META_HACK)
1003   /* Ugly, but working hack to keep prefix-meta around. */
1004   else if (_rl_stricmp (funname, "prefix-meta") == 0)
1005     {
1006       char seq[2];
1007
1008       seq[0] = key;
1009       seq[1] = '\0';
1010       rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1011     }
1012 #endif /* PREFIX_META_HACK */
1013   else
1014     rl_bind_key (key, rl_named_function (funname));
1015   return 0;
1016 }
1017
1018 /* Simple structure for boolean readline variables (i.e., those that can
1019    have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1020    false. */
1021
1022 static struct {
1023   char *name;
1024   int *value;
1025 } boolean_varlist [] = {
1026 #if defined (PAREN_MATCHING)
1027   { "blink-matching-paren",     &rl_blink_matching_paren },
1028 #endif
1029   { "convert-meta",             &_rl_convert_meta_chars_to_ascii },
1030   { "disable-completion",       &rl_inhibit_completion },
1031   { "enable-keypad",            &_rl_enable_keypad },
1032   { "expand-tilde",             &rl_complete_with_tilde_expansion },
1033   { "horizontal-scroll-mode",   &_rl_horizontal_scroll_mode },
1034   { "input-meta",               &_rl_meta_flag },
1035   { "mark-directories",         &_rl_complete_mark_directories },
1036   { "mark-modified-lines",      &_rl_mark_modified_lines },
1037   { "meta-flag",                &_rl_meta_flag },
1038   { "output-meta",              &_rl_output_meta_chars },
1039   { "show-all-if-ambiguous",    &_rl_complete_show_all },
1040 #if defined (VISIBLE_STATS)
1041   { "visible-stats",            &rl_visible_stats },
1042 #endif /* VISIBLE_STATS */
1043   { (char *)NULL, (int *)NULL }
1044 };
1045
1046 int
1047 rl_variable_bind (name, value)
1048      char *name, *value;
1049 {
1050   register int i;
1051
1052   /* Check for simple variables first. */
1053   for (i = 0; boolean_varlist[i].name; i++)
1054     {
1055       if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1056         {
1057           /* A variable is TRUE if the "value" is "on", "1" or "". */
1058           *boolean_varlist[i].value = *value == 0 ||
1059                                       _rl_stricmp (value, "on") == 0 ||
1060                                       (value[0] == '1' && value[1] == '\0');
1061           return 0;
1062         }
1063     }
1064
1065   /* Not a boolean variable, so check for specials. */
1066
1067   /* Editing mode change? */
1068   if (_rl_stricmp (name, "editing-mode") == 0)
1069     {
1070       if (_rl_strnicmp (value, "vi", 2) == 0)
1071         {
1072 #if defined (VI_MODE)
1073           _rl_keymap = vi_insertion_keymap;
1074           rl_editing_mode = vi_mode;
1075 #endif /* VI_MODE */
1076         }
1077       else if (_rl_strnicmp (value, "emacs", 5) == 0)
1078         {
1079           _rl_keymap = emacs_standard_keymap;
1080           rl_editing_mode = emacs_mode;
1081         }
1082     }
1083
1084   /* Comment string change? */
1085   else if (_rl_stricmp (name, "comment-begin") == 0)
1086     {
1087       if (*value)
1088         {
1089           if (_rl_comment_begin)
1090             free (_rl_comment_begin);
1091
1092           _rl_comment_begin = savestring (value);
1093         }
1094     }
1095   else if (_rl_stricmp (name, "completion-query-items") == 0)
1096     {
1097       int nval = 100;
1098       if (*value)
1099         {
1100           nval = atoi (value);
1101           if (nval < 0)
1102             nval = 0;
1103         }
1104       rl_completion_query_items = nval;
1105     }
1106   else if (_rl_stricmp (name, "keymap") == 0)
1107     {
1108       Keymap kmap;
1109       kmap = rl_get_keymap_by_name (value);
1110       if (kmap)
1111         rl_set_keymap (kmap);
1112     }
1113   else if (_rl_stricmp (name, "bell-style") == 0)
1114     {
1115       if (!*value)
1116         _rl_bell_preference = AUDIBLE_BELL;
1117       else
1118         {
1119           if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1120             _rl_bell_preference = NO_BELL;
1121           else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1122             _rl_bell_preference = AUDIBLE_BELL;
1123           else if (_rl_stricmp (value, "visible") == 0)
1124             _rl_bell_preference = VISIBLE_BELL;
1125         }
1126     }
1127   else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1128     {
1129       /* Backwards compatibility. */
1130       if (*value && (_rl_stricmp (value, "on") == 0 ||
1131                      (*value == '1' && !value[1])))
1132         _rl_bell_preference = VISIBLE_BELL;
1133       else
1134         _rl_bell_preference = AUDIBLE_BELL;
1135     }
1136
1137   return 0;
1138 }
1139
1140 /* Return the character which matches NAME.
1141    For example, `Space' returns ' '. */
1142
1143 typedef struct {
1144   char *name;
1145   int value;
1146 } assoc_list;
1147
1148 static assoc_list name_key_alist[] = {
1149   { "DEL", 0x7f },
1150   { "ESC", '\033' },
1151   { "Escape", '\033' },
1152   { "LFD", '\n' },
1153   { "Newline", '\n' },
1154   { "RET", '\r' },
1155   { "Return", '\r' },
1156   { "Rubout", 0x7f },
1157   { "SPC", ' ' },
1158   { "Space", ' ' },
1159   { "Tab", 0x09 },
1160   { (char *)0x0, 0 }
1161 };
1162
1163 static int
1164 glean_key_from_name (name)
1165      char *name;
1166 {
1167   register int i;
1168
1169   for (i = 0; name_key_alist[i].name; i++)
1170     if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1171       return (name_key_alist[i].value);
1172
1173   return (*(unsigned char *)name);      /* XXX was return (*name) */
1174 }
1175
1176 /* Auxiliary functions to manage keymaps. */
1177 static struct {
1178   char *name;
1179   Keymap map;
1180 } keymap_names[] = {
1181   { "emacs", emacs_standard_keymap },
1182   { "emacs-standard", emacs_standard_keymap },
1183   { "emacs-meta", emacs_meta_keymap },
1184   { "emacs-ctlx", emacs_ctlx_keymap },
1185 #if defined (VI_MODE)
1186   { "vi", vi_movement_keymap },
1187   { "vi-move", vi_movement_keymap },
1188   { "vi-command", vi_movement_keymap },
1189   { "vi-insert", vi_insertion_keymap },
1190 #endif /* VI_MODE */
1191   { (char *)0x0, (Keymap)0x0 }
1192 };
1193
1194 Keymap
1195 rl_get_keymap_by_name (name)
1196      char *name;
1197 {
1198   register int i;
1199
1200   for (i = 0; keymap_names[i].name; i++)
1201     if (strcmp (name, keymap_names[i].name) == 0)
1202       return (keymap_names[i].map);
1203   return ((Keymap) NULL);
1204 }
1205
1206 char *
1207 rl_get_keymap_name (map)
1208      Keymap map;
1209 {
1210   register int i;
1211   for (i = 0; keymap_names[i].name; i++)
1212     if (map == keymap_names[i].map)
1213       return (keymap_names[i].name);
1214   return ((char *)NULL);
1215 }
1216   
1217 void
1218 rl_set_keymap (map)
1219      Keymap map;
1220 {
1221   if (map)
1222     _rl_keymap = map;
1223 }
1224
1225 Keymap
1226 rl_get_keymap ()
1227 {
1228   return (_rl_keymap);
1229 }
1230
1231 void
1232 rl_set_keymap_from_edit_mode ()
1233 {
1234   if (rl_editing_mode == emacs_mode)
1235     _rl_keymap = emacs_standard_keymap;
1236 #if defined (VI_MODE)
1237   else if (rl_editing_mode == vi_mode)
1238     _rl_keymap = vi_insertion_keymap;
1239 #endif /* VI_MODE */
1240 }
1241
1242 char *
1243 rl_get_keymap_name_from_edit_mode ()
1244 {
1245   if (rl_editing_mode == emacs_mode)
1246     return "emacs";
1247 #if defined (VI_MODE)
1248   else if (rl_editing_mode == vi_mode)
1249     return "vi";
1250 #endif /* VI_MODE */
1251   else
1252     return "none";
1253 }
1254
1255 /* **************************************************************** */
1256 /*                                                                  */
1257 /*                Key Binding and Function Information              */
1258 /*                                                                  */
1259 /* **************************************************************** */
1260
1261 /* Each of the following functions produces information about the
1262    state of keybindings and functions known to Readline.  The info
1263    is always printed to rl_outstream, and in such a way that it can
1264    be read back in (i.e., passed to rl_parse_and_bind (). */
1265
1266 /* Print the names of functions known to Readline. */
1267 void
1268 rl_list_funmap_names ()
1269 {
1270   register int i;
1271   char **funmap_names;
1272
1273   funmap_names = rl_funmap_names ();
1274
1275   if (!funmap_names)
1276     return;
1277
1278   for (i = 0; funmap_names[i]; i++)
1279     fprintf (rl_outstream, "%s\n", funmap_names[i]);
1280
1281   free (funmap_names);
1282 }
1283
1284 static char *
1285 _rl_get_keyname (key)
1286      int key;
1287 {
1288   char *keyname;
1289   int i, c;
1290
1291   keyname = (char *)xmalloc (8);
1292
1293   c = key;
1294   /* Since this is going to be used to write out keysequence-function
1295      pairs for possible inclusion in an inputrc file, we don't want to
1296      do any special meta processing on KEY. */
1297
1298 #if 0
1299   /* We might want to do this, but the old version of the code did not. */
1300
1301   /* If this is an escape character, we don't want to do any more processing.
1302      Just add the special ESC key sequence and return. */
1303   if (c == ESC)
1304     {
1305       keyseq[0] = '\\';
1306       keyseq[1] = 'e';
1307       keyseq[2] = '\0';
1308       return keyseq;
1309     }
1310 #endif
1311
1312   /* RUBOUT is translated directly into \C-? */
1313   if (key == RUBOUT)
1314     {
1315       keyname[0] = '\\';
1316       keyname[1] = 'C';
1317       keyname[2] = '-';
1318       keyname[3] = '?';
1319       keyname[4] = '\0';
1320       return keyname;
1321     }
1322
1323   i = 0;
1324   /* Now add special prefixes needed for control characters.  This can
1325      potentially change C. */
1326   if (CTRL_CHAR (c))
1327     {
1328       keyname[i++] = '\\';
1329       keyname[i++] = 'C';
1330       keyname[i++] = '-';
1331       c = _rl_to_lower (UNCTRL (c));
1332     }
1333
1334   /* Now, if the character needs to be quoted with a backslash, do that. */
1335   if (c == '\\' || c == '"')
1336     keyname[i++] = '\\';
1337
1338   /* Now add the key, terminate the string, and return it. */
1339   keyname[i++] = (char) c;
1340   keyname[i] = '\0';
1341
1342   return keyname;
1343 }
1344
1345 /* Return a NULL terminated array of strings which represent the key
1346    sequences that are used to invoke FUNCTION in MAP. */
1347 char **
1348 rl_invoking_keyseqs_in_map (function, map)
1349      Function *function;
1350      Keymap map;
1351 {
1352   register int key;
1353   char **result;
1354   int result_index, result_size;
1355
1356   result = (char **)NULL;
1357   result_index = result_size = 0;
1358
1359   for (key = 0; key < KEYMAP_SIZE; key++)
1360     {
1361       switch (map[key].type)
1362         {
1363         case ISMACR:
1364           /* Macros match, if, and only if, the pointers are identical.
1365              Thus, they are treated exactly like functions in here. */
1366         case ISFUNC:
1367           /* If the function in the keymap is the one we are looking for,
1368              then add the current KEY to the list of invoking keys. */
1369           if (map[key].function == function)
1370             {
1371               char *keyname;
1372
1373               keyname = _rl_get_keyname (key);
1374
1375               if (result_index + 2 > result_size)
1376                 {
1377                   result_size += 10;
1378                   result = (char **) xrealloc (result, result_size * sizeof (char *));
1379                 }
1380
1381               result[result_index++] = keyname;
1382               result[result_index] = (char *)NULL;
1383             }
1384           break;
1385
1386         case ISKMAP:
1387           {
1388             char **seqs;
1389             register int i;
1390
1391             /* Find the list of keyseqs in this map which have FUNCTION as
1392                their target.  Add the key sequences found to RESULT. */
1393             if (map[key].function)
1394               seqs =
1395                 rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1396             else
1397               break;
1398
1399             if (seqs == 0)
1400               break;
1401
1402             for (i = 0; seqs[i]; i++)
1403               {
1404                 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1405
1406                 if (key == ESC)
1407                   sprintf (keyname, "\\e");
1408                 else if (CTRL_CHAR (key))
1409                   sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1410                 else if (key == RUBOUT)
1411                   sprintf (keyname, "\\C-?");
1412                 else if (key == '\\' || key == '"')
1413                   {
1414                     keyname[0] = '\\';
1415                     keyname[1] = (char) key;
1416                     keyname[2] = '\0';
1417                   }
1418                 else
1419                   {
1420                     keyname[0] = (char) key;
1421                     keyname[1] = '\0';
1422                   }
1423                 
1424                 strcat (keyname, seqs[i]);
1425                 free (seqs[i]);
1426
1427                 if (result_index + 2 > result_size)
1428                   {
1429                     result_size += 10;
1430                     result = (char **) xrealloc (result, result_size * sizeof (char *));
1431                   }
1432
1433                 result[result_index++] = keyname;
1434                 result[result_index] = (char *)NULL;
1435               }
1436
1437             free (seqs);
1438           }
1439           break;
1440         }
1441     }
1442   return (result);
1443 }
1444
1445 /* Return a NULL terminated array of strings which represent the key
1446    sequences that can be used to invoke FUNCTION using the current keymap. */
1447 char **
1448 rl_invoking_keyseqs (function)
1449      Function *function;
1450 {
1451   return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1452 }
1453
1454 /* Print all of the functions and their bindings to rl_outstream.  If
1455    PRINT_READABLY is non-zero, then print the output in such a way
1456    that it can be read back in. */
1457 void
1458 rl_function_dumper (print_readably)
1459      int print_readably;
1460 {
1461   register int i;
1462   char **names;
1463   char *name;
1464
1465   names = rl_funmap_names ();
1466
1467   fprintf (rl_outstream, "\n");
1468
1469   for (i = 0; name = names[i]; i++)
1470     {
1471       Function *function;
1472       char **invokers;
1473
1474       function = rl_named_function (name);
1475       invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1476
1477       if (print_readably)
1478         {
1479           if (!invokers)
1480             fprintf (rl_outstream, "# %s (not bound)\n", name);
1481           else
1482             {
1483               register int j;
1484
1485               for (j = 0; invokers[j]; j++)
1486                 {
1487                   fprintf (rl_outstream, "\"%s\": %s\n",
1488                            invokers[j], name);
1489                   free (invokers[j]);
1490                 }
1491
1492               free (invokers);
1493             }
1494         }
1495       else
1496         {
1497           if (!invokers)
1498             fprintf (rl_outstream, "%s is not bound to any keys\n",
1499                      name);
1500           else
1501             {
1502               register int j;
1503
1504               fprintf (rl_outstream, "%s can be found on ", name);
1505
1506               for (j = 0; invokers[j] && j < 5; j++)
1507                 {
1508                   fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1509                            invokers[j + 1] ? ", " : ".\n");
1510                 }
1511
1512               if (j == 5 && invokers[j])
1513                 fprintf (rl_outstream, "...\n");
1514
1515               for (j = 0; invokers[j]; j++)
1516                 free (invokers[j]);
1517
1518               free (invokers);
1519             }
1520         }
1521     }
1522 }
1523
1524 /* Print all of the current functions and their bindings to
1525    rl_outstream.  If an explicit argument is given, then print
1526    the output in such a way that it can be read back in. */
1527 int
1528 rl_dump_functions (count, key)
1529      int count, key;
1530 {
1531   rl_function_dumper (rl_explicit_arg);
1532   rl_on_new_line ();
1533   return (0);
1534 }
1535
1536 static void
1537 _rl_macro_dumper_internal (print_readably, map, prefix)
1538      int print_readably;
1539      Keymap map;
1540      char *prefix;
1541 {
1542   register int key;
1543   char *keyname, *out;
1544   int prefix_len;
1545
1546   for (key = 0; key < KEYMAP_SIZE; key++)
1547     {
1548       switch (map[key].type)
1549         {
1550         case ISMACR:
1551           keyname = _rl_get_keyname (key);
1552           out = (char *)map[key].function;
1553           if (print_readably)
1554             fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
1555                                                          keyname,
1556                                                          out ? out : "");
1557           else
1558             fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
1559                                                         keyname,
1560                                                         out ? out : "");
1561           free (keyname);
1562           break;
1563         case ISFUNC:
1564           break;
1565         case ISKMAP:
1566           prefix_len = prefix ? strlen (prefix) : 0;
1567           if (key == ESC)
1568             {
1569               keyname = xmalloc (3 + prefix_len);
1570               if (prefix)
1571                 strcpy (keyname, prefix);
1572               keyname[prefix_len] = '\\';
1573               keyname[prefix_len + 1] = 'e';
1574               keyname[prefix_len + 2] = '\0';
1575             }
1576           else
1577             {
1578               keyname = _rl_get_keyname (key);
1579               if (prefix)
1580                 {
1581                   out = xmalloc (strlen (keyname) + prefix_len + 1);
1582                   strcpy (out, prefix);
1583                   strcpy (out + prefix_len, keyname);
1584                   free (keyname);
1585                   keyname = out;
1586                 }
1587             }
1588
1589           _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
1590           free (keyname);
1591           break;
1592         }
1593     }
1594 }
1595
1596 void
1597 rl_macro_dumper (print_readably)
1598      int print_readably;
1599 {
1600   _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
1601 }
1602
1603 int
1604 rl_dump_macros (count, key)
1605      int count, key;
1606 {
1607   rl_macro_dumper (rl_explicit_arg);
1608   rl_on_new_line ();
1609   return (0);
1610 }
1611
1612 void
1613 rl_variable_dumper (print_readably)
1614      int print_readably;
1615 {
1616   int i;
1617   char *kname;
1618
1619   for (i = 0; boolean_varlist[i].name; i++)
1620     {
1621       if (print_readably)
1622         fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
1623                                *boolean_varlist[i].value ? "on" : "off");
1624       else
1625         fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
1626                                *boolean_varlist[i].value ? "on" : "off");
1627     }
1628
1629   /* bell-style */
1630   switch (_rl_bell_preference)
1631     {
1632     case NO_BELL: kname = "none"; break;
1633     case VISIBLE_BELL: kname = "visible"; break;
1634     case AUDIBLE_BELL:
1635     default: kname = "audible"; break;
1636     }
1637   if (print_readably)
1638     fprintf (rl_outstream, "set bell-style %s\n", kname);
1639   else
1640     fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
1641
1642   /* comment-begin */
1643   if (print_readably)
1644     fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
1645   else
1646     fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : "");
1647
1648   /* completion-query-items */
1649   if (print_readably)
1650     fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items);
1651   else
1652     fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items);
1653
1654   /* editing-mode */
1655   if (print_readably)
1656     fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1657   else
1658     fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1659
1660   /* keymap */
1661   kname = rl_get_keymap_name (_rl_keymap);
1662   if (kname == 0)
1663     kname = rl_get_keymap_name_from_edit_mode ();
1664   if (print_readably)
1665     fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none");
1666   else
1667     fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none");
1668 }
1669
1670 /* Print all of the current variables and their values to
1671    rl_outstream.  If an explicit argument is given, then print
1672    the output in such a way that it can be read back in. */
1673 int
1674 rl_dump_variables (count, key)
1675      int count, key;
1676 {
1677   rl_variable_dumper (rl_explicit_arg);
1678   rl_on_new_line ();
1679   return (0);
1680 }
1681
1682 /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
1683 void
1684 _rl_bind_if_unbound (keyseq, default_func)
1685      char *keyseq;
1686      Function *default_func;
1687 {
1688   Function *func;
1689
1690   if (keyseq)
1691     {
1692       func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
1693       if (!func || func == rl_do_lowercase_version)
1694         rl_set_key (keyseq, default_func, _rl_keymap);
1695     }
1696 }
1697
1698 /* Return non-zero if any members of ARRAY are a substring in STRING. */
1699 static int
1700 substring_member_of_array (string, array)
1701      char *string, **array;
1702 {
1703   while (*array)
1704     {
1705       if (_rl_strindex (string, *array))
1706         return (1);
1707       array++;
1708     }
1709   return (0);
1710 }