1 /* text.c -- text handling commands for readline. */
3 /* Copyright (C) 1987-2010 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 /* **************************************************************** */
76 /* Insert and Delete */
78 /* **************************************************************** */
80 /* Insert a string of text into the line at point. This is the only
81 way that you should do insertion. _rl_insert_char () calls this
82 function. Returns the number of characters inserted. */
84 rl_insert_text (string)
89 l = (string && *string) ? strlen (string) : 0;
93 if (rl_end + l >= rl_line_buffer_len)
94 rl_extend_line_buffer (rl_end + l);
96 for (i = rl_end; i >= rl_point; i--)
97 rl_line_buffer[i + l] = rl_line_buffer[i];
98 strncpy (rl_line_buffer + rl_point, string, l);
100 /* Remember how to undo this if we aren't undoing something. */
101 if (_rl_doing_an_undo == 0)
103 /* If possible and desirable, concatenate the undos. */
106 (rl_undo_list->what == UNDO_INSERT) &&
107 (rl_undo_list->end == rl_point) &&
108 (rl_undo_list->end - rl_undo_list->start < 20))
111 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
115 rl_line_buffer[rl_end] = '\0';
119 /* Delete the string between FROM and TO. FROM is inclusive, TO is not.
120 Returns the number of characters deleted. */
122 rl_delete_text (from, 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 (fix_mark_too)
176 _RL_FIX_POINT (rl_point);
178 _RL_FIX_POINT (rl_mark);
182 /* Replace the contents of the line buffer between START and END with
183 TEXT. The operation is undoable. To replace the entire line in an
184 undoable mode, use _rl_replace_text(text, 0, rl_end); */
186 _rl_replace_text (text, start, end)
193 rl_begin_undo_group ();
195 rl_delete_text (start, end + 1);
198 n = rl_insert_text (text);
199 rl_end_undo_group ();
204 /* Replace the current line buffer contents with TEXT. If CLEAR_UNDO is
205 non-zero, we free the current undo list. */
207 rl_replace_line (text, clear_undo)
214 if (len >= rl_line_buffer_len)
215 rl_extend_line_buffer (len);
216 strcpy (rl_line_buffer, text);
220 rl_free_undo_list ();
225 /* **************************************************************** */
227 /* Readline character functions */
229 /* **************************************************************** */
231 /* This is not a gap editor, just a stupid line input routine. No hair
232 is involved in writing any of the functions, and none should be. */
236 rl_end is the place in the string that we would place '\0';
237 i.e., it is always safe to place '\0' there.
239 rl_point is the place in the string where the cursor is. Sometimes
240 this is the same as rl_end.
242 Any command that is called interactively receives two arguments.
243 The first is a count: the numeric arg passed to this command.
244 The second is the key which invoked this command.
247 /* **************************************************************** */
249 /* Movement Commands */
251 /* **************************************************************** */
253 /* Note that if you `optimize' the display for these functions, you cannot
254 use said functions in other functions which do not do optimizing display.
255 I.e., you will have to update the data base for rl_redisplay, and you
256 might as well let rl_redisplay do that job. */
258 /* Move forward COUNT bytes. */
260 rl_forward_byte (count, key)
264 return (rl_backward_byte (-count, key));
270 end = rl_point + count;
271 #if defined (VI_MODE)
272 lend = rl_end > 0 ? rl_end - (VI_COMMAND_MODE()) : rl_end;
293 _rl_forward_char_internal (count)
298 #if defined (HANDLE_MULTIBYTE)
299 point = _rl_find_next_mbchar (rl_line_buffer, rl_point, count, MB_FIND_NONZERO);
301 #if defined (VI_MODE)
302 if (point >= rl_end && VI_COMMAND_MODE())
303 point = _rl_find_prev_mbchar (rl_line_buffer, rl_end, MB_FIND_NONZERO);
309 point = rl_point + count;
317 #if defined (HANDLE_MULTIBYTE)
318 /* Move forward COUNT characters. */
320 rl_forward_char (count, key)
325 if (MB_CUR_MAX == 1 || rl_byte_oriented)
326 return (rl_forward_byte (count, key));
329 return (rl_backward_char (-count, key));
333 if (rl_point == rl_end && EMACS_MODE())
339 point = _rl_forward_char_internal (count);
341 if (rl_point == point)
349 #else /* !HANDLE_MULTIBYTE */
351 rl_forward_char (count, key)
354 return (rl_forward_byte (count, key));
356 #endif /* !HANDLE_MULTIBYTE */
358 /* Backwards compatibility. */
360 rl_forward (count, key)
363 return (rl_forward_char (count, key));
366 /* Move backward COUNT bytes. */
368 rl_backward_byte (count, key)
372 return (rl_forward_byte (-count, key));
376 if (rl_point < count)
391 #if defined (HANDLE_MULTIBYTE)
392 /* Move backward COUNT characters. */
394 rl_backward_char (count, key)
399 if (MB_CUR_MAX == 1 || rl_byte_oriented)
400 return (rl_backward_byte (count, key));
403 return (rl_forward_char (-count, key));
409 while (count > 0 && point > 0)
411 point = _rl_find_prev_mbchar (rl_line_buffer, point, MB_FIND_NONZERO);
427 rl_backward_char (count, key)
430 return (rl_backward_byte (count, key));
434 /* Backwards compatibility. */
436 rl_backward (count, key)
439 return (rl_backward_char (count, key));
442 /* Move to the beginning of the line. */
444 rl_beg_of_line (count, key)
451 /* Move to the end of the line. */
453 rl_end_of_line (count, key)
460 /* Move forward a word. We do what Emacs does. Handles multibyte chars. */
462 rl_forward_word (count, key)
468 return (rl_backward_word (-count, key));
472 if (rl_point == rl_end)
475 /* If we are not in a word, move forward until we are in one.
476 Then, move forward until we hit a non-alphabetic character. */
477 c = _rl_char_value (rl_line_buffer, rl_point);
479 if (_rl_walphabetic (c) == 0)
481 rl_point = MB_NEXTCHAR (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
482 while (rl_point < rl_end)
484 c = _rl_char_value (rl_line_buffer, rl_point);
485 if (_rl_walphabetic (c))
487 rl_point = MB_NEXTCHAR (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
491 if (rl_point == rl_end)
494 rl_point = MB_NEXTCHAR (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
495 while (rl_point < rl_end)
497 c = _rl_char_value (rl_line_buffer, rl_point);
498 if (_rl_walphabetic (c) == 0)
500 rl_point = MB_NEXTCHAR (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
509 /* Move backward a word. We do what Emacs does. Handles multibyte chars. */
511 rl_backward_word (count, key)
517 return (rl_forward_word (-count, key));
524 /* Like rl_forward_word (), except that we look at the characters
525 just before point. */
527 p = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_NONZERO);
528 c = _rl_char_value (rl_line_buffer, p);
530 if (_rl_walphabetic (c) == 0)
535 p = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_NONZERO);
536 c = _rl_char_value (rl_line_buffer, p);
537 if (_rl_walphabetic (c))
545 p = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_NONZERO);
546 c = _rl_char_value (rl_line_buffer, p);
547 if (_rl_walphabetic (c) == 0)
559 /* Clear the current line. Numeric argument to C-l does this. */
561 rl_refresh_line (ignore1, ignore2)
562 int ignore1, ignore2;
566 curr_line = _rl_current_display_line ();
568 _rl_move_vert (curr_line);
569 _rl_move_cursor_relative (0, rl_line_buffer); /* XXX is this right */
571 _rl_clear_to_eol (0); /* arg of 0 means to not use spaces */
573 rl_forced_update_display ();
574 rl_display_fixed = 1;
579 /* C-l typed to a line without quoting clears the screen, and then reprints
580 the prompt and the current input line. Given a numeric arg, redraw only
583 rl_clear_screen (count, key)
588 rl_refresh_line (count, key);
592 _rl_clear_screen (); /* calls termcap function to clear screen */
593 rl_forced_update_display ();
594 rl_display_fixed = 1;
600 rl_skip_csi_sequence (count, key)
605 RL_SETSTATE (RL_STATE_MOREINPUT);
608 while (ch >= 0x20 && ch < 0x40);
609 RL_UNSETSTATE (RL_STATE_MOREINPUT);
615 rl_arrow_keys (count, c)
620 RL_SETSTATE(RL_STATE_MOREINPUT);
622 RL_UNSETSTATE(RL_STATE_MOREINPUT);
624 switch (_rl_to_upper (ch))
627 rl_get_previous_history (count, ch);
631 rl_get_next_history (count, ch);
635 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
636 rl_forward_char (count, ch);
638 rl_forward_byte (count, ch);
642 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
643 rl_backward_char (count, ch);
645 rl_backward_byte (count, ch);
655 /* **************************************************************** */
659 /* **************************************************************** */
661 #ifdef HANDLE_MULTIBYTE
662 static char pending_bytes[MB_LEN_MAX];
663 static int pending_bytes_length = 0;
664 static mbstate_t ps = {0};
667 /* Insert the character C at the current location, moving point forward.
668 If C introduces a multibyte sequence, we read the whole sequence and
669 then insert the multibyte char into the line buffer. */
671 _rl_insert_char (count, c)
676 #ifdef HANDLE_MULTIBYTE
678 char incoming[MB_LEN_MAX + 1];
679 int incoming_length = 0;
681 static int stored_count = 0;
687 #if defined (HANDLE_MULTIBYTE)
688 if (MB_CUR_MAX == 1 || rl_byte_oriented)
699 if (stored_count <= 0)
700 stored_count = count;
702 count = stored_count;
705 pending_bytes[pending_bytes_length++] = c;
706 ret = mbrtowc (&wc, pending_bytes, pending_bytes_length, &ps);
708 if (ret == (size_t)-2)
710 /* Bytes too short to compose character, try to wait for next byte.
711 Restore the state of the byte sequence, because in this case the
712 effect of mbstate is undefined. */
716 else if (ret == (size_t)-1)
718 /* Invalid byte sequence for the current locale. Treat first byte
719 as a single character. */
720 incoming[0] = pending_bytes[0];
723 pending_bytes_length--;
724 memmove (pending_bytes, pending_bytes + 1, pending_bytes_length);
725 /* Clear the state of the byte sequence, because in this case the
726 effect of mbstate is undefined. */
727 memset (&ps, 0, sizeof (mbstate_t));
729 else if (ret == (size_t)0)
733 pending_bytes_length--;
734 /* Clear the state of the byte sequence, because in this case the
735 effect of mbstate is undefined. */
736 memset (&ps, 0, sizeof (mbstate_t));
740 /* We successfully read a single multibyte character. */
741 memcpy (incoming, pending_bytes, pending_bytes_length);
742 incoming[pending_bytes_length] = '\0';
743 incoming_length = pending_bytes_length;
744 pending_bytes_length = 0;
747 #endif /* HANDLE_MULTIBYTE */
749 /* If we can optimize, then do it. But don't let people crash
750 readline because of extra large arguments. */
751 if (count > 1 && count <= TEXT_COUNT_MAX)
753 #if defined (HANDLE_MULTIBYTE)
754 string_size = count * incoming_length;
755 string = (char *)xmalloc (1 + string_size);
758 while (i < string_size)
760 strncpy (string + i, incoming, incoming_length);
761 i += incoming_length;
765 #else /* !HANDLE_MULTIBYTE */
766 string = (char *)xmalloc (1 + count);
768 for (i = 0; i < count; i++)
770 #endif /* !HANDLE_MULTIBYTE */
773 rl_insert_text (string);
779 if (count > TEXT_COUNT_MAX)
782 #if defined (HANDLE_MULTIBYTE)
783 string_size = incoming_length * TEXT_COUNT_MAX;
784 string = (char *)xmalloc (1 + string_size);
787 while (i < string_size)
789 strncpy (string + i, incoming, incoming_length);
790 i += incoming_length;
795 decreaser = (count > TEXT_COUNT_MAX) ? TEXT_COUNT_MAX : count;
796 string[decreaser*incoming_length] = '\0';
797 rl_insert_text (string);
804 #else /* !HANDLE_MULTIBYTE */
805 char str[TEXT_COUNT_MAX+1];
807 for (i = 0; i < TEXT_COUNT_MAX; i++)
812 decreaser = (count > TEXT_COUNT_MAX ? TEXT_COUNT_MAX : count);
813 str[decreaser] = '\0';
814 rl_insert_text (str);
817 #endif /* !HANDLE_MULTIBYTE */
822 if (MB_CUR_MAX == 1 || rl_byte_oriented)
824 /* We are inserting a single character.
825 If there is pending input, then make a string of all of the
826 pending characters that are bound to rl_insert, and insert
827 them all. Don't do this if we're current reading input from
829 if ((RL_ISSTATE (RL_STATE_MACROINPUT) == 0) && _rl_pushed_input_available ())
830 _rl_insert_typein (c);
833 /* Inserting a single character. */
838 rl_insert_text (str);
841 #if defined (HANDLE_MULTIBYTE)
844 rl_insert_text (incoming);
852 /* Overwrite the character at point (or next COUNT characters) with C.
853 If C introduces a multibyte character sequence, read the entire sequence
854 before starting the overwrite loop. */
856 _rl_overwrite_char (count, c)
860 #if defined (HANDLE_MULTIBYTE)
861 char mbkey[MB_LEN_MAX];
864 /* Read an entire multibyte character sequence to insert COUNT times. */
865 if (count > 0 && MB_CUR_MAX > 1 && rl_byte_oriented == 0)
866 k = _rl_read_mbstring (c, mbkey, MB_LEN_MAX);
869 rl_begin_undo_group ();
871 for (i = 0; i < count; i++)
873 #if defined (HANDLE_MULTIBYTE)
874 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
875 rl_insert_text (mbkey);
878 _rl_insert_char (1, c);
880 if (rl_point < rl_end)
884 rl_end_undo_group ();
893 return (rl_insert_mode == RL_IM_INSERT ? _rl_insert_char (count, c)
894 : _rl_overwrite_char (count, c));
897 /* Insert the next typed character verbatim. */
899 _rl_insert_next (count)
904 RL_SETSTATE(RL_STATE_MOREINPUT);
906 RL_UNSETSTATE(RL_STATE_MOREINPUT);
911 if (RL_ISSTATE (RL_STATE_MACRODEF))
912 _rl_add_macro_char (c);
914 #if defined (HANDLE_SIGNALS)
915 if (RL_ISSTATE (RL_STATE_CALLBACK) == 0)
916 _rl_restore_tty_signals ();
919 return (_rl_insert_char (count, c));
922 #if defined (READLINE_CALLBACKS)
924 _rl_insert_next_callback (data)
925 _rl_callback_generic_arg *data;
931 /* Deregister function, let rl_callback_read_char deallocate data */
932 _rl_callback_func = 0;
933 _rl_want_redisplay = 1;
935 return _rl_insert_next (count);
940 rl_quoted_insert (count, key)
943 /* Let's see...should the callback interface futz with signal handling? */
944 #if defined (HANDLE_SIGNALS)
945 if (RL_ISSTATE (RL_STATE_CALLBACK) == 0)
946 _rl_disable_tty_signals ();
949 #if defined (READLINE_CALLBACKS)
950 if (RL_ISSTATE (RL_STATE_CALLBACK))
952 _rl_callback_data = _rl_callback_data_alloc (count);
953 _rl_callback_func = _rl_insert_next_callback;
958 return _rl_insert_next (count);
961 /* Insert a tab character. */
963 rl_tab_insert (count, key)
966 return (_rl_insert_char (count, '\t'));
969 /* What to do when a NEWLINE is pressed. We accept the whole line.
970 KEY is the key that invoked this command. I guess it could have
971 meaning in the future. */
973 rl_newline (count, key)
978 if (_rl_history_preserve_point)
979 _rl_history_saved_point = (rl_point == rl_end) ? -1 : rl_point;
981 RL_SETSTATE(RL_STATE_DONE);
983 #if defined (VI_MODE)
984 if (rl_editing_mode == vi_mode)
986 _rl_vi_done_inserting ();
987 if (_rl_vi_textmod_command (_rl_vi_last_command) == 0) /* XXX */
988 _rl_vi_reset_last ();
992 /* If we've been asked to erase empty lines, suppress the final update,
993 since _rl_update_final calls rl_crlf(). */
994 if (rl_erase_empty_line && rl_point == 0 && rl_end == 0)
1002 /* What to do for some uppercase characters, like meta characters,
1003 and some characters appearing in emacs_ctlx_keymap. This function
1004 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1005 is special cased. */
1007 rl_do_lowercase_version (ignore1, ignore2)
1008 int ignore1, ignore2;
1013 /* This is different from what vi does, so the code's not shared. Emacs
1014 rubout in overwrite mode has one oddity: it replaces a control
1015 character that's displayed as two characters (^X) with two spaces. */
1017 _rl_overwrite_rubout (count, key)
1031 /* L == number of spaces to insert */
1032 for (i = l = 0; i < count; i++)
1034 rl_backward_char (1, key);
1035 l += rl_character_len (rl_line_buffer[rl_point], rl_point); /* not exactly right */
1038 rl_begin_undo_group ();
1040 if (count > 1 || rl_explicit_arg)
1041 rl_kill_text (opoint, rl_point);
1043 rl_delete_text (opoint, rl_point);
1045 /* Emacs puts point at the beginning of the sequence of spaces. */
1046 if (rl_point < rl_end)
1049 _rl_insert_char (l, ' ');
1053 rl_end_undo_group ();
1058 /* Rubout the character behind point. */
1060 rl_rubout (count, key)
1064 return (rl_delete (-count, key));
1072 if (rl_insert_mode == RL_IM_OVERWRITE)
1073 return (_rl_overwrite_rubout (count, key));
1075 return (_rl_rubout_char (count, key));
1079 _rl_rubout_char (count, key)
1085 /* Duplicated code because this is called from other parts of the library. */
1087 return (rl_delete (-count, key));
1095 orig_point = rl_point;
1096 if (count > 1 || rl_explicit_arg)
1098 rl_backward_char (count, key);
1099 rl_kill_text (orig_point, rl_point);
1101 else if (MB_CUR_MAX == 1 || rl_byte_oriented)
1103 c = rl_line_buffer[--rl_point];
1104 rl_delete_text (rl_point, orig_point);
1105 /* The erase-at-end-of-line hack is of questionable merit now. */
1106 if (rl_point == rl_end && ISPRINT (c) && _rl_last_c_pos)
1109 l = rl_character_len (c, rl_point);
1110 _rl_erase_at_end_of_line (l);
1115 rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO);
1116 rl_delete_text (rl_point, orig_point);
1122 /* Delete the character under the cursor. Given a numeric argument,
1123 kill that many characters instead. */
1125 rl_delete (count, key)
1131 return (_rl_rubout_char (-count, key));
1133 if (rl_point == rl_end)
1139 if (count > 1 || rl_explicit_arg)
1142 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1143 rl_forward_char (count, key);
1145 rl_forward_byte (count, key);
1147 rl_kill_text (xpoint, rl_point);
1152 xpoint = MB_NEXTCHAR (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
1153 rl_delete_text (rl_point, xpoint);
1158 /* Delete the character under the cursor, unless the insertion
1159 point is at the end of the line, in which case the character
1160 behind the cursor is deleted. COUNT is obeyed and may be used
1161 to delete forward or backward that many characters. */
1163 rl_rubout_or_delete (count, key)
1166 if (rl_end != 0 && rl_point == rl_end)
1167 return (_rl_rubout_char (count, key));
1169 return (rl_delete (count, key));
1172 /* Delete all spaces and tabs around point. */
1174 rl_delete_horizontal_space (count, ignore)
1179 while (rl_point && whitespace (rl_line_buffer[rl_point - 1]))
1184 while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
1187 if (start != rl_point)
1189 rl_delete_text (start, rl_point);
1199 /* Like the tcsh editing function delete-char-or-list. The eof character
1200 is caught before this is invoked, so this really does the same thing as
1201 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1203 rl_delete_or_show_completions (count, key)
1206 if (rl_end != 0 && rl_point == rl_end)
1207 return (rl_possible_completions (count, key));
1209 return (rl_delete (count, key));
1212 #ifndef RL_COMMENT_BEGIN_DEFAULT
1213 #define RL_COMMENT_BEGIN_DEFAULT "#"
1216 /* Turn the current line into a comment in shell history.
1217 A K*rn shell style function. */
1219 rl_insert_comment (count, key)
1222 char *rl_comment_text;
1225 rl_beg_of_line (1, key);
1226 rl_comment_text = _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT;
1228 if (rl_explicit_arg == 0)
1229 rl_insert_text (rl_comment_text);
1232 rl_comment_len = strlen (rl_comment_text);
1233 if (STREQN (rl_comment_text, rl_line_buffer, rl_comment_len))
1234 rl_delete_text (rl_point, rl_point + rl_comment_len);
1236 rl_insert_text (rl_comment_text);
1239 (*rl_redisplay_function) ();
1240 rl_newline (1, '\n');
1245 /* **************************************************************** */
1249 /* **************************************************************** */
1251 /* The three kinds of things that we know how to do. */
1256 /* Uppercase the word at point. */
1258 rl_upcase_word (count, key)
1261 return (rl_change_case (count, UpCase));
1264 /* Lowercase the word at point. */
1266 rl_downcase_word (count, key)
1269 return (rl_change_case (count, DownCase));
1272 /* Upcase the first letter, downcase the rest. */
1274 rl_capitalize_word (count, key)
1277 return (rl_change_case (count, CapCase));
1280 /* The meaty function.
1281 Change the case of COUNT words, performing OP on them.
1282 OP is one of UpCase, DownCase, or CapCase.
1283 If a negative argument is given, leave point where it started,
1284 otherwise, leave it where it moves to. */
1286 rl_change_case (count, op)
1289 int start, next, end;
1290 int inword, c, nc, nop;
1291 #if defined (HANDLE_MULTIBYTE)
1293 char mb[MB_LEN_MAX+1];
1300 rl_forward_word (count, 0);
1303 if (op != UpCase && op != DownCase && op != CapCase)
1312 #if defined (HANDLE_MULTIBYTE)
1313 memset (&mps, 0, sizeof (mbstate_t));
1316 /* We are going to modify some text, so let's prepare to undo it. */
1317 rl_modifying (start, end);
1322 c = _rl_char_value (rl_line_buffer, start);
1323 /* This assumes that the upper and lower case versions are the same width. */
1324 next = MB_NEXTCHAR (rl_line_buffer, start, 1, MB_FIND_NONZERO);
1326 if (_rl_walphabetic (c) == 0)
1335 nop = inword ? DownCase : UpCase;
1340 if (MB_CUR_MAX == 1 || rl_byte_oriented || isascii (c))
1342 nc = (nop == UpCase) ? _rl_to_upper (c) : _rl_to_lower (c);
1343 rl_line_buffer[start] = nc;
1345 #if defined (HANDLE_MULTIBYTE)
1348 m = mbrtowc (&wc, rl_line_buffer + start, end - start, &mps);
1349 if (MB_INVALIDCH (m))
1350 wc = (wchar_t)rl_line_buffer[start];
1351 else if (MB_NULLWCH (m))
1353 nwc = (nop == UpCase) ? _rl_to_wupper (wc) : _rl_to_wlower (wc);
1354 if (nwc != wc) /* just skip unchanged characters */
1356 mlen = wcrtomb (mb, nwc, &mps);
1359 /* Assume the same width */
1360 strncpy (rl_line_buffer + start, mb, mlen);
1372 /* **************************************************************** */
1376 /* **************************************************************** */
1378 /* Transpose the words at point. If point is at the end of the line,
1379 transpose the two words before point. */
1381 rl_transpose_words (count, key)
1384 char *word1, *word2;
1385 int w1_beg, w1_end, w2_beg, w2_end;
1386 int orig_point = rl_point;
1391 /* Find the two words. */
1392 rl_forward_word (count, key);
1394 rl_backward_word (1, key);
1396 rl_backward_word (count, key);
1398 rl_forward_word (1, key);
1401 /* Do some check to make sure that there really are two words. */
1402 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
1405 rl_point = orig_point;
1409 /* Get the text of the words. */
1410 word1 = rl_copy_text (w1_beg, w1_end);
1411 word2 = rl_copy_text (w2_beg, w2_end);
1413 /* We are about to do many insertions and deletions. Remember them
1414 as one operation. */
1415 rl_begin_undo_group ();
1417 /* Do the stuff at word2 first, so that we don't have to worry
1418 about word1 moving. */
1420 rl_delete_text (w2_beg, w2_end);
1421 rl_insert_text (word1);
1424 rl_delete_text (w1_beg, w1_end);
1425 rl_insert_text (word2);
1427 /* This is exactly correct since the text before this point has not
1428 changed in length. */
1431 /* I think that does it. */
1432 rl_end_undo_group ();
1439 /* Transpose the characters at point. If point is at the end of the line,
1440 then transpose the characters before point. */
1442 rl_transpose_chars (count, key)
1445 #if defined (HANDLE_MULTIBYTE)
1451 int char_length, prev_point;
1456 if (!rl_point || rl_end < 2)
1462 rl_begin_undo_group ();
1464 if (rl_point == rl_end)
1466 rl_point = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_NONZERO);
1470 prev_point = rl_point;
1471 rl_point = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_NONZERO);
1473 #if defined (HANDLE_MULTIBYTE)
1474 char_length = prev_point - rl_point;
1475 dummy = (char *)xmalloc (char_length + 1);
1476 for (i = 0; i < char_length; i++)
1477 dummy[i] = rl_line_buffer[rl_point + i];
1480 dummy[0] = rl_line_buffer[rl_point];
1481 dummy[char_length = 1] = '\0';
1484 rl_delete_text (rl_point, rl_point + char_length);
1486 rl_point = _rl_find_next_mbchar (rl_line_buffer, rl_point, count, MB_FIND_NONZERO);
1489 rl_insert_text (dummy);
1490 rl_end_undo_group ();
1492 #if defined (HANDLE_MULTIBYTE)
1499 /* **************************************************************** */
1501 /* Character Searching */
1503 /* **************************************************************** */
1506 #if defined (HANDLE_MULTIBYTE)
1507 _rl_char_search_internal (count, dir, smbchar, len)
1512 _rl_char_search_internal (count, dir, schar)
1513 int count, dir, schar;
1517 #if defined (HANDLE_MULTIBYTE)
1525 inc = (dir < 0) ? -1 : 1;
1528 if ((dir < 0 && pos <= 0) || (dir > 0 && pos >= rl_end))
1534 #if defined (HANDLE_MULTIBYTE)
1535 pos = (inc > 0) ? _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY)
1536 : _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY);
1542 #if defined (HANDLE_MULTIBYTE)
1543 if (_rl_is_mbchar_matched (rl_line_buffer, pos, rl_end, smbchar, len))
1545 if (rl_line_buffer[pos] == schar)
1550 rl_point = (dir == BTO) ? _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY)
1553 rl_point = (dir == FTO) ? _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY)
1557 #if defined (HANDLE_MULTIBYTE)
1561 #if defined (HANDLE_MULTIBYTE)
1562 while ((dir < 0) ? (pos = _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY)) != prepos
1563 : (pos = _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY)) != prepos);
1565 while ((dir < 0) ? pos-- : ++pos < rl_end);
1571 /* Search COUNT times for a character read from the current input stream.
1572 FDIR is the direction to search if COUNT is non-negative; otherwise
1573 the search goes in BDIR. So much is dependent on HANDLE_MULTIBYTE
1574 that there are two separate versions of this function. */
1575 #if defined (HANDLE_MULTIBYTE)
1577 _rl_char_search (count, fdir, bdir)
1578 int count, fdir, bdir;
1580 char mbchar[MB_LEN_MAX];
1583 mb_len = _rl_read_mbchar (mbchar, MB_LEN_MAX);
1589 return (_rl_char_search_internal (-count, bdir, mbchar, mb_len));
1591 return (_rl_char_search_internal (count, fdir, mbchar, mb_len));
1593 #else /* !HANDLE_MULTIBYTE */
1595 _rl_char_search (count, fdir, bdir)
1596 int count, fdir, bdir;
1600 RL_SETSTATE(RL_STATE_MOREINPUT);
1602 RL_UNSETSTATE(RL_STATE_MOREINPUT);
1608 return (_rl_char_search_internal (-count, bdir, c));
1610 return (_rl_char_search_internal (count, fdir, c));
1612 #endif /* !HANDLE_MULTIBYTE */
1614 #if defined (READLINE_CALLBACKS)
1616 _rl_char_search_callback (data)
1617 _rl_callback_generic_arg *data;
1619 _rl_callback_func = 0;
1620 _rl_want_redisplay = 1;
1622 return (_rl_char_search (data->count, data->i1, data->i2));
1627 rl_char_search (count, key)
1630 #if defined (READLINE_CALLBACKS)
1631 if (RL_ISSTATE (RL_STATE_CALLBACK))
1633 _rl_callback_data = _rl_callback_data_alloc (count);
1634 _rl_callback_data->i1 = FFIND;
1635 _rl_callback_data->i2 = BFIND;
1636 _rl_callback_func = _rl_char_search_callback;
1641 return (_rl_char_search (count, FFIND, BFIND));
1645 rl_backward_char_search (count, key)
1648 #if defined (READLINE_CALLBACKS)
1649 if (RL_ISSTATE (RL_STATE_CALLBACK))
1651 _rl_callback_data = _rl_callback_data_alloc (count);
1652 _rl_callback_data->i1 = BFIND;
1653 _rl_callback_data->i2 = FFIND;
1654 _rl_callback_func = _rl_char_search_callback;
1659 return (_rl_char_search (count, BFIND, FFIND));
1662 /* **************************************************************** */
1664 /* The Mark and the Region. */
1666 /* **************************************************************** */
1668 /* Set the mark at POSITION. */
1670 _rl_set_mark_at_pos (position)
1673 if (position > rl_end)
1680 /* A bindable command to set the mark. */
1682 rl_set_mark (count, key)
1685 return (_rl_set_mark_at_pos (rl_explicit_arg ? count : rl_point));
1688 /* Exchange the position of mark and point. */
1690 rl_exchange_point_and_mark (count, key)
1693 if (rl_mark > rl_end)
1702 SWAP (rl_point, rl_mark);