1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 1, or
12 (at your option) any later version.
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
30 #include "posixstat.h"
32 #if defined (HAVE_SYS_FILE_H)
33 # include <sys/file.h>
34 #endif /* HAVE_SYS_FILE_H */
36 #if defined (HAVE_UNISTD_H)
38 #endif /* HAVE_UNISTD_H */
40 #if defined (HAVE_STDLIB_H)
43 # include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
46 #if defined (HAVE_LOCALE_H)
54 /* System-specific feature definitions and include files. */
58 # define INCL_DOSPROCESS
62 /* Some standard library routines. */
66 #ifndef RL_LIBRARY_VERSION
67 # define RL_LIBRARY_VERSION "4.0"
70 /* Evaluates its arguments multiple times. */
71 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
73 /* NOTE: Functions and variables prefixed with `_rl_' are
74 pseudo-global: they are global so they can be shared
75 between files in the readline library, but are not intended
76 to be visible to readline callers. */
78 /* Variables and functions imported from terminal.c */
79 extern int _rl_init_terminal_io ();
80 extern void _rl_enable_meta_key ();
82 extern void _rl_output_character_function ();
84 extern int _rl_output_character_function ();
87 extern int _rl_enable_meta
;
88 extern int _rl_term_autowrap
;
89 extern int screenwidth
, screenheight
, screenchars
;
91 /* Variables and functions imported from rltty.c. */
92 extern void rl_prep_terminal (), rl_deprep_terminal ();
93 extern void rltty_set_default_bindings ();
95 /* Functions imported from util.c. */
96 extern void _rl_abort_internal ();
97 extern void rl_extend_line_buffer ();
98 extern int alphabetic ();
100 /* Functions imported from bind.c. */
101 extern void _rl_bind_if_unbound ();
103 /* Functions imported from input.c. */
104 extern int _rl_any_typein ();
105 extern void _rl_insert_typein ();
106 extern int rl_read_key ();
108 /* Functions imported from nls.c */
109 extern int _rl_init_eightbit ();
111 /* Functions imported from shell.c */
112 extern char *get_env_value ();
114 /* External redisplay functions and variables from display.c */
115 extern void _rl_move_vert ();
116 extern void _rl_update_final ();
117 extern void _rl_clear_to_eol ();
118 extern void _rl_clear_screen ();
119 extern void _rl_erase_entire_line ();
121 extern void _rl_erase_at_end_of_line ();
122 extern void _rl_move_cursor_relative ();
124 extern int _rl_vis_botlin
;
125 extern int _rl_last_c_pos
;
126 extern int _rl_horizontal_scroll_mode
;
127 extern int rl_display_fixed
;
128 extern int _rl_suppress_redisplay
;
129 extern char *rl_display_prompt
;
131 /* Variables imported from complete.c. */
132 extern char *rl_completer_word_break_characters
;
133 extern char *rl_basic_word_break_characters
;
134 extern int rl_completion_query_items
;
135 extern int rl_complete_with_tilde_expansion
;
137 /* Variables and functions from macro.c. */
138 extern void _rl_add_macro_char ();
139 extern void _rl_with_macro_input ();
140 extern int _rl_next_macro_key ();
141 extern int _rl_defining_kbd_macro
;
143 #if defined (VI_MODE)
144 /* Functions imported from vi_mode.c. */
145 extern void _rl_vi_set_last ();
146 extern void _rl_vi_reset_last ();
147 extern void _rl_vi_done_inserting ();
148 extern int _rl_vi_textmod_command ();
149 extern void _rl_vi_initialize_line ();
152 extern UNDO_LIST
*rl_undo_list
;
153 extern int _rl_doing_an_undo
;
155 /* Forward declarations used in this file. */
156 void _rl_free_history_entry ();
159 int _rl_init_argument ();
161 static char *readline_internal ();
162 static void readline_initialize_everything ();
163 static void start_using_history ();
164 static void bind_arrow_keys ();
166 #if !defined (__GO32__) || defined (HAVE_TERMIOS_H)
167 static void readline_default_bindings ();
168 #endif /* !__GO32__ */
170 #if defined (__GO32__)
173 # if !defined (__DJGPP__)
174 # undef HANDLE_SIGNALS
175 # endif /* !__DJGPP__ */
176 #endif /* __GO32__ */
178 extern char *xmalloc (), *xrealloc ();
180 /* **************************************************************** */
182 /* Line editing input utility */
184 /* **************************************************************** */
186 char *rl_library_version
= RL_LIBRARY_VERSION
;
188 /* A pointer to the keymap that is currently in use.
189 By default, it is the standard emacs keymap. */
190 Keymap _rl_keymap
= emacs_standard_keymap
;
192 /* The current style of editing. */
193 int rl_editing_mode
= emacs_mode
;
195 /* Non-zero if we called this function from _rl_dispatch(). It's present
196 so functions can find out whether they were called from a key binding
197 or directly from an application. */
200 /* Non-zero if the previous command was a kill command. */
201 int _rl_last_command_was_kill
= 0;
203 /* The current value of the numeric argument specified by the user. */
204 int rl_numeric_arg
= 1;
206 /* Non-zero if an argument was typed. */
207 int rl_explicit_arg
= 0;
209 /* Temporary value used while generating the argument. */
212 /* Non-zero means we have been called at least once before. */
213 static int rl_initialized
;
215 /* If non-zero, this program is running in an EMACS buffer. */
216 static int running_in_emacs
;
218 /* The current offset in the current input line. */
221 /* Mark in the current input line. */
224 /* Length of the current input line. */
227 /* Make this non-zero to return the current input_line. */
230 /* The last function executed by readline. */
231 Function
*rl_last_func
= (Function
*)NULL
;
233 /* Top level environment for readline_internal (). */
234 procenv_t readline_top_level
;
236 /* The streams we interact with. */
237 FILE *_rl_in_stream
, *_rl_out_stream
;
239 /* The names of the streams that we do input and output to. */
240 FILE *rl_instream
= (FILE *)NULL
;
241 FILE *rl_outstream
= (FILE *)NULL
;
243 /* Non-zero means echo characters as they are read. */
244 int readline_echoing_p
= 1;
246 /* Current prompt. */
248 int rl_visible_prompt_length
= 0;
250 /* The number of characters read in order to type this complete command. */
251 int rl_key_sequence_length
= 0;
253 /* If non-zero, then this is the address of a function to call just
254 before readline_internal_setup () prints the first prompt. */
255 Function
*rl_startup_hook
= (Function
*)NULL
;
257 /* If non-zero, this is the address of a function to call just before
258 readline_internal_setup () returns and readline_internal starts
259 reading input characters. */
260 Function
*rl_pre_input_hook
= (Function
*)NULL
;
262 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
263 static char *the_line
;
265 /* The character that can generate an EOF. Really read from
266 the terminal driver... just defaulted here. */
267 int _rl_eof_char
= CTRL ('D');
269 /* Non-zero makes this the next keystroke to read. */
270 int rl_pending_input
= 0;
272 /* Pointer to a useful terminal name. */
273 char *rl_terminal_name
= (char *)NULL
;
275 /* Non-zero means to always use horizontal scrolling in line display. */
276 int _rl_horizontal_scroll_mode
= 0;
278 /* Non-zero means to display an asterisk at the starts of history lines
279 which have been modified. */
280 int _rl_mark_modified_lines
= 0;
282 /* The style of `bell' notification preferred. This can be set to NO_BELL,
283 AUDIBLE_BELL, or VISIBLE_BELL. */
284 int _rl_bell_preference
= AUDIBLE_BELL
;
286 /* String inserted into the line by rl_insert_comment (). */
287 char *_rl_comment_begin
;
289 /* Keymap holding the function currently being executed. */
290 Keymap rl_executing_keymap
;
292 /* Non-zero means to erase entire line, including prompt, on empty input lines. */
293 int rl_erase_empty_line
= 0;
295 /* Line buffer and maintenence. */
296 char *rl_line_buffer
= (char *)NULL
;
297 int rl_line_buffer_len
= 0;
299 /* Forward declarations used by the display and termcap code. */
301 /* **************************************************************** */
303 /* `Forward' declarations */
305 /* **************************************************************** */
307 /* Non-zero means do not parse any lines other than comments and
308 parser directives. */
309 unsigned char _rl_parsing_conditionalized_out
= 0;
311 /* Non-zero means to convert characters with the meta bit set to
312 escape-prefixed characters so we can indirect through
313 emacs_meta_keymap or vi_escape_keymap. */
314 int _rl_convert_meta_chars_to_ascii
= 1;
316 /* Non-zero means to output characters with the meta bit set directly
317 rather than as a meta-prefixed escape sequence. */
318 int _rl_output_meta_chars
= 0;
320 /* **************************************************************** */
322 /* Top Level Functions */
324 /* **************************************************************** */
326 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
327 int _rl_meta_flag
= 0; /* Forward declaration */
329 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
330 none. A return value of NULL means that EOF was encountered. */
339 /* If we are at EOF return a NULL string. */
340 if (rl_pending_input
== EOF
)
342 rl_pending_input
= 0;
343 return ((char *)NULL
);
346 rl_visible_prompt_length
= rl_expand_prompt (rl_prompt
);
349 (*rl_prep_term_function
) (_rl_meta_flag
);
351 #if defined (HANDLE_SIGNALS)
355 value
= readline_internal ();
356 (*rl_deprep_term_function
) ();
358 #if defined (HANDLE_SIGNALS)
365 #if defined (READLINE_CALLBACKS)
366 # define STATIC_CALLBACK
368 # define STATIC_CALLBACK static
372 readline_internal_setup ()
374 _rl_in_stream
= rl_instream
;
375 _rl_out_stream
= rl_outstream
;
378 (*rl_startup_hook
) ();
380 if (readline_echoing_p
== 0)
384 fprintf (_rl_out_stream
, "%s", rl_prompt
);
385 fflush (_rl_out_stream
);
391 (*rl_redisplay_function
) ();
392 #if defined (VI_MODE)
393 if (rl_editing_mode
== vi_mode
)
394 rl_vi_insertion_mode (1, 0);
398 if (rl_pre_input_hook
)
399 (*rl_pre_input_hook
) ();
402 STATIC_CALLBACK
char *
403 readline_internal_teardown (eof
)
409 /* Restore the original of this history line, iff the line that we
410 are editing was originally in the history, AND the line has changed. */
411 entry
= current_history ();
413 if (entry
&& rl_undo_list
)
415 temp
= savestring (the_line
);
416 rl_revert_line (1, 0);
417 entry
= replace_history_entry (where_history (), the_line
, (histdata_t
)NULL
);
418 _rl_free_history_entry (entry
);
420 strcpy (the_line
, temp
);
424 /* At any rate, it is highly likely that this line has an undo list. Get
429 return (eof
? (char *)NULL
: savestring (the_line
));
433 #if defined (READLINE_CALLBACKS)
434 readline_internal_char ()
436 readline_internal_charloop ()
439 static int lastc
, eof_found
;
445 #if !defined (READLINE_CALLBACKS)
449 lk
= _rl_last_command_was_kill
;
451 code
= setjmp (readline_top_level
);
454 (*rl_redisplay_function
) ();
456 if (rl_pending_input
== 0)
458 /* Then initialize the argument and number of keys read. */
459 _rl_init_argument ();
460 rl_key_sequence_length
= 0;
465 /* EOF typed to a non-blank line is a <NL>. */
466 if (c
== EOF
&& rl_end
)
469 /* The character _rl_eof_char typed to blank line, and not as the
470 previous character is interpreted as EOF. */
471 if (((c
== _rl_eof_char
&& lastc
!= c
) || c
== EOF
) && !rl_end
)
473 #if defined (READLINE_CALLBACKS)
474 return (rl_done
= 1);
482 _rl_dispatch (c
, _rl_keymap
);
484 /* If there was no change in _rl_last_command_was_kill, then no kill
485 has taken place. Note that if input is pending we are reading
486 a prefix command, so nothing has changed yet. */
487 if (rl_pending_input
== 0 && lk
== _rl_last_command_was_kill
)
488 _rl_last_command_was_kill
= 0;
490 #if defined (VI_MODE)
491 /* In vi mode, when you exit insert mode, the cursor moves back
492 over the previous character. We explicitly check for that here. */
493 if (rl_editing_mode
== vi_mode
&& _rl_keymap
== vi_movement_keymap
)
498 (*rl_redisplay_function
) ();
500 /* If the application writer has told us to erase the entire line if
501 the only character typed was something bound to rl_newline, do so. */
502 if (rl_erase_empty_line
&& rl_done
&& rl_last_func
== rl_newline
&&
503 rl_point
== 0 && rl_end
== 0)
504 _rl_erase_entire_line ();
506 #if defined (READLINE_CALLBACKS)
515 #if defined (READLINE_CALLBACKS)
517 readline_internal_charloop ()
522 eof
= readline_internal_char ();
525 #endif /* READLINE_CALLBACKS */
527 /* Read a line of input from the global rl_instream, doing output on
528 the global rl_outstream.
529 If rl_prompt is non-null, then that is our prompt. */
535 readline_internal_setup ();
536 eof
= readline_internal_charloop ();
537 return (readline_internal_teardown (eof
));
541 _rl_init_line_state ()
543 rl_point
= rl_end
= 0;
544 the_line
= rl_line_buffer
;
551 the_line
= rl_line_buffer
;
554 /* Do the command associated with KEY in MAP.
555 If the associated command is really a keymap, then read
556 another key, and dispatch into that map. */
558 _rl_dispatch (key
, map
)
566 if (META_CHAR (key
) && _rl_convert_meta_chars_to_ascii
)
568 if (map
[ESC
].type
== ISKMAP
)
570 if (_rl_defining_kbd_macro
)
571 _rl_add_macro_char (ESC
);
572 map
= FUNCTION_TO_KEYMAP (map
, ESC
);
574 rl_key_sequence_length
+= 2;
575 return (_rl_dispatch (key
, map
));
582 if (_rl_defining_kbd_macro
)
583 _rl_add_macro_char (key
);
586 switch (map
[key
].type
)
589 func
= map
[key
].function
;
590 if (func
!= (Function
*)NULL
)
592 /* Special case rl_do_lowercase_version (). */
593 if (func
== rl_do_lowercase_version
)
594 return (_rl_dispatch (_rl_to_lower (key
), map
));
596 rl_executing_keymap
= map
;
599 _rl_suppress_redisplay
= (map
[key
].function
== rl_insert
) && _rl_input_available ();
603 r
= (*map
[key
].function
)(rl_numeric_arg
* rl_arg_sign
, key
);
606 /* If we have input pending, then the last command was a prefix
607 command. Don't change the state of rl_last_func. Otherwise,
608 remember the last command executed in this variable. */
609 if (!rl_pending_input
&& map
[key
].function
!= rl_digit_argument
)
610 rl_last_func
= map
[key
].function
;
614 _rl_abort_internal ();
620 if (map
[key
].function
!= (Function
*)NULL
)
622 rl_key_sequence_length
++;
623 newkey
= rl_read_key ();
624 r
= _rl_dispatch (newkey
, FUNCTION_TO_KEYMAP (map
, key
));
628 _rl_abort_internal ();
634 if (map
[key
].function
!= (Function
*)NULL
)
636 macro
= savestring ((char *)map
[key
].function
);
637 _rl_with_macro_input (macro
);
642 #if defined (VI_MODE)
643 if (rl_editing_mode
== vi_mode
&& _rl_keymap
== vi_movement_keymap
&&
644 _rl_vi_textmod_command (key
))
645 _rl_vi_set_last (key
, rl_numeric_arg
, rl_arg_sign
);
650 /* **************************************************************** */
652 /* Initializations */
654 /* **************************************************************** */
656 /* Initialize readline (and terminal if not already). */
660 /* If we have never been called before, initialize the
661 terminal and data structures. */
664 readline_initialize_everything ();
668 /* Initalize the current line information. */
669 _rl_init_line_state ();
671 /* We aren't done yet. We haven't even gotten started yet! */
674 /* Tell the history routines what is going on. */
675 start_using_history ();
677 /* Make the display buffer match the state of the line. */
678 rl_reset_line_state ();
680 /* No such function typed yet. */
681 rl_last_func
= (Function
*)NULL
;
683 /* Parsing of key-bindings begins in an enabled state. */
684 _rl_parsing_conditionalized_out
= 0;
686 #if defined (VI_MODE)
687 if (rl_editing_mode
== vi_mode
)
688 _rl_vi_initialize_line ();
694 #if defined (__EMX__)
696 _emx_build_environ ()
703 DosGetInfoBlocks (&tibp
, &pibp
);
704 t
= pibp
->pib_pchenv
;
707 tp
= environ
= (char **)xmalloc ((c
+ 1) * sizeof (char *));
708 t
= pibp
->pib_pchenv
;
718 /* Initialize the entire state of the world. */
720 readline_initialize_everything ()
722 #if defined (__EMX__)
724 _emx_build_environ ();
727 /* Find out if we are running in Emacs. */
728 running_in_emacs
= get_env_value ("EMACS") != (char *)0;
730 /* Set up input and output if they are not already set up. */
735 rl_outstream
= stdout
;
737 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
738 may change, but they may also be used before readline_internal ()
740 _rl_in_stream
= rl_instream
;
741 _rl_out_stream
= rl_outstream
;
743 /* Allocate data structures. */
744 if (rl_line_buffer
== 0)
745 rl_line_buffer
= xmalloc (rl_line_buffer_len
= DEFAULT_BUFFER_SIZE
);
747 /* Initialize the terminal interface. */
748 _rl_init_terminal_io ((char *)NULL
);
750 #if !defined (__GO32__) || defined (HAVE_TERMIOS_H)
751 /* Bind tty characters to readline functions. */
752 readline_default_bindings ();
753 #endif /* !__GO32__ || HAVE_TERMIOS_H */
755 /* Initialize the function names. */
756 rl_initialize_funmap ();
758 /* Decide whether we should automatically go into eight-bit mode. */
759 _rl_init_eightbit ();
761 /* Read in the init file. */
762 rl_read_init_file ((char *)NULL
);
765 if (_rl_horizontal_scroll_mode
&& _rl_term_autowrap
)
768 screenchars
-= screenheight
;
771 /* Override the effect of any `set keymap' assignments in the
773 rl_set_keymap_from_edit_mode ();
775 /* Try to bind a common arrow key prefix, if not already bound. */
778 /* Enable the meta key, if this terminal has one. */
780 _rl_enable_meta_key ();
782 /* If the completion parser's default word break characters haven't
783 been set yet, then do so now. */
784 if (rl_completer_word_break_characters
== (char *)NULL
)
785 rl_completer_word_break_characters
= rl_basic_word_break_characters
;
788 /* If this system allows us to look at the values of the regular
789 input editing characters, then bind them to their readline
790 equivalents, iff the characters are not bound to keymaps. */
792 readline_default_bindings ()
794 rltty_set_default_bindings (_rl_keymap
);
798 bind_arrow_keys_internal ()
802 f
= rl_function_of_keyseq ("\033[A", _rl_keymap
, (int *)NULL
);
803 if (!f
|| f
== rl_do_lowercase_version
)
805 _rl_bind_if_unbound ("\033[A", rl_get_previous_history
);
806 _rl_bind_if_unbound ("\033[B", rl_get_next_history
);
807 _rl_bind_if_unbound ("\033[C", rl_forward
);
808 _rl_bind_if_unbound ("\033[D", rl_backward
);
811 f
= rl_function_of_keyseq ("\033OA", _rl_keymap
, (int *)NULL
);
812 if (!f
|| f
== rl_do_lowercase_version
)
814 _rl_bind_if_unbound ("\033OA", rl_get_previous_history
);
815 _rl_bind_if_unbound ("\033OB", rl_get_next_history
);
816 _rl_bind_if_unbound ("\033OC", rl_forward
);
817 _rl_bind_if_unbound ("\033OD", rl_backward
);
821 /* Try and bind the common arrow key prefix after giving termcap and
822 the inputrc file a chance to bind them and create `real' keymaps
823 for the arrow key prefix. */
829 xkeymap
= _rl_keymap
;
831 _rl_keymap
= emacs_standard_keymap
;
832 bind_arrow_keys_internal ();
834 #if defined (VI_MODE)
835 _rl_keymap
= vi_movement_keymap
;
836 bind_arrow_keys_internal ();
839 _rl_keymap
= xkeymap
;
843 /* **************************************************************** */
845 /* Numeric Arguments */
847 /* **************************************************************** */
849 /* Handle C-u style numeric args, as well as M--, and M-digits. */
853 int key
, c
, sawminus
, sawdigits
;
857 sawminus
= sawdigits
= 0;
860 if (rl_numeric_arg
> 1000000)
862 sawdigits
= rl_explicit_arg
= rl_numeric_arg
= 0;
864 rl_restore_prompt ();
868 rl_message ("(arg: %d) ", rl_arg_sign
* rl_numeric_arg
);
869 key
= c
= rl_read_key ();
871 /* If we see a key bound to `universal-argument' after seeing digits,
872 it ends the argument but is otherwise ignored. */
873 if (_rl_keymap
[c
].type
== ISFUNC
&&
874 _rl_keymap
[c
].function
== rl_universal_argument
)
883 key
= rl_read_key ();
884 rl_restore_prompt ();
886 return (_rl_dispatch (key
, _rl_keymap
));
894 rl_numeric_arg
= rl_explicit_arg
? (rl_numeric_arg
* 10) + c
- '0' : c
- '0';
895 sawdigits
= rl_explicit_arg
= 1;
897 else if (c
== '-' && rl_explicit_arg
== 0)
899 rl_numeric_arg
= sawminus
= 1;
904 /* Make M-- command equivalent to M--1 command. */
905 if (sawminus
&& rl_numeric_arg
== 1 && rl_explicit_arg
== 0)
907 rl_restore_prompt ();
909 return (_rl_dispatch (key
, _rl_keymap
));
916 /* Add the current digit to the argument in progress. */
918 rl_digit_argument (ignore
, key
)
921 rl_pending_input
= key
;
922 return (rl_digit_loop ());
925 /* What to do when you abort reading an argument. */
927 rl_discard_argument ()
931 _rl_init_argument ();
935 /* Create a default argument. */
939 rl_numeric_arg
= rl_arg_sign
= 1;
944 /* C-u, universal argument. Multiply the current argument by 4.
945 Read a key. If the key has nothing to do with arguments, then
946 dispatch on it. If the key is the abort character then abort. */
948 rl_universal_argument (count
, key
)
952 return (rl_digit_loop ());
955 /* **************************************************************** */
957 /* Insert and Delete */
959 /* **************************************************************** */
961 /* Insert a string of text into the line at point. This is the only
962 way that you should do insertion. rl_insert () calls this
965 rl_insert_text (string
)
968 register int i
, l
= strlen (string
);
970 if (rl_end
+ l
>= rl_line_buffer_len
)
971 rl_extend_line_buffer (rl_end
+ l
);
973 for (i
= rl_end
; i
>= rl_point
; i
--)
974 the_line
[i
+ l
] = the_line
[i
];
975 strncpy (the_line
+ rl_point
, string
, l
);
977 /* Remember how to undo this if we aren't undoing something. */
978 if (!_rl_doing_an_undo
)
980 /* If possible and desirable, concatenate the undos. */
983 (rl_undo_list
->what
== UNDO_INSERT
) &&
984 (rl_undo_list
->end
== rl_point
) &&
985 (rl_undo_list
->end
- rl_undo_list
->start
< 20))
988 rl_add_undo (UNDO_INSERT
, rl_point
, rl_point
+ l
, (char *)NULL
);
992 the_line
[rl_end
] = '\0';
996 /* Delete the string between FROM and TO. FROM is
997 inclusive, TO is not. */
999 rl_delete_text (from
, to
)
1002 register char *text
;
1003 register int diff
, i
;
1005 /* Fix it if the caller is confused. */
1009 /* fix boundaries */
1017 text
= rl_copy_text (from
, to
);
1019 /* Some versions of strncpy() can't handle overlapping arguments. */
1021 for (i
= from
; i
< rl_end
- diff
; i
++)
1022 the_line
[i
] = the_line
[i
+ diff
];
1024 /* Remember how to undo this delete. */
1025 if (_rl_doing_an_undo
== 0)
1026 rl_add_undo (UNDO_DELETE
, from
, to
, text
);
1031 the_line
[rl_end
] = '\0';
1035 /* Fix up point so that it is within the line boundaries after killing
1036 text. If FIX_MARK_TOO is non-zero, the mark is forced within line
1039 #define _RL_FIX_POINT(x) \
1048 _rl_fix_point (fix_mark_too
)
1051 _RL_FIX_POINT (rl_point
);
1053 _RL_FIX_POINT (rl_mark
);
1055 #undef _RL_FIX_POINT
1058 _rl_replace_text (text
, start
, end
)
1062 rl_begin_undo_group ();
1063 rl_delete_text (start
, end
+ 1);
1065 rl_insert_text (text
);
1066 rl_end_undo_group ();
1069 /* **************************************************************** */
1071 /* Readline character functions */
1073 /* **************************************************************** */
1075 /* This is not a gap editor, just a stupid line input routine. No hair
1076 is involved in writing any of the functions, and none should be. */
1080 rl_end is the place in the string that we would place '\0';
1081 i.e., it is always safe to place '\0' there.
1083 rl_point is the place in the string where the cursor is. Sometimes
1084 this is the same as rl_end.
1086 Any command that is called interactively receives two arguments.
1087 The first is a count: the numeric arg pased to this command.
1088 The second is the key which invoked this command.
1091 /* **************************************************************** */
1093 /* Movement Commands */
1095 /* **************************************************************** */
1097 /* Note that if you `optimize' the display for these functions, you cannot
1098 use said functions in other functions which do not do optimizing display.
1099 I.e., you will have to update the data base for rl_redisplay, and you
1100 might as well let rl_redisplay do that job. */
1102 /* Move forward COUNT characters. */
1104 rl_forward (count
, key
)
1108 rl_backward (-count
, key
);
1111 int end
= rl_point
+ count
;
1112 #if defined (VI_MODE)
1113 int lend
= rl_end
- (rl_editing_mode
== vi_mode
);
1129 /* Move backward COUNT characters. */
1131 rl_backward (count
, key
)
1135 rl_forward (-count
, key
);
1138 if (rl_point
< count
)
1149 /* Move to the beginning of the line. */
1151 rl_beg_of_line (count
, key
)
1158 /* Move to the end of the line. */
1160 rl_end_of_line (count
, key
)
1167 /* Move forward a word. We do what Emacs does. */
1169 rl_forward_word (count
, key
)
1176 rl_backward_word (-count
, key
);
1182 if (rl_point
== rl_end
)
1185 /* If we are not in a word, move forward until we are in one.
1186 Then, move forward until we hit a non-alphabetic character. */
1187 c
= the_line
[rl_point
];
1188 if (alphabetic (c
) == 0)
1190 while (++rl_point
< rl_end
)
1192 c
= the_line
[rl_point
];
1197 if (rl_point
== rl_end
)
1199 while (++rl_point
< rl_end
)
1201 c
= the_line
[rl_point
];
1202 if (alphabetic (c
) == 0)
1210 /* Move backward a word. We do what Emacs does. */
1212 rl_backward_word (count
, key
)
1219 rl_forward_word (-count
, key
);
1228 /* Like rl_forward_word (), except that we look at the characters
1229 just before point. */
1231 c
= the_line
[rl_point
- 1];
1232 if (alphabetic (c
) == 0)
1236 c
= the_line
[rl_point
- 1];
1244 c
= the_line
[rl_point
- 1];
1245 if (alphabetic (c
) == 0)
1255 /* Clear the current line. Numeric argument to C-l does this. */
1257 rl_refresh_line (ignore1
, ignore2
)
1258 int ignore1
, ignore2
;
1260 int curr_line
, nleft
;
1262 /* Find out whether or not there might be invisible characters in the
1264 if (rl_display_prompt
== rl_prompt
)
1265 nleft
= _rl_last_c_pos
- screenwidth
- rl_visible_prompt_length
;
1267 nleft
= _rl_last_c_pos
- screenwidth
;
1270 curr_line
= 1 + nleft
/ screenwidth
;
1274 _rl_move_vert (curr_line
);
1275 _rl_move_cursor_relative (0, the_line
); /* XXX is this right */
1277 #if defined (__GO32__) && !defined (__DJGPP__)
1279 int row
, col
, width
, row_start
;
1281 ScreenGetCursor (&row
, &col
);
1282 width
= ScreenCols ();
1283 row_start
= ScreenPrimary
+ (row
* width
);
1284 memset (row_start
+ col
, 0, (width
- col
) * 2);
1286 #else /* !__GO32__ || __DJGPP__ */
1287 _rl_clear_to_eol (0); /* arg of 0 means to not use spaces */
1288 #endif /* !__GO32__ || __DJGPP__ */
1290 rl_forced_update_display ();
1291 rl_display_fixed
= 1;
1296 /* C-l typed to a line without quoting clears the screen, and then reprints
1297 the prompt and the current input line. Given a numeric arg, redraw only
1298 the current line. */
1300 rl_clear_screen (count
, key
)
1303 if (rl_explicit_arg
)
1305 rl_refresh_line (count
, key
);
1309 _rl_clear_screen (); /* calls termcap function to clear screen */
1310 rl_forced_update_display ();
1311 rl_display_fixed
= 1;
1317 rl_arrow_keys (count
, c
)
1322 ch
= rl_read_key ();
1324 switch (_rl_to_upper (ch
))
1327 rl_get_previous_history (count
, ch
);
1331 rl_get_next_history (count
, ch
);
1335 rl_forward (count
, ch
);
1339 rl_backward (count
, ch
);
1349 /* **************************************************************** */
1353 /* **************************************************************** */
1355 /* Insert the character C at the current location, moving point forward. */
1357 rl_insert (count
, c
)
1366 /* If we can optimize, then do it. But don't let people crash
1367 readline because of extra large arguments. */
1368 if (count
> 1 && count
<= 1024)
1370 string
= xmalloc (1 + count
);
1372 for (i
= 0; i
< count
; i
++)
1376 rl_insert_text (string
);
1387 for (i
= 0; i
< 1024; i
++)
1392 decreaser
= (count
> 1024 ? 1024 : count
);
1393 str
[decreaser
] = '\0';
1394 rl_insert_text (str
);
1401 /* We are inserting a single character.
1402 If there is pending input, then make a string of all of the
1403 pending characters that are bound to rl_insert, and insert
1405 if (_rl_any_typein ())
1406 _rl_insert_typein (c
);
1409 /* Inserting a single character. */
1414 rl_insert_text (str
);
1419 /* Insert the next typed character verbatim. */
1421 rl_quoted_insert (count
, key
)
1427 return (rl_insert (count
, c
));
1430 /* Insert a tab character. */
1432 rl_tab_insert (count
, key
)
1435 return (rl_insert (count
, '\t'));
1438 /* What to do when a NEWLINE is pressed. We accept the whole line.
1439 KEY is the key that invoked this command. I guess it could have
1440 meaning in the future. */
1442 rl_newline (count
, key
)
1447 #if defined (VI_MODE)
1448 if (rl_editing_mode
== vi_mode
)
1450 _rl_vi_done_inserting ();
1451 _rl_vi_reset_last ();
1453 #endif /* VI_MODE */
1455 /* If we've been asked to erase empty lines, suppress the final update,
1456 since _rl_update_final calls crlf(). */
1457 if (rl_erase_empty_line
&& rl_point
== 0 && rl_end
== 0)
1460 if (readline_echoing_p
)
1461 _rl_update_final ();
1465 /* What to do for some uppercase characters, like meta characters,
1466 and some characters appearing in emacs_ctlx_keymap. This function
1467 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1468 is special cased. */
1470 rl_do_lowercase_version (ignore1
, ignore2
)
1471 int ignore1
, ignore2
;
1476 /* Rubout the character behind point. */
1478 rl_rubout (count
, key
)
1483 rl_delete (-count
, key
);
1493 if (count
> 1 || rl_explicit_arg
)
1495 int orig_point
= rl_point
;
1496 rl_backward (count
, key
);
1497 rl_kill_text (orig_point
, rl_point
);
1501 int c
= the_line
[--rl_point
];
1502 rl_delete_text (rl_point
, rl_point
+ 1);
1504 if (rl_point
== rl_end
&& isprint (c
) && _rl_last_c_pos
)
1507 l
= rl_character_len (c
, rl_point
);
1508 _rl_erase_at_end_of_line (l
);
1514 /* Delete the character under the cursor. Given a numeric argument,
1515 kill that many characters instead. */
1517 rl_delete (count
, key
)
1521 return (rl_rubout (-count
, key
));
1523 if (rl_point
== rl_end
)
1529 if (count
> 1 || rl_explicit_arg
)
1531 int orig_point
= rl_point
;
1532 rl_forward (count
, key
);
1533 rl_kill_text (orig_point
, rl_point
);
1534 rl_point
= orig_point
;
1538 return (rl_delete_text (rl_point
, rl_point
+ 1));
1541 /* Delete the character under the cursor, unless the insertion
1542 point is at the end of the line, in which case the character
1543 behind the cursor is deleted. COUNT is obeyed and may be used
1544 to delete forward or backward that many characters. */
1546 rl_rubout_or_delete (count
, key
)
1549 if (rl_end
!= 0 && rl_point
== rl_end
)
1550 return (rl_rubout (count
, key
));
1552 return (rl_delete (count
, key
));
1555 /* Delete all spaces and tabs around point. */
1557 rl_delete_horizontal_space (count
, ignore
)
1560 int start
= rl_point
;
1562 while (rl_point
&& whitespace (the_line
[rl_point
- 1]))
1567 while (rl_point
< rl_end
&& whitespace (the_line
[rl_point
]))
1570 if (start
!= rl_point
)
1572 rl_delete_text (start
, rl_point
);
1578 /* Like the tcsh editing function delete-char-or-list. The eof character
1579 is caught before this is invoked, so this really does the same thing as
1580 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1582 rl_delete_or_show_completions (count
, key
)
1585 if (rl_end
!= 0 && rl_point
== rl_end
)
1586 return (rl_possible_completions (count
, key
));
1588 return (rl_delete (count
, key
));
1591 #ifndef RL_COMMENT_BEGIN_DEFAULT
1592 #define RL_COMMENT_BEGIN_DEFAULT "#"
1595 /* Turn the current line into a comment in shell history.
1596 A K*rn shell style function. */
1598 rl_insert_comment (count
, key
)
1601 rl_beg_of_line (1, key
);
1602 rl_insert_text (_rl_comment_begin
? _rl_comment_begin
1603 : RL_COMMENT_BEGIN_DEFAULT
);
1604 (*rl_redisplay_function
) ();
1605 rl_newline (1, '\n');
1609 /* **************************************************************** */
1613 /* **************************************************************** */
1615 /* The three kinds of things that we know how to do. */
1620 static int rl_change_case ();
1622 /* Uppercase the word at point. */
1624 rl_upcase_word (count
, key
)
1627 return (rl_change_case (count
, UpCase
));
1630 /* Lowercase the word at point. */
1632 rl_downcase_word (count
, key
)
1635 return (rl_change_case (count
, DownCase
));
1638 /* Upcase the first letter, downcase the rest. */
1640 rl_capitalize_word (count
, key
)
1643 return (rl_change_case (count
, CapCase
));
1646 /* The meaty function.
1647 Change the case of COUNT words, performing OP on them.
1648 OP is one of UpCase, DownCase, or CapCase.
1649 If a negative argument is given, leave point where it started,
1650 otherwise, leave it where it moves to. */
1652 rl_change_case (count
, op
)
1655 register int start
, end
;
1659 rl_forward_word (count
, 0);
1665 /* We are going to modify some text, so let's prepare to undo it. */
1666 rl_modifying (start
, end
);
1668 for (inword
= 0; start
< end
; start
++)
1670 c
= the_line
[start
];
1674 the_line
[start
] = _rl_to_upper (c
);
1678 the_line
[start
] = _rl_to_lower (c
);
1682 the_line
[start
] = (inword
== 0) ? _rl_to_upper (c
) : _rl_to_lower (c
);
1683 inword
= alphabetic (the_line
[start
]);
1695 /* **************************************************************** */
1699 /* **************************************************************** */
1701 /* Transpose the words at point. */
1703 rl_transpose_words (count
, key
)
1706 char *word1
, *word2
;
1707 int w1_beg
, w1_end
, w2_beg
, w2_end
;
1708 int orig_point
= rl_point
;
1713 /* Find the two words. */
1714 rl_forward_word (count
, key
);
1716 rl_backward_word (1, key
);
1718 rl_backward_word (count
, key
);
1720 rl_forward_word (1, key
);
1723 /* Do some check to make sure that there really are two words. */
1724 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
1727 rl_point
= orig_point
;
1731 /* Get the text of the words. */
1732 word1
= rl_copy_text (w1_beg
, w1_end
);
1733 word2
= rl_copy_text (w2_beg
, w2_end
);
1735 /* We are about to do many insertions and deletions. Remember them
1736 as one operation. */
1737 rl_begin_undo_group ();
1739 /* Do the stuff at word2 first, so that we don't have to worry
1740 about word1 moving. */
1742 rl_delete_text (w2_beg
, w2_end
);
1743 rl_insert_text (word1
);
1746 rl_delete_text (w1_beg
, w1_end
);
1747 rl_insert_text (word2
);
1749 /* This is exactly correct since the text before this point has not
1750 changed in length. */
1753 /* I think that does it. */
1754 rl_end_undo_group ();
1761 /* Transpose the characters at point. If point is at the end of the line,
1762 then transpose the characters before point. */
1764 rl_transpose_chars (count
, key
)
1772 if (!rl_point
|| rl_end
< 2)
1778 rl_begin_undo_group ();
1780 if (rl_point
== rl_end
)
1787 dummy
[0] = the_line
[rl_point
];
1790 rl_delete_text (rl_point
, rl_point
+ 1);
1794 rl_insert_text (dummy
);
1796 rl_end_undo_group ();
1800 /* **************************************************************** */
1802 /* Character Searching */
1804 /* **************************************************************** */
1807 _rl_char_search_internal (count
, dir
, schar
)
1808 int count
, dir
, schar
;
1813 inc
= (dir
< 0) ? -1 : 1;
1816 if ((dir
< 0 && pos
<= 0) || (dir
> 0 && pos
>= rl_end
))
1825 if (rl_line_buffer
[pos
] == schar
)
1829 rl_point
= (dir
== BTO
) ? pos
+ 1 : pos
;
1831 rl_point
= (dir
== FTO
) ? pos
- 1 : pos
;
1835 while ((dir
< 0) ? pos
-- : ++pos
< rl_end
);
1840 /* Search COUNT times for a character read from the current input stream.
1841 FDIR is the direction to search if COUNT is non-negative; otherwise
1842 the search goes in BDIR. */
1844 _rl_char_search (count
, fdir
, bdir
)
1845 int count
, fdir
, bdir
;
1851 return (_rl_char_search_internal (-count
, bdir
, c
));
1853 return (_rl_char_search_internal (count
, fdir
, c
));
1857 rl_char_search (count
, key
)
1860 return (_rl_char_search (count
, FFIND
, BFIND
));
1864 rl_backward_char_search (count
, key
)
1867 return (_rl_char_search (count
, BFIND
, FFIND
));
1870 /* **************************************************************** */
1872 /* History Utilities */
1874 /* **************************************************************** */
1876 /* We already have a history library, and that is what we use to control
1877 the history features of readline. This is our local interface to
1878 the history mechanism. */
1880 /* While we are editing the history, this is the saved
1881 version of the original line. */
1882 HIST_ENTRY
*saved_line_for_history
= (HIST_ENTRY
*)NULL
;
1884 /* Set the history pointer back to the last entry in the history. */
1886 start_using_history ()
1889 if (saved_line_for_history
)
1890 _rl_free_history_entry (saved_line_for_history
);
1892 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
1895 /* Free the contents (and containing structure) of a HIST_ENTRY. */
1897 _rl_free_history_entry (entry
)
1907 /* Perhaps put back the current line if it has changed. */
1909 maybe_replace_line ()
1913 temp
= current_history ();
1914 /* If the current line has changed, save the changes. */
1915 if (temp
&& ((UNDO_LIST
*)(temp
->data
) != rl_undo_list
))
1917 temp
= replace_history_entry (where_history (), the_line
, (histdata_t
)rl_undo_list
);
1924 /* Put back the saved_line_for_history if there is one. */
1926 maybe_unsave_line ()
1930 if (saved_line_for_history
)
1932 line_len
= strlen (saved_line_for_history
->line
);
1934 if (line_len
>= rl_line_buffer_len
)
1935 rl_extend_line_buffer (line_len
);
1937 strcpy (the_line
, saved_line_for_history
->line
);
1938 rl_undo_list
= (UNDO_LIST
*)saved_line_for_history
->data
;
1939 _rl_free_history_entry (saved_line_for_history
);
1940 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
1941 rl_end
= rl_point
= strlen (the_line
);
1948 /* Save the current line in saved_line_for_history. */
1952 if (saved_line_for_history
== 0)
1954 saved_line_for_history
= (HIST_ENTRY
*)xmalloc (sizeof (HIST_ENTRY
));
1955 saved_line_for_history
->line
= savestring (the_line
);
1956 saved_line_for_history
->data
= (char *)rl_undo_list
;
1961 /* **************************************************************** */
1963 /* History Commands */
1965 /* **************************************************************** */
1967 /* Meta-< goes to the start of the history. */
1969 rl_beginning_of_history (count
, key
)
1972 return (rl_get_previous_history (1 + where_history (), key
));
1975 /* Meta-> goes to the end of the history. (The current line). */
1977 rl_end_of_history (count
, key
)
1980 maybe_replace_line ();
1982 maybe_unsave_line ();
1986 /* Move down to the next history line. */
1988 rl_get_next_history (count
, key
)
1995 return (rl_get_previous_history (-count
, key
));
2000 maybe_replace_line ();
2002 temp
= (HIST_ENTRY
*)NULL
;
2005 temp
= next_history ();
2012 maybe_unsave_line ();
2015 line_len
= strlen (temp
->line
);
2017 if (line_len
>= rl_line_buffer_len
)
2018 rl_extend_line_buffer (line_len
);
2020 strcpy (the_line
, temp
->line
);
2021 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
2022 rl_end
= rl_point
= strlen (the_line
);
2023 #if defined (VI_MODE)
2024 if (rl_editing_mode
== vi_mode
)
2026 #endif /* VI_MODE */
2031 /* Get the previous item out of our interactive history, making it the current
2032 line. If there is no previous history, just ding. */
2034 rl_get_previous_history (count
, key
)
2037 HIST_ENTRY
*old_temp
, *temp
;
2041 return (rl_get_next_history (-count
, key
));
2046 /* If we don't have a line saved, then save this one. */
2049 /* If the current line has changed, save the changes. */
2050 maybe_replace_line ();
2052 temp
= old_temp
= (HIST_ENTRY
*)NULL
;
2055 temp
= previous_history ();
2063 /* If there was a large argument, and we moved back to the start of the
2064 history, that is not an error. So use the last value found. */
2065 if (!temp
&& old_temp
)
2072 line_len
= strlen (temp
->line
);
2074 if (line_len
>= rl_line_buffer_len
)
2075 rl_extend_line_buffer (line_len
);
2077 strcpy (the_line
, temp
->line
);
2078 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
2079 rl_end
= rl_point
= line_len
;
2081 #if defined (VI_MODE)
2082 if (rl_editing_mode
== vi_mode
)
2084 #endif /* VI_MODE */
2089 /* **************************************************************** */
2091 /* The Mark and the Region. */
2093 /* **************************************************************** */
2095 /* Set the mark at POSITION. */
2097 _rl_set_mark_at_pos (position
)
2100 if (position
> rl_end
)
2107 /* A bindable command to set the mark. */
2109 rl_set_mark (count
, key
)
2112 return (_rl_set_mark_at_pos (rl_explicit_arg
? count
: rl_point
));
2115 /* Exchange the position of mark and point. */
2117 rl_exchange_point_and_mark (count
, key
)
2120 if (rl_mark
> rl_end
)
2129 SWAP (rl_point
, rl_mark
);
2134 /* **************************************************************** */
2138 /* **************************************************************** */
2139 /* How to toggle back and forth between editing modes. */
2141 rl_vi_editing_mode (count
, key
)
2144 #if defined (VI_MODE)
2145 rl_editing_mode
= vi_mode
;
2146 rl_vi_insertion_mode (1, key
);
2147 #endif /* VI_MODE */
2152 rl_emacs_editing_mode (count
, key
)
2155 rl_editing_mode
= emacs_mode
;
2156 _rl_keymap
= emacs_standard_keymap
;