forked from vim/vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdexpand.c
4063 lines (3697 loc) · 97 KB
/
cmdexpand.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.
*/
/*
* cmdexpand.c: functions for command-line completion
*/
#include "vim.h"
static int cmd_showtail; // Only show path tail in lists ?
static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
char_u ***matches, int *numMatches,
char_u *((*func)(expand_T *, int)), int escaped);
static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
static char_u *showmatches_gettail(char_u *s);
static int expand_showtail(expand_T *xp);
static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
#if defined(FEAT_EVAL)
static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
#endif
// "compl_match_array" points the currently displayed list of entries in the
// popup menu. It is NULL when there is no popup menu.
static pumitem_T *compl_match_array = NULL;
static int compl_match_arraysize;
// First column in cmdline of the matched item for completion.
static int compl_startcol;
static int compl_selected;
#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
/*
* Returns TRUE if fuzzy completion is supported for a given cmdline completion
* context.
*/
static int
cmdline_fuzzy_completion_supported(expand_T *xp)
{
return (vim_strchr(p_wop, WOP_FUZZY) != NULL
&& xp->xp_context != EXPAND_BOOL_SETTINGS
&& xp->xp_context != EXPAND_COLORS
&& xp->xp_context != EXPAND_COMPILER
&& xp->xp_context != EXPAND_DIRECTORIES
&& xp->xp_context != EXPAND_FILES
&& xp->xp_context != EXPAND_FILES_IN_PATH
&& xp->xp_context != EXPAND_FILETYPE
&& xp->xp_context != EXPAND_HELP
&& xp->xp_context != EXPAND_OLD_SETTING
&& xp->xp_context != EXPAND_OWNSYNTAX
&& xp->xp_context != EXPAND_PACKADD
&& xp->xp_context != EXPAND_RUNTIME
&& xp->xp_context != EXPAND_SHELLCMD
&& xp->xp_context != EXPAND_TAGS
&& xp->xp_context != EXPAND_TAGS_LISTFILES
&& xp->xp_context != EXPAND_USER_LIST);
}
/*
* Returns TRUE if fuzzy completion for cmdline completion is enabled and
* 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
* matching.
*/
int
cmdline_fuzzy_complete(char_u *fuzzystr)
{
return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
}
/*
* sort function for the completion matches.
* <SNR> functions should be sorted to the end.
*/
static int
sort_func_compare(const void *s1, const void *s2)
{
char_u *p1 = *(char_u **)s1;
char_u *p2 = *(char_u **)s2;
if (*p1 != '<' && *p2 == '<') return -1;
if (*p1 == '<' && *p2 != '<') return 1;
return STRCMP(p1, p2);
}
/*
* Escape special characters in the cmdline completion matches.
*/
static void
wildescape(
expand_T *xp,
char_u *str,
int numfiles,
char_u **files)
{
char_u *p;
int vse_what = xp->xp_context == EXPAND_BUFFERS
? VSE_BUFFER : VSE_NONE;
if (xp->xp_context == EXPAND_FILES
|| xp->xp_context == EXPAND_FILES_IN_PATH
|| xp->xp_context == EXPAND_SHELLCMD
|| xp->xp_context == EXPAND_BUFFERS
|| xp->xp_context == EXPAND_DIRECTORIES)
{
// Insert a backslash into a file name before a space, \, %, #
// and wildmatch characters, except '~'.
for (int i = 0; i < numfiles; ++i)
{
// for ":set path=" we need to escape spaces twice
if (xp->xp_backslash == XP_BS_THREE)
{
p = vim_strsave_escaped(files[i], (char_u *)" ");
if (p != NULL)
{
vim_free(files[i]);
files[i] = p;
#if defined(BACKSLASH_IN_FILENAME)
p = vim_strsave_escaped(files[i], (char_u *)" ");
if (p != NULL)
{
vim_free(files[i]);
files[i] = p;
}
#endif
}
}
#ifdef BACKSLASH_IN_FILENAME
p = vim_strsave_fnameescape(files[i], vse_what);
#else
p = vim_strsave_fnameescape(files[i],
xp->xp_shell ? VSE_SHELL : vse_what);
#endif
if (p != NULL)
{
vim_free(files[i]);
files[i] = p;
}
// If 'str' starts with "\~", replace "~" at start of
// files[i] with "\~".
if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
escape_fname(&files[i]);
}
xp->xp_backslash = XP_BS_NONE;
// If the first file starts with a '+' escape it. Otherwise it
// could be seen as "+cmd".
if (*files[0] == '+')
escape_fname(&files[0]);
}
else if (xp->xp_context == EXPAND_TAGS)
{
// Insert a backslash before characters in a tag name that
// would terminate the ":tag" command.
for (int i = 0; i < numfiles; ++i)
{
p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
if (p != NULL)
{
vim_free(files[i]);
files[i] = p;
}
}
}
}
/*
* Escape special characters in the cmdline completion matches.
*/
static void
ExpandEscape(
expand_T *xp,
char_u *str,
int numfiles,
char_u **files,
int options)
{
// May change home directory back to "~"
if (options & WILD_HOME_REPLACE)
tilde_replace(str, numfiles, files);
if (options & WILD_ESCAPE)
wildescape(xp, str, numfiles, files);
}
/*
* Return FAIL if this is not an appropriate context in which to do
* completion of anything, return OK if it is (even if there are no matches).
* For the caller, this means that the character is just passed through like a
* normal character (instead of being expanded). This allows :s/^I^D etc.
*/
int
nextwild(
expand_T *xp,
int type,
int options, // extra options for ExpandOne()
int escape) // if TRUE, escape the returned matches
{
cmdline_info_T *ccline = get_cmdline_info();
int i, j;
char_u *p1;
char_u *p2;
int difflen;
int v;
if (xp->xp_numfiles == -1)
{
set_expand_context(xp);
cmd_showtail = expand_showtail(xp);
}
if (xp->xp_context == EXPAND_UNSUCCESSFUL)
{
beep_flush();
return OK; // Something illegal on command line
}
if (xp->xp_context == EXPAND_NOTHING)
{
// Caller can use the character as a normal char instead
return FAIL;
}
// If cmd_silent is set then don't show the dots, because redrawcmd() below
// won't remove them.
if (!cmd_silent)
{
msg_puts("..."); // show that we are busy
out_flush();
}
i = (int)(xp->xp_pattern - ccline->cmdbuff);
xp->xp_pattern_len = ccline->cmdpos - i;
if (type == WILD_NEXT || type == WILD_PREV
|| type == WILD_PAGEUP || type == WILD_PAGEDOWN)
{
// Get next/previous match for a previous expanded pattern.
p2 = ExpandOne(xp, NULL, NULL, 0, type);
}
else
{
if (cmdline_fuzzy_completion_supported(xp))
// If fuzzy matching, don't modify the search string
p1 = vim_strsave(xp->xp_pattern);
else
p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
// Translate string into pattern and expand it.
if (p1 == NULL)
p2 = NULL;
else
{
int use_options = options |
WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
if (escape)
use_options |= WILD_ESCAPE;
if (p_wic)
use_options += WILD_ICASE;
p2 = ExpandOne(xp, p1,
vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
use_options, type);
vim_free(p1);
// longest match: make sure it is not shorter, happens with :help
if (p2 != NULL && type == WILD_LONGEST)
{
for (j = 0; j < xp->xp_pattern_len; ++j)
if (ccline->cmdbuff[i + j] == '*'
|| ccline->cmdbuff[i + j] == '?')
break;
if ((int)STRLEN(p2) < j)
VIM_CLEAR(p2);
}
}
}
if (p2 != NULL && !got_int)
{
difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
{
v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
xp->xp_pattern = ccline->cmdbuff + i;
}
else
v = OK;
if (v == OK)
{
mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
&ccline->cmdbuff[ccline->cmdpos],
(size_t)(ccline->cmdlen - ccline->cmdpos + 1));
mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
ccline->cmdlen += difflen;
ccline->cmdpos += difflen;
}
}
vim_free(p2);
redrawcmd();
cursorcmd();
// When expanding a ":map" command and no matches are found, assume that
// the key is supposed to be inserted literally
if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
return FAIL;
if (xp->xp_numfiles <= 0 && p2 == NULL)
beep_flush();
else if (xp->xp_numfiles == 1)
// free expanded pattern
(void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
return OK;
}
/*
* Create and display a cmdline completion popup menu with items from
* 'matches'.
*/
static int
cmdline_pum_create(
cmdline_info_T *ccline,
expand_T *xp,
char_u **matches,
int numMatches,
int showtail)
{
int i;
int columns;
// Add all the completion matches
compl_match_arraysize = numMatches;
compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
for (i = 0; i < numMatches; i++)
{
compl_match_array[i].pum_text = SHOW_MATCH(i);
compl_match_array[i].pum_info = NULL;
compl_match_array[i].pum_extra = NULL;
compl_match_array[i].pum_kind = NULL;
}
// Compute the popup menu starting column
compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
columns = vim_strsize(xp->xp_pattern);
if (showtail)
{
columns += vim_strsize(showmatches_gettail(matches[0]));
columns -= vim_strsize(matches[0]);
}
if (columns >= compl_startcol)
compl_startcol = 0;
else
compl_startcol -= columns;
// no default selection
compl_selected = -1;
cmdline_pum_display();
return EXPAND_OK;
}
/*
* Display the cmdline completion matches in a popup menu
*/
void
cmdline_pum_display(void)
{
pum_display(compl_match_array, compl_match_arraysize, compl_selected);
}
/*
* Returns TRUE if the cmdline completion popup menu is being displayed.
*/
int
cmdline_pum_active(void)
{
return pum_visible() && compl_match_array != NULL;
}
/*
* Remove the cmdline completion popup menu (if present), free the list of
* items and refresh the screen.
*/
void
cmdline_pum_remove(void)
{
int save_p_lz = p_lz;
int save_KeyTyped = KeyTyped;
pum_undisplay();
VIM_CLEAR(compl_match_array);
p_lz = FALSE; // avoid the popup menu hanging around
update_screen(0);
p_lz = save_p_lz;
redrawcmd();
// When a function is called (e.g. for 'foldtext') KeyTyped might be reset
// as a side effect.
KeyTyped = save_KeyTyped;
}
void
cmdline_pum_cleanup(cmdline_info_T *cclp)
{
cmdline_pum_remove();
wildmenu_cleanup(cclp);
}
/*
* Returns the starting column number to use for the cmdline completion popup
* menu.
*/
int
cmdline_compl_startcol(void)
{
return compl_startcol;
}
/*
* Return the number of characters that should be skipped in a status match.
* These are backslashes used for escaping. Do show backslashes in help tags.
*/
static int
skip_status_match_char(expand_T *xp, char_u *s)
{
if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
#ifdef FEAT_MENU
|| ((xp->xp_context == EXPAND_MENUS
|| xp->xp_context == EXPAND_MENUNAMES)
&& (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
#endif
)
{
#ifndef BACKSLASH_IN_FILENAME
if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
return 2;
#endif
return 1;
}
return 0;
}
/*
* Get the length of an item as it will be shown in the status line.
*/
static int
status_match_len(expand_T *xp, char_u *s)
{
int len = 0;
#ifdef FEAT_MENU
int emenu = xp->xp_context == EXPAND_MENUS
|| xp->xp_context == EXPAND_MENUNAMES;
// Check for menu separators - replace with '|'.
if (emenu && menu_is_separator(s))
return 1;
#endif
while (*s != NUL)
{
s += skip_status_match_char(xp, s);
len += ptr2cells(s);
MB_PTR_ADV(s);
}
return len;
}
/*
* Show wildchar matches in the status line.
* Show at least the "match" item.
* We start at item 'first_match' in the list and show all matches that fit.
*
* If inversion is possible we use it. Else '=' characters are used.
*/
static void
win_redr_status_matches(
expand_T *xp,
int num_matches,
char_u **matches, // list of matches
int match,
int showtail)
{
int row;
char_u *buf;
int len;
int clen; // length in screen cells
int fillchar;
int attr;
int i;
int highlight = TRUE;
char_u *selstart = NULL;
int selstart_col = 0;
char_u *selend = NULL;
static int first_match = 0;
int add_left = FALSE;
char_u *s;
#ifdef FEAT_MENU
int emenu;
#endif
int l;
if (matches == NULL) // interrupted completion?
return;
if (has_mbyte)
buf = alloc(Columns * MB_MAXBYTES + 1);
else
buf = alloc(Columns + 1);
if (buf == NULL)
return;
if (match == -1) // don't show match but original text
{
match = 0;
highlight = FALSE;
}
// count 1 for the ending ">"
clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
if (match == 0)
first_match = 0;
else if (match < first_match)
{
// jumping left, as far as we can go
first_match = match;
add_left = TRUE;
}
else
{
// check if match fits on the screen
for (i = first_match; i < match; ++i)
clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
if (first_match > 0)
clen += 2;
// jumping right, put match at the left
if ((long)clen > Columns)
{
first_match = match;
// if showing the last match, we can add some on the left
clen = 2;
for (i = match; i < num_matches; ++i)
{
clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
if ((long)clen >= Columns)
break;
}
if (i == num_matches)
add_left = TRUE;
}
}
if (add_left)
while (first_match > 0)
{
clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
if ((long)clen >= Columns)
break;
--first_match;
}
fillchar = fillchar_status(&attr, curwin);
if (first_match == 0)
{
*buf = NUL;
len = 0;
}
else
{
STRCPY(buf, "< ");
len = 2;
}
clen = len;
i = first_match;
while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
{
if (i == match)
{
selstart = buf + len;
selstart_col = clen;
}
s = SHOW_MATCH(i);
// Check for menu separators - replace with '|'
#ifdef FEAT_MENU
emenu = (xp->xp_context == EXPAND_MENUS
|| xp->xp_context == EXPAND_MENUNAMES);
if (emenu && menu_is_separator(s))
{
STRCPY(buf + len, transchar('|'));
l = (int)STRLEN(buf + len);
len += l;
clen += l;
}
else
#endif
for ( ; *s != NUL; ++s)
{
s += skip_status_match_char(xp, s);
clen += ptr2cells(s);
if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
{
STRNCPY(buf + len, s, l);
s += l - 1;
len += l;
}
else
{
STRCPY(buf + len, transchar_byte(*s));
len += (int)STRLEN(buf + len);
}
}
if (i == match)
selend = buf + len;
*(buf + len++) = ' ';
*(buf + len++) = ' ';
clen += 2;
if (++i == num_matches)
break;
}
if (i != num_matches)
{
*(buf + len++) = '>';
++clen;
}
buf[len] = NUL;
row = cmdline_row - 1;
if (row >= 0)
{
if (wild_menu_showing == 0)
{
if (msg_scrolled > 0)
{
// Put the wildmenu just above the command line. If there is
// no room, scroll the screen one line up.
if (cmdline_row == Rows - 1)
{
screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
++msg_scrolled;
}
else
{
++cmdline_row;
++row;
}
wild_menu_showing = WM_SCROLLED;
}
else
{
// Create status line if needed by setting 'laststatus' to 2.
// Set 'winminheight' to zero to avoid that the window is
// resized.
if (lastwin->w_status_height == 0)
{
save_p_ls = p_ls;
save_p_wmh = p_wmh;
p_ls = 2;
p_wmh = 0;
last_status(FALSE);
}
wild_menu_showing = WM_SHOWN;
}
}
screen_puts(buf, row, 0, attr);
if (selstart != NULL && highlight)
{
*selend = NUL;
screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
}
screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
}
win_redraw_last_status(topframe);
vim_free(buf);
}
/*
* Get the next or prev cmdline completion match. The index of the match is set
* in "p_findex"
*/
static char_u *
get_next_or_prev_match(
int mode,
expand_T *xp,
int *p_findex,
char_u *orig_save)
{
int findex = *p_findex;
int ht;
if (xp->xp_numfiles <= 0)
return NULL;
if (mode == WILD_PREV)
{
if (findex == -1)
findex = xp->xp_numfiles;
--findex;
}
else if (mode == WILD_NEXT)
++findex;
else if (mode == WILD_PAGEUP)
{
if (findex == 0)
// at the first entry, don't select any entries
findex = -1;
else if (findex == -1)
// no entry is selected. select the last entry
findex = xp->xp_numfiles - 1;
else
{
// go up by the pum height
ht = pum_get_height();
if (ht > 3)
ht -= 2;
findex -= ht;
if (findex < 0)
// few entries left, select the first entry
findex = 0;
}
}
else // mode == WILD_PAGEDOWN
{
if (findex == xp->xp_numfiles - 1)
// at the last entry, don't select any entries
findex = -1;
else if (findex == -1)
// no entry is selected. select the first entry
findex = 0;
else
{
// go down by the pum height
ht = pum_get_height();
if (ht > 3)
ht -= 2;
findex += ht;
if (findex >= xp->xp_numfiles)
// few entries left, select the last entry
findex = xp->xp_numfiles - 1;
}
}
// When wrapping around, return the original string, set findex to -1.
if (findex < 0)
{
if (orig_save == NULL)
findex = xp->xp_numfiles - 1;
else
findex = -1;
}
if (findex >= xp->xp_numfiles)
{
if (orig_save == NULL)
findex = 0;
else
findex = -1;
}
if (compl_match_array)
{
compl_selected = findex;
cmdline_pum_display();
}
else if (p_wmnu)
win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
findex, cmd_showtail);
*p_findex = findex;
if (findex == -1)
return vim_strsave(orig_save);
return vim_strsave(xp->xp_files[findex]);
}
/*
* Start the command-line expansion and get the matches.
*/
static char_u *
ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
{
int non_suf_match; // number without matching suffix
int i;
char_u *ss = NULL;
// Do the expansion.
if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
options) == FAIL)
{
#ifdef FNAME_ILLEGAL
// Illegal file name has been silently skipped. But when there
// are wildcards, the real problem is that there was no match,
// causing the pattern to be added, which has illegal characters.
if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
semsg(_(e_no_match_str_2), str);
#endif
}
else if (xp->xp_numfiles == 0)
{
if (!(options & WILD_SILENT))
semsg(_(e_no_match_str_2), str);
}
else
{
// Escape the matches for use on the command line.
ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
// Check for matching suffixes in file names.
if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
{
if (xp->xp_numfiles)
non_suf_match = xp->xp_numfiles;
else
non_suf_match = 1;
if ((xp->xp_context == EXPAND_FILES
|| xp->xp_context == EXPAND_DIRECTORIES)
&& xp->xp_numfiles > 1)
{
// More than one match; check suffix.
// The files will have been sorted on matching suffix in
// expand_wildcards, only need to check the first two.
non_suf_match = 0;
for (i = 0; i < 2; ++i)
if (match_suffix(xp->xp_files[i]))
++non_suf_match;
}
if (non_suf_match != 1)
{
// Can we ever get here unless it's while expanding
// interactively? If not, we can get rid of this all
// together. Don't really want to wait for this message
// (and possibly have to hit return to continue!).
if (!(options & WILD_SILENT))
emsg(_(e_too_many_file_names));
else if (!(options & WILD_NO_BEEP))
beep_flush();
}
if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
ss = vim_strsave(xp->xp_files[0]);
}
}
return ss;
}
/*
* Return the longest common part in the list of cmdline completion matches.
*/
static char_u *
find_longest_match(expand_T *xp, int options)
{
long_u len;
int mb_len = 1;
int c0, ci;
int i;
char_u *ss;
for (len = 0; xp->xp_files[0][len]; len += mb_len)
{
if (has_mbyte)
{
mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
}
else
c0 = xp->xp_files[0][len];
for (i = 1; i < xp->xp_numfiles; ++i)
{
if (has_mbyte)
ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
else
ci = xp->xp_files[i][len];
if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
|| xp->xp_context == EXPAND_FILES
|| xp->xp_context == EXPAND_SHELLCMD
|| xp->xp_context == EXPAND_BUFFERS))
{
if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
break;
}
else if (c0 != ci)
break;
}
if (i < xp->xp_numfiles)
{
if (!(options & WILD_NO_BEEP))
vim_beep(BO_WILD);
break;
}
}
ss = alloc(len + 1);
if (ss)
vim_strncpy(ss, xp->xp_files[0], (size_t)len);
return ss;
}
/*
* Do wildcard expansion on the string "str".
* Chars that should not be expanded must be preceded with a backslash.
* Return a pointer to allocated memory containing the new string.
* Return NULL for failure.
*
* "orig" is the originally expanded string, copied to allocated memory. It
* should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
* WILD_PREV "orig" should be NULL.
*
* Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
* is WILD_EXPAND_FREE or WILD_ALL.
*
* mode = WILD_FREE: just free previously expanded matches
* mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
* mode = WILD_EXPAND_KEEP: normal expansion, keep matches
* mode = WILD_NEXT: use next match in multiple match, wrap to first
* mode = WILD_PREV: use previous match in multiple match, wrap to first
* mode = WILD_ALL: return all matches concatenated
* mode = WILD_LONGEST: return longest matched part
* mode = WILD_ALL_KEEP: get all matches, keep matches
* mode = WILD_APPLY: apply the item selected in the cmdline completion
* popup menu and close the menu.
* mode = WILD_CANCEL: cancel and close the cmdline completion popup and
* use the original text.
*
* options = WILD_LIST_NOTFOUND: list entries without a match
* options = WILD_HOME_REPLACE: do home_replace() for buffer names
* options = WILD_USE_NL: Use '\n' for WILD_ALL
* options = WILD_NO_BEEP: Don't beep for multiple matches
* options = WILD_ADD_SLASH: add a slash after directory names
* options = WILD_KEEP_ALL: don't remove 'wildignore' entries
* options = WILD_SILENT: don't print warning messages
* options = WILD_ESCAPE: put backslash before special chars
* options = WILD_ICASE: ignore case for files
* options = WILD_ALLLINKS; keep broken links
*
* The variables xp->xp_context and xp->xp_backslash must have been set!
*/
char_u *
ExpandOne(
expand_T *xp,
char_u *str,
char_u *orig, // allocated copy of original of expanded string
int options,
int mode)
{
char_u *ss = NULL;
static int findex;
static char_u *orig_save = NULL; // kept value of orig
int orig_saved = FALSE;
int i;
long_u len;
// first handle the case of using an old match
if (mode == WILD_NEXT || mode == WILD_PREV
|| mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
return get_next_or_prev_match(mode, xp, &findex, orig_save);
if (mode == WILD_CANCEL)
ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
else if (mode == WILD_APPLY)
ss = vim_strsave(findex == -1 ? (orig_save ?
orig_save : (char_u *)"") : xp->xp_files[findex]);
// free old names
if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
{
FreeWild(xp->xp_numfiles, xp->xp_files);
xp->xp_numfiles = -1;
VIM_CLEAR(orig_save);
// The entries from xp_files may be used in the PUM, remove it.
if (compl_match_array != NULL)
cmdline_pum_remove();
}
findex = 0;
if (mode == WILD_FREE) // only release file name
return NULL;
if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
{
vim_free(orig_save);
orig_save = orig;
orig_saved = TRUE;
ss = ExpandOne_start(mode, xp, str, options);
}