-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcmd.c
executable file
·334 lines (295 loc) · 6.42 KB
/
cmd.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
#include "cmd.h"
// Couple of private functions
static void get_input(editor_t* e, char* input, const char* cmd) {
// This function puts a string in a ncurses window, add moves the
// cursor as well
// Here we are creating a vim like "command prompt"
mvwaddstr(e->cmd_win.win, 0,0, ":");
// Refresh the window
wrefresh(e->cmd_win.win);
// Display the user's input
echo();
// Get a string from a ncurses window
wgetstr(e->cmd_win.win, input);
// The whole "echo" and "noecho" are used to tell if ncurses should
// "echo" or display what the user typed in. Since we usually handle
// all the input manually, we turn it off here
noecho();
// Clear the command window
wclear(e->cmd_win.win);
wrefresh(e->cmd_win.win);
}
static int run_cmd(editor_t* e, const char* cmd, const char* arg) {
// Save the file
if(strcmp(cmd, "w") == 0) {
return buffer_write(e->buf, arg);
}
// Quit (close) the current buffer
if(strcmp(cmd, "q") == 0) {
// If this is the only buffer, then quit the app
if (e->buf->next == NULL && e->buf->prev == NULL) {
q_flag = 1;
return 1;
}
return del_buffer(e, 0);
}
// Close all buffers
if(strcmp(cmd, "qa") == 0) {
// Just set the flag, the term function will take care of
// this for us
q_flag = 1;
}
// Edit or a new file (buffer replace)
if(strcmp(cmd, "edit") == 0) {
if (arg) {
free(e->buf->name);
e->buf->name = strdup(arg);
}
return buffer_read(e->buf, arg);
}
// Open a file in a new buffer
if(strcmp(cmd, "open") == 0) {
if (arg) {
new_buffer(e, 0);
free(e->buf->name);
e->buf->name = strdup(arg);
return buffer_read(e->buf, NULL);
}
}
// bad argument
return -1;
}
int command(editor_t* e, int key) {
char input[BUFSIZ] = {};
get_input(e, input, NULL);
if (strlen(input) == 0) {
return -1;
}
// Parse the command and then run it
char* cmd = strtrim(input);
char* arg = strchr(cmd, ' ');
if (arg) {
*arg = '\0';
arg = arg + 1;
}
return run_cmd(e, cmd, arg);
}
int read_file(editor_t* e, int key) {
new_buffer(e, key);
char input[BUFSIZ] = {};
get_input(e, input, NULL);
// User didn't put anything in
if (strlen(input) == 0) {
return -1;
} else {
// Let read the given file into the buffer
e->buf->name = strdup(input);
return buffer_read(e->buf, NULL);
}
}
int write_file(editor_t* e, int key) {
if(!e->buf->name) {
char input[BUFSIZ] = {};
get_input(e, input, NULL);
// User didn't put anything in
if (strlen(input) == 0) {
return -1;
} else {
// Set the name
e->buf->name = strdup(input);
}
}
return buffer_write(e->buf, NULL);
}
int beg_of_line(editor_t* e, int key) {
e->buf->col = 0;
return 1;
}
int end_of_line(editor_t* e, int key) {
e->buf->col = strlen(e->buf->line->str);
return 1;
}
int next_char(editor_t* e, int key) {
// If the next character is on the same line
if(e->buf->col + 1 <= strlen(e->buf->line->str)) {
e->buf->col++;
return 1;
} else if (e->buf->line->next) {
// Otherwise move to next line
next_line(e, key);
beg_of_line(e, key);
return 1;
}
return 0;
}
int prev_char(editor_t* e, int key) {
if(e->buf->col + 1 > 0) {
e->buf->col--;
return 1;
} else if (e->buf->line->prev) {
prev_line(e, key);
end_of_line(e, key);
return 1;
}
return 0;
}
int next_word(editor_t* e, int key) {
// Basically, find the next space and move there
while(next_char(e, key)) {
char ch = e->buf->line->str[e->buf->col];
if(!isblank(ch) && ch != '\0') {
if (e->buf->col == 0) {
return 1;
} else {
char pr_char = e->buf->line->str[e->buf->col - 1];
if (isblank(pr_char)) {
return 1;
}
}
}
}
return 0;
}
int prev_word(editor_t* e, int key) {
while(prev_char(e, key)) {
char ch = e->buf->line->str[e->buf->col];
if(!isblank(ch) && ch != '\0') {
if (e->buf->col == 0) {
return 1;
} else {
char pr_char = e->buf->line->str[e->buf->col - 1];
if (isblank(pr_char)) {
return 1;
}
}
}
}
return 0;
}
int next_line(editor_t* e, int key) {
line_t* next_l = e->buf->line->next;
int ret_val = 0;
if(next_l) {
e->buf->line = next_l;
e->buf->row++;
ret_val = 1;
}
if (e->buf->col > strlen(e->buf->line->str)) {
end_of_line(e, key);
}
return ret_val;
}
int prev_line(editor_t* e, int key) {
line_t* prev_l = e->buf->line->prev;
int ret_val = 0;
if(prev_l) {
e->buf->line = prev_l;
e->buf->row--;
ret_val = 1;
}
if (e->buf->col > strlen(e->buf->line->str)) {
end_of_line(e, key);
}
return ret_val;
}
int new_line(editor_t* e, int key) {
char* split = e->buf->line->str + e->buf->col;
size_t len = strlen(split);
line_t* l = line_create(split, len);
memset(split, '\0', len);
if(e->buf->line->next) {
buffer_insert(e->buf, e->buf->line->next, l);
} else {
buffer_append(e->buf, l);
}
next_line(e, key);
beg_of_line(e, key);
return 1;
}
int back_char(editor_t* e, int key) {
line_t* l = e->buf->line;
prev_char(e, key);
if (e->buf->line == l->prev) {
line_concat(e->buf->line, l->str, strlen(l->str));
buffer_erase(e->buf, l);
} else {
line_delete(e->buf->line, e->buf->col);
}
return 1;
}
int del_char(editor_t* e, int key) {
line_delete(e->buf->line, e->buf->col);
return 1;
}
int add_char(editor_t* e, int key) {
line_insert(e->buf->line, key, e->buf->col);
next_char(e, key);
return 1;
}
int add_tab(editor_t* e, int key) {
int i;
for(i = 0; i < 4; i++) {
add_char(e, ' ');
}
return 1;
}
int new_buffer(editor_t* e, int key) {
buffer_t* buf = buffer_create(NULL);
buf->next = e->buf;
buf->prev = e->buf->prev;
if (buf->next) {
buf->next->prev = buf;
}
if (buf->prev) {
buf->prev->next = buf;
}
if (e->beg == e->buf) {
e->beg = buf;
}
e->buf = buf;
return 1;
}
int del_buffer(editor_t* e, int key) {
buffer_t* buf = e->buf;
if (buf == e->beg) {
e->beg = buf->next;
} else {
buf->prev->next = buf->next;
}
if(buf == e->end) {
e->end = buf->prev;
} else {
buf->next->prev = buf->prev;
}
e->buf = buf->next ?: e->beg;
buffer_free(buf);
return 1;
}
int next_buffer(editor_t* e, int key) {
if (e->buf->next) {
e->buf = e->buf->next;
} else {
e->buf = e->beg;
}
return 1;
}
int prev_buffer(editor_t* e, int key){
if (e->buf->prev) {
e->buf = e->buf->prev;
} else {
e->buf = e->end;
}
return 1;
}
int beg_of_buffer(editor_t* e, int key){
e->buf->line = e->buf->beg;
e->buf->row = 0;
beg_of_line(e, key);
return 1;
}
int end_of_buffer(editor_t* e, int key){
e->buf->line = e->buf->end;
e->buf->row = e->buf->size - 1;
end_of_line(e, key);
return 1;
}