-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyhandler.c
393 lines (358 loc) · 9.5 KB
/
keyhandler.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
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
/**
* keyhandler.c
*
* All key handling and reading functions go here
* Part of the TAV text editor
*
* Author: Abhishek Shrivastav <[email protected]>
*/
#include "helper.h"
/*
* This function is used to handle all the keypresses.
*
* Depending on the mode and keypress this function will call respective
* routines
*
*/
void readKey(FILE* h)
{
if (strcmp(g_tavProps.mode, NORMAL_MODE) == 0)
readKeyNormal();
else
{
g_tavProps.is_mod = 1;
char c;
if (h == 0)
c = getc(stdin);
else
c = getc(h);
int cur_col = g_tavProps.cursor_col;
int cur_seq_len = g_tavProps.current_seq -> len;
if (c == ESCAPE)
handleEscapeSequence();
else if (c == '\t')
{
for (int i = 0; i < TAB_LENGTH; i++)
{
insert(g_tavProps.current_seq, cur_col, 1, ' ');
modify_seq_len(1);
modify_cur_pos(0, 1, 1);
}
}
else if (c == '\r')
{
/* Enter also has three cases
* 1. Enter at line ending
* 2. Enter at line start
* 3. Enter at line middle
*
* Have to update all the corresponding row numbers correctly!!
*/
// below condition means that we are at the row end
sequence* new_seq = malloc(sizeof(sequence));
new_seq -> prev = (struct sequence *) g_tavProps.current_seq;
new_seq -> next = g_tavProps.current_seq -> next;
new_seq -> seq_row = g_tavProps.current_seq -> seq_row + 1;
updateRowNumber(g_tavProps.current_seq -> next, 1);
if (cur_col == cur_seq_len)
{
new_seq -> len = 0;
new_seq -> max_len = LINE_SIZE;
new_seq -> data = calloc(LINE_SIZE , sizeof(char));
}
else
{
/*
* since we are pressing enter at the line middle/start its possible that
* the remaining line length > LINE_SIZE. Need to allocate memory as
* required.
*/
new_seq -> len = g_tavProps.current_seq -> len - cur_col;
new_seq -> max_len = new_seq -> len + LINE_SIZE;
new_seq -> data = calloc(new_seq -> max_len , sizeof(char));
// syntax is destination, source, amount to copy
memcpy(new_seq -> data, g_tavProps.current_seq -> data + cur_col, new_seq-> len);
g_tavProps.current_seq -> len -= new_seq -> len;
g_tavProps.current_seq -> data[cur_col] = '\0';
}
if (g_tavProps.current_seq -> next != NULL)
g_tavProps.current_seq -> next -> prev = new_seq;
g_tavProps.current_seq -> next = (struct sequence *) new_seq;
g_tavProps.current_seq = new_seq;
g_tavProps.act_rows++; // increase the number of active rows by 1
modify_cur_pos(LF, CR, 1);
}
else if (c == BACKSPACE)
{
// handle backspace here, similar to enter at line end
if (cur_col == cur_seq_len)
g_tavProps.current_seq -> data[cur_seq_len-1] = '\0';
else
insert(g_tavProps.current_seq, cur_col-1, 1, '\0');
modify_seq_len(-1);
modify_cur_pos(0, -1, 1);
}
else
{
// append the read character to the end of currrent sequence or the cursor
// pos?
// below condition means that we are inserting at the row end
if (cur_col == cur_seq_len)
{
g_tavProps.current_seq -> data[cur_seq_len] = c;
g_tavProps.current_seq -> data[cur_seq_len + 1] = '\0';
}
else
insert(g_tavProps.current_seq, cur_col, 1, c);
modify_seq_len(1);
modify_cur_pos(0, 1, 1);
}
}
}
/*
* This method is used to readKey in normal mode
*
*/
void readKeyNormal(void)
{
char c;
c = getc(stdin);
switch (c)
{
case 'i':
case 'I':
g_tavProps.mode = INSERT_MODE;
break;
case ESCAPE:
handleEscapeSequence();
break;
case 'h':
case 'H':
// go left
modify_cur_pos(0, -1, 0);
break;
case 'k':
case 'K':
// go up
modify_cur_pos(-1, 0, 0);
break;
case 'j':
case 'J':
// go down
modify_cur_pos(1, 0, 0);
break;
case 'l':
case 'L':
// go right;
modify_cur_pos(0, 1, 0);
break;
case 'r':
replace();
break;
case 'G':
gHandler(1);
break;
case 'g':
c = getchar();
if (c == 'g')
gHandler(-1);
break;
case ':':
interpretCommand();
break;
}
}
/*
* This method is used to insert the text in between a sequence
* syntax for memmove was inspired by a stackoverflow post
*
* @param s: The sequence in which we want to insert
* @param make_room_at: position in which we want to insert
* @param room_to_make: amount of room to make
* @param toInsert: char value to insert, '\0' indicates a backspace
*/
void insert(sequence* s, int make_room_at, int room_to_make, char toInsert)
{
char* base = s -> data;
// max length of the sequence
int len = s -> max_len;
if (toInsert == '\0')
{
memmove (
base + make_room_at,
base + make_room_at + 1,
len - (make_room_at + room_to_make)
);
}
else
{
memmove (
base + make_room_at + room_to_make,
base + make_room_at,
len - (make_room_at + room_to_make)
);
base[make_room_at] = toInsert;
}
}
/*
* This method is used to modify the value of cursor positions
*
* It also handles the cases related to invalid positions
* Basically the cursor should not be able to move to invalid locations using
* arrow keys.
*
* @param row: value by which to modify the row +- 1
* @param col: value by which to modify the col +- 1
* @param KorA: 1- means called by keypress, 0- means called by arrow
*/
void modify_cur_pos(int row, int col, int KorA)
{
int col_limit = g_tavProps.current_seq -> len;
int row_limit = g_tavProps.act_rows;
// if we are doing a arrow movement. make sure that its a valid movement
// else we return
if (KorA == 0)
{
if (g_tavProps.cursor_row + row >= row_limit || g_tavProps.cursor_col + col > col_limit)
return;
}
// if enter was pressed as a keypress
if (row == LF)
{
g_tavProps.cursor_row += 1;
g_tavProps.actual_row += 1; //
if (col == CR)
g_tavProps.cursor_col = 0;
}
else
{
// below statements execute when there has been a keypress or a valid
// arrow movement was detected
g_tavProps.cursor_row += row;
g_tavProps.actual_row += row;
g_tavProps.cursor_col += col;
if (g_tavProps.actual_row >= g_tavProps.w_row - 1)
g_tavProps.actual_row = g_tavProps.w_row - 2;
// code for updating the current_seq as well
if (row != 0)
{
if (row == -1)
{
if (g_tavProps.current_seq -> prev != NULL)
g_tavProps.current_seq = (sequence *) g_tavProps.current_seq -> prev;
}
else
{
if (g_tavProps.current_seq -> next != NULL)
g_tavProps.current_seq = (sequence *) g_tavProps.current_seq -> next;
}
col_limit = g_tavProps.current_seq -> len;
}
}
// we don't want to go to the negative rows
if (g_tavProps.cursor_row < 0)
{
g_tavProps.cursor_row = 0;
g_tavProps.actual_row = 0;
}
// cursor values < 0 make no sense
if (g_tavProps.cursor_col < 0)
g_tavProps.cursor_col = 0;
// cursor values > col_limit are invalid
if (g_tavProps.cursor_col > col_limit)
g_tavProps.cursor_col = col_limit;
// below if conditions change the start line and endline values of the
// document on basis of the 'row value' of the current sequence being
// pointed at.
if (g_tavProps.current_seq -> seq_row >= g_tavProps.end_line)
{
g_tavProps.start_line++;
g_tavProps.end_line++;
}
// removed '=' because don't want to scroll up when typing on the topmost
// line
if (g_tavProps.current_seq -> seq_row < g_tavProps.start_line)
{
g_tavProps.start_line--;
g_tavProps.end_line--;
}
}
/*
* This method is used to either increment or decrement the current seq length
*
* @param val: Value by which to change the sequence length
*/
void modify_seq_len(int val)
{
g_tavProps.current_seq -> len += val;
if (g_tavProps.current_seq -> len < 0)
g_tavProps.current_seq -> len = 0;
// if the array is almost about to exceed the size, reallocate
int m_len = g_tavProps.current_seq -> max_len;
if (g_tavProps.current_seq -> len > m_len - 10)
{
g_tavProps.current_seq -> data = (char *) realloc(g_tavProps.current_seq -> data, m_len * 2 * sizeof(char));
g_tavProps.current_seq -> max_len = m_len * 2;
}
}
/*
* This function will handle the escape sequence of keys
*/
void handleEscapeSequence(void)
{
/*
* Since every escape sequence is basically three characters
* First has already been read
*/
/* asm("int $3"); */
int c;
c = getc(stdin);
if (c == '[')
{
int key = getc(stdin);
switch(key)
{
case 'A':
key = UP;
break;
case 'B':
key = DOWN;
break;
case 'C':
key = RIGHT;
break;
case 'D':
key = LEFT;
break;
case '3':
key = DELETE;
break;
default:
key = DELETE;
break;
}
if (key != 0)
{
switch (key)
{
case UP:
modify_cur_pos(-1, 0, 0);
break;
case DOWN:
modify_cur_pos(1, 0, 0);
break;
case LEFT:
modify_cur_pos(0, -1, 0);
break;
case RIGHT:
modify_cur_pos(0, 1, 0);
break;
case DELETE:
c = getc(stdin);
break;
}
}
}
else if (c == ESCAPE)
g_tavProps.mode = NORMAL_MODE;
}