1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright 1987, 1989, 1991, 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. */
32 /* This is needed to include support for TIOCGWINSZ and window resizing. */
33 #if defined (OSF1) || defined (BSD386) || defined (_386BSD) || defined (AIX)
34 # include <sys/ioctl.h>
37 #if defined (HAVE_UNISTD_H)
42 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
47 extern char * getenv ();
52 /* System-specific feature definitions and include files. */
55 /* Some standard library routines. */
59 /* NOTE: Functions and variables prefixed with `_rl_' are
60 pseudo-global: they are global so they can be shared
61 between files in the readline library, but are not intended
62 to be visible to readline callers. */
64 /* Functions imported from other files in the library. */
65 extern char *tgetstr ();
66 extern void rl_prep_terminal (), rl_deprep_terminal ();
67 extern Function *rl_function_of_keyseq ();
68 extern char *tilde_expand ();
70 /* External redisplay functions and variables from display.c */
71 extern void rl_redisplay ();
72 extern void _rl_move_vert ();
74 extern void _rl_erase_at_end_of_line ();
75 extern void _rl_move_cursor_relative ();
77 extern int _rl_vis_botlin;
78 extern int _rl_last_c_pos;
79 extern int rl_display_fixed;
81 /* Variables imported from complete.c. */
82 extern char *rl_completer_word_break_characters;
83 extern char *rl_basic_word_break_characters;
84 extern Function *rl_symbolic_link_hook;
85 extern int rl_completion_query_items;
86 extern int rl_complete_with_tilde_expansion;
88 /* Forward declarations used in this file. */
90 void free_history_entry ();
91 int _rl_output_character_function ();
92 void _rl_set_screen_size ();
95 static void readline_default_bindings ();
100 # undef HANDLE_SIGNALS
104 /* **************************************************************** */
106 /* Line editing input utility */
108 /* **************************************************************** */
110 static char *LibraryVersion = "2.0 (Cygnus)";
112 /* A pointer to the keymap that is currently in use.
113 By default, it is the standard emacs keymap. */
114 Keymap _rl_keymap = emacs_standard_keymap;
116 /* The current style of editing. */
117 int rl_editing_mode = emacs_mode;
119 /* Non-zero if the previous command was a kill command. */
120 static int last_command_was_kill = 0;
122 /* The current value of the numeric argument specified by the user. */
123 int rl_numeric_arg = 1;
125 /* Non-zero if an argument was typed. */
126 int rl_explicit_arg = 0;
128 /* Temporary value used while generating the argument. */
131 /* Non-zero means we have been called at least once before. */
132 static int rl_initialized = 0;
134 /* If non-zero, this program is running in an EMACS buffer. */
135 static char *running_in_emacs = (char *)NULL;
137 /* The current offset in the current input line. */
140 /* Mark in the current input line. */
143 /* Length of the current input line. */
146 /* Make this non-zero to return the current input_line. */
149 /* The last function executed by readline. */
150 Function *rl_last_func = (Function *)NULL;
152 /* Top level environment for readline_internal (). */
153 static jmp_buf readline_top_level;
155 /* The streams we interact with. */
156 static FILE *in_stream, *out_stream;
158 /* The names of the streams that we do input and output to. */
159 FILE *rl_instream = (FILE *)NULL;
160 FILE *rl_outstream = (FILE *)NULL;
162 /* Non-zero means echo characters as they are read. */
163 int readline_echoing_p = 1;
165 /* Current prompt. */
168 /* The number of characters read in order to type this complete command. */
169 int rl_key_sequence_length = 0;
171 /* If non-zero, then this is the address of a function to call just
172 before readline_internal () prints the first prompt. */
173 Function *rl_startup_hook = (Function *)NULL;
175 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
176 static char *the_line;
178 /* The character that can generate an EOF. Really read from
179 the terminal driver... just defaulted here. */
180 int _rl_eof_char = CTRL ('D');
182 /* Non-zero makes this the next keystroke to read. */
183 int rl_pending_input = 0;
185 /* Pointer to a useful terminal name. */
186 char *rl_terminal_name = (char *)NULL;
188 /* Non-zero means to always use horizontal scrolling in line display. */
189 int _rl_horizontal_scroll_mode = 0;
191 /* Non-zero means to display an asterisk at the starts of history lines
192 which have been modified. */
193 int _rl_mark_modified_lines = 0;
195 /* Non-zero means to use a visible bell if one is available rather than
196 simply ringing the terminal bell. */
197 int _rl_prefer_visible_bell = 0;
199 /* Line buffer and maintenence. */
200 char *rl_line_buffer = (char *)NULL;
201 int rl_line_buffer_len = 0;
202 #define DEFAULT_BUFFER_SIZE 256
204 #if defined (VISIBLE_STATS)
205 int rl_visible_stats = 0;
206 #endif /* VISIBLE_STATS */
209 /* **************************************************************** */
211 /* `Forward' declarations */
213 /* **************************************************************** */
215 /* Non-zero means do not parse any lines other than comments and
216 parser directives. */
217 unsigned char _rl_parsing_conditionalized_out = 0;
219 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
220 static int defining_kbd_macro = 0;
222 /* Non-zero means to convert characters with the meta bit set to
223 escape-prefixed characters so we can indirect through
224 emacs_meta_keymap or vi_escape_keymap. */
225 int _rl_convert_meta_chars_to_ascii = 1;
227 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
229 static int doing_an_undo;
231 /* **************************************************************** */
233 /* Top Level Functions */
235 /* **************************************************************** */
237 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
238 int _rl_meta_flag = 0; /* Forward declaration */
240 /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
241 none. A return value of NULL means that EOF was encountered. */
246 char *readline_internal ();
251 /* If we are at EOF return a NULL string. */
252 if (rl_pending_input == EOF)
254 rl_pending_input = 0;
255 return ((char *)NULL);
259 rl_prep_terminal (_rl_meta_flag);
261 #if defined (HANDLE_SIGNALS)
265 value = readline_internal ();
266 rl_deprep_terminal ();
268 #if defined (HANDLE_SIGNALS)
275 /* Read a line of input from the global rl_instream, doing output on
276 the global rl_outstream.
277 If rl_prompt is non-null, then that is our prompt. */
281 int lastc, c, eof_found;
283 in_stream = rl_instream;
284 out_stream = rl_outstream;
290 (*rl_startup_hook) ();
292 if (!readline_echoing_p)
296 fprintf (out_stream, "%s", rl_prompt);
304 #if defined (VI_MODE)
305 if (rl_editing_mode == vi_mode)
306 rl_vi_insertion_mode ();
312 int lk = last_command_was_kill;
315 code = setjmp (readline_top_level);
320 if (!rl_pending_input)
322 /* Then initialize the argument and number of keys read. */
324 rl_key_sequence_length = 0;
329 /* EOF typed to a non-blank line is a <NL>. */
330 if (c == EOF && rl_end)
333 /* The character _rl_eof_char typed to blank line, and not as the
334 previous character is interpreted as EOF. */
335 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
342 rl_dispatch (c, _rl_keymap);
344 /* If there was no change in last_command_was_kill, then no kill
345 has taken place. Note that if input is pending we are reading
346 a prefix command, so nothing has changed yet. */
347 if (!rl_pending_input)
349 if (lk == last_command_was_kill)
350 last_command_was_kill = 0;
353 #if defined (VI_MODE)
354 /* In vi mode, when you exit insert mode, the cursor moves back
355 over the previous character. We explicitly check for that here. */
356 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
364 /* Restore the original of this history line, iff the line that we
365 are editing was originally in the history, AND the line has changed. */
367 HIST_ENTRY *entry = current_history ();
369 if (entry && rl_undo_list)
371 char *temp = savestring (the_line);
373 entry = replace_history_entry (where_history (), the_line,
375 free_history_entry (entry);
377 strcpy (the_line, temp);
382 /* At any rate, it is highly likely that this line has an undo list. Get
390 return (savestring (the_line));
394 /* **************************************************************** */
396 /* Character Input Buffering */
398 /* **************************************************************** */
400 static int pop_index = 0, push_index = 0, ibuffer_len = 511;
401 static unsigned char ibuffer[512];
403 /* Non-null means it is a pointer to a function to run while waiting for
405 Function *rl_event_hook = (Function *)NULL;
407 #define any_typein (push_index != pop_index)
409 /* Add KEY to the buffer of characters to be read. */
416 rl_pending_input = EOF;
418 ibuffer[push_index++] = key;
419 if (push_index >= ibuffer_len)
423 /* Return the amount of space available in the
424 buffer for stuffing characters. */
428 if (pop_index > push_index)
429 return (pop_index - push_index);
431 return (ibuffer_len - (push_index - pop_index));
434 /* Get a key from the buffer of characters to be read.
435 Return the key in KEY.
436 Result is KEY if there was a key, or 0 if there wasn't. */
441 if (push_index == pop_index)
444 *key = ibuffer[pop_index++];
446 if (pop_index >= ibuffer_len)
452 /* Stuff KEY into the *front* of the input buffer.
453 Returns non-zero if successful, zero if there is
454 no space left in the buffer. */
459 if (ibuffer_space ())
463 pop_index = ibuffer_len - 1;
464 ibuffer[pop_index] = key;
470 /* If a character is available to be read, then read it
471 and stuff it into IBUFFER. Otherwise, just return. */
483 else if (kbhit () && ibuffer_space ())
484 rl_stuff_char (getkey ());
487 int tty = fileno (in_stream);
488 register int tem, result = -1;
492 #if defined (FIONREAD)
493 result = ioctl (tty, FIONREAD, &chars_avail);
496 #if defined (O_NDELAY)
501 flags = fcntl (tty, F_GETFL, 0);
503 fcntl (tty, F_SETFL, (flags | O_NDELAY));
504 chars_avail = read (tty, &input, 1);
506 fcntl (tty, F_SETFL, flags);
507 if (chars_avail == -1 && errno == EAGAIN)
510 #endif /* O_NDELAY */
512 /* If there's nothing available, don't waste time trying to read
514 if (chars_avail == 0)
517 tem = ibuffer_space ();
519 if (chars_avail > tem)
522 /* One cannot read all of the available input. I can only read a single
523 character at a time, or else programs which require input can be
524 thwarted. If the buffer is larger than one character, I lose.
526 if (tem < ibuffer_len)
531 while (chars_avail--)
532 rl_stuff_char (rl_getc (in_stream));
537 rl_stuff_char (input);
539 #endif /* def __GO32__/else */
542 static int next_macro_key ();
543 /* Read a key, including pending input. */
549 rl_key_sequence_length++;
551 if (rl_pending_input)
553 c = rl_pending_input;
554 rl_pending_input = 0;
558 /* If input is coming from a macro, then use that. */
559 if (c = next_macro_key ())
562 /* If the user has an event function, then call it periodically. */
565 while (rl_event_hook && !rl_get_char (&c))
573 if (!rl_get_char (&c))
574 c = rl_getc (in_stream);
581 /* Found later in this file. */
582 static void add_macro_char (), with_macro_input ();
584 /* Do the command associated with KEY in MAP.
585 If the associated command is really a keymap, then read
586 another key, and dispatch into that map. */
588 rl_dispatch (key, map)
592 #if defined (VI_MODE)
593 extern int _rl_vi_last_command, _rl_vi_last_repeat, _rl_vi_last_arg_sign;
596 if (defining_kbd_macro)
597 add_macro_char (key);
599 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
601 if (map[ESC].type == ISKMAP)
603 map = (Keymap)map[ESC].function;
605 rl_key_sequence_length += 2;
606 rl_dispatch (key, map);
613 switch (map[key].type)
617 Function *func = map[key].function;
619 if (func != (Function *)NULL)
621 /* Special case rl_do_lowercase_version (). */
622 if (func == rl_do_lowercase_version)
624 rl_dispatch (to_lower (key), map);
628 (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
630 /* If we have input pending, then the last command was a prefix
631 command. Don't change the state of rl_last_func. Otherwise,
632 remember the last command executed in this variable. */
633 if (!rl_pending_input)
634 rl_last_func = map[key].function;
645 if (map[key].function != (Function *)NULL)
649 rl_key_sequence_length++;
650 newkey = rl_read_key ();
651 rl_dispatch (newkey, (Keymap)map[key].function);
661 if (map[key].function != (Function *)NULL)
665 macro = savestring ((char *)map[key].function);
666 with_macro_input (macro);
671 #if defined (VI_MODE)
672 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
673 rl_vi_textmod_command (key))
675 _rl_vi_last_command = key;
676 _rl_vi_last_repeat = rl_numeric_arg;
677 _rl_vi_last_arg_sign = rl_arg_sign;
683 /* **************************************************************** */
685 /* Hacking Keyboard Macros */
687 /* **************************************************************** */
689 /* The currently executing macro string. If this is non-zero,
690 then it is a malloc ()'ed string where input is coming from. */
691 static char *executing_macro = (char *)NULL;
693 /* The offset in the above string to the next character to be read. */
694 static int executing_macro_index = 0;
696 /* The current macro string being built. Characters get stuffed
697 in here by add_macro_char (). */
698 static char *current_macro = (char *)NULL;
700 /* The size of the buffer allocated to current_macro. */
701 static int current_macro_size = 0;
703 /* The index at which characters are being added to current_macro. */
704 static int current_macro_index = 0;
706 /* A structure used to save nested macro strings.
707 It is a linked list of string/index for each saved macro. */
709 struct saved_macro *next;
714 /* The list of saved macros. */
715 struct saved_macro *macro_list = (struct saved_macro *)NULL;
717 /* Forward declarations of static functions. Thank you C. */
718 static void push_executing_macro (), pop_executing_macro ();
720 /* This one has to be declared earlier in the file. */
721 /* static void add_macro_char (); */
723 /* Set up to read subsequent input from STRING.
724 STRING is free ()'ed when we are done with it. */
726 with_macro_input (string)
729 push_executing_macro ();
730 executing_macro = string;
731 executing_macro_index = 0;
734 /* Return the next character available from a macro, or 0 if
735 there are no macro characters. */
739 if (!executing_macro)
742 if (!executing_macro[executing_macro_index])
744 pop_executing_macro ();
745 return (next_macro_key ());
748 return (executing_macro[executing_macro_index++]);
751 /* Save the currently executing macro on a stack of saved macros. */
753 push_executing_macro ()
755 struct saved_macro *saver;
757 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
758 saver->next = macro_list;
759 saver->index = executing_macro_index;
760 saver->string = executing_macro;
765 /* Discard the current macro, replacing it with the one
766 on the top of the stack of saved macros. */
768 pop_executing_macro ()
771 free (executing_macro);
773 executing_macro = (char *)NULL;
774 executing_macro_index = 0;
778 struct saved_macro *disposer = macro_list;
779 executing_macro = macro_list->string;
780 executing_macro_index = macro_list->index;
781 macro_list = macro_list->next;
786 /* Add a character to the macro being built. */
791 if (current_macro_index + 1 >= current_macro_size)
794 current_macro = (char *)xmalloc (current_macro_size = 25);
797 (char *)xrealloc (current_macro, current_macro_size += 25);
800 current_macro[current_macro_index++] = c;
801 current_macro[current_macro_index] = '\0';
804 /* Begin defining a keyboard macro.
805 Keystrokes are recorded as they are executed.
806 End the definition with rl_end_kbd_macro ().
807 If a numeric argument was explicitly typed, then append this
808 definition to the end of the existing macro, and start by
809 re-executing the existing macro. */
810 rl_start_kbd_macro (ignore1, ignore2)
811 int ignore1, ignore2;
813 if (defining_kbd_macro)
819 with_macro_input (savestring (current_macro));
822 current_macro_index = 0;
824 defining_kbd_macro = 1;
827 /* Stop defining a keyboard macro.
828 A numeric argument says to execute the macro right now,
829 that many times, counting the definition as the first time. */
830 rl_end_kbd_macro (count, ignore)
833 if (!defining_kbd_macro)
836 current_macro_index -= (rl_key_sequence_length - 1);
837 current_macro[current_macro_index] = '\0';
839 defining_kbd_macro = 0;
841 rl_call_last_kbd_macro (--count, 0);
844 /* Execute the most recently defined keyboard macro.
845 COUNT says how many times to execute it. */
846 rl_call_last_kbd_macro (count, ignore)
853 with_macro_input (savestring (current_macro));
857 /* **************************************************************** */
859 /* Initializations */
861 /* **************************************************************** */
863 /* Initliaze readline (and terminal if not already). */
866 /* If we have never been called before, initialize the
867 terminal and data structures. */
870 readline_initialize_everything ();
874 /* Initalize the current line information. */
875 rl_point = rl_end = 0;
876 the_line = rl_line_buffer;
879 /* We aren't done yet. We haven't even gotten started yet! */
882 /* Tell the history routines what is going on. */
883 start_using_history ();
885 /* Make the display buffer match the state of the line. */
886 rl_reset_line_state ();
888 /* No such function typed yet. */
889 rl_last_func = (Function *)NULL;
891 /* Parsing of key-bindings begins in an enabled state. */
892 _rl_parsing_conditionalized_out = 0;
895 /* Initialize the entire state of the world. */
896 readline_initialize_everything ()
898 /* Find out if we are running in Emacs. */
899 running_in_emacs = getenv ("EMACS");
901 /* Set up input and output if they are not already set up. */
906 rl_outstream = stdout;
908 /* Bind in_stream and out_stream immediately. These values may change,
909 but they may also be used before readline_internal () is called. */
910 in_stream = rl_instream;
911 out_stream = rl_outstream;
913 /* Allocate data structures. */
916 (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
918 /* Initialize the terminal interface. */
919 init_terminal_io ((char *)NULL);
921 /* Bind tty characters to readline functions. */
922 readline_default_bindings ();
924 /* Initialize the function names. */
925 rl_initialize_funmap ();
927 /* Read in the init file. */
928 rl_read_init_file ((char *)NULL);
930 /* If the completion parser's default word break characters haven't
931 been set yet, then do so now. */
933 if (rl_completer_word_break_characters == (char *)NULL)
934 rl_completer_word_break_characters = rl_basic_word_break_characters;
938 /* If this system allows us to look at the values of the regular
939 input editing characters, then bind them to their readline
940 equivalents, iff the characters are not bound to keymaps. */
942 readline_default_bindings ()
944 rltty_set_default_bindings (_rl_keymap);
948 /* **************************************************************** */
950 /* Numeric Arguments */
952 /* **************************************************************** */
954 /* Handle C-u style numeric args, as well as M--, and M-digits. */
956 /* Add the current digit to the argument in progress. */
957 rl_digit_argument (ignore, key)
960 rl_pending_input = key;
964 /* What to do when you abort reading an argument. */
965 rl_discard_argument ()
972 /* Create a default argument. */
975 rl_numeric_arg = rl_arg_sign = 1;
979 /* C-u, universal argument. Multiply the current argument by 4.
980 Read a key. If the key has nothing to do with arguments, then
981 dispatch on it. If the key is the abort character then abort. */
982 rl_universal_argument ()
993 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
994 key = c = rl_read_key ();
996 if (_rl_keymap[c].type == ISFUNC &&
997 _rl_keymap[c].function == rl_universal_argument)
1005 if (rl_explicit_arg)
1006 rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
1008 rl_numeric_arg = (c - '0');
1009 rl_explicit_arg = 1;
1013 if (c == '-' && !rl_explicit_arg)
1020 rl_clear_message ();
1021 rl_dispatch (key, _rl_keymap);
1028 /* **************************************************************** */
1030 /* Terminal and Termcap */
1032 /* **************************************************************** */
1034 static char *term_buffer = (char *)NULL;
1035 static char *term_string_buffer = (char *)NULL;
1037 /* Non-zero means this terminal can't really do anything. */
1039 /* On Solaris2, sys/types.h #includes sys/reg.h, which #defines PC.
1040 Unfortunately, PC is a global variable used by the termcap library. */
1043 #if !defined (__linux__)
1046 #endif /* __linux__ */
1048 /* Some strings to control terminal actions. These are output by tputs (). */
1049 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1051 int screenwidth, screenheight;
1053 /* Non-zero if we determine that the terminal can do character insertion. */
1054 int terminal_can_insert = 0;
1056 /* How to insert characters. */
1057 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1059 /* How to delete characters. */
1060 char *term_dc, *term_DC;
1062 #if defined (HACK_TERMCAP_MOTION)
1063 char *term_forward_char;
1064 #endif /* HACK_TERMCAP_MOTION */
1066 /* How to go up a line. */
1069 /* True if we have funny auto-line-wrap ("am" and "xn"). */
1072 /* A visible bell, if the terminal can be made to flash the screen. */
1075 /* Non-zero means that this terminal has a meta key. */
1078 /* The string to write to turn on the meta key, if this term has one. */
1081 /* The string to write to turn off the meta key, if this term has one. */
1084 /* The key sequences output by the arrow keys, if this terminal has any. */
1085 char *term_ku, *term_kd, *term_kr, *term_kl;
1087 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1089 rl_reset_terminal (terminal_name)
1090 char *terminal_name;
1092 init_terminal_io (terminal_name);
1095 /* Set readline's idea of the screen size. TTY is a file descriptor open
1096 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
1097 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
1098 non-null serve to check whether or not we have initialized termcap. */
1100 _rl_set_screen_size (tty, ignore_env)
1101 int tty, ignore_env;
1103 #if defined (TIOCGWINSZ)
1104 struct winsize window_size;
1105 #endif /* TIOCGWINSZ */
1107 #if defined (TIOCGWINSZ)
1108 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
1110 screenwidth = (int) window_size.ws_col;
1111 screenheight = (int) window_size.ws_row;
1113 #endif /* TIOCGWINSZ */
1115 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
1117 if (screenwidth <= 0)
1121 if (!ignore_env && (sw = getenv ("COLUMNS")))
1122 screenwidth = atoi (sw);
1124 if (screenwidth <= 0 && term_string_buffer)
1125 screenwidth = tgetnum ("co");
1128 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
1130 if (screenheight <= 0)
1134 if (!ignore_env && (sh = getenv ("LINES")))
1135 screenheight = atoi (sh);
1137 if (screenheight <= 0 && term_string_buffer)
1138 screenheight = tgetnum ("li");
1141 /* If all else fails, default to 80x24 terminal. */
1142 if (screenwidth <= 0)
1145 if (screenheight <= 0)
1149 /* If we're being compiled as part of bash, set the environment
1150 variables $LINES and $COLUMNS to new values. */
1151 set_lines_and_columns (screenheight, screenwidth);
1154 /* If we don't have xn (most modern terminals do),
1155 don't use the last column. */
1160 init_terminal_io (terminal_name)
1161 char *terminal_name;
1164 screenwidth = ScreenCols ();
1165 screenheight = ScreenRows ();
1167 term_im = term_ei = term_ic = term_IC = (char *)NULL;
1168 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1170 /* Does the _GO32_ have a meta key? I don't know. */
1172 term_mm = term_mo = (char *)NULL;
1174 /* It probably has arrow keys, but I don't know what they are. */
1175 term_ku = term_kd = term_kr = term_kl = (char *)NULL;
1177 #if defined (HACK_TERMCAP_MOTION)
1178 term_forward_char = (char *)NULL;
1180 terminal_can_insert = term_xn = 0;
1182 #else /* !__GO32__ */
1183 char *term, *buffer;
1186 term = terminal_name ? terminal_name : getenv ("TERM");
1188 if (!term_string_buffer)
1189 term_string_buffer = (char *)xmalloc (2048);
1192 term_buffer = (char *)xmalloc (2048);
1194 buffer = term_string_buffer;
1196 term_clrpag = term_cr = term_clreol = (char *)NULL;
1201 if (tgetent (term_buffer, term) <= 0)
1207 term_im = term_ei = term_ic = term_IC = (char *)NULL;
1208 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1209 term_ku = term_kd = term_kl = term_kr = (char *)NULL;
1210 #if defined (HACK_TERMCAP_MOTION)
1211 term_forward_char = (char *)NULL;
1213 terminal_can_insert = term_xn = 0;
1217 BC = tgetstr ("pc", &buffer);
1218 PC = buffer ? *buffer : 0;
1220 term_backspace = tgetstr ("le", &buffer);
1222 term_cr = tgetstr ("cr", &buffer);
1223 term_clreol = tgetstr ("ce", &buffer);
1224 term_clrpag = tgetstr ("cl", &buffer);
1229 #if defined (HACK_TERMCAP_MOTION)
1230 term_forward_char = tgetstr ("nd", &buffer);
1231 #endif /* HACK_TERMCAP_MOTION */
1234 tty = fileno (rl_instream);
1238 screenwidth = screenheight = 0;
1240 term_xn = tgetflag ("am") && tgetflag ("xn");
1242 _rl_set_screen_size (tty, 0);
1244 term_im = tgetstr ("im", &buffer);
1245 term_ei = tgetstr ("ei", &buffer);
1246 term_IC = tgetstr ("IC", &buffer);
1247 term_ic = tgetstr ("ic", &buffer);
1249 /* "An application program can assume that the terminal can do
1250 character insertion if *any one of* the capabilities `IC',
1251 `im', `ic' or `ip' is provided." But we can't do anything if
1252 only `ip' is provided, so... */
1253 terminal_can_insert = (term_IC || term_im || term_ic);
1255 term_up = tgetstr ("up", &buffer);
1256 term_dc = tgetstr ("dc", &buffer);
1257 term_DC = tgetstr ("DC", &buffer);
1259 visible_bell = tgetstr ("vb", &buffer);
1261 /* Check to see if this terminal has a meta key. */
1262 term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
1265 term_mm = tgetstr ("mm", &buffer);
1266 term_mo = tgetstr ("mo", &buffer);
1270 term_mm = (char *)NULL;
1271 term_mo = (char *)NULL;
1274 /* Attempt to find and bind the arrow keys. Do not override already
1275 bound keys in an overzealous attempt, however. */
1276 term_ku = tgetstr ("ku", &buffer);
1277 term_kd = tgetstr ("kd", &buffer);
1278 term_kr = tgetstr ("kr", &buffer);
1279 term_kl = tgetstr ("kl", &buffer);
1285 func = rl_function_of_keyseq (term_ku, _rl_keymap, (int *)NULL);
1287 if (!func || func == rl_do_lowercase_version)
1288 rl_set_key (term_ku, rl_get_previous_history, _rl_keymap);
1295 func = rl_function_of_keyseq (term_kd, _rl_keymap, (int *)NULL);
1297 if (!func || func == rl_do_lowercase_version)
1298 rl_set_key (term_kd, rl_get_next_history, _rl_keymap);
1305 func = rl_function_of_keyseq (term_kr, _rl_keymap, (int *)NULL);
1307 if (!func || func == rl_do_lowercase_version)
1308 rl_set_key (term_kr, rl_forward, _rl_keymap);
1315 func = rl_function_of_keyseq (term_kl, _rl_keymap, (int *)NULL);
1317 if (!func || func == rl_do_lowercase_version)
1318 rl_set_key (term_kl, rl_backward, _rl_keymap);
1320 #endif /* !__GO32__ */
1323 /* A function for the use of tputs () */
1325 _rl_output_character_function (c)
1328 return putc (c, out_stream);
1331 /* Write COUNT characters from STRING to the output stream. */
1333 _rl_output_some_chars (string, count)
1337 fwrite (string, 1, count, out_stream);
1342 /* Move the cursor back. */
1350 for (i = 0; i < count; i++)
1351 tputs (term_backspace, 1, _rl_output_character_function);
1353 #endif /* !__GO32__ */
1354 for (i = 0; i < count; i++)
1355 putc ('\b', out_stream);
1358 /* Move to the start of the next line. */
1361 #if defined (NEW_TTY_DRIVER)
1362 tputs (term_cr, 1, _rl_output_character_function);
1363 #endif /* NEW_TTY_DRIVER */
1364 putc ('\n', out_stream);
1368 /* **************************************************************** */
1370 /* Utility Functions */
1372 /* **************************************************************** */
1374 /* Return 0 if C is not a member of the class of characters that belong
1375 in words, or 1 if it is. */
1377 int allow_pathname_alphabetic_chars = 0;
1378 char *pathname_alphabetic_chars = "/-_=~.#$";
1384 if (pure_alphabetic (c) || (numeric (c)))
1387 if (allow_pathname_alphabetic_chars)
1388 return ((int) strchr (pathname_alphabetic_chars, c));
1393 /* Return non-zero if C is a numeric character. */
1398 return (c >= '0' && c <= '9');
1401 /* Ring the terminal bell. */
1405 if (readline_echoing_p)
1408 if (_rl_prefer_visible_bell && visible_bell)
1409 tputs (visible_bell, 1, _rl_output_character_function);
1411 #endif /* !__GO32__ */
1413 fprintf (stderr, "\007");
1420 /* How to abort things. */
1424 rl_clear_message ();
1425 rl_init_argument ();
1426 rl_pending_input = 0;
1428 defining_kbd_macro = 0;
1429 while (executing_macro)
1430 pop_executing_macro ();
1432 rl_last_func = (Function *)NULL;
1433 longjmp (readline_top_level, 1);
1436 /* Return a copy of the string between FROM and TO.
1437 FROM is inclusive, TO is not. */
1439 rl_copy_text (from, to)
1442 register int length;
1445 /* Fix it if the caller is confused. */
1454 copy = (char *)xmalloc (1 + length);
1455 strncpy (copy, the_line + from, length);
1456 copy[length] = '\0';
1460 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
1463 rl_extend_line_buffer (len)
1466 while (len >= rl_line_buffer_len)
1469 (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
1471 the_line = rl_line_buffer;
1475 /* **************************************************************** */
1477 /* Insert and Delete */
1479 /* **************************************************************** */
1481 /* Insert a string of text into the line at point. This is the only
1482 way that you should do insertion. rl_insert () calls this
1484 rl_insert_text (string)
1487 register int i, l = strlen (string);
1489 if (rl_end + l >= rl_line_buffer_len)
1490 rl_extend_line_buffer (rl_end + l);
1492 for (i = rl_end; i >= rl_point; i--)
1493 the_line[i + l] = the_line[i];
1494 strncpy (the_line + rl_point, string, l);
1496 /* Remember how to undo this if we aren't undoing something. */
1499 /* If possible and desirable, concatenate the undos. */
1500 if ((strlen (string) == 1) &&
1502 (rl_undo_list->what == UNDO_INSERT) &&
1503 (rl_undo_list->end == rl_point) &&
1504 (rl_undo_list->end - rl_undo_list->start < 20))
1505 rl_undo_list->end++;
1507 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
1511 the_line[rl_end] = '\0';
1514 /* Delete the string between FROM and TO. FROM is
1515 inclusive, TO is not. */
1516 rl_delete_text (from, to)
1519 register char *text;
1521 /* Fix it if the caller is confused. */
1528 text = rl_copy_text (from, to);
1529 strncpy (the_line + from, the_line + to, rl_end - to);
1531 /* Remember how to undo this delete. */
1533 rl_add_undo (UNDO_DELETE, from, to, text);
1537 rl_end -= (to - from);
1538 the_line[rl_end] = '\0';
1542 /* **************************************************************** */
1544 /* Readline character functions */
1546 /* **************************************************************** */
1548 /* This is not a gap editor, just a stupid line input routine. No hair
1549 is involved in writing any of the functions, and none should be. */
1553 rl_end is the place in the string that we would place '\0';
1554 i.e., it is always safe to place '\0' there.
1556 rl_point is the place in the string where the cursor is. Sometimes
1557 this is the same as rl_end.
1559 Any command that is called interactively receives two arguments.
1560 The first is a count: the numeric arg pased to this command.
1561 The second is the key which invoked this command.
1565 /* **************************************************************** */
1567 /* Movement Commands */
1569 /* **************************************************************** */
1571 /* Note that if you `optimize' the display for these functions, you cannot
1572 use said functions in other functions which do not do optimizing display.
1573 I.e., you will have to update the data base for rl_redisplay, and you
1574 might as well let rl_redisplay do that job. */
1576 /* Move forward COUNT characters. */
1581 rl_backward (-count);
1585 #if defined (VI_MODE)
1586 if (rl_point >= (rl_end - (rl_editing_mode == vi_mode)))
1588 if (rl_point == rl_end)
1589 #endif /* VI_MODE */
1600 /* Move backward COUNT characters. */
1605 rl_forward (-count);
1620 /* Move to the beginning of the line. */
1626 /* Move to the end of the line. */
1632 /* Move forward a word. We do what Emacs does. */
1633 rl_forward_word (count)
1640 rl_backward_word (-count);
1646 if (rl_point == rl_end)
1649 /* If we are not in a word, move forward until we are in one.
1650 Then, move forward until we hit a non-alphabetic character. */
1651 c = the_line[rl_point];
1652 if (!alphabetic (c))
1654 while (++rl_point < rl_end)
1656 c = the_line[rl_point];
1657 if (alphabetic (c)) break;
1660 if (rl_point == rl_end) return;
1661 while (++rl_point < rl_end)
1663 c = the_line[rl_point];
1664 if (!alphabetic (c)) break;
1670 /* Move backward a word. We do what Emacs does. */
1671 rl_backward_word (count)
1678 rl_forward_word (-count);
1687 /* Like rl_forward_word (), except that we look at the characters
1688 just before point. */
1690 c = the_line[rl_point - 1];
1691 if (!alphabetic (c))
1695 c = the_line[rl_point - 1];
1696 if (alphabetic (c)) break;
1702 c = the_line[rl_point - 1];
1703 if (!alphabetic (c))
1711 /* Clear the current line. Numeric argument to C-l does this. */
1714 int curr_line = _rl_last_c_pos / screenwidth;
1716 _rl_move_vert (curr_line);
1717 _rl_move_cursor_relative (0, the_line); /* XXX is this right */
1721 int row, col, width, row_start;
1723 ScreenGetCursor (&row, &col);
1724 width = ScreenCols ();
1725 row_start = ScreenPrimary + (row * width);
1726 memset (row_start + col, 0, (width - col) * 2);
1728 #else /* __GO32__ */
1730 tputs (term_clreol, 1, _rl_output_character_function);
1731 #endif /* __GO32__/else */
1733 rl_forced_update_display ();
1734 rl_display_fixed = 1;
1737 /* C-l typed to a line without quoting clears the screen, and then reprints
1738 the prompt and the current input line. Given a numeric arg, redraw only
1739 the current line. */
1742 if (rl_explicit_arg)
1750 tputs (term_clrpag, 1, _rl_output_character_function);
1752 #endif /* !__GO32__ */
1755 rl_forced_update_display ();
1756 rl_display_fixed = 1;
1759 rl_arrow_keys (count, c)
1764 ch = rl_read_key ();
1766 switch (to_upper (ch))
1769 rl_get_previous_history (count);
1773 rl_get_next_history (count);
1781 rl_backward (count);
1790 /* **************************************************************** */
1794 /* **************************************************************** */
1796 /* Insert the character C at the current location, moving point forward. */
1797 rl_insert (count, c)
1806 /* If we can optimize, then do it. But don't let people crash
1807 readline because of extra large arguments. */
1808 if (count > 1 && count < 1024)
1810 string = (char *)alloca (1 + count);
1812 for (i = 0; i < count; i++)
1816 rl_insert_text (string);
1824 string = (char *)alloca (1024 + 1);
1826 for (i = 0; i < 1024; i++)
1831 decreaser = (count > 1024 ? 1024 : count);
1832 string[decreaser] = '\0';
1833 rl_insert_text (string);
1839 /* We are inserting a single character.
1840 If there is pending input, then make a string of all of the
1841 pending characters that are bound to rl_insert, and insert
1848 string = (char *)alloca (ibuffer_len + 1);
1851 while ((t = rl_get_char (&key)) &&
1852 (_rl_keymap[key].type == ISFUNC &&
1853 _rl_keymap[key].function == rl_insert))
1857 rl_unget_char (key);
1860 rl_insert_text (string);
1865 /* Inserting a single character. */
1866 string = (char *)alloca (2);
1870 rl_insert_text (string);
1874 /* Insert the next typed character verbatim. */
1875 rl_quoted_insert (count)
1881 rl_insert (count, c);
1884 /* Insert a tab character. */
1885 rl_tab_insert (count)
1888 rl_insert (count, '\t');
1891 /* What to do when a NEWLINE is pressed. We accept the whole line.
1892 KEY is the key that invoked this command. I guess it could have
1893 meaning in the future. */
1894 rl_newline (count, key)
1900 #if defined (VI_MODE)
1902 extern int _rl_vi_doing_insert;
1903 if (_rl_vi_doing_insert)
1905 rl_end_undo_group ();
1906 _rl_vi_doing_insert = 0;
1911 #endif /* VI_MODE */
1913 if (readline_echoing_p)
1915 _rl_move_vert (_rl_vis_botlin);
1918 fflush (out_stream);
1923 rl_clean_up_for_exit ()
1925 if (readline_echoing_p)
1927 _rl_move_vert (_rl_vis_botlin);
1929 fflush (out_stream);
1930 rl_restart_output ();
1934 /* What to do for some uppercase characters, like meta characters,
1935 and some characters appearing in emacs_ctlx_keymap. This function
1936 is just a stub, you bind keys to it and the code in rl_dispatch ()
1937 is special cased. */
1938 rl_do_lowercase_version (ignore1, ignore2)
1939 int ignore1, ignore2;
1943 /* Rubout the character behind point. */
1959 if (count > 1 || rl_explicit_arg)
1961 int orig_point = rl_point;
1962 rl_backward (count);
1963 rl_kill_text (orig_point, rl_point);
1967 int c = the_line[--rl_point];
1968 rl_delete_text (rl_point, rl_point + 1);
1970 if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
1973 l = rl_character_len (c, rl_point);
1974 _rl_erase_at_end_of_line (l);
1979 /* Delete the character under the cursor. Given a numeric argument,
1980 kill that many characters instead. */
1981 rl_delete (count, invoking_key)
1982 int count, invoking_key;
1990 if (rl_point == rl_end)
1996 if (count > 1 || rl_explicit_arg)
1998 int orig_point = rl_point;
2000 rl_kill_text (orig_point, rl_point);
2001 rl_point = orig_point;
2004 rl_delete_text (rl_point, rl_point + 1);
2007 /* Delete all spaces and tabs around point. */
2008 rl_delete_horizontal_space (count, ignore)
2011 int start = rl_point;
2013 while (rl_point && whitespace (the_line[rl_point - 1]))
2018 while (rl_point < rl_end && whitespace (the_line[rl_point]))
2021 if (start == rl_point)
2025 rl_delete_text (start, rl_point);
2030 /* **************************************************************** */
2034 /* **************************************************************** */
2036 /* The next two functions mimic unix line editing behaviour, except they
2037 save the deleted text on the kill ring. This is safer than not saving
2038 it, and since we have a ring, nobody should get screwed. */
2040 /* This does what C-w does in Unix. We can't prevent people from
2041 using behaviour that they expect. */
2042 rl_unix_word_rubout ()
2048 int orig_point = rl_point;
2050 while (rl_point && whitespace (the_line[rl_point - 1]))
2053 while (rl_point && !whitespace (the_line[rl_point - 1]))
2056 rl_kill_text (rl_point, orig_point);
2060 /* Here is C-u doing what Unix does. You don't *have* to use these
2061 key-bindings. We have a choice of killing the entire line, or
2062 killing from where we are to the start of the line. We choose the
2063 latter, because if you are a Unix weenie, then you haven't backspaced
2064 into the line at all, and if you aren't, then you know what you are
2066 rl_unix_line_discard ()
2072 rl_kill_text (rl_point, 0);
2078 /* **************************************************************** */
2080 /* Commands For Typos */
2082 /* **************************************************************** */
2084 /* Random and interesting things in here. */
2086 /* **************************************************************** */
2090 /* **************************************************************** */
2092 /* The three kinds of things that we know how to do. */
2097 /* Uppercase the word at point. */
2098 rl_upcase_word (count)
2101 rl_change_case (count, UpCase);
2104 /* Lowercase the word at point. */
2105 rl_downcase_word (count)
2108 rl_change_case (count, DownCase);
2111 /* Upcase the first letter, downcase the rest. */
2112 rl_capitalize_word (count)
2115 rl_change_case (count, CapCase);
2118 /* The meaty function.
2119 Change the case of COUNT words, performing OP on them.
2120 OP is one of UpCase, DownCase, or CapCase.
2121 If a negative argument is given, leave point where it started,
2122 otherwise, leave it where it moves to. */
2123 rl_change_case (count, op)
2126 register int start = rl_point, end;
2129 rl_forward_word (count);
2139 /* We are going to modify some text, so let's prepare to undo it. */
2140 rl_modifying (start, end);
2142 for (; start < end; start++)
2147 the_line[start] = to_upper (the_line[start]);
2151 the_line[start] = to_lower (the_line[start]);
2157 the_line[start] = to_upper (the_line[start]);
2162 the_line[start] = to_lower (the_line[start]);
2164 if (!pure_alphabetic (the_line[start]))
2175 /* **************************************************************** */
2179 /* **************************************************************** */
2181 /* Transpose the words at point. */
2182 rl_transpose_words (count)
2185 char *word1, *word2;
2186 int w1_beg, w1_end, w2_beg, w2_end;
2187 int orig_point = rl_point;
2191 /* Find the two words. */
2192 rl_forward_word (count);
2194 rl_backward_word (1);
2196 rl_backward_word (count);
2198 rl_forward_word (1);
2201 /* Do some check to make sure that there really are two words. */
2202 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
2205 rl_point = orig_point;
2209 /* Get the text of the words. */
2210 word1 = rl_copy_text (w1_beg, w1_end);
2211 word2 = rl_copy_text (w2_beg, w2_end);
2213 /* We are about to do many insertions and deletions. Remember them
2214 as one operation. */
2215 rl_begin_undo_group ();
2217 /* Do the stuff at word2 first, so that we don't have to worry
2218 about word1 moving. */
2220 rl_delete_text (w2_beg, w2_end);
2221 rl_insert_text (word1);
2224 rl_delete_text (w1_beg, w1_end);
2225 rl_insert_text (word2);
2227 /* This is exactly correct since the text before this point has not
2228 changed in length. */
2231 /* I think that does it. */
2232 rl_end_undo_group ();
2233 free (word1); free (word2);
2236 /* Transpose the characters at point. If point is at the end of the line,
2237 then transpose the characters before point. */
2238 rl_transpose_chars (count)
2246 if (!rl_point || rl_end < 2)
2252 rl_begin_undo_group ();
2254 if (rl_point == rl_end)
2261 dummy[0] = the_line[rl_point];
2264 rl_delete_text (rl_point, rl_point + 1);
2267 if (rl_point > rl_end)
2269 else if (rl_point < 0)
2271 rl_insert_text (dummy);
2273 rl_end_undo_group ();
2276 /* **************************************************************** */
2278 /* Undo, and Undoing */
2280 /* **************************************************************** */
2282 /* The current undo list for THE_LINE. */
2283 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
2285 /* Remember how to undo something. Concatenate some undos if that
2287 rl_add_undo (what, start, end, text)
2288 enum undo_code what;
2292 UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
2294 temp->start = start;
2297 temp->next = rl_undo_list;
2298 rl_undo_list = temp;
2301 /* Free the existing undo list. */
2304 while (rl_undo_list)
2306 UNDO_LIST *release = rl_undo_list;
2307 rl_undo_list = rl_undo_list->next;
2309 if (release->what == UNDO_DELETE)
2310 free (release->text);
2314 rl_undo_list = (UNDO_LIST *)NULL;
2317 /* Undo the next thing in the list. Return 0 if there
2318 is nothing to undo, or non-zero if there was. */
2323 int waiting_for_begin = 0;
2331 switch (rl_undo_list->what) {
2333 /* Undoing deletes means inserting some text. */
2335 rl_point = rl_undo_list->start;
2336 rl_insert_text (rl_undo_list->text);
2337 free (rl_undo_list->text);
2340 /* Undoing inserts means deleting some text. */
2342 rl_delete_text (rl_undo_list->start, rl_undo_list->end);
2343 rl_point = rl_undo_list->start;
2346 /* Undoing an END means undoing everything 'til we get to
2349 waiting_for_begin++;
2352 /* Undoing a BEGIN means that we are done with this group. */
2354 if (waiting_for_begin)
2355 waiting_for_begin--;
2367 release = rl_undo_list;
2368 rl_undo_list = rl_undo_list->next;
2371 if (waiting_for_begin)
2377 /* Begin a group. Subsequent undos are undone as an atomic operation. */
2378 rl_begin_undo_group ()
2380 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
2383 /* End an undo group started with rl_begin_undo_group (). */
2384 rl_end_undo_group ()
2386 rl_add_undo (UNDO_END, 0, 0, 0);
2389 /* Save an undo entry for the text from START to END. */
2390 rl_modifying (start, end)
2402 char *temp = rl_copy_text (start, end);
2403 rl_begin_undo_group ();
2404 rl_add_undo (UNDO_DELETE, start, end, temp);
2405 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
2406 rl_end_undo_group ();
2410 /* Revert the current line to its previous state. */
2417 while (rl_undo_list)
2422 /* Do some undoing of things that were done. */
2423 rl_undo_command (count)
2425 if (count < 0) return; /* Nothing to do. */
2441 /* **************************************************************** */
2443 /* History Utilities */
2445 /* **************************************************************** */
2447 /* We already have a history library, and that is what we use to control
2448 the history features of readline. However, this is our local interface
2449 to the history mechanism. */
2451 /* While we are editing the history, this is the saved
2452 version of the original line. */
2453 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
2455 /* Set the history pointer back to the last entry in the history. */
2456 start_using_history ()
2459 if (saved_line_for_history)
2460 free_history_entry (saved_line_for_history);
2462 saved_line_for_history = (HIST_ENTRY *)NULL;
2465 /* Free the contents (and containing structure) of a HIST_ENTRY. */
2467 free_history_entry (entry)
2476 /* Perhaps put back the current line if it has changed. */
2477 maybe_replace_line ()
2479 HIST_ENTRY *temp = current_history ();
2481 /* If the current line has changed, save the changes. */
2482 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
2484 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
2490 /* Put back the saved_line_for_history if there is one. */
2491 maybe_unsave_line ()
2493 if (saved_line_for_history)
2497 line_len = strlen (saved_line_for_history->line);
2499 if (line_len >= rl_line_buffer_len)
2500 rl_extend_line_buffer (line_len);
2502 strcpy (the_line, saved_line_for_history->line);
2503 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
2504 free_history_entry (saved_line_for_history);
2505 saved_line_for_history = (HIST_ENTRY *)NULL;
2506 rl_end = rl_point = strlen (the_line);
2512 /* Save the current line in saved_line_for_history. */
2515 if (!saved_line_for_history)
2517 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
2518 saved_line_for_history->line = savestring (the_line);
2519 saved_line_for_history->data = (char *)rl_undo_list;
2523 /* **************************************************************** */
2525 /* History Commands */
2527 /* **************************************************************** */
2529 /* Meta-< goes to the start of the history. */
2530 rl_beginning_of_history ()
2532 rl_get_previous_history (1 + where_history ());
2535 /* Meta-> goes to the end of the history. (The current line). */
2536 rl_end_of_history ()
2538 maybe_replace_line ();
2540 maybe_unsave_line ();
2543 /* Move down to the next history line. */
2544 rl_get_next_history (count)
2547 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2551 rl_get_previous_history (-count);
2558 maybe_replace_line ();
2562 temp = next_history ();
2569 maybe_unsave_line ();
2574 line_len = strlen (temp->line);
2576 if (line_len >= rl_line_buffer_len)
2577 rl_extend_line_buffer (line_len);
2579 strcpy (the_line, temp->line);
2580 rl_undo_list = (UNDO_LIST *)temp->data;
2581 rl_end = rl_point = strlen (the_line);
2582 #if defined (VI_MODE)
2583 if (rl_editing_mode == vi_mode)
2585 #endif /* VI_MODE */
2589 /* Get the previous item out of our interactive history, making it the current
2590 line. If there is no previous history, just ding. */
2591 rl_get_previous_history (count)
2594 HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
2595 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2599 rl_get_next_history (-count);
2606 /* If we don't have a line saved, then save this one. */
2609 /* If the current line has changed, save the changes. */
2610 maybe_replace_line ();
2614 temp = previous_history ();
2622 /* If there was a large argument, and we moved back to the start of the
2623 history, that is not an error. So use the last value found. */
2624 if (!temp && old_temp)
2633 line_len = strlen (temp->line);
2635 if (line_len >= rl_line_buffer_len)
2636 rl_extend_line_buffer (line_len);
2638 strcpy (the_line, temp->line);
2639 rl_undo_list = (UNDO_LIST *)temp->data;
2640 rl_end = rl_point = line_len;
2642 #if defined (VI_MODE)
2643 if (rl_editing_mode == vi_mode)
2645 #endif /* VI_MODE */
2649 /* Make C be the next command to be executed. */
2653 rl_pending_input = c;
2656 /* **************************************************************** */
2658 /* The Mark and the Region. */
2660 /* **************************************************************** */
2662 /* Set the mark at POSITION. */
2663 rl_set_mark (position)
2666 if (position > rl_end)
2672 /* Exchange the position of mark and point. */
2673 rl_exchange_mark_and_point ()
2675 if (rl_mark > rl_end)
2685 int temp = rl_point;
2693 /* **************************************************************** */
2695 /* Killing Mechanism */
2697 /* **************************************************************** */
2699 /* What we assume for a max number of kills. */
2700 #define DEFAULT_MAX_KILLS 10
2702 /* The real variable to look at to find out when to flush kills. */
2703 int rl_max_kills = DEFAULT_MAX_KILLS;
2705 /* Where to store killed text. */
2706 char **rl_kill_ring = (char **)NULL;
2708 /* Where we are in the kill ring. */
2709 int rl_kill_index = 0;
2711 /* How many slots we have in the kill ring. */
2712 int rl_kill_ring_length = 0;
2714 /* How to say that you only want to save a certain amount
2715 of kill material. */
2716 rl_set_retained_kills (num)
2720 /* The way to kill something. This appends or prepends to the last
2721 kill, if the last command was a kill command. if FROM is less
2722 than TO, then the text is appended, otherwise prepended. If the
2723 last command was not a kill command, then a new slot is made for
2725 rl_kill_text (from, to)
2729 char *text = rl_copy_text (from, to);
2731 /* Is there anything to kill? */
2735 last_command_was_kill++;
2739 /* Delete the copied text from the line. */
2740 rl_delete_text (from, to);
2742 /* First, find the slot to work with. */
2743 if (!last_command_was_kill)
2745 /* Get a new slot. */
2748 /* If we don't have any defined, then make one. */
2749 rl_kill_ring = (char **)
2750 xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
2755 /* We have to add a new slot on the end, unless we have
2756 exceeded the max limit for remembering kills. */
2757 slot = rl_kill_ring_length;
2758 if (slot == rl_max_kills)
2761 free (rl_kill_ring[0]);
2762 for (i = 0; i < slot; i++)
2763 rl_kill_ring[i] = rl_kill_ring[i + 1];
2769 xrealloc (rl_kill_ring,
2770 ((slot = (rl_kill_ring_length += 1)) + 1)
2778 slot = rl_kill_ring_length - 1;
2781 /* If the last command was a kill, prepend or append. */
2782 if (last_command_was_kill && rl_editing_mode != vi_mode)
2784 char *old = rl_kill_ring[slot];
2785 char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
2799 rl_kill_ring[slot] = new;
2803 rl_kill_ring[slot] = text;
2805 rl_kill_index = slot;
2806 last_command_was_kill++;
2809 /* Now REMEMBER! In order to do prepending or appending correctly, kill
2810 commands always make rl_point's original position be the FROM argument,
2811 and rl_point's extent be the TO argument. */
2813 /* **************************************************************** */
2815 /* Killing Commands */
2817 /* **************************************************************** */
2819 /* Delete the word at point, saving the text in the kill ring. */
2820 rl_kill_word (count)
2823 int orig_point = rl_point;
2826 rl_backward_kill_word (-count);
2829 rl_forward_word (count);
2831 if (rl_point != orig_point)
2832 rl_kill_text (orig_point, rl_point);
2834 rl_point = orig_point;
2838 /* Rubout the word before point, placing it on the kill ring. */
2839 rl_backward_kill_word (count)
2842 int orig_point = rl_point;
2845 rl_kill_word (-count);
2848 rl_backward_word (count);
2850 if (rl_point != orig_point)
2851 rl_kill_text (orig_point, rl_point);
2855 /* Kill from here to the end of the line. If DIRECTION is negative, kill
2856 back to the line start instead. */
2857 rl_kill_line (direction)
2860 int orig_point = rl_point;
2863 rl_backward_kill_line (1);
2867 if (orig_point != rl_point)
2868 rl_kill_text (orig_point, rl_point);
2869 rl_point = orig_point;
2873 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
2874 forwards to the line end instead. */
2875 rl_backward_kill_line (direction)
2878 int orig_point = rl_point;
2889 rl_kill_text (orig_point, rl_point);
2894 /* Yank back the last killed text. This ignores arguments. */
2900 rl_set_mark (rl_point);
2901 rl_insert_text (rl_kill_ring[rl_kill_index]);
2904 /* If the last command was yank, or yank_pop, and the text just
2905 before point is identical to the current kill item, then
2906 delete that text from the line, rotate the index down, and
2907 yank back some other text. */
2912 if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
2918 l = strlen (rl_kill_ring[rl_kill_index]);
2919 if (((rl_point - l) >= 0) &&
2920 (strncmp (the_line + (rl_point - l),
2921 rl_kill_ring[rl_kill_index], l) == 0))
2923 rl_delete_text ((rl_point - l), rl_point);
2926 if (rl_kill_index < 0)
2927 rl_kill_index = rl_kill_ring_length - 1;
2935 /* Yank the COUNTth argument from the previous history line. */
2936 rl_yank_nth_arg (count, ignore)
2939 register HIST_ENTRY *entry = previous_history ();
2950 arg = history_arg_extract (count, count, entry->line);
2957 rl_begin_undo_group ();
2959 #if defined (VI_MODE)
2960 /* Vi mode always inserts a space before yanking the argument, and it
2961 inserts it right *after* rl_point. */
2962 if (rl_editing_mode == vi_mode)
2964 #endif /* VI_MODE */
2966 if (rl_point && the_line[rl_point - 1] != ' ')
2967 rl_insert_text (" ");
2969 rl_insert_text (arg);
2972 rl_end_undo_group ();
2975 /* How to toggle back and forth between editing modes. */
2976 rl_vi_editing_mode ()
2978 #if defined (VI_MODE)
2979 rl_editing_mode = vi_mode;
2980 rl_vi_insertion_mode ();
2981 #endif /* VI_MODE */
2984 rl_emacs_editing_mode ()
2986 rl_editing_mode = emacs_mode;
2987 _rl_keymap = emacs_standard_keymap;
2991 /* **************************************************************** */
2993 /* USG (System V) Support */
2995 /* **************************************************************** */
3006 return (getkey () & 0x7f);
3007 #endif /* __GO32__ */
3011 result = read (fileno (stream), &c, sizeof (unsigned char));
3013 if (result == sizeof (unsigned char))
3016 /* If zero characters are returned, then the file that we are
3017 reading from is empty! Return EOF in that case. */
3021 #if defined (EWOULDBLOCK)
3022 if (errno == EWOULDBLOCK)
3026 if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3028 if (flags & O_NDELAY)
3031 fcntl (fileno (stream), F_SETFL, flags);
3036 #endif /* EWOULDBLOCK */
3038 #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
3039 if (errno == EAGAIN)
3043 if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3045 if (flags & O_NONBLOCK)
3047 flags &= ~O_NONBLOCK;
3048 fcntl (fileno (stream), F_SETFL, flags);
3052 #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
3055 /* If the error that we received was SIGINT, then try again,
3056 this is simply an interrupted system call to read ().
3057 Otherwise, some error ocurred, also signifying EOF. */
3060 #endif /* !__GO32__ */
3065 _rl_savestring (str)
3068 char *copy = (char*) xmalloc (strlen (str) + 1);
3073 #if defined (STATIC_MALLOC)
3075 /* **************************************************************** */
3077 /* xmalloc and xrealloc () */
3079 /* **************************************************************** */
3081 static void memory_error_and_abort ();
3087 char *temp = (char *)malloc (bytes);
3090 memory_error_and_abort ();
3095 xrealloc (pointer, bytes)
3102 temp = (char *)malloc (bytes);
3104 temp = (char *)realloc (pointer, bytes);
3107 memory_error_and_abort ();
3113 memory_error_and_abort ()
3115 fprintf (stderr, "readline: Out of virtual memory!\n");
3118 #endif /* STATIC_MALLOC */
3121 /* **************************************************************** */
3123 /* Testing Readline */
3125 /* **************************************************************** */
3131 HIST_ENTRY **history_list ();
3132 char *temp = (char *)NULL;
3133 char *prompt = "readline% ";
3138 temp = readline (prompt);
3144 /* If there is anything on the line, print it and remember it. */
3147 fprintf (stderr, "%s\r\n", temp);
3151 /* Check for `command' that we handle. */
3152 if (strcmp (temp, "quit") == 0)
3155 if (strcmp (temp, "list") == 0)
3157 HIST_ENTRY **list = history_list ();
3161 for (i = 0; list[i]; i++)
3163 fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
3164 free (list[i]->line);
3178 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"