1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2, or
12 (at your option) any later version.
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
30 #include "posixstat.h"
32 #if defined (HAVE_SYS_FILE_H)
33 # include <sys/file.h>
34 #endif /* HAVE_SYS_FILE_H */
36 #if defined (HAVE_UNISTD_H)
38 #endif /* HAVE_UNISTD_H */
40 #if defined (HAVE_STDLIB_H)
43 # include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
46 #if defined (HAVE_LOCALE_H)
53 /* System-specific feature definitions and include files. */
57 # define INCL_DOSPROCESS
61 /* Some standard library routines. */
65 #include "rlprivate.h"
69 #ifndef RL_LIBRARY_VERSION
70 # define RL_LIBRARY_VERSION "4.2a"
73 #ifndef RL_READLINE_VERSION
74 # define RL_READLINE_VERSION 0x0402
77 /* Evaluates its arguments multiple times. */
78 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
80 /* Forward declarations used in this file. */
81 void _rl_free_history_entry PARAMS((HIST_ENTRY *));
83 static char *readline_internal PARAMS((void));
84 static void readline_initialize_everything PARAMS((void));
85 static void start_using_history PARAMS((void));
86 static void bind_arrow_keys PARAMS((void));
87 static int rl_change_case PARAMS((int, int));
89 static void readline_default_bindings PARAMS((void));
91 /* **************************************************************** */
93 /* Line editing input utility */
95 /* **************************************************************** */
97 const char *rl_library_version = RL_LIBRARY_VERSION;
99 int rl_readline_version = RL_READLINE_VERSION;
101 /* True if this is `real' readline as opposed to some stub substitute. */
102 int rl_gnu_readline_p = 1;
104 /* A pointer to the keymap that is currently in use.
105 By default, it is the standard emacs keymap. */
106 Keymap _rl_keymap = emacs_standard_keymap;
108 /* The current style of editing. */
109 int rl_editing_mode = emacs_mode;
111 /* Non-zero if we called this function from _rl_dispatch(). It's present
112 so functions can find out whether they were called from a key binding
113 or directly from an application. */
116 /* Non-zero if the previous command was a kill command. */
117 int _rl_last_command_was_kill = 0;
119 /* The current value of the numeric argument specified by the user. */
120 int rl_numeric_arg = 1;
122 /* Non-zero if an argument was typed. */
123 int rl_explicit_arg = 0;
125 /* Temporary value used while generating the argument. */
128 /* Non-zero means we have been called at least once before. */
129 static int rl_initialized;
132 /* If non-zero, this program is running in an EMACS buffer. */
133 static int running_in_emacs;
136 /* Flags word encapsulating the current readline state. */
137 int rl_readline_state = RL_STATE_NONE;
139 /* The current offset in the current input line. */
142 /* Mark in the current input line. */
145 /* Length of the current input line. */
148 /* Make this non-zero to return the current input_line. */
151 /* The last function executed by readline. */
152 rl_command_func_t *rl_last_func = (rl_command_func_t *)NULL;
154 /* Top level environment for readline_internal (). */
155 procenv_t readline_top_level;
157 /* The streams we interact with. */
158 FILE *_rl_in_stream, *_rl_out_stream;
160 /* The names of the streams that we do input and output to. */
161 FILE *rl_instream = (FILE *)NULL;
162 FILE *rl_outstream = (FILE *)NULL;
164 /* Non-zero means echo characters as they are read. Defaults to no echo;
165 set to 1 if there is a controlling terminal, we can get its attributes,
166 and the attributes include `echo'. Look at rltty.c:prepare_terminal_settings
167 for the code that sets it. */
168 int readline_echoing_p = 0;
170 /* Current prompt. */
171 char *rl_prompt = (char *)NULL;
172 int rl_visible_prompt_length = 0;
174 /* Set to non-zero by calling application if it has already printed rl_prompt
175 and does not want readline to do it the first time. */
176 int rl_already_prompted = 0;
178 /* The number of characters read in order to type this complete command. */
179 int rl_key_sequence_length = 0;
181 /* If non-zero, then this is the address of a function to call just
182 before readline_internal_setup () prints the first prompt. */
183 rl_hook_func_t *rl_startup_hook = (rl_hook_func_t *)NULL;
185 /* If non-zero, this is the address of a function to call just before
186 readline_internal_setup () returns and readline_internal starts
187 reading input characters. */
188 rl_hook_func_t *rl_pre_input_hook = (rl_hook_func_t *)NULL;
190 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
191 static char *the_line;
193 /* The character that can generate an EOF. Really read from
194 the terminal driver... just defaulted here. */
195 int _rl_eof_char = CTRL ('D');
197 /* Non-zero makes this the next keystroke to read. */
198 int rl_pending_input = 0;
200 /* Pointer to a useful terminal name. */
201 const char *rl_terminal_name = (const char *)NULL;
203 /* Non-zero means to always use horizontal scrolling in line display. */
204 int _rl_horizontal_scroll_mode = 0;
206 /* Non-zero means to display an asterisk at the starts of history lines
207 which have been modified. */
208 int _rl_mark_modified_lines = 0;
210 /* The style of `bell' notification preferred. This can be set to NO_BELL,
211 AUDIBLE_BELL, or VISIBLE_BELL. */
212 int _rl_bell_preference = AUDIBLE_BELL;
214 /* String inserted into the line by rl_insert_comment (). */
215 char *_rl_comment_begin;
217 /* Keymap holding the function currently being executed. */
218 Keymap rl_executing_keymap;
220 /* Non-zero means to erase entire line, including prompt, on empty input lines. */
221 int rl_erase_empty_line = 0;
223 /* Non-zero means to read only this many characters rather than up to a
224 character bound to accept-line. */
225 int rl_num_chars_to_read;
227 /* Line buffer and maintenence. */
228 char *rl_line_buffer = (char *)NULL;
229 int rl_line_buffer_len = 0;
231 /* Forward declarations used by the display, termcap, and history code. */
233 /* **************************************************************** */
235 /* `Forward' declarations */
237 /* **************************************************************** */
239 /* Non-zero means do not parse any lines other than comments and
240 parser directives. */
241 unsigned char _rl_parsing_conditionalized_out = 0;
243 /* Non-zero means to convert characters with the meta bit set to
244 escape-prefixed characters so we can indirect through
245 emacs_meta_keymap or vi_escape_keymap. */
246 int _rl_convert_meta_chars_to_ascii = 1;
248 /* Non-zero means to output characters with the meta bit set directly
249 rather than as a meta-prefixed escape sequence. */
250 int _rl_output_meta_chars = 0;
252 /* If non-zero, rl_get_previous_history and rl_get_next_history attempt
253 to preserve the value of rl_point from line to line. */
254 int _rl_history_preserve_point = 0;
256 /* Saved target point for when _rl_history_preserve_point is set. Special
257 value of -1 means that point is at the end of the line. */
258 static int _rl_history_saved_point = -1;
260 /* **************************************************************** */
262 /* Top Level Functions */
264 /* **************************************************************** */
266 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
267 int _rl_meta_flag = 0; /* Forward declaration */
269 /* Set up the prompt and expand it. Called from readline() and
270 rl_callback_handler_install (). */
272 rl_set_prompt (prompt)
276 rl_prompt = prompt ? savestring (prompt) : (char *)NULL;
278 rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
282 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
283 none. A return value of NULL means that EOF was encountered. */
290 /* If we are at EOF return a NULL string. */
291 if (rl_pending_input == EOF)
293 rl_clear_pending_input ();
294 return ((char *)NULL);
297 rl_set_prompt (prompt);
300 (*rl_prep_term_function) (_rl_meta_flag);
302 #if defined (HANDLE_SIGNALS)
306 value = readline_internal ();
307 (*rl_deprep_term_function) ();
309 #if defined (HANDLE_SIGNALS)
316 #if defined (READLINE_CALLBACKS)
317 # define STATIC_CALLBACK
319 # define STATIC_CALLBACK static
323 readline_internal_setup ()
327 _rl_in_stream = rl_instream;
328 _rl_out_stream = rl_outstream;
331 (*rl_startup_hook) ();
333 if (readline_echoing_p == 0)
335 if (rl_prompt && rl_already_prompted == 0)
337 nprompt = _rl_strip_prompt (rl_prompt);
338 fprintf (_rl_out_stream, "%s", nprompt);
339 fflush (_rl_out_stream);
345 if (rl_prompt && rl_already_prompted)
346 rl_on_new_line_with_prompt ();
349 (*rl_redisplay_function) ();
350 #if defined (VI_MODE)
351 if (rl_editing_mode == vi_mode)
352 rl_vi_insertion_mode (1, 0);
356 if (rl_pre_input_hook)
357 (*rl_pre_input_hook) ();
360 STATIC_CALLBACK char *
361 readline_internal_teardown (eof)
367 /* Restore the original of this history line, iff the line that we
368 are editing was originally in the history, AND the line has changed. */
369 entry = current_history ();
371 if (entry && rl_undo_list)
373 temp = savestring (the_line);
374 rl_revert_line (1, 0);
375 entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
376 _rl_free_history_entry (entry);
378 strcpy (the_line, temp);
382 /* At any rate, it is highly likely that this line has an undo list. Get
385 rl_free_undo_list ();
387 return (eof ? (char *)NULL : savestring (the_line));
391 #if defined (READLINE_CALLBACKS)
392 readline_internal_char ()
394 readline_internal_charloop ()
397 static int lastc, eof_found;
403 #if !defined (READLINE_CALLBACKS)
407 lk = _rl_last_command_was_kill;
409 code = setjmp (readline_top_level);
412 (*rl_redisplay_function) ();
414 if (rl_pending_input == 0)
416 /* Then initialize the argument and number of keys read. */
417 _rl_init_argument ();
418 rl_key_sequence_length = 0;
421 RL_SETSTATE(RL_STATE_READCMD);
423 RL_UNSETSTATE(RL_STATE_READCMD);
425 /* EOF typed to a non-blank line is a <NL>. */
426 if (c == EOF && rl_end)
429 /* The character _rl_eof_char typed to blank line, and not as the
430 previous character is interpreted as EOF. */
431 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
433 #if defined (READLINE_CALLBACKS)
434 RL_SETSTATE(RL_STATE_DONE);
435 return (rl_done = 1);
443 _rl_dispatch ((unsigned char)c, _rl_keymap);
445 /* If there was no change in _rl_last_command_was_kill, then no kill
446 has taken place. Note that if input is pending we are reading
447 a prefix command, so nothing has changed yet. */
448 if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
449 _rl_last_command_was_kill = 0;
451 #if defined (VI_MODE)
452 /* In vi mode, when you exit insert mode, the cursor moves back
453 over the previous character. We explicitly check for that here. */
454 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
458 if (rl_num_chars_to_read && rl_end >= rl_num_chars_to_read)
460 (*rl_redisplay_function) ();
461 rl_newline (1, '\n');
465 (*rl_redisplay_function) ();
467 /* If the application writer has told us to erase the entire line if
468 the only character typed was something bound to rl_newline, do so. */
469 if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
470 rl_point == 0 && rl_end == 0)
471 _rl_erase_entire_line ();
473 #if defined (READLINE_CALLBACKS)
482 #if defined (READLINE_CALLBACKS)
484 readline_internal_charloop ()
489 eof = readline_internal_char ();
492 #endif /* READLINE_CALLBACKS */
494 /* Read a line of input from the global rl_instream, doing output on
495 the global rl_outstream.
496 If rl_prompt is non-null, then that is our prompt. */
502 readline_internal_setup ();
503 eof = readline_internal_charloop ();
504 return (readline_internal_teardown (eof));
508 _rl_init_line_state ()
510 rl_point = rl_end = 0;
511 the_line = rl_line_buffer;
518 the_line = rl_line_buffer;
521 /* Do the command associated with KEY in MAP.
522 If the associated command is really a keymap, then read
523 another key, and dispatch into that map. */
525 _rl_dispatch (key, map)
531 rl_command_func_t *func;
533 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
535 if (map[ESC].type == ISKMAP)
537 if (_rl_defining_kbd_macro)
538 _rl_add_macro_char (ESC);
539 map = FUNCTION_TO_KEYMAP (map, ESC);
541 rl_key_sequence_length += 2;
542 return (_rl_dispatch (key, map));
549 if (_rl_defining_kbd_macro)
550 _rl_add_macro_char (key);
553 switch (map[key].type)
556 func = map[key].function;
559 /* Special case rl_do_lowercase_version (). */
560 if (func == rl_do_lowercase_version)
561 return (_rl_dispatch (_rl_to_lower (key), map));
563 rl_executing_keymap = map;
566 _rl_suppress_redisplay = (map[key].function == rl_insert) && _rl_input_available ();
570 RL_SETSTATE(RL_STATE_DISPATCHING);
571 r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
572 RL_UNSETSTATE(RL_STATE_DISPATCHING);
575 /* If we have input pending, then the last command was a prefix
576 command. Don't change the state of rl_last_func. Otherwise,
577 remember the last command executed in this variable. */
578 if (rl_pending_input == 0 && map[key].function != rl_digit_argument)
579 rl_last_func = map[key].function;
583 _rl_abort_internal ();
589 if (map[key].function != 0)
591 rl_key_sequence_length++;
594 RL_SETSTATE(RL_STATE_METANEXT);
595 RL_SETSTATE(RL_STATE_MOREINPUT);
596 newkey = rl_read_key ();
597 RL_UNSETSTATE(RL_STATE_MOREINPUT);
599 RL_UNSETSTATE(RL_STATE_METANEXT);
603 _rl_abort_internal ();
607 r = _rl_dispatch (newkey, FUNCTION_TO_KEYMAP (map, key));
611 _rl_abort_internal ();
617 if (map[key].function != 0)
619 macro = savestring ((char *)map[key].function);
620 _rl_with_macro_input (macro);
625 #if defined (VI_MODE)
626 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
627 _rl_vi_textmod_command (key))
628 _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
633 /* **************************************************************** */
635 /* Initializations */
637 /* **************************************************************** */
639 /* Initialize readline (and terminal if not already). */
643 /* If we have never been called before, initialize the
644 terminal and data structures. */
647 RL_SETSTATE(RL_STATE_INITIALIZING);
648 readline_initialize_everything ();
649 RL_UNSETSTATE(RL_STATE_INITIALIZING);
651 RL_SETSTATE(RL_STATE_INITIALIZED);
654 /* Initalize the current line information. */
655 _rl_init_line_state ();
657 /* We aren't done yet. We haven't even gotten started yet! */
659 RL_UNSETSTATE(RL_STATE_DONE);
661 /* Tell the history routines what is going on. */
662 start_using_history ();
664 /* Make the display buffer match the state of the line. */
665 rl_reset_line_state ();
667 /* No such function typed yet. */
668 rl_last_func = (rl_command_func_t *)NULL;
670 /* Parsing of key-bindings begins in an enabled state. */
671 _rl_parsing_conditionalized_out = 0;
673 #if defined (VI_MODE)
674 if (rl_editing_mode == vi_mode)
675 _rl_vi_initialize_line ();
682 #if defined (__EMX__)
684 _emx_build_environ ()
691 DosGetInfoBlocks (&tibp, &pibp);
692 t = pibp->pib_pchenv;
695 tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
696 t = pibp->pib_pchenv;
707 /* Initialize the entire state of the world. */
709 readline_initialize_everything ()
712 #if defined (__EMX__)
714 _emx_build_environ ();
719 /* Find out if we are running in Emacs -- UNUSED. */
720 running_in_emacs = sh_get_env_value ("EMACS") != (char *)0;
723 /* Set up input and output if they are not already set up. */
728 rl_outstream = stdout;
730 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
731 may change, but they may also be used before readline_internal ()
733 _rl_in_stream = rl_instream;
734 _rl_out_stream = rl_outstream;
736 /* Allocate data structures. */
737 if (rl_line_buffer == 0)
738 rl_line_buffer = (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
740 /* Initialize the terminal interface. */
741 if (rl_terminal_name == 0)
742 rl_terminal_name = sh_get_env_value ("TERM");
743 _rl_init_terminal_io (rl_terminal_name);
745 /* Bind tty characters to readline functions. */
746 readline_default_bindings ();
748 /* Initialize the function names. */
749 rl_initialize_funmap ();
751 /* Decide whether we should automatically go into eight-bit mode. */
752 _rl_init_eightbit ();
754 /* Read in the init file. */
755 rl_read_init_file ((char *)NULL);
758 if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
761 _rl_screenchars -= _rl_screenheight;
764 /* Override the effect of any `set keymap' assignments in the
766 rl_set_keymap_from_edit_mode ();
768 /* Try to bind a common arrow key prefix, if not already bound. */
771 /* Enable the meta key, if this terminal has one. */
773 _rl_enable_meta_key ();
775 /* If the completion parser's default word break characters haven't
776 been set yet, then do so now. */
777 if (rl_completer_word_break_characters == (char *)NULL)
778 rl_completer_word_break_characters = rl_basic_word_break_characters;
781 /* If this system allows us to look at the values of the regular
782 input editing characters, then bind them to their readline
783 equivalents, iff the characters are not bound to keymaps. */
785 readline_default_bindings ()
787 rl_tty_set_default_bindings (_rl_keymap);
791 bind_arrow_keys_internal ()
793 rl_command_func_t *f;
795 #if defined (__MSDOS__)
796 f = rl_function_of_keyseq ("\033[0A", _rl_keymap, (int *)NULL);
797 if (!f || f == rl_do_lowercase_version)
799 _rl_bind_if_unbound ("\033[0A", rl_get_previous_history);
800 _rl_bind_if_unbound ("\033[0B", rl_backward);
801 _rl_bind_if_unbound ("\033[0C", rl_forward);
802 _rl_bind_if_unbound ("\033[0D", rl_get_next_history);
806 f = rl_function_of_keyseq ("\033[A", _rl_keymap, (int *)NULL);
807 if (!f || f == rl_do_lowercase_version)
809 _rl_bind_if_unbound ("\033[A", rl_get_previous_history);
810 _rl_bind_if_unbound ("\033[B", rl_get_next_history);
811 _rl_bind_if_unbound ("\033[C", rl_forward);
812 _rl_bind_if_unbound ("\033[D", rl_backward);
815 f = rl_function_of_keyseq ("\033OA", _rl_keymap, (int *)NULL);
816 if (!f || f == rl_do_lowercase_version)
818 _rl_bind_if_unbound ("\033OA", rl_get_previous_history);
819 _rl_bind_if_unbound ("\033OB", rl_get_next_history);
820 _rl_bind_if_unbound ("\033OC", rl_forward);
821 _rl_bind_if_unbound ("\033OD", rl_backward);
825 /* Try and bind the common arrow key prefix after giving termcap and
826 the inputrc file a chance to bind them and create `real' keymaps
827 for the arrow key prefix. */
833 xkeymap = _rl_keymap;
835 _rl_keymap = emacs_standard_keymap;
836 bind_arrow_keys_internal ();
838 #if defined (VI_MODE)
839 _rl_keymap = vi_movement_keymap;
840 bind_arrow_keys_internal ();
843 _rl_keymap = xkeymap;
847 /* **************************************************************** */
849 /* Numeric Arguments */
851 /* **************************************************************** */
853 /* Handle C-u style numeric args, as well as M--, and M-digits. */
857 int key, c, sawminus, sawdigits;
861 RL_SETSTATE(RL_STATE_NUMERICARG);
862 sawminus = sawdigits = 0;
865 if (rl_numeric_arg > 1000000)
867 sawdigits = rl_explicit_arg = rl_numeric_arg = 0;
869 rl_restore_prompt ();
871 RL_UNSETSTATE(RL_STATE_NUMERICARG);
874 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
875 RL_SETSTATE(RL_STATE_MOREINPUT);
876 key = c = rl_read_key ();
877 RL_UNSETSTATE(RL_STATE_MOREINPUT);
881 _rl_abort_internal ();
885 /* If we see a key bound to `universal-argument' after seeing digits,
886 it ends the argument but is otherwise ignored. */
887 if (_rl_keymap[c].type == ISFUNC &&
888 _rl_keymap[c].function == rl_universal_argument)
897 RL_SETSTATE(RL_STATE_MOREINPUT);
898 key = rl_read_key ();
899 RL_UNSETSTATE(RL_STATE_MOREINPUT);
900 rl_restore_prompt ();
902 RL_UNSETSTATE(RL_STATE_NUMERICARG);
903 return (_rl_dispatch (key, _rl_keymap));
911 rl_numeric_arg = rl_explicit_arg ? (rl_numeric_arg * 10) + c - '0' : c - '0';
912 sawdigits = rl_explicit_arg = 1;
914 else if (c == '-' && rl_explicit_arg == 0)
916 rl_numeric_arg = sawminus = 1;
921 /* Make M-- command equivalent to M--1 command. */
922 if (sawminus && rl_numeric_arg == 1 && rl_explicit_arg == 0)
924 rl_restore_prompt ();
926 RL_UNSETSTATE(RL_STATE_NUMERICARG);
927 return (_rl_dispatch (key, _rl_keymap));
934 /* Add the current digit to the argument in progress. */
936 rl_digit_argument (ignore, key)
939 rl_execute_next (key);
940 return (rl_digit_loop ());
943 /* What to do when you abort reading an argument. */
945 rl_discard_argument ()
949 _rl_init_argument ();
953 /* Create a default argument. */
957 rl_numeric_arg = rl_arg_sign = 1;
962 /* C-u, universal argument. Multiply the current argument by 4.
963 Read a key. If the key has nothing to do with arguments, then
964 dispatch on it. If the key is the abort character then abort. */
966 rl_universal_argument (count, key)
970 return (rl_digit_loop ());
973 /* **************************************************************** */
975 /* Insert and Delete */
977 /* **************************************************************** */
979 /* Insert a string of text into the line at point. This is the only
980 way that you should do insertion. rl_insert () calls this
983 rl_insert_text (string)
986 register int i, l = strlen (string);
988 if (rl_end + l >= rl_line_buffer_len)
989 rl_extend_line_buffer (rl_end + l);
991 for (i = rl_end; i >= rl_point; i--)
992 the_line[i + l] = the_line[i];
993 strncpy (the_line + rl_point, string, l);
995 /* Remember how to undo this if we aren't undoing something. */
996 if (!_rl_doing_an_undo)
998 /* If possible and desirable, concatenate the undos. */
1001 (rl_undo_list->what == UNDO_INSERT) &&
1002 (rl_undo_list->end == rl_point) &&
1003 (rl_undo_list->end - rl_undo_list->start < 20))
1004 rl_undo_list->end++;
1006 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
1010 the_line[rl_end] = '\0';
1014 /* Delete the string between FROM and TO. FROM is
1015 inclusive, TO is not. */
1017 rl_delete_text (from, to)
1020 register char *text;
1021 register int diff, i;
1023 /* Fix it if the caller is confused. */
1027 /* fix boundaries */
1035 text = rl_copy_text (from, to);
1037 /* Some versions of strncpy() can't handle overlapping arguments. */
1039 for (i = from; i < rl_end - diff; i++)
1040 the_line[i] = the_line[i + diff];
1042 /* Remember how to undo this delete. */
1043 if (_rl_doing_an_undo == 0)
1044 rl_add_undo (UNDO_DELETE, from, to, text);
1049 the_line[rl_end] = '\0';
1053 /* Fix up point so that it is within the line boundaries after killing
1054 text. If FIX_MARK_TOO is non-zero, the mark is forced within line
1057 #define _RL_FIX_POINT(x) \
1066 _rl_fix_point (fix_mark_too)
1069 _RL_FIX_POINT (rl_point);
1071 _RL_FIX_POINT (rl_mark);
1073 #undef _RL_FIX_POINT
1076 _rl_replace_text (text, start, end)
1080 rl_begin_undo_group ();
1081 rl_delete_text (start, end + 1);
1083 rl_insert_text (text);
1084 rl_end_undo_group ();
1087 /* **************************************************************** */
1089 /* Readline character functions */
1091 /* **************************************************************** */
1093 /* This is not a gap editor, just a stupid line input routine. No hair
1094 is involved in writing any of the functions, and none should be. */
1098 rl_end is the place in the string that we would place '\0';
1099 i.e., it is always safe to place '\0' there.
1101 rl_point is the place in the string where the cursor is. Sometimes
1102 this is the same as rl_end.
1104 Any command that is called interactively receives two arguments.
1105 The first is a count: the numeric arg pased to this command.
1106 The second is the key which invoked this command.
1109 /* **************************************************************** */
1111 /* Movement Commands */
1113 /* **************************************************************** */
1115 /* Note that if you `optimize' the display for these functions, you cannot
1116 use said functions in other functions which do not do optimizing display.
1117 I.e., you will have to update the data base for rl_redisplay, and you
1118 might as well let rl_redisplay do that job. */
1120 /* Move forward COUNT characters. */
1122 rl_forward (count, key)
1126 rl_backward (-count, key);
1129 int end = rl_point + count;
1130 #if defined (VI_MODE)
1131 int lend = rl_end > 0 ? rl_end - (rl_editing_mode == vi_mode) : rl_end;
1151 /* Move backward COUNT characters. */
1153 rl_backward (count, key)
1157 rl_forward (-count, key);
1160 if (rl_point < count)
1175 /* Move to the beginning of the line. */
1177 rl_beg_of_line (count, key)
1184 /* Move to the end of the line. */
1186 rl_end_of_line (count, key)
1193 /* Move forward a word. We do what Emacs does. */
1195 rl_forward_word (count, key)
1202 rl_backward_word (-count, key);
1208 if (rl_point == rl_end)
1211 /* If we are not in a word, move forward until we are in one.
1212 Then, move forward until we hit a non-alphabetic character. */
1213 c = the_line[rl_point];
1214 if (rl_alphabetic (c) == 0)
1216 while (++rl_point < rl_end)
1218 c = the_line[rl_point];
1219 if (rl_alphabetic (c))
1223 if (rl_point == rl_end)
1225 while (++rl_point < rl_end)
1227 c = the_line[rl_point];
1228 if (rl_alphabetic (c) == 0)
1236 /* Move backward a word. We do what Emacs does. */
1238 rl_backward_word (count, key)
1245 rl_forward_word (-count, key);
1254 /* Like rl_forward_word (), except that we look at the characters
1255 just before point. */
1257 c = the_line[rl_point - 1];
1258 if (rl_alphabetic (c) == 0)
1262 c = the_line[rl_point - 1];
1263 if (rl_alphabetic (c))
1270 c = the_line[rl_point - 1];
1271 if (rl_alphabetic (c) == 0)
1281 /* Clear the current line. Numeric argument to C-l does this. */
1283 rl_refresh_line (ignore1, ignore2)
1284 int ignore1, ignore2;
1288 curr_line = _rl_current_display_line ();
1290 _rl_move_vert (curr_line);
1291 _rl_move_cursor_relative (0, the_line); /* XXX is this right */
1293 _rl_clear_to_eol (0); /* arg of 0 means to not use spaces */
1295 rl_forced_update_display ();
1296 rl_display_fixed = 1;
1301 /* C-l typed to a line without quoting clears the screen, and then reprints
1302 the prompt and the current input line. Given a numeric arg, redraw only
1303 the current line. */
1305 rl_clear_screen (count, key)
1308 if (rl_explicit_arg)
1310 rl_refresh_line (count, key);
1314 _rl_clear_screen (); /* calls termcap function to clear screen */
1315 rl_forced_update_display ();
1316 rl_display_fixed = 1;
1322 rl_arrow_keys (count, c)
1327 RL_SETSTATE(RL_STATE_MOREINPUT);
1328 ch = rl_read_key ();
1329 RL_UNSETSTATE(RL_STATE_MOREINPUT);
1331 switch (_rl_to_upper (ch))
1334 rl_get_previous_history (count, ch);
1338 rl_get_next_history (count, ch);
1342 rl_forward (count, ch);
1346 rl_backward (count, ch);
1356 /* **************************************************************** */
1360 /* **************************************************************** */
1362 /* Insert the character C at the current location, moving point forward. */
1364 rl_insert (count, c)
1373 /* If we can optimize, then do it. But don't let people crash
1374 readline because of extra large arguments. */
1375 if (count > 1 && count <= 1024)
1377 string = (char *)xmalloc (1 + count);
1379 for (i = 0; i < count; i++)
1383 rl_insert_text (string);
1394 for (i = 0; i < 1024; i++)
1399 decreaser = (count > 1024 ? 1024 : count);
1400 str[decreaser] = '\0';
1401 rl_insert_text (str);
1408 /* We are inserting a single character.
1409 If there is pending input, then make a string of all of the
1410 pending characters that are bound to rl_insert, and insert
1412 if (_rl_any_typein ())
1413 _rl_insert_typein (c);
1416 /* Inserting a single character. */
1421 rl_insert_text (str);
1426 /* Insert the next typed character verbatim. */
1428 rl_quoted_insert (count, key)
1433 #if defined (HANDLE_SIGNALS)
1434 _rl_disable_tty_signals ();
1437 RL_SETSTATE(RL_STATE_MOREINPUT);
1439 RL_UNSETSTATE(RL_STATE_MOREINPUT);
1441 #if defined (HANDLE_SIGNALS)
1442 _rl_restore_tty_signals ();
1445 return (rl_insert (count, c));
1448 /* Insert a tab character. */
1450 rl_tab_insert (count, key)
1453 return (rl_insert (count, '\t'));
1456 /* What to do when a NEWLINE is pressed. We accept the whole line.
1457 KEY is the key that invoked this command. I guess it could have
1458 meaning in the future. */
1460 rl_newline (count, key)
1465 if (_rl_history_preserve_point)
1466 _rl_history_saved_point = (rl_point == rl_end) ? -1 : rl_point;
1468 RL_SETSTATE(RL_STATE_DONE);
1470 #if defined (VI_MODE)
1471 if (rl_editing_mode == vi_mode)
1473 _rl_vi_done_inserting ();
1474 _rl_vi_reset_last ();
1476 #endif /* VI_MODE */
1478 /* If we've been asked to erase empty lines, suppress the final update,
1479 since _rl_update_final calls rl_crlf(). */
1480 if (rl_erase_empty_line && rl_point == 0 && rl_end == 0)
1483 if (readline_echoing_p)
1484 _rl_update_final ();
1488 /* What to do for some uppercase characters, like meta characters,
1489 and some characters appearing in emacs_ctlx_keymap. This function
1490 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1491 is special cased. */
1493 rl_do_lowercase_version (ignore1, ignore2)
1494 int ignore1, ignore2;
1499 /* Rubout the character behind point. */
1501 rl_rubout (count, key)
1506 rl_delete (-count, key);
1516 if (count > 1 || rl_explicit_arg)
1518 int orig_point = rl_point;
1519 rl_backward (count, key);
1520 rl_kill_text (orig_point, rl_point);
1524 unsigned char c = the_line[--rl_point];
1525 rl_delete_text (rl_point, rl_point + 1);
1527 if (rl_point == rl_end && ISPRINT (c) && _rl_last_c_pos)
1530 l = rl_character_len (c, rl_point);
1531 _rl_erase_at_end_of_line (l);
1537 /* Delete the character under the cursor. Given a numeric argument,
1538 kill that many characters instead. */
1540 rl_delete (count, key)
1544 return (rl_rubout (-count, key));
1546 if (rl_point == rl_end)
1552 if (count > 1 || rl_explicit_arg)
1554 int orig_point = rl_point;
1555 rl_forward (count, key);
1556 rl_kill_text (orig_point, rl_point);
1557 rl_point = orig_point;
1561 return (rl_delete_text (rl_point, rl_point + 1));
1564 /* Delete the character under the cursor, unless the insertion
1565 point is at the end of the line, in which case the character
1566 behind the cursor is deleted. COUNT is obeyed and may be used
1567 to delete forward or backward that many characters. */
1569 rl_rubout_or_delete (count, key)
1572 if (rl_end != 0 && rl_point == rl_end)
1573 return (rl_rubout (count, key));
1575 return (rl_delete (count, key));
1578 /* Delete all spaces and tabs around point. */
1580 rl_delete_horizontal_space (count, ignore)
1583 int start = rl_point;
1585 while (rl_point && whitespace (the_line[rl_point - 1]))
1590 while (rl_point < rl_end && whitespace (the_line[rl_point]))
1593 if (start != rl_point)
1595 rl_delete_text (start, rl_point);
1601 /* Like the tcsh editing function delete-char-or-list. The eof character
1602 is caught before this is invoked, so this really does the same thing as
1603 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1605 rl_delete_or_show_completions (count, key)
1608 if (rl_end != 0 && rl_point == rl_end)
1609 return (rl_possible_completions (count, key));
1611 return (rl_delete (count, key));
1614 #ifndef RL_COMMENT_BEGIN_DEFAULT
1615 #define RL_COMMENT_BEGIN_DEFAULT "#"
1618 /* Turn the current line into a comment in shell history.
1619 A K*rn shell style function. */
1621 rl_insert_comment (count, key)
1624 rl_beg_of_line (1, key);
1625 rl_insert_text (_rl_comment_begin ? _rl_comment_begin
1626 : RL_COMMENT_BEGIN_DEFAULT);
1627 (*rl_redisplay_function) ();
1628 rl_newline (1, '\n');
1632 /* **************************************************************** */
1636 /* **************************************************************** */
1638 /* The three kinds of things that we know how to do. */
1643 /* Uppercase the word at point. */
1645 rl_upcase_word (count, key)
1648 return (rl_change_case (count, UpCase));
1651 /* Lowercase the word at point. */
1653 rl_downcase_word (count, key)
1656 return (rl_change_case (count, DownCase));
1659 /* Upcase the first letter, downcase the rest. */
1661 rl_capitalize_word (count, key)
1664 return (rl_change_case (count, CapCase));
1667 /* The meaty function.
1668 Change the case of COUNT words, performing OP on them.
1669 OP is one of UpCase, DownCase, or CapCase.
1670 If a negative argument is given, leave point where it started,
1671 otherwise, leave it where it moves to. */
1673 rl_change_case (count, op)
1676 register int start, end;
1680 rl_forward_word (count, 0);
1686 /* We are going to modify some text, so let's prepare to undo it. */
1687 rl_modifying (start, end);
1689 for (inword = 0; start < end; start++)
1691 c = the_line[start];
1695 the_line[start] = _rl_to_upper (c);
1699 the_line[start] = _rl_to_lower (c);
1703 the_line[start] = (inword == 0) ? _rl_to_upper (c) : _rl_to_lower (c);
1704 inword = rl_alphabetic (the_line[start]);
1716 /* **************************************************************** */
1720 /* **************************************************************** */
1722 /* Transpose the words at point. If point is at the end of the line,
1723 transpose the two words before point. */
1725 rl_transpose_words (count, key)
1728 char *word1, *word2;
1729 int w1_beg, w1_end, w2_beg, w2_end;
1730 int orig_point = rl_point;
1735 /* Find the two words. */
1736 rl_forward_word (count, key);
1738 rl_backward_word (1, key);
1740 rl_backward_word (count, key);
1742 rl_forward_word (1, key);
1745 /* Do some check to make sure that there really are two words. */
1746 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
1749 rl_point = orig_point;
1753 /* Get the text of the words. */
1754 word1 = rl_copy_text (w1_beg, w1_end);
1755 word2 = rl_copy_text (w2_beg, w2_end);
1757 /* We are about to do many insertions and deletions. Remember them
1758 as one operation. */
1759 rl_begin_undo_group ();
1761 /* Do the stuff at word2 first, so that we don't have to worry
1762 about word1 moving. */
1764 rl_delete_text (w2_beg, w2_end);
1765 rl_insert_text (word1);
1768 rl_delete_text (w1_beg, w1_end);
1769 rl_insert_text (word2);
1771 /* This is exactly correct since the text before this point has not
1772 changed in length. */
1775 /* I think that does it. */
1776 rl_end_undo_group ();
1783 /* Transpose the characters at point. If point is at the end of the line,
1784 then transpose the characters before point. */
1786 rl_transpose_chars (count, key)
1794 if (!rl_point || rl_end < 2)
1800 rl_begin_undo_group ();
1802 if (rl_point == rl_end)
1809 dummy[0] = the_line[rl_point];
1812 rl_delete_text (rl_point, rl_point + 1);
1816 rl_insert_text (dummy);
1818 rl_end_undo_group ();
1822 /* **************************************************************** */
1824 /* Character Searching */
1826 /* **************************************************************** */
1829 _rl_char_search_internal (count, dir, schar)
1830 int count, dir, schar;
1835 inc = (dir < 0) ? -1 : 1;
1838 if ((dir < 0 && pos <= 0) || (dir > 0 && pos >= rl_end))
1847 if (rl_line_buffer[pos] == schar)
1851 rl_point = (dir == BTO) ? pos + 1 : pos;
1853 rl_point = (dir == FTO) ? pos - 1 : pos;
1857 while ((dir < 0) ? pos-- : ++pos < rl_end);
1862 /* Search COUNT times for a character read from the current input stream.
1863 FDIR is the direction to search if COUNT is non-negative; otherwise
1864 the search goes in BDIR. */
1866 _rl_char_search (count, fdir, bdir)
1867 int count, fdir, bdir;
1871 RL_SETSTATE(RL_STATE_MOREINPUT);
1873 RL_UNSETSTATE(RL_STATE_MOREINPUT);
1876 return (_rl_char_search_internal (-count, bdir, c));
1878 return (_rl_char_search_internal (count, fdir, c));
1882 rl_char_search (count, key)
1885 return (_rl_char_search (count, FFIND, BFIND));
1889 rl_backward_char_search (count, key)
1892 return (_rl_char_search (count, BFIND, FFIND));
1895 /* **************************************************************** */
1897 /* History Utilities */
1899 /* **************************************************************** */
1901 /* We already have a history library, and that is what we use to control
1902 the history features of readline. This is our local interface to
1903 the history mechanism. */
1905 /* While we are editing the history, this is the saved
1906 version of the original line. */
1907 HIST_ENTRY *_rl_saved_line_for_history = (HIST_ENTRY *)NULL;
1909 /* Set the history pointer back to the last entry in the history. */
1911 start_using_history ()
1914 if (_rl_saved_line_for_history)
1915 _rl_free_history_entry (_rl_saved_line_for_history);
1917 _rl_saved_line_for_history = (HIST_ENTRY *)NULL;
1920 /* Free the contents (and containing structure) of a HIST_ENTRY. */
1922 _rl_free_history_entry (entry)
1932 /* Perhaps put back the current line if it has changed. */
1934 rl_maybe_replace_line ()
1938 temp = current_history ();
1939 /* If the current line has changed, save the changes. */
1940 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
1942 temp = replace_history_entry (where_history (), the_line, (histdata_t)rl_undo_list);
1949 /* Restore the _rl_saved_line_for_history if there is one. */
1951 rl_maybe_unsave_line ()
1955 if (_rl_saved_line_for_history)
1957 line_len = strlen (_rl_saved_line_for_history->line);
1959 if (line_len >= rl_line_buffer_len)
1960 rl_extend_line_buffer (line_len);
1962 strcpy (the_line, _rl_saved_line_for_history->line);
1963 rl_undo_list = (UNDO_LIST *)_rl_saved_line_for_history->data;
1964 _rl_free_history_entry (_rl_saved_line_for_history);
1965 _rl_saved_line_for_history = (HIST_ENTRY *)NULL;
1966 rl_end = rl_point = strlen (the_line);
1973 /* Save the current line in _rl_saved_line_for_history. */
1975 rl_maybe_save_line ()
1977 if (_rl_saved_line_for_history == 0)
1979 _rl_saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
1980 _rl_saved_line_for_history->line = savestring (the_line);
1981 _rl_saved_line_for_history->data = (char *)rl_undo_list;
1987 _rl_free_saved_history_line ()
1989 if (_rl_saved_line_for_history)
1991 _rl_free_history_entry (_rl_saved_line_for_history);
1992 _rl_saved_line_for_history = (HIST_ENTRY *)NULL;
1997 /* **************************************************************** */
1999 /* History Commands */
2001 /* **************************************************************** */
2003 /* Meta-< goes to the start of the history. */
2005 rl_beginning_of_history (count, key)
2008 return (rl_get_previous_history (1 + where_history (), key));
2011 /* Meta-> goes to the end of the history. (The current line). */
2013 rl_end_of_history (count, key)
2016 rl_maybe_replace_line ();
2018 rl_maybe_unsave_line ();
2022 /* Move down to the next history line. */
2024 rl_get_next_history (count, key)
2031 return (rl_get_previous_history (-count, key));
2036 rl_maybe_replace_line ();
2038 /* either not saved by rl_newline or at end of line, so set appropriately. */
2039 if (_rl_history_saved_point == -1 && (rl_point || rl_end))
2040 _rl_history_saved_point = (rl_point == rl_end) ? -1 : rl_point;
2042 temp = (HIST_ENTRY *)NULL;
2045 temp = next_history ();
2052 rl_maybe_unsave_line ();
2055 line_len = strlen (temp->line);
2057 if (line_len >= rl_line_buffer_len)
2058 rl_extend_line_buffer (line_len);
2060 strcpy (the_line, temp->line);
2061 rl_undo_list = (UNDO_LIST *)temp->data;
2062 rl_end = strlen (the_line);
2063 rl_point = (_rl_history_preserve_point && _rl_history_saved_point != -1)
2064 ? _rl_history_saved_point
2066 if (rl_point > rl_end)
2068 #if defined (VI_MODE)
2069 if (rl_editing_mode == vi_mode)
2071 #endif /* VI_MODE */
2076 /* Get the previous item out of our interactive history, making it the current
2077 line. If there is no previous history, just ding. */
2079 rl_get_previous_history (count, key)
2082 HIST_ENTRY *old_temp, *temp;
2086 return (rl_get_next_history (-count, key));
2091 /* either not saved by rl_newline or at end of line, so set appropriately. */
2092 if (_rl_history_saved_point == -1 && (rl_point || rl_end))
2093 _rl_history_saved_point = (rl_point == rl_end) ? -1 : rl_point;
2095 /* If we don't have a line saved, then save this one. */
2096 rl_maybe_save_line ();
2098 /* If the current line has changed, save the changes. */
2099 rl_maybe_replace_line ();
2101 temp = old_temp = (HIST_ENTRY *)NULL;
2104 temp = previous_history ();
2112 /* If there was a large argument, and we moved back to the start of the
2113 history, that is not an error. So use the last value found. */
2114 if (!temp && old_temp)
2121 line_len = strlen (temp->line);
2123 if (line_len >= rl_line_buffer_len)
2124 rl_extend_line_buffer (line_len);
2126 strcpy (the_line, temp->line);
2127 rl_undo_list = (UNDO_LIST *)temp->data;
2129 rl_point = (_rl_history_preserve_point && _rl_history_saved_point != -1)
2130 ? _rl_history_saved_point
2132 if (rl_point > rl_end)
2135 #if defined (VI_MODE)
2136 if (rl_editing_mode == vi_mode)
2138 #endif /* VI_MODE */
2143 /* **************************************************************** */
2145 /* The Mark and the Region. */
2147 /* **************************************************************** */
2149 /* Set the mark at POSITION. */
2151 _rl_set_mark_at_pos (position)
2154 if (position > rl_end)
2161 /* A bindable command to set the mark. */
2163 rl_set_mark (count, key)
2166 return (_rl_set_mark_at_pos (rl_explicit_arg ? count : rl_point));
2169 /* Exchange the position of mark and point. */
2171 rl_exchange_point_and_mark (count, key)
2174 if (rl_mark > rl_end)
2183 SWAP (rl_point, rl_mark);
2188 /* **************************************************************** */
2192 /* **************************************************************** */
2193 /* How to toggle back and forth between editing modes. */
2195 rl_vi_editing_mode (count, key)
2198 #if defined (VI_MODE)
2199 rl_editing_mode = vi_mode;
2200 rl_vi_insertion_mode (1, key);
2201 #endif /* VI_MODE */
2206 rl_emacs_editing_mode (count, key)
2209 rl_editing_mode = emacs_mode;
2210 _rl_keymap = emacs_standard_keymap;