1 /* text.c -- text handling commands for readline. */
3 /* Copyright (C) 1987-2017 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';
160 /* Fix up point so that it is within the line boundaries after killing
161 text. If FIX_MARK_TOO is non-zero, the mark is forced within line
164 #define _RL_FIX_POINT(x) \
173 _rl_fix_point (int fix_mark_too
)
175 _RL_FIX_POINT (rl_point
);
177 _RL_FIX_POINT (rl_mark
);
181 /* Replace the contents of the line buffer between START and END with
182 TEXT. The operation is undoable. To replace the entire line in an
183 undoable mode, use _rl_replace_text(text, 0, rl_end); */
185 _rl_replace_text (const char *text
, int start
, int end
)
190 rl_begin_undo_group ();
192 rl_delete_text (start
, end
+ 1);
195 n
= rl_insert_text (text
);
196 rl_end_undo_group ();
201 /* Replace the current line buffer contents with TEXT. If CLEAR_UNDO is
202 non-zero, we free the current undo list. */
204 rl_replace_line (const char *text
, int clear_undo
)
209 if (len
>= rl_line_buffer_len
)
210 rl_extend_line_buffer (len
);
211 strcpy (rl_line_buffer
, text
);
215 rl_free_undo_list ();
220 /* **************************************************************** */
222 /* Readline character functions */
224 /* **************************************************************** */
226 /* This is not a gap editor, just a stupid line input routine. No hair
227 is involved in writing any of the functions, and none should be. */
231 rl_end is the place in the string that we would place '\0';
232 i.e., it is always safe to place '\0' there.
234 rl_point is the place in the string where the cursor is. Sometimes
235 this is the same as rl_end.
237 Any command that is called interactively receives two arguments.
238 The first is a count: the numeric arg passed to this command.
239 The second is the key which invoked this command.
242 /* **************************************************************** */
244 /* Movement Commands */
246 /* **************************************************************** */
248 /* Note that if you `optimize' the display for these functions, you cannot
249 use said functions in other functions which do not do optimizing display.
250 I.e., you will have to update the data base for rl_redisplay, and you
251 might as well let rl_redisplay do that job. */
253 /* Move forward COUNT bytes. */
255 rl_forward_byte (int count
, int key
)
258 return (rl_backward_byte (-count
, key
));
264 end
= rl_point
+ count
;
265 #if defined (VI_MODE)
266 lend
= rl_end
> 0 ? rl_end
- (VI_COMMAND_MODE()) : rl_end
;
287 _rl_forward_char_internal (int count
)
291 #if defined (HANDLE_MULTIBYTE)
292 point
= _rl_find_next_mbchar (rl_line_buffer
, rl_point
, count
, MB_FIND_NONZERO
);
294 #if defined (VI_MODE)
295 if (point
>= rl_end
&& VI_COMMAND_MODE())
296 point
= _rl_find_prev_mbchar (rl_line_buffer
, rl_end
, MB_FIND_NONZERO
);
302 point
= rl_point
+ count
;
311 _rl_backward_char_internal (int count
)
316 #if defined (HANDLE_MULTIBYTE)
319 while (count
> 0 && point
> 0)
321 point
= _rl_find_prev_mbchar (rl_line_buffer
, point
, MB_FIND_NONZERO
);
325 return 0; /* XXX - rl_ding() here? */
337 #if defined (HANDLE_MULTIBYTE)
338 /* Move forward COUNT characters. */
340 rl_forward_char (int count
, int key
)
344 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
345 return (rl_forward_byte (count
, key
));
348 return (rl_backward_char (-count
, key
));
352 if (rl_point
== rl_end
&& EMACS_MODE())
358 point
= _rl_forward_char_internal (count
);
360 if (rl_point
== point
)
368 #else /* !HANDLE_MULTIBYTE */
370 rl_forward_char (int count
, int key
)
372 return (rl_forward_byte (count
, key
));
374 #endif /* !HANDLE_MULTIBYTE */
376 /* Backwards compatibility. */
378 rl_forward (int count
, int key
)
380 return (rl_forward_char (count
, key
));
383 /* Move backward COUNT bytes. */
385 rl_backward_byte (int count
, int key
)
388 return (rl_forward_byte (-count
, key
));
392 if (rl_point
< count
)
407 #if defined (HANDLE_MULTIBYTE)
408 /* Move backward COUNT characters. */
410 rl_backward_char (int count
, int key
)
414 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
415 return (rl_backward_byte (count
, key
));
418 return (rl_forward_char (-count
, key
));
424 while (count
> 0 && point
> 0)
426 point
= _rl_find_prev_mbchar (rl_line_buffer
, point
, MB_FIND_NONZERO
);
442 rl_backward_char (int count
, int key
)
444 return (rl_backward_byte (count
, key
));
448 /* Backwards compatibility. */
450 rl_backward (int count
, int key
)
452 return (rl_backward_char (count
, key
));
455 /* Move to the beginning of the line. */
457 rl_beg_of_line (int count
, int key
)
463 /* Move to the end of the line. */
465 rl_end_of_line (int count
, int key
)
471 /* Move forward a word. We do what Emacs does. Handles multibyte chars. */
473 rl_forward_word (int count
, int key
)
478 return (rl_backward_word (-count
, key
));
482 if (rl_point
== rl_end
)
485 /* If we are not in a word, move forward until we are in one.
486 Then, move forward until we hit a non-alphabetic character. */
487 c
= _rl_char_value (rl_line_buffer
, rl_point
);
489 if (_rl_walphabetic (c
) == 0)
491 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
492 while (rl_point
< rl_end
)
494 c
= _rl_char_value (rl_line_buffer
, rl_point
);
495 if (_rl_walphabetic (c
))
497 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
501 if (rl_point
== rl_end
)
504 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
505 while (rl_point
< rl_end
)
507 c
= _rl_char_value (rl_line_buffer
, rl_point
);
508 if (_rl_walphabetic (c
) == 0)
510 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
519 /* Move backward a word. We do what Emacs does. Handles multibyte chars. */
521 rl_backward_word (int count
, int key
)
526 return (rl_forward_word (-count
, key
));
533 /* Like rl_forward_word (), except that we look at the characters
534 just before point. */
536 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
537 c
= _rl_char_value (rl_line_buffer
, p
);
539 if (_rl_walphabetic (c
) == 0)
544 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
545 c
= _rl_char_value (rl_line_buffer
, p
);
546 if (_rl_walphabetic (c
))
554 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
555 c
= _rl_char_value (rl_line_buffer
, p
);
556 if (_rl_walphabetic (c
) == 0)
568 /* Clear the current line. Numeric argument to C-l does this. */
570 rl_refresh_line (int ignore1
, int ignore2
)
574 curr_line
= _rl_current_display_line ();
576 _rl_move_vert (curr_line
);
577 _rl_move_cursor_relative (0, rl_line_buffer
); /* XXX is this right */
579 _rl_clear_to_eol (0); /* arg of 0 means to not use spaces */
581 rl_redraw_prompt_last_line ();
582 rl_display_fixed
= 1;
587 /* C-l typed to a line without quoting clears the screen, and then reprints
588 the prompt and the current input line. Given a numeric arg, redraw only
591 rl_clear_screen (int count
, int key
)
595 rl_refresh_line (count
, key
);
599 _rl_clear_screen (); /* calls termcap function to clear screen */
600 rl_forced_update_display ();
601 rl_display_fixed
= 1;
607 rl_previous_screen_line (int count
, int key
)
611 c
= _rl_term_autowrap
? _rl_screenwidth
: (_rl_screenwidth
+ 1);
612 return (rl_backward_char (c
, key
));
616 rl_next_screen_line (int count
, int key
)
620 c
= _rl_term_autowrap
? _rl_screenwidth
: (_rl_screenwidth
+ 1);
621 return (rl_forward_char (c
, key
));
625 rl_skip_csi_sequence (int count
, int key
)
629 RL_SETSTATE (RL_STATE_MOREINPUT
);
632 while (ch
>= 0x20 && ch
< 0x40);
633 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
639 rl_arrow_keys (int count
, int key
)
643 RL_SETSTATE(RL_STATE_MOREINPUT
);
645 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
649 switch (_rl_to_upper (ch
))
652 rl_get_previous_history (count
, ch
);
656 rl_get_next_history (count
, ch
);
660 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
661 rl_forward_char (count
, ch
);
663 rl_forward_byte (count
, ch
);
667 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
668 rl_backward_char (count
, ch
);
670 rl_backward_byte (count
, ch
);
680 /* **************************************************************** */
684 /* **************************************************************** */
686 #ifdef HANDLE_MULTIBYTE
687 static char pending_bytes
[MB_LEN_MAX
];
688 static int pending_bytes_length
= 0;
689 static mbstate_t ps
= {0};
692 /* Insert the character C at the current location, moving point forward.
693 If C introduces a multibyte sequence, we read the whole sequence and
694 then insert the multibyte char into the line buffer. */
696 _rl_insert_char (int count
, int c
)
700 #ifdef HANDLE_MULTIBYTE
702 char incoming
[MB_LEN_MAX
+ 1];
703 int incoming_length
= 0;
705 static int stored_count
= 0;
711 #if defined (HANDLE_MULTIBYTE)
712 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
718 else if (_rl_utf8locale
&& (c
& 0x80) == 0)
729 if (stored_count
<= 0)
730 stored_count
= count
;
732 count
= stored_count
;
735 pending_bytes
[pending_bytes_length
++] = c
;
736 ret
= mbrtowc (&wc
, pending_bytes
, pending_bytes_length
, &ps
);
738 if (ret
== (size_t)-2)
740 /* Bytes too short to compose character, try to wait for next byte.
741 Restore the state of the byte sequence, because in this case the
742 effect of mbstate is undefined. */
746 else if (ret
== (size_t)-1)
748 /* Invalid byte sequence for the current locale. Treat first byte
749 as a single character. */
750 incoming
[0] = pending_bytes
[0];
753 pending_bytes_length
--;
754 memmove (pending_bytes
, pending_bytes
+ 1, pending_bytes_length
);
755 /* Clear the state of the byte sequence, because in this case the
756 effect of mbstate is undefined. */
757 memset (&ps
, 0, sizeof (mbstate_t));
759 else if (ret
== (size_t)0)
763 pending_bytes_length
--;
764 /* Clear the state of the byte sequence, because in this case the
765 effect of mbstate is undefined. */
766 memset (&ps
, 0, sizeof (mbstate_t));
770 incoming
[0] = pending_bytes
[0];
771 incoming
[incoming_length
= 1] = '\0';
772 pending_bytes_length
= 0;
776 /* We successfully read a single multibyte character. */
777 memcpy (incoming
, pending_bytes
, pending_bytes_length
);
778 incoming
[pending_bytes_length
] = '\0';
779 incoming_length
= pending_bytes_length
;
780 pending_bytes_length
= 0;
783 #endif /* HANDLE_MULTIBYTE */
785 /* If we can optimize, then do it. But don't let people crash
786 readline because of extra large arguments. */
787 if (count
> 1 && count
<= TEXT_COUNT_MAX
)
789 #if defined (HANDLE_MULTIBYTE)
790 string_size
= count
* incoming_length
;
791 string
= (char *)xmalloc (1 + string_size
);
794 while (i
< string_size
)
796 if (incoming_length
== 1)
797 string
[i
++] = *incoming
;
800 strncpy (string
+ i
, incoming
, incoming_length
);
801 i
+= incoming_length
;
806 #else /* !HANDLE_MULTIBYTE */
807 string
= (char *)xmalloc (1 + count
);
809 for (i
= 0; i
< count
; i
++)
811 #endif /* !HANDLE_MULTIBYTE */
814 rl_insert_text (string
);
820 if (count
> TEXT_COUNT_MAX
)
823 #if defined (HANDLE_MULTIBYTE)
824 string_size
= incoming_length
* TEXT_COUNT_MAX
;
825 string
= (char *)xmalloc (1 + string_size
);
828 while (i
< string_size
)
830 if (incoming_length
== 1)
831 string
[i
++] = *incoming
;
834 strncpy (string
+ i
, incoming
, incoming_length
);
835 i
+= incoming_length
;
841 decreaser
= (count
> TEXT_COUNT_MAX
) ? TEXT_COUNT_MAX
: count
;
842 string
[decreaser
*incoming_length
] = '\0';
843 rl_insert_text (string
);
850 #else /* !HANDLE_MULTIBYTE */
851 char str
[TEXT_COUNT_MAX
+1];
853 for (i
= 0; i
< TEXT_COUNT_MAX
; i
++)
858 decreaser
= (count
> TEXT_COUNT_MAX
? TEXT_COUNT_MAX
: count
);
859 str
[decreaser
] = '\0';
860 rl_insert_text (str
);
863 #endif /* !HANDLE_MULTIBYTE */
868 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
870 /* We are inserting a single character.
871 If there is pending input, then make a string of all of the
872 pending characters that are bound to rl_insert, and insert
873 them all. Don't do this if we're current reading input from
875 if ((RL_ISSTATE (RL_STATE_MACROINPUT
) == 0) && _rl_pushed_input_available ())
876 _rl_insert_typein (c
);
879 /* Inserting a single character. */
884 rl_insert_text (str
);
887 #if defined (HANDLE_MULTIBYTE)
890 rl_insert_text (incoming
);
898 /* Overwrite the character at point (or next COUNT characters) with C.
899 If C introduces a multibyte character sequence, read the entire sequence
900 before starting the overwrite loop. */
902 _rl_overwrite_char (int count
, int c
)
905 #if defined (HANDLE_MULTIBYTE)
906 char mbkey
[MB_LEN_MAX
];
909 /* Read an entire multibyte character sequence to insert COUNT times. */
910 if (count
> 0 && MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
911 k
= _rl_read_mbstring (c
, mbkey
, MB_LEN_MAX
);
914 rl_begin_undo_group ();
916 for (i
= 0; i
< count
; i
++)
918 #if defined (HANDLE_MULTIBYTE)
919 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
920 rl_insert_text (mbkey
);
923 _rl_insert_char (1, c
);
925 if (rl_point
< rl_end
)
929 rl_end_undo_group ();
935 rl_insert (int count
, int c
)
939 r
= (rl_insert_mode
== RL_IM_INSERT
) ? _rl_insert_char (count
, c
) : _rl_overwrite_char (count
, c
);
941 /* XXX -- attempt to batch-insert pending input that maps to self-insert */
943 n
= (unsigned short)-2;
944 while (_rl_optimize_typeahead
&&
945 rl_num_chars_to_read
== 0 &&
946 (RL_ISSTATE (RL_STATE_INPUTPENDING
|RL_STATE_MACROINPUT
) == 0) &&
947 _rl_pushed_input_available () == 0 &&
948 _rl_input_queued (0) &&
949 (n
= rl_read_key ()) > 0 &&
950 _rl_keymap
[(unsigned char)n
].type
== ISFUNC
&&
951 _rl_keymap
[(unsigned char)n
].function
== rl_insert
)
953 r
= (rl_insert_mode
== RL_IM_INSERT
) ? _rl_insert_char (1, n
) : _rl_overwrite_char (1, n
);
954 /* _rl_insert_char keeps its own set of pending characters to compose a
955 complete multibyte character, and only returns 1 if it sees a character
956 that's part of a multibyte character but too short to complete one. We
957 can try to read another character in the hopes that we will get the
958 next one or just punt. Right now we try to read another character.
959 We don't want to call rl_insert_next if _rl_insert_char has already
960 stored the character in the pending_bytes array because that will
961 result in doubled input. */
962 n
= (unsigned short)-2;
963 x
++; /* count of bytes of typeahead read, currently unused */
964 if (r
== 1) /* read partial multibyte character */
966 if (rl_done
|| r
!= 0)
970 if (n
!= (unsigned short)-2) /* -2 = sentinel value for having inserted N */
972 /* setting rl_pending_input inhibits setting rl_last_func so we do it
974 rl_last_func
= rl_insert
;
975 _rl_reset_argument ();
976 rl_executing_keyseq
[rl_key_sequence_length
= 0] = '\0';
977 r
= rl_execute_next (n
);
983 /* Insert the next typed character verbatim. */
985 _rl_insert_next (int count
)
989 RL_SETSTATE(RL_STATE_MOREINPUT
);
991 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
996 if (RL_ISSTATE (RL_STATE_MACRODEF
))
997 _rl_add_macro_char (c
);
999 #if defined (HANDLE_SIGNALS)
1000 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
1001 _rl_restore_tty_signals ();
1004 return (_rl_insert_char (count
, c
));
1007 #if defined (READLINE_CALLBACKS)
1009 _rl_insert_next_callback (_rl_callback_generic_arg
*data
)
1013 count
= data
->count
;
1019 r
= _rl_insert_next (1);
1020 _rl_want_redisplay
= 1;
1021 /* If we should keep going, leave the callback function installed */
1022 if (data
->count
< 0 && r
== 0)
1024 count
= 0; /* data->count == 0 || r != 0; force break below */
1027 /* Deregister function, let rl_callback_read_char deallocate data */
1028 _rl_callback_func
= 0;
1029 _rl_want_redisplay
= 1;
1034 return _rl_insert_next (count
);
1039 rl_quoted_insert (int count
, int key
)
1041 /* Let's see...should the callback interface futz with signal handling? */
1042 #if defined (HANDLE_SIGNALS)
1043 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
1044 _rl_disable_tty_signals ();
1047 #if defined (READLINE_CALLBACKS)
1048 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1050 _rl_callback_data
= _rl_callback_data_alloc (count
);
1051 _rl_callback_func
= _rl_insert_next_callback
;
1056 /* A negative count means to quote the next -COUNT characters. */
1062 r
= _rl_insert_next (1);
1063 while (r
== 0 && ++count
< 0);
1067 return _rl_insert_next (count
);
1070 /* Insert a tab character. */
1072 rl_tab_insert (int count
, int key
)
1074 return (_rl_insert_char (count
, '\t'));
1077 /* What to do when a NEWLINE is pressed. We accept the whole line.
1078 KEY is the key that invoked this command. I guess it could have
1079 meaning in the future. */
1081 rl_newline (int count
, int key
)
1085 if (_rl_history_preserve_point
)
1086 _rl_history_saved_point
= (rl_point
== rl_end
) ? -1 : rl_point
;
1088 RL_SETSTATE(RL_STATE_DONE
);
1090 #if defined (VI_MODE)
1091 if (rl_editing_mode
== vi_mode
)
1093 _rl_vi_done_inserting ();
1094 if (_rl_vi_textmod_command (_rl_vi_last_command
) == 0) /* XXX */
1095 _rl_vi_reset_last ();
1097 #endif /* VI_MODE */
1099 /* If we've been asked to erase empty lines, suppress the final update,
1100 since _rl_update_final calls rl_crlf(). */
1101 if (rl_erase_empty_line
&& rl_point
== 0 && rl_end
== 0)
1105 _rl_update_final ();
1109 /* What to do for some uppercase characters, like meta characters,
1110 and some characters appearing in emacs_ctlx_keymap. This function
1111 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1112 is special cased. */
1114 rl_do_lowercase_version (int ignore1
, int ignore2
)
1119 /* This is different from what vi does, so the code's not shared. Emacs
1120 rubout in overwrite mode has one oddity: it replaces a control
1121 character that's displayed as two characters (^X) with two spaces. */
1123 _rl_overwrite_rubout (int count
, int key
)
1136 /* L == number of spaces to insert */
1137 for (i
= l
= 0; i
< count
; i
++)
1139 rl_backward_char (1, key
);
1140 l
+= rl_character_len (rl_line_buffer
[rl_point
], rl_point
); /* not exactly right */
1143 rl_begin_undo_group ();
1145 if (count
> 1 || rl_explicit_arg
)
1146 rl_kill_text (opoint
, rl_point
);
1148 rl_delete_text (opoint
, rl_point
);
1150 /* Emacs puts point at the beginning of the sequence of spaces. */
1151 if (rl_point
< rl_end
)
1154 _rl_insert_char (l
, ' ');
1158 rl_end_undo_group ();
1163 /* Rubout the character behind point. */
1165 rl_rubout (int count
, int key
)
1168 return (rl_delete (-count
, key
));
1176 if (rl_insert_mode
== RL_IM_OVERWRITE
)
1177 return (_rl_overwrite_rubout (count
, key
));
1179 return (_rl_rubout_char (count
, key
));
1183 _rl_rubout_char (int count
, int key
)
1188 /* Duplicated code because this is called from other parts of the library. */
1190 return (rl_delete (-count
, key
));
1198 orig_point
= rl_point
;
1199 if (count
> 1 || rl_explicit_arg
)
1201 rl_backward_char (count
, key
);
1202 rl_kill_text (orig_point
, rl_point
);
1204 else if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
1206 c
= rl_line_buffer
[--rl_point
];
1207 rl_delete_text (rl_point
, orig_point
);
1208 /* The erase-at-end-of-line hack is of questionable merit now. */
1209 if (rl_point
== rl_end
&& ISPRINT ((unsigned char)c
) && _rl_last_c_pos
)
1212 l
= rl_character_len (c
, rl_point
);
1213 _rl_erase_at_end_of_line (l
);
1218 rl_point
= _rl_find_prev_mbchar (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1219 rl_delete_text (rl_point
, orig_point
);
1225 /* Delete the character under the cursor. Given a numeric argument,
1226 kill that many characters instead. */
1228 rl_delete (int count
, int key
)
1233 return (_rl_rubout_char (-count
, key
));
1235 if (rl_point
== rl_end
)
1241 if (count
> 1 || rl_explicit_arg
)
1244 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1245 rl_forward_char (count
, key
);
1247 rl_forward_byte (count
, key
);
1249 rl_kill_text (xpoint
, rl_point
);
1254 xpoint
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
1255 rl_delete_text (rl_point
, xpoint
);
1260 /* Delete the character under the cursor, unless the insertion
1261 point is at the end of the line, in which case the character
1262 behind the cursor is deleted. COUNT is obeyed and may be used
1263 to delete forward or backward that many characters. */
1265 rl_rubout_or_delete (int count
, int key
)
1267 if (rl_end
!= 0 && rl_point
== rl_end
)
1268 return (_rl_rubout_char (count
, key
));
1270 return (rl_delete (count
, key
));
1273 /* Delete all spaces and tabs around point. */
1275 rl_delete_horizontal_space (int count
, int ignore
)
1279 while (rl_point
&& whitespace (rl_line_buffer
[rl_point
- 1]))
1284 while (rl_point
< rl_end
&& whitespace (rl_line_buffer
[rl_point
]))
1287 if (start
!= rl_point
)
1289 rl_delete_text (start
, rl_point
);
1299 /* Like the tcsh editing function delete-char-or-list. The eof character
1300 is caught before this is invoked, so this really does the same thing as
1301 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1303 rl_delete_or_show_completions (int count
, int key
)
1305 if (rl_end
!= 0 && rl_point
== rl_end
)
1306 return (rl_possible_completions (count
, key
));
1308 return (rl_delete (count
, key
));
1311 #ifndef RL_COMMENT_BEGIN_DEFAULT
1312 #define RL_COMMENT_BEGIN_DEFAULT "#"
1315 /* Turn the current line into a comment in shell history.
1316 A K*rn shell style function. */
1318 rl_insert_comment (int count
, int key
)
1320 char *rl_comment_text
;
1323 rl_beg_of_line (1, key
);
1324 rl_comment_text
= _rl_comment_begin
? _rl_comment_begin
: RL_COMMENT_BEGIN_DEFAULT
;
1326 if (rl_explicit_arg
== 0)
1327 rl_insert_text (rl_comment_text
);
1330 rl_comment_len
= strlen (rl_comment_text
);
1331 if (STREQN (rl_comment_text
, rl_line_buffer
, rl_comment_len
))
1332 rl_delete_text (rl_point
, rl_point
+ rl_comment_len
);
1334 rl_insert_text (rl_comment_text
);
1337 (*rl_redisplay_function
) ();
1338 rl_newline (1, '\n');
1343 /* **************************************************************** */
1347 /* **************************************************************** */
1349 /* The three kinds of things that we know how to do. */
1354 /* Uppercase the word at point. */
1356 rl_upcase_word (int count
, int key
)
1358 return (rl_change_case (count
, UpCase
));
1361 /* Lowercase the word at point. */
1363 rl_downcase_word (int count
, int key
)
1365 return (rl_change_case (count
, DownCase
));
1368 /* Upcase the first letter, downcase the rest. */
1370 rl_capitalize_word (int count
, int key
)
1372 return (rl_change_case (count
, CapCase
));
1375 /* The meaty function.
1376 Change the case of COUNT words, performing OP on them.
1377 OP is one of UpCase, DownCase, or CapCase.
1378 If a negative argument is given, leave point where it started,
1379 otherwise, leave it where it moves to. */
1381 rl_change_case (int count
, int op
)
1383 int start
, next
, end
;
1384 int inword
, nc
, nop
;
1386 #if defined (HANDLE_MULTIBYTE)
1388 char mb
[MB_LEN_MAX
+1];
1395 rl_forward_word (count
, 0);
1398 if (op
!= UpCase
&& op
!= DownCase
&& op
!= CapCase
)
1407 #if defined (HANDLE_MULTIBYTE)
1408 memset (&mps
, 0, sizeof (mbstate_t));
1411 /* We are going to modify some text, so let's prepare to undo it. */
1412 rl_modifying (start
, end
);
1417 c
= _rl_char_value (rl_line_buffer
, start
);
1418 /* This assumes that the upper and lower case versions are the same width. */
1419 next
= MB_NEXTCHAR (rl_line_buffer
, start
, 1, MB_FIND_NONZERO
);
1421 if (_rl_walphabetic (c
) == 0)
1430 nop
= inword
? DownCase
: UpCase
;
1435 /* Can't check isascii here; some languages (e.g, Turkish) have
1436 multibyte upper and lower case equivalents of single-byte ascii
1438 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
1440 nc
= (nop
== UpCase
) ? _rl_to_upper (c
) : _rl_to_lower (c
);
1441 rl_line_buffer
[start
] = nc
;
1443 #if defined (HANDLE_MULTIBYTE)
1446 m
= mbrtowc (&wc
, rl_line_buffer
+ start
, end
- start
, &mps
);
1447 if (MB_INVALIDCH (m
))
1448 wc
= (wchar_t)rl_line_buffer
[start
];
1449 else if (MB_NULLWCH (m
))
1451 nwc
= (nop
== UpCase
) ? _rl_to_wupper (wc
) : _rl_to_wlower (wc
);
1452 if (nwc
!= wc
) /* just skip unchanged characters */
1455 mlen
= wcrtomb (mb
, nwc
, &mps
);
1458 /* what to do if m != mlen? adjust below */
1459 /* m == length of old char, mlen == length of new char */
1460 s
= rl_line_buffer
+ start
;
1461 e
= rl_line_buffer
+ rl_end
;
1463 memcpy (s
, mb
, mlen
);
1466 memcpy (s
, mb
, mlen
);
1467 memmove (s
+ mlen
, s
+ m
, (e
- s
) - m
);
1468 next
-= m
- mlen
; /* next char changes */
1469 end
-= m
- mlen
; /* end of word changes */
1470 rl_end
-= m
- mlen
; /* end of line changes */
1471 rl_line_buffer
[rl_end
] = 0;
1475 rl_extend_line_buffer (mlen
- m
+ 1);
1476 memmove (s
+ mlen
, s
+ m
, (e
- s
) - m
);
1477 memcpy (s
, mb
, mlen
);
1478 next
+= mlen
- m
; /* next char changes */
1479 end
+= mlen
- m
; /* end of word changes */
1480 rl_end
+= mlen
- m
; /* end of line changes */
1481 rl_line_buffer
[rl_end
] = 0;
1494 /* **************************************************************** */
1498 /* **************************************************************** */
1500 /* Transpose the words at point. If point is at the end of the line,
1501 transpose the two words before point. */
1503 rl_transpose_words (int count
, int key
)
1505 char *word1
, *word2
;
1506 int w1_beg
, w1_end
, w2_beg
, w2_end
;
1507 int orig_point
= rl_point
;
1512 /* Find the two words. */
1513 rl_forward_word (count
, key
);
1515 rl_backward_word (1, key
);
1517 rl_backward_word (count
, key
);
1519 rl_forward_word (1, key
);
1522 /* Do some check to make sure that there really are two words. */
1523 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
1526 rl_point
= orig_point
;
1530 /* Get the text of the words. */
1531 word1
= rl_copy_text (w1_beg
, w1_end
);
1532 word2
= rl_copy_text (w2_beg
, w2_end
);
1534 /* We are about to do many insertions and deletions. Remember them
1535 as one operation. */
1536 rl_begin_undo_group ();
1538 /* Do the stuff at word2 first, so that we don't have to worry
1539 about word1 moving. */
1541 rl_delete_text (w2_beg
, w2_end
);
1542 rl_insert_text (word1
);
1545 rl_delete_text (w1_beg
, w1_end
);
1546 rl_insert_text (word2
);
1548 /* This is exactly correct since the text before this point has not
1549 changed in length. */
1552 /* I think that does it. */
1553 rl_end_undo_group ();
1560 /* Transpose the characters at point. If point is at the end of the line,
1561 then transpose the characters before point. */
1563 rl_transpose_chars (int count
, int key
)
1565 #if defined (HANDLE_MULTIBYTE)
1571 int char_length
, prev_point
;
1576 if (!rl_point
|| rl_end
< 2)
1582 rl_begin_undo_group ();
1584 if (rl_point
== rl_end
)
1586 rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1590 prev_point
= rl_point
;
1591 rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1593 #if defined (HANDLE_MULTIBYTE)
1594 char_length
= prev_point
- rl_point
;
1595 dummy
= (char *)xmalloc (char_length
+ 1);
1596 for (i
= 0; i
< char_length
; i
++)
1597 dummy
[i
] = rl_line_buffer
[rl_point
+ i
];
1600 dummy
[0] = rl_line_buffer
[rl_point
];
1601 dummy
[char_length
= 1] = '\0';
1604 rl_delete_text (rl_point
, rl_point
+ char_length
);
1606 rl_point
= _rl_find_next_mbchar (rl_line_buffer
, rl_point
, count
, MB_FIND_NONZERO
);
1609 rl_insert_text (dummy
);
1610 rl_end_undo_group ();
1612 #if defined (HANDLE_MULTIBYTE)
1619 /* **************************************************************** */
1621 /* Character Searching */
1623 /* **************************************************************** */
1626 #if defined (HANDLE_MULTIBYTE)
1627 _rl_char_search_internal (int count
, int dir
, char *smbchar
, int len
)
1629 _rl_char_search_internal (int count
, int dir
, int schar
)
1633 #if defined (HANDLE_MULTIBYTE)
1641 inc
= (dir
< 0) ? -1 : 1;
1644 if ((dir
< 0 && pos
<= 0) || (dir
> 0 && pos
>= rl_end
))
1650 #if defined (HANDLE_MULTIBYTE)
1651 pos
= (inc
> 0) ? _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)
1652 : _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
);
1658 #if defined (HANDLE_MULTIBYTE)
1659 if (_rl_is_mbchar_matched (rl_line_buffer
, pos
, rl_end
, smbchar
, len
))
1661 if (rl_line_buffer
[pos
] == schar
)
1666 rl_point
= (dir
== BTO
) ? _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)
1669 rl_point
= (dir
== FTO
) ? _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
)
1673 #if defined (HANDLE_MULTIBYTE)
1677 #if defined (HANDLE_MULTIBYTE)
1678 while ((dir
< 0) ? (pos
= _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
)) != prepos
1679 : (pos
= _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)) != prepos
);
1681 while ((dir
< 0) ? pos
-- : ++pos
< rl_end
);
1687 /* Search COUNT times for a character read from the current input stream.
1688 FDIR is the direction to search if COUNT is non-negative; otherwise
1689 the search goes in BDIR. So much is dependent on HANDLE_MULTIBYTE
1690 that there are two separate versions of this function. */
1691 #if defined (HANDLE_MULTIBYTE)
1693 _rl_char_search (int count
, int fdir
, int bdir
)
1695 char mbchar
[MB_LEN_MAX
];
1698 mb_len
= _rl_read_mbchar (mbchar
, MB_LEN_MAX
);
1704 return (_rl_char_search_internal (-count
, bdir
, mbchar
, mb_len
));
1706 return (_rl_char_search_internal (count
, fdir
, mbchar
, mb_len
));
1708 #else /* !HANDLE_MULTIBYTE */
1710 _rl_char_search (int count
, int fdir
, int bdir
)
1714 RL_SETSTATE(RL_STATE_MOREINPUT
);
1716 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
1722 return (_rl_char_search_internal (-count
, bdir
, c
));
1724 return (_rl_char_search_internal (count
, fdir
, c
));
1726 #endif /* !HANDLE_MULTIBYTE */
1728 #if defined (READLINE_CALLBACKS)
1730 _rl_char_search_callback (data
)
1731 _rl_callback_generic_arg
*data
;
1733 _rl_callback_func
= 0;
1734 _rl_want_redisplay
= 1;
1736 return (_rl_char_search (data
->count
, data
->i1
, data
->i2
));
1741 rl_char_search (int count
, int key
)
1743 #if defined (READLINE_CALLBACKS)
1744 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1746 _rl_callback_data
= _rl_callback_data_alloc (count
);
1747 _rl_callback_data
->i1
= FFIND
;
1748 _rl_callback_data
->i2
= BFIND
;
1749 _rl_callback_func
= _rl_char_search_callback
;
1754 return (_rl_char_search (count
, FFIND
, BFIND
));
1758 rl_backward_char_search (int count
, int key
)
1760 #if defined (READLINE_CALLBACKS)
1761 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1763 _rl_callback_data
= _rl_callback_data_alloc (count
);
1764 _rl_callback_data
->i1
= BFIND
;
1765 _rl_callback_data
->i2
= FFIND
;
1766 _rl_callback_func
= _rl_char_search_callback
;
1771 return (_rl_char_search (count
, BFIND
, FFIND
));
1774 /* **************************************************************** */
1776 /* The Mark and the Region. */
1778 /* **************************************************************** */
1780 /* Set the mark at POSITION. */
1782 _rl_set_mark_at_pos (int position
)
1784 if (position
< 0 || position
> rl_end
)
1791 /* A bindable command to set the mark. */
1793 rl_set_mark (int count
, int key
)
1795 return (_rl_set_mark_at_pos (rl_explicit_arg
? count
: rl_point
));
1798 /* Exchange the position of mark and point. */
1800 rl_exchange_point_and_mark (int count
, int key
)
1802 if (rl_mark
> rl_end
)
1808 rl_mark
= 0; /* like _RL_FIX_POINT */
1812 SWAP (rl_point
, rl_mark
);