Deleted Added
full compact
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

--- 56 unchanged lines hidden (view full) ---

65extern int _rl_horizontal_scroll_mode;
66extern int _rl_mark_modified_lines;
67extern int _rl_bell_preference;
68extern int _rl_meta_flag;
69extern int _rl_convert_meta_chars_to_ascii;
70extern int _rl_output_meta_chars;
71extern int _rl_complete_show_all;
72extern int _rl_complete_mark_directories;
73extern int _rl_print_completions_horizontally;
74extern int _rl_completion_case_fold;
75extern int _rl_enable_keypad;
76#if defined (PAREN_MATCHING)
77extern int rl_blink_matching_paren;
78#endif /* PAREN_MATCHING */
79#if defined (VISIBLE_STATS)
80extern int rl_visible_stats;
81#endif /* VISIBLE_STATS */
82extern int rl_complete_with_tilde_expansion;

--- 19 unchanged lines hidden (view full) ---

102extern char *get_env_value ();
103
104/* Variables exported by this file. */
105Keymap rl_binding_keymap;
106
107/* Forward declarations */
108void rl_set_keymap_from_edit_mode ();
109
110static int _rl_read_init_file ();
111static int glean_key_from_name ();
112static int substring_member_of_array ();
113
114extern char *xmalloc (), *xrealloc ();
115
116/* **************************************************************** */
117/* */
118/* Binding keys */

--- 77 unchanged lines hidden (view full) ---

196int
197rl_unbind_key_in_map (key, map)
198 int key;
199 Keymap map;
200{
201 return (rl_bind_key_in_map (key, (Function *)NULL, map));
202}
203
204/* Unbind all keys bound to FUNCTION in MAP. */
205int
206rl_unbind_function_in_map (func, map)
207 Function *func;
208 Keymap map;
209{
210 register int i;
211
212 for (i = 0; i < KEYMAP_SIZE; i++)
213 {
214 if (map[i].type == ISFUNC && map[i].function == func)
215 map[i].function = (Function *)NULL;
216 }
217}
218
219int
220rl_unbind_command_in_map (command, map)
221 char *command;
222 Keymap map;
223{
224 Function *func;
225 register int i;
226
227 func = rl_named_function (command);
228 if (func == 0)
229 return 0;
230 return (rl_unbind_function_in_map (func, map));
231}
232
233/* Bind the key sequence represented by the string KEYSEQ to
234 FUNCTION. This makes new keymaps as necessary. The initial
235 place to do bindings is in MAP. */
236int
237rl_set_key (keyseq, function, map)
238 char *keyseq;
239 Function *function;
240 Keymap map;

--- 99 unchanged lines hidden (view full) ---

340/* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
341 an array of characters. LEN gets the final length of ARRAY. Return
342 non-zero if there was an error parsing SEQ. */
343int
344rl_translate_keyseq (seq, array, len)
345 char *seq, *array;
346 int *len;
347{
316 register int i, c, l;
348 register int i, c, l, temp;
349
350 for (i = l = 0; c = seq[i]; i++)
351 {
352 if (c == '\\')
353 {
354 c = seq[++i];
355
356 if (c == 0)
357 break;
358
327 if (((c == 'C' || c == 'M') && seq[i + 1] == '-') || (c == 'e'))
359 /* Handle \C- and \M- prefixes. */
360 if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
361 {
362 /* Handle special case of backwards define. */
363 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
364 {
365 array[l++] = ESC;
366 i += 5;
367 array[l++] = CTRL (_rl_to_upper (seq[i]));
335 if (!seq[i])
368 if (seq[i] == '\0')
369 i--;
337 continue;
370 }
339
340 switch (c)
371 else if (c == 'M')
372 {
342 case 'M':
373 i++;
374 array[l++] = ESC; /* XXX */
345 break;
346
347 case 'C':
375 }
376 else if (c == 'C')
377 {
378 i += 2;
379 /* Special hack for C-?... */
380 array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
351 break;
352
353 case 'e':
354 array[l++] = ESC;
381 }
356
382 continue;
383 }
384
385 /* Translate other backslash-escaped characters. These are the
386 same escape sequences that bash's `echo' and `printf' builtins
387 handle, with the addition of \d -> RUBOUT. A backslash
388 preceding a character that is not special is stripped. */
389 switch (c)
390 {
391 case 'a':
392 array[l++] = '\007';
393 break;
394 case 'b':
395 array[l++] = '\b';
396 break;
397 case 'd':
398 array[l++] = RUBOUT; /* readline-specific */
399 break;
400 case 'e':
401 array[l++] = ESC;
402 break;
403 case 'f':
404 array[l++] = '\f';
405 break;
406 case 'n':
407 array[l++] = NEWLINE;
408 break;
409 case 'r':
410 array[l++] = RETURN;
411 break;
412 case 't':
413 array[l++] = TAB;
414 break;
415 case 'v':
416 array[l++] = 0x0B;
417 break;
418 case '\\':
419 array[l++] = '\\';
420 break;
421 case '0': case '1': case '2': case '3':
422 case '4': case '5': case '6': case '7':
423 i++;
424 for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++)
425 c = (c * 8) + OCTVALUE (seq[i]);
426 i--; /* auto-increment in for loop */
427 array[l++] = c % (largest_char + 1);
428 break;
429 case 'x':
430 i++;
431 for (temp = 3, c = 0; isxdigit (seq[i]) && temp--; i++)
432 c = (c * 16) + HEXVALUE (seq[i]);
433 if (temp == 3)
434 c = 'x';
435 i--; /* auto-increment in for loop */
436 array[l++] = c % (largest_char + 1);
437 break;
438 default: /* backslashes before non-special chars just add the char */
439 array[l++] = c;
440 break; /* the backslash is stripped */
441 }
442 continue;
443 }
444
445 array[l++] = c;
446 }
447
448 *len = l;
449 array[l] = '\0';
450 return (0);
451}
452

--- 168 unchanged lines hidden (view full) ---

621 return ((Function *) NULL);
622}
623
624/* The last key bindings file read. */
625static char *last_readline_init_file = (char *)NULL;
626
627/* The file we're currently reading key bindings from. */
628static char *current_readline_init_file;
629static int current_readline_init_include_level;
630static int current_readline_init_lineno;
631
632/* Read FILENAME into a locally-allocated buffer and return the buffer.
633 The size of the buffer is returned in *SIZEP. Returns NULL if any
634 errors were encountered. */
635static char *
636_rl_read_file (filename, sizep)
637 char *filename;
638 size_t *sizep;
639{
640 struct stat finfo;
641 size_t file_size;
642 char *buffer;
643 int i, file;
644
645 if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY, 0666)) < 0)
646 return ((char *)NULL);
647
648 file_size = (size_t)finfo.st_size;
649
650 /* check for overflow on very large files */
651 if (file_size != finfo.st_size || file_size + 1 < file_size)
652 {
653 if (file >= 0)
654 close (file);
655#if defined (EFBIG)
656 errno = EFBIG;
657#endif
658 return ((char *)NULL);
659 }
660
661 /* Read the file into BUFFER. */
662 buffer = (char *)xmalloc (file_size + 1);
663 i = read (file, buffer, file_size);
664 close (file);
665
666 if (i < file_size)
667 {
668 free (buffer);
669 return ((char *)NULL);
670 }
671
672 buffer[file_size] = '\0';
673 if (sizep)
674 *sizep = file_size;
675 return (buffer);
676}
677
678/* Re-read the current keybindings file. */
679int
680rl_re_read_init_file (count, ignore)
681 int count, ignore;
682{
683 int r;
684 r = rl_read_init_file ((char *)NULL);
685 rl_set_keymap_from_edit_mode ();

--- 6 unchanged lines hidden (view full) ---

692 2. the value of the shell variable `INPUTRC'
693 3. ~/.inputrc
694 If the file existed and could be opened and read, 0 is returned,
695 otherwise errno is returned. */
696int
697rl_read_init_file (filename)
698 char *filename;
699{
568 register int i;
569 char *buffer, *openname, *line, *end;
570 struct stat finfo;
571 int file;
572
700 /* Default the filename. */
701 if (filename == 0)
702 {
703 filename = last_readline_init_file;
704 if (filename == 0)
705 filename = get_env_value ("INPUTRC");
706 if (filename == 0)
707 filename = DEFAULT_INPUTRC;
708 }
709
710 if (*filename == 0)
711 filename = DEFAULT_INPUTRC;
712
586 current_readline_init_file = filename;
587 openname = tilde_expand (filename);
713 return (_rl_read_init_file (filename, 0));
714}
715
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);
716static int
717_rl_read_init_file (filename, include_level)
718 char *filename;
719 int include_level;
720{
721 register int i;
722 char *buffer, *openname, *line, *end;
723 size_t file_size;
724
598 if (filename != last_readline_init_file)
599 {
600 if (last_readline_init_file)
601 free (last_readline_init_file);
725 current_readline_init_file = filename;
726 current_readline_init_include_level = include_level;
727
728 openname = tilde_expand (filename);
729 buffer = _rl_read_file (openname, &file_size);
730 if (buffer == 0)
731 return (errno);
732
733 if (include_level == 0 && filename != last_readline_init_file)
734 {
735 FREE (last_readline_init_file);
736 last_readline_init_file = savestring (filename);
737 }
738
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
739 /* Loop over the lines in the file. Lines that start with `#' are
740 comments; all other lines are commands for readline initialization. */
741 current_readline_init_lineno = 1;
742 line = buffer;
618 end = buffer + finfo.st_size;
743 end = buffer + file_size;
744 while (line < end)
745 {
746 /* Find the end of this line. */
747 for (i = 0; line + i != end && line[i] != '\n'; i++);
748
749 /* Mark end of line. */
750 line[i] = '\0';
751

--- 7 unchanged lines hidden (view full) ---

759 /* If the line is not a comment, then parse it. */
760 if (*line && *line != '#')
761 rl_parse_and_bind (line);
762
763 /* Move to the next line. */
764 line += i + 1;
765 current_readline_init_lineno++;
766 }
767
768 free (buffer);
769 return (0);
770}
771
772static void
773_rl_init_file_error (msg)
774 char *msg;
775{

--- 42 unchanged lines hidden (view full) ---

818 return 0;
819
820 /* Isolate first argument. */
821 for (i = 0; args[i] && !whitespace (args[i]); i++);
822
823 if (args[i])
824 args[i++] = '\0';
825
700 /* Handle "if term=foo" and "if mode=emacs" constructs. If this
826 /* Handle "$if term=foo" and "$if mode=emacs" constructs. If this
827 isn't term=foo, or mode=emacs, then check to see if the first
828 word in ARGS is the same as the value stored in rl_readline_name. */
829 if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
830 {
831 char *tem, *tname;
832
833 /* Terminals like "aaa-60" are equivalent to "aaa". */
834 tname = savestring (rl_terminal_name);

--- 35 unchanged lines hidden (view full) ---

870
871/* Invert the current parser state if there is anything on the stack. */
872static int
873parser_else (args)
874 char *args;
875{
876 register int i;
877
752 if (!if_stack_depth)
878 if (if_stack_depth == 0)
879 {
754 /* Error message? */
880 _rl_init_file_error ("$else found without matching $if");
881 return 0;
882 }
883
884 /* Check the previous (n - 1) levels of the stack to make sure that
885 we haven't previously turned off parsing. */
886 for (i = 0; i < if_stack_depth - 1; i++)
887 if (if_stack[i] == 1)
888 return 0;

--- 7 unchanged lines hidden (view full) ---

896 _rl_parsing_conditionalized_out from the stack. */
897static int
898parser_endif (args)
899 char *args;
900{
901 if (if_stack_depth)
902 _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
903 else
778 {
779 /* *** What, no error message? *** */
780 }
904 _rl_init_file_error ("$endif without matching $if");
905 return 0;
906}
907
908static int
909parser_include (args)
910 char *args;
911{
912 char *old_init_file, *e;
913 int old_line_number, old_include_level, r;
914
915 if (_rl_parsing_conditionalized_out)
916 return (0);
917
918 old_init_file = current_readline_init_file;
919 old_line_number = current_readline_init_lineno;
920 old_include_level = current_readline_init_include_level;
921
922 e = strchr (args, '\n');
923 if (e)
924 *e = '\0';
925 r = _rl_read_init_file (args, old_include_level + 1);
926
927 current_readline_init_file = old_init_file;
928 current_readline_init_lineno = old_line_number;
929 current_readline_init_include_level = old_include_level;
930
931 return r;
932}
933
934/* Associate textual names with actual functions. */
935static struct {
936 char *name;
937 Function *function;
938} parser_directives [] = {
939 { "if", parser_if },
940 { "endif", parser_endif },
941 { "else", parser_else },
942 { "include", parser_include },
943 { (char *)0x0, (Function *)0x0 }
944};
945
946/* Handle a parser directive. STATEMENT is the line of the directive
947 without any leading `$'. */
948static int
949handle_parser_directive (statement)
950 char *statement;

--- 20 unchanged lines hidden (view full) ---

971 /* Lookup the command, and act on it. */
972 for (i = 0; parser_directives[i].name; i++)
973 if (_rl_stricmp (directive, parser_directives[i].name) == 0)
974 {
975 (*parser_directives[i].function) (args);
976 return (0);
977 }
978
828 /* *** Should an error message be output? */
979 /* display an error message about the unknown parser directive */
980 _rl_init_file_error ("unknown parser directive");
981 return (1);
982}
983
984/* Read the binding command from STRING and perform it.
985 A key binding command looks like: Keyname: function-name\0,
986 a variable binding command looks like: set variable value.
987 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
988int

--- 98 unchanged lines hidden (view full) ---

1087 to the matching delimiter. We allow the backslash to quote the
1088 delimiter characters in the macro body. */
1089 /* This code exists to allow whitespace in macro expansions, which
1090 would otherwise be gobbled up by the next `for' loop.*/
1091 /* XXX - it may be desirable to allow backslash quoting only if " is
1092 the quoted string delimiter, like the shell. */
1093 if (*funname == '\'' || *funname == '"')
1094 {
943 int delimiter = string[i++];
944 int passc = 0;
1095 int delimiter = string[i++], passc;
1096
946 for (; c = string[i]; i++)
1097 for (passc = 0; c = string[i]; i++)
1098 {
1099 if (passc)
1100 {
1101 passc = 0;
1102 continue;
1103 }
1104
1105 if (c == '\\')

--- 21 unchanged lines hidden (view full) ---

1127 {
1128 return 0;
1129 }
1130
1131 /* If this is a new-style key-binding, then do the binding with
1132 rl_set_key (). Otherwise, let the older code deal with it. */
1133 if (*string == '"')
1134 {
984 char *seq = xmalloc (1 + strlen (string));
985 register int j, k = 0;
986 int passc = 0;
1135 char *seq;
1136 register int j, k, passc;
1137
988 for (j = 1; string[j]; j++)
1138 seq = xmalloc (1 + strlen (string));
1139 for (j = 1, k = passc = 0; string[j]; j++)
1140 {
1141 /* Allow backslash to quote characters, but leave them in place.
1142 This allows a string to end with a backslash quoting another
1143 backslash, or with a backslash quoting a double quote. The
1144 backslashes are left in place for rl_translate_keyseq (). */
1145 if (passc || (string[j] == '\\'))
1146 {
1147 seq[k++] = string[j];

--- 76 unchanged lines hidden (view full) ---

1224
1225static struct {
1226 char *name;
1227 int *value;
1228} boolean_varlist [] = {
1229#if defined (PAREN_MATCHING)
1230 { "blink-matching-paren", &rl_blink_matching_paren },
1231#endif
1232 { "completion-ignore-case", &_rl_completion_case_fold },
1233 { "convert-meta", &_rl_convert_meta_chars_to_ascii },
1234 { "disable-completion", &rl_inhibit_completion },
1235 { "enable-keypad", &_rl_enable_keypad },
1236 { "expand-tilde", &rl_complete_with_tilde_expansion },
1237 { "horizontal-scroll-mode", &_rl_horizontal_scroll_mode },
1238 { "input-meta", &_rl_meta_flag },
1239 { "mark-directories", &_rl_complete_mark_directories },
1240 { "mark-modified-lines", &_rl_mark_modified_lines },
1241 { "meta-flag", &_rl_meta_flag },
1242 { "output-meta", &_rl_output_meta_chars },
1243 { "print-completions-horizontally", &_rl_print_completions_horizontally },
1244 { "show-all-if-ambiguous", &_rl_complete_show_all },
1245#if defined (VISIBLE_STATS)
1246 { "visible-stats", &rl_visible_stats },
1247#endif /* VISIBLE_STATS */
1248 { (char *)NULL, (int *)NULL }
1249};
1250
1251int

--- 82 unchanged lines hidden (view full) ---

1334 /* Backwards compatibility. */
1335 if (*value && (_rl_stricmp (value, "on") == 0 ||
1336 (*value == '1' && !value[1])))
1337 _rl_bell_preference = VISIBLE_BELL;
1338 else
1339 _rl_bell_preference = AUDIBLE_BELL;
1340 }
1341
1342 /* For the time being, unknown variable names are simply ignored. */
1343 return 0;
1344}
1345
1346/* Return the character which matches NAME.
1347 For example, `Space' returns ' '. */
1348
1349typedef struct {
1350 char *name;

--- 136 unchanged lines hidden (view full) ---

1487 free (funmap_names);
1488}
1489
1490static char *
1491_rl_get_keyname (key)
1492 int key;
1493{
1494 char *keyname;
1341 int i, c;
1495 int i, c, v;
1496
1497 keyname = (char *)xmalloc (8);
1498
1499 c = key;
1500 /* Since this is going to be used to write out keysequence-function
1501 pairs for possible inclusion in an inputrc file, we don't want to
1502 do any special meta processing on KEY. */
1503

--- 28 unchanged lines hidden (view full) ---

1532 if (CTRL_CHAR (c))
1533 {
1534 keyname[i++] = '\\';
1535 keyname[i++] = 'C';
1536 keyname[i++] = '-';
1537 c = _rl_to_lower (UNCTRL (c));
1538 }
1539
1540 /* XXX experimental code. Turn the characters that are not ASCII or
1541 ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
1542 This changes C. */
1543 if (c >= 128 && c <= 159)
1544 {
1545 keyname[i++] = '\\';
1546 keyname[i++] = '2';
1547 c -= 128;
1548 keyname[i++] = (c / 8) + '0';
1549 c = (c % 8) + '0';
1550 }
1551
1552 /* Now, if the character needs to be quoted with a backslash, do that. */
1553 if (c == '\\' || c == '"')
1554 keyname[i++] = '\\';
1555
1556 /* Now add the key, terminate the string, and return it. */
1557 keyname[i++] = (char) c;
1558 keyname[i] = '\0';
1559

--- 293 unchanged lines hidden (view full) ---

1853 else
1854 fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
1855 *boolean_varlist[i].value ? "on" : "off");
1856 }
1857
1858 /* bell-style */
1859 switch (_rl_bell_preference)
1860 {
1695 case NO_BELL: kname = "none"; break;
1696 case VISIBLE_BELL: kname = "visible"; break;
1861 case NO_BELL:
1862 kname = "none"; break;
1863 case VISIBLE_BELL:
1864 kname = "visible"; break;
1865 case AUDIBLE_BELL:
1698 default: kname = "audible"; break;
1866 default:
1867 kname = "audible"; break;
1868 }
1869 if (print_readably)
1870 fprintf (rl_outstream, "set bell-style %s\n", kname);
1871 else
1872 fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
1873
1874 /* comment-begin */
1875 if (print_readably)

--- 69 unchanged lines hidden ---