1/*
2 * zle.h - header file for line editor
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
30#ifdef MULTIBYTE_SUPPORT
31typedef wchar_t ZLE_CHAR_T;
32typedef wchar_t *ZLE_STRING_T;
33typedef wint_t   ZLE_INT_T;
34#define ZLE_CHAR_SIZE	sizeof(wchar_t)
35
36
37#define ZLEEOF	WEOF
38
39/* Functions that operate on a ZLE_STRING_T. */
40#define ZS_memcpy wmemcpy
41#define ZS_memmove wmemmove
42#define ZS_memset wmemset
43#define ZS_memcmp wmemcmp
44#define ZS_strlen wcslen
45#define ZS_strcpy wcscpy
46#define ZS_strncpy wcsncpy
47#define ZS_strncmp wcsncmp
48#define ZS_zarrdup wcs_zarrdup
49#define ZS_width wcslen
50#define ZS_strchr wcschr
51#define ZS_memchr wmemchr
52
53/*
54 * Functions that operate on a metafied string.
55 * These versions handle multibyte characters.
56 */
57#define ZMB_nicewidth(s)	mb_niceformat(s, NULL, NULL, 0)
58
59/* Functions that operate on ZLE_CHAR_T. */
60#define ZC_ialpha iswalpha
61#define ZC_ialnum iswalnum
62#define ZC_iblank wcsiblank
63#define ZC_icntrl iswcntrl
64#define ZC_idigit iswdigit
65#define ZC_iident(x) wcsitype((x), IIDENT)
66#define ZC_ilower iswlower
67#define ZC_inblank iswspace
68#define ZC_iupper iswupper
69#define ZC_iword(x) wcsitype((x), IWORD)
70
71#define ZC_tolower towlower
72#define ZC_toupper towupper
73
74#define LASTFULLCHAR	lastchar_wide
75#define LASTFULLCHAR_T  ZLE_INT_T
76
77/*
78 * We may need to handle combining character alignment.
79 * The following fix up the position of the cursor so that it
80 * never ends up over a zero-width punctuation character following
81 * an alphanumeric character.  The first is used if we were
82 * moving the cursor left, the second if we were moving right or
83 * if something under the cursor may have changed.
84 */
85#define CCLEFT()	alignmultiwordleft(&zlecs, 1)
86#define CCRIGHT()	alignmultiwordright(&zlecs, 1)
87/*
88 * Same for any other position
89 */
90#define CCLEFTPOS(pos)	alignmultiwordleft(&pos, 1)
91#define CCRIGHTPOS(pos)	alignmultiwordright(&pos, 1)
92/*
93 * Increment or decrement the cursor position, skipping over
94 * combining characters.
95 */
96#define INCCS()		inccs()
97#define DECCS()		deccs()
98/*
99 * Same for any other position.
100 */
101#define INCPOS(pos)	incpos(&pos)
102#define DECPOS(pos)	decpos(&pos)
103
104#else  /* Not MULTIBYTE_SUPPORT: old single-byte code */
105
106typedef char ZLE_CHAR_T;
107typedef char *ZLE_STRING_T;
108typedef int ZLE_INT_T;
109#define ZLE_CHAR_SIZE	sizeof(ZLE_CHAR_T)
110
111#define ZLEEOF	EOF
112
113/* Functions that operate on a ZLE_STRING_T. */
114#define ZS_memcpy memcpy
115#define ZS_memmove memmove
116#define ZS_memset memset
117#define ZS_memcmp memcmp
118#define ZS_zarrdup zarrdup
119#define ZS_width ztrlen
120#define ZS_strchr strchr
121#define ZS_memchr memchr
122
123/*
124 * Functions that operate on a metafied string.
125 * These versions don't handle multibyte characters.
126 */
127#define ZMB_nicewidth	niceztrlen
128
129#ifdef __GNUC__
130static inline size_t ZS_strlen(ZLE_STRING_T s)
131{ return strlen((char*)s); }
132static inline ZLE_STRING_T ZS_strcpy(ZLE_STRING_T t, ZLE_STRING_T f)
133{ return (ZLE_STRING_T)strcpy((char*)t, (char*)f); }
134static inline ZLE_STRING_T ZS_strncpy(ZLE_STRING_T t, ZLE_STRING_T f, size_t l)
135{ return (ZLE_STRING_T)strncpy((char*)t, (char*)f, l); }
136static inline int ZS_strncmp(ZLE_STRING_T s1, ZLE_STRING_T s2, size_t l)
137{ return strncmp((char*)s1, (char*)s2, l); }
138#else
139#define ZS_strlen(s) strlen((char*)(s))
140#define ZS_strcpy(t,f) strcpy((char*)(t),(char*)(f))
141#define ZS_strncpy(t,f,l) strncpy((char*)(t),(char*)(f),(l))
142#define ZS_strncmp(s1,s2,l) strncmp((char*)(s1),(char*)(s2),(l))
143#endif
144
145/* Functions that operate on ZLE_CHAR_T. */
146#define ZC_ialpha ialpha
147#define ZC_ialnum ialnum
148#define ZC_iblank iblank
149#define ZC_icntrl icntrl
150#define ZC_idigit idigit
151#define ZC_iident iident
152#define ZC_ilower islower
153#define ZC_inblank inblank
154#define ZC_iupper isupper
155#define ZC_iword iword
156
157#define ZC_tolower tulower
158#define ZC_toupper tuupper
159
160#define LASTFULLCHAR	lastchar
161#define LASTFULLCHAR_T	int
162
163/* Combining character alignment: none in this mode */
164#define CCLEFT()
165#define CCRIGHT()
166#define CCLEFTPOS(pos)
167#define CCRIGHTPOS(pos)
168/*
169 * Increment or decrement the cursor position: simple in this case.
170 */
171#define INCCS()		((void)(zlecs++))
172#define DECCS()		((void)(zlecs--))
173/*
174 * Same for any other position.
175 */
176#define INCPOS(pos)	((void)(pos++))
177#define DECPOS(pos)	((void)(pos--))
178
179#endif
180
181
182typedef struct widget *Widget;
183typedef struct thingy *Thingy;
184
185/* widgets (ZLE functions) */
186
187typedef int (*ZleIntFunc) _((char **));
188
189struct widget {
190    int flags;		/* flags (see below) */
191    Thingy first;	/* `first' thingy that names this widget */
192    union {
193	ZleIntFunc fn;	/* pointer to internally implemented widget */
194	char *fnnam;	/* name of the shell function for user-defined widget */
195	struct {
196	    ZleIntFunc fn; /* internal widget function to call */
197	    char *wid;     /* name of widget to call */
198	    char *func;    /* name of shell function to call */
199	} comp;
200    } u;
201};
202
203#define WIDGET_INT	(1<<0)	/* widget is internally implemented */
204#define WIDGET_NCOMP    (1<<1)	/* new style completion widget */
205#define ZLE_MENUCMP	(1<<2)	/* DON'T invalidate completion list */
206#define ZLE_YANK	(1<<3)
207#define ZLE_LINEMOVE	(1<<4)	/* command is a line-oriented movement */
208#define ZLE_LASTCOL     (1<<5)	/* command maintains lastcol correctly */
209#define ZLE_KILL	(1<<6)
210#define ZLE_KEEPSUFFIX	(1<<7)	/* DON'T remove added suffix */
211#define ZLE_NOTCOMMAND  (1<<8)	/* widget should not alter lastcmd */
212#define ZLE_ISCOMP      (1<<9)	/* usable for new style completion */
213
214/* thingies */
215
216struct thingy {
217    HashNode next;	/* next node in the hash chain */
218    char *nam;		/* name of the thingy */
219    int flags;		/* TH_* flags (see below) */
220    int rc;		/* reference count */
221    Widget widget;	/* widget named by this thingy */
222    Thingy samew;	/* `next' thingy (circularly) naming the same widget */
223};
224
225/* DISABLED is (1<<0) */
226#define TH_IMMORTAL	(1<<1)    /* can't refer to a different widget */
227
228/* command modifier prefixes */
229
230struct modifier {
231    int flags;		/* MOD_* flags (see below) */
232    int mult;		/* repeat count */
233    int tmult;		/* repeat count actually being edited */
234    int vibuf;		/* vi cut buffer */
235    int base;		/* numeric base for digit arguments (usually 10) */
236};
237
238#define MOD_MULT  (1<<0)   /* a repeat count has been selected */
239#define MOD_TMULT (1<<1)   /* a repeat count is being entered */
240#define MOD_VIBUF (1<<2)   /* a vi cut buffer has been selected */
241#define MOD_VIAPP (1<<3)   /* appending to the vi cut buffer */
242#define MOD_NEG   (1<<4)   /* last command was negate argument */
243
244/* current modifier status */
245
246#define zmult (zmod.mult)
247
248/* flags to cut() and cuttext() and other front-ends */
249
250#define CUT_FRONT   (1<<0)   /* Text goes in front of cut buffer */
251#define CUT_REPLACE (1<<1)   /* Text replaces cut buffer */
252#define CUT_RAW     (1<<2)   /*
253			      * Raw character counts (not used in cut itself).
254			      * This is used when the values are offsets
255			      * into the zleline array rather than numbers
256			      * of visible characters directly input by
257			      * the user.
258			      */
259
260/* undo system */
261
262struct change {
263    struct change *prev, *next;	/* adjacent changes */
264    int flags;			/* see below */
265    int hist;			/* history line being changed */
266    int off;			/* offset of the text changes */
267    ZLE_STRING_T del;		/* characters to delete */
268    int dell;			/* no. of characters in del */
269    ZLE_STRING_T ins;		/* characters to insert */
270    int insl;			/* no. of characters in ins */
271    int old_cs, new_cs;		/* old and new cursor positions */
272    zlong changeno;             /* unique number of this change */
273};
274
275#define CH_NEXT (1<<0)   /* next structure is also part of this change */
276#define CH_PREV (1<<1)   /* previous structure is also part of this change */
277
278/* known thingies */
279
280#define Th(X) (&thingies[X])
281
282/* opaque keymap type */
283
284typedef struct keymap *Keymap;
285
286typedef void (*KeyScanFunc) _((char *, Thingy, char *, void *));
287
288#define invicmdmode() (!strcmp(curkeymapname, "vicmd"))
289
290/* Standard type of suffix removal. */
291
292#ifdef MULTIBYTE_SUPPORT
293#define NO_INSERT_CHAR	WEOF
294#else
295#define NO_INSERT_CHAR  256
296#endif
297#define removesuffix() iremovesuffix(NO_INSERT_CHAR, 0)
298
299/*
300 * Cut/kill buffer type.  The buffer itself is purely binary data, not
301 * NUL-terminated.  len is a length count (N.B. number of characters,
302 * not size in bytes).  flags uses the CUTBUFFER_* constants defined
303 * below.
304 */
305
306struct cutbuffer {
307    ZLE_STRING_T buf;
308    size_t len;
309    char flags;
310};
311
312typedef struct cutbuffer *Cutbuffer;
313
314#define CUTBUFFER_LINE 1   /* for vi: buffer contains whole lines of data */
315
316#define KRINGCTDEF 8   /* default number of buffers in the kill ring */
317
318/* Types of completion. */
319
320#define COMP_COMPLETE        0
321#define COMP_LIST_COMPLETE   1
322#define COMP_SPELL           2
323#define COMP_EXPAND          3
324#define COMP_EXPAND_COMPLETE 4
325#define COMP_LIST_EXPAND     5
326#define COMP_ISEXPAND(X) ((X) >= COMP_EXPAND)
327
328/* Information about one brace run. */
329
330typedef struct brinfo *Brinfo;
331
332struct brinfo {
333    Brinfo next;		/* next in list */
334    Brinfo prev;		/* previous (only for closing braces) */
335    char *str;			/* the string to insert */
336    int pos;			/* original position */
337    int qpos;			/* original position, with quoting */
338    int curpos;			/* position for current match */
339};
340
341/* Convenience macros for the hooks */
342
343#define LISTMATCHESHOOK    (zlehooks + 0)
344#define COMPLETEHOOK       (zlehooks + 1)
345#define BEFORECOMPLETEHOOK (zlehooks + 2)
346#define AFTERCOMPLETEHOOK  (zlehooks + 3)
347#define ACCEPTCOMPHOOK     (zlehooks + 4)
348#define REVERSEMENUHOOK    (zlehooks + 5)
349#define INVALIDATELISTHOOK (zlehooks + 6)
350
351/* complete hook data struct */
352
353typedef struct compldat *Compldat;
354
355struct compldat {
356    char *s;
357    int lst;
358    int incmd;
359};
360
361/* List completion matches. */
362
363#define listmatches() runhookdef(LISTMATCHESHOOK, NULL)
364
365/* Invalidate the completion list. */
366
367#define invalidatelist() runhookdef(INVALIDATELISTHOOK, NULL)
368
369/* Bit flags to setline */
370enum {
371    ZSL_COPY = 1,		/* Copy the argument, don't modify it */
372    ZSL_TOEND = 2,		/* Go to the end of the new line */
373};
374
375
376/* Type arguments to addsuffix() */
377enum suffixtype {
378    SUFTYP_POSSTR,		/* String of characters to match */
379    SUFTYP_NEGSTR,		/* String of characters not to match */
380    SUFTYP_POSRNG,		/* Range of characters to match */
381    SUFTYP_NEGRNG		/* Range of characters not to match */
382};
383
384/* Additional flags to suffixes */
385enum suffixflags {
386    SUFFLAGS_SPACE = 0x0001	/* Add a space when removing suffix */
387};
388
389
390/* Flags for the region_highlight structure */
391enum {
392    /* Offsets include predisplay */
393    ZRH_PREDISPLAY = 1
394};
395
396/*
397 * Attributes used for highlighting regions.
398 * and mark.
399 */
400struct region_highlight {
401    /* Attributes turned on in the region */
402    int atr;
403    /* Start of the region */
404    int start;
405    /* Start of the region in metafied ZLE line */
406    int start_meta;
407    /*
408     * End of the region:  position of the first character not highlighted
409     * (the same system as for point and mark).
410     */
411    int end;
412    /* End of the region in metafied ZLE line */
413    int end_meta;
414    /*
415     * Any of the flags defined above.
416     */
417    int flags;
418};
419
420/*
421 * Count of special uses of region highlighting, which account
422 * for the first few elements of region_highlights.
423 * 0: region between point and mark
424 * 1: isearch region
425 * 2: suffix
426 */
427#define N_SPECIAL_HIGHLIGHTS	(3)
428
429
430#ifdef MULTIBYTE_SUPPORT
431/*
432 * We use a wint_t here, since we need an invalid character as a
433 * placeholder and wint_t guarantees that we can use WEOF to do this.
434 */
435typedef wint_t REFRESH_CHAR;
436#else
437typedef char REFRESH_CHAR;
438#endif
439
440/*
441 * Description of one screen cell in zle_refresh.c
442 */
443typedef struct {
444    /*
445     * The (possibly wide) character.
446     * If atr contains TXT_MULTIWORD_MASK, an index into the set of multiword
447     * symbols (only if MULTIBYTE_SUPPORT is present).
448     */
449    REFRESH_CHAR chr;
450    /*
451     * Its attributes.  'On' attributes (TXT_ATTR_ON_MASK) are
452     * applied before the character, 'off' attributes (TXT_ATTR_OFF_MASK)
453     * after it.  'On' attributes are present for all characters that
454     * need the effect; 'off' attributes are only present for the
455     * last character in the sequence.
456     */
457    int atr;
458} REFRESH_ELEMENT;
459
460/* A string of screen cells */
461typedef REFRESH_ELEMENT *REFRESH_STRING;
462
463
464#if defined(MULTIBYTE_SUPPORT) && defined(__STDC_ISO_10646__)
465/*
466 * With ISO 10646 there is a private range defined within
467 * the encoding.  We use this for storing single-byte
468 * characters in sections of strings that wouldn't convert to wide
469 * characters.  This allows to preserve the string when transformed
470 * back to multibyte strings.
471 */
472
473/* The start of the private range we use, for 256 characters */
474#define ZSH_INVALID_WCHAR_BASE	(0xe000U)
475/* Detect a wide character within our range */
476#define ZSH_INVALID_WCHAR_TEST(x)			\
477    ((unsigned)(x) >= ZSH_INVALID_WCHAR_BASE &&		\
478     (unsigned)(x) <= (ZSH_INVALID_WCHAR_BASE + 255u))
479/* Turn a wide character in that range back to single byte */
480#define ZSH_INVALID_WCHAR_TO_CHAR(x)			\
481    ((char)((unsigned)(x) - ZSH_INVALID_WCHAR_BASE))
482/* Turn a wide character in that range to an integer */
483#define ZSH_INVALID_WCHAR_TO_INT(x)			\
484    ((int)((unsigned)(x) - ZSH_INVALID_WCHAR_BASE))
485/* Turn a single byte character into a private wide character */
486#define ZSH_CHAR_TO_INVALID_WCHAR(x)			\
487    ((wchar_t)(STOUC(x) + ZSH_INVALID_WCHAR_BASE))
488#endif
489
490
491#ifdef DEBUG
492#define METACHECK()		\
493	DPUTS(zlemetaline == NULL, "line not metafied")
494#define UNMETACHECK()		\
495	DPUTS(zlemetaline != NULL, "line metafied")
496#else
497#define METACHECK()
498#define UNMETACHECK()
499#endif
500
501
502typedef struct watch_fd *Watch_fd;
503
504struct watch_fd {
505    /* Function to call */
506    char *func;
507    /* Watched fd */
508    int fd;
509    /* 1 if func is called as a widget */
510    int widget;
511};
512