-
Notifications
You must be signed in to change notification settings - Fork 2
/
tab.c
374 lines (334 loc) · 8.91 KB
/
tab.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
#include <glob.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "emsys.h"
#include "re.h"
#include "row.h"
#include "tab.h"
#include "undo.h"
#include "unicode.h"
uint8_t *tabCompleteBufferNames(struct editorConfig *ed, uint8_t *input,
struct editorBuffer *currentBuffer) {
char **completions = NULL;
int count = 0;
int capacity = 8; // Initial capacity
uint8_t *ret = input;
// Allocate initial memory
completions = malloc(capacity * sizeof(char *));
if (completions == NULL) {
// Handle allocation failure
return ret;
}
// Collect matching buffer names
for (struct editorBuffer *b = ed->firstBuf; b != NULL; b = b->next) {
if (b == currentBuffer)
continue;
char *name = b->filename ? b->filename : "*scratch*";
if (strncmp(name, (char *)input, strlen((char *)input)) == 0) {
if (count + 1 >= capacity) {
// Double capacity and reallocate
capacity *= 2;
char **new_completions = realloc(
completions, capacity * sizeof(char *));
if (new_completions == NULL) {
// Handle reallocation failure
// Free existing completions and return
for (int i = 0; i < count; i++) {
free(completions[i]);
}
free(completions);
return ret;
}
completions = new_completions;
}
completions[count++] = stringdup(name);
}
}
if (count < 1) {
goto cleanup;
}
if (count == 1) {
ret = (uint8_t *)stringdup(completions[0]);
goto cleanup;
}
// Multiple matches, allow cycling through them
int cur = 0;
for (;;) {
editorSetStatusMessage("Multiple options: %s",
completions[cur]);
editorRefreshScreen();
editorCursorBottomLine(strlen(completions[cur]) + 19);
int c = editorReadKey();
switch (c) {
case '\r':
ret = (uint8_t *)stringdup(completions[cur]);
goto cleanup;
case CTRL('i'):
cur = (cur + 1) % count;
break;
case BACKTAB:
cur = (cur == 0) ? count - 1 : cur - 1;
break;
case CTRL('g'):
goto cleanup;
}
}
cleanup:
for (int i = 0; i < count; i++) {
free(completions[i]);
}
free(completions);
return ret;
}
uint8_t *tabCompleteFiles(uint8_t *prompt) {
glob_t globlist;
uint8_t *ret = prompt;
if (*prompt == '~') {
char *home_dir = getenv("HOME");
if (!home_dir) {
// Handle error: HOME environment variable not found
return prompt;
}
size_t home_len = strlen(home_dir);
size_t prompt_len = strlen((char *)prompt);
char *new_prompt =
malloc(home_len + prompt_len - 1 +
1); // -1 for removed '~', +1 for null terminator
if (!new_prompt) {
// Handle memory allocation failure
return prompt;
}
strcpy(new_prompt, home_dir);
strcpy(new_prompt + home_len, prompt + 1); // Skip the '~'
prompt = (uint8_t *)new_prompt;
}
/*
* Define this to do manual globbing. It does mean you'll have
* to add the *s yourself. However, it will let you tab
* complete for more interesting scenarios, like
* *dir/other*dir/file.*.gz -> mydir/otherFOOdir/file.tar.gz
*/
#ifndef EMSYS_NO_SIMPLE_GLOB
int end = strlen((char *)prompt);
prompt[end] = '*';
prompt[end + 1] = 0;
#endif
#ifndef GLOB_TILDE
/* This isn't in POSIX, so define a fallback. */
#define GLOB_TILDE 0
#endif
if (glob((char *)prompt, GLOB_TILDE | GLOB_MARK, NULL, &globlist))
goto TC_FILES_CLEANUP;
size_t cur = 0;
if (globlist.gl_pathc < 1)
goto TC_FILES_CLEANUP;
if (globlist.gl_pathc == 1)
goto TC_FILES_ACCEPT;
int curw = stringWidth((uint8_t *)globlist.gl_pathv[cur]);
for (;;) {
editorSetStatusMessage("Multiple options: %s",
globlist.gl_pathv[cur]);
editorRefreshScreen();
editorCursorBottomLine(curw + 19);
int c = editorReadKey();
switch (c) {
case '\r':;
TC_FILES_ACCEPT:;
ret = calloc(strlen(globlist.gl_pathv[cur]) + 1, 1);
strcpy((char *)ret, globlist.gl_pathv[cur]);
goto TC_FILES_CLEANUP;
break;
case CTRL('i'):
cur++;
if (cur >= globlist.gl_pathc) {
cur = 0;
}
curw = stringWidth((uint8_t *)globlist.gl_pathv[cur]);
break;
case BACKTAB:
if (cur == 0) {
cur = globlist.gl_pathc - 1;
} else {
cur--;
}
curw = stringWidth((uint8_t *)globlist.gl_pathv[cur]);
break;
case CTRL('g'):
goto TC_FILES_CLEANUP;
break;
}
}
TC_FILES_CLEANUP:
globfree(&globlist);
#ifndef EMSYS_NO_SIMPLE_GLOB
prompt[end] = 0;
#endif
return ret;
}
static int alnum(uint8_t c) {
return c > 127 || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9') || c == '_';
}
static int sortstring(const void *str1, const void *str2) {
char *const *pp1 = str1;
char *const *pp2 = str2;
return strcmp(*pp1, *pp2);
}
void editorCompleteWord(struct editorConfig *ed, struct editorBuffer *bufr) {
if (bufr->cy >= bufr->numrows || bufr->cx == 0) {
editorSetStatusMessage("Nothing to complete here.");
return;
}
struct erow *row = &bufr->row[bufr->cy];
int wordStart = bufr->cx;
for (int i = bufr->cx - 1; i >= 0; i--) {
if (!alnum(row->chars[i]))
break;
wordStart = i;
}
if (wordStart == bufr->cx) {
editorSetStatusMessage("Nothing to complete here.");
return;
}
char rpattern[] = "[A-Za-z0-9\200-\377_]+";
char *word = calloc(bufr->cx - wordStart + 1 + sizeof(rpattern), 1);
strncpy(word, (char *)&row->chars[wordStart], bufr->cx - wordStart);
strcat(word, rpattern);
int ncand = 0;
int scand = 32;
char **candidates = malloc(sizeof(uint8_t *) * scand);
re_t pattern = re_compile(word);
/* This is a deeply naive algorithm. */
/* First, find every word that starts with the word to complete */
for (struct editorBuffer *buf = ed->firstBuf; buf; buf = buf->next) {
for (int i = 0; i < buf->numrows; i++) {
if (buf == bufr && buf->cy == i)
continue;
struct erow *row = &bufr->row[i];
int match_length;
int match_idx = re_matchp(pattern, (char *)row->chars,
&match_length);
if (match_idx >= 0) {
candidates[ncand] = calloc(match_length + 1, 1);
strncpy(candidates[ncand],
(char *)&row->chars[match_idx],
match_length);
ncand++;
if (ncand >= scand) {
scand <<= 1;
candidates =
realloc(candidates,
sizeof(char *) * scand);
}
}
}
}
/* Dunmatchin'. Restore word to non-regex contents. */
word[bufr->cx - wordStart] = 0;
/* No matches? Cleanup. */
if (ncand == 0) {
editorSetStatusMessage("No match for %s", word);
goto COMPLETE_WORD_CLEANUP;
}
int sel = 0;
/* Only one candidate? Take it. */
if (ncand == 1) {
goto COMPLETE_WORD_DONE;
}
/* Next, sort the list */
qsort(candidates, ncand, sizeof(char *), sortstring);
/* Finally, uniq' it. We now have our candidate list. */
int newlen = 1;
char *prev = NULL;
for (int i = 0; i < ncand; i++) {
if (prev == NULL) {
prev = candidates[i];
continue;
}
if (strcmp(prev, candidates[i])) {
/* Nonduplicate, copy it over. */
prev = candidates[i];
candidates[newlen++] = prev;
} else {
/* We don't need the memory for duplicates any more. */
free(candidates[i]);
}
}
ncand = newlen;
/* If after all that mess we only have one candidate, use it. */
if (ncand == 1) {
goto COMPLETE_WORD_DONE;
}
/* Otherwise, standard tab complete interface. */
int selw = stringWidth((uint8_t *)candidates[sel]);
for (;;) {
editorSetStatusMessage("Multiple options: %s", candidates[sel]);
editorRefreshScreen();
editorCursorBottomLine(selw + 19);
int c = editorReadKey();
switch (c) {
case '\r':
goto COMPLETE_WORD_DONE;
break;
case EXPAND:
case CTRL('i'):
sel++;
if (sel >= ncand) {
sel = 0;
}
selw = stringWidth((uint8_t *)candidates[sel]);
break;
case BACKTAB:
if (sel == 0) {
sel = ncand - 1;
} else {
sel--;
}
selw = stringWidth((uint8_t *)candidates[sel]);
break;
case CTRL('g'):
editorSetStatusMessage("Canceled");
goto COMPLETE_WORD_CLEANUP;
break;
}
}
/* Finally, make the modification to the row, setup undos, and
* cleanup. */
COMPLETE_WORD_DONE:;
/* Length of the rest of the completed word */
int completelen = strlen(candidates[sel]) - (bufr->cx - wordStart);
struct editorUndo *new = newUndo();
new->prev = bufr->undo;
new->startx = bufr->cx;
new->starty = bufr->cy;
new->endx = bufr->cx + completelen;
new->endy = bufr->cy;
new->datalen = completelen;
if (new->datasize < completelen + 1) {
new->data = realloc(new->data, new->datalen + 1);
new->datasize = new->datalen + 1;
}
new->append = 0;
new->delete = 0;
new->data[0] = 0;
strcat((char *)new->data, &candidates[sel][bufr->cx - wordStart]);
bufr->undo = new;
row->chars = realloc(row->chars, row->size + 1 + completelen);
memcpy(&row->chars[bufr->cx + completelen], &row->chars[bufr->cx],
row->size - bufr->cx);
memcpy(&row->chars[bufr->cx], &candidates[sel][bufr->cx - wordStart],
completelen);
row->size += completelen;
row->chars[row->size] = 0;
editorUpdateRow(row);
editorSetStatusMessage("Expanded %.30s to %.30s", word,
candidates[sel]);
bufr->cx += completelen;
COMPLETE_WORD_CLEANUP:
for (int i = 0; i < ncand; i++) {
free(candidates[i]);
}
free(candidates);
free(word);
}