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.2"
73 /* Evaluates its arguments multiple times. */
74 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
76 /* Forward declarations used in this file. */
77 void _rl_free_history_entry __P((HIST_ENTRY *));
79 static char *readline_internal __P((void));
80 static void readline_initialize_everything __P((void));
81 static void start_using_history __P((void));
82 static void bind_arrow_keys __P((void));
83 static int rl_change_case __P((int, int));
85 static void readline_default_bindings __P((void));
87 /* **************************************************************** */
89 /* Line editing input utility */
91 /* **************************************************************** */
93 const char *rl_library_version = RL_LIBRARY_VERSION;
95 /* True if this is `real' readline as opposed to some stub substitute. */
96 int rl_gnu_readline_p = 1;
98 /* A pointer to the keymap that is currently in use.
99 By default, it is the standard emacs keymap. */
100 Keymap _rl_keymap = emacs_standard_keymap;
102 /* The current style of editing. */
103 int rl_editing_mode = emacs_mode;
105 /* Non-zero if we called this function from _rl_dispatch(). It's present
106 so functions can find out whether they were called from a key binding
107 or directly from an application. */
110 /* Non-zero if the previous command was a kill command. */
111 int _rl_last_command_was_kill = 0;
113 /* The current value of the numeric argument specified by the user. */
114 int rl_numeric_arg = 1;
116 /* Non-zero if an argument was typed. */
117 int rl_explicit_arg = 0;
119 /* Temporary value used while generating the argument. */
122 /* Non-zero means we have been called at least once before. */
123 static int rl_initialized;
126 /* If non-zero, this program is running in an EMACS buffer. */
127 static int running_in_emacs;
130 /* Flags word encapsulating the current readline state. */
131 int rl_readline_state = RL_STATE_NONE;
133 /* The current offset in the current input line. */
136 /* Mark in the current input line. */
139 /* Length of the current input line. */
142 /* Make this non-zero to return the current input_line. */
145 /* The last function executed by readline. */
146 rl_command_func_t *rl_last_func = (rl_command_func_t *)NULL;
148 /* Top level environment for readline_internal (). */
149 procenv_t readline_top_level;
151 /* The streams we interact with. */
152 FILE *_rl_in_stream, *_rl_out_stream;
154 /* The names of the streams that we do input and output to. */
155 FILE *rl_instream = (FILE *)NULL;
156 FILE *rl_outstream = (FILE *)NULL;
158 /* Non-zero means echo characters as they are read. */
159 int readline_echoing_p = 1;
161 /* Current prompt. */
162 char *rl_prompt = (char *)NULL;
163 int rl_visible_prompt_length = 0;
165 /* Set to non-zero by calling application if it has already printed rl_prompt
166 and does not want readline to do it the first time. */
167 int rl_already_prompted = 0;
169 /* The number of characters read in order to type this complete command. */
170 int rl_key_sequence_length = 0;
172 /* If non-zero, then this is the address of a function to call just
173 before readline_internal_setup () prints the first prompt. */
174 rl_hook_func_t *rl_startup_hook = (rl_hook_func_t *)NULL;
176 /* If non-zero, this is the address of a function to call just before
177 readline_internal_setup () returns and readline_internal starts
178 reading input characters. */
179 rl_hook_func_t *rl_pre_input_hook = (rl_hook_func_t *)NULL;
181 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
182 static char *the_line;
184 /* The character that can generate an EOF. Really read from
185 the terminal driver... just defaulted here. */
186 int _rl_eof_char = CTRL ('D');
188 /* Non-zero makes this the next keystroke to read. */
189 int rl_pending_input = 0;
191 /* Pointer to a useful terminal name. */
192 const char *rl_terminal_name = (const char *)NULL;
194 /* Non-zero means to always use horizontal scrolling in line display. */
195 int _rl_horizontal_scroll_mode = 0;
197 /* Non-zero means to display an asterisk at the starts of history lines
198 which have been modified. */
199 int _rl_mark_modified_lines = 0;
201 /* The style of `bell' notification preferred. This can be set to NO_BELL,
202 AUDIBLE_BELL, or VISIBLE_BELL. */
203 int _rl_bell_preference = AUDIBLE_BELL;
205 /* String inserted into the line by rl_insert_comment (). */
206 char *_rl_comment_begin;
208 /* Keymap holding the function currently being executed. */
209 Keymap rl_executing_keymap;
211 /* Non-zero means to erase entire line, including prompt, on empty input lines. */
212 int rl_erase_empty_line = 0;
214 /* Non-zero means to read only this many characters rather than up to a
215 character bound to accept-line. */
216 int rl_num_chars_to_read;
218 /* Line buffer and maintenence. */
219 char *rl_line_buffer = (char *)NULL;
220 int rl_line_buffer_len = 0;
222 /* Forward declarations used by the display and termcap code. */
224 /* **************************************************************** */
226 /* `Forward' declarations */
228 /* **************************************************************** */
230 /* Non-zero means do not parse any lines other than comments and
231 parser directives. */
232 unsigned char _rl_parsing_conditionalized_out = 0;
234 /* Non-zero means to convert characters with the meta bit set to
235 escape-prefixed characters so we can indirect through
236 emacs_meta_keymap or vi_escape_keymap. */
237 int _rl_convert_meta_chars_to_ascii = 1;
239 /* Non-zero means to output characters with the meta bit set directly
240 rather than as a meta-prefixed escape sequence. */
241 int _rl_output_meta_chars = 0;
243 /* **************************************************************** */
245 /* Top Level Functions */
247 /* **************************************************************** */
249 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
250 int _rl_meta_flag = 0; /* Forward declaration */
252 /* Set up the prompt and expand it. Called from readline() and
253 rl_callback_handler_install (). */
255 rl_set_prompt (prompt)
259 rl_prompt = prompt ? savestring (prompt) : (char *)NULL;
261 rl_visible_prompt_length = (rl_prompt && *rl_prompt)
262 ? rl_expand_prompt (rl_prompt)
267 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
268 none. A return value of NULL means that EOF was encountered. */
275 /* If we are at EOF return a NULL string. */
276 if (rl_pending_input == EOF)
278 rl_clear_pending_input ();
279 return ((char *)NULL);
282 rl_set_prompt (prompt);
285 (*rl_prep_term_function) (_rl_meta_flag);
287 #if defined (HANDLE_SIGNALS)
291 value = readline_internal ();
292 (*rl_deprep_term_function) ();
294 #if defined (HANDLE_SIGNALS)
301 #if defined (READLINE_CALLBACKS)
302 # define STATIC_CALLBACK
304 # define STATIC_CALLBACK static
308 readline_internal_setup ()
312 _rl_in_stream = rl_instream;
313 _rl_out_stream = rl_outstream;
316 (*rl_startup_hook) ();
318 if (readline_echoing_p == 0)
320 if (rl_prompt && rl_already_prompted == 0)
322 nprompt = _rl_strip_prompt (rl_prompt);
323 fprintf (_rl_out_stream, "%s", nprompt);
324 fflush (_rl_out_stream);
330 if (rl_prompt && rl_already_prompted)
331 rl_on_new_line_with_prompt ();
334 (*rl_redisplay_function) ();
335 #if defined (VI_MODE)
336 if (rl_editing_mode == vi_mode)
337 rl_vi_insertion_mode (1, 0);
341 if (rl_pre_input_hook)
342 (*rl_pre_input_hook) ();
345 STATIC_CALLBACK char *
346 readline_internal_teardown (eof)
352 /* Restore the original of this history line, iff the line that we
353 are editing was originally in the history, AND the line has changed. */
354 entry = current_history ();
356 if (entry && rl_undo_list)
358 temp = savestring (the_line);
359 rl_revert_line (1, 0);
360 entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
361 _rl_free_history_entry (entry);
363 strcpy (the_line, temp);
367 /* At any rate, it is highly likely that this line has an undo list. Get
370 rl_free_undo_list ();
372 return (eof ? (char *)NULL : savestring (the_line));
376 #if defined (READLINE_CALLBACKS)
377 readline_internal_char ()
379 readline_internal_charloop ()
382 static int lastc, eof_found;
388 #if !defined (READLINE_CALLBACKS)
392 lk = _rl_last_command_was_kill;
394 code = setjmp (readline_top_level);
397 (*rl_redisplay_function) ();
399 if (rl_pending_input == 0)
401 /* Then initialize the argument and number of keys read. */
402 _rl_init_argument ();
403 rl_key_sequence_length = 0;
406 RL_SETSTATE(RL_STATE_READCMD);
408 RL_UNSETSTATE(RL_STATE_READCMD);
410 /* EOF typed to a non-blank line is a <NL>. */
411 if (c == EOF && rl_end)
414 /* The character _rl_eof_char typed to blank line, and not as the
415 previous character is interpreted as EOF. */
416 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
418 #if defined (READLINE_CALLBACKS)
419 RL_SETSTATE(RL_STATE_DONE);
420 return (rl_done = 1);
428 _rl_dispatch ((unsigned char)c, _rl_keymap);
430 /* If there was no change in _rl_last_command_was_kill, then no kill
431 has taken place. Note that if input is pending we are reading
432 a prefix command, so nothing has changed yet. */
433 if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
434 _rl_last_command_was_kill = 0;
436 #if defined (VI_MODE)
437 /* In vi mode, when you exit insert mode, the cursor moves back
438 over the previous character. We explicitly check for that here. */
439 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
443 if (rl_num_chars_to_read && rl_end >= rl_num_chars_to_read)
445 (*rl_redisplay_function) ();
446 rl_newline (1, '\n');
450 (*rl_redisplay_function) ();
452 /* If the application writer has told us to erase the entire line if
453 the only character typed was something bound to rl_newline, do so. */
454 if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
455 rl_point == 0 && rl_end == 0)
456 _rl_erase_entire_line ();
458 #if defined (READLINE_CALLBACKS)
467 #if defined (READLINE_CALLBACKS)
469 readline_internal_charloop ()
474 eof = readline_internal_char ();
477 #endif /* READLINE_CALLBACKS */
479 /* Read a line of input from the global rl_instream, doing output on
480 the global rl_outstream.
481 If rl_prompt is non-null, then that is our prompt. */
487 readline_internal_setup ();
488 eof = readline_internal_charloop ();
489 return (readline_internal_teardown (eof));
493 _rl_init_line_state ()
495 rl_point = rl_end = 0;
496 the_line = rl_line_buffer;
503 the_line = rl_line_buffer;
506 /* Do the command associated with KEY in MAP.
507 If the associated command is really a keymap, then read
508 another key, and dispatch into that map. */
510 _rl_dispatch (key, map)
516 rl_command_func_t *func;
518 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
520 if (map[ESC].type == ISKMAP)
522 if (_rl_defining_kbd_macro)
523 _rl_add_macro_char (ESC);
524 map = FUNCTION_TO_KEYMAP (map, ESC);
526 rl_key_sequence_length += 2;
527 return (_rl_dispatch (key, map));
534 if (_rl_defining_kbd_macro)
535 _rl_add_macro_char (key);
538 switch (map[key].type)
541 func = map[key].function;
544 /* Special case rl_do_lowercase_version (). */
545 if (func == rl_do_lowercase_version)
546 return (_rl_dispatch (_rl_to_lower (key), map));
548 rl_executing_keymap = map;
551 _rl_suppress_redisplay = (map[key].function == rl_insert) && _rl_input_available ();
555 RL_SETSTATE(RL_STATE_DISPATCHING);
556 r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
557 RL_UNSETSTATE(RL_STATE_DISPATCHING);
560 /* If we have input pending, then the last command was a prefix
561 command. Don't change the state of rl_last_func. Otherwise,
562 remember the last command executed in this variable. */
563 if (rl_pending_input == 0 && map[key].function != rl_digit_argument)
564 rl_last_func = map[key].function;
568 _rl_abort_internal ();
574 if (map[key].function != 0)
576 rl_key_sequence_length++;
579 RL_SETSTATE(RL_STATE_METANEXT);
580 RL_SETSTATE(RL_STATE_MOREINPUT);
581 newkey = rl_read_key ();
582 RL_UNSETSTATE(RL_STATE_MOREINPUT);
584 RL_UNSETSTATE(RL_STATE_METANEXT);
586 r = _rl_dispatch (newkey, FUNCTION_TO_KEYMAP (map, key));
590 _rl_abort_internal ();
596 if (map[key].function != 0)
598 macro = savestring ((char *)map[key].function);
599 _rl_with_macro_input (macro);
604 #if defined (VI_MODE)
605 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
606 _rl_vi_textmod_command (key))
607 _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
612 /* **************************************************************** */
614 /* Initializations */
616 /* **************************************************************** */
618 /* Initialize readline (and terminal if not already). */
622 /* If we have never been called before, initialize the
623 terminal and data structures. */
626 RL_SETSTATE(RL_STATE_INITIALIZING);
627 readline_initialize_everything ();
628 RL_UNSETSTATE(RL_STATE_INITIALIZING);
630 RL_SETSTATE(RL_STATE_INITIALIZED);
633 /* Initalize the current line information. */
634 _rl_init_line_state ();
636 /* We aren't done yet. We haven't even gotten started yet! */
638 RL_UNSETSTATE(RL_STATE_DONE);
640 /* Tell the history routines what is going on. */
641 start_using_history ();
643 /* Make the display buffer match the state of the line. */
644 rl_reset_line_state ();
646 /* No such function typed yet. */
647 rl_last_func = (rl_command_func_t *)NULL;
649 /* Parsing of key-bindings begins in an enabled state. */
650 _rl_parsing_conditionalized_out = 0;
652 #if defined (VI_MODE)
653 if (rl_editing_mode == vi_mode)
654 _rl_vi_initialize_line ();
661 #if defined (__EMX__)
663 _emx_build_environ ()
670 DosGetInfoBlocks (&tibp, &pibp);
671 t = pibp->pib_pchenv;
674 tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
675 t = pibp->pib_pchenv;
686 /* Initialize the entire state of the world. */
688 readline_initialize_everything ()
691 #if defined (__EMX__)
693 _emx_build_environ ();
698 /* Find out if we are running in Emacs -- UNUSED. */
699 running_in_emacs = sh_get_env_value ("EMACS") != (char *)0;
702 /* Set up input and output if they are not already set up. */
707 rl_outstream = stdout;
709 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
710 may change, but they may also be used before readline_internal ()
712 _rl_in_stream = rl_instream;
713 _rl_out_stream = rl_outstream;
715 /* Allocate data structures. */
716 if (rl_line_buffer == 0)
717 rl_line_buffer = xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
719 /* Initialize the terminal interface. */
720 if (rl_terminal_name == 0)
721 rl_terminal_name = sh_get_env_value ("TERM");
722 _rl_init_terminal_io (rl_terminal_name);
724 /* Bind tty characters to readline functions. */
725 readline_default_bindings ();
727 /* Initialize the function names. */
728 rl_initialize_funmap ();
730 /* Decide whether we should automatically go into eight-bit mode. */
731 _rl_init_eightbit ();
733 /* Read in the init file. */
734 rl_read_init_file ((char *)NULL);
737 if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
740 _rl_screenchars -= _rl_screenheight;
743 /* Override the effect of any `set keymap' assignments in the
745 rl_set_keymap_from_edit_mode ();
747 /* Try to bind a common arrow key prefix, if not already bound. */
750 /* Enable the meta key, if this terminal has one. */
752 _rl_enable_meta_key ();
754 /* If the completion parser's default word break characters haven't
755 been set yet, then do so now. */
756 if (rl_completer_word_break_characters == (char *)NULL)
757 rl_completer_word_break_characters = rl_basic_word_break_characters;
760 /* If this system allows us to look at the values of the regular
761 input editing characters, then bind them to their readline
762 equivalents, iff the characters are not bound to keymaps. */
764 readline_default_bindings ()
766 rl_tty_set_default_bindings (_rl_keymap);
770 bind_arrow_keys_internal ()
772 rl_command_func_t *f;
774 #if defined (__MSDOS__)
775 f = rl_function_of_keyseq ("\033[0A", _rl_keymap, (int *)NULL);
776 if (!f || f == rl_do_lowercase_version)
778 _rl_bind_if_unbound ("\033[0A", rl_get_previous_history);
779 _rl_bind_if_unbound ("\033[0B", rl_backward);
780 _rl_bind_if_unbound ("\033[0C", rl_forward);
781 _rl_bind_if_unbound ("\033[0D", rl_get_next_history);
785 f = rl_function_of_keyseq ("\033[A", _rl_keymap, (int *)NULL);
786 if (!f || f == rl_do_lowercase_version)
788 _rl_bind_if_unbound ("\033[A", rl_get_previous_history);
789 _rl_bind_if_unbound ("\033[B", rl_get_next_history);
790 _rl_bind_if_unbound ("\033[C", rl_forward);
791 _rl_bind_if_unbound ("\033[D", rl_backward);
794 f = rl_function_of_keyseq ("\033OA", _rl_keymap, (int *)NULL);
795 if (!f || f == rl_do_lowercase_version)
797 _rl_bind_if_unbound ("\033OA", rl_get_previous_history);
798 _rl_bind_if_unbound ("\033OB", rl_get_next_history);
799 _rl_bind_if_unbound ("\033OC", rl_forward);
800 _rl_bind_if_unbound ("\033OD", rl_backward);
804 /* Try and bind the common arrow key prefix after giving termcap and
805 the inputrc file a chance to bind them and create `real' keymaps
806 for the arrow key prefix. */
812 xkeymap = _rl_keymap;
814 _rl_keymap = emacs_standard_keymap;
815 bind_arrow_keys_internal ();
817 #if defined (VI_MODE)
818 _rl_keymap = vi_movement_keymap;
819 bind_arrow_keys_internal ();
822 _rl_keymap = xkeymap;
826 /* **************************************************************** */
828 /* Numeric Arguments */
830 /* **************************************************************** */
832 /* Handle C-u style numeric args, as well as M--, and M-digits. */
836 int key, c, sawminus, sawdigits;
840 RL_SETSTATE(RL_STATE_NUMERICARG);
841 sawminus = sawdigits = 0;
844 if (rl_numeric_arg > 1000000)
846 sawdigits = rl_explicit_arg = rl_numeric_arg = 0;
848 rl_restore_prompt ();
850 RL_UNSETSTATE(RL_STATE_NUMERICARG);
853 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
854 RL_SETSTATE(RL_STATE_MOREINPUT);
855 key = c = rl_read_key ();
856 RL_UNSETSTATE(RL_STATE_MOREINPUT);
858 /* If we see a key bound to `universal-argument' after seeing digits,
859 it ends the argument but is otherwise ignored. */
860 if (_rl_keymap[c].type == ISFUNC &&
861 _rl_keymap[c].function == rl_universal_argument)
870 RL_SETSTATE(RL_STATE_MOREINPUT);
871 key = rl_read_key ();
872 RL_UNSETSTATE(RL_STATE_MOREINPUT);
873 rl_restore_prompt ();
875 RL_UNSETSTATE(RL_STATE_NUMERICARG);
876 return (_rl_dispatch (key, _rl_keymap));
884 rl_numeric_arg = rl_explicit_arg ? (rl_numeric_arg * 10) + c - '0' : c - '0';
885 sawdigits = rl_explicit_arg = 1;
887 else if (c == '-' && rl_explicit_arg == 0)
889 rl_numeric_arg = sawminus = 1;
894 /* Make M-- command equivalent to M--1 command. */
895 if (sawminus && rl_numeric_arg == 1 && rl_explicit_arg == 0)
897 rl_restore_prompt ();
899 RL_UNSETSTATE(RL_STATE_NUMERICARG);
900 return (_rl_dispatch (key, _rl_keymap));
904 RL_UNSETSTATE(RL_STATE_NUMERICARG);
908 /* Add the current digit to the argument in progress. */
910 rl_digit_argument (ignore, key)
913 rl_execute_next (key);
914 return (rl_digit_loop ());
917 /* What to do when you abort reading an argument. */
919 rl_discard_argument ()
923 _rl_init_argument ();
927 /* Create a default argument. */
931 rl_numeric_arg = rl_arg_sign = 1;
936 /* C-u, universal argument. Multiply the current argument by 4.
937 Read a key. If the key has nothing to do with arguments, then
938 dispatch on it. If the key is the abort character then abort. */
940 rl_universal_argument (count, key)
944 return (rl_digit_loop ());
947 /* **************************************************************** */
949 /* Insert and Delete */
951 /* **************************************************************** */
953 /* Insert a string of text into the line at point. This is the only
954 way that you should do insertion. rl_insert () calls this
957 rl_insert_text (string)
960 register int i, l = strlen (string);
962 if (rl_end + l >= rl_line_buffer_len)
963 rl_extend_line_buffer (rl_end + l);
965 for (i = rl_end; i >= rl_point; i--)
966 the_line[i + l] = the_line[i];
967 strncpy (the_line + rl_point, string, l);
969 /* Remember how to undo this if we aren't undoing something. */
970 if (!_rl_doing_an_undo)
972 /* If possible and desirable, concatenate the undos. */
975 (rl_undo_list->what == UNDO_INSERT) &&
976 (rl_undo_list->end == rl_point) &&
977 (rl_undo_list->end - rl_undo_list->start < 20))
980 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
984 the_line[rl_end] = '\0';
988 /* Delete the string between FROM and TO. FROM is
989 inclusive, TO is not. */
991 rl_delete_text (from, to)
995 register int diff, i;
997 /* Fix it if the caller is confused. */
1001 /* fix boundaries */
1009 text = rl_copy_text (from, to);
1011 /* Some versions of strncpy() can't handle overlapping arguments. */
1013 for (i = from; i < rl_end - diff; i++)
1014 the_line[i] = the_line[i + diff];
1016 /* Remember how to undo this delete. */
1017 if (_rl_doing_an_undo == 0)
1018 rl_add_undo (UNDO_DELETE, from, to, text);
1023 the_line[rl_end] = '\0';
1027 /* Fix up point so that it is within the line boundaries after killing
1028 text. If FIX_MARK_TOO is non-zero, the mark is forced within line
1031 #define _RL_FIX_POINT(x) \
1040 _rl_fix_point (fix_mark_too)
1043 _RL_FIX_POINT (rl_point);
1045 _RL_FIX_POINT (rl_mark);
1047 #undef _RL_FIX_POINT
1050 _rl_replace_text (text, start, end)
1054 rl_begin_undo_group ();
1055 rl_delete_text (start, end + 1);
1057 rl_insert_text (text);
1058 rl_end_undo_group ();
1061 /* **************************************************************** */
1063 /* Readline character functions */
1065 /* **************************************************************** */
1067 /* This is not a gap editor, just a stupid line input routine. No hair
1068 is involved in writing any of the functions, and none should be. */
1072 rl_end is the place in the string that we would place '\0';
1073 i.e., it is always safe to place '\0' there.
1075 rl_point is the place in the string where the cursor is. Sometimes
1076 this is the same as rl_end.
1078 Any command that is called interactively receives two arguments.
1079 The first is a count: the numeric arg pased to this command.
1080 The second is the key which invoked this command.
1083 /* **************************************************************** */
1085 /* Movement Commands */
1087 /* **************************************************************** */
1089 /* Note that if you `optimize' the display for these functions, you cannot
1090 use said functions in other functions which do not do optimizing display.
1091 I.e., you will have to update the data base for rl_redisplay, and you
1092 might as well let rl_redisplay do that job. */
1094 /* Move forward COUNT characters. */
1096 rl_forward (count, key)
1100 rl_backward (-count, key);
1103 int end = rl_point + count;
1104 #if defined (VI_MODE)
1105 int lend = rl_end > 0 ? rl_end - (rl_editing_mode == vi_mode) : rl_end;
1125 /* Move backward COUNT characters. */
1127 rl_backward (count, key)
1131 rl_forward (-count, key);
1134 if (rl_point < count)
1145 /* Move to the beginning of the line. */
1147 rl_beg_of_line (count, key)
1154 /* Move to the end of the line. */
1156 rl_end_of_line (count, key)
1163 /* Move forward a word. We do what Emacs does. */
1165 rl_forward_word (count, key)
1172 rl_backward_word (-count, key);
1178 if (rl_point == rl_end)
1181 /* If we are not in a word, move forward until we are in one.
1182 Then, move forward until we hit a non-alphabetic character. */
1183 c = the_line[rl_point];
1184 if (rl_alphabetic (c) == 0)
1186 while (++rl_point < rl_end)
1188 c = the_line[rl_point];
1189 if (rl_alphabetic (c))
1193 if (rl_point == rl_end)
1195 while (++rl_point < rl_end)
1197 c = the_line[rl_point];
1198 if (rl_alphabetic (c) == 0)
1206 /* Move backward a word. We do what Emacs does. */
1208 rl_backward_word (count, key)
1215 rl_forward_word (-count, key);
1224 /* Like rl_forward_word (), except that we look at the characters
1225 just before point. */
1227 c = the_line[rl_point - 1];
1228 if (rl_alphabetic (c) == 0)
1232 c = the_line[rl_point - 1];
1233 if (rl_alphabetic (c))
1240 c = the_line[rl_point - 1];
1241 if (rl_alphabetic (c) == 0)
1251 /* Clear the current line. Numeric argument to C-l does this. */
1253 rl_refresh_line (ignore1, ignore2)
1254 int ignore1, ignore2;
1258 curr_line = _rl_current_display_line ();
1260 _rl_move_vert (curr_line);
1261 _rl_move_cursor_relative (0, the_line); /* XXX is this right */
1263 _rl_clear_to_eol (0); /* arg of 0 means to not use spaces */
1265 rl_forced_update_display ();
1266 rl_display_fixed = 1;
1271 /* C-l typed to a line without quoting clears the screen, and then reprints
1272 the prompt and the current input line. Given a numeric arg, redraw only
1273 the current line. */
1275 rl_clear_screen (count, key)
1278 if (rl_explicit_arg)
1280 rl_refresh_line (count, key);
1284 _rl_clear_screen (); /* calls termcap function to clear screen */
1285 rl_forced_update_display ();
1286 rl_display_fixed = 1;
1292 rl_arrow_keys (count, c)
1297 RL_SETSTATE(RL_STATE_MOREINPUT);
1298 ch = rl_read_key ();
1299 RL_UNSETSTATE(RL_STATE_MOREINPUT);
1301 switch (_rl_to_upper (ch))
1304 rl_get_previous_history (count, ch);
1308 rl_get_next_history (count, ch);
1312 rl_forward (count, ch);
1316 rl_backward (count, ch);
1326 /* **************************************************************** */
1330 /* **************************************************************** */
1332 /* Insert the character C at the current location, moving point forward. */
1334 rl_insert (count, c)
1343 /* If we can optimize, then do it. But don't let people crash
1344 readline because of extra large arguments. */
1345 if (count > 1 && count <= 1024)
1347 string = xmalloc (1 + count);
1349 for (i = 0; i < count; i++)
1353 rl_insert_text (string);
1364 for (i = 0; i < 1024; i++)
1369 decreaser = (count > 1024 ? 1024 : count);
1370 str[decreaser] = '\0';
1371 rl_insert_text (str);
1378 /* We are inserting a single character.
1379 If there is pending input, then make a string of all of the
1380 pending characters that are bound to rl_insert, and insert
1382 if (_rl_any_typein ())
1383 _rl_insert_typein (c);
1386 /* Inserting a single character. */
1391 rl_insert_text (str);
1396 /* Insert the next typed character verbatim. */
1398 rl_quoted_insert (count, key)
1403 #if defined (HANDLE_SIGNALS)
1404 _rl_disable_tty_signals ();
1407 RL_SETSTATE(RL_STATE_MOREINPUT);
1409 RL_UNSETSTATE(RL_STATE_MOREINPUT);
1411 #if defined (HANDLE_SIGNALS)
1412 _rl_restore_tty_signals ();
1415 return (rl_insert (count, c));
1418 /* Insert a tab character. */
1420 rl_tab_insert (count, key)
1423 return (rl_insert (count, '\t'));
1426 /* What to do when a NEWLINE is pressed. We accept the whole line.
1427 KEY is the key that invoked this command. I guess it could have
1428 meaning in the future. */
1430 rl_newline (count, key)
1434 RL_SETSTATE(RL_STATE_DONE);
1436 #if defined (VI_MODE)
1437 if (rl_editing_mode == vi_mode)
1439 _rl_vi_done_inserting ();
1440 _rl_vi_reset_last ();
1442 #endif /* VI_MODE */
1444 /* If we've been asked to erase empty lines, suppress the final update,
1445 since _rl_update_final calls rl_crlf(). */
1446 if (rl_erase_empty_line && rl_point == 0 && rl_end == 0)
1449 if (readline_echoing_p)
1450 _rl_update_final ();
1454 /* What to do for some uppercase characters, like meta characters,
1455 and some characters appearing in emacs_ctlx_keymap. This function
1456 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1457 is special cased. */
1459 rl_do_lowercase_version (ignore1, ignore2)
1460 int ignore1, ignore2;
1465 /* Rubout the character behind point. */
1467 rl_rubout (count, key)
1472 rl_delete (-count, key);
1482 if (count > 1 || rl_explicit_arg)
1484 int orig_point = rl_point;
1485 rl_backward (count, key);
1486 rl_kill_text (orig_point, rl_point);
1490 int c = the_line[--rl_point];
1491 rl_delete_text (rl_point, rl_point + 1);
1493 if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
1496 l = rl_character_len (c, rl_point);
1497 _rl_erase_at_end_of_line (l);
1503 /* Delete the character under the cursor. Given a numeric argument,
1504 kill that many characters instead. */
1506 rl_delete (count, key)
1510 return (rl_rubout (-count, key));
1512 if (rl_point == rl_end)
1518 if (count > 1 || rl_explicit_arg)
1520 int orig_point = rl_point;
1521 rl_forward (count, key);
1522 rl_kill_text (orig_point, rl_point);
1523 rl_point = orig_point;
1527 return (rl_delete_text (rl_point, rl_point + 1));
1530 /* Delete the character under the cursor, unless the insertion
1531 point is at the end of the line, in which case the character
1532 behind the cursor is deleted. COUNT is obeyed and may be used
1533 to delete forward or backward that many characters. */
1535 rl_rubout_or_delete (count, key)
1538 if (rl_end != 0 && rl_point == rl_end)
1539 return (rl_rubout (count, key));
1541 return (rl_delete (count, key));
1544 /* Delete all spaces and tabs around point. */
1546 rl_delete_horizontal_space (count, ignore)
1549 int start = rl_point;
1551 while (rl_point && whitespace (the_line[rl_point - 1]))
1556 while (rl_point < rl_end && whitespace (the_line[rl_point]))
1559 if (start != rl_point)
1561 rl_delete_text (start, rl_point);
1567 /* Like the tcsh editing function delete-char-or-list. The eof character
1568 is caught before this is invoked, so this really does the same thing as
1569 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1571 rl_delete_or_show_completions (count, key)
1574 if (rl_end != 0 && rl_point == rl_end)
1575 return (rl_possible_completions (count, key));
1577 return (rl_delete (count, key));
1580 #ifndef RL_COMMENT_BEGIN_DEFAULT
1581 #define RL_COMMENT_BEGIN_DEFAULT "#"
1584 /* Turn the current line into a comment in shell history.
1585 A K*rn shell style function. */
1587 rl_insert_comment (count, key)
1590 rl_beg_of_line (1, key);
1591 rl_insert_text (_rl_comment_begin ? _rl_comment_begin
1592 : RL_COMMENT_BEGIN_DEFAULT);
1593 (*rl_redisplay_function) ();
1594 rl_newline (1, '\n');
1598 /* **************************************************************** */
1602 /* **************************************************************** */
1604 /* The three kinds of things that we know how to do. */
1609 /* Uppercase the word at point. */
1611 rl_upcase_word (count, key)
1614 return (rl_change_case (count, UpCase));
1617 /* Lowercase the word at point. */
1619 rl_downcase_word (count, key)
1622 return (rl_change_case (count, DownCase));
1625 /* Upcase the first letter, downcase the rest. */
1627 rl_capitalize_word (count, key)
1630 return (rl_change_case (count, CapCase));
1633 /* The meaty function.
1634 Change the case of COUNT words, performing OP on them.
1635 OP is one of UpCase, DownCase, or CapCase.
1636 If a negative argument is given, leave point where it started,
1637 otherwise, leave it where it moves to. */
1639 rl_change_case (count, op)
1642 register int start, end;
1646 rl_forward_word (count, 0);
1652 /* We are going to modify some text, so let's prepare to undo it. */
1653 rl_modifying (start, end);
1655 for (inword = 0; start < end; start++)
1657 c = the_line[start];
1661 the_line[start] = _rl_to_upper (c);
1665 the_line[start] = _rl_to_lower (c);
1669 the_line[start] = (inword == 0) ? _rl_to_upper (c) : _rl_to_lower (c);
1670 inword = rl_alphabetic (the_line[start]);
1682 /* **************************************************************** */
1686 /* **************************************************************** */
1688 /* Transpose the words at point. */
1690 rl_transpose_words (count, key)
1693 char *word1, *word2;
1694 int w1_beg, w1_end, w2_beg, w2_end;
1695 int orig_point = rl_point;
1700 /* Find the two words. */
1701 rl_forward_word (count, key);
1703 rl_backward_word (1, key);
1705 rl_backward_word (count, key);
1707 rl_forward_word (1, key);
1710 /* Do some check to make sure that there really are two words. */
1711 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
1714 rl_point = orig_point;
1718 /* Get the text of the words. */
1719 word1 = rl_copy_text (w1_beg, w1_end);
1720 word2 = rl_copy_text (w2_beg, w2_end);
1722 /* We are about to do many insertions and deletions. Remember them
1723 as one operation. */
1724 rl_begin_undo_group ();
1726 /* Do the stuff at word2 first, so that we don't have to worry
1727 about word1 moving. */
1729 rl_delete_text (w2_beg, w2_end);
1730 rl_insert_text (word1);
1733 rl_delete_text (w1_beg, w1_end);
1734 rl_insert_text (word2);
1736 /* This is exactly correct since the text before this point has not
1737 changed in length. */
1740 /* I think that does it. */
1741 rl_end_undo_group ();
1748 /* Transpose the characters at point. If point is at the end of the line,
1749 then transpose the characters before point. */
1751 rl_transpose_chars (count, key)
1759 if (!rl_point || rl_end < 2)
1765 rl_begin_undo_group ();
1767 if (rl_point == rl_end)
1774 dummy[0] = the_line[rl_point];
1777 rl_delete_text (rl_point, rl_point + 1);
1781 rl_insert_text (dummy);
1783 rl_end_undo_group ();
1787 /* **************************************************************** */
1789 /* Character Searching */
1791 /* **************************************************************** */
1794 _rl_char_search_internal (count, dir, schar)
1795 int count, dir, schar;
1800 inc = (dir < 0) ? -1 : 1;
1803 if ((dir < 0 && pos <= 0) || (dir > 0 && pos >= rl_end))
1812 if (rl_line_buffer[pos] == schar)
1816 rl_point = (dir == BTO) ? pos + 1 : pos;
1818 rl_point = (dir == FTO) ? pos - 1 : pos;
1822 while ((dir < 0) ? pos-- : ++pos < rl_end);
1827 /* Search COUNT times for a character read from the current input stream.
1828 FDIR is the direction to search if COUNT is non-negative; otherwise
1829 the search goes in BDIR. */
1831 _rl_char_search (count, fdir, bdir)
1832 int count, fdir, bdir;
1836 RL_SETSTATE(RL_STATE_MOREINPUT);
1838 RL_UNSETSTATE(RL_STATE_MOREINPUT);
1841 return (_rl_char_search_internal (-count, bdir, c));
1843 return (_rl_char_search_internal (count, fdir, c));
1847 rl_char_search (count, key)
1850 return (_rl_char_search (count, FFIND, BFIND));
1854 rl_backward_char_search (count, key)
1857 return (_rl_char_search (count, BFIND, FFIND));
1860 /* **************************************************************** */
1862 /* History Utilities */
1864 /* **************************************************************** */
1866 /* We already have a history library, and that is what we use to control
1867 the history features of readline. This is our local interface to
1868 the history mechanism. */
1870 /* While we are editing the history, this is the saved
1871 version of the original line. */
1872 HIST_ENTRY *_rl_saved_line_for_history = (HIST_ENTRY *)NULL;
1874 /* Set the history pointer back to the last entry in the history. */
1876 start_using_history ()
1879 if (_rl_saved_line_for_history)
1880 _rl_free_history_entry (_rl_saved_line_for_history);
1882 _rl_saved_line_for_history = (HIST_ENTRY *)NULL;
1885 /* Free the contents (and containing structure) of a HIST_ENTRY. */
1887 _rl_free_history_entry (entry)
1897 /* Perhaps put back the current line if it has changed. */
1899 rl_maybe_replace_line ()
1903 temp = current_history ();
1904 /* If the current line has changed, save the changes. */
1905 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
1907 temp = replace_history_entry (where_history (), the_line, (histdata_t)rl_undo_list);
1914 /* Restore the _rl_saved_line_for_history if there is one. */
1916 rl_maybe_unsave_line ()
1920 if (_rl_saved_line_for_history)
1922 line_len = strlen (_rl_saved_line_for_history->line);
1924 if (line_len >= rl_line_buffer_len)
1925 rl_extend_line_buffer (line_len);
1927 strcpy (the_line, _rl_saved_line_for_history->line);
1928 rl_undo_list = (UNDO_LIST *)_rl_saved_line_for_history->data;
1929 _rl_free_history_entry (_rl_saved_line_for_history);
1930 _rl_saved_line_for_history = (HIST_ENTRY *)NULL;
1931 rl_end = rl_point = strlen (the_line);
1938 /* Save the current line in _rl_saved_line_for_history. */
1940 rl_maybe_save_line ()
1942 if (_rl_saved_line_for_history == 0)
1944 _rl_saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
1945 _rl_saved_line_for_history->line = savestring (the_line);
1946 _rl_saved_line_for_history->data = (char *)rl_undo_list;
1952 _rl_free_saved_history_line ()
1954 if (_rl_saved_line_for_history)
1956 _rl_free_history_entry (_rl_saved_line_for_history);
1957 _rl_saved_line_for_history = (HIST_ENTRY *)NULL;
1962 /* **************************************************************** */
1964 /* History Commands */
1966 /* **************************************************************** */
1968 /* Meta-< goes to the start of the history. */
1970 rl_beginning_of_history (count, key)
1973 return (rl_get_previous_history (1 + where_history (), key));
1976 /* Meta-> goes to the end of the history. (The current line). */
1978 rl_end_of_history (count, key)
1981 rl_maybe_replace_line ();
1983 rl_maybe_unsave_line ();
1987 /* Move down to the next history line. */
1989 rl_get_next_history (count, key)
1996 return (rl_get_previous_history (-count, key));
2001 rl_maybe_replace_line ();
2003 temp = (HIST_ENTRY *)NULL;
2006 temp = next_history ();
2013 rl_maybe_unsave_line ();
2016 line_len = strlen (temp->line);
2018 if (line_len >= rl_line_buffer_len)
2019 rl_extend_line_buffer (line_len);
2021 strcpy (the_line, temp->line);
2022 rl_undo_list = (UNDO_LIST *)temp->data;
2023 rl_end = rl_point = strlen (the_line);
2024 #if defined (VI_MODE)
2025 if (rl_editing_mode == vi_mode)
2027 #endif /* VI_MODE */
2032 /* Get the previous item out of our interactive history, making it the current
2033 line. If there is no previous history, just ding. */
2035 rl_get_previous_history (count, key)
2038 HIST_ENTRY *old_temp, *temp;
2042 return (rl_get_next_history (-count, key));
2047 /* If we don't have a line saved, then save this one. */
2048 rl_maybe_save_line ();
2050 /* If the current line has changed, save the changes. */
2051 rl_maybe_replace_line ();
2053 temp = old_temp = (HIST_ENTRY *)NULL;
2056 temp = previous_history ();
2064 /* If there was a large argument, and we moved back to the start of the
2065 history, that is not an error. So use the last value found. */
2066 if (!temp && old_temp)
2073 line_len = strlen (temp->line);
2075 if (line_len >= rl_line_buffer_len)
2076 rl_extend_line_buffer (line_len);
2078 strcpy (the_line, temp->line);
2079 rl_undo_list = (UNDO_LIST *)temp->data;
2080 rl_end = rl_point = line_len;
2082 #if defined (VI_MODE)
2083 if (rl_editing_mode == vi_mode)
2085 #endif /* VI_MODE */
2090 /* **************************************************************** */
2092 /* The Mark and the Region. */
2094 /* **************************************************************** */
2096 /* Set the mark at POSITION. */
2098 _rl_set_mark_at_pos (position)
2101 if (position > rl_end)
2108 /* A bindable command to set the mark. */
2110 rl_set_mark (count, key)
2113 return (_rl_set_mark_at_pos (rl_explicit_arg ? count : rl_point));
2116 /* Exchange the position of mark and point. */
2118 rl_exchange_point_and_mark (count, key)
2121 if (rl_mark > rl_end)
2130 SWAP (rl_point, rl_mark);
2135 /* **************************************************************** */
2139 /* **************************************************************** */
2140 /* How to toggle back and forth between editing modes. */
2142 rl_vi_editing_mode (count, key)
2145 #if defined (VI_MODE)
2146 rl_editing_mode = vi_mode;
2147 rl_vi_insertion_mode (1, key);
2148 #endif /* VI_MODE */
2153 rl_emacs_editing_mode (count, key)
2156 rl_editing_mode = emacs_mode;
2157 _rl_keymap = emacs_standard_keymap;