-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectlist.c
More file actions
237 lines (211 loc) · 7.03 KB
/
Copy pathselectlist.c
File metadata and controls
237 lines (211 loc) · 7.03 KB
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
/* selectlist plugin: in-process selection picker rendered into a modal.
*
* Items are owned (deep-copied) by the picker. Keys j/k/<Down>/<Up> move
* the highlight, g/G jump to ends, Enter picks, q/<Esc> cancels. The
* pick callback receives (index, item, user) AFTER the modal is torn
* down, so the callback may itself open a new picker. */
#include "hed.h"
#include "selectlist/selectlist.h"
#include "ui/winmodal.h"
#include "buf/buffer.h"
#include "buf/row.h"
#include <string.h>
#include <stdlib.h>
static struct {
int active;
Window *modal;
int buf_idx;
char **items;
int count;
int selected;
SelectListCallback cb;
void *user;
} sl;
static void sl_repopulate(void) {
if (!sl.active) return;
Buffer *buf = &E.buffers[sl.buf_idx];
while (buf->num_rows > 0) buf_row_del_in(buf, 0);
for (int i = 0; i < sl.count; i++) {
const char *prefix = (i == sl.selected) ? "> " : " ";
size_t plen = 2;
size_t ilen = strlen(sl.items[i]);
char *line = malloc(plen + ilen + 1);
if (!line) continue;
memcpy(line, prefix, plen);
memcpy(line + plen, sl.items[i], ilen);
line[plen + ilen] = '\0';
buf_row_insert_in(buf, i, line, plen + ilen);
free(line);
}
buf->dirty = 0;
buf->readonly = 1;
if (sl.modal) {
if (sl.selected < sl.modal->row_offset)
sl.modal->row_offset = sl.selected;
if (sl.selected >= sl.modal->row_offset + sl.modal->height)
sl.modal->row_offset = sl.selected - sl.modal->height + 1;
sl.modal->cursor.y = sl.selected;
sl.modal->cursor.x = 0;
}
}
static void sl_close(void) {
if (!sl.active) return;
Window *modal = sl.modal;
int buf_idx = sl.buf_idx;
char **items = sl.items;
int count = sl.count;
memset(&sl, 0, sizeof(sl));
sl.buf_idx = -1;
if (modal) {
winmodal_hide(modal);
winmodal_destroy(modal);
}
if (buf_idx >= 0 && buf_idx < (int)arrlen(E.buffers)) {
E.buffers[buf_idx].dirty = 0;
buf_close(buf_idx);
}
for (int i = 0; i < count; i++) free(items[i]);
free(items);
}
static void sl_keypress(HookKeyEvent *event) {
if (!sl.active || !event) return;
Window *modal = winmodal_current();
if (!modal || modal != sl.modal) return;
/* Swallow every key while our modal is open — otherwise unhandled
* keys would fall through to the editor and edit the modal buffer
* (the modal is the current window). */
event->consumed = 1;
switch (event->key) {
case 'j':
case KEY_ARROW_DOWN:
if (sl.selected < sl.count - 1) sl.selected++;
sl_repopulate();
break;
case 'k':
case KEY_ARROW_UP:
if (sl.selected > 0) sl.selected--;
sl_repopulate();
break;
case 'g':
sl.selected = 0;
sl_repopulate();
break;
case 'G':
sl.selected = sl.count - 1;
sl_repopulate();
break;
case '\r':
case '\n': {
SelectListCallback cb = sl.cb;
void *user = sl.user;
int idx = sl.selected;
char *copy = sl.items[idx] ? strdup(sl.items[idx]) : NULL;
sl_close();
if (cb) cb(idx, copy ? copy : "", user);
free(copy);
break;
}
case 'q':
case '\x1b':
sl_close();
break;
}
}
static int sl_attach(Window *modal,
const char *const *items, int count,
SelectListCallback cb, void *user) {
int buf_idx = -1;
if (buf_new_scratch("select", &buf_idx) != ED_OK) {
winmodal_destroy(modal);
return -1;
}
Buffer *buf = &E.buffers[buf_idx];
modal->buffer_index = buf_idx;
sl.active = 1;
sl.modal = modal;
sl.buf_idx = buf_idx;
sl.selected = 0;
sl.cb = cb;
sl.user = user;
sl.count = count;
sl.items = malloc(sizeof(char *) * (size_t)count);
if (!sl.items) {
memset(&sl, 0, sizeof(sl));
sl.buf_idx = -1;
buf->dirty = 0;
buf_close(buf_idx);
winmodal_destroy(modal);
return -1;
}
for (int i = 0; i < count; i++) sl.items[i] = strdup(items[i] ? items[i] : "");
sl_repopulate();
winmodal_show(modal);
ed_set_status_message("selectlist: j/k move Enter select q/Esc cancel");
return 0;
}
int selectlist_open(int x, int y, int width, int height,
const char *const *items, int count,
SelectListCallback cb, void *user) {
if (!items || count <= 0) return -1;
if (sl.active) sl_close();
if (width <= 0) width = 30;
if (height <= 0) height = count > 10 ? 10 : count;
Window *modal = winmodal_create(x, y, width, height);
if (!modal) return -1;
return sl_attach(modal, items, count, cb, user);
}
int selectlist_open_anchored(int anchor_x, int anchor_y, int width,
const char *const *items, int count,
WModalAnchor prefer,
SelectListCallback cb, void *user) {
if (!items || count <= 0) return -1;
if (sl.active) sl_close();
if (width <= 0) width = 30;
int height = count > 10 ? 10 : count;
Window *modal = winmodal_create_anchored(anchor_x, anchor_y, width, height, prefer);
if (!modal) return -1;
return sl_attach(modal, items, count, cb, user);
}
/* --- demo command -------------------------------------------------- */
static void on_demo_pick(int idx, const char *item, void *user) {
(void)user;
ed_set_status_message("selectlist: picked [%d] %s", idx, item);
}
static void cmd_selectlist_demo(const char *args) {
(void)args;
static const char *demo[] = {
"alpha", "bravo", "charlie", "delta",
"echo", "foxtrot", "golf", "hotel",
};
int n = (int)(sizeof(demo) / sizeof(demo[0]));
if (arrlen(E.windows) == 0) {
ed_set_status_message("selectlist-demo: no window");
return;
}
Window *cur = &E.windows[E.current_window];
int anchor_y = (cur->cursor.y - cur->row_offset) + cur->top;
int anchor_x = cur->left;
Buffer *buf = (cur->buffer_index >= 0 &&
cur->buffer_index < (int)arrlen(E.buffers))
? &E.buffers[cur->buffer_index]
: NULL;
if (buf && cur->cursor.y < buf->num_rows) {
int rx = buf_row_cx_to_rx(&buf->rows[cur->cursor.y], cur->cursor.x);
anchor_x = (rx - cur->col_offset) + cur->left;
}
selectlist_open_anchored(anchor_x, anchor_y, 24, demo, n,
WMODAL_AUTO, on_demo_pick, NULL);
}
static int selectlist_init(void) {
sl.buf_idx = -1;
cmd("selectlist-demo", cmd_selectlist_demo,
"show a sample SelectList anchored at the cursor");
hook_register_key(HOOK_KEYPRESS, sl_keypress);
return 0;
}
const Plugin plugin_selectlist = {
.name = "selectlist",
.desc = "in-process selection picker (modal)",
.init = selectlist_init,
.deinit = NULL,
};