-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.cc
1783 lines (1492 loc) · 41.8 KB
/
text.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* text.c -- text handling commands for readline. */
/* Copyright (C) 1987-2017 Free Software Foundation, Inc.
This file is part of the GNU Readline Library (Readline), a library
for reading lines of text with interactive input and history editing.
Readline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Readline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Readline. If not, see <http://www.gnu.org/licenses/>.
*/
#define READLINE_LIBRARY
#if defined(HAVE_CONFIG_H)
#include <config.hh>
#endif
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#if defined(HAVE_STDLIB_H)
#include <stdlib.h>
#else
#include "ansi_stdlib.hh"
#endif /* HAVE_STDLIB_H */
#if defined(HAVE_LOCALE_H)
#include <locale.h>
#endif
#include <stdio.h>
/* System-specific feature definitions and include files. */
#include "rldefs.hh"
#include "rlmbutil.hh"
#if defined(__EMX__)
#define INCL_DOSPROCESS
#include <os2.h>
#endif /* __EMX__ */
/* Some standard library routines. */
#include "history.hh"
#include "readline.hh"
#include "rlprivate.hh"
#include "rlshell.hh"
#include "xmalloc.hh"
/* Forward declarations. */
static int rl_change_case PARAMS((int, int));
static int _rl_char_search PARAMS((int, int, int));
#if defined(READLINE_CALLBACKS)
static int _rl_insert_next_callback PARAMS((_rl_callback_generic_arg*));
static int _rl_char_search_callback PARAMS((_rl_callback_generic_arg*));
#endif
/* The largest chunk of text that can be inserted in one call to
rl_insert_text. Text blocks larger than this are divided. */
#define TEXT_COUNT_MAX 1024
int _rl_optimize_typeahead= 1; /* rl_insert tries to read typeahead */
/* **************************************************************** */
/* */
/* Insert and Delete */
/* */
/* **************************************************************** */
/* Insert a string of text into the line at point. This is the only
way that you should do insertion. _rl_insert_char () calls this
function. Returns the number of characters inserted. */
int rl_insert_text(const char* string)
{
int i, l;
l= (string && *string) ? strlen(string) : 0;
if (l == 0)
return 0;
if (rl_end + l >= rl_line_buffer_len)
rl_extend_line_buffer(rl_end + l);
for (i= rl_end; i >= rl_point; i--)
rl_line_buffer[i + l]= rl_line_buffer[i];
strncpy(rl_line_buffer + rl_point, string, l);
/* Remember how to undo this if we aren't undoing something. */
if (_rl_doing_an_undo == 0)
{
/* If possible and desirable, concatenate the undos. */
if ((l == 1) && rl_undo_list && (rl_undo_list->what == UNDO_INSERT) &&
(rl_undo_list->end == rl_point) &&
(rl_undo_list->end - rl_undo_list->start < 20))
rl_undo_list->end++;
else
rl_add_undo(UNDO_INSERT, rl_point, rl_point + l, (char*)NULL);
}
rl_point+= l;
rl_end+= l;
rl_line_buffer[rl_end]= '\0';
return l;
}
/* Delete the string between FROM and TO. FROM is inclusive, TO is not.
Returns the number of characters deleted. */
int rl_delete_text(int from, int to)
{
char* text;
int diff, i;
/* Fix it if the caller is confused. */
if (from > to)
SWAP(from, to);
/* fix boundaries */
if (to > rl_end)
{
to= rl_end;
if (from > to)
from= to;
}
if (from < 0)
from= 0;
text= rl_copy_text(from, to);
/* Some versions of strncpy() can't handle overlapping arguments. */
diff= to - from;
for (i= from; i < rl_end - diff; i++)
rl_line_buffer[i]= rl_line_buffer[i + diff];
/* Remember how to undo this delete. */
if (_rl_doing_an_undo == 0)
rl_add_undo(UNDO_DELETE, from, to, text);
else
xfree(text);
rl_end-= diff;
rl_line_buffer[rl_end]= '\0';
return (diff);
}
/* Fix up point so that it is within the line boundaries after killing
text. If FIX_MARK_TOO is non-zero, the mark is forced within line
boundaries also. */
#define _RL_FIX_POINT(x) \
do \
{ \
if (x > rl_end) \
x= rl_end; \
else if (x < 0) \
x= 0; \
} while (0)
void _rl_fix_point(int fix_mark_too)
{
_RL_FIX_POINT(rl_point);
if (fix_mark_too)
_RL_FIX_POINT(rl_mark);
}
#undef _RL_FIX_POINT
/* Replace the contents of the line buffer between START and END with
TEXT. The operation is undoable. To replace the entire line in an
undoable mode, use _rl_replace_text(text, 0, rl_end); */
int _rl_replace_text(const char* text, int start, int end)
{
int n;
n= 0;
rl_begin_undo_group();
if (start <= end)
rl_delete_text(start, end + 1);
rl_point= start;
if (*text)
n= rl_insert_text(text);
rl_end_undo_group();
return n;
}
/* Replace the current line buffer contents with TEXT. If CLEAR_UNDO is
non-zero, we free the current undo list. */
void rl_replace_line(const char* text, int clear_undo)
{
int len;
len= strlen(text);
if (len >= rl_line_buffer_len)
rl_extend_line_buffer(len);
strcpy(rl_line_buffer, text);
rl_end= len;
if (clear_undo)
rl_free_undo_list();
_rl_fix_point(1);
}
/* **************************************************************** */
/* */
/* Readline character functions */
/* */
/* **************************************************************** */
/* This is not a gap editor, just a stupid line input routine. No hair
is involved in writing any of the functions, and none should be. */
/* Note that:
rl_end is the place in the string that we would place '\0';
i.e., it is always safe to place '\0' there.
rl_point is the place in the string where the cursor is. Sometimes
this is the same as rl_end.
Any command that is called interactively receives two arguments.
The first is a count: the numeric arg passed to this command.
The second is the key which invoked this command.
*/
/* **************************************************************** */
/* */
/* Movement Commands */
/* */
/* **************************************************************** */
/* Note that if you `optimize' the display for these functions, you cannot
use said functions in other functions which do not do optimizing
display. I.e., you will have to update the data base for rl_redisplay,
and you might as well let rl_redisplay do that job. */
/* Move forward COUNT bytes. */
int rl_forward_byte(int count, int key)
{
if (count < 0)
return (rl_backward_byte(-count, key));
if (count > 0)
{
int end, lend;
end= rl_point + count;
#if defined(VI_MODE)
lend= rl_end > 0 ? rl_end - (VI_COMMAND_MODE()) : rl_end;
#else
lend= rl_end;
#endif
if (end > lend)
{
rl_point= lend;
rl_ding();
}
else
rl_point= end;
}
if (rl_end < 0)
rl_end= 0;
return 0;
}
int _rl_forward_char_internal(int count)
{
int point;
#if defined(HANDLE_MULTIBYTE)
point= _rl_find_next_mbchar(
rl_line_buffer, rl_point, count, MB_FIND_NONZERO);
#if defined(VI_MODE)
if (point >= rl_end && VI_COMMAND_MODE())
point= _rl_find_prev_mbchar(rl_line_buffer, rl_end, MB_FIND_NONZERO);
#endif
if (rl_end < 0)
rl_end= 0;
#else
point= rl_point + count;
#endif
if (point > rl_end)
point= rl_end;
return (point);
}
int _rl_backward_char_internal(int count)
{
int point;
point= rl_point;
#if defined(HANDLE_MULTIBYTE)
if (count > 0)
{
while (count > 0 && point > 0)
{
point= _rl_find_prev_mbchar(rl_line_buffer, point, MB_FIND_NONZERO);
count--;
}
if (count > 0)
return 0; /* XXX - rl_ding() here? */
}
#else
if (count > 0)
point-= count;
#endif
if (point < 0)
point= 0;
return (point);
}
#if defined(HANDLE_MULTIBYTE)
/* Move forward COUNT characters. */
int rl_forward_char(int count, int key)
{
int point;
if (MB_CUR_MAX == 1 || rl_byte_oriented)
return (rl_forward_byte(count, key));
if (count < 0)
return (rl_backward_char(-count, key));
if (count > 0)
{
if (rl_point == rl_end && EMACS_MODE())
{
rl_ding();
return 0;
}
point= _rl_forward_char_internal(count);
if (rl_point == point)
rl_ding();
rl_point= point;
}
return 0;
}
#else /* !HANDLE_MULTIBYTE */
int rl_forward_char(int count, int key)
{
return (rl_forward_byte(count, key));
}
#endif /* !HANDLE_MULTIBYTE */
/* Backwards compatibility. */
int rl_forward(int count, int key)
{
return (rl_forward_char(count, key));
}
/* Move backward COUNT bytes. */
int rl_backward_byte(int count, int key)
{
if (count < 0)
return (rl_forward_byte(-count, key));
if (count > 0)
{
if (rl_point < count)
{
rl_point= 0;
rl_ding();
}
else
rl_point-= count;
}
if (rl_point < 0)
rl_point= 0;
return 0;
}
#if defined(HANDLE_MULTIBYTE)
/* Move backward COUNT characters. */
int rl_backward_char(int count, int key)
{
int point;
if (MB_CUR_MAX == 1 || rl_byte_oriented)
return (rl_backward_byte(count, key));
if (count < 0)
return (rl_forward_char(-count, key));
if (count > 0)
{
point= rl_point;
while (count > 0 && point > 0)
{
point= _rl_find_prev_mbchar(rl_line_buffer, point, MB_FIND_NONZERO);
count--;
}
if (count > 0)
{
rl_point= 0;
rl_ding();
}
else
rl_point= point;
}
return 0;
}
#else
int rl_backward_char(int count, int key)
{
return (rl_backward_byte(count, key));
}
#endif
/* Backwards compatibility. */
int rl_backward(int count, int key)
{
return (rl_backward_char(count, key));
}
/* Move to the beginning of the line. */
int rl_beg_of_line(int count, int key)
{
rl_point= 0;
return 0;
}
/* Move to the end of the line. */
int rl_end_of_line(int count, int key)
{
rl_point= rl_end;
return 0;
}
/* Move forward a word. We do what Emacs does. Handles multibyte chars.
*/
int rl_forward_word(int count, int key)
{
int c;
if (count < 0)
return (rl_backward_word(-count, key));
while (count)
{
if (rl_point == rl_end)
return 0;
/* If we are not in a word, move forward until we are in one.
Then, move forward until we hit a non-alphabetic character. */
c= _rl_char_value(rl_line_buffer, rl_point);
if (_rl_walphabetic(c) == 0)
{
rl_point= MB_NEXTCHAR(rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
while (rl_point < rl_end)
{
c= _rl_char_value(rl_line_buffer, rl_point);
if (_rl_walphabetic(c))
break;
rl_point=
MB_NEXTCHAR(rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
}
}
if (rl_point == rl_end)
return 0;
rl_point= MB_NEXTCHAR(rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
while (rl_point < rl_end)
{
c= _rl_char_value(rl_line_buffer, rl_point);
if (_rl_walphabetic(c) == 0)
break;
rl_point= MB_NEXTCHAR(rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
}
--count;
}
return 0;
}
/* Move backward a word. We do what Emacs does. Handles multibyte chars.
*/
int rl_backward_word(int count, int key)
{
int c, p;
if (count < 0)
return (rl_forward_word(-count, key));
while (count)
{
if (rl_point == 0)
return 0;
/* Like rl_forward_word (), except that we look at the characters
just before point. */
p= MB_PREVCHAR(rl_line_buffer, rl_point, MB_FIND_NONZERO);
c= _rl_char_value(rl_line_buffer, p);
if (_rl_walphabetic(c) == 0)
{
rl_point= p;
while (rl_point > 0)
{
p= MB_PREVCHAR(rl_line_buffer, rl_point, MB_FIND_NONZERO);
c= _rl_char_value(rl_line_buffer, p);
if (_rl_walphabetic(c))
break;
rl_point= p;
}
}
while (rl_point)
{
p= MB_PREVCHAR(rl_line_buffer, rl_point, MB_FIND_NONZERO);
c= _rl_char_value(rl_line_buffer, p);
if (_rl_walphabetic(c) == 0)
break;
else
rl_point= p;
}
--count;
}
return 0;
}
/* Clear the current line. Numeric argument to C-l does this. */
int rl_refresh_line(int ignore1, int ignore2)
{
int curr_line;
curr_line= _rl_current_display_line();
_rl_move_vert(curr_line);
_rl_move_cursor_relative(0, rl_line_buffer); /* XXX is this right */
_rl_clear_to_eol(0); /* arg of 0 means to not use spaces */
rl_redraw_prompt_last_line();
rl_display_fixed= 1;
return 0;
}
/* C-l typed to a line without quoting clears the screen, and then reprints
the prompt and the current input line. Given a numeric arg, redraw only
the current line. */
int rl_clear_screen(int count, int key)
{
if (rl_explicit_arg)
{
rl_refresh_line(count, key);
return 0;
}
_rl_clear_screen(); /* calls termcap function to clear screen */
rl_forced_update_display();
rl_display_fixed= 1;
return 0;
}
int rl_previous_screen_line(int count, int key)
{
int c;
c= _rl_term_autowrap ? _rl_screenwidth : (_rl_screenwidth + 1);
return (rl_backward_char(c, key));
}
int rl_next_screen_line(int count, int key)
{
int c;
c= _rl_term_autowrap ? _rl_screenwidth : (_rl_screenwidth + 1);
return (rl_forward_char(c, key));
}
int rl_skip_csi_sequence(int count, int key)
{
int ch;
RL_SETSTATE(RL_STATE_MOREINPUT);
do
ch= rl_read_key();
while (ch >= 0x20 && ch < 0x40);
RL_UNSETSTATE(RL_STATE_MOREINPUT);
return (ch < 0);
}
int rl_arrow_keys(int count, int key)
{
int ch;
RL_SETSTATE(RL_STATE_MOREINPUT);
ch= rl_read_key();
RL_UNSETSTATE(RL_STATE_MOREINPUT);
if (ch < 0)
return (1);
switch (_rl_to_upper(ch))
{
case 'A':
rl_get_previous_history(count, ch);
break;
case 'B':
rl_get_next_history(count, ch);
break;
case 'C':
if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
rl_forward_char(count, ch);
else
rl_forward_byte(count, ch);
break;
case 'D':
if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
rl_backward_char(count, ch);
else
rl_backward_byte(count, ch);
break;
default:
rl_ding();
}
return 0;
}
/* **************************************************************** */
/* */
/* Text commands */
/* */
/* **************************************************************** */
#ifdef HANDLE_MULTIBYTE
static char pending_bytes[MB_LEN_MAX];
static int pending_bytes_length= 0;
static mbstate_t ps = { 0 };
#endif
/* Insert the character C at the current location, moving point forward.
If C introduces a multibyte sequence, we read the whole sequence and
then insert the multibyte char into the line buffer. */
int _rl_insert_char(int count, int c)
{
int i;
char* string;
#ifdef HANDLE_MULTIBYTE
int string_size;
char incoming[MB_LEN_MAX + 1];
int incoming_length= 0;
mbstate_t ps_back;
static int stored_count= 0;
#endif
if (count <= 0)
return 0;
#if defined(HANDLE_MULTIBYTE)
if (MB_CUR_MAX == 1 || rl_byte_oriented)
{
incoming[0] = c;
incoming[1] = '\0';
incoming_length= 1;
}
else if (_rl_utf8locale && (c & 0x80) == 0)
{
incoming[0] = c;
incoming[1] = '\0';
incoming_length= 1;
}
else
{
wchar_t wc;
size_t ret;
if (stored_count <= 0)
stored_count= count;
else
count= stored_count;
ps_back = ps;
pending_bytes[pending_bytes_length++]= c;
ret= mbrtowc(&wc, pending_bytes, pending_bytes_length, &ps);
if (ret == (size_t)-2)
{
/* Bytes too short to compose character, try to wait for next byte.
Restore the state of the byte sequence, because in this case the
effect of mbstate is undefined. */
ps= ps_back;
return 1;
}
else if (ret == (size_t)-1)
{
/* Invalid byte sequence for the current locale. Treat first byte
as a single character. */
incoming[0] = pending_bytes[0];
incoming[1] = '\0';
incoming_length= 1;
pending_bytes_length--;
memmove(pending_bytes, pending_bytes + 1, pending_bytes_length);
/* Clear the state of the byte sequence, because in this case the
effect of mbstate is undefined. */
memset(&ps, 0, sizeof(mbstate_t));
}
else if (ret == (size_t)0)
{
incoming[0] = '\0';
incoming_length= 0;
pending_bytes_length--;
/* Clear the state of the byte sequence, because in this case the
effect of mbstate is undefined. */
memset(&ps, 0, sizeof(mbstate_t));
}
else if (ret == 1)
{
incoming[0] = pending_bytes[0];
incoming[incoming_length= 1]= '\0';
pending_bytes_length = 0;
}
else
{
/* We successfully read a single multibyte character. */
memcpy(incoming, pending_bytes, pending_bytes_length);
incoming[pending_bytes_length]= '\0';
incoming_length = pending_bytes_length;
pending_bytes_length = 0;
}
}
#endif /* HANDLE_MULTIBYTE */
/* If we can optimize, then do it. But don't let people crash
readline because of extra large arguments. */
if (count > 1 && count <= TEXT_COUNT_MAX)
{
#if defined(HANDLE_MULTIBYTE)
string_size= count * incoming_length;
string = (char*)xmalloc(1 + string_size);
i= 0;
while (i < string_size)
{
if (incoming_length == 1)
string[i++]= *incoming;
else
{
strncpy(string + i, incoming, incoming_length);
i+= incoming_length;
}
}
incoming_length= 0;
stored_count = 0;
#else /* !HANDLE_MULTIBYTE */
string= (char*)xmalloc(1 + count);
for (i= 0; i < count; i++)
string[i]= c;
#endif /* !HANDLE_MULTIBYTE */
string[i]= '\0';
rl_insert_text(string);
xfree(string);
return 0;
}
if (count > TEXT_COUNT_MAX)
{
int decreaser;
#if defined(HANDLE_MULTIBYTE)
string_size= incoming_length * TEXT_COUNT_MAX;
string = (char*)xmalloc(1 + string_size);
i= 0;
while (i < string_size)
{
if (incoming_length == 1)
string[i++]= *incoming;
else
{
strncpy(string + i, incoming, incoming_length);
i+= incoming_length;
}
}
while (count)
{
decreaser= (count > TEXT_COUNT_MAX) ? TEXT_COUNT_MAX : count;
string[decreaser * incoming_length]= '\0';
rl_insert_text(string);
count-= decreaser;
}
xfree(string);
incoming_length= 0;
stored_count = 0;
#else /* !HANDLE_MULTIBYTE */
char str[TEXT_COUNT_MAX + 1];
for (i= 0; i < TEXT_COUNT_MAX; i++)
str[i]= c;
while (count)
{
decreaser= (count > TEXT_COUNT_MAX ? TEXT_COUNT_MAX : count);
str[decreaser]= '\0';
rl_insert_text(str);
count-= decreaser;
}
#endif /* !HANDLE_MULTIBYTE */
return 0;
}
if (MB_CUR_MAX == 1 || rl_byte_oriented)
{
/* We are inserting a single character.
If there is pending input, then make a string of all of the
pending characters that are bound to rl_insert, and insert
them all. Don't do this if we're current reading input from
a macro. */
if ((RL_ISSTATE(RL_STATE_MACROINPUT) == 0) &&
_rl_pushed_input_available())
_rl_insert_typein(c);
else
{
/* Inserting a single character. */
char str[2];
str[1]= '\0';
str[0]= c;
rl_insert_text(str);
}
}
#if defined(HANDLE_MULTIBYTE)
else
{
rl_insert_text(incoming);
stored_count= 0;
}
#endif
return 0;
}
/* Overwrite the character at point (or next COUNT characters) with C.
If C introduces a multibyte character sequence, read the entire sequence
before starting the overwrite loop. */
int _rl_overwrite_char(int count, int c)
{
int i;
#if defined(HANDLE_MULTIBYTE)
char mbkey[MB_LEN_MAX];
int k;
/* Read an entire multibyte character sequence to insert COUNT times. */
if (count > 0 && MB_CUR_MAX > 1 && rl_byte_oriented == 0)
k= _rl_read_mbstring(c, mbkey, MB_LEN_MAX);
#endif
rl_begin_undo_group();
for (i= 0; i < count; i++)
{
#if defined(HANDLE_MULTIBYTE)
if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
rl_insert_text(mbkey);
else
#endif
_rl_insert_char(1, c);
if (rl_point < rl_end)
rl_delete(1, c);
}
rl_end_undo_group();
return 0;
}
int rl_insert(int count, int c)
{
int r, n, x;
r= (rl_insert_mode == RL_IM_INSERT) ? _rl_insert_char(count, c)
: _rl_overwrite_char(count, c);
/* XXX -- attempt to batch-insert pending input that maps to self-insert
*/
x= 0;
n= (unsigned short)-2;
while (_rl_optimize_typeahead && rl_num_chars_to_read == 0 &&
(RL_ISSTATE(RL_STATE_INPUTPENDING | RL_STATE_MACROINPUT) == 0) &&
_rl_pushed_input_available() == 0 && _rl_input_queued(0) &&
(n= rl_read_key()) > 0 &&
_rl_keymap[(unsigned char)n].type == ISFUNC &&
_rl_keymap[(unsigned char)n].function == rl_insert)
{
r= (rl_insert_mode == RL_IM_INSERT) ? _rl_insert_char(1, n)
: _rl_overwrite_char(1, n);
/* _rl_insert_char keeps its own set of pending characters to compose a
complete multibyte character, and only returns 1 if it sees a character
that's part of a multibyte character but too short to complete one. We
can try to read another character in the hopes that we will get the
next one or just punt. Right now we try to read another character.
We don't want to call rl_insert_next if _rl_insert_char has already
stored the character in the pending_bytes array because that will
result in doubled input. */
n= (unsigned short)-2;
x++; /* count of bytes of typeahead read, currently unused */
if (r == 1) /* read partial multibyte character */
continue;
if (rl_done || r != 0)
break;
}
if (n !=
(unsigned short)-2) /* -2 = sentinel value for having inserted N */
{
/* setting rl_pending_input inhibits setting rl_last_func so we do it
ourselves here */
rl_last_func= rl_insert;
_rl_reset_argument();
rl_executing_keyseq[rl_key_sequence_length= 0]= '\0';
r = rl_execute_next(n);
}
return r;
}
/* Insert the next typed character verbatim. */
static int _rl_insert_next(int count)
{
int c;
RL_SETSTATE(RL_STATE_MOREINPUT);
c= rl_read_key();
RL_UNSETSTATE(RL_STATE_MOREINPUT);
if (c < 0)
return 1;
if (RL_ISSTATE(RL_STATE_MACRODEF))
_rl_add_macro_char(c);
#if defined(HANDLE_SIGNALS)
if (RL_ISSTATE(RL_STATE_CALLBACK) == 0)
_rl_restore_tty_signals();
#endif
return (_rl_insert_char(count, c));
}
#if defined(READLINE_CALLBACKS)
static int _rl_insert_next_callback(_rl_callback_generic_arg* data)
{
int count, r;
count= data->count;
r = 0;
if (count < 0)
{
data->count++;
r = _rl_insert_next(1);
_rl_want_redisplay= 1;
/* If we should keep going, leave the callback function installed */
if (data->count < 0 && r == 0)
return r;
count= 0; /* data->count == 0 || r != 0; force break below */