1 /* text.c -- text handling commands for readline. */
3 /* Copyright (C) 1987-2021 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 (int, int);
63 static int _rl_char_search (int, int, int);
65 #if defined (READLINE_CALLBACKS)
66 static int _rl_insert_next_callback (_rl_callback_generic_arg
*);
67 static int _rl_char_search_callback (_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
];
100 strncpy (rl_line_buffer
+ rl_point
, string
, l
);
102 /* Remember how to undo this if we aren't undoing something. */
103 if (_rl_doing_an_undo
== 0)
105 /* If possible and desirable, concatenate the undos. */
108 (rl_undo_list
->what
== UNDO_INSERT
) &&
109 (rl_undo_list
->end
== rl_point
) &&
110 (rl_undo_list
->end
- rl_undo_list
->start
< 20))
113 rl_add_undo (UNDO_INSERT
, rl_point
, rl_point
+ l
, (char *)NULL
);
117 rl_line_buffer
[rl_end
] = '\0';
121 /* Delete the string between FROM and TO. FROM is inclusive, TO is not.
122 Returns the number of characters deleted. */
124 rl_delete_text (int from
, int to
)
127 register int diff
, i
;
129 /* Fix it if the caller is confused. */
143 text
= rl_copy_text (from
, to
);
145 /* Some versions of strncpy() can't handle overlapping arguments. */
147 for (i
= from
; i
< rl_end
- diff
; i
++)
148 rl_line_buffer
[i
] = rl_line_buffer
[i
+ diff
];
150 /* Remember how to undo this delete. */
151 if (_rl_doing_an_undo
== 0)
152 rl_add_undo (UNDO_DELETE
, from
, to
, text
);
157 rl_line_buffer
[rl_end
] = '\0';
162 /* Fix up point so that it is within the line boundaries after killing
163 text. If FIX_MARK_TOO is non-zero, the mark is forced within line
166 #define _RL_FIX_POINT(x) \
175 _rl_fix_point (int fix_mark_too
)
177 _RL_FIX_POINT (rl_point
);
179 _RL_FIX_POINT (rl_mark
);
185 _RL_FIX_POINT (rl_mark
);
189 /* Replace the contents of the line buffer between START and END with
190 TEXT. The operation is undoable. To replace the entire line in an
191 undoable mode, use _rl_replace_text(text, 0, rl_end); */
193 _rl_replace_text (const char *text
, int start
, int end
)
198 rl_begin_undo_group ();
200 rl_delete_text (start
, end
+ 1);
203 n
= rl_insert_text (text
);
204 rl_end_undo_group ();
209 /* Replace the current line buffer contents with TEXT. If CLEAR_UNDO is
210 non-zero, we free the current undo list. */
212 rl_replace_line (const char *text
, int clear_undo
)
217 if (len
>= rl_line_buffer_len
)
218 rl_extend_line_buffer (len
);
219 strcpy (rl_line_buffer
, text
);
223 rl_free_undo_list ();
228 /* **************************************************************** */
230 /* Readline character functions */
232 /* **************************************************************** */
234 /* This is not a gap editor, just a stupid line input routine. No hair
235 is involved in writing any of the functions, and none should be. */
239 rl_end is the place in the string that we would place '\0';
240 i.e., it is always safe to place '\0' there.
242 rl_point is the place in the string where the cursor is. Sometimes
243 this is the same as rl_end.
245 Any command that is called interactively receives two arguments.
246 The first is a count: the numeric arg passed to this command.
247 The second is the key which invoked this command.
250 /* **************************************************************** */
252 /* Movement Commands */
254 /* **************************************************************** */
256 /* Note that if you `optimize' the display for these functions, you cannot
257 use said functions in other functions which do not do optimizing display.
258 I.e., you will have to update the data base for rl_redisplay, and you
259 might as well let rl_redisplay do that job. */
261 /* Move forward COUNT bytes. */
263 rl_forward_byte (int count
, int key
)
266 return (rl_backward_byte (-count
, key
));
272 end
= rl_point
+ count
;
273 #if defined (VI_MODE)
274 lend
= rl_end
> 0 ? rl_end
- (VI_COMMAND_MODE()) : rl_end
;
295 _rl_forward_char_internal (int count
)
299 #if defined (HANDLE_MULTIBYTE)
300 point
= _rl_find_next_mbchar (rl_line_buffer
, rl_point
, count
, MB_FIND_NONZERO
);
302 #if defined (VI_MODE)
303 if (point
>= rl_end
&& VI_COMMAND_MODE())
304 point
= _rl_find_prev_mbchar (rl_line_buffer
, rl_end
, MB_FIND_NONZERO
);
310 point
= rl_point
+ count
;
319 _rl_backward_char_internal (int count
)
324 #if defined (HANDLE_MULTIBYTE)
327 while (count
> 0 && point
> 0)
329 point
= _rl_find_prev_mbchar (rl_line_buffer
, point
, MB_FIND_NONZERO
);
333 return 0; /* XXX - rl_ding() here? */
345 #if defined (HANDLE_MULTIBYTE)
346 /* Move forward COUNT characters. */
348 rl_forward_char (int count
, int key
)
352 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
353 return (rl_forward_byte (count
, key
));
356 return (rl_backward_char (-count
, key
));
360 if (rl_point
== rl_end
&& EMACS_MODE())
366 point
= _rl_forward_char_internal (count
);
368 if (rl_point
== point
)
376 #else /* !HANDLE_MULTIBYTE */
378 rl_forward_char (int count
, int key
)
380 return (rl_forward_byte (count
, key
));
382 #endif /* !HANDLE_MULTIBYTE */
384 /* Backwards compatibility. */
386 rl_forward (int count
, int key
)
388 return (rl_forward_char (count
, key
));
391 /* Move backward COUNT bytes. */
393 rl_backward_byte (int count
, int key
)
396 return (rl_forward_byte (-count
, key
));
400 if (rl_point
< count
)
415 #if defined (HANDLE_MULTIBYTE)
416 /* Move backward COUNT characters. */
418 rl_backward_char (int count
, int key
)
422 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
423 return (rl_backward_byte (count
, key
));
426 return (rl_forward_char (-count
, key
));
432 while (count
> 0 && point
> 0)
434 point
= _rl_find_prev_mbchar (rl_line_buffer
, point
, MB_FIND_NONZERO
);
450 rl_backward_char (int count
, int key
)
452 return (rl_backward_byte (count
, key
));
456 /* Backwards compatibility. */
458 rl_backward (int count
, int key
)
460 return (rl_backward_char (count
, key
));
463 /* Move to the beginning of the line. */
465 rl_beg_of_line (int count
, int key
)
471 /* Move to the end of the line. */
473 rl_end_of_line (int count
, int key
)
479 /* Move forward a word. We do what Emacs does. Handles multibyte chars. */
481 rl_forward_word (int count
, int key
)
486 return (rl_backward_word (-count
, key
));
490 if (rl_point
> rl_end
)
492 if (rl_point
== rl_end
)
495 /* If we are not in a word, move forward until we are in one.
496 Then, move forward until we hit a non-alphabetic character. */
497 c
= _rl_char_value (rl_line_buffer
, rl_point
);
499 if (_rl_walphabetic (c
) == 0)
501 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
502 while (rl_point
< rl_end
)
504 c
= _rl_char_value (rl_line_buffer
, rl_point
);
505 if (_rl_walphabetic (c
))
507 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
511 if (rl_point
> rl_end
)
513 if (rl_point
== rl_end
)
516 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
517 while (rl_point
< rl_end
)
519 c
= _rl_char_value (rl_line_buffer
, rl_point
);
520 if (_rl_walphabetic (c
) == 0)
522 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
531 /* Move backward a word. We do what Emacs does. Handles multibyte chars. */
533 rl_backward_word (int count
, int key
)
538 return (rl_forward_word (-count
, key
));
545 /* Like rl_forward_word (), except that we look at the characters
546 just before point. */
548 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
549 c
= _rl_char_value (rl_line_buffer
, p
);
551 if (_rl_walphabetic (c
) == 0)
556 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
557 c
= _rl_char_value (rl_line_buffer
, p
);
558 if (_rl_walphabetic (c
))
566 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
567 c
= _rl_char_value (rl_line_buffer
, p
);
568 if (_rl_walphabetic (c
) == 0)
580 /* Clear the current line. Numeric argument to C-l does this. */
582 rl_refresh_line (int ignore1
, int ignore2
)
585 rl_display_fixed
= 1;
589 /* C-l typed to a line without quoting clears the screen, and then reprints
590 the prompt and the current input line. Given a numeric arg, redraw only
593 rl_clear_screen (int count
, int key
)
597 rl_refresh_line (count
, key
);
601 _rl_clear_screen (0); /* calls termcap function to clear screen */
602 rl_keep_mark_active ();
603 rl_forced_update_display ();
604 rl_display_fixed
= 1;
610 rl_clear_display (int count
, int key
)
612 _rl_clear_screen (1); /* calls termcap function to clear screen and scrollback buffer */
613 rl_forced_update_display ();
614 rl_display_fixed
= 1;
620 rl_previous_screen_line (int count
, int key
)
624 c
= _rl_term_autowrap
? _rl_screenwidth
: (_rl_screenwidth
+ 1);
625 return (rl_backward_char (c
, key
));
629 rl_next_screen_line (int count
, int key
)
633 c
= _rl_term_autowrap
? _rl_screenwidth
: (_rl_screenwidth
+ 1);
634 return (rl_forward_char (c
, key
));
638 rl_skip_csi_sequence (int count
, int key
)
642 RL_SETSTATE (RL_STATE_MOREINPUT
);
645 while (ch
>= 0x20 && ch
< 0x40);
646 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
652 rl_arrow_keys (int count
, int key
)
656 RL_SETSTATE(RL_STATE_MOREINPUT
);
658 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
662 switch (_rl_to_upper (ch
))
665 rl_get_previous_history (count
, ch
);
669 rl_get_next_history (count
, ch
);
673 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
674 rl_forward_char (count
, ch
);
676 rl_forward_byte (count
, ch
);
680 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
681 rl_backward_char (count
, ch
);
683 rl_backward_byte (count
, ch
);
693 /* **************************************************************** */
697 /* **************************************************************** */
699 #ifdef HANDLE_MULTIBYTE
700 static char pending_bytes
[MB_LEN_MAX
];
701 static int pending_bytes_length
= 0;
702 static mbstate_t ps
= {0};
705 /* Insert the character C at the current location, moving point forward.
706 If C introduces a multibyte sequence, we read the whole sequence and
707 then insert the multibyte char into the line buffer. */
709 _rl_insert_char (int count
, int c
)
713 #ifdef HANDLE_MULTIBYTE
715 char incoming
[MB_LEN_MAX
+ 1];
716 int incoming_length
= 0;
718 static int stored_count
= 0;
724 #if defined (HANDLE_MULTIBYTE)
725 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
731 else if (_rl_utf8locale
&& (c
& 0x80) == 0)
742 if (stored_count
<= 0)
743 stored_count
= count
;
745 count
= stored_count
;
748 pending_bytes
[pending_bytes_length
++] = c
;
749 ret
= MBRTOWC (&wc
, pending_bytes
, pending_bytes_length
, &ps
);
751 if (ret
== (size_t)-2)
753 /* Bytes too short to compose character, try to wait for next byte.
754 Restore the state of the byte sequence, because in this case the
755 effect of mbstate is undefined. */
759 else if (ret
== (size_t)-1)
761 /* Invalid byte sequence for the current locale. Treat first byte
762 as a single character. */
763 incoming
[0] = pending_bytes
[0];
766 pending_bytes_length
--;
767 memmove (pending_bytes
, pending_bytes
+ 1, pending_bytes_length
);
768 /* Clear the state of the byte sequence, because in this case the
769 effect of mbstate is undefined. */
770 memset (&ps
, 0, sizeof (mbstate_t));
772 else if (ret
== (size_t)0)
776 pending_bytes_length
--;
777 /* Clear the state of the byte sequence, because in this case the
778 effect of mbstate is undefined. */
779 memset (&ps
, 0, sizeof (mbstate_t));
783 incoming
[0] = pending_bytes
[0];
784 incoming
[incoming_length
= 1] = '\0';
785 pending_bytes_length
= 0;
789 /* We successfully read a single multibyte character. */
790 memcpy (incoming
, pending_bytes
, pending_bytes_length
);
791 incoming
[pending_bytes_length
] = '\0';
792 incoming_length
= pending_bytes_length
;
793 pending_bytes_length
= 0;
796 #endif /* HANDLE_MULTIBYTE */
798 /* If we can optimize, then do it. But don't let people crash
799 readline because of extra large arguments. */
800 if (count
> 1 && count
<= TEXT_COUNT_MAX
)
802 #if defined (HANDLE_MULTIBYTE)
803 string_size
= count
* incoming_length
;
804 string
= (char *)xmalloc (1 + string_size
);
807 while (i
< string_size
)
809 if (incoming_length
== 1)
810 string
[i
++] = *incoming
;
813 strncpy (string
+ i
, incoming
, incoming_length
);
814 i
+= incoming_length
;
819 #else /* !HANDLE_MULTIBYTE */
820 string
= (char *)xmalloc (1 + count
);
822 for (i
= 0; i
< count
; i
++)
824 #endif /* !HANDLE_MULTIBYTE */
827 rl_insert_text (string
);
833 if (count
> TEXT_COUNT_MAX
)
836 #if defined (HANDLE_MULTIBYTE)
837 string_size
= incoming_length
* TEXT_COUNT_MAX
;
838 string
= (char *)xmalloc (1 + string_size
);
841 while (i
< string_size
)
843 if (incoming_length
== 1)
844 string
[i
++] = *incoming
;
847 strncpy (string
+ i
, incoming
, incoming_length
);
848 i
+= incoming_length
;
854 decreaser
= (count
> TEXT_COUNT_MAX
) ? TEXT_COUNT_MAX
: count
;
855 string
[decreaser
*incoming_length
] = '\0';
856 rl_insert_text (string
);
863 #else /* !HANDLE_MULTIBYTE */
864 char str
[TEXT_COUNT_MAX
+1];
866 for (i
= 0; i
< TEXT_COUNT_MAX
; i
++)
871 decreaser
= (count
> TEXT_COUNT_MAX
? TEXT_COUNT_MAX
: count
);
872 str
[decreaser
] = '\0';
873 rl_insert_text (str
);
876 #endif /* !HANDLE_MULTIBYTE */
881 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
883 /* We are inserting a single character.
884 If there is pending input, then make a string of all of the
885 pending characters that are bound to rl_insert, and insert
886 them all. Don't do this if we're current reading input from
888 if ((RL_ISSTATE (RL_STATE_MACROINPUT
) == 0) && _rl_pushed_input_available ())
889 _rl_insert_typein (c
);
892 /* Inserting a single character. */
897 rl_insert_text (str
);
900 #if defined (HANDLE_MULTIBYTE)
903 rl_insert_text (incoming
);
911 /* Overwrite the character at point (or next COUNT characters) with C.
912 If C introduces a multibyte character sequence, read the entire sequence
913 before starting the overwrite loop. */
915 _rl_overwrite_char (int count
, int c
)
918 #if defined (HANDLE_MULTIBYTE)
919 char mbkey
[MB_LEN_MAX
];
922 /* Read an entire multibyte character sequence to insert COUNT times. */
924 if (count
> 0 && MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
925 k
= _rl_read_mbstring (c
, mbkey
, MB_LEN_MAX
);
930 rl_begin_undo_group ();
932 for (i
= 0; i
< count
; i
++)
934 #if defined (HANDLE_MULTIBYTE)
935 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
936 rl_insert_text (mbkey
);
939 _rl_insert_char (1, c
);
941 if (rl_point
< rl_end
)
945 rl_end_undo_group ();
951 rl_insert (int count
, int c
)
955 r
= (rl_insert_mode
== RL_IM_INSERT
) ? _rl_insert_char (count
, c
) : _rl_overwrite_char (count
, c
);
957 /* XXX -- attempt to batch-insert pending input that maps to self-insert */
959 n
= (unsigned short)-2;
960 while (_rl_optimize_typeahead
&&
961 rl_num_chars_to_read
== 0 &&
962 (RL_ISSTATE (RL_STATE_INPUTPENDING
|RL_STATE_MACROINPUT
) == 0) &&
963 _rl_pushed_input_available () == 0 &&
964 _rl_input_queued (0) &&
965 (n
= rl_read_key ()) > 0 &&
966 _rl_keymap
[(unsigned char)n
].type
== ISFUNC
&&
967 _rl_keymap
[(unsigned char)n
].function
== rl_insert
)
969 r
= (rl_insert_mode
== RL_IM_INSERT
) ? _rl_insert_char (1, n
) : _rl_overwrite_char (1, n
);
970 /* _rl_insert_char keeps its own set of pending characters to compose a
971 complete multibyte character, and only returns 1 if it sees a character
972 that's part of a multibyte character but too short to complete one. We
973 can try to read another character in the hopes that we will get the
974 next one or just punt. Right now we try to read another character.
975 We don't want to call rl_insert_next if _rl_insert_char has already
976 stored the character in the pending_bytes array because that will
977 result in doubled input. */
978 n
= (unsigned short)-2;
979 x
++; /* count of bytes of typeahead read, currently unused */
980 if (r
== 1) /* read partial multibyte character */
982 if (rl_done
|| r
!= 0)
986 if (n
!= (unsigned short)-2) /* -2 = sentinel value for having inserted N */
988 /* setting rl_pending_input inhibits setting rl_last_func so we do it
990 rl_last_func
= rl_insert
;
991 _rl_reset_argument ();
992 rl_executing_keyseq
[rl_key_sequence_length
= 0] = '\0';
993 r
= rl_execute_next (n
);
999 /* Insert the next typed character verbatim. */
1001 _rl_insert_next (int count
)
1005 RL_SETSTATE(RL_STATE_MOREINPUT
);
1007 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
1012 if (RL_ISSTATE (RL_STATE_MACRODEF
))
1013 _rl_add_macro_char (c
);
1015 #if defined (HANDLE_SIGNALS)
1016 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
1017 _rl_restore_tty_signals ();
1020 return (_rl_insert_char (count
, c
));
1023 #if defined (READLINE_CALLBACKS)
1025 _rl_insert_next_callback (_rl_callback_generic_arg
*data
)
1029 count
= data
->count
;
1035 r
= _rl_insert_next (1);
1036 _rl_want_redisplay
= 1;
1037 /* If we should keep going, leave the callback function installed */
1038 if (data
->count
< 0 && r
== 0)
1040 count
= 0; /* data->count == 0 || r != 0; force break below */
1043 /* Deregister function, let rl_callback_read_char deallocate data */
1044 _rl_callback_func
= 0;
1045 _rl_want_redisplay
= 1;
1050 return _rl_insert_next (count
);
1055 rl_quoted_insert (int count
, int key
)
1057 /* Let's see...should the callback interface futz with signal handling? */
1058 #if defined (HANDLE_SIGNALS)
1059 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
1060 _rl_disable_tty_signals ();
1063 #if defined (READLINE_CALLBACKS)
1064 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1066 _rl_callback_data
= _rl_callback_data_alloc (count
);
1067 _rl_callback_func
= _rl_insert_next_callback
;
1072 /* A negative count means to quote the next -COUNT characters. */
1078 r
= _rl_insert_next (1);
1079 while (r
== 0 && ++count
< 0);
1083 return _rl_insert_next (count
);
1086 /* Insert a tab character. */
1088 rl_tab_insert (int count
, int key
)
1090 return (_rl_insert_char (count
, '\t'));
1093 /* What to do when a NEWLINE is pressed. We accept the whole line.
1094 KEY is the key that invoked this command. I guess it could have
1095 meaning in the future. */
1097 rl_newline (int count
, int key
)
1099 if (rl_mark_active_p ())
1101 rl_deactivate_mark ();
1102 (*rl_redisplay_function
) ();
1103 _rl_want_redisplay
= 0;
1108 if (_rl_history_preserve_point
)
1109 _rl_history_saved_point
= (rl_point
== rl_end
) ? -1 : rl_point
;
1111 RL_SETSTATE(RL_STATE_DONE
);
1113 #if defined (VI_MODE)
1114 if (rl_editing_mode
== vi_mode
)
1116 _rl_vi_done_inserting ();
1117 if (_rl_vi_textmod_command (_rl_vi_last_command
) == 0) /* XXX */
1118 _rl_vi_reset_last ();
1120 #endif /* VI_MODE */
1122 /* If we've been asked to erase empty lines, suppress the final update,
1123 since _rl_update_final calls rl_crlf(). */
1124 if (rl_erase_empty_line
&& rl_point
== 0 && rl_end
== 0)
1128 _rl_update_final ();
1132 /* What to do for some uppercase characters, like meta characters,
1133 and some characters appearing in emacs_ctlx_keymap. This function
1134 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1135 is special cased. */
1137 rl_do_lowercase_version (int ignore1
, int ignore2
)
1139 return 99999; /* prevent from being combined with _rl_null_function */
1142 /* This is different from what vi does, so the code's not shared. Emacs
1143 rubout in overwrite mode has one oddity: it replaces a control
1144 character that's displayed as two characters (^X) with two spaces. */
1146 _rl_overwrite_rubout (int count
, int key
)
1159 /* L == number of spaces to insert */
1160 for (i
= l
= 0; i
< count
; i
++)
1162 rl_backward_char (1, key
);
1163 l
+= rl_character_len (rl_line_buffer
[rl_point
], rl_point
); /* not exactly right */
1166 rl_begin_undo_group ();
1168 if (count
> 1 || rl_explicit_arg
)
1169 rl_kill_text (opoint
, rl_point
);
1171 rl_delete_text (opoint
, rl_point
);
1173 /* Emacs puts point at the beginning of the sequence of spaces. */
1174 if (rl_point
< rl_end
)
1177 _rl_insert_char (l
, ' ');
1181 rl_end_undo_group ();
1186 /* Rubout the character behind point. */
1188 rl_rubout (int count
, int key
)
1191 return (rl_delete (-count
, key
));
1199 if (rl_insert_mode
== RL_IM_OVERWRITE
)
1200 return (_rl_overwrite_rubout (count
, key
));
1202 return (_rl_rubout_char (count
, key
));
1206 _rl_rubout_char (int count
, int key
)
1211 /* Duplicated code because this is called from other parts of the library. */
1213 return (rl_delete (-count
, key
));
1221 orig_point
= rl_point
;
1222 if (count
> 1 || rl_explicit_arg
)
1224 rl_backward_char (count
, key
);
1225 rl_kill_text (orig_point
, rl_point
);
1227 else if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
1229 c
= rl_line_buffer
[--rl_point
];
1230 rl_delete_text (rl_point
, orig_point
);
1231 /* The erase-at-end-of-line hack is of questionable merit now. */
1232 if (rl_point
== rl_end
&& ISPRINT ((unsigned char)c
) && _rl_last_c_pos
)
1235 l
= rl_character_len (c
, rl_point
);
1236 _rl_erase_at_end_of_line (l
);
1241 rl_point
= _rl_find_prev_mbchar (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1242 rl_delete_text (rl_point
, orig_point
);
1248 /* Delete the character under the cursor. Given a numeric argument,
1249 kill that many characters instead. */
1251 rl_delete (int count
, int key
)
1256 return (_rl_rubout_char (-count
, key
));
1258 if (rl_point
== rl_end
)
1264 if (count
> 1 || rl_explicit_arg
)
1267 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1268 rl_forward_char (count
, key
);
1270 rl_forward_byte (count
, key
);
1272 rl_kill_text (xpoint
, rl_point
);
1277 xpoint
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
1278 rl_delete_text (rl_point
, xpoint
);
1283 /* Delete the character under the cursor, unless the insertion
1284 point is at the end of the line, in which case the character
1285 behind the cursor is deleted. COUNT is obeyed and may be used
1286 to delete forward or backward that many characters. */
1288 rl_rubout_or_delete (int count
, int key
)
1290 if (rl_end
!= 0 && rl_point
== rl_end
)
1291 return (_rl_rubout_char (count
, key
));
1293 return (rl_delete (count
, key
));
1296 /* Delete all spaces and tabs around point. */
1298 rl_delete_horizontal_space (int count
, int ignore
)
1302 while (rl_point
&& whitespace (rl_line_buffer
[rl_point
- 1]))
1307 while (rl_point
< rl_end
&& whitespace (rl_line_buffer
[rl_point
]))
1310 if (start
!= rl_point
)
1312 rl_delete_text (start
, rl_point
);
1322 /* Like the tcsh editing function delete-char-or-list. The eof character
1323 is caught before this is invoked, so this really does the same thing as
1324 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1326 rl_delete_or_show_completions (int count
, int key
)
1328 if (rl_end
!= 0 && rl_point
== rl_end
)
1329 return (rl_possible_completions (count
, key
));
1331 return (rl_delete (count
, key
));
1334 #ifndef RL_COMMENT_BEGIN_DEFAULT
1335 #define RL_COMMENT_BEGIN_DEFAULT "#"
1338 /* Turn the current line into a comment in shell history.
1339 A K*rn shell style function. */
1341 rl_insert_comment (int count
, int key
)
1343 char *rl_comment_text
;
1346 rl_beg_of_line (1, key
);
1347 rl_comment_text
= _rl_comment_begin
? _rl_comment_begin
: RL_COMMENT_BEGIN_DEFAULT
;
1349 if (rl_explicit_arg
== 0)
1350 rl_insert_text (rl_comment_text
);
1353 rl_comment_len
= strlen (rl_comment_text
);
1354 if (STREQN (rl_comment_text
, rl_line_buffer
, rl_comment_len
))
1355 rl_delete_text (rl_point
, rl_point
+ rl_comment_len
);
1357 rl_insert_text (rl_comment_text
);
1360 (*rl_redisplay_function
) ();
1361 rl_newline (1, '\n');
1366 /* **************************************************************** */
1370 /* **************************************************************** */
1372 /* The three kinds of things that we know how to do. */
1377 /* Uppercase the word at point. */
1379 rl_upcase_word (int count
, int key
)
1381 return (rl_change_case (count
, UpCase
));
1384 /* Lowercase the word at point. */
1386 rl_downcase_word (int count
, int key
)
1388 return (rl_change_case (count
, DownCase
));
1391 /* Upcase the first letter, downcase the rest. */
1393 rl_capitalize_word (int count
, int key
)
1395 return (rl_change_case (count
, CapCase
));
1398 /* The meaty function.
1399 Change the case of COUNT words, performing OP on them.
1400 OP is one of UpCase, DownCase, or CapCase.
1401 If a negative argument is given, leave point where it started,
1402 otherwise, leave it where it moves to. */
1404 rl_change_case (int count
, int op
)
1406 int start
, next
, end
;
1407 int inword
, nc
, nop
;
1409 #if defined (HANDLE_MULTIBYTE)
1411 char mb
[MB_LEN_MAX
+1];
1418 rl_forward_word (count
, 0);
1421 if (op
!= UpCase
&& op
!= DownCase
&& op
!= CapCase
)
1430 #if defined (HANDLE_MULTIBYTE)
1431 memset (&mps
, 0, sizeof (mbstate_t));
1434 /* We are going to modify some text, so let's prepare to undo it. */
1435 rl_modifying (start
, end
);
1440 c
= _rl_char_value (rl_line_buffer
, start
);
1441 /* This assumes that the upper and lower case versions are the same width. */
1442 next
= MB_NEXTCHAR (rl_line_buffer
, start
, 1, MB_FIND_NONZERO
);
1444 if (_rl_walphabetic (c
) == 0)
1453 nop
= inword
? DownCase
: UpCase
;
1458 /* Can't check isascii here; some languages (e.g, Turkish) have
1459 multibyte upper and lower case equivalents of single-byte ascii
1461 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
1463 nc
= (nop
== UpCase
) ? _rl_to_upper (c
) : _rl_to_lower (c
);
1464 rl_line_buffer
[start
] = nc
;
1466 #if defined (HANDLE_MULTIBYTE)
1469 m
= MBRTOWC (&wc
, rl_line_buffer
+ start
, end
- start
, &mps
);
1470 if (MB_INVALIDCH (m
))
1471 wc
= (WCHAR_T
)rl_line_buffer
[start
];
1472 else if (MB_NULLWCH (m
))
1474 nwc
= (nop
== UpCase
) ? _rl_to_wupper (wc
) : _rl_to_wlower (wc
);
1475 if (nwc
!= wc
) /* just skip unchanged characters */
1480 memset (&ts
, 0, sizeof (mbstate_t));
1481 mlen
= WCRTOMB (mb
, nwc
, &ts
);
1485 memset (&ts
, 0, sizeof (mbstate_t));
1486 mlen
= WCRTOMB (mb
, nwc
, &ts
);
1487 if (mlen
< 0) /* should not happen */
1488 strncpy (mb
, rl_line_buffer
+ start
, mlen
= m
);
1492 /* what to do if m != mlen? adjust below */
1493 /* m == length of old char, mlen == length of new char */
1494 s
= rl_line_buffer
+ start
;
1495 e
= rl_line_buffer
+ rl_end
;
1497 memcpy (s
, mb
, mlen
);
1500 memcpy (s
, mb
, mlen
);
1501 memmove (s
+ mlen
, s
+ m
, (e
- s
) - m
);
1502 next
-= m
- mlen
; /* next char changes */
1503 end
-= m
- mlen
; /* end of word changes */
1504 rl_end
-= m
- mlen
; /* end of line changes */
1505 rl_line_buffer
[rl_end
] = 0;
1509 rl_extend_line_buffer (rl_end
+ mlen
+ (e
- s
) - m
+ 2);
1510 s
= rl_line_buffer
+ start
; /* have to redo this */
1511 e
= rl_line_buffer
+ rl_end
;
1512 memmove (s
+ mlen
, s
+ m
, (e
- s
) - m
);
1513 memcpy (s
, mb
, mlen
);
1514 next
+= mlen
- m
; /* next char changes */
1515 end
+= mlen
- m
; /* end of word changes */
1516 rl_end
+= mlen
- m
; /* end of line changes */
1517 rl_line_buffer
[rl_end
] = 0;
1530 /* **************************************************************** */
1534 /* **************************************************************** */
1536 /* Transpose the words at point. If point is at the end of the line,
1537 transpose the two words before point. */
1539 rl_transpose_words (int count
, int key
)
1541 char *word1
, *word2
;
1542 int w1_beg
, w1_end
, w2_beg
, w2_end
;
1543 int orig_point
, orig_end
;
1545 orig_point
= rl_point
;
1551 /* Find the two words. */
1552 rl_forward_word (count
, key
);
1554 rl_backward_word (1, key
);
1556 rl_backward_word (count
, key
);
1558 rl_forward_word (1, key
);
1561 /* Do some check to make sure that there really are two words. */
1562 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
1565 rl_point
= orig_point
;
1569 /* Get the text of the words. */
1570 word1
= rl_copy_text (w1_beg
, w1_end
);
1571 word2
= rl_copy_text (w2_beg
, w2_end
);
1573 /* We are about to do many insertions and deletions. Remember them
1574 as one operation. */
1575 rl_begin_undo_group ();
1577 /* Do the stuff at word2 first, so that we don't have to worry
1578 about word1 moving. */
1580 rl_delete_text (w2_beg
, w2_end
);
1581 rl_insert_text (word1
);
1584 rl_delete_text (w1_beg
, w1_end
);
1585 rl_insert_text (word2
);
1587 /* This is exactly correct since the text before this point has not
1588 changed in length. */
1590 rl_end
= orig_end
; /* just make sure */
1592 /* I think that does it. */
1593 rl_end_undo_group ();
1600 /* Transpose the characters at point. If point is at the end of the line,
1601 then transpose the characters before point. */
1603 rl_transpose_chars (int count
, int key
)
1605 #if defined (HANDLE_MULTIBYTE)
1611 int char_length
, prev_point
;
1616 if (!rl_point
|| rl_end
< 2)
1622 rl_begin_undo_group ();
1624 if (rl_point
== rl_end
)
1626 rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1630 prev_point
= rl_point
;
1631 rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1633 #if defined (HANDLE_MULTIBYTE)
1634 char_length
= prev_point
- rl_point
;
1635 dummy
= (char *)xmalloc (char_length
+ 1);
1636 for (i
= 0; i
< char_length
; i
++)
1637 dummy
[i
] = rl_line_buffer
[rl_point
+ i
];
1640 dummy
[0] = rl_line_buffer
[rl_point
];
1641 dummy
[char_length
= 1] = '\0';
1644 rl_delete_text (rl_point
, rl_point
+ char_length
);
1646 rl_point
= _rl_find_next_mbchar (rl_line_buffer
, rl_point
, count
, MB_FIND_NONZERO
);
1649 rl_insert_text (dummy
);
1650 rl_end_undo_group ();
1652 #if defined (HANDLE_MULTIBYTE)
1659 /* **************************************************************** */
1661 /* Character Searching */
1663 /* **************************************************************** */
1666 #if defined (HANDLE_MULTIBYTE)
1667 _rl_char_search_internal (int count
, int dir
, char *smbchar
, int len
)
1669 _rl_char_search_internal (int count
, int dir
, int schar
)
1673 #if defined (HANDLE_MULTIBYTE)
1681 inc
= (dir
< 0) ? -1 : 1;
1684 if ((dir
< 0 && pos
<= 0) || (dir
> 0 && pos
>= rl_end
))
1690 #if defined (HANDLE_MULTIBYTE)
1691 pos
= (inc
> 0) ? _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)
1692 : _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
);
1698 #if defined (HANDLE_MULTIBYTE)
1699 if (_rl_is_mbchar_matched (rl_line_buffer
, pos
, rl_end
, smbchar
, len
))
1701 if (rl_line_buffer
[pos
] == schar
)
1706 rl_point
= (dir
== BTO
) ? _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)
1709 rl_point
= (dir
== FTO
) ? _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
)
1713 #if defined (HANDLE_MULTIBYTE)
1717 #if defined (HANDLE_MULTIBYTE)
1718 while ((dir
< 0) ? (pos
= _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
)) != prepos
1719 : (pos
= _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)) != prepos
);
1721 while ((dir
< 0) ? pos
-- : ++pos
< rl_end
);
1727 /* Search COUNT times for a character read from the current input stream.
1728 FDIR is the direction to search if COUNT is non-negative; otherwise
1729 the search goes in BDIR. So much is dependent on HANDLE_MULTIBYTE
1730 that there are two separate versions of this function. */
1731 #if defined (HANDLE_MULTIBYTE)
1733 _rl_char_search (int count
, int fdir
, int bdir
)
1735 char mbchar
[MB_LEN_MAX
];
1738 mb_len
= _rl_read_mbchar (mbchar
, MB_LEN_MAX
);
1744 return (_rl_char_search_internal (-count
, bdir
, mbchar
, mb_len
));
1746 return (_rl_char_search_internal (count
, fdir
, mbchar
, mb_len
));
1748 #else /* !HANDLE_MULTIBYTE */
1750 _rl_char_search (int count
, int fdir
, int bdir
)
1754 c
= _rl_bracketed_read_key ();
1759 return (_rl_char_search_internal (-count
, bdir
, c
));
1761 return (_rl_char_search_internal (count
, fdir
, c
));
1763 #endif /* !HANDLE_MULTIBYTE */
1765 #if defined (READLINE_CALLBACKS)
1767 _rl_char_search_callback (data
)
1768 _rl_callback_generic_arg
*data
;
1770 _rl_callback_func
= 0;
1771 _rl_want_redisplay
= 1;
1773 return (_rl_char_search (data
->count
, data
->i1
, data
->i2
));
1778 rl_char_search (int count
, int key
)
1780 #if defined (READLINE_CALLBACKS)
1781 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1783 _rl_callback_data
= _rl_callback_data_alloc (count
);
1784 _rl_callback_data
->i1
= FFIND
;
1785 _rl_callback_data
->i2
= BFIND
;
1786 _rl_callback_func
= _rl_char_search_callback
;
1791 return (_rl_char_search (count
, FFIND
, BFIND
));
1795 rl_backward_char_search (int count
, int key
)
1797 #if defined (READLINE_CALLBACKS)
1798 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1800 _rl_callback_data
= _rl_callback_data_alloc (count
);
1801 _rl_callback_data
->i1
= BFIND
;
1802 _rl_callback_data
->i2
= FFIND
;
1803 _rl_callback_func
= _rl_char_search_callback
;
1808 return (_rl_char_search (count
, BFIND
, FFIND
));
1811 /* **************************************************************** */
1813 /* The Mark and the Region. */
1815 /* **************************************************************** */
1817 /* Set the mark at POSITION. */
1819 _rl_set_mark_at_pos (int position
)
1821 if (position
< 0 || position
> rl_end
)
1828 /* A bindable command to set the mark. */
1830 rl_set_mark (int count
, int key
)
1832 return (_rl_set_mark_at_pos (rl_explicit_arg
? count
: rl_point
));
1835 /* Exchange the position of mark and point. */
1837 rl_exchange_point_and_mark (int count
, int key
)
1839 if (rl_mark
> rl_end
)
1845 rl_mark
= 0; /* like _RL_FIX_POINT */
1850 SWAP (rl_point
, rl_mark
);
1851 rl_activate_mark ();
1857 /* Active mark support */
1859 /* Is the region active? */
1860 static int mark_active
= 0;
1862 /* Does the current command want the mark to remain active when it completes? */
1863 int _rl_keep_mark_active
;
1866 rl_keep_mark_active (void)
1868 _rl_keep_mark_active
++;
1872 rl_activate_mark (void)
1875 rl_keep_mark_active ();
1879 rl_deactivate_mark (void)
1885 rl_mark_active_p (void)
1887 return (mark_active
);