1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 1, or
12 (at your option) any later version.
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #define READLINE_LIBRARY
26 #include <sys/types.h>
28 #if !defined (NO_SYS_FILE)
29 # include <sys/file.h>
30 #endif /* !NO_SYS_FILE */
33 #if defined (HAVE_UNISTD_H)
35 #endif /* HAVE_UNISTD_H */
37 #if defined (HAVE_STDLIB_H)
40 # include "ansi_stdlib.h"
41 #endif /* HAVE_STDLIB_H */
44 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
51 #include "posixstat.h"
53 /* System-specific feature definitions and include files. */
56 #if defined (GWINSZ_IN_SYS_IOCTL) || (defined (VSTATUS) && !defined (SunOS4))
57 # include <sys/ioctl.h>
58 #endif /* GWINSZ_IN_SYS_IOCTL || VSTATUS */
60 /* Some standard library routines. */
64 /* NOTE: Functions and variables prefixed with `_rl_' are
65 pseudo-global: they are global so they can be shared
66 between files in the readline library, but are not intended
67 to be visible to readline callers. */
69 /* Functions imported from other files in the library. */
70 extern char *tgetstr ();
71 extern void rl_prep_terminal (), rl_deprep_terminal ();
73 extern void _rl_bind_if_unbound ();
75 /* External redisplay functions and variables from display.c */
76 extern void _rl_move_vert ();
77 extern void _rl_update_final ();
79 extern void _rl_erase_at_end_of_line ();
80 extern void _rl_move_cursor_relative ();
82 extern int _rl_vis_botlin;
83 extern int _rl_last_c_pos;
84 extern int _rl_horizontal_scroll_mode;
85 extern int rl_display_fixed;
86 extern char *rl_display_prompt;
88 /* Variables imported from complete.c. */
89 extern char *rl_completer_word_break_characters;
90 extern char *rl_basic_word_break_characters;
91 extern int rl_completion_query_items;
92 extern int rl_complete_with_tilde_expansion;
95 extern void _rl_vi_set_last ();
96 extern void _rl_vi_reset_last ();
97 extern void _rl_vi_done_inserting ();
100 /* Forward declarations used in this file. */
101 void _rl_free_history_entry ();
104 void _rl_set_screen_size ();
105 int _rl_output_character_function ();
107 static char *readline_internal ();
108 static void readline_initialize_everything ();
109 static int init_terminal_io ();
110 static void start_using_history ();
111 static void bind_arrow_keys ();
113 #if !defined (__GO32__)
114 static void readline_default_bindings ();
115 #endif /* !__GO32__ */
117 #if defined (__GO32__)
119 # undef HANDLE_SIGNALS
120 #endif /* __GO32__ */
122 #if defined (STATIC_MALLOC)
123 static char *xmalloc (), *xrealloc ();
125 extern char *xmalloc (), *xrealloc ();
126 #endif /* STATIC_MALLOC */
129 /* **************************************************************** */
131 /* Line editing input utility */
133 /* **************************************************************** */
135 static char *LibraryVersion = "2.0";
137 /* A pointer to the keymap that is currently in use.
138 By default, it is the standard emacs keymap. */
139 Keymap _rl_keymap = emacs_standard_keymap;
141 /* The current style of editing. */
142 int rl_editing_mode = emacs_mode;
144 /* Non-zero if the previous command was a kill command. */
145 static int last_command_was_kill = 0;
147 /* The current value of the numeric argument specified by the user. */
148 int rl_numeric_arg = 1;
150 /* Non-zero if an argument was typed. */
151 int rl_explicit_arg = 0;
153 /* Temporary value used while generating the argument. */
156 /* Non-zero means we have been called at least once before. */
157 static int rl_initialized = 0;
159 /* If non-zero, this program is running in an EMACS buffer. */
160 static int running_in_emacs = 0;
162 /* The current offset in the current input line. */
165 /* Mark in the current input line. */
168 /* Length of the current input line. */
171 /* Make this non-zero to return the current input_line. */
174 /* The last function executed by readline. */
175 Function *rl_last_func = (Function *)NULL;
177 /* Top level environment for readline_internal (). */
178 static jmp_buf readline_top_level;
180 /* The streams we interact with. */
181 static FILE *in_stream, *out_stream;
183 /* The names of the streams that we do input and output to. */
184 FILE *rl_instream = (FILE *)NULL;
185 FILE *rl_outstream = (FILE *)NULL;
187 /* Non-zero means echo characters as they are read. */
188 int readline_echoing_p = 1;
190 /* Current prompt. */
192 int rl_visible_prompt_length = 0;
194 /* The number of characters read in order to type this complete command. */
195 int rl_key_sequence_length = 0;
197 /* If non-zero, then this is the address of a function to call just
198 before readline_internal () prints the first prompt. */
199 Function *rl_startup_hook = (Function *)NULL;
201 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
202 static char *the_line;
204 /* The character that can generate an EOF. Really read from
205 the terminal driver... just defaulted here. */
206 int _rl_eof_char = CTRL ('D');
208 /* Non-zero makes this the next keystroke to read. */
209 int rl_pending_input = 0;
211 /* Pointer to a useful terminal name. */
212 char *rl_terminal_name = (char *)NULL;
214 /* Non-zero means to always use horizontal scrolling in line display. */
215 int _rl_horizontal_scroll_mode = 0;
217 /* Non-zero means to display an asterisk at the starts of history lines
218 which have been modified. */
219 int _rl_mark_modified_lines = 0;
221 /* The style of `bell' notification preferred. This can be set to NO_BELL,
222 AUDIBLE_BELL, or VISIBLE_BELL. */
223 int _rl_bell_preference = AUDIBLE_BELL;
225 /* Line buffer and maintenence. */
226 char *rl_line_buffer = (char *)NULL;
227 int rl_line_buffer_len = 0;
228 #define DEFAULT_BUFFER_SIZE 256
230 /* Forward declarations used by the display and termcap code. */
232 int screenwidth, screenheight, screenchars;
235 /* **************************************************************** */
237 /* `Forward' declarations */
239 /* **************************************************************** */
241 /* Non-zero means do not parse any lines other than comments and
242 parser directives. */
243 unsigned char _rl_parsing_conditionalized_out = 0;
245 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
246 static int defining_kbd_macro = 0;
248 /* Non-zero means to convert characters with the meta bit set to
249 escape-prefixed characters so we can indirect through
250 emacs_meta_keymap or vi_escape_keymap. */
251 int _rl_convert_meta_chars_to_ascii = 1;
253 /* Non-zero means to output characters with the meta bit set directly
254 rather than as a meta-prefixed escape sequence. */
255 int _rl_output_meta_chars = 0;
257 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
259 static int doing_an_undo = 0;
261 /* **************************************************************** */
263 /* Top Level Functions */
265 /* **************************************************************** */
267 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
268 int _rl_meta_flag = 0; /* Forward declaration */
270 /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
271 none. A return value of NULL means that EOF was encountered. */
280 /* If we are at EOF return a NULL string. */
281 if (rl_pending_input == EOF)
283 rl_pending_input = 0;
284 return ((char *)NULL);
287 rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
290 rl_prep_terminal (_rl_meta_flag);
292 #if defined (HANDLE_SIGNALS)
296 value = readline_internal ();
297 rl_deprep_terminal ();
299 #if defined (HANDLE_SIGNALS)
306 /* Read a line of input from the global rl_instream, doing output on
307 the global rl_outstream.
308 If rl_prompt is non-null, then that is our prompt. */
312 int lastc, c, eof_found;
314 in_stream = rl_instream;
315 out_stream = rl_outstream;
321 (*rl_startup_hook) ();
323 if (!readline_echoing_p)
327 fprintf (out_stream, "%s", rl_prompt);
335 #if defined (VI_MODE)
336 if (rl_editing_mode == vi_mode)
337 rl_vi_insertion_mode ();
343 int lk = last_command_was_kill;
346 code = setjmp (readline_top_level);
351 if (!rl_pending_input)
353 /* Then initialize the argument and number of keys read. */
355 rl_key_sequence_length = 0;
360 /* EOF typed to a non-blank line is a <NL>. */
361 if (c == EOF && rl_end)
364 /* The character _rl_eof_char typed to blank line, and not as the
365 previous character is interpreted as EOF. */
366 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
373 _rl_dispatch (c, _rl_keymap);
375 /* If there was no change in last_command_was_kill, then no kill
376 has taken place. Note that if input is pending we are reading
377 a prefix command, so nothing has changed yet. */
378 if (!rl_pending_input)
380 if (lk == last_command_was_kill)
381 last_command_was_kill = 0;
384 #if defined (VI_MODE)
385 /* In vi mode, when you exit insert mode, the cursor moves back
386 over the previous character. We explicitly check for that here. */
387 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
395 /* Restore the original of this history line, iff the line that we
396 are editing was originally in the history, AND the line has changed. */
398 HIST_ENTRY *entry = current_history ();
400 if (entry && rl_undo_list)
402 char *temp = savestring (the_line);
404 entry = replace_history_entry (where_history (), the_line,
406 _rl_free_history_entry (entry);
408 strcpy (the_line, temp);
413 /* At any rate, it is highly likely that this line has an undo list. Get
421 return (savestring (the_line));
424 /* **************************************************************** */
426 /* Character Input Buffering */
428 /* **************************************************************** */
430 static int pop_index = 0, push_index = 0, ibuffer_len = 511;
431 static unsigned char ibuffer[512];
433 /* Non-null means it is a pointer to a function to run while waiting for
435 Function *rl_event_hook = (Function *)NULL;
437 #define any_typein (push_index != pop_index)
439 /* Add KEY to the buffer of characters to be read. */
446 rl_pending_input = EOF;
448 ibuffer[push_index++] = key;
449 if (push_index >= ibuffer_len)
454 /* Return the amount of space available in the
455 buffer for stuffing characters. */
459 if (pop_index > push_index)
460 return (pop_index - push_index);
462 return (ibuffer_len - (push_index - pop_index));
465 /* Get a key from the buffer of characters to be read.
466 Return the key in KEY.
467 Result is KEY if there was a key, or 0 if there wasn't. */
472 if (push_index == pop_index)
475 *key = ibuffer[pop_index++];
477 if (pop_index >= ibuffer_len)
483 /* Stuff KEY into the *front* of the input buffer.
484 Returns non-zero if successful, zero if there is
485 no space left in the buffer. */
490 if (ibuffer_space ())
494 pop_index = ibuffer_len - 1;
495 ibuffer[pop_index] = key;
501 /* If a character is available to be read, then read it
502 and stuff it into IBUFFER. Otherwise, just return. */
506 #if defined (__GO32__)
516 else if (kbhit () && ibuffer_space ())
517 rl_stuff_char (getkey ());
518 #else /* !__GO32__ */
520 int tty = fileno (in_stream);
521 register int tem, result = -1;
525 #if defined (FIONREAD)
526 result = ioctl (tty, FIONREAD, &chars_avail);
529 #if defined (O_NDELAY)
534 flags = fcntl (tty, F_GETFL, 0);
536 fcntl (tty, F_SETFL, (flags | O_NDELAY));
537 chars_avail = read (tty, &input, 1);
539 fcntl (tty, F_SETFL, flags);
540 if (chars_avail == -1 && errno == EAGAIN)
543 #endif /* O_NDELAY */
545 /* If there's nothing available, don't waste time trying to read
547 if (chars_avail == 0)
550 tem = ibuffer_space ();
552 if (chars_avail > tem)
555 /* One cannot read all of the available input. I can only read a single
556 character at a time, or else programs which require input can be
557 thwarted. If the buffer is larger than one character, I lose.
559 if (tem < ibuffer_len)
564 while (chars_avail--)
565 rl_stuff_char (rl_getc (in_stream));
570 rl_stuff_char (input);
572 #endif /* !__GO32__ */
575 static int next_macro_key ();
576 /* Read a key, including pending input. */
582 rl_key_sequence_length++;
584 if (rl_pending_input)
586 c = rl_pending_input;
587 rl_pending_input = 0;
591 /* If input is coming from a macro, then use that. */
592 if (c = next_macro_key ())
595 /* If the user has an event function, then call it periodically. */
598 while (rl_event_hook && !rl_get_char (&c))
606 if (!rl_get_char (&c))
607 c = rl_getc (in_stream);
614 /* Found later in this file. */
615 static void add_macro_char (), with_macro_input ();
617 /* Do the command associated with KEY in MAP.
618 If the associated command is really a keymap, then read
619 another key, and dispatch into that map. */
621 _rl_dispatch (key, map)
627 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
629 if (map[ESC].type == ISKMAP)
631 if (defining_kbd_macro)
632 add_macro_char (ESC);
633 map = FUNCTION_TO_KEYMAP (map, ESC);
635 rl_key_sequence_length += 2;
636 return (_rl_dispatch (key, map));
643 if (defining_kbd_macro)
644 add_macro_char (key);
646 switch (map[key].type)
650 Function *func = map[key].function;
652 if (func != (Function *)NULL)
654 /* Special case rl_do_lowercase_version (). */
655 if (func == rl_do_lowercase_version)
656 return (_rl_dispatch (to_lower (key), map));
658 r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
660 /* If we have input pending, then the last command was a prefix
661 command. Don't change the state of rl_last_func. Otherwise,
662 remember the last command executed in this variable. */
663 if (!rl_pending_input)
664 rl_last_func = map[key].function;
675 if (map[key].function != (Function *)NULL)
679 rl_key_sequence_length++;
680 newkey = rl_read_key ();
681 r = _rl_dispatch (newkey, FUNCTION_TO_KEYMAP (map, key));
691 if (map[key].function != (Function *)NULL)
695 macro = savestring ((char *)map[key].function);
696 with_macro_input (macro);
701 #if defined (VI_MODE)
702 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
703 rl_vi_textmod_command (key))
704 _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
710 /* **************************************************************** */
712 /* Hacking Keyboard Macros */
714 /* **************************************************************** */
716 /* The currently executing macro string. If this is non-zero,
717 then it is a malloc ()'ed string where input is coming from. */
718 static char *executing_macro = (char *)NULL;
720 /* The offset in the above string to the next character to be read. */
721 static int executing_macro_index = 0;
723 /* The current macro string being built. Characters get stuffed
724 in here by add_macro_char (). */
725 static char *current_macro = (char *)NULL;
727 /* The size of the buffer allocated to current_macro. */
728 static int current_macro_size = 0;
730 /* The index at which characters are being added to current_macro. */
731 static int current_macro_index = 0;
733 /* A structure used to save nested macro strings.
734 It is a linked list of string/index for each saved macro. */
736 struct saved_macro *next;
741 /* The list of saved macros. */
742 struct saved_macro *macro_list = (struct saved_macro *)NULL;
744 /* Forward declarations of static functions. Thank you C. */
745 static void push_executing_macro (), pop_executing_macro ();
747 /* This one has to be declared earlier in the file. */
748 /* static void add_macro_char (); */
750 /* Set up to read subsequent input from STRING.
751 STRING is free ()'ed when we are done with it. */
753 with_macro_input (string)
756 push_executing_macro ();
757 executing_macro = string;
758 executing_macro_index = 0;
761 /* Return the next character available from a macro, or 0 if
762 there are no macro characters. */
766 if (!executing_macro)
769 if (!executing_macro[executing_macro_index])
771 pop_executing_macro ();
772 return (next_macro_key ());
775 return (executing_macro[executing_macro_index++]);
778 /* Save the currently executing macro on a stack of saved macros. */
780 push_executing_macro ()
782 struct saved_macro *saver;
784 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
785 saver->next = macro_list;
786 saver->sindex = executing_macro_index;
787 saver->string = executing_macro;
792 /* Discard the current macro, replacing it with the one
793 on the top of the stack of saved macros. */
795 pop_executing_macro ()
798 free (executing_macro);
800 executing_macro = (char *)NULL;
801 executing_macro_index = 0;
805 struct saved_macro *disposer = macro_list;
806 executing_macro = macro_list->string;
807 executing_macro_index = macro_list->sindex;
808 macro_list = macro_list->next;
813 /* Add a character to the macro being built. */
818 if (current_macro_index + 1 >= current_macro_size)
821 current_macro = xmalloc (current_macro_size = 25);
823 current_macro = xrealloc (current_macro, current_macro_size += 25);
826 current_macro[current_macro_index++] = c;
827 current_macro[current_macro_index] = '\0';
830 /* Begin defining a keyboard macro.
831 Keystrokes are recorded as they are executed.
832 End the definition with rl_end_kbd_macro ().
833 If a numeric argument was explicitly typed, then append this
834 definition to the end of the existing macro, and start by
835 re-executing the existing macro. */
836 rl_start_kbd_macro (ignore1, ignore2)
837 int ignore1, ignore2;
839 if (defining_kbd_macro)
848 with_macro_input (savestring (current_macro));
851 current_macro_index = 0;
853 defining_kbd_macro = 1;
857 /* Stop defining a keyboard macro.
858 A numeric argument says to execute the macro right now,
859 that many times, counting the definition as the first time. */
860 rl_end_kbd_macro (count, ignore)
863 if (!defining_kbd_macro)
869 current_macro_index -= (rl_key_sequence_length - 1);
870 current_macro[current_macro_index] = '\0';
872 defining_kbd_macro = 0;
874 return (rl_call_last_kbd_macro (--count, 0));
877 /* Execute the most recently defined keyboard macro.
878 COUNT says how many times to execute it. */
879 rl_call_last_kbd_macro (count, ignore)
885 if (defining_kbd_macro)
887 ding (); /* no recursive macros */
888 current_macro[--current_macro_index] = '\0'; /* erase this char */
893 with_macro_input (savestring (current_macro));
898 _rl_kill_kbd_macro ()
902 free (current_macro);
903 current_macro = (char *) NULL;
905 current_macro_size = current_macro_index = 0;
909 free (executing_macro);
910 executing_macro = (char *) NULL;
912 executing_macro_index = 0;
914 defining_kbd_macro = 0;
917 /* **************************************************************** */
919 /* Initializations */
921 /* **************************************************************** */
923 /* Initliaze readline (and terminal if not already). */
926 /* If we have never been called before, initialize the
927 terminal and data structures. */
930 readline_initialize_everything ();
934 /* Initalize the current line information. */
935 rl_point = rl_end = 0;
936 the_line = rl_line_buffer;
939 /* We aren't done yet. We haven't even gotten started yet! */
942 /* Tell the history routines what is going on. */
943 start_using_history ();
945 /* Make the display buffer match the state of the line. */
946 rl_reset_line_state ();
948 /* No such function typed yet. */
949 rl_last_func = (Function *)NULL;
951 /* Parsing of key-bindings begins in an enabled state. */
952 _rl_parsing_conditionalized_out = 0;
957 /* Initialize the entire state of the world. */
959 readline_initialize_everything ()
963 /* Find out if we are running in Emacs. */
964 running_in_emacs = getenv ("EMACS") != (char *)0;
966 /* Set up input and output if they are not already set up. */
971 rl_outstream = stdout;
973 /* Bind in_stream and out_stream immediately. These values may change,
974 but they may also be used before readline_internal () is called. */
975 in_stream = rl_instream;
976 out_stream = rl_outstream;
978 /* Allocate data structures. */
980 rl_line_buffer = xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
982 /* Initialize the terminal interface. */
983 init_terminal_io ((char *)NULL);
985 #if !defined (__GO32__)
986 /* Bind tty characters to readline functions. */
987 readline_default_bindings ();
988 #endif /* !__GO32__ */
990 /* Initialize the function names. */
991 rl_initialize_funmap ();
993 /* Check for LC_CTYPE and use its value to decide the defaults for
994 8-bit character input and output. */
995 t = getenv ("LC_CTYPE");
996 if (t && (strcmp (t, "iso-8859-1") == 0 || strcmp (t, "iso_8859_1") == 0 ||
997 strcmp (t, "ISO-8859-1") == 0))
1000 _rl_convert_meta_chars_to_ascii = 0;
1001 _rl_output_meta_chars = 1;
1004 /* Read in the init file. */
1005 rl_read_init_file ((char *)NULL);
1008 if (_rl_horizontal_scroll_mode && term_xn)
1011 screenchars -= screenheight;
1014 /* Override the effect of any `set keymap' assignments in the
1016 rl_set_keymap_from_edit_mode ();
1018 /* Try to bind a common arrow key prefix, if not already bound. */
1021 /* If the completion parser's default word break characters haven't
1022 been set yet, then do so now. */
1023 if (rl_completer_word_break_characters == (char *)NULL)
1024 rl_completer_word_break_characters = rl_basic_word_break_characters;
1027 /* If this system allows us to look at the values of the regular
1028 input editing characters, then bind them to their readline
1029 equivalents, iff the characters are not bound to keymaps. */
1031 readline_default_bindings ()
1033 rltty_set_default_bindings (_rl_keymap);
1037 bind_arrow_keys_internal ()
1041 f = rl_function_of_keyseq ("\033[A", _rl_keymap, (int *)NULL);
1042 if (!f || f == rl_do_lowercase_version)
1044 _rl_bind_if_unbound ("\033[A", rl_get_previous_history);
1045 _rl_bind_if_unbound ("\033[B", rl_get_next_history);
1046 _rl_bind_if_unbound ("\033[C", rl_forward);
1047 _rl_bind_if_unbound ("\033[D", rl_backward);
1050 f = rl_function_of_keyseq ("\033OA", _rl_keymap, (int *)NULL);
1051 if (!f || f == rl_do_lowercase_version)
1053 _rl_bind_if_unbound ("\033OA", rl_get_previous_history);
1054 _rl_bind_if_unbound ("\033OB", rl_get_next_history);
1055 _rl_bind_if_unbound ("\033OC", rl_forward);
1056 _rl_bind_if_unbound ("\033OD", rl_backward);
1060 /* Try and bind the common arrow key prefix after giving termcap and
1061 the inputrc file a chance to bind them and create `real' keymaps
1062 for the arrow key prefix. */
1068 xkeymap = _rl_keymap;
1070 _rl_keymap = emacs_standard_keymap;
1071 bind_arrow_keys_internal ();
1073 #if defined (VI_MODE)
1074 _rl_keymap = vi_movement_keymap;
1075 bind_arrow_keys_internal ();
1078 _rl_keymap = xkeymap;
1082 /* **************************************************************** */
1084 /* Numeric Arguments */
1086 /* **************************************************************** */
1088 /* Handle C-u style numeric args, as well as M--, and M-digits. */
1096 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
1097 key = c = rl_read_key ();
1099 if (_rl_keymap[c].type == ISFUNC &&
1100 _rl_keymap[c].function == rl_universal_argument)
1102 rl_numeric_arg *= 4;
1108 if (rl_explicit_arg)
1109 rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
1111 rl_numeric_arg = (c - '0');
1112 rl_explicit_arg = 1;
1116 if (c == '-' && !rl_explicit_arg)
1123 rl_clear_message ();
1124 return (_rl_dispatch (key, _rl_keymap));
1131 /* Add the current digit to the argument in progress. */
1132 rl_digit_argument (ignore, key)
1135 rl_pending_input = key;
1136 return (rl_digit_loop ());
1139 /* What to do when you abort reading an argument. */
1140 rl_discard_argument ()
1143 rl_clear_message ();
1144 rl_init_argument ();
1148 /* Create a default argument. */
1151 rl_numeric_arg = rl_arg_sign = 1;
1152 rl_explicit_arg = 0;
1156 /* C-u, universal argument. Multiply the current argument by 4.
1157 Read a key. If the key has nothing to do with arguments, then
1158 dispatch on it. If the key is the abort character then abort. */
1159 rl_universal_argument ()
1161 rl_numeric_arg *= 4;
1162 return (rl_digit_loop ());
1165 /* **************************************************************** */
1167 /* Terminal and Termcap */
1169 /* **************************************************************** */
1171 static char *term_buffer = (char *)NULL;
1172 static char *term_string_buffer = (char *)NULL;
1174 static int tcap_initialized = 0;
1176 /* Non-zero means this terminal can't really do anything. */
1178 /* On Solaris2, sys/types.h #includes sys/reg.h, which #defines PC.
1179 Unfortunately, PC is a global variable used by the termcap library. */
1182 #if !defined (__linux__)
1183 /* If this causes problems, add back the `extern'. */
1184 /*extern*/ char PC, *BC, *UP;
1185 #endif /* __linux__ */
1187 /* Some strings to control terminal actions. These are output by tputs (). */
1188 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1191 /* Non-zero if we determine that the terminal can do character insertion. */
1192 int terminal_can_insert = 0;
1194 /* How to insert characters. */
1195 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1197 /* How to delete characters. */
1198 char *term_dc, *term_DC;
1200 #if defined (HACK_TERMCAP_MOTION)
1201 char *term_forward_char;
1202 #endif /* HACK_TERMCAP_MOTION */
1204 /* How to go up a line. */
1207 /* A visible bell, if the terminal can be made to flash the screen. */
1210 /* Non-zero means that this terminal has a meta key. */
1213 /* The string to write to turn on the meta key, if this term has one. */
1216 /* The string to write to turn off the meta key, if this term has one. */
1219 /* The key sequences output by the arrow keys, if this terminal has any. */
1220 char *term_ku, *term_kd, *term_kr, *term_kl;
1222 /* How to initialize and reset the arrow keys, if this terminal has any. */
1223 char *term_ks, *term_ke;
1225 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1227 rl_reset_terminal (terminal_name)
1228 char *terminal_name;
1230 init_terminal_io (terminal_name);
1234 /* Set readline's idea of the screen size. TTY is a file descriptor open
1235 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
1236 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
1237 non-null serve to check whether or not we have initialized termcap. */
1239 _rl_set_screen_size (tty, ignore_env)
1240 int tty, ignore_env;
1242 #if defined (TIOCGWINSZ)
1243 struct winsize window_size;
1244 #endif /* TIOCGWINSZ */
1246 #if defined (TIOCGWINSZ)
1247 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
1249 screenwidth = (int) window_size.ws_col;
1250 screenheight = (int) window_size.ws_row;
1252 #endif /* TIOCGWINSZ */
1254 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
1256 if (screenwidth <= 0)
1260 if (!ignore_env && (sw = getenv ("COLUMNS")))
1261 screenwidth = atoi (sw);
1263 if (screenwidth <= 0 && term_string_buffer)
1264 screenwidth = tgetnum ("co");
1267 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
1269 if (screenheight <= 0)
1273 if (!ignore_env && (sh = getenv ("LINES")))
1274 screenheight = atoi (sh);
1276 if (screenheight <= 0 && term_string_buffer)
1277 screenheight = tgetnum ("li");
1280 /* If all else fails, default to 80x24 terminal. */
1281 if (screenwidth <= 1)
1284 if (screenheight <= 0)
1288 /* If we're being compiled as part of bash, set the environment
1289 variables $LINES and $COLUMNS to new values. */
1290 set_lines_and_columns (screenheight, screenwidth);
1296 screenchars = screenwidth * screenheight;
1304 /* This should be kept sorted, just in case we decide to change the
1305 search algorithm to something smarter. */
1306 static struct _tc_string tc_strings[] =
1323 "le", &term_backspace,
1326 #if defined (HACK_TERMCAP_MOTION)
1327 "nd", &term_forward_char,
1331 "vb", &visible_bell,
1334 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
1336 /* Read the desired terminal capability strings into BP. The capabilities
1337 are described in the TC_STRINGS table. */
1339 get_term_capabilities (bp)
1344 for (i = 0; i < NUM_TC_STRINGS; i++)
1345 *(tc_strings[i].tc_value) = tgetstr (tc_strings[i].tc_var, bp);
1346 tcap_initialized = 1;
1350 init_terminal_io (terminal_name)
1351 char *terminal_name;
1353 #if defined (__GO32__)
1354 screenwidth = ScreenCols ();
1355 screenheight = ScreenRows ();
1356 screenchars = screenwidth * screenheight;
1358 term_im = term_ei = term_ic = term_IC = (char *)NULL;
1359 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1361 /* Does the __GO32__ have a meta key? I don't know. */
1363 term_mm = term_mo = (char *)NULL;
1365 /* It probably has arrow keys, but I don't know what they are. */
1366 term_ku = term_kd = term_kr = term_kl = (char *)NULL;
1368 #if defined (HACK_TERMCAP_MOTION)
1369 term_forward_char = (char *)NULL;
1370 #endif /* HACK_TERMCAP_MOTION */
1371 terminal_can_insert = term_xn = 0;
1373 #else /* !__GO32__ */
1375 char *term, *buffer;
1379 term = terminal_name ? terminal_name : getenv ("TERM");
1381 if (!term_string_buffer)
1382 term_string_buffer = xmalloc (2048);
1385 term_buffer = xmalloc (2048);
1387 buffer = term_string_buffer;
1389 term_clrpag = term_cr = term_clreol = (char *)NULL;
1394 if (tgetent (term_buffer, term) <= 0)
1399 screenchars = 79 * 24;
1401 term_im = term_ei = term_ic = term_IC = (char *)NULL;
1402 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1403 term_ku = term_kd = term_kl = term_kr = (char *)NULL;
1404 #if defined (HACK_TERMCAP_MOTION)
1405 term_forward_char = (char *)NULL;
1407 terminal_can_insert = 0;
1411 get_term_capabilities (&buffer);
1413 /* Set up the variables that the termcap library expects the application
1415 PC = term_pc ? *term_pc : 0;
1416 BC = term_backspace;
1423 tty = fileno (rl_instream);
1427 screenwidth = screenheight = 0;
1429 term_xn = tgetflag ("am") && tgetflag ("xn");
1431 _rl_set_screen_size (tty, 0);
1433 /* "An application program can assume that the terminal can do
1434 character insertion if *any one of* the capabilities `IC',
1435 `im', `ic' or `ip' is provided." But we can't do anything if
1436 only `ip' is provided, so... */
1437 terminal_can_insert = (term_IC || term_im || term_ic);
1439 /* Check to see if this terminal has a meta key and clear the capability
1440 variables if there is none. */
1441 term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
1444 term_mm = (char *)NULL;
1445 term_mo = (char *)NULL;
1448 /* Attempt to find and bind the arrow keys. Do not override already
1449 bound keys in an overzealous attempt, however. */
1450 xkeymap = _rl_keymap;
1452 _rl_keymap = emacs_standard_keymap;
1453 _rl_bind_if_unbound (term_ku, rl_get_previous_history);
1454 _rl_bind_if_unbound (term_kd, rl_get_next_history);
1455 _rl_bind_if_unbound (term_kr, rl_forward);
1456 _rl_bind_if_unbound (term_kl, rl_backward);
1458 #if defined (VI_MODE)
1459 _rl_keymap = vi_movement_keymap;
1460 _rl_bind_if_unbound (term_ku, rl_get_previous_history);
1461 _rl_bind_if_unbound (term_kd, rl_get_next_history);
1462 _rl_bind_if_unbound (term_kr, rl_forward);
1463 _rl_bind_if_unbound (term_kl, rl_backward);
1464 #endif /* VI_MODE */
1466 _rl_keymap = xkeymap;
1468 #endif /* !__GO32__ */
1473 rl_get_termcap (cap)
1478 if (tcap_initialized == 0)
1479 return ((char *)NULL);
1480 for (i = 0; i < NUM_TC_STRINGS; i++)
1482 if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
1483 return *(tc_strings[i].tc_value);
1485 return ((char *)NULL);
1488 /* A function for the use of tputs () */
1490 _rl_output_character_function (c)
1493 return putc (c, out_stream);
1496 /* Write COUNT characters from STRING to the output stream. */
1498 _rl_output_some_chars (string, count)
1502 fwrite (string, 1, count, out_stream);
1505 /* Move the cursor back. */
1511 #if !defined (__GO32__)
1513 for (i = 0; i < count; i++)
1514 tputs (term_backspace, 1, _rl_output_character_function);
1516 #endif /* !__GO32__ */
1517 for (i = 0; i < count; i++)
1518 putc ('\b', out_stream);
1522 /* Move to the start of the next line. */
1525 #if defined (NEW_TTY_DRIVER)
1526 tputs (term_cr, 1, _rl_output_character_function);
1527 #endif /* NEW_TTY_DRIVER */
1528 putc ('\n', out_stream);
1532 rl_tty_status (count, key)
1535 #if defined (TIOCSTAT)
1536 ioctl (1, TIOCSTAT, (char *)0);
1545 /* **************************************************************** */
1547 /* Utility Functions */
1549 /* **************************************************************** */
1551 /* Return 0 if C is not a member of the class of characters that belong
1552 in words, or 1 if it is. */
1554 int allow_pathname_alphabetic_chars = 0;
1555 char *pathname_alphabetic_chars = "/-_=~.#$";
1561 if (pure_alphabetic (c) || (digit_p (c)))
1564 if (allow_pathname_alphabetic_chars)
1565 return (strchr (pathname_alphabetic_chars, c) != NULL);
1570 /* Ring the terminal bell. */
1574 if (readline_echoing_p)
1576 #if !defined (__GO32__)
1577 switch (_rl_bell_preference)
1585 tputs (visible_bell, 1, _rl_output_character_function);
1590 fprintf (stderr, "\007");
1594 #else /* __GO32__ */
1595 fprintf (stderr, "\007");
1597 #endif /* __GO32__ */
1603 /* How to abort things. */
1604 rl_abort (count, key)
1608 rl_clear_message ();
1609 rl_init_argument ();
1610 rl_pending_input = 0;
1612 defining_kbd_macro = 0;
1613 while (executing_macro)
1614 pop_executing_macro ();
1616 rl_last_func = (Function *)NULL;
1617 longjmp (readline_top_level, 1);
1620 /* Return a copy of the string between FROM and TO.
1621 FROM is inclusive, TO is not. */
1623 rl_copy_text (from, to)
1626 register int length;
1629 /* Fix it if the caller is confused. */
1638 copy = xmalloc (1 + length);
1639 strncpy (copy, the_line + from, length);
1640 copy[length] = '\0';
1644 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
1647 rl_extend_line_buffer (len)
1650 while (len >= rl_line_buffer_len)
1652 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
1653 rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
1656 the_line = rl_line_buffer;
1660 /* **************************************************************** */
1662 /* Insert and Delete */
1664 /* **************************************************************** */
1666 /* Insert a string of text into the line at point. This is the only
1667 way that you should do insertion. rl_insert () calls this
1669 rl_insert_text (string)
1672 register int i, l = strlen (string);
1674 if (rl_end + l >= rl_line_buffer_len)
1675 rl_extend_line_buffer (rl_end + l);
1677 for (i = rl_end; i >= rl_point; i--)
1678 the_line[i + l] = the_line[i];
1679 strncpy (the_line + rl_point, string, l);
1681 /* Remember how to undo this if we aren't undoing something. */
1684 /* If possible and desirable, concatenate the undos. */
1687 (rl_undo_list->what == UNDO_INSERT) &&
1688 (rl_undo_list->end == rl_point) &&
1689 (rl_undo_list->end - rl_undo_list->start < 20))
1690 rl_undo_list->end++;
1692 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
1696 the_line[rl_end] = '\0';
1700 /* Delete the string between FROM and TO. FROM is
1701 inclusive, TO is not. */
1702 rl_delete_text (from, to)
1705 register char *text;
1706 register int diff, i;
1708 /* Fix it if the caller is confused. */
1719 text = rl_copy_text (from, to);
1721 /* Some versions of strncpy() can't handle overlapping arguments. */
1723 for (i = from; i < rl_end - diff; i++)
1724 the_line[i] = the_line[i + diff];
1726 /* Remember how to undo this delete. */
1728 rl_add_undo (UNDO_DELETE, from, to, text);
1733 the_line[rl_end] = '\0';
1738 /* **************************************************************** */
1740 /* Readline character functions */
1742 /* **************************************************************** */
1744 /* This is not a gap editor, just a stupid line input routine. No hair
1745 is involved in writing any of the functions, and none should be. */
1749 rl_end is the place in the string that we would place '\0';
1750 i.e., it is always safe to place '\0' there.
1752 rl_point is the place in the string where the cursor is. Sometimes
1753 this is the same as rl_end.
1755 Any command that is called interactively receives two arguments.
1756 The first is a count: the numeric arg pased to this command.
1757 The second is the key which invoked this command.
1761 /* **************************************************************** */
1763 /* Movement Commands */
1765 /* **************************************************************** */
1767 /* Note that if you `optimize' the display for these functions, you cannot
1768 use said functions in other functions which do not do optimizing display.
1769 I.e., you will have to update the data base for rl_redisplay, and you
1770 might as well let rl_redisplay do that job. */
1772 /* Move forward COUNT characters. */
1773 rl_forward (count, key)
1777 rl_backward (-count);
1780 int end = rl_point + count;
1781 #if defined (VI_MODE)
1782 int lend = rl_end - (rl_editing_mode == vi_mode);
1798 /* Move backward COUNT characters. */
1799 rl_backward (count, key)
1803 rl_forward (-count);
1806 if (rl_point < count)
1817 /* Move to the beginning of the line. */
1818 rl_beg_of_line (count, key)
1825 /* Move to the end of the line. */
1826 rl_end_of_line (count, key)
1833 /* Move forward a word. We do what Emacs does. */
1834 rl_forward_word (count, key)
1841 rl_backward_word (-count);
1847 if (rl_point == rl_end)
1850 /* If we are not in a word, move forward until we are in one.
1851 Then, move forward until we hit a non-alphabetic character. */
1852 c = the_line[rl_point];
1853 if (!alphabetic (c))
1855 while (++rl_point < rl_end)
1857 c = the_line[rl_point];
1862 if (rl_point == rl_end)
1864 while (++rl_point < rl_end)
1866 c = the_line[rl_point];
1867 if (!alphabetic (c))
1875 /* Move backward a word. We do what Emacs does. */
1876 rl_backward_word (count, key)
1883 rl_forward_word (-count);
1892 /* Like rl_forward_word (), except that we look at the characters
1893 just before point. */
1895 c = the_line[rl_point - 1];
1896 if (!alphabetic (c))
1900 c = the_line[rl_point - 1];
1908 c = the_line[rl_point - 1];
1909 if (!alphabetic (c))
1919 /* Clear the current line. Numeric argument to C-l does this. */
1922 int curr_line, nleft;
1924 /* Find out whether or not there might be invisible characters in the
1926 if (rl_display_prompt == rl_prompt)
1927 nleft = _rl_last_c_pos - screenwidth - rl_visible_prompt_length;
1929 nleft = _rl_last_c_pos - screenwidth;
1932 curr_line = 1 + nleft / screenwidth;
1936 _rl_move_vert (curr_line);
1937 _rl_move_cursor_relative (0, the_line); /* XXX is this right */
1939 #if defined (__GO32__)
1941 int row, col, width, row_start;
1943 ScreenGetCursor (&row, &col);
1944 width = ScreenCols ();
1945 row_start = ScreenPrimary + (row * width);
1946 memset (row_start + col, 0, (width - col) * 2);
1948 #else /* !__GO32__ */
1950 tputs (term_clreol, 1, _rl_output_character_function);
1951 #endif /* !__GO32__ */
1953 rl_forced_update_display ();
1954 rl_display_fixed = 1;
1959 /* C-l typed to a line without quoting clears the screen, and then reprints
1960 the prompt and the current input line. Given a numeric arg, redraw only
1961 the current line. */
1962 rl_clear_screen (count, key)
1965 if (rl_explicit_arg)
1971 #if !defined (__GO32__)
1973 tputs (term_clrpag, 1, _rl_output_character_function);
1975 #endif /* !__GO32__ */
1978 rl_forced_update_display ();
1979 rl_display_fixed = 1;
1984 rl_arrow_keys (count, c)
1989 ch = rl_read_key ();
1991 switch (to_upper (ch))
1994 rl_get_previous_history (count);
1998 rl_get_next_history (count);
2006 rl_backward (count);
2016 /* **************************************************************** */
2020 /* **************************************************************** */
2022 /* Insert the character C at the current location, moving point forward. */
2023 rl_insert (count, c)
2032 /* If we can optimize, then do it. But don't let people crash
2033 readline because of extra large arguments. */
2034 if (count > 1 && count < 1024)
2036 string = xmalloc (1 + count);
2038 for (i = 0; i < count; i++)
2042 rl_insert_text (string);
2053 for (i = 0; i < 1024; i++)
2058 decreaser = (count > 1024 ? 1024 : count);
2059 str[decreaser] = '\0';
2060 rl_insert_text (str);
2067 /* We are inserting a single character.
2068 If there is pending input, then make a string of all of the
2069 pending characters that are bound to rl_insert, and insert
2076 string = xmalloc (ibuffer_len + 1);
2079 while ((t = rl_get_char (&key)) &&
2080 (_rl_keymap[key].type == ISFUNC &&
2081 _rl_keymap[key].function == rl_insert))
2085 rl_unget_char (key);
2088 rl_insert_text (string);
2093 /* Inserting a single character. */
2098 rl_insert_text (str);
2103 /* Insert the next typed character verbatim. */
2104 rl_quoted_insert (count, key)
2110 return (rl_insert (count, c));
2113 /* Insert a tab character. */
2114 rl_tab_insert (count, key)
2117 return (rl_insert (count, '\t'));
2120 /* What to do when a NEWLINE is pressed. We accept the whole line.
2121 KEY is the key that invoked this command. I guess it could have
2122 meaning in the future. */
2123 rl_newline (count, key)
2128 #if defined (VI_MODE)
2129 _rl_vi_done_inserting ();
2130 _rl_vi_reset_last ();
2132 #endif /* VI_MODE */
2134 if (readline_echoing_p)
2135 _rl_update_final ();
2139 rl_clean_up_for_exit ()
2141 if (readline_echoing_p)
2143 _rl_move_vert (_rl_vis_botlin);
2145 fflush (out_stream);
2146 rl_restart_output ();
2151 /* What to do for some uppercase characters, like meta characters,
2152 and some characters appearing in emacs_ctlx_keymap. This function
2153 is just a stub, you bind keys to it and the code in _rl_dispatch ()
2154 is special cased. */
2155 rl_do_lowercase_version (ignore1, ignore2)
2156 int ignore1, ignore2;
2161 /* Rubout the character behind point. */
2162 rl_rubout (count, key)
2177 if (count > 1 || rl_explicit_arg)
2179 int orig_point = rl_point;
2180 rl_backward (count);
2181 rl_kill_text (orig_point, rl_point);
2185 int c = the_line[--rl_point];
2186 rl_delete_text (rl_point, rl_point + 1);
2188 if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
2191 l = rl_character_len (c, rl_point);
2192 _rl_erase_at_end_of_line (l);
2198 /* Delete the character under the cursor. Given a numeric argument,
2199 kill that many characters instead. */
2200 rl_delete (count, invoking_key)
2201 int count, invoking_key;
2205 return (rl_rubout (-count));
2208 if (rl_point == rl_end)
2214 if (count > 1 || rl_explicit_arg)
2216 int orig_point = rl_point;
2218 rl_kill_text (orig_point, rl_point);
2219 rl_point = orig_point;
2223 return (rl_delete_text (rl_point, rl_point + 1));
2227 /* Delete all spaces and tabs around point. */
2228 rl_delete_horizontal_space (count, ignore)
2231 int start = rl_point;
2233 while (rl_point && whitespace (the_line[rl_point - 1]))
2238 while (rl_point < rl_end && whitespace (the_line[rl_point]))
2241 if (start != rl_point)
2243 rl_delete_text (start, rl_point);
2250 /* **************************************************************** */
2254 /* **************************************************************** */
2256 /* The next two functions mimic unix line editing behaviour, except they
2257 save the deleted text on the kill ring. This is safer than not saving
2258 it, and since we have a ring, nobody should get screwed. */
2260 /* This does what C-w does in Unix. We can't prevent people from
2261 using behaviour that they expect. */
2262 rl_unix_word_rubout (count, key)
2269 int orig_point = rl_point;
2275 while (rl_point && whitespace (the_line[rl_point - 1]))
2278 while (rl_point && !whitespace (the_line[rl_point - 1]))
2282 rl_kill_text (orig_point, rl_point);
2287 /* Here is C-u doing what Unix does. You don't *have* to use these
2288 key-bindings. We have a choice of killing the entire line, or
2289 killing from where we are to the start of the line. We choose the
2290 latter, because if you are a Unix weenie, then you haven't backspaced
2291 into the line at all, and if you aren't, then you know what you are
2293 rl_unix_line_discard (count, key)
2300 rl_kill_text (rl_point, 0);
2307 /* **************************************************************** */
2309 /* Commands For Typos */
2311 /* **************************************************************** */
2313 /* Random and interesting things in here. */
2315 /* **************************************************************** */
2319 /* **************************************************************** */
2321 /* The three kinds of things that we know how to do. */
2326 static int rl_change_case ();
2328 /* Uppercase the word at point. */
2329 rl_upcase_word (count, key)
2332 return (rl_change_case (count, UpCase));
2335 /* Lowercase the word at point. */
2336 rl_downcase_word (count, key)
2339 return (rl_change_case (count, DownCase));
2342 /* Upcase the first letter, downcase the rest. */
2343 rl_capitalize_word (count, key)
2346 return (rl_change_case (count, CapCase));
2349 /* The meaty function.
2350 Change the case of COUNT words, performing OP on them.
2351 OP is one of UpCase, DownCase, or CapCase.
2352 If a negative argument is given, leave point where it started,
2353 otherwise, leave it where it moves to. */
2355 rl_change_case (count, op)
2358 register int start = rl_point, end;
2361 rl_forward_word (count);
2371 /* We are going to modify some text, so let's prepare to undo it. */
2372 rl_modifying (start, end);
2374 for (; start < end; start++)
2379 the_line[start] = to_upper (the_line[start]);
2383 the_line[start] = to_lower (the_line[start]);
2389 the_line[start] = to_upper (the_line[start]);
2394 the_line[start] = to_lower (the_line[start]);
2396 if (!pure_alphabetic (the_line[start]))
2409 /* **************************************************************** */
2413 /* **************************************************************** */
2415 /* Transpose the words at point. */
2416 rl_transpose_words (count, key)
2419 char *word1, *word2;
2420 int w1_beg, w1_end, w2_beg, w2_end;
2421 int orig_point = rl_point;
2426 /* Find the two words. */
2427 rl_forward_word (count);
2429 rl_backward_word (1);
2431 rl_backward_word (count);
2433 rl_forward_word (1);
2436 /* Do some check to make sure that there really are two words. */
2437 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
2440 rl_point = orig_point;
2444 /* Get the text of the words. */
2445 word1 = rl_copy_text (w1_beg, w1_end);
2446 word2 = rl_copy_text (w2_beg, w2_end);
2448 /* We are about to do many insertions and deletions. Remember them
2449 as one operation. */
2450 rl_begin_undo_group ();
2452 /* Do the stuff at word2 first, so that we don't have to worry
2453 about word1 moving. */
2455 rl_delete_text (w2_beg, w2_end);
2456 rl_insert_text (word1);
2459 rl_delete_text (w1_beg, w1_end);
2460 rl_insert_text (word2);
2462 /* This is exactly correct since the text before this point has not
2463 changed in length. */
2466 /* I think that does it. */
2467 rl_end_undo_group ();
2474 /* Transpose the characters at point. If point is at the end of the line,
2475 then transpose the characters before point. */
2476 rl_transpose_chars (count, key)
2484 if (!rl_point || rl_end < 2)
2490 rl_begin_undo_group ();
2492 if (rl_point == rl_end)
2499 dummy[0] = the_line[rl_point];
2502 rl_delete_text (rl_point, rl_point + 1);
2505 if (rl_point > rl_end)
2507 else if (rl_point < 0)
2509 rl_insert_text (dummy);
2511 rl_end_undo_group ();
2515 /* **************************************************************** */
2517 /* Undo, and Undoing */
2519 /* **************************************************************** */
2521 /* The current undo list for THE_LINE. */
2522 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
2524 /* Remember how to undo something. Concatenate some undos if that
2527 rl_add_undo (what, start, end, text)
2528 enum undo_code what;
2532 UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
2534 temp->start = start;
2537 temp->next = rl_undo_list;
2538 rl_undo_list = temp;
2541 /* Free the existing undo list. */
2545 while (rl_undo_list)
2547 UNDO_LIST *release = rl_undo_list;
2548 rl_undo_list = rl_undo_list->next;
2550 if (release->what == UNDO_DELETE)
2551 free (release->text);
2555 rl_undo_list = (UNDO_LIST *)NULL;
2558 /* Undo the next thing in the list. Return 0 if there
2559 is nothing to undo, or non-zero if there was. */
2564 int waiting_for_begin = 0;
2572 switch (rl_undo_list->what) {
2574 /* Undoing deletes means inserting some text. */
2576 rl_point = rl_undo_list->start;
2577 rl_insert_text (rl_undo_list->text);
2578 free (rl_undo_list->text);
2581 /* Undoing inserts means deleting some text. */
2583 rl_delete_text (rl_undo_list->start, rl_undo_list->end);
2584 rl_point = rl_undo_list->start;
2587 /* Undoing an END means undoing everything 'til we get to
2590 waiting_for_begin++;
2593 /* Undoing a BEGIN means that we are done with this group. */
2595 if (waiting_for_begin)
2596 waiting_for_begin--;
2604 release = rl_undo_list;
2605 rl_undo_list = rl_undo_list->next;
2608 if (waiting_for_begin)
2614 /* Begin a group. Subsequent undos are undone as an atomic operation. */
2616 rl_begin_undo_group ()
2618 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
2622 /* End an undo group started with rl_begin_undo_group (). */
2624 rl_end_undo_group ()
2626 rl_add_undo (UNDO_END, 0, 0, 0);
2630 /* Save an undo entry for the text from START to END. */
2631 rl_modifying (start, end)
2643 char *temp = rl_copy_text (start, end);
2644 rl_begin_undo_group ();
2645 rl_add_undo (UNDO_DELETE, start, end, temp);
2646 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
2647 rl_end_undo_group ();
2652 /* Revert the current line to its previous state. */
2654 rl_revert_line (count, key)
2661 while (rl_undo_list)
2667 /* Do some undoing of things that were done. */
2669 rl_undo_command (count, key)
2673 return 0; /* Nothing to do. */
2688 /* **************************************************************** */
2690 /* History Utilities */
2692 /* **************************************************************** */
2694 /* We already have a history library, and that is what we use to control
2695 the history features of readline. However, this is our local interface
2696 to the history mechanism. */
2698 /* While we are editing the history, this is the saved
2699 version of the original line. */
2700 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
2702 /* Set the history pointer back to the last entry in the history. */
2704 start_using_history ()
2707 if (saved_line_for_history)
2708 _rl_free_history_entry (saved_line_for_history);
2710 saved_line_for_history = (HIST_ENTRY *)NULL;
2713 /* Free the contents (and containing structure) of a HIST_ENTRY. */
2715 _rl_free_history_entry (entry)
2725 /* Perhaps put back the current line if it has changed. */
2726 maybe_replace_line ()
2728 HIST_ENTRY *temp = current_history ();
2730 /* If the current line has changed, save the changes. */
2731 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
2733 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
2740 /* Put back the saved_line_for_history if there is one. */
2741 maybe_unsave_line ()
2743 if (saved_line_for_history)
2747 line_len = strlen (saved_line_for_history->line);
2749 if (line_len >= rl_line_buffer_len)
2750 rl_extend_line_buffer (line_len);
2752 strcpy (the_line, saved_line_for_history->line);
2753 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
2754 _rl_free_history_entry (saved_line_for_history);
2755 saved_line_for_history = (HIST_ENTRY *)NULL;
2756 rl_end = rl_point = strlen (the_line);
2763 /* Save the current line in saved_line_for_history. */
2766 if (!saved_line_for_history)
2768 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
2769 saved_line_for_history->line = savestring (the_line);
2770 saved_line_for_history->data = (char *)rl_undo_list;
2775 /* **************************************************************** */
2777 /* History Commands */
2779 /* **************************************************************** */
2781 /* Meta-< goes to the start of the history. */
2782 rl_beginning_of_history (count, key)
2785 return (rl_get_previous_history (1 + where_history ()));
2788 /* Meta-> goes to the end of the history. (The current line). */
2789 rl_end_of_history (count, key)
2792 maybe_replace_line ();
2794 maybe_unsave_line ();
2798 /* Move down to the next history line. */
2799 rl_get_next_history (count, key)
2802 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2805 return (rl_get_previous_history (-count));
2810 maybe_replace_line ();
2814 temp = next_history ();
2821 maybe_unsave_line ();
2826 line_len = strlen (temp->line);
2828 if (line_len >= rl_line_buffer_len)
2829 rl_extend_line_buffer (line_len);
2831 strcpy (the_line, temp->line);
2832 rl_undo_list = (UNDO_LIST *)temp->data;
2833 rl_end = rl_point = strlen (the_line);
2834 #if defined (VI_MODE)
2835 if (rl_editing_mode == vi_mode)
2837 #endif /* VI_MODE */
2842 /* Get the previous item out of our interactive history, making it the current
2843 line. If there is no previous history, just ding. */
2844 rl_get_previous_history (count, key)
2847 HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
2848 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2851 return (rl_get_next_history (-count));
2856 /* If we don't have a line saved, then save this one. */
2859 /* If the current line has changed, save the changes. */
2860 maybe_replace_line ();
2864 temp = previous_history ();
2872 /* If there was a large argument, and we moved back to the start of the
2873 history, that is not an error. So use the last value found. */
2874 if (!temp && old_temp)
2883 line_len = strlen (temp->line);
2885 if (line_len >= rl_line_buffer_len)
2886 rl_extend_line_buffer (line_len);
2888 strcpy (the_line, temp->line);
2889 rl_undo_list = (UNDO_LIST *)temp->data;
2890 rl_end = rl_point = line_len;
2892 #if defined (VI_MODE)
2893 if (rl_editing_mode == vi_mode)
2895 #endif /* VI_MODE */
2900 /* Make C be the next command to be executed. */
2904 rl_pending_input = c;
2908 /* **************************************************************** */
2910 /* The Mark and the Region. */
2912 /* **************************************************************** */
2914 /* Set the mark at POSITION. */
2915 rl_set_mark (position)
2918 if (position > rl_end)
2925 /* Exchange the position of mark and point. */
2926 rl_exchange_mark_and_point (count, key)
2929 if (rl_mark > rl_end)
2939 int temp = rl_point;
2948 /* **************************************************************** */
2950 /* Killing Mechanism */
2952 /* **************************************************************** */
2954 /* What we assume for a max number of kills. */
2955 #define DEFAULT_MAX_KILLS 10
2957 /* The real variable to look at to find out when to flush kills. */
2958 int rl_max_kills = DEFAULT_MAX_KILLS;
2960 /* Where to store killed text. */
2961 char **rl_kill_ring = (char **)NULL;
2963 /* Where we are in the kill ring. */
2964 int rl_kill_index = 0;
2966 /* How many slots we have in the kill ring. */
2967 int rl_kill_ring_length = 0;
2969 /* How to say that you only want to save a certain amount
2970 of kill material. */
2971 rl_set_retained_kills (num)
2977 /* The way to kill something. This appends or prepends to the last
2978 kill, if the last command was a kill command. if FROM is less
2979 than TO, then the text is appended, otherwise prepended. If the
2980 last command was not a kill command, then a new slot is made for
2982 rl_kill_text (from, to)
2988 /* Is there anything to kill? */
2991 last_command_was_kill++;
2995 text = rl_copy_text (from, to);
2997 /* Delete the copied text from the line. */
2998 rl_delete_text (from, to);
3000 /* First, find the slot to work with. */
3001 if (!last_command_was_kill)
3003 /* Get a new slot. */
3006 /* If we don't have any defined, then make one. */
3007 rl_kill_ring = (char **)
3008 xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
3009 rl_kill_ring[slot = 0] = (char *)NULL;
3013 /* We have to add a new slot on the end, unless we have
3014 exceeded the max limit for remembering kills. */
3015 slot = rl_kill_ring_length;
3016 if (slot == rl_max_kills)
3019 free (rl_kill_ring[0]);
3020 for (i = 0; i < slot; i++)
3021 rl_kill_ring[i] = rl_kill_ring[i + 1];
3025 slot = rl_kill_ring_length += 1;
3026 rl_kill_ring = (char **)xrealloc (rl_kill_ring, slot * sizeof (char *));
3028 rl_kill_ring[--slot] = (char *)NULL;
3032 slot = rl_kill_ring_length - 1;
3034 /* If the last command was a kill, prepend or append. */
3035 if (last_command_was_kill && rl_editing_mode != vi_mode)
3037 char *old = rl_kill_ring[slot];
3038 char *new = xmalloc (1 + strlen (old) + strlen (text));
3052 rl_kill_ring[slot] = new;
3056 rl_kill_ring[slot] = text;
3058 rl_kill_index = slot;
3059 last_command_was_kill++;
3063 /* Now REMEMBER! In order to do prepending or appending correctly, kill
3064 commands always make rl_point's original position be the FROM argument,
3065 and rl_point's extent be the TO argument. */
3067 /* **************************************************************** */
3069 /* Killing Commands */
3071 /* **************************************************************** */
3073 /* Delete the word at point, saving the text in the kill ring. */
3074 rl_kill_word (count, key)
3077 int orig_point = rl_point;
3080 return (rl_backward_kill_word (-count));
3083 rl_forward_word (count);
3085 if (rl_point != orig_point)
3086 rl_kill_text (orig_point, rl_point);
3088 rl_point = orig_point;
3093 /* Rubout the word before point, placing it on the kill ring. */
3094 rl_backward_kill_word (count, ignore)
3097 int orig_point = rl_point;
3100 return (rl_kill_word (-count));
3103 rl_backward_word (count);
3105 if (rl_point != orig_point)
3106 rl_kill_text (orig_point, rl_point);
3111 /* Kill from here to the end of the line. If DIRECTION is negative, kill
3112 back to the line start instead. */
3113 rl_kill_line (direction, ignore)
3114 int direction, ignore;
3116 int orig_point = rl_point;
3119 return (rl_backward_kill_line (1));
3122 rl_end_of_line (1, ignore);
3123 if (orig_point != rl_point)
3124 rl_kill_text (orig_point, rl_point);
3125 rl_point = orig_point;
3130 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
3131 forwards to the line end instead. */
3132 rl_backward_kill_line (direction, ignore)
3133 int direction, ignore;
3135 int orig_point = rl_point;
3138 return (rl_kill_line (1));
3145 rl_beg_of_line (1, ignore);
3146 rl_kill_text (orig_point, rl_point);
3152 /* Kill the whole line, no matter where point is. */
3153 rl_kill_full_line (count, ignore)
3156 rl_begin_undo_group ();
3158 rl_kill_text (rl_point, rl_end);
3159 rl_end_undo_group ();
3163 /* Yank back the last killed text. This ignores arguments. */
3164 rl_yank (count, ignore)
3169 rl_abort (count, ignore);
3173 rl_set_mark (rl_point);
3174 rl_insert_text (rl_kill_ring[rl_kill_index]);
3178 /* If the last command was yank, or yank_pop, and the text just
3179 before point is identical to the current kill item, then
3180 delete that text from the line, rotate the index down, and
3181 yank back some other text. */
3182 rl_yank_pop (count, key)
3187 if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
3194 l = strlen (rl_kill_ring[rl_kill_index]);
3195 if (((rl_point - l) >= 0) &&
3196 (strncmp (the_line + (rl_point - l),
3197 rl_kill_ring[rl_kill_index], l) == 0))
3199 rl_delete_text ((rl_point - l), rl_point);
3202 if (rl_kill_index < 0)
3203 rl_kill_index = rl_kill_ring_length - 1;
3214 /* Yank the COUNTth argument from the previous history line. */
3215 rl_yank_nth_arg (count, ignore)
3218 register HIST_ENTRY *entry = previous_history ();
3229 arg = history_arg_extract (count, count, entry->line);
3236 rl_begin_undo_group ();
3238 #if defined (VI_MODE)
3239 /* Vi mode always inserts a space before yanking the argument, and it
3240 inserts it right *after* rl_point. */
3241 if (rl_editing_mode == vi_mode)
3243 rl_vi_append_mode ();
3244 rl_insert_text (" ");
3246 #endif /* VI_MODE */
3248 rl_insert_text (arg);
3251 rl_end_undo_group ();
3255 /* Yank the last argument from the previous history line. This `knows'
3256 how rl_yank_nth_arg treats a count of `$'. With an argument, this
3257 behaves the same as rl_yank_nth_arg. */
3259 rl_yank_last_arg (count, key)
3262 if (rl_explicit_arg)
3263 return (rl_yank_nth_arg (count, key));
3265 return (rl_yank_nth_arg ('$', key));
3268 /* How to toggle back and forth between editing modes. */
3269 rl_vi_editing_mode (count, key)
3272 #if defined (VI_MODE)
3273 rl_editing_mode = vi_mode;
3274 rl_vi_insertion_mode ();
3276 #endif /* VI_MODE */
3279 rl_emacs_editing_mode (count, key)
3282 rl_editing_mode = emacs_mode;
3283 _rl_keymap = emacs_standard_keymap;
3288 /* **************************************************************** */
3290 /* USG (System V) Support */
3292 /* **************************************************************** */
3301 #if defined (__GO32__)
3303 return (getkey () & 0x7F);
3304 #endif /* __GO32__ */
3308 result = read (fileno (stream), &c, sizeof (unsigned char));
3310 if (result == sizeof (unsigned char))
3313 /* If zero characters are returned, then the file that we are
3314 reading from is empty! Return EOF in that case. */
3318 #if defined (EWOULDBLOCK)
3319 if (errno == EWOULDBLOCK)
3323 if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3325 if (flags & O_NDELAY)
3328 fcntl (fileno (stream), F_SETFL, flags);
3333 #endif /* EWOULDBLOCK */
3335 #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
3336 if (errno == EAGAIN)
3340 if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3342 if (flags & O_NONBLOCK)
3344 flags &= ~O_NONBLOCK;
3345 fcntl (fileno (stream), F_SETFL, flags);
3349 #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
3351 #if !defined (__GO32__)
3352 /* If the error that we received was SIGINT, then try again,
3353 this is simply an interrupted system call to read ().
3354 Otherwise, some error ocurred, also signifying EOF. */
3357 #endif /* !__GO32__ */
3361 #if !defined (SHELL)
3365 /* Backwards compatibilty, now that savestring has been removed from
3366 all `public' readline header files. */
3371 return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));
3375 /* Function equivalents for the macros defined in chartypes.h. */
3381 return (isupper (c));
3389 return (islower (c));
3392 #undef pure_alphabetic
3397 return (isupper (c) || islower (c));
3405 return (isdigit (c));
3413 return (isupper (c) ? tolower (c) : c);
3421 return (islower (c) ? toupper (c) : c);
3429 return (isdigit (c) ? c - '0' : c);
3432 #if defined (STATIC_MALLOC)
3434 /* **************************************************************** */
3436 /* xmalloc and xrealloc () */
3438 /* **************************************************************** */
3440 static void memory_error_and_abort ();
3446 char *temp = (char *)malloc (bytes);
3449 memory_error_and_abort ();
3454 xrealloc (pointer, bytes)
3461 temp = (char *)malloc (bytes);
3463 temp = (char *)realloc (pointer, bytes);
3466 memory_error_and_abort ();
3472 memory_error_and_abort ()
3474 fprintf (stderr, "readline: Out of virtual memory!\n");
3477 #endif /* STATIC_MALLOC */
3480 /* **************************************************************** */
3482 /* Testing Readline */
3484 /* **************************************************************** */
3490 HIST_ENTRY **history_list ();
3491 char *temp = (char *)NULL;
3492 char *prompt = "readline% ";
3497 temp = readline (prompt);
3503 /* If there is anything on the line, print it and remember it. */
3506 fprintf (stderr, "%s\r\n", temp);
3510 /* Check for `command' that we handle. */
3511 if (strcmp (temp, "quit") == 0)
3514 if (strcmp (temp, "list") == 0)
3516 HIST_ENTRY **list = history_list ();
3520 for (i = 0; list[i]; i++)
3522 fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
3523 free (list[i]->line);
3537 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"