Import GNU Readline 8.1
[deliverable/binutils-gdb.git] / readline / readline / readline.c
1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
3
4 /* Copyright (C) 1987-2020 Free Software Foundation, Inc.
5
6 This file is part of the GNU Readline Library (Readline), a library
7 for reading lines of text with interactive input and history editing.
8
9 Readline is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Readline is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Readline. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #define READLINE_LIBRARY
24
25 #if defined (HAVE_CONFIG_H)
26 # include <config.h>
27 #endif
28
29 #include <sys/types.h>
30 #include "posixstat.h"
31 #include <fcntl.h>
32 #if defined (HAVE_SYS_FILE_H)
33 # include <sys/file.h>
34 #endif /* HAVE_SYS_FILE_H */
35
36 #if defined (HAVE_UNISTD_H)
37 # include <unistd.h>
38 #endif /* HAVE_UNISTD_H */
39
40 #if defined (HAVE_STDLIB_H)
41 # include <stdlib.h>
42 #else
43 # include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
45
46 #if defined (HAVE_LOCALE_H)
47 # include <locale.h>
48 #endif
49
50 #include <stdio.h>
51 #include "posixjmp.h"
52 #include <errno.h>
53
54 #if !defined (errno)
55 extern int errno;
56 #endif /* !errno */
57
58 /* System-specific feature definitions and include files. */
59 #include "rldefs.h"
60 #include "rlmbutil.h"
61
62 #if defined (__EMX__)
63 # define INCL_DOSPROCESS
64 # include <os2.h>
65 #endif /* __EMX__ */
66
67 /* Some standard library routines. */
68 #include "readline.h"
69 #include "history.h"
70
71 #include "rlprivate.h"
72 #include "rlshell.h"
73 #include "xmalloc.h"
74
75 #ifndef RL_LIBRARY_VERSION
76 # define RL_LIBRARY_VERSION "8.0"
77 #endif
78
79 #ifndef RL_READLINE_VERSION
80 # define RL_READLINE_VERSION 0x0800
81 #endif
82
83 extern void _rl_free_history_entry PARAMS((HIST_ENTRY *));
84
85 #if defined (COLOR_SUPPORT)
86 extern void _rl_parse_colors PARAMS((void)); /* XXX */
87 #endif
88
89
90 /* Forward declarations used in this file. */
91 static char *readline_internal PARAMS((void));
92 static void readline_initialize_everything PARAMS((void));
93
94 static void bind_arrow_keys_internal PARAMS((Keymap));
95 static void bind_arrow_keys PARAMS((void));
96
97 static void bind_bracketed_paste_prefix PARAMS((void));
98
99 static void readline_default_bindings PARAMS((void));
100 static void reset_default_bindings PARAMS((void));
101
102 static int _rl_subseq_result PARAMS((int, Keymap, int, int));
103 static int _rl_subseq_getchar PARAMS((int));
104
105 /* **************************************************************** */
106 /* */
107 /* Line editing input utility */
108 /* */
109 /* **************************************************************** */
110
111 const char *rl_library_version = RL_LIBRARY_VERSION;
112
113 int rl_readline_version = RL_READLINE_VERSION;
114
115 /* True if this is `real' readline as opposed to some stub substitute. */
116 int rl_gnu_readline_p = 1;
117
118 /* A pointer to the keymap that is currently in use.
119 By default, it is the standard emacs keymap. */
120 Keymap _rl_keymap = emacs_standard_keymap;
121
122 /* The current style of editing. */
123 int rl_editing_mode = emacs_mode;
124
125 /* The current insert mode: input (the default) or overwrite */
126 int rl_insert_mode = RL_IM_DEFAULT;
127
128 /* Non-zero if we called this function from _rl_dispatch(). It's present
129 so functions can find out whether they were called from a key binding
130 or directly from an application. */
131 int rl_dispatching;
132
133 /* Non-zero if the previous command was a kill command. */
134 int _rl_last_command_was_kill = 0;
135
136 /* The current value of the numeric argument specified by the user. */
137 int rl_numeric_arg = 1;
138
139 /* Non-zero if an argument was typed. */
140 int rl_explicit_arg = 0;
141
142 /* Temporary value used while generating the argument. */
143 int rl_arg_sign = 1;
144
145 /* Non-zero means we have been called at least once before. */
146 static int rl_initialized;
147
148 #if 0
149 /* If non-zero, this program is running in an EMACS buffer. */
150 static int running_in_emacs;
151 #endif
152
153 /* Flags word encapsulating the current readline state. */
154 unsigned long rl_readline_state = RL_STATE_NONE;
155
156 /* The current offset in the current input line. */
157 int rl_point;
158
159 /* Mark in the current input line. */
160 int rl_mark;
161
162 /* Length of the current input line. */
163 int rl_end;
164
165 /* Make this non-zero to return the current input_line. */
166 int rl_done;
167
168 /* The last function executed by readline. */
169 rl_command_func_t *rl_last_func = (rl_command_func_t *)NULL;
170
171 /* Top level environment for readline_internal (). */
172 procenv_t _rl_top_level;
173
174 /* The streams we interact with. */
175 FILE *_rl_in_stream, *_rl_out_stream;
176
177 /* The names of the streams that we do input and output to. */
178 FILE *rl_instream = (FILE *)NULL;
179 FILE *rl_outstream = (FILE *)NULL;
180
181 /* Non-zero means echo characters as they are read. Defaults to no echo;
182 set to 1 if there is a controlling terminal, we can get its attributes,
183 and the attributes include `echo'. Look at rltty.c:prepare_terminal_settings
184 for the code that sets it. */
185 int _rl_echoing_p = 0;
186
187 /* Current prompt. */
188 char *rl_prompt = (char *)NULL;
189 int rl_visible_prompt_length = 0;
190
191 /* Set to non-zero by calling application if it has already printed rl_prompt
192 and does not want readline to do it the first time. */
193 int rl_already_prompted = 0;
194
195 /* The number of characters read in order to type this complete command. */
196 int rl_key_sequence_length = 0;
197
198 /* If non-zero, then this is the address of a function to call just
199 before readline_internal_setup () prints the first prompt. */
200 rl_hook_func_t *rl_startup_hook = (rl_hook_func_t *)NULL;
201
202 /* Any readline function can set this and have it run just before the user's
203 rl_startup_hook. */
204 rl_hook_func_t *_rl_internal_startup_hook = (rl_hook_func_t *)NULL;
205
206 /* If non-zero, this is the address of a function to call just before
207 readline_internal_setup () returns and readline_internal starts
208 reading input characters. */
209 rl_hook_func_t *rl_pre_input_hook = (rl_hook_func_t *)NULL;
210
211 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
212 static char *the_line;
213
214 /* The character that can generate an EOF. Really read from
215 the terminal driver... just defaulted here. */
216 int _rl_eof_char = CTRL ('D');
217
218 /* Non-zero makes this the next keystroke to read. */
219 int rl_pending_input = 0;
220
221 /* If non-zero when readline_internal returns, it means we found EOF */
222 int _rl_eof_found = 0;
223
224 /* Pointer to a useful terminal name. */
225 const char *rl_terminal_name = (const char *)NULL;
226
227 /* Non-zero means to always use horizontal scrolling in line display. */
228 int _rl_horizontal_scroll_mode = 0;
229
230 /* Non-zero means to display an asterisk at the starts of history lines
231 which have been modified. */
232 int _rl_mark_modified_lines = 0;
233
234 /* The style of `bell' notification preferred. This can be set to NO_BELL,
235 AUDIBLE_BELL, or VISIBLE_BELL. */
236 int _rl_bell_preference = AUDIBLE_BELL;
237
238 /* String inserted into the line by rl_insert_comment (). */
239 char *_rl_comment_begin;
240
241 /* Keymap holding the function currently being executed. */
242 Keymap rl_executing_keymap;
243
244 /* Keymap we're currently using to dispatch. */
245 Keymap _rl_dispatching_keymap;
246
247 /* Non-zero means to erase entire line, including prompt, on empty input lines. */
248 int rl_erase_empty_line = 0;
249
250 /* Non-zero means to read only this many characters rather than up to a
251 character bound to accept-line. */
252 int rl_num_chars_to_read = 0;
253
254 /* Line buffer and maintenance. */
255 char *rl_line_buffer = (char *)NULL;
256 int rl_line_buffer_len = 0;
257
258 /* Key sequence `contexts' */
259 _rl_keyseq_cxt *_rl_kscxt = 0;
260
261 int rl_executing_key;
262 char *rl_executing_keyseq = 0;
263 int _rl_executing_keyseq_size = 0;
264
265 struct _rl_cmd _rl_pending_command;
266 struct _rl_cmd *_rl_command_to_execute = (struct _rl_cmd *)NULL;
267
268 /* Timeout (specified in milliseconds) when reading characters making up an
269 ambiguous multiple-key sequence */
270 int _rl_keyseq_timeout = 500;
271
272 #define RESIZE_KEYSEQ_BUFFER() \
273 do \
274 { \
275 if (rl_key_sequence_length + 2 >= _rl_executing_keyseq_size) \
276 { \
277 _rl_executing_keyseq_size += 16; \
278 rl_executing_keyseq = xrealloc (rl_executing_keyseq, _rl_executing_keyseq_size); \
279 } \
280 } \
281 while (0);
282
283 /* Forward declarations used by the display, termcap, and history code. */
284
285 /* **************************************************************** */
286 /* */
287 /* `Forward' declarations */
288 /* */
289 /* **************************************************************** */
290
291 /* Non-zero means do not parse any lines other than comments and
292 parser directives. */
293 unsigned char _rl_parsing_conditionalized_out = 0;
294
295 /* Non-zero means to convert characters with the meta bit set to
296 escape-prefixed characters so we can indirect through
297 emacs_meta_keymap or vi_escape_keymap. */
298 int _rl_convert_meta_chars_to_ascii = 1;
299
300 /* Non-zero means to output characters with the meta bit set directly
301 rather than as a meta-prefixed escape sequence. */
302 int _rl_output_meta_chars = 0;
303
304 /* Non-zero means to look at the termios special characters and bind
305 them to equivalent readline functions at startup. */
306 int _rl_bind_stty_chars = 1;
307
308 /* Non-zero means to go through the history list at every newline (or
309 whenever rl_done is set and readline returns) and revert each line to
310 its initial state. */
311 int _rl_revert_all_at_newline = 0;
312
313 /* Non-zero means to honor the termios ECHOCTL bit and echo control
314 characters corresponding to keyboard-generated signals. */
315 int _rl_echo_control_chars = 1;
316
317 /* Non-zero means to prefix the displayed prompt with a character indicating
318 the editing mode: @ for emacs, : for vi-command, + for vi-insert. */
319 int _rl_show_mode_in_prompt = 0;
320
321 /* Non-zero means to attempt to put the terminal in `bracketed paste mode',
322 where it will prefix pasted text with an escape sequence and send
323 another to mark the end of the paste. */
324 int _rl_enable_bracketed_paste = BRACKETED_PASTE_DEFAULT;
325 int _rl_enable_active_region = BRACKETED_PASTE_DEFAULT;
326
327 /* **************************************************************** */
328 /* */
329 /* Top Level Functions */
330 /* */
331 /* **************************************************************** */
332
333 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
334 int _rl_meta_flag = 0; /* Forward declaration */
335
336 /* Set up the prompt and expand it. Called from readline() and
337 rl_callback_handler_install (). */
338 int
339 rl_set_prompt (const char *prompt)
340 {
341 FREE (rl_prompt);
342 rl_prompt = prompt ? savestring (prompt) : (char *)NULL;
343 rl_display_prompt = rl_prompt ? rl_prompt : "";
344
345 rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
346 return 0;
347 }
348
349 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
350 none. A return value of NULL means that EOF was encountered. */
351 char *
352 readline (const char *prompt)
353 {
354 char *value;
355 #if 0
356 int in_callback;
357 #endif
358
359 /* If we are at EOF return a NULL string. */
360 if (rl_pending_input == EOF)
361 {
362 rl_clear_pending_input ();
363 return ((char *)NULL);
364 }
365
366 #if 0
367 /* If readline() is called after installing a callback handler, temporarily
368 turn off the callback state to avoid ensuing messiness. Patch supplied
369 by the gdb folks. XXX -- disabled. This can be fooled and readline
370 left in a strange state by a poorly-timed longjmp. */
371 if (in_callback = RL_ISSTATE (RL_STATE_CALLBACK))
372 RL_UNSETSTATE (RL_STATE_CALLBACK);
373 #endif
374
375 rl_set_prompt (prompt);
376
377 rl_initialize ();
378 if (rl_prep_term_function)
379 (*rl_prep_term_function) (_rl_meta_flag);
380
381 #if defined (HANDLE_SIGNALS)
382 rl_set_signals ();
383 #endif
384
385 value = readline_internal ();
386 if (rl_deprep_term_function)
387 (*rl_deprep_term_function) ();
388
389 #if defined (HANDLE_SIGNALS)
390 rl_clear_signals ();
391 #endif
392
393 #if 0
394 if (in_callback)
395 RL_SETSTATE (RL_STATE_CALLBACK);
396 #endif
397
398 #if HAVE_DECL_AUDIT_USER_TTY && defined (HAVE_LIBAUDIT_H) && defined (ENABLE_TTY_AUDIT_SUPPORT)
399 if (value)
400 _rl_audit_tty (value);
401 #endif
402
403 return (value);
404 }
405
406 #if defined (READLINE_CALLBACKS)
407 # define STATIC_CALLBACK
408 #else
409 # define STATIC_CALLBACK static
410 #endif
411
412 STATIC_CALLBACK void
413 readline_internal_setup (void)
414 {
415 char *nprompt;
416
417 _rl_in_stream = rl_instream;
418 _rl_out_stream = rl_outstream;
419
420 /* Enable the meta key only for the duration of readline(), if this
421 terminal has one and the terminal has been initialized */
422 if (_rl_enable_meta & RL_ISSTATE (RL_STATE_TERMPREPPED))
423 _rl_enable_meta_key ();
424
425 if (rl_startup_hook)
426 (*rl_startup_hook) ();
427
428 if (_rl_internal_startup_hook)
429 (*_rl_internal_startup_hook) ();
430
431 rl_deactivate_mark ();
432
433 #if defined (VI_MODE)
434 if (rl_editing_mode == vi_mode)
435 rl_vi_insertion_mode (1, 'i'); /* don't want to reset last */
436 else
437 #endif /* VI_MODE */
438 if (_rl_show_mode_in_prompt)
439 _rl_reset_prompt ();
440
441 /* If we're not echoing, we still want to at least print a prompt, because
442 rl_redisplay will not do it for us. If the calling application has a
443 custom redisplay function, though, let that function handle it. */
444 if (_rl_echoing_p == 0 && rl_redisplay_function == rl_redisplay)
445 {
446 if (rl_prompt && rl_already_prompted == 0)
447 {
448 nprompt = _rl_strip_prompt (rl_prompt);
449 fprintf (_rl_out_stream, "%s", nprompt);
450 fflush (_rl_out_stream);
451 xfree (nprompt);
452 }
453 }
454 else
455 {
456 if (rl_prompt && rl_already_prompted)
457 rl_on_new_line_with_prompt ();
458 else
459 rl_on_new_line ();
460 (*rl_redisplay_function) ();
461 }
462
463 if (rl_pre_input_hook)
464 (*rl_pre_input_hook) ();
465
466 RL_CHECK_SIGNALS ();
467 }
468
469 STATIC_CALLBACK char *
470 readline_internal_teardown (int eof)
471 {
472 char *temp;
473 HIST_ENTRY *entry;
474
475 RL_CHECK_SIGNALS ();
476
477 /* Restore the original of this history line, iff the line that we
478 are editing was originally in the history, AND the line has changed. */
479 entry = current_history ();
480
481 if (entry && rl_undo_list)
482 {
483 temp = savestring (the_line);
484 rl_revert_line (1, 0);
485 entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
486 _rl_free_history_entry (entry);
487
488 strcpy (the_line, temp);
489 xfree (temp);
490 }
491
492 if (_rl_revert_all_at_newline)
493 _rl_revert_all_lines ();
494
495 /* At any rate, it is highly likely that this line has an undo list. Get
496 rid of it now. */
497 if (rl_undo_list)
498 rl_free_undo_list ();
499
500 /* Disable the meta key, if this terminal has one and we were told to use it.
501 The check whether or not we sent the enable string is in
502 _rl_disable_meta_key(); the flag is set in _rl_enable_meta_key */
503 _rl_disable_meta_key ();
504
505 /* Restore normal cursor, if available. */
506 _rl_set_insert_mode (RL_IM_INSERT, 0);
507
508 return (eof ? (char *)NULL : savestring (the_line));
509 }
510
511 void
512 _rl_internal_char_cleanup (void)
513 {
514 #if defined (VI_MODE)
515 /* In vi mode, when you exit insert mode, the cursor moves back
516 over the previous character. We explicitly check for that here. */
517 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
518 rl_vi_check ();
519 #endif /* VI_MODE */
520
521 if (rl_num_chars_to_read && rl_end >= rl_num_chars_to_read)
522 {
523 (*rl_redisplay_function) ();
524 _rl_want_redisplay = 0;
525 rl_newline (1, '\n');
526 }
527
528 if (rl_done == 0)
529 {
530 (*rl_redisplay_function) ();
531 _rl_want_redisplay = 0;
532 }
533
534 /* If the application writer has told us to erase the entire line if
535 the only character typed was something bound to rl_newline, do so. */
536 if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
537 rl_point == 0 && rl_end == 0)
538 _rl_erase_entire_line ();
539 }
540
541 STATIC_CALLBACK int
542 #if defined (READLINE_CALLBACKS)
543 readline_internal_char (void)
544 #else
545 readline_internal_charloop (void)
546 #endif
547 {
548 static int lastc, eof_found;
549 int c, code, lk, r;
550
551 lastc = EOF;
552
553 #if !defined (READLINE_CALLBACKS)
554 eof_found = 0;
555 while (rl_done == 0)
556 {
557 #endif
558 lk = _rl_last_command_was_kill;
559
560 #if defined (HAVE_POSIX_SIGSETJMP)
561 code = sigsetjmp (_rl_top_level, 0);
562 #else
563 code = setjmp (_rl_top_level);
564 #endif
565
566 if (code)
567 {
568 (*rl_redisplay_function) ();
569 _rl_want_redisplay = 0;
570 /* If we get here, we're not being called from something dispatched
571 from _rl_callback_read_char(), which sets up its own value of
572 _rl_top_level (saving and restoring the old, of course), so
573 we can just return here. */
574 if (RL_ISSTATE (RL_STATE_CALLBACK))
575 return (0);
576 }
577
578 if (rl_pending_input == 0)
579 {
580 /* Then initialize the argument and number of keys read. */
581 _rl_reset_argument ();
582 rl_executing_keyseq[rl_key_sequence_length = 0] = '\0';
583 }
584
585 RL_SETSTATE(RL_STATE_READCMD);
586 c = rl_read_key ();
587 RL_UNSETSTATE(RL_STATE_READCMD);
588
589 /* look at input.c:rl_getc() for the circumstances under which this will
590 be returned; punt immediately on read error without converting it to
591 a newline; assume that rl_read_key has already called the signal
592 handler. */
593 if (c == READERR)
594 {
595 #if defined (READLINE_CALLBACKS)
596 RL_SETSTATE(RL_STATE_DONE);
597 return (rl_done = 1);
598 #else
599 eof_found = 1;
600 break;
601 #endif
602 }
603
604 /* EOF typed to a non-blank line is ^D the first time, EOF the second
605 time in a row. This won't return any partial line read from the tty.
606 If we want to change this, to force any existing line to be returned
607 when read(2) reads EOF, for example, this is the place to change. */
608 if (c == EOF && rl_end)
609 {
610 if (RL_SIG_RECEIVED ())
611 {
612 RL_CHECK_SIGNALS ();
613 if (rl_signal_event_hook)
614 (*rl_signal_event_hook) (); /* XXX */
615 }
616
617 /* XXX - reading two consecutive EOFs returns EOF */
618 if (RL_ISSTATE (RL_STATE_TERMPREPPED))
619 {
620 if (lastc == _rl_eof_char || lastc == EOF)
621 rl_end = 0;
622 else
623 c = _rl_eof_char;
624 }
625 else
626 c = NEWLINE;
627 }
628
629 /* The character _rl_eof_char typed to blank line, and not as the
630 previous character is interpreted as EOF. This doesn't work when
631 READLINE_CALLBACKS is defined, so hitting a series of ^Ds will
632 erase all the chars on the line and then return EOF. */
633 if (((c == _rl_eof_char && lastc != c) || c == EOF) && rl_end == 0)
634 {
635 #if defined (READLINE_CALLBACKS)
636 RL_SETSTATE(RL_STATE_DONE);
637 return (rl_done = 1);
638 #else
639 eof_found = 1;
640 break;
641 #endif
642 }
643
644 lastc = c;
645 r = _rl_dispatch ((unsigned char)c, _rl_keymap);
646 RL_CHECK_SIGNALS ();
647
648 if (_rl_command_to_execute)
649 {
650 (*rl_redisplay_function) ();
651
652 rl_executing_keymap = _rl_command_to_execute->map;
653 rl_executing_key = _rl_command_to_execute->key;
654
655 rl_dispatching = 1;
656 RL_SETSTATE(RL_STATE_DISPATCHING);
657 r = (*(_rl_command_to_execute->func)) (_rl_command_to_execute->count, _rl_command_to_execute->key);
658 _rl_command_to_execute = 0;
659 RL_UNSETSTATE(RL_STATE_DISPATCHING);
660 rl_dispatching = 0;
661
662 RL_CHECK_SIGNALS ();
663 }
664
665 /* If there was no change in _rl_last_command_was_kill, then no kill
666 has taken place. Note that if input is pending we are reading
667 a prefix command, so nothing has changed yet. */
668 if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
669 _rl_last_command_was_kill = 0;
670
671 if (_rl_keep_mark_active)
672 _rl_keep_mark_active = 0;
673 else if (rl_mark_active_p ())
674 rl_deactivate_mark ();
675
676 _rl_internal_char_cleanup ();
677
678 #if defined (READLINE_CALLBACKS)
679 return 0;
680 #else
681 }
682
683 return (eof_found);
684 #endif
685 }
686
687 #if defined (READLINE_CALLBACKS)
688 static int
689 readline_internal_charloop (void)
690 {
691 int eof = 1;
692
693 while (rl_done == 0)
694 eof = readline_internal_char ();
695 return (eof);
696 }
697 #endif /* READLINE_CALLBACKS */
698
699 /* Read a line of input from the global rl_instream, doing output on
700 the global rl_outstream.
701 If rl_prompt is non-null, then that is our prompt. */
702 static char *
703 readline_internal (void)
704 {
705 readline_internal_setup ();
706 _rl_eof_found = readline_internal_charloop ();
707 return (readline_internal_teardown (_rl_eof_found));
708 }
709
710 void
711 _rl_init_line_state (void)
712 {
713 rl_point = rl_end = rl_mark = 0;
714 the_line = rl_line_buffer;
715 the_line[0] = 0;
716 }
717
718 void
719 _rl_set_the_line (void)
720 {
721 the_line = rl_line_buffer;
722 }
723
724 #if defined (READLINE_CALLBACKS)
725 _rl_keyseq_cxt *
726 _rl_keyseq_cxt_alloc (void)
727 {
728 _rl_keyseq_cxt *cxt;
729
730 cxt = (_rl_keyseq_cxt *)xmalloc (sizeof (_rl_keyseq_cxt));
731
732 cxt->flags = cxt->subseq_arg = cxt->subseq_retval = 0;
733
734 cxt->okey = 0;
735 cxt->ocxt = _rl_kscxt;
736 cxt->childval = 42; /* sentinel value */
737
738 return cxt;
739 }
740
741 void
742 _rl_keyseq_cxt_dispose (_rl_keyseq_cxt *cxt)
743 {
744 xfree (cxt);
745 }
746
747 void
748 _rl_keyseq_chain_dispose (void)
749 {
750 _rl_keyseq_cxt *cxt;
751
752 while (_rl_kscxt)
753 {
754 cxt = _rl_kscxt;
755 _rl_kscxt = _rl_kscxt->ocxt;
756 _rl_keyseq_cxt_dispose (cxt);
757 }
758 }
759 #endif
760
761 static int
762 _rl_subseq_getchar (int key)
763 {
764 int k;
765
766 if (key == ESC)
767 RL_SETSTATE(RL_STATE_METANEXT);
768 RL_SETSTATE(RL_STATE_MOREINPUT);
769 k = rl_read_key ();
770 RL_UNSETSTATE(RL_STATE_MOREINPUT);
771 if (key == ESC)
772 RL_UNSETSTATE(RL_STATE_METANEXT);
773
774 return k;
775 }
776
777 #if defined (READLINE_CALLBACKS)
778 int
779 _rl_dispatch_callback (_rl_keyseq_cxt *cxt)
780 {
781 int nkey, r;
782
783 /* For now */
784 /* The first time this context is used, we want to read input and dispatch
785 on it. When traversing the chain of contexts back `up', we want to use
786 the value from the next context down. We're simulating recursion using
787 a chain of contexts. */
788 if ((cxt->flags & KSEQ_DISPATCHED) == 0)
789 {
790 nkey = _rl_subseq_getchar (cxt->okey);
791 if (nkey < 0)
792 {
793 _rl_abort_internal ();
794 return -1;
795 }
796 r = _rl_dispatch_subseq (nkey, cxt->dmap, cxt->subseq_arg);
797 cxt->flags |= KSEQ_DISPATCHED;
798 }
799 else
800 r = cxt->childval;
801
802 /* For now */
803 if (r != -3) /* don't do this if we indicate there will be other matches */
804 r = _rl_subseq_result (r, cxt->oldmap, cxt->okey, (cxt->flags & KSEQ_SUBSEQ));
805
806 RL_CHECK_SIGNALS ();
807 /* We only treat values < 0 specially to simulate recursion. */
808 if (r >= 0 || (r == -1 && (cxt->flags & KSEQ_SUBSEQ) == 0)) /* success! or failure! */
809 {
810 _rl_keyseq_chain_dispose ();
811 RL_UNSETSTATE (RL_STATE_MULTIKEY);
812 return r;
813 }
814
815 if (r != -3) /* magic value that says we added to the chain */
816 _rl_kscxt = cxt->ocxt;
817 if (_rl_kscxt)
818 _rl_kscxt->childval = r;
819 if (r != -3)
820 _rl_keyseq_cxt_dispose (cxt);
821
822 return r;
823 }
824 #endif /* READLINE_CALLBACKS */
825
826 /* Do the command associated with KEY in MAP.
827 If the associated command is really a keymap, then read
828 another key, and dispatch into that map. */
829 int
830 _rl_dispatch (register int key, Keymap map)
831 {
832 _rl_dispatching_keymap = map;
833 return _rl_dispatch_subseq (key, map, 0);
834 }
835
836 int
837 _rl_dispatch_subseq (register int key, Keymap map, int got_subseq)
838 {
839 int r, newkey;
840 char *macro;
841 rl_command_func_t *func;
842 #if defined (READLINE_CALLBACKS)
843 _rl_keyseq_cxt *cxt;
844 #endif
845
846 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
847 {
848 if (map[ESC].type == ISKMAP)
849 {
850 if (RL_ISSTATE (RL_STATE_MACRODEF))
851 _rl_add_macro_char (ESC);
852 RESIZE_KEYSEQ_BUFFER ();
853 rl_executing_keyseq[rl_key_sequence_length++] = ESC;
854 map = FUNCTION_TO_KEYMAP (map, ESC);
855 key = UNMETA (key);
856 return (_rl_dispatch (key, map));
857 }
858 else
859 rl_ding ();
860 return 0;
861 }
862
863 if (RL_ISSTATE (RL_STATE_MACRODEF))
864 _rl_add_macro_char (key);
865
866 r = 0;
867 switch (map[key].type)
868 {
869 case ISFUNC:
870 func = map[key].function;
871 if (func)
872 {
873 /* Special case rl_do_lowercase_version (). */
874 if (func == rl_do_lowercase_version)
875 /* Should we do anything special if key == ANYOTHERKEY? */
876 return (_rl_dispatch (_rl_to_lower ((unsigned char)key), map));
877
878 rl_executing_keymap = map;
879 rl_executing_key = key;
880
881 RESIZE_KEYSEQ_BUFFER();
882 rl_executing_keyseq[rl_key_sequence_length++] = key;
883 rl_executing_keyseq[rl_key_sequence_length] = '\0';
884
885 rl_dispatching = 1;
886 RL_SETSTATE(RL_STATE_DISPATCHING);
887 r = (*func) (rl_numeric_arg * rl_arg_sign, key);
888 RL_UNSETSTATE(RL_STATE_DISPATCHING);
889 rl_dispatching = 0;
890
891 /* If we have input pending, then the last command was a prefix
892 command. Don't change the state of rl_last_func. Otherwise,
893 remember the last command executed in this variable. */
894 #if defined (VI_MODE)
895 if (rl_pending_input == 0 && map[key].function != rl_digit_argument && map[key].function != rl_vi_arg_digit)
896 #else
897 if (rl_pending_input == 0 && map[key].function != rl_digit_argument)
898 #endif
899 rl_last_func = map[key].function;
900
901 RL_CHECK_SIGNALS ();
902 }
903 else if (map[ANYOTHERKEY].function)
904 {
905 /* OK, there's no function bound in this map, but there is a
906 shadow function that was overridden when the current keymap
907 was created. Return -2 to note that. */
908 if (RL_ISSTATE (RL_STATE_MACROINPUT))
909 _rl_prev_macro_key ();
910 else
911 _rl_unget_char (key);
912 if (rl_key_sequence_length > 0)
913 rl_executing_keyseq[--rl_key_sequence_length] = '\0';
914 return -2;
915 }
916 else if (got_subseq)
917 {
918 /* Return -1 to note that we're in a subsequence, but we don't
919 have a matching key, nor was one overridden. This means
920 we need to back up the recursion chain and find the last
921 subsequence that is bound to a function. */
922 if (RL_ISSTATE (RL_STATE_MACROINPUT))
923 _rl_prev_macro_key ();
924 else
925 _rl_unget_char (key);
926 if (rl_key_sequence_length > 0)
927 rl_executing_keyseq[--rl_key_sequence_length] = '\0';
928 return -1;
929 }
930 else
931 {
932 #if defined (READLINE_CALLBACKS)
933 RL_UNSETSTATE (RL_STATE_MULTIKEY);
934 _rl_keyseq_chain_dispose ();
935 #endif
936 _rl_abort_internal ();
937 return -1;
938 }
939 break;
940
941 case ISKMAP:
942 if (map[key].function != 0)
943 {
944 #if defined (VI_MODE)
945 /* The only way this test will be true is if a subsequence has been
946 bound starting with ESC, generally the arrow keys. What we do is
947 check whether there's input in the queue, which there generally
948 will be if an arrow key has been pressed, and, if there's not,
949 just dispatch to (what we assume is) rl_vi_movement_mode right
950 away. This is essentially an input test with a zero timeout (by
951 default) or a timeout determined by the value of `keyseq-timeout' */
952 /* _rl_keyseq_timeout specified in milliseconds; _rl_input_queued
953 takes microseconds, so multiply by 1000 */
954 if (rl_editing_mode == vi_mode && key == ESC && map == vi_insertion_keymap &&
955 (RL_ISSTATE (RL_STATE_INPUTPENDING|RL_STATE_MACROINPUT) == 0) &&
956 _rl_pushed_input_available () == 0 &&
957 _rl_input_queued ((_rl_keyseq_timeout > 0) ? _rl_keyseq_timeout*1000 : 0) == 0)
958 return (_rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key)));
959 /* This is a very specific test. It can possibly be generalized in
960 the future, but for now it handles a specific case of ESC being
961 the last character in a keyboard macro. */
962 if (rl_editing_mode == vi_mode && key == ESC && map == vi_insertion_keymap &&
963 (RL_ISSTATE (RL_STATE_INPUTPENDING) == 0) &&
964 (RL_ISSTATE (RL_STATE_MACROINPUT) && _rl_peek_macro_key () == 0) &&
965 _rl_pushed_input_available () == 0 &&
966 _rl_input_queued ((_rl_keyseq_timeout > 0) ? _rl_keyseq_timeout*1000 : 0) == 0)
967 return (_rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key)));
968 #endif
969
970 RESIZE_KEYSEQ_BUFFER ();
971 rl_executing_keyseq[rl_key_sequence_length++] = key;
972 _rl_dispatching_keymap = FUNCTION_TO_KEYMAP (map, key);
973
974 /* Allocate new context here. Use linked contexts (linked through
975 cxt->ocxt) to simulate recursion */
976 #if defined (READLINE_CALLBACKS)
977 # if defined (VI_MODE)
978 /* If we're redoing a vi mode command and we know there is a shadowed
979 function corresponding to this key, just call it -- all the redoable
980 vi mode commands already have all the input they need, and rl_vi_redo
981 assumes that one call to rl_dispatch is sufficient to complete the
982 command. */
983 if (_rl_vi_redoing && RL_ISSTATE (RL_STATE_CALLBACK) &&
984 map[ANYOTHERKEY].function != 0)
985 return (_rl_subseq_result (-2, map, key, got_subseq));
986 # endif
987 if (RL_ISSTATE (RL_STATE_CALLBACK))
988 {
989 /* Return 0 only the first time, to indicate success to
990 _rl_callback_read_char. The rest of the time, we're called
991 from _rl_dispatch_callback, so we return -3 to indicate
992 special handling is necessary. */
993 r = RL_ISSTATE (RL_STATE_MULTIKEY) ? -3 : 0;
994 cxt = _rl_keyseq_cxt_alloc ();
995
996 if (got_subseq)
997 cxt->flags |= KSEQ_SUBSEQ;
998 cxt->okey = key;
999 cxt->oldmap = map;
1000 cxt->dmap = _rl_dispatching_keymap;
1001 cxt->subseq_arg = got_subseq || cxt->dmap[ANYOTHERKEY].function;
1002
1003 RL_SETSTATE (RL_STATE_MULTIKEY);
1004 _rl_kscxt = cxt;
1005
1006 return r; /* don't indicate immediate success */
1007 }
1008 #endif
1009
1010 /* Tentative inter-character timeout for potential multi-key
1011 sequences? If no input within timeout, abort sequence and
1012 act as if we got non-matching input. */
1013 /* _rl_keyseq_timeout specified in milliseconds; _rl_input_queued
1014 takes microseconds, so multiply by 1000 */
1015 if (_rl_keyseq_timeout > 0 &&
1016 (RL_ISSTATE (RL_STATE_INPUTPENDING|RL_STATE_MACROINPUT) == 0) &&
1017 _rl_pushed_input_available () == 0 &&
1018 _rl_dispatching_keymap[ANYOTHERKEY].function &&
1019 _rl_input_queued (_rl_keyseq_timeout*1000) == 0)
1020 {
1021 if (rl_key_sequence_length > 0)
1022 rl_executing_keyseq[--rl_key_sequence_length] = '\0';
1023 return (_rl_subseq_result (-2, map, key, got_subseq));
1024 }
1025
1026 newkey = _rl_subseq_getchar (key);
1027 if (newkey < 0)
1028 {
1029 _rl_abort_internal ();
1030 return -1;
1031 }
1032
1033 r = _rl_dispatch_subseq (newkey, _rl_dispatching_keymap, got_subseq || map[ANYOTHERKEY].function);
1034 return _rl_subseq_result (r, map, key, got_subseq);
1035 }
1036 else
1037 {
1038 _rl_abort_internal (); /* XXX */
1039 return -1;
1040 }
1041 break;
1042
1043 case ISMACR:
1044 if (map[key].function != 0)
1045 {
1046 rl_executing_keyseq[rl_key_sequence_length] = '\0';
1047 macro = savestring ((char *)map[key].function);
1048 _rl_with_macro_input (macro);
1049 return 0;
1050 }
1051 break;
1052 }
1053
1054 #if defined (VI_MODE)
1055 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
1056 key != ANYOTHERKEY &&
1057 _rl_dispatching_keymap == vi_movement_keymap &&
1058 _rl_vi_textmod_command (key))
1059 _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
1060 #endif
1061
1062 return (r);
1063 }
1064
1065 static int
1066 _rl_subseq_result (int r, Keymap map, int key, int got_subseq)
1067 {
1068 Keymap m;
1069 int type, nt;
1070 rl_command_func_t *func, *nf;
1071
1072 if (r == -2)
1073 /* We didn't match anything, and the keymap we're indexed into
1074 shadowed a function previously bound to that prefix. Call
1075 the function. The recursive call to _rl_dispatch_subseq has
1076 already taken care of pushing any necessary input back onto
1077 the input queue with _rl_unget_char. */
1078 {
1079 m = _rl_dispatching_keymap;
1080 type = m[ANYOTHERKEY].type;
1081 func = m[ANYOTHERKEY].function;
1082 if (type == ISFUNC && func == rl_do_lowercase_version)
1083 r = _rl_dispatch (_rl_to_lower ((unsigned char)key), map);
1084 else if (type == ISFUNC)
1085 {
1086 /* If we shadowed a function, whatever it is, we somehow need a
1087 keymap with map[key].func == shadowed-function.
1088 Let's use this one. Then we can dispatch using the original
1089 key, since there are commands (e.g., in vi mode) for which it
1090 matters. */
1091 nt = m[key].type;
1092 nf = m[key].function;
1093
1094 m[key].type = type;
1095 m[key].function = func;
1096 /* Don't change _rl_dispatching_keymap, set it here */
1097 _rl_dispatching_keymap = map; /* previous map */
1098 r = _rl_dispatch_subseq (key, m, 0);
1099 m[key].type = nt;
1100 m[key].function = nf;
1101 }
1102 else
1103 /* We probably shadowed a keymap, so keep going. */
1104 r = _rl_dispatch (ANYOTHERKEY, m);
1105 }
1106 else if (r < 0 && map[ANYOTHERKEY].function)
1107 {
1108 /* We didn't match (r is probably -1), so return something to
1109 tell the caller that it should try ANYOTHERKEY for an
1110 overridden function. */
1111 if (RL_ISSTATE (RL_STATE_MACROINPUT))
1112 _rl_prev_macro_key ();
1113 else
1114 _rl_unget_char (key);
1115 if (rl_key_sequence_length > 0)
1116 rl_executing_keyseq[--rl_key_sequence_length] = '\0';
1117 _rl_dispatching_keymap = map;
1118 return -2;
1119 }
1120 else if (r < 0 && got_subseq) /* XXX */
1121 {
1122 /* OK, back up the chain. */
1123 if (RL_ISSTATE (RL_STATE_MACROINPUT))
1124 _rl_prev_macro_key ();
1125 else
1126 _rl_unget_char (key);
1127 if (rl_key_sequence_length > 0)
1128 rl_executing_keyseq[--rl_key_sequence_length] = '\0';
1129 _rl_dispatching_keymap = map;
1130 return -1;
1131 }
1132
1133 return r;
1134 }
1135
1136 /* **************************************************************** */
1137 /* */
1138 /* Initializations */
1139 /* */
1140 /* **************************************************************** */
1141
1142 /* Initialize readline (and terminal if not already). */
1143 int
1144 rl_initialize (void)
1145 {
1146 /* If we have never been called before, initialize the
1147 terminal and data structures. */
1148 if (rl_initialized == 0)
1149 {
1150 RL_SETSTATE(RL_STATE_INITIALIZING);
1151 readline_initialize_everything ();
1152 RL_UNSETSTATE(RL_STATE_INITIALIZING);
1153 rl_initialized++;
1154 RL_SETSTATE(RL_STATE_INITIALIZED);
1155 }
1156 else
1157 (void)_rl_init_locale (); /* check current locale */
1158
1159 /* Initialize the current line information. */
1160 _rl_init_line_state ();
1161
1162 /* We aren't done yet. We haven't even gotten started yet! */
1163 rl_done = 0;
1164 RL_UNSETSTATE(RL_STATE_DONE);
1165
1166 /* Tell the history routines what is going on. */
1167 _rl_start_using_history ();
1168
1169 /* Make the display buffer match the state of the line. */
1170 rl_reset_line_state ();
1171
1172 /* No such function typed yet. */
1173 rl_last_func = (rl_command_func_t *)NULL;
1174
1175 /* Parsing of key-bindings begins in an enabled state. */
1176 _rl_parsing_conditionalized_out = 0;
1177
1178 #if defined (VI_MODE)
1179 if (rl_editing_mode == vi_mode)
1180 _rl_vi_initialize_line ();
1181 #endif
1182
1183 /* Each line starts in insert mode (the default). */
1184 _rl_set_insert_mode (RL_IM_DEFAULT, 1);
1185
1186 return 0;
1187 }
1188
1189 #if 0
1190 #if defined (__EMX__)
1191 static void
1192 _emx_build_environ (void)
1193 {
1194 TIB *tibp;
1195 PIB *pibp;
1196 char *t, **tp;
1197 int c;
1198
1199 DosGetInfoBlocks (&tibp, &pibp);
1200 t = pibp->pib_pchenv;
1201 for (c = 1; *t; c++)
1202 t += strlen (t) + 1;
1203 tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
1204 t = pibp->pib_pchenv;
1205 while (*t)
1206 {
1207 *tp++ = t;
1208 t += strlen (t) + 1;
1209 }
1210 *tp = 0;
1211 }
1212 #endif /* __EMX__ */
1213 #endif
1214
1215 /* Initialize the entire state of the world. */
1216 static void
1217 readline_initialize_everything (void)
1218 {
1219 #if 0
1220 #if defined (__EMX__)
1221 if (environ == 0)
1222 _emx_build_environ ();
1223 #endif
1224 #endif
1225
1226 #if 0
1227 /* Find out if we are running in Emacs -- UNUSED. */
1228 running_in_emacs = sh_get_env_value ("EMACS") != (char *)0;
1229 #endif
1230
1231 /* Set up input and output if they are not already set up. */
1232 if (!rl_instream)
1233 rl_instream = stdin;
1234
1235 if (!rl_outstream)
1236 rl_outstream = stdout;
1237
1238 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
1239 may change, but they may also be used before readline_internal ()
1240 is called. */
1241 _rl_in_stream = rl_instream;
1242 _rl_out_stream = rl_outstream;
1243
1244 /* Allocate data structures. */
1245 if (rl_line_buffer == 0)
1246 rl_line_buffer = (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
1247
1248 /* Initialize the terminal interface. */
1249 if (rl_terminal_name == 0)
1250 rl_terminal_name = sh_get_env_value ("TERM");
1251 _rl_init_terminal_io (rl_terminal_name);
1252
1253 /* Bind tty characters to readline functions. */
1254 readline_default_bindings ();
1255
1256 /* Initialize the function names. */
1257 rl_initialize_funmap ();
1258
1259 /* Decide whether we should automatically go into eight-bit mode. */
1260 _rl_init_eightbit ();
1261
1262 /* Read in the init file. */
1263 rl_read_init_file ((char *)NULL);
1264
1265 /* XXX */
1266 if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
1267 {
1268 _rl_screenwidth--;
1269 _rl_screenchars -= _rl_screenheight;
1270 }
1271
1272 /* Override the effect of any `set keymap' assignments in the
1273 inputrc file. */
1274 rl_set_keymap_from_edit_mode ();
1275
1276 /* Try to bind a common arrow key prefix, if not already bound. */
1277 bind_arrow_keys ();
1278
1279 /* Bind the bracketed paste prefix assuming that the user will enable
1280 it on terminals that support it. */
1281 bind_bracketed_paste_prefix ();
1282
1283 /* If the completion parser's default word break characters haven't
1284 been set yet, then do so now. */
1285 if (rl_completer_word_break_characters == (char *)NULL)
1286 rl_completer_word_break_characters = (char *)rl_basic_word_break_characters;
1287
1288 #if defined (COLOR_SUPPORT)
1289 if (_rl_colored_stats || _rl_colored_completion_prefix)
1290 _rl_parse_colors ();
1291 #endif
1292
1293 rl_executing_keyseq = malloc (_rl_executing_keyseq_size = 16);
1294 if (rl_executing_keyseq)
1295 rl_executing_keyseq[rl_key_sequence_length = 0] = '\0';
1296 }
1297
1298 /* If this system allows us to look at the values of the regular
1299 input editing characters, then bind them to their readline
1300 equivalents, iff the characters are not bound to keymaps. */
1301 static void
1302 readline_default_bindings (void)
1303 {
1304 if (_rl_bind_stty_chars)
1305 rl_tty_set_default_bindings (_rl_keymap);
1306 }
1307
1308 /* Reset the default bindings for the terminal special characters we're
1309 interested in back to rl_insert and read the new ones. */
1310 static void
1311 reset_default_bindings (void)
1312 {
1313 if (_rl_bind_stty_chars)
1314 {
1315 rl_tty_unset_default_bindings (_rl_keymap);
1316 rl_tty_set_default_bindings (_rl_keymap);
1317 }
1318 }
1319
1320 /* Bind some common arrow key sequences in MAP. */
1321 static void
1322 bind_arrow_keys_internal (Keymap map)
1323 {
1324 Keymap xkeymap;
1325
1326 xkeymap = _rl_keymap;
1327 _rl_keymap = map;
1328
1329 #if defined (__MSDOS__)
1330 rl_bind_keyseq_if_unbound ("\033[0A", rl_get_previous_history);
1331 rl_bind_keyseq_if_unbound ("\033[0B", rl_backward_char);
1332 rl_bind_keyseq_if_unbound ("\033[0C", rl_forward_char);
1333 rl_bind_keyseq_if_unbound ("\033[0D", rl_get_next_history);
1334 #endif
1335
1336 rl_bind_keyseq_if_unbound ("\033[A", rl_get_previous_history);
1337 rl_bind_keyseq_if_unbound ("\033[B", rl_get_next_history);
1338 rl_bind_keyseq_if_unbound ("\033[C", rl_forward_char);
1339 rl_bind_keyseq_if_unbound ("\033[D", rl_backward_char);
1340 rl_bind_keyseq_if_unbound ("\033[H", rl_beg_of_line);
1341 rl_bind_keyseq_if_unbound ("\033[F", rl_end_of_line);
1342
1343 rl_bind_keyseq_if_unbound ("\033OA", rl_get_previous_history);
1344 rl_bind_keyseq_if_unbound ("\033OB", rl_get_next_history);
1345 rl_bind_keyseq_if_unbound ("\033OC", rl_forward_char);
1346 rl_bind_keyseq_if_unbound ("\033OD", rl_backward_char);
1347 rl_bind_keyseq_if_unbound ("\033OH", rl_beg_of_line);
1348 rl_bind_keyseq_if_unbound ("\033OF", rl_end_of_line);
1349
1350 /* Key bindings for control-arrow keys */
1351 rl_bind_keyseq_if_unbound ("\033[1;5C", rl_forward_word);
1352 rl_bind_keyseq_if_unbound ("\033[1;5D", rl_backward_word);
1353 rl_bind_keyseq_if_unbound ("\033[3;5~", rl_kill_word);
1354
1355 /* Key bindings for alt-arrow keys */
1356 rl_bind_keyseq_if_unbound ("\033[1;3C", rl_forward_word);
1357 rl_bind_keyseq_if_unbound ("\033[1;3D", rl_backward_word);
1358
1359 #if defined (__MINGW32__)
1360 rl_bind_keyseq_if_unbound ("\340H", rl_get_previous_history);
1361 rl_bind_keyseq_if_unbound ("\340P", rl_get_next_history);
1362 rl_bind_keyseq_if_unbound ("\340M", rl_forward_char);
1363 rl_bind_keyseq_if_unbound ("\340K", rl_backward_char);
1364 rl_bind_keyseq_if_unbound ("\340G", rl_beg_of_line);
1365 rl_bind_keyseq_if_unbound ("\340O", rl_end_of_line);
1366 rl_bind_keyseq_if_unbound ("\340S", rl_delete);
1367 rl_bind_keyseq_if_unbound ("\340R", rl_overwrite_mode);
1368
1369 /* These may or may not work because of the embedded NUL. */
1370 rl_bind_keyseq_if_unbound ("\\000H", rl_get_previous_history);
1371 rl_bind_keyseq_if_unbound ("\\000P", rl_get_next_history);
1372 rl_bind_keyseq_if_unbound ("\\000M", rl_forward_char);
1373 rl_bind_keyseq_if_unbound ("\\000K", rl_backward_char);
1374 rl_bind_keyseq_if_unbound ("\\000G", rl_beg_of_line);
1375 rl_bind_keyseq_if_unbound ("\\000O", rl_end_of_line);
1376 rl_bind_keyseq_if_unbound ("\\000S", rl_delete);
1377 rl_bind_keyseq_if_unbound ("\\000R", rl_overwrite_mode);
1378 #endif
1379
1380 _rl_keymap = xkeymap;
1381 }
1382
1383 /* Try and bind the common arrow key prefixes after giving termcap and
1384 the inputrc file a chance to bind them and create `real' keymaps
1385 for the arrow key prefix. */
1386 static void
1387 bind_arrow_keys (void)
1388 {
1389 bind_arrow_keys_internal (emacs_standard_keymap);
1390
1391 #if defined (VI_MODE)
1392 bind_arrow_keys_internal (vi_movement_keymap);
1393 /* Unbind vi_movement_keymap[ESC] to allow users to repeatedly hit ESC
1394 in vi command mode while still allowing the arrow keys to work. */
1395 if (vi_movement_keymap[ESC].type == ISKMAP)
1396 rl_bind_keyseq_in_map ("\033", (rl_command_func_t *)NULL, vi_movement_keymap);
1397 bind_arrow_keys_internal (vi_insertion_keymap);
1398 #endif
1399 }
1400
1401 static void
1402 bind_bracketed_paste_prefix (void)
1403 {
1404 Keymap xkeymap;
1405
1406 xkeymap = _rl_keymap;
1407
1408 _rl_keymap = emacs_standard_keymap;
1409 rl_bind_keyseq_if_unbound (BRACK_PASTE_PREF, rl_bracketed_paste_begin);
1410
1411 #if defined (VI_MODE)
1412 _rl_keymap = vi_insertion_keymap;
1413 rl_bind_keyseq_if_unbound (BRACK_PASTE_PREF, rl_bracketed_paste_begin);
1414 /* XXX - is there a reason to do this in the vi command keymap? */
1415 #endif
1416
1417 _rl_keymap = xkeymap;
1418 }
1419
1420 /* **************************************************************** */
1421 /* */
1422 /* Saving and Restoring Readline's state */
1423 /* */
1424 /* **************************************************************** */
1425
1426 int
1427 rl_save_state (struct readline_state *sp)
1428 {
1429 if (sp == 0)
1430 return -1;
1431
1432 sp->point = rl_point;
1433 sp->end = rl_end;
1434 sp->mark = rl_mark;
1435 sp->buffer = rl_line_buffer;
1436 sp->buflen = rl_line_buffer_len;
1437 sp->ul = rl_undo_list;
1438 sp->prompt = rl_prompt;
1439
1440 sp->rlstate = rl_readline_state;
1441 sp->done = rl_done;
1442 sp->kmap = _rl_keymap;
1443
1444 sp->lastfunc = rl_last_func;
1445 sp->insmode = rl_insert_mode;
1446 sp->edmode = rl_editing_mode;
1447 sp->kseq = rl_executing_keyseq;
1448 sp->kseqlen = rl_key_sequence_length;
1449 sp->inf = rl_instream;
1450 sp->outf = rl_outstream;
1451 sp->pendingin = rl_pending_input;
1452 sp->macro = rl_executing_macro;
1453
1454 sp->catchsigs = rl_catch_signals;
1455 sp->catchsigwinch = rl_catch_sigwinch;
1456
1457 sp->entryfunc = rl_completion_entry_function;
1458 sp->menuentryfunc = rl_menu_completion_entry_function;
1459 sp->ignorefunc = rl_ignore_some_completions_function;
1460 sp->attemptfunc = rl_attempted_completion_function;
1461 sp->wordbreakchars = rl_completer_word_break_characters;
1462
1463 return (0);
1464 }
1465
1466 int
1467 rl_restore_state (struct readline_state *sp)
1468 {
1469 if (sp == 0)
1470 return -1;
1471
1472 rl_point = sp->point;
1473 rl_end = sp->end;
1474 rl_mark = sp->mark;
1475 the_line = rl_line_buffer = sp->buffer;
1476 rl_line_buffer_len = sp->buflen;
1477 rl_undo_list = sp->ul;
1478 rl_prompt = sp->prompt;
1479
1480 rl_readline_state = sp->rlstate;
1481 rl_done = sp->done;
1482 _rl_keymap = sp->kmap;
1483
1484 rl_last_func = sp->lastfunc;
1485 rl_insert_mode = sp->insmode;
1486 rl_editing_mode = sp->edmode;
1487 rl_executing_keyseq = sp->kseq;
1488 rl_key_sequence_length = sp->kseqlen;
1489 rl_instream = sp->inf;
1490 rl_outstream = sp->outf;
1491 rl_pending_input = sp->pendingin;
1492 rl_executing_macro = sp->macro;
1493
1494 rl_catch_signals = sp->catchsigs;
1495 rl_catch_sigwinch = sp->catchsigwinch;
1496
1497 rl_completion_entry_function = sp->entryfunc;
1498 rl_menu_completion_entry_function = sp->menuentryfunc;
1499 rl_ignore_some_completions_function = sp->ignorefunc;
1500 rl_attempted_completion_function = sp->attemptfunc;
1501 rl_completer_word_break_characters = sp->wordbreakchars;
1502
1503 rl_deactivate_mark ();
1504
1505 return (0);
1506 }
1507
1508 /* Functions to manage the string that is the current key sequence. */
1509
1510 void
1511 _rl_init_executing_keyseq (void)
1512 {
1513 rl_executing_keyseq[rl_key_sequence_length = 0] = '\0';
1514 }
1515
1516 void
1517 _rl_term_executing_keyseq (void)
1518 {
1519 rl_executing_keyseq[rl_key_sequence_length] = '\0';
1520 }
1521
1522 void
1523 _rl_end_executing_keyseq (void)
1524 {
1525 if (rl_key_sequence_length > 0)
1526 rl_executing_keyseq[--rl_key_sequence_length] = '\0';
1527 }
1528
1529 void
1530 _rl_add_executing_keyseq (int key)
1531 {
1532 RESIZE_KEYSEQ_BUFFER ();
1533 rl_executing_keyseq[rl_key_sequence_length++] = key;
1534 }
This page took 0.086931 seconds and 4 git commands to generate.