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