-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
term.h
222 lines (188 loc) · 3.89 KB
/
term.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Terminal-handling definitions common to the interpreter and edlis.
* This also includes some presentation-related functions, e.g. syntax highlighting.
*
* There are two relevant library layers here:
* 1. Curses is high-level, but only appropriate for fullscreen programs
* 2. Terminfo is lower-level (curses is built on it)
*
* I only considered standardized libraries (e.g. X/Open).
*/
#ifndef TERM_H
#define TERM_H
//#include "compat/curses_stubs.h"
#ifndef FULLSCREEN
#include "compat/term_stubs.h"
#endif
#include <stdbool.h>
#define COLOR_CYAN 6
// special charactor
#define EOL '\n'
#define RET '\r'
#define TAB '\t'
#define SPACE ' '
#define ESC 27
// TCC does not have support for "static conts" as compile time constant
#ifdef __TINYC__
#define NUL '\0'
#define BEL '\a'
#define BS '\b'
#else
static const char NUL = '\0';
static const char BEL = '\a';
static const char BS = '\b';
#endif
#define DEL 127
#ifndef FULLSCREEN
#define ARROW_PREFIX '['
extern char ed_key_down;
extern char ed_key_left;
extern char ed_key_right;
extern char ed_key_up;
#endif
#ifdef FULLSCREEN
/*
* Edlis uses the higher-level curses interface
*/
__dead void errw(const char *msg);
#define CHECK(fn, ...) { \
if ((fn)(__VA_ARGS__) == ERR) { \
errw(#fn); \
} \
}
static inline void ESCHOME(void)
{
CHECK(move, 0, 0);
}
static inline void ESCTOP(void)
{
CHECK(move, 1, 0);
}
static inline void ESCCLS(void)
{
CHECK(clear);
}
static inline void ESCCLS1(void)
{
CHECK(clrtobot);
}
static inline void ESCCLSL(void)
{
CHECK(clrtoeol);
}
static inline void ESCMVLEFT(int x)
{
int dummy, cur_y;
getyx(stdscr, cur_y, dummy);
CHECK(move, cur_y, x - 1);
}
static inline void ESCCLSLA(void)
{
ESCMVLEFT(1);
CHECK(clrtoeol);
}
static inline void ESCMOVE(int y, int x)
{
CHECK(move, y - 1, x - 1);
}
static inline void ESCFORG(void)
{
if (has_colors()) {
CHECK(color_set, 0, NULL);
}
}
enum Color { RED_ON_DFL =
1, YELLOW_ON_DFL, BLUE_ON_DFL, MAGENTA_ON_DFL, CYAN_ON_DFL,
DFL_ON_CYAN, GREEN_ON_DFL,
};
static inline void ESCBCYAN(void)
{
if (has_colors()) {
CHECK(color_set, DFL_ON_CYAN, NULL);
}
}
static inline void ESCBORG(void)
{
if (has_colors()) {
CHECK(color_set, 0, NULL);
}
}
static inline void ESCREV(void)
{
CHECK(attron, A_REVERSE);
}
static inline void ESCRST(void)
{
CHECK(attrset, A_NORMAL);
}
static inline void ESCBOLD(void)
{
CHECK(attron, A_BOLD);
}
#elif defined(WITHOUT_CURSES)
#define ESCCLSL()
#define ESCMVLEFT(x)
#define ESCMVU()
#define ESCSCR()
#define ESCFORG()
#define ESCBCYAN()
#define ESCBORG()
#define ESCREV()
#define ESCRST()
#define ESCBOLD()
#else
/*
* The REPL uses the lower-level terminfo interface because we don't want
* to clear the screen
*/
static inline void ESCCLSL(void)
{
putp(clr_eol);
}
static inline void ESCMVLEFT(int x)
{
putp(tparm(column_address, x - 1));
}
static inline void ESCMVU(void)
{
putp(cursor_up);
}
static inline void ESCSCR(void)
{
putp(scroll_forward);
}
static inline void ESCFORG(void)
{
putp(exit_attribute_mode);
}
static inline void ESCBCYAN(void)
{
putp(tparm(set_a_background, COLOR_CYAN));
}
static inline void ESCBORG(void)
{
putp(exit_attribute_mode);
}
static inline void ESCREV(void)
{
putp(enter_reverse_mode);
}
static inline void ESCRST(void)
{
putp(exit_attribute_mode);
}
static inline void ESCBOLD(void)
{
putp(enter_bold_mode);
}
#endif
enum HighlightToken { HIGHLIGHT_NONE, HIGHLIGHT_OPERATOR, HIGHLIGHT_BUILTIN,
HIGHLIGHT_STRING, HIGHLIGHT_COMMENT, HIGHLIGHT_EXTENDED, HIGHLIGHT_DOUBLEQUOTE,
HIGHLIGHT_MULTILINE_COMMENT, HIGHLIGHT_QUOTE, HIGHLIGHT_FUNCTION,
};
enum HighlightToken maybe_match(const char *str);
bool in_special_table(const char *str);
void gather_fuzzy_matches(const char *str,
const char *candidates[], int *candidate_pt);
#define COMPLETION_CANDIDATES_MAX 50
#endif