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 1, 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 675 Mass Ave, Cambridge, MA 02139, 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)
54 /* System-specific feature definitions and include files. */
58 # define INCL_DOSPROCESS
62 /* Some standard library routines. */
66 #ifndef RL_LIBRARY_VERSION
67 # define RL_LIBRARY_VERSION "4.0"
70 /* Evaluates its arguments multiple times. */
71 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
73 /* NOTE: Functions and variables prefixed with `_rl_' are
74 pseudo-global: they are global so they can be shared
75 between files in the readline library, but are not intended
76 to be visible to readline callers. */
78 /* Variables and functions imported from terminal.c */
79 extern int _rl_init_terminal_io ();
80 extern void _rl_enable_meta_key ();
82 extern void _rl_output_character_function ();
84 extern int _rl_output_character_function ();
87 extern int _rl_enable_meta;
88 extern int _rl_term_autowrap;
89 extern int screenwidth, screenheight, screenchars;
91 /* Variables and functions imported from rltty.c. */
92 extern void rl_prep_terminal (), rl_deprep_terminal ();
93 extern void rltty_set_default_bindings ();
95 /* Functions imported from util.c. */
96 extern void _rl_abort_internal ();
97 extern void rl_extend_line_buffer ();
98 extern int alphabetic ();
100 /* Functions imported from bind.c. */
101 extern void _rl_bind_if_unbound ();
103 /* Functions imported from input.c. */
104 extern int _rl_any_typein ();
105 extern void _rl_insert_typein ();
106 extern int rl_read_key ();
108 /* Functions imported from nls.c */
109 extern int _rl_init_eightbit ();
111 /* Functions imported from shell.c */
112 extern char *get_env_value ();
114 /* External redisplay functions and variables from display.c */
115 extern void _rl_move_vert ();
116 extern void _rl_update_final ();
117 extern void _rl_clear_to_eol ();
118 extern void _rl_clear_screen ();
119 extern void _rl_erase_entire_line ();
121 extern void _rl_erase_at_end_of_line ();
122 extern void _rl_move_cursor_relative ();
124 extern int _rl_vis_botlin;
125 extern int _rl_last_c_pos;
126 extern int _rl_horizontal_scroll_mode;
127 extern int rl_display_fixed;
128 extern int _rl_suppress_redisplay;
129 extern char *rl_display_prompt;
131 /* Variables imported from complete.c. */
132 extern char *rl_completer_word_break_characters;
133 extern char *rl_basic_word_break_characters;
134 extern int rl_completion_query_items;
135 extern int rl_complete_with_tilde_expansion;
137 /* Variables and functions from macro.c. */
138 extern void _rl_add_macro_char ();
139 extern void _rl_with_macro_input ();
140 extern int _rl_next_macro_key ();
141 extern int _rl_defining_kbd_macro;
143 #if defined (VI_MODE)
144 /* Functions imported from vi_mode.c. */
145 extern void _rl_vi_set_last ();
146 extern void _rl_vi_reset_last ();
147 extern void _rl_vi_done_inserting ();
148 extern int _rl_vi_textmod_command ();
149 extern void _rl_vi_initialize_line ();
152 extern UNDO_LIST *rl_undo_list;
153 extern int _rl_doing_an_undo;
155 /* Forward declarations used in this file. */
156 void _rl_free_history_entry ();
159 int _rl_init_argument ();
161 static char *readline_internal ();
162 static void readline_initialize_everything ();
163 static void start_using_history ();
164 static void bind_arrow_keys ();
166 #if !defined (__GO32__)
167 static void readline_default_bindings ();
168 #endif /* !__GO32__ */
170 #if defined (__GO32__)
173 # undef HANDLE_SIGNALS
174 #endif /* __GO32__ */
176 extern char *xmalloc (), *xrealloc ();
178 /* **************************************************************** */
180 /* Line editing input utility */
182 /* **************************************************************** */
184 char *rl_library_version = RL_LIBRARY_VERSION;
186 /* A pointer to the keymap that is currently in use.
187 By default, it is the standard emacs keymap. */
188 Keymap _rl_keymap = emacs_standard_keymap;
190 /* The current style of editing. */
191 int rl_editing_mode = emacs_mode;
193 /* Non-zero if we called this function from _rl_dispatch(). It's present
194 so functions can find out whether they were called from a key binding
195 or directly from an application. */
198 /* Non-zero if the previous command was a kill command. */
199 int _rl_last_command_was_kill = 0;
201 /* The current value of the numeric argument specified by the user. */
202 int rl_numeric_arg = 1;
204 /* Non-zero if an argument was typed. */
205 int rl_explicit_arg = 0;
207 /* Temporary value used while generating the argument. */
210 /* Non-zero means we have been called at least once before. */
211 static int rl_initialized;
213 /* If non-zero, this program is running in an EMACS buffer. */
214 static int running_in_emacs;
216 /* The current offset in the current input line. */
219 /* Mark in the current input line. */
222 /* Length of the current input line. */
225 /* Make this non-zero to return the current input_line. */
228 /* The last function executed by readline. */
229 Function *rl_last_func = (Function *)NULL;
231 /* Top level environment for readline_internal (). */
232 procenv_t readline_top_level;
234 /* The streams we interact with. */
235 FILE *_rl_in_stream, *_rl_out_stream;
237 /* The names of the streams that we do input and output to. */
238 FILE *rl_instream = (FILE *)NULL;
239 FILE *rl_outstream = (FILE *)NULL;
241 /* Non-zero means echo characters as they are read. */
242 int readline_echoing_p = 1;
244 /* Current prompt. */
246 int rl_visible_prompt_length = 0;
248 /* The number of characters read in order to type this complete command. */
249 int rl_key_sequence_length = 0;
251 /* If non-zero, then this is the address of a function to call just
252 before readline_internal_setup () prints the first prompt. */
253 Function *rl_startup_hook = (Function *)NULL;
255 /* If non-zero, this is the address of a function to call just before
256 readline_internal_setup () returns and readline_internal starts
257 reading input characters. */
258 Function *rl_pre_input_hook = (Function *)NULL;
260 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
261 static char *the_line;
263 /* The character that can generate an EOF. Really read from
264 the terminal driver... just defaulted here. */
265 int _rl_eof_char = CTRL ('D');
267 /* Non-zero makes this the next keystroke to read. */
268 int rl_pending_input = 0;
270 /* Pointer to a useful terminal name. */
271 char *rl_terminal_name = (char *)NULL;
273 /* Non-zero means to always use horizontal scrolling in line display. */
274 int _rl_horizontal_scroll_mode = 0;
276 /* Non-zero means to display an asterisk at the starts of history lines
277 which have been modified. */
278 int _rl_mark_modified_lines = 0;
280 /* The style of `bell' notification preferred. This can be set to NO_BELL,
281 AUDIBLE_BELL, or VISIBLE_BELL. */
282 int _rl_bell_preference = AUDIBLE_BELL;
284 /* String inserted into the line by rl_insert_comment (). */
285 char *_rl_comment_begin;
287 /* Keymap holding the function currently being executed. */
288 Keymap rl_executing_keymap;
290 /* Non-zero means to erase entire line, including prompt, on empty input lines. */
291 int rl_erase_empty_line = 0;
293 /* Line buffer and maintenence. */
294 char *rl_line_buffer = (char *)NULL;
295 int rl_line_buffer_len = 0;
297 /* Forward declarations used by the display and termcap code. */
299 /* **************************************************************** */
301 /* `Forward' declarations */
303 /* **************************************************************** */
305 /* Non-zero means do not parse any lines other than comments and
306 parser directives. */
307 unsigned char _rl_parsing_conditionalized_out = 0;
309 /* Non-zero means to convert characters with the meta bit set to
310 escape-prefixed characters so we can indirect through
311 emacs_meta_keymap or vi_escape_keymap. */
312 int _rl_convert_meta_chars_to_ascii = 1;
314 /* Non-zero means to output characters with the meta bit set directly
315 rather than as a meta-prefixed escape sequence. */
316 int _rl_output_meta_chars = 0;
318 /* **************************************************************** */
320 /* Top Level Functions */
322 /* **************************************************************** */
324 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
325 int _rl_meta_flag = 0; /* Forward declaration */
327 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
328 none. A return value of NULL means that EOF was encountered. */
337 /* If we are at EOF return a NULL string. */
338 if (rl_pending_input == EOF)
340 rl_pending_input = 0;
341 return ((char *)NULL);
344 rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
347 (*rl_prep_term_function) (_rl_meta_flag);
349 #if defined (HANDLE_SIGNALS)
353 value = readline_internal ();
354 (*rl_deprep_term_function) ();
356 #if defined (HANDLE_SIGNALS)
363 #if defined (READLINE_CALLBACKS)
364 # define STATIC_CALLBACK
366 # define STATIC_CALLBACK static
370 readline_internal_setup ()
372 _rl_in_stream = rl_instream;
373 _rl_out_stream = rl_outstream;
376 (*rl_startup_hook) ();
378 if (readline_echoing_p == 0)
382 fprintf (_rl_out_stream, "%s", rl_prompt);
383 fflush (_rl_out_stream);
389 (*rl_redisplay_function) ();
390 #if defined (VI_MODE)
391 if (rl_editing_mode == vi_mode)
392 rl_vi_insertion_mode (1, 0);
396 if (rl_pre_input_hook)
397 (*rl_pre_input_hook) ();
400 STATIC_CALLBACK char *
401 readline_internal_teardown (eof)
407 /* Restore the original of this history line, iff the line that we
408 are editing was originally in the history, AND the line has changed. */
409 entry = current_history ();
411 if (entry && rl_undo_list)
413 temp = savestring (the_line);
414 rl_revert_line (1, 0);
415 entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
416 _rl_free_history_entry (entry);
418 strcpy (the_line, temp);
422 /* At any rate, it is highly likely that this line has an undo list. Get
427 return (eof ? (char *)NULL : savestring (the_line));
431 #if defined (READLINE_CALLBACKS)
432 readline_internal_char ()
434 readline_internal_charloop ()
437 static int lastc, eof_found;
443 #if !defined (READLINE_CALLBACKS)
447 lk = _rl_last_command_was_kill;
449 code = setjmp (readline_top_level);
452 (*rl_redisplay_function) ();
454 if (rl_pending_input == 0)
456 /* Then initialize the argument and number of keys read. */
457 _rl_init_argument ();
458 rl_key_sequence_length = 0;
463 /* EOF typed to a non-blank line is a <NL>. */
464 if (c == EOF && rl_end)
467 /* The character _rl_eof_char typed to blank line, and not as the
468 previous character is interpreted as EOF. */
469 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
471 #if defined (READLINE_CALLBACKS)
472 return (rl_done = 1);
480 _rl_dispatch (c, _rl_keymap);
482 /* If there was no change in _rl_last_command_was_kill, then no kill
483 has taken place. Note that if input is pending we are reading
484 a prefix command, so nothing has changed yet. */
485 if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
486 _rl_last_command_was_kill = 0;
488 #if defined (VI_MODE)
489 /* In vi mode, when you exit insert mode, the cursor moves back
490 over the previous character. We explicitly check for that here. */
491 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
496 (*rl_redisplay_function) ();
498 /* If the application writer has told us to erase the entire line if
499 the only character typed was something bound to rl_newline, do so. */
500 if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
501 rl_point == 0 && rl_end == 0)
502 _rl_erase_entire_line ();
504 #if defined (READLINE_CALLBACKS)
513 #if defined (READLINE_CALLBACKS)
515 readline_internal_charloop ()
520 eof = readline_internal_char ();
523 #endif /* READLINE_CALLBACKS */
525 /* Read a line of input from the global rl_instream, doing output on
526 the global rl_outstream.
527 If rl_prompt is non-null, then that is our prompt. */
533 readline_internal_setup ();
534 eof = readline_internal_charloop ();
535 return (readline_internal_teardown (eof));
539 _rl_init_line_state ()
541 rl_point = rl_end = 0;
542 the_line = rl_line_buffer;
549 the_line = rl_line_buffer;
552 /* Do the command associated with KEY in MAP.
553 If the associated command is really a keymap, then read
554 another key, and dispatch into that map. */
556 _rl_dispatch (key, map)
564 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
566 if (map[ESC].type == ISKMAP)
568 if (_rl_defining_kbd_macro)
569 _rl_add_macro_char (ESC);
570 map = FUNCTION_TO_KEYMAP (map, ESC);
572 rl_key_sequence_length += 2;
573 return (_rl_dispatch (key, map));
580 if (_rl_defining_kbd_macro)
581 _rl_add_macro_char (key);
584 switch (map[key].type)
587 func = map[key].function;
588 if (func != (Function *)NULL)
590 /* Special case rl_do_lowercase_version (). */
591 if (func == rl_do_lowercase_version)
592 return (_rl_dispatch (_rl_to_lower (key), map));
594 rl_executing_keymap = map;
597 _rl_suppress_redisplay = (map[key].function == rl_insert) && _rl_input_available ();
601 r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
604 /* If we have input pending, then the last command was a prefix
605 command. Don't change the state of rl_last_func. Otherwise,
606 remember the last command executed in this variable. */
607 if (!rl_pending_input && map[key].function != rl_digit_argument)
608 rl_last_func = map[key].function;
612 _rl_abort_internal ();
618 if (map[key].function != (Function *)NULL)
620 rl_key_sequence_length++;
621 newkey = rl_read_key ();
622 r = _rl_dispatch (newkey, FUNCTION_TO_KEYMAP (map, key));
626 _rl_abort_internal ();
632 if (map[key].function != (Function *)NULL)
634 macro = savestring ((char *)map[key].function);
635 _rl_with_macro_input (macro);
640 #if defined (VI_MODE)
641 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
642 _rl_vi_textmod_command (key))
643 _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
648 /* **************************************************************** */
650 /* Initializations */
652 /* **************************************************************** */
654 /* Initialize readline (and terminal if not already). */
658 /* If we have never been called before, initialize the
659 terminal and data structures. */
662 readline_initialize_everything ();
666 /* Initalize the current line information. */
667 _rl_init_line_state ();
669 /* We aren't done yet. We haven't even gotten started yet! */
672 /* Tell the history routines what is going on. */
673 start_using_history ();
675 /* Make the display buffer match the state of the line. */
676 rl_reset_line_state ();
678 /* No such function typed yet. */
679 rl_last_func = (Function *)NULL;
681 /* Parsing of key-bindings begins in an enabled state. */
682 _rl_parsing_conditionalized_out = 0;
684 #if defined (VI_MODE)
685 if (rl_editing_mode == vi_mode)
686 _rl_vi_initialize_line ();
692 #if defined (__EMX__)
694 _emx_build_environ ()
701 DosGetInfoBlocks (&tibp, &pibp);
702 t = pibp->pib_pchenv;
705 tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
706 t = pibp->pib_pchenv;
716 /* Initialize the entire state of the world. */
718 readline_initialize_everything ()
720 #if defined (__EMX__)
722 _emx_build_environ ();
725 /* Find out if we are running in Emacs. */
726 running_in_emacs = get_env_value ("EMACS") != (char *)0;
728 /* Set up input and output if they are not already set up. */
733 rl_outstream = stdout;
735 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
736 may change, but they may also be used before readline_internal ()
738 _rl_in_stream = rl_instream;
739 _rl_out_stream = rl_outstream;
741 /* Allocate data structures. */
742 if (rl_line_buffer == 0)
743 rl_line_buffer = xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
745 /* Initialize the terminal interface. */
746 _rl_init_terminal_io ((char *)NULL);
748 #if !defined (__GO32__)
749 /* Bind tty characters to readline functions. */
750 readline_default_bindings ();
751 #endif /* !__GO32__ */
753 /* Initialize the function names. */
754 rl_initialize_funmap ();
756 /* Decide whether we should automatically go into eight-bit mode. */
757 _rl_init_eightbit ();
759 /* Read in the init file. */
760 rl_read_init_file ((char *)NULL);
763 if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
766 screenchars -= screenheight;
769 /* Override the effect of any `set keymap' assignments in the
771 rl_set_keymap_from_edit_mode ();
773 /* Try to bind a common arrow key prefix, if not already bound. */
776 /* Enable the meta key, if this terminal has one. */
778 _rl_enable_meta_key ();
780 /* If the completion parser's default word break characters haven't
781 been set yet, then do so now. */
782 if (rl_completer_word_break_characters == (char *)NULL)
783 rl_completer_word_break_characters = rl_basic_word_break_characters;
786 /* If this system allows us to look at the values of the regular
787 input editing characters, then bind them to their readline
788 equivalents, iff the characters are not bound to keymaps. */
790 readline_default_bindings ()
792 rltty_set_default_bindings (_rl_keymap);
796 bind_arrow_keys_internal ()
800 f = rl_function_of_keyseq ("\033[A", _rl_keymap, (int *)NULL);
801 if (!f || f == rl_do_lowercase_version)
803 _rl_bind_if_unbound ("\033[A", rl_get_previous_history);
804 _rl_bind_if_unbound ("\033[B", rl_get_next_history);
805 _rl_bind_if_unbound ("\033[C", rl_forward);
806 _rl_bind_if_unbound ("\033[D", rl_backward);
809 f = rl_function_of_keyseq ("\033OA", _rl_keymap, (int *)NULL);
810 if (!f || f == rl_do_lowercase_version)
812 _rl_bind_if_unbound ("\033OA", rl_get_previous_history);
813 _rl_bind_if_unbound ("\033OB", rl_get_next_history);
814 _rl_bind_if_unbound ("\033OC", rl_forward);
815 _rl_bind_if_unbound ("\033OD", rl_backward);
819 /* Try and bind the common arrow key prefix after giving termcap and
820 the inputrc file a chance to bind them and create `real' keymaps
821 for the arrow key prefix. */
827 xkeymap = _rl_keymap;
829 _rl_keymap = emacs_standard_keymap;
830 bind_arrow_keys_internal ();
832 #if defined (VI_MODE)
833 _rl_keymap = vi_movement_keymap;
834 bind_arrow_keys_internal ();
837 _rl_keymap = xkeymap;
841 /* **************************************************************** */
843 /* Numeric Arguments */
845 /* **************************************************************** */
847 /* Handle C-u style numeric args, as well as M--, and M-digits. */
851 int key, c, sawminus, sawdigits;
855 sawminus = sawdigits = 0;
858 if (rl_numeric_arg > 1000000)
860 sawdigits = rl_explicit_arg = rl_numeric_arg = 0;
862 rl_restore_prompt ();
866 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
867 key = c = rl_read_key ();
869 /* If we see a key bound to `universal-argument' after seeing digits,
870 it ends the argument but is otherwise ignored. */
871 if (_rl_keymap[c].type == ISFUNC &&
872 _rl_keymap[c].function == rl_universal_argument)
881 key = rl_read_key ();
882 rl_restore_prompt ();
884 return (_rl_dispatch (key, _rl_keymap));
892 rl_numeric_arg = rl_explicit_arg ? (rl_numeric_arg * 10) + c - '0' : c - '0';
893 sawdigits = rl_explicit_arg = 1;
895 else if (c == '-' && rl_explicit_arg == 0)
897 rl_numeric_arg = sawminus = 1;
902 /* Make M-- command equivalent to M--1 command. */
903 if (sawminus && rl_numeric_arg == 1 && rl_explicit_arg == 0)
905 rl_restore_prompt ();
907 return (_rl_dispatch (key, _rl_keymap));
914 /* Add the current digit to the argument in progress. */
916 rl_digit_argument (ignore, key)
919 rl_pending_input = key;
920 return (rl_digit_loop ());
923 /* What to do when you abort reading an argument. */
925 rl_discard_argument ()
929 _rl_init_argument ();
933 /* Create a default argument. */
937 rl_numeric_arg = rl_arg_sign = 1;
942 /* C-u, universal argument. Multiply the current argument by 4.
943 Read a key. If the key has nothing to do with arguments, then
944 dispatch on it. If the key is the abort character then abort. */
946 rl_universal_argument (count, key)
950 return (rl_digit_loop ());
953 /* **************************************************************** */
955 /* Insert and Delete */
957 /* **************************************************************** */
959 /* Insert a string of text into the line at point. This is the only
960 way that you should do insertion. rl_insert () calls this
963 rl_insert_text (string)
966 register int i, l = strlen (string);
968 if (rl_end + l >= rl_line_buffer_len)
969 rl_extend_line_buffer (rl_end + l);
971 for (i = rl_end; i >= rl_point; i--)
972 the_line[i + l] = the_line[i];
973 strncpy (the_line + rl_point, string, l);
975 /* Remember how to undo this if we aren't undoing something. */
976 if (!_rl_doing_an_undo)
978 /* If possible and desirable, concatenate the undos. */
981 (rl_undo_list->what == UNDO_INSERT) &&
982 (rl_undo_list->end == rl_point) &&
983 (rl_undo_list->end - rl_undo_list->start < 20))
986 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
990 the_line[rl_end] = '\0';
994 /* Delete the string between FROM and TO. FROM is
995 inclusive, TO is not. */
997 rl_delete_text (from, to)
1000 register char *text;
1001 register int diff, i;
1003 /* Fix it if the caller is confused. */
1007 /* fix boundaries */
1015 text = rl_copy_text (from, to);
1017 /* Some versions of strncpy() can't handle overlapping arguments. */
1019 for (i = from; i < rl_end - diff; i++)
1020 the_line[i] = the_line[i + diff];
1022 /* Remember how to undo this delete. */
1023 if (_rl_doing_an_undo == 0)
1024 rl_add_undo (UNDO_DELETE, from, to, text);
1029 the_line[rl_end] = '\0';
1033 /* Fix up point so that it is within the line boundaries after killing
1034 text. If FIX_MARK_TOO is non-zero, the mark is forced within line
1037 #define _RL_FIX_POINT(x) \
1046 _rl_fix_point (fix_mark_too)
1049 _RL_FIX_POINT (rl_point);
1051 _RL_FIX_POINT (rl_mark);
1053 #undef _RL_FIX_POINT
1056 _rl_replace_text (text, start, end)
1060 rl_begin_undo_group ();
1061 rl_delete_text (start, end + 1);
1063 rl_insert_text (text);
1064 rl_end_undo_group ();
1067 /* **************************************************************** */
1069 /* Readline character functions */
1071 /* **************************************************************** */
1073 /* This is not a gap editor, just a stupid line input routine. No hair
1074 is involved in writing any of the functions, and none should be. */
1078 rl_end is the place in the string that we would place '\0';
1079 i.e., it is always safe to place '\0' there.
1081 rl_point is the place in the string where the cursor is. Sometimes
1082 this is the same as rl_end.
1084 Any command that is called interactively receives two arguments.
1085 The first is a count: the numeric arg pased to this command.
1086 The second is the key which invoked this command.
1089 /* **************************************************************** */
1091 /* Movement Commands */
1093 /* **************************************************************** */
1095 /* Note that if you `optimize' the display for these functions, you cannot
1096 use said functions in other functions which do not do optimizing display.
1097 I.e., you will have to update the data base for rl_redisplay, and you
1098 might as well let rl_redisplay do that job. */
1100 /* Move forward COUNT characters. */
1102 rl_forward (count, key)
1106 rl_backward (-count, key);
1109 int end = rl_point + count;
1110 #if defined (VI_MODE)
1111 int lend = rl_end - (rl_editing_mode == vi_mode);
1127 /* Move backward COUNT characters. */
1129 rl_backward (count, key)
1133 rl_forward (-count, key);
1136 if (rl_point < count)
1147 /* Move to the beginning of the line. */
1149 rl_beg_of_line (count, key)
1156 /* Move to the end of the line. */
1158 rl_end_of_line (count, key)
1165 /* Move forward a word. We do what Emacs does. */
1167 rl_forward_word (count, key)
1174 rl_backward_word (-count, key);
1180 if (rl_point == rl_end)
1183 /* If we are not in a word, move forward until we are in one.
1184 Then, move forward until we hit a non-alphabetic character. */
1185 c = the_line[rl_point];
1186 if (alphabetic (c) == 0)
1188 while (++rl_point < rl_end)
1190 c = the_line[rl_point];
1195 if (rl_point == rl_end)
1197 while (++rl_point < rl_end)
1199 c = the_line[rl_point];
1200 if (alphabetic (c) == 0)
1208 /* Move backward a word. We do what Emacs does. */
1210 rl_backward_word (count, key)
1217 rl_forward_word (-count, key);
1226 /* Like rl_forward_word (), except that we look at the characters
1227 just before point. */
1229 c = the_line[rl_point - 1];
1230 if (alphabetic (c) == 0)
1234 c = the_line[rl_point - 1];
1242 c = the_line[rl_point - 1];
1243 if (alphabetic (c) == 0)
1253 /* Clear the current line. Numeric argument to C-l does this. */
1255 rl_refresh_line (ignore1, ignore2)
1256 int ignore1, ignore2;
1258 int curr_line, nleft;
1260 /* Find out whether or not there might be invisible characters in the
1262 if (rl_display_prompt == rl_prompt)
1263 nleft = _rl_last_c_pos - screenwidth - rl_visible_prompt_length;
1265 nleft = _rl_last_c_pos - screenwidth;
1268 curr_line = 1 + nleft / screenwidth;
1272 _rl_move_vert (curr_line);
1273 _rl_move_cursor_relative (0, the_line); /* XXX is this right */
1275 #if defined (__GO32__)
1277 int row, col, width, row_start;
1279 ScreenGetCursor (&row, &col);
1280 width = ScreenCols ();
1281 row_start = ScreenPrimary + (row * width);
1282 memset (row_start + col, 0, (width - col) * 2);
1284 #else /* !__GO32__ */
1285 _rl_clear_to_eol (0); /* arg of 0 means to not use spaces */
1286 #endif /* !__GO32__ */
1288 rl_forced_update_display ();
1289 rl_display_fixed = 1;
1294 /* C-l typed to a line without quoting clears the screen, and then reprints
1295 the prompt and the current input line. Given a numeric arg, redraw only
1296 the current line. */
1298 rl_clear_screen (count, key)
1301 if (rl_explicit_arg)
1303 rl_refresh_line (count, key);
1307 _rl_clear_screen (); /* calls termcap function to clear screen */
1308 rl_forced_update_display ();
1309 rl_display_fixed = 1;
1315 rl_arrow_keys (count, c)
1320 ch = rl_read_key ();
1322 switch (_rl_to_upper (ch))
1325 rl_get_previous_history (count, ch);
1329 rl_get_next_history (count, ch);
1333 rl_forward (count, ch);
1337 rl_backward (count, ch);
1347 /* **************************************************************** */
1351 /* **************************************************************** */
1353 /* Insert the character C at the current location, moving point forward. */
1355 rl_insert (count, c)
1364 /* If we can optimize, then do it. But don't let people crash
1365 readline because of extra large arguments. */
1366 if (count > 1 && count <= 1024)
1368 string = xmalloc (1 + count);
1370 for (i = 0; i < count; i++)
1374 rl_insert_text (string);
1385 for (i = 0; i < 1024; i++)
1390 decreaser = (count > 1024 ? 1024 : count);
1391 str[decreaser] = '\0';
1392 rl_insert_text (str);
1399 /* We are inserting a single character.
1400 If there is pending input, then make a string of all of the
1401 pending characters that are bound to rl_insert, and insert
1403 if (_rl_any_typein ())
1404 _rl_insert_typein (c);
1407 /* Inserting a single character. */
1412 rl_insert_text (str);
1417 /* Insert the next typed character verbatim. */
1419 rl_quoted_insert (count, key)
1425 return (rl_insert (count, c));
1428 /* Insert a tab character. */
1430 rl_tab_insert (count, key)
1433 return (rl_insert (count, '\t'));
1436 /* What to do when a NEWLINE is pressed. We accept the whole line.
1437 KEY is the key that invoked this command. I guess it could have
1438 meaning in the future. */
1440 rl_newline (count, key)
1445 #if defined (VI_MODE)
1446 if (rl_editing_mode == vi_mode)
1448 _rl_vi_done_inserting ();
1449 _rl_vi_reset_last ();
1451 #endif /* VI_MODE */
1453 /* If we've been asked to erase empty lines, suppress the final update,
1454 since _rl_update_final calls crlf(). */
1455 if (rl_erase_empty_line && rl_point == 0 && rl_end == 0)
1458 if (readline_echoing_p)
1459 _rl_update_final ();
1463 /* What to do for some uppercase characters, like meta characters,
1464 and some characters appearing in emacs_ctlx_keymap. This function
1465 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1466 is special cased. */
1468 rl_do_lowercase_version (ignore1, ignore2)
1469 int ignore1, ignore2;
1474 /* Rubout the character behind point. */
1476 rl_rubout (count, key)
1481 rl_delete (-count, key);
1491 if (count > 1 || rl_explicit_arg)
1493 int orig_point = rl_point;
1494 rl_backward (count, key);
1495 rl_kill_text (orig_point, rl_point);
1499 int c = the_line[--rl_point];
1500 rl_delete_text (rl_point, rl_point + 1);
1502 if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
1505 l = rl_character_len (c, rl_point);
1506 _rl_erase_at_end_of_line (l);
1512 /* Delete the character under the cursor. Given a numeric argument,
1513 kill that many characters instead. */
1515 rl_delete (count, key)
1519 return (rl_rubout (-count, key));
1521 if (rl_point == rl_end)
1527 if (count > 1 || rl_explicit_arg)
1529 int orig_point = rl_point;
1530 rl_forward (count, key);
1531 rl_kill_text (orig_point, rl_point);
1532 rl_point = orig_point;
1536 return (rl_delete_text (rl_point, rl_point + 1));
1539 /* Delete the character under the cursor, unless the insertion
1540 point is at the end of the line, in which case the character
1541 behind the cursor is deleted. COUNT is obeyed and may be used
1542 to delete forward or backward that many characters. */
1544 rl_rubout_or_delete (count, key)
1547 if (rl_end != 0 && rl_point == rl_end)
1548 return (rl_rubout (count, key));
1550 return (rl_delete (count, key));
1553 /* Delete all spaces and tabs around point. */
1555 rl_delete_horizontal_space (count, ignore)
1558 int start = rl_point;
1560 while (rl_point && whitespace (the_line[rl_point - 1]))
1565 while (rl_point < rl_end && whitespace (the_line[rl_point]))
1568 if (start != rl_point)
1570 rl_delete_text (start, rl_point);
1576 /* Like the tcsh editing function delete-char-or-list. The eof character
1577 is caught before this is invoked, so this really does the same thing as
1578 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1580 rl_delete_or_show_completions (count, key)
1583 if (rl_end != 0 && rl_point == rl_end)
1584 return (rl_possible_completions (count, key));
1586 return (rl_delete (count, key));
1589 #ifndef RL_COMMENT_BEGIN_DEFAULT
1590 #define RL_COMMENT_BEGIN_DEFAULT "#"
1593 /* Turn the current line into a comment in shell history.
1594 A K*rn shell style function. */
1596 rl_insert_comment (count, key)
1599 rl_beg_of_line (1, key);
1600 rl_insert_text (_rl_comment_begin ? _rl_comment_begin
1601 : RL_COMMENT_BEGIN_DEFAULT);
1602 (*rl_redisplay_function) ();
1603 rl_newline (1, '\n');
1607 /* **************************************************************** */
1611 /* **************************************************************** */
1613 /* The three kinds of things that we know how to do. */
1618 static int rl_change_case ();
1620 /* Uppercase the word at point. */
1622 rl_upcase_word (count, key)
1625 return (rl_change_case (count, UpCase));
1628 /* Lowercase the word at point. */
1630 rl_downcase_word (count, key)
1633 return (rl_change_case (count, DownCase));
1636 /* Upcase the first letter, downcase the rest. */
1638 rl_capitalize_word (count, key)
1641 return (rl_change_case (count, CapCase));
1644 /* The meaty function.
1645 Change the case of COUNT words, performing OP on them.
1646 OP is one of UpCase, DownCase, or CapCase.
1647 If a negative argument is given, leave point where it started,
1648 otherwise, leave it where it moves to. */
1650 rl_change_case (count, op)
1653 register int start, end;
1657 rl_forward_word (count, 0);
1663 /* We are going to modify some text, so let's prepare to undo it. */
1664 rl_modifying (start, end);
1666 for (inword = 0; start < end; start++)
1668 c = the_line[start];
1672 the_line[start] = _rl_to_upper (c);
1676 the_line[start] = _rl_to_lower (c);
1680 the_line[start] = (inword == 0) ? _rl_to_upper (c) : _rl_to_lower (c);
1681 inword = alphabetic (the_line[start]);
1693 /* **************************************************************** */
1697 /* **************************************************************** */
1699 /* Transpose the words at point. */
1701 rl_transpose_words (count, key)
1704 char *word1, *word2;
1705 int w1_beg, w1_end, w2_beg, w2_end;
1706 int orig_point = rl_point;
1711 /* Find the two words. */
1712 rl_forward_word (count, key);
1714 rl_backward_word (1, key);
1716 rl_backward_word (count, key);
1718 rl_forward_word (1, key);
1721 /* Do some check to make sure that there really are two words. */
1722 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
1725 rl_point = orig_point;
1729 /* Get the text of the words. */
1730 word1 = rl_copy_text (w1_beg, w1_end);
1731 word2 = rl_copy_text (w2_beg, w2_end);
1733 /* We are about to do many insertions and deletions. Remember them
1734 as one operation. */
1735 rl_begin_undo_group ();
1737 /* Do the stuff at word2 first, so that we don't have to worry
1738 about word1 moving. */
1740 rl_delete_text (w2_beg, w2_end);
1741 rl_insert_text (word1);
1744 rl_delete_text (w1_beg, w1_end);
1745 rl_insert_text (word2);
1747 /* This is exactly correct since the text before this point has not
1748 changed in length. */
1751 /* I think that does it. */
1752 rl_end_undo_group ();
1759 /* Transpose the characters at point. If point is at the end of the line,
1760 then transpose the characters before point. */
1762 rl_transpose_chars (count, key)
1770 if (!rl_point || rl_end < 2)
1776 rl_begin_undo_group ();
1778 if (rl_point == rl_end)
1785 dummy[0] = the_line[rl_point];
1788 rl_delete_text (rl_point, rl_point + 1);
1792 rl_insert_text (dummy);
1794 rl_end_undo_group ();
1798 /* **************************************************************** */
1800 /* Character Searching */
1802 /* **************************************************************** */
1805 _rl_char_search_internal (count, dir, schar)
1806 int count, dir, schar;
1811 inc = (dir < 0) ? -1 : 1;
1814 if ((dir < 0 && pos <= 0) || (dir > 0 && pos >= rl_end))
1823 if (rl_line_buffer[pos] == schar)
1827 rl_point = (dir == BTO) ? pos + 1 : pos;
1829 rl_point = (dir == FTO) ? pos - 1 : pos;
1833 while ((dir < 0) ? pos-- : ++pos < rl_end);
1838 /* Search COUNT times for a character read from the current input stream.
1839 FDIR is the direction to search if COUNT is non-negative; otherwise
1840 the search goes in BDIR. */
1842 _rl_char_search (count, fdir, bdir)
1843 int count, fdir, bdir;
1849 return (_rl_char_search_internal (-count, bdir, c));
1851 return (_rl_char_search_internal (count, fdir, c));
1855 rl_char_search (count, key)
1858 return (_rl_char_search (count, FFIND, BFIND));
1862 rl_backward_char_search (count, key)
1865 return (_rl_char_search (count, BFIND, FFIND));
1868 /* **************************************************************** */
1870 /* History Utilities */
1872 /* **************************************************************** */
1874 /* We already have a history library, and that is what we use to control
1875 the history features of readline. This is our local interface to
1876 the history mechanism. */
1878 /* While we are editing the history, this is the saved
1879 version of the original line. */
1880 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
1882 /* Set the history pointer back to the last entry in the history. */
1884 start_using_history ()
1887 if (saved_line_for_history)
1888 _rl_free_history_entry (saved_line_for_history);
1890 saved_line_for_history = (HIST_ENTRY *)NULL;
1893 /* Free the contents (and containing structure) of a HIST_ENTRY. */
1895 _rl_free_history_entry (entry)
1905 /* Perhaps put back the current line if it has changed. */
1907 maybe_replace_line ()
1911 temp = current_history ();
1912 /* If the current line has changed, save the changes. */
1913 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
1915 temp = replace_history_entry (where_history (), the_line, (histdata_t)rl_undo_list);
1922 /* Put back the saved_line_for_history if there is one. */
1924 maybe_unsave_line ()
1928 if (saved_line_for_history)
1930 line_len = strlen (saved_line_for_history->line);
1932 if (line_len >= rl_line_buffer_len)
1933 rl_extend_line_buffer (line_len);
1935 strcpy (the_line, saved_line_for_history->line);
1936 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
1937 _rl_free_history_entry (saved_line_for_history);
1938 saved_line_for_history = (HIST_ENTRY *)NULL;
1939 rl_end = rl_point = strlen (the_line);
1946 /* Save the current line in saved_line_for_history. */
1950 if (saved_line_for_history == 0)
1952 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
1953 saved_line_for_history->line = savestring (the_line);
1954 saved_line_for_history->data = (char *)rl_undo_list;
1959 /* **************************************************************** */
1961 /* History Commands */
1963 /* **************************************************************** */
1965 /* Meta-< goes to the start of the history. */
1967 rl_beginning_of_history (count, key)
1970 return (rl_get_previous_history (1 + where_history (), key));
1973 /* Meta-> goes to the end of the history. (The current line). */
1975 rl_end_of_history (count, key)
1978 maybe_replace_line ();
1980 maybe_unsave_line ();
1984 /* Move down to the next history line. */
1986 rl_get_next_history (count, key)
1993 return (rl_get_previous_history (-count, key));
1998 maybe_replace_line ();
2000 temp = (HIST_ENTRY *)NULL;
2003 temp = next_history ();
2010 maybe_unsave_line ();
2013 line_len = strlen (temp->line);
2015 if (line_len >= rl_line_buffer_len)
2016 rl_extend_line_buffer (line_len);
2018 strcpy (the_line, temp->line);
2019 rl_undo_list = (UNDO_LIST *)temp->data;
2020 rl_end = rl_point = strlen (the_line);
2021 #if defined (VI_MODE)
2022 if (rl_editing_mode == vi_mode)
2024 #endif /* VI_MODE */
2029 /* Get the previous item out of our interactive history, making it the current
2030 line. If there is no previous history, just ding. */
2032 rl_get_previous_history (count, key)
2035 HIST_ENTRY *old_temp, *temp;
2039 return (rl_get_next_history (-count, key));
2044 /* If we don't have a line saved, then save this one. */
2047 /* If the current line has changed, save the changes. */
2048 maybe_replace_line ();
2050 temp = old_temp = (HIST_ENTRY *)NULL;
2053 temp = previous_history ();
2061 /* If there was a large argument, and we moved back to the start of the
2062 history, that is not an error. So use the last value found. */
2063 if (!temp && old_temp)
2070 line_len = strlen (temp->line);
2072 if (line_len >= rl_line_buffer_len)
2073 rl_extend_line_buffer (line_len);
2075 strcpy (the_line, temp->line);
2076 rl_undo_list = (UNDO_LIST *)temp->data;
2077 rl_end = rl_point = line_len;
2079 #if defined (VI_MODE)
2080 if (rl_editing_mode == vi_mode)
2082 #endif /* VI_MODE */
2087 /* **************************************************************** */
2089 /* The Mark and the Region. */
2091 /* **************************************************************** */
2093 /* Set the mark at POSITION. */
2095 _rl_set_mark_at_pos (position)
2098 if (position > rl_end)
2105 /* A bindable command to set the mark. */
2107 rl_set_mark (count, key)
2110 return (_rl_set_mark_at_pos (rl_explicit_arg ? count : rl_point));
2113 /* Exchange the position of mark and point. */
2115 rl_exchange_point_and_mark (count, key)
2118 if (rl_mark > rl_end)
2127 SWAP (rl_point, rl_mark);
2132 /* **************************************************************** */
2136 /* **************************************************************** */
2137 /* How to toggle back and forth between editing modes. */
2139 rl_vi_editing_mode (count, key)
2142 #if defined (VI_MODE)
2143 rl_editing_mode = vi_mode;
2144 rl_vi_insertion_mode (1, key);
2145 #endif /* VI_MODE */
2150 rl_emacs_editing_mode (count, key)
2153 rl_editing_mode = emacs_mode;
2154 _rl_keymap = emacs_standard_keymap;