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