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 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_forced_update_display ();
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
);
626 switch (_rl_to_upper (ch
))
629 rl_get_previous_history (count
, ch
);
633 rl_get_next_history (count
, ch
);
637 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
638 rl_forward_char (count
, ch
);
640 rl_forward_byte (count
, ch
);
644 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
645 rl_backward_char (count
, ch
);
647 rl_backward_byte (count
, ch
);
657 /* **************************************************************** */
661 /* **************************************************************** */
663 #ifdef HANDLE_MULTIBYTE
664 static char pending_bytes
[MB_LEN_MAX
];
665 static int pending_bytes_length
= 0;
666 static mbstate_t ps
= {0};
669 /* Insert the character C at the current location, moving point forward.
670 If C introduces a multibyte sequence, we read the whole sequence and
671 then insert the multibyte char into the line buffer. */
673 _rl_insert_char (count
, c
)
678 #ifdef HANDLE_MULTIBYTE
680 char incoming
[MB_LEN_MAX
+ 1];
681 int incoming_length
= 0;
683 static int stored_count
= 0;
689 #if defined (HANDLE_MULTIBYTE)
690 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
701 if (stored_count
<= 0)
702 stored_count
= count
;
704 count
= stored_count
;
707 pending_bytes
[pending_bytes_length
++] = c
;
708 ret
= mbrtowc (&wc
, pending_bytes
, pending_bytes_length
, &ps
);
710 if (ret
== (size_t)-2)
712 /* Bytes too short to compose character, try to wait for next byte.
713 Restore the state of the byte sequence, because in this case the
714 effect of mbstate is undefined. */
718 else if (ret
== (size_t)-1)
720 /* Invalid byte sequence for the current locale. Treat first byte
721 as a single character. */
722 incoming
[0] = pending_bytes
[0];
725 pending_bytes_length
--;
726 memmove (pending_bytes
, pending_bytes
+ 1, pending_bytes_length
);
727 /* Clear the state of the byte sequence, because in this case the
728 effect of mbstate is undefined. */
729 memset (&ps
, 0, sizeof (mbstate_t));
731 else if (ret
== (size_t)0)
735 pending_bytes_length
--;
736 /* Clear the state of the byte sequence, because in this case the
737 effect of mbstate is undefined. */
738 memset (&ps
, 0, sizeof (mbstate_t));
742 /* We successfully read a single multibyte character. */
743 memcpy (incoming
, pending_bytes
, pending_bytes_length
);
744 incoming
[pending_bytes_length
] = '\0';
745 incoming_length
= pending_bytes_length
;
746 pending_bytes_length
= 0;
749 #endif /* HANDLE_MULTIBYTE */
751 /* If we can optimize, then do it. But don't let people crash
752 readline because of extra large arguments. */
753 if (count
> 1 && count
<= TEXT_COUNT_MAX
)
755 #if defined (HANDLE_MULTIBYTE)
756 string_size
= count
* incoming_length
;
757 string
= (char *)xmalloc (1 + string_size
);
760 while (i
< string_size
)
762 strncpy (string
+ i
, incoming
, incoming_length
);
763 i
+= incoming_length
;
767 #else /* !HANDLE_MULTIBYTE */
768 string
= (char *)xmalloc (1 + count
);
770 for (i
= 0; i
< count
; i
++)
772 #endif /* !HANDLE_MULTIBYTE */
775 rl_insert_text (string
);
781 if (count
> TEXT_COUNT_MAX
)
784 #if defined (HANDLE_MULTIBYTE)
785 string_size
= incoming_length
* TEXT_COUNT_MAX
;
786 string
= (char *)xmalloc (1 + string_size
);
789 while (i
< string_size
)
791 strncpy (string
+ i
, incoming
, incoming_length
);
792 i
+= incoming_length
;
797 decreaser
= (count
> TEXT_COUNT_MAX
) ? TEXT_COUNT_MAX
: count
;
798 string
[decreaser
*incoming_length
] = '\0';
799 rl_insert_text (string
);
806 #else /* !HANDLE_MULTIBYTE */
807 char str
[TEXT_COUNT_MAX
+1];
809 for (i
= 0; i
< TEXT_COUNT_MAX
; i
++)
814 decreaser
= (count
> TEXT_COUNT_MAX
? TEXT_COUNT_MAX
: count
);
815 str
[decreaser
] = '\0';
816 rl_insert_text (str
);
819 #endif /* !HANDLE_MULTIBYTE */
824 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
826 /* We are inserting a single character.
827 If there is pending input, then make a string of all of the
828 pending characters that are bound to rl_insert, and insert
829 them all. Don't do this if we're current reading input from
831 if ((RL_ISSTATE (RL_STATE_MACROINPUT
) == 0) && _rl_pushed_input_available ())
832 _rl_insert_typein (c
);
835 /* Inserting a single character. */
840 rl_insert_text (str
);
843 #if defined (HANDLE_MULTIBYTE)
846 rl_insert_text (incoming
);
854 /* Overwrite the character at point (or next COUNT characters) with C.
855 If C introduces a multibyte character sequence, read the entire sequence
856 before starting the overwrite loop. */
858 _rl_overwrite_char (count
, c
)
862 #if defined (HANDLE_MULTIBYTE)
863 char mbkey
[MB_LEN_MAX
];
866 /* Read an entire multibyte character sequence to insert COUNT times. */
867 if (count
> 0 && MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
868 k
= _rl_read_mbstring (c
, mbkey
, MB_LEN_MAX
);
871 rl_begin_undo_group ();
873 for (i
= 0; i
< count
; i
++)
875 #if defined (HANDLE_MULTIBYTE)
876 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
877 rl_insert_text (mbkey
);
880 _rl_insert_char (1, c
);
882 if (rl_point
< rl_end
)
886 rl_end_undo_group ();
897 r
= (rl_insert_mode
== RL_IM_INSERT
) ? _rl_insert_char (count
, c
) : _rl_overwrite_char (count
, c
);
899 /* XXX -- attempt to batch-insert pending input that maps to self-insert */
901 n
= (unsigned short)-2;
902 while (_rl_optimize_typeahead
&&
903 (RL_ISSTATE (RL_STATE_INPUTPENDING
|RL_STATE_MACROINPUT
) == 0) &&
904 _rl_pushed_input_available () == 0 &&
905 _rl_input_queued (0) &&
906 (n
= rl_read_key ()) > 0 &&
907 _rl_keymap
[(unsigned char)n
].type
== ISFUNC
&&
908 _rl_keymap
[(unsigned char)n
].function
== rl_insert
)
910 r
= (rl_insert_mode
== RL_IM_INSERT
) ? _rl_insert_char (1, n
) : _rl_overwrite_char (1, n
);
911 /* _rl_insert_char keeps its own set of pending characters to compose a
912 complete multibyte character, and only returns 1 if it sees a character
913 that's part of a multibyte character but too short to complete one. We
914 can try to read another character in the hopes that we will get the
915 next one or just punt. Right now we try to read another character.
916 We don't want to call rl_insert_next if _rl_insert_char has already
917 stored the character in the pending_bytes array because that will
918 result in doubled input. */
919 n
= (unsigned short)-2;
920 x
++; /* count of bytes of typeahead read, currently unused */
921 if (r
== 1) /* read partial multibyte character */
923 if (rl_done
|| r
!= 0)
927 if (n
!= (unsigned short)-2) /* -2 = sentinel value for having inserted N */
928 r
= rl_execute_next (n
);
933 /* Insert the next typed character verbatim. */
935 _rl_insert_next (count
)
940 RL_SETSTATE(RL_STATE_MOREINPUT
);
942 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
947 if (RL_ISSTATE (RL_STATE_MACRODEF
))
948 _rl_add_macro_char (c
);
950 #if defined (HANDLE_SIGNALS)
951 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
952 _rl_restore_tty_signals ();
955 return (_rl_insert_char (count
, c
));
958 #if defined (READLINE_CALLBACKS)
960 _rl_insert_next_callback (data
)
961 _rl_callback_generic_arg
*data
;
967 /* Deregister function, let rl_callback_read_char deallocate data */
968 _rl_callback_func
= 0;
969 _rl_want_redisplay
= 1;
971 return _rl_insert_next (count
);
976 rl_quoted_insert (count
, key
)
979 /* Let's see...should the callback interface futz with signal handling? */
980 #if defined (HANDLE_SIGNALS)
981 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
982 _rl_disable_tty_signals ();
985 #if defined (READLINE_CALLBACKS)
986 if (RL_ISSTATE (RL_STATE_CALLBACK
))
988 _rl_callback_data
= _rl_callback_data_alloc (count
);
989 _rl_callback_func
= _rl_insert_next_callback
;
994 return _rl_insert_next (count
);
997 /* Insert a tab character. */
999 rl_tab_insert (count
, key
)
1002 return (_rl_insert_char (count
, '\t'));
1005 /* What to do when a NEWLINE is pressed. We accept the whole line.
1006 KEY is the key that invoked this command. I guess it could have
1007 meaning in the future. */
1009 rl_newline (count
, key
)
1014 if (_rl_history_preserve_point
)
1015 _rl_history_saved_point
= (rl_point
== rl_end
) ? -1 : rl_point
;
1017 RL_SETSTATE(RL_STATE_DONE
);
1019 #if defined (VI_MODE)
1020 if (rl_editing_mode
== vi_mode
)
1022 _rl_vi_done_inserting ();
1023 if (_rl_vi_textmod_command (_rl_vi_last_command
) == 0) /* XXX */
1024 _rl_vi_reset_last ();
1026 #endif /* VI_MODE */
1028 /* If we've been asked to erase empty lines, suppress the final update,
1029 since _rl_update_final calls rl_crlf(). */
1030 if (rl_erase_empty_line
&& rl_point
== 0 && rl_end
== 0)
1034 _rl_update_final ();
1038 /* What to do for some uppercase characters, like meta characters,
1039 and some characters appearing in emacs_ctlx_keymap. This function
1040 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1041 is special cased. */
1043 rl_do_lowercase_version (ignore1
, ignore2
)
1044 int ignore1
, ignore2
;
1049 /* This is different from what vi does, so the code's not shared. Emacs
1050 rubout in overwrite mode has one oddity: it replaces a control
1051 character that's displayed as two characters (^X) with two spaces. */
1053 _rl_overwrite_rubout (count
, key
)
1067 /* L == number of spaces to insert */
1068 for (i
= l
= 0; i
< count
; i
++)
1070 rl_backward_char (1, key
);
1071 l
+= rl_character_len (rl_line_buffer
[rl_point
], rl_point
); /* not exactly right */
1074 rl_begin_undo_group ();
1076 if (count
> 1 || rl_explicit_arg
)
1077 rl_kill_text (opoint
, rl_point
);
1079 rl_delete_text (opoint
, rl_point
);
1081 /* Emacs puts point at the beginning of the sequence of spaces. */
1082 if (rl_point
< rl_end
)
1085 _rl_insert_char (l
, ' ');
1089 rl_end_undo_group ();
1094 /* Rubout the character behind point. */
1096 rl_rubout (count
, key
)
1100 return (rl_delete (-count
, key
));
1108 if (rl_insert_mode
== RL_IM_OVERWRITE
)
1109 return (_rl_overwrite_rubout (count
, key
));
1111 return (_rl_rubout_char (count
, key
));
1115 _rl_rubout_char (count
, key
)
1121 /* Duplicated code because this is called from other parts of the library. */
1123 return (rl_delete (-count
, key
));
1131 orig_point
= rl_point
;
1132 if (count
> 1 || rl_explicit_arg
)
1134 rl_backward_char (count
, key
);
1135 rl_kill_text (orig_point
, rl_point
);
1137 else if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
1139 c
= rl_line_buffer
[--rl_point
];
1140 rl_delete_text (rl_point
, orig_point
);
1141 /* The erase-at-end-of-line hack is of questionable merit now. */
1142 if (rl_point
== rl_end
&& ISPRINT ((unsigned char)c
) && _rl_last_c_pos
)
1145 l
= rl_character_len (c
, rl_point
);
1146 _rl_erase_at_end_of_line (l
);
1151 rl_point
= _rl_find_prev_mbchar (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1152 rl_delete_text (rl_point
, orig_point
);
1158 /* Delete the character under the cursor. Given a numeric argument,
1159 kill that many characters instead. */
1161 rl_delete (count
, key
)
1167 return (_rl_rubout_char (-count
, key
));
1169 if (rl_point
== rl_end
)
1175 if (count
> 1 || rl_explicit_arg
)
1178 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1179 rl_forward_char (count
, key
);
1181 rl_forward_byte (count
, key
);
1183 rl_kill_text (xpoint
, rl_point
);
1188 xpoint
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
1189 rl_delete_text (rl_point
, xpoint
);
1194 /* Delete the character under the cursor, unless the insertion
1195 point is at the end of the line, in which case the character
1196 behind the cursor is deleted. COUNT is obeyed and may be used
1197 to delete forward or backward that many characters. */
1199 rl_rubout_or_delete (count
, key
)
1202 if (rl_end
!= 0 && rl_point
== rl_end
)
1203 return (_rl_rubout_char (count
, key
));
1205 return (rl_delete (count
, key
));
1208 /* Delete all spaces and tabs around point. */
1210 rl_delete_horizontal_space (count
, ignore
)
1215 while (rl_point
&& whitespace (rl_line_buffer
[rl_point
- 1]))
1220 while (rl_point
< rl_end
&& whitespace (rl_line_buffer
[rl_point
]))
1223 if (start
!= rl_point
)
1225 rl_delete_text (start
, rl_point
);
1235 /* Like the tcsh editing function delete-char-or-list. The eof character
1236 is caught before this is invoked, so this really does the same thing as
1237 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1239 rl_delete_or_show_completions (count
, key
)
1242 if (rl_end
!= 0 && rl_point
== rl_end
)
1243 return (rl_possible_completions (count
, key
));
1245 return (rl_delete (count
, key
));
1248 #ifndef RL_COMMENT_BEGIN_DEFAULT
1249 #define RL_COMMENT_BEGIN_DEFAULT "#"
1252 /* Turn the current line into a comment in shell history.
1253 A K*rn shell style function. */
1255 rl_insert_comment (count
, key
)
1258 char *rl_comment_text
;
1261 rl_beg_of_line (1, key
);
1262 rl_comment_text
= _rl_comment_begin
? _rl_comment_begin
: RL_COMMENT_BEGIN_DEFAULT
;
1264 if (rl_explicit_arg
== 0)
1265 rl_insert_text (rl_comment_text
);
1268 rl_comment_len
= strlen (rl_comment_text
);
1269 if (STREQN (rl_comment_text
, rl_line_buffer
, rl_comment_len
))
1270 rl_delete_text (rl_point
, rl_point
+ rl_comment_len
);
1272 rl_insert_text (rl_comment_text
);
1275 (*rl_redisplay_function
) ();
1276 rl_newline (1, '\n');
1281 /* **************************************************************** */
1285 /* **************************************************************** */
1287 /* The three kinds of things that we know how to do. */
1292 /* Uppercase the word at point. */
1294 rl_upcase_word (count
, key
)
1297 return (rl_change_case (count
, UpCase
));
1300 /* Lowercase the word at point. */
1302 rl_downcase_word (count
, key
)
1305 return (rl_change_case (count
, DownCase
));
1308 /* Upcase the first letter, downcase the rest. */
1310 rl_capitalize_word (count
, key
)
1313 return (rl_change_case (count
, CapCase
));
1316 /* The meaty function.
1317 Change the case of COUNT words, performing OP on them.
1318 OP is one of UpCase, DownCase, or CapCase.
1319 If a negative argument is given, leave point where it started,
1320 otherwise, leave it where it moves to. */
1322 rl_change_case (count
, op
)
1325 int start
, next
, end
;
1326 int inword
, c
, nc
, nop
;
1327 #if defined (HANDLE_MULTIBYTE)
1329 char mb
[MB_LEN_MAX
+1];
1336 rl_forward_word (count
, 0);
1339 if (op
!= UpCase
&& op
!= DownCase
&& op
!= CapCase
)
1348 #if defined (HANDLE_MULTIBYTE)
1349 memset (&mps
, 0, sizeof (mbstate_t));
1352 /* We are going to modify some text, so let's prepare to undo it. */
1353 rl_modifying (start
, end
);
1358 c
= _rl_char_value (rl_line_buffer
, start
);
1359 /* This assumes that the upper and lower case versions are the same width. */
1360 next
= MB_NEXTCHAR (rl_line_buffer
, start
, 1, MB_FIND_NONZERO
);
1362 if (_rl_walphabetic (c
) == 0)
1371 nop
= inword
? DownCase
: UpCase
;
1376 if (MB_CUR_MAX
== 1 || rl_byte_oriented
|| isascii ((unsigned char)c
))
1378 nc
= (nop
== UpCase
) ? _rl_to_upper (c
) : _rl_to_lower (c
);
1379 rl_line_buffer
[start
] = nc
;
1381 #if defined (HANDLE_MULTIBYTE)
1384 m
= mbrtowc (&wc
, rl_line_buffer
+ start
, end
- start
, &mps
);
1385 if (MB_INVALIDCH (m
))
1386 wc
= (wchar_t)rl_line_buffer
[start
];
1387 else if (MB_NULLWCH (m
))
1389 nwc
= (nop
== UpCase
) ? _rl_to_wupper (wc
) : _rl_to_wlower (wc
);
1390 if (nwc
!= wc
) /* just skip unchanged characters */
1392 mlen
= wcrtomb (mb
, nwc
, &mps
);
1395 /* Assume the same width */
1396 strncpy (rl_line_buffer
+ start
, mb
, mlen
);
1408 /* **************************************************************** */
1412 /* **************************************************************** */
1414 /* Transpose the words at point. If point is at the end of the line,
1415 transpose the two words before point. */
1417 rl_transpose_words (count
, key
)
1420 char *word1
, *word2
;
1421 int w1_beg
, w1_end
, w2_beg
, w2_end
;
1422 int orig_point
= rl_point
;
1427 /* Find the two words. */
1428 rl_forward_word (count
, key
);
1430 rl_backward_word (1, key
);
1432 rl_backward_word (count
, key
);
1434 rl_forward_word (1, key
);
1437 /* Do some check to make sure that there really are two words. */
1438 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
1441 rl_point
= orig_point
;
1445 /* Get the text of the words. */
1446 word1
= rl_copy_text (w1_beg
, w1_end
);
1447 word2
= rl_copy_text (w2_beg
, w2_end
);
1449 /* We are about to do many insertions and deletions. Remember them
1450 as one operation. */
1451 rl_begin_undo_group ();
1453 /* Do the stuff at word2 first, so that we don't have to worry
1454 about word1 moving. */
1456 rl_delete_text (w2_beg
, w2_end
);
1457 rl_insert_text (word1
);
1460 rl_delete_text (w1_beg
, w1_end
);
1461 rl_insert_text (word2
);
1463 /* This is exactly correct since the text before this point has not
1464 changed in length. */
1467 /* I think that does it. */
1468 rl_end_undo_group ();
1475 /* Transpose the characters at point. If point is at the end of the line,
1476 then transpose the characters before point. */
1478 rl_transpose_chars (count
, key
)
1481 #if defined (HANDLE_MULTIBYTE)
1487 int char_length
, prev_point
;
1492 if (!rl_point
|| rl_end
< 2)
1498 rl_begin_undo_group ();
1500 if (rl_point
== rl_end
)
1502 rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1506 prev_point
= rl_point
;
1507 rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1509 #if defined (HANDLE_MULTIBYTE)
1510 char_length
= prev_point
- rl_point
;
1511 dummy
= (char *)xmalloc (char_length
+ 1);
1512 for (i
= 0; i
< char_length
; i
++)
1513 dummy
[i
] = rl_line_buffer
[rl_point
+ i
];
1516 dummy
[0] = rl_line_buffer
[rl_point
];
1517 dummy
[char_length
= 1] = '\0';
1520 rl_delete_text (rl_point
, rl_point
+ char_length
);
1522 rl_point
= _rl_find_next_mbchar (rl_line_buffer
, rl_point
, count
, MB_FIND_NONZERO
);
1525 rl_insert_text (dummy
);
1526 rl_end_undo_group ();
1528 #if defined (HANDLE_MULTIBYTE)
1535 /* **************************************************************** */
1537 /* Character Searching */
1539 /* **************************************************************** */
1542 #if defined (HANDLE_MULTIBYTE)
1543 _rl_char_search_internal (count
, dir
, smbchar
, len
)
1548 _rl_char_search_internal (count
, dir
, schar
)
1549 int count
, dir
, schar
;
1553 #if defined (HANDLE_MULTIBYTE)
1561 inc
= (dir
< 0) ? -1 : 1;
1564 if ((dir
< 0 && pos
<= 0) || (dir
> 0 && pos
>= rl_end
))
1570 #if defined (HANDLE_MULTIBYTE)
1571 pos
= (inc
> 0) ? _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)
1572 : _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
);
1578 #if defined (HANDLE_MULTIBYTE)
1579 if (_rl_is_mbchar_matched (rl_line_buffer
, pos
, rl_end
, smbchar
, len
))
1581 if (rl_line_buffer
[pos
] == schar
)
1586 rl_point
= (dir
== BTO
) ? _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)
1589 rl_point
= (dir
== FTO
) ? _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
)
1593 #if defined (HANDLE_MULTIBYTE)
1597 #if defined (HANDLE_MULTIBYTE)
1598 while ((dir
< 0) ? (pos
= _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
)) != prepos
1599 : (pos
= _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)) != prepos
);
1601 while ((dir
< 0) ? pos
-- : ++pos
< rl_end
);
1607 /* Search COUNT times for a character read from the current input stream.
1608 FDIR is the direction to search if COUNT is non-negative; otherwise
1609 the search goes in BDIR. So much is dependent on HANDLE_MULTIBYTE
1610 that there are two separate versions of this function. */
1611 #if defined (HANDLE_MULTIBYTE)
1613 _rl_char_search (count
, fdir
, bdir
)
1614 int count
, fdir
, bdir
;
1616 char mbchar
[MB_LEN_MAX
];
1619 mb_len
= _rl_read_mbchar (mbchar
, MB_LEN_MAX
);
1625 return (_rl_char_search_internal (-count
, bdir
, mbchar
, mb_len
));
1627 return (_rl_char_search_internal (count
, fdir
, mbchar
, mb_len
));
1629 #else /* !HANDLE_MULTIBYTE */
1631 _rl_char_search (count
, fdir
, bdir
)
1632 int count
, fdir
, bdir
;
1636 RL_SETSTATE(RL_STATE_MOREINPUT
);
1638 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
1644 return (_rl_char_search_internal (-count
, bdir
, c
));
1646 return (_rl_char_search_internal (count
, fdir
, c
));
1648 #endif /* !HANDLE_MULTIBYTE */
1650 #if defined (READLINE_CALLBACKS)
1652 _rl_char_search_callback (data
)
1653 _rl_callback_generic_arg
*data
;
1655 _rl_callback_func
= 0;
1656 _rl_want_redisplay
= 1;
1658 return (_rl_char_search (data
->count
, data
->i1
, data
->i2
));
1663 rl_char_search (count
, key
)
1666 #if defined (READLINE_CALLBACKS)
1667 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1669 _rl_callback_data
= _rl_callback_data_alloc (count
);
1670 _rl_callback_data
->i1
= FFIND
;
1671 _rl_callback_data
->i2
= BFIND
;
1672 _rl_callback_func
= _rl_char_search_callback
;
1677 return (_rl_char_search (count
, FFIND
, BFIND
));
1681 rl_backward_char_search (count
, key
)
1684 #if defined (READLINE_CALLBACKS)
1685 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1687 _rl_callback_data
= _rl_callback_data_alloc (count
);
1688 _rl_callback_data
->i1
= BFIND
;
1689 _rl_callback_data
->i2
= FFIND
;
1690 _rl_callback_func
= _rl_char_search_callback
;
1695 return (_rl_char_search (count
, BFIND
, FFIND
));
1698 /* **************************************************************** */
1700 /* The Mark and the Region. */
1702 /* **************************************************************** */
1704 /* Set the mark at POSITION. */
1706 _rl_set_mark_at_pos (position
)
1709 if (position
> rl_end
)
1716 /* A bindable command to set the mark. */
1718 rl_set_mark (count
, key
)
1721 return (_rl_set_mark_at_pos (rl_explicit_arg
? count
: rl_point
));
1724 /* Exchange the position of mark and point. */
1726 rl_exchange_point_and_mark (count
, key
)
1729 if (rl_mark
> rl_end
)
1738 SWAP (rl_point
, rl_mark
);