1 /* text.c -- text handling commands for readline. */
3 /* Copyright (C) 1987-2021,2023-2024 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 (int, int);
63 static int _rl_char_search (int, int, int);
65 #if defined (READLINE_CALLBACKS)
66 static int _rl_insert_next_callback (_rl_callback_generic_arg
*);
67 static int _rl_char_search_callback (_rl_callback_generic_arg
*);
70 /* The largest chunk of text that can be inserted in one call to
71 rl_insert_text. Text blocks larger than this are divided. */
72 #define TEXT_COUNT_MAX 1024
74 int _rl_optimize_typeahead
= 1; /* rl_insert tries to read typeahead */
76 /* **************************************************************** */
78 /* Insert and Delete */
80 /* **************************************************************** */
82 /* Insert a string of text into the line at point. This is the only
83 way that you should do insertion. _rl_insert_char () calls this
84 function. Returns the number of characters inserted. */
86 rl_insert_text (const char *string
)
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
];
101 strncpy (rl_line_buffer
+ rl_point
, string
, l
);
103 /* Remember how to undo this if we aren't undoing something. */
104 if (_rl_doing_an_undo
== 0)
106 /* If possible and desirable, concatenate the undos. */
109 (rl_undo_list
->what
== UNDO_INSERT
) &&
110 (rl_undo_list
->end
== rl_point
) &&
111 (rl_undo_list
->end
- rl_undo_list
->start
< 20))
114 rl_add_undo (UNDO_INSERT
, rl_point
, rl_point
+ l
, (char *)NULL
);
118 rl_line_buffer
[rl_end
] = '\0';
122 /* Delete the string between FROM and TO. FROM is inclusive, TO is not.
123 Returns the number of characters deleted. */
125 rl_delete_text (int from
, int 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';
163 /* Fix up point so that it is within the line boundaries after killing
164 text. If FIX_MARK_TOO is non-zero, the mark is forced within line
167 #define _RL_FIX_POINT(x) \
176 _rl_fix_point (int fix_mark_too
)
178 _RL_FIX_POINT (rl_point
);
180 _RL_FIX_POINT (rl_mark
);
186 _RL_FIX_POINT (rl_mark
);
190 /* Replace the contents of the line buffer between START and END with
191 TEXT. The operation is undoable. To replace the entire line in an
192 undoable mode, use _rl_replace_text(text, 0, rl_end); */
194 _rl_replace_text (const char *text
, int start
, int end
)
199 rl_begin_undo_group ();
201 rl_delete_text (start
, end
+ 1);
204 n
= rl_insert_text (text
);
205 rl_end_undo_group ();
210 /* Replace the current line buffer contents with TEXT. If CLEAR_UNDO is
211 non-zero, we free the current undo list. */
213 rl_replace_line (const char *text
, int clear_undo
)
218 if (len
>= rl_line_buffer_len
)
219 rl_extend_line_buffer (len
);
220 strcpy (rl_line_buffer
, text
);
224 rl_free_undo_list ();
229 /* **************************************************************** */
231 /* Readline character functions */
233 /* **************************************************************** */
235 /* This is not a gap editor, just a stupid line input routine. No hair
236 is involved in writing any of the functions, and none should be. */
240 rl_end is the place in the string that we would place '\0';
241 i.e., it is always safe to place '\0' there.
243 rl_point is the place in the string where the cursor is. Sometimes
244 this is the same as rl_end.
246 Any command that is called interactively receives two arguments.
247 The first is a count: the numeric arg passed to this command.
248 The second is the key which invoked this command.
251 /* **************************************************************** */
253 /* Movement Commands */
255 /* **************************************************************** */
257 /* Note that if you `optimize' the display for these functions, you cannot
258 use said functions in other functions which do not do optimizing display.
259 I.e., you will have to update the data base for rl_redisplay, and you
260 might as well let rl_redisplay do that job. */
262 /* Move forward COUNT bytes. */
264 rl_forward_byte (int count
, int key
)
267 return (rl_backward_byte (-count
, key
));
273 end
= rl_point
+ count
;
274 #if defined (VI_MODE)
275 lend
= rl_end
> 0 ? rl_end
- (VI_COMMAND_MODE()) : rl_end
;
296 _rl_forward_char_internal (int 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
;
320 _rl_backward_char_internal (int count
)
325 #if defined (HANDLE_MULTIBYTE)
328 while (count
> 0 && point
> 0)
330 point
= _rl_find_prev_mbchar (rl_line_buffer
, point
, MB_FIND_NONZERO
);
334 return 0; /* XXX - rl_ding() here? */
346 #if defined (HANDLE_MULTIBYTE)
347 /* Move forward COUNT characters. */
349 rl_forward_char (int count
, int key
)
353 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
354 return (rl_forward_byte (count
, key
));
357 return (rl_backward_char (-count
, key
));
361 if (rl_point
== rl_end
&& EMACS_MODE())
367 point
= _rl_forward_char_internal (count
);
369 if (rl_point
== point
)
377 #else /* !HANDLE_MULTIBYTE */
379 rl_forward_char (int count
, int key
)
381 return (rl_forward_byte (count
, key
));
383 #endif /* !HANDLE_MULTIBYTE */
385 /* Backwards compatibility. */
387 rl_forward (int count
, int key
)
389 return (rl_forward_char (count
, key
));
392 /* Move backward COUNT bytes. */
394 rl_backward_byte (int count
, int key
)
397 return (rl_forward_byte (-count
, key
));
401 if (rl_point
< count
)
416 #if defined (HANDLE_MULTIBYTE)
417 /* Move backward COUNT characters. */
419 rl_backward_char (int count
, int key
)
423 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
424 return (rl_backward_byte (count
, key
));
427 return (rl_forward_char (-count
, key
));
433 while (count
> 0 && point
> 0)
435 point
= _rl_find_prev_mbchar (rl_line_buffer
, point
, MB_FIND_NONZERO
);
451 rl_backward_char (int count
, int key
)
453 return (rl_backward_byte (count
, key
));
457 /* Backwards compatibility. */
459 rl_backward (int count
, int key
)
461 return (rl_backward_char (count
, key
));
464 /* Move to the beginning of the line. */
466 rl_beg_of_line (int count
, int key
)
472 /* Move to the end of the line. */
474 rl_end_of_line (int count
, int key
)
480 /* Move forward a word. We do what Emacs does. Handles multibyte chars. */
482 rl_forward_word (int count
, int key
)
487 return (rl_backward_word (-count
, key
));
491 if (rl_point
> rl_end
)
493 if (rl_point
== rl_end
)
496 /* If we are not in a word, move forward until we are in one.
497 Then, move forward until we hit a non-alphabetic character. */
498 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
);
503 while (rl_point
< rl_end
)
505 c
= _rl_char_value (rl_line_buffer
, rl_point
);
506 if (_rl_walphabetic (c
))
508 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
512 if (rl_point
> rl_end
)
514 if (rl_point
== rl_end
)
517 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
518 while (rl_point
< rl_end
)
520 c
= _rl_char_value (rl_line_buffer
, rl_point
);
521 if (_rl_walphabetic (c
) == 0)
523 rl_point
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
532 /* Move backward a word. We do what Emacs does. Handles multibyte chars. */
534 rl_backward_word (int count
, int key
)
539 return (rl_forward_word (-count
, key
));
546 /* Like rl_forward_word (), except that we look at the characters
547 just before point. */
549 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
550 c
= _rl_char_value (rl_line_buffer
, p
);
552 if (_rl_walphabetic (c
) == 0)
557 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
558 c
= _rl_char_value (rl_line_buffer
, p
);
559 if (_rl_walphabetic (c
))
567 p
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
568 c
= _rl_char_value (rl_line_buffer
, p
);
569 if (_rl_walphabetic (c
) == 0)
581 /* Clear the current line. Numeric argument to C-l does this. */
583 rl_refresh_line (int ignore1
, int ignore2
)
586 rl_display_fixed
= 1;
590 /* C-l typed to a line without quoting clears the screen, and then reprints
591 the prompt and the current input line. Given a numeric arg, redraw only
594 rl_clear_screen (int count
, int key
)
598 rl_refresh_line (count
, key
);
602 _rl_clear_screen (0); /* calls termcap function to clear screen */
603 rl_keep_mark_active ();
604 rl_forced_update_display ();
605 rl_display_fixed
= 1;
611 rl_clear_display (int count
, int key
)
613 _rl_clear_screen (1); /* calls termcap function to clear screen and scrollback buffer */
614 rl_forced_update_display ();
615 rl_display_fixed
= 1;
621 rl_previous_screen_line (int count
, int key
)
625 c
= _rl_term_autowrap
? _rl_screenwidth
: (_rl_screenwidth
+ 1);
626 return (rl_backward_char (c
, key
));
630 rl_next_screen_line (int count
, int key
)
634 c
= _rl_term_autowrap
? _rl_screenwidth
: (_rl_screenwidth
+ 1);
635 return (rl_forward_char (c
, key
));
639 rl_skip_csi_sequence (int count
, int key
)
643 RL_SETSTATE (RL_STATE_MOREINPUT
);
646 while (ch
>= 0x20 && ch
< 0x40);
647 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
653 rl_arrow_keys (int count
, int key
)
657 RL_SETSTATE(RL_STATE_MOREINPUT
);
659 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
663 switch (_rl_to_upper (ch
))
666 rl_get_previous_history (count
, ch
);
670 rl_get_next_history (count
, ch
);
674 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
675 rl_forward_char (count
, ch
);
677 rl_forward_byte (count
, ch
);
681 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
682 rl_backward_char (count
, ch
);
684 rl_backward_byte (count
, ch
);
694 /* **************************************************************** */
698 /* **************************************************************** */
700 #ifdef HANDLE_MULTIBYTE
701 static char pending_bytes
[MB_LEN_MAX
];
702 static int pending_bytes_length
= 0;
703 static mbstate_t ps
= {0};
706 /* Insert the character C at the current location, moving point forward.
707 If C introduces a multibyte sequence, we read the whole sequence and
708 then insert the multibyte char into the line buffer.
709 If C == 0, we immediately insert any pending partial multibyte character,
710 assuming that we have read a character that doesn't map to self-insert.
711 This doesn't completely handle characters that are part of a multibyte
712 character but map to editing functions. */
714 _rl_insert_char (int count
, int c
)
718 #ifdef HANDLE_MULTIBYTE
720 char incoming
[MB_LEN_MAX
+ 1];
721 int incoming_length
= 0;
723 static int stored_count
= 0;
726 #if !defined (HANDLE_MULTIBYTE)
734 if (pending_bytes_length
== 0)
736 if (stored_count
<= 0)
737 stored_count
= count
;
739 count
= stored_count
;
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;
745 memset (&ps
, 0, sizeof (mbstate_t));
747 else if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
753 else if (_rl_utf8locale
&& (c
& 0x80) == 0)
755 if (pending_bytes_length
)
756 _rl_insert_char (0, 0);
767 if (stored_count
<= 0)
768 stored_count
= count
;
770 count
= stored_count
;
773 pending_bytes
[pending_bytes_length
++] = c
;
774 ret
= MBRTOWC (&wc
, pending_bytes
, pending_bytes_length
, &ps
);
776 if (ret
== (size_t)-2)
778 /* Bytes too short to compose character, try to wait for next byte.
779 Restore the state of the byte sequence, because in this case the
780 effect of mbstate is undefined. */
784 else if (ret
== (size_t)-1)
786 /* Invalid byte sequence for the current locale. Treat first byte
787 as a single character. */
788 incoming
[0] = pending_bytes
[0];
791 pending_bytes_length
--;
792 if (pending_bytes_length
)
793 memmove (pending_bytes
, pending_bytes
+ 1, pending_bytes_length
);
794 /* Clear the state of the byte sequence, because in this case the
795 effect of mbstate is undefined. */
796 memset (&ps
, 0, sizeof (mbstate_t));
798 else if (ret
== (size_t)0)
802 pending_bytes_length
--;
803 /* Clear the state of the byte sequence, because in this case the
804 effect of mbstate is undefined. */
805 memset (&ps
, 0, sizeof (mbstate_t));
809 incoming
[0] = pending_bytes
[0];
810 incoming
[incoming_length
= 1] = '\0';
811 pending_bytes_length
= 0;
815 /* We successfully read a single multibyte character. */
816 memcpy (incoming
, pending_bytes
, pending_bytes_length
);
817 incoming
[pending_bytes_length
] = '\0';
818 incoming_length
= pending_bytes_length
;
819 pending_bytes_length
= 0;
822 #endif /* HANDLE_MULTIBYTE */
824 /* If we can optimize, then do it. But don't let people crash
825 readline because of extra large arguments. */
826 if (count
> 1 && count
<= TEXT_COUNT_MAX
)
828 #if defined (HANDLE_MULTIBYTE)
829 string_size
= count
* incoming_length
;
830 string
= (char *)xmalloc (1 + string_size
);
833 while (i
< string_size
)
835 if (incoming_length
== 1)
836 string
[i
++] = *incoming
;
839 strncpy (string
+ i
, incoming
, incoming_length
);
840 i
+= incoming_length
;
845 #else /* !HANDLE_MULTIBYTE */
846 string
= (char *)xmalloc (1 + count
);
848 for (i
= 0; i
< count
; i
++)
850 #endif /* !HANDLE_MULTIBYTE */
853 rl_insert_text (string
);
856 #if defined (HANDLE_MULTIBYTE)
857 return (pending_bytes_length
!= 0);
863 if (count
> TEXT_COUNT_MAX
)
866 #if defined (HANDLE_MULTIBYTE)
867 string_size
= incoming_length
* TEXT_COUNT_MAX
;
868 string
= (char *)xmalloc (1 + string_size
);
871 while (i
< string_size
)
873 if (incoming_length
== 1)
874 string
[i
++] = *incoming
;
877 strncpy (string
+ i
, incoming
, incoming_length
);
878 i
+= incoming_length
;
884 decreaser
= (count
> TEXT_COUNT_MAX
) ? TEXT_COUNT_MAX
: count
;
885 string
[decreaser
*incoming_length
] = '\0';
886 rl_insert_text (string
);
894 return (pending_bytes_length
!= 0);
895 #else /* !HANDLE_MULTIBYTE */
896 char str
[TEXT_COUNT_MAX
+1];
898 for (i
= 0; i
< TEXT_COUNT_MAX
; i
++)
903 decreaser
= (count
> TEXT_COUNT_MAX
? TEXT_COUNT_MAX
: count
);
904 str
[decreaser
] = '\0';
905 rl_insert_text (str
);
910 #endif /* !HANDLE_MULTIBYTE */
913 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
915 /* We are inserting a single character.
916 If there is pending input, then make a string of all of the
917 pending characters that are bound to rl_insert, and insert
918 them all. Don't do this if we're current reading input from
920 if ((RL_ISSTATE (RL_STATE_MACROINPUT
) == 0) && _rl_pushed_input_available ())
921 _rl_insert_typein (c
);
924 /* Inserting a single character. */
929 rl_insert_text (str
);
932 #if defined (HANDLE_MULTIBYTE)
935 rl_insert_text (incoming
);
939 return (pending_bytes_length
!= 0);
945 /* Overwrite the character at point (or next COUNT characters) with C.
946 If C introduces a multibyte character sequence, read the entire sequence
947 before starting the overwrite loop. */
949 _rl_overwrite_char (int count
, int c
)
952 #if defined (HANDLE_MULTIBYTE)
953 char mbkey
[MB_LEN_MAX
];
956 /* Read an entire multibyte character sequence to insert COUNT times. */
958 if (count
> 0 && MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
959 k
= _rl_read_mbstring (c
, mbkey
, MB_LEN_MAX
);
964 rl_begin_undo_group ();
966 for (i
= 0; i
< count
; i
++)
968 #if defined (HANDLE_MULTIBYTE)
969 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
970 rl_insert_text (mbkey
);
973 _rl_insert_char (1, c
);
975 if (rl_point
< rl_end
)
979 rl_end_undo_group ();
985 rl_insert (int count
, int c
)
989 r
= (rl_insert_mode
== RL_IM_INSERT
) ? _rl_insert_char (count
, c
) : _rl_overwrite_char (count
, c
);
991 /* XXX -- attempt to batch-insert pending input that maps to self-insert */
993 n
= (unsigned short)-2;
994 while (_rl_optimize_typeahead
&&
995 rl_num_chars_to_read
== 0 &&
996 (RL_ISSTATE (RL_STATE_INPUTPENDING
|RL_STATE_MACROINPUT
) == 0) &&
997 _rl_pushed_input_available () == 0 &&
998 _rl_input_queued (0) &&
999 (n
= rl_read_key ()) > 0 &&
1000 _rl_keymap
[(unsigned char)n
].type
== ISFUNC
&&
1001 _rl_keymap
[(unsigned char)n
].function
== rl_insert
)
1003 r
= (rl_insert_mode
== RL_IM_INSERT
) ? _rl_insert_char (1, n
) : _rl_overwrite_char (1, n
);
1004 /* _rl_insert_char keeps its own set of pending characters to compose a
1005 complete multibyte character, and only returns 1 if it sees a character
1006 that's part of a multibyte character but too short to complete one. We
1007 can try to read another character in the hopes that we will get the
1008 next one or just punt. Right now we try to read another character.
1009 We don't want to call rl_insert_next if _rl_insert_char has already
1010 stored the character in the pending_bytes array because that will
1011 result in doubled input. */
1012 n
= (unsigned short)-2;
1013 x
++; /* count of bytes of typeahead read, currently unused */
1014 if (r
== 1) /* read partial multibyte character */
1016 if (rl_done
|| r
!= 0)
1020 /* If we didn't insert n and there are pending bytes, we need to insert
1021 them if _rl_insert_char didn't do that on its own. */
1022 if (r
== 1 && rl_insert_mode
== RL_IM_INSERT
)
1023 r
= _rl_insert_char (0, 0); /* flush partial multibyte char */
1025 if (n
!= (unsigned short)-2) /* -2 = sentinel value for having inserted N */
1027 /* setting rl_pending_input inhibits setting rl_last_func so we do it
1029 rl_last_func
= rl_insert
;
1030 _rl_reset_argument ();
1031 rl_executing_keyseq
[rl_key_sequence_length
= 0] = '\0';
1032 r
= rl_execute_next (n
);
1037 /* Insert the next typed character verbatim. */
1039 _rl_insert_next (int count
)
1043 RL_SETSTATE(RL_STATE_MOREINPUT
);
1045 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
1050 if (RL_ISSTATE (RL_STATE_MACRODEF
))
1051 _rl_add_macro_char (c
);
1053 #if defined (HANDLE_SIGNALS)
1054 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
1055 _rl_restore_tty_signals ();
1058 return (_rl_insert_char (count
, c
));
1061 #if defined (READLINE_CALLBACKS)
1063 _rl_insert_next_callback (_rl_callback_generic_arg
*data
)
1067 count
= data
->count
;
1073 r
= _rl_insert_next (1);
1074 _rl_want_redisplay
= 1;
1075 /* If we should keep going, leave the callback function installed */
1076 if (data
->count
< 0 && r
== 0)
1078 count
= 0; /* data->count == 0 || r != 0; force break below */
1081 /* Deregister function, let rl_callback_read_char deallocate data */
1082 _rl_callback_func
= 0;
1083 _rl_want_redisplay
= 1;
1088 return _rl_insert_next (count
);
1093 rl_quoted_insert (int count
, int key
)
1097 /* Let's see...should the callback interface futz with signal handling? */
1098 #if defined (HANDLE_SIGNALS)
1099 if (RL_ISSTATE (RL_STATE_CALLBACK
) == 0)
1100 _rl_disable_tty_signals ();
1103 #if defined (READLINE_CALLBACKS)
1104 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1106 _rl_callback_data
= _rl_callback_data_alloc (count
);
1107 _rl_callback_func
= _rl_insert_next_callback
;
1112 /* A negative count means to quote the next -COUNT characters. */
1116 r
= _rl_insert_next (1);
1117 while (r
== 0 && ++count
< 0);
1120 r
= _rl_insert_next (count
);
1123 _rl_insert_char (0, 0); /* insert partial multibyte character */
1128 /* Insert a tab character. */
1130 rl_tab_insert (int count
, int key
)
1132 return (_rl_insert_char (count
, '\t'));
1135 /* What to do when a NEWLINE is pressed. We accept the whole line.
1136 KEY is the key that invoked this command. I guess it could have
1137 meaning in the future. */
1139 rl_newline (int count
, int key
)
1141 if (rl_mark_active_p ())
1143 rl_deactivate_mark ();
1144 (*rl_redisplay_function
) ();
1145 _rl_want_redisplay
= 0;
1150 if (_rl_history_preserve_point
)
1151 _rl_history_saved_point
= (rl_point
== rl_end
) ? -1 : rl_point
;
1153 RL_SETSTATE(RL_STATE_DONE
);
1155 #if defined (VI_MODE)
1156 if (rl_editing_mode
== vi_mode
)
1158 _rl_vi_done_inserting ();
1159 if (_rl_vi_textmod_command (_rl_vi_last_command
) == 0) /* XXX */
1160 _rl_vi_reset_last ();
1162 #endif /* VI_MODE */
1164 /* If we've been asked to erase empty lines, suppress the final update,
1165 since _rl_update_final calls rl_crlf(). */
1166 if (rl_erase_empty_line
&& rl_point
== 0 && rl_end
== 0)
1170 _rl_update_final ();
1174 /* What to do for some uppercase characters, like meta characters,
1175 and some characters appearing in emacs_ctlx_keymap. This function
1176 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1177 is special cased. */
1179 rl_do_lowercase_version (int ignore1
, int ignore2
)
1181 return 99999; /* prevent from being combined with _rl_null_function */
1184 /* This is different from what vi does, so the code's not shared. Emacs
1185 rubout in overwrite mode has one oddity: it replaces a control
1186 character that's displayed as two characters (^X) with two spaces. */
1188 _rl_overwrite_rubout (int count
, int key
)
1201 /* L == number of spaces to insert */
1202 for (i
= l
= 0; i
< count
; i
++)
1204 rl_backward_char (1, key
);
1205 l
+= rl_character_len (rl_line_buffer
[rl_point
], rl_point
); /* not exactly right */
1208 rl_begin_undo_group ();
1210 if (count
> 1 || rl_explicit_arg
)
1211 rl_kill_text (opoint
, rl_point
);
1213 rl_delete_text (opoint
, rl_point
);
1215 /* Emacs puts point at the beginning of the sequence of spaces. */
1216 if (rl_point
< rl_end
)
1219 _rl_insert_char (l
, ' ');
1223 rl_end_undo_group ();
1228 /* Rubout the character behind point. */
1230 rl_rubout (int count
, int key
)
1233 return (rl_delete (-count
, key
));
1241 if (rl_insert_mode
== RL_IM_OVERWRITE
)
1242 return (_rl_overwrite_rubout (count
, key
));
1244 return (_rl_rubout_char (count
, key
));
1248 _rl_rubout_char (int count
, int key
)
1253 /* Duplicated code because this is called from other parts of the library. */
1255 return (rl_delete (-count
, key
));
1263 orig_point
= rl_point
;
1264 if (count
> 1 || rl_explicit_arg
)
1266 rl_backward_char (count
, key
);
1267 rl_kill_text (orig_point
, rl_point
);
1269 else if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
1271 c
= rl_line_buffer
[--rl_point
];
1272 rl_delete_text (rl_point
, orig_point
);
1273 /* The erase-at-end-of-line hack is of questionable merit now. */
1274 if (rl_point
== rl_end
&& ISPRINT ((unsigned char)c
) && _rl_last_c_pos
&& _rl_last_v_pos
== 0)
1277 l
= rl_character_len (c
, rl_point
);
1278 if (_rl_last_c_pos
>= l
)
1279 _rl_erase_at_end_of_line (l
);
1284 rl_point
= _rl_find_prev_mbchar (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1285 rl_delete_text (rl_point
, orig_point
);
1291 /* Delete the character under the cursor. Given a numeric argument,
1292 kill that many characters instead. */
1294 rl_delete (int count
, int key
)
1299 return (_rl_rubout_char (-count
, key
));
1301 if (rl_point
== rl_end
)
1307 if (count
> 1 || rl_explicit_arg
)
1310 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1311 rl_forward_char (count
, key
);
1313 rl_forward_byte (count
, key
);
1315 rl_kill_text (xpoint
, rl_point
);
1320 xpoint
= MB_NEXTCHAR (rl_line_buffer
, rl_point
, 1, MB_FIND_NONZERO
);
1321 rl_delete_text (rl_point
, xpoint
);
1326 /* Delete the character under the cursor, unless the insertion
1327 point is at the end of the line, in which case the character
1328 behind the cursor is deleted. COUNT is obeyed and may be used
1329 to delete forward or backward that many characters. */
1331 rl_rubout_or_delete (int count
, int key
)
1333 if (rl_end
!= 0 && rl_point
== rl_end
)
1334 return (_rl_rubout_char (count
, key
));
1336 return (rl_delete (count
, key
));
1339 /* Delete all spaces and tabs around point. */
1341 rl_delete_horizontal_space (int count
, int ignore
)
1345 while (rl_point
&& whitespace (rl_line_buffer
[rl_point
- 1]))
1350 while (rl_point
< rl_end
&& whitespace (rl_line_buffer
[rl_point
]))
1353 if (start
!= rl_point
)
1355 rl_delete_text (start
, rl_point
);
1365 /* Like the tcsh editing function delete-char-or-list. The eof character
1366 is caught before this is invoked, so this really does the same thing as
1367 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1369 rl_delete_or_show_completions (int count
, int key
)
1371 if (rl_end
!= 0 && rl_point
== rl_end
)
1372 return (rl_possible_completions (count
, key
));
1374 return (rl_delete (count
, key
));
1377 #ifndef RL_COMMENT_BEGIN_DEFAULT
1378 #define RL_COMMENT_BEGIN_DEFAULT "#"
1381 /* Turn the current line into a comment in shell history.
1382 A K*rn shell style function. */
1384 rl_insert_comment (int count
, int key
)
1386 char *rl_comment_text
;
1389 rl_beg_of_line (1, key
);
1390 rl_comment_text
= _rl_comment_begin
? _rl_comment_begin
: RL_COMMENT_BEGIN_DEFAULT
;
1392 if (rl_explicit_arg
== 0)
1393 rl_insert_text (rl_comment_text
);
1396 rl_comment_len
= strlen (rl_comment_text
);
1397 if (STREQN (rl_comment_text
, rl_line_buffer
, rl_comment_len
))
1398 rl_delete_text (rl_point
, rl_point
+ rl_comment_len
);
1400 rl_insert_text (rl_comment_text
);
1403 (*rl_redisplay_function
) ();
1404 rl_newline (1, '\n');
1409 /* **************************************************************** */
1413 /* **************************************************************** */
1415 /* The three kinds of things that we know how to do. */
1420 /* Uppercase the word at point. */
1422 rl_upcase_word (int count
, int key
)
1424 return (rl_change_case (count
, UpCase
));
1427 /* Lowercase the word at point. */
1429 rl_downcase_word (int count
, int key
)
1431 return (rl_change_case (count
, DownCase
));
1434 /* Upcase the first letter, downcase the rest. */
1436 rl_capitalize_word (int count
, int key
)
1438 return (rl_change_case (count
, CapCase
));
1441 /* The meaty function.
1442 Change the case of COUNT words, performing OP on them.
1443 OP is one of UpCase, DownCase, or CapCase.
1444 If a negative argument is given, leave point where it started,
1445 otherwise, leave it where it moves to. */
1447 rl_change_case (int count
, int op
)
1449 int start
, next
, end
;
1450 int inword
, nc
, nop
;
1453 #if defined (HANDLE_MULTIBYTE)
1455 char mb
[MB_LEN_MAX
+1];
1461 rl_forward_word (count
, 0);
1464 if (op
!= UpCase
&& op
!= DownCase
&& op
!= CapCase
)
1473 #if defined (HANDLE_MULTIBYTE)
1474 memset (&mps
, 0, sizeof (mbstate_t));
1477 /* We are going to modify some text, so let's prepare to undo it. */
1478 rl_modifying (start
, end
);
1483 c
= _rl_char_value (rl_line_buffer
, start
);
1484 /* This assumes that the upper and lower case versions are the same width. */
1485 next
= MB_NEXTCHAR (rl_line_buffer
, start
, 1, MB_FIND_NONZERO
);
1487 if (_rl_walphabetic (c
) == 0)
1496 nop
= inword
? DownCase
: UpCase
;
1501 /* Can't check isascii here; some languages (e.g, Turkish) have
1502 multibyte upper and lower case equivalents of single-byte ascii
1504 if (MB_CUR_MAX
== 1 || rl_byte_oriented
)
1508 nc
= (nop
== UpCase
) ? _rl_to_upper (uc
) : _rl_to_lower (uc
);
1509 rl_line_buffer
[start
] = nc
;
1511 #if defined (HANDLE_MULTIBYTE)
1514 m
= MBRTOWC (&wc
, rl_line_buffer
+ start
, end
- start
, &mps
);
1515 if (MB_INVALIDCH (m
))
1517 c
= rl_line_buffer
[start
];
1518 next
= start
+ 1; /* potentially redundant */
1519 goto change_singlebyte
;
1521 else if (MB_NULLWCH (m
))
1523 start
= next
; /* don't bother with null wide characters */
1526 nwc
= (nop
== UpCase
) ? _rl_to_wupper (wc
) : _rl_to_wlower (wc
);
1527 if (nwc
!= wc
) /* just skip unchanged characters */
1532 memset (&ts
, 0, sizeof (mbstate_t));
1533 mlen
= WCRTOMB (mb
, nwc
, &ts
);
1535 if (MB_INVALIDCH (mlen
))
1538 memset (&ts
, 0, sizeof (mbstate_t));
1539 mlen
= WCRTOMB (mb
, nwc
, &ts
);
1540 if (MB_INVALIDCH (mlen
)) /* should not happen */
1541 strncpy (mb
, rl_line_buffer
+ start
, mlen
= m
);
1545 /* what to do if m != mlen? adjust below */
1546 /* m == length of old char, mlen == length of new char */
1547 s
= rl_line_buffer
+ start
;
1548 e
= rl_line_buffer
+ rl_end
;
1550 memcpy (s
, mb
, mlen
);
1553 memcpy (s
, mb
, mlen
);
1554 memmove (s
+ mlen
, s
+ m
, (e
- s
) - m
);
1555 next
-= m
- mlen
; /* next char changes */
1556 end
-= m
- mlen
; /* end of word changes */
1557 rl_end
-= m
- mlen
; /* end of line changes */
1558 rl_line_buffer
[rl_end
] = 0;
1562 rl_extend_line_buffer (rl_end
+ mlen
+ (e
- s
) - m
+ 2);
1563 s
= rl_line_buffer
+ start
; /* have to redo this */
1564 e
= rl_line_buffer
+ rl_end
;
1565 memmove (s
+ mlen
, s
+ m
, (e
- s
) - m
);
1566 memcpy (s
, mb
, mlen
);
1567 next
+= mlen
- m
; /* next char changes */
1568 end
+= mlen
- m
; /* end of word changes */
1569 rl_end
+= mlen
- m
; /* end of line changes */
1570 rl_line_buffer
[rl_end
] = 0;
1583 /* **************************************************************** */
1587 /* **************************************************************** */
1589 /* Transpose the words at point. If point is at the end of the line,
1590 transpose the two words before point. */
1592 rl_transpose_words (int count
, int key
)
1594 char *word1
, *word2
;
1595 int w1_beg
, w1_end
, w2_beg
, w2_end
;
1596 int orig_point
, orig_end
;
1598 orig_point
= rl_point
;
1604 /* Find the two words. */
1605 rl_forward_word (count
, key
);
1607 rl_backward_word (1, key
);
1609 rl_backward_word (count
, key
);
1611 rl_forward_word (1, key
);
1614 /* Do some check to make sure that there really are two words. */
1615 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
1618 rl_point
= orig_point
;
1622 /* Get the text of the words. */
1623 word1
= rl_copy_text (w1_beg
, w1_end
);
1624 word2
= rl_copy_text (w2_beg
, w2_end
);
1626 /* We are about to do many insertions and deletions. Remember them
1627 as one operation. */
1628 rl_begin_undo_group ();
1630 /* Do the stuff at word2 first, so that we don't have to worry
1631 about word1 moving. */
1633 rl_delete_text (w2_beg
, w2_end
);
1634 rl_insert_text (word1
);
1637 rl_delete_text (w1_beg
, w1_end
);
1638 rl_insert_text (word2
);
1640 /* This is exactly correct since the text before this point has not
1641 changed in length. */
1643 rl_end
= orig_end
; /* just make sure */
1645 /* I think that does it. */
1646 rl_end_undo_group ();
1653 /* Transpose the characters at point. If point is at the end of the line,
1654 then transpose the characters before point. */
1656 rl_transpose_chars (int count
, int key
)
1658 #if defined (HANDLE_MULTIBYTE)
1664 int char_length
, prev_point
;
1669 if (!rl_point
|| rl_end
< 2)
1675 rl_begin_undo_group ();
1677 if (rl_point
== rl_end
)
1679 rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1683 prev_point
= rl_point
;
1684 rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_NONZERO
);
1686 #if defined (HANDLE_MULTIBYTE)
1687 char_length
= prev_point
- rl_point
;
1688 dummy
= (char *)xmalloc (char_length
+ 1);
1689 for (i
= 0; i
< char_length
; i
++)
1690 dummy
[i
] = rl_line_buffer
[rl_point
+ i
];
1693 dummy
[0] = rl_line_buffer
[rl_point
];
1694 dummy
[char_length
= 1] = '\0';
1697 rl_delete_text (rl_point
, rl_point
+ char_length
);
1699 rl_point
= _rl_find_next_mbchar (rl_line_buffer
, rl_point
, count
, MB_FIND_NONZERO
);
1702 rl_insert_text (dummy
);
1703 rl_end_undo_group ();
1705 #if defined (HANDLE_MULTIBYTE)
1712 /* **************************************************************** */
1714 /* Character Searching */
1716 /* **************************************************************** */
1719 #if defined (HANDLE_MULTIBYTE)
1720 _rl_char_search_internal (int count
, int dir
, char *smbchar
, int len
)
1722 _rl_char_search_internal (int count
, int dir
, int schar
)
1726 #if defined (HANDLE_MULTIBYTE)
1734 inc
= (dir
< 0) ? -1 : 1;
1737 if ((dir
< 0 && pos
<= 0) || (dir
> 0 && pos
>= rl_end
))
1743 #if defined (HANDLE_MULTIBYTE)
1744 pos
= (inc
> 0) ? _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)
1745 : _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
);
1751 #if defined (HANDLE_MULTIBYTE)
1752 if (_rl_is_mbchar_matched (rl_line_buffer
, pos
, rl_end
, smbchar
, len
))
1754 if (rl_line_buffer
[pos
] == schar
)
1759 rl_point
= (dir
== BTO
) ? _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)
1762 rl_point
= (dir
== FTO
) ? _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
)
1766 #if defined (HANDLE_MULTIBYTE)
1770 #if defined (HANDLE_MULTIBYTE)
1771 while ((dir
< 0) ? (pos
= _rl_find_prev_mbchar (rl_line_buffer
, pos
, MB_FIND_ANY
)) != prepos
1772 : (pos
= _rl_find_next_mbchar (rl_line_buffer
, pos
, 1, MB_FIND_ANY
)) != prepos
);
1774 while ((dir
< 0) ? pos
-- : ++pos
< rl_end
);
1780 /* Search COUNT times for a character read from the current input stream.
1781 FDIR is the direction to search if COUNT is non-negative; otherwise
1782 the search goes in BDIR. So much is dependent on HANDLE_MULTIBYTE
1783 that there are two separate versions of this function. */
1784 #if defined (HANDLE_MULTIBYTE)
1786 _rl_char_search (int count
, int fdir
, int bdir
)
1788 char mbchar
[MB_LEN_MAX
];
1791 mb_len
= _rl_read_mbchar (mbchar
, MB_LEN_MAX
);
1797 return (_rl_char_search_internal (-count
, bdir
, mbchar
, mb_len
));
1799 return (_rl_char_search_internal (count
, fdir
, mbchar
, mb_len
));
1801 #else /* !HANDLE_MULTIBYTE */
1803 _rl_char_search (int count
, int fdir
, int bdir
)
1807 c
= _rl_bracketed_read_key ();
1812 return (_rl_char_search_internal (-count
, bdir
, c
));
1814 return (_rl_char_search_internal (count
, fdir
, c
));
1816 #endif /* !HANDLE_MULTIBYTE */
1818 #if defined (READLINE_CALLBACKS)
1820 _rl_char_search_callback (_rl_callback_generic_arg
*data
)
1822 _rl_callback_func
= 0;
1823 _rl_want_redisplay
= 1;
1825 return (_rl_char_search (data
->count
, data
->i1
, data
->i2
));
1830 rl_char_search (int count
, int key
)
1832 #if defined (READLINE_CALLBACKS)
1833 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1835 _rl_callback_data
= _rl_callback_data_alloc (count
);
1836 _rl_callback_data
->i1
= FFIND
;
1837 _rl_callback_data
->i2
= BFIND
;
1838 _rl_callback_func
= _rl_char_search_callback
;
1843 return (_rl_char_search (count
, FFIND
, BFIND
));
1847 rl_backward_char_search (int count
, int key
)
1849 #if defined (READLINE_CALLBACKS)
1850 if (RL_ISSTATE (RL_STATE_CALLBACK
))
1852 _rl_callback_data
= _rl_callback_data_alloc (count
);
1853 _rl_callback_data
->i1
= BFIND
;
1854 _rl_callback_data
->i2
= FFIND
;
1855 _rl_callback_func
= _rl_char_search_callback
;
1860 return (_rl_char_search (count
, BFIND
, FFIND
));
1863 /* **************************************************************** */
1865 /* The Mark and the Region. */
1867 /* **************************************************************** */
1869 /* Set the mark at POSITION. */
1871 _rl_set_mark_at_pos (int position
)
1873 if (position
< 0 || position
> rl_end
)
1880 /* A bindable command to set the mark. */
1882 rl_set_mark (int count
, int key
)
1884 return (_rl_set_mark_at_pos (rl_explicit_arg
? count
: rl_point
));
1887 /* Exchange the position of mark and point. */
1889 rl_exchange_point_and_mark (int count
, int key
)
1891 if (rl_mark
> rl_end
)
1897 rl_mark
= 0; /* like _RL_FIX_POINT */
1902 SWAP (rl_point
, rl_mark
);
1903 rl_activate_mark ();
1909 /* Active mark support */
1911 /* Is the region active? */
1912 static int mark_active
= 0;
1914 /* Does the current command want the mark to remain active when it completes? */
1915 int _rl_keep_mark_active
;
1918 rl_keep_mark_active (void)
1920 _rl_keep_mark_active
++;
1924 rl_activate_mark (void)
1927 rl_keep_mark_active ();
1931 rl_deactivate_mark (void)
1937 rl_mark_active_p (void)
1939 return (mark_active
);
1942 /* **************************************************************** */
1944 /* Reading a string entered from the keyboard */
1946 /* **************************************************************** */
1948 /* A very simple set of functions to read a string from the keyboard using
1949 the line buffer as temporary storage. The caller can set a completion
1950 function to perform completion on TAB and SPACE. */
1952 /* XXX - this is all very similar to the search stuff but with a different
1955 static HIST_ENTRY
*_rl_saved_line_for_readstr
;
1956 _rl_readstr_cxt
*_rl_rscxt
;
1959 _rl_rscxt_alloc (int flags
)
1961 _rl_readstr_cxt
*cxt
;
1963 cxt
= (_rl_readstr_cxt
*)xmalloc (sizeof (_rl_readstr_cxt
));
1967 cxt
->save_point
= rl_point
;
1968 cxt
->save_mark
= rl_mark
;
1969 cxt
->save_line
= where_history ();
1971 cxt
->prevc
= cxt
->lastc
= 0;
1973 cxt
->compfunc
= NULL
;
1979 _rl_rscxt_dispose (_rl_readstr_cxt
*cxt
, int flags
)
1984 /* This isn't used yet */
1986 _rl_free_saved_readstr_line ()
1988 if (_rl_saved_line_for_readstr
)
1989 /* This doesn't free any saved undo list, if it needs to,
1990 rl_clear_history shows how to do it. */
1991 _rl_free_saved_line (_rl_saved_line_for_readstr
);
1992 _rl_saved_line_for_readstr
= (HIST_ENTRY
*)NULL
;
1996 _rl_unsave_saved_readstr_line ()
1998 if (_rl_saved_line_for_readstr
)
2000 _rl_free_undo_list (rl_undo_list
);
2001 _rl_unsave_line (_rl_saved_line_for_readstr
); /* restores rl_undo_list */
2003 _rl_saved_line_for_readstr
= (HIST_ENTRY
*)NULL
;
2007 _rl_readstr_init (int pchar
, int flags
)
2009 _rl_readstr_cxt
*cxt
;
2012 cxt
= _rl_rscxt_alloc (flags
);
2014 _rl_saved_line_for_readstr
= _rl_alloc_saved_line ();
2017 rl_line_buffer
[0] = 0;
2018 rl_end
= rl_point
= 0;
2020 p
= _rl_make_prompt_for_search (pchar
? pchar
: '@');
2021 cxt
->flags
|= READSTR_FREEPMT
;
2022 rl_message ("%s", p
);
2025 RL_SETSTATE (RL_STATE_READSTR
);
2033 _rl_readstr_cleanup (_rl_readstr_cxt
*cxt
, int r
)
2035 _rl_rscxt_dispose (cxt
, 0);
2038 RL_UNSETSTATE (RL_STATE_READSTR
);
2044 _rl_readstr_restore (_rl_readstr_cxt
*cxt
)
2046 _rl_unsave_saved_readstr_line (); /* restores rl_undo_list */
2047 rl_point
= cxt
->save_point
;
2048 rl_mark
= cxt
->save_mark
;
2049 if (cxt
->flags
& READSTR_FREEPMT
)
2050 rl_restore_prompt (); /* _rl_make_prompt_for_search saved it */
2051 cxt
->flags
&= ~READSTR_FREEPMT
;
2052 rl_clear_message ();
2057 _rl_readstr_sigcleanup (_rl_readstr_cxt
*cxt
, int r
)
2059 if (cxt
->flags
& READSTR_FREEPMT
)
2060 rl_restore_prompt (); /* _rl_make_prompt_for_search saved it */
2061 cxt
->flags
&= ~READSTR_FREEPMT
;
2062 return (_rl_readstr_cleanup (cxt
, r
));
2066 _rl_readstr_getchar (_rl_readstr_cxt
*cxt
)
2070 cxt
->prevc
= cxt
->lastc
;
2072 /* Read a key and decide how to proceed. */
2073 RL_SETSTATE(RL_STATE_MOREINPUT
);
2074 c
= cxt
->lastc
= rl_read_key ();
2075 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
2077 #if defined (HANDLE_MULTIBYTE)
2078 /* This ends up with C (and LASTC) being set to the last byte of the
2079 multibyte character. In most cases c == lastc == mb[0] */
2080 if (c
>= 0 && MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
2081 c
= cxt
->lastc
= _rl_read_mbstring (cxt
->lastc
, cxt
->mb
, MB_LEN_MAX
);
2084 RL_CHECK_SIGNALS ();
2088 /* Process just-read character C according to readstr context CXT. Return -1
2089 if the caller should abort the read, 0 if we should break out of the
2090 loop, and 1 if we should continue to read characters. This can perform
2091 completion on the string read so far (stored in rl_line_buffer) if the
2092 caller has set up a completion function. The completion function can
2093 return -1 to indicate that we should abort the read. If we return -1
2094 we will call _rl_readstr_restore to clean up the state, leaving the caller
2095 to free the context. */
2097 _rl_readstr_dispatch (_rl_readstr_cxt
*cxt
, int c
)
2104 /* could consider looking up the function bound to they key and dispatching
2105 off that, but you want most characters inserted by default without having
2110 rl_unix_word_rubout (1, c
);
2114 rl_unix_line_discard (1, c
);
2119 n
= rl_quoted_insert (1, c
);
2122 _rl_readstr_restore (cxt
);
2125 cxt
->lastc
= (rl_point
> 0) ? rl_line_buffer
[rl_point
- 1] : rl_line_buffer
[0]; /* preserve prevc */
2136 _rl_readstr_restore (cxt
);
2139 _rl_rubout_char (1, c
);
2145 _rl_readstr_restore (cxt
);
2149 /* Allow users to bracketed-paste text into the string.
2150 Similar code is in search.c:_rl_nsearch_dispatch(). */
2151 if (_rl_enable_bracketed_paste
&& ((n
= _rl_nchars_available ()) >= (BRACK_PASTE_SLEN
-1)))
2153 if (_rl_read_bracketed_paste_prefix (c
) == 1)
2154 rl_bracketed_paste_begin (1, c
);
2157 c
= rl_read_key (); /* get the ESC that got pushed back */
2158 _rl_insert_char (1, c
);
2162 _rl_insert_char (1, c
);
2166 if ((cxt
->flags
& READSTR_NOSPACE
) == 0)
2168 _rl_insert_char (1, c
);
2173 /* Perform completion if the caller has set a completion function. */
2174 n
= (cxt
->compfunc
) ? (*cxt
->compfunc
) (cxt
, c
) : _rl_insert_char (1, c
);
2177 _rl_readstr_restore (cxt
);
2189 #if defined (HANDLE_MULTIBYTE)
2190 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
2191 rl_insert_text (cxt
->mb
);
2194 _rl_insert_char (1, c
);
2198 (*rl_redisplay_function
) ();
2199 rl_deactivate_mark ();
2203 /* **************************************************************** */
2205 /* Reading and Executing named commands */
2207 /* **************************************************************** */
2209 /* A completion generator for bindable readline command names. */
2211 readcmd_completion_function (const char *text
, int state
)
2213 static const char **cmdlist
= NULL
;
2214 static size_t lind
, nlen
;
2215 const char *cmdname
;
2222 cmdlist
= rl_funmap_names ();
2224 nlen
= RL_STRLEN (text
);
2226 if (cmdlist
== 0 || cmdlist
[lind
] == 0)
2227 return (char *)NULL
;
2229 while (cmdlist
[lind
])
2231 cmdname
= cmdlist
[lind
++];
2232 if (STREQN (text
, cmdname
, nlen
))
2233 return (savestring (cmdname
));
2235 return ((char *)NULL
);
2239 _rl_display_cmdname_matches (char **matches
)
2244 old
= rl_filename_completion_desired
;
2245 rl_filename_completion_desired
= 0;
2247 /* There is more than one match. Find out how many there are,
2248 and find the maximum printed length of a single entry. */
2249 for (max
= 0, i
= 1; matches
[i
]; i
++)
2251 len
= strlen (matches
[i
]);
2258 rl_display_match_list (matches
, len
, max
);
2259 rl_filename_completion_desired
= old
;
2261 rl_forced_update_display ();
2262 rl_display_fixed
= 1;
2266 _rl_readcmd_complete (_rl_readstr_cxt
*cxt
, int c
)
2272 matches
= rl_completion_matches (rl_line_buffer
, readcmd_completion_function
);
2274 if (RL_SIG_RECEIVED())
2276 _rl_free_match_list (matches
);
2278 RL_CHECK_SIGNALS ();
2281 else if (matches
== 0)
2284 /* Whether or not there are multiple matches, we just want to append the
2285 new characters in matches[0]. We display possible matches if we didn't
2289 prefix
= matches
[0];
2290 plen
= strlen (prefix
);
2295 for (n
= rl_end
; n
< plen
&& prefix
[n
]; n
++)
2296 _rl_insert_char (1, prefix
[n
]);
2298 else if (matches
[1])
2299 _rl_display_cmdname_matches (matches
);
2300 _rl_free_match_list (matches
);
2306 /* Use the readstr functions to read a bindable command name using the
2307 line buffer, with completion. */
2309 _rl_read_command_name ()
2311 _rl_readstr_cxt
*cxt
;
2315 cxt
= _rl_readstr_init ('!', READSTR_NOSPACE
);
2316 cxt
->compfunc
= _rl_readcmd_complete
;
2318 /* skip callback stuff for now */
2322 c
= _rl_readstr_getchar (cxt
);
2326 _rl_readstr_restore (cxt
);
2327 _rl_readstr_cleanup (cxt
, r
);
2334 r
= _rl_readstr_dispatch (cxt
, c
);
2337 _rl_readstr_cleanup (cxt
, r
);
2338 return NULL
; /* dispatch function cleans up */
2344 ret
= savestring (rl_line_buffer
);
2346 /* Now restore the original line and perform one final redisplay. */
2347 _rl_readstr_restore (cxt
);
2348 (*rl_redisplay_function
) ();
2350 /* And free up the context. */
2351 _rl_readstr_cleanup (cxt
, r
);
2355 /* Read a command name from the keyboard and execute it as if the bound key
2356 sequence had been entered. */
2358 rl_execute_named_command (int count
, int key
)
2361 rl_command_func_t
*func
;
2364 command
= _rl_read_command_name ();
2365 if (command
== 0 || *command
== '\0')
2370 func
= rl_named_function (command
);
2376 prev
= rl_dispatching
;
2377 ostate
= RL_ISSTATE (RL_STATE_DISPATCHING
);
2379 RL_SETSTATE (RL_STATE_DISPATCHING
); /* make sure it's set */
2380 r
= (*func
) (count
, key
);
2382 RL_UNSETSTATE (RL_STATE_DISPATCHING
); /* unset it if it wasn't set */
2383 rl_dispatching
= prev
;