-
Notifications
You must be signed in to change notification settings - Fork 3
/
slackline.c
329 lines (283 loc) · 6.05 KB
/
slackline.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright (c) 2015-2023 Jan Klemkow <[email protected]>
* Copyright (c) 2022-2023 Tom Schwindl <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <grapheme.h>
#include "slackline_internals.h"
#include "slackline.h"
#include "util.h"
/* CTRL+W: stop erasing if certain characters are reached. */
#define IS_WORD_BREAK "\f\n\r\t\v (){}[]\\/#,.=-+|%$!@^&*"
struct slackline *
sl_init(void)
{
char *mode = getenv("EDITOR");
struct slackline *sl = malloc(sizeof *sl);
if (sl == NULL)
return NULL;
sl->bufsize = BUFSIZ;
if ((sl->buf = malloc(sl->bufsize)) == NULL) {
free(sl);
return NULL;
}
memset(sl->ubuf, 0, sizeof(sl->ubuf));
sl->ubuf_len = 0;
sl_reset(sl);
sl->mode = SL_DEFAULT;
if (mode != NULL) {
if (strcmp(mode, "emacs") == 0)
sl->mode = SL_EMACS;
else if (strcmp(mode, "vi") == 0)
sl->mode = SL_VI;
}
return sl;
}
void
sl_free(struct slackline *sl)
{
free(sl->buf);
free(sl);
}
void
sl_reset(struct slackline *sl)
{
sl->buf[0] = '\0';
sl->ptr = sl->buf;
sl->last = sl->buf;
sl->bcur = 0;
sl->blen = 0;
sl->rcur = 0;
sl->rlen = 0;
sl->esc = ESC_NONE;
sl->ubuf_len = 0;
}
void
sl_mode(struct slackline *sl, enum mode mode)
{
sl->mode = mode;
}
size_t
sl_postobyte(struct slackline *sl, size_t pos)
{
char *ptr = &sl->buf[0];
size_t byte = 0;
for (;pos > 0; pos--)
byte += grapheme_next_character_break_utf8(ptr+byte,
sl->blen-byte);
return byte;
}
char *
sl_postoptr(struct slackline *sl, size_t pos)
{
return &sl->buf[sl_postobyte(sl, pos)];
}
void
sl_backspace(struct slackline *sl)
{
char *ncur;
if (sl->rcur == 0)
return;
ncur = sl_postoptr(sl, sl->rcur - 1);
if (sl->rcur < sl->rlen)
memmove(ncur, sl->ptr, sl->last - sl->ptr);
sl->rcur--;
sl->rlen--;
sl->bcur = sl_postobyte(sl, sl->rcur);
sl->blen = sl_postobyte(sl, sl->rlen);
sl->last -= sl->ptr - ncur;
*sl->last = '\0';
sl->ptr = ncur;
}
void
sl_move(struct slackline *sl, enum direction dir)
{
switch (dir) {
case HOME:
sl->bcur = sl->rcur = 0;
sl->ptr = sl->buf;
return;
case END:
sl->rcur = sl->rlen;
break;
case RIGHT:
if (sl->rcur < sl->rlen)
sl->rcur++;
break;
case LEFT:
if (sl->rcur > 0)
sl->rcur--;
break;
}
sl->bcur = sl_postobyte(sl, sl->rcur);
sl->ptr = sl->buf + sl->bcur;
}
static void
sl_default(struct slackline *sl, int key)
{
switch (key) {
case ESC_KEY:
sl->esc = ESC;
break;
case CTRL_U:
sl_reset(sl);
break;
case CTRL_W: /* erase previous word */
while (sl->rcur != 0 && strchr(IS_WORD_BREAK, *(sl->ptr-1)) != NULL)
sl_backspace(sl);
while (sl->rcur != 0 && strchr(IS_WORD_BREAK, *(sl->ptr-1)) == NULL)
sl_backspace(sl);
break;
case BACKSPACE:
case VT_BACKSPACE:
sl_backspace(sl);
break;
default:
break;
}
}
static int
sl_esc(struct slackline *sl, int key)
{
/* handle escape sequences */
switch (sl->esc) {
case ESC_NONE:
break;
case ESC:
sl->esc = key == '[' ? ESC_BRACKET : ESC_NONE;
return 1;
case ESC_BRACKET:
switch (key) {
case 'A': /* up */
case 'B': /* down */
break;
case 'C': /* right */
sl_move(sl, RIGHT);
break;
case 'D': /* left */
sl_move(sl, LEFT);
break;
case 'H': /* Home */
sl_move(sl, HOME);
break;
case 'F': /* End */
sl_move(sl, END);
break;
case 'P': /* delete */
if (sl->rcur == sl->rlen)
break;
sl_move(sl, RIGHT);
sl_backspace(sl);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
sl->nummod = key;
sl->esc = ESC_BRACKET_NUM;
return 1;
}
sl->esc = ESC_NONE;
return 1;
case ESC_BRACKET_NUM:
switch(key) {
case '~':
switch(sl->nummod) {
case '1': /* Home */
case '7':
sl_move(sl, HOME);
break;
case '4': /* End */
case '8':
sl_move(sl, END);
break;
case '3': /* Delete */
if (sl->rcur == sl->rlen)
break;
sl_move(sl, RIGHT);
sl_backspace(sl);
break;
}
sl->esc = ESC_NONE;
return 1;
}
}
return 0;
}
int
sl_keystroke(struct slackline *sl, int key)
{
uint_least32_t cp;
if (sl == NULL || sl->rlen < sl->rcur)
return -1;
if (sl_esc(sl, key))
return 0;
if (!iscntrl((unsigned char) key))
goto compose;
switch (sl->mode) {
case SL_DEFAULT:
sl_default(sl, key);
break;
case SL_EMACS:
sl_default(sl, key);
sl_emacs(sl, key);
break;
case SL_VI:
/* TODO: implement vi-mode */
break;
}
return 0;
compose:
/* byte-wise composing of UTF-8 runes */
sl->ubuf[sl->ubuf_len++] = key;
if (grapheme_decode_utf8(sl->ubuf, sl->ubuf_len, &cp) > sl->ubuf_len ||
cp == GRAPHEME_INVALID_CODEPOINT)
return 0;
if (sl->blen + sl->ubuf_len >= sl->bufsize) {
char *nbuf;
if ((nbuf = realloc(sl->buf, sl->bufsize * 2)) == NULL)
return -1;
sl->ptr = nbuf + (sl->ptr - sl->buf);
sl->last = nbuf + (sl->last - sl->buf);
sl->buf = nbuf;
sl->bufsize *= 2;
}
/* add character to buffer */
if (sl->rcur < sl->rlen) { /* insert into buffer */
char *cur = sl_postoptr(sl, sl->rcur);
char *end = sl_postoptr(sl, sl->rlen);
char *ncur = cur + sl->ubuf_len;
memmove(ncur, cur, end - cur);
}
memcpy(sl_postoptr(sl, sl->rcur), sl->ubuf, sl->ubuf_len);
sl->ptr += sl->ubuf_len;
sl->last += sl->ubuf_len;
sl->bcur += sl->ubuf_len;
sl->blen += sl->ubuf_len;
sl->ubuf_len = 0;
sl->rcur++;
sl->rlen++;
*sl->last = '\0';
return 0;
}