1 /* bind.c -- key binding and startup file support for the readline library. */
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 1, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include <sys/types.h>
32 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
37 /* System-specific feature definitions and include files. */
40 /* Some standard library routines. */
44 #if !defined (strchr) && !defined (__STDC__)
45 extern char *strchr (), *strrchr ();
46 #endif /* !strchr && !__STDC__ */
48 extern char *tilde_expand ();
50 extern int _rl_horizontal_scroll_mode
;
51 extern int _rl_mark_modified_lines
;
52 extern int _rl_prefer_visible_bell
;
53 extern int _rl_meta_flag
;
54 extern int rl_blink_matching_paren
;
55 extern int _rl_convert_meta_chars_to_ascii
;
56 #if defined (VISIBLE_STATS)
57 extern int rl_visible_stats
;
58 #endif /* VISIBLE_STATS */
59 extern int rl_complete_with_tilde_expansion
;
60 extern int rl_completion_query_items
;
62 extern int rl_explicit_arg
;
63 extern int rl_editing_mode
;
64 extern unsigned short _rl_parsing_conditionalized_out
;
65 extern Keymap _rl_keymap
;
67 extern char *possible_control_prefixes
[], *possible_meta_prefixes
[];
69 extern char **rl_funmap_names ();
71 static void rl_generic_bind ();
72 static int glean_key_from_name ();
73 static int stricmp (), strnicmp ();
75 #if defined (STATIC_MALLOC)
76 static char *xmalloc (), *xrealloc ();
78 extern char *xmalloc (), *xrealloc ();
79 #endif /* STATIC_MALLOC */
81 /* **************************************************************** */
85 /* **************************************************************** */
87 /* rl_add_defun (char *name, Function *function, int key)
88 Add NAME to the list of named functions. Make FUNCTION be the function
89 that gets called. If KEY is not -1, then bind it. */
90 rl_add_defun (name
, function
, key
)
96 rl_bind_key (key
, function
);
97 rl_add_funmap_entry (name
, function
);
100 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
102 rl_bind_key (key
, function
)
109 if (META_CHAR (key
) && _rl_convert_meta_chars_to_ascii
)
111 if (_rl_keymap
[ESC
].type
== ISKMAP
)
113 Keymap escmap
= (Keymap
)_rl_keymap
[ESC
].function
;
116 escmap
[key
].type
= ISFUNC
;
117 escmap
[key
].function
= function
;
123 _rl_keymap
[key
].type
= ISFUNC
;
124 _rl_keymap
[key
].function
= function
;
128 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
131 rl_bind_key_in_map (key
, function
, map
)
137 Keymap oldmap
= _rl_keymap
;
140 result
= rl_bind_key (key
, function
);
145 /* Make KEY do nothing in the currently selected keymap.
146 Returns non-zero in case of error. */
151 return (rl_bind_key (key
, (Function
*)NULL
));
154 /* Make KEY do nothing in MAP.
155 Returns non-zero in case of error. */
157 rl_unbind_key_in_map (key
, map
)
161 return (rl_bind_key_in_map (key
, (Function
*)NULL
, map
));
164 /* Bind the key sequence represented by the string KEYSEQ to
165 FUNCTION. This makes new keymaps as necessary. The initial
166 place to do bindings is in MAP. */
167 rl_set_key (keyseq
, function
, map
)
172 rl_generic_bind (ISFUNC
, keyseq
, function
, map
);
175 /* Bind the key sequence represented by the string KEYSEQ to
176 the string of characters MACRO. This makes new keymaps as
177 necessary. The initial place to do bindings is in MAP. */
178 rl_macro_bind (keyseq
, macro
, map
)
179 char *keyseq
, *macro
;
185 macro_keys
= (char *)xmalloc ((2 * strlen (macro
)) + 1);
187 if (rl_translate_keyseq (macro
, macro_keys
, ¯o_keys_len
))
192 rl_generic_bind (ISMACR
, keyseq
, macro_keys
, map
);
195 /* Bind the key sequence represented by the string KEYSEQ to
196 the arbitrary pointer DATA. TYPE says what kind of data is
197 pointed to by DATA, right now this can be a function (ISFUNC),
198 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
199 as necessary. The initial place to do bindings is in MAP. */
202 rl_generic_bind (type
, keyseq
, data
, map
)
211 /* If no keys to bind to, exit right away. */
212 if (!keyseq
|| !*keyseq
)
219 keys
= (char *)alloca (1 + (2 * strlen (keyseq
)));
221 /* Translate the ASCII representation of KEYSEQ into an array of
222 characters. Stuff the characters into KEYS, and the length of
223 KEYS into KEYS_LEN. */
224 if (rl_translate_keyseq (keyseq
, keys
, &keys_len
))
227 /* Bind keys, making new keymaps as necessary. */
228 for (i
= 0; i
< keys_len
; i
++)
230 int ic
= (int) ((unsigned char)keys
[i
]);
232 if (_rl_convert_meta_chars_to_ascii
&& META_CHAR (ic
))
235 if (map
[ESC
].type
== ISKMAP
)
236 map
= (Keymap
) map
[ESC
].function
;
239 if ((i
+ 1) < keys_len
)
241 if (map
[ic
].type
!= ISKMAP
)
243 if (map
[ic
].type
== ISMACR
)
244 free ((char *)map
[ic
].function
);
246 map
[ic
].type
= ISKMAP
;
247 map
[ic
].function
= (Function
*)rl_make_bare_keymap ();
249 map
= (Keymap
)map
[ic
].function
;
253 if (map
[ic
].type
== ISMACR
)
254 free ((char *)map
[ic
].function
);
256 map
[ic
].function
= (Function
*)data
;
262 /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
263 an array of characters. LEN gets the final length of ARRAY. Return
264 non-zero if there was an error parsing SEQ. */
265 rl_translate_keyseq (seq
, array
, len
)
269 register int i
, c
, l
= 0;
271 for (i
= 0; c
= seq
[i
]; i
++)
280 if (((c
== 'C' || c
== 'M') && seq
[i
+ 1] == '-') ||
283 /* Handle special case of backwards define. */
284 if (strncmp (&seq
[i
], "C-\\M-", 5) == 0)
288 array
[l
++] = CTRL (to_upper (seq
[i
]));
303 /* Special hack for C-?... */
307 array
[l
++] = CTRL (to_upper (seq
[i
]));
325 /* Return a pointer to the function that STRING represents.
326 If STRING doesn't have a matching function, then a NULL pointer
329 rl_named_function (string
)
334 rl_initialize_funmap ();
336 for (i
= 0; funmap
[i
]; i
++)
337 if (stricmp (funmap
[i
]->name
, string
) == 0)
338 return (funmap
[i
]->function
);
339 return ((Function
*)NULL
);
342 /* Return the function (or macro) definition which would be invoked via
343 KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
344 used. TYPE, if non-NULL, is a pointer to an int which will receive the
345 type of the object pointed to. One of ISFUNC (function), ISKMAP (keymap),
346 or ISMACR (macro). */
348 rl_function_of_keyseq (keyseq
, map
, type
)
358 for (i
= 0; keyseq
&& keyseq
[i
]; i
++)
362 if (META_CHAR (ic
) && _rl_convert_meta_chars_to_ascii
)
364 if (map
[ESC
].type
!= ISKMAP
)
367 *type
= map
[ESC
].type
;
369 return (map
[ESC
].function
);
373 map
= (Keymap
)map
[ESC
].function
;
378 if (map
[ic
].type
== ISKMAP
)
380 /* If this is the last key in the key sequence, return the
387 return (map
[ic
].function
);
390 map
= (Keymap
)map
[ic
].function
;
395 *type
= map
[ic
].type
;
397 return (map
[ic
].function
);
402 /* The last key bindings file read. */
403 static char *last_readline_init_file
= (char *)NULL
;
405 /* Re-read the current keybindings file. */
406 rl_re_read_init_file (count
, ignore
)
409 rl_read_init_file ((char *)NULL
);
412 /* The final, last-ditch effort file name for an init file. */
414 /* Don't know what to do, but this is a guess */
415 #define DEFAULT_INPUTRC "/INPUTRC";
417 #define DEFAULT_INPUTRC "~/.inputrc"
420 /* Do key bindings from a file. If FILENAME is NULL it defaults
421 to `~/.inputrc'. If the file existed and could be opened and
422 read, 0 is returned, otherwise errno is returned. */
424 rl_read_init_file (filename
)
428 char *buffer
, *openname
, *line
, *end
;
432 /* Default the filename. */
435 if (last_readline_init_file
)
436 filename
= last_readline_init_file
;
438 filename
= DEFAULT_INPUTRC
;
441 openname
= tilde_expand (filename
);
443 if (!openname
|| *openname
== '\000')
446 if ((stat (openname
, &finfo
) < 0) ||
447 (file
= open (openname
, O_RDONLY
, 0666)) < 0)
455 if (last_readline_init_file
)
456 free (last_readline_init_file
);
458 last_readline_init_file
= savestring (filename
);
460 /* Read the file into BUFFER. */
461 buffer
= (char *)xmalloc ((int)finfo
.st_size
+ 1);
462 i
= read (file
, buffer
, finfo
.st_size
);
465 if (i
!= finfo
.st_size
)
468 /* Loop over the lines in the file. Lines that start with `#' are
469 comments; all other lines are commands for readline initialization. */
471 end
= buffer
+ finfo
.st_size
;
474 /* Find the end of this line. */
475 for (i
= 0; line
+ i
!= end
&& line
[i
] != '\n'; i
++);
477 /* Mark end of line. */
480 /* If the line is not a comment, then parse it. */
481 if (*line
&& *line
!= '#')
482 rl_parse_and_bind (line
);
484 /* Move to the next line. */
491 /* **************************************************************** */
493 /* Parser Directives */
495 /* **************************************************************** */
499 /* Calling programs set this to have their argv[0]. */
500 char *rl_readline_name
= "other";
502 /* Stack of previous values of parsing_conditionalized_out. */
503 static unsigned char *if_stack
= (unsigned char *)NULL
;
504 static int if_stack_depth
= 0;
505 static int if_stack_size
= 0;
507 /* Push _rl_parsing_conditionalized_out, and set parser state based
515 /* Push parser state. */
516 if (if_stack_depth
+ 1 >= if_stack_size
)
519 if_stack
= (unsigned char *)xmalloc (if_stack_size
= 20);
521 if_stack
= (unsigned char *)xrealloc (if_stack
, if_stack_size
+= 20);
523 if_stack
[if_stack_depth
++] = _rl_parsing_conditionalized_out
;
525 /* If parsing is turned off, then nothing can turn it back on except
526 for finding the matching endif. In that case, return right now. */
527 if (_rl_parsing_conditionalized_out
)
530 /* Isolate first argument. */
531 for (i
= 0; args
[i
] && !whitespace (args
[i
]); i
++);
536 /* Handle "if term=foo" and "if mode=emacs" constructs. If this
537 isn't term=foo, or mode=emacs, then check to see if the first
538 word in ARGS is the same as the value stored in rl_readline_name. */
539 if (rl_terminal_name
&& strnicmp (args
, "term=", 5) == 0)
543 /* Terminals like "aaa-60" are equivalent to "aaa". */
544 tname
= savestring (rl_terminal_name
);
545 tem
= (char*) strrchr (tname
, '-');
549 /* Test the `long' and `short' forms of the terminal name so that
550 if someone has a `sun-cmd' and does not want to have bindings
551 that will be executed if the terminal is a `sun', they can put
552 `$if term=sun-cmd' into their .inputrc. */
553 if ((stricmp (args
+ 5, tname
) == 0) ||
554 (stricmp (args
+ 5, rl_terminal_name
) == 0))
555 _rl_parsing_conditionalized_out
= 0;
557 _rl_parsing_conditionalized_out
= 1;
561 #if defined (VI_MODE)
562 else if (strnicmp (args
, "mode=", 5) == 0)
566 if (stricmp (args
+ 5, "emacs") == 0)
568 else if (stricmp (args
+ 5, "vi") == 0)
573 if (mode
== rl_editing_mode
)
574 _rl_parsing_conditionalized_out
= 0;
576 _rl_parsing_conditionalized_out
= 1;
579 /* Check to see if the first word in ARGS is the same as the
580 value stored in rl_readline_name. */
581 else if (stricmp (args
, rl_readline_name
) == 0)
582 _rl_parsing_conditionalized_out
= 0;
584 _rl_parsing_conditionalized_out
= 1;
588 /* Invert the current parser state if there is anything on the stack. */
601 /* Check the previous (n - 1) levels of the stack to make sure that
602 we haven't previously turned off parsing. */
603 for (i
= 0; i
< if_stack_depth
- 1; i
++)
604 if (if_stack
[i
] == 1)
607 /* Invert the state of parsing if at top level. */
608 _rl_parsing_conditionalized_out
= !_rl_parsing_conditionalized_out
;
612 /* Terminate a conditional, popping the value of
613 _rl_parsing_conditionalized_out from the stack. */
619 _rl_parsing_conditionalized_out
= if_stack
[--if_stack_depth
];
622 /* *** What, no error message? *** */
627 /* Associate textual names with actual functions. */
631 } parser_directives
[] = {
633 { "endif", parser_endif
},
634 { "else", parser_else
},
635 { (char *)0x0, (Function
*)0x0 }
638 /* Handle a parser directive. STATEMENT is the line of the directive
639 without any leading `$'. */
641 handle_parser_directive (statement
)
645 char *directive
, *args
;
647 /* Isolate the actual directive. */
649 /* Skip whitespace. */
650 for (i
= 0; whitespace (statement
[i
]); i
++);
652 directive
= &statement
[i
];
654 for (; statement
[i
] && !whitespace (statement
[i
]); i
++);
657 statement
[i
++] = '\0';
659 for (; statement
[i
] && whitespace (statement
[i
]); i
++);
661 args
= &statement
[i
];
663 /* Lookup the command, and act on it. */
664 for (i
= 0; parser_directives
[i
].name
; i
++)
665 if (stricmp (directive
, parser_directives
[i
].name
) == 0)
667 (*parser_directives
[i
].function
) (args
);
671 /* *** Should an error message be output? */
675 /* Ugly but working hack for binding prefix meta. */
676 #define PREFIX_META_HACK
678 static int substring_member_of_array ();
680 /* Read the binding command from STRING and perform it.
681 A key binding command looks like: Keyname: function-name\0,
682 a variable binding command looks like: set variable value.
683 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
684 rl_parse_and_bind (string
)
687 char *funname
, *kname
;
689 int key
, equivalency
;
691 while (string
&& whitespace (*string
))
694 if (!string
|| !*string
|| *string
== '#')
697 /* If this is a parser directive, act on it. */
700 handle_parser_directive (&string
[1]);
704 /* If we aren't supposed to be parsing right now, then we're done. */
705 if (_rl_parsing_conditionalized_out
)
709 /* If this keyname is a complex key expression surrounded by quotes,
710 advance to after the matching close quote. This code allows the
711 backslash to quote characters in the key expression. */
716 for (i
= 1; c
= string
[i
]; i
++)
735 /* Advance to the colon (:) or whitespace which separates the two objects. */
736 for (; (c
= string
[i
]) && c
!= ':' && c
!= ' ' && c
!= '\t'; i
++ );
738 equivalency
= (c
== ':' && string
[i
+ 1] == '=');
740 /* Mark the end of the command (or keyname). */
744 /* If doing assignment, skip the '=' sign as well. */
748 /* If this is a command to set a variable, then do that. */
749 if (stricmp (string
, "set") == 0)
751 char *var
= string
+ i
;
754 /* Make VAR point to start of variable name. */
755 while (*var
&& whitespace (*var
)) var
++;
757 /* Make value point to start of value string. */
759 while (*value
&& !whitespace (*value
)) value
++;
762 while (*value
&& whitespace (*value
)) value
++;
764 rl_variable_bind (var
, value
);
768 /* Skip any whitespace between keyname and funname. */
769 for (; string
[i
] && whitespace (string
[i
]); i
++);
770 funname
= &string
[i
];
772 /* Now isolate funname.
773 For straight function names just look for whitespace, since
774 that will signify the end of the string. But this could be a
775 macro definition. In that case, the string is quoted, so skip
776 to the matching delimiter. We allow the backslash to quote the
777 delimiter characters in the macro body. */
778 /* This code exists to allow whitespace in macro expansions, which
779 would otherwise be gobbled up by the next `for' loop.*/
780 /* XXX - it may be desirable to allow backslash quoting only if " is
781 the quoted string delimiter, like the shell. */
782 if (*funname
== '\'' || *funname
== '"')
784 int delimiter
= string
[i
++];
787 for (; c
= string
[i
]; i
++)
808 /* Advance to the end of the string. */
809 for (; string
[i
] && !whitespace (string
[i
]); i
++);
811 /* No extra whitespace at the end of the string. */
814 /* Handle equivalency bindings here. Make the left-hand side be exactly
815 whatever the right-hand evaluates to, including keymaps. */
821 /* If this is a new-style key-binding, then do the binding with
822 rl_set_key (). Otherwise, let the older code deal with it. */
825 char *seq
= (char *)alloca (1 + strlen (string
));
826 register int j
, k
= 0;
829 for (j
= 1; string
[j
]; j
++)
831 /* Allow backslash to quote characters, but leave them in place.
832 This allows a string to end with a backslash quoting another
833 backslash, or with a backslash quoting a double quote. The
834 backslashes are left in place for rl_translate_keyseq (). */
835 if (passc
|| (string
[j
] == '\\'))
837 seq
[k
++] = string
[j
];
842 if (string
[j
] == '"')
845 seq
[k
++] = string
[j
];
850 if (*funname
== '\'' || *funname
== '"')
852 j
= strlen (funname
);
854 /* Remove the delimiting quotes from each end of FUNNAME. */
855 if (j
&& funname
[j
- 1] == *funname
)
856 funname
[j
- 1] = '\0';
858 rl_macro_bind (seq
, &funname
[1], _rl_keymap
);
861 rl_set_key (seq
, rl_named_function (funname
), _rl_keymap
);
866 /* Get the actual character we want to deal with. */
867 kname
= (char*) strrchr (string
, '-');
873 key
= glean_key_from_name (kname
);
875 /* Add in control and meta bits. */
876 if (substring_member_of_array (string
, possible_control_prefixes
))
877 key
= CTRL (to_upper (key
));
879 if (substring_member_of_array (string
, possible_meta_prefixes
))
882 /* Temporary. Handle old-style keyname with macro-binding. */
883 if (*funname
== '\'' || *funname
== '"')
886 int fl
= strlen (funname
);
888 seq
[0] = key
; seq
[1] = '\0';
889 if (fl
&& funname
[fl
- 1] == *funname
)
890 funname
[fl
- 1] = '\0';
892 rl_macro_bind (seq
, &funname
[1], _rl_keymap
);
894 #if defined (PREFIX_META_HACK)
895 /* Ugly, but working hack to keep prefix-meta around. */
896 else if (stricmp (funname
, "prefix-meta") == 0)
902 rl_generic_bind (ISKMAP
, seq
, (char *)emacs_meta_keymap
, _rl_keymap
);
904 #endif /* PREFIX_META_HACK */
906 rl_bind_key (key
, rl_named_function (funname
));
909 /* Simple structure for boolean readline variables (i.e., those that can
910 have one of two values; either "On" or 1 for truth, or "Off" or 0 for
916 } boolean_varlist
[] = {
917 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode
},
918 { "mark-modified-lines", &_rl_mark_modified_lines
},
919 { "prefer-visible-bell", &_rl_prefer_visible_bell
},
920 { "meta-flag", &_rl_meta_flag
},
921 { "blink-matching-paren", &rl_blink_matching_paren
},
922 { "convert-meta", &_rl_convert_meta_chars_to_ascii
},
923 #if defined (VISIBLE_STATS)
924 { "visible-stats", &rl_visible_stats
},
925 #endif /* VISIBLE_STATS */
926 { "expand-tilde", &rl_complete_with_tilde_expansion
},
927 { (char *)NULL
, (int *)NULL
}
930 rl_variable_bind (name
, value
)
935 /* Check for simple variables first. */
936 for (i
= 0; boolean_varlist
[i
].name
; i
++)
938 if (stricmp (name
, boolean_varlist
[i
].name
) == 0)
940 /* A variable is TRUE if the "value" is "on", "1" or "". */
942 (stricmp (value
, "On") == 0) ||
943 (value
[0] == '1' && value
[1] == '\0'))
944 *boolean_varlist
[i
].value
= 1;
946 *boolean_varlist
[i
].value
= 0;
951 /* Not a boolean variable, so check for specials. */
953 /* Editing mode change? */
954 if (stricmp (name
, "editing-mode") == 0)
956 if (strnicmp (value
, "vi", 2) == 0)
958 #if defined (VI_MODE)
959 _rl_keymap
= vi_insertion_keymap
;
960 rl_editing_mode
= vi_mode
;
963 /* What state is the terminal in? I'll tell you:
964 non-determinate! That means we cannot do any output. */
969 else if (strnicmp (value
, "emacs", 5) == 0)
971 _rl_keymap
= emacs_standard_keymap
;
972 rl_editing_mode
= emacs_mode
;
976 /* Comment string change? */
977 else if (stricmp (name
, "comment-begin") == 0)
979 #if defined (VI_MODE)
980 extern char *rl_vi_comment_begin
;
984 if (rl_vi_comment_begin
)
985 free (rl_vi_comment_begin
);
987 rl_vi_comment_begin
= savestring (value
);
991 else if (stricmp (name
, "completion-query-items") == 0)
1000 rl_completion_query_items
= nval
;
1004 /* Return the character which matches NAME.
1005 For example, `Space' returns ' '. */
1012 static assoc_list name_key_alist
[] = {
1015 { "Escape", '\033' },
1017 { "Newline", '\n' },
1028 glean_key_from_name (name
)
1033 for (i
= 0; name_key_alist
[i
].name
; i
++)
1034 if (stricmp (name
, name_key_alist
[i
].name
) == 0)
1035 return (name_key_alist
[i
].value
);
1037 return (*(unsigned char *)name
); /* XXX was return (*name) */
1040 /* Auxiliary functions to manage keymaps. */
1044 } keymap_names
[] = {
1045 { "emacs", emacs_standard_keymap
},
1046 { "emacs-standard", emacs_standard_keymap
},
1047 { "emacs-meta", emacs_meta_keymap
},
1048 { "emacs-ctlx", emacs_ctlx_keymap
},
1049 #if defined (VI_MODE)
1050 { "vi", vi_movement_keymap
},
1051 { "vi-move", vi_movement_keymap
},
1052 { "vi-command", vi_movement_keymap
},
1053 { "vi-insert", vi_insertion_keymap
},
1054 #endif /* VI_MODE */
1055 { (char *)0x0, (Keymap
)0x0 }
1059 rl_get_keymap_by_name (name
)
1064 for (i
= 0; keymap_names
[i
].name
; i
++)
1065 if (strcmp (name
, keymap_names
[i
].name
) == 0)
1066 return (keymap_names
[i
].map
);
1067 return ((Keymap
) NULL
);
1081 return (_rl_keymap
);
1085 rl_set_keymap_from_edit_mode ()
1087 if (rl_editing_mode
== emacs_mode
)
1088 _rl_keymap
= emacs_standard_keymap
;
1089 else if (rl_editing_mode
== vi_mode
)
1090 _rl_keymap
= vi_insertion_keymap
;
1093 /* **************************************************************** */
1095 /* Key Binding and Function Information */
1097 /* **************************************************************** */
1099 /* Each of the following functions produces information about the
1100 state of keybindings and functions known to Readline. The info
1101 is always printed to rl_outstream, and in such a way that it can
1102 be read back in (i.e., passed to rl_parse_and_bind (). */
1104 /* Print the names of functions known to Readline. */
1106 rl_list_funmap_names (ignore
)
1110 char **funmap_names
;
1112 funmap_names
= rl_funmap_names ();
1117 for (i
= 0; funmap_names
[i
]; i
++)
1118 fprintf (rl_outstream
, "%s\n", funmap_names
[i
]);
1120 free (funmap_names
);
1123 /* Return a NULL terminated array of strings which represent the key
1124 sequences that are used to invoke FUNCTION in MAP. */
1126 invoking_keyseqs_in_map (function
, map
)
1132 int result_index
, result_size
;
1134 result
= (char **)NULL
;
1135 result_index
= result_size
= 0;
1137 for (key
= 0; key
< 128; key
++)
1139 switch (map
[key
].type
)
1142 /* Macros match, if, and only if, the pointers are identical.
1143 Thus, they are treated exactly like functions in here. */
1145 /* If the function in the keymap is the one we are looking for,
1146 then add the current KEY to the list of invoking keys. */
1147 if (map
[key
].function
== function
)
1149 char *keyname
= (char *)xmalloc (5);
1152 sprintf (keyname
, "\\C-%c", to_lower (UNCTRL (key
)));
1153 else if (key
== RUBOUT
)
1154 sprintf (keyname
, "\\C-?");
1155 else if (key
== '\\' || key
== '"')
1158 keyname
[1] = (char) key
;
1163 keyname
[0] = (char) key
;
1167 if (result_index
+ 2 > result_size
)
1168 result
= (char **) xrealloc
1169 (result
, (result_size
+= 10) * sizeof (char *));
1171 result
[result_index
++] = keyname
;
1172 result
[result_index
] = (char *)NULL
;
1178 char **seqs
= (char **)NULL
;
1180 /* Find the list of keyseqs in this map which have FUNCTION as
1181 their target. Add the key sequences found to RESULT. */
1182 if (map
[key
].function
)
1184 invoking_keyseqs_in_map (function
, (Keymap
)map
[key
].function
);
1190 for (i
= 0; seqs
[i
]; i
++)
1192 char *keyname
= (char *)xmalloc (6 + strlen (seqs
[i
]));
1195 sprintf (keyname
, "\\e");
1196 else if (CTRL_P (key
))
1197 sprintf (keyname
, "\\C-%c", to_lower (UNCTRL (key
)));
1198 else if (key
== RUBOUT
)
1199 sprintf (keyname
, "\\C-?");
1200 else if (key
== '\\' || key
== '"')
1203 keyname
[1] = (char) key
;
1208 keyname
[0] = (char) key
;
1212 strcat (keyname
, seqs
[i
]);
1214 if (result_index
+ 2 > result_size
)
1215 result
= (char **) xrealloc
1216 (result
, (result_size
+= 10) * sizeof (char *));
1218 result
[result_index
++] = keyname
;
1219 result
[result_index
] = (char *)NULL
;
1229 /* Return a NULL terminated array of strings which represent the key
1230 sequences that can be used to invoke FUNCTION using the current keymap. */
1232 rl_invoking_keyseqs (function
)
1235 return (invoking_keyseqs_in_map (function
, _rl_keymap
));
1238 /* Print all of the current functions and their bindings to
1239 rl_outstream. If an explicit argument is given, then print
1240 the output in such a way that it can be read back in. */
1242 rl_dump_functions (count
)
1245 void rl_function_dumper ();
1247 rl_function_dumper (rl_explicit_arg
);
1252 /* Print all of the functions and their bindings to rl_outstream. If
1253 PRINT_READABLY is non-zero, then print the output in such a way
1254 that it can be read back in. */
1256 rl_function_dumper (print_readably
)
1263 names
= rl_funmap_names ();
1265 fprintf (rl_outstream
, "\n");
1267 for (i
= 0; name
= names
[i
]; i
++)
1272 function
= rl_named_function (name
);
1273 invokers
= invoking_keyseqs_in_map (function
, _rl_keymap
);
1278 fprintf (rl_outstream
, "# %s (not bound)\n", name
);
1283 for (j
= 0; invokers
[j
]; j
++)
1285 fprintf (rl_outstream
, "\"%s\": %s\n",
1296 fprintf (rl_outstream
, "%s is not bound to any keys\n",
1302 fprintf (rl_outstream
, "%s can be found on ", name
);
1304 for (j
= 0; invokers
[j
] && j
< 5; j
++)
1306 fprintf (rl_outstream
, "\"%s\"%s", invokers
[j
],
1307 invokers
[j
+ 1] ? ", " : ".\n");
1310 if (j
== 5 && invokers
[j
])
1311 fprintf (rl_outstream
, "...\n");
1313 for (j
= 0; invokers
[j
]; j
++)
1323 /* **************************************************************** */
1325 /* String Utility Functions */
1327 /* **************************************************************** */
1329 static char *strindex ();
1331 /* Return non-zero if any members of ARRAY are a substring in STRING. */
1333 substring_member_of_array (string
, array
)
1334 char *string
, **array
;
1338 if (strindex (string
, *array
))
1345 /* Whoops, Unix doesn't have strnicmp. */
1347 /* Compare at most COUNT characters from string1 to string2. Case
1350 strnicmp (string1
, string2
, count
)
1351 char *string1
, *string2
;
1353 register char ch1
, ch2
;
1359 if (to_upper(ch1
) == to_upper(ch2
))
1366 /* strcmp (), but caseless. */
1368 stricmp (string1
, string2
)
1369 char *string1
, *string2
;
1371 register char ch1
, ch2
;
1373 while (*string1
&& *string2
)
1377 if (to_upper(ch1
) != to_upper(ch2
))
1380 return (*string1
| *string2
);
1383 /* Determine if s2 occurs in s1. If so, return a pointer to the
1384 match in s1. The compare is case insensitive. */
1387 register char *s1
, *s2
;
1389 register int i
, l
= strlen (s2
);
1390 register int len
= strlen (s1
);
1392 for (i
= 0; (len
- i
) >= l
; i
++)
1393 if (strnicmp (&s1
[i
], s2
, l
) == 0)
1395 return ((char *)NULL
);