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 pased 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_any_typein ())
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 defined (HANDLE_SIGNALS)
912 if (RL_ISSTATE (RL_STATE_CALLBACK) == 0)
913 _rl_restore_tty_signals ();
916 return (_rl_insert_char (count, c));
919 #if defined (READLINE_CALLBACKS)
921 _rl_insert_next_callback (data)
922 _rl_callback_generic_arg *data;
928 /* Deregister function, let rl_callback_read_char deallocate data */
929 _rl_callback_func = 0;
930 _rl_want_redisplay = 1;
932 return _rl_insert_next (count);
937 rl_quoted_insert (count, key)
940 /* Let's see...should the callback interface futz with signal handling? */
941 #if defined (HANDLE_SIGNALS)
942 if (RL_ISSTATE (RL_STATE_CALLBACK) == 0)
943 _rl_disable_tty_signals ();
946 #if defined (READLINE_CALLBACKS)
947 if (RL_ISSTATE (RL_STATE_CALLBACK))
949 _rl_callback_data = _rl_callback_data_alloc (count);
950 _rl_callback_func = _rl_insert_next_callback;
955 return _rl_insert_next (count);
958 /* Insert a tab character. */
960 rl_tab_insert (count, key)
963 return (_rl_insert_char (count, '\t'));
966 /* What to do when a NEWLINE is pressed. We accept the whole line.
967 KEY is the key that invoked this command. I guess it could have
968 meaning in the future. */
970 rl_newline (count, key)
975 if (_rl_history_preserve_point)
976 _rl_history_saved_point = (rl_point == rl_end) ? -1 : rl_point;
978 RL_SETSTATE(RL_STATE_DONE);
980 #if defined (VI_MODE)
981 if (rl_editing_mode == vi_mode)
983 _rl_vi_done_inserting ();
984 if (_rl_vi_textmod_command (_rl_vi_last_command) == 0) /* XXX */
985 _rl_vi_reset_last ();
989 /* If we've been asked to erase empty lines, suppress the final update,
990 since _rl_update_final calls rl_crlf(). */
991 if (rl_erase_empty_line && rl_point == 0 && rl_end == 0)
999 /* What to do for some uppercase characters, like meta characters,
1000 and some characters appearing in emacs_ctlx_keymap. This function
1001 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1002 is special cased. */
1004 rl_do_lowercase_version (ignore1, ignore2)
1005 int ignore1, ignore2;
1010 /* This is different from what vi does, so the code's not shared. Emacs
1011 rubout in overwrite mode has one oddity: it replaces a control
1012 character that's displayed as two characters (^X) with two spaces. */
1014 _rl_overwrite_rubout (count, key)
1028 /* L == number of spaces to insert */
1029 for (i = l = 0; i < count; i++)
1031 rl_backward_char (1, key);
1032 l += rl_character_len (rl_line_buffer[rl_point], rl_point); /* not exactly right */
1035 rl_begin_undo_group ();
1037 if (count > 1 || rl_explicit_arg)
1038 rl_kill_text (opoint, rl_point);
1040 rl_delete_text (opoint, rl_point);
1042 /* Emacs puts point at the beginning of the sequence of spaces. */
1043 if (rl_point < rl_end)
1046 _rl_insert_char (l, ' ');
1050 rl_end_undo_group ();
1055 /* Rubout the character behind point. */
1057 rl_rubout (count, key)
1061 return (rl_delete (-count, key));
1069 if (rl_insert_mode == RL_IM_OVERWRITE)
1070 return (_rl_overwrite_rubout (count, key));
1072 return (_rl_rubout_char (count, key));
1076 _rl_rubout_char (count, key)
1082 /* Duplicated code because this is called from other parts of the library. */
1084 return (rl_delete (-count, key));
1092 orig_point = rl_point;
1093 if (count > 1 || rl_explicit_arg)
1095 rl_backward_char (count, key);
1096 rl_kill_text (orig_point, rl_point);
1098 else if (MB_CUR_MAX == 1 || rl_byte_oriented)
1100 c = rl_line_buffer[--rl_point];
1101 rl_delete_text (rl_point, orig_point);
1102 /* The erase-at-end-of-line hack is of questionable merit now. */
1103 if (rl_point == rl_end && ISPRINT (c) && _rl_last_c_pos)
1106 l = rl_character_len (c, rl_point);
1107 _rl_erase_at_end_of_line (l);
1112 rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO);
1113 rl_delete_text (rl_point, orig_point);
1119 /* Delete the character under the cursor. Given a numeric argument,
1120 kill that many characters instead. */
1122 rl_delete (count, key)
1128 return (_rl_rubout_char (-count, key));
1130 if (rl_point == rl_end)
1136 if (count > 1 || rl_explicit_arg)
1139 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1140 rl_forward_char (count, key);
1142 rl_forward_byte (count, key);
1144 rl_kill_text (xpoint, rl_point);
1149 xpoint = MB_NEXTCHAR (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
1150 rl_delete_text (rl_point, xpoint);
1155 /* Delete the character under the cursor, unless the insertion
1156 point is at the end of the line, in which case the character
1157 behind the cursor is deleted. COUNT is obeyed and may be used
1158 to delete forward or backward that many characters. */
1160 rl_rubout_or_delete (count, key)
1163 if (rl_end != 0 && rl_point == rl_end)
1164 return (_rl_rubout_char (count, key));
1166 return (rl_delete (count, key));
1169 /* Delete all spaces and tabs around point. */
1171 rl_delete_horizontal_space (count, ignore)
1176 while (rl_point && whitespace (rl_line_buffer[rl_point - 1]))
1181 while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
1184 if (start != rl_point)
1186 rl_delete_text (start, rl_point);
1196 /* Like the tcsh editing function delete-char-or-list. The eof character
1197 is caught before this is invoked, so this really does the same thing as
1198 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1200 rl_delete_or_show_completions (count, key)
1203 if (rl_end != 0 && rl_point == rl_end)
1204 return (rl_possible_completions (count, key));
1206 return (rl_delete (count, key));
1209 #ifndef RL_COMMENT_BEGIN_DEFAULT
1210 #define RL_COMMENT_BEGIN_DEFAULT "#"
1213 /* Turn the current line into a comment in shell history.
1214 A K*rn shell style function. */
1216 rl_insert_comment (count, key)
1219 char *rl_comment_text;
1222 rl_beg_of_line (1, key);
1223 rl_comment_text = _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT;
1225 if (rl_explicit_arg == 0)
1226 rl_insert_text (rl_comment_text);
1229 rl_comment_len = strlen (rl_comment_text);
1230 if (STREQN (rl_comment_text, rl_line_buffer, rl_comment_len))
1231 rl_delete_text (rl_point, rl_point + rl_comment_len);
1233 rl_insert_text (rl_comment_text);
1236 (*rl_redisplay_function) ();
1237 rl_newline (1, '\n');
1242 /* **************************************************************** */
1246 /* **************************************************************** */
1248 /* The three kinds of things that we know how to do. */
1253 /* Uppercase the word at point. */
1255 rl_upcase_word (count, key)
1258 return (rl_change_case (count, UpCase));
1261 /* Lowercase the word at point. */
1263 rl_downcase_word (count, key)
1266 return (rl_change_case (count, DownCase));
1269 /* Upcase the first letter, downcase the rest. */
1271 rl_capitalize_word (count, key)
1274 return (rl_change_case (count, CapCase));
1277 /* The meaty function.
1278 Change the case of COUNT words, performing OP on them.
1279 OP is one of UpCase, DownCase, or CapCase.
1280 If a negative argument is given, leave point where it started,
1281 otherwise, leave it where it moves to. */
1283 rl_change_case (count, op)
1286 int start, next, end;
1287 int inword, c, nc, nop;
1288 #if defined (HANDLE_MULTIBYTE)
1290 char mb[MB_LEN_MAX+1];
1297 rl_forward_word (count, 0);
1300 if (op != UpCase && op != DownCase && op != CapCase)
1309 #if defined (HANDLE_MULTIBYTE)
1310 memset (&mps, 0, sizeof (mbstate_t));
1313 /* We are going to modify some text, so let's prepare to undo it. */
1314 rl_modifying (start, end);
1319 c = _rl_char_value (rl_line_buffer, start);
1320 /* This assumes that the upper and lower case versions are the same width. */
1321 next = MB_NEXTCHAR (rl_line_buffer, start, 1, MB_FIND_NONZERO);
1323 if (_rl_walphabetic (c) == 0)
1332 nop = inword ? DownCase : UpCase;
1337 if (MB_CUR_MAX == 1 || rl_byte_oriented || isascii (c))
1339 nc = (nop == UpCase) ? _rl_to_upper (c) : _rl_to_lower (c);
1340 rl_line_buffer[start] = nc;
1342 #if defined (HANDLE_MULTIBYTE)
1345 m = mbrtowc (&wc, rl_line_buffer + start, end - start, &mps);
1346 if (MB_INVALIDCH (m))
1347 wc = (wchar_t)rl_line_buffer[start];
1348 else if (MB_NULLWCH (m))
1350 nwc = (nop == UpCase) ? _rl_to_wupper (wc) : _rl_to_wlower (wc);
1351 if (nwc != wc) /* just skip unchanged characters */
1353 mlen = wcrtomb (mb, nwc, &mps);
1356 /* Assume the same width */
1357 strncpy (rl_line_buffer + start, mb, mlen);
1369 /* **************************************************************** */
1373 /* **************************************************************** */
1375 /* Transpose the words at point. If point is at the end of the line,
1376 transpose the two words before point. */
1378 rl_transpose_words (count, key)
1381 char *word1, *word2;
1382 int w1_beg, w1_end, w2_beg, w2_end;
1383 int orig_point = rl_point;
1388 /* Find the two words. */
1389 rl_forward_word (count, key);
1391 rl_backward_word (1, key);
1393 rl_backward_word (count, key);
1395 rl_forward_word (1, key);
1398 /* Do some check to make sure that there really are two words. */
1399 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
1402 rl_point = orig_point;
1406 /* Get the text of the words. */
1407 word1 = rl_copy_text (w1_beg, w1_end);
1408 word2 = rl_copy_text (w2_beg, w2_end);
1410 /* We are about to do many insertions and deletions. Remember them
1411 as one operation. */
1412 rl_begin_undo_group ();
1414 /* Do the stuff at word2 first, so that we don't have to worry
1415 about word1 moving. */
1417 rl_delete_text (w2_beg, w2_end);
1418 rl_insert_text (word1);
1421 rl_delete_text (w1_beg, w1_end);
1422 rl_insert_text (word2);
1424 /* This is exactly correct since the text before this point has not
1425 changed in length. */
1428 /* I think that does it. */
1429 rl_end_undo_group ();
1436 /* Transpose the characters at point. If point is at the end of the line,
1437 then transpose the characters before point. */
1439 rl_transpose_chars (count, key)
1442 #if defined (HANDLE_MULTIBYTE)
1448 int char_length, prev_point;
1453 if (!rl_point || rl_end < 2)
1459 rl_begin_undo_group ();
1461 if (rl_point == rl_end)
1463 rl_point = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_NONZERO);
1467 prev_point = rl_point;
1468 rl_point = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_NONZERO);
1470 #if defined (HANDLE_MULTIBYTE)
1471 char_length = prev_point - rl_point;
1472 dummy = (char *)xmalloc (char_length + 1);
1473 for (i = 0; i < char_length; i++)
1474 dummy[i] = rl_line_buffer[rl_point + i];
1477 dummy[0] = rl_line_buffer[rl_point];
1478 dummy[char_length = 1] = '\0';
1481 rl_delete_text (rl_point, rl_point + char_length);
1483 rl_point = _rl_find_next_mbchar (rl_line_buffer, rl_point, count, MB_FIND_NONZERO);
1486 rl_insert_text (dummy);
1487 rl_end_undo_group ();
1489 #if defined (HANDLE_MULTIBYTE)
1496 /* **************************************************************** */
1498 /* Character Searching */
1500 /* **************************************************************** */
1503 #if defined (HANDLE_MULTIBYTE)
1504 _rl_char_search_internal (count, dir, smbchar, len)
1509 _rl_char_search_internal (count, dir, schar)
1510 int count, dir, schar;
1514 #if defined (HANDLE_MULTIBYTE)
1522 inc = (dir < 0) ? -1 : 1;
1525 if ((dir < 0 && pos <= 0) || (dir > 0 && pos >= rl_end))
1531 #if defined (HANDLE_MULTIBYTE)
1532 pos = (inc > 0) ? _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY)
1533 : _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY);
1539 #if defined (HANDLE_MULTIBYTE)
1540 if (_rl_is_mbchar_matched (rl_line_buffer, pos, rl_end, smbchar, len))
1542 if (rl_line_buffer[pos] == schar)
1547 rl_point = (dir == BTO) ? _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY)
1550 rl_point = (dir == FTO) ? _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY)
1554 #if defined (HANDLE_MULTIBYTE)
1558 #if defined (HANDLE_MULTIBYTE)
1559 while ((dir < 0) ? (pos = _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY)) != prepos
1560 : (pos = _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY)) != prepos);
1562 while ((dir < 0) ? pos-- : ++pos < rl_end);
1568 /* Search COUNT times for a character read from the current input stream.
1569 FDIR is the direction to search if COUNT is non-negative; otherwise
1570 the search goes in BDIR. So much is dependent on HANDLE_MULTIBYTE
1571 that there are two separate versions of this function. */
1572 #if defined (HANDLE_MULTIBYTE)
1574 _rl_char_search (count, fdir, bdir)
1575 int count, fdir, bdir;
1577 char mbchar[MB_LEN_MAX];
1580 mb_len = _rl_read_mbchar (mbchar, MB_LEN_MAX);
1586 return (_rl_char_search_internal (-count, bdir, mbchar, mb_len));
1588 return (_rl_char_search_internal (count, fdir, mbchar, mb_len));
1590 #else /* !HANDLE_MULTIBYTE */
1592 _rl_char_search (count, fdir, bdir)
1593 int count, fdir, bdir;
1597 RL_SETSTATE(RL_STATE_MOREINPUT);
1599 RL_UNSETSTATE(RL_STATE_MOREINPUT);
1605 return (_rl_char_search_internal (-count, bdir, c));
1607 return (_rl_char_search_internal (count, fdir, c));
1609 #endif /* !HANDLE_MULTIBYTE */
1611 #if defined (READLINE_CALLBACKS)
1613 _rl_char_search_callback (data)
1614 _rl_callback_generic_arg *data;
1616 _rl_callback_func = 0;
1617 _rl_want_redisplay = 1;
1619 return (_rl_char_search (data->count, data->i1, data->i2));
1624 rl_char_search (count, key)
1627 #if defined (READLINE_CALLBACKS)
1628 if (RL_ISSTATE (RL_STATE_CALLBACK))
1630 _rl_callback_data = _rl_callback_data_alloc (count);
1631 _rl_callback_data->i1 = FFIND;
1632 _rl_callback_data->i2 = BFIND;
1633 _rl_callback_func = _rl_char_search_callback;
1638 return (_rl_char_search (count, FFIND, BFIND));
1642 rl_backward_char_search (count, key)
1645 #if defined (READLINE_CALLBACKS)
1646 if (RL_ISSTATE (RL_STATE_CALLBACK))
1648 _rl_callback_data = _rl_callback_data_alloc (count);
1649 _rl_callback_data->i1 = BFIND;
1650 _rl_callback_data->i2 = FFIND;
1651 _rl_callback_func = _rl_char_search_callback;
1656 return (_rl_char_search (count, BFIND, FFIND));
1659 /* **************************************************************** */
1661 /* The Mark and the Region. */
1663 /* **************************************************************** */
1665 /* Set the mark at POSITION. */
1667 _rl_set_mark_at_pos (position)
1670 if (position > rl_end)
1677 /* A bindable command to set the mark. */
1679 rl_set_mark (count, key)
1682 return (_rl_set_mark_at_pos (rl_explicit_arg ? count : rl_point));
1685 /* Exchange the position of mark and point. */
1687 rl_exchange_point_and_mark (count, key)
1690 if (rl_mark > rl_end)
1699 SWAP (rl_point, rl_mark);