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 2, 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 59 Temple Place, Suite 330, Boston, MA 02111 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)
53 /* System-specific feature definitions and include files. */
57 # define INCL_DOSPROCESS
61 /* Some standard library routines. */
65 #include "rlprivate.h"
69 #ifndef RL_LIBRARY_VERSION
70 # define RL_LIBRARY_VERSION "4.1"
73 /* Evaluates its arguments multiple times. */
74 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
76 /* Forward declarations used in this file. */
77 void _rl_free_history_entry
__P((HIST_ENTRY
*));
79 static char *readline_internal
__P((void));
80 static void readline_initialize_everything
__P((void));
81 static void start_using_history
__P((void));
82 static void bind_arrow_keys
__P((void));
83 static int rl_change_case
__P((int, int));
85 static void readline_default_bindings
__P((void));
87 /* **************************************************************** */
89 /* Line editing input utility */
91 /* **************************************************************** */
93 char *rl_library_version
= RL_LIBRARY_VERSION
;
95 int rl_gnu_readline_p
= 1;
97 /* A pointer to the keymap that is currently in use.
98 By default, it is the standard emacs keymap. */
99 Keymap _rl_keymap
= emacs_standard_keymap
;
101 /* The current style of editing. */
102 int rl_editing_mode
= emacs_mode
;
104 /* Non-zero if we called this function from _rl_dispatch(). It's present
105 so functions can find out whether they were called from a key binding
106 or directly from an application. */
109 /* Non-zero if the previous command was a kill command. */
110 int _rl_last_command_was_kill
= 0;
112 /* The current value of the numeric argument specified by the user. */
113 int rl_numeric_arg
= 1;
115 /* Non-zero if an argument was typed. */
116 int rl_explicit_arg
= 0;
118 /* Temporary value used while generating the argument. */
121 /* Non-zero means we have been called at least once before. */
122 static int rl_initialized
;
124 /* If non-zero, this program is running in an EMACS buffer. */
125 static int running_in_emacs
;
127 /* The current offset in the current input line. */
130 /* Mark in the current input line. */
133 /* Length of the current input line. */
136 /* Make this non-zero to return the current input_line. */
139 /* The last function executed by readline. */
140 Function
*rl_last_func
= (Function
*)NULL
;
142 /* Top level environment for readline_internal (). */
143 procenv_t readline_top_level
;
145 /* The streams we interact with. */
146 FILE *_rl_in_stream
, *_rl_out_stream
;
148 /* The names of the streams that we do input and output to. */
149 FILE *rl_instream
= (FILE *)NULL
;
150 FILE *rl_outstream
= (FILE *)NULL
;
152 /* Non-zero means echo characters as they are read. */
153 int readline_echoing_p
= 1;
155 /* Current prompt. */
157 int rl_visible_prompt_length
= 0;
159 /* Set to non-zero by calling application if it has already printed rl_prompt
160 and does not want readline to do it the first time. */
161 int rl_already_prompted
= 0;
163 /* The number of characters read in order to type this complete command. */
164 int rl_key_sequence_length
= 0;
166 /* If non-zero, then this is the address of a function to call just
167 before readline_internal_setup () prints the first prompt. */
168 Function
*rl_startup_hook
= (Function
*)NULL
;
170 /* If non-zero, this is the address of a function to call just before
171 readline_internal_setup () returns and readline_internal starts
172 reading input characters. */
173 Function
*rl_pre_input_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 /* The style of `bell' notification preferred. This can be set to NO_BELL,
196 AUDIBLE_BELL, or VISIBLE_BELL. */
197 int _rl_bell_preference
= AUDIBLE_BELL
;
199 /* String inserted into the line by rl_insert_comment (). */
200 char *_rl_comment_begin
;
202 /* Keymap holding the function currently being executed. */
203 Keymap rl_executing_keymap
;
205 /* Non-zero means to erase entire line, including prompt, on empty input lines. */
206 int rl_erase_empty_line
= 0;
208 /* Non-zero means to read only this many characters rather than up to a
209 character bound to accept-line. */
210 int rl_num_chars_to_read
;
212 /* Line buffer and maintenence. */
213 char *rl_line_buffer
= (char *)NULL
;
214 int rl_line_buffer_len
= 0;
216 /* Forward declarations used by the display and termcap code. */
218 /* **************************************************************** */
220 /* `Forward' declarations */
222 /* **************************************************************** */
224 /* Non-zero means do not parse any lines other than comments and
225 parser directives. */
226 unsigned char _rl_parsing_conditionalized_out
= 0;
228 /* Non-zero means to convert characters with the meta bit set to
229 escape-prefixed characters so we can indirect through
230 emacs_meta_keymap or vi_escape_keymap. */
231 int _rl_convert_meta_chars_to_ascii
= 1;
233 /* Non-zero means to output characters with the meta bit set directly
234 rather than as a meta-prefixed escape sequence. */
235 int _rl_output_meta_chars
= 0;
237 /* **************************************************************** */
239 /* Top Level Functions */
241 /* **************************************************************** */
243 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
244 int _rl_meta_flag
= 0; /* Forward declaration */
246 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
247 none. A return value of NULL means that EOF was encountered. */
256 /* If we are at EOF return a NULL string. */
257 if (rl_pending_input
== EOF
)
259 rl_pending_input
= 0;
260 return ((char *)NULL
);
263 rl_visible_prompt_length
= rl_expand_prompt (rl_prompt
);
266 (*rl_prep_term_function
) (_rl_meta_flag
);
268 #if defined (HANDLE_SIGNALS)
272 value
= readline_internal ();
273 (*rl_deprep_term_function
) ();
275 #if defined (HANDLE_SIGNALS)
282 #if defined (READLINE_CALLBACKS)
283 # define STATIC_CALLBACK
285 # define STATIC_CALLBACK static
289 readline_internal_setup ()
293 _rl_in_stream
= rl_instream
;
294 _rl_out_stream
= rl_outstream
;
297 (*rl_startup_hook
) ();
299 if (readline_echoing_p
== 0)
301 if (rl_prompt
&& rl_already_prompted
== 0)
303 nprompt
= _rl_strip_prompt (rl_prompt
);
304 fprintf (_rl_out_stream
, "%s", nprompt
);
305 fflush (_rl_out_stream
);
311 if (rl_prompt
&& rl_already_prompted
)
312 rl_on_new_line_with_prompt ();
315 (*rl_redisplay_function
) ();
316 #if defined (VI_MODE)
317 if (rl_editing_mode
== vi_mode
)
318 rl_vi_insertion_mode (1, 0);
322 if (rl_pre_input_hook
)
323 (*rl_pre_input_hook
) ();
326 STATIC_CALLBACK
char *
327 readline_internal_teardown (eof
)
333 /* Restore the original of this history line, iff the line that we
334 are editing was originally in the history, AND the line has changed. */
335 entry
= current_history ();
337 if (entry
&& rl_undo_list
)
339 temp
= savestring (the_line
);
340 rl_revert_line (1, 0);
341 entry
= replace_history_entry (where_history (), the_line
, (histdata_t
)NULL
);
342 _rl_free_history_entry (entry
);
344 strcpy (the_line
, temp
);
348 /* At any rate, it is highly likely that this line has an undo list. Get
353 return (eof
? (char *)NULL
: savestring (the_line
));
357 #if defined (READLINE_CALLBACKS)
358 readline_internal_char ()
360 readline_internal_charloop ()
363 static int lastc
, eof_found
;
369 #if !defined (READLINE_CALLBACKS)
373 lk
= _rl_last_command_was_kill
;
375 code
= setjmp (readline_top_level
);
378 (*rl_redisplay_function
) ();
380 if (rl_pending_input
== 0)
382 /* Then initialize the argument and number of keys read. */
383 _rl_init_argument ();
384 rl_key_sequence_length
= 0;
389 /* EOF typed to a non-blank line is a <NL>. */
390 if (c
== EOF
&& rl_end
)
393 /* The character _rl_eof_char typed to blank line, and not as the
394 previous character is interpreted as EOF. */
395 if (((c
== _rl_eof_char
&& lastc
!= c
) || c
== EOF
) && !rl_end
)
397 #if defined (READLINE_CALLBACKS)
398 return (rl_done
= 1);
406 _rl_dispatch ((unsigned char)c
, _rl_keymap
);
408 /* If there was no change in _rl_last_command_was_kill, then no kill
409 has taken place. Note that if input is pending we are reading
410 a prefix command, so nothing has changed yet. */
411 if (rl_pending_input
== 0 && lk
== _rl_last_command_was_kill
)
412 _rl_last_command_was_kill
= 0;
414 #if defined (VI_MODE)
415 /* In vi mode, when you exit insert mode, the cursor moves back
416 over the previous character. We explicitly check for that here. */
417 if (rl_editing_mode
== vi_mode
&& _rl_keymap
== vi_movement_keymap
)
421 if (rl_num_chars_to_read
&& rl_end
>= rl_num_chars_to_read
)
423 (*rl_redisplay_function
) ();
424 rl_newline (1, '\n');
428 (*rl_redisplay_function
) ();
430 /* If the application writer has told us to erase the entire line if
431 the only character typed was something bound to rl_newline, do so. */
432 if (rl_erase_empty_line
&& rl_done
&& rl_last_func
== rl_newline
&&
433 rl_point
== 0 && rl_end
== 0)
434 _rl_erase_entire_line ();
436 #if defined (READLINE_CALLBACKS)
445 #if defined (READLINE_CALLBACKS)
447 readline_internal_charloop ()
452 eof
= readline_internal_char ();
455 #endif /* READLINE_CALLBACKS */
457 /* Read a line of input from the global rl_instream, doing output on
458 the global rl_outstream.
459 If rl_prompt is non-null, then that is our prompt. */
465 readline_internal_setup ();
466 eof
= readline_internal_charloop ();
467 return (readline_internal_teardown (eof
));
471 _rl_init_line_state ()
473 rl_point
= rl_end
= 0;
474 the_line
= rl_line_buffer
;
481 the_line
= rl_line_buffer
;
484 /* Do the command associated with KEY in MAP.
485 If the associated command is really a keymap, then read
486 another key, and dispatch into that map. */
488 _rl_dispatch (key
, map
)
496 if (META_CHAR (key
) && _rl_convert_meta_chars_to_ascii
)
498 if (map
[ESC
].type
== ISKMAP
)
500 if (_rl_defining_kbd_macro
)
501 _rl_add_macro_char (ESC
);
502 map
= FUNCTION_TO_KEYMAP (map
, ESC
);
504 rl_key_sequence_length
+= 2;
505 return (_rl_dispatch (key
, map
));
512 if (_rl_defining_kbd_macro
)
513 _rl_add_macro_char (key
);
516 switch (map
[key
].type
)
519 func
= map
[key
].function
;
520 if (func
!= (Function
*)NULL
)
522 /* Special case rl_do_lowercase_version (). */
523 if (func
== rl_do_lowercase_version
)
524 return (_rl_dispatch (_rl_to_lower (key
), map
));
526 rl_executing_keymap
= map
;
529 _rl_suppress_redisplay
= (map
[key
].function
== rl_insert
) && _rl_input_available ();
533 r
= (*map
[key
].function
)(rl_numeric_arg
* rl_arg_sign
, key
);
536 /* If we have input pending, then the last command was a prefix
537 command. Don't change the state of rl_last_func. Otherwise,
538 remember the last command executed in this variable. */
539 if (!rl_pending_input
&& map
[key
].function
!= rl_digit_argument
)
540 rl_last_func
= map
[key
].function
;
544 _rl_abort_internal ();
550 if (map
[key
].function
!= (Function
*)NULL
)
552 rl_key_sequence_length
++;
553 newkey
= rl_read_key ();
554 r
= _rl_dispatch (newkey
, FUNCTION_TO_KEYMAP (map
, key
));
558 _rl_abort_internal ();
564 if (map
[key
].function
!= (Function
*)NULL
)
566 macro
= savestring ((char *)map
[key
].function
);
567 _rl_with_macro_input (macro
);
572 #if defined (VI_MODE)
573 if (rl_editing_mode
== vi_mode
&& _rl_keymap
== vi_movement_keymap
&&
574 _rl_vi_textmod_command (key
))
575 _rl_vi_set_last (key
, rl_numeric_arg
, rl_arg_sign
);
580 /* **************************************************************** */
582 /* Initializations */
584 /* **************************************************************** */
586 /* Initialize readline (and terminal if not already). */
590 /* If we have never been called before, initialize the
591 terminal and data structures. */
594 readline_initialize_everything ();
598 /* Initalize the current line information. */
599 _rl_init_line_state ();
601 /* We aren't done yet. We haven't even gotten started yet! */
604 /* Tell the history routines what is going on. */
605 start_using_history ();
607 /* Make the display buffer match the state of the line. */
608 rl_reset_line_state ();
610 /* No such function typed yet. */
611 rl_last_func
= (Function
*)NULL
;
613 /* Parsing of key-bindings begins in an enabled state. */
614 _rl_parsing_conditionalized_out
= 0;
616 #if defined (VI_MODE)
617 if (rl_editing_mode
== vi_mode
)
618 _rl_vi_initialize_line ();
625 #if defined (__EMX__)
627 _emx_build_environ ()
634 DosGetInfoBlocks (&tibp
, &pibp
);
635 t
= pibp
->pib_pchenv
;
638 tp
= environ
= (char **)xmalloc ((c
+ 1) * sizeof (char *));
639 t
= pibp
->pib_pchenv
;
650 /* Initialize the entire state of the world. */
652 readline_initialize_everything ()
655 #if defined (__EMX__)
657 _emx_build_environ ();
661 /* Find out if we are running in Emacs. */
662 running_in_emacs
= get_env_value ("EMACS") != (char *)0;
664 /* Set up input and output if they are not already set up. */
669 rl_outstream
= stdout
;
671 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
672 may change, but they may also be used before readline_internal ()
674 _rl_in_stream
= rl_instream
;
675 _rl_out_stream
= rl_outstream
;
677 /* Allocate data structures. */
678 if (rl_line_buffer
== 0)
679 rl_line_buffer
= xmalloc (rl_line_buffer_len
= DEFAULT_BUFFER_SIZE
);
681 /* Initialize the terminal interface. */
682 _rl_init_terminal_io ((char *)NULL
);
684 /* Bind tty characters to readline functions. */
685 readline_default_bindings ();
687 /* Initialize the function names. */
688 rl_initialize_funmap ();
690 /* Decide whether we should automatically go into eight-bit mode. */
691 _rl_init_eightbit ();
693 /* Read in the init file. */
694 rl_read_init_file ((char *)NULL
);
697 if (_rl_horizontal_scroll_mode
&& _rl_term_autowrap
)
700 screenchars
-= screenheight
;
703 /* Override the effect of any `set keymap' assignments in the
705 rl_set_keymap_from_edit_mode ();
707 /* Try to bind a common arrow key prefix, if not already bound. */
710 /* Enable the meta key, if this terminal has one. */
712 _rl_enable_meta_key ();
714 /* If the completion parser's default word break characters haven't
715 been set yet, then do so now. */
716 if (rl_completer_word_break_characters
== (char *)NULL
)
717 rl_completer_word_break_characters
= rl_basic_word_break_characters
;
720 /* If this system allows us to look at the values of the regular
721 input editing characters, then bind them to their readline
722 equivalents, iff the characters are not bound to keymaps. */
724 readline_default_bindings ()
726 rltty_set_default_bindings (_rl_keymap
);
730 bind_arrow_keys_internal ()
734 #if defined (__MSDOS__)
735 f
= rl_function_of_keyseq ("\033[0A", _rl_keymap
, (int *)NULL
);
736 if (!f
|| f
== rl_do_lowercase_version
)
738 _rl_bind_if_unbound ("\033[0A", rl_get_previous_history
);
739 _rl_bind_if_unbound ("\033[0B", rl_backward
);
740 _rl_bind_if_unbound ("\033[0C", rl_forward
);
741 _rl_bind_if_unbound ("\033[0D", rl_get_next_history
);
745 f
= rl_function_of_keyseq ("\033[A", _rl_keymap
, (int *)NULL
);
746 if (!f
|| f
== rl_do_lowercase_version
)
748 _rl_bind_if_unbound ("\033[A", rl_get_previous_history
);
749 _rl_bind_if_unbound ("\033[B", rl_get_next_history
);
750 _rl_bind_if_unbound ("\033[C", rl_forward
);
751 _rl_bind_if_unbound ("\033[D", rl_backward
);
754 f
= rl_function_of_keyseq ("\033OA", _rl_keymap
, (int *)NULL
);
755 if (!f
|| f
== rl_do_lowercase_version
)
757 _rl_bind_if_unbound ("\033OA", rl_get_previous_history
);
758 _rl_bind_if_unbound ("\033OB", rl_get_next_history
);
759 _rl_bind_if_unbound ("\033OC", rl_forward
);
760 _rl_bind_if_unbound ("\033OD", rl_backward
);
764 /* Try and bind the common arrow key prefix after giving termcap and
765 the inputrc file a chance to bind them and create `real' keymaps
766 for the arrow key prefix. */
772 xkeymap
= _rl_keymap
;
774 _rl_keymap
= emacs_standard_keymap
;
775 bind_arrow_keys_internal ();
777 #if defined (VI_MODE)
778 _rl_keymap
= vi_movement_keymap
;
779 bind_arrow_keys_internal ();
782 _rl_keymap
= xkeymap
;
786 /* **************************************************************** */
788 /* Numeric Arguments */
790 /* **************************************************************** */
792 /* Handle C-u style numeric args, as well as M--, and M-digits. */
796 int key
, c
, sawminus
, sawdigits
;
800 sawminus
= sawdigits
= 0;
803 if (rl_numeric_arg
> 1000000)
805 sawdigits
= rl_explicit_arg
= rl_numeric_arg
= 0;
807 rl_restore_prompt ();
811 rl_message ("(arg: %d) ", rl_arg_sign
* rl_numeric_arg
);
812 key
= c
= rl_read_key ();
814 /* If we see a key bound to `universal-argument' after seeing digits,
815 it ends the argument but is otherwise ignored. */
816 if (_rl_keymap
[c
].type
== ISFUNC
&&
817 _rl_keymap
[c
].function
== rl_universal_argument
)
826 key
= rl_read_key ();
827 rl_restore_prompt ();
829 return (_rl_dispatch (key
, _rl_keymap
));
837 rl_numeric_arg
= rl_explicit_arg
? (rl_numeric_arg
* 10) + c
- '0' : c
- '0';
838 sawdigits
= rl_explicit_arg
= 1;
840 else if (c
== '-' && rl_explicit_arg
== 0)
842 rl_numeric_arg
= sawminus
= 1;
847 /* Make M-- command equivalent to M--1 command. */
848 if (sawminus
&& rl_numeric_arg
== 1 && rl_explicit_arg
== 0)
850 rl_restore_prompt ();
852 return (_rl_dispatch (key
, _rl_keymap
));
859 /* Add the current digit to the argument in progress. */
861 rl_digit_argument (ignore
, key
)
864 rl_pending_input
= key
;
865 return (rl_digit_loop ());
868 /* What to do when you abort reading an argument. */
870 rl_discard_argument ()
874 _rl_init_argument ();
878 /* Create a default argument. */
882 rl_numeric_arg
= rl_arg_sign
= 1;
887 /* C-u, universal argument. Multiply the current argument by 4.
888 Read a key. If the key has nothing to do with arguments, then
889 dispatch on it. If the key is the abort character then abort. */
891 rl_universal_argument (count
, key
)
895 return (rl_digit_loop ());
898 /* **************************************************************** */
900 /* Insert and Delete */
902 /* **************************************************************** */
904 /* Insert a string of text into the line at point. This is the only
905 way that you should do insertion. rl_insert () calls this
908 rl_insert_text (string
)
911 register int i
, l
= strlen (string
);
913 if (rl_end
+ l
>= rl_line_buffer_len
)
914 rl_extend_line_buffer (rl_end
+ l
);
916 for (i
= rl_end
; i
>= rl_point
; i
--)
917 the_line
[i
+ l
] = the_line
[i
];
918 strncpy (the_line
+ rl_point
, string
, l
);
920 /* Remember how to undo this if we aren't undoing something. */
921 if (!_rl_doing_an_undo
)
923 /* If possible and desirable, concatenate the undos. */
926 (rl_undo_list
->what
== UNDO_INSERT
) &&
927 (rl_undo_list
->end
== rl_point
) &&
928 (rl_undo_list
->end
- rl_undo_list
->start
< 20))
931 rl_add_undo (UNDO_INSERT
, rl_point
, rl_point
+ l
, (char *)NULL
);
935 the_line
[rl_end
] = '\0';
939 /* Delete the string between FROM and TO. FROM is
940 inclusive, TO is not. */
942 rl_delete_text (from
, to
)
946 register int diff
, i
;
948 /* Fix it if the caller is confused. */
960 text
= rl_copy_text (from
, to
);
962 /* Some versions of strncpy() can't handle overlapping arguments. */
964 for (i
= from
; i
< rl_end
- diff
; i
++)
965 the_line
[i
] = the_line
[i
+ diff
];
967 /* Remember how to undo this delete. */
968 if (_rl_doing_an_undo
== 0)
969 rl_add_undo (UNDO_DELETE
, from
, to
, text
);
974 the_line
[rl_end
] = '\0';
978 /* Fix up point so that it is within the line boundaries after killing
979 text. If FIX_MARK_TOO is non-zero, the mark is forced within line
982 #define _RL_FIX_POINT(x) \
991 _rl_fix_point (fix_mark_too
)
994 _RL_FIX_POINT (rl_point
);
996 _RL_FIX_POINT (rl_mark
);
1001 _rl_replace_text (text
, start
, end
)
1005 rl_begin_undo_group ();
1006 rl_delete_text (start
, end
+ 1);
1008 rl_insert_text (text
);
1009 rl_end_undo_group ();
1012 /* **************************************************************** */
1014 /* Readline character functions */
1016 /* **************************************************************** */
1018 /* This is not a gap editor, just a stupid line input routine. No hair
1019 is involved in writing any of the functions, and none should be. */
1023 rl_end is the place in the string that we would place '\0';
1024 i.e., it is always safe to place '\0' there.
1026 rl_point is the place in the string where the cursor is. Sometimes
1027 this is the same as rl_end.
1029 Any command that is called interactively receives two arguments.
1030 The first is a count: the numeric arg pased to this command.
1031 The second is the key which invoked this command.
1034 /* **************************************************************** */
1036 /* Movement Commands */
1038 /* **************************************************************** */
1040 /* Note that if you `optimize' the display for these functions, you cannot
1041 use said functions in other functions which do not do optimizing display.
1042 I.e., you will have to update the data base for rl_redisplay, and you
1043 might as well let rl_redisplay do that job. */
1045 /* Move forward COUNT characters. */
1047 rl_forward (count
, key
)
1051 rl_backward (-count
, key
);
1054 int end
= rl_point
+ count
;
1055 #if defined (VI_MODE)
1056 int lend
= rl_end
- (rl_editing_mode
== vi_mode
);
1076 /* Move backward COUNT characters. */
1078 rl_backward (count
, key
)
1082 rl_forward (-count
, key
);
1085 if (rl_point
< count
)
1096 /* Move to the beginning of the line. */
1098 rl_beg_of_line (count
, key
)
1105 /* Move to the end of the line. */
1107 rl_end_of_line (count
, key
)
1114 /* Move forward a word. We do what Emacs does. */
1116 rl_forward_word (count
, key
)
1123 rl_backward_word (-count
, key
);
1129 if (rl_point
== rl_end
)
1132 /* If we are not in a word, move forward until we are in one.
1133 Then, move forward until we hit a non-alphabetic character. */
1134 c
= the_line
[rl_point
];
1135 if (alphabetic (c
) == 0)
1137 while (++rl_point
< rl_end
)
1139 c
= the_line
[rl_point
];
1144 if (rl_point
== rl_end
)
1146 while (++rl_point
< rl_end
)
1148 c
= the_line
[rl_point
];
1149 if (alphabetic (c
) == 0)
1157 /* Move backward a word. We do what Emacs does. */
1159 rl_backward_word (count
, key
)
1166 rl_forward_word (-count
, key
);
1175 /* Like rl_forward_word (), except that we look at the characters
1176 just before point. */
1178 c
= the_line
[rl_point
- 1];
1179 if (alphabetic (c
) == 0)
1183 c
= the_line
[rl_point
- 1];
1191 c
= the_line
[rl_point
- 1];
1192 if (alphabetic (c
) == 0)
1202 /* Clear the current line. Numeric argument to C-l does this. */
1204 rl_refresh_line (ignore1
, ignore2
)
1205 int ignore1
, ignore2
;
1209 curr_line
= _rl_current_display_line ();
1211 _rl_move_vert (curr_line
);
1212 _rl_move_cursor_relative (0, the_line
); /* XXX is this right */
1214 _rl_clear_to_eol (0); /* arg of 0 means to not use spaces */
1216 rl_forced_update_display ();
1217 rl_display_fixed
= 1;
1222 /* C-l typed to a line without quoting clears the screen, and then reprints
1223 the prompt and the current input line. Given a numeric arg, redraw only
1224 the current line. */
1226 rl_clear_screen (count
, key
)
1229 if (rl_explicit_arg
)
1231 rl_refresh_line (count
, key
);
1235 _rl_clear_screen (); /* calls termcap function to clear screen */
1236 rl_forced_update_display ();
1237 rl_display_fixed
= 1;
1243 rl_arrow_keys (count
, c
)
1248 ch
= rl_read_key ();
1250 switch (_rl_to_upper (ch
))
1253 rl_get_previous_history (count
, ch
);
1257 rl_get_next_history (count
, ch
);
1261 rl_forward (count
, ch
);
1265 rl_backward (count
, ch
);
1275 /* **************************************************************** */
1279 /* **************************************************************** */
1281 /* Insert the character C at the current location, moving point forward. */
1283 rl_insert (count
, c
)
1292 /* If we can optimize, then do it. But don't let people crash
1293 readline because of extra large arguments. */
1294 if (count
> 1 && count
<= 1024)
1296 string
= xmalloc (1 + count
);
1298 for (i
= 0; i
< count
; i
++)
1302 rl_insert_text (string
);
1313 for (i
= 0; i
< 1024; i
++)
1318 decreaser
= (count
> 1024 ? 1024 : count
);
1319 str
[decreaser
] = '\0';
1320 rl_insert_text (str
);
1327 /* We are inserting a single character.
1328 If there is pending input, then make a string of all of the
1329 pending characters that are bound to rl_insert, and insert
1331 if (_rl_any_typein ())
1332 _rl_insert_typein (c
);
1335 /* Inserting a single character. */
1340 rl_insert_text (str
);
1345 /* Insert the next typed character verbatim. */
1347 rl_quoted_insert (count
, key
)
1352 #if defined (HANDLE_SIGNALS)
1353 _rl_disable_tty_signals ();
1356 #if defined (HANDLE_SIGNALS)
1357 _rl_restore_tty_signals ();
1360 return (rl_insert (count
, c
));
1363 /* Insert a tab character. */
1365 rl_tab_insert (count
, key
)
1368 return (rl_insert (count
, '\t'));
1371 /* What to do when a NEWLINE is pressed. We accept the whole line.
1372 KEY is the key that invoked this command. I guess it could have
1373 meaning in the future. */
1375 rl_newline (count
, key
)
1380 #if defined (VI_MODE)
1381 if (rl_editing_mode
== vi_mode
)
1383 _rl_vi_done_inserting ();
1384 _rl_vi_reset_last ();
1386 #endif /* VI_MODE */
1388 /* If we've been asked to erase empty lines, suppress the final update,
1389 since _rl_update_final calls crlf(). */
1390 if (rl_erase_empty_line
&& rl_point
== 0 && rl_end
== 0)
1393 if (readline_echoing_p
)
1394 _rl_update_final ();
1398 /* What to do for some uppercase characters, like meta characters,
1399 and some characters appearing in emacs_ctlx_keymap. This function
1400 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1401 is special cased. */
1403 rl_do_lowercase_version (ignore1
, ignore2
)
1404 int ignore1
, ignore2
;
1409 /* Rubout the character behind point. */
1411 rl_rubout (count
, key
)
1416 rl_delete (-count
, key
);
1426 if (count
> 1 || rl_explicit_arg
)
1428 int orig_point
= rl_point
;
1429 rl_backward (count
, key
);
1430 rl_kill_text (orig_point
, rl_point
);
1434 int c
= the_line
[--rl_point
];
1435 rl_delete_text (rl_point
, rl_point
+ 1);
1437 if (rl_point
== rl_end
&& isprint (c
) && _rl_last_c_pos
)
1440 l
= rl_character_len (c
, rl_point
);
1441 _rl_erase_at_end_of_line (l
);
1447 /* Delete the character under the cursor. Given a numeric argument,
1448 kill that many characters instead. */
1450 rl_delete (count
, key
)
1454 return (rl_rubout (-count
, key
));
1456 if (rl_point
== rl_end
)
1462 if (count
> 1 || rl_explicit_arg
)
1464 int orig_point
= rl_point
;
1465 rl_forward (count
, key
);
1466 rl_kill_text (orig_point
, rl_point
);
1467 rl_point
= orig_point
;
1471 return (rl_delete_text (rl_point
, rl_point
+ 1));
1474 /* Delete the character under the cursor, unless the insertion
1475 point is at the end of the line, in which case the character
1476 behind the cursor is deleted. COUNT is obeyed and may be used
1477 to delete forward or backward that many characters. */
1479 rl_rubout_or_delete (count
, key
)
1482 if (rl_end
!= 0 && rl_point
== rl_end
)
1483 return (rl_rubout (count
, key
));
1485 return (rl_delete (count
, key
));
1488 /* Delete all spaces and tabs around point. */
1490 rl_delete_horizontal_space (count
, ignore
)
1493 int start
= rl_point
;
1495 while (rl_point
&& whitespace (the_line
[rl_point
- 1]))
1500 while (rl_point
< rl_end
&& whitespace (the_line
[rl_point
]))
1503 if (start
!= rl_point
)
1505 rl_delete_text (start
, rl_point
);
1511 /* Like the tcsh editing function delete-char-or-list. The eof character
1512 is caught before this is invoked, so this really does the same thing as
1513 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1515 rl_delete_or_show_completions (count
, key
)
1518 if (rl_end
!= 0 && rl_point
== rl_end
)
1519 return (rl_possible_completions (count
, key
));
1521 return (rl_delete (count
, key
));
1524 #ifndef RL_COMMENT_BEGIN_DEFAULT
1525 #define RL_COMMENT_BEGIN_DEFAULT "#"
1528 /* Turn the current line into a comment in shell history.
1529 A K*rn shell style function. */
1531 rl_insert_comment (count
, key
)
1534 rl_beg_of_line (1, key
);
1535 rl_insert_text (_rl_comment_begin
? _rl_comment_begin
1536 : RL_COMMENT_BEGIN_DEFAULT
);
1537 (*rl_redisplay_function
) ();
1538 rl_newline (1, '\n');
1542 /* **************************************************************** */
1546 /* **************************************************************** */
1548 /* The three kinds of things that we know how to do. */
1553 /* Uppercase the word at point. */
1555 rl_upcase_word (count
, key
)
1558 return (rl_change_case (count
, UpCase
));
1561 /* Lowercase the word at point. */
1563 rl_downcase_word (count
, key
)
1566 return (rl_change_case (count
, DownCase
));
1569 /* Upcase the first letter, downcase the rest. */
1571 rl_capitalize_word (count
, key
)
1574 return (rl_change_case (count
, CapCase
));
1577 /* The meaty function.
1578 Change the case of COUNT words, performing OP on them.
1579 OP is one of UpCase, DownCase, or CapCase.
1580 If a negative argument is given, leave point where it started,
1581 otherwise, leave it where it moves to. */
1583 rl_change_case (count
, op
)
1586 register int start
, end
;
1590 rl_forward_word (count
, 0);
1596 /* We are going to modify some text, so let's prepare to undo it. */
1597 rl_modifying (start
, end
);
1599 for (inword
= 0; start
< end
; start
++)
1601 c
= the_line
[start
];
1605 the_line
[start
] = _rl_to_upper (c
);
1609 the_line
[start
] = _rl_to_lower (c
);
1613 the_line
[start
] = (inword
== 0) ? _rl_to_upper (c
) : _rl_to_lower (c
);
1614 inword
= alphabetic (the_line
[start
]);
1626 /* **************************************************************** */
1630 /* **************************************************************** */
1632 /* Transpose the words at point. */
1634 rl_transpose_words (count
, key
)
1637 char *word1
, *word2
;
1638 int w1_beg
, w1_end
, w2_beg
, w2_end
;
1639 int orig_point
= rl_point
;
1644 /* Find the two words. */
1645 rl_forward_word (count
, key
);
1647 rl_backward_word (1, key
);
1649 rl_backward_word (count
, key
);
1651 rl_forward_word (1, key
);
1654 /* Do some check to make sure that there really are two words. */
1655 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
1658 rl_point
= orig_point
;
1662 /* Get the text of the words. */
1663 word1
= rl_copy_text (w1_beg
, w1_end
);
1664 word2
= rl_copy_text (w2_beg
, w2_end
);
1666 /* We are about to do many insertions and deletions. Remember them
1667 as one operation. */
1668 rl_begin_undo_group ();
1670 /* Do the stuff at word2 first, so that we don't have to worry
1671 about word1 moving. */
1673 rl_delete_text (w2_beg
, w2_end
);
1674 rl_insert_text (word1
);
1677 rl_delete_text (w1_beg
, w1_end
);
1678 rl_insert_text (word2
);
1680 /* This is exactly correct since the text before this point has not
1681 changed in length. */
1684 /* I think that does it. */
1685 rl_end_undo_group ();
1692 /* Transpose the characters at point. If point is at the end of the line,
1693 then transpose the characters before point. */
1695 rl_transpose_chars (count
, key
)
1703 if (!rl_point
|| rl_end
< 2)
1709 rl_begin_undo_group ();
1711 if (rl_point
== rl_end
)
1718 dummy
[0] = the_line
[rl_point
];
1721 rl_delete_text (rl_point
, rl_point
+ 1);
1725 rl_insert_text (dummy
);
1727 rl_end_undo_group ();
1731 /* **************************************************************** */
1733 /* Character Searching */
1735 /* **************************************************************** */
1738 _rl_char_search_internal (count
, dir
, schar
)
1739 int count
, dir
, schar
;
1744 inc
= (dir
< 0) ? -1 : 1;
1747 if ((dir
< 0 && pos
<= 0) || (dir
> 0 && pos
>= rl_end
))
1756 if (rl_line_buffer
[pos
] == schar
)
1760 rl_point
= (dir
== BTO
) ? pos
+ 1 : pos
;
1762 rl_point
= (dir
== FTO
) ? pos
- 1 : pos
;
1766 while ((dir
< 0) ? pos
-- : ++pos
< rl_end
);
1771 /* Search COUNT times for a character read from the current input stream.
1772 FDIR is the direction to search if COUNT is non-negative; otherwise
1773 the search goes in BDIR. */
1775 _rl_char_search (count
, fdir
, bdir
)
1776 int count
, fdir
, bdir
;
1782 return (_rl_char_search_internal (-count
, bdir
, c
));
1784 return (_rl_char_search_internal (count
, fdir
, c
));
1788 rl_char_search (count
, key
)
1791 return (_rl_char_search (count
, FFIND
, BFIND
));
1795 rl_backward_char_search (count
, key
)
1798 return (_rl_char_search (count
, BFIND
, FFIND
));
1801 /* **************************************************************** */
1803 /* History Utilities */
1805 /* **************************************************************** */
1807 /* We already have a history library, and that is what we use to control
1808 the history features of readline. This is our local interface to
1809 the history mechanism. */
1811 /* While we are editing the history, this is the saved
1812 version of the original line. */
1813 HIST_ENTRY
*saved_line_for_history
= (HIST_ENTRY
*)NULL
;
1815 /* Set the history pointer back to the last entry in the history. */
1817 start_using_history ()
1820 if (saved_line_for_history
)
1821 _rl_free_history_entry (saved_line_for_history
);
1823 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
1826 /* Free the contents (and containing structure) of a HIST_ENTRY. */
1828 _rl_free_history_entry (entry
)
1838 /* Perhaps put back the current line if it has changed. */
1840 maybe_replace_line ()
1844 temp
= current_history ();
1845 /* If the current line has changed, save the changes. */
1846 if (temp
&& ((UNDO_LIST
*)(temp
->data
) != rl_undo_list
))
1848 temp
= replace_history_entry (where_history (), the_line
, (histdata_t
)rl_undo_list
);
1855 /* Put back the saved_line_for_history if there is one. */
1857 maybe_unsave_line ()
1861 if (saved_line_for_history
)
1863 line_len
= strlen (saved_line_for_history
->line
);
1865 if (line_len
>= rl_line_buffer_len
)
1866 rl_extend_line_buffer (line_len
);
1868 strcpy (the_line
, saved_line_for_history
->line
);
1869 rl_undo_list
= (UNDO_LIST
*)saved_line_for_history
->data
;
1870 _rl_free_history_entry (saved_line_for_history
);
1871 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
1872 rl_end
= rl_point
= strlen (the_line
);
1879 /* Save the current line in saved_line_for_history. */
1883 if (saved_line_for_history
== 0)
1885 saved_line_for_history
= (HIST_ENTRY
*)xmalloc (sizeof (HIST_ENTRY
));
1886 saved_line_for_history
->line
= savestring (the_line
);
1887 saved_line_for_history
->data
= (char *)rl_undo_list
;
1892 /* **************************************************************** */
1894 /* History Commands */
1896 /* **************************************************************** */
1898 /* Meta-< goes to the start of the history. */
1900 rl_beginning_of_history (count
, key
)
1903 return (rl_get_previous_history (1 + where_history (), key
));
1906 /* Meta-> goes to the end of the history. (The current line). */
1908 rl_end_of_history (count
, key
)
1911 maybe_replace_line ();
1913 maybe_unsave_line ();
1917 /* Move down to the next history line. */
1919 rl_get_next_history (count
, key
)
1926 return (rl_get_previous_history (-count
, key
));
1931 maybe_replace_line ();
1933 temp
= (HIST_ENTRY
*)NULL
;
1936 temp
= next_history ();
1943 maybe_unsave_line ();
1946 line_len
= strlen (temp
->line
);
1948 if (line_len
>= rl_line_buffer_len
)
1949 rl_extend_line_buffer (line_len
);
1951 strcpy (the_line
, temp
->line
);
1952 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
1953 rl_end
= rl_point
= strlen (the_line
);
1954 #if defined (VI_MODE)
1955 if (rl_editing_mode
== vi_mode
)
1957 #endif /* VI_MODE */
1962 /* Get the previous item out of our interactive history, making it the current
1963 line. If there is no previous history, just ding. */
1965 rl_get_previous_history (count
, key
)
1968 HIST_ENTRY
*old_temp
, *temp
;
1972 return (rl_get_next_history (-count
, key
));
1977 /* If we don't have a line saved, then save this one. */
1980 /* If the current line has changed, save the changes. */
1981 maybe_replace_line ();
1983 temp
= old_temp
= (HIST_ENTRY
*)NULL
;
1986 temp
= previous_history ();
1994 /* If there was a large argument, and we moved back to the start of the
1995 history, that is not an error. So use the last value found. */
1996 if (!temp
&& old_temp
)
2003 line_len
= strlen (temp
->line
);
2005 if (line_len
>= rl_line_buffer_len
)
2006 rl_extend_line_buffer (line_len
);
2008 strcpy (the_line
, temp
->line
);
2009 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
2010 rl_end
= rl_point
= line_len
;
2012 #if defined (VI_MODE)
2013 if (rl_editing_mode
== vi_mode
)
2015 #endif /* VI_MODE */
2020 /* **************************************************************** */
2022 /* The Mark and the Region. */
2024 /* **************************************************************** */
2026 /* Set the mark at POSITION. */
2028 _rl_set_mark_at_pos (position
)
2031 if (position
> rl_end
)
2038 /* A bindable command to set the mark. */
2040 rl_set_mark (count
, key
)
2043 return (_rl_set_mark_at_pos (rl_explicit_arg
? count
: rl_point
));
2046 /* Exchange the position of mark and point. */
2048 rl_exchange_point_and_mark (count
, key
)
2051 if (rl_mark
> rl_end
)
2060 SWAP (rl_point
, rl_mark
);
2065 /* **************************************************************** */
2069 /* **************************************************************** */
2070 /* How to toggle back and forth between editing modes. */
2072 rl_vi_editing_mode (count
, key
)
2075 #if defined (VI_MODE)
2076 rl_editing_mode
= vi_mode
;
2077 rl_vi_insertion_mode (1, key
);
2078 #endif /* VI_MODE */
2083 rl_emacs_editing_mode (count
, key
)
2086 rl_editing_mode
= emacs_mode
;
2087 _rl_keymap
= emacs_standard_keymap
;