forked from vim/vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_getln.c
4857 lines (4372 loc) · 113 KB
/
ex_getln.c
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
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* ex_getln.c: Functions for entering and editing an Ex command line.
*/
#include "vim.h"
#ifndef MAX
# define MAX(x,y) ((x) > (y) ? (x) : (y))
#endif
// Return value when handling keys in command-line mode.
#define CMDLINE_NOT_CHANGED 1
#define CMDLINE_CHANGED 2
#define GOTO_NORMAL_MODE 3
#define PROCESS_NEXT_KEY 4
// The current cmdline_info. It is initialized in getcmdline() and after that
// used by other functions. When invoking getcmdline() recursively it needs
// to be saved with save_cmdline() and restored with restore_cmdline().
static cmdline_info_T ccline;
#ifdef FEAT_EVAL
static int new_cmdpos; // position set by set_cmdline_pos()
#endif
static int extra_char = NUL; // extra character to display when redrawing
// the command line
static int extra_char_shift;
#ifdef FEAT_RIGHTLEFT
static int cmd_hkmap = 0; // Hebrew mapping during command line
#endif
static char_u *getcmdline_int(int firstc, long count, int indent, int clear_ccline);
static int cmdline_charsize(int idx);
static void set_cmdspos(void);
static void set_cmdspos_cursor(void);
static void correct_cmdspos(int idx, int cells);
static void alloc_cmdbuff(int len);
static void draw_cmdline(int start, int len);
static void save_cmdline(cmdline_info_T *ccp);
static void restore_cmdline(cmdline_info_T *ccp);
static int cmdline_paste(int regname, int literally, int remcr);
static void redrawcmdprompt(void);
static int ccheck_abbr(int);
static int open_cmdwin(void);
#ifdef FEAT_SEARCH_EXTRA
static int empty_pattern_magic(char_u *pat, size_t len, magic_T magic_val);
#endif
static int cedit_key = -1; // key value of 'cedit' option
static void
trigger_cmd_autocmd(int typechar, int evt)
{
char_u typestr[2];
typestr[0] = typechar;
typestr[1] = NUL;
apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
}
/*
* Abandon the command line.
*/
static void
abandon_cmdline(void)
{
VIM_CLEAR(ccline.cmdbuff);
if (msg_scrolled == 0)
compute_cmdrow();
msg("");
redraw_cmdline = TRUE;
}
#ifdef FEAT_SEARCH_EXTRA
/*
* Guess that the pattern matches everything. Only finds specific cases, such
* as a trailing \|, which can happen while typing a pattern.
*/
static int
empty_pattern(char_u *p, int delim)
{
size_t n = STRLEN(p);
magic_T magic_val = MAGIC_ON;
if (n > 0)
(void) skip_regexp_ex(p, delim, magic_isset(), NULL, NULL, &magic_val);
else
return TRUE;
return empty_pattern_magic(p, n, magic_val);
}
static int
empty_pattern_magic(char_u *p, size_t len, magic_T magic_val)
{
// remove trailing \v and the like
while (len >= 2 && p[len - 2] == '\\'
&& vim_strchr((char_u *)"mMvVcCZ", p[len - 1]) != NULL)
len -= 2;
// true, if the pattern is empty, or the pattern ends with \| and magic is
// set (or it ends with '|' and very magic is set)
return len == 0 || (len > 1
&& ((p[len - 2] == '\\'
&& p[len - 1] == '|' && magic_val == MAGIC_ON)
|| (p[len - 2] != '\\'
&& p[len - 1] == '|' && magic_val == MAGIC_ALL)));
}
// Struct to store the viewstate during 'incsearch' highlighting.
typedef struct {
colnr_T vs_curswant;
colnr_T vs_leftcol;
linenr_T vs_topline;
# ifdef FEAT_DIFF
int vs_topfill;
# endif
linenr_T vs_botline;
linenr_T vs_empty_rows;
} viewstate_T;
static void
save_viewstate(viewstate_T *vs)
{
vs->vs_curswant = curwin->w_curswant;
vs->vs_leftcol = curwin->w_leftcol;
vs->vs_topline = curwin->w_topline;
# ifdef FEAT_DIFF
vs->vs_topfill = curwin->w_topfill;
# endif
vs->vs_botline = curwin->w_botline;
vs->vs_empty_rows = curwin->w_empty_rows;
}
static void
restore_viewstate(viewstate_T *vs)
{
curwin->w_curswant = vs->vs_curswant;
curwin->w_leftcol = vs->vs_leftcol;
curwin->w_topline = vs->vs_topline;
# ifdef FEAT_DIFF
curwin->w_topfill = vs->vs_topfill;
# endif
curwin->w_botline = vs->vs_botline;
curwin->w_empty_rows = vs->vs_empty_rows;
}
// Struct to store the state of 'incsearch' highlighting.
typedef struct {
pos_T search_start; // where 'incsearch' starts searching
pos_T save_cursor;
int winid; // window where this state is valid
viewstate_T init_viewstate;
viewstate_T old_viewstate;
pos_T match_start;
pos_T match_end;
int did_incsearch;
int incsearch_postponed;
optmagic_T magic_overruled_save;
} incsearch_state_T;
static void
init_incsearch_state(incsearch_state_T *is_state)
{
is_state->winid = curwin->w_id;
is_state->match_start = curwin->w_cursor;
is_state->did_incsearch = FALSE;
is_state->incsearch_postponed = FALSE;
is_state->magic_overruled_save = magic_overruled;
CLEAR_POS(&is_state->match_end);
is_state->save_cursor = curwin->w_cursor; // may be restored later
is_state->search_start = curwin->w_cursor;
save_viewstate(&is_state->init_viewstate);
save_viewstate(&is_state->old_viewstate);
}
/*
* First move cursor to end of match, then to the start. This
* moves the whole match onto the screen when 'nowrap' is set.
*/
static void
set_search_match(pos_T *t)
{
t->lnum += search_match_lines;
t->col = search_match_endcol;
if (t->lnum > curbuf->b_ml.ml_line_count)
{
t->lnum = curbuf->b_ml.ml_line_count;
coladvance((colnr_T)MAXCOL);
}
}
/*
* Return TRUE when 'incsearch' highlighting is to be done.
* Sets search_first_line and search_last_line to the address range.
* May change the last search pattern.
*/
static int
do_incsearch_highlighting(
int firstc,
int *search_delim,
incsearch_state_T *is_state,
int *skiplen,
int *patlen)
{
char_u *cmd;
cmdmod_T dummy_cmdmod;
char_u *p;
int delim_optional = FALSE;
int delim;
char_u *end;
char *dummy;
exarg_T ea;
pos_T save_cursor;
int use_last_pat;
int retval = FALSE;
magic_T magic = 0;
*skiplen = 0;
*patlen = ccline.cmdlen;
if (!p_is || cmd_silent)
return FALSE;
// by default search all lines
search_first_line = 0;
search_last_line = MAXLNUM;
if (firstc == '/' || firstc == '?')
{
*search_delim = firstc;
return TRUE;
}
if (firstc != ':')
return FALSE;
++emsg_off;
CLEAR_FIELD(ea);
ea.line1 = 1;
ea.line2 = 1;
ea.cmd = ccline.cmdbuff;
ea.addr_type = ADDR_LINES;
parse_command_modifiers(&ea, &dummy, &dummy_cmdmod, TRUE);
cmd = skip_range(ea.cmd, TRUE, NULL);
if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
goto theend;
// Skip over "substitute" to find the pattern separator.
for (p = cmd; ASCII_ISALPHA(*p); ++p)
;
if (*skipwhite(p) == NUL)
goto theend;
if (STRNCMP(cmd, "substitute", p - cmd) == 0
|| STRNCMP(cmd, "smagic", p - cmd) == 0
|| STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
|| STRNCMP(cmd, "vglobal", p - cmd) == 0)
{
if (*cmd == 's' && cmd[1] == 'm')
magic_overruled = OPTION_MAGIC_ON;
else if (*cmd == 's' && cmd[1] == 'n')
magic_overruled = OPTION_MAGIC_OFF;
}
else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
{
// skip over ! and flags
if (*p == '!')
p = skipwhite(p + 1);
while (ASCII_ISALPHA(*(p = skipwhite(p))))
++p;
if (*p == NUL)
goto theend;
}
else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
|| STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
|| STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
|| STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
|| STRNCMP(cmd, "global", p - cmd) == 0)
{
// skip over "!"
if (*p == '!')
{
p++;
if (*skipwhite(p) == NUL)
goto theend;
}
if (*cmd != 'g')
delim_optional = TRUE;
}
else
goto theend;
p = skipwhite(p);
delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
*search_delim = delim;
end = skip_regexp_ex(p, delim, magic_isset(), NULL, NULL, &magic);
use_last_pat = end == p && *end == delim;
if (end == p && !use_last_pat)
goto theend;
// Don't do 'hlsearch' highlighting if the pattern matches everything.
if (!use_last_pat)
{
char c = *end;
int empty;
*end = NUL;
empty = empty_pattern_magic(p, STRLEN(p), magic);
*end = c;
if (empty)
goto theend;
}
// found a non-empty pattern or //
*skiplen = (int)(p - ccline.cmdbuff);
*patlen = (int)(end - p);
// parse the address range
save_cursor = curwin->w_cursor;
curwin->w_cursor = is_state->search_start;
parse_cmd_address(&ea, &dummy, TRUE);
if (ea.addr_count > 0)
{
// Allow for reverse match.
if (ea.line2 < ea.line1)
{
search_first_line = ea.line2;
search_last_line = ea.line1;
}
else
{
search_first_line = ea.line1;
search_last_line = ea.line2;
}
}
else if (cmd[0] == 's' && cmd[1] != 'o')
{
// :s defaults to the current line
search_first_line = curwin->w_cursor.lnum;
search_last_line = curwin->w_cursor.lnum;
}
curwin->w_cursor = save_cursor;
retval = TRUE;
theend:
--emsg_off;
return retval;
}
static void
finish_incsearch_highlighting(
int gotesc,
incsearch_state_T *is_state,
int call_update_screen)
{
if (!is_state->did_incsearch)
return;
is_state->did_incsearch = FALSE;
if (gotesc)
curwin->w_cursor = is_state->save_cursor;
else
{
if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
{
// put the '" mark at the original position
curwin->w_cursor = is_state->save_cursor;
setpcmark();
}
curwin->w_cursor = is_state->search_start;
}
restore_viewstate(&is_state->old_viewstate);
highlight_match = FALSE;
// by default search all lines
search_first_line = 0;
search_last_line = MAXLNUM;
magic_overruled = is_state->magic_overruled_save;
validate_cursor(); // needed for TAB
status_redraw_all();
redraw_all_later(UPD_SOME_VALID);
if (call_update_screen)
update_screen(UPD_SOME_VALID);
}
/*
* Do 'incsearch' highlighting if desired.
*/
static void
may_do_incsearch_highlighting(
int firstc,
long count,
incsearch_state_T *is_state)
{
int skiplen, patlen;
int found; // do_search() result
pos_T end_pos;
#ifdef FEAT_RELTIME
searchit_arg_T sia;
#endif
int next_char;
int use_last_pat;
int did_do_incsearch = is_state->did_incsearch;
int search_delim;
// Parsing range may already set the last search pattern.
// NOTE: must call restore_last_search_pattern() before returning!
save_last_search_pattern();
if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
&skiplen, &patlen))
{
restore_last_search_pattern();
finish_incsearch_highlighting(FALSE, is_state, TRUE);
if (did_do_incsearch && vpeekc() == NUL)
// may have skipped a redraw, do it now
redrawcmd();
return;
}
// If there is a character waiting, search and redraw later.
if (char_avail())
{
restore_last_search_pattern();
is_state->incsearch_postponed = TRUE;
return;
}
is_state->incsearch_postponed = FALSE;
if (search_first_line == 0)
// start at the original cursor position
curwin->w_cursor = is_state->search_start;
else if (search_first_line > curbuf->b_ml.ml_line_count)
{
// start after the last line
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
curwin->w_cursor.col = MAXCOL;
}
else
{
// start at the first line in the range
curwin->w_cursor.lnum = search_first_line;
curwin->w_cursor.col = 0;
}
// Use the previous pattern for ":s//".
next_char = ccline.cmdbuff[skiplen + patlen];
use_last_pat = patlen == 0 && skiplen > 0
&& ccline.cmdbuff[skiplen - 1] == next_char;
// If there is no pattern, don't do anything.
if (patlen == 0 && !use_last_pat)
{
found = 0;
set_no_hlsearch(TRUE); // turn off previous highlight
redraw_all_later(UPD_SOME_VALID);
}
else
{
int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
cursor_off(); // so the user knows we're busy
out_flush();
++emsg_off; // so it doesn't beep if bad expr
if (!p_hls)
search_flags += SEARCH_KEEP;
if (search_first_line != 0)
search_flags += SEARCH_START;
ccline.cmdbuff[skiplen + patlen] = NUL;
#ifdef FEAT_RELTIME
CLEAR_FIELD(sia);
// Set the time limit to half a second.
sia.sa_tm = 500;
#endif
found = do_search(NULL, firstc == ':' ? '/' : firstc, search_delim,
ccline.cmdbuff + skiplen, count, search_flags,
#ifdef FEAT_RELTIME
&sia
#else
NULL
#endif
);
ccline.cmdbuff[skiplen + patlen] = next_char;
--emsg_off;
if (curwin->w_cursor.lnum < search_first_line
|| curwin->w_cursor.lnum > search_last_line)
{
// match outside of address range
found = 0;
curwin->w_cursor = is_state->search_start;
}
// if interrupted while searching, behave like it failed
if (got_int)
{
(void)vpeekc(); // remove <C-C> from input stream
got_int = FALSE; // don't abandon the command line
found = 0;
}
else if (char_avail())
// cancelled searching because a char was typed
is_state->incsearch_postponed = TRUE;
}
if (found != 0)
highlight_match = TRUE; // highlight position
else
highlight_match = FALSE; // remove highlight
// First restore the old curwin values, so the screen is positioned in the
// same way as the actual search command.
restore_viewstate(&is_state->old_viewstate);
changed_cline_bef_curs();
update_topline();
if (found != 0)
{
pos_T save_pos = curwin->w_cursor;
is_state->match_start = curwin->w_cursor;
set_search_match(&curwin->w_cursor);
validate_cursor();
end_pos = curwin->w_cursor;
is_state->match_end = end_pos;
curwin->w_cursor = save_pos;
}
else
end_pos = curwin->w_cursor; // shutup gcc 4
// Disable 'hlsearch' highlighting if the pattern matches everything.
// Avoids a flash when typing "foo\|".
if (!use_last_pat)
{
next_char = ccline.cmdbuff[skiplen + patlen];
ccline.cmdbuff[skiplen + patlen] = NUL;
if (empty_pattern(ccline.cmdbuff + skiplen, search_delim)
&& !no_hlsearch)
{
redraw_all_later(UPD_SOME_VALID);
set_no_hlsearch(TRUE);
}
ccline.cmdbuff[skiplen + patlen] = next_char;
}
validate_cursor();
// May redraw the status line to show the cursor position.
if (p_ru && curwin->w_status_height > 0)
curwin->w_redr_status = TRUE;
update_screen(UPD_SOME_VALID);
highlight_match = FALSE;
restore_last_search_pattern();
// Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
// end of the pattern, e.g. for ":s/pat/".
if (ccline.cmdbuff[skiplen + patlen] != NUL)
curwin->w_cursor = is_state->search_start;
else if (found != 0)
curwin->w_cursor = end_pos;
msg_starthere();
redrawcmdline();
is_state->did_incsearch = TRUE;
}
/*
* May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
* or previous match.
* Returns FAIL when jumping to cmdline_not_changed;
*/
static int
may_adjust_incsearch_highlighting(
int firstc,
long count,
incsearch_state_T *is_state,
int c)
{
int skiplen, patlen;
pos_T t;
char_u *pat;
int search_flags = SEARCH_NOOF;
int i;
int save;
int search_delim;
// Parsing range may already set the last search pattern.
// NOTE: must call restore_last_search_pattern() before returning!
save_last_search_pattern();
if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
&skiplen, &patlen))
{
restore_last_search_pattern();
return OK;
}
if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
{
restore_last_search_pattern();
return FAIL;
}
if (search_delim == ccline.cmdbuff[skiplen])
{
pat = last_search_pattern();
if (pat == NULL)
{
restore_last_search_pattern();
return FAIL;
}
skiplen = 0;
patlen = (int)STRLEN(pat);
}
else
pat = ccline.cmdbuff + skiplen;
cursor_off();
out_flush();
if (c == Ctrl_G)
{
t = is_state->match_end;
if (LT_POS(is_state->match_start, is_state->match_end))
// Start searching at the end of the match not at the beginning of
// the next column.
(void)decl(&t);
search_flags += SEARCH_COL;
}
else
t = is_state->match_start;
if (!p_hls)
search_flags += SEARCH_KEEP;
++emsg_off;
save = pat[patlen];
pat[patlen] = NUL;
i = searchit(curwin, curbuf, &t, NULL,
c == Ctrl_G ? FORWARD : BACKWARD,
pat, count, search_flags, RE_SEARCH, NULL);
--emsg_off;
pat[patlen] = save;
if (i)
{
is_state->search_start = is_state->match_start;
is_state->match_end = t;
is_state->match_start = t;
if (c == Ctrl_T && firstc != '?')
{
// Move just before the current match, so that when nv_search
// finishes the cursor will be put back on the match.
is_state->search_start = t;
(void)decl(&is_state->search_start);
}
else if (c == Ctrl_G && firstc == '?')
{
// Move just after the current match, so that when nv_search
// finishes the cursor will be put back on the match.
is_state->search_start = t;
(void)incl(&is_state->search_start);
}
if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
{
// wrap around
is_state->search_start = t;
if (firstc == '?')
(void)incl(&is_state->search_start);
else
(void)decl(&is_state->search_start);
}
set_search_match(&is_state->match_end);
curwin->w_cursor = is_state->match_start;
changed_cline_bef_curs();
update_topline();
validate_cursor();
highlight_match = TRUE;
save_viewstate(&is_state->old_viewstate);
update_screen(UPD_NOT_VALID);
highlight_match = FALSE;
redrawcmdline();
curwin->w_cursor = is_state->match_end;
}
else
vim_beep(BO_ERROR);
restore_last_search_pattern();
return FAIL;
}
/*
* When CTRL-L typed: add character from the match to the pattern.
* May set "*c" to the added character.
* Return OK when jumping to cmdline_not_changed.
*/
static int
may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
{
int skiplen, patlen, search_delim;
// Parsing range may already set the last search pattern.
// NOTE: must call restore_last_search_pattern() before returning!
save_last_search_pattern();
if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
&skiplen, &patlen))
{
restore_last_search_pattern();
return FAIL;
}
restore_last_search_pattern();
// Add a character from under the cursor for 'incsearch'.
if (is_state->did_incsearch)
{
curwin->w_cursor = is_state->match_end;
*c = gchar_cursor();
if (*c != NUL)
{
// If 'ignorecase' and 'smartcase' are set and the
// command line has no uppercase characters, convert
// the character to lowercase.
if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
*c = MB_TOLOWER(*c);
if (*c == search_delim || vim_strchr((char_u *)(
magic_isset() ? "\\~^$.*[" : "\\^$"), *c) != NULL)
{
// put a backslash before special characters
stuffcharReadbuff(*c);
*c = '\\';
}
// add any composing characters
if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
{
int save_c = *c;
while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
{
curwin->w_cursor.col += mb_char2len(*c);
*c = gchar_cursor();
stuffcharReadbuff(*c);
}
*c = save_c;
}
return FAIL;
}
}
return OK;
}
#endif
#ifdef FEAT_ARABIC
/*
* Return TRUE if the command line has an Arabic character at or after "start"
* for "len" bytes.
*/
static int
cmdline_has_arabic(int start, int len)
{
int j;
int mb_l;
int u8c;
char_u *p;
int u8cc[MAX_MCO];
if (!enc_utf8)
return FALSE;
for (j = start; j < start + len; j += mb_l)
{
p = ccline.cmdbuff + j;
u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
mb_l = utfc_ptr2len_len(p, start + len - j);
if (ARABIC_CHAR(u8c))
return TRUE;
}
return FALSE;
}
#endif
void
cmdline_init(void)
{
CLEAR_FIELD(ccline);
}
/*
* Handle CTRL-\ pressed in Command-line mode:
* - CTRL-\ CTRL-N goes to Normal mode
* - CTRL-\ CTRL-G goes to Insert mode when 'insertmode' is set
* - CTRL-\ e prompts for an expression.
*/
static int
cmdline_handle_ctrl_bsl(int c, int *gotesc)
{
++no_mapping;
++allow_keys;
c = plain_vgetc();
--no_mapping;
--allow_keys;
// CTRL-\ e doesn't work when obtaining an expression, unless it
// is in a mapping.
if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
|| (ccline.cmdfirstc == '=' && KeyTyped)
#ifdef FEAT_EVAL
|| cmdline_star > 0
#endif
))
{
vungetc(c);
return PROCESS_NEXT_KEY;
}
#ifdef FEAT_EVAL
if (c == 'e')
{
char_u *p = NULL;
int len;
/*
* Replace the command line with the result of an expression.
* This will call getcmdline() recursively in get_expr_register().
*/
if (ccline.cmdpos == ccline.cmdlen)
new_cmdpos = 99999; // keep it at the end
else
new_cmdpos = ccline.cmdpos;
c = get_expr_register();
if (c == '=')
{
// Evaluate the expression. Set "textlock" to avoid nasty things
// like going to another buffer.
++textlock;
p = get_expr_line();
--textlock;
if (p != NULL)
{
len = (int)STRLEN(p);
if (realloc_cmdbuff(len + 1) == OK)
{
ccline.cmdlen = len;
STRCPY(ccline.cmdbuff, p);
vim_free(p);
// Restore the cursor or use the position set with
// set_cmdline_pos().
if (new_cmdpos > ccline.cmdlen)
ccline.cmdpos = ccline.cmdlen;
else
ccline.cmdpos = new_cmdpos;
KeyTyped = FALSE; // Don't do p_wc completion.
redrawcmd();
return CMDLINE_CHANGED;
}
vim_free(p);
}
}
beep_flush();
got_int = FALSE; // don't abandon the command line
did_emsg = FALSE;
emsg_on_display = FALSE;
redrawcmd();
return CMDLINE_NOT_CHANGED;
}
#endif
if (c == Ctrl_G && p_im && restart_edit == 0)
restart_edit = 'a';
*gotesc = TRUE; // will free ccline.cmdbuff after putting it
// in history
return GOTO_NORMAL_MODE;
}
/*
* Completion for 'wildchar' or 'wildcharm' key.
* - hitting <ESC> twice means: abandon command line.
* - wildcard expansion is only done when the 'wildchar' key is really
* typed, not when it comes from a macro
* Returns CMDLINE_CHANGED if command line is changed or CMDLINE_NOT_CHANGED.
*/
static int
cmdline_wildchar_complete(
int c,
int escape,
int *did_wild_list,
int *wim_index_p,
expand_T *xp,
int *gotesc)
{
int wim_index = *wim_index_p;
int res;
int j;
int options = WILD_NO_BEEP;
if (wim_flags[wim_index] & WIM_BUFLASTUSED)
options |= WILD_BUFLASTUSED;
if (xp->xp_numfiles > 0) // typed p_wc at least twice
{
// if 'wildmode' contains "list" may still need to list
if (xp->xp_numfiles > 1
&& !*did_wild_list
&& ((wim_flags[wim_index] & WIM_LIST)
|| (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)))
{
(void)showmatches(xp,
p_wmnu && ((wim_flags[wim_index] & WIM_LIST) == 0));
redrawcmd();
*did_wild_list = TRUE;
}
if (wim_flags[wim_index] & WIM_LONGEST)
res = nextwild(xp, WILD_LONGEST, options, escape);
else if (wim_flags[wim_index] & WIM_FULL)
res = nextwild(xp, WILD_NEXT, options, escape);
else
res = OK; // don't insert 'wildchar' now
}
else // typed p_wc first time
{
wim_index = 0;
j = ccline.cmdpos;
// if 'wildmode' first contains "longest", get longest
// common part
if (wim_flags[0] & WIM_LONGEST)
res = nextwild(xp, WILD_LONGEST, options, escape);
else
res = nextwild(xp, WILD_EXPAND_KEEP, options, escape);
// if interrupted while completing, behave like it failed
if (got_int)
{
(void)vpeekc(); // remove <C-C> from input stream
got_int = FALSE; // don't abandon the command line
(void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
xp->xp_context = EXPAND_NOTHING;
*wim_index_p = wim_index;
return CMDLINE_CHANGED;
}
// when more than one match, and 'wildmode' first contains
// "list", or no change and 'wildmode' contains "longest,list",
// list all matches
if (res == OK && xp->xp_numfiles > 1)
{
// a "longest" that didn't do anything is skipped (but not
// "list:longest")
if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
wim_index = 1;
if ((wim_flags[wim_index] & WIM_LIST)
|| (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0))
{
if (!(wim_flags[0] & WIM_LONGEST))
{
int p_wmnu_save = p_wmnu;
p_wmnu = 0;
// remove match
nextwild(xp, WILD_PREV, 0, escape);
p_wmnu = p_wmnu_save;
}
(void)showmatches(xp, p_wmnu
&& ((wim_flags[wim_index] & WIM_LIST) == 0));
redrawcmd();
*did_wild_list = TRUE;
if (wim_flags[wim_index] & WIM_LONGEST)
nextwild(xp, WILD_LONGEST, options, escape);
else if (wim_flags[wim_index] & WIM_FULL)
nextwild(xp, WILD_NEXT, options, escape);
}
else
vim_beep(BO_WILD);
}
else if (xp->xp_numfiles == -1)
xp->xp_context = EXPAND_NOTHING;
}
if (wim_index < 3)
++wim_index;
if (c == ESC)
*gotesc = TRUE;
*wim_index_p = wim_index;
return (res == OK) ? CMDLINE_CHANGED : CMDLINE_NOT_CHANGED;
}