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. */
59 /* Some standard library routines. */
63 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
65 /* NOTE: Functions and variables prefixed with `_rl_' are
66 pseudo-global: they are global so they can be shared
67 between files in the readline library, but are not intended
68 to be visible to readline callers. */
70 /* Variables and functions imported from terminal.c */
71 extern int _rl_init_terminal_io ();
72 extern void _rl_enable_meta_key ();
73 extern int _rl_output_character_function ();
74 extern void _rl_get_screen_size ();
76 extern int _rl_enable_meta;
77 extern int _rl_term_autowrap;
78 extern char *term_backspace, *term_clreol, *term_clrpag;
79 extern int screenwidth, screenheight, screenchars;
81 /* Variables and functions imported from rltty.c. */
82 extern void rl_prep_terminal (), rl_deprep_terminal ();
83 extern void rltty_set_default_bindings ();
85 /* Functions imported from util.c. */
86 extern void _rl_abort_internal ();
87 extern void rl_extend_line_buffer ();
88 extern int alphabetic ();
90 /* Functions imported from bind.c. */
91 extern void _rl_bind_if_unbound ();
92 extern int rl_set_keymap_from_edit_mode ();
94 /* Functions imported from input.c. */
95 extern int _rl_any_typein ();
96 extern void _rl_insert_typein ();
97 extern int rl_read_key ();
99 /* Functions imported from nls.c */
100 extern int _rl_init_eightbit ();
102 /* External redisplay functions and variables from display.c */
103 extern void _rl_move_vert ();
104 extern void _rl_update_final ();
106 extern void _rl_erase_at_end_of_line ();
107 extern void _rl_move_cursor_relative ();
109 extern int _rl_vis_botlin;
110 extern int _rl_last_c_pos;
111 extern int _rl_horizontal_scroll_mode;
112 extern int rl_display_fixed;
113 extern int _rl_suppress_redisplay;
114 extern char *rl_display_prompt;
116 /* Variables imported from complete.c. */
117 extern char *rl_completer_word_break_characters;
118 extern char *rl_basic_word_break_characters;
119 extern int rl_completion_query_items;
120 extern int rl_complete_with_tilde_expansion;
122 /* Variables and functions from macro.c. */
123 extern void _rl_add_macro_char ();
124 extern void _rl_with_macro_input ();
125 extern int _rl_next_macro_key ();
126 extern int _rl_defining_kbd_macro;
128 #if defined (VI_MODE)
129 /* Functions imported from vi_mode.c. */
130 extern void _rl_vi_set_last ();
131 extern void _rl_vi_reset_last ();
132 extern void _rl_vi_done_inserting ();
133 extern int _rl_vi_textmod_command ();
134 extern void _rl_vi_initialize_line ();
137 extern UNDO_LIST *rl_undo_list;
138 extern int _rl_doing_an_undo;
140 /* Forward declarations used in this file. */
141 void _rl_free_history_entry ();
144 int _rl_init_argument ();
146 static char *readline_internal ();
147 static void readline_initialize_everything ();
148 static void start_using_history ();
149 static void bind_arrow_keys ();
151 #if !defined (__GO32__)
152 static void readline_default_bindings ();
153 #endif /* !__GO32__ */
155 #if defined (__GO32__)
157 # undef HANDLE_SIGNALS
158 #endif /* __GO32__ */
160 extern char *xmalloc (), *xrealloc ();
162 /* **************************************************************** */
164 /* Line editing input utility */
166 /* **************************************************************** */
168 char *rl_library_version = "2.1";
170 /* A pointer to the keymap that is currently in use.
171 By default, it is the standard emacs keymap. */
172 Keymap _rl_keymap = emacs_standard_keymap;
174 /* The current style of editing. */
175 int rl_editing_mode = emacs_mode;
177 /* Non-zero if the previous command was a kill command. */
178 int _rl_last_command_was_kill = 0;
180 /* The current value of the numeric argument specified by the user. */
181 int rl_numeric_arg = 1;
183 /* Non-zero if an argument was typed. */
184 int rl_explicit_arg = 0;
186 /* Temporary value used while generating the argument. */
189 /* Non-zero means we have been called at least once before. */
190 static int rl_initialized;
192 /* If non-zero, this program is running in an EMACS buffer. */
193 static int running_in_emacs;
195 /* The current offset in the current input line. */
198 /* Mark in the current input line. */
201 /* Length of the current input line. */
204 /* Make this non-zero to return the current input_line. */
207 /* The last function executed by readline. */
208 Function *rl_last_func = (Function *)NULL;
210 /* Top level environment for readline_internal (). */
211 jmp_buf readline_top_level;
213 /* The streams we interact with. */
214 FILE *_rl_in_stream, *_rl_out_stream;
216 /* The names of the streams that we do input and output to. */
217 FILE *rl_instream = (FILE *)NULL;
218 FILE *rl_outstream = (FILE *)NULL;
220 /* Non-zero means echo characters as they are read. */
221 int readline_echoing_p = 1;
223 /* Current prompt. */
225 int rl_visible_prompt_length = 0;
227 /* The number of characters read in order to type this complete command. */
228 int rl_key_sequence_length = 0;
230 /* If non-zero, then this is the address of a function to call just
231 before readline_internal () prints the first prompt. */
232 Function *rl_startup_hook = (Function *)NULL;
234 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
235 static char *the_line;
237 /* The character that can generate an EOF. Really read from
238 the terminal driver... just defaulted here. */
239 int _rl_eof_char = CTRL ('D');
241 /* Non-zero makes this the next keystroke to read. */
242 int rl_pending_input = 0;
244 /* Pointer to a useful terminal name. */
245 char *rl_terminal_name = (char *)NULL;
247 /* Non-zero means to always use horizontal scrolling in line display. */
248 int _rl_horizontal_scroll_mode = 0;
250 /* Non-zero means to display an asterisk at the starts of history lines
251 which have been modified. */
252 int _rl_mark_modified_lines = 0;
254 /* The style of `bell' notification preferred. This can be set to NO_BELL,
255 AUDIBLE_BELL, or VISIBLE_BELL. */
256 int _rl_bell_preference = AUDIBLE_BELL;
258 /* String inserted into the line by rl_insert_comment (). */
259 char *_rl_comment_begin;
261 /* Keymap holding the function currently being executed. */
262 Keymap rl_executing_keymap;
264 /* Line buffer and maintenence. */
265 char *rl_line_buffer = (char *)NULL;
266 int rl_line_buffer_len = 0;
268 /* Forward declarations used by the display and termcap code. */
270 /* **************************************************************** */
272 /* `Forward' declarations */
274 /* **************************************************************** */
276 /* Non-zero means do not parse any lines other than comments and
277 parser directives. */
278 unsigned char _rl_parsing_conditionalized_out = 0;
280 /* Non-zero means to convert characters with the meta bit set to
281 escape-prefixed characters so we can indirect through
282 emacs_meta_keymap or vi_escape_keymap. */
283 int _rl_convert_meta_chars_to_ascii = 1;
285 /* Non-zero means to output characters with the meta bit set directly
286 rather than as a meta-prefixed escape sequence. */
287 int _rl_output_meta_chars = 0;
289 /* **************************************************************** */
291 /* Top Level Functions */
293 /* **************************************************************** */
295 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
296 int _rl_meta_flag = 0; /* Forward declaration */
298 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
299 none. A return value of NULL means that EOF was encountered. */
308 /* If we are at EOF return a NULL string. */
309 if (rl_pending_input == EOF)
311 rl_pending_input = 0;
312 return ((char *)NULL);
315 rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
318 (*rl_prep_term_function) (_rl_meta_flag);
320 #if defined (HANDLE_SIGNALS)
324 value = readline_internal ();
325 (*rl_deprep_term_function) ();
327 #if defined (HANDLE_SIGNALS)
334 #if defined (READLINE_CALLBACKS)
335 # define STATIC_CALLBACK
337 # define STATIC_CALLBACK static
341 readline_internal_setup ()
343 _rl_in_stream = rl_instream;
344 _rl_out_stream = rl_outstream;
347 (*rl_startup_hook) ();
349 if (readline_echoing_p == 0)
353 fprintf (_rl_out_stream, "%s", rl_prompt);
354 fflush (_rl_out_stream);
360 (*rl_redisplay_function) ();
361 #if defined (VI_MODE)
362 if (rl_editing_mode == vi_mode)
363 rl_vi_insertion_mode ();
368 STATIC_CALLBACK char *
369 readline_internal_teardown (eof)
375 /* Restore the original of this history line, iff the line that we
376 are editing was originally in the history, AND the line has changed. */
377 entry = current_history ();
379 if (entry && rl_undo_list)
381 temp = savestring (the_line);
382 rl_revert_line (1, 0);
383 entry = replace_history_entry (where_history (), the_line, (HIST_ENTRY *)NULL);
384 _rl_free_history_entry (entry);
386 strcpy (the_line, temp);
390 /* At any rate, it is highly likely that this line has an undo list. Get
395 return (eof ? (char *)NULL : savestring (the_line));
399 #if defined (READLINE_CALLBACKS)
400 readline_internal_char ()
402 readline_internal_charloop ()
405 static int lastc, eof_found;
411 #if !defined (READLINE_CALLBACKS)
415 lk = _rl_last_command_was_kill;
417 code = setjmp (readline_top_level);
420 (*rl_redisplay_function) ();
422 if (rl_pending_input == 0)
424 /* Then initialize the argument and number of keys read. */
425 _rl_init_argument ();
426 rl_key_sequence_length = 0;
431 /* EOF typed to a non-blank line is a <NL>. */
432 if (c == EOF && rl_end)
435 /* The character _rl_eof_char typed to blank line, and not as the
436 previous character is interpreted as EOF. */
437 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
439 #if defined (READLINE_CALLBACKS)
440 return (rl_done = 1);
448 _rl_dispatch (c, _rl_keymap);
450 /* If there was no change in _rl_last_command_was_kill, then no kill
451 has taken place. Note that if input is pending we are reading
452 a prefix command, so nothing has changed yet. */
453 if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
454 _rl_last_command_was_kill = 0;
456 #if defined (VI_MODE)
457 /* In vi mode, when you exit insert mode, the cursor moves back
458 over the previous character. We explicitly check for that here. */
459 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
464 (*rl_redisplay_function) ();
466 #if defined (READLINE_CALLBACKS)
475 #if defined (READLINE_CALLBACKS)
477 readline_internal_charloop ()
482 eof = readline_internal_char ();
485 #endif /* READLINE_CALLBACKS */
487 /* Read a line of input from the global rl_instream, doing output on
488 the global rl_outstream.
489 If rl_prompt is non-null, then that is our prompt. */
495 readline_internal_setup ();
496 eof = readline_internal_charloop ();
497 return (readline_internal_teardown (eof));
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)
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;
542 if (func != (Function *)NULL)
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 ();
554 r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
556 /* If we have input pending, then the last command was a prefix
557 command. Don't change the state of rl_last_func. Otherwise,
558 remember the last command executed in this variable. */
559 if (!rl_pending_input)
560 rl_last_func = map[key].function;
564 _rl_abort_internal ();
570 if (map[key].function != (Function *)NULL)
572 rl_key_sequence_length++;
573 newkey = rl_read_key ();
574 r = _rl_dispatch (newkey, FUNCTION_TO_KEYMAP (map, key));
578 _rl_abort_internal ();
584 if (map[key].function != (Function *)NULL)
586 macro = savestring ((char *)map[key].function);
587 _rl_with_macro_input (macro);
592 #if defined (VI_MODE)
593 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
594 _rl_vi_textmod_command (key))
595 _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
600 /* **************************************************************** */
602 /* Initializations */
604 /* **************************************************************** */
606 /* Initliaze readline (and terminal if not already). */
610 /* If we have never been called before, initialize the
611 terminal and data structures. */
614 readline_initialize_everything ();
618 /* Initalize the current line information. */
619 rl_point = rl_end = 0;
620 the_line = rl_line_buffer;
623 /* We aren't done yet. We haven't even gotten started yet! */
626 /* Tell the history routines what is going on. */
627 start_using_history ();
629 /* Make the display buffer match the state of the line. */
630 rl_reset_line_state ();
632 /* No such function typed yet. */
633 rl_last_func = (Function *)NULL;
635 /* Parsing of key-bindings begins in an enabled state. */
636 _rl_parsing_conditionalized_out = 0;
638 #if defined (VI_MODE)
639 if (rl_editing_mode == vi_mode)
640 _rl_vi_initialize_line ();
646 /* Initialize the entire state of the world. */
648 readline_initialize_everything ()
650 /* Find out if we are running in Emacs. */
651 running_in_emacs = getenv ("EMACS") != (char *)0;
653 /* Set up input and output if they are not already set up. */
658 rl_outstream = stdout;
660 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
661 may change, but they may also be used before readline_internal ()
663 _rl_in_stream = rl_instream;
664 _rl_out_stream = rl_outstream;
666 /* Allocate data structures. */
668 rl_line_buffer = xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
670 /* Initialize the terminal interface. */
671 _rl_init_terminal_io ((char *)NULL);
673 #if !defined (__GO32__)
674 /* Bind tty characters to readline functions. */
675 readline_default_bindings ();
676 #endif /* !__GO32__ */
678 /* Initialize the function names. */
679 rl_initialize_funmap ();
681 /* Decide whether we should automatically go into eight-bit mode. */
682 _rl_init_eightbit ();
684 /* Read in the init file. */
685 rl_read_init_file ((char *)NULL);
688 if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
691 screenchars -= screenheight;
694 /* Override the effect of any `set keymap' assignments in the
696 rl_set_keymap_from_edit_mode ();
698 /* Try to bind a common arrow key prefix, if not already bound. */
701 /* Enable the meta key, if this terminal has one. */
703 _rl_enable_meta_key ();
705 /* If the completion parser's default word break characters haven't
706 been set yet, then do so now. */
707 if (rl_completer_word_break_characters == (char *)NULL)
708 rl_completer_word_break_characters = rl_basic_word_break_characters;
711 /* If this system allows us to look at the values of the regular
712 input editing characters, then bind them to their readline
713 equivalents, iff the characters are not bound to keymaps. */
715 readline_default_bindings ()
717 rltty_set_default_bindings (_rl_keymap);
721 bind_arrow_keys_internal ()
725 f = rl_function_of_keyseq ("\033[A", _rl_keymap, (int *)NULL);
726 if (!f || f == rl_do_lowercase_version)
728 _rl_bind_if_unbound ("\033[A", rl_get_previous_history);
729 _rl_bind_if_unbound ("\033[B", rl_get_next_history);
730 _rl_bind_if_unbound ("\033[C", rl_forward);
731 _rl_bind_if_unbound ("\033[D", rl_backward);
734 f = rl_function_of_keyseq ("\033OA", _rl_keymap, (int *)NULL);
735 if (!f || f == rl_do_lowercase_version)
737 _rl_bind_if_unbound ("\033OA", rl_get_previous_history);
738 _rl_bind_if_unbound ("\033OB", rl_get_next_history);
739 _rl_bind_if_unbound ("\033OC", rl_forward);
740 _rl_bind_if_unbound ("\033OD", rl_backward);
744 /* Try and bind the common arrow key prefix after giving termcap and
745 the inputrc file a chance to bind them and create `real' keymaps
746 for the arrow key prefix. */
752 xkeymap = _rl_keymap;
754 _rl_keymap = emacs_standard_keymap;
755 bind_arrow_keys_internal ();
757 #if defined (VI_MODE)
758 _rl_keymap = vi_movement_keymap;
759 bind_arrow_keys_internal ();
762 _rl_keymap = xkeymap;
766 /* **************************************************************** */
768 /* Numeric Arguments */
770 /* **************************************************************** */
772 /* Handle C-u style numeric args, as well as M--, and M-digits. */
776 int key, c, sawminus;
783 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
784 key = c = rl_read_key ();
786 if (_rl_keymap[c].type == ISFUNC &&
787 _rl_keymap[c].function == rl_universal_argument)
795 rl_numeric_arg = rl_explicit_arg ? (rl_numeric_arg * 10) + c - '0' : c - '0';
800 if (c == '-' && rl_explicit_arg == 0)
802 rl_numeric_arg = sawminus = 1;
807 /* Make M-- command equivalent to M--1 command. */
808 if (sawminus && rl_numeric_arg == 1 && rl_explicit_arg == 0)
810 _rl_restore_prompt ();
812 return (_rl_dispatch (key, _rl_keymap));
820 /* Add the current digit to the argument in progress. */
822 rl_digit_argument (ignore, key)
825 rl_pending_input = key;
826 return (rl_digit_loop ());
829 /* What to do when you abort reading an argument. */
831 rl_discard_argument ()
835 _rl_init_argument ();
839 /* Create a default argument. */
843 rl_numeric_arg = rl_arg_sign = 1;
848 /* C-u, universal argument. Multiply the current argument by 4.
849 Read a key. If the key has nothing to do with arguments, then
850 dispatch on it. If the key is the abort character then abort. */
852 rl_universal_argument (count, key)
856 return (rl_digit_loop ());
859 /* **************************************************************** */
861 /* Insert and Delete */
863 /* **************************************************************** */
865 /* Insert a string of text into the line at point. This is the only
866 way that you should do insertion. rl_insert () calls this
869 rl_insert_text (string)
872 register int i, l = strlen (string);
874 if (rl_end + l >= rl_line_buffer_len)
875 rl_extend_line_buffer (rl_end + l);
877 for (i = rl_end; i >= rl_point; i--)
878 the_line[i + l] = the_line[i];
879 strncpy (the_line + rl_point, string, l);
881 /* Remember how to undo this if we aren't undoing something. */
882 if (!_rl_doing_an_undo)
884 /* If possible and desirable, concatenate the undos. */
887 (rl_undo_list->what == UNDO_INSERT) &&
888 (rl_undo_list->end == rl_point) &&
889 (rl_undo_list->end - rl_undo_list->start < 20))
892 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
896 the_line[rl_end] = '\0';
900 /* Delete the string between FROM and TO. FROM is
901 inclusive, TO is not. */
903 rl_delete_text (from, to)
907 register int diff, i;
909 /* Fix it if the caller is confused. */
921 text = rl_copy_text (from, to);
923 /* Some versions of strncpy() can't handle overlapping arguments. */
925 for (i = from; i < rl_end - diff; i++)
926 the_line[i] = the_line[i + diff];
928 /* Remember how to undo this delete. */
929 if (_rl_doing_an_undo == 0)
930 rl_add_undo (UNDO_DELETE, from, to, text);
935 the_line[rl_end] = '\0';
939 /* **************************************************************** */
941 /* Readline character functions */
943 /* **************************************************************** */
945 /* This is not a gap editor, just a stupid line input routine. No hair
946 is involved in writing any of the functions, and none should be. */
950 rl_end is the place in the string that we would place '\0';
951 i.e., it is always safe to place '\0' there.
953 rl_point is the place in the string where the cursor is. Sometimes
954 this is the same as rl_end.
956 Any command that is called interactively receives two arguments.
957 The first is a count: the numeric arg pased to this command.
958 The second is the key which invoked this command.
961 /* **************************************************************** */
963 /* Movement Commands */
965 /* **************************************************************** */
967 /* Note that if you `optimize' the display for these functions, you cannot
968 use said functions in other functions which do not do optimizing display.
969 I.e., you will have to update the data base for rl_redisplay, and you
970 might as well let rl_redisplay do that job. */
972 /* Move forward COUNT characters. */
974 rl_forward (count, key)
978 rl_backward (-count, key);
981 int end = rl_point + count;
982 #if defined (VI_MODE)
983 int lend = rl_end - (rl_editing_mode == vi_mode);
999 /* Move backward COUNT characters. */
1001 rl_backward (count, key)
1005 rl_forward (-count, key);
1008 if (rl_point < count)
1019 /* Move to the beginning of the line. */
1021 rl_beg_of_line (count, key)
1028 /* Move to the end of the line. */
1030 rl_end_of_line (count, key)
1037 /* Move forward a word. We do what Emacs does. */
1039 rl_forward_word (count, key)
1046 rl_backward_word (-count, key);
1052 if (rl_point == rl_end)
1055 /* If we are not in a word, move forward until we are in one.
1056 Then, move forward until we hit a non-alphabetic character. */
1057 c = the_line[rl_point];
1058 if (alphabetic (c) == 0)
1060 while (++rl_point < rl_end)
1062 c = the_line[rl_point];
1067 if (rl_point == rl_end)
1069 while (++rl_point < rl_end)
1071 c = the_line[rl_point];
1072 if (alphabetic (c) == 0)
1080 /* Move backward a word. We do what Emacs does. */
1082 rl_backward_word (count, key)
1089 rl_forward_word (-count, key);
1098 /* Like rl_forward_word (), except that we look at the characters
1099 just before point. */
1101 c = the_line[rl_point - 1];
1102 if (alphabetic (c) == 0)
1106 c = the_line[rl_point - 1];
1114 c = the_line[rl_point - 1];
1115 if (alphabetic (c) == 0)
1125 /* Clear the current line. Numeric argument to C-l does this. */
1129 int curr_line, nleft;
1131 /* Find out whether or not there might be invisible characters in the
1133 if (rl_display_prompt == rl_prompt)
1134 nleft = _rl_last_c_pos - screenwidth - rl_visible_prompt_length;
1136 nleft = _rl_last_c_pos - screenwidth;
1139 curr_line = 1 + nleft / screenwidth;
1143 _rl_move_vert (curr_line);
1144 _rl_move_cursor_relative (0, the_line); /* XXX is this right */
1146 #if defined (__GO32__)
1148 int row, col, width, row_start;
1150 ScreenGetCursor (&row, &col);
1151 width = ScreenCols ();
1152 row_start = ScreenPrimary + (row * width);
1153 memset (row_start + col, 0, (width - col) * 2);
1155 #else /* !__GO32__ */
1157 tputs (term_clreol, 1, _rl_output_character_function);
1158 #endif /* !__GO32__ */
1160 rl_forced_update_display ();
1161 rl_display_fixed = 1;
1166 /* C-l typed to a line without quoting clears the screen, and then reprints
1167 the prompt and the current input line. Given a numeric arg, redraw only
1168 the current line. */
1170 rl_clear_screen (count, key)
1173 if (rl_explicit_arg)
1179 #if !defined (__GO32__)
1181 tputs (term_clrpag, 1, _rl_output_character_function);
1183 #endif /* !__GO32__ */
1186 rl_forced_update_display ();
1187 rl_display_fixed = 1;
1193 rl_arrow_keys (count, c)
1198 ch = rl_read_key ();
1200 switch (_rl_to_upper (ch))
1203 rl_get_previous_history (count, ch);
1207 rl_get_next_history (count, ch);
1211 rl_forward (count, ch);
1215 rl_backward (count, ch);
1225 /* **************************************************************** */
1229 /* **************************************************************** */
1231 /* Insert the character C at the current location, moving point forward. */
1233 rl_insert (count, c)
1242 /* If we can optimize, then do it. But don't let people crash
1243 readline because of extra large arguments. */
1244 if (count > 1 && count <= 1024)
1246 string = xmalloc (1 + count);
1248 for (i = 0; i < count; i++)
1252 rl_insert_text (string);
1263 for (i = 0; i < 1024; i++)
1268 decreaser = (count > 1024 ? 1024 : count);
1269 str[decreaser] = '\0';
1270 rl_insert_text (str);
1277 /* We are inserting a single character.
1278 If there is pending input, then make a string of all of the
1279 pending characters that are bound to rl_insert, and insert
1281 if (_rl_any_typein ())
1282 _rl_insert_typein (c);
1285 /* Inserting a single character. */
1290 rl_insert_text (str);
1295 /* Insert the next typed character verbatim. */
1297 rl_quoted_insert (count, key)
1303 return (rl_insert (count, c));
1306 /* Insert a tab character. */
1308 rl_tab_insert (count, key)
1311 return (rl_insert (count, '\t'));
1314 /* What to do when a NEWLINE is pressed. We accept the whole line.
1315 KEY is the key that invoked this command. I guess it could have
1316 meaning in the future. */
1318 rl_newline (count, key)
1323 #if defined (VI_MODE)
1324 _rl_vi_done_inserting ();
1325 _rl_vi_reset_last ();
1326 #endif /* VI_MODE */
1328 if (readline_echoing_p)
1329 _rl_update_final ();
1333 /* What to do for some uppercase characters, like meta characters,
1334 and some characters appearing in emacs_ctlx_keymap. This function
1335 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1336 is special cased. */
1338 rl_do_lowercase_version (ignore1, ignore2)
1339 int ignore1, ignore2;
1344 /* Rubout the character behind point. */
1346 rl_rubout (count, key)
1351 rl_delete (-count, key);
1361 if (count > 1 || rl_explicit_arg)
1363 int orig_point = rl_point;
1364 rl_backward (count, key);
1365 rl_kill_text (orig_point, rl_point);
1369 int c = the_line[--rl_point];
1370 rl_delete_text (rl_point, rl_point + 1);
1372 if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
1375 l = rl_character_len (c, rl_point);
1376 _rl_erase_at_end_of_line (l);
1382 /* Delete the character under the cursor. Given a numeric argument,
1383 kill that many characters instead. */
1385 rl_delete (count, key)
1389 return (rl_rubout (-count, key));
1391 if (rl_point == rl_end)
1397 if (count > 1 || rl_explicit_arg)
1399 int orig_point = rl_point;
1400 rl_forward (count, key);
1401 rl_kill_text (orig_point, rl_point);
1402 rl_point = orig_point;
1406 return (rl_delete_text (rl_point, rl_point + 1));
1410 /* Delete all spaces and tabs around point. */
1412 rl_delete_horizontal_space (count, ignore)
1415 int start = rl_point;
1417 while (rl_point && whitespace (the_line[rl_point - 1]))
1422 while (rl_point < rl_end && whitespace (the_line[rl_point]))
1425 if (start != rl_point)
1427 rl_delete_text (start, rl_point);
1433 #ifndef RL_COMMENT_BEGIN_DEFAULT
1434 #define RL_COMMENT_BEGIN_DEFAULT "#"
1437 /* Turn the current line into a comment in shell history.
1438 A K*rn shell style function. */
1440 rl_insert_comment (count, key)
1443 rl_beg_of_line (1, key);
1444 rl_insert_text (_rl_comment_begin ? _rl_comment_begin
1445 : RL_COMMENT_BEGIN_DEFAULT);
1446 (*rl_redisplay_function) ();
1447 rl_newline (1, '\n');
1451 /* **************************************************************** */
1455 /* **************************************************************** */
1457 /* The three kinds of things that we know how to do. */
1462 static int rl_change_case ();
1464 /* Uppercase the word at point. */
1466 rl_upcase_word (count, key)
1469 return (rl_change_case (count, UpCase));
1472 /* Lowercase the word at point. */
1474 rl_downcase_word (count, key)
1477 return (rl_change_case (count, DownCase));
1480 /* Upcase the first letter, downcase the rest. */
1482 rl_capitalize_word (count, key)
1485 return (rl_change_case (count, CapCase));
1488 /* The meaty function.
1489 Change the case of COUNT words, performing OP on them.
1490 OP is one of UpCase, DownCase, or CapCase.
1491 If a negative argument is given, leave point where it started,
1492 otherwise, leave it where it moves to. */
1494 rl_change_case (count, op)
1497 register int start, end;
1501 rl_forward_word (count, 0);
1507 /* We are going to modify some text, so let's prepare to undo it. */
1508 rl_modifying (start, end);
1510 for (inword = 0; start < end; start++)
1512 c = the_line[start];
1516 the_line[start] = _rl_to_upper (c);
1520 the_line[start] = _rl_to_lower (c);
1524 the_line[start] = (inword == 0) ? _rl_to_upper (c) : _rl_to_lower (c);
1525 inword = alphabetic (the_line[start]);
1537 /* **************************************************************** */
1541 /* **************************************************************** */
1543 /* Transpose the words at point. */
1545 rl_transpose_words (count, key)
1548 char *word1, *word2;
1549 int w1_beg, w1_end, w2_beg, w2_end;
1550 int orig_point = rl_point;
1555 /* Find the two words. */
1556 rl_forward_word (count, key);
1558 rl_backward_word (1, key);
1560 rl_backward_word (count, key);
1562 rl_forward_word (1, key);
1565 /* Do some check to make sure that there really are two words. */
1566 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
1569 rl_point = orig_point;
1573 /* Get the text of the words. */
1574 word1 = rl_copy_text (w1_beg, w1_end);
1575 word2 = rl_copy_text (w2_beg, w2_end);
1577 /* We are about to do many insertions and deletions. Remember them
1578 as one operation. */
1579 rl_begin_undo_group ();
1581 /* Do the stuff at word2 first, so that we don't have to worry
1582 about word1 moving. */
1584 rl_delete_text (w2_beg, w2_end);
1585 rl_insert_text (word1);
1588 rl_delete_text (w1_beg, w1_end);
1589 rl_insert_text (word2);
1591 /* This is exactly correct since the text before this point has not
1592 changed in length. */
1595 /* I think that does it. */
1596 rl_end_undo_group ();
1603 /* Transpose the characters at point. If point is at the end of the line,
1604 then transpose the characters before point. */
1606 rl_transpose_chars (count, key)
1614 if (!rl_point || rl_end < 2)
1620 rl_begin_undo_group ();
1622 if (rl_point == rl_end)
1629 dummy[0] = the_line[rl_point];
1632 rl_delete_text (rl_point, rl_point + 1);
1635 if (rl_point > rl_end)
1637 else if (rl_point < 0)
1639 rl_insert_text (dummy);
1641 rl_end_undo_group ();
1645 /* **************************************************************** */
1647 /* Character Searching */
1649 /* **************************************************************** */
1652 _rl_char_search_internal (count, dir, schar)
1653 int count, dir, schar;
1658 inc = (dir < 0) ? -1 : 1;
1661 if ((dir < 0 && pos <= 0) || (dir > 0 && pos >= rl_end))
1670 if (rl_line_buffer[pos] == schar)
1674 rl_point = (dir == BTO) ? pos + 1 : pos;
1676 rl_point = (dir == FTO) ? pos - 1 : pos;
1680 while ((dir < 0) ? pos-- : ++pos < rl_end);
1685 /* Search COUNT times for a character read from the current input stream.
1686 FDIR is the direction to search if COUNT is non-negative; otherwise
1687 the search goes in BDIR. */
1689 _rl_char_search (count, fdir, bdir)
1690 int count, fdir, bdir;
1696 return (_rl_char_search_internal (-count, bdir, c));
1698 return (_rl_char_search_internal (count, fdir, c));
1702 rl_char_search (count, key)
1705 return (_rl_char_search (count, FFIND, BFIND));
1709 rl_backward_char_search (count, key)
1712 return (_rl_char_search (count, BFIND, FFIND));
1715 /* **************************************************************** */
1717 /* History Utilities */
1719 /* **************************************************************** */
1721 /* We already have a history library, and that is what we use to control
1722 the history features of readline. This is our local interface to
1723 the history mechanism. */
1725 /* While we are editing the history, this is the saved
1726 version of the original line. */
1727 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
1729 /* Set the history pointer back to the last entry in the history. */
1731 start_using_history ()
1734 if (saved_line_for_history)
1735 _rl_free_history_entry (saved_line_for_history);
1737 saved_line_for_history = (HIST_ENTRY *)NULL;
1740 /* Free the contents (and containing structure) of a HIST_ENTRY. */
1742 _rl_free_history_entry (entry)
1752 /* Perhaps put back the current line if it has changed. */
1754 maybe_replace_line ()
1758 temp = current_history ();
1759 /* If the current line has changed, save the changes. */
1760 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
1762 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
1769 /* Put back the saved_line_for_history if there is one. */
1771 maybe_unsave_line ()
1775 if (saved_line_for_history)
1777 line_len = strlen (saved_line_for_history->line);
1779 if (line_len >= rl_line_buffer_len)
1780 rl_extend_line_buffer (line_len);
1782 strcpy (the_line, saved_line_for_history->line);
1783 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
1784 _rl_free_history_entry (saved_line_for_history);
1785 saved_line_for_history = (HIST_ENTRY *)NULL;
1786 rl_end = rl_point = strlen (the_line);
1793 /* Save the current line in saved_line_for_history. */
1797 if (saved_line_for_history == 0)
1799 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
1800 saved_line_for_history->line = savestring (the_line);
1801 saved_line_for_history->data = (char *)rl_undo_list;
1806 /* **************************************************************** */
1808 /* History Commands */
1810 /* **************************************************************** */
1812 /* Meta-< goes to the start of the history. */
1814 rl_beginning_of_history (count, key)
1817 return (rl_get_previous_history (1 + where_history (), key));
1820 /* Meta-> goes to the end of the history. (The current line). */
1822 rl_end_of_history (count, key)
1825 maybe_replace_line ();
1827 maybe_unsave_line ();
1831 /* Move down to the next history line. */
1833 rl_get_next_history (count, key)
1840 return (rl_get_previous_history (-count, key));
1845 maybe_replace_line ();
1847 temp = (HIST_ENTRY *)NULL;
1850 temp = next_history ();
1857 maybe_unsave_line ();
1860 line_len = strlen (temp->line);
1862 if (line_len >= rl_line_buffer_len)
1863 rl_extend_line_buffer (line_len);
1865 strcpy (the_line, temp->line);
1866 rl_undo_list = (UNDO_LIST *)temp->data;
1867 rl_end = rl_point = strlen (the_line);
1868 #if defined (VI_MODE)
1869 if (rl_editing_mode == vi_mode)
1871 #endif /* VI_MODE */
1876 /* Get the previous item out of our interactive history, making it the current
1877 line. If there is no previous history, just ding. */
1879 rl_get_previous_history (count, key)
1882 HIST_ENTRY *old_temp, *temp;
1886 return (rl_get_next_history (-count, key));
1891 /* If we don't have a line saved, then save this one. */
1894 /* If the current line has changed, save the changes. */
1895 maybe_replace_line ();
1897 temp = old_temp = (HIST_ENTRY *)NULL;
1900 temp = previous_history ();
1908 /* If there was a large argument, and we moved back to the start of the
1909 history, that is not an error. So use the last value found. */
1910 if (!temp && old_temp)
1917 line_len = strlen (temp->line);
1919 if (line_len >= rl_line_buffer_len)
1920 rl_extend_line_buffer (line_len);
1922 strcpy (the_line, temp->line);
1923 rl_undo_list = (UNDO_LIST *)temp->data;
1924 rl_end = rl_point = line_len;
1926 #if defined (VI_MODE)
1927 if (rl_editing_mode == vi_mode)
1929 #endif /* VI_MODE */
1934 /* **************************************************************** */
1936 /* The Mark and the Region. */
1938 /* **************************************************************** */
1940 /* Set the mark at POSITION. */
1942 _rl_set_mark_at_pos (position)
1945 if (position > rl_end)
1952 /* A bindable command to set the mark. */
1954 rl_set_mark (count, key)
1957 return (_rl_set_mark_at_pos (rl_explicit_arg ? count : rl_point));
1960 /* Exchange the position of mark and point. */
1962 rl_exchange_point_and_mark (count, key)
1965 if (rl_mark > rl_end)
1974 SWAP (rl_point, rl_mark);
1979 /* **************************************************************** */
1983 /* **************************************************************** */
1984 /* How to toggle back and forth between editing modes. */
1986 rl_vi_editing_mode (count, key)
1989 #if defined (VI_MODE)
1990 rl_editing_mode = vi_mode;
1991 rl_vi_insertion_mode ();
1992 #endif /* VI_MODE */
1997 rl_emacs_editing_mode (count, key)
2000 rl_editing_mode = emacs_mode;
2001 _rl_keymap = emacs_standard_keymap;