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