xref: /freebsd/contrib/nvi/common/key.h (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1 /*-
2  * Copyright (c) 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1991, 1993, 1994, 1995, 1996
5  *	Keith Bostic.  All rights reserved.
6  *
7  * See the LICENSE file for redistribution information.
8  *
9  *	$Id: key.h,v 10.56 2013/11/13 12:15:27 zy Exp $
10  */
11 
12 #include "multibyte.h"
13 
14 #ifdef USE_WIDECHAR
15 #define FILE2INT5(sp,buf,n,nlen,w,wlen)					    \
16     sp->conv.file2int(sp, n, nlen, &buf, &wlen, &w)
17 #define INT2FILE(sp,w,wlen,n,nlen) 					    \
18     sp->conv.int2file(sp, w, wlen, &sp->cw, &nlen, &n)
19 #define CHAR2INT5(sp,buf,n,nlen,w,wlen)					    \
20     sp->conv.sys2int(sp, n, nlen, &buf, &wlen, &w)
21 #define INT2CHAR(sp,w,wlen,n,nlen) 					    \
22     sp->conv.int2sys(sp, w, wlen, &sp->cw, &nlen, &n)
23 #define INPUT2INT5(sp,cw,n,nlen,w,wlen)					    \
24     sp->conv.input2int(sp, n, nlen, &(cw), &wlen, &w)
25 #define CONST
26 #define INTISWIDE(c)        (wctob(c) == EOF)
27 #define CHAR_WIDTH(sp, ch)  wcwidth(ch)
28 #define CAN_PRINT(sp, ch)   (CHAR_WIDTH(sp, ch) > 0)
29 #else
30 #define FILE2INT5(sp,buf,n,nlen,w,wlen) \
31     (w = n, wlen = nlen, 0)
32 #define INT2FILE(sp,w,wlen,n,nlen) \
33     (n = w, nlen = wlen, 0)
34 #define CHAR2INT5(sp,buf,n,nlen,w,wlen) \
35     (w = n, wlen = nlen, 0)
36 #define INT2CHAR(sp,w,wlen,n,nlen) \
37     (n = w, nlen = wlen, 0)
38 #define INPUT2INT5(sp,buf,n,nlen,w,wlen) \
39     (w = n, wlen = nlen, 0)
40 #define CONST               const
41 #define INTISWIDE(c)        0
42 #define CHAR_WIDTH(sp, ch)  1
43 #define CAN_PRINT(sp, ch)   isprint(ch)
44 #endif
45 #define FILE2INT(sp,n,nlen,w,wlen)					    \
46     FILE2INT5(sp,sp->cw,n,nlen,w,wlen)
47 #define CHAR2INT(sp,n,nlen,w,wlen)					    \
48     CHAR2INT5(sp,sp->cw,n,nlen,w,wlen)
49 
50 /* The maximum number of columns any character can take up on a screen. */
51 #define	MAX_CHARACTER_COLUMNS	7
52 
53 /*
54  * Event types.
55  *
56  * The program structure depends on the event loop being able to return
57  * E_EOF/E_ERR multiple times -- eventually enough things will end due
58  * to the events that vi will reach the command level for the screen, at
59  * which point the exit flags will be set and vi will exit.
60  */
61 typedef enum {
62 	E_NOTUSED = 0,			/* Not set. */
63 	E_CHARACTER,			/* Input character: e_c set. */
64 	E_EOF,				/* End of input (NOT ^D). */
65 	E_ERR,				/* Input error. */
66 	E_INTERRUPT,			/* Interrupt. */
67 	E_REPAINT,			/* Repaint: e_flno, e_tlno set. */
68 	E_SIGHUP,			/* SIGHUP. */
69 	E_SIGTERM,			/* SIGTERM. */
70 	E_STRING,			/* Input string: e_csp, e_len set. */
71 	E_TIMEOUT,			/* Timeout. */
72 	E_WRESIZE,			/* Window resize. */
73 } e_event_t;
74 
75 /*
76  * Character values.
77  */
78 typedef enum {
79 	K_NOTUSED = 0,			/* Not set. */
80 	K_BACKSLASH,			/*  \ */
81 	K_CARAT,			/*  ^ */
82 	K_CNTRLD,			/* ^D */
83 	K_CNTRLR,			/* ^R */
84 	K_CNTRLT,			/* ^T */
85 	K_CNTRLZ,			/* ^Z */
86 	K_COLON,			/*  : */
87 	K_CR,				/* \r */
88 	K_ESCAPE,			/* ^[ */
89 	K_FORMFEED,			/* \f */
90 	K_HEXCHAR,			/* ^X */
91 	K_NL,				/* \n */
92 	K_RIGHTBRACE,			/*  } */
93 	K_RIGHTPAREN,			/*  ) */
94 	K_TAB,				/* \t */
95 	K_VERASE,			/* set from tty: default ^H */
96 	K_VKILL,			/* set from tty: default ^U */
97 	K_VLNEXT,			/* set from tty: default ^V */
98 	K_VWERASE,			/* set from tty: default ^W */
99 	K_ZERO				/*  0 */
100 } e_key_t;
101 
102 struct _event {
103 	TAILQ_ENTRY(_event) q;		/* Linked list of events. */
104 	e_event_t e_event;		/* Event type. */
105 	union {
106 		struct {		/* Input character. */
107 			CHAR_T c;	/* Character. */
108 			e_key_t value;	/* Key type. */
109 
110 #define	CH_ABBREVIATED	0x01		/* Character is from an abbreviation. */
111 #define	CH_MAPPED	0x02		/* Character is from a map. */
112 #define	CH_NOMAP	0x04		/* Do not map the character. */
113 #define	CH_QUOTED	0x08		/* Character is already quoted. */
114 			u_int8_t flags;
115 		} _e_ch;
116 #define	e_ch	_u_event._e_ch		/* !!! The structure, not the char. */
117 #define	e_c	_u_event._e_ch.c
118 #define	e_value	_u_event._e_ch.value
119 #define	e_flags	_u_event._e_ch.flags
120 
121 		struct {		/* Screen position, size. */
122 			size_t lno1;	/* Line number. */
123 			size_t cno1;	/* Column number. */
124 			size_t lno2;	/* Line number. */
125 			size_t cno2;	/* Column number. */
126 		} _e_mark;
127 #define	e_lno	_u_event._e_mark.lno1	/* Single location. */
128 #define	e_cno	_u_event._e_mark.cno1
129 #define	e_flno	_u_event._e_mark.lno1	/* Text region. */
130 #define	e_fcno	_u_event._e_mark.cno1
131 #define	e_tlno	_u_event._e_mark.lno2
132 #define	e_tcno	_u_event._e_mark.cno2
133 
134 		struct {		/* Input string. */
135 			CHAR_T	*asp;	/* Allocated string. */
136 			CHAR_T	*csp;	/* String. */
137 			size_t	 len;	/* String length. */
138 		} _e_str;
139 #define	e_asp	_u_event._e_str.asp
140 #define	e_csp	_u_event._e_str.csp
141 #define	e_len	_u_event._e_str.len
142 	} _u_event;
143 };
144 
145 typedef struct _keylist {
146 	e_key_t value;			/* Special value. */
147 	int	ch;			/* Key. */
148 } KEYLIST;
149 extern KEYLIST keylist[];
150 
151 					/* Return if more keys in queue. */
152 #define	KEYS_WAITING(sp)	((sp)->gp->i_cnt != 0)
153 #define	MAPPED_KEYS_WAITING(sp)						\
154 	(KEYS_WAITING(sp) &&						\
155 	    F_ISSET(&sp->gp->i_event[sp->gp->i_next].e_ch, CH_MAPPED))
156 
157 /*
158  * Ex/vi commands are generally separated by whitespace characters.  We
159  * can't use the standard isspace(3) macro because it returns true for
160  * characters like ^K in the ASCII character set.  The POSIX isblank(3)
161  * has the same problem for non-ASCII locale, so we need a standalone one.
162  *
163  * XXX
164  * Note side effect, ch is evaluated multiple times.
165  */
166 #define	cmdskip(ch)	((ch) == ' ' || (ch) == '\t')
167 
168 /* The "standard" tab width, for displaying things to users. */
169 #define	STANDARD_TAB	6
170 
171 /* Various special characters, messages. */
172 #define	CH_BSEARCH	'?'		/* Backward search prompt. */
173 #define	CH_CURSOR	' '		/* Cursor character. */
174 #define	CH_ENDMARK	'$'		/* End of a range. */
175 #define	CH_EXPROMPT	':'		/* Ex prompt. */
176 #define	CH_FSEARCH	'/'		/* Forward search prompt. */
177 #define	CH_HEX		'\030'		/* Leading hex character. */
178 #define	CH_LITERAL	'\026'		/* ASCII ^V. */
179 #define	CH_NO		'n'		/* No. */
180 #define	CH_NOT_DIGIT	'a'		/* A non-isdigit() character. */
181 #define	CH_QUIT		'q'		/* Quit. */
182 #define	CH_YES		'y'		/* Yes. */
183 
184 /*
185  * Checking for interrupts means that we look at the bit that gets set if the
186  * screen code supports asynchronous events, and call back into the event code
187  * so that non-asynchronous screens get a chance to post the interrupt.
188  *
189  * INTERRUPT_CHECK is the number of lines "operated" on before checking for
190  * interrupts.
191  */
192 #define	INTERRUPT_CHECK	100
193 #define	INTERRUPTED(sp)							\
194 	(F_ISSET((sp)->gp, G_INTERRUPTED) ||				\
195 	(!v_event_get(sp, NULL, 0, EC_INTERRUPT) &&			\
196 	F_ISSET((sp)->gp, G_INTERRUPTED)))
197 #define	CLR_INTERRUPT(sp)						\
198 	F_CLR((sp)->gp, G_INTERRUPTED)
199 
200 /* Flags describing types of characters being requested. */
201 #define	EC_INTERRUPT	0x001		/* Checking for interrupts. */
202 #define	EC_MAPCOMMAND	0x002		/* Apply the command map. */
203 #define	EC_MAPINPUT	0x004		/* Apply the input map. */
204 #define	EC_MAPNODIGIT	0x008		/* Return to a digit. */
205 #define	EC_QUOTED	0x010		/* Try to quote next character */
206 #define	EC_RAW		0x020		/* Any next character. XXX: not used. */
207 #define	EC_TIMEOUT	0x040		/* Timeout to next character. */
208 
209 /* Flags describing text input special cases. */
210 #define	TXT_ADDNEWLINE	0x00000001	/* Replay starts on a new line. */
211 #define	TXT_AICHARS	0x00000002	/* Leading autoindent chars. */
212 #define	TXT_ALTWERASE	0x00000004	/* Option: altwerase. */
213 #define	TXT_APPENDEOL	0x00000008	/* Appending after EOL. */
214 #define	TXT_AUTOINDENT	0x00000010	/* Autoindent set this line. */
215 #define	TXT_BACKSLASH	0x00000020	/* Backslashes escape characters. */
216 #define	TXT_BEAUTIFY	0x00000040	/* Only printable characters. */
217 #define	TXT_BS		0x00000080	/* Backspace returns the buffer. */
218 #define	TXT_CEDIT	0x00000100	/* Can return TERM_CEDIT. */
219 #define	TXT_CNTRLD	0x00000200	/* Control-D is a command. */
220 #define	TXT_CNTRLT	0x00000400	/* Control-T is an indent special. */
221 #define	TXT_CR		0x00000800	/* CR returns the buffer. */
222 #define	TXT_DOTTERM	0x00001000	/* Leading '.' terminates the input. */
223 #define	TXT_EMARK	0x00002000	/* End of replacement mark. */
224 #define	TXT_EOFCHAR	0x00004000	/* ICANON set, return EOF character. */
225 #define	TXT_ESCAPE	0x00008000	/* Escape returns the buffer. */
226 #define	TXT_FILEC	0x00010000	/* Option: filec. */
227 #define	TXT_INFOLINE	0x00020000	/* Editing the info line. */
228 #define	TXT_MAPINPUT	0x00040000	/* Apply the input map. */
229 #define	TXT_NLECHO	0x00080000	/* Echo the newline. */
230 #define	TXT_NUMBER	0x00100000	/* Number the line. */
231 #define	TXT_OVERWRITE	0x00200000	/* Overwrite characters. */
232 #define	TXT_PROMPT	0x00400000	/* Display a prompt. */
233 #define	TXT_RECORD	0x00800000	/* Record for replay. */
234 #define	TXT_REPLACE	0x01000000	/* Replace; don't delete overwrite. */
235 #define	TXT_REPLAY	0x02000000	/* Replay the last input. */
236 #define	TXT_RESOLVE	0x04000000	/* Resolve the text into the file. */
237 #define	TXT_SEARCHINCR	0x08000000	/* Incremental search. */
238 #define	TXT_SHOWMATCH	0x10000000	/* Option: showmatch. */
239 #define	TXT_TTYWERASE	0x20000000	/* Option: ttywerase. */
240 #define	TXT_WRAPMARGIN	0x40000000	/* Option: wrapmargin. */
241