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