-
Notifications
You must be signed in to change notification settings - Fork 25
/
command.c
356 lines (314 loc) · 8.56 KB
/
command.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
/* command.c, Atto Emacs, Public Domain, Hugh Barney, 2016, Derived from: Anthony's Editor January 93 */
#include "header.h"
void quit() { done = 1; }
void up() { curbp->b_point = lncolumn(curbp, upup(curbp, curbp->b_point),curbp->b_col); }
void down() { curbp->b_point = lncolumn(curbp, dndn(curbp, curbp->b_point),curbp->b_col); }
void lnbegin() { curbp->b_point = segstart(curbp, lnstart(curbp,curbp->b_point), curbp->b_point); }
void version() { msg(VERSION); }
void top() { curbp->b_point = 0; }
void bottom() { curbp->b_point = pos(curbp, curbp->b_ebuf); if (curbp->b_epage < pos(curbp, curbp->b_ebuf)) curbp->b_reframe = 1;}
void block() { curbp->b_mark = curbp->b_point; }
void copy() { copy_cut(FALSE); }
void cut() { copy_cut(TRUE); }
void resize_terminal() { one_window(curwp); }
void quit_ask()
{
if (modified_buffers() > 0) {
mvaddstr(MSGLINE, 0, "Modified buffers exist; really exit (y/n) ?");
clrtoeol();
if (!yesno(FALSE))
return;
}
quit();
}
/* flag = default answer, FALSE=n, TRUE=y */
int yesno(int flag)
{
int ch;
addstr(flag ? " y\b" : " n\b");
refresh();
ch = getch();
if (ch == '\r' || ch == '\n')
return (flag);
return (tolower(ch) == 'y');
}
void redraw()
{
window_t *wp;
clear();
for (wp=wheadp; wp != NULL; wp = wp->w_next)
wp->w_update = TRUE;
update_display();
}
void left()
{
int n = prev_utf8_char_size();
while (0 < curbp->b_point && n-- > 0)
--curbp->b_point;
}
void right()
{
int n = utf8_size(*ptr(curbp,curbp->b_point));
while ((curbp->b_point < pos(curbp, curbp->b_ebuf)) && n-- > 0)
++curbp->b_point;
}
/* work out number of bytes based on first byte */
int utf8_size(char_t c)
{
if (c >= 192 && c < 224) return 2;
if (c >= 224 && c < 240) return 3;
if (c >= 240 && c < 248) return 4;
return 1; /* if in doubt it is 1 */
}
int prev_utf8_char_size()
{
int n;
for (n=2;n<5;n++)
if (-1 < curbp->b_point - n && (utf8_size(*(ptr(curbp, curbp->b_point - n))) == n))
return n;
return 1;
}
void lnend()
{
if (curbp->b_point == pos(curbp, curbp->b_ebuf)) return; /* do nothing if EOF */
curbp->b_point = dndn(curbp, curbp->b_point);
point_t p = curbp->b_point;
left();
curbp->b_point = (*ptr(curbp, curbp->b_point) == '\n') ? curbp->b_point : p;
}
void wleft()
{
char_t *p;
while (!isspace(*(p = ptr(curbp, curbp->b_point))) && curbp->b_buf < p)
--curbp->b_point;
while (isspace(*(p = ptr(curbp, curbp->b_point))) && curbp->b_buf < p)
--curbp->b_point;
}
void pgdown()
{
curbp->b_page = curbp->b_point = upup(curbp, curbp->b_epage);
while (0 < curbp->b_row--)
down();
curbp->b_epage = pos(curbp, curbp->b_ebuf);
}
void pgup()
{
int i = curwp->w_rows;
while (0 < --i) {
curbp->b_page = upup(curbp, curbp->b_page);
up();
}
}
void wright()
{
char_t *p;
while (!isspace(*(p = ptr(curbp, curbp->b_point))) && p < curbp->b_ebuf)
++curbp->b_point;
while (isspace(*(p = ptr(curbp, curbp->b_point))) && p < curbp->b_ebuf)
++curbp->b_point;
}
void insert()
{
assert(curbp->b_gap <= curbp->b_egap);
if (curbp->b_gap == curbp->b_egap && !growgap(curbp, CHUNK))
return;
curbp->b_point = movegap(curbp, curbp->b_point);
/* overwrite if mid line, not EOL or EOF, CR will insert as normal */
if ((curbp->b_flags & B_OVERWRITE) && *input != '\r' && *(ptr(curbp, curbp->b_point)) != '\n' && curbp->b_point < pos(curbp,curbp->b_ebuf) ) {
*(ptr(curbp, curbp->b_point)) = *input;
if (curbp->b_point < pos(curbp, curbp->b_ebuf))
++curbp->b_point;
} else {
*curbp->b_gap++ = *input == '\r' ? '\n' : *input;
curbp->b_point = pos(curbp, curbp->b_egap);
// force reframe if scrolled off bottom of screen and at EOF
if (curbp->b_point == pos(curbp, curbp->b_ebuf) && curbp->b_point >= curbp->b_epage) curbp->b_reframe = 1;
}
curbp->b_flags |= B_MODIFIED;
}
void backsp()
{
curbp->b_point = movegap(curbp, curbp->b_point);
if (curbp->b_buf < curbp->b_gap) {
curbp->b_gap -= prev_utf8_char_size();
curbp->b_flags |= B_MODIFIED;
}
curbp->b_point = pos(curbp, curbp->b_egap);
}
void delete()
{
curbp->b_point = movegap(curbp, curbp->b_point);
if (curbp->b_egap < curbp->b_ebuf) {
curbp->b_egap += utf8_size(*curbp->b_egap);
curbp->b_point = pos(curbp, curbp->b_egap);
curbp->b_flags |= B_MODIFIED;
}
}
void gotoline()
{
int line;
point_t p;
if (getinput("Goto line: ", temp, STRBUF_S, F_CLEAR)) {
line = atoi(temp);
p = line_to_point(line);
if (p != -1) {
curbp->b_point = p;
msg("Line %d", line);
} else {
msg("Line %d, not found", line);
}
}
}
void insertfile()
{
if (getfilename("Insert file: ", temp, NAME_MAX))
(void)insert_file(temp, TRUE);
}
void readfile()
{
buffer_t *bp;
temp[0] = '\0';
int result = getfilename("Find file: ", (char*)temp, NAME_MAX);
/* int result = getinput("Find file: ", (char*)temp, NAME_MAX, F_CLEAR); */
if (result) {
bp = find_buffer(temp, TRUE);
disassociate_b(curwp); /* we are leaving the old buffer for a new one */
curbp = bp;
associate_b2w(curbp, curwp);
/* load the file if not already loaded */
if (bp != NULL && bp->b_fname[0] == '\0') {
if (!load_file(temp)) {
msg("New file %s", temp);
}
strncpy(curbp->b_fname, temp, NAME_MAX);
curbp->b_fname[NAME_MAX] = '\0'; /* truncate if required */
}
}
}
void savebuffer()
{
if (curbp->b_fname[0] != '\0') {
save(curbp->b_fname);
return;
} else {
writefile();
}
refresh();
}
void writefile()
{
strncpy(temp, curbp->b_fname, NAME_MAX);
if (getinput("Write file: ", temp, NAME_MAX, F_NONE))
if (save(temp) == TRUE)
strncpy(curbp->b_fname, temp, NAME_MAX);
}
void killbuffer()
{
buffer_t *kill_bp = curbp;
buffer_t *bp;
int bcount = count_buffers();
/* do nothing if only buffer left is the scratch buffer */
if (bcount == 1 && 0 == strcmp(get_buffer_name(curbp), "*scratch*"))
return;
if (curbp->b_flags & B_MODIFIED) {
mvaddstr(MSGLINE, 0, "Discard changes (y/n) ?");
clrtoeol();
if (!yesno(FALSE))
return;
}
if (bcount == 1) {
/* create a scratch buffer */
bp = find_buffer("*scratch*", TRUE);
strcpy(bp->b_bname, "*scratch*");
}
next_buffer();
assert(kill_bp != curbp);
delete_buffer(kill_bp);
}
void iblock()
{
block();
msg("Mark set");
}
void toggle_overwrite_mode() {
if (curbp->b_flags & B_OVERWRITE)
curbp->b_flags &= ~B_OVERWRITE;
else
curbp->b_flags |= B_OVERWRITE;
}
void killtoeol()
{
if (curbp->b_point == pos(curbp, curbp->b_ebuf))
return; /* do nothing if at end of file */
if (*(ptr(curbp, curbp->b_point)) == 0xa) {
delete(); /* delete CR if at start of empty line */
} else {
curbp->b_mark = curbp->b_point;
lnend();
if (curbp->b_mark != curbp->b_point) copy_cut(TRUE);
}
}
void copy_cut(int cut)
{
char_t *p;
/* if no mark or point == marker, nothing doing */
if (curbp->b_mark == NOMARK || curbp->b_point == curbp->b_mark)
return;
if (scrap != NULL) {
free(scrap);
scrap = NULL;
}
if (curbp->b_point < curbp->b_mark) {
/* point above marker: move gap under point, region = marker - point */
(void) movegap(curbp, curbp->b_point);
p = ptr(curbp, curbp->b_point);
nscrap = curbp->b_mark - curbp->b_point;
} else {
/* if point below marker: move gap under marker, region = point - marker */
(void) movegap(curbp, curbp->b_mark);
p = ptr(curbp, curbp->b_mark);
nscrap = curbp->b_point - curbp->b_mark;
}
if ((scrap = (char_t*) malloc(nscrap)) == NULL) {
msg("No more memory available.");
} else {
(void) memcpy(scrap, p, nscrap * sizeof (char_t));
if (cut) {
curbp->b_egap += nscrap; /* if cut expand gap down */
curbp->b_point = pos(curbp, curbp->b_egap); /* set point to after region */
curbp->b_flags |= B_MODIFIED;
msg("%ld bytes cut.", nscrap);
} else {
msg("%ld bytes copied.", nscrap);
}
curbp->b_mark = NOMARK; /* unmark */
}
}
void paste()
{
if(curbp->b_flags & B_OVERWRITE)
return;
if (nscrap <= 0) {
msg("Scrap is empty. Nothing to paste.");
} else if (nscrap < curbp->b_egap - curbp->b_gap || growgap(curbp, nscrap)) {
curbp->b_point = movegap(curbp, curbp->b_point);
memcpy(curbp->b_gap, scrap, nscrap * sizeof (char_t));
curbp->b_gap += nscrap;
curbp->b_point = pos(curbp, curbp->b_egap);
curbp->b_flags |= B_MODIFIED;
}
}
void showpos()
{
int current, lastln;
point_t end_p = pos(curbp, curbp->b_ebuf);
get_line_stats(¤t, &lastln);
if (curbp->b_point == end_p) {
msg("[EOB] Line = %d/%d Point = %d/%d", current, lastln,
curbp->b_point, ((curbp->b_ebuf - curbp->b_buf) - (curbp->b_egap - curbp->b_gap)));
} else {
msg("Char = %s 0x%x Line = %d/%d Point = %d/%d", unctrl(*(ptr(curbp, curbp->b_point))), *(ptr(curbp, curbp->b_point)),
current, lastln,
curbp->b_point, ((curbp->b_ebuf - curbp->b_buf) - (curbp->b_egap - curbp->b_gap)));
}
}