-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.main.c_prev.tmp
354 lines (301 loc) · 12.8 KB
/
.main.c_prev.tmp
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
#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>
#include <sys/stat.h>
#include "editor_structs.h"
#include "gap_buffer.h"
#include "init_editor.h"
#include "editor_func.h"
#include "line.h"
#include "stack.c"
#include "features.h"
int main(int argc, char *argv[]) {
// if file name not entered
if(argc < 2) {
printf("Please Enter filename.\n");
return 0;
}
FILE *fd_store_prev, *fd_store_next, *fd_main;
win window_1;
// initialize window
init_window(&window_1, TOT_LINES_IN_WINDOW);
// init stack for undo operation
stack st;
init(&st);
// init codebase files
FILE *fd_cb_key, *fd_cb_data;
fd_cb_key = fopen(".codebase.key", "r+");
fd_cb_data = fopen(".codebase.data","r+");
int codebase_mode = 0;
// init codebase into trie
TrieNode_c *codebase = init_codebase(fd_cb_key);
int new_file = 0;
// check file corresponding to input
// filename is present or not
struct stat perm;
if(stat(argv[1], &perm) == -1)
new_file = 1;
// if file is directory, cannot open
if(S_ISDIR(perm.st_mode)) {
printf("Cannot Open Directory.\n");
return 0;
}
// check whether file has write permission or not
int read_only = 0;
if(! (perm.st_mode & S_IWUSR))
read_only = 1;
// if file not present create new with 'read write' permission
if(new_file)
fd_main = fopen(argv[1], "w+");
// else load file into data structure
else
fd_main = load_file(&window_1, argv[1]);
char prev_file[50] = ".";
char next_file[50] = ".";
// generate tmp filenames, filename_prev.tmp and filename_next.tmp
strcat(strcat(prev_file, argv[1]), "_prev.tmp");
strcat(strcat(next_file, argv[1]), "_next.tmp");
// open tmp files
fd_store_prev = fopen(prev_file, "w+");
fd_store_next = fopen(next_file, "w+");
// curses interface
initscr();
noecho();
keypad(stdscr, true);
// init colors for syntax hilighting
init_colors();
TrieNode *keyword = init_keywords();
// init shortcut keys
char **shortcut_key = init_shortcut_keys();
int ch;
int win_line = 0, win_col = 0, line_no = 0;
int pos_changed = 0;
// print whole page at start
print_page(window_1, keyword);
if(read_only)
print_ReadOnly();
// print co ordinates
print_loc(line_no, win_col);
// move cursor
move(line_no, win_col);
refresh();
int cnt = 0;
while(true) {
// take user input
ch = getch();
cnt++;
int move_left = 0;
char start_bracket, end_bracket;
// check if input is any shortcut key
int sk_index = shortcut_key_indx(&ch, &move_left);
// check if input key is bracket
check_bracket(&ch, &start_bracket, &end_bracket);
int up = 1, down = 1;
if(ch == KEY_NPAGE) {
ch = KEY_DOWN;
down = window_1.tot_lines-1;
line_no += window_1.tot_lines-1 - win_line;
win_line = window_1.tot_lines-1;
int h_indx = head_index(window_1, win_line);
if(win_col > (window_1.head)[h_indx].line_size)
win_col = (window_1.head)[h_indx].line_size;
}
else if(ch == KEY_PPAGE) {
ch = KEY_UP;
up = window_1.tot_lines-1;
line_no -= win_line;
win_line = 0;
int h_indx = head_index(window_1, win_line);
if(win_col > (window_1.head)[h_indx].line_size)
win_col = (window_1.head)[h_indx].line_size;
}
switch(ch) {
// quit without save
case 'q':
// close all files
fclose(fd_main);
fclose(fd_store_prev);
fclose(fd_store_next);
fclose(fd_cb_key);
fclose(fd_cb_data);
// remove all extra files
if(new_file)
remove(argv[1]);
remove(prev_file);
remove(next_file);
// end ncurses interface
endwin();
return 0;
// save and quit
case CTRL('y'):
goto SAVE;
// undo function
case KEY_F(4):
undo(&st, &window_1, &line_no, &win_line, &win_col, fd_store_prev, fd_store_next, fd_main);
print_page(window_1, keyword);
break;
// left arrow key
case KEY_LEFT:
// decrese column no if not 0
pos_changed = 1;
if(win_col)
win_col--;
break;
// right arrow key
case KEY_RIGHT:
// increase column no if cursor not at end of line
pos_changed = 1;
if(win_col < (window_1.head)[head_index(window_1, win_line)].line_size)
win_col++;
break;
// down arrow key
case KEY_DOWN:
for(int k = 0; k < down; k++) {
pos_changed = 1;
if(win_line < window_1.tot_lines - 1) {
// increase line no
win_line++;
line_no++;
int h_indx = head_index(window_1, win_line);
// update column no
if(win_col > (window_1.head)[h_indx].line_size)
win_col = (window_1.head)[h_indx].line_size;
if((window_1.head)[h_indx].line.curr_line[0] == MAX_CHAR) {
win_line--;
line_no--;
}
}
// if down arrow at bottom of window, load next line
else {
// load next line
int check = load_next_line(&window_1, fd_store_prev, fd_store_next, fd_main);
// if next line successfully loaded
if(check == SUCCESS) {
line_no++;
int h_indx = head_index(window_1, win_line);
// adjust column number
// adjust column no
if(win_col > (window_1.head)[h_indx].line_size)
win_col = (window_1.head)[h_indx].line_size;
// store info in stack for undo function
store_info(&st, 0, ch, LOAD_NEXT_LINE, win_line, win_col);
}
// print updated page on screen
print_page(window_1, keyword);
}
}
break;
// up arrow key
case KEY_UP:
for(int k = 0; k < up; k++) {
pos_changed = 1;
// if curr line not first
if(win_line > 0) {
// decrease line no
win_line--;
line_no--;
// adjust column no
int h_indx = head_index(window_1, win_line);
if(win_col > (window_1.head)[h_indx].line_size)
win_col = (window_1.head)[h_indx].line_size;
}
// if up arrow pressed at top of window, load prev line
else {
int check = load_prev_line(&window_1, fd_store_prev, fd_store_next);
// if prev line successfully loaded
if(check == SUCCESS) {
// update line no and column no
line_no--;
int h_indx = head_index(window_1, win_line);
if(win_col > (window_1.head)[h_indx].line_size)
win_col = (window_1.head)[h_indx].line_size;
// store info in stack
store_info(&st, 0, ch, LOAD_PREV_LINE, win_line, win_col);
}
// print updated page on screen
print_page(window_1, keyword);
}
}
break;
case KEY_BACKSPACE: {
if(read_only)
goto READ_ONLY;
// at start of file, do nothing
if(line_no == 0 && win_col == 0)
continue;
char operation = DEL_CHAR;
if(win_col == 0) {
operation = DEL_LINE;
line_no--;
}
char data = del_from_pos(&window_1, &win_line, &win_col, fd_store_prev, fd_store_next, fd_main);
store_info(&st, pos_changed, data, operation, win_line, win_col);
pos_changed = 0;
if(operation == DEL_LINE)
print_page(window_1, keyword);
else
print_line(window_1, win_line, keyword);
break;
}
// enter key
case '\n':
if(read_only)
goto READ_ONLY;
line_no++;
insert_new_line_at_pos(&window_1, &win_line, &win_col, fd_store_prev, fd_store_next, fd_main);
store_info(&st, pos_changed, ch, INSERT_NEW_LINE, win_line, win_col);
pos_changed = 0;
// print updated page on screen
print_page(window_1, keyword);
break;
case '\t': {
if(read_only)
goto READ_ONLY;
int h_indx = head_index(window_1, win_line);
for(int i = 0; i < TAB_SPACE; i++) {
(window_1.head)[h_indx].line_size++;
insert_at_pos(&((window_1.head)[h_indx].line), win_col++, ' ');
store_info(&st, pos_changed, ch, INSERT_CHAR, win_line, win_col);
if(pos_changed)
pos_changed = 0;
}
print_line(window_1, win_line, keyword);
break;
}
// insert bracket with pair
case BRACKET: {
if(read_only)
goto READ_ONLY;
// insert bracket with pair
int h_indx = head_index(window_1, win_line);
(window_1.head)[h_indx].line_size += 2;
insert_at_pos(&((window_1.head)[h_indx].line), win_col++, start_bracket);
insert_at_pos(&((window_1.head)[h_indx].line), win_col++, end_bracket);
store_info(&st, pos_changed, ch, INSERT_CHAR, win_line, win_col);
win_col--;
pos_changed = 0;
print_line(window_1, win_line, keyword);
break;
}
// insert ; at end
case CTRL(';'): {
int h_indx = head_index(window_1, win_line);
insert_at_pos(&((window_1.head)[h_indx].line), (window_1.head)[h_indx].line_size++, ';');
print_line(window_1, win_line, keyword);
break;
}
// move to start of line
case CTRL('h'):
pos_changed = 1;
win_col = 0;
break;
// move to end of line
case CTRL('l'):
pos_changed = 1;
win_col = (window_1.head)[head_index(window_1, win_line)].line_size;
break;
// move to top line of window
case CTRL('o'): {
pos_changed = 1;
line_no -= win_line;
win_line = 0;