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 /* A visible bell, if the terminal can be made to flash the screen. */
1070 /* Non-zero means that this terminal has a meta key. */
1073 /* The string to write to turn on the meta key, if this term has one. */
1076 /* The string to write to turn off the meta key, if this term has one. */
1079 /* The key sequences output by the arrow keys, if this terminal has any. */
1080 char *term_ku
, *term_kd
, *term_kr
, *term_kl
;
1082 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1084 rl_reset_terminal (terminal_name
)
1085 char *terminal_name
;
1087 init_terminal_io (terminal_name
);
1090 /* Set readline's idea of the screen size. TTY is a file descriptor open
1091 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
1092 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
1093 non-null serve to check whether or not we have initialized termcap. */
1095 _rl_set_screen_size (tty
, ignore_env
)
1096 int tty
, ignore_env
;
1098 #if defined (TIOCGWINSZ)
1099 struct winsize window_size
;
1100 #endif /* TIOCGWINSZ */
1102 #if defined (TIOCGWINSZ)
1103 if (ioctl (tty
, TIOCGWINSZ
, &window_size
) == 0)
1105 screenwidth
= (int) window_size
.ws_col
;
1106 screenheight
= (int) window_size
.ws_row
;
1108 #endif /* TIOCGWINSZ */
1110 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
1112 if (screenwidth
<= 0)
1116 if (!ignore_env
&& (sw
= getenv ("COLUMNS")))
1117 screenwidth
= atoi (sw
);
1119 if (screenwidth
<= 0 && term_string_buffer
)
1120 screenwidth
= tgetnum ("co");
1123 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
1125 if (screenheight
<= 0)
1129 if (!ignore_env
&& (sh
= getenv ("LINES")))
1130 screenheight
= atoi (sh
);
1132 if (screenheight
<= 0 && term_string_buffer
)
1133 screenheight
= tgetnum ("li");
1136 /* If all else fails, default to 80x24 terminal. */
1137 if (screenwidth
<= 0)
1140 if (screenheight
<= 0)
1144 /* If we're being compiled as part of bash, set the environment
1145 variables $LINES and $COLUMNS to new values. */
1146 set_lines_and_columns (screenheight
, screenwidth
);
1152 init_terminal_io (terminal_name
)
1153 char *terminal_name
;
1156 screenwidth
= ScreenCols ();
1157 screenheight
= ScreenRows ();
1159 term_im
= term_ei
= term_ic
= term_IC
= (char *)NULL
;
1160 term_up
= term_dc
= term_DC
= visible_bell
= (char *)NULL
;
1162 /* Does the _GO32_ have a meta key? I don't know. */
1164 term_mm
= term_mo
= (char *)NULL
;
1166 /* It probably has arrow keys, but I don't know what they are. */
1167 term_ku
= term_kd
= term_kr
= term_kl
= (char *)NULL
;
1169 #if defined (HACK_TERMCAP_MOTION)
1170 term_forward_char
= (char *)NULL
;
1172 terminal_can_insert
= 0;
1174 #else /* !__GO32__ */
1175 char *term
, *buffer
;
1178 term
= terminal_name
? terminal_name
: getenv ("TERM");
1180 if (!term_string_buffer
)
1181 term_string_buffer
= (char *)xmalloc (2048);
1184 term_buffer
= (char *)xmalloc (2048);
1186 buffer
= term_string_buffer
;
1188 term_clrpag
= term_cr
= term_clreol
= (char *)NULL
;
1193 if (tgetent (term_buffer
, term
) <= 0)
1199 term_im
= term_ei
= term_ic
= term_IC
= (char *)NULL
;
1200 term_up
= term_dc
= term_DC
= visible_bell
= (char *)NULL
;
1201 term_ku
= term_kd
= term_kl
= term_kr
= (char *)NULL
;
1202 #if defined (HACK_TERMCAP_MOTION)
1203 term_forward_char
= (char *)NULL
;
1205 terminal_can_insert
= 0;
1209 BC
= tgetstr ("pc", &buffer
);
1210 PC
= buffer
? *buffer
: 0;
1212 term_backspace
= tgetstr ("le", &buffer
);
1214 term_cr
= tgetstr ("cr", &buffer
);
1215 term_clreol
= tgetstr ("ce", &buffer
);
1216 term_clrpag
= tgetstr ("cl", &buffer
);
1221 #if defined (HACK_TERMCAP_MOTION)
1222 term_forward_char
= tgetstr ("nd", &buffer
);
1223 #endif /* HACK_TERMCAP_MOTION */
1226 tty
= fileno (rl_instream
);
1230 screenwidth
= screenheight
= 0;
1232 _rl_set_screen_size (tty
, 0);
1234 term_im
= tgetstr ("im", &buffer
);
1235 term_ei
= tgetstr ("ei", &buffer
);
1236 term_IC
= tgetstr ("IC", &buffer
);
1237 term_ic
= tgetstr ("ic", &buffer
);
1239 /* "An application program can assume that the terminal can do
1240 character insertion if *any one of* the capabilities `IC',
1241 `im', `ic' or `ip' is provided." But we can't do anything if
1242 only `ip' is provided, so... */
1243 terminal_can_insert
= (term_IC
|| term_im
|| term_ic
);
1245 term_up
= tgetstr ("up", &buffer
);
1246 term_dc
= tgetstr ("dc", &buffer
);
1247 term_DC
= tgetstr ("DC", &buffer
);
1249 visible_bell
= tgetstr ("vb", &buffer
);
1251 /* Check to see if this terminal has a meta key. */
1252 term_has_meta
= (tgetflag ("km") || tgetflag ("MT"));
1255 term_mm
= tgetstr ("mm", &buffer
);
1256 term_mo
= tgetstr ("mo", &buffer
);
1260 term_mm
= (char *)NULL
;
1261 term_mo
= (char *)NULL
;
1264 /* Attempt to find and bind the arrow keys. Do not override already
1265 bound keys in an overzealous attempt, however. */
1266 term_ku
= tgetstr ("ku", &buffer
);
1267 term_kd
= tgetstr ("kd", &buffer
);
1268 term_kr
= tgetstr ("kr", &buffer
);
1269 term_kl
= tgetstr ("kl", &buffer
);
1275 func
= rl_function_of_keyseq (term_ku
, _rl_keymap
, (int *)NULL
);
1277 if (!func
|| func
== rl_do_lowercase_version
)
1278 rl_set_key (term_ku
, rl_get_previous_history
, _rl_keymap
);
1285 func
= rl_function_of_keyseq (term_kd
, _rl_keymap
, (int *)NULL
);
1287 if (!func
|| func
== rl_do_lowercase_version
)
1288 rl_set_key (term_kd
, rl_get_next_history
, _rl_keymap
);
1295 func
= rl_function_of_keyseq (term_kr
, _rl_keymap
, (int *)NULL
);
1297 if (!func
|| func
== rl_do_lowercase_version
)
1298 rl_set_key (term_kr
, rl_forward
, _rl_keymap
);
1305 func
= rl_function_of_keyseq (term_kl
, _rl_keymap
, (int *)NULL
);
1307 if (!func
|| func
== rl_do_lowercase_version
)
1308 rl_set_key (term_kl
, rl_backward
, _rl_keymap
);
1310 #endif /* !__GO32__ */
1313 /* A function for the use of tputs () */
1315 _rl_output_character_function (c
)
1318 putc (c
, out_stream
);
1321 /* Write COUNT characters from STRING to the output stream. */
1323 _rl_output_some_chars (string
, count
)
1327 fwrite (string
, 1, count
, out_stream
);
1332 /* Move the cursor back. */
1340 for (i
= 0; i
< count
; i
++)
1341 tputs (term_backspace
, 1, _rl_output_character_function
);
1343 #endif /* !__GO32__ */
1344 for (i
= 0; i
< count
; i
++)
1345 putc ('\b', out_stream
);
1348 /* Move to the start of the next line. */
1351 #if defined (NEW_TTY_DRIVER)
1352 tputs (term_cr
, 1, _rl_output_character_function
);
1353 #endif /* NEW_TTY_DRIVER */
1354 putc ('\n', out_stream
);
1358 /* **************************************************************** */
1360 /* Utility Functions */
1362 /* **************************************************************** */
1364 /* Return 0 if C is not a member of the class of characters that belong
1365 in words, or 1 if it is. */
1367 int allow_pathname_alphabetic_chars
= 0;
1368 char *pathname_alphabetic_chars
= "/-_=~.#$";
1374 if (pure_alphabetic (c
) || (numeric (c
)))
1377 if (allow_pathname_alphabetic_chars
)
1378 return ((int) strchr (pathname_alphabetic_chars
, c
));
1383 /* Return non-zero if C is a numeric character. */
1388 return (c
>= '0' && c
<= '9');
1391 /* Ring the terminal bell. */
1395 if (readline_echoing_p
)
1398 if (_rl_prefer_visible_bell
&& visible_bell
)
1399 tputs (visible_bell
, 1, _rl_output_character_function
);
1401 #endif /* !__GO32__ */
1403 fprintf (stderr
, "\007");
1410 /* How to abort things. */
1414 rl_clear_message ();
1415 rl_init_argument ();
1416 rl_pending_input
= 0;
1418 defining_kbd_macro
= 0;
1419 while (executing_macro
)
1420 pop_executing_macro ();
1422 rl_last_func
= (Function
*)NULL
;
1423 longjmp (readline_top_level
, 1);
1426 /* Return a copy of the string between FROM and TO.
1427 FROM is inclusive, TO is not. */
1429 rl_copy_text (from
, to
)
1432 register int length
;
1435 /* Fix it if the caller is confused. */
1444 copy
= (char *)xmalloc (1 + length
);
1445 strncpy (copy
, the_line
+ from
, length
);
1446 copy
[length
] = '\0';
1450 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
1453 rl_extend_line_buffer (len
)
1456 while (len
>= rl_line_buffer_len
)
1459 (rl_line_buffer
, rl_line_buffer_len
+= DEFAULT_BUFFER_SIZE
);
1461 the_line
= rl_line_buffer
;
1465 /* **************************************************************** */
1467 /* Insert and Delete */
1469 /* **************************************************************** */
1471 /* Insert a string of text into the line at point. This is the only
1472 way that you should do insertion. rl_insert () calls this
1474 rl_insert_text (string
)
1477 register int i
, l
= strlen (string
);
1479 if (rl_end
+ l
>= rl_line_buffer_len
)
1480 rl_extend_line_buffer (rl_end
+ l
);
1482 for (i
= rl_end
; i
>= rl_point
; i
--)
1483 the_line
[i
+ l
] = the_line
[i
];
1484 strncpy (the_line
+ rl_point
, string
, l
);
1486 /* Remember how to undo this if we aren't undoing something. */
1489 /* If possible and desirable, concatenate the undos. */
1490 if ((strlen (string
) == 1) &&
1492 (rl_undo_list
->what
== UNDO_INSERT
) &&
1493 (rl_undo_list
->end
== rl_point
) &&
1494 (rl_undo_list
->end
- rl_undo_list
->start
< 20))
1495 rl_undo_list
->end
++;
1497 rl_add_undo (UNDO_INSERT
, rl_point
, rl_point
+ l
, (char *)NULL
);
1501 the_line
[rl_end
] = '\0';
1504 /* Delete the string between FROM and TO. FROM is
1505 inclusive, TO is not. */
1506 rl_delete_text (from
, to
)
1509 register char *text
;
1511 /* Fix it if the caller is confused. */
1518 text
= rl_copy_text (from
, to
);
1519 strncpy (the_line
+ from
, the_line
+ to
, rl_end
- to
);
1521 /* Remember how to undo this delete. */
1523 rl_add_undo (UNDO_DELETE
, from
, to
, text
);
1527 rl_end
-= (to
- from
);
1528 the_line
[rl_end
] = '\0';
1532 /* **************************************************************** */
1534 /* Readline character functions */
1536 /* **************************************************************** */
1538 /* This is not a gap editor, just a stupid line input routine. No hair
1539 is involved in writing any of the functions, and none should be. */
1543 rl_end is the place in the string that we would place '\0';
1544 i.e., it is always safe to place '\0' there.
1546 rl_point is the place in the string where the cursor is. Sometimes
1547 this is the same as rl_end.
1549 Any command that is called interactively receives two arguments.
1550 The first is a count: the numeric arg pased to this command.
1551 The second is the key which invoked this command.
1555 /* **************************************************************** */
1557 /* Movement Commands */
1559 /* **************************************************************** */
1561 /* Note that if you `optimize' the display for these functions, you cannot
1562 use said functions in other functions which do not do optimizing display.
1563 I.e., you will have to update the data base for rl_redisplay, and you
1564 might as well let rl_redisplay do that job. */
1566 /* Move forward COUNT characters. */
1571 rl_backward (-count
);
1575 #if defined (VI_MODE)
1576 if (rl_point
>= (rl_end
- (rl_editing_mode
== vi_mode
)))
1578 if (rl_point
== rl_end
)
1579 #endif /* VI_MODE */
1590 /* Move backward COUNT characters. */
1595 rl_forward (-count
);
1610 /* Move to the beginning of the line. */
1616 /* Move to the end of the line. */
1622 /* Move forward a word. We do what Emacs does. */
1623 rl_forward_word (count
)
1630 rl_backward_word (-count
);
1636 if (rl_point
== rl_end
)
1639 /* If we are not in a word, move forward until we are in one.
1640 Then, move forward until we hit a non-alphabetic character. */
1641 c
= the_line
[rl_point
];
1642 if (!alphabetic (c
))
1644 while (++rl_point
< rl_end
)
1646 c
= the_line
[rl_point
];
1647 if (alphabetic (c
)) break;
1650 if (rl_point
== rl_end
) return;
1651 while (++rl_point
< rl_end
)
1653 c
= the_line
[rl_point
];
1654 if (!alphabetic (c
)) break;
1660 /* Move backward a word. We do what Emacs does. */
1661 rl_backward_word (count
)
1668 rl_forward_word (-count
);
1677 /* Like rl_forward_word (), except that we look at the characters
1678 just before point. */
1680 c
= the_line
[rl_point
- 1];
1681 if (!alphabetic (c
))
1685 c
= the_line
[rl_point
- 1];
1686 if (alphabetic (c
)) break;
1692 c
= the_line
[rl_point
- 1];
1693 if (!alphabetic (c
))
1701 /* Clear the current line. Numeric argument to C-l does this. */
1704 int curr_line
= _rl_last_c_pos
/ screenwidth
;
1706 _rl_move_vert (curr_line
);
1707 _rl_move_cursor_relative (0, the_line
); /* XXX is this right */
1711 int row
, col
, width
, row_start
;
1713 ScreenGetCursor (&row
, &col
);
1714 width
= ScreenCols ();
1715 row_start
= ScreenPrimary
+ (row
* width
);
1716 memset (row_start
+ col
, 0, (width
- col
) * 2);
1718 #else /* __GO32__ */
1720 tputs (term_clreol
, 1, _rl_output_character_function
);
1721 #endif /* __GO32__/else */
1723 rl_forced_update_display ();
1724 rl_display_fixed
= 1;
1727 /* C-l typed to a line without quoting clears the screen, and then reprints
1728 the prompt and the current input line. Given a numeric arg, redraw only
1729 the current line. */
1732 if (rl_explicit_arg
)
1740 tputs (term_clrpag
, 1, _rl_output_character_function
);
1742 #endif /* !__GO32__ */
1745 rl_forced_update_display ();
1746 rl_display_fixed
= 1;
1749 rl_arrow_keys (count
, c
)
1754 ch
= rl_read_key ();
1756 switch (to_upper (ch
))
1759 rl_get_previous_history (count
);
1763 rl_get_next_history (count
);
1771 rl_backward (count
);
1780 /* **************************************************************** */
1784 /* **************************************************************** */
1786 /* Insert the character C at the current location, moving point forward. */
1787 rl_insert (count
, c
)
1796 /* If we can optimize, then do it. But don't let people crash
1797 readline because of extra large arguments. */
1798 if (count
> 1 && count
< 1024)
1800 string
= (char *)alloca (1 + count
);
1802 for (i
= 0; i
< count
; i
++)
1806 rl_insert_text (string
);
1814 string
= (char *)alloca (1024 + 1);
1816 for (i
= 0; i
< 1024; i
++)
1821 decreaser
= (count
> 1024 ? 1024 : count
);
1822 string
[decreaser
] = '\0';
1823 rl_insert_text (string
);
1829 /* We are inserting a single character.
1830 If there is pending input, then make a string of all of the
1831 pending characters that are bound to rl_insert, and insert
1838 string
= (char *)alloca (ibuffer_len
+ 1);
1841 while ((t
= rl_get_char (&key
)) &&
1842 (_rl_keymap
[key
].type
== ISFUNC
&&
1843 _rl_keymap
[key
].function
== rl_insert
))
1847 rl_unget_char (key
);
1850 rl_insert_text (string
);
1855 /* Inserting a single character. */
1856 string
= (char *)alloca (2);
1860 rl_insert_text (string
);
1864 /* Insert the next typed character verbatim. */
1865 rl_quoted_insert (count
)
1871 rl_insert (count
, c
);
1874 /* Insert a tab character. */
1875 rl_tab_insert (count
)
1878 rl_insert (count
, '\t');
1881 /* What to do when a NEWLINE is pressed. We accept the whole line.
1882 KEY is the key that invoked this command. I guess it could have
1883 meaning in the future. */
1884 rl_newline (count
, key
)
1890 #if defined (VI_MODE)
1892 extern int _rl_vi_doing_insert
;
1893 if (_rl_vi_doing_insert
)
1895 rl_end_undo_group ();
1896 _rl_vi_doing_insert
= 0;
1901 #endif /* VI_MODE */
1903 if (readline_echoing_p
)
1905 _rl_move_vert (_rl_vis_botlin
);
1908 fflush (out_stream
);
1913 rl_clean_up_for_exit ()
1915 if (readline_echoing_p
)
1917 _rl_move_vert (_rl_vis_botlin
);
1919 fflush (out_stream
);
1920 rl_restart_output ();
1924 /* What to do for some uppercase characters, like meta characters,
1925 and some characters appearing in emacs_ctlx_keymap. This function
1926 is just a stub, you bind keys to it and the code in rl_dispatch ()
1927 is special cased. */
1928 rl_do_lowercase_version (ignore1
, ignore2
)
1929 int ignore1
, ignore2
;
1933 /* Rubout the character behind point. */
1949 if (count
> 1 || rl_explicit_arg
)
1951 int orig_point
= rl_point
;
1952 rl_backward (count
);
1953 rl_kill_text (orig_point
, rl_point
);
1957 int c
= the_line
[--rl_point
];
1958 rl_delete_text (rl_point
, rl_point
+ 1);
1960 if (rl_point
== rl_end
&& isprint (c
) && _rl_last_c_pos
)
1963 l
= rl_character_len (c
, rl_point
);
1964 _rl_erase_at_end_of_line (l
);
1969 /* Delete the character under the cursor. Given a numeric argument,
1970 kill that many characters instead. */
1971 rl_delete (count
, invoking_key
)
1972 int count
, invoking_key
;
1980 if (rl_point
== rl_end
)
1986 if (count
> 1 || rl_explicit_arg
)
1988 int orig_point
= rl_point
;
1990 rl_kill_text (orig_point
, rl_point
);
1991 rl_point
= orig_point
;
1994 rl_delete_text (rl_point
, rl_point
+ 1);
1997 /* Delete all spaces and tabs around point. */
1998 rl_delete_horizontal_space (count
, ignore
)
2001 int start
= rl_point
;
2003 while (rl_point
&& whitespace (the_line
[rl_point
- 1]))
2008 while (rl_point
< rl_end
&& whitespace (the_line
[rl_point
]))
2011 if (start
== rl_point
)
2015 rl_delete_text (start
, rl_point
);
2020 /* **************************************************************** */
2024 /* **************************************************************** */
2026 /* The next two functions mimic unix line editing behaviour, except they
2027 save the deleted text on the kill ring. This is safer than not saving
2028 it, and since we have a ring, nobody should get screwed. */
2030 /* This does what C-w does in Unix. We can't prevent people from
2031 using behaviour that they expect. */
2032 rl_unix_word_rubout ()
2038 int orig_point
= rl_point
;
2040 while (rl_point
&& whitespace (the_line
[rl_point
- 1]))
2043 while (rl_point
&& !whitespace (the_line
[rl_point
- 1]))
2046 rl_kill_text (rl_point
, orig_point
);
2050 /* Here is C-u doing what Unix does. You don't *have* to use these
2051 key-bindings. We have a choice of killing the entire line, or
2052 killing from where we are to the start of the line. We choose the
2053 latter, because if you are a Unix weenie, then you haven't backspaced
2054 into the line at all, and if you aren't, then you know what you are
2056 rl_unix_line_discard ()
2062 rl_kill_text (rl_point
, 0);
2068 /* **************************************************************** */
2070 /* Commands For Typos */
2072 /* **************************************************************** */
2074 /* Random and interesting things in here. */
2076 /* **************************************************************** */
2080 /* **************************************************************** */
2082 /* The three kinds of things that we know how to do. */
2087 /* Uppercase the word at point. */
2088 rl_upcase_word (count
)
2091 rl_change_case (count
, UpCase
);
2094 /* Lowercase the word at point. */
2095 rl_downcase_word (count
)
2098 rl_change_case (count
, DownCase
);
2101 /* Upcase the first letter, downcase the rest. */
2102 rl_capitalize_word (count
)
2105 rl_change_case (count
, CapCase
);
2108 /* The meaty function.
2109 Change the case of COUNT words, performing OP on them.
2110 OP is one of UpCase, DownCase, or CapCase.
2111 If a negative argument is given, leave point where it started,
2112 otherwise, leave it where it moves to. */
2113 rl_change_case (count
, op
)
2116 register int start
= rl_point
, end
;
2119 rl_forward_word (count
);
2129 /* We are going to modify some text, so let's prepare to undo it. */
2130 rl_modifying (start
, end
);
2132 for (; start
< end
; start
++)
2137 the_line
[start
] = to_upper (the_line
[start
]);
2141 the_line
[start
] = to_lower (the_line
[start
]);
2147 the_line
[start
] = to_upper (the_line
[start
]);
2152 the_line
[start
] = to_lower (the_line
[start
]);
2154 if (!pure_alphabetic (the_line
[start
]))
2165 /* **************************************************************** */
2169 /* **************************************************************** */
2171 /* Transpose the words at point. */
2172 rl_transpose_words (count
)
2175 char *word1
, *word2
;
2176 int w1_beg
, w1_end
, w2_beg
, w2_end
;
2177 int orig_point
= rl_point
;
2181 /* Find the two words. */
2182 rl_forward_word (count
);
2184 rl_backward_word (1);
2186 rl_backward_word (count
);
2188 rl_forward_word (1);
2191 /* Do some check to make sure that there really are two words. */
2192 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
2195 rl_point
= orig_point
;
2199 /* Get the text of the words. */
2200 word1
= rl_copy_text (w1_beg
, w1_end
);
2201 word2
= rl_copy_text (w2_beg
, w2_end
);
2203 /* We are about to do many insertions and deletions. Remember them
2204 as one operation. */
2205 rl_begin_undo_group ();
2207 /* Do the stuff at word2 first, so that we don't have to worry
2208 about word1 moving. */
2210 rl_delete_text (w2_beg
, w2_end
);
2211 rl_insert_text (word1
);
2214 rl_delete_text (w1_beg
, w1_end
);
2215 rl_insert_text (word2
);
2217 /* This is exactly correct since the text before this point has not
2218 changed in length. */
2221 /* I think that does it. */
2222 rl_end_undo_group ();
2223 free (word1
); free (word2
);
2226 /* Transpose the characters at point. If point is at the end of the line,
2227 then transpose the characters before point. */
2228 rl_transpose_chars (count
)
2236 if (!rl_point
|| rl_end
< 2)
2242 rl_begin_undo_group ();
2244 if (rl_point
== rl_end
)
2251 dummy
[0] = the_line
[rl_point
];
2254 rl_delete_text (rl_point
, rl_point
+ 1);
2257 if (rl_point
> rl_end
)
2259 else if (rl_point
< 0)
2261 rl_insert_text (dummy
);
2263 rl_end_undo_group ();
2266 /* **************************************************************** */
2268 /* Undo, and Undoing */
2270 /* **************************************************************** */
2272 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
2274 static int doing_an_undo
= 0;
2276 /* The current undo list for THE_LINE. */
2277 UNDO_LIST
*rl_undo_list
= (UNDO_LIST
*)NULL
;
2279 /* Remember how to undo something. Concatenate some undos if that
2281 rl_add_undo (what
, start
, end
, text
)
2282 enum undo_code what
;
2286 UNDO_LIST
*temp
= (UNDO_LIST
*)xmalloc (sizeof (UNDO_LIST
));
2288 temp
->start
= start
;
2291 temp
->next
= rl_undo_list
;
2292 rl_undo_list
= temp
;
2295 /* Free the existing undo list. */
2298 while (rl_undo_list
)
2300 UNDO_LIST
*release
= rl_undo_list
;
2301 rl_undo_list
= rl_undo_list
->next
;
2303 if (release
->what
== UNDO_DELETE
)
2304 free (release
->text
);
2308 rl_undo_list
= (UNDO_LIST
*)NULL
;
2311 /* Undo the next thing in the list. Return 0 if there
2312 is nothing to undo, or non-zero if there was. */
2317 int waiting_for_begin
= 0;
2325 switch (rl_undo_list
->what
) {
2327 /* Undoing deletes means inserting some text. */
2329 rl_point
= rl_undo_list
->start
;
2330 rl_insert_text (rl_undo_list
->text
);
2331 free (rl_undo_list
->text
);
2334 /* Undoing inserts means deleting some text. */
2336 rl_delete_text (rl_undo_list
->start
, rl_undo_list
->end
);
2337 rl_point
= rl_undo_list
->start
;
2340 /* Undoing an END means undoing everything 'til we get to
2343 waiting_for_begin
++;
2346 /* Undoing a BEGIN means that we are done with this group. */
2348 if (waiting_for_begin
)
2349 waiting_for_begin
--;
2361 release
= rl_undo_list
;
2362 rl_undo_list
= rl_undo_list
->next
;
2365 if (waiting_for_begin
)
2371 /* Begin a group. Subsequent undos are undone as an atomic operation. */
2372 rl_begin_undo_group ()
2374 rl_add_undo (UNDO_BEGIN
, 0, 0, 0);
2377 /* End an undo group started with rl_begin_undo_group (). */
2378 rl_end_undo_group ()
2380 rl_add_undo (UNDO_END
, 0, 0, 0);
2383 /* Save an undo entry for the text from START to END. */
2384 rl_modifying (start
, end
)
2396 char *temp
= rl_copy_text (start
, end
);
2397 rl_begin_undo_group ();
2398 rl_add_undo (UNDO_DELETE
, start
, end
, temp
);
2399 rl_add_undo (UNDO_INSERT
, start
, end
, (char *)NULL
);
2400 rl_end_undo_group ();
2404 /* Revert the current line to its previous state. */
2411 while (rl_undo_list
)
2416 /* Do some undoing of things that were done. */
2417 rl_undo_command (count
)
2419 if (count
< 0) return; /* Nothing to do. */
2435 /* **************************************************************** */
2437 /* History Utilities */
2439 /* **************************************************************** */
2441 /* We already have a history library, and that is what we use to control
2442 the history features of readline. However, this is our local interface
2443 to the history mechanism. */
2445 /* While we are editing the history, this is the saved
2446 version of the original line. */
2447 HIST_ENTRY
*saved_line_for_history
= (HIST_ENTRY
*)NULL
;
2449 /* Set the history pointer back to the last entry in the history. */
2450 start_using_history ()
2453 if (saved_line_for_history
)
2454 free_history_entry (saved_line_for_history
);
2456 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
2459 /* Free the contents (and containing structure) of a HIST_ENTRY. */
2461 free_history_entry (entry
)
2470 /* Perhaps put back the current line if it has changed. */
2471 maybe_replace_line ()
2473 HIST_ENTRY
*temp
= current_history ();
2475 /* If the current line has changed, save the changes. */
2476 if (temp
&& ((UNDO_LIST
*)(temp
->data
) != rl_undo_list
))
2478 temp
= replace_history_entry (where_history (), the_line
, rl_undo_list
);
2484 /* Put back the saved_line_for_history if there is one. */
2485 maybe_unsave_line ()
2487 if (saved_line_for_history
)
2491 line_len
= strlen (saved_line_for_history
->line
);
2493 if (line_len
>= rl_line_buffer_len
)
2494 rl_extend_line_buffer (line_len
);
2496 strcpy (the_line
, saved_line_for_history
->line
);
2497 rl_undo_list
= (UNDO_LIST
*)saved_line_for_history
->data
;
2498 free_history_entry (saved_line_for_history
);
2499 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
2500 rl_end
= rl_point
= strlen (the_line
);
2506 /* Save the current line in saved_line_for_history. */
2509 if (!saved_line_for_history
)
2511 saved_line_for_history
= (HIST_ENTRY
*)xmalloc (sizeof (HIST_ENTRY
));
2512 saved_line_for_history
->line
= savestring (the_line
);
2513 saved_line_for_history
->data
= (char *)rl_undo_list
;
2517 /* **************************************************************** */
2519 /* History Commands */
2521 /* **************************************************************** */
2523 /* Meta-< goes to the start of the history. */
2524 rl_beginning_of_history ()
2526 rl_get_previous_history (1 + where_history ());
2529 /* Meta-> goes to the end of the history. (The current line). */
2530 rl_end_of_history ()
2532 maybe_replace_line ();
2534 maybe_unsave_line ();
2537 /* Move down to the next history line. */
2538 rl_get_next_history (count
)
2541 HIST_ENTRY
*temp
= (HIST_ENTRY
*)NULL
;
2545 rl_get_previous_history (-count
);
2552 maybe_replace_line ();
2556 temp
= next_history ();
2563 maybe_unsave_line ();
2568 line_len
= strlen (temp
->line
);
2570 if (line_len
>= rl_line_buffer_len
)
2571 rl_extend_line_buffer (line_len
);
2573 strcpy (the_line
, temp
->line
);
2574 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
2575 rl_end
= rl_point
= strlen (the_line
);
2576 #if defined (VI_MODE)
2577 if (rl_editing_mode
== vi_mode
)
2579 #endif /* VI_MODE */
2583 /* Get the previous item out of our interactive history, making it the current
2584 line. If there is no previous history, just ding. */
2585 rl_get_previous_history (count
)
2588 HIST_ENTRY
*old_temp
= (HIST_ENTRY
*)NULL
;
2589 HIST_ENTRY
*temp
= (HIST_ENTRY
*)NULL
;
2593 rl_get_next_history (-count
);
2600 /* If we don't have a line saved, then save this one. */
2603 /* If the current line has changed, save the changes. */
2604 maybe_replace_line ();
2608 temp
= previous_history ();
2616 /* If there was a large argument, and we moved back to the start of the
2617 history, that is not an error. So use the last value found. */
2618 if (!temp
&& old_temp
)
2627 line_len
= strlen (temp
->line
);
2629 if (line_len
>= rl_line_buffer_len
)
2630 rl_extend_line_buffer (line_len
);
2632 strcpy (the_line
, temp
->line
);
2633 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
2634 rl_end
= rl_point
= line_len
;
2636 #if defined (VI_MODE)
2637 if (rl_editing_mode
== vi_mode
)
2639 #endif /* VI_MODE */
2643 /* Make C be the next command to be executed. */
2647 rl_pending_input
= c
;
2650 /* **************************************************************** */
2652 /* The Mark and the Region. */
2654 /* **************************************************************** */
2656 /* Set the mark at POSITION. */
2657 rl_set_mark (position
)
2660 if (position
> rl_end
)
2666 /* Exchange the position of mark and point. */
2667 rl_exchange_mark_and_point ()
2669 if (rl_mark
> rl_end
)
2679 int temp
= rl_point
;
2687 /* **************************************************************** */
2689 /* Killing Mechanism */
2691 /* **************************************************************** */
2693 /* What we assume for a max number of kills. */
2694 #define DEFAULT_MAX_KILLS 10
2696 /* The real variable to look at to find out when to flush kills. */
2697 int rl_max_kills
= DEFAULT_MAX_KILLS
;
2699 /* Where to store killed text. */
2700 char **rl_kill_ring
= (char **)NULL
;
2702 /* Where we are in the kill ring. */
2703 int rl_kill_index
= 0;
2705 /* How many slots we have in the kill ring. */
2706 int rl_kill_ring_length
= 0;
2708 /* How to say that you only want to save a certain amount
2709 of kill material. */
2710 rl_set_retained_kills (num
)
2714 /* The way to kill something. This appends or prepends to the last
2715 kill, if the last command was a kill command. if FROM is less
2716 than TO, then the text is appended, otherwise prepended. If the
2717 last command was not a kill command, then a new slot is made for
2719 rl_kill_text (from
, to
)
2723 char *text
= rl_copy_text (from
, to
);
2725 /* Is there anything to kill? */
2729 last_command_was_kill
++;
2733 /* Delete the copied text from the line. */
2734 rl_delete_text (from
, to
);
2736 /* First, find the slot to work with. */
2737 if (!last_command_was_kill
)
2739 /* Get a new slot. */
2742 /* If we don't have any defined, then make one. */
2743 rl_kill_ring
= (char **)
2744 xmalloc (((rl_kill_ring_length
= 1) + 1) * sizeof (char *));
2749 /* We have to add a new slot on the end, unless we have
2750 exceeded the max limit for remembering kills. */
2751 slot
= rl_kill_ring_length
;
2752 if (slot
== rl_max_kills
)
2755 free (rl_kill_ring
[0]);
2756 for (i
= 0; i
< slot
; i
++)
2757 rl_kill_ring
[i
] = rl_kill_ring
[i
+ 1];
2763 xrealloc (rl_kill_ring
,
2764 ((slot
= (rl_kill_ring_length
+= 1)) + 1)
2772 slot
= rl_kill_ring_length
- 1;
2775 /* If the last command was a kill, prepend or append. */
2776 if (last_command_was_kill
&& rl_editing_mode
!= vi_mode
)
2778 char *old
= rl_kill_ring
[slot
];
2779 char *new = (char *)xmalloc (1 + strlen (old
) + strlen (text
));
2793 rl_kill_ring
[slot
] = new;
2797 rl_kill_ring
[slot
] = text
;
2799 rl_kill_index
= slot
;
2800 last_command_was_kill
++;
2803 /* Now REMEMBER! In order to do prepending or appending correctly, kill
2804 commands always make rl_point's original position be the FROM argument,
2805 and rl_point's extent be the TO argument. */
2807 /* **************************************************************** */
2809 /* Killing Commands */
2811 /* **************************************************************** */
2813 /* Delete the word at point, saving the text in the kill ring. */
2814 rl_kill_word (count
)
2817 int orig_point
= rl_point
;
2820 rl_backward_kill_word (-count
);
2823 rl_forward_word (count
);
2825 if (rl_point
!= orig_point
)
2826 rl_kill_text (orig_point
, rl_point
);
2828 rl_point
= orig_point
;
2832 /* Rubout the word before point, placing it on the kill ring. */
2833 rl_backward_kill_word (count
)
2836 int orig_point
= rl_point
;
2839 rl_kill_word (-count
);
2842 rl_backward_word (count
);
2844 if (rl_point
!= orig_point
)
2845 rl_kill_text (orig_point
, rl_point
);
2849 /* Kill from here to the end of the line. If DIRECTION is negative, kill
2850 back to the line start instead. */
2851 rl_kill_line (direction
)
2854 int orig_point
= rl_point
;
2857 rl_backward_kill_line (1);
2861 if (orig_point
!= rl_point
)
2862 rl_kill_text (orig_point
, rl_point
);
2863 rl_point
= orig_point
;
2867 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
2868 forwards to the line end instead. */
2869 rl_backward_kill_line (direction
)
2872 int orig_point
= rl_point
;
2883 rl_kill_text (orig_point
, rl_point
);
2888 /* Yank back the last killed text. This ignores arguments. */
2894 rl_set_mark (rl_point
);
2895 rl_insert_text (rl_kill_ring
[rl_kill_index
]);
2898 /* If the last command was yank, or yank_pop, and the text just
2899 before point is identical to the current kill item, then
2900 delete that text from the line, rotate the index down, and
2901 yank back some other text. */
2906 if (((rl_last_func
!= rl_yank_pop
) && (rl_last_func
!= rl_yank
)) ||
2912 l
= strlen (rl_kill_ring
[rl_kill_index
]);
2913 if (((rl_point
- l
) >= 0) &&
2914 (strncmp (the_line
+ (rl_point
- l
),
2915 rl_kill_ring
[rl_kill_index
], l
) == 0))
2917 rl_delete_text ((rl_point
- l
), rl_point
);
2920 if (rl_kill_index
< 0)
2921 rl_kill_index
= rl_kill_ring_length
- 1;
2929 /* Yank the COUNTth argument from the previous history line. */
2930 rl_yank_nth_arg (count
, ignore
)
2933 register HIST_ENTRY
*entry
= previous_history ();
2944 arg
= history_arg_extract (count
, count
, entry
->line
);
2951 rl_begin_undo_group ();
2953 #if defined (VI_MODE)
2954 /* Vi mode always inserts a space before yanking the argument, and it
2955 inserts it right *after* rl_point. */
2956 if (rl_editing_mode
== vi_mode
)
2958 #endif /* VI_MODE */
2960 if (rl_point
&& the_line
[rl_point
- 1] != ' ')
2961 rl_insert_text (" ");
2963 rl_insert_text (arg
);
2966 rl_end_undo_group ();
2969 /* How to toggle back and forth between editing modes. */
2970 rl_vi_editing_mode ()
2972 #if defined (VI_MODE)
2973 rl_editing_mode
= vi_mode
;
2974 rl_vi_insertion_mode ();
2975 #endif /* VI_MODE */
2978 rl_emacs_editing_mode ()
2980 rl_editing_mode
= emacs_mode
;
2981 _rl_keymap
= emacs_standard_keymap
;
2985 /* **************************************************************** */
2987 /* USG (System V) Support */
2989 /* **************************************************************** */
3000 return (getkey () & 0x7f);
3001 #endif /* __GO32__ */
3005 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
3007 if (result
== sizeof (unsigned char))
3010 /* If zero characters are returned, then the file that we are
3011 reading from is empty! Return EOF in that case. */
3015 #if defined (EWOULDBLOCK)
3016 if (errno
== EWOULDBLOCK
)
3020 if ((flags
= fcntl (fileno (stream
), F_GETFL
, 0)) < 0)
3022 if (flags
& O_NDELAY
)
3025 fcntl (fileno (stream
), F_SETFL
, flags
);
3030 #endif /* EWOULDBLOCK */
3032 #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
3033 if (errno
== EAGAIN
)
3037 if ((flags
= fcntl (fileno (stream
), F_GETFL
, 0)) < 0)
3039 if (flags
& O_NONBLOCK
)
3041 flags
&= ~O_NONBLOCK
;
3042 fcntl (fileno (stream
), F_SETFL
, flags
);
3046 #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
3049 /* If the error that we received was SIGINT, then try again,
3050 this is simply an interrupted system call to read ().
3051 Otherwise, some error ocurred, also signifying EOF. */
3054 #endif /* !__GO32__ */
3058 #if defined (STATIC_MALLOC)
3060 /* **************************************************************** */
3062 /* xmalloc and xrealloc () */
3064 /* **************************************************************** */
3066 static void memory_error_and_abort ();
3072 char *temp
= (char *)malloc (bytes
);
3075 memory_error_and_abort ();
3080 xrealloc (pointer
, bytes
)
3087 temp
= (char *)malloc (bytes
);
3089 temp
= (char *)realloc (pointer
, bytes
);
3092 memory_error_and_abort ();
3098 memory_error_and_abort ()
3100 fprintf (stderr
, "readline: Out of virtual memory!\n");
3103 #endif /* STATIC_MALLOC */
3106 /* **************************************************************** */
3108 /* Testing Readline */
3110 /* **************************************************************** */
3116 HIST_ENTRY
**history_list ();
3117 char *temp
= (char *)NULL
;
3118 char *prompt
= "readline% ";
3123 temp
= readline (prompt
);
3129 /* If there is anything on the line, print it and remember it. */
3132 fprintf (stderr
, "%s\r\n", temp
);
3136 /* Check for `command' that we handle. */
3137 if (strcmp (temp
, "quit") == 0)
3140 if (strcmp (temp
, "list") == 0)
3142 HIST_ENTRY
**list
= history_list ();
3146 for (i
= 0; list
[i
]; i
++)
3148 fprintf (stderr
, "%d: %s\r\n", i
, list
[i
]->line
);
3149 free (list
[i
]->line
);
3163 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"