1 /* text.c -- text handling commands for readline. */
3 /* Copyright (C) 1987-2016 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 (string
)
91 l
= (string
&& *string
) ? strlen (string
) : 0;
95 if (rl_end
+ l
>= rl_line_buffer_len
)
96 rl_extend_line_buffer (rl_end
+ l
);
98 for (i
= rl_end
; i
>= rl_point
; i
--)
99 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 (from
, to
)
128 register int diff
, i
;
130 /* Fix it if the caller is confused. */
144 text
= rl_copy_text (from
, to
);
146 /* Some versions of strncpy() can't handle overlapping arguments. */
148 for (i
= from
; i
< rl_end
- diff
; i
++)
149 rl_line_buffer
[i
] = rl_line_buffer
[i
+ diff
];
151 /* Remember how to undo this delete. */
152 if (_rl_doing_an_undo
== 0)
153 rl_add_undo (UNDO_DELETE
, from
, to
, text
);
158 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 (fix_mark_too
)
178 _RL_FIX_POINT (rl_point
);
180 _RL_FIX_POINT (rl_mark
);
184 /* Replace the contents of the line buffer between START and END with
185 TEXT. The operation is undoable. To replace the entire line in an
186 undoable mode, use _rl_replace_text(text, 0, rl_end); */
188 _rl_replace_text (text
, start
, end
)
195 rl_begin_undo_group ();
197 rl_delete_text (start
, end
+ 1);
200 n
= rl_insert_text (text
);
201 rl_end_undo_group ();
206 /* Replace the current line buffer contents with TEXT. If CLEAR_UNDO is
207 non-zero, we free the current undo list. */
209 rl_replace_line (text
, 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 (count
, 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 (count
)
300 #if defined (HANDLE_MULTIBYTE)
301 point
= _rl_find_next_mbchar (rl_line_buffer
, rl_point
, count
, MB_FIND_NONZERO
);
303 #if defined (VI_MODE)
304 if (point
>= rl_end
&& VI_COMMAND_MODE())
305 point
= _rl_find_prev_mbchar (rl_line_buffer
, rl_end
, MB_FIND_NONZERO
);
311 point
= rl_point
+ count
;
319 #if defined (HANDLE_MULTIBYTE)
320 /* Move forward COUNT characters. */
322 rl_forward_char (count
, key
)
327 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
328 return (rl_forward_byte (count
, key
));
331 return (rl_backward_char (-count
, key
));
335 if (rl_point
== rl_end
&& EMACS_MODE())
341 point
= _rl_forward_char_internal (count
);
343 if (rl_point
== point
)
351 #else /* !HANDLE_MULTIBYTE */
353 rl_forward_char (count
, key
)
356 return (rl_forward_byte (count
, key
));
358 #endif /* !HANDLE_MULTIBYTE */
360 /* Backwards compatibility. */
362 rl_forward (count
, key
)
365 return (rl_forward_char (count
, key
));
368 /* Move backward COUNT bytes. */
370 rl_backward_byte (count
, key
)
374 return (rl_forward_byte (-count
, key
));
378 if (rl_point
< count
)
393 #if defined (HANDLE_MULTIBYTE)
394 /* Move backward COUNT characters. */
396 rl_backward_char (count
, key
)
401 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
402 return (rl_backward_byte (count
, key
));
405 return (rl_forward_char (-count
, key
));
411 while (count
> 0 && point
> 0)
413 point
= _rl_find_prev_mbchar (rl_line_buffer
, point
, MB_FIND_NONZERO
);
429 rl_backward_char (count
, key
)
432 return (rl_backward_byte (count
, key
));
436 /* Backwards compatibility. */
438 rl_backward (count
, key
)
441 return (rl_backward_char (count
, key
));
444 /* Move to the beginning of the line. */
446 rl_beg_of_line (count
, key
)
453 /* Move to the end of the line. */
455 rl_end_of_line (count
, key
)
462 /* Move forward a word. We do what Emacs does. Handles multibyte chars. */
464 rl_forward_word (count
, key
)
470 return (rl_backward_word (-count
, key
));
474 if (rl_point
== rl_end
)
477 /* If we are not in a word, move forward until we are in one.
478 Then, move forward until we hit a non-alphabetic character. */
479 c
= _rl_char_value (rl_line_buffer
, rl_point
);
481 if (_rl_walphabetic (c
) == 0)
483 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
484 while (rl_point
< rl_end
)
486 c
= _rl_char_value (rl_line_buffer
, rl_point
);
487 if (_rl_walphabetic (c
))
489 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
493 if (rl_point
== rl_end
)
496 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
497 while (rl_point
< rl_end
)
499 c
= _rl_char_value (rl_line_buffer
, rl_point
);
500 if (_rl_walphabetic (c
) == 0)
502 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
511 /* Move backward a word. We do what Emacs does. Handles multibyte chars. */
513 rl_backward_word (count
, key
)
519 return (rl_forward_word (-count
, key
));
526 /* Like rl_forward_word (), except that we look at the characters
527 just before point. */
529 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
530 c
= _rl_char_value (rl_line_buffer
, p
);
532 if (_rl_walphabetic (c
) == 0)
537 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
538 c
= _rl_char_value (rl_line_buffer
, p
);
539 if (_rl_walphabetic (c
))
547 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
548 c
= _rl_char_value (rl_line_buffer
, p
);
549 if (_rl_walphabetic (c
) == 0)
561 /* Clear the current line. Numeric argument to C-l does this. */
563 rl_refresh_line (ignore1
, ignore2
)
564 int ignore1
, ignore2
;
568 curr_line
= _rl_current_display_line ();
570 _rl_move_vert (curr_line
);
571 _rl_move_cursor_relative (0, rl_line_buffer
); /* XXX is this right */
573 _rl_clear_to_eol (0); /* arg of 0 means to not use spaces */
575 rl_redraw_prompt_last_line ();
576 rl_display_fixed
= 1;
581 /* C-l typed to a line without quoting clears the screen, and then reprints
582 the prompt and the current input line. Given a numeric arg, redraw only
585 rl_clear_screen (count
, key
)
590 rl_refresh_line (count
, key
);
594 _rl_clear_screen (); /* calls termcap function to clear screen */
595 rl_forced_update_display ();
596 rl_display_fixed
= 1;
602 rl_skip_csi_sequence (count
, key
)
607 RL_SETSTATE (RL_STATE_MOREINPUT
);
610 while (ch
>= 0x20 && ch
< 0x40);
611 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
617 rl_arrow_keys (count
, c
)
622 RL_SETSTATE(RL_STATE_MOREINPUT
);
624 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
628 switch (_rl_to_upper (ch
))
631 rl_get_previous_history (count
, ch
);
635 rl_get_next_history (count
, ch
);
639 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
640 rl_forward_char (count
, ch
);
642 rl_forward_byte (count
, ch
);
646 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
647 rl_backward_char (count
, ch
);
649 rl_backward_byte (count
, ch
);
659 /* **************************************************************** */
663 /* **************************************************************** */
665 #ifdef HANDLE_MULTIBYTE
666 static char pending_bytes
[MB_LEN_MAX
];
667 static int pending_bytes_length
= 0;
668 static mbstate_t ps
= {0};
671 /* Insert the character C at the current location, moving point forward.
672 If C introduces a multibyte sequence, we read the whole sequence and
673 then insert the multibyte char into the line buffer. */
675 _rl_insert_char (count
, c
)
680 #ifdef HANDLE_MULTIBYTE
682 char incoming
[MB_LEN_MAX
+ 1];
683 int incoming_length
= 0;
685 static int stored_count
= 0;
691 #if defined (HANDLE_MULTIBYTE)
692 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
703 if (stored_count
<= 0)
704 stored_count
= count
;
706 count
= stored_count
;
709 pending_bytes
[pending_bytes_length
++] = c
;
710 ret
= mbrtowc (&wc
, pending_bytes
, pending_bytes_length
, &ps
);
712 if (ret
== (size_t)-2)
714 /* Bytes too short to compose character, try to wait for next byte.
715 Restore the state of the byte sequence, because in this case the
716 effect of mbstate is undefined. */
720 else if (ret
== (size_t)-1)
722 /* Invalid byte sequence for the current locale. Treat first byte
723 as a single character. */
724 incoming
[0] = pending_bytes
[0];
727 pending_bytes_length
--;
728 memmove (pending_bytes
, pending_bytes
+ 1, pending_bytes_length
);
729 /* Clear the state of the byte sequence, because in this case the
730 effect of mbstate is undefined. */
731 memset (&ps
, 0, sizeof (mbstate_t));
733 else if (ret
== (size_t)0)
737 pending_bytes_length
--;
738 /* Clear the state of the byte sequence, because in this case the
739 effect of mbstate is undefined. */
740 memset (&ps
, 0, sizeof (mbstate_t));
744 /* We successfully read a single multibyte character. */
745 memcpy (incoming
, pending_bytes
, pending_bytes_length
);
746 incoming
[pending_bytes_length
] = '\0';
747 incoming_length
= pending_bytes_length
;
748 pending_bytes_length
= 0;
751 #endif /* HANDLE_MULTIBYTE */
753 /* If we can optimize, then do it. But don't let people crash
754 readline because of extra large arguments. */
755 if (count
> 1 && count
<= TEXT_COUNT_MAX
)
757 #if defined (HANDLE_MULTIBYTE)
758 string_size
= count
* incoming_length
;
759 string
= (char *)xmalloc (1 + string_size
);
762 while (i
< string_size
)
764 strncpy (string
+ i
, incoming
, incoming_length
);
765 i
+= incoming_length
;
769 #else /* !HANDLE_MULTIBYTE */
770 string
= (char *)xmalloc (1 + count
);
772 for (i
= 0; i
< count
; i
++)
774 #endif /* !HANDLE_MULTIBYTE */
777 rl_insert_text (string
);
783 if (count
> TEXT_COUNT_MAX
)
786 #if defined (HANDLE_MULTIBYTE)
787 string_size
= incoming_length
* TEXT_COUNT_MAX
;
788 string
= (char *)xmalloc (1 + string_size
);
791 while (i
< string_size
)
793 strncpy (string
+ i
, incoming
, incoming_length
);
794 i
+= incoming_length
;
799 decreaser
= (count
> TEXT_COUNT_MAX
) ? TEXT_COUNT_MAX
: count
;
800 string
[decreaser
*incoming_length
] = '\0';
801 rl_insert_text (string
);
808 #else /* !HANDLE_MULTIBYTE */
809 char str
[TEXT_COUNT_MAX
+1];
811 for (i
= 0; i
< TEXT_COUNT_MAX
; i
++)
816 decreaser
= (count
> TEXT_COUNT_MAX
? TEXT_COUNT_MAX
: count
);
817 str
[decreaser
] = '\0';
818 rl_insert_text (str
);
821 #endif /* !HANDLE_MULTIBYTE */
826 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
828 /* We are inserting a single character.
829 If there is pending input, then make a string of all of the
830 pending characters that are bound to rl_insert, and insert
831 them all. Don't do this if we're current reading input from
833 if ((RL_ISSTATE (RL_STATE_MACROINPUT
) == 0) && _rl_pushed_input_available ())
834 _rl_insert_typein (c
);
837 /* Inserting a single character. */
842 rl_insert_text (str
);
845 #if defined (HANDLE_MULTIBYTE)
848 rl_insert_text (incoming
);
856 /* Overwrite the character at point (or next COUNT characters) with C.
857 If C introduces a multibyte character sequence, read the entire sequence
858 before starting the overwrite loop. */
860 _rl_overwrite_char (count
, c
)
864 #if defined (HANDLE_MULTIBYTE)
865 char mbkey
[MB_LEN_MAX
];
868 /* Read an entire multibyte character sequence to insert COUNT times. */
869 if (count
> 0 && MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
870 k
= _rl_read_mbstring (c
, mbkey
, MB_LEN_MAX
);
873 rl_begin_undo_group ();
875 for (i
= 0; i
< count
; i
++)
877 #if defined (HANDLE_MULTIBYTE)
878 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
879 rl_insert_text (mbkey
);
882 _rl_insert_char (1, c
);
884 if (rl_point
< rl_end
)
888 rl_end_undo_group ();
899 r
= (rl_insert_mode
== RL_IM_INSERT
) ? _rl_insert_char (count
, c
) : _rl_overwrite_char (count
, c
);
901 /* XXX -- attempt to batch-insert pending input that maps to self-insert */
903 n
= (unsigned short)-2;
904 while (_rl_optimize_typeahead
&&
905 (RL_ISSTATE (RL_STATE_INPUTPENDING
|RL_STATE_MACROINPUT
) == 0) &&
906 _rl_pushed_input_available () == 0 &&
907 _rl_input_queued (0) &&
908 (n
= rl_read_key ()) > 0 &&
909 _rl_keymap
[(unsigned char)n
].type
== ISFUNC
&&
910 _rl_keymap
[(unsigned char)n
].function
== rl_insert
)
912 r
= (rl_insert_mode
== RL_IM_INSERT
) ? _rl_insert_char (1, n
) : _rl_overwrite_char (1, n
);
913 /* _rl_insert_char keeps its own set of pending characters to compose a
914 complete multibyte character, and only returns 1 if it sees a character
915 that's part of a multibyte character but too short to complete one. We
916 can try to read another character in the hopes that we will get the
917 next one or just punt. Right now we try to read another character.
918 We don't want to call rl_insert_next if _rl_insert_char has already
919 stored the character in the pending_bytes array because that will
920 result in doubled input. */
921 n
= (unsigned short)-2;
922 x
++; /* count of bytes of typeahead read, currently unused */
923 if (r
== 1) /* read partial multibyte character */
925 if (rl_done
|| r
!= 0)
929 if (n
!= (unsigned short)-2) /* -2 = sentinel value for having inserted N */
931 /* setting rl_pending_input inhibits setting rl_last_func so we do it
933 rl_last_func
= rl_insert
;
934 _rl_reset_argument ();
935 rl_executing_keyseq
[rl_key_sequence_length
= 0] = '\0';
936 r
= rl_execute_next (n
);
942 /* Insert the next typed character verbatim. */
944 _rl_insert_next (count
)
949 RL_SETSTATE(RL_STATE_MOREINPUT
);
951 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
956 if (RL_ISSTATE (RL_STATE_MACRODEF
))
957 _rl_add_macro_char (c
);
959 #if defined (HANDLE_SIGNALS)
960 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
961 _rl_restore_tty_signals ();
964 return (_rl_insert_char (count
, c
));
967 #if defined (READLINE_CALLBACKS)
969 _rl_insert_next_callback (data
)
970 _rl_callback_generic_arg
*data
;
976 /* Deregister function, let rl_callback_read_char deallocate data */
977 _rl_callback_func
= 0;
978 _rl_want_redisplay
= 1;
980 return _rl_insert_next (count
);
985 rl_quoted_insert (count
, key
)
988 /* Let's see...should the callback interface futz with signal handling? */
989 #if defined (HANDLE_SIGNALS)
990 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
991 _rl_disable_tty_signals ();
994 #if defined (READLINE_CALLBACKS)
995 if (RL_ISSTATE (RL_STATE_CALLBACK
))
997 _rl_callback_data
= _rl_callback_data_alloc (count
);
998 _rl_callback_func
= _rl_insert_next_callback
;
1003 return _rl_insert_next (count
);
1006 /* Insert a tab character. */
1008 rl_tab_insert (count
, key
)
1011 return (_rl_insert_char (count
, '\t'));
1014 /* What to do when a NEWLINE is pressed. We accept the whole line.
1015 KEY is the key that invoked this command. I guess it could have
1016 meaning in the future. */
1018 rl_newline (count
, key
)
1023 if (_rl_history_preserve_point
)
1024 _rl_history_saved_point
= (rl_point
== rl_end
) ? -1 : rl_point
;
1026 RL_SETSTATE(RL_STATE_DONE
);
1028 #if defined (VI_MODE)
1029 if (rl_editing_mode
== vi_mode
)
1031 _rl_vi_done_inserting ();
1032 if (_rl_vi_textmod_command (_rl_vi_last_command
) == 0) /* XXX */
1033 _rl_vi_reset_last ();
1035 #endif /* VI_MODE */
1037 /* If we've been asked to erase empty lines, suppress the final update,
1038 since _rl_update_final calls rl_crlf(). */
1039 if (rl_erase_empty_line
&& rl_point
== 0 && rl_end
== 0)
1043 _rl_update_final ();
1047 /* What to do for some uppercase characters, like meta characters,
1048 and some characters appearing in emacs_ctlx_keymap. This function
1049 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1050 is special cased. */
1052 rl_do_lowercase_version (ignore1
, ignore2
)
1053 int ignore1
, ignore2
;
1058 /* This is different from what vi does, so the code's not shared. Emacs
1059 rubout in overwrite mode has one oddity: it replaces a control
1060 character that's displayed as two characters (^X) with two spaces. */
1062 _rl_overwrite_rubout (count
, key
)
1076 /* L == number of spaces to insert */
1077 for (i
= l
= 0; i
< count
; i
++)
1079 rl_backward_char (1, key
);
1080 l
+= rl_character_len (rl_line_buffer
[rl_point
], rl_point
); /* not exactly right */
1083 rl_begin_undo_group ();
1085 if (count
> 1 || rl_explicit_arg
)
1086 rl_kill_text (opoint
, rl_point
);
1088 rl_delete_text (opoint
, rl_point
);
1090 /* Emacs puts point at the beginning of the sequence of spaces. */
1091 if (rl_point
< rl_end
)
1094 _rl_insert_char (l
, ' ');
1098 rl_end_undo_group ();
1103 /* Rubout the character behind point. */
1105 rl_rubout (count
, key
)
1109 return (rl_delete (-count
, key
));
1117 if (rl_insert_mode
== RL_IM_OVERWRITE
)
1118 return (_rl_overwrite_rubout (count
, key
));
1120 return (_rl_rubout_char (count
, key
));
1124 _rl_rubout_char (count
, key
)
1130 /* Duplicated code because this is called from other parts of the library. */
1132 return (rl_delete (-count
, key
));
1140 orig_point
= rl_point
;
1141 if (count
> 1 || rl_explicit_arg
)
1143 rl_backward_char (count
, key
);
1144 rl_kill_text (orig_point
, rl_point
);
1146 else if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
1148 c
= rl_line_buffer
[--rl_point
];
1149 rl_delete_text (rl_point
, orig_point
);
1150 /* The erase-at-end-of-line hack is of questionable merit now. */
1151 if (rl_point
== rl_end
&& ISPRINT ((unsigned char)c
) && _rl_last_c_pos
)
1154 l
= rl_character_len (c
, rl_point
);
1155 _rl_erase_at_end_of_line (l
);
1160 rl_point
= _rl_find_prev_mbchar (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1161 rl_delete_text (rl_point
, orig_point
);
1167 /* Delete the character under the cursor. Given a numeric argument,
1168 kill that many characters instead. */
1170 rl_delete (count
, key
)
1176 return (_rl_rubout_char (-count
, key
));
1178 if (rl_point
== rl_end
)
1184 if (count
> 1 || rl_explicit_arg
)
1187 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1188 rl_forward_char (count
, key
);
1190 rl_forward_byte (count
, key
);
1192 rl_kill_text (xpoint
, rl_point
);
1197 xpoint
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
1198 rl_delete_text (rl_point
, xpoint
);
1203 /* Delete the character under the cursor, unless the insertion
1204 point is at the end of the line, in which case the character
1205 behind the cursor is deleted. COUNT is obeyed and may be used
1206 to delete forward or backward that many characters. */
1208 rl_rubout_or_delete (count
, key
)
1211 if (rl_end
!= 0 && rl_point
== rl_end
)
1212 return (_rl_rubout_char (count
, key
));
1214 return (rl_delete (count
, key
));
1217 /* Delete all spaces and tabs around point. */
1219 rl_delete_horizontal_space (count
, ignore
)
1224 while (rl_point
&& whitespace (rl_line_buffer
[rl_point
- 1]))
1229 while (rl_point
< rl_end
&& whitespace (rl_line_buffer
[rl_point
]))
1232 if (start
!= rl_point
)
1234 rl_delete_text (start
, rl_point
);
1244 /* Like the tcsh editing function delete-char-or-list. The eof character
1245 is caught before this is invoked, so this really does the same thing as
1246 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1248 rl_delete_or_show_completions (count
, key
)
1251 if (rl_end
!= 0 && rl_point
== rl_end
)
1252 return (rl_possible_completions (count
, key
));
1254 return (rl_delete (count
, key
));
1257 #ifndef RL_COMMENT_BEGIN_DEFAULT
1258 #define RL_COMMENT_BEGIN_DEFAULT "#"
1261 /* Turn the current line into a comment in shell history.
1262 A K*rn shell style function. */
1264 rl_insert_comment (count
, key
)
1267 char *rl_comment_text
;
1270 rl_beg_of_line (1, key
);
1271 rl_comment_text
= _rl_comment_begin
? _rl_comment_begin
: RL_COMMENT_BEGIN_DEFAULT
;
1273 if (rl_explicit_arg
== 0)
1274 rl_insert_text (rl_comment_text
);
1277 rl_comment_len
= strlen (rl_comment_text
);
1278 if (STREQN (rl_comment_text
, rl_line_buffer
, rl_comment_len
))
1279 rl_delete_text (rl_point
, rl_point
+ rl_comment_len
);
1281 rl_insert_text (rl_comment_text
);
1284 (*rl_redisplay_function
) ();
1285 rl_newline (1, '\n');
1290 /* **************************************************************** */
1294 /* **************************************************************** */
1296 /* The three kinds of things that we know how to do. */
1301 /* Uppercase the word at point. */
1303 rl_upcase_word (count
, key
)
1306 return (rl_change_case (count
, UpCase
));
1309 /* Lowercase the word at point. */
1311 rl_downcase_word (count
, key
)
1314 return (rl_change_case (count
, DownCase
));
1317 /* Upcase the first letter, downcase the rest. */
1319 rl_capitalize_word (count
, key
)
1322 return (rl_change_case (count
, CapCase
));
1325 /* The meaty function.
1326 Change the case of COUNT words, performing OP on them.
1327 OP is one of UpCase, DownCase, or CapCase.
1328 If a negative argument is given, leave point where it started,
1329 otherwise, leave it where it moves to. */
1331 rl_change_case (count
, op
)
1334 int start
, next
, end
;
1335 int inword
, c
, nc
, nop
;
1336 #if defined (HANDLE_MULTIBYTE)
1338 char mb
[MB_LEN_MAX
+1];
1345 rl_forward_word (count
, 0);
1348 if (op
!= UpCase
&& op
!= DownCase
&& op
!= CapCase
)
1357 #if defined (HANDLE_MULTIBYTE)
1358 memset (&mps
, 0, sizeof (mbstate_t));
1361 /* We are going to modify some text, so let's prepare to undo it. */
1362 rl_modifying (start
, end
);
1367 c
= _rl_char_value (rl_line_buffer
, start
);
1368 /* This assumes that the upper and lower case versions are the same width. */
1369 next
= MB_NEXTCHAR (rl_line_buffer
, start
, 1, MB_FIND_NONZERO
);
1371 if (_rl_walphabetic (c
) == 0)
1380 nop
= inword
? DownCase
: UpCase
;
1385 if (MB_CUR_MAX
== 1 || rl_byte_oriented
|| isascii ((unsigned char)c
))
1387 nc
= (nop
== UpCase
) ? _rl_to_upper (c
) : _rl_to_lower (c
);
1388 rl_line_buffer
[start
] = nc
;
1390 #if defined (HANDLE_MULTIBYTE)
1393 m
= mbrtowc (&wc
, rl_line_buffer
+ start
, end
- start
, &mps
);
1394 if (MB_INVALIDCH (m
))
1395 wc
= (wchar_t)rl_line_buffer
[start
];
1396 else if (MB_NULLWCH (m
))
1398 nwc
= (nop
== UpCase
) ? _rl_to_wupper (wc
) : _rl_to_wlower (wc
);
1399 if (nwc
!= wc
) /* just skip unchanged characters */
1401 mlen
= wcrtomb (mb
, nwc
, &mps
);
1404 /* Assume the same width */
1405 strncpy (rl_line_buffer
+ start
, mb
, mlen
);
1417 /* **************************************************************** */
1421 /* **************************************************************** */
1423 /* Transpose the words at point. If point is at the end of the line,
1424 transpose the two words before point. */
1426 rl_transpose_words (count
, key
)
1429 char *word1
, *word2
;
1430 int w1_beg
, w1_end
, w2_beg
, w2_end
;
1431 int orig_point
= rl_point
;
1436 /* Find the two words. */
1437 rl_forward_word (count
, key
);
1439 rl_backward_word (1, key
);
1441 rl_backward_word (count
, key
);
1443 rl_forward_word (1, key
);
1446 /* Do some check to make sure that there really are two words. */
1447 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
1450 rl_point
= orig_point
;
1454 /* Get the text of the words. */
1455 word1
= rl_copy_text (w1_beg
, w1_end
);
1456 word2
= rl_copy_text (w2_beg
, w2_end
);
1458 /* We are about to do many insertions and deletions. Remember them
1459 as one operation. */
1460 rl_begin_undo_group ();
1462 /* Do the stuff at word2 first, so that we don't have to worry
1463 about word1 moving. */
1465 rl_delete_text (w2_beg
, w2_end
);
1466 rl_insert_text (word1
);
1469 rl_delete_text (w1_beg
, w1_end
);
1470 rl_insert_text (word2
);
1472 /* This is exactly correct since the text before this point has not
1473 changed in length. */
1476 /* I think that does it. */
1477 rl_end_undo_group ();
1484 /* Transpose the characters at point. If point is at the end of the line,
1485 then transpose the characters before point. */
1487 rl_transpose_chars (count
, key
)
1490 #if defined (HANDLE_MULTIBYTE)
1496 int char_length
, prev_point
;
1501 if (!rl_point
|| rl_end
< 2)
1507 rl_begin_undo_group ();
1509 if (rl_point
== rl_end
)
1511 rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1515 prev_point
= rl_point
;
1516 rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1518 #if defined (HANDLE_MULTIBYTE)
1519 char_length
= prev_point
- rl_point
;
1520 dummy
= (char *)xmalloc (char_length
+ 1);
1521 for (i
= 0; i
< char_length
; i
++)
1522 dummy
[i
] = rl_line_buffer
[rl_point
+ i
];
1525 dummy
[0] = rl_line_buffer
[rl_point
];
1526 dummy
[char_length
= 1] = '\0';
1529 rl_delete_text (rl_point
, rl_point
+ char_length
);
1531 rl_point
= _rl_find_next_mbchar (rl_line_buffer
, rl_point
, count
, MB_FIND_NONZERO
);
1534 rl_insert_text (dummy
);
1535 rl_end_undo_group ();
1537 #if defined (HANDLE_MULTIBYTE)
1544 /* **************************************************************** */
1546 /* Character Searching */
1548 /* **************************************************************** */
1551 #if defined (HANDLE_MULTIBYTE)
1552 _rl_char_search_internal (count
, dir
, smbchar
, len
)
1557 _rl_char_search_internal (count
, dir
, schar
)
1558 int count
, dir
, schar
;
1562 #if defined (HANDLE_MULTIBYTE)
1570 inc
= (dir
< 0) ? -1 : 1;
1573 if ((dir
< 0 && pos
<= 0) || (dir
> 0 && pos
>= rl_end
))
1579 #if defined (HANDLE_MULTIBYTE)
1580 pos
= (inc
> 0) ? _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)
1581 : _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
);
1587 #if defined (HANDLE_MULTIBYTE)
1588 if (_rl_is_mbchar_matched (rl_line_buffer
, pos
, rl_end
, smbchar
, len
))
1590 if (rl_line_buffer
[pos
] == schar
)
1595 rl_point
= (dir
== BTO
) ? _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)
1598 rl_point
= (dir
== FTO
) ? _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
)
1602 #if defined (HANDLE_MULTIBYTE)
1606 #if defined (HANDLE_MULTIBYTE)
1607 while ((dir
< 0) ? (pos
= _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
)) != prepos
1608 : (pos
= _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)) != prepos
);
1610 while ((dir
< 0) ? pos
-- : ++pos
< rl_end
);
1616 /* Search COUNT times for a character read from the current input stream.
1617 FDIR is the direction to search if COUNT is non-negative; otherwise
1618 the search goes in BDIR. So much is dependent on HANDLE_MULTIBYTE
1619 that there are two separate versions of this function. */
1620 #if defined (HANDLE_MULTIBYTE)
1622 _rl_char_search (count
, fdir
, bdir
)
1623 int count
, fdir
, bdir
;
1625 char mbchar
[MB_LEN_MAX
];
1628 mb_len
= _rl_read_mbchar (mbchar
, MB_LEN_MAX
);
1634 return (_rl_char_search_internal (-count
, bdir
, mbchar
, mb_len
));
1636 return (_rl_char_search_internal (count
, fdir
, mbchar
, mb_len
));
1638 #else /* !HANDLE_MULTIBYTE */
1640 _rl_char_search (count
, fdir
, bdir
)
1641 int count
, fdir
, bdir
;
1645 RL_SETSTATE(RL_STATE_MOREINPUT
);
1647 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
1653 return (_rl_char_search_internal (-count
, bdir
, c
));
1655 return (_rl_char_search_internal (count
, fdir
, c
));
1657 #endif /* !HANDLE_MULTIBYTE */
1659 #if defined (READLINE_CALLBACKS)
1661 _rl_char_search_callback (data
)
1662 _rl_callback_generic_arg
*data
;
1664 _rl_callback_func
= 0;
1665 _rl_want_redisplay
= 1;
1667 return (_rl_char_search (data
->count
, data
->i1
, data
->i2
));
1672 rl_char_search (count
, key
)
1675 #if defined (READLINE_CALLBACKS)
1676 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1678 _rl_callback_data
= _rl_callback_data_alloc (count
);
1679 _rl_callback_data
->i1
= FFIND
;
1680 _rl_callback_data
->i2
= BFIND
;
1681 _rl_callback_func
= _rl_char_search_callback
;
1686 return (_rl_char_search (count
, FFIND
, BFIND
));
1690 rl_backward_char_search (count
, key
)
1693 #if defined (READLINE_CALLBACKS)
1694 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1696 _rl_callback_data
= _rl_callback_data_alloc (count
);
1697 _rl_callback_data
->i1
= BFIND
;
1698 _rl_callback_data
->i2
= FFIND
;
1699 _rl_callback_func
= _rl_char_search_callback
;
1704 return (_rl_char_search (count
, BFIND
, FFIND
));
1707 /* **************************************************************** */
1709 /* The Mark and the Region. */
1711 /* **************************************************************** */
1713 /* Set the mark at POSITION. */
1715 _rl_set_mark_at_pos (position
)
1718 if (position
> rl_end
)
1725 /* A bindable command to set the mark. */
1727 rl_set_mark (count
, key
)
1730 return (_rl_set_mark_at_pos (rl_explicit_arg
? count
: rl_point
));
1733 /* Exchange the position of mark and point. */
1735 rl_exchange_point_and_mark (count
, key
)
1738 if (rl_mark
> rl_end
)
1747 SWAP (rl_point
, rl_mark
);