/
complist.c
3509 lines (3170 loc) · 82.7 KB
/
complist.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
/*
* complist.c - completion listing enhancements
*
* 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 "complist.mdh"
#include "complist.pro"
/* Information about the list shown. */
/*
* noselect: 1 if complistmatches indicated we shouldn't do selection.
* Tested in domenuselect.
* mselect: Local copy of the index of the currently selected match.
* Initialised to the gnum entry of the current match for
* each completion.
* inselect: 1 if we already selecting matches; tested in complistmatches()
* mcol: The column for the selected completion. As we never scroll
* horizontally this applies both the screen and the logical array.
* mline: The line for the selected completion in the logical array of
* all matches, not all of which may be on screen at once.
* mcols: Local copy of columns used in sizing arrays.
* mlines: The number of lines in the logical array of all matches,
* initialised from listdat.nlines.
*/
static int noselect, mselect, inselect, mcol, mline, mcols, mlines;
/*
* selected: Used to signal between domenucomplete() and menuselect()
* that a selected entry has been found. Or something.
* mlbeg: The first line of the logical array of all matches that
* fits on screen. Setting this to -1 forces a redraw.
* mlend: The line after the last that fits on screen.
* mscroll: 1 if the scrolling prompt is shown on screen.
* mrestlines: The number of screen lines remaining to be processed.
*/
static int selected, mlbeg = -1, mlend = 9999999, mscroll, mrestlines;
/*
* mnew: 1 if a new selection menu is being displayed.
* mlastcols: stored value of mcols for use in calculating mnew.
* mlastlines: stored value of mlines for use in calculating mnew.
* mhasstat: Indicates if the status line is present (but maybe not
* yet printed).
* mfirstl: The first line of the logical array of all matches to
* be shown on screen, -1 if this has not yet been determined.
* mlastm: The index of the selected match in some circumstances; used
* if an explicit number for a match is passed to compprintfmt();
* initialised from the total number of matches. I realise this
* isn't very illuminating.
*/
static int mnew, mlastcols, mlastlines, mhasstat, mfirstl, mlastm;
/*
* mlprinted: Used to signal the number of additional lines printed
* when outputting matches (as argument passing is a bit
* screwy within the completion system).
* molbeg: The last value of mlbeg; -1 if invalid, -42 if, er, very
* invalid. Used in calculations of how much to draw.
* mocol: The last value of mcol.
* moline: The last value of mline.
* mstatprinted: Indicates that the status line has now been printed,
* c.f. mhasstat.
*/
static int mlprinted, molbeg = -2, mocol = 0, moline = 0, mstatprinted;
/*
* mstatus: The message printed when scrolling.
* mlistp: The message printed when merely listing.
*/
static char *mstatus, *mlistp;
/*
* mtab is the logical array of all matches referred to above. It
* contains mcols*mlines entries. These entries contain a pointer to
* the match structure which is in use at a particular point. Note
* that for multiple line entries lines after the first contain NULL.
*
* mmtabp is a pointer to the selected entry in mtab.
*/
static Cmatch **mtab, **mmtabp;
/*
* Used to indicate that the list has changed and needs redisplaying.
*/
static int mtab_been_reallocated;
/*
* Array and pointer for the match group in exactly the same layout
* as mtab and mmtabp.
*/
static Cmgroup *mgtab, *mgtabp;
#ifdef DEBUG
/*
* Allow us to keep track of pointer arithmetic for mgtab; could
* just as well have been for mtab but wasn't.
*/
int mgtabsize;
#endif
/*
* Used in mtab/mgtab, for explanations.
*
* UUUUUUUUUUUUUUURRRRGHHHHHHHHHH!!!!!!!!! --- pws
*/
#define MMARK ((unsigned long) 1)
#define mmarked(v) (((unsigned long) (v)) & MMARK)
#define mtmark(v) ((Cmatch *) (((unsigned long) (v)) | MMARK))
#define mtunmark(v) ((Cmatch *) (((unsigned long) (v)) & ~MMARK))
#define mgmark(v) ((Cmgroup) (((unsigned long) (v)) | MMARK))
#define mgunmark(v) ((Cmgroup) (((unsigned long) (v)) & ~MMARK))
/* Information for in-string colours. */
/* Maximum number of in-string colours supported. */
#define MAX_POS 11
static int nrefs;
static int begpos[MAX_POS], curisbeg;
static int endpos[MAX_POS];
static int sendpos[MAX_POS], curissend; /* sorted end positions */
static char **patcols, *curiscols[MAX_POS];
static int curiscol;
/* The last color used. */
static char *last_cap;
/* We use the parameters ZLS_COLORS and ZLS_COLOURS in the same way as
* the color ls does. It's just that we don't support the `or' file
* type. */
/*
* menu-select widget: used to test if it's already loaded.
*/
static Widget w_menuselect;
/*
* Similarly for the menuselect and listscroll keymaps.
*/
static Keymap mskeymap, lskeymap;
/* Indixes into the terminal string arrays. */
#define COL_NO 0
#define COL_FI 1
#define COL_DI 2
#define COL_LN 3
#define COL_PI 4
#define COL_SO 5
#define COL_BD 6
#define COL_CD 7
#define COL_OR 8
#define COL_MI 9
#define COL_SU 10
#define COL_SG 11
#define COL_TW 12
#define COL_OW 13
#define COL_ST 14
#define COL_EX 15
#define COL_LC 16
#define COL_RC 17
#define COL_EC 18
#define COL_TC 19
#define COL_SP 20
#define COL_MA 21
#define COL_HI 22
#define COL_DU 23
#define COL_SA 24
#define NUM_COLS 25
/* Names of the terminal strings. */
static char *colnames[] = {
"no", "fi", "di", "ln", "pi", "so", "bd", "cd", "or", "mi",
"su", "sg", "tw", "ow", "st", "ex",
"lc", "rc", "ec", "tc", "sp", "ma", "hi", "du", "sa", NULL
};
/* Default values. */
static char *defcols[] = {
"0", "0", "1;31", "1;36", "33", "1;35", "1;33", "1;33", NULL, NULL,
"37;41", "30;43", "30;42", "34;42", "37;44", "1;32",
"\033[", "m", NULL, "0", "0", "7", NULL, NULL, "0"
};
/* This describes a terminal string for a file type. */
typedef struct filecol *Filecol;
struct filecol {
Patprog prog; /* group pattern */
char *col; /* color string */
Filecol next; /* next one */
};
/* This describes a terminal string for a pattern. */
typedef struct patcol *Patcol;
struct patcol {
Patprog prog;
Patprog pat; /* pattern for match */
char *cols[MAX_POS + 1];
Patcol next;
};
/* This describes a terminal string for a filename extension. */
typedef struct extcol *Extcol;
struct extcol {
Patprog prog; /* group pattern or NULL */
char *ext; /* the extension */
char *col; /* the terminal color string */
Extcol next; /* the next one in the list */
};
/* This holds all terminal strings. */
typedef struct listcols *Listcols;
/* values for listcol flags */
enum {
/* ln=target: follow symlinks to determine highlighting */
LC_FOLLOW_SYMLINKS = 0x0001
};
struct listcols {
Filecol files[NUM_COLS]; /* strings for file types */
Patcol pats; /* strings for patterns */
Extcol exts; /* strings for extensions */
int flags; /* special settings, see above */
};
/*
* Contains information about the colours to be used for entries.
* Sometimes mcolors is passed as an argument even though it's
* available to all the functions.
*/
static struct listcols mcolors;
/* Combined length of LC and RC, maximum length of capability strings. */
static int lr_caplen, max_caplen;
/* This parses the value of a definition (the part after the `=').
* The return value is a pointer to the character after it. */
static char *
getcolval(char *s, int multi)
{
char *p, *o = s;
for (p = s; *s && *s != ':' && (!multi || *s != '='); p++, s++) {
if (*s == '\\' && s[1]) {
switch (*++s) {
case 'a': *p = '\007'; break;
case 'n': *p = '\n'; break;
case 'b': *p = '\b'; break;
case 't': *p = '\t'; break;
case 'v': *p = '\v'; break;
case 'f': *p = '\f'; break;
case 'r': *p = '\r'; break;
case 'e': *p = '\033'; break;
case '_': *p = ' '; break;
case '?': *p = '\177'; break;
default:
if (*s >= '0' && *s <= '7') {
int i = STOUC(*s);
if (*++s >= '0' && *s <= '7') {
i = (i * 8) + STOUC(*s);
if (*++s >= '0' && *s <= '7')
i = (i * 8) + STOUC(*s);
}
*p = (char) i;
} else
*p = *s;
}
} else if (*s == '^') {
if ((s[1] >= '@' && s[1] <= '_') ||
(s[1] >= 'a' && s[1] <= 'z'))
*p = (char) (STOUC(*s) & ~0x60);
else if (s[1] == '?')
*p = '\177';
else {
*p++ = *s;
*p = s[1];
}
s++;
} else
*p = *s;
}
if (p != s)
*p = '\0';
if ((s - o) > max_caplen)
max_caplen = s - o;
return s;
}
/* This parses one definition. Return value is a pointer to the
* character after it. */
static char *
getcoldef(char *s)
{
Patprog gprog = NULL;
if (*s == '(') {
char *p;
int l = 0;
for (p = s + 1, l = 0; *p && (*p != ')' || l); p++)
if (*p == '\\' && p[1])
p++;
else if (*p == '(')
l++;
else if (*p == ')')
l--;
if (*p == ')') {
char sav = p[1];
p[1] = '\0';
tokenize(s);
gprog = patcompile(s, 0, NULL);
p[1] =sav;
s = p + 1;
}
}
if (*s == '*') {
Extcol ec, eo;
char *n, *p;
/* This is for an extension. */
n = ++s;
while (*s && *s != '=')
s++;
if (!*s)
return s;
*s++ = '\0';
p = getcolval(s, 0);
ec = (Extcol) zhalloc(sizeof(*ec));
ec->prog = gprog;
ec->ext = n;
ec->col = s;
ec->next = NULL;
if ((eo = mcolors.exts)) {
while (eo->next)
eo = eo->next;
eo->next = ec;
} else
mcolors.exts = ec;
if (*p)
*p++ = '\0';
return p;
} else if (*s == '=') {
char *p = ++s, *t, *cols[MAX_POS];
int ncols = 0;
Patprog prog;
/* This is for a pattern. */
while (*s && *s != '=')
s++;
if (!*s)
return s;
*s++ = '\0';
while (1) {
t = getcolval(s, 1);
if (ncols < MAX_POS)
cols[ncols++] = s;
s = t;
if (*s != '=')
break;
*s++ = '\0';
}
tokenize(p);
if ((prog = patcompile(p, 0, NULL))) {
Patcol pc, po;
int i;
pc = (Patcol) zhalloc(sizeof(*pc));
pc->prog = gprog;
pc->pat = prog;
for (i = 0; i < ncols; i++)
pc->cols[i] = cols[i];
pc->cols[i] = NULL;
pc->next = NULL;
if ((po = mcolors.pats)) {
while (po->next)
po = po->next;
po->next = pc;
} else
mcolors.pats = pc;
}
if (*t)
*t++ = '\0';
return t;
} else {
char *n = s, *p, **nn;
int i;
/* This is for a file type. */
while (*s && *s != '=')
s++;
if (!*s)
return s;
*s++ = '\0';
for (i = 0, nn = colnames; *nn; i++, nn++)
if (!strcmp(n, *nn))
break;
/*
* special case: highlighting link targets
*/
if (i == COL_LN && strpfx("target", s) &&
(s[6] == ':' || !s[6])) {
mcolors.flags |= LC_FOLLOW_SYMLINKS;
p = s + 6;
} else {
p = getcolval(s, 0);
if (*nn) {
Filecol fc, fo;
fc = (Filecol) zhalloc(sizeof(*fc));
fc->prog = (i == COL_EC || i == COL_LC || i == COL_RC ?
NULL : gprog);
fc->col = s;
fc->next = NULL;
if ((fo = mcolors.files[i])) {
while (fo->next)
fo = fo->next;
fo->next = fc;
} else
mcolors.files[i] = fc;
}
if (*p)
*p++ = '\0';
}
return p;
}
}
static Filecol
filecol(char *col)
{
Filecol fc;
fc = (Filecol) zhalloc(sizeof(*fc));
fc->prog = NULL;
fc->col = col;
fc->next = NULL;
return fc;
}
/*
* This initializes the given terminal color structure.
*/
static void
getcols()
{
char *s;
int i, l;
max_caplen = lr_caplen = 0;
mcolors.flags = 0;
queue_signals();
if (!(s = getsparam("ZLS_COLORS")) &&
!(s = getsparam("ZLS_COLOURS"))) {
for (i = 0; i < NUM_COLS; i++)
mcolors.files[i] = filecol("");
mcolors.pats = NULL;
mcolors.exts = NULL;
if ((s = tcstr[TCSTANDOUTBEG]) && s[0]) {
mcolors.files[COL_MA] = filecol(s);
mcolors.files[COL_EC] = filecol(tcstr[TCSTANDOUTEND]);
} else
mcolors.files[COL_MA] = filecol(defcols[COL_MA]);
lr_caplen = 0;
if ((max_caplen = strlen(mcolors.files[COL_MA]->col)) <
(l = strlen(mcolors.files[COL_EC]->col)))
max_caplen = l;
unqueue_signals();
return;
}
/* Reset the global color structure. */
memset(&mcolors, 0, sizeof(mcolors));
s = dupstring(s);
while (*s)
if (*s == ':')
s++;
else
s = getcoldef(s);
unqueue_signals();
/* Use default values for those that aren't set explicitly. */
for (i = 0; i < NUM_COLS; i++) {
if (!mcolors.files[i] || !mcolors.files[i]->col)
mcolors.files[i] = filecol(defcols[i]);
if (mcolors.files[i] && mcolors.files[i]->col &&
(l = strlen(mcolors.files[i]->col)) > max_caplen)
max_caplen = l;
}
lr_caplen = strlen(mcolors.files[COL_LC]->col) +
strlen(mcolors.files[COL_RC]->col);
/* Default for orphan is same as link. */
if (!mcolors.files[COL_OR] || !mcolors.files[COL_OR]->col)
mcolors.files[COL_OR] = mcolors.files[COL_LN];
/* Default for missing files: currently not used */
if (!mcolors.files[COL_MI] || !mcolors.files[COL_MI]->col)
mcolors.files[COL_MI] = mcolors.files[COL_FI];
return;
}
static void
zlrputs(char *cap)
{
if (!*last_cap || strcmp(last_cap, cap)) {
VARARR(char, buf, lr_caplen + max_caplen + 1);
strcpy(buf, mcolors.files[COL_LC]->col);
strcat(buf, cap);
strcat(buf, mcolors.files[COL_RC]->col);
tputs(buf, 1, putshout);
strcpy(last_cap, cap);
}
}
static void
zcputs(char *group, int colour)
{
Filecol fc;
for (fc = mcolors.files[colour]; fc; fc = fc->next)
if (fc->col &&
(!fc->prog || !group || pattry(fc->prog, group))) {
zlrputs(fc->col);
return;
}
zlrputs("0");
}
/* Turn off colouring. */
static void
zcoff(void)
{
if (mcolors.files[COL_EC] && mcolors.files[COL_EC]->col) {
tputs(mcolors.files[COL_EC]->col, 1, putshout);
*last_cap = '\0';
} else
zcputs(NULL, COL_NO);
}
static void
initiscol()
{
int i;
zlrputs(patcols[0]);
curiscols[curiscol = 0] = *patcols++;
curisbeg = curissend = 0;
for (i = 0; i < nrefs; i++)
sendpos[i] = 0xfffffff;
for (; i < MAX_POS; i++)
begpos[i] = endpos[i] = sendpos[i] = 0xfffffff;
}
static void
doiscol(int pos)
{
int fi;
while (pos > sendpos[curissend]) {
curissend++;
if (curiscol) {
zcputs(NULL, COL_NO);
zlrputs(curiscols[--curiscol]);
}
}
while (((fi = (endpos[curisbeg] < begpos[curisbeg] ||
begpos[curisbeg] == -1)) ||
pos == begpos[curisbeg]) && *patcols) {
if (!fi) {
int i, j, e = endpos[curisbeg];
/* insert e in sendpos */
for (i = curissend; sendpos[i] <= e; ++i)
;
for (j = MAX_POS - 1; j > i; --j)
sendpos[j] = sendpos[j-1];
sendpos[i] = e;
zcputs(NULL, COL_NO);
zlrputs(*patcols);
curiscols[++curiscol] = *patcols;
}
++patcols;
++curisbeg;
}
}
/* Stripped-down version of printfmt(). But can do in-string colouring. */
static int
clprintfmt(char *p, int ml)
{
int cc = 0, i = 0, ask, beg;
initiscol();
for (; *p; p++) {
doiscol(i++);
cc++;
if (*p == '\n') {
if (mlbeg >= 0 && tccan(TCCLEAREOL))
tcout(TCCLEAREOL);
cc = 0;
}
if (ml == mlend - 1 && (cc % zterm_columns) == zterm_columns - 1)
return 0;
if (*p == Meta) {
p++;
putc(*p ^ 32, shout);
} else
putc(*p, shout);
if ((beg = !(cc % zterm_columns)))
ml++;
if (mscroll && !(cc % zterm_columns) &&
!--mrestlines && (ask = asklistscroll(ml)))
return ask;
}
if (mlbeg >= 0 && tccan(TCCLEAREOL))
tcout(TCCLEAREOL);
return 0;
}
/*
* Local version of nicezputs() with in-string colouring
* and scrolling.
*/
static int
clnicezputs(int do_colors, char *s, int ml)
{
int i = 0, col = 0, ask, oml = ml;
char *t;
ZLE_CHAR_T cc;
#ifdef MULTIBYTE_SUPPORT
/*
* ums is the untokenized, unmetafied string (length umlen)
* uptr is a pointer into it
* sptr is the start of the nice character representation
* wptr is the point at which the wide character itself starts
* (but may be the end of the string if the character was fully
* prettified).
* ret is the return status from the conversion to a wide character
* umleft is the remaining length of the unmetafied string to output
* umlen is the full length of the unmetafied string
* width is the full printing width of a prettified character,
* including both ASCII prettification and the wide character itself.
* mbs is the shift state of the conversion to wide characters.
*/
char *ums, *uptr, *sptr, *wptr;
int umleft, umlen, eol = 0;
size_t width;
mbstate_t mbs;
memset(&mbs, 0, sizeof mbs);
ums = ztrdup(s);
untokenize(ums);
uptr = unmetafy(ums, ¨en);
umleft = umlen;
if (do_colors)
initiscol();
mb_metacharinit();
while (umleft > 0) {
size_t cnt = eol ? MB_INVALID : mbrtowc(&cc, uptr, umleft, &mbs);
switch (cnt) {
case MB_INCOMPLETE:
eol = 1;
/* FALL THROUGH */
case MB_INVALID:
/* This handles byte values that aren't valid wide-character
* sequences. */
sptr = nicechar(*uptr);
/* everything here is ASCII... */
width = strlen(sptr);
wptr = sptr + width;
cnt = 1;
/* Get mbs out of its undefined state. */
memset(&mbs, 0, sizeof mbs);
break;
case 0:
/* This handles a '\0' in the input (which is a real char
* to us, not a terminator). */
cnt = 1;
/* FALL THROUGH */
default:
sptr = wcs_nicechar(cc, &width, &wptr);
break;
}
umleft -= cnt;
uptr += cnt;
if (do_colors) {
/*
* The code for the colo[u]ri[s/z]ation is obscure (surprised?)
* but if we do it for every input character, as we do in
* the simple case, we shouldn't go too far wrong.
*/
while (cnt--)
doiscol(i++);
}
/*
* Loop over characters in the output of the nice
* representation. This will often correspond to one input
* (possibly multibyte) character.
*/
for (t = sptr; *t; t++) {
/* Input is metafied... */
int nc = (*t == Meta) ? STOUC(*++t ^ 32) : STOUC(*t);
/* Is the screen full? */
if (ml == mlend - 1 && col == zterm_columns - 1) {
mlprinted = ml - oml;
return 0;
}
if (t < wptr) {
/* outputting ASCII, so single-width */
putc(nc, shout);
col++;
width--;
} else {
/* outputting a single wide character, do the lot */
putc(nc, shout);
/* don't check column until finished */
if (t[1])
continue;
/* now we've done the entire rest of the representation */
col += width;
}
/*
* There might be problems with characters of printing width
* greater than one here.
*/
if (col > zterm_columns) {
ml++;
if (mscroll && !--mrestlines && (ask = asklistscroll(ml))) {
mlprinted = ml - oml;
return ask;
}
col -= zterm_columns;
if (do_colors)
fputs(" \010", shout);
}
}
}
free(ums);
#else
if (do_colors)
initiscol();
while ((cc = *s++)) {
if (do_colors)
doiscol(i++);
if (itok(cc)) {
if (cc <= Comma)
cc = ztokens[cc - Pound];
else
continue;
}
if (cc == Meta)
cc = *s++ ^ 32;
for (t = nicechar(cc); *t; t++) {
int nc = (*t == Meta) ? STOUC(*++t ^ 32) : STOUC(*t);
if (ml == mlend - 1 && col == zterm_columns - 1) {
mlprinted = ml - oml;
return 0;
}
putc(nc, shout);
if (++col > zterm_columns) {
ml++;
if (mscroll && !--mrestlines && (ask = asklistscroll(ml))) {
mlprinted = ml - oml;
return ask;
}
col = 0;
if (do_colors)
fputs(" \010", shout);
}
}
}
#endif
mlprinted = ml - oml;
return 0;
}
/* Get the terminal color string for the given match. */
static int
putmatchcol(char *group, char *n)
{
Patcol pc;
for (pc = mcolors.pats; pc; pc = pc->next) {
nrefs = MAX_POS - 1;
if ((!pc->prog || !group || pattry(pc->prog, group)) &&
pattryrefs(pc->pat, n, -1, -1, 0, &nrefs, begpos, endpos)) {
if (pc->cols[1]) {
patcols = pc->cols;
return 1;
}
zlrputs(pc->cols[0]);
return 0;
}
}
zcputs(group, COL_NO);
return 0;
}
/* Get the terminal color string for the file with the given name and
* file modes. */
static int
putfilecol(char *group, char *filename, mode_t m, int special)
{
int colour = -1;
Extcol ec;
Patcol pc;
int len;
for (pc = mcolors.pats; pc; pc = pc->next) {
nrefs = MAX_POS - 1;
if ((!pc->prog || !group || pattry(pc->prog, group)) &&
pattryrefs(pc->pat, filename, -1, -1, 0, &nrefs, begpos, endpos)) {
if (pc->cols[1]) {
patcols = pc->cols;
return 1;
}
zlrputs(pc->cols[0]);
return 0;
}
}
if (special != -1) {
colour = special;
} else if (S_ISDIR(m)) {
if (m & S_IWOTH)
if (m & S_ISVTX)
colour = COL_TW;
else
colour = COL_OW;
else if (m & S_ISVTX)
colour = COL_ST;
else
colour = COL_DI;
} else if (S_ISLNK(m))
colour = COL_LN;
else if (S_ISFIFO(m))
colour = COL_PI;
else if (S_ISSOCK(m))
colour = COL_SO;
else if (S_ISBLK(m))
colour = COL_BD;
else if (S_ISCHR(m))
colour = COL_CD;
else if (m & S_ISUID)
colour = COL_SU;
else if (m & S_ISGID)
colour = COL_SG;
else if (S_ISREG(m) && (m & S_IXUGO))
colour = COL_EX;
if (colour != -1) {
zcputs(group, colour);
return 0;
}
for (ec = mcolors.exts; ec; ec = ec->next)
if (strsfx(ec->ext, filename) &&
(!ec->prog || !group || pattry(ec->prog, group))) {
zlrputs(ec->col);
return 0;
}
/* Check for suffix alias */
len = strlen(filename);
/* shortest valid suffix format is a.b */
if (len > 2) {
char *suf = filename + len - 1;
while (suf > filename+1) {
if (suf[-1] == '.') {
if (sufaliastab->getnode(sufaliastab, suf)) {
zcputs(group, COL_SA);
return 0;
}
break;
}
suf--;
}
}
zcputs(group, COL_FI);
return 0;
}
static Cmgroup last_group;
/**/
static int
asklistscroll(int ml)
{
Thingy cmd;
int i, ret = 0;
compprintfmt(NULL, 1, 1, 1, ml, NULL);
fflush(shout);
zsetterm();
selectlocalmap(lskeymap);
if (!(cmd = getkeycmd()) || cmd == Th(z_sendbreak))
ret = 1;
else if (cmd == Th(z_acceptline) ||
cmd == Th(z_downhistory) ||
cmd == Th(z_downlineorhistory) ||
cmd == Th(z_downlineorsearch) ||
cmd == Th(z_vidownlineorhistory))
mrestlines = 1;
else if (cmd == Th(z_completeword) ||
cmd == Th(z_expandorcomplete) ||
cmd == Th(z_expandorcompleteprefix) ||
cmd == Th(z_menucomplete) ||
cmd == Th(z_menuexpandorcomplete) ||
!strcmp(cmd->nam, "menu-select") ||
!strcmp(cmd->nam, "complete-word") ||
!strcmp(cmd->nam, "expand-or-complete") ||
!strcmp(cmd->nam, "expand-or-complete-prefix") ||
!strcmp(cmd->nam, "menu-complete") ||
!strcmp(cmd->nam, "menu-expand-or-complete"))
mrestlines = zterm_lines - 1;
else if (cmd == Th(z_acceptsearch))
ret = 1;
else {
ungetkeycmd();