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