1 /* text.c -- text handling commands for readline. */
3 /* Copyright (C) 1987-2020 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #if defined (HAVE_UNISTD_H)
30 #endif /* HAVE_UNISTD_H */
32 #if defined (HAVE_STDLIB_H)
35 # include "ansi_stdlib.h"
36 #endif /* HAVE_STDLIB_H */
38 #if defined (HAVE_LOCALE_H)
44 /* System-specific feature definitions and include files. */
49 # define INCL_DOSPROCESS
53 /* Some standard library routines. */
57 #include "rlprivate.h"
61 /* Forward declarations. */
62 static int rl_change_case
PARAMS((int, int));
63 static int _rl_char_search
PARAMS((int, int, int));
65 #if defined (READLINE_CALLBACKS)
66 static int _rl_insert_next_callback
PARAMS((_rl_callback_generic_arg
*));
67 static int _rl_char_search_callback
PARAMS((_rl_callback_generic_arg
*));
70 /* The largest chunk of text that can be inserted in one call to
71 rl_insert_text. Text blocks larger than this are divided. */
72 #define TEXT_COUNT_MAX 1024
74 int _rl_optimize_typeahead
= 1; /* rl_insert tries to read typeahead */
76 /* **************************************************************** */
78 /* Insert and Delete */
80 /* **************************************************************** */
82 /* Insert a string of text into the line at point. This is the only
83 way that you should do insertion. _rl_insert_char () calls this
84 function. Returns the number of characters inserted. */
86 rl_insert_text (const char *string
)
90 l
= (string
&& *string
) ? strlen (string
) : 0;
94 if (rl_end
+ l
>= rl_line_buffer_len
)
95 rl_extend_line_buffer (rl_end
+ l
);
97 for (i
= rl_end
; i
>= rl_point
; i
--)
98 rl_line_buffer
[i
+ l
] = rl_line_buffer
[i
];
99 strncpy (rl_line_buffer
+ rl_point
, string
, l
);
101 /* Remember how to undo this if we aren't undoing something. */
102 if (_rl_doing_an_undo
== 0)
104 /* If possible and desirable, concatenate the undos. */
107 (rl_undo_list
->what
== UNDO_INSERT
) &&
108 (rl_undo_list
->end
== rl_point
) &&
109 (rl_undo_list
->end
- rl_undo_list
->start
< 20))
112 rl_add_undo (UNDO_INSERT
, rl_point
, rl_point
+ l
, (char *)NULL
);
116 rl_line_buffer
[rl_end
] = '\0';
120 /* Delete the string between FROM and TO. FROM is inclusive, TO is not.
121 Returns the number of characters deleted. */
123 rl_delete_text (int from
, int to
)
126 register int diff
, i
;
128 /* Fix it if the caller is confused. */
142 text
= rl_copy_text (from
, to
);
144 /* Some versions of strncpy() can't handle overlapping arguments. */
146 for (i
= from
; i
< rl_end
- diff
; i
++)
147 rl_line_buffer
[i
] = rl_line_buffer
[i
+ diff
];
149 /* Remember how to undo this delete. */
150 if (_rl_doing_an_undo
== 0)
151 rl_add_undo (UNDO_DELETE
, from
, to
, text
);
156 rl_line_buffer
[rl_end
] = '\0';
161 /* Fix up point so that it is within the line boundaries after killing
162 text. If FIX_MARK_TOO is non-zero, the mark is forced within line
165 #define _RL_FIX_POINT(x) \
174 _rl_fix_point (int fix_mark_too
)
176 _RL_FIX_POINT (rl_point
);
178 _RL_FIX_POINT (rl_mark
);
184 _RL_FIX_POINT (rl_mark
);
188 /* Replace the contents of the line buffer between START and END with
189 TEXT. The operation is undoable. To replace the entire line in an
190 undoable mode, use _rl_replace_text(text, 0, rl_end); */
192 _rl_replace_text (const char *text
, int start
, int end
)
197 rl_begin_undo_group ();
199 rl_delete_text (start
, end
+ 1);
202 n
= rl_insert_text (text
);
203 rl_end_undo_group ();
208 /* Replace the current line buffer contents with TEXT. If CLEAR_UNDO is
209 non-zero, we free the current undo list. */
211 rl_replace_line (const char *text
, int clear_undo
)
216 if (len
>= rl_line_buffer_len
)
217 rl_extend_line_buffer (len
);
218 strcpy (rl_line_buffer
, text
);
222 rl_free_undo_list ();
227 /* **************************************************************** */
229 /* Readline character functions */
231 /* **************************************************************** */
233 /* This is not a gap editor, just a stupid line input routine. No hair
234 is involved in writing any of the functions, and none should be. */
238 rl_end is the place in the string that we would place '\0';
239 i.e., it is always safe to place '\0' there.
241 rl_point is the place in the string where the cursor is. Sometimes
242 this is the same as rl_end.
244 Any command that is called interactively receives two arguments.
245 The first is a count: the numeric arg passed to this command.
246 The second is the key which invoked this command.
249 /* **************************************************************** */
251 /* Movement Commands */
253 /* **************************************************************** */
255 /* Note that if you `optimize' the display for these functions, you cannot
256 use said functions in other functions which do not do optimizing display.
257 I.e., you will have to update the data base for rl_redisplay, and you
258 might as well let rl_redisplay do that job. */
260 /* Move forward COUNT bytes. */
262 rl_forward_byte (int count
, int key
)
265 return (rl_backward_byte (-count
, key
));
271 end
= rl_point
+ count
;
272 #if defined (VI_MODE)
273 lend
= rl_end
> 0 ? rl_end
- (VI_COMMAND_MODE()) : rl_end
;
294 _rl_forward_char_internal (int count
)
298 #if defined (HANDLE_MULTIBYTE)
299 point
= _rl_find_next_mbchar (rl_line_buffer
, rl_point
, count
, MB_FIND_NONZERO
);
301 #if defined (VI_MODE)
302 if (point
>= rl_end
&& VI_COMMAND_MODE())
303 point
= _rl_find_prev_mbchar (rl_line_buffer
, rl_end
, MB_FIND_NONZERO
);
309 point
= rl_point
+ count
;
318 _rl_backward_char_internal (int count
)
323 #if defined (HANDLE_MULTIBYTE)
326 while (count
> 0 && point
> 0)
328 point
= _rl_find_prev_mbchar (rl_line_buffer
, point
, MB_FIND_NONZERO
);
332 return 0; /* XXX - rl_ding() here? */
344 #if defined (HANDLE_MULTIBYTE)
345 /* Move forward COUNT characters. */
347 rl_forward_char (int count
, int key
)
351 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
352 return (rl_forward_byte (count
, key
));
355 return (rl_backward_char (-count
, key
));
359 if (rl_point
== rl_end
&& EMACS_MODE())
365 point
= _rl_forward_char_internal (count
);
367 if (rl_point
== point
)
375 #else /* !HANDLE_MULTIBYTE */
377 rl_forward_char (int count
, int key
)
379 return (rl_forward_byte (count
, key
));
381 #endif /* !HANDLE_MULTIBYTE */
383 /* Backwards compatibility. */
385 rl_forward (int count
, int key
)
387 return (rl_forward_char (count
, key
));
390 /* Move backward COUNT bytes. */
392 rl_backward_byte (int count
, int key
)
395 return (rl_forward_byte (-count
, key
));
399 if (rl_point
< count
)
414 #if defined (HANDLE_MULTIBYTE)
415 /* Move backward COUNT characters. */
417 rl_backward_char (int count
, int key
)
421 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
422 return (rl_backward_byte (count
, key
));
425 return (rl_forward_char (-count
, key
));
431 while (count
> 0 && point
> 0)
433 point
= _rl_find_prev_mbchar (rl_line_buffer
, point
, MB_FIND_NONZERO
);
449 rl_backward_char (int count
, int key
)
451 return (rl_backward_byte (count
, key
));
455 /* Backwards compatibility. */
457 rl_backward (int count
, int key
)
459 return (rl_backward_char (count
, key
));
462 /* Move to the beginning of the line. */
464 rl_beg_of_line (int count
, int key
)
470 /* Move to the end of the line. */
472 rl_end_of_line (int count
, int key
)
478 /* Move forward a word. We do what Emacs does. Handles multibyte chars. */
480 rl_forward_word (int count
, int key
)
485 return (rl_backward_word (-count
, key
));
489 if (rl_point
> rl_end
)
491 if (rl_point
== rl_end
)
494 /* If we are not in a word, move forward until we are in one.
495 Then, move forward until we hit a non-alphabetic character. */
496 c
= _rl_char_value (rl_line_buffer
, rl_point
);
498 if (_rl_walphabetic (c
) == 0)
500 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
501 while (rl_point
< rl_end
)
503 c
= _rl_char_value (rl_line_buffer
, rl_point
);
504 if (_rl_walphabetic (c
))
506 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
510 if (rl_point
> rl_end
)
512 if (rl_point
== rl_end
)
515 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
516 while (rl_point
< rl_end
)
518 c
= _rl_char_value (rl_line_buffer
, rl_point
);
519 if (_rl_walphabetic (c
) == 0)
521 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
530 /* Move backward a word. We do what Emacs does. Handles multibyte chars. */
532 rl_backward_word (int count
, int key
)
537 return (rl_forward_word (-count
, key
));
544 /* Like rl_forward_word (), except that we look at the characters
545 just before point. */
547 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
548 c
= _rl_char_value (rl_line_buffer
, p
);
550 if (_rl_walphabetic (c
) == 0)
555 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
556 c
= _rl_char_value (rl_line_buffer
, p
);
557 if (_rl_walphabetic (c
))
565 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
566 c
= _rl_char_value (rl_line_buffer
, p
);
567 if (_rl_walphabetic (c
) == 0)
579 /* Clear the current line. Numeric argument to C-l does this. */
581 rl_refresh_line (int ignore1
, int ignore2
)
584 rl_display_fixed
= 1;
588 /* C-l typed to a line without quoting clears the screen, and then reprints
589 the prompt and the current input line. Given a numeric arg, redraw only
592 rl_clear_screen (int count
, int key
)
596 rl_refresh_line (count
, key
);
600 _rl_clear_screen (0); /* calls termcap function to clear screen */
601 rl_keep_mark_active ();
602 rl_forced_update_display ();
603 rl_display_fixed
= 1;
609 rl_clear_display (int count
, int key
)
611 _rl_clear_screen (1); /* calls termcap function to clear screen and scrollback buffer */
612 rl_forced_update_display ();
613 rl_display_fixed
= 1;
619 rl_previous_screen_line (int count
, int key
)
623 c
= _rl_term_autowrap
? _rl_screenwidth
: (_rl_screenwidth
+ 1);
624 return (rl_backward_char (c
, key
));
628 rl_next_screen_line (int count
, int key
)
632 c
= _rl_term_autowrap
? _rl_screenwidth
: (_rl_screenwidth
+ 1);
633 return (rl_forward_char (c
, key
));
637 rl_skip_csi_sequence (int count
, int key
)
641 RL_SETSTATE (RL_STATE_MOREINPUT
);
644 while (ch
>= 0x20 && ch
< 0x40);
645 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
651 rl_arrow_keys (int count
, int key
)
655 RL_SETSTATE(RL_STATE_MOREINPUT
);
657 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
661 switch (_rl_to_upper (ch
))
664 rl_get_previous_history (count
, ch
);
668 rl_get_next_history (count
, ch
);
672 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
673 rl_forward_char (count
, ch
);
675 rl_forward_byte (count
, ch
);
679 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
680 rl_backward_char (count
, ch
);
682 rl_backward_byte (count
, ch
);
692 /* **************************************************************** */
696 /* **************************************************************** */
698 #ifdef HANDLE_MULTIBYTE
699 static char pending_bytes
[MB_LEN_MAX
];
700 static int pending_bytes_length
= 0;
701 static mbstate_t ps
= {0};
704 /* Insert the character C at the current location, moving point forward.
705 If C introduces a multibyte sequence, we read the whole sequence and
706 then insert the multibyte char into the line buffer. */
708 _rl_insert_char (int count
, int c
)
712 #ifdef HANDLE_MULTIBYTE
714 char incoming
[MB_LEN_MAX
+ 1];
715 int incoming_length
= 0;
717 static int stored_count
= 0;
723 #if defined (HANDLE_MULTIBYTE)
724 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
730 else if (_rl_utf8locale
&& (c
& 0x80) == 0)
741 if (stored_count
<= 0)
742 stored_count
= count
;
744 count
= stored_count
;
747 pending_bytes
[pending_bytes_length
++] = c
;
748 ret
= mbrtowc (&wc
, pending_bytes
, pending_bytes_length
, &ps
);
750 if (ret
== (size_t)-2)
752 /* Bytes too short to compose character, try to wait for next byte.
753 Restore the state of the byte sequence, because in this case the
754 effect of mbstate is undefined. */
758 else if (ret
== (size_t)-1)
760 /* Invalid byte sequence for the current locale. Treat first byte
761 as a single character. */
762 incoming
[0] = pending_bytes
[0];
765 pending_bytes_length
--;
766 memmove (pending_bytes
, pending_bytes
+ 1, pending_bytes_length
);
767 /* Clear the state of the byte sequence, because in this case the
768 effect of mbstate is undefined. */
769 memset (&ps
, 0, sizeof (mbstate_t));
771 else if (ret
== (size_t)0)
775 pending_bytes_length
--;
776 /* Clear the state of the byte sequence, because in this case the
777 effect of mbstate is undefined. */
778 memset (&ps
, 0, sizeof (mbstate_t));
782 incoming
[0] = pending_bytes
[0];
783 incoming
[incoming_length
= 1] = '\0';
784 pending_bytes_length
= 0;
788 /* We successfully read a single multibyte character. */
789 memcpy (incoming
, pending_bytes
, pending_bytes_length
);
790 incoming
[pending_bytes_length
] = '\0';
791 incoming_length
= pending_bytes_length
;
792 pending_bytes_length
= 0;
795 #endif /* HANDLE_MULTIBYTE */
797 /* If we can optimize, then do it. But don't let people crash
798 readline because of extra large arguments. */
799 if (count
> 1 && count
<= TEXT_COUNT_MAX
)
801 #if defined (HANDLE_MULTIBYTE)
802 string_size
= count
* incoming_length
;
803 string
= (char *)xmalloc (1 + string_size
);
806 while (i
< string_size
)
808 if (incoming_length
== 1)
809 string
[i
++] = *incoming
;
812 strncpy (string
+ i
, incoming
, incoming_length
);
813 i
+= incoming_length
;
818 #else /* !HANDLE_MULTIBYTE */
819 string
= (char *)xmalloc (1 + count
);
821 for (i
= 0; i
< count
; i
++)
823 #endif /* !HANDLE_MULTIBYTE */
826 rl_insert_text (string
);
832 if (count
> TEXT_COUNT_MAX
)
835 #if defined (HANDLE_MULTIBYTE)
836 string_size
= incoming_length
* TEXT_COUNT_MAX
;
837 string
= (char *)xmalloc (1 + string_size
);
840 while (i
< string_size
)
842 if (incoming_length
== 1)
843 string
[i
++] = *incoming
;
846 strncpy (string
+ i
, incoming
, incoming_length
);
847 i
+= incoming_length
;
853 decreaser
= (count
> TEXT_COUNT_MAX
) ? TEXT_COUNT_MAX
: count
;
854 string
[decreaser
*incoming_length
] = '\0';
855 rl_insert_text (string
);
862 #else /* !HANDLE_MULTIBYTE */
863 char str
[TEXT_COUNT_MAX
+1];
865 for (i
= 0; i
< TEXT_COUNT_MAX
; i
++)
870 decreaser
= (count
> TEXT_COUNT_MAX
? TEXT_COUNT_MAX
: count
);
871 str
[decreaser
] = '\0';
872 rl_insert_text (str
);
875 #endif /* !HANDLE_MULTIBYTE */
880 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
882 /* We are inserting a single character.
883 If there is pending input, then make a string of all of the
884 pending characters that are bound to rl_insert, and insert
885 them all. Don't do this if we're current reading input from
887 if ((RL_ISSTATE (RL_STATE_MACROINPUT
) == 0) && _rl_pushed_input_available ())
888 _rl_insert_typein (c
);
891 /* Inserting a single character. */
896 rl_insert_text (str
);
899 #if defined (HANDLE_MULTIBYTE)
902 rl_insert_text (incoming
);
910 /* Overwrite the character at point (or next COUNT characters) with C.
911 If C introduces a multibyte character sequence, read the entire sequence
912 before starting the overwrite loop. */
914 _rl_overwrite_char (int count
, int c
)
917 #if defined (HANDLE_MULTIBYTE)
918 char mbkey
[MB_LEN_MAX
];
921 /* Read an entire multibyte character sequence to insert COUNT times. */
922 if (count
> 0 && MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
923 k
= _rl_read_mbstring (c
, mbkey
, MB_LEN_MAX
);
926 rl_begin_undo_group ();
928 for (i
= 0; i
< count
; i
++)
930 #if defined (HANDLE_MULTIBYTE)
931 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
932 rl_insert_text (mbkey
);
935 _rl_insert_char (1, c
);
937 if (rl_point
< rl_end
)
941 rl_end_undo_group ();
947 rl_insert (int count
, int c
)
951 r
= (rl_insert_mode
== RL_IM_INSERT
) ? _rl_insert_char (count
, c
) : _rl_overwrite_char (count
, c
);
953 /* XXX -- attempt to batch-insert pending input that maps to self-insert */
955 n
= (unsigned short)-2;
956 while (_rl_optimize_typeahead
&&
957 rl_num_chars_to_read
== 0 &&
958 (RL_ISSTATE (RL_STATE_INPUTPENDING
|RL_STATE_MACROINPUT
) == 0) &&
959 _rl_pushed_input_available () == 0 &&
960 _rl_input_queued (0) &&
961 (n
= rl_read_key ()) > 0 &&
962 _rl_keymap
[(unsigned char)n
].type
== ISFUNC
&&
963 _rl_keymap
[(unsigned char)n
].function
== rl_insert
)
965 r
= (rl_insert_mode
== RL_IM_INSERT
) ? _rl_insert_char (1, n
) : _rl_overwrite_char (1, n
);
966 /* _rl_insert_char keeps its own set of pending characters to compose a
967 complete multibyte character, and only returns 1 if it sees a character
968 that's part of a multibyte character but too short to complete one. We
969 can try to read another character in the hopes that we will get the
970 next one or just punt. Right now we try to read another character.
971 We don't want to call rl_insert_next if _rl_insert_char has already
972 stored the character in the pending_bytes array because that will
973 result in doubled input. */
974 n
= (unsigned short)-2;
975 x
++; /* count of bytes of typeahead read, currently unused */
976 if (r
== 1) /* read partial multibyte character */
978 if (rl_done
|| r
!= 0)
982 if (n
!= (unsigned short)-2) /* -2 = sentinel value for having inserted N */
984 /* setting rl_pending_input inhibits setting rl_last_func so we do it
986 rl_last_func
= rl_insert
;
987 _rl_reset_argument ();
988 rl_executing_keyseq
[rl_key_sequence_length
= 0] = '\0';
989 r
= rl_execute_next (n
);
995 /* Insert the next typed character verbatim. */
997 _rl_insert_next (int count
)
1001 RL_SETSTATE(RL_STATE_MOREINPUT
);
1003 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
1008 if (RL_ISSTATE (RL_STATE_MACRODEF
))
1009 _rl_add_macro_char (c
);
1011 #if defined (HANDLE_SIGNALS)
1012 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
1013 _rl_restore_tty_signals ();
1016 return (_rl_insert_char (count
, c
));
1019 #if defined (READLINE_CALLBACKS)
1021 _rl_insert_next_callback (_rl_callback_generic_arg
*data
)
1025 count
= data
->count
;
1031 r
= _rl_insert_next (1);
1032 _rl_want_redisplay
= 1;
1033 /* If we should keep going, leave the callback function installed */
1034 if (data
->count
< 0 && r
== 0)
1036 count
= 0; /* data->count == 0 || r != 0; force break below */
1039 /* Deregister function, let rl_callback_read_char deallocate data */
1040 _rl_callback_func
= 0;
1041 _rl_want_redisplay
= 1;
1046 return _rl_insert_next (count
);
1051 rl_quoted_insert (int count
, int key
)
1053 /* Let's see...should the callback interface futz with signal handling? */
1054 #if defined (HANDLE_SIGNALS)
1055 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
1056 _rl_disable_tty_signals ();
1059 #if defined (READLINE_CALLBACKS)
1060 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1062 _rl_callback_data
= _rl_callback_data_alloc (count
);
1063 _rl_callback_func
= _rl_insert_next_callback
;
1068 /* A negative count means to quote the next -COUNT characters. */
1074 r
= _rl_insert_next (1);
1075 while (r
== 0 && ++count
< 0);
1079 return _rl_insert_next (count
);
1082 /* Insert a tab character. */
1084 rl_tab_insert (int count
, int key
)
1086 return (_rl_insert_char (count
, '\t'));
1089 /* What to do when a NEWLINE is pressed. We accept the whole line.
1090 KEY is the key that invoked this command. I guess it could have
1091 meaning in the future. */
1093 rl_newline (int count
, int key
)
1095 if (rl_mark_active_p ())
1097 rl_deactivate_mark ();
1098 (*rl_redisplay_function
) ();
1099 _rl_want_redisplay
= 0;
1104 if (_rl_history_preserve_point
)
1105 _rl_history_saved_point
= (rl_point
== rl_end
) ? -1 : rl_point
;
1107 RL_SETSTATE(RL_STATE_DONE
);
1109 #if defined (VI_MODE)
1110 if (rl_editing_mode
== vi_mode
)
1112 _rl_vi_done_inserting ();
1113 if (_rl_vi_textmod_command (_rl_vi_last_command
) == 0) /* XXX */
1114 _rl_vi_reset_last ();
1116 #endif /* VI_MODE */
1118 /* If we've been asked to erase empty lines, suppress the final update,
1119 since _rl_update_final calls rl_crlf(). */
1120 if (rl_erase_empty_line
&& rl_point
== 0 && rl_end
== 0)
1124 _rl_update_final ();
1128 /* What to do for some uppercase characters, like meta characters,
1129 and some characters appearing in emacs_ctlx_keymap. This function
1130 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1131 is special cased. */
1133 rl_do_lowercase_version (int ignore1
, int ignore2
)
1138 /* This is different from what vi does, so the code's not shared. Emacs
1139 rubout in overwrite mode has one oddity: it replaces a control
1140 character that's displayed as two characters (^X) with two spaces. */
1142 _rl_overwrite_rubout (int count
, int key
)
1155 /* L == number of spaces to insert */
1156 for (i
= l
= 0; i
< count
; i
++)
1158 rl_backward_char (1, key
);
1159 l
+= rl_character_len (rl_line_buffer
[rl_point
], rl_point
); /* not exactly right */
1162 rl_begin_undo_group ();
1164 if (count
> 1 || rl_explicit_arg
)
1165 rl_kill_text (opoint
, rl_point
);
1167 rl_delete_text (opoint
, rl_point
);
1169 /* Emacs puts point at the beginning of the sequence of spaces. */
1170 if (rl_point
< rl_end
)
1173 _rl_insert_char (l
, ' ');
1177 rl_end_undo_group ();
1182 /* Rubout the character behind point. */
1184 rl_rubout (int count
, int key
)
1187 return (rl_delete (-count
, key
));
1195 if (rl_insert_mode
== RL_IM_OVERWRITE
)
1196 return (_rl_overwrite_rubout (count
, key
));
1198 return (_rl_rubout_char (count
, key
));
1202 _rl_rubout_char (int count
, int key
)
1207 /* Duplicated code because this is called from other parts of the library. */
1209 return (rl_delete (-count
, key
));
1217 orig_point
= rl_point
;
1218 if (count
> 1 || rl_explicit_arg
)
1220 rl_backward_char (count
, key
);
1221 rl_kill_text (orig_point
, rl_point
);
1223 else if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
1225 c
= rl_line_buffer
[--rl_point
];
1226 rl_delete_text (rl_point
, orig_point
);
1227 /* The erase-at-end-of-line hack is of questionable merit now. */
1228 if (rl_point
== rl_end
&& ISPRINT ((unsigned char)c
) && _rl_last_c_pos
)
1231 l
= rl_character_len (c
, rl_point
);
1232 _rl_erase_at_end_of_line (l
);
1237 rl_point
= _rl_find_prev_mbchar (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1238 rl_delete_text (rl_point
, orig_point
);
1244 /* Delete the character under the cursor. Given a numeric argument,
1245 kill that many characters instead. */
1247 rl_delete (int count
, int key
)
1252 return (_rl_rubout_char (-count
, key
));
1254 if (rl_point
== rl_end
)
1260 if (count
> 1 || rl_explicit_arg
)
1263 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1264 rl_forward_char (count
, key
);
1266 rl_forward_byte (count
, key
);
1268 rl_kill_text (xpoint
, rl_point
);
1273 xpoint
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
1274 rl_delete_text (rl_point
, xpoint
);
1279 /* Delete the character under the cursor, unless the insertion
1280 point is at the end of the line, in which case the character
1281 behind the cursor is deleted. COUNT is obeyed and may be used
1282 to delete forward or backward that many characters. */
1284 rl_rubout_or_delete (int count
, int key
)
1286 if (rl_end
!= 0 && rl_point
== rl_end
)
1287 return (_rl_rubout_char (count
, key
));
1289 return (rl_delete (count
, key
));
1292 /* Delete all spaces and tabs around point. */
1294 rl_delete_horizontal_space (int count
, int ignore
)
1298 while (rl_point
&& whitespace (rl_line_buffer
[rl_point
- 1]))
1303 while (rl_point
< rl_end
&& whitespace (rl_line_buffer
[rl_point
]))
1306 if (start
!= rl_point
)
1308 rl_delete_text (start
, rl_point
);
1318 /* Like the tcsh editing function delete-char-or-list. The eof character
1319 is caught before this is invoked, so this really does the same thing as
1320 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1322 rl_delete_or_show_completions (int count
, int key
)
1324 if (rl_end
!= 0 && rl_point
== rl_end
)
1325 return (rl_possible_completions (count
, key
));
1327 return (rl_delete (count
, key
));
1330 #ifndef RL_COMMENT_BEGIN_DEFAULT
1331 #define RL_COMMENT_BEGIN_DEFAULT "#"
1334 /* Turn the current line into a comment in shell history.
1335 A K*rn shell style function. */
1337 rl_insert_comment (int count
, int key
)
1339 char *rl_comment_text
;
1342 rl_beg_of_line (1, key
);
1343 rl_comment_text
= _rl_comment_begin
? _rl_comment_begin
: RL_COMMENT_BEGIN_DEFAULT
;
1345 if (rl_explicit_arg
== 0)
1346 rl_insert_text (rl_comment_text
);
1349 rl_comment_len
= strlen (rl_comment_text
);
1350 if (STREQN (rl_comment_text
, rl_line_buffer
, rl_comment_len
))
1351 rl_delete_text (rl_point
, rl_point
+ rl_comment_len
);
1353 rl_insert_text (rl_comment_text
);
1356 (*rl_redisplay_function
) ();
1357 rl_newline (1, '\n');
1362 /* **************************************************************** */
1366 /* **************************************************************** */
1368 /* The three kinds of things that we know how to do. */
1373 /* Uppercase the word at point. */
1375 rl_upcase_word (int count
, int key
)
1377 return (rl_change_case (count
, UpCase
));
1380 /* Lowercase the word at point. */
1382 rl_downcase_word (int count
, int key
)
1384 return (rl_change_case (count
, DownCase
));
1387 /* Upcase the first letter, downcase the rest. */
1389 rl_capitalize_word (int count
, int key
)
1391 return (rl_change_case (count
, CapCase
));
1394 /* The meaty function.
1395 Change the case of COUNT words, performing OP on them.
1396 OP is one of UpCase, DownCase, or CapCase.
1397 If a negative argument is given, leave point where it started,
1398 otherwise, leave it where it moves to. */
1400 rl_change_case (int count
, int op
)
1402 int start
, next
, end
;
1403 int inword
, nc
, nop
;
1405 #if defined (HANDLE_MULTIBYTE)
1407 char mb
[MB_LEN_MAX
+1];
1414 rl_forward_word (count
, 0);
1417 if (op
!= UpCase
&& op
!= DownCase
&& op
!= CapCase
)
1426 #if defined (HANDLE_MULTIBYTE)
1427 memset (&mps
, 0, sizeof (mbstate_t));
1430 /* We are going to modify some text, so let's prepare to undo it. */
1431 rl_modifying (start
, end
);
1436 c
= _rl_char_value (rl_line_buffer
, start
);
1437 /* This assumes that the upper and lower case versions are the same width. */
1438 next
= MB_NEXTCHAR (rl_line_buffer
, start
, 1, MB_FIND_NONZERO
);
1440 if (_rl_walphabetic (c
) == 0)
1449 nop
= inword
? DownCase
: UpCase
;
1454 /* Can't check isascii here; some languages (e.g, Turkish) have
1455 multibyte upper and lower case equivalents of single-byte ascii
1457 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
1459 nc
= (nop
== UpCase
) ? _rl_to_upper (c
) : _rl_to_lower (c
);
1460 rl_line_buffer
[start
] = nc
;
1462 #if defined (HANDLE_MULTIBYTE)
1465 m
= mbrtowc (&wc
, rl_line_buffer
+ start
, end
- start
, &mps
);
1466 if (MB_INVALIDCH (m
))
1467 wc
= (wchar_t)rl_line_buffer
[start
];
1468 else if (MB_NULLWCH (m
))
1470 nwc
= (nop
== UpCase
) ? _rl_to_wupper (wc
) : _rl_to_wlower (wc
);
1471 if (nwc
!= wc
) /* just skip unchanged characters */
1476 memset (&ts
, 0, sizeof (mbstate_t));
1477 mlen
= wcrtomb (mb
, nwc
, &ts
);
1481 memset (&ts
, 0, sizeof (mbstate_t));
1482 mlen
= wcrtomb (mb
, nwc
, &ts
);
1483 if (mlen
< 0) /* should not happen */
1484 strncpy (mb
, rl_line_buffer
+ start
, mlen
= m
);
1488 /* what to do if m != mlen? adjust below */
1489 /* m == length of old char, mlen == length of new char */
1490 s
= rl_line_buffer
+ start
;
1491 e
= rl_line_buffer
+ rl_end
;
1493 memcpy (s
, mb
, mlen
);
1496 memcpy (s
, mb
, mlen
);
1497 memmove (s
+ mlen
, s
+ m
, (e
- s
) - m
);
1498 next
-= m
- mlen
; /* next char changes */
1499 end
-= m
- mlen
; /* end of word changes */
1500 rl_end
-= m
- mlen
; /* end of line changes */
1501 rl_line_buffer
[rl_end
] = 0;
1505 rl_extend_line_buffer (rl_end
+ mlen
+ (e
- s
) - m
+ 2);
1506 s
= rl_line_buffer
+ start
; /* have to redo this */
1507 e
= rl_line_buffer
+ rl_end
;
1508 memmove (s
+ mlen
, s
+ m
, (e
- s
) - m
);
1509 memcpy (s
, mb
, mlen
);
1510 next
+= mlen
- m
; /* next char changes */
1511 end
+= mlen
- m
; /* end of word changes */
1512 rl_end
+= mlen
- m
; /* end of line changes */
1513 rl_line_buffer
[rl_end
] = 0;
1526 /* **************************************************************** */
1530 /* **************************************************************** */
1532 /* Transpose the words at point. If point is at the end of the line,
1533 transpose the two words before point. */
1535 rl_transpose_words (int count
, int key
)
1537 char *word1
, *word2
;
1538 int w1_beg
, w1_end
, w2_beg
, w2_end
;
1539 int orig_point
= rl_point
;
1544 /* Find the two words. */
1545 rl_forward_word (count
, key
);
1547 rl_backward_word (1, key
);
1549 rl_backward_word (count
, key
);
1551 rl_forward_word (1, key
);
1554 /* Do some check to make sure that there really are two words. */
1555 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
1558 rl_point
= orig_point
;
1562 /* Get the text of the words. */
1563 word1
= rl_copy_text (w1_beg
, w1_end
);
1564 word2
= rl_copy_text (w2_beg
, w2_end
);
1566 /* We are about to do many insertions and deletions. Remember them
1567 as one operation. */
1568 rl_begin_undo_group ();
1570 /* Do the stuff at word2 first, so that we don't have to worry
1571 about word1 moving. */
1573 rl_delete_text (w2_beg
, w2_end
);
1574 rl_insert_text (word1
);
1577 rl_delete_text (w1_beg
, w1_end
);
1578 rl_insert_text (word2
);
1580 /* This is exactly correct since the text before this point has not
1581 changed in length. */
1584 /* I think that does it. */
1585 rl_end_undo_group ();
1592 /* Transpose the characters at point. If point is at the end of the line,
1593 then transpose the characters before point. */
1595 rl_transpose_chars (int count
, int key
)
1597 #if defined (HANDLE_MULTIBYTE)
1603 int char_length
, prev_point
;
1608 if (!rl_point
|| rl_end
< 2)
1614 rl_begin_undo_group ();
1616 if (rl_point
== rl_end
)
1618 rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1622 prev_point
= rl_point
;
1623 rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1625 #if defined (HANDLE_MULTIBYTE)
1626 char_length
= prev_point
- rl_point
;
1627 dummy
= (char *)xmalloc (char_length
+ 1);
1628 for (i
= 0; i
< char_length
; i
++)
1629 dummy
[i
] = rl_line_buffer
[rl_point
+ i
];
1632 dummy
[0] = rl_line_buffer
[rl_point
];
1633 dummy
[char_length
= 1] = '\0';
1636 rl_delete_text (rl_point
, rl_point
+ char_length
);
1638 rl_point
= _rl_find_next_mbchar (rl_line_buffer
, rl_point
, count
, MB_FIND_NONZERO
);
1641 rl_insert_text (dummy
);
1642 rl_end_undo_group ();
1644 #if defined (HANDLE_MULTIBYTE)
1651 /* **************************************************************** */
1653 /* Character Searching */
1655 /* **************************************************************** */
1658 #if defined (HANDLE_MULTIBYTE)
1659 _rl_char_search_internal (int count
, int dir
, char *smbchar
, int len
)
1661 _rl_char_search_internal (int count
, int dir
, int schar
)
1665 #if defined (HANDLE_MULTIBYTE)
1673 inc
= (dir
< 0) ? -1 : 1;
1676 if ((dir
< 0 && pos
<= 0) || (dir
> 0 && pos
>= rl_end
))
1682 #if defined (HANDLE_MULTIBYTE)
1683 pos
= (inc
> 0) ? _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)
1684 : _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
);
1690 #if defined (HANDLE_MULTIBYTE)
1691 if (_rl_is_mbchar_matched (rl_line_buffer
, pos
, rl_end
, smbchar
, len
))
1693 if (rl_line_buffer
[pos
] == schar
)
1698 rl_point
= (dir
== BTO
) ? _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)
1701 rl_point
= (dir
== FTO
) ? _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
)
1705 #if defined (HANDLE_MULTIBYTE)
1709 #if defined (HANDLE_MULTIBYTE)
1710 while ((dir
< 0) ? (pos
= _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
)) != prepos
1711 : (pos
= _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)) != prepos
);
1713 while ((dir
< 0) ? pos
-- : ++pos
< rl_end
);
1719 /* Search COUNT times for a character read from the current input stream.
1720 FDIR is the direction to search if COUNT is non-negative; otherwise
1721 the search goes in BDIR. So much is dependent on HANDLE_MULTIBYTE
1722 that there are two separate versions of this function. */
1723 #if defined (HANDLE_MULTIBYTE)
1725 _rl_char_search (int count
, int fdir
, int bdir
)
1727 char mbchar
[MB_LEN_MAX
];
1730 mb_len
= _rl_read_mbchar (mbchar
, MB_LEN_MAX
);
1736 return (_rl_char_search_internal (-count
, bdir
, mbchar
, mb_len
));
1738 return (_rl_char_search_internal (count
, fdir
, mbchar
, mb_len
));
1740 #else /* !HANDLE_MULTIBYTE */
1742 _rl_char_search (int count
, int fdir
, int bdir
)
1746 c
= _rl_bracketed_read_key ();
1751 return (_rl_char_search_internal (-count
, bdir
, c
));
1753 return (_rl_char_search_internal (count
, fdir
, c
));
1755 #endif /* !HANDLE_MULTIBYTE */
1757 #if defined (READLINE_CALLBACKS)
1759 _rl_char_search_callback (data
)
1760 _rl_callback_generic_arg
*data
;
1762 _rl_callback_func
= 0;
1763 _rl_want_redisplay
= 1;
1765 return (_rl_char_search (data
->count
, data
->i1
, data
->i2
));
1770 rl_char_search (int count
, int key
)
1772 #if defined (READLINE_CALLBACKS)
1773 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1775 _rl_callback_data
= _rl_callback_data_alloc (count
);
1776 _rl_callback_data
->i1
= FFIND
;
1777 _rl_callback_data
->i2
= BFIND
;
1778 _rl_callback_func
= _rl_char_search_callback
;
1783 return (_rl_char_search (count
, FFIND
, BFIND
));
1787 rl_backward_char_search (int count
, int key
)
1789 #if defined (READLINE_CALLBACKS)
1790 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1792 _rl_callback_data
= _rl_callback_data_alloc (count
);
1793 _rl_callback_data
->i1
= BFIND
;
1794 _rl_callback_data
->i2
= FFIND
;
1795 _rl_callback_func
= _rl_char_search_callback
;
1800 return (_rl_char_search (count
, BFIND
, FFIND
));
1803 /* **************************************************************** */
1805 /* The Mark and the Region. */
1807 /* **************************************************************** */
1809 /* Set the mark at POSITION. */
1811 _rl_set_mark_at_pos (int position
)
1813 if (position
< 0 || position
> rl_end
)
1820 /* A bindable command to set the mark. */
1822 rl_set_mark (int count
, int key
)
1824 return (_rl_set_mark_at_pos (rl_explicit_arg
? count
: rl_point
));
1827 /* Exchange the position of mark and point. */
1829 rl_exchange_point_and_mark (int count
, int key
)
1831 if (rl_mark
> rl_end
)
1837 rl_mark
= 0; /* like _RL_FIX_POINT */
1842 SWAP (rl_point
, rl_mark
);
1843 rl_activate_mark ();
1849 /* Active mark support */
1851 /* Is the region active? */
1852 static int mark_active
= 0;
1854 /* Does the current command want the mark to remain active when it completes? */
1855 int _rl_keep_mark_active
;
1858 rl_keep_mark_active (void)
1860 _rl_keep_mark_active
++;
1864 rl_activate_mark (void)
1867 rl_keep_mark_active ();
1871 rl_deactivate_mark (void)
1877 rl_mark_active_p (void)
1879 return (mark_active
);