Repository navigation
Expand file tree
/
Copy pathinput.c
More file actions
425 lines (406 loc) · 8.41 KB
/
Copy pathinput.c
File metadata and controls
425 lines (406 loc) · 8.41 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#define _XOPEN_SOURCE_EXTENDED 1
#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include <time.h>
#include <wchar.h>
#include <ncursesw/curses.h>
#include "undo.h"
#include "input.h"
#include "screen.h"
#include "text.h"
#include "menu.h"
#include "config.h"
int collecting_paste = 0;
/* globals from ee.c */
extern int in;
extern int scr_vert;
extern int scr_horz;
extern int horiz_offset;
extern int last_line;
extern int gold;
extern WINDOW *text_win;
extern ee_char *point;
/* helpers from ee.c */
char *get_string(const char *prompt, int advance);
void bottom(void);
void command_prompt(void);
void down(void);
void search_prompt(void);
void redo_action(void);
void bol(void);
void delete(int disp);
void insert_line(int disp);
void undo_action(void);
void left(int disp);
void insert(int character);
void move_rel(int direction, int lines);
void eol(void);
void top(void);
void up(void);
void undel_word(void);
void del_word(void);
int search(int display_message);
void del_line(void);
void undel_line(void);
extern struct menu_entries main_menu[];
void adv_word(void);
void adv_line(void);
void paint_info_win(void);
void midscreen(int line, ee_char *pnt);
static int read_seq(wint_t *seq, int len)
{
for (int i = 0; i < len; i++) {
if (wget_wch(text_win, &seq[i]) == ERR) {
for (int j = i - 1; j >= 0; j--)
unget_wch(seq[j]);
return 0;
}
}
return 1;
}
static int check_start_seq(void)
{
wint_t s[5];
if (!read_seq(s, 5))
return 0;
if (s[0] == '[' && s[1] == '2' && s[2] == '0' && s[3] == '0' && s[4] == '~')
return 1;
for (int i = 4; i >= 0; i--)
unget_wch(s[i]);
return 0;
}
static int check_end_seq(void)
{
wint_t s[5];
if (!read_seq(s, 5))
return 0;
if (s[0] == '[' && s[1] == '2' && s[2] == '0' && s[3] == '1' && s[4] == '~')
return 1;
for (int i = 4; i >= 0; i--)
unget_wch(s[i]);
return 0;
}
int collect_input_chunk(int **buf)
{
collecting_paste = 0;
size_t cap = 64;
int *p = malloc(cap * sizeof(int));
int len = 0;
wint_t ch;
int rc = wget_wch(text_win, &ch);
if (rc == ERR) {
free(p);
*buf = NULL;
return 0;
}
if (ch == 27) {
nodelay(text_win, TRUE);
if (check_start_seq()) {
undo_begin_chunk();
collecting_paste = 1;
cap = 1024;
p = realloc(p, cap * sizeof(int));
len = 0;
while (1) {
rc = wget_wch(text_win, &ch);
if (rc == ERR)
continue;
if (ch == 27 && check_end_seq()) {
nodelay(text_win, FALSE);
*buf = p;
return len;
}
if ((size_t)len >= cap) {
cap *= 2;
p = realloc(p, cap * sizeof(int));
}
p[len++] = (int)ch;
}
}
nodelay(text_win, FALSE);
}
p[len++] = (int)ch;
nodelay(text_win, TRUE);
struct timespec delay = {0, 30000000};
while (1) {
rc = wget_wch(text_win, &ch);
if (rc == ERR) {
nanosleep(&delay, NULL);
rc = wget_wch(text_win, &ch);
if (rc == ERR)
break;
}
if (ch == 27 && check_start_seq()) {
unget_wch('~');
unget_wch('0');
unget_wch('0');
unget_wch('2');
unget_wch('[');
unget_wch(27);
break;
}
if ((size_t)len >= cap) {
cap *= 2;
p = realloc(p, cap * sizeof(int));
}
p[len++] = (int)ch;
}
nodelay(text_win, FALSE);
*buf = p;
return len;
}
void start_action(void)
{
undo_begin_chunk();
}
void
control(void)
{
char *string;
if (in == 1) /* control a */
{
string = get_string(ascii_code_str, TRUE);
if (*string != '\0')
{
in = atoi(string);
wmove(text_win, scr_vert, (scr_horz - horiz_offset));
insert(in);
}
free(string);
}
else if (in == 2) /* control b */
bottom();
else if (in == 3) /* control c */
{
command_prompt();
}
else if (in == 4) /* control d */
down();
else if (in == 5) /* control e */
search_prompt();
else if (in == 6) /* control f */
redo_action();
else if (in == 7) /* control g */
bol();
else if (in == 8) /* control h */
delete(TRUE);
else if (in == 9) /* control i */
;
else if (in == 10) /* control j */
insert_line(TRUE);
else if (in == 11) /* control k */
undo_action();
else if (in == 12) /* control l */
left(TRUE);
else if (in == 13) /* control m */
insert_line(TRUE);
else if (in == 14) /* control n */
move_rel('d', max(5, (last_line - 5)));
else if (in == 15) /* control o */
eol();
else if (in == 16) /* control p */
move_rel('u', max(5, (last_line - 5)));
else if (in == 17) /* control q */
;
else if (in == 18) /* control r */
right(TRUE);
else if (in == 19) /* control s */
;
else if (in == 20) /* control t */
top();
else if (in == 21) /* control u */
up();
else if (in == 22) /* control v */
undel_word();
else if (in == 23) /* control w */
del_word();
else if (in == 24) /* control x */
search(TRUE);
else if (in == 25) /* control y */
del_line();
else if (in == 26) /* control z */
undel_line();
else if (in == 27) /* control [ (escape) */
{
menu_op(main_menu);
}
}
/*
| Emacs control-key bindings
*/
void
emacs_control(void)
{
char *string;
if (in == 1) /* control a */
bol();
else if (in == 2) /* control b */
left(TRUE);
else if (in == 3) /* control c */
{
command_prompt();
}
else if (in == 4) /* control d */
undo_action();
else if (in == 5) /* control e */
eol();
else if (in == 6) /* control f */
right(TRUE);
else if (in == 7) /* control g */
move_rel('u', max(5, (last_line - 5)));
else if (in == 8) /* control h */
delete(TRUE);
else if (in == 9) /* control i */
;
else if (in == 10) /* control j */
redo_action();
else if (in == 11) /* control k */
del_line();
else if (in == 12) /* control l */
undel_line();
else if (in == 13) /* control m */
insert_line(TRUE);
else if (in == 14) /* control n */
down();
else if (in == 15) /* control o */
{
string = get_string(ascii_code_str, TRUE);
if (*string != '\0')
{
in = atoi(string);
wmove(text_win, scr_vert, (scr_horz - horiz_offset));
insert(in);
}
free(string);
}
else if (in == 16) /* control p */
up();
else if (in == 17) /* control q */
;
else if (in == 18) /* control r */
undel_word();
else if (in == 19) /* control s */
;
else if (in == 20) /* control t */
top();
else if (in == 21) /* control u */
bottom();
else if (in == 22) /* control v */
move_rel('d', max(5, (last_line - 5)));
else if (in == 23) /* control w */
del_word();
else if (in == 24) /* control x */
search(TRUE);
else if (in == 25) /* control y */
search_prompt();
else if (in == 26) /* control z */
adv_word();
else if (in == 27) /* control [ (escape) */
{
menu_op(main_menu);
}
}
void
function_key(void)
{
if (in == KEY_LEFT)
left(TRUE);
else if (in == KEY_RIGHT)
right(TRUE);
else if (in == KEY_HOME)
bol();
else if (in == KEY_END)
eol();
else if (in == KEY_UP)
up();
else if (in == KEY_DOWN)
down();
else if (in == KEY_NPAGE)
move_rel('d', max( 5, (last_line - 5)));
else if (in == KEY_PPAGE)
move_rel('u', max(5, (last_line - 5)));
else if (in == KEY_DL)
del_line();
else if (in == KEY_DC)
undo_action();
else if (in == KEY_BACKSPACE)
delete(TRUE);
else if (in == KEY_IL)
{ /* insert a line before current line */
insert_line(TRUE);
left(TRUE);
}
else if (in == KEY_F(1))
gold = !gold;
else if (in == KEY_F(2))
{
if (gold)
{
gold = FALSE;
undel_line();
}
else
redo_action();
}
else if (in == KEY_F(3))
{
if (gold)
{
gold = FALSE;
undel_word();
}
else
del_word();
}
else if (in == KEY_F(4))
{
if (gold)
{
gold = FALSE;
paint_info_win();
midscreen(scr_vert, point);
}
else
adv_word();
}
else if (in == KEY_F(5))
{
if (gold)
{
gold = FALSE;
search_prompt();
}
else
search(TRUE);
}
else if (in == KEY_F(6))
{
if (gold)
{
gold = FALSE;
bottom();
}
else
top();
}
else if (in == KEY_F(7))
{
if (gold)
{
gold = FALSE;
eol();
}
else
bol();
}
else if (in == KEY_F(8))
{
if (gold)
{
gold = FALSE;
command_prompt();
}
else
adv_line();
}
}