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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
#define _DEFAULT_SOURCE
#include "completion.h"
#include <dirent.h>
#include <errno.h>
#include <libgen.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "dged/binding.h"
#include "dged/buffer.h"
#include "dged/buffer_view.h"
#include "dged/buffers.h"
#include "dged/display.h"
#include "dged/minibuffer.h"
#include "dged/path.h"
#include "dged/window.h"
#include "bindings.h"
#include "frame-hooks.h"
struct buffer_completion {
struct buffer *buffer;
uint32_t insert_hook_id;
uint32_t remove_hook_id;
VEC(struct completion_provider) providers;
};
struct completion_item {
struct region area;
struct completion completion;
};
static struct completion_state {
VEC(struct buffer_completion) buffer_completions;
VEC(struct completion_item) completions;
uint64_t completion_index;
struct buffer *completions_buffer;
buffer_keymap_id keymap_id;
struct buffer *target;
layer_id highlight_current_layer;
bool insert_in_progress;
bool paused;
} g_state;
static struct region active_completion_region(struct completion_state *state) {
struct region reg =
region_new((struct location){0, 0}, (struct location){0, 0});
if (state->completion_index < VEC_SIZE(&state->completions)) {
reg = VEC_ENTRIES(&state->completions)[state->completion_index].area;
}
return reg;
}
static int32_t goto_next_completion(struct command_ctx ctx, int argc,
const char *argv[]) {
(void)ctx;
(void)argc;
(void)argv;
if (!completion_active()) {
return 0;
}
if (VEC_EMPTY(&g_state.completions)) {
g_state.completion_index = 0;
return 0;
}
size_t ncompletions = VEC_SIZE(&g_state.completions);
if (g_state.completion_index >= ncompletions - 1) {
g_state.completion_index = ncompletions - 1;
return 0;
}
++g_state.completion_index;
if (completion_active()) {
buffer_view_goto(window_buffer_view(popup_window()),
active_completion_region(&g_state).begin);
}
return 0;
}
static int32_t goto_prev_completion(struct command_ctx ctx, int argc,
const char *argv[]) {
(void)ctx;
(void)argc;
(void)argv;
if (!completion_active()) {
return 0;
}
if (g_state.completion_index == 0) {
return 0;
}
--g_state.completion_index;
if (completion_active()) {
buffer_view_goto(window_buffer_view(popup_window()),
active_completion_region(&g_state).begin);
}
return 0;
}
static int32_t insert_completion(struct command_ctx ctx, int argc,
const char *argv[]) {
(void)ctx;
(void)argc;
(void)argv;
if (!completion_active()) {
return 0;
}
struct buffer_view *bv = window_buffer_view(popup_window());
struct window *target_window = windows_get_active();
struct buffer_view *target = window_buffer_view(target_window);
VEC_FOR_EACH(&g_state.completions, struct completion_item * item) {
if (region_is_inside(item->area, bv->dot)) {
g_state.insert_in_progress = true;
item->completion.selected(item->completion.data, target);
g_state.insert_in_progress = false;
return 0;
}
}
return 0;
}
COMMAND_FN("next-completion", next_completion, goto_next_completion, NULL)
COMMAND_FN("prev-completion", prev_completion, goto_prev_completion, NULL)
COMMAND_FN("insert-completion", insert_completion, insert_completion, NULL)
static void clear_completions(struct completion_state *state) {
buffer_clear(state->completions_buffer);
VEC_FOR_EACH(&state->completions, struct completion_item * item) {
if (item->completion.cleanup != NULL) {
item->completion.cleanup(item->completion.data);
}
}
VEC_CLEAR(&state->completions);
state->completion_index = 0;
if (completion_active()) {
buffer_view_goto(window_buffer_view(popup_window()),
(struct location){0, 0});
}
}
static void update_window_position(struct completion_state *state) {
size_t ncompletions = VEC_SIZE(&state->completions);
struct window *target_window = windows_get_active();
struct window *root_wind = root_window();
size_t nlines = buffer_num_lines(state->completions_buffer);
size_t max_width = 10;
window_set_buffer_e(popup_window(), state->completions_buffer, false, false);
struct window_position winpos = window_position(target_window);
struct buffer_view *view = window_buffer_view(target_window);
uint32_t height = ncompletions > 10 ? 10 : ncompletions;
size_t xpos =
winpos.x + view->fringe_width + (view->dot.col - view->scroll.col) + 1;
// should it be over or under?
size_t relative_line = (view->dot.line - view->scroll.line);
size_t ypos = winpos.y + relative_line;
if (ypos > 10) {
ypos -= height + 1;
} else {
ypos += 3;
}
for (uint64_t i = 0; i < nlines; ++i) {
size_t linelen = buffer_line_length(state->completions_buffer, i);
if (linelen > max_width) {
max_width = linelen;
}
}
size_t available = window_width(root_wind) - xpos - 5;
max_width = max_width >= available ? available : max_width;
windows_show_popup(ypos, xpos, max_width, height);
}
static void update_window_pos_frame_hook(void *data) {
struct completion_state *state = (struct completion_state *)data;
update_window_position(state);
}
static void open_completion(struct completion_state *state) {
size_t ncompletions = VEC_SIZE(&state->completions);
if (ncompletions == 0) {
abort_completion();
return;
}
struct window *target_window = windows_get_active();
struct buffer *buffer = window_buffer(target_window);
if (!completion_active() || state->target != buffer) {
// clear any previous keymaps
if (g_state.keymap_id != (uint64_t)-1) {
buffer_remove_keymap(g_state.keymap_id);
}
g_state.keymap_id = (uint64_t)-1;
struct keymap km = keymap_create("completion", 8);
struct binding comp_bindings[] = {
ANONYMOUS_BINDING(Ctrl, 'N', &next_completion_command),
ANONYMOUS_BINDING(Ctrl, 'P', &prev_completion_command),
ANONYMOUS_BINDING(ENTER, &insert_completion_command),
};
keymap_bind_keys(&km, comp_bindings,
sizeof(comp_bindings) / sizeof(comp_bindings[0]));
state->keymap_id = buffer_add_keymap(buffer, km);
state->target = buffer;
}
// need to run next frame to have the correct position
run_next_frame(update_window_pos_frame_hook, state);
}
static void add_completions_impl(struct completion *completions,
size_t ncompletions) {
for (uint32_t i = 0; i < ncompletions; ++i) {
struct completion *c = &completions[i];
struct region area = c->render(c->data, g_state.completions_buffer);
VEC_APPEND(&g_state.completions, struct completion_item * new);
new->area = area;
new->completion = *c;
}
open_completion(&g_state);
}
static void update_completions(struct completion_state *state,
struct buffer *buffer, struct location location,
bool deletion) {
clear_completions(state);
struct buffer_completion *buffer_config = NULL;
VEC_FOR_EACH(&state->buffer_completions, struct buffer_completion * bc) {
if (buffer == bc->buffer) {
buffer_config = bc;
break;
}
}
if (buffer_config == NULL) {
return;
}
VEC_FOR_EACH(&buffer_config->providers,
struct completion_provider * provider) {
struct completion_context comp_ctx = (struct completion_context){
.buffer = buffer,
.location = location,
.add_completions = add_completions_impl,
};
provider->complete(comp_ctx, deletion, provider->userdata);
}
}
static void update_comp_buffer(struct buffer *buffer, void *userdata) {
struct completion_state *state = (struct completion_state *)userdata;
buffer_clear_text_property_layer(buffer, state->highlight_current_layer);
if (buffer_is_empty(buffer)) {
abort_completion();
}
struct region reg = active_completion_region(state);
if (region_has_size(reg)) {
buffer_add_text_property_to_layer(buffer, reg.begin, reg.end,
(struct text_property){
.type = TextProperty_Colors,
.data.colors =
(struct text_property_colors){
.inverted = true,
.set_fg = false,
.set_bg = false,
.underline = false,
},
},
state->highlight_current_layer);
}
}
static void on_buffer_changed(struct buffer *buffer, struct edit_location edit,
bool deletion, void *userdata) {
struct completion_state *state = (struct completion_state *)userdata;
if (state->insert_in_progress || state->paused) {
return;
}
update_completions(state, buffer, edit.coordinates.end, deletion);
}
static void on_buffer_insert(struct buffer *buffer, struct edit_location edit,
void *userdata) {
on_buffer_changed(buffer, edit, false, userdata);
}
static void on_buffer_delete(struct buffer *buffer, struct edit_location edit,
void *userdata) {
on_buffer_changed(buffer, edit, true, userdata);
}
void init_completion(struct buffers *buffers) {
if (g_state.completions_buffer == NULL) {
struct buffer b = buffer_create("*completions*");
b.lazy_row_add = false;
b.force_show_ws_off = true;
b.retain_properties = true;
g_state.completions_buffer = buffers_add(buffers, b);
}
g_state.highlight_current_layer =
buffer_add_text_property_layer(g_state.completions_buffer);
buffer_add_update_hook(g_state.completions_buffer, update_comp_buffer,
&g_state);
g_state.keymap_id = (uint64_t)-1;
g_state.target = NULL;
VEC_INIT(&g_state.buffer_completions, 50);
VEC_INIT(&g_state.completions, 50);
g_state.completion_index = 0;
g_state.insert_in_progress = false;
g_state.paused = false;
}
void add_completion_providers(struct buffer *source,
struct completion_provider *providers,
uint32_t nproviders) {
struct buffer_completion *comp = NULL;
VEC_FOR_EACH(&g_state.buffer_completions, struct buffer_completion * c) {
if (c->buffer == source) {
comp = c;
break;
}
}
if (comp == NULL) {
VEC_APPEND(&g_state.buffer_completions,
struct buffer_completion * new_comp);
uint32_t insert_hook_id =
buffer_add_insert_hook(source, on_buffer_insert, &g_state);
uint32_t remove_hook_id =
buffer_add_delete_hook(source, on_buffer_delete, &g_state);
new_comp->buffer = source;
new_comp->insert_hook_id = insert_hook_id;
new_comp->remove_hook_id = remove_hook_id;
VEC_INIT(&new_comp->providers, nproviders);
comp = new_comp;
}
for (uint32_t i = 0; i < nproviders; ++i) {
VEC_PUSH(&comp->providers, providers[i]);
}
}
void complete(struct buffer *buffer, struct location at) {
update_completions(&g_state, buffer, at, false);
}
void abort_completion(void) {
windows_close_popup();
if (g_state.keymap_id != (uint64_t)-1) {
buffer_remove_keymap(g_state.keymap_id);
}
g_state.keymap_id = (uint64_t)-1;
g_state.target = NULL;
}
bool completion_active(void) {
return popup_window_visible() &&
window_buffer(popup_window()) == g_state.completions_buffer;
}
static void do_nothing(void *userdata) { (void)userdata; }
static void cleanup_buffer_completion(struct buffer_completion *comp) {
buffer_remove_delete_hook(comp->buffer, comp->remove_hook_id, do_nothing);
buffer_remove_insert_hook(comp->buffer, comp->insert_hook_id, do_nothing);
VEC_FOR_EACH(&comp->providers, struct completion_provider * provider) {
if (provider->cleanup != NULL) {
provider->cleanup(provider->userdata);
}
}
VEC_DESTROY(&comp->providers);
}
void disable_completion(struct buffer *buffer) {
VEC_FOR_EACH_INDEXED(&g_state.buffer_completions,
struct buffer_completion * comp, i) {
if (buffer == comp->buffer) {
VEC_SWAP(&g_state.buffer_completions, i,
VEC_SIZE(&g_state.buffer_completions) - 1);
VEC_POP(&g_state.buffer_completions, struct buffer_completion removed);
cleanup_buffer_completion(&removed);
}
}
}
void destroy_completion(void) {
clear_completions(&g_state);
// clean up any active completions we might have
VEC_FOR_EACH(&g_state.buffer_completions, struct buffer_completion * comp) {
cleanup_buffer_completion(comp);
}
VEC_DESTROY(&g_state.buffer_completions);
VEC_DESTROY(&g_state.completions);
}
void pause_completion() { g_state.paused = true; }
void resume_completion() { g_state.paused = false; }
|