-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdexpand.c
2976 lines (2739 loc) · 72.9 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 void set_expand_context(expand_T *xp);
static int ExpandGeneric(expand_T *xp, regmatch_T *regmatch,
int *num_file, char_u ***file,
char_u *((*func)(expand_T *, int)), int escaped);
static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
static int expand_showtail(expand_T *xp);
static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
#if defined(FEAT_EVAL)
static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
#endif
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);
}
static void
ExpandEscape(
expand_T *xp,
char_u *str,
int numfiles,
char_u **files,
int options)
{
int i;
char_u *p;
int vse_what = xp->xp_context == EXPAND_BUFFERS
? VSE_BUFFER : VSE_NONE;
// May change home directory back to "~"
if (options & WILD_HOME_REPLACE)
tilde_replace(str, numfiles, files);
if (options & WILD_ESCAPE)
{
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 (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 (i = 0; i < numfiles; ++i)
{
p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
if (p != NULL)
{
vim_free(files[i]);
files[i] = p;
}
}
}
}
}
/*
* 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;
}
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)
{
// Get next/previous match for a previous expanded pattern.
p2 = ExpandOne(xp, NULL, NULL, 0, type);
}
else
{
// Translate string into pattern and expand it.
if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
xp->xp_context)) == 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;
}
/*
* 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
*
* 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;
int non_suf_match; // number without matching suffix
// first handle the case of using an old match
if (mode == WILD_NEXT || mode == WILD_PREV)
{
if (xp->xp_numfiles > 0)
{
if (mode == WILD_PREV)
{
if (findex == -1)
findex = xp->xp_numfiles;
--findex;
}
else // mode == WILD_NEXT
++findex;
// 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;
}
#ifdef FEAT_WILDMENU
if (p_wmnu)
win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
findex, cmd_showtail);
#endif
if (findex == -1)
return vim_strsave(orig_save);
return vim_strsave(xp->xp_files[findex]);
}
else
return NULL;
}
// 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);
}
findex = 0;
if (mode == WILD_FREE) // only release file name
return NULL;
if (xp->xp_numfiles == -1)
{
vim_free(orig_save);
orig_save = orig;
orig_saved = TRUE;
// Do the expansion.
if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
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_nomatch2), str);
#endif
}
else if (xp->xp_numfiles == 0)
{
if (!(options & WILD_SILENT))
semsg(_(e_nomatch2), 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_toomany));
else if (!(options & WILD_NO_BEEP))
beep_flush();
}
if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
ss = vim_strsave(xp->xp_files[0]);
}
}
}
// Find longest common part
if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
{
int mb_len = 1;
int c0, ci;
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);
findex = -1; // next p_wc gets first one
}
// Concatenate all matching names
if (mode == WILD_ALL && xp->xp_numfiles > 0)
{
len = 0;
for (i = 0; i < xp->xp_numfiles; ++i)
len += (long_u)STRLEN(xp->xp_files[i]) + 1;
ss = alloc(len);
if (ss != NULL)
{
*ss = NUL;
for (i = 0; i < xp->xp_numfiles; ++i)
{
STRCAT(ss, xp->xp_files[i]);
if (i != xp->xp_numfiles - 1)
STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
}
}
}
if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
ExpandCleanup(xp);
// Free "orig" if it wasn't stored in "orig_save".
if (!orig_saved)
vim_free(orig);
return ss;
}
/*
* Prepare an expand structure for use.
*/
void
ExpandInit(expand_T *xp)
{
CLEAR_POINTER(xp);
xp->xp_backslash = XP_BS_NONE;
xp->xp_numfiles = -1;
}
/*
* Cleanup an expand structure after use.
*/
void
ExpandCleanup(expand_T *xp)
{
if (xp->xp_numfiles >= 0)
{
FreeWild(xp->xp_numfiles, xp->xp_files);
xp->xp_numfiles = -1;
}
}
/*
* Show all matches for completion on the command line.
* Returns EXPAND_NOTHING when the character that triggered expansion should
* be inserted like a normal character.
*/
int
showmatches(expand_T *xp, int wildmenu UNUSED)
{
cmdline_info_T *ccline = get_cmdline_info();
#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
int num_files;
char_u **files_found;
int i, j, k;
int maxlen;
int lines;
int columns;
char_u *p;
int lastlen;
int attr;
int showtail;
if (xp->xp_numfiles == -1)
{
set_expand_context(xp);
i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
&num_files, &files_found);
showtail = expand_showtail(xp);
if (i != EXPAND_OK)
return i;
}
else
{
num_files = xp->xp_numfiles;
files_found = xp->xp_files;
showtail = cmd_showtail;
}
#ifdef FEAT_WILDMENU
if (!wildmenu)
{
#endif
msg_didany = FALSE; // lines_left will be set
msg_start(); // prepare for paging
msg_putchar('\n');
out_flush();
cmdline_row = msg_row;
msg_didany = FALSE; // lines_left will be set again
msg_start(); // prepare for paging
#ifdef FEAT_WILDMENU
}
#endif
if (got_int)
got_int = FALSE; // only int. the completion, not the cmd line
#ifdef FEAT_WILDMENU
else if (wildmenu)
win_redr_status_matches(xp, num_files, files_found, -1, showtail);
#endif
else
{
// find the length of the longest file name
maxlen = 0;
for (i = 0; i < num_files; ++i)
{
if (!showtail && (xp->xp_context == EXPAND_FILES
|| xp->xp_context == EXPAND_SHELLCMD
|| xp->xp_context == EXPAND_BUFFERS))
{
home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
j = vim_strsize(NameBuff);
}
else
j = vim_strsize(L_SHOWFILE(i));
if (j > maxlen)
maxlen = j;
}
if (xp->xp_context == EXPAND_TAGS_LISTFILES)
lines = num_files;
else
{
// compute the number of columns and lines for the listing
maxlen += 2; // two spaces between file names
columns = ((int)Columns + 2) / maxlen;
if (columns < 1)
columns = 1;
lines = (num_files + columns - 1) / columns;
}
attr = HL_ATTR(HLF_D); // find out highlighting for directories
if (xp->xp_context == EXPAND_TAGS_LISTFILES)
{
msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
msg_clr_eos();
msg_advance(maxlen - 3);
msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
}
// list the files line by line
for (i = 0; i < lines; ++i)
{
lastlen = 999;
for (k = i; k < num_files; k += lines)
{
if (xp->xp_context == EXPAND_TAGS_LISTFILES)
{
msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
p = files_found[k] + STRLEN(files_found[k]) + 1;
msg_advance(maxlen + 1);
msg_puts((char *)p);
msg_advance(maxlen + 3);
msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
break;
}
for (j = maxlen - lastlen; --j >= 0; )
msg_putchar(' ');
if (xp->xp_context == EXPAND_FILES
|| xp->xp_context == EXPAND_SHELLCMD
|| xp->xp_context == EXPAND_BUFFERS)
{
// highlight directories
if (xp->xp_numfiles != -1)
{
char_u *halved_slash;
char_u *exp_path;
char_u *path;
// Expansion was done before and special characters
// were escaped, need to halve backslashes. Also
// $HOME has been replaced with ~/.
exp_path = expand_env_save_opt(files_found[k], TRUE);
path = exp_path != NULL ? exp_path : files_found[k];
halved_slash = backslash_halve_save(path);
j = mch_isdir(halved_slash != NULL ? halved_slash
: files_found[k]);
vim_free(exp_path);
if (halved_slash != path)
vim_free(halved_slash);
}
else
// Expansion was done here, file names are literal.
j = mch_isdir(files_found[k]);
if (showtail)
p = L_SHOWFILE(k);
else
{
home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
TRUE);
p = NameBuff;
}
}
else
{
j = FALSE;
p = L_SHOWFILE(k);
}
lastlen = msg_outtrans_attr(p, j ? attr : 0);
}
if (msg_col > 0) // when not wrapped around
{
msg_clr_eos();
msg_putchar('\n');
}
out_flush(); // show one line at a time
if (got_int)
{
got_int = FALSE;
break;
}
}
// we redraw the command below the lines that we have just listed
// This is a bit tricky, but it saves a lot of screen updating.
cmdline_row = msg_row; // will put it back later
}
if (xp->xp_numfiles == -1)
FreeWild(num_files, files_found);
return EXPAND_OK;
}
/*
* Private gettail for showmatches() (and win_redr_status_matches()):
* Find tail of file name path, but ignore trailing "/".
*/
char_u *
sm_gettail(char_u *s)
{
char_u *p;
char_u *t = s;
int had_sep = FALSE;
for (p = s; *p != NUL; )
{
if (vim_ispathsep(*p)
#ifdef BACKSLASH_IN_FILENAME
&& !rem_backslash(p)
#endif
)
had_sep = TRUE;
else if (had_sep)
{
t = p;
had_sep = FALSE;
}
MB_PTR_ADV(p);
}
return t;
}
/*
* Return TRUE if we only need to show the tail of completion matches.
* When not completing file names or there is a wildcard in the path FALSE is
* returned.
*/
static int
expand_showtail(expand_T *xp)
{
char_u *s;
char_u *end;
// When not completing file names a "/" may mean something different.
if (xp->xp_context != EXPAND_FILES
&& xp->xp_context != EXPAND_SHELLCMD
&& xp->xp_context != EXPAND_DIRECTORIES)
return FALSE;
end = gettail(xp->xp_pattern);
if (end == xp->xp_pattern) // there is no path separator
return FALSE;
for (s = xp->xp_pattern; s < end; s++)
{
// Skip escaped wildcards. Only when the backslash is not a path
// separator, on DOS the '*' "path\*\file" must not be skipped.
if (rem_backslash(s))
++s;
else if (vim_strchr((char_u *)"*?[", *s) != NULL)
return FALSE;
}
return TRUE;
}
/*
* Prepare a string for expansion.
* When expanding file names: The string will be used with expand_wildcards().
* Copy "fname[len]" into allocated memory and add a '*' at the end.
* When expanding other names: The string will be used with regcomp(). Copy
* the name into allocated memory and prepend "^".
*/
char_u *
addstar(
char_u *fname,
int len,
int context) // EXPAND_FILES etc.
{
char_u *retval;
int i, j;
int new_len;
char_u *tail;
int ends_in_star;
if (context != EXPAND_FILES
&& context != EXPAND_FILES_IN_PATH
&& context != EXPAND_SHELLCMD
&& context != EXPAND_DIRECTORIES)
{
// Matching will be done internally (on something other than files).
// So we convert the file-matching-type wildcards into our kind for
// use with vim_regcomp(). First work out how long it will be:
// For help tags the translation is done in find_help_tags().
// For a tag pattern starting with "/" no translation is needed.
if (context == EXPAND_HELP
|| context == EXPAND_COLORS
|| context == EXPAND_COMPILER
|| context == EXPAND_OWNSYNTAX
|| context == EXPAND_FILETYPE
|| context == EXPAND_PACKADD
|| ((context == EXPAND_TAGS_LISTFILES
|| context == EXPAND_TAGS)
&& fname[0] == '/'))
retval = vim_strnsave(fname, len);
else
{
new_len = len + 2; // +2 for '^' at start, NUL at end
for (i = 0; i < len; i++)
{
if (fname[i] == '*' || fname[i] == '~')
new_len++; // '*' needs to be replaced by ".*"
// '~' needs to be replaced by "\~"
// Buffer names are like file names. "." should be literal
if (context == EXPAND_BUFFERS && fname[i] == '.')
new_len++; // "." becomes "\."
// Custom expansion takes care of special things, match
// backslashes literally (perhaps also for other types?)
if ((context == EXPAND_USER_DEFINED
|| context == EXPAND_USER_LIST) && fname[i] == '\\')
new_len++; // '\' becomes "\\"
}
retval = alloc(new_len);
if (retval != NULL)
{
retval[0] = '^';
j = 1;
for (i = 0; i < len; i++, j++)
{
// Skip backslash. But why? At least keep it for custom
// expansion.
if (context != EXPAND_USER_DEFINED
&& context != EXPAND_USER_LIST
&& fname[i] == '\\'
&& ++i == len)
break;
switch (fname[i])
{
case '*': retval[j++] = '.';
break;
case '~': retval[j++] = '\\';
break;
case '?': retval[j] = '.';
continue;
case '.': if (context == EXPAND_BUFFERS)
retval[j++] = '\\';
break;
case '\\': if (context == EXPAND_USER_DEFINED
|| context == EXPAND_USER_LIST)
retval[j++] = '\\';
break;
}
retval[j] = fname[i];
}
retval[j] = NUL;
}
}
}
else
{
retval = alloc(len + 4);
if (retval != NULL)
{
vim_strncpy(retval, fname, len);
// Don't add a star to *, ~, ~user, $var or `cmd`.
// * would become **, which walks the whole tree.
// ~ would be at the start of the file name, but not the tail.
// $ could be anywhere in the tail.
// ` could be anywhere in the file name.
// When the name ends in '$' don't add a star, remove the '$'.
tail = gettail(retval);
ends_in_star = (len > 0 && retval[len - 1] == '*');
#ifndef BACKSLASH_IN_FILENAME
for (i = len - 2; i >= 0; --i)
{
if (retval[i] != '\\')
break;
ends_in_star = !ends_in_star;
}
#endif
if ((*retval != '~' || tail != retval)
&& !ends_in_star
&& vim_strchr(tail, '$') == NULL
&& vim_strchr(retval, '`') == NULL)
retval[len++] = '*';
else if (len > 0 && retval[len - 1] == '$')
--len;
retval[len] = NUL;
}
}
return retval;
}
/*
* Must parse the command line so far to work out what context we are in.
* Completion can then be done based on that context.
* This routine sets the variables:
* xp->xp_pattern The start of the pattern to be expanded within
* the command line (ends at the cursor).
* xp->xp_context The type of thing to expand. Will be one of:
*
* EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
* the command line, like an unknown command. Caller
* should beep.
* EXPAND_NOTHING Unrecognised context for completion, use char like
* a normal char, rather than for completion. eg
* :s/^I/
* EXPAND_COMMANDS Cursor is still touching the command, so complete
* it.
* EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
* EXPAND_FILES After command with EX_XFILE set, or after setting
* with P_EXPAND set. eg :e ^I, :w>>^I
* EXPAND_DIRECTORIES In some cases this is used instead of the latter
* when we know only directories are of interest. eg
* :set dir=^I
* EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
* EXPAND_SETTINGS Complete variable names. eg :set d^I
* EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
* EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
* EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
* EXPAND_HELP Complete tags from the file 'helpfile'/tags
* EXPAND_EVENTS Complete event names
* EXPAND_SYNTAX Complete :syntax command arguments
* EXPAND_HIGHLIGHT Complete highlight (syntax) group names
* EXPAND_AUGROUP Complete autocommand group names
* EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
* EXPAND_MAPPINGS Complete mapping and abbreviation names,
* eg :unmap a^I , :cunab x^I
* EXPAND_FUNCTIONS Complete internal or user defined function names,
* eg :call sub^I
* EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
* EXPAND_EXPRESSION Complete internal or user defined function/variable
* names in expressions, eg :while s^I
* EXPAND_ENV_VARS Complete environment variable names
* EXPAND_USER Complete user names
*/
static void
set_expand_context(expand_T *xp)
{
cmdline_info_T *ccline = get_cmdline_info();
// only expansion for ':', '>' and '=' command-lines
if (ccline->cmdfirstc != ':'
#ifdef FEAT_EVAL
&& ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
&& !ccline->input_fn
#endif
)
{
xp->xp_context = EXPAND_NOTHING;
return;
}
set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
}
/*
* This is all pretty much copied from do_one_cmd(), with all the extra stuff
* we don't need/want deleted. Maybe this could be done better if we didn't
* repeat all this stuff. The only problem is that they may not stay
* perfectly compatible with each other, but then the command line syntax
* probably won't change that much -- webb.
*/
static char_u *
set_one_cmd_context(
expand_T *xp,
char_u *buff) // buffer for command string
{
char_u *p;
char_u *cmd, *arg;
int len = 0;
exarg_T ea;
int compl = EXPAND_NOTHING;
int delim;
int forceit = FALSE;
int usefilter = FALSE; // filter instead of file name
ExpandInit(xp);
xp->xp_pattern = buff;
xp->xp_line = buff;
xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
ea.argt = 0;
// 1. skip comment lines and leading space, colons or bars
for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
;
xp->xp_pattern = cmd;
if (*cmd == NUL)
return NULL;
if (*cmd == '"') // ignore comment lines
{
xp->xp_context = EXPAND_NOTHING;
return NULL;
}
// 3. Skip over the range to find the command.
cmd = skip_range(cmd, TRUE, &xp->xp_context);
xp->xp_pattern = cmd;