/
zle_tricky.c
2987 lines (2705 loc) · 72 KB
/
zle_tricky.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
/*
* zle_tricky.c - expansion and completion
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1992-1997 Paul Falstad
* 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 Paul Falstad 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 Paul Falstad and the Zsh Development Group have been advised of
* the possibility of such damage.
*
* Paul Falstad 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 Paul Falstad and the
* Zsh Development Group have no obligation to provide maintenance,
* support, updates, enhancements, or modifications.
*
*/
#include "zle.mdh"
#include "zle_tricky.pro"
/*
* The main part of ZLE maintains the line being edited as binary data,
* but here, where we interface with the lexer and other bits of zsh, we
* need the line metafied and, if necessary, converted from wide
* characters into multibyte strings. On entry to the
* expansion/completion system, we metafy the line from zleline into
* zlemetaline, with zlell and zlecs adjusted into zlemetall zlemetacs
* to match. zlemetall and zlemetacs refer to raw character positions,
* in other words a metafied character contributes 2 to each. All
* completion and expansion is done on the metafied line. Immediately
* before returning, the line is unmetafied again, so that zleline,
* zlell and zlecs are once again valid. (zlell and zlecs might have
* changed during completion, so they can't be merely saved and
* restored.) The various indexes into the line that are used in this
* file only are not translated: they remain indexes into the metafied
* line.
*
* zlemetaline is always NULL when not in use and non-NULL when in use.
* This can be used to test if the line is metafied. It would be
* possible to use zlecs and zlell directly, updated as appropriate when
* metafying and unmetafying, instead of zlemetacs and zlemetall,
* however the current system seems clearer.
*/
#define inststr(X) inststrlen((X),1,-1)
/*
* The state of the line being edited between metafy_line()
* unmetafy_line().
*
* zlemetacs and zlemetall are defined in lex.c.
*/
/**/
mod_export char *zlemetaline;
/**/
mod_export int metalinesz;
/* The line before completion was tried. */
/**/
mod_export char *origline;
/**/
mod_export int origcs, origll;
/* Words on the command line, for use in completion */
/**/
mod_export int clwsize, clwnum, clwpos;
/**/
mod_export char **clwords;
/* offs is the cursor position within the tokenized *
* current word after removing nulargs. */
/**/
mod_export int offs;
/* These control the type of completion that will be done. They are *
* affected by the choice of ZLE command and by relevant shell options. *
* usemenu is set to 2 if we have to start automenu and 3 if we have to *
* insert a match as if for menucompletion but without really starting it. */
/**/
mod_export int usemenu, useglob;
/* != 0 if we would insert a TAB if we weren't calling a completion widget. */
/**/
mod_export int wouldinstab;
/* != 0 if we are in the middle of a menu completion. May be == 2 to force *
* menu completion even if using different widgets. */
/**/
mod_export int menucmp;
/* Lists of brace-infos before/after cursor (first and last for each). */
/**/
mod_export Brinfo brbeg, lastbrbeg, brend, lastbrend;
/**/
mod_export int nbrbeg, nbrend;
/**/
mod_export char *lastprebr, *lastpostbr;
/* !=0 if we have a valid completion list. */
/**/
mod_export int validlist;
/* Non-zero if we have to redisplay the list of matches. */
/**/
mod_export int showagain = 0;
/* This holds the word we are working on without braces removed. */
static char *origword;
/* The quoted prefix/suffix and a flag saying if we want to add the
* closing quote. */
/**/
mod_export char *qipre, *qisuf, *autoq;
/* This contains the name of the function to call if this is for a new *
* style completion. */
/**/
mod_export char *compfunc = NULL;
/* Non-zero if the last completion done was ambiguous (used to find *
* out if AUTOMENU should start). More precisely, it's nonzero after *
* successfully doing any completion, unless the completion was *
* unambiguous and did not cause the display of a completion list. *
* From the other point of view, it's nonzero iff AUTOMENU (if set) *
* should kick in on another completion. *
* *
* If both AUTOMENU and BASHAUTOLIST are set, then we get a listing *
* on the second tab, a` la bash, and then automenu kicks in when *
* lastambig == 2. */
/**/
mod_export int lastambig, bashlistfirst;
/* Arguments for and return value of completion widget. */
/**/
mod_export char **cfargs;
/**/
mod_export int cfret;
/* != 0 if recursive calls to completion are (temporarily) allowed */
/**/
mod_export int comprecursive;
/* != 0 if there are any defined completion widgets. */
/**/
int hascompwidgets;
/*
* Find out if we have to insert a tab (instead of trying to complete).
* The line is not metafied here.
*/
/**/
static int
usetab(void)
{
ZLE_STRING_T s = zleline + zlecs - 1;
if (keybuf[0] != '\t' || keybuf[1])
return 0;
for (; s >= zleline && *s != ZWC('\n'); s--)
if (*s != ZWC('\t') && *s != ZWC(' '))
return 0;
if (compfunc) {
wouldinstab = 1;
return 0;
}
return 1;
}
/**/
int
completecall(char **args)
{
cfargs = args;
cfret = 0;
compfunc = compwidget->u.comp.func;
if (compwidget->u.comp.fn(zlenoargs) && !cfret)
cfret = 1;
compfunc = NULL;
return cfret;
}
/**/
int
completeword(char **args)
{
usemenu = !!isset(MENUCOMPLETE);
useglob = isset(GLOBCOMPLETE);
wouldinstab = 0;
if (lastchar == '\t' && usetab())
return selfinsert(args);
else {
int ret;
if (lastambig == 1 && isset(BASHAUTOLIST) && !usemenu && !menucmp) {
bashlistfirst = 1;
ret = docomplete(COMP_LIST_COMPLETE);
bashlistfirst = 0;
lastambig = 2;
} else
ret = docomplete(COMP_COMPLETE);
return ret;
}
}
/**/
mod_export int
menucomplete(char **args)
{
usemenu = 1;
useglob = isset(GLOBCOMPLETE);
wouldinstab = 0;
if (lastchar == '\t' && usetab())
return selfinsert(args);
else
return docomplete(COMP_COMPLETE);
}
/**/
int
listchoices(UNUSED(char **args))
{
usemenu = !!isset(MENUCOMPLETE);
useglob = isset(GLOBCOMPLETE);
wouldinstab = 0;
return docomplete(COMP_LIST_COMPLETE);
}
/**/
int
spellword(UNUSED(char **args))
{
usemenu = useglob = 0;
wouldinstab = 0;
return docomplete(COMP_SPELL);
}
/**/
int
deletecharorlist(char **args)
{
usemenu = !!isset(MENUCOMPLETE);
useglob = isset(GLOBCOMPLETE);
wouldinstab = 0;
/* Line not yet metafied */
if (zlecs != zlell) {
fixsuffix();
invalidatelist();
return deletechar(args);
}
return docomplete(COMP_LIST_COMPLETE);
}
/**/
int
expandword(char **args)
{
usemenu = useglob = 0;
wouldinstab = 0;
if (lastchar == '\t' && usetab())
return selfinsert(args);
else
return docomplete(COMP_EXPAND);
}
/**/
int
expandorcomplete(char **args)
{
usemenu = !!isset(MENUCOMPLETE);
useglob = isset(GLOBCOMPLETE);
wouldinstab = 0;
if (lastchar == '\t' && usetab())
return selfinsert(args);
else {
int ret;
if (lastambig == 1 && isset(BASHAUTOLIST) && !usemenu && !menucmp) {
bashlistfirst = 1;
ret = docomplete(COMP_LIST_COMPLETE);
bashlistfirst = 0;
lastambig = 2;
} else
ret = docomplete(COMP_EXPAND_COMPLETE);
return ret;
}
}
/**/
int
menuexpandorcomplete(char **args)
{
usemenu = 1;
useglob = isset(GLOBCOMPLETE);
wouldinstab = 0;
if (lastchar == '\t' && usetab())
return selfinsert(args);
else
return docomplete(COMP_EXPAND_COMPLETE);
}
/**/
int
listexpand(UNUSED(char **args))
{
usemenu = !!isset(MENUCOMPLETE);
useglob = isset(GLOBCOMPLETE);
wouldinstab = 0;
return docomplete(COMP_LIST_EXPAND);
}
/**/
mod_export int
reversemenucomplete(char **args)
{
wouldinstab = 0;
if (!menucmp) {
menucomplete(args);
/*
* Drop through, since we are now on the first item instead of
* the last. We've already updated the display, so this is a
* bit inefficient, but it's simple and it works.
*/
}
runhookdef(REVERSEMENUHOOK, NULL);
return 0;
}
/**/
int
acceptandmenucomplete(char **args)
{
wouldinstab = 0;
if (!menucmp)
return 1;
runhookdef(ACCEPTCOMPHOOK, NULL);
return menucomplete(args);
}
/* These are flags saying if we are completing in the command *
* position, in a redirection, or in a parameter expansion. */
/**/
mod_export int lincmd, linredir, linarr;
/* The string for the redirection operator. */
/**/
mod_export char *rdstr;
static char rdstrbuf[20];
/* The list of redirections on the line. */
/**/
mod_export LinkList rdstrs;
/* This holds the name of the current command (used to find the right *
* compctl). */
/**/
mod_export char *cmdstr;
/* This hold the name of the variable we are working on. */
/**/
mod_export char *varname;
/*
* != 0 if we are in a subscript.
* Of course, this being the completion code, you're expected to guess
* what the different numbers actually mean, but here's a cheat:
* 1: Key of an ordinary array
* 2: Key of a hash
* 3: Ummm.... this appears to be a special case of 2. After a lot
* of uncommented code looking for groups of brackets, we suddenly
* decide to set it to 2. The only upshot seems to be that compctl
* then doesn't add a matching ']' at the end, so I think it means
* there's one there already.
*/
/**/
mod_export int insubscr;
/* Parameter pointer for completing keys of an assoc array. */
/**/
mod_export Param keypm;
/*
* instring takes one of the QT_* values defined in zsh.h.
* It's never QT_TICK, instead we use inbackt.
* TODO: can we combine the two?
*/
/**/
mod_export int instring, inbackt;
/*
* Convenience macro for calling quotestring (formerly bslashquote() (formerly
* quotename())).
* This uses the instring variable above.
*/
#define quotename(s, e) \
quotestring(s, e, instring == QT_NONE ? QT_BACKSLASH : instring)
/* Check if the given string is the name of a parameter and if this *
* parameter is one worth expanding. */
/**/
static int
checkparams(char *p)
{
int t0, n, l = strlen(p), e = 0;
struct hashnode *hn;
for (t0 = paramtab->hsize - 1, n = 0; n < 2 && t0 >= 0; t0--)
for (hn = paramtab->nodes[t0]; n < 2 && hn; hn = hn->next)
if (pfxlen(p, hn->nam) == l) {
n++;
if ((int)strlen(hn->nam) == l)
e = 1;
}
return (n == 1) ? (getsparam(p) != NULL) :
(!menucmp && e && (!hascompmod || isset(RECEXACT)));
}
/* Check if the given string has wildcards. The difficulty is that we *
* have to treat things like job specifications (%...) and parameter *
* expressions correctly. */
/**/
static int
cmphaswilds(char *str)
{
if ((*str == Inbrack || *str == Outbrack) && !str[1])
return 0;
/* If a leading % is immediately followed by ?, then don't *
* treat that ? as a wildcard. This is so you don't have *
* to escape job references such as %?foo. */
if (str[0] == '%' && str[1] ==Quest)
str += 2;
for (; *str;) {
if (*str == String || *str == Qstring) {
/* A parameter expression. */
if (*++str == Inbrace)
skipparens(Inbrace, Outbrace, &str);
else if (*str == String || *str == Qstring)
str++;
else {
/* Skip all the things a parameter expression might start *
* with (before we come to the parameter name). */
for (; *str; str++)
if (*str != '^' && *str != Hat &&
*str != '=' && *str != Equals &&
*str != '~' && *str != Tilde)
break;
if (*str == '#' || *str == Pound)
str++;
/* Star and Quest are parameter names here, not wildcards */
if (*str == Star || *str == Quest)
str++;
}
} else {
/* Not a parameter expression so we check for wildcards */
if (((*str == Pound || *str == Hat) && isset(EXTENDEDGLOB)) ||
*str == Star || *str == Bar || *str == Quest ||
!skipparens(Inbrack, Outbrack, &str) ||
!skipparens(Inang, Outang, &str) ||
(unset(IGNOREBRACES) &&
!skipparens(Inbrace, Outbrace, &str)) ||
(*str == Inpar && str[1] == ':' &&
!skipparens(Inpar, Outpar, &str)))
return 1;
if (*str)
str++;
}
}
return 0;
}
/* Check if we have to complete a parameter name. */
/**/
char *
parambeg(char *s)
{
char *p;
/* Try to find a `$'. */
for (p = s + offs; p > s && *p != String && *p != Qstring; p--);
if (*p == String || *p == Qstring) {
/* Handle $$'s */
while (p > s && (p[-1] == String || p[-1] == Qstring))
p--;
while ((p[1] == String || p[1] == Qstring) &&
(p[2] == String || p[2] == Qstring))
p += 2;
}
if ((*p == String || *p == Qstring) &&
p[1] != Inpar && p[1] != Inbrack && p[1] != '\'') {
/*
* This is really a parameter expression (not $(...) or $[...]
* or $'...').
*/
char *b = p + 1, *e = b;
int n = 0, br = 1;
if (*b == Inbrace) {
char *tb = b;
/* If this is a ${...}, see if we are before the '}'. */
if (!skipparens(Inbrace, Outbrace, &tb))
return NULL;
/* Ignore the possible (...) flags. */
b++, br++;
n = skipparens(Inpar, Outpar, &b);
}
/* Ignore the stuff before the parameter name. */
for (; *b; b++)
if (*b != '^' && *b != Hat &&
*b != '=' && *b != Equals &&
*b != '~' && *b != Tilde)
break;
if (*b == '#' || *b == Pound || *b == '+')
b++;
e = b;
if (br) {
while (*e == Dnull)
e++;
}
/* Find the end of the name. */
if (*e == Quest || *e == Star || *e == String || *e == Qstring ||
*e == '?' || *e == '*' || *e == '$' ||
*e == '-' || *e == '!' || *e == '@')
e++;
else if (idigit(*e))
while (idigit(*e))
e++;
else
e = itype_end(e, IIDENT, 0);
/* Now make sure that the cursor is inside the name. */
if (offs <= e - s && offs >= b - s && n <= 0) {
if (br) {
p = e;
while (*p == Dnull)
p++;
}
/* It is. */
return b;
}
}
return NULL;
}
/* The main entry point for completion. */
/**/
static int
docomplete(int lst)
{
static int active = 0;
char *s, *ol;
int olst = lst, chl = 0, ne = noerrs, ocs, ret = 0, dat[2];
if (active && !comprecursive) {
zwarn("completion cannot be used recursively (yet)");
return 1;
}
active = 1;
comprecursive = 0;
makecommaspecial(0);
if (undoing)
setlastline();
/* From the C-code's point of view, we can only use compctl as a default
* type of completion. Load it if it hasn't been loaded already and
* no completion widgets are defined. */
if (!module_loaded("zsh/compctl") && !hascompwidgets)
(void)load_module("zsh/compctl", NULL, 0);
if (runhookdef(BEFORECOMPLETEHOOK, (void *) &lst)) {
active = 0;
return 0;
}
/* Expand history references before starting completion. If anything *
* changed, do no more. */
if (doexpandhist()) {
active = 0;
return 0;
}
metafy_line();
ocs = zlemetacs;
origline = dupstring(zlemetaline);
origcs = zlemetacs;
origll = zlemetall;
if (!isfirstln && (chline != NULL || zle_chline != NULL)) {
ol = dupstring(zlemetaline);
/*
* Make sure that chline is zero-terminated.
* zle_chline always is and hptr doesn't point into it anyway.
*/
if (!zle_chline)
*hptr = '\0';
zlemetacs = 0;
inststr(zle_chline ? zle_chline : chline);
chl = zlemetacs;
zlemetacs += ocs;
} else
ol = NULL;
inwhat = IN_NOTHING;
zsfree(qipre);
qipre = ztrdup("");
zsfree(qisuf);
qisuf = ztrdup("");
zsfree(autoq);
autoq = NULL;
/* Get the word to complete.
* NOTE: get_comp_string() calls pushheap(), but not popheap(). */
noerrs = 1;
s = get_comp_string();
DPUTS(wb < 0 || zlemetacs < wb || zlemetacs > we,
"BUG: 0 <= wb <= zlemetacs <= we is not true!");
noerrs = ne;
/* For vi mode, reset the start-of-insertion pointer to the beginning *
* of the word being completed, if it is currently later. Vi itself *
* would never change the pointer in the middle of an insertion, but *
* then vi doesn't have completion. More to the point, this is only *
* an emulation. */
if (viinsbegin > ztrsub(zlemetaline + wb, zlemetaline))
viinsbegin = ztrsub(zlemetaline + wb, zlemetaline);
/* If we added chline to the line buffer, reset the original contents. */
if (ol) {
zlemetacs -= chl;
wb -= chl;
we -= chl;
if (wb < 0) {
strcpy(zlemetaline, ol);
zlemetall = strlen(zlemetaline);
zlemetacs = ocs;
popheap();
unmetafy_line();
zsfree(s);
active = 0;
makecommaspecial(0);
return 1;
}
ocs = zlemetacs;
zlemetacs = 0;
foredel(chl, CUT_RAW);
zlemetacs = ocs;
}
freeheap();
/* Save the lexer state, in case the completion code uses the lexer *
* somewhere (e.g. when processing a compctl -s flag). */
lexsave();
if (inwhat == IN_ENV)
lincmd = 0;
if (s) {
if (lst == COMP_EXPAND_COMPLETE) {
/* Check if we have to do expansion or completion. */
char *q = s;
if (*q == Equals) {
/* The word starts with `=', see if we can expand it. */
q = s + 1;
if (cmdnamtab->getnode(cmdnamtab, q) || hashcmd(q, pathchecked)) {
if (!hascompmod || isset(RECEXACT))
lst = COMP_EXPAND;
else {
int t0, n = 0;
struct hashnode *hn;
for (t0 = cmdnamtab->hsize - 1; t0 >= 0; t0--)
for (hn = cmdnamtab->nodes[t0]; hn;
hn = hn->next) {
if (strpfx(q, hn->nam) && findcmd(hn->nam, 0))
n++;
if (n == 2)
break;
}
if (n == 1)
lst = COMP_EXPAND;
}
}
}
if (lst == COMP_EXPAND_COMPLETE)
do {
/* Check if there is a parameter expression. */
for (; *q && *q != String; q++);
if (*q == String && q[1] != Inpar && q[1] != Inbrack) {
if (*++q == Inbrace) {
if (! skipparens(Inbrace, Outbrace, &q) &&
q == s + zlemetacs - wb)
lst = COMP_EXPAND;
} else {
char *t, sav, sav2;
/* Skip the things parameter expressions might *
* start with (the things before the parameter *
* name). */
for (; *q; q++)
if (*q != '^' && *q != Hat &&
*q != '=' && *q != Equals &&
*q != '~' && *q != Tilde)
break;
if ((*q == '#' || *q == Pound || *q == '+') &&
q[1] != String)
q++;
sav2 = *(t = q);
if (*q == Quest || *q == Star || *q == String ||
*q == Qstring)
*q = ztokens[*q - Pound], ++q;
else if (*q == '?' || *q == '*' || *q == '$' ||
*q == '-' || *q == '!' || *q == '@')
q++;
else if (idigit(*q))
do q++; while (idigit(*q));
else
q = itype_end(q, IIDENT, 0);
sav = *q;
*q = '\0';
if (zlemetacs - wb == q - s &&
(idigit(sav2) || checkparams(t)))
lst = COMP_EXPAND;
*q = sav;
*t = sav2;
}
if (lst != COMP_EXPAND)
lst = COMP_COMPLETE;
} else
break;
} while (q < s + zlemetacs - wb);
if (lst == COMP_EXPAND_COMPLETE) {
/* If it is still not clear if we should use expansion or *
* completion and there is a `$' or a backtick in the word, *
* than do expansion. */
for (q = s; *q; q++)
if (*q == Tick || *q == Qtick ||
*q == String || *q == Qstring)
break;
lst = *q ? COMP_EXPAND : COMP_COMPLETE;
}
/* And do expansion if there are wildcards and globcomplete is *
* not used. */
if (unset(GLOBCOMPLETE) && cmphaswilds(s))
lst = COMP_EXPAND;
}
if (lincmd && (inwhat == IN_NOTHING))
inwhat = IN_CMD;
if (lst == COMP_SPELL) {
char *w = dupstring(origword), *x, *q, *ox;
for (q = w; *q; q++)
if (inull(*q))
*q = Nularg;
zlemetacs = wb;
foredel(we - wb, CUT_RAW);
untokenize(x = ox = dupstring(w));
if (*w == Tilde || *w == Equals || *w == String)
*x = *w;
spckword(&x, 0, lincmd, 0);
ret = !strcmp(x, ox);
untokenize(x);
inststr(x);
} else if (COMP_ISEXPAND(lst)) {
/* Do expansion. */
char *ol = (olst == COMP_EXPAND ||
olst == COMP_EXPAND_COMPLETE) ?
dupstring(zlemetaline) : zlemetaline;
int ocs = zlemetacs, ne = noerrs;
noerrs = 1;
ret = doexpansion(origword, lst, olst, lincmd);
lastambig = 0;
noerrs = ne;
/* If expandorcomplete was invoked and the expansion didn't *
* change the command line, do completion. */
if (olst == COMP_EXPAND_COMPLETE &&
!strcmp(ol, zlemetaline)) {
zlemetacs = ocs;
errflag = 0;
if (!compfunc) {
char *p;
p = s;
if (*p == Tilde || *p == Equals)
p++;
for (; *p; p++)
if (itok(*p)) {
if (*p != String && *p != Qstring)
*p = ztokens[*p - Pound];
else if (p[1] == Inbrace)
p++, skipparens(Inbrace, Outbrace, &p);
}
}
ret = docompletion(s, lst, lincmd);
} else {
if (ret)
clearlist = 1;
if (!strcmp(ol, zlemetaline)) {
/* We may have removed some quotes. For completion, other
* parts of the code re-install them, but for expansion
* we have to do it here. */
zlemetacs = 0;
foredel(zlemetall, CUT_RAW);
spaceinline(origll);
memcpy(zlemetaline, origline, origll);
zlemetacs = origcs;
}
}
} else
/* Just do completion. */
ret = docompletion(s, lst, lincmd);
zsfree(s);
} else
ret = 1;
/* Reset the lexer state, pop the heap. */
lexrestore();
popheap();
dat[0] = lst;
dat[1] = ret;
runhookdef(AFTERCOMPLETEHOOK, (void *) dat);
unmetafy_line();
active = 0;
makecommaspecial(0);
return dat[1];
}
/* 1 if we are completing the prefix */
static int comppref;
/* This function inserts an `x' in the command line at the cursor position. *
* *
* Oh, you want to know why? Well, if completion is tried somewhere on an *
* empty part of the command line, the lexer code would normally not be *
* able to give us the `word' we want to complete, since there is no word. *
* But we need to call the lexer to find out where we are (and for which *
* command we are completing and such things). So we temporarily add a `x' *
* (any character without special meaning would do the job) at the cursor *
* position, than the lexer gives us the word `x' and its beginning and end *
* positions and we can remove the `x'. *
* *
* If we are just completing the prefix (comppref set), we also insert a *
* space after the x to end the word. We never need to remove the space: *
* anywhere we are able to retrieve a word for completion it will be *
* discarded as whitespace. It has the effect of making any suffix *
* referrable to as the next word on the command line when indexing *
* from a completion function. */
/**/
static void
addx(char **ptmp)
{
int addspace = 0;
if (!zlemetaline[zlemetacs] || zlemetaline[zlemetacs] == '\n' ||
(iblank(zlemetaline[zlemetacs]) &&
(!zlemetacs || zlemetaline[zlemetacs-1] != '\\')) ||
zlemetaline[zlemetacs] == ')' || zlemetaline[zlemetacs] == '`' ||
zlemetaline[zlemetacs] == '}' ||
zlemetaline[zlemetacs] == ';' || zlemetaline[zlemetacs] == '|' ||
zlemetaline[zlemetacs] == '&' ||
zlemetaline[zlemetacs] == '>' || zlemetaline[zlemetacs] == '<' ||
(instring != QT_NONE && (zlemetaline[zlemetacs] == '"' ||
zlemetaline[zlemetacs] == '\'')) ||
(addspace = (comppref && !iblank(zlemetaline[zlemetacs])))) {
*ptmp = zlemetaline;
zlemetaline = zhalloc(strlen(zlemetaline) + 3 + addspace);
memcpy(zlemetaline, *ptmp, zlemetacs);
zlemetaline[zlemetacs] = 'x';
if (addspace)
zlemetaline[zlemetacs+1] = ' ';
strcpy(zlemetaline + zlemetacs + 1 + addspace, (*ptmp) + zlemetacs);
addedx = 1 + addspace;
} else {
addedx = 0;
*ptmp = NULL;
}
}
/* Like dupstring, but add an extra space at the end of the string. */
/**/
mod_export char *
dupstrspace(const char *str)
{
int len = strlen(str);
char *t = (char *) hcalloc(len + 2);
strcpy(t, str);
strcpy(t+len, " ");
return t;
}
/*
* These functions metafy and unmetafy the ZLE buffer, as described at
* the top of this file. They *must* be called in matching pairs,
* around all the expansion/completion code.
*
* The variables zleline, zlell and zlecs are metafied into
* zlemetaline, zlemetall and zlemetacs. Only the latter variables
* should be referred to from above zle (i.e. in the main shell),
* or when using the completion API (if that's not too strong a
* way of referring to it).
*/
/**/
mod_export void
metafy_line(void)
{
UNMETACHECK();
zlemetaline = zlelineasstring(zleline, zlell, zlecs,
&zlemetall, &zlemetacs, 0);
metalinesz = zlemetall;
/*
* We will always allocate a new zleline based on zlemetaline.
*/
free(zleline);
zleline = NULL;
}
/**/
mod_export void
unmetafy_line(void)
{
METACHECK();
/* paranoia */
zlemetaline[zlemetall] = '\0';
zleline = stringaszleline(zlemetaline, zlemetacs, &zlell, &linesz, &zlecs);
free(zlemetaline);
zlemetaline = NULL;
/*
* If we inserted combining characters under the cursor we
* won't have tested the effect yet. So fix it up now.
*/
CCRIGHT();
}
/* Free a brinfo list. */
/**/
mod_export void