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 void rl_vi_set_last ();
68 extern Function *rl_function_of_keyseq ();
69 extern char *tilde_expand ();
71 /* External redisplay functions and variables from display.c */
72 extern void rl_redisplay ();
73 extern void _rl_move_vert ();
75 extern void _rl_erase_at_end_of_line ();
76 extern void _rl_move_cursor_relative ();
78 extern int _rl_vis_botlin;
79 extern int _rl_last_c_pos;
80 extern int rl_display_fixed;
82 /* Variables imported from complete.c. */
83 extern char *rl_completer_word_break_characters;
84 extern char *rl_basic_word_break_characters;
85 extern Function *rl_symbolic_link_hook;
86 extern int rl_completion_query_items;
87 extern int rl_complete_with_tilde_expansion;
89 /* Forward declarations used in this file. */
91 void free_history_entry ();
92 int _rl_output_character_function ();
93 void _rl_set_screen_size ();
94 void free_undo_list (), rl_add_undo ();
96 #if !defined (__GO32__)
97 static void readline_default_bindings ();
98 #endif /* !__GO32__ */
100 #if defined (__GO32__)
102 # undef HANDLE_SIGNALS
103 #endif /* __GO32__ */
105 #if defined (STATIC_MALLOC)
106 static char *xmalloc (), *xrealloc ();
108 extern char *xmalloc (), *xrealloc ();
109 #endif /* STATIC_MALLOC */
112 /* **************************************************************** */
114 /* Line editing input utility */
116 /* **************************************************************** */
118 static char *LibraryVersion = "2.0 (Cygnus)";
120 /* A pointer to the keymap that is currently in use.
121 By default, it is the standard emacs keymap. */
122 Keymap _rl_keymap = emacs_standard_keymap;
124 /* The current style of editing. */
125 int rl_editing_mode = emacs_mode;
127 /* Non-zero if the previous command was a kill command. */
128 static int last_command_was_kill = 0;
130 /* The current value of the numeric argument specified by the user. */
131 int rl_numeric_arg = 1;
133 /* Non-zero if an argument was typed. */
134 int rl_explicit_arg = 0;
136 /* Temporary value used while generating the argument. */
139 /* Non-zero means we have been called at least once before. */
140 static int rl_initialized = 0;
142 /* If non-zero, this program is running in an EMACS buffer. */
143 static char *running_in_emacs = (char *)NULL;
145 /* The current offset in the current input line. */
148 /* Mark in the current input line. */
151 /* Length of the current input line. */
154 /* Make this non-zero to return the current input_line. */
157 /* The last function executed by readline. */
158 Function *rl_last_func = (Function *)NULL;
160 /* Top level environment for readline_internal (). */
161 static jmp_buf readline_top_level;
163 /* The streams we interact with. */
164 static FILE *in_stream, *out_stream;
166 /* The names of the streams that we do input and output to. */
167 FILE *rl_instream = (FILE *)NULL;
168 FILE *rl_outstream = (FILE *)NULL;
170 /* Non-zero means echo characters as they are read. */
171 int readline_echoing_p = 1;
173 /* Current prompt. */
176 /* The number of characters read in order to type this complete command. */
177 int rl_key_sequence_length = 0;
179 /* If non-zero, then this is the address of a function to call just
180 before readline_internal () prints the first prompt. */
181 Function *rl_startup_hook = (Function *)NULL;
183 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
184 static char *the_line;
186 /* The character that can generate an EOF. Really read from
187 the terminal driver... just defaulted here. */
188 int _rl_eof_char = CTRL ('D');
190 /* Non-zero makes this the next keystroke to read. */
191 int rl_pending_input = 0;
193 /* Pointer to a useful terminal name. */
194 char *rl_terminal_name = (char *)NULL;
196 /* Non-zero means to always use horizontal scrolling in line display. */
197 int _rl_horizontal_scroll_mode = 0;
199 /* Non-zero means to display an asterisk at the starts of history lines
200 which have been modified. */
201 int _rl_mark_modified_lines = 0;
203 /* Non-zero means to use a visible bell if one is available rather than
204 simply ringing the terminal bell. */
205 int _rl_prefer_visible_bell = 0;
207 /* Line buffer and maintenence. */
208 char *rl_line_buffer = (char *)NULL;
209 int rl_line_buffer_len = 0;
210 #define DEFAULT_BUFFER_SIZE 256
213 /* **************************************************************** */
215 /* `Forward' declarations */
217 /* **************************************************************** */
219 /* Non-zero means do not parse any lines other than comments and
220 parser directives. */
221 unsigned char _rl_parsing_conditionalized_out = 0;
223 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
224 static int defining_kbd_macro = 0;
226 /* Non-zero means to convert characters with the meta bit set to
227 escape-prefixed characters so we can indirect through
228 emacs_meta_keymap or vi_escape_keymap. */
229 int _rl_convert_meta_chars_to_ascii = 1;
231 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
233 static int doing_an_undo = 0;
235 /* **************************************************************** */
237 /* Top Level Functions */
239 /* **************************************************************** */
241 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
242 int _rl_meta_flag = 0; /* Forward declaration */
244 /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
245 none. A return value of NULL means that EOF was encountered. */
250 char *readline_internal ();
255 /* If we are at EOF return a NULL string. */
256 if (rl_pending_input == EOF)
258 rl_pending_input = 0;
259 return ((char *)NULL);
263 rl_prep_terminal (_rl_meta_flag);
265 #if defined (HANDLE_SIGNALS)
269 value = readline_internal ();
270 rl_deprep_terminal ();
272 #if defined (HANDLE_SIGNALS)
279 /* Read a line of input from the global rl_instream, doing output on
280 the global rl_outstream.
281 If rl_prompt is non-null, then that is our prompt. */
285 int lastc, c, eof_found;
287 in_stream = rl_instream;
288 out_stream = rl_outstream;
294 (*rl_startup_hook) ();
296 if (!readline_echoing_p)
300 fprintf (out_stream, "%s", rl_prompt);
308 #if defined (VI_MODE)
309 if (rl_editing_mode == vi_mode)
310 rl_vi_insertion_mode ();
316 int lk = last_command_was_kill;
319 code = setjmp (readline_top_level);
324 if (!rl_pending_input)
326 /* Then initialize the argument and number of keys read. */
328 rl_key_sequence_length = 0;
333 /* EOF typed to a non-blank line is a <NL>. */
334 if (c == EOF && rl_end)
337 /* The character _rl_eof_char typed to blank line, and not as the
338 previous character is interpreted as EOF. */
339 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
346 rl_dispatch (c, _rl_keymap);
348 /* If there was no change in last_command_was_kill, then no kill
349 has taken place. Note that if input is pending we are reading
350 a prefix command, so nothing has changed yet. */
351 if (!rl_pending_input)
353 if (lk == last_command_was_kill)
354 last_command_was_kill = 0;
357 #if defined (VI_MODE)
358 /* In vi mode, when you exit insert mode, the cursor moves back
359 over the previous character. We explicitly check for that here. */
360 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
368 /* Restore the original of this history line, iff the line that we
369 are editing was originally in the history, AND the line has changed. */
371 HIST_ENTRY *entry = current_history ();
373 if (entry && rl_undo_list)
375 char *temp = savestring (the_line);
377 entry = replace_history_entry (where_history (), the_line,
379 free_history_entry (entry);
381 strcpy (the_line, temp);
386 /* At any rate, it is highly likely that this line has an undo list. Get
394 return (savestring (the_line));
397 /* **************************************************************** */
399 /* Character Input Buffering */
401 /* **************************************************************** */
403 static int pop_index = 0, push_index = 0, ibuffer_len = 511;
404 static unsigned char ibuffer[512];
406 /* Non-null means it is a pointer to a function to run while waiting for
408 Function *rl_event_hook = (Function *)NULL;
410 #define any_typein (push_index != pop_index)
412 /* Add KEY to the buffer of characters to be read. */
419 rl_pending_input = EOF;
421 ibuffer[push_index++] = key;
422 if (push_index >= ibuffer_len)
426 /* Return the amount of space available in the
427 buffer for stuffing characters. */
431 if (pop_index > push_index)
432 return (pop_index - push_index);
434 return (ibuffer_len - (push_index - pop_index));
437 /* Get a key from the buffer of characters to be read.
438 Return the key in KEY.
439 Result is KEY if there was a key, or 0 if there wasn't. */
444 if (push_index == pop_index)
447 *key = ibuffer[pop_index++];
449 if (pop_index >= ibuffer_len)
455 /* Stuff KEY into the *front* of the input buffer.
456 Returns non-zero if successful, zero if there is
457 no space left in the buffer. */
462 if (ibuffer_space ())
466 pop_index = ibuffer_len - 1;
467 ibuffer[pop_index] = key;
473 /* If a character is available to be read, then read it
474 and stuff it into IBUFFER. Otherwise, just return. */
478 #if defined (__GO32__)
488 else if (kbhit () && ibuffer_space ())
489 rl_stuff_char (getkey ());
490 #else /* !__GO32__ */
492 int tty = fileno (in_stream);
493 register int tem, result = -1;
497 #if defined (FIONREAD)
498 result = ioctl (tty, FIONREAD, &chars_avail);
501 #if defined (O_NDELAY)
506 flags = fcntl (tty, F_GETFL, 0);
508 fcntl (tty, F_SETFL, (flags | O_NDELAY));
509 chars_avail = read (tty, &input, 1);
511 fcntl (tty, F_SETFL, flags);
512 if (chars_avail == -1 && errno == EAGAIN)
515 #endif /* O_NDELAY */
517 /* If there's nothing available, don't waste time trying to read
519 if (chars_avail == 0)
522 tem = ibuffer_space ();
524 if (chars_avail > tem)
527 /* One cannot read all of the available input. I can only read a single
528 character at a time, or else programs which require input can be
529 thwarted. If the buffer is larger than one character, I lose.
531 if (tem < ibuffer_len)
536 while (chars_avail--)
537 rl_stuff_char (rl_getc (in_stream));
542 rl_stuff_char (input);
544 #endif /* !__GO32__ */
547 static int next_macro_key ();
548 /* Read a key, including pending input. */
554 rl_key_sequence_length++;
556 if (rl_pending_input)
558 c = rl_pending_input;
559 rl_pending_input = 0;
563 /* If input is coming from a macro, then use that. */
564 if (c = next_macro_key ())
567 /* If the user has an event function, then call it periodically. */
570 while (rl_event_hook && !rl_get_char (&c))
578 if (!rl_get_char (&c))
579 c = rl_getc (in_stream);
586 /* Found later in this file. */
587 static void add_macro_char (), with_macro_input ();
589 /* Do the command associated with KEY in MAP.
590 If the associated command is really a keymap, then read
591 another key, and dispatch into that map. */
593 rl_dispatch (key, map)
597 #if defined (VI_MODE)
598 extern int _rl_vi_last_command, _rl_vi_last_repeat, _rl_vi_last_arg_sign;
601 if (defining_kbd_macro)
602 add_macro_char (key);
604 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
606 if (map[ESC].type == ISKMAP)
609 map = (Keymap)((int)map[ESC].function);
611 map = (Keymap)map[ESC].function;
614 rl_key_sequence_length += 2;
615 rl_dispatch (key, map);
622 switch (map[key].type)
626 Function *func = map[key].function;
628 if (func != (Function *)NULL)
630 /* Special case rl_do_lowercase_version (). */
631 if (func == rl_do_lowercase_version)
633 rl_dispatch (to_lower (key), map);
637 (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
639 /* If we have input pending, then the last command was a prefix
640 command. Don't change the state of rl_last_func. Otherwise,
641 remember the last command executed in this variable. */
642 if (!rl_pending_input)
643 rl_last_func = map[key].function;
654 if (map[key].function != (Function *)NULL)
658 rl_key_sequence_length++;
659 newkey = rl_read_key ();
661 /* If you cast map[key].function to type (Keymap) on a Cray,
662 the compiler takes the value of may[key].function and
663 divides it by 4 to convert between pointer types (pointers
664 to functions and pointers to structs are different sizes).
665 This is not what is wanted. */
666 rl_dispatch (newkey, (Keymap)((int)map[key].function));
668 rl_dispatch (newkey, (Keymap)map[key].function);
679 if (map[key].function != (Function *)NULL)
683 macro = savestring ((char *)map[key].function);
684 with_macro_input (macro);
689 #if defined (VI_MODE)
690 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
691 rl_vi_textmod_command (key))
693 _rl_vi_last_command = key;
694 _rl_vi_last_repeat = rl_numeric_arg;
695 _rl_vi_last_arg_sign = rl_arg_sign;
701 /* **************************************************************** */
703 /* Hacking Keyboard Macros */
705 /* **************************************************************** */
707 /* The currently executing macro string. If this is non-zero,
708 then it is a malloc ()'ed string where input is coming from. */
709 static char *executing_macro = (char *)NULL;
711 /* The offset in the above string to the next character to be read. */
712 static int executing_macro_index = 0;
714 /* The current macro string being built. Characters get stuffed
715 in here by add_macro_char (). */
716 static char *current_macro = (char *)NULL;
718 /* The size of the buffer allocated to current_macro. */
719 static int current_macro_size = 0;
721 /* The index at which characters are being added to current_macro. */
722 static int current_macro_index = 0;
724 /* A structure used to save nested macro strings.
725 It is a linked list of string/index for each saved macro. */
727 struct saved_macro *next;
732 /* The list of saved macros. */
733 struct saved_macro *macro_list = (struct saved_macro *)NULL;
735 /* Forward declarations of static functions. Thank you C. */
736 static void push_executing_macro (), pop_executing_macro ();
738 /* This one has to be declared earlier in the file. */
739 /* static void add_macro_char (); */
741 /* Set up to read subsequent input from STRING.
742 STRING is free ()'ed when we are done with it. */
744 with_macro_input (string)
747 push_executing_macro ();
748 executing_macro = string;
749 executing_macro_index = 0;
752 /* Return the next character available from a macro, or 0 if
753 there are no macro characters. */
757 if (!executing_macro)
760 if (!executing_macro[executing_macro_index])
762 pop_executing_macro ();
763 return (next_macro_key ());
766 return (executing_macro[executing_macro_index++]);
769 /* Save the currently executing macro on a stack of saved macros. */
771 push_executing_macro ()
773 struct saved_macro *saver;
775 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
776 saver->next = macro_list;
777 saver->index = executing_macro_index;
778 saver->string = executing_macro;
783 /* Discard the current macro, replacing it with the one
784 on the top of the stack of saved macros. */
786 pop_executing_macro ()
789 free (executing_macro);
791 executing_macro = (char *)NULL;
792 executing_macro_index = 0;
796 struct saved_macro *disposer = macro_list;
797 executing_macro = macro_list->string;
798 executing_macro_index = macro_list->index;
799 macro_list = macro_list->next;
804 /* Add a character to the macro being built. */
809 if (current_macro_index + 1 >= current_macro_size)
812 current_macro = (char *)xmalloc (current_macro_size = 25);
815 (char *)xrealloc (current_macro, current_macro_size += 25);
818 current_macro[current_macro_index++] = c;
819 current_macro[current_macro_index] = '\0';
822 /* Begin defining a keyboard macro.
823 Keystrokes are recorded as they are executed.
824 End the definition with rl_end_kbd_macro ().
825 If a numeric argument was explicitly typed, then append this
826 definition to the end of the existing macro, and start by
827 re-executing the existing macro. */
828 rl_start_kbd_macro (ignore1, ignore2)
829 int ignore1, ignore2;
831 if (defining_kbd_macro)
837 with_macro_input (savestring (current_macro));
840 current_macro_index = 0;
842 defining_kbd_macro = 1;
845 /* Stop defining a keyboard macro.
846 A numeric argument says to execute the macro right now,
847 that many times, counting the definition as the first time. */
848 rl_end_kbd_macro (count, ignore)
851 if (!defining_kbd_macro)
854 current_macro_index -= (rl_key_sequence_length - 1);
855 current_macro[current_macro_index] = '\0';
857 defining_kbd_macro = 0;
859 rl_call_last_kbd_macro (--count, 0);
862 /* Execute the most recently defined keyboard macro.
863 COUNT says how many times to execute it. */
864 rl_call_last_kbd_macro (count, ignore)
870 if (defining_kbd_macro)
872 ding (); /* no recursive macros */
873 current_macro[--current_macro_index] = '\0'; /* erase this char */
878 with_macro_input (savestring (current_macro));
882 _rl_kill_kbd_macro ()
886 free (current_macro);
887 current_macro = (char *) NULL;
889 current_macro_size = current_macro_index = 0;
893 free (executing_macro);
894 executing_macro = (char *) NULL;
896 executing_macro_index = 0;
898 defining_kbd_macro = 0;
902 /* **************************************************************** */
904 /* Initializations */
906 /* **************************************************************** */
908 /* Initliaze readline (and terminal if not already). */
911 /* If we have never been called before, initialize the
912 terminal and data structures. */
915 readline_initialize_everything ();
919 /* Initalize the current line information. */
920 rl_point = rl_end = 0;
921 the_line = rl_line_buffer;
924 /* We aren't done yet. We haven't even gotten started yet! */
927 /* Tell the history routines what is going on. */
928 start_using_history ();
930 /* Make the display buffer match the state of the line. */
931 rl_reset_line_state ();
933 /* No such function typed yet. */
934 rl_last_func = (Function *)NULL;
936 /* Parsing of key-bindings begins in an enabled state. */
937 _rl_parsing_conditionalized_out = 0;
940 /* Initialize the entire state of the world. */
941 readline_initialize_everything ()
943 /* Find out if we are running in Emacs. */
944 running_in_emacs = getenv ("EMACS");
946 /* Set up input and output if they are not already set up. */
951 rl_outstream = stdout;
953 /* Bind in_stream and out_stream immediately. These values may change,
954 but they may also be used before readline_internal () is called. */
955 in_stream = rl_instream;
956 out_stream = rl_outstream;
958 /* Allocate data structures. */
961 (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
963 /* Initialize the terminal interface. */
964 init_terminal_io ((char *)NULL);
966 #if !defined (__GO32__)
967 /* Bind tty characters to readline functions. */
968 readline_default_bindings ();
969 #endif /* !__GO32__ */
971 /* Initialize the function names. */
972 rl_initialize_funmap ();
974 /* Read in the init file. */
975 rl_read_init_file ((char *)NULL);
977 /* If the completion parser's default word break characters haven't
978 been set yet, then do so now. */
980 if (rl_completer_word_break_characters == (char *)NULL)
981 rl_completer_word_break_characters = rl_basic_word_break_characters;
985 /* If this system allows us to look at the values of the regular
986 input editing characters, then bind them to their readline
987 equivalents, iff the characters are not bound to keymaps. */
988 #if !defined (__GO32__)
990 readline_default_bindings ()
992 rltty_set_default_bindings (_rl_keymap);
994 #endif /* !__GO32__ */
997 /* **************************************************************** */
999 /* Numeric Arguments */
1001 /* **************************************************************** */
1003 /* Handle C-u style numeric args, as well as M--, and M-digits. */
1005 /* Add the current digit to the argument in progress. */
1006 rl_digit_argument (ignore, key)
1009 rl_pending_input = key;
1013 /* What to do when you abort reading an argument. */
1014 rl_discard_argument ()
1017 rl_clear_message ();
1018 rl_init_argument ();
1021 /* Create a default argument. */
1024 rl_numeric_arg = rl_arg_sign = 1;
1025 rl_explicit_arg = 0;
1028 /* C-u, universal argument. Multiply the current argument by 4.
1029 Read a key. If the key has nothing to do with arguments, then
1030 dispatch on it. If the key is the abort character then abort. */
1031 rl_universal_argument ()
1033 rl_numeric_arg *= 4;
1042 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
1043 key = c = rl_read_key ();
1045 if (_rl_keymap[c].type == ISFUNC &&
1046 _rl_keymap[c].function == rl_universal_argument)
1048 rl_numeric_arg *= 4;
1054 if (rl_explicit_arg)
1055 rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
1057 rl_numeric_arg = (c - '0');
1058 rl_explicit_arg = 1;
1062 if (c == '-' && !rl_explicit_arg)
1069 rl_clear_message ();
1070 rl_dispatch (key, _rl_keymap);
1077 /* **************************************************************** */
1079 /* Terminal and Termcap */
1081 /* **************************************************************** */
1083 static char *term_buffer = (char *)NULL;
1084 static char *term_string_buffer = (char *)NULL;
1086 /* Non-zero means this terminal can't really do anything. */
1088 /* On Solaris2, sys/types.h #includes sys/reg.h, which #defines PC.
1089 Unfortunately, PC is a global variable used by the termcap library. */
1092 #if !defined (__linux__)
1095 #endif /* __linux__ */
1097 /* Some strings to control terminal actions. These are output by tputs (). */
1098 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1100 int screenwidth, screenheight;
1102 /* Non-zero if we determine that the terminal can do character insertion. */
1103 int terminal_can_insert = 0;
1105 /* How to insert characters. */
1106 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1108 /* How to delete characters. */
1109 char *term_dc, *term_DC;
1111 #if defined (HACK_TERMCAP_MOTION)
1112 char *term_forward_char;
1113 #endif /* HACK_TERMCAP_MOTION */
1115 /* How to go up a line. */
1118 /* True if we have funny auto-line-wrap ("am" and "xn"). */
1121 /* A visible bell, if the terminal can be made to flash the screen. */
1124 /* Non-zero means that this terminal has a meta key. */
1127 /* The string to write to turn on the meta key, if this term has one. */
1130 /* The string to write to turn off the meta key, if this term has one. */
1133 /* The key sequences output by the arrow keys, if this terminal has any. */
1134 char *term_ku, *term_kd, *term_kr, *term_kl;
1136 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1138 rl_reset_terminal (terminal_name)
1139 char *terminal_name;
1141 init_terminal_io (terminal_name);
1144 /* Set readline's idea of the screen size. TTY is a file descriptor open
1145 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
1146 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
1147 non-null serve to check whether or not we have initialized termcap. */
1149 _rl_set_screen_size (tty, ignore_env)
1150 int tty, ignore_env;
1152 #if defined (TIOCGWINSZ) && !defined (TIOCGWINSZ_BROKEN)
1153 struct winsize window_size;
1154 #endif /* TIOCGWINSZ */
1156 #if defined (TIOCGWINSZ) && !defined (TIOCGWINSZ_BROKEN)
1157 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
1159 screenwidth = (int) window_size.ws_col;
1160 screenheight = (int) window_size.ws_row;
1162 #endif /* TIOCGWINSZ */
1164 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
1166 if (screenwidth <= 0)
1170 if (!ignore_env && (sw = getenv ("COLUMNS")))
1171 screenwidth = atoi (sw);
1173 if (screenwidth <= 0 && term_string_buffer)
1174 screenwidth = tgetnum ("co");
1177 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
1179 if (screenheight <= 0)
1183 if (!ignore_env && (sh = getenv ("LINES")))
1184 screenheight = atoi (sh);
1186 if (screenheight <= 0 && term_string_buffer)
1187 screenheight = tgetnum ("li");
1190 /* If all else fails, default to 80x24 terminal. */
1191 if (screenwidth <= 0)
1194 if (screenheight <= 0)
1198 /* If we're being compiled as part of bash, set the environment
1199 variables $LINES and $COLUMNS to new values. */
1200 set_lines_and_columns (screenheight, screenwidth);
1203 /* If we don't have xn (most modern terminals do),
1204 don't use the last column. */
1209 init_terminal_io (terminal_name)
1210 char *terminal_name;
1212 #if defined (__GO32__)
1213 screenwidth = ScreenCols ();
1214 screenheight = ScreenRows ();
1216 term_im = term_ei = term_ic = term_IC = (char *)NULL;
1217 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1219 /* Does the __GO32__ have a meta key? I don't know. */
1221 term_mm = term_mo = (char *)NULL;
1223 /* It probably has arrow keys, but I don't know what they are. */
1224 term_ku = term_kd = term_kr = term_kl = (char *)NULL;
1226 #if defined (HACK_TERMCAP_MOTION)
1227 term_forward_char = (char *)NULL;
1228 #endif /* HACK_TERMCAP_MOTION */
1229 terminal_can_insert = term_xn = 0;
1231 #else /* !__GO32__ */
1233 char *term, *buffer;
1236 term = terminal_name ? terminal_name : getenv ("TERM");
1238 if (!term_string_buffer)
1239 term_string_buffer = (char *)xmalloc (2048);
1242 term_buffer = (char *)xmalloc (2048);
1244 buffer = term_string_buffer;
1246 term_clrpag = term_cr = term_clreol = (char *)NULL;
1251 if (tgetent (term_buffer, term) <= 0)
1257 term_im = term_ei = term_ic = term_IC = (char *)NULL;
1258 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1259 term_ku = term_kd = term_kl = term_kr = (char *)NULL;
1260 #if defined (HACK_TERMCAP_MOTION)
1261 term_forward_char = (char *)NULL;
1263 terminal_can_insert = term_xn = 0;
1267 BC = tgetstr ("pc", &buffer);
1268 PC = buffer ? *buffer : 0;
1270 term_backspace = tgetstr ("le", &buffer);
1272 term_cr = tgetstr ("cr", &buffer);
1273 term_clreol = tgetstr ("ce", &buffer);
1274 term_clrpag = tgetstr ("cl", &buffer);
1279 #if defined (HACK_TERMCAP_MOTION)
1280 term_forward_char = tgetstr ("nd", &buffer);
1281 #endif /* HACK_TERMCAP_MOTION */
1284 tty = fileno (rl_instream);
1288 screenwidth = screenheight = 0;
1290 term_xn = tgetflag ("am") && tgetflag ("xn");
1292 _rl_set_screen_size (tty, 0);
1294 term_im = tgetstr ("im", &buffer);
1295 term_ei = tgetstr ("ei", &buffer);
1296 term_IC = tgetstr ("IC", &buffer);
1297 term_ic = tgetstr ("ic", &buffer);
1299 /* "An application program can assume that the terminal can do
1300 character insertion if *any one of* the capabilities `IC',
1301 `im', `ic' or `ip' is provided." But we can't do anything if
1302 only `ip' is provided, so... */
1303 terminal_can_insert = (term_IC || term_im || term_ic);
1305 term_up = tgetstr ("up", &buffer);
1306 term_dc = tgetstr ("dc", &buffer);
1307 term_DC = tgetstr ("DC", &buffer);
1309 visible_bell = tgetstr ("vb", &buffer);
1311 /* Check to see if this terminal has a meta key. */
1312 term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
1315 term_mm = tgetstr ("mm", &buffer);
1316 term_mo = tgetstr ("mo", &buffer);
1320 term_mm = (char *)NULL;
1321 term_mo = (char *)NULL;
1324 /* Attempt to find and bind the arrow keys. Do not override already
1325 bound keys in an overzealous attempt, however. */
1326 term_ku = tgetstr ("ku", &buffer);
1327 term_kd = tgetstr ("kd", &buffer);
1328 term_kr = tgetstr ("kr", &buffer);
1329 term_kl = tgetstr ("kl", &buffer);
1335 func = rl_function_of_keyseq (term_ku, _rl_keymap, (int *)NULL);
1337 if (!func || func == rl_do_lowercase_version)
1338 rl_set_key (term_ku, rl_get_previous_history, _rl_keymap);
1345 func = rl_function_of_keyseq (term_kd, _rl_keymap, (int *)NULL);
1347 if (!func || func == rl_do_lowercase_version)
1348 rl_set_key (term_kd, rl_get_next_history, _rl_keymap);
1355 func = rl_function_of_keyseq (term_kr, _rl_keymap, (int *)NULL);
1357 if (!func || func == rl_do_lowercase_version)
1358 rl_set_key (term_kr, rl_forward, _rl_keymap);
1365 func = rl_function_of_keyseq (term_kl, _rl_keymap, (int *)NULL);
1367 if (!func || func == rl_do_lowercase_version)
1368 rl_set_key (term_kl, rl_backward, _rl_keymap);
1370 #endif /* !__GO32__ */
1374 /* A function for the use of tputs () */
1376 _rl_output_character_function (c)
1379 return putc (c, out_stream);
1382 /* Write COUNT characters from STRING to the output stream. */
1384 _rl_output_some_chars (string, count)
1388 fwrite (string, 1, count, out_stream);
1391 /* Move the cursor back. */
1397 #if !defined (__GO32__)
1399 for (i = 0; i < count; i++)
1400 tputs (term_backspace, 1, _rl_output_character_function);
1402 #endif /* !__GO32__ */
1403 for (i = 0; i < count; i++)
1404 putc ('\b', out_stream);
1407 /* Move to the start of the next line. */
1410 #if defined (NEW_TTY_DRIVER)
1411 tputs (term_cr, 1, _rl_output_character_function);
1412 #endif /* NEW_TTY_DRIVER */
1413 putc ('\n', out_stream);
1417 /* **************************************************************** */
1419 /* Utility Functions */
1421 /* **************************************************************** */
1423 /* Return 0 if C is not a member of the class of characters that belong
1424 in words, or 1 if it is. */
1426 int allow_pathname_alphabetic_chars = 0;
1427 char *pathname_alphabetic_chars = "/-_=~.#$";
1433 if (pure_alphabetic (c) || (numeric (c)))
1436 if (allow_pathname_alphabetic_chars)
1437 return (strchr (pathname_alphabetic_chars, c) != NULL);
1442 /* Return non-zero if C is a numeric character. */
1447 return (c >= '0' && c <= '9');
1450 /* Ring the terminal bell. */
1454 if (readline_echoing_p)
1456 #if !defined (__GO32__)
1457 if (_rl_prefer_visible_bell && visible_bell)
1458 tputs (visible_bell, 1, _rl_output_character_function);
1460 #endif /* !__GO32__ */
1462 fprintf (stderr, "\007");
1469 /* How to abort things. */
1473 rl_clear_message ();
1474 rl_init_argument ();
1475 rl_pending_input = 0;
1477 defining_kbd_macro = 0;
1478 while (executing_macro)
1479 pop_executing_macro ();
1481 rl_last_func = (Function *)NULL;
1482 longjmp (readline_top_level, 1);
1485 /* Return a copy of the string between FROM and TO.
1486 FROM is inclusive, TO is not. */
1488 rl_copy_text (from, to)
1491 register int length;
1494 /* Fix it if the caller is confused. */
1503 copy = (char *)xmalloc (1 + length);
1504 strncpy (copy, the_line + from, length);
1505 copy[length] = '\0';
1509 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
1512 rl_extend_line_buffer (len)
1515 while (len >= rl_line_buffer_len)
1518 (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
1520 the_line = rl_line_buffer;
1524 /* **************************************************************** */
1526 /* Insert and Delete */
1528 /* **************************************************************** */
1530 /* Insert a string of text into the line at point. This is the only
1531 way that you should do insertion. rl_insert () calls this
1533 rl_insert_text (string)
1536 register int i, l = strlen (string);
1538 if (rl_end + l >= rl_line_buffer_len)
1539 rl_extend_line_buffer (rl_end + l);
1541 for (i = rl_end; i >= rl_point; i--)
1542 the_line[i + l] = the_line[i];
1543 strncpy (the_line + rl_point, string, l);
1545 /* Remember how to undo this if we aren't undoing something. */
1548 /* If possible and desirable, concatenate the undos. */
1549 if ((strlen (string) == 1) &&
1551 (rl_undo_list->what == UNDO_INSERT) &&
1552 (rl_undo_list->end == rl_point) &&
1553 (rl_undo_list->end - rl_undo_list->start < 20))
1554 rl_undo_list->end++;
1556 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
1560 the_line[rl_end] = '\0';
1563 /* Delete the string between FROM and TO. FROM is
1564 inclusive, TO is not. */
1565 rl_delete_text (from, to)
1568 register char *text;
1570 /* Fix it if the caller is confused. */
1577 text = rl_copy_text (from, to);
1578 strncpy (the_line + from, the_line + to, rl_end - to);
1580 /* Remember how to undo this delete. */
1582 rl_add_undo (UNDO_DELETE, from, to, text);
1586 rl_end -= (to - from);
1587 the_line[rl_end] = '\0';
1591 /* **************************************************************** */
1593 /* Readline character functions */
1595 /* **************************************************************** */
1597 /* This is not a gap editor, just a stupid line input routine. No hair
1598 is involved in writing any of the functions, and none should be. */
1602 rl_end is the place in the string that we would place '\0';
1603 i.e., it is always safe to place '\0' there.
1605 rl_point is the place in the string where the cursor is. Sometimes
1606 this is the same as rl_end.
1608 Any command that is called interactively receives two arguments.
1609 The first is a count: the numeric arg pased to this command.
1610 The second is the key which invoked this command.
1614 /* **************************************************************** */
1616 /* Movement Commands */
1618 /* **************************************************************** */
1620 /* Note that if you `optimize' the display for these functions, you cannot
1621 use said functions in other functions which do not do optimizing display.
1622 I.e., you will have to update the data base for rl_redisplay, and you
1623 might as well let rl_redisplay do that job. */
1625 /* Move forward COUNT characters. */
1630 rl_backward (-count);
1634 #if defined (VI_MODE)
1635 if (rl_point >= (rl_end - (rl_editing_mode == vi_mode)))
1637 if (rl_point == rl_end)
1638 #endif /* VI_MODE */
1650 /* Move backward COUNT characters. */
1655 rl_forward (-count);
1671 /* Move to the beginning of the line. */
1678 /* Move to the end of the line. */
1685 /* Move forward a word. We do what Emacs does. */
1686 rl_forward_word (count)
1693 rl_backward_word (-count);
1699 if (rl_point == rl_end)
1702 /* If we are not in a word, move forward until we are in one.
1703 Then, move forward until we hit a non-alphabetic character. */
1704 c = the_line[rl_point];
1705 if (!alphabetic (c))
1707 while (++rl_point < rl_end)
1709 c = the_line[rl_point];
1710 if (alphabetic (c)) break;
1713 if (rl_point == rl_end) return;
1714 while (++rl_point < rl_end)
1716 c = the_line[rl_point];
1717 if (!alphabetic (c)) break;
1724 /* Move backward a word. We do what Emacs does. */
1725 rl_backward_word (count)
1732 rl_forward_word (-count);
1741 /* Like rl_forward_word (), except that we look at the characters
1742 just before point. */
1744 c = the_line[rl_point - 1];
1745 if (!alphabetic (c))
1749 c = the_line[rl_point - 1];
1750 if (alphabetic (c)) break;
1756 c = the_line[rl_point - 1];
1757 if (!alphabetic (c))
1766 /* Clear the current line. Numeric argument to C-l does this. */
1769 int curr_line = _rl_last_c_pos / screenwidth;
1771 _rl_move_vert (curr_line);
1772 _rl_move_cursor_relative (0, the_line); /* XXX is this right */
1774 #if defined (__GO32__)
1776 int row, col, width, row_start;
1778 ScreenGetCursor (&row, &col);
1779 width = ScreenCols ();
1780 row_start = ScreenPrimary + (row * width);
1781 memset (row_start + col, 0, (width - col) * 2);
1783 #else /* !__GO32__ */
1785 tputs (term_clreol, 1, _rl_output_character_function);
1786 #endif /* !__GO32__ */
1788 rl_forced_update_display ();
1789 rl_display_fixed = 1;
1794 /* C-l typed to a line without quoting clears the screen, and then reprints
1795 the prompt and the current input line. Given a numeric arg, redraw only
1796 the current line. */
1799 if (rl_explicit_arg)
1805 #if !defined (__GO32__)
1807 tputs (term_clrpag, 1, _rl_output_character_function);
1809 #endif /* !__GO32__ */
1812 rl_forced_update_display ();
1813 rl_display_fixed = 1;
1818 rl_arrow_keys (count, c)
1823 ch = rl_read_key ();
1825 switch (to_upper (ch))
1828 rl_get_previous_history (count);
1832 rl_get_next_history (count);
1840 rl_backward (count);
1850 /* **************************************************************** */
1854 /* **************************************************************** */
1856 /* Insert the character C at the current location, moving point forward. */
1857 rl_insert (count, c)
1866 /* If we can optimize, then do it. But don't let people crash
1867 readline because of extra large arguments. */
1868 if (count > 1 && count < 1024)
1870 string = (char *)alloca (1 + count);
1872 for (i = 0; i < count; i++)
1876 rl_insert_text (string);
1884 string = (char *)alloca (1024 + 1);
1886 for (i = 0; i < 1024; i++)
1891 decreaser = (count > 1024 ? 1024 : count);
1892 string[decreaser] = '\0';
1893 rl_insert_text (string);
1899 /* We are inserting a single character.
1900 If there is pending input, then make a string of all of the
1901 pending characters that are bound to rl_insert, and insert
1908 string = (char *)alloca (ibuffer_len + 1);
1911 while ((t = rl_get_char (&key)) &&
1912 (_rl_keymap[key].type == ISFUNC &&
1913 _rl_keymap[key].function == rl_insert))
1917 rl_unget_char (key);
1920 rl_insert_text (string);
1924 /* Inserting a single character. */
1925 string = (char *)alloca (2);
1929 rl_insert_text (string);
1934 /* Insert the next typed character verbatim. */
1935 rl_quoted_insert (count)
1941 return (rl_insert (count, c));
1945 /* Insert a tab character. */
1946 rl_tab_insert (count)
1949 return (rl_insert (count, '\t'));
1952 /* What to do when a NEWLINE is pressed. We accept the whole line.
1953 KEY is the key that invoked this command. I guess it could have
1954 meaning in the future. */
1955 rl_newline (count, key)
1960 #if defined (VI_MODE)
1962 extern int _rl_vi_doing_insert;
1963 if (_rl_vi_doing_insert)
1965 rl_end_undo_group ();
1966 _rl_vi_doing_insert = 0;
1971 #endif /* VI_MODE */
1973 if (readline_echoing_p)
1975 _rl_move_vert (_rl_vis_botlin);
1978 fflush (out_stream);
1984 rl_clean_up_for_exit ()
1986 if (readline_echoing_p)
1988 _rl_move_vert (_rl_vis_botlin);
1990 fflush (out_stream);
1991 rl_restart_output ();
1996 /* What to do for some uppercase characters, like meta characters,
1997 and some characters appearing in emacs_ctlx_keymap. This function
1998 is just a stub, you bind keys to it and the code in rl_dispatch ()
1999 is special cased. */
2000 rl_do_lowercase_version (ignore1, ignore2)
2001 int ignore1, ignore2;
2006 /* Rubout the character behind point. */
2022 if (count > 1 || rl_explicit_arg)
2024 int orig_point = rl_point;
2025 rl_backward (count);
2026 rl_kill_text (orig_point, rl_point);
2030 int c = the_line[--rl_point];
2031 rl_delete_text (rl_point, rl_point + 1);
2033 if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
2036 l = rl_character_len (c, rl_point);
2037 _rl_erase_at_end_of_line (l);
2043 /* Delete the character under the cursor. Given a numeric argument,
2044 kill that many characters instead. */
2045 rl_delete (count, invoking_key)
2046 int count, invoking_key;
2050 return (rl_rubout (-count));
2053 if (rl_point == rl_end)
2059 if (count > 1 || rl_explicit_arg)
2061 int orig_point = rl_point;
2063 rl_kill_text (orig_point, rl_point);
2064 rl_point = orig_point;
2068 return (rl_delete_text (rl_point, rl_point + 1));
2072 /* Delete all spaces and tabs around point. */
2073 rl_delete_horizontal_space (count, ignore)
2076 int start = rl_point;
2078 while (rl_point && whitespace (the_line[rl_point - 1]))
2083 while (rl_point < rl_end && whitespace (the_line[rl_point]))
2086 if (start != rl_point)
2088 rl_delete_text (start, rl_point);
2095 /* **************************************************************** */
2099 /* **************************************************************** */
2101 /* The next two functions mimic unix line editing behaviour, except they
2102 save the deleted text on the kill ring. This is safer than not saving
2103 it, and since we have a ring, nobody should get screwed. */
2105 /* This does what C-w does in Unix. We can't prevent people from
2106 using behaviour that they expect. */
2107 rl_unix_word_rubout ()
2113 int orig_point = rl_point;
2115 while (rl_point && whitespace (the_line[rl_point - 1]))
2118 while (rl_point && !whitespace (the_line[rl_point - 1]))
2121 rl_kill_text (rl_point, orig_point);
2126 /* Here is C-u doing what Unix does. You don't *have* to use these
2127 key-bindings. We have a choice of killing the entire line, or
2128 killing from where we are to the start of the line. We choose the
2129 latter, because if you are a Unix weenie, then you haven't backspaced
2130 into the line at all, and if you aren't, then you know what you are
2132 rl_unix_line_discard ()
2138 rl_kill_text (rl_point, 0);
2145 /* **************************************************************** */
2147 /* Commands For Typos */
2149 /* **************************************************************** */
2151 /* Random and interesting things in here. */
2153 /* **************************************************************** */
2157 /* **************************************************************** */
2159 /* The three kinds of things that we know how to do. */
2164 static int rl_change_case ();
2166 /* Uppercase the word at point. */
2167 rl_upcase_word (count)
2170 return (rl_change_case (count, UpCase));
2173 /* Lowercase the word at point. */
2174 rl_downcase_word (count)
2177 return (rl_change_case (count, DownCase));
2180 /* Upcase the first letter, downcase the rest. */
2181 rl_capitalize_word (count)
2184 return (rl_change_case (count, CapCase));
2187 /* The meaty function.
2188 Change the case of COUNT words, performing OP on them.
2189 OP is one of UpCase, DownCase, or CapCase.
2190 If a negative argument is given, leave point where it started,
2191 otherwise, leave it where it moves to. */
2193 rl_change_case (count, op)
2196 register int start = rl_point, end;
2199 rl_forward_word (count);
2209 /* We are going to modify some text, so let's prepare to undo it. */
2210 rl_modifying (start, end);
2212 for (; start < end; start++)
2217 the_line[start] = to_upper (the_line[start]);
2221 the_line[start] = to_lower (the_line[start]);
2227 the_line[start] = to_upper (the_line[start]);
2232 the_line[start] = to_lower (the_line[start]);
2234 if (!pure_alphabetic (the_line[start]))
2247 /* **************************************************************** */
2251 /* **************************************************************** */
2253 /* Transpose the words at point. */
2254 rl_transpose_words (count)
2257 char *word1, *word2;
2258 int w1_beg, w1_end, w2_beg, w2_end;
2259 int orig_point = rl_point;
2264 /* Find the two words. */
2265 rl_forward_word (count);
2267 rl_backward_word (1);
2269 rl_backward_word (count);
2271 rl_forward_word (1);
2274 /* Do some check to make sure that there really are two words. */
2275 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
2278 rl_point = orig_point;
2282 /* Get the text of the words. */
2283 word1 = rl_copy_text (w1_beg, w1_end);
2284 word2 = rl_copy_text (w2_beg, w2_end);
2286 /* We are about to do many insertions and deletions. Remember them
2287 as one operation. */
2288 rl_begin_undo_group ();
2290 /* Do the stuff at word2 first, so that we don't have to worry
2291 about word1 moving. */
2293 rl_delete_text (w2_beg, w2_end);
2294 rl_insert_text (word1);
2297 rl_delete_text (w1_beg, w1_end);
2298 rl_insert_text (word2);
2300 /* This is exactly correct since the text before this point has not
2301 changed in length. */
2304 /* I think that does it. */
2305 rl_end_undo_group ();
2312 /* Transpose the characters at point. If point is at the end of the line,
2313 then transpose the characters before point. */
2314 rl_transpose_chars (count)
2322 if (!rl_point || rl_end < 2)
2328 rl_begin_undo_group ();
2330 if (rl_point == rl_end)
2337 dummy[0] = the_line[rl_point];
2340 rl_delete_text (rl_point, rl_point + 1);
2343 if (rl_point > rl_end)
2345 else if (rl_point < 0)
2347 rl_insert_text (dummy);
2349 rl_end_undo_group ();
2353 /* **************************************************************** */
2355 /* Undo, and Undoing */
2357 /* **************************************************************** */
2359 /* The current undo list for THE_LINE. */
2360 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
2362 /* Remember how to undo something. Concatenate some undos if that
2365 rl_add_undo (what, start, end, text)
2366 enum undo_code what;
2370 UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
2372 temp->start = start;
2375 temp->next = rl_undo_list;
2376 rl_undo_list = temp;
2379 /* Free the existing undo list. */
2383 while (rl_undo_list)
2385 UNDO_LIST *release = rl_undo_list;
2386 rl_undo_list = rl_undo_list->next;
2388 if (release->what == UNDO_DELETE)
2389 free (release->text);
2393 rl_undo_list = (UNDO_LIST *)NULL;
2396 /* Undo the next thing in the list. Return 0 if there
2397 is nothing to undo, or non-zero if there was. */
2402 int waiting_for_begin = 0;
2410 switch (rl_undo_list->what) {
2412 /* Undoing deletes means inserting some text. */
2414 rl_point = rl_undo_list->start;
2415 rl_insert_text (rl_undo_list->text);
2416 free (rl_undo_list->text);
2419 /* Undoing inserts means deleting some text. */
2421 rl_delete_text (rl_undo_list->start, rl_undo_list->end);
2422 rl_point = rl_undo_list->start;
2425 /* Undoing an END means undoing everything 'til we get to
2428 waiting_for_begin++;
2431 /* Undoing a BEGIN means that we are done with this group. */
2433 if (waiting_for_begin)
2434 waiting_for_begin--;
2446 release = rl_undo_list;
2447 rl_undo_list = rl_undo_list->next;
2450 if (waiting_for_begin)
2456 /* Begin a group. Subsequent undos are undone as an atomic operation. */
2457 rl_begin_undo_group ()
2459 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
2463 /* End an undo group started with rl_begin_undo_group (). */
2464 rl_end_undo_group ()
2466 rl_add_undo (UNDO_END, 0, 0, 0);
2470 /* Save an undo entry for the text from START to END. */
2471 rl_modifying (start, end)
2483 char *temp = rl_copy_text (start, end);
2484 rl_begin_undo_group ();
2485 rl_add_undo (UNDO_DELETE, start, end, temp);
2486 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
2487 rl_end_undo_group ();
2492 /* Revert the current line to its previous state. */
2499 while (rl_undo_list)
2505 /* Do some undoing of things that were done. */
2506 rl_undo_command (count)
2510 return 0; /* Nothing to do. */
2525 /* **************************************************************** */
2527 /* History Utilities */
2529 /* **************************************************************** */
2531 /* We already have a history library, and that is what we use to control
2532 the history features of readline. However, this is our local interface
2533 to the history mechanism. */
2535 /* While we are editing the history, this is the saved
2536 version of the original line. */
2537 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
2539 /* Set the history pointer back to the last entry in the history. */
2540 start_using_history ()
2543 if (saved_line_for_history)
2544 free_history_entry (saved_line_for_history);
2546 saved_line_for_history = (HIST_ENTRY *)NULL;
2550 /* Free the contents (and containing structure) of a HIST_ENTRY. */
2552 free_history_entry (entry)
2562 /* Perhaps put back the current line if it has changed. */
2563 maybe_replace_line ()
2565 HIST_ENTRY *temp = current_history ();
2567 /* If the current line has changed, save the changes. */
2568 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
2570 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
2577 /* Put back the saved_line_for_history if there is one. */
2578 maybe_unsave_line ()
2580 if (saved_line_for_history)
2584 line_len = strlen (saved_line_for_history->line);
2586 if (line_len >= rl_line_buffer_len)
2587 rl_extend_line_buffer (line_len);
2589 strcpy (the_line, saved_line_for_history->line);
2590 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
2591 free_history_entry (saved_line_for_history);
2592 saved_line_for_history = (HIST_ENTRY *)NULL;
2593 rl_end = rl_point = strlen (the_line);
2600 /* Save the current line in saved_line_for_history. */
2603 if (!saved_line_for_history)
2605 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
2606 saved_line_for_history->line = savestring (the_line);
2607 saved_line_for_history->data = (char *)rl_undo_list;
2612 /* **************************************************************** */
2614 /* History Commands */
2616 /* **************************************************************** */
2618 /* Meta-< goes to the start of the history. */
2619 rl_beginning_of_history ()
2621 return (rl_get_previous_history (1 + where_history ()));
2624 /* Meta-> goes to the end of the history. (The current line). */
2625 rl_end_of_history ()
2627 maybe_replace_line ();
2629 maybe_unsave_line ();
2633 /* Move down to the next history line. */
2634 rl_get_next_history (count)
2637 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2640 return (rl_get_previous_history (-count));
2645 maybe_replace_line ();
2649 temp = next_history ();
2656 maybe_unsave_line ();
2661 line_len = strlen (temp->line);
2663 if (line_len >= rl_line_buffer_len)
2664 rl_extend_line_buffer (line_len);
2666 strcpy (the_line, temp->line);
2667 rl_undo_list = (UNDO_LIST *)temp->data;
2668 rl_end = rl_point = strlen (the_line);
2669 #if defined (VI_MODE)
2670 if (rl_editing_mode == vi_mode)
2672 #endif /* VI_MODE */
2677 /* Get the previous item out of our interactive history, making it the current
2678 line. If there is no previous history, just ding. */
2679 rl_get_previous_history (count)
2682 HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
2683 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2686 return (rl_get_next_history (-count));
2691 /* If we don't have a line saved, then save this one. */
2694 /* If the current line has changed, save the changes. */
2695 maybe_replace_line ();
2699 temp = previous_history ();
2707 /* If there was a large argument, and we moved back to the start of the
2708 history, that is not an error. So use the last value found. */
2709 if (!temp && old_temp)
2718 line_len = strlen (temp->line);
2720 if (line_len >= rl_line_buffer_len)
2721 rl_extend_line_buffer (line_len);
2723 strcpy (the_line, temp->line);
2724 rl_undo_list = (UNDO_LIST *)temp->data;
2725 rl_end = rl_point = line_len;
2727 #if defined (VI_MODE)
2728 if (rl_editing_mode == vi_mode)
2730 #endif /* VI_MODE */
2735 /* Make C be the next command to be executed. */
2739 rl_pending_input = c;
2743 /* **************************************************************** */
2745 /* The Mark and the Region. */
2747 /* **************************************************************** */
2749 /* Set the mark at POSITION. */
2750 rl_set_mark (position)
2753 if (position > rl_end)
2760 /* Exchange the position of mark and point. */
2761 rl_exchange_mark_and_point ()
2763 if (rl_mark > rl_end)
2773 int temp = rl_point;
2782 /* **************************************************************** */
2784 /* Killing Mechanism */
2786 /* **************************************************************** */
2788 /* What we assume for a max number of kills. */
2789 #define DEFAULT_MAX_KILLS 10
2791 /* The real variable to look at to find out when to flush kills. */
2792 int rl_max_kills = DEFAULT_MAX_KILLS;
2794 /* Where to store killed text. */
2795 char **rl_kill_ring = (char **)NULL;
2797 /* Where we are in the kill ring. */
2798 int rl_kill_index = 0;
2800 /* How many slots we have in the kill ring. */
2801 int rl_kill_ring_length = 0;
2803 /* How to say that you only want to save a certain amount
2804 of kill material. */
2805 rl_set_retained_kills (num)
2811 /* The way to kill something. This appends or prepends to the last
2812 kill, if the last command was a kill command. if FROM is less
2813 than TO, then the text is appended, otherwise prepended. If the
2814 last command was not a kill command, then a new slot is made for
2816 rl_kill_text (from, to)
2820 char *text = rl_copy_text (from, to);
2822 /* Is there anything to kill? */
2826 last_command_was_kill++;
2830 /* Delete the copied text from the line. */
2831 rl_delete_text (from, to);
2833 /* First, find the slot to work with. */
2834 if (!last_command_was_kill)
2836 /* Get a new slot. */
2839 /* If we don't have any defined, then make one. */
2840 rl_kill_ring = (char **)
2841 xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
2846 /* We have to add a new slot on the end, unless we have
2847 exceeded the max limit for remembering kills. */
2848 slot = rl_kill_ring_length;
2849 if (slot == rl_max_kills)
2852 free (rl_kill_ring[0]);
2853 for (i = 0; i < slot; i++)
2854 rl_kill_ring[i] = rl_kill_ring[i + 1];
2860 xrealloc (rl_kill_ring,
2861 ((slot = (rl_kill_ring_length += 1)) + 1)
2869 slot = rl_kill_ring_length - 1;
2872 /* If the last command was a kill, prepend or append. */
2873 if (last_command_was_kill && rl_editing_mode != vi_mode)
2875 char *old = rl_kill_ring[slot];
2876 char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
2890 rl_kill_ring[slot] = new;
2894 rl_kill_ring[slot] = text;
2896 rl_kill_index = slot;
2897 last_command_was_kill++;
2901 /* Now REMEMBER! In order to do prepending or appending correctly, kill
2902 commands always make rl_point's original position be the FROM argument,
2903 and rl_point's extent be the TO argument. */
2905 /* **************************************************************** */
2907 /* Killing Commands */
2909 /* **************************************************************** */
2911 /* Delete the word at point, saving the text in the kill ring. */
2912 rl_kill_word (count)
2915 int orig_point = rl_point;
2918 return (rl_backward_kill_word (-count));
2921 rl_forward_word (count);
2923 if (rl_point != orig_point)
2924 rl_kill_text (orig_point, rl_point);
2926 rl_point = orig_point;
2931 /* Rubout the word before point, placing it on the kill ring. */
2932 rl_backward_kill_word (count)
2935 int orig_point = rl_point;
2938 return (rl_kill_word (-count));
2941 rl_backward_word (count);
2943 if (rl_point != orig_point)
2944 rl_kill_text (orig_point, rl_point);
2948 /* Kill from here to the end of the line. If DIRECTION is negative, kill
2949 back to the line start instead. */
2950 rl_kill_line (direction)
2953 int orig_point = rl_point;
2956 return (rl_backward_kill_line (1));
2960 if (orig_point != rl_point)
2961 rl_kill_text (orig_point, rl_point);
2962 rl_point = orig_point;
2967 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
2968 forwards to the line end instead. */
2969 rl_backward_kill_line (direction)
2972 int orig_point = rl_point;
2975 return (rl_kill_line (1));
2983 rl_kill_text (orig_point, rl_point);
2989 /* Yank back the last killed text. This ignores arguments. */
2998 rl_set_mark (rl_point);
2999 rl_insert_text (rl_kill_ring[rl_kill_index]);
3003 /* If the last command was yank, or yank_pop, and the text just
3004 before point is identical to the current kill item, then
3005 delete that text from the line, rotate the index down, and
3006 yank back some other text. */
3011 if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
3018 l = strlen (rl_kill_ring[rl_kill_index]);
3019 if (((rl_point - l) >= 0) &&
3020 (strncmp (the_line + (rl_point - l),
3021 rl_kill_ring[rl_kill_index], l) == 0))
3023 rl_delete_text ((rl_point - l), rl_point);
3026 if (rl_kill_index < 0)
3027 rl_kill_index = rl_kill_ring_length - 1;
3038 /* Yank the COUNTth argument from the previous history line. */
3039 rl_yank_nth_arg (count, ignore)
3042 register HIST_ENTRY *entry = previous_history ();
3053 arg = history_arg_extract (count, count, entry->line);
3060 rl_begin_undo_group ();
3062 #if defined (VI_MODE)
3063 /* Vi mode always inserts a space before yanking the argument, and it
3064 inserts it right *after* rl_point. */
3065 if (rl_editing_mode == vi_mode)
3067 #endif /* VI_MODE */
3070 if (rl_point && the_line[rl_point - 1] != ' ')
3071 rl_insert_text (" ");
3074 rl_insert_text (arg);
3077 rl_end_undo_group ();
3081 /* How to toggle back and forth between editing modes. */
3082 rl_vi_editing_mode ()
3084 #if defined (VI_MODE)
3085 rl_editing_mode = vi_mode;
3086 rl_vi_insertion_mode ();
3088 #endif /* VI_MODE */
3091 rl_emacs_editing_mode ()
3093 rl_editing_mode = emacs_mode;
3094 _rl_keymap = emacs_standard_keymap;
3099 /* **************************************************************** */
3101 /* USG (System V) Support */
3103 /* **************************************************************** */
3112 #if defined (__GO32__)
3115 #endif /* __GO32__ */
3119 result = read (fileno (stream), &c, sizeof (unsigned char));
3121 if (result == sizeof (unsigned char))
3124 /* If zero characters are returned, then the file that we are
3125 reading from is empty! Return EOF in that case. */
3129 #if defined (EWOULDBLOCK)
3130 if (errno == EWOULDBLOCK)
3134 if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3136 if (flags & O_NDELAY)
3139 fcntl (fileno (stream), F_SETFL, flags);
3144 #endif /* EWOULDBLOCK */
3146 #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
3147 if (errno == EAGAIN)
3151 if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3153 if (flags & O_NONBLOCK)
3155 flags &= ~O_NONBLOCK;
3156 fcntl (fileno (stream), F_SETFL, flags);
3160 #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
3162 #if !defined (__GO32__)
3163 /* If the error that we received was SIGINT, then try again,
3164 this is simply an interrupted system call to read ().
3165 Otherwise, some error ocurred, also signifying EOF. */
3168 #endif /* !__GO32__ */
3173 _rl_savestring (str)
3176 char *copy = (char*) xmalloc (strlen (str) + 1);
3181 #if defined (STATIC_MALLOC)
3183 /* **************************************************************** */
3185 /* xmalloc and xrealloc () */
3187 /* **************************************************************** */
3189 static void memory_error_and_abort ();
3195 char *temp = (char *)malloc (bytes);
3198 memory_error_and_abort ();
3203 xrealloc (pointer, bytes)
3210 temp = (char *)malloc (bytes);
3212 temp = (char *)realloc (pointer, bytes);
3215 memory_error_and_abort ();
3221 memory_error_and_abort ()
3223 fprintf (stderr, "readline: Out of virtual memory!\n");
3226 #endif /* STATIC_MALLOC */
3229 /* **************************************************************** */
3231 /* Testing Readline */
3233 /* **************************************************************** */
3239 HIST_ENTRY **history_list ();
3240 char *temp = (char *)NULL;
3241 char *prompt = "readline% ";
3246 temp = readline (prompt);
3252 /* If there is anything on the line, print it and remember it. */
3255 fprintf (stderr, "%s\r\n", temp);
3259 /* Check for `command' that we handle. */
3260 if (strcmp (temp, "quit") == 0)
3263 if (strcmp (temp, "list") == 0)
3265 HIST_ENTRY **list = history_list ();
3269 for (i = 0; list[i]; i++)
3271 fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
3272 free (list[i]->line);
3286 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"