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 "2.2-bash"
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 ();
86 extern void _rl_get_screen_size ();
88 extern int _rl_enable_meta
;
89 extern int _rl_term_autowrap
;
90 extern int screenwidth
, screenheight
, screenchars
;
92 /* Variables and functions imported from rltty.c. */
93 extern void rl_prep_terminal (), rl_deprep_terminal ();
94 extern void rltty_set_default_bindings ();
96 /* Functions imported from util.c. */
97 extern void _rl_abort_internal ();
98 extern void rl_extend_line_buffer ();
99 extern int alphabetic ();
101 /* Functions imported from bind.c. */
102 extern void _rl_bind_if_unbound ();
103 extern int rl_set_keymap_from_edit_mode ();
105 /* Functions imported from input.c. */
106 extern int _rl_any_typein ();
107 extern void _rl_insert_typein ();
108 extern int rl_read_key ();
110 /* Functions imported from nls.c */
111 extern int _rl_init_eightbit ();
113 /* Functions imported from shell.c */
114 extern char *get_env_value ();
116 /* External redisplay functions and variables from display.c */
117 extern void _rl_move_vert ();
118 extern void _rl_update_final ();
119 extern void _rl_clear_to_eol ();
120 extern void _rl_clear_screen ();
122 extern void _rl_save_prompt ();
123 extern void _rl_restore_prompt ();
125 extern void _rl_erase_at_end_of_line ();
126 extern void _rl_move_cursor_relative ();
128 extern int _rl_vis_botlin
;
129 extern int _rl_last_c_pos
;
130 extern int _rl_horizontal_scroll_mode
;
131 extern int rl_display_fixed
;
132 extern int _rl_suppress_redisplay
;
133 extern char *rl_display_prompt
;
135 /* Variables imported from complete.c. */
136 extern char *rl_completer_word_break_characters
;
137 extern char *rl_basic_word_break_characters
;
138 extern int rl_completion_query_items
;
139 extern int rl_complete_with_tilde_expansion
;
141 /* Variables and functions from macro.c. */
142 extern void _rl_add_macro_char ();
143 extern void _rl_with_macro_input ();
144 extern int _rl_next_macro_key ();
145 extern int _rl_defining_kbd_macro
;
147 #if defined (VI_MODE)
148 /* Functions imported from vi_mode.c. */
149 extern void _rl_vi_set_last ();
150 extern void _rl_vi_reset_last ();
151 extern void _rl_vi_done_inserting ();
152 extern int _rl_vi_textmod_command ();
153 extern void _rl_vi_initialize_line ();
156 extern UNDO_LIST
*rl_undo_list
;
157 extern int _rl_doing_an_undo
;
159 /* Forward declarations used in this file. */
160 void _rl_free_history_entry ();
163 int _rl_init_argument ();
165 static char *readline_internal ();
166 static void readline_initialize_everything ();
167 static void start_using_history ();
168 static void bind_arrow_keys ();
170 #if !defined (__GO32__)
171 static void readline_default_bindings ();
172 #endif /* !__GO32__ */
174 #if defined (__GO32__)
177 # undef HANDLE_SIGNALS
178 #endif /* __GO32__ */
180 extern char *xmalloc (), *xrealloc ();
182 /* **************************************************************** */
184 /* Line editing input utility */
186 /* **************************************************************** */
188 char *rl_library_version
= RL_LIBRARY_VERSION
;
190 /* A pointer to the keymap that is currently in use.
191 By default, it is the standard emacs keymap. */
192 Keymap _rl_keymap
= emacs_standard_keymap
;
194 /* The current style of editing. */
195 int rl_editing_mode
= emacs_mode
;
197 /* Non-zero if we called this function from _rl_dispatch(). It's present
198 so functions can find out whether they were called from a key binding
199 or directly from an application. */
202 /* Non-zero if the previous command was a kill command. */
203 int _rl_last_command_was_kill
= 0;
205 /* The current value of the numeric argument specified by the user. */
206 int rl_numeric_arg
= 1;
208 /* Non-zero if an argument was typed. */
209 int rl_explicit_arg
= 0;
211 /* Temporary value used while generating the argument. */
214 /* Non-zero means we have been called at least once before. */
215 static int rl_initialized
;
217 /* If non-zero, this program is running in an EMACS buffer. */
218 static int running_in_emacs
;
220 /* The current offset in the current input line. */
223 /* Mark in the current input line. */
226 /* Length of the current input line. */
229 /* Make this non-zero to return the current input_line. */
232 /* The last function executed by readline. */
233 Function
*rl_last_func
= (Function
*)NULL
;
235 /* Top level environment for readline_internal (). */
236 procenv_t readline_top_level
;
238 /* The streams we interact with. */
239 FILE *_rl_in_stream
, *_rl_out_stream
;
241 /* The names of the streams that we do input and output to. */
242 FILE *rl_instream
= (FILE *)NULL
;
243 FILE *rl_outstream
= (FILE *)NULL
;
245 /* Non-zero means echo characters as they are read. */
246 int readline_echoing_p
= 1;
248 /* Current prompt. */
250 int rl_visible_prompt_length
= 0;
252 /* The number of characters read in order to type this complete command. */
253 int rl_key_sequence_length
= 0;
255 /* If non-zero, then this is the address of a function to call just
256 before readline_internal () prints the first prompt. */
257 Function
*rl_startup_hook
= (Function
*)NULL
;
259 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
260 static char *the_line
;
262 /* The character that can generate an EOF. Really read from
263 the terminal driver... just defaulted here. */
264 int _rl_eof_char
= CTRL ('D');
266 /* Non-zero makes this the next keystroke to read. */
267 int rl_pending_input
= 0;
269 /* Pointer to a useful terminal name. */
270 char *rl_terminal_name
= (char *)NULL
;
272 /* Non-zero means to always use horizontal scrolling in line display. */
273 int _rl_horizontal_scroll_mode
= 0;
275 /* Non-zero means to display an asterisk at the starts of history lines
276 which have been modified. */
277 int _rl_mark_modified_lines
= 0;
279 /* The style of `bell' notification preferred. This can be set to NO_BELL,
280 AUDIBLE_BELL, or VISIBLE_BELL. */
281 int _rl_bell_preference
= AUDIBLE_BELL
;
283 /* String inserted into the line by rl_insert_comment (). */
284 char *_rl_comment_begin
;
286 /* Keymap holding the function currently being executed. */
287 Keymap rl_executing_keymap
;
289 /* Line buffer and maintenence. */
290 char *rl_line_buffer
= (char *)NULL
;
291 int rl_line_buffer_len
= 0;
293 /* Forward declarations used by the display and termcap code. */
295 /* **************************************************************** */
297 /* `Forward' declarations */
299 /* **************************************************************** */
301 /* Non-zero means do not parse any lines other than comments and
302 parser directives. */
303 unsigned char _rl_parsing_conditionalized_out
= 0;
305 /* Non-zero means to convert characters with the meta bit set to
306 escape-prefixed characters so we can indirect through
307 emacs_meta_keymap or vi_escape_keymap. */
308 int _rl_convert_meta_chars_to_ascii
= 1;
310 /* Non-zero means to output characters with the meta bit set directly
311 rather than as a meta-prefixed escape sequence. */
312 int _rl_output_meta_chars
= 0;
314 /* **************************************************************** */
316 /* Top Level Functions */
318 /* **************************************************************** */
320 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
321 int _rl_meta_flag
= 0; /* Forward declaration */
323 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
324 none. A return value of NULL means that EOF was encountered. */
333 /* If we are at EOF return a NULL string. */
334 if (rl_pending_input
== EOF
)
336 rl_pending_input
= 0;
337 return ((char *)NULL
);
340 rl_visible_prompt_length
= rl_expand_prompt (rl_prompt
);
343 (*rl_prep_term_function
) (_rl_meta_flag
);
345 #if defined (HANDLE_SIGNALS)
349 value
= readline_internal ();
350 (*rl_deprep_term_function
) ();
352 #if defined (HANDLE_SIGNALS)
359 #if defined (READLINE_CALLBACKS)
360 # define STATIC_CALLBACK
362 # define STATIC_CALLBACK static
366 readline_internal_setup ()
368 _rl_in_stream
= rl_instream
;
369 _rl_out_stream
= rl_outstream
;
372 (*rl_startup_hook
) ();
374 if (readline_echoing_p
== 0)
378 fprintf (_rl_out_stream
, "%s", rl_prompt
);
379 fflush (_rl_out_stream
);
385 (*rl_redisplay_function
) ();
386 #if defined (VI_MODE)
387 if (rl_editing_mode
== vi_mode
)
388 rl_vi_insertion_mode (1, 0);
393 STATIC_CALLBACK
char *
394 readline_internal_teardown (eof
)
400 /* Restore the original of this history line, iff the line that we
401 are editing was originally in the history, AND the line has changed. */
402 entry
= current_history ();
404 if (entry
&& rl_undo_list
)
406 temp
= savestring (the_line
);
407 rl_revert_line (1, 0);
408 entry
= replace_history_entry (where_history (), the_line
, (HIST_ENTRY
*)NULL
);
409 _rl_free_history_entry (entry
);
411 strcpy (the_line
, temp
);
415 /* At any rate, it is highly likely that this line has an undo list. Get
420 return (eof
? (char *)NULL
: savestring (the_line
));
424 #if defined (READLINE_CALLBACKS)
425 readline_internal_char ()
427 readline_internal_charloop ()
430 static int lastc
, eof_found
;
436 #if !defined (READLINE_CALLBACKS)
440 lk
= _rl_last_command_was_kill
;
442 code
= setjmp (readline_top_level
);
445 (*rl_redisplay_function
) ();
447 if (rl_pending_input
== 0)
449 /* Then initialize the argument and number of keys read. */
450 _rl_init_argument ();
451 rl_key_sequence_length
= 0;
456 /* EOF typed to a non-blank line is a <NL>. */
457 if (c
== EOF
&& rl_end
)
460 /* The character _rl_eof_char typed to blank line, and not as the
461 previous character is interpreted as EOF. */
462 if (((c
== _rl_eof_char
&& lastc
!= c
) || c
== EOF
) && !rl_end
)
464 #if defined (READLINE_CALLBACKS)
465 return (rl_done
= 1);
473 _rl_dispatch (c
, _rl_keymap
);
475 /* If there was no change in _rl_last_command_was_kill, then no kill
476 has taken place. Note that if input is pending we are reading
477 a prefix command, so nothing has changed yet. */
478 if (rl_pending_input
== 0 && lk
== _rl_last_command_was_kill
)
479 _rl_last_command_was_kill
= 0;
481 #if defined (VI_MODE)
482 /* In vi mode, when you exit insert mode, the cursor moves back
483 over the previous character. We explicitly check for that here. */
484 if (rl_editing_mode
== vi_mode
&& _rl_keymap
== vi_movement_keymap
)
489 (*rl_redisplay_function
) ();
491 #if defined (READLINE_CALLBACKS)
500 #if defined (READLINE_CALLBACKS)
502 readline_internal_charloop ()
507 eof
= readline_internal_char ();
510 #endif /* READLINE_CALLBACKS */
512 /* Read a line of input from the global rl_instream, doing output on
513 the global rl_outstream.
514 If rl_prompt is non-null, then that is our prompt. */
520 readline_internal_setup ();
521 eof
= readline_internal_charloop ();
522 return (readline_internal_teardown (eof
));
526 _rl_init_line_state ()
528 rl_point
= rl_end
= 0;
529 the_line
= rl_line_buffer
;
536 the_line
= rl_line_buffer
;
539 /* Do the command associated with KEY in MAP.
540 If the associated command is really a keymap, then read
541 another key, and dispatch into that map. */
543 _rl_dispatch (key
, map
)
551 if (META_CHAR (key
) && _rl_convert_meta_chars_to_ascii
)
553 if (map
[ESC
].type
== ISKMAP
)
555 if (_rl_defining_kbd_macro
)
556 _rl_add_macro_char (ESC
);
557 map
= FUNCTION_TO_KEYMAP (map
, ESC
);
559 rl_key_sequence_length
+= 2;
560 return (_rl_dispatch (key
, map
));
567 if (_rl_defining_kbd_macro
)
568 _rl_add_macro_char (key
);
571 switch (map
[key
].type
)
574 func
= map
[key
].function
;
575 if (func
!= (Function
*)NULL
)
577 /* Special case rl_do_lowercase_version (). */
578 if (func
== rl_do_lowercase_version
)
579 return (_rl_dispatch (_rl_to_lower (key
), map
));
581 rl_executing_keymap
= map
;
584 _rl_suppress_redisplay
= (map
[key
].function
== rl_insert
) && _rl_input_available ();
588 r
= (*map
[key
].function
)(rl_numeric_arg
* rl_arg_sign
, key
);
591 /* If we have input pending, then the last command was a prefix
592 command. Don't change the state of rl_last_func. Otherwise,
593 remember the last command executed in this variable. */
594 if (!rl_pending_input
&& map
[key
].function
!= rl_digit_argument
)
595 rl_last_func
= map
[key
].function
;
599 _rl_abort_internal ();
605 if (map
[key
].function
!= (Function
*)NULL
)
607 rl_key_sequence_length
++;
608 newkey
= rl_read_key ();
609 r
= _rl_dispatch (newkey
, FUNCTION_TO_KEYMAP (map
, key
));
613 _rl_abort_internal ();
619 if (map
[key
].function
!= (Function
*)NULL
)
621 macro
= savestring ((char *)map
[key
].function
);
622 _rl_with_macro_input (macro
);
627 #if defined (VI_MODE)
628 if (rl_editing_mode
== vi_mode
&& _rl_keymap
== vi_movement_keymap
&&
629 _rl_vi_textmod_command (key
))
630 _rl_vi_set_last (key
, rl_numeric_arg
, rl_arg_sign
);
635 /* **************************************************************** */
637 /* Initializations */
639 /* **************************************************************** */
641 /* Initialize readline (and terminal if not already). */
645 /* If we have never been called before, initialize the
646 terminal and data structures. */
649 readline_initialize_everything ();
653 /* Initalize the current line information. */
654 _rl_init_line_state ();
656 /* We aren't done yet. We haven't even gotten started yet! */
659 /* Tell the history routines what is going on. */
660 start_using_history ();
662 /* Make the display buffer match the state of the line. */
663 rl_reset_line_state ();
665 /* No such function typed yet. */
666 rl_last_func
= (Function
*)NULL
;
668 /* Parsing of key-bindings begins in an enabled state. */
669 _rl_parsing_conditionalized_out
= 0;
671 #if defined (VI_MODE)
672 if (rl_editing_mode
== vi_mode
)
673 _rl_vi_initialize_line ();
679 #if defined (__EMX__)
681 _emx_build_environ ()
688 DosGetInfoBlocks (&tibp
, &pibp
);
689 t
= pibp
->pib_pchenv
;
692 tp
= environ
= (char **)xmalloc ((c
+ 1) * sizeof (char *));
693 t
= pibp
->pib_pchenv
;
703 /* Initialize the entire state of the world. */
705 readline_initialize_everything ()
707 #if defined (__EMX__)
709 _emx_build_environ ();
712 /* Find out if we are running in Emacs. */
713 running_in_emacs
= get_env_value ("EMACS") != (char *)0;
715 /* Set up input and output if they are not already set up. */
720 rl_outstream
= stdout
;
722 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
723 may change, but they may also be used before readline_internal ()
725 _rl_in_stream
= rl_instream
;
726 _rl_out_stream
= rl_outstream
;
728 /* Allocate data structures. */
729 if (rl_line_buffer
== 0)
730 rl_line_buffer
= xmalloc (rl_line_buffer_len
= DEFAULT_BUFFER_SIZE
);
732 /* Initialize the terminal interface. */
733 _rl_init_terminal_io ((char *)NULL
);
735 #if !defined (__GO32__)
736 /* Bind tty characters to readline functions. */
737 readline_default_bindings ();
738 #endif /* !__GO32__ */
740 /* Initialize the function names. */
741 rl_initialize_funmap ();
743 /* Decide whether we should automatically go into eight-bit mode. */
744 _rl_init_eightbit ();
746 /* Read in the init file. */
747 rl_read_init_file ((char *)NULL
);
750 if (_rl_horizontal_scroll_mode
&& _rl_term_autowrap
)
753 screenchars
-= screenheight
;
756 /* Override the effect of any `set keymap' assignments in the
758 rl_set_keymap_from_edit_mode ();
760 /* Try to bind a common arrow key prefix, if not already bound. */
763 /* Enable the meta key, if this terminal has one. */
765 _rl_enable_meta_key ();
767 /* If the completion parser's default word break characters haven't
768 been set yet, then do so now. */
769 if (rl_completer_word_break_characters
== (char *)NULL
)
770 rl_completer_word_break_characters
= rl_basic_word_break_characters
;
773 /* If this system allows us to look at the values of the regular
774 input editing characters, then bind them to their readline
775 equivalents, iff the characters are not bound to keymaps. */
777 readline_default_bindings ()
779 rltty_set_default_bindings (_rl_keymap
);
783 bind_arrow_keys_internal ()
787 f
= rl_function_of_keyseq ("\033[A", _rl_keymap
, (int *)NULL
);
788 if (!f
|| f
== rl_do_lowercase_version
)
790 _rl_bind_if_unbound ("\033[A", rl_get_previous_history
);
791 _rl_bind_if_unbound ("\033[B", rl_get_next_history
);
792 _rl_bind_if_unbound ("\033[C", rl_forward
);
793 _rl_bind_if_unbound ("\033[D", rl_backward
);
796 f
= rl_function_of_keyseq ("\033OA", _rl_keymap
, (int *)NULL
);
797 if (!f
|| f
== rl_do_lowercase_version
)
799 _rl_bind_if_unbound ("\033OA", rl_get_previous_history
);
800 _rl_bind_if_unbound ("\033OB", rl_get_next_history
);
801 _rl_bind_if_unbound ("\033OC", rl_forward
);
802 _rl_bind_if_unbound ("\033OD", rl_backward
);
806 /* Try and bind the common arrow key prefix after giving termcap and
807 the inputrc file a chance to bind them and create `real' keymaps
808 for the arrow key prefix. */
814 xkeymap
= _rl_keymap
;
816 _rl_keymap
= emacs_standard_keymap
;
817 bind_arrow_keys_internal ();
819 #if defined (VI_MODE)
820 _rl_keymap
= vi_movement_keymap
;
821 bind_arrow_keys_internal ();
824 _rl_keymap
= xkeymap
;
828 /* **************************************************************** */
830 /* Numeric Arguments */
832 /* **************************************************************** */
834 /* Handle C-u style numeric args, as well as M--, and M-digits. */
838 int key
, c
, sawminus
, sawdigits
;
842 sawminus
= sawdigits
= 0;
845 rl_message ("(arg: %d) ", rl_arg_sign
* rl_numeric_arg
);
846 key
= c
= rl_read_key ();
848 /* If we see a key bound to `universal-argument' after seeing digits,
849 it ends the argument but is otherwise ignored. */
850 if (_rl_keymap
[c
].type
== ISFUNC
&&
851 _rl_keymap
[c
].function
== rl_universal_argument
)
860 key
= rl_read_key ();
861 _rl_restore_prompt ();
863 return (_rl_dispatch (key
, _rl_keymap
));
871 rl_numeric_arg
= rl_explicit_arg
? (rl_numeric_arg
* 10) + c
- '0' : c
- '0';
872 sawdigits
= rl_explicit_arg
= 1;
874 else if (c
== '-' && rl_explicit_arg
== 0)
876 rl_numeric_arg
= sawminus
= 1;
881 /* Make M-- command equivalent to M--1 command. */
882 if (sawminus
&& rl_numeric_arg
== 1 && rl_explicit_arg
== 0)
884 _rl_restore_prompt ();
886 return (_rl_dispatch (key
, _rl_keymap
));
893 /* Add the current digit to the argument in progress. */
895 rl_digit_argument (ignore
, key
)
898 rl_pending_input
= key
;
899 return (rl_digit_loop ());
902 /* What to do when you abort reading an argument. */
904 rl_discard_argument ()
908 _rl_init_argument ();
912 /* Create a default argument. */
916 rl_numeric_arg
= rl_arg_sign
= 1;
921 /* C-u, universal argument. Multiply the current argument by 4.
922 Read a key. If the key has nothing to do with arguments, then
923 dispatch on it. If the key is the abort character then abort. */
925 rl_universal_argument (count
, key
)
929 return (rl_digit_loop ());
932 /* **************************************************************** */
934 /* Insert and Delete */
936 /* **************************************************************** */
938 /* Insert a string of text into the line at point. This is the only
939 way that you should do insertion. rl_insert () calls this
942 rl_insert_text (string
)
945 register int i
, l
= strlen (string
);
947 if (rl_end
+ l
>= rl_line_buffer_len
)
948 rl_extend_line_buffer (rl_end
+ l
);
950 for (i
= rl_end
; i
>= rl_point
; i
--)
951 the_line
[i
+ l
] = the_line
[i
];
952 strncpy (the_line
+ rl_point
, string
, l
);
954 /* Remember how to undo this if we aren't undoing something. */
955 if (!_rl_doing_an_undo
)
957 /* If possible and desirable, concatenate the undos. */
960 (rl_undo_list
->what
== UNDO_INSERT
) &&
961 (rl_undo_list
->end
== rl_point
) &&
962 (rl_undo_list
->end
- rl_undo_list
->start
< 20))
965 rl_add_undo (UNDO_INSERT
, rl_point
, rl_point
+ l
, (char *)NULL
);
969 the_line
[rl_end
] = '\0';
973 /* Delete the string between FROM and TO. FROM is
974 inclusive, TO is not. */
976 rl_delete_text (from
, to
)
980 register int diff
, i
;
982 /* Fix it if the caller is confused. */
994 text
= rl_copy_text (from
, to
);
996 /* Some versions of strncpy() can't handle overlapping arguments. */
998 for (i
= from
; i
< rl_end
- diff
; i
++)
999 the_line
[i
] = the_line
[i
+ diff
];
1001 /* Remember how to undo this delete. */
1002 if (_rl_doing_an_undo
== 0)
1003 rl_add_undo (UNDO_DELETE
, from
, to
, text
);
1008 the_line
[rl_end
] = '\0';
1012 /* Fix up point so that it is within the line boundaries after killing
1013 text. If FIX_MARK_TOO is non-zero, the mark is forced within line
1016 #define _RL_FIX_POINT(x) \
1025 _rl_fix_point (fix_mark_too
)
1028 _RL_FIX_POINT (rl_point
);
1030 _RL_FIX_POINT (rl_mark
);
1032 #undef _RL_FIX_POINT
1035 _rl_replace_text (text
, start
, end
)
1039 rl_begin_undo_group ();
1040 rl_delete_text (start
, end
+ 1);
1042 rl_insert_text (text
);
1043 rl_end_undo_group ();
1046 /* **************************************************************** */
1048 /* Readline character functions */
1050 /* **************************************************************** */
1052 /* This is not a gap editor, just a stupid line input routine. No hair
1053 is involved in writing any of the functions, and none should be. */
1057 rl_end is the place in the string that we would place '\0';
1058 i.e., it is always safe to place '\0' there.
1060 rl_point is the place in the string where the cursor is. Sometimes
1061 this is the same as rl_end.
1063 Any command that is called interactively receives two arguments.
1064 The first is a count: the numeric arg pased to this command.
1065 The second is the key which invoked this command.
1068 /* **************************************************************** */
1070 /* Movement Commands */
1072 /* **************************************************************** */
1074 /* Note that if you `optimize' the display for these functions, you cannot
1075 use said functions in other functions which do not do optimizing display.
1076 I.e., you will have to update the data base for rl_redisplay, and you
1077 might as well let rl_redisplay do that job. */
1079 /* Move forward COUNT characters. */
1081 rl_forward (count
, key
)
1085 rl_backward (-count
, key
);
1088 int end
= rl_point
+ count
;
1089 #if defined (VI_MODE)
1090 int lend
= rl_end
- (rl_editing_mode
== vi_mode
);
1106 /* Move backward COUNT characters. */
1108 rl_backward (count
, key
)
1112 rl_forward (-count
, key
);
1115 if (rl_point
< count
)
1126 /* Move to the beginning of the line. */
1128 rl_beg_of_line (count
, key
)
1135 /* Move to the end of the line. */
1137 rl_end_of_line (count
, key
)
1144 /* Move forward a word. We do what Emacs does. */
1146 rl_forward_word (count
, key
)
1153 rl_backward_word (-count
, key
);
1159 if (rl_point
== rl_end
)
1162 /* If we are not in a word, move forward until we are in one.
1163 Then, move forward until we hit a non-alphabetic character. */
1164 c
= the_line
[rl_point
];
1165 if (alphabetic (c
) == 0)
1167 while (++rl_point
< rl_end
)
1169 c
= the_line
[rl_point
];
1174 if (rl_point
== rl_end
)
1176 while (++rl_point
< rl_end
)
1178 c
= the_line
[rl_point
];
1179 if (alphabetic (c
) == 0)
1187 /* Move backward a word. We do what Emacs does. */
1189 rl_backward_word (count
, key
)
1196 rl_forward_word (-count
, key
);
1205 /* Like rl_forward_word (), except that we look at the characters
1206 just before point. */
1208 c
= the_line
[rl_point
- 1];
1209 if (alphabetic (c
) == 0)
1213 c
= the_line
[rl_point
- 1];
1221 c
= the_line
[rl_point
- 1];
1222 if (alphabetic (c
) == 0)
1232 /* Clear the current line. Numeric argument to C-l does this. */
1236 int curr_line
, nleft
;
1238 /* Find out whether or not there might be invisible characters in the
1240 if (rl_display_prompt
== rl_prompt
)
1241 nleft
= _rl_last_c_pos
- screenwidth
- rl_visible_prompt_length
;
1243 nleft
= _rl_last_c_pos
- screenwidth
;
1246 curr_line
= 1 + nleft
/ screenwidth
;
1250 _rl_move_vert (curr_line
);
1251 _rl_move_cursor_relative (0, the_line
); /* XXX is this right */
1253 #if defined (__GO32__)
1255 int row
, col
, width
, row_start
;
1257 ScreenGetCursor (&row
, &col
);
1258 width
= ScreenCols ();
1259 row_start
= ScreenPrimary
+ (row
* width
);
1260 memset (row_start
+ col
, 0, (width
- col
) * 2);
1262 #else /* !__GO32__ */
1263 _rl_clear_to_eol (0); /* arg of 0 means to not use spaces */
1264 #endif /* !__GO32__ */
1266 rl_forced_update_display ();
1267 rl_display_fixed
= 1;
1272 /* C-l typed to a line without quoting clears the screen, and then reprints
1273 the prompt and the current input line. Given a numeric arg, redraw only
1274 the current line. */
1276 rl_clear_screen (count
, key
)
1279 if (rl_explicit_arg
)
1285 _rl_clear_screen (); /* calls termcap function to clear screen */
1286 rl_forced_update_display ();
1287 rl_display_fixed
= 1;
1293 rl_arrow_keys (count
, c
)
1298 ch
= rl_read_key ();
1300 switch (_rl_to_upper (ch
))
1303 rl_get_previous_history (count
, ch
);
1307 rl_get_next_history (count
, ch
);
1311 rl_forward (count
, ch
);
1315 rl_backward (count
, ch
);
1325 /* **************************************************************** */
1329 /* **************************************************************** */
1331 /* Insert the character C at the current location, moving point forward. */
1333 rl_insert (count
, c
)
1342 /* If we can optimize, then do it. But don't let people crash
1343 readline because of extra large arguments. */
1344 if (count
> 1 && count
<= 1024)
1346 string
= xmalloc (1 + count
);
1348 for (i
= 0; i
< count
; i
++)
1352 rl_insert_text (string
);
1363 for (i
= 0; i
< 1024; i
++)
1368 decreaser
= (count
> 1024 ? 1024 : count
);
1369 str
[decreaser
] = '\0';
1370 rl_insert_text (str
);
1377 /* We are inserting a single character.
1378 If there is pending input, then make a string of all of the
1379 pending characters that are bound to rl_insert, and insert
1381 if (_rl_any_typein ())
1382 _rl_insert_typein (c
);
1385 /* Inserting a single character. */
1390 rl_insert_text (str
);
1395 /* Insert the next typed character verbatim. */
1397 rl_quoted_insert (count
, key
)
1403 return (rl_insert (count
, c
));
1406 /* Insert a tab character. */
1408 rl_tab_insert (count
, key
)
1411 return (rl_insert (count
, '\t'));
1414 /* What to do when a NEWLINE is pressed. We accept the whole line.
1415 KEY is the key that invoked this command. I guess it could have
1416 meaning in the future. */
1418 rl_newline (count
, key
)
1423 #if defined (VI_MODE)
1424 if (rl_editing_mode
== vi_mode
)
1426 _rl_vi_done_inserting ();
1427 _rl_vi_reset_last ();
1429 #endif /* VI_MODE */
1431 if (readline_echoing_p
)
1432 _rl_update_final ();
1436 /* What to do for some uppercase characters, like meta characters,
1437 and some characters appearing in emacs_ctlx_keymap. This function
1438 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1439 is special cased. */
1441 rl_do_lowercase_version (ignore1
, ignore2
)
1442 int ignore1
, ignore2
;
1447 /* Rubout the character behind point. */
1449 rl_rubout (count
, key
)
1454 rl_delete (-count
, key
);
1464 if (count
> 1 || rl_explicit_arg
)
1466 int orig_point
= rl_point
;
1467 rl_backward (count
, key
);
1468 rl_kill_text (orig_point
, rl_point
);
1472 int c
= the_line
[--rl_point
];
1473 rl_delete_text (rl_point
, rl_point
+ 1);
1475 if (rl_point
== rl_end
&& isprint (c
) && _rl_last_c_pos
)
1478 l
= rl_character_len (c
, rl_point
);
1479 _rl_erase_at_end_of_line (l
);
1485 /* Delete the character under the cursor. Given a numeric argument,
1486 kill that many characters instead. */
1488 rl_delete (count
, key
)
1492 return (rl_rubout (-count
, key
));
1494 if (rl_point
== rl_end
)
1500 if (count
> 1 || rl_explicit_arg
)
1502 int orig_point
= rl_point
;
1503 rl_forward (count
, key
);
1504 rl_kill_text (orig_point
, rl_point
);
1505 rl_point
= orig_point
;
1509 return (rl_delete_text (rl_point
, rl_point
+ 1));
1513 /* Delete all spaces and tabs around point. */
1515 rl_delete_horizontal_space (count
, ignore
)
1518 int start
= rl_point
;
1520 while (rl_point
&& whitespace (the_line
[rl_point
- 1]))
1525 while (rl_point
< rl_end
&& whitespace (the_line
[rl_point
]))
1528 if (start
!= rl_point
)
1530 rl_delete_text (start
, rl_point
);
1536 #ifndef RL_COMMENT_BEGIN_DEFAULT
1537 #define RL_COMMENT_BEGIN_DEFAULT "#"
1540 /* Turn the current line into a comment in shell history.
1541 A K*rn shell style function. */
1543 rl_insert_comment (count
, key
)
1546 rl_beg_of_line (1, key
);
1547 rl_insert_text (_rl_comment_begin
? _rl_comment_begin
1548 : RL_COMMENT_BEGIN_DEFAULT
);
1549 (*rl_redisplay_function
) ();
1550 rl_newline (1, '\n');
1554 /* **************************************************************** */
1558 /* **************************************************************** */
1560 /* The three kinds of things that we know how to do. */
1565 static int rl_change_case ();
1567 /* Uppercase the word at point. */
1569 rl_upcase_word (count
, key
)
1572 return (rl_change_case (count
, UpCase
));
1575 /* Lowercase the word at point. */
1577 rl_downcase_word (count
, key
)
1580 return (rl_change_case (count
, DownCase
));
1583 /* Upcase the first letter, downcase the rest. */
1585 rl_capitalize_word (count
, key
)
1588 return (rl_change_case (count
, CapCase
));
1591 /* The meaty function.
1592 Change the case of COUNT words, performing OP on them.
1593 OP is one of UpCase, DownCase, or CapCase.
1594 If a negative argument is given, leave point where it started,
1595 otherwise, leave it where it moves to. */
1597 rl_change_case (count
, op
)
1600 register int start
, end
;
1604 rl_forward_word (count
, 0);
1610 /* We are going to modify some text, so let's prepare to undo it. */
1611 rl_modifying (start
, end
);
1613 for (inword
= 0; start
< end
; start
++)
1615 c
= the_line
[start
];
1619 the_line
[start
] = _rl_to_upper (c
);
1623 the_line
[start
] = _rl_to_lower (c
);
1627 the_line
[start
] = (inword
== 0) ? _rl_to_upper (c
) : _rl_to_lower (c
);
1628 inword
= alphabetic (the_line
[start
]);
1640 /* **************************************************************** */
1644 /* **************************************************************** */
1646 /* Transpose the words at point. */
1648 rl_transpose_words (count
, key
)
1651 char *word1
, *word2
;
1652 int w1_beg
, w1_end
, w2_beg
, w2_end
;
1653 int orig_point
= rl_point
;
1658 /* Find the two words. */
1659 rl_forward_word (count
, key
);
1661 rl_backward_word (1, key
);
1663 rl_backward_word (count
, key
);
1665 rl_forward_word (1, key
);
1668 /* Do some check to make sure that there really are two words. */
1669 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
1672 rl_point
= orig_point
;
1676 /* Get the text of the words. */
1677 word1
= rl_copy_text (w1_beg
, w1_end
);
1678 word2
= rl_copy_text (w2_beg
, w2_end
);
1680 /* We are about to do many insertions and deletions. Remember them
1681 as one operation. */
1682 rl_begin_undo_group ();
1684 /* Do the stuff at word2 first, so that we don't have to worry
1685 about word1 moving. */
1687 rl_delete_text (w2_beg
, w2_end
);
1688 rl_insert_text (word1
);
1691 rl_delete_text (w1_beg
, w1_end
);
1692 rl_insert_text (word2
);
1694 /* This is exactly correct since the text before this point has not
1695 changed in length. */
1698 /* I think that does it. */
1699 rl_end_undo_group ();
1706 /* Transpose the characters at point. If point is at the end of the line,
1707 then transpose the characters before point. */
1709 rl_transpose_chars (count
, key
)
1717 if (!rl_point
|| rl_end
< 2)
1723 rl_begin_undo_group ();
1725 if (rl_point
== rl_end
)
1732 dummy
[0] = the_line
[rl_point
];
1735 rl_delete_text (rl_point
, rl_point
+ 1);
1739 rl_insert_text (dummy
);
1741 rl_end_undo_group ();
1745 /* **************************************************************** */
1747 /* Character Searching */
1749 /* **************************************************************** */
1752 _rl_char_search_internal (count
, dir
, schar
)
1753 int count
, dir
, schar
;
1758 inc
= (dir
< 0) ? -1 : 1;
1761 if ((dir
< 0 && pos
<= 0) || (dir
> 0 && pos
>= rl_end
))
1770 if (rl_line_buffer
[pos
] == schar
)
1774 rl_point
= (dir
== BTO
) ? pos
+ 1 : pos
;
1776 rl_point
= (dir
== FTO
) ? pos
- 1 : pos
;
1780 while ((dir
< 0) ? pos
-- : ++pos
< rl_end
);
1785 /* Search COUNT times for a character read from the current input stream.
1786 FDIR is the direction to search if COUNT is non-negative; otherwise
1787 the search goes in BDIR. */
1789 _rl_char_search (count
, fdir
, bdir
)
1790 int count
, fdir
, bdir
;
1796 return (_rl_char_search_internal (-count
, bdir
, c
));
1798 return (_rl_char_search_internal (count
, fdir
, c
));
1802 rl_char_search (count
, key
)
1805 return (_rl_char_search (count
, FFIND
, BFIND
));
1809 rl_backward_char_search (count
, key
)
1812 return (_rl_char_search (count
, BFIND
, FFIND
));
1815 /* **************************************************************** */
1817 /* History Utilities */
1819 /* **************************************************************** */
1821 /* We already have a history library, and that is what we use to control
1822 the history features of readline. This is our local interface to
1823 the history mechanism. */
1825 /* While we are editing the history, this is the saved
1826 version of the original line. */
1827 HIST_ENTRY
*saved_line_for_history
= (HIST_ENTRY
*)NULL
;
1829 /* Set the history pointer back to the last entry in the history. */
1831 start_using_history ()
1834 if (saved_line_for_history
)
1835 _rl_free_history_entry (saved_line_for_history
);
1837 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
1840 /* Free the contents (and containing structure) of a HIST_ENTRY. */
1842 _rl_free_history_entry (entry
)
1852 /* Perhaps put back the current line if it has changed. */
1854 maybe_replace_line ()
1858 temp
= current_history ();
1859 /* If the current line has changed, save the changes. */
1860 if (temp
&& ((UNDO_LIST
*)(temp
->data
) != rl_undo_list
))
1862 temp
= replace_history_entry (where_history (), the_line
, rl_undo_list
);
1869 /* Put back the saved_line_for_history if there is one. */
1871 maybe_unsave_line ()
1875 if (saved_line_for_history
)
1877 line_len
= strlen (saved_line_for_history
->line
);
1879 if (line_len
>= rl_line_buffer_len
)
1880 rl_extend_line_buffer (line_len
);
1882 strcpy (the_line
, saved_line_for_history
->line
);
1883 rl_undo_list
= (UNDO_LIST
*)saved_line_for_history
->data
;
1884 _rl_free_history_entry (saved_line_for_history
);
1885 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
1886 rl_end
= rl_point
= strlen (the_line
);
1893 /* Save the current line in saved_line_for_history. */
1897 if (saved_line_for_history
== 0)
1899 saved_line_for_history
= (HIST_ENTRY
*)xmalloc (sizeof (HIST_ENTRY
));
1900 saved_line_for_history
->line
= savestring (the_line
);
1901 saved_line_for_history
->data
= (char *)rl_undo_list
;
1906 /* **************************************************************** */
1908 /* History Commands */
1910 /* **************************************************************** */
1912 /* Meta-< goes to the start of the history. */
1914 rl_beginning_of_history (count
, key
)
1917 return (rl_get_previous_history (1 + where_history (), key
));
1920 /* Meta-> goes to the end of the history. (The current line). */
1922 rl_end_of_history (count
, key
)
1925 maybe_replace_line ();
1927 maybe_unsave_line ();
1931 /* Move down to the next history line. */
1933 rl_get_next_history (count
, key
)
1940 return (rl_get_previous_history (-count
, key
));
1945 maybe_replace_line ();
1947 temp
= (HIST_ENTRY
*)NULL
;
1950 temp
= next_history ();
1957 maybe_unsave_line ();
1960 line_len
= strlen (temp
->line
);
1962 if (line_len
>= rl_line_buffer_len
)
1963 rl_extend_line_buffer (line_len
);
1965 strcpy (the_line
, temp
->line
);
1966 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
1967 rl_end
= rl_point
= strlen (the_line
);
1968 #if defined (VI_MODE)
1969 if (rl_editing_mode
== vi_mode
)
1971 #endif /* VI_MODE */
1976 /* Get the previous item out of our interactive history, making it the current
1977 line. If there is no previous history, just ding. */
1979 rl_get_previous_history (count
, key
)
1982 HIST_ENTRY
*old_temp
, *temp
;
1986 return (rl_get_next_history (-count
, key
));
1991 /* If we don't have a line saved, then save this one. */
1994 /* If the current line has changed, save the changes. */
1995 maybe_replace_line ();
1997 temp
= old_temp
= (HIST_ENTRY
*)NULL
;
2000 temp
= previous_history ();
2008 /* If there was a large argument, and we moved back to the start of the
2009 history, that is not an error. So use the last value found. */
2010 if (!temp
&& old_temp
)
2017 line_len
= strlen (temp
->line
);
2019 if (line_len
>= rl_line_buffer_len
)
2020 rl_extend_line_buffer (line_len
);
2022 strcpy (the_line
, temp
->line
);
2023 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
2024 rl_end
= rl_point
= line_len
;
2026 #if defined (VI_MODE)
2027 if (rl_editing_mode
== vi_mode
)
2029 #endif /* VI_MODE */
2034 /* **************************************************************** */
2036 /* The Mark and the Region. */
2038 /* **************************************************************** */
2040 /* Set the mark at POSITION. */
2042 _rl_set_mark_at_pos (position
)
2045 if (position
> rl_end
)
2052 /* A bindable command to set the mark. */
2054 rl_set_mark (count
, key
)
2057 return (_rl_set_mark_at_pos (rl_explicit_arg
? count
: rl_point
));
2060 /* Exchange the position of mark and point. */
2062 rl_exchange_point_and_mark (count
, key
)
2065 if (rl_mark
> rl_end
)
2074 SWAP (rl_point
, rl_mark
);
2079 /* **************************************************************** */
2083 /* **************************************************************** */
2084 /* How to toggle back and forth between editing modes. */
2086 rl_vi_editing_mode (count
, key
)
2089 #if defined (VI_MODE)
2090 rl_editing_mode
= vi_mode
;
2091 rl_vi_insertion_mode (1, key
);
2092 #endif /* VI_MODE */
2097 rl_emacs_editing_mode (count
, key
)
2100 rl_editing_mode
= emacs_mode
;
2101 _rl_keymap
= emacs_standard_keymap
;