mirrored from git://git.code.sf.net/p/zsh/code
/
compresult.c
2351 lines (2096 loc) · 57.8 KB
/
compresult.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
/*
* compresult.c - the complete module, completion result handling
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1999 Sven Wischnowsky
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and to distribute modified versions of this software for any
* purpose, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* In no event shall Sven Wischnowsky or the Zsh Development Group be liable
* to any party for direct, indirect, special, incidental, or consequential
* damages arising out of the use of this software and its documentation,
* even if Sven Wischnowsky and the Zsh Development Group have been advised of
* the possibility of such damage.
*
* Sven Wischnowsky and the Zsh Development Group specifically disclaim any
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose. The software
* provided hereunder is on an "as is" basis, and Sven Wischnowsky and the
* Zsh Development Group have no obligation to provide maintenance,
* support, updates, enhancements, or modifications.
*
*/
#include "complete.mdh"
#include "compresult.pro"
/* This counts how often the list of completions was invalidated.
* Can be used to detect if we have a new list. */
/**/
mod_export int invcount;
#define inststr(X) inststrlen((X),1,-1)
/* This cuts the cline list before the stuff that isn't worth
* inserting in the line. */
/**/
static Cline
cut_cline(Cline l)
{
Cline q, p, e = NULL, maxp = NULL;
int sum = 0, max = 0, tmp, ls = 0, miss = 0;
/* If no match was added with matching, we don't really know
* which parts of the unambiguous string are worth keeping,
* so for now we keep everything (in the hope that this
* produces a string containing at least everything that was
* originally on the line). */
if (!hasmatched) {
cline_setlens(l, 0);
return l;
}
e = l = cp_cline(l, 0);
/* First, search the last struct for which we have something on
* the line. Anything before that is kept. */
for (q = NULL, p = l; p; p = p->next) {
if (p->orig || p->olen || !(p->flags & CLF_NEW))
e = p->next;
if (!p->suffix && (p->wlen || p->llen || p->prefix))
q = p;
}
if (!e && q && !q->orig && !q->olen && (q->flags & CLF_MISS) &&
(!(q->flags & CLF_MATCHED) || (!q->prefix && !q->suffix)) &&
(q->word ? q->wlen : q->llen) < 3) {
q->word = q->line = NULL;
q->wlen = q->llen = 0;
}
/* Then keep all structs without missing characters. */
while (e && !(e->flags & CLF_MISS))
e = e->next;
if (e) {
/* Then we see if there is another struct with missing
* characters. If not, we keep the whole list. */
for (p = e->next; p && !(p->flags & CLF_MISS); p = p->next);
if (p) {
for (p = e; p; p = p->next) {
if (!(p->flags & CLF_MISS))
sum += p->max;
else {
tmp = cline_sublen(p);
if (tmp > 2 && tmp > ((p->max + p->min) >> 1))
sum += tmp - (p->max - tmp);
else if (tmp < p->min)
sum -= (((p->max + p->min) >> 1) - tmp) << (tmp < 2);
}
if (sum > max) {
max = sum;
maxp = p;
}
}
if (max)
e = maxp;
else {
int len = 0;
cline_setlens(l, 0);
ls = 1;
for (p = e; p; p = p->next)
len += p->min;
if (len > ((minmlen << 1) / 3))
goto end;
}
e->line = e->word = NULL;
e->llen = e->wlen = e->olen = 0;
e->next = NULL;
}
}
end:
/* Sanity check. If there are no parts with missing characters but
* parts with joined substrings, remove those. */
for (p = l, e = 0, tmp = 0; p; p = p->next) {
if (p->flags & (CLF_MISS|CLF_DIFF))
miss = 1;
for (q = p->prefix; q; q = q->next)
if (q->flags & CLF_JOIN) {
e = p;
tmp = 0;
break;
}
for (q = p->suffix; q; q = q->next)
if (q->flags & CLF_JOIN) {
e = p;
tmp = 1;
break;
}
}
if (e && (!miss || cline_sublen(e) == e->min)) {
for (p = (tmp ? e->suffix : e->prefix);
p && p->next && !(p->next->flags & CLF_JOIN); p = p->next);
if (p)
p->next = NULL;
}
if (!ls)
cline_setlens(l, 0);
return l;
}
/* This builds the unambiguous string. If ins is one, it is immediately
* inserted into the line. Otherwise csp is used to return the relative
* cursor position in the string returned and posl contains all
* positions with missing or ambiguous characters. If ins is two, csp
* and posl contain real command line positions (including braces). */
/**/
static char *
cline_str(Cline l, int ins, int *csp, LinkList posl)
{
Cline s;
int ocs = zlemetacs, ncs, pcs, scs, opos = -1, npos;
int pm, pmax, pmm, pma, sm, smax, smm, sma, d, dm, mid;
int i, j, li = 0, cbr, padd = (ins ? wb - ocs : -ocs);
Brinfo brp, brs;
METACHECK();
l = cut_cline(l);
pmm = pma = smm = sma = dm = pcs = scs = 0;
pm = pmax = sm = smax = d = mid = cbr = -1;
brp = brs = NULL;
/* Get the information about the brace beginning and end we have
* to re-insert. */
if (ins) {
Brinfo bp;
int olen = we - wb;
if ((brp = brbeg)) {
for (bp = brbeg; bp; bp = bp->next) {
bp->curpos = (hasunqu ? bp->pos : bp->qpos);
olen -= strlen(bp->str);
}
}
if ((brs = lastbrend)) {
for (bp = brend; bp; bp = bp->next)
olen -= strlen(bp->str);
for (bp = brend; bp; bp = bp->next)
bp->curpos = olen - (hasunqu ? bp->pos : bp->qpos);
}
while (brp && !brp->curpos) {
inststrlen(brp->str, 1, -1);
brp = brp->next;
}
while (brs && !brs->curpos) {
if (cbr < 0)
cbr = zlemetacs;
inststrlen(brs->str, 1, -1);
brs = brs->prev;
}
}
/* Walk through the top-level cline list. */
while (l) {
/* Insert the original string if no prefix. */
if (l->olen && !(l->flags & CLF_SUF) && !l->prefix) {
pcs = zlemetacs + l->olen;
inststrlen(l->orig, 1, l->olen);
} else {
/* Otherwise insert the prefix. */
for (s = l->prefix; s; s = s->next) {
pcs = zlemetacs + s->llen;
if (s->flags & CLF_LINE)
inststrlen(s->line, 1, s->llen);
else
inststrlen(s->word, 1, s->wlen);
scs = zlemetacs;
if ((s->flags & CLF_DIFF) && (!dm || (s->flags & CLF_MATCHED))) {
d = zlemetacs; dm = s->flags & CLF_MATCHED;
if (posl && (npos = zlemetacs + padd) != opos) {
opos = npos;
addlinknode(posl, (void *) ((long) npos));
}
}
li += s->llen;
}
}
if (ins) {
int ocs, bl;
while (brp && li >= brp->curpos) {
ocs = zlemetacs;
bl = strlen(brp->str);
zlemetacs = pcs - (li - brp->curpos);
inststrlen(brp->str, 1, bl);
zlemetacs = ocs + bl;
pcs += bl;
scs += bl;
brp = brp->next;
}
}
/* Remember the position if this is the first prefix with
* missing characters. */
if ((l->flags & CLF_MISS) && !(l->flags & CLF_SUF)) {
if (posl && (npos = zlemetacs + padd) != opos) {
opos = npos;
addlinknode(posl, (void *) ((long) npos));
}
if (((pmax <= (l->max - l->min) || (pma && l->max != l->min)) &&
(!pmm || (l->flags & CLF_MATCHED))) ||
((l->flags & CLF_MATCHED) && !pmm)) {
pm = zlemetacs; pmax = l->max - l->min; pmm = l->flags & CLF_MATCHED;
pma = ((l->prefix || l->suffix) && l->min == cline_sublen(l));
}
}
if (ins) {
int ocs, bl;
while (brs && li >= brs->curpos) {
ocs = zlemetacs;
bl = strlen(brs->str);
zlemetacs = scs - (li - brs->curpos);
if (cbr < 0)
cbr = zlemetacs;
inststrlen(brs->str, 1, bl);
zlemetacs = ocs + bl;
pcs += bl;
brs = brs->prev;
}
}
pcs = zlemetacs;
/* Insert the anchor. */
if (l->flags & CLF_LINE)
inststrlen(l->line, 1, l->llen);
else
inststrlen(l->word, 1, l->wlen);
scs = zlemetacs;
if (ins) {
int ocs, bl;
li += l->llen;
while (brp && li >= brp->curpos) {
ocs = zlemetacs;
bl = strlen(brp->str);
zlemetacs = pcs + l->llen - (li - brp->curpos);
inststrlen(brp->str, 1, bl);
zlemetacs = ocs + bl;
pcs += bl;
scs += bl;
brp = brp->next;
}
}
/* Remember the cursor position for suffixes and mids. */
if (l->flags & CLF_MISS) {
if (l->flags & CLF_MID)
mid = zlemetacs;
else if (l->flags & CLF_SUF) {
if (posl && (npos = zlemetacs + padd) != opos) {
opos = npos;
addlinknode(posl, (void *) ((long) npos));
}
if (((smax <= (l->min - l->max) || (sma && l->max != l->min)) &&
(!smm || (l->flags & CLF_MATCHED))) ||
((l->flags & CLF_MATCHED) && !smm)) {
sm = zlemetacs; smax = l->min - l->max; smm = l->flags & CLF_MATCHED;
sma = ((l->prefix || l->suffix) && l->min == cline_sublen(l));
}
}
}
if (ins) {
int ocs, bl;
while (brs && li >= brs->curpos) {
ocs = zlemetacs;
bl = strlen(brs->str);
zlemetacs = scs - (li - brs->curpos);
if (cbr < 0)
cbr = zlemetacs;
inststrlen(brs->str, 1, bl);
zlemetacs = ocs + bl;
pcs += bl;
brs = brs->prev;
}
}
/* And now insert the suffix or the original string. */
if (l->olen && (l->flags & CLF_SUF) && !l->suffix) {
pcs = zlemetacs;
inststrlen(l->orig, 1, l->olen);
if (ins) {
int ocs, bl;
li += l->olen;
while (brp && li >= brp->curpos) {
ocs = zlemetacs;
bl = strlen(brp->str);
zlemetacs = pcs + l->olen - (li - brp->curpos);
inststrlen(brp->str, 1, bl);
zlemetacs = ocs + bl;
pcs += bl;
brp = brp->next;
}
while (brs && li >= brs->curpos) {
ocs = zlemetacs;
bl = strlen(brs->str);
zlemetacs = pcs + l->olen - (li - brs->curpos);
if (cbr < 0)
cbr = zlemetacs;
inststrlen(brs->str, 1, bl);
zlemetacs = ocs + bl;
pcs += bl;
brs = brs->prev;
}
}
} else {
Cline js = NULL;
for (j = -1, i = 0, s = l->suffix; s; s = s->next) {
if (j < 0 && (s->flags & CLF_DIFF))
j = i, js = s;
pcs = zlemetacs;
if (s->flags & CLF_LINE) {
inststrlen(s->line, 0, s->llen);
i += s->llen; scs = zlemetacs + s->llen;
} else {
inststrlen(s->word, 0, s->wlen);
i += s->wlen; scs = zlemetacs + s->wlen;
}
if (ins) {
int ocs, bl;
li += s->llen;
while (brp && li >= brp->curpos) {
ocs = zlemetacs;
bl = strlen(brp->str);
zlemetacs = pcs + (li - brp->curpos);
inststrlen(brp->str, 1, bl);
zlemetacs = ocs + bl;
pcs += bl;
scs += bl;
brp = brp->next;
}
while (brs && li >= brs->curpos) {
ocs = zlemetacs;
bl = strlen(brs->str);
zlemetacs = scs - (li - brs->curpos);
if (cbr < 0)
cbr = zlemetacs;
inststrlen(brs->str, 1, bl);
zlemetacs = ocs + bl;
pcs += bl;
brs = brs->prev;
}
}
}
zlemetacs += i;
if (j >= 0 && (!dm || (js->flags & CLF_MATCHED))) {
d = zlemetacs - j; dm = js->flags & CLF_MATCHED;
if (posl && (npos = zlemetacs - j + padd) != opos) {
opos = npos;
addlinknode(posl, (void *) ((long) npos));
}
}
}
l = l->next;
}
if (posl && (npos = zlemetacs + padd) != opos)
#if 0
/* This could be used to put an extra colon before the end-of-word
* position if there is nothing missing. */
addlinknode(posl, (void *) ((long) -npos));
#endif
addlinknode(posl, (void *) ((long) npos));
if (ins) {
int ocs = zlemetacs;
for (; brp; brp = brp->next)
inststrlen(brp->str, 1, -1);
for (; brs; brs = brs->prev) {
if (cbr < 0)
cbr = zlemetacs;
inststrlen(brs->str, 1, -1);
}
if (mid >= ocs)
mid += zlemetacs - ocs;
if (pm >= ocs)
pm += zlemetacs - ocs;
if (sm >= ocs)
sm += zlemetacs - ocs;
if (d >= ocs)
d += zlemetacs - ocs;
if (posl) {
LinkNode node;
long p;
for (node = firstnode(posl); node; incnode(node)) {
p = (long) getdata(node);
if (p >= ocs)
setdata(node, (void *) (p + zlemetacs - ocs));
}
}
}
/* This calculates the new cursor position. If we had a mid cline
* with missing characters, we take this, otherwise if we have a
* prefix with missing characters, we take that, the same for a
* suffix, and finally a place where the matches differ. */
ncs = (mid >= 0 ? mid :
(cbr >= 0 ? cbr :
(pm >= 0 ? pm : (sm >= 0 ? sm : (d >= 0 ? d : zlemetacs)))));
if (ins != 1) {
/* We always inserted the string in the line. If that was not
* requested, we copy it and remove from the line. */
char *r = zalloc((i = zlemetacs - ocs) + 1);
memcpy(r, zlemetaline + ocs, i);
r[i] = '\0';
zlemetacs = ocs;
foredel(i, CUT_RAW);
if (csp)
*csp = ncs - ocs;
return r;
}
lastend = zlemetacs;
zlemetacs = ncs;
return NULL;
}
/* Small utility function turning a list of positions into a colon
* separated string. */
static char *
build_pos_string(LinkList list)
{
LinkNode node;
int l;
char buf[40], *s;
long p;
for (node = firstnode(list), l = 0; node; incnode(node)) {
p = (long) getdata(node);
#if 0
/* This could be used to put an extra colon before the end-of-word
* position if there is nothing missing. */
if (p < 0)
sprintf(buf, ":%ld", -p);
else
#endif
sprintf(buf, "%ld", p);
setdata(node, dupstring(buf));
l += 1 + strlen(buf);
}
s = (char *) zalloc(l * sizeof(char));
*s = 0;
for (node = firstnode(list); node;) {
strcat(s, (char *) getdata(node));
incnode(node);
if (node)
strcat(s, ":");
}
return s;
}
/* This is a utility function using the function above to allow access
* to the unambiguous string and cursor position via compstate. */
/**/
char *
unambig_data(int *cp, char **pp, char **ip)
{
static char *scache = NULL, *pcache = NULL, *icache = NULL;
static int ccache;
if (mnum && ainfo) {
if (mnum != unambig_mnum) {
LinkList list = newlinklist();
zsfree(scache);
scache = cline_str((ainfo->count ? ainfo->line : fainfo->line),
0, &ccache, list);
zsfree(pcache);
if (empty(list))
pcache = ztrdup("");
else
pcache = build_pos_string(list);
zsfree(icache);
list = newlinklist();
zsfree(cline_str((ainfo->count ? ainfo->line : fainfo->line),
2, NULL, list));
if (empty(list))
icache = ztrdup("");
else
icache = build_pos_string(list);
}
} else if (mnum != unambig_mnum || !ainfo || !scache) {
zsfree(scache);
scache = ztrdup("");
zsfree(pcache);
pcache = ztrdup("");
zsfree(icache);
icache = ztrdup("");
ccache = 0;
}
unambig_mnum = mnum;
if (cp)
*cp = ccache + 1;
if (pp)
*pp = pcache;
if (ip)
*ip = icache;
return scache;
}
/* Insert the given match. This returns the number of bytes inserted.
* scs is used to return the position where a automatically created suffix
* has to be inserted. */
/**/
static int
instmatch(Cmatch m, int *scs)
{
int l, r = 0, ocs, a = zlemetacs, brb = 0, bradd, *brpos;
Brinfo bp;
METACHECK();
zsfree(lastprebr);
zsfree(lastpostbr);
lastprebr = lastpostbr = NULL;
/* Ignored prefix. */
if (m->ipre) {
char *p = m->ipre + (menuacc ? m->qipl : 0);
inststrlen(p, 1, (l = strlen(p)));
r += l;
}
/* -P prefix. */
if (m->pre) {
inststrlen(m->pre, 1, (l = strlen(m->pre)));
r += l;
}
/* Path prefix. */
if (m->ppre) {
inststrlen(m->ppre, 1, (l = strlen(m->ppre)));
r += l;
}
/* The string itself. */
inststrlen(m->str, 1, (l = strlen(m->str)));
r += l;
ocs = zlemetacs;
/* Re-insert the brace beginnings, if any. */
if (brbeg) {
int pcs = zlemetacs;
l = 0;
for (bp = brbeg, brpos = m->brpl,
bradd = (m->pre ? strlen(m->pre) : 0);
bp; bp = bp->next, brpos++) {
zlemetacs = a + *brpos + bradd;
pcs = zlemetacs;
l = strlen(bp->str);
bradd += l;
brpcs = zlemetacs;
inststrlen(bp->str, 1, l);
r += l;
ocs += l;
}
lastprebr = (char *) zalloc(pcs - a + 1);
memcpy(lastprebr, zlemetaline + a, pcs - a);
lastprebr[pcs - a] = '\0';
zlemetacs = ocs;
}
/* Path suffix. */
if (m->psuf) {
inststrlen(m->psuf, 1, (l = strlen(m->psuf)));
r += l;
}
/* Re-insert the brace end. */
if (brend) {
a = zlemetacs;
for (bp = brend, brpos = m->brsl, bradd = 0; bp; bp = bp->next, brpos++) {
zlemetacs = a - *brpos;
ocs = brscs = zlemetacs;
l = strlen(bp->str);
bradd += l;
inststrlen(bp->str, 1, l);
brb = zlemetacs;
r += l;
}
zlemetacs = a + bradd;
if (scs)
*scs = ocs;
} else {
brscs = -1;
if (scs)
*scs = zlemetacs;
}
/* -S suffix */
if (m->suf) {
inststrlen(m->suf, 1, (l = strlen(m->suf)));
r += l;
}
/* ignored suffix */
if (m->isuf) {
inststrlen(m->isuf, 1, (l = strlen(m->isuf)));
r += l;
}
if (brend) {
lastpostbr = (char *) zalloc(zlemetacs - brb + 1);
memcpy(lastpostbr, zlemetaline + brb, zlemetacs - brb);
lastpostbr[zlemetacs - brb] = '\0';
}
lastend = zlemetacs;
zlemetacs = ocs;
return r;
}
/* Check if the match has the given prefix/suffix before/after the
* braces. */
/**/
mod_export int
hasbrpsfx(Cmatch m, char *pre, char *suf)
{
int was_meta;
if (m->flags & CMF_ALL)
return 1;
/* May not be metafied if calculating whether to show a list. */
if (zlemetaline == NULL) {
was_meta = 0;
metafy_line();
} else
was_meta = 1;
{
char *op = lastprebr, *os = lastpostbr;
VARARR(char, oline, zlemetall);
int oll = zlemetall, newll, ole = lastend;
int opcs = brpcs, oscs = brscs, ret;
zle_save_positions();
memcpy(oline, zlemetaline, zlemetall);
lastprebr = lastpostbr = NULL;
instmatch(m, NULL);
zlemetacs = 0;
foredel(zlemetall, CUT_RAW);
spaceinline(oll);
memcpy(zlemetaline, oline, oll);
/* we do not want to restore zlemetall */
newll = zlemetall;
zle_restore_positions();
zlemetall = newll;
lastend = ole;
brpcs = opcs;
brscs = oscs;
ret = (((!pre && !lastprebr) ||
(pre && lastprebr && !strcmp(pre, lastprebr))) &&
((!suf && !lastpostbr) ||
(suf && lastpostbr && !strcmp(suf, lastpostbr))));
zsfree(lastprebr);
zsfree(lastpostbr);
lastprebr = op;
lastpostbr = os;
if (!was_meta)
unmetafy_line();
return ret;
}
}
/* Handle the case were we found more than one match. */
/**/
int
do_ambiguous(void)
{
int ret = 0;
menucmp = menuacc = 0;
/* If we have to insert the first match, call do_single(). This is *
* how REC_EXACT takes effect. We effectively turn the ambiguous *
* completion into an unambiguous one. */
if (ainfo && ainfo->exact == 1 && !(fromcomp & FC_LINE)) {
minfo.cur = NULL;
do_single(ainfo->exactm);
invalidatelist();
return ret;
}
/* Setting lastambig here means that the completion is ambiguous and *
* AUTO_MENU might want to start a menu completion next time round, *
* but this might be overridden below if we can complete an *
* unambiguous prefix. */
lastambig = 1;
if (iforcemenu != -1 &&
(usemenu || (haspattern && comppatinsert &&
!strcmp(comppatinsert, "menu")))) {
/* We are in a position to start using menu completion due to one *
* of the menu completion options, or due to the menu-complete- *
* word command, or due to using GLOB_COMPLETE which does menu- *
* style completion regardless of the setting of the normal menu *
* completion options. */
do_ambig_menu();
} else if (ainfo) {
int atend = (zlemetacs == we), la, eq, tcs;
VARARR(char, old, we - wb);
minfo.cur = NULL;
minfo.asked = 0;
fixsuffix();
/* First remove the old string from the line. */
tcs = zlemetacs;
zlemetacs = wb;
memcpy(old, zlemetaline + wb, we - wb);
foredel(we - wb, CUT_RAW);
/* Now get the unambiguous string and insert it into the line. */
cline_str(ainfo->line, 1, NULL, NULL);
/* Sometimes the different match specs used may result in a cline
* that gives an empty string. If that happened, we re-insert the
* old string. Unless there were matches added with -U, that is. */
if (lastend < we && !lenchanged && !hasunmatched) {
zlemetacs = wb;
foredel(lastend - wb, CUT_RAW);
inststrlen(old, 0, we - wb);
lastend = we;
zlemetacs = tcs;
}
if (eparq) {
tcs = zlemetacs;
zlemetacs = lastend;
for (eq = eparq; eq; eq--)
inststrlen("\"", 0, 1);
zlemetacs = tcs;
}
/* la is non-zero if listambiguous may be used. Copying and
* comparing the line looks like BFI but it is the easiest
* solution. Really. */
la = (zlemetall != origll || strncmp(origline, zlemetaline, zlemetall));
/* If REC_EXACT and AUTO_MENU are set and what we inserted is an *
* exact match, we want menu completion the next time round *
* so we set fromcomp, to ensure that the word on the line is not *
* taken as an exact match. Also we remember if we just moved the *
* cursor into the word. */
fromcomp = ((isset(AUTOMENU) ? FC_LINE : 0) |
((atend && zlemetacs != lastend) ? FC_INWORD : 0));
/* Probably move the cursor to the end. */
if (movetoend == 3)
zlemetacs = lastend;
/* If the LIST_AMBIGUOUS option (meaning roughly `show a list only *
* if the completion is completely ambiguous') is set, and some *
* prefix was inserted, return now, bypassing the list-displaying *
* code. On the way, invalidate the list and note that we don't *
* want to enter an AUTO_MENU immediately. */
if ((uselist == 3 ||
(!uselist && isset(BASHAUTOLIST) && isset(LISTAMBIGUOUS))) &&
la && iforcemenu != -1) {
int fc = fromcomp;
invalidatelist();
fromcomp = fc;
lastambig = 0;
clearlist = 1;
return ret;
}
} else
return ret;
/* At this point, we might want a completion listing. Show the listing *
* if it is needed. */
if (isset(LISTBEEP) && !oldlist)
ret = 1;
if (uselist && (usemenu != 2 || (!listshown && !oldlist)) &&
((!showinglist && (!listshown || !oldlist)) ||
(usemenu == 3 && !oldlist)) &&
(smatches >= 2 || forcelist))
showinglist = -2;
return ret;
}
/* This is a stat that ignores backslashes in the filename. The `ls' *
* parameter says if we have to do lstat() or stat(). I think this *
* should instead be done by use of a general function to expand a *
* filename (stripping backslashes), combined with the actual *
* (l)stat(). *
* Make sure input is unmetafied */
/**/
mod_export int
ztat(char *nam, struct stat *buf, int ls)
{
int ret;
nam = unmeta(nam);
if (!nam)
return -1;
if ((ret = ls ? lstat(nam, buf) : stat(nam, buf))) {
char *p, *q;
for (p = q = nam; *q; q++)
if (*q == '\\' && q[1])
*p++ = *++q;
else
*p++ = *q;
*p = '\0';
ret = ls ? lstat(nam, buf) : stat(nam, buf);
}
return ret;
}
/* Insert all matches in the command line. */
/**/
void
do_allmatches(UNUSED(int end))
{
int first = 1, nm = nmatches - 1, omc = menucmp, oma = menuacc, e;
Cmatch *mc;
struct menuinfo mi;
char *p = (brbeg ? ztrdup(lastbrbeg->str) : NULL);
memcpy(&mi, &minfo, sizeof(struct menuinfo));
menucmp = 1;
menuacc = 0;
for (minfo.group = amatches;
minfo.group && !(minfo.group)->mcount;
minfo.group = (minfo.group)->next) {
#ifdef ZSH_HEAP_DEBUG
if (memory_validate(minfo.group->heap_id)) {
HEAP_ERROR(minfo.group->heap_id);
}
#endif
}
mc = (minfo.group)->matches;
while (1) {
if (!((*mc)->flags & CMF_ALL)) {
if (!first)
accept_last();
first = 0;
if (!omc && !--nm)
menucmp = 0;
do_single(*mc);
}
minfo.cur = mc;
if (!*++(minfo.cur)) {
do {
if (!(minfo.group = (minfo.group)->next))
break;
} while (!(minfo.group)->mcount);
if (!minfo.group)
break;
minfo.cur = minfo.group->matches;
}
mc = minfo.cur;
}
menucmp = omc;
menuacc = oma;
e = minfo.end;
memcpy(&minfo, &mi, sizeof(struct menuinfo));
minfo.end = e;
minfo.len = e - minfo.pos;
if (p) {
zsfree(lastbrbeg->str);
lastbrbeg->str = p;
}
}
/* Insert a single match in the command line. */
/**/
mod_export void
do_single(Cmatch m)
{
int l, sr = 0, scs;
int havesuff = 0;
int partest = (m->ripre || ((m->flags & CMF_ISPAR) && parpre));
char *str = m->orig, *ppre = m->ppre, *psuf = m->psuf, *prpre = m->prpre;
if (!prpre) prpre = "";
if (!ppre) ppre = "";
if (!psuf) psuf = "";
fixsuffix();
if (!minfo.cur) {
/* We are currently not in a menu-completion, *
* so set the position variables. */
minfo.pos = wb;
minfo.we = (movetoend >= 2 || (movetoend == 1 && !menucmp) ||
(!movetoend && zlemetacs == we));
minfo.end = we;
}
/* If we are already in a menu-completion or if we have done a *
* glob completion, we have to delete some of the stuff on the *
* command line. */
if (minfo.cur)
l = minfo.len + minfo.insc;
else
l = we - wb;
minfo.insc = 0;
zlemetacs = minfo.pos;
foredel(l, CUT_RAW);
if (m->flags & CMF_ALL) {
do_allmatches(0);
return;
}
/* And then we insert the new string. */