1/*
2 * comp.h - header file for completion
3 *
4 * This file is part of zsh, the Z shell.
5 *
6 * Copyright (c) 1992-1997 Paul Falstad
7 * All rights reserved.
8 *
9 * Permission is hereby granted, without written agreement and without
10 * license or royalty fees, to use, copy, modify, and distribute this
11 * software and to distribute modified versions of this software for any
12 * purpose, provided that the above copyright notice and the following
13 * two paragraphs appear in all copies of this software.
14 *
15 * In no event shall Paul Falstad or the Zsh Development Group be liable
16 * to any party for direct, indirect, special, incidental, or consequential
17 * damages arising out of the use of this software and its documentation,
18 * even if Paul Falstad and the Zsh Development Group have been advised of
19 * the possibility of such damage.
20 *
21 * Paul Falstad and the Zsh Development Group specifically disclaim any
22 * warranties, including, but not limited to, the implied warranties of
23 * merchantability and fitness for a particular purpose.  The software
24 * provided hereunder is on an "as is" basis, and Paul Falstad and the
25 * Zsh Development Group have no obligation to provide maintenance,
26 * support, updates, enhancements, or modifications.
27 *
28 */
29
30typedef struct cmatcher  *Cmatcher;
31typedef struct cmlist    *Cmlist;
32typedef struct cpattern  *Cpattern;
33typedef struct menuinfo  *Menuinfo;
34typedef struct cexpl *Cexpl;
35typedef struct cmgroup *Cmgroup;
36typedef struct cmatch *Cmatch;
37
38/* This is for explantion strings. */
39
40struct cexpl {
41    int always;                 /* display even without matches */
42    char *str;			/* the string */
43    int count;			/* the number of matches */
44    int fcount;			/* number of matches with fignore ignored */
45};
46
47/* This describes a group of matches. */
48
49struct cmgroup {
50    char *name;			/* the name of this group */
51    Cmgroup prev;		/* previous on the list */
52    Cmgroup next;		/* next one in list */
53    int flags;			/* see CGF_* below */
54    int mcount;			/* number of matches */
55    Cmatch *matches;		/* the matches */
56    int lcount;			/* number of things to list here */
57    int llcount;		/* number of line-displays */
58    char **ylist;		/* things to list */
59    int ecount;			/* number of explanation string */
60    Cexpl *expls;		/* explanation strings */
61    int ccount;			/* number of compctls used */
62    LinkList lexpls;		/* list of explanation string while building */
63    LinkList lmatches;		/* list of matches */
64    LinkList lfmatches;		/* list of matches without fignore */
65    LinkList lallccs;		/* list of used compctls */
66    int num;			/* number of this group */
67    int nbrbeg;			/* number of opened braces */
68    int nbrend;			/* number of closed braces */
69    int new;			/* new matches since last permalloc() */
70    /* The following is collected/used during listing. */
71    int dcount;			/* number of matches to list in columns */
72    int cols;			/* number of columns */
73    int lins;			/* number of lines */
74    int width;			/* column width */
75    int *widths;		/* column widths for listpacked */
76    int totl;			/* total length */
77    int shortest;		/* length of shortest match */
78    Cmgroup perm;		/* perm. alloced version of this group */
79#ifdef ZSH_HEAP_DEBUG
80    Heapid heap_id;
81#endif
82};
83
84
85#define CGF_NOSORT   1		/* don't sort this group */
86#define CGF_LINES    2		/* these are to be printed on different lines */
87#define CGF_HASDL    4		/* has display strings printed on separate lines */
88#define CGF_UNIQALL  8		/* remove all duplicates */
89#define CGF_UNIQCON 16		/* remove consecutive duplicates */
90#define CGF_PACKED  32		/* LIST_PACKED for this group */
91#define CGF_ROWS    64		/* LIST_ROWS_FIRST for this group */
92#define CGF_FILES   128		/* contains file names */
93
94/* This is the struct used to hold matches. */
95
96struct cmatch {
97    char *str;			/* the match itself */
98    char *orig;                 /* the match string unquoted */
99    char *ipre;			/* ignored prefix, has to be re-inserted */
100    char *ripre;		/* ignored prefix, unquoted */
101    char *isuf;			/* ignored suffix */
102    char *ppre;			/* the path prefix */
103    char *psuf;			/* the path suffix */
104    char *prpre;		/* path prefix for opendir */
105    char *pre;			/* prefix string from -P */
106    char *suf;			/* suffix string from -S */
107    char *disp;			/* string to display (compadd -d) */
108    char *autoq;		/* closing quote to add automatically */
109    int flags;			/* see CMF_* below */
110    int *brpl;			/* places where to put the brace prefixes */
111    int *brsl;			/* ...and the suffixes */
112    char *rems;			/* when to remove the suffix */
113    char *remf;			/* shell function to call for suffix-removal */
114    int qipl;			/* length of quote-prefix */
115    int qisl;			/* length of quote-suffix */
116    int rnum;			/* group relative number */
117    int gnum;			/* global number */
118    mode_t mode;                /* mode field of a stat */
119    char modec;                 /* LIST_TYPE-character for mode or nul */
120    mode_t fmode;               /* mode field of a stat, following symlink */
121    char fmodec;                /* LIST_TYPE-character for fmode or nul */
122};
123
124#define CMF_FILE     (1<< 0)	/* this is a file */
125#define CMF_REMOVE   (1<< 1)	/* remove the suffix */
126#define CMF_ISPAR    (1<< 2)	/* is paramter expansion */
127#define CMF_PARBR    (1<< 3)	/* paramter expansion with a brace */
128#define CMF_PARNEST  (1<< 4)	/* nested paramter expansion */
129#define CMF_NOLIST   (1<< 5)	/* should not be listed */
130#define CMF_DISPLINE (1<< 6)	/* display strings one per line */
131#define CMF_HIDE     (1<< 7)	/* temporarily hide this one */
132#define CMF_NOSPACE  (1<< 8)	/* don't add a space */
133#define CMF_PACKED   (1<< 9)	/* prefer LIST_PACKED */
134#define CMF_ROWS     (1<<10)	/* prefer LIST_ROWS_FIRST */
135#define CMF_MULT     (1<<11)	/* string appears more than once */
136#define CMF_FMULT    (1<<12)	/* first of multiple equal strings */
137#define CMF_ALL      (1<<13)	/* a match representing all other matches */
138#define CMF_DUMMY    (1<<14)	/* unselectable dummy match */
139#define CMF_MORDER   (1<<15)    /* order by matches, not display strings */
140
141/* Stuff for completion matcher control. */
142
143struct cmlist {
144    Cmlist next;		/* next one in the list of global matchers */
145    Cmatcher matcher;		/* the matcher definition */
146    char *str;			/* the string for it */
147};
148
149struct cmatcher {
150    int refc;			/* reference counter */
151    Cmatcher next;		/* next matcher */
152    int flags;			/* see CMF_* below */
153    Cpattern line;		/* what matches on the line */
154    int llen;			/* length of line pattern */
155    Cpattern word;		/* what matches in the word */
156    int wlen;			/* length of word pattern */
157    Cpattern left;		/* left anchor */
158    int lalen;			/* length of left anchor */
159    Cpattern right;		/* right anchor */
160    int ralen;			/* length of right anchor */
161};
162
163#define CMF_LINE  1
164#define CMF_LEFT  2
165#define CMF_RIGHT 4
166#define CMF_INTER 8
167
168/*
169 * Types of cpattern structure.
170 * Note freecpattern() assumes any <= CPAT_EQUIV have string.
171 */
172enum {
173    CPAT_CCLASS,		/* [...]: ordinary character class */
174    CPAT_NCLASS,		/* [!...]: ordinary character class, negated */
175    CPAT_EQUIV,			/* {...}: equivalence class */
176    CPAT_ANY,			/* ?: any character */
177    CPAT_CHAR			/* Single character given explicitly */
178};
179
180/*
181 * A pattern element in a matcher specification.
182 * Unlike normal patterns this only presents one character in
183 * either the test completion or the word on the command line.
184 */
185struct cpattern {
186    Cpattern next;		/* next sub-pattern */
187    int tp;			/* type of object as above */
188    union {
189	char *str;		/* if a character class, the objects
190				 * in it in a similar form to normal
191				 * pattern matching (a metafied string
192				 * with tokens).
193				 * Note the allocated length may be longer
194				 * than the null-terminated string.
195				 */
196	convchar_t chr;		/* if a single character, it */
197    } u;
198};
199
200/*
201 * For now this just handles single-byte characters.
202 * TODO: this will change.
203 */
204#ifdef MULTIBYTE_SUPPORT
205#define PATMATCHRANGE(r, c, ip, mtp)	mb_patmatchrange(r, c, ip, mtp)
206#define PATMATCHINDEX(r, i, cp, mtp)	mb_patmatchindex(r, i, cp, mtp)
207#define CONVCAST(c)			((wchar_t)(c))
208#define CHR_INVALID			(WEOF)
209#else
210#define PATMATCHRANGE(r, c, ip, mtp)	patmatchrange(r, c, ip, mtp)
211#define PATMATCHINDEX(r, i, cp, mtp)	patmatchindex(r, i, cp, mtp)
212#define CONVCAST(c)			(c)
213#define CHR_INVALID			(-1)
214#endif
215
216/* This is a special return value for parse_cmatcher(), *
217 * signalling an error. */
218
219#define pcm_err ((Cmatcher) 1)
220
221/* Information about what to put on the line as the unambiguous string.
222 * The code always keeps lists of these structs up to date while
223 * matches are added (in the aminfo structs below).
224 * The lists have two levels: in the first one we have one struct per
225 * word-part, where parts are separated by the anchors of `*' patterns.
226 * These structs have pointers (in the prefix and suffix fields) to
227 * lists of cline structs describing the strings before or after the
228 * the anchor. */
229
230typedef struct cline *Cline;
231typedef struct clsub Clsub;
232
233struct cline {
234    Cline next;
235    int flags;
236    char *line;
237    int llen;
238    char *word;
239    int wlen;
240    char *orig;
241    int olen;
242    int slen;
243    Cline prefix, suffix;
244    int min, max;
245};
246
247#define CLF_MISS      1
248#define CLF_DIFF      2
249#define CLF_SUF       4
250#define CLF_MID       8
251#define CLF_NEW      16
252#define CLF_LINE     32
253#define CLF_JOIN     64
254#define CLF_MATCHED 128
255#define CLF_SKIP    256
256
257/* Information for ambiguous completions. One for fignore ignored and   *
258 * one for normal completion. */
259
260typedef struct aminfo *Aminfo;
261
262struct aminfo {
263    Cmatch firstm;		/* the first match                        */
264    int exact;			/* if there was an exact match            */
265    Cmatch exactm;		/* the exact match (if any)               */
266    int count;			/* number of matches                      */
267    Cline line;			/* unambiguous line string                */
268};
269
270/* Information about menucompletion stuff. */
271
272struct menuinfo {
273    Cmgroup group;		/* position in the group list */
274    Cmatch *cur;		/* match currently inserted */
275    int pos;			/* begin on line */
276    int len;			/* length of inserted string */
277    int end;			/* end on the line */
278    int we;			/* non-zero if the cursor was at the end */
279    int insc;			/* length of suffix inserted */
280    int asked;			/* we asked if the list should be shown */
281    char *prebr;		/* prefix before a brace, if any */
282    char *postbr;		/* suffix after a brace */
283};
284
285/* Flags for compadd and addmatches(). */
286
287#define CAF_QUOTE    1
288#define CAF_NOSORT   2
289#define CAF_MATCH    4
290#define CAF_UNIQCON  8
291#define CAF_UNIQALL 16
292#define CAF_ARRAYS  32
293#define CAF_KEYS    64
294#define CAF_ALL    128
295
296/* Data for compadd and addmatches() */
297
298typedef struct cadata *Cadata;
299
300struct cadata {
301    char *ipre;			/* ignored prefix (-i) */
302    char *isuf;			/* ignored suffix (-I) */
303    char *ppre;			/* `path' prefix (-p) */
304    char *psuf;			/* `path' suffix (-s) */
305    char *prpre;		/* expanded `path' prefix (-W) */
306    char *pre;			/* prefix to insert (-P) */
307    char *suf;			/* suffix to insert (-S) */
308    char *group;		/* name of the group (-[JV]) */
309    char *rems;			/* remove suffix on chars... (-r) */
310    char *remf;			/* function to remove suffix (-R) */
311    char *ign;			/* ignored suffixes (-F) */
312    int flags;			/* CMF_* flags (-[fqn]) */
313    int aflags;			/* CAF_* flags (-[QUa]) */
314    Cmatcher match;		/* match spec (parsed from -M) */
315    char *exp;			/* explanation (-X) */
316    char *apar;			/* array to store matches in (-A) */
317    char *opar;			/* array to store originals in (-O) */
318    char *dpar;			/* array to delete non-matches in (-D) */
319    char *disp;			/* array with display lists (-d) */
320    char *mesg;			/* message to show unconditionally (-x) */
321    int dummies;               /* add that many dummy matches */
322};
323
324/* List data. */
325
326typedef struct cldata *Cldata;
327
328struct cldata {
329    int zterm_columns;		/* screen width */
330    int zterm_lines;		/* screen height */
331    int menuacc;		/* value of global menuacc */
332    int valid;			/* no need to calculate anew */
333    int nlist;			/* number of matches to list */
334    int nlines;			/* number of lines needed */
335    int hidden;			/* != 0 if there are hidden matches */
336    int onlyexpl;		/* != 0 if only explanations to print */
337    int showall;		/* != 0 if hidden matches should be shown */
338};
339
340typedef void (*CLPrintFunc)(Cmgroup, Cmatch *, int, int, int, int);
341
342/* Flags for fromcomp. */
343
344#define FC_LINE   1
345#define FC_INWORD 2
346
347/* Flags for special parameters. */
348
349#define CPN_WORDS      0
350#define CP_WORDS       (1 <<  CPN_WORDS)
351#define CPN_REDIRS     1
352#define CP_REDIRS      (1 <<  CPN_REDIRS)
353#define CPN_CURRENT    2
354#define CP_CURRENT     (1 <<  CPN_CURRENT)
355#define CPN_PREFIX     3
356#define CP_PREFIX      (1 <<  CPN_PREFIX)
357#define CPN_SUFFIX     4
358#define CP_SUFFIX      (1 <<  CPN_SUFFIX)
359#define CPN_IPREFIX    5
360#define CP_IPREFIX     (1 <<  CPN_IPREFIX)
361#define CPN_ISUFFIX    6
362#define CP_ISUFFIX     (1 <<  CPN_ISUFFIX)
363#define CPN_QIPREFIX   7
364#define CP_QIPREFIX    (1 <<  CPN_QIPREFIX)
365#define CPN_QISUFFIX   8
366#define CP_QISUFFIX    (1 <<  CPN_QISUFFIX)
367#define CPN_COMPSTATE  9
368#define CP_COMPSTATE   (1 <<  CPN_COMPSTATE)
369
370#define CP_REALPARAMS  10
371#define CP_ALLREALS    ((unsigned int) 0x3ff)
372
373
374#define CPN_NMATCHES   0
375#define CP_NMATCHES    (1 << CPN_NMATCHES)
376#define CPN_CONTEXT    1
377#define CP_CONTEXT     (1 << CPN_CONTEXT)
378#define CPN_PARAMETER  2
379#define CP_PARAMETER   (1 << CPN_PARAMETER)
380#define CPN_REDIRECT   3
381#define CP_REDIRECT    (1 << CPN_REDIRECT)
382#define CPN_QUOTE      4
383#define CP_QUOTE       (1 << CPN_QUOTE)
384#define CPN_QUOTING    5
385#define CP_QUOTING     (1 << CPN_QUOTING)
386#define CPN_RESTORE    6
387#define CP_RESTORE     (1 << CPN_RESTORE)
388#define CPN_LIST       7
389#define CP_LIST        (1 << CPN_LIST)
390#define CPN_INSERT     8
391#define CP_INSERT      (1 << CPN_INSERT)
392#define CPN_EXACT      9
393#define CP_EXACT       (1 << CPN_EXACT)
394#define CPN_EXACTSTR   10
395#define CP_EXACTSTR    (1 << CPN_EXACTSTR)
396#define CPN_PATMATCH   11
397#define CP_PATMATCH    (1 << CPN_PATMATCH)
398#define CPN_PATINSERT  12
399#define CP_PATINSERT   (1 << CPN_PATINSERT)
400#define CPN_UNAMBIG    13
401#define CP_UNAMBIG     (1 << CPN_UNAMBIG)
402#define CPN_UNAMBIGC   14
403#define CP_UNAMBIGC    (1 << CPN_UNAMBIGC)
404#define CPN_UNAMBIGP   15
405#define CP_UNAMBIGP    (1 << CPN_UNAMBIGP)
406#define CPN_INSERTP    16
407#define CP_INSERTP     (1 << CPN_INSERTP)
408#define CPN_LISTMAX    17
409#define CP_LISTMAX     (1 << CPN_LISTMAX)
410#define CPN_LASTPROMPT 18
411#define CP_LASTPROMPT  (1 << CPN_LASTPROMPT)
412#define CPN_TOEND      19
413#define CP_TOEND       (1 << CPN_TOEND)
414#define CPN_OLDLIST    20
415#define CP_OLDLIST     (1 << CPN_OLDLIST)
416#define CPN_OLDINS     21
417#define CP_OLDINS      (1 << CPN_OLDINS)
418#define CPN_VARED      22
419#define CP_VARED       (1 << CPN_VARED)
420#define CPN_LISTLINES  23
421#define CP_LISTLINES   (1 << CPN_LISTLINES)
422#define CPN_QUOTES     24
423#define CP_QUOTES      (1 << CPN_QUOTES)
424#define CPN_IGNORED    25
425#define CP_IGNORED     (1 << CPN_IGNORED)
426
427#define CP_KEYPARAMS   26
428#define CP_ALLKEYS     ((unsigned int) 0x3ffffff)
429
430/* Hooks. */
431
432#define INSERTMATCHHOOK     (comphooks + 0)
433#define MENUSTARTHOOK       (comphooks + 1)
434#define COMPCTLMAKEHOOK     (comphooks + 2)
435#define COMPCTLCLEANUPHOOK  (comphooks + 3)
436#define COMPLISTMATCHESHOOK (comphooks + 4)
437
438/* compctl hook data struct */
439
440struct ccmakedat {
441    char *str;
442    int incmd;
443    int lst;
444};
445
446/* Data given to offered hooks. */
447
448typedef struct chdata *Chdata;
449
450struct chdata {
451    Cmgroup matches;		/* the matches generated */
452    int num;			/* the number of matches */
453    int nmesg;			/* the number of messages */
454    Cmatch cur;			/* current match or NULL */
455};
456
457/* The number of columns to leave empty between rows of matches. */
458
459#define CM_SPACE  2
460
461