1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 1, or
12 (at your option) any later version.
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 675 Mass Ave, Cambridge, MA 02139, USA. */
32 /* This is needed to include support for TIOCGWINSZ and window resizing. */
33 #if defined (OSF1) || defined (BSD386) || defined (_386BSD) || defined (AIX)
34 # include <sys/ioctl.h>
37 #if defined (HAVE_UNISTD_H)
42 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
47 extern char * getenv ();
52 /* System-specific feature definitions and include files. */
55 /* Some standard library routines. */
59 /* NOTE: Functions and variables prefixed with `_rl_' are
60 pseudo-global: they are global so they can be shared
61 between files in the readline library, but are not intended
62 to be visible to readline callers. */
64 /* Functions imported from other files in the library. */
65 extern char *tgetstr ();
66 extern void rl_prep_terminal (), rl_deprep_terminal ();
67 extern Function
*rl_function_of_keyseq ();
68 extern char *tilde_expand ();
70 /* External redisplay functions and variables from display.c */
71 extern void rl_redisplay ();
72 extern void _rl_move_vert ();
74 extern void _rl_erase_at_end_of_line ();
75 extern void _rl_move_cursor_relative ();
77 extern int _rl_vis_botlin
;
78 extern int _rl_last_c_pos
;
79 extern int rl_display_fixed
;
81 /* Variables imported from complete.c. */
82 extern char *rl_completer_word_break_characters
;
83 extern char *rl_basic_word_break_characters
;
84 extern Function
*rl_symbolic_link_hook
;
85 extern int rl_completion_query_items
;
86 extern int rl_complete_with_tilde_expansion
;
88 /* Forward declarations used in this file. */
90 void free_history_entry ();
91 void _rl_output_character_function ();
92 void _rl_set_screen_size ();
95 static void readline_default_bindings ();
100 # undef HANDLE_SIGNALS
104 /* **************************************************************** */
106 /* Line editing input utility */
108 /* **************************************************************** */
110 static char *LibraryVersion
= "2.0 (Cygnus)";
112 /* A pointer to the keymap that is currently in use.
113 By default, it is the standard emacs keymap. */
114 Keymap _rl_keymap
= emacs_standard_keymap
;
116 /* The current style of editing. */
117 int rl_editing_mode
= emacs_mode
;
119 /* Non-zero if the previous command was a kill command. */
120 static int last_command_was_kill
= 0;
122 /* The current value of the numeric argument specified by the user. */
123 int rl_numeric_arg
= 1;
125 /* Non-zero if an argument was typed. */
126 int rl_explicit_arg
= 0;
128 /* Temporary value used while generating the argument. */
131 /* Non-zero means we have been called at least once before. */
132 static int rl_initialized
= 0;
134 /* If non-zero, this program is running in an EMACS buffer. */
135 static char *running_in_emacs
= (char *)NULL
;
137 /* The current offset in the current input line. */
140 /* Mark in the current input line. */
143 /* Length of the current input line. */
146 /* Make this non-zero to return the current input_line. */
149 /* The last function executed by readline. */
150 Function
*rl_last_func
= (Function
*)NULL
;
152 /* Top level environment for readline_internal (). */
153 static jmp_buf readline_top_level
;
155 /* The streams we interact with. */
156 static FILE *in_stream
, *out_stream
;
158 /* The names of the streams that we do input and output to. */
159 FILE *rl_instream
= (FILE *)NULL
;
160 FILE *rl_outstream
= (FILE *)NULL
;
162 /* Non-zero means echo characters as they are read. */
163 int readline_echoing_p
= 1;
165 /* Current prompt. */
168 /* The number of characters read in order to type this complete command. */
169 int rl_key_sequence_length
= 0;
171 /* If non-zero, then this is the address of a function to call just
172 before readline_internal () prints the first prompt. */
173 Function
*rl_startup_hook
= (Function
*)NULL
;
175 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
176 static char *the_line
;
178 /* The character that can generate an EOF. Really read from
179 the terminal driver... just defaulted here. */
180 int _rl_eof_char
= CTRL ('D');
182 /* Non-zero makes this the next keystroke to read. */
183 int rl_pending_input
= 0;
185 /* Pointer to a useful terminal name. */
186 char *rl_terminal_name
= (char *)NULL
;
188 /* Non-zero means to always use horizontal scrolling in line display. */
189 int _rl_horizontal_scroll_mode
= 0;
191 /* Non-zero means to display an asterisk at the starts of history lines
192 which have been modified. */
193 int _rl_mark_modified_lines
= 0;
195 /* Non-zero means to use a visible bell if one is available rather than
196 simply ringing the terminal bell. */
197 int _rl_prefer_visible_bell
= 0;
199 /* Line buffer and maintenence. */
200 char *rl_line_buffer
= (char *)NULL
;
201 int rl_line_buffer_len
= 0;
202 #define DEFAULT_BUFFER_SIZE 256
204 #if defined (VISIBLE_STATS)
205 int rl_visible_stats
= 0;
206 #endif /* VISIBLE_STATS */
209 /* **************************************************************** */
211 /* `Forward' declarations */
213 /* **************************************************************** */
215 /* Non-zero means do not parse any lines other than comments and
216 parser directives. */
217 unsigned char _rl_parsing_conditionalized_out
= 0;
219 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
220 static int defining_kbd_macro
= 0;
222 /* Non-zero means to convert characters with the meta bit set to
223 escape-prefixed characters so we can indirect through
224 emacs_meta_keymap or vi_escape_keymap. */
225 int _rl_convert_meta_chars_to_ascii
= 1;
227 static int doing_an_undo
;
229 /* **************************************************************** */
231 /* Top Level Functions */
233 /* **************************************************************** */
235 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
236 int _rl_meta_flag
= 0; /* Forward declaration */
238 /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
239 none. A return value of NULL means that EOF was encountered. */
244 char *readline_internal ();
249 /* If we are at EOF return a NULL string. */
250 if (rl_pending_input
== EOF
)
252 rl_pending_input
= 0;
253 return ((char *)NULL
);
257 rl_prep_terminal (_rl_meta_flag
);
259 #if defined (HANDLE_SIGNALS)
263 value
= readline_internal ();
264 rl_deprep_terminal ();
266 #if defined (HANDLE_SIGNALS)
273 /* Read a line of input from the global rl_instream, doing output on
274 the global rl_outstream.
275 If rl_prompt is non-null, then that is our prompt. */
279 int lastc
, c
, eof_found
;
281 in_stream
= rl_instream
;
282 out_stream
= rl_outstream
;
288 (*rl_startup_hook
) ();
290 if (!readline_echoing_p
)
294 fprintf (out_stream
, "%s", rl_prompt
);
302 #if defined (VI_MODE)
303 if (rl_editing_mode
== vi_mode
)
304 rl_vi_insertion_mode ();
310 int lk
= last_command_was_kill
;
313 code
= setjmp (readline_top_level
);
318 if (!rl_pending_input
)
320 /* Then initialize the argument and number of keys read. */
322 rl_key_sequence_length
= 0;
327 /* EOF typed to a non-blank line is a <NL>. */
328 if (c
== EOF
&& rl_end
)
331 /* The character _rl_eof_char typed to blank line, and not as the
332 previous character is interpreted as EOF. */
333 if (((c
== _rl_eof_char
&& lastc
!= c
) || c
== EOF
) && !rl_end
)
340 rl_dispatch (c
, _rl_keymap
);
342 /* If there was no change in last_command_was_kill, then no kill
343 has taken place. Note that if input is pending we are reading
344 a prefix command, so nothing has changed yet. */
345 if (!rl_pending_input
)
347 if (lk
== last_command_was_kill
)
348 last_command_was_kill
= 0;
351 #if defined (VI_MODE)
352 /* In vi mode, when you exit insert mode, the cursor moves back
353 over the previous character. We explicitly check for that here. */
354 if (rl_editing_mode
== vi_mode
&& _rl_keymap
== vi_movement_keymap
)
362 /* Restore the original of this history line, iff the line that we
363 are editing was originally in the history, AND the line has changed. */
365 HIST_ENTRY
*entry
= current_history ();
367 if (entry
&& rl_undo_list
)
369 char *temp
= savestring (the_line
);
371 entry
= replace_history_entry (where_history (), the_line
,
373 free_history_entry (entry
);
375 strcpy (the_line
, temp
);
380 /* At any rate, it is highly likely that this line has an undo list. Get
388 return (savestring (the_line
));
392 /* **************************************************************** */
394 /* Character Input Buffering */
396 /* **************************************************************** */
398 static int pop_index
= 0, push_index
= 0, ibuffer_len
= 511;
399 static unsigned char ibuffer
[512];
401 /* Non-null means it is a pointer to a function to run while waiting for
403 Function
*rl_event_hook
= (Function
*)NULL
;
405 #define any_typein (push_index != pop_index)
407 /* Add KEY to the buffer of characters to be read. */
414 rl_pending_input
= EOF
;
416 ibuffer
[push_index
++] = key
;
417 if (push_index
>= ibuffer_len
)
421 /* Return the amount of space available in the
422 buffer for stuffing characters. */
426 if (pop_index
> push_index
)
427 return (pop_index
- push_index
);
429 return (ibuffer_len
- (push_index
- pop_index
));
432 /* Get a key from the buffer of characters to be read.
433 Return the key in KEY.
434 Result is KEY if there was a key, or 0 if there wasn't. */
439 if (push_index
== pop_index
)
442 *key
= ibuffer
[pop_index
++];
444 if (pop_index
>= ibuffer_len
)
450 /* Stuff KEY into the *front* of the input buffer.
451 Returns non-zero if successful, zero if there is
452 no space left in the buffer. */
457 if (ibuffer_space ())
461 pop_index
= ibuffer_len
- 1;
462 ibuffer
[pop_index
] = key
;
468 /* If a character is available to be read, then read it
469 and stuff it into IBUFFER. Otherwise, just return. */
481 else if (kbhit () && ibuffer_space ())
482 rl_stuff_char (getkey ());
485 int tty
= fileno (in_stream
);
486 register int tem
, result
= -1;
490 #if defined (FIONREAD)
491 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
494 #if defined (O_NDELAY)
499 flags
= fcntl (tty
, F_GETFL
, 0);
501 fcntl (tty
, F_SETFL
, (flags
| O_NDELAY
));
502 chars_avail
= read (tty
, &input
, 1);
504 fcntl (tty
, F_SETFL
, flags
);
505 if (chars_avail
== -1 && errno
== EAGAIN
)
508 #endif /* O_NDELAY */
510 /* If there's nothing available, don't waste time trying to read
512 if (chars_avail
== 0)
515 tem
= ibuffer_space ();
517 if (chars_avail
> tem
)
520 /* One cannot read all of the available input. I can only read a single
521 character at a time, or else programs which require input can be
522 thwarted. If the buffer is larger than one character, I lose.
524 if (tem
< ibuffer_len
)
529 while (chars_avail
--)
530 rl_stuff_char (rl_getc (in_stream
));
535 rl_stuff_char (input
);
537 #endif /* def __GO32__/else */
540 static int next_macro_key ();
541 /* Read a key, including pending input. */
547 rl_key_sequence_length
++;
549 if (rl_pending_input
)
551 c
= rl_pending_input
;
552 rl_pending_input
= 0;
556 /* If input is coming from a macro, then use that. */
557 if (c
= next_macro_key ())
560 /* If the user has an event function, then call it periodically. */
563 while (rl_event_hook
&& !rl_get_char (&c
))
571 if (!rl_get_char (&c
))
572 c
= rl_getc (in_stream
);
579 /* Found later in this file. */
580 static void add_macro_char (), with_macro_input ();
582 /* Do the command associated with KEY in MAP.
583 If the associated command is really a keymap, then read
584 another key, and dispatch into that map. */
586 rl_dispatch (key
, map
)
590 #if defined (VI_MODE)
591 extern int _rl_vi_last_command
, _rl_vi_last_repeat
, _rl_vi_last_arg_sign
;
594 if (defining_kbd_macro
)
595 add_macro_char (key
);
597 if (META_CHAR (key
) && _rl_convert_meta_chars_to_ascii
)
599 if (map
[ESC
].type
== ISKMAP
)
601 map
= (Keymap
)map
[ESC
].function
;
603 rl_key_sequence_length
+= 2;
604 rl_dispatch (key
, map
);
611 switch (map
[key
].type
)
615 Function
*func
= map
[key
].function
;
617 if (func
!= (Function
*)NULL
)
619 /* Special case rl_do_lowercase_version (). */
620 if (func
== rl_do_lowercase_version
)
622 rl_dispatch (to_lower (key
), map
);
626 (*map
[key
].function
)(rl_numeric_arg
* rl_arg_sign
, key
);
628 /* If we have input pending, then the last command was a prefix
629 command. Don't change the state of rl_last_func. Otherwise,
630 remember the last command executed in this variable. */
631 if (!rl_pending_input
)
632 rl_last_func
= map
[key
].function
;
643 if (map
[key
].function
!= (Function
*)NULL
)
647 rl_key_sequence_length
++;
648 newkey
= rl_read_key ();
649 rl_dispatch (newkey
, (Keymap
)map
[key
].function
);
659 if (map
[key
].function
!= (Function
*)NULL
)
663 macro
= savestring ((char *)map
[key
].function
);
664 with_macro_input (macro
);
669 #if defined (VI_MODE)
670 if (rl_editing_mode
== vi_mode
&& _rl_keymap
== vi_movement_keymap
&&
671 rl_vi_textmod_command (key
))
673 _rl_vi_last_command
= key
;
674 _rl_vi_last_repeat
= rl_numeric_arg
;
675 _rl_vi_last_arg_sign
= rl_arg_sign
;
681 /* **************************************************************** */
683 /* Hacking Keyboard Macros */
685 /* **************************************************************** */
687 /* The currently executing macro string. If this is non-zero,
688 then it is a malloc ()'ed string where input is coming from. */
689 static char *executing_macro
= (char *)NULL
;
691 /* The offset in the above string to the next character to be read. */
692 static int executing_macro_index
= 0;
694 /* The current macro string being built. Characters get stuffed
695 in here by add_macro_char (). */
696 static char *current_macro
= (char *)NULL
;
698 /* The size of the buffer allocated to current_macro. */
699 static int current_macro_size
= 0;
701 /* The index at which characters are being added to current_macro. */
702 static int current_macro_index
= 0;
704 /* A structure used to save nested macro strings.
705 It is a linked list of string/index for each saved macro. */
707 struct saved_macro
*next
;
712 /* The list of saved macros. */
713 struct saved_macro
*macro_list
= (struct saved_macro
*)NULL
;
715 /* Forward declarations of static functions. Thank you C. */
716 static void push_executing_macro (), pop_executing_macro ();
718 /* This one has to be declared earlier in the file. */
719 /* static void add_macro_char (); */
721 /* Set up to read subsequent input from STRING.
722 STRING is free ()'ed when we are done with it. */
724 with_macro_input (string
)
727 push_executing_macro ();
728 executing_macro
= string
;
729 executing_macro_index
= 0;
732 /* Return the next character available from a macro, or 0 if
733 there are no macro characters. */
737 if (!executing_macro
)
740 if (!executing_macro
[executing_macro_index
])
742 pop_executing_macro ();
743 return (next_macro_key ());
746 return (executing_macro
[executing_macro_index
++]);
749 /* Save the currently executing macro on a stack of saved macros. */
751 push_executing_macro ()
753 struct saved_macro
*saver
;
755 saver
= (struct saved_macro
*)xmalloc (sizeof (struct saved_macro
));
756 saver
->next
= macro_list
;
757 saver
->index
= executing_macro_index
;
758 saver
->string
= executing_macro
;
763 /* Discard the current macro, replacing it with the one
764 on the top of the stack of saved macros. */
766 pop_executing_macro ()
769 free (executing_macro
);
771 executing_macro
= (char *)NULL
;
772 executing_macro_index
= 0;
776 struct saved_macro
*disposer
= macro_list
;
777 executing_macro
= macro_list
->string
;
778 executing_macro_index
= macro_list
->index
;
779 macro_list
= macro_list
->next
;
784 /* Add a character to the macro being built. */
789 if (current_macro_index
+ 1 >= current_macro_size
)
792 current_macro
= (char *)xmalloc (current_macro_size
= 25);
795 (char *)xrealloc (current_macro
, current_macro_size
+= 25);
798 current_macro
[current_macro_index
++] = c
;
799 current_macro
[current_macro_index
] = '\0';
802 /* Begin defining a keyboard macro.
803 Keystrokes are recorded as they are executed.
804 End the definition with rl_end_kbd_macro ().
805 If a numeric argument was explicitly typed, then append this
806 definition to the end of the existing macro, and start by
807 re-executing the existing macro. */
808 rl_start_kbd_macro (ignore1
, ignore2
)
809 int ignore1
, ignore2
;
811 if (defining_kbd_macro
)
817 with_macro_input (savestring (current_macro
));
820 current_macro_index
= 0;
822 defining_kbd_macro
= 1;
825 /* Stop defining a keyboard macro.
826 A numeric argument says to execute the macro right now,
827 that many times, counting the definition as the first time. */
828 rl_end_kbd_macro (count
, ignore
)
831 if (!defining_kbd_macro
)
834 current_macro_index
-= (rl_key_sequence_length
- 1);
835 current_macro
[current_macro_index
] = '\0';
837 defining_kbd_macro
= 0;
839 rl_call_last_kbd_macro (--count
, 0);
842 /* Execute the most recently defined keyboard macro.
843 COUNT says how many times to execute it. */
844 rl_call_last_kbd_macro (count
, ignore
)
851 with_macro_input (savestring (current_macro
));
855 /* **************************************************************** */
857 /* Initializations */
859 /* **************************************************************** */
861 /* Initliaze readline (and terminal if not already). */
864 /* If we have never been called before, initialize the
865 terminal and data structures. */
868 readline_initialize_everything ();
872 /* Initalize the current line information. */
873 rl_point
= rl_end
= 0;
874 the_line
= rl_line_buffer
;
877 /* We aren't done yet. We haven't even gotten started yet! */
880 /* Tell the history routines what is going on. */
881 start_using_history ();
883 /* Make the display buffer match the state of the line. */
884 rl_reset_line_state ();
886 /* No such function typed yet. */
887 rl_last_func
= (Function
*)NULL
;
889 /* Parsing of key-bindings begins in an enabled state. */
890 _rl_parsing_conditionalized_out
= 0;
893 /* Initialize the entire state of the world. */
894 readline_initialize_everything ()
896 /* Find out if we are running in Emacs. */
897 running_in_emacs
= getenv ("EMACS");
899 /* Set up input and output if they are not already set up. */
904 rl_outstream
= stdout
;
906 /* Bind in_stream and out_stream immediately. These values may change,
907 but they may also be used before readline_internal () is called. */
908 in_stream
= rl_instream
;
909 out_stream
= rl_outstream
;
911 /* Allocate data structures. */
914 (char *)xmalloc (rl_line_buffer_len
= DEFAULT_BUFFER_SIZE
);
916 /* Initialize the terminal interface. */
917 init_terminal_io ((char *)NULL
);
919 /* Bind tty characters to readline functions. */
920 readline_default_bindings ();
922 /* Initialize the function names. */
923 rl_initialize_funmap ();
925 /* Read in the init file. */
926 rl_read_init_file ((char *)NULL
);
928 /* If the completion parser's default word break characters haven't
929 been set yet, then do so now. */
931 if (rl_completer_word_break_characters
== (char *)NULL
)
932 rl_completer_word_break_characters
= rl_basic_word_break_characters
;
936 /* If this system allows us to look at the values of the regular
937 input editing characters, then bind them to their readline
938 equivalents, iff the characters are not bound to keymaps. */
940 readline_default_bindings ()
942 rltty_set_default_bindings (_rl_keymap
);
946 /* **************************************************************** */
948 /* Numeric Arguments */
950 /* **************************************************************** */
952 /* Handle C-u style numeric args, as well as M--, and M-digits. */
954 /* Add the current digit to the argument in progress. */
955 rl_digit_argument (ignore
, key
)
958 rl_pending_input
= key
;
962 /* What to do when you abort reading an argument. */
963 rl_discard_argument ()
970 /* Create a default argument. */
973 rl_numeric_arg
= rl_arg_sign
= 1;
977 /* C-u, universal argument. Multiply the current argument by 4.
978 Read a key. If the key has nothing to do with arguments, then
979 dispatch on it. If the key is the abort character then abort. */
980 rl_universal_argument ()
991 rl_message ("(arg: %d) ", rl_arg_sign
* rl_numeric_arg
);
992 key
= c
= rl_read_key ();
994 if (_rl_keymap
[c
].type
== ISFUNC
&&
995 _rl_keymap
[c
].function
== rl_universal_argument
)
1003 if (rl_explicit_arg
)
1004 rl_numeric_arg
= (rl_numeric_arg
* 10) + (c
- '0');
1006 rl_numeric_arg
= (c
- '0');
1007 rl_explicit_arg
= 1;
1011 if (c
== '-' && !rl_explicit_arg
)
1018 rl_clear_message ();
1019 rl_dispatch (key
, _rl_keymap
);
1026 /* **************************************************************** */
1028 /* Terminal and Termcap */
1030 /* **************************************************************** */
1032 static char *term_buffer
= (char *)NULL
;
1033 static char *term_string_buffer
= (char *)NULL
;
1035 /* Non-zero means this terminal can't really do anything. */
1037 /* On Solaris2, sys/types.h #includes sys/reg.h, which #defines PC.
1038 Unfortunately, PC is a global variable used by the termcap library. */
1041 #if !defined (__linux__)
1044 #endif /* __linux__ */
1046 /* Some strings to control terminal actions. These are output by tputs (). */
1047 char *term_goto
, *term_clreol
, *term_cr
, *term_clrpag
, *term_backspace
;
1049 int screenwidth
, screenheight
;
1051 /* Non-zero if we determine that the terminal can do character insertion. */
1052 int terminal_can_insert
= 0;
1054 /* How to insert characters. */
1055 char *term_im
, *term_ei
, *term_ic
, *term_ip
, *term_IC
;
1057 /* How to delete characters. */
1058 char *term_dc
, *term_DC
;
1060 #if defined (HACK_TERMCAP_MOTION)
1061 char *term_forward_char
;
1062 #endif /* HACK_TERMCAP_MOTION */
1064 /* How to go up a line. */
1067 /* True if we have funny auto-line-wrap ("am" and "xn"). */
1070 /* A visible bell, if the terminal can be made to flash the screen. */
1073 /* Non-zero means that this terminal has a meta key. */
1076 /* The string to write to turn on the meta key, if this term has one. */
1079 /* The string to write to turn off the meta key, if this term has one. */
1082 /* The key sequences output by the arrow keys, if this terminal has any. */
1083 char *term_ku
, *term_kd
, *term_kr
, *term_kl
;
1085 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1087 rl_reset_terminal (terminal_name
)
1088 char *terminal_name
;
1090 init_terminal_io (terminal_name
);
1093 /* Set readline's idea of the screen size. TTY is a file descriptor open
1094 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
1095 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
1096 non-null serve to check whether or not we have initialized termcap. */
1098 _rl_set_screen_size (tty
, ignore_env
)
1099 int tty
, ignore_env
;
1101 #if defined (TIOCGWINSZ)
1102 struct winsize window_size
;
1103 #endif /* TIOCGWINSZ */
1105 #if defined (TIOCGWINSZ)
1106 if (ioctl (tty
, TIOCGWINSZ
, &window_size
) == 0)
1108 screenwidth
= (int) window_size
.ws_col
;
1109 screenheight
= (int) window_size
.ws_row
;
1111 #endif /* TIOCGWINSZ */
1113 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
1115 if (screenwidth
<= 0)
1119 if (!ignore_env
&& (sw
= getenv ("COLUMNS")))
1120 screenwidth
= atoi (sw
);
1122 if (screenwidth
<= 0 && term_string_buffer
)
1123 screenwidth
= tgetnum ("co");
1126 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
1128 if (screenheight
<= 0)
1132 if (!ignore_env
&& (sh
= getenv ("LINES")))
1133 screenheight
= atoi (sh
);
1135 if (screenheight
<= 0 && term_string_buffer
)
1136 screenheight
= tgetnum ("li");
1139 /* If all else fails, default to 80x24 terminal. */
1140 if (screenwidth
<= 0)
1143 if (screenheight
<= 0)
1147 /* If we're being compiled as part of bash, set the environment
1148 variables $LINES and $COLUMNS to new values. */
1149 set_lines_and_columns (screenheight
, screenwidth
);
1152 /* If we don't have xn (most modern terminals do),
1153 don't use the last column. */
1158 init_terminal_io (terminal_name
)
1159 char *terminal_name
;
1162 screenwidth
= ScreenCols ();
1163 screenheight
= ScreenRows ();
1165 term_im
= term_ei
= term_ic
= term_IC
= (char *)NULL
;
1166 term_up
= term_dc
= term_DC
= visible_bell
= (char *)NULL
;
1168 /* Does the _GO32_ have a meta key? I don't know. */
1170 term_mm
= term_mo
= (char *)NULL
;
1172 /* It probably has arrow keys, but I don't know what they are. */
1173 term_ku
= term_kd
= term_kr
= term_kl
= (char *)NULL
;
1175 #if defined (HACK_TERMCAP_MOTION)
1176 term_forward_char
= (char *)NULL
;
1178 terminal_can_insert
= term_xn
= 0;
1180 #else /* !__GO32__ */
1181 char *term
, *buffer
;
1184 term
= terminal_name
? terminal_name
: getenv ("TERM");
1186 if (!term_string_buffer
)
1187 term_string_buffer
= (char *)xmalloc (2048);
1190 term_buffer
= (char *)xmalloc (2048);
1192 buffer
= term_string_buffer
;
1194 term_clrpag
= term_cr
= term_clreol
= (char *)NULL
;
1199 if (tgetent (term_buffer
, term
) <= 0)
1205 term_im
= term_ei
= term_ic
= term_IC
= (char *)NULL
;
1206 term_up
= term_dc
= term_DC
= visible_bell
= (char *)NULL
;
1207 term_ku
= term_kd
= term_kl
= term_kr
= (char *)NULL
;
1208 #if defined (HACK_TERMCAP_MOTION)
1209 term_forward_char
= (char *)NULL
;
1211 terminal_can_insert
= term_xn
= 0;
1215 BC
= tgetstr ("pc", &buffer
);
1216 PC
= buffer
? *buffer
: 0;
1218 term_backspace
= tgetstr ("le", &buffer
);
1220 term_cr
= tgetstr ("cr", &buffer
);
1221 term_clreol
= tgetstr ("ce", &buffer
);
1222 term_clrpag
= tgetstr ("cl", &buffer
);
1227 #if defined (HACK_TERMCAP_MOTION)
1228 term_forward_char
= tgetstr ("nd", &buffer
);
1229 #endif /* HACK_TERMCAP_MOTION */
1232 tty
= fileno (rl_instream
);
1236 screenwidth
= screenheight
= 0;
1238 term_xn
= tgetflag ("am", &buffer
) && tgetflag ("xn", &buffer
);
1240 _rl_set_screen_size (tty
, 0);
1242 term_im
= tgetstr ("im", &buffer
);
1243 term_ei
= tgetstr ("ei", &buffer
);
1244 term_IC
= tgetstr ("IC", &buffer
);
1245 term_ic
= tgetstr ("ic", &buffer
);
1247 /* "An application program can assume that the terminal can do
1248 character insertion if *any one of* the capabilities `IC',
1249 `im', `ic' or `ip' is provided." But we can't do anything if
1250 only `ip' is provided, so... */
1251 terminal_can_insert
= (term_IC
|| term_im
|| term_ic
);
1253 term_up
= tgetstr ("up", &buffer
);
1254 term_dc
= tgetstr ("dc", &buffer
);
1255 term_DC
= tgetstr ("DC", &buffer
);
1257 visible_bell
= tgetstr ("vb", &buffer
);
1259 /* Check to see if this terminal has a meta key. */
1260 term_has_meta
= (tgetflag ("km") || tgetflag ("MT"));
1263 term_mm
= tgetstr ("mm", &buffer
);
1264 term_mo
= tgetstr ("mo", &buffer
);
1268 term_mm
= (char *)NULL
;
1269 term_mo
= (char *)NULL
;
1272 /* Attempt to find and bind the arrow keys. Do not override already
1273 bound keys in an overzealous attempt, however. */
1274 term_ku
= tgetstr ("ku", &buffer
);
1275 term_kd
= tgetstr ("kd", &buffer
);
1276 term_kr
= tgetstr ("kr", &buffer
);
1277 term_kl
= tgetstr ("kl", &buffer
);
1283 func
= rl_function_of_keyseq (term_ku
, _rl_keymap
, (int *)NULL
);
1285 if (!func
|| func
== rl_do_lowercase_version
)
1286 rl_set_key (term_ku
, rl_get_previous_history
, _rl_keymap
);
1293 func
= rl_function_of_keyseq (term_kd
, _rl_keymap
, (int *)NULL
);
1295 if (!func
|| func
== rl_do_lowercase_version
)
1296 rl_set_key (term_kd
, rl_get_next_history
, _rl_keymap
);
1303 func
= rl_function_of_keyseq (term_kr
, _rl_keymap
, (int *)NULL
);
1305 if (!func
|| func
== rl_do_lowercase_version
)
1306 rl_set_key (term_kr
, rl_forward
, _rl_keymap
);
1313 func
= rl_function_of_keyseq (term_kl
, _rl_keymap
, (int *)NULL
);
1315 if (!func
|| func
== rl_do_lowercase_version
)
1316 rl_set_key (term_kl
, rl_backward
, _rl_keymap
);
1318 #endif /* !__GO32__ */
1321 /* A function for the use of tputs () */
1323 _rl_output_character_function (c
)
1326 putc (c
, out_stream
);
1329 /* Write COUNT characters from STRING to the output stream. */
1331 _rl_output_some_chars (string
, count
)
1335 fwrite (string
, 1, count
, out_stream
);
1340 /* Move the cursor back. */
1348 for (i
= 0; i
< count
; i
++)
1349 tputs (term_backspace
, 1, _rl_output_character_function
);
1351 #endif /* !__GO32__ */
1352 for (i
= 0; i
< count
; i
++)
1353 putc ('\b', out_stream
);
1356 /* Move to the start of the next line. */
1359 #if defined (NEW_TTY_DRIVER)
1360 tputs (term_cr
, 1, _rl_output_character_function
);
1361 #endif /* NEW_TTY_DRIVER */
1362 putc ('\n', out_stream
);
1366 /* **************************************************************** */
1368 /* Utility Functions */
1370 /* **************************************************************** */
1372 /* Return 0 if C is not a member of the class of characters that belong
1373 in words, or 1 if it is. */
1375 int allow_pathname_alphabetic_chars
= 0;
1376 char *pathname_alphabetic_chars
= "/-_=~.#$";
1382 if (pure_alphabetic (c
) || (numeric (c
)))
1385 if (allow_pathname_alphabetic_chars
)
1386 return ((int) strchr (pathname_alphabetic_chars
, c
));
1391 /* Return non-zero if C is a numeric character. */
1396 return (c
>= '0' && c
<= '9');
1399 /* Ring the terminal bell. */
1403 if (readline_echoing_p
)
1406 if (_rl_prefer_visible_bell
&& visible_bell
)
1407 tputs (visible_bell
, 1, _rl_output_character_function
);
1409 #endif /* !__GO32__ */
1411 fprintf (stderr
, "\007");
1418 /* How to abort things. */
1422 rl_clear_message ();
1423 rl_init_argument ();
1424 rl_pending_input
= 0;
1426 defining_kbd_macro
= 0;
1427 while (executing_macro
)
1428 pop_executing_macro ();
1430 rl_last_func
= (Function
*)NULL
;
1431 longjmp (readline_top_level
, 1);
1434 /* Return a copy of the string between FROM and TO.
1435 FROM is inclusive, TO is not. */
1437 rl_copy_text (from
, to
)
1440 register int length
;
1443 /* Fix it if the caller is confused. */
1452 copy
= (char *)xmalloc (1 + length
);
1453 strncpy (copy
, the_line
+ from
, length
);
1454 copy
[length
] = '\0';
1458 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
1461 rl_extend_line_buffer (len
)
1464 while (len
>= rl_line_buffer_len
)
1467 (rl_line_buffer
, rl_line_buffer_len
+= DEFAULT_BUFFER_SIZE
);
1469 the_line
= rl_line_buffer
;
1473 /* **************************************************************** */
1475 /* Insert and Delete */
1477 /* **************************************************************** */
1479 /* Insert a string of text into the line at point. This is the only
1480 way that you should do insertion. rl_insert () calls this
1482 rl_insert_text (string
)
1485 register int i
, l
= strlen (string
);
1487 if (rl_end
+ l
>= rl_line_buffer_len
)
1488 rl_extend_line_buffer (rl_end
+ l
);
1490 for (i
= rl_end
; i
>= rl_point
; i
--)
1491 the_line
[i
+ l
] = the_line
[i
];
1492 strncpy (the_line
+ rl_point
, string
, l
);
1494 /* Remember how to undo this if we aren't undoing something. */
1497 /* If possible and desirable, concatenate the undos. */
1498 if ((strlen (string
) == 1) &&
1500 (rl_undo_list
->what
== UNDO_INSERT
) &&
1501 (rl_undo_list
->end
== rl_point
) &&
1502 (rl_undo_list
->end
- rl_undo_list
->start
< 20))
1503 rl_undo_list
->end
++;
1505 rl_add_undo (UNDO_INSERT
, rl_point
, rl_point
+ l
, (char *)NULL
);
1509 the_line
[rl_end
] = '\0';
1512 /* Delete the string between FROM and TO. FROM is
1513 inclusive, TO is not. */
1514 rl_delete_text (from
, to
)
1517 register char *text
;
1519 /* Fix it if the caller is confused. */
1526 text
= rl_copy_text (from
, to
);
1527 strncpy (the_line
+ from
, the_line
+ to
, rl_end
- to
);
1529 /* Remember how to undo this delete. */
1531 rl_add_undo (UNDO_DELETE
, from
, to
, text
);
1535 rl_end
-= (to
- from
);
1536 the_line
[rl_end
] = '\0';
1540 /* **************************************************************** */
1542 /* Readline character functions */
1544 /* **************************************************************** */
1546 /* This is not a gap editor, just a stupid line input routine. No hair
1547 is involved in writing any of the functions, and none should be. */
1551 rl_end is the place in the string that we would place '\0';
1552 i.e., it is always safe to place '\0' there.
1554 rl_point is the place in the string where the cursor is. Sometimes
1555 this is the same as rl_end.
1557 Any command that is called interactively receives two arguments.
1558 The first is a count: the numeric arg pased to this command.
1559 The second is the key which invoked this command.
1563 /* **************************************************************** */
1565 /* Movement Commands */
1567 /* **************************************************************** */
1569 /* Note that if you `optimize' the display for these functions, you cannot
1570 use said functions in other functions which do not do optimizing display.
1571 I.e., you will have to update the data base for rl_redisplay, and you
1572 might as well let rl_redisplay do that job. */
1574 /* Move forward COUNT characters. */
1579 rl_backward (-count
);
1583 #if defined (VI_MODE)
1584 if (rl_point
>= (rl_end
- (rl_editing_mode
== vi_mode
)))
1586 if (rl_point
== rl_end
)
1587 #endif /* VI_MODE */
1598 /* Move backward COUNT characters. */
1603 rl_forward (-count
);
1618 /* Move to the beginning of the line. */
1624 /* Move to the end of the line. */
1630 /* Move forward a word. We do what Emacs does. */
1631 rl_forward_word (count
)
1638 rl_backward_word (-count
);
1644 if (rl_point
== rl_end
)
1647 /* If we are not in a word, move forward until we are in one.
1648 Then, move forward until we hit a non-alphabetic character. */
1649 c
= the_line
[rl_point
];
1650 if (!alphabetic (c
))
1652 while (++rl_point
< rl_end
)
1654 c
= the_line
[rl_point
];
1655 if (alphabetic (c
)) break;
1658 if (rl_point
== rl_end
) return;
1659 while (++rl_point
< rl_end
)
1661 c
= the_line
[rl_point
];
1662 if (!alphabetic (c
)) break;
1668 /* Move backward a word. We do what Emacs does. */
1669 rl_backward_word (count
)
1676 rl_forward_word (-count
);
1685 /* Like rl_forward_word (), except that we look at the characters
1686 just before point. */
1688 c
= the_line
[rl_point
- 1];
1689 if (!alphabetic (c
))
1693 c
= the_line
[rl_point
- 1];
1694 if (alphabetic (c
)) break;
1700 c
= the_line
[rl_point
- 1];
1701 if (!alphabetic (c
))
1709 /* Clear the current line. Numeric argument to C-l does this. */
1712 int curr_line
= _rl_last_c_pos
/ screenwidth
;
1714 _rl_move_vert (curr_line
);
1715 _rl_move_cursor_relative (0, the_line
); /* XXX is this right */
1719 int row
, col
, width
, row_start
;
1721 ScreenGetCursor (&row
, &col
);
1722 width
= ScreenCols ();
1723 row_start
= ScreenPrimary
+ (row
* width
);
1724 memset (row_start
+ col
, 0, (width
- col
) * 2);
1726 #else /* __GO32__ */
1728 tputs (term_clreol
, 1, _rl_output_character_function
);
1729 #endif /* __GO32__/else */
1731 rl_forced_update_display ();
1732 rl_display_fixed
= 1;
1735 /* C-l typed to a line without quoting clears the screen, and then reprints
1736 the prompt and the current input line. Given a numeric arg, redraw only
1737 the current line. */
1740 if (rl_explicit_arg
)
1748 tputs (term_clrpag
, 1, _rl_output_character_function
);
1750 #endif /* !__GO32__ */
1753 rl_forced_update_display ();
1754 rl_display_fixed
= 1;
1757 rl_arrow_keys (count
, c
)
1762 ch
= rl_read_key ();
1764 switch (to_upper (ch
))
1767 rl_get_previous_history (count
);
1771 rl_get_next_history (count
);
1779 rl_backward (count
);
1788 /* **************************************************************** */
1792 /* **************************************************************** */
1794 /* Insert the character C at the current location, moving point forward. */
1795 rl_insert (count
, c
)
1804 /* If we can optimize, then do it. But don't let people crash
1805 readline because of extra large arguments. */
1806 if (count
> 1 && count
< 1024)
1808 string
= (char *)alloca (1 + count
);
1810 for (i
= 0; i
< count
; i
++)
1814 rl_insert_text (string
);
1822 string
= (char *)alloca (1024 + 1);
1824 for (i
= 0; i
< 1024; i
++)
1829 decreaser
= (count
> 1024 ? 1024 : count
);
1830 string
[decreaser
] = '\0';
1831 rl_insert_text (string
);
1837 /* We are inserting a single character.
1838 If there is pending input, then make a string of all of the
1839 pending characters that are bound to rl_insert, and insert
1846 string
= (char *)alloca (ibuffer_len
+ 1);
1849 while ((t
= rl_get_char (&key
)) &&
1850 (_rl_keymap
[key
].type
== ISFUNC
&&
1851 _rl_keymap
[key
].function
== rl_insert
))
1855 rl_unget_char (key
);
1858 rl_insert_text (string
);
1863 /* Inserting a single character. */
1864 string
= (char *)alloca (2);
1868 rl_insert_text (string
);
1872 /* Insert the next typed character verbatim. */
1873 rl_quoted_insert (count
)
1879 rl_insert (count
, c
);
1882 /* Insert a tab character. */
1883 rl_tab_insert (count
)
1886 rl_insert (count
, '\t');
1889 /* What to do when a NEWLINE is pressed. We accept the whole line.
1890 KEY is the key that invoked this command. I guess it could have
1891 meaning in the future. */
1892 rl_newline (count
, key
)
1898 #if defined (VI_MODE)
1900 extern int _rl_vi_doing_insert
;
1901 if (_rl_vi_doing_insert
)
1903 rl_end_undo_group ();
1904 _rl_vi_doing_insert
= 0;
1909 #endif /* VI_MODE */
1911 if (readline_echoing_p
)
1913 _rl_move_vert (_rl_vis_botlin
);
1916 fflush (out_stream
);
1921 rl_clean_up_for_exit ()
1923 if (readline_echoing_p
)
1925 _rl_move_vert (_rl_vis_botlin
);
1927 fflush (out_stream
);
1928 rl_restart_output ();
1932 /* What to do for some uppercase characters, like meta characters,
1933 and some characters appearing in emacs_ctlx_keymap. This function
1934 is just a stub, you bind keys to it and the code in rl_dispatch ()
1935 is special cased. */
1936 rl_do_lowercase_version (ignore1
, ignore2
)
1937 int ignore1
, ignore2
;
1941 /* Rubout the character behind point. */
1957 if (count
> 1 || rl_explicit_arg
)
1959 int orig_point
= rl_point
;
1960 rl_backward (count
);
1961 rl_kill_text (orig_point
, rl_point
);
1965 int c
= the_line
[--rl_point
];
1966 rl_delete_text (rl_point
, rl_point
+ 1);
1968 if (rl_point
== rl_end
&& isprint (c
) && _rl_last_c_pos
)
1971 l
= rl_character_len (c
, rl_point
);
1972 _rl_erase_at_end_of_line (l
);
1977 /* Delete the character under the cursor. Given a numeric argument,
1978 kill that many characters instead. */
1979 rl_delete (count
, invoking_key
)
1980 int count
, invoking_key
;
1988 if (rl_point
== rl_end
)
1994 if (count
> 1 || rl_explicit_arg
)
1996 int orig_point
= rl_point
;
1998 rl_kill_text (orig_point
, rl_point
);
1999 rl_point
= orig_point
;
2002 rl_delete_text (rl_point
, rl_point
+ 1);
2005 /* Delete all spaces and tabs around point. */
2006 rl_delete_horizontal_space (count
, ignore
)
2009 int start
= rl_point
;
2011 while (rl_point
&& whitespace (the_line
[rl_point
- 1]))
2016 while (rl_point
< rl_end
&& whitespace (the_line
[rl_point
]))
2019 if (start
== rl_point
)
2023 rl_delete_text (start
, rl_point
);
2028 /* **************************************************************** */
2032 /* **************************************************************** */
2034 /* The next two functions mimic unix line editing behaviour, except they
2035 save the deleted text on the kill ring. This is safer than not saving
2036 it, and since we have a ring, nobody should get screwed. */
2038 /* This does what C-w does in Unix. We can't prevent people from
2039 using behaviour that they expect. */
2040 rl_unix_word_rubout ()
2046 int orig_point
= rl_point
;
2048 while (rl_point
&& whitespace (the_line
[rl_point
- 1]))
2051 while (rl_point
&& !whitespace (the_line
[rl_point
- 1]))
2054 rl_kill_text (rl_point
, orig_point
);
2058 /* Here is C-u doing what Unix does. You don't *have* to use these
2059 key-bindings. We have a choice of killing the entire line, or
2060 killing from where we are to the start of the line. We choose the
2061 latter, because if you are a Unix weenie, then you haven't backspaced
2062 into the line at all, and if you aren't, then you know what you are
2064 rl_unix_line_discard ()
2070 rl_kill_text (rl_point
, 0);
2076 /* **************************************************************** */
2078 /* Commands For Typos */
2080 /* **************************************************************** */
2082 /* Random and interesting things in here. */
2084 /* **************************************************************** */
2088 /* **************************************************************** */
2090 /* The three kinds of things that we know how to do. */
2095 /* Uppercase the word at point. */
2096 rl_upcase_word (count
)
2099 rl_change_case (count
, UpCase
);
2102 /* Lowercase the word at point. */
2103 rl_downcase_word (count
)
2106 rl_change_case (count
, DownCase
);
2109 /* Upcase the first letter, downcase the rest. */
2110 rl_capitalize_word (count
)
2113 rl_change_case (count
, CapCase
);
2116 /* The meaty function.
2117 Change the case of COUNT words, performing OP on them.
2118 OP is one of UpCase, DownCase, or CapCase.
2119 If a negative argument is given, leave point where it started,
2120 otherwise, leave it where it moves to. */
2121 rl_change_case (count
, op
)
2124 register int start
= rl_point
, end
;
2127 rl_forward_word (count
);
2137 /* We are going to modify some text, so let's prepare to undo it. */
2138 rl_modifying (start
, end
);
2140 for (; start
< end
; start
++)
2145 the_line
[start
] = to_upper (the_line
[start
]);
2149 the_line
[start
] = to_lower (the_line
[start
]);
2155 the_line
[start
] = to_upper (the_line
[start
]);
2160 the_line
[start
] = to_lower (the_line
[start
]);
2162 if (!pure_alphabetic (the_line
[start
]))
2173 /* **************************************************************** */
2177 /* **************************************************************** */
2179 /* Transpose the words at point. */
2180 rl_transpose_words (count
)
2183 char *word1
, *word2
;
2184 int w1_beg
, w1_end
, w2_beg
, w2_end
;
2185 int orig_point
= rl_point
;
2189 /* Find the two words. */
2190 rl_forward_word (count
);
2192 rl_backward_word (1);
2194 rl_backward_word (count
);
2196 rl_forward_word (1);
2199 /* Do some check to make sure that there really are two words. */
2200 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
2203 rl_point
= orig_point
;
2207 /* Get the text of the words. */
2208 word1
= rl_copy_text (w1_beg
, w1_end
);
2209 word2
= rl_copy_text (w2_beg
, w2_end
);
2211 /* We are about to do many insertions and deletions. Remember them
2212 as one operation. */
2213 rl_begin_undo_group ();
2215 /* Do the stuff at word2 first, so that we don't have to worry
2216 about word1 moving. */
2218 rl_delete_text (w2_beg
, w2_end
);
2219 rl_insert_text (word1
);
2222 rl_delete_text (w1_beg
, w1_end
);
2223 rl_insert_text (word2
);
2225 /* This is exactly correct since the text before this point has not
2226 changed in length. */
2229 /* I think that does it. */
2230 rl_end_undo_group ();
2231 free (word1
); free (word2
);
2234 /* Transpose the characters at point. If point is at the end of the line,
2235 then transpose the characters before point. */
2236 rl_transpose_chars (count
)
2244 if (!rl_point
|| rl_end
< 2)
2250 rl_begin_undo_group ();
2252 if (rl_point
== rl_end
)
2259 dummy
[0] = the_line
[rl_point
];
2262 rl_delete_text (rl_point
, rl_point
+ 1);
2265 if (rl_point
> rl_end
)
2267 else if (rl_point
< 0)
2269 rl_insert_text (dummy
);
2271 rl_end_undo_group ();
2274 /* **************************************************************** */
2276 /* Undo, and Undoing */
2278 /* **************************************************************** */
2280 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
2282 static int doing_an_undo
= 0;
2284 /* The current undo list for THE_LINE. */
2285 UNDO_LIST
*rl_undo_list
= (UNDO_LIST
*)NULL
;
2287 /* Remember how to undo something. Concatenate some undos if that
2289 rl_add_undo (what
, start
, end
, text
)
2290 enum undo_code what
;
2294 UNDO_LIST
*temp
= (UNDO_LIST
*)xmalloc (sizeof (UNDO_LIST
));
2296 temp
->start
= start
;
2299 temp
->next
= rl_undo_list
;
2300 rl_undo_list
= temp
;
2303 /* Free the existing undo list. */
2306 while (rl_undo_list
)
2308 UNDO_LIST
*release
= rl_undo_list
;
2309 rl_undo_list
= rl_undo_list
->next
;
2311 if (release
->what
== UNDO_DELETE
)
2312 free (release
->text
);
2316 rl_undo_list
= (UNDO_LIST
*)NULL
;
2319 /* Undo the next thing in the list. Return 0 if there
2320 is nothing to undo, or non-zero if there was. */
2325 int waiting_for_begin
= 0;
2333 switch (rl_undo_list
->what
) {
2335 /* Undoing deletes means inserting some text. */
2337 rl_point
= rl_undo_list
->start
;
2338 rl_insert_text (rl_undo_list
->text
);
2339 free (rl_undo_list
->text
);
2342 /* Undoing inserts means deleting some text. */
2344 rl_delete_text (rl_undo_list
->start
, rl_undo_list
->end
);
2345 rl_point
= rl_undo_list
->start
;
2348 /* Undoing an END means undoing everything 'til we get to
2351 waiting_for_begin
++;
2354 /* Undoing a BEGIN means that we are done with this group. */
2356 if (waiting_for_begin
)
2357 waiting_for_begin
--;
2369 release
= rl_undo_list
;
2370 rl_undo_list
= rl_undo_list
->next
;
2373 if (waiting_for_begin
)
2379 /* Begin a group. Subsequent undos are undone as an atomic operation. */
2380 rl_begin_undo_group ()
2382 rl_add_undo (UNDO_BEGIN
, 0, 0, 0);
2385 /* End an undo group started with rl_begin_undo_group (). */
2386 rl_end_undo_group ()
2388 rl_add_undo (UNDO_END
, 0, 0, 0);
2391 /* Save an undo entry for the text from START to END. */
2392 rl_modifying (start
, end
)
2404 char *temp
= rl_copy_text (start
, end
);
2405 rl_begin_undo_group ();
2406 rl_add_undo (UNDO_DELETE
, start
, end
, temp
);
2407 rl_add_undo (UNDO_INSERT
, start
, end
, (char *)NULL
);
2408 rl_end_undo_group ();
2412 /* Revert the current line to its previous state. */
2419 while (rl_undo_list
)
2424 /* Do some undoing of things that were done. */
2425 rl_undo_command (count
)
2427 if (count
< 0) return; /* Nothing to do. */
2443 /* **************************************************************** */
2445 /* History Utilities */
2447 /* **************************************************************** */
2449 /* We already have a history library, and that is what we use to control
2450 the history features of readline. However, this is our local interface
2451 to the history mechanism. */
2453 /* While we are editing the history, this is the saved
2454 version of the original line. */
2455 HIST_ENTRY
*saved_line_for_history
= (HIST_ENTRY
*)NULL
;
2457 /* Set the history pointer back to the last entry in the history. */
2458 start_using_history ()
2461 if (saved_line_for_history
)
2462 free_history_entry (saved_line_for_history
);
2464 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
2467 /* Free the contents (and containing structure) of a HIST_ENTRY. */
2469 free_history_entry (entry
)
2478 /* Perhaps put back the current line if it has changed. */
2479 maybe_replace_line ()
2481 HIST_ENTRY
*temp
= current_history ();
2483 /* If the current line has changed, save the changes. */
2484 if (temp
&& ((UNDO_LIST
*)(temp
->data
) != rl_undo_list
))
2486 temp
= replace_history_entry (where_history (), the_line
, rl_undo_list
);
2492 /* Put back the saved_line_for_history if there is one. */
2493 maybe_unsave_line ()
2495 if (saved_line_for_history
)
2499 line_len
= strlen (saved_line_for_history
->line
);
2501 if (line_len
>= rl_line_buffer_len
)
2502 rl_extend_line_buffer (line_len
);
2504 strcpy (the_line
, saved_line_for_history
->line
);
2505 rl_undo_list
= (UNDO_LIST
*)saved_line_for_history
->data
;
2506 free_history_entry (saved_line_for_history
);
2507 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
2508 rl_end
= rl_point
= strlen (the_line
);
2514 /* Save the current line in saved_line_for_history. */
2517 if (!saved_line_for_history
)
2519 saved_line_for_history
= (HIST_ENTRY
*)xmalloc (sizeof (HIST_ENTRY
));
2520 saved_line_for_history
->line
= savestring (the_line
);
2521 saved_line_for_history
->data
= (char *)rl_undo_list
;
2525 /* **************************************************************** */
2527 /* History Commands */
2529 /* **************************************************************** */
2531 /* Meta-< goes to the start of the history. */
2532 rl_beginning_of_history ()
2534 rl_get_previous_history (1 + where_history ());
2537 /* Meta-> goes to the end of the history. (The current line). */
2538 rl_end_of_history ()
2540 maybe_replace_line ();
2542 maybe_unsave_line ();
2545 /* Move down to the next history line. */
2546 rl_get_next_history (count
)
2549 HIST_ENTRY
*temp
= (HIST_ENTRY
*)NULL
;
2553 rl_get_previous_history (-count
);
2560 maybe_replace_line ();
2564 temp
= next_history ();
2571 maybe_unsave_line ();
2576 line_len
= strlen (temp
->line
);
2578 if (line_len
>= rl_line_buffer_len
)
2579 rl_extend_line_buffer (line_len
);
2581 strcpy (the_line
, temp
->line
);
2582 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
2583 rl_end
= rl_point
= strlen (the_line
);
2584 #if defined (VI_MODE)
2585 if (rl_editing_mode
== vi_mode
)
2587 #endif /* VI_MODE */
2591 /* Get the previous item out of our interactive history, making it the current
2592 line. If there is no previous history, just ding. */
2593 rl_get_previous_history (count
)
2596 HIST_ENTRY
*old_temp
= (HIST_ENTRY
*)NULL
;
2597 HIST_ENTRY
*temp
= (HIST_ENTRY
*)NULL
;
2601 rl_get_next_history (-count
);
2608 /* If we don't have a line saved, then save this one. */
2611 /* If the current line has changed, save the changes. */
2612 maybe_replace_line ();
2616 temp
= previous_history ();
2624 /* If there was a large argument, and we moved back to the start of the
2625 history, that is not an error. So use the last value found. */
2626 if (!temp
&& old_temp
)
2635 line_len
= strlen (temp
->line
);
2637 if (line_len
>= rl_line_buffer_len
)
2638 rl_extend_line_buffer (line_len
);
2640 strcpy (the_line
, temp
->line
);
2641 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
2642 rl_end
= rl_point
= line_len
;
2644 #if defined (VI_MODE)
2645 if (rl_editing_mode
== vi_mode
)
2647 #endif /* VI_MODE */
2651 /* Make C be the next command to be executed. */
2655 rl_pending_input
= c
;
2658 /* **************************************************************** */
2660 /* The Mark and the Region. */
2662 /* **************************************************************** */
2664 /* Set the mark at POSITION. */
2665 rl_set_mark (position
)
2668 if (position
> rl_end
)
2674 /* Exchange the position of mark and point. */
2675 rl_exchange_mark_and_point ()
2677 if (rl_mark
> rl_end
)
2687 int temp
= rl_point
;
2695 /* **************************************************************** */
2697 /* Killing Mechanism */
2699 /* **************************************************************** */
2701 /* What we assume for a max number of kills. */
2702 #define DEFAULT_MAX_KILLS 10
2704 /* The real variable to look at to find out when to flush kills. */
2705 int rl_max_kills
= DEFAULT_MAX_KILLS
;
2707 /* Where to store killed text. */
2708 char **rl_kill_ring
= (char **)NULL
;
2710 /* Where we are in the kill ring. */
2711 int rl_kill_index
= 0;
2713 /* How many slots we have in the kill ring. */
2714 int rl_kill_ring_length
= 0;
2716 /* How to say that you only want to save a certain amount
2717 of kill material. */
2718 rl_set_retained_kills (num
)
2722 /* The way to kill something. This appends or prepends to the last
2723 kill, if the last command was a kill command. if FROM is less
2724 than TO, then the text is appended, otherwise prepended. If the
2725 last command was not a kill command, then a new slot is made for
2727 rl_kill_text (from
, to
)
2731 char *text
= rl_copy_text (from
, to
);
2733 /* Is there anything to kill? */
2737 last_command_was_kill
++;
2741 /* Delete the copied text from the line. */
2742 rl_delete_text (from
, to
);
2744 /* First, find the slot to work with. */
2745 if (!last_command_was_kill
)
2747 /* Get a new slot. */
2750 /* If we don't have any defined, then make one. */
2751 rl_kill_ring
= (char **)
2752 xmalloc (((rl_kill_ring_length
= 1) + 1) * sizeof (char *));
2757 /* We have to add a new slot on the end, unless we have
2758 exceeded the max limit for remembering kills. */
2759 slot
= rl_kill_ring_length
;
2760 if (slot
== rl_max_kills
)
2763 free (rl_kill_ring
[0]);
2764 for (i
= 0; i
< slot
; i
++)
2765 rl_kill_ring
[i
] = rl_kill_ring
[i
+ 1];
2771 xrealloc (rl_kill_ring
,
2772 ((slot
= (rl_kill_ring_length
+= 1)) + 1)
2780 slot
= rl_kill_ring_length
- 1;
2783 /* If the last command was a kill, prepend or append. */
2784 if (last_command_was_kill
&& rl_editing_mode
!= vi_mode
)
2786 char *old
= rl_kill_ring
[slot
];
2787 char *new = (char *)xmalloc (1 + strlen (old
) + strlen (text
));
2801 rl_kill_ring
[slot
] = new;
2805 rl_kill_ring
[slot
] = text
;
2807 rl_kill_index
= slot
;
2808 last_command_was_kill
++;
2811 /* Now REMEMBER! In order to do prepending or appending correctly, kill
2812 commands always make rl_point's original position be the FROM argument,
2813 and rl_point's extent be the TO argument. */
2815 /* **************************************************************** */
2817 /* Killing Commands */
2819 /* **************************************************************** */
2821 /* Delete the word at point, saving the text in the kill ring. */
2822 rl_kill_word (count
)
2825 int orig_point
= rl_point
;
2828 rl_backward_kill_word (-count
);
2831 rl_forward_word (count
);
2833 if (rl_point
!= orig_point
)
2834 rl_kill_text (orig_point
, rl_point
);
2836 rl_point
= orig_point
;
2840 /* Rubout the word before point, placing it on the kill ring. */
2841 rl_backward_kill_word (count
)
2844 int orig_point
= rl_point
;
2847 rl_kill_word (-count
);
2850 rl_backward_word (count
);
2852 if (rl_point
!= orig_point
)
2853 rl_kill_text (orig_point
, rl_point
);
2857 /* Kill from here to the end of the line. If DIRECTION is negative, kill
2858 back to the line start instead. */
2859 rl_kill_line (direction
)
2862 int orig_point
= rl_point
;
2865 rl_backward_kill_line (1);
2869 if (orig_point
!= rl_point
)
2870 rl_kill_text (orig_point
, rl_point
);
2871 rl_point
= orig_point
;
2875 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
2876 forwards to the line end instead. */
2877 rl_backward_kill_line (direction
)
2880 int orig_point
= rl_point
;
2891 rl_kill_text (orig_point
, rl_point
);
2896 /* Yank back the last killed text. This ignores arguments. */
2902 rl_set_mark (rl_point
);
2903 rl_insert_text (rl_kill_ring
[rl_kill_index
]);
2906 /* If the last command was yank, or yank_pop, and the text just
2907 before point is identical to the current kill item, then
2908 delete that text from the line, rotate the index down, and
2909 yank back some other text. */
2914 if (((rl_last_func
!= rl_yank_pop
) && (rl_last_func
!= rl_yank
)) ||
2920 l
= strlen (rl_kill_ring
[rl_kill_index
]);
2921 if (((rl_point
- l
) >= 0) &&
2922 (strncmp (the_line
+ (rl_point
- l
),
2923 rl_kill_ring
[rl_kill_index
], l
) == 0))
2925 rl_delete_text ((rl_point
- l
), rl_point
);
2928 if (rl_kill_index
< 0)
2929 rl_kill_index
= rl_kill_ring_length
- 1;
2937 /* Yank the COUNTth argument from the previous history line. */
2938 rl_yank_nth_arg (count
, ignore
)
2941 register HIST_ENTRY
*entry
= previous_history ();
2952 arg
= history_arg_extract (count
, count
, entry
->line
);
2959 rl_begin_undo_group ();
2961 #if defined (VI_MODE)
2962 /* Vi mode always inserts a space before yanking the argument, and it
2963 inserts it right *after* rl_point. */
2964 if (rl_editing_mode
== vi_mode
)
2966 #endif /* VI_MODE */
2968 if (rl_point
&& the_line
[rl_point
- 1] != ' ')
2969 rl_insert_text (" ");
2971 rl_insert_text (arg
);
2974 rl_end_undo_group ();
2977 /* How to toggle back and forth between editing modes. */
2978 rl_vi_editing_mode ()
2980 #if defined (VI_MODE)
2981 rl_editing_mode
= vi_mode
;
2982 rl_vi_insertion_mode ();
2983 #endif /* VI_MODE */
2986 rl_emacs_editing_mode ()
2988 rl_editing_mode
= emacs_mode
;
2989 _rl_keymap
= emacs_standard_keymap
;
2993 /* **************************************************************** */
2995 /* USG (System V) Support */
2997 /* **************************************************************** */
3008 return (getkey () & 0x7f);
3009 #endif /* __GO32__ */
3013 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
3015 if (result
== sizeof (unsigned char))
3018 /* If zero characters are returned, then the file that we are
3019 reading from is empty! Return EOF in that case. */
3023 #if defined (EWOULDBLOCK)
3024 if (errno
== EWOULDBLOCK
)
3028 if ((flags
= fcntl (fileno (stream
), F_GETFL
, 0)) < 0)
3030 if (flags
& O_NDELAY
)
3033 fcntl (fileno (stream
), F_SETFL
, flags
);
3038 #endif /* EWOULDBLOCK */
3040 #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
3041 if (errno
== EAGAIN
)
3045 if ((flags
= fcntl (fileno (stream
), F_GETFL
, 0)) < 0)
3047 if (flags
& O_NONBLOCK
)
3049 flags
&= ~O_NONBLOCK
;
3050 fcntl (fileno (stream
), F_SETFL
, flags
);
3054 #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
3057 /* If the error that we received was SIGINT, then try again,
3058 this is simply an interrupted system call to read ().
3059 Otherwise, some error ocurred, also signifying EOF. */
3062 #endif /* !__GO32__ */
3066 #if defined (STATIC_MALLOC)
3068 /* **************************************************************** */
3070 /* xmalloc and xrealloc () */
3072 /* **************************************************************** */
3074 static void memory_error_and_abort ();
3080 char *temp
= (char *)malloc (bytes
);
3083 memory_error_and_abort ();
3088 xrealloc (pointer
, bytes
)
3095 temp
= (char *)malloc (bytes
);
3097 temp
= (char *)realloc (pointer
, bytes
);
3100 memory_error_and_abort ();
3106 memory_error_and_abort ()
3108 fprintf (stderr
, "readline: Out of virtual memory!\n");
3111 #endif /* STATIC_MALLOC */
3114 /* **************************************************************** */
3116 /* Testing Readline */
3118 /* **************************************************************** */
3124 HIST_ENTRY
**history_list ();
3125 char *temp
= (char *)NULL
;
3126 char *prompt
= "readline% ";
3131 temp
= readline (prompt
);
3137 /* If there is anything on the line, print it and remember it. */
3140 fprintf (stderr
, "%s\r\n", temp
);
3144 /* Check for `command' that we handle. */
3145 if (strcmp (temp
, "quit") == 0)
3148 if (strcmp (temp
, "list") == 0)
3150 HIST_ENTRY
**list
= history_list ();
3154 for (i
= 0; list
[i
]; i
++)
3156 fprintf (stderr
, "%d: %s\r\n", i
, list
[i
]->line
);
3157 free (list
[i
]->line
);
3171 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"