Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <stdlib.h>
18 #include <string.h>
20 #include "minibuffer.h"
21 #include "ui.h"
22 #include "utf8.h"
24 static void *minibuffer_metadata(void);
25 static void minibuffer_hist_save_entry(void);
26 static void yornp_self_insert(void);
27 static void yornp_abort(void);
28 static void read_self_insert(void);
29 static void read_abort(void);
30 static void read_select(void);
32 static void (*yornp_cb)(int, struct tab *);
33 static struct tab *yornp_data;
35 static void (*read_cb)(const char*, struct tab *);
36 static struct tab *read_data;
38 struct histhead eecmd_history,
39 ir_history,
40 lu_history,
41 read_history;
43 struct ministate ministate;
45 struct buffer minibufferwin;
47 /*
48 * Recompute the visible completions. If add is 1, don't consider the
49 * ones already hidden.
50 */
51 void
52 recompute_completions(int add)
53 {
54 struct line *l;
55 struct vline *vl;
56 struct buffer *b;
58 if (in_minibuffer != MB_COMPREAD)
59 return;
61 b = &ministate.compl.buffer;
62 TAILQ_FOREACH(l, &b->page.head, lines) {
63 l->type = LINE_COMPL;
64 if (add && l->flags & L_HIDDEN)
65 continue;
66 if (strcasestr(l->line, ministate.buf) != NULL)
67 l->flags &= ~L_HIDDEN;
68 else
69 l->flags |= L_HIDDEN;
70 }
72 if (b->current_line == NULL)
73 b->current_line = TAILQ_FIRST(&b->head);
74 b->current_line = adjust_line(b->current_line, b);
75 vl = b->current_line;
76 if (vl != NULL)
77 vl->parent->type = LINE_COMPL_CURRENT;
78 }
80 static void *
81 minibuffer_metadata(void)
82 {
83 struct vline *vl;
85 vl = ministate.compl.buffer.current_line;
87 if (vl == NULL || vl->parent->flags & L_HIDDEN)
88 return NULL;
90 return vl->parent->meta.data;
91 }
93 static void
94 minibuffer_hist_save_entry(void)
95 {
96 struct hist *hist;
98 if (ministate.history == NULL)
99 return;
101 if ((hist = calloc(1, sizeof(*hist))) == NULL)
102 abort();
104 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
106 if (TAILQ_EMPTY(&ministate.history->head))
107 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
108 else
109 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
110 ministate.history->len++;
113 /*
114 * taint the minibuffer cache: if we're currently showing a history
115 * element, copy that to the current buf and reset the "history
116 * navigation" thing.
117 */
118 void
119 minibuffer_taint_hist(void)
121 if (ministate.hist_cur == NULL)
122 return;
124 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
125 ministate.hist_cur = NULL;
126 ministate.buffer.current_line->line = ministate.buf;
129 void
130 minibuffer_self_insert(void)
132 char *c, tmp[5] = {0};
133 size_t len;
135 minibuffer_taint_hist();
137 if (thiskey.cp == 0)
138 return;
140 len = utf8_encode(thiskey.cp, tmp);
141 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
142 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
143 return;
145 memmove(c + len, c, strlen(c)+1);
146 memcpy(c, tmp, len);
147 ministate.buffer.cpoff++;
149 recompute_completions(1);
152 void
153 sensible_self_insert(void)
155 if (thiskey.meta || unicode_isspace(thiskey.key) ||
156 !unicode_isgraph(thiskey.key)) {
157 global_key_unbound();
158 return;
161 minibuffer_self_insert();
164 void
165 eecmd_self_insert(void)
167 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
168 !unicode_isgraph(thiskey.cp)) {
169 global_key_unbound();
170 return;
173 minibuffer_self_insert();
176 void
177 eecmd_select(void)
179 struct cmd *cmd;
181 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
182 if (!strcmp(cmd->cmd, ministate.buf)) {
183 exit_minibuffer();
184 minibuffer_hist_save_entry();
185 cmd->fn(current_buffer());
186 return;
190 message("No match");
193 void
194 ir_self_insert(void)
196 minibuffer_self_insert();
199 void
200 ir_select(void)
202 char buf[1025] = {0};
203 struct phos_uri uri;
204 struct tab *tab;
206 tab = current_tab();
208 exit_minibuffer();
209 minibuffer_hist_save_entry();
211 /* a bit ugly but... */
212 memcpy(&uri, &tab->uri, sizeof(tab->uri));
213 phos_uri_set_query(&uri, ministate.buf);
214 phos_serialize_uri(&uri, buf, sizeof(buf));
215 load_url_in_tab(tab, buf);
218 void
219 lu_select(void)
221 exit_minibuffer();
222 minibuffer_hist_save_entry();
223 load_url_in_tab(current_tab(), ministate.buf);
226 void
227 bp_select(void)
229 exit_minibuffer();
230 if (*ministate.buf != '\0')
231 add_to_bookmarks(ministate.buf);
232 else
233 message("Abort.");
236 void
237 ts_select(void)
239 struct tab *tab;
241 if ((tab = minibuffer_metadata()) == NULL) {
242 message("No tab selected");
243 return;
246 exit_minibuffer();
247 switch_to_tab(tab);
250 void
251 ls_select(void)
253 struct line *l;
255 if ((l = minibuffer_metadata()) == NULL) {
256 message("No link selected");
257 return;
260 exit_minibuffer();
261 load_url_in_tab(current_tab(), l->meta.alt);
264 static inline void
265 jump_to_line(struct line *l)
267 struct vline *vl;
268 struct tab *tab;
270 tab = current_tab();
272 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
273 if (vl->parent == l)
274 break;
277 if (vl == NULL)
278 message("Ops, %s error! Please report to %s",
279 __func__, PACKAGE_BUGREPORT);
280 else
281 tab->buffer.current_line = vl;
284 void
285 swiper_select(void)
287 struct line *l;
289 if ((l = minibuffer_metadata()) == NULL) {
290 message("No line selected");
291 return;
294 exit_minibuffer();
295 jump_to_line(l);
298 void
299 toc_select(void)
301 struct line *l;
303 if ((l = minibuffer_metadata()) == NULL) {
304 message("No line selected");
305 return;
308 exit_minibuffer();
309 jump_to_line(l);
312 static void
313 yornp_self_insert(void)
315 if (thiskey.key != 'y' && thiskey.key != 'n') {
316 message("Please answer y or n");
317 return;
320 exit_minibuffer();
321 yornp_cb(thiskey.key == 'y', yornp_data);
324 static void
325 yornp_abort(void)
327 exit_minibuffer();
328 yornp_cb(0, yornp_data);
331 static void
332 read_self_insert(void)
334 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
335 global_key_unbound();
336 return;
339 minibuffer_self_insert();
342 static void
343 read_abort(void)
345 exit_minibuffer();
346 read_cb(NULL, read_data);
349 static void
350 read_select(void)
352 exit_minibuffer();
353 minibuffer_hist_save_entry();
354 read_cb(ministate.buf, read_data);
357 /*
358 * TODO: we should collect this asynchronously...
359 */
360 static inline void
361 populate_compl_buffer(complfn *fn, void *data)
363 const char *s;
364 struct line *l;
365 struct buffer *b;
366 struct parser *p;
367 void *linedata = NULL;
369 b = &ministate.compl.buffer;
370 p = &b->page;
372 while ((s = fn(&data, &linedata)) != NULL) {
373 if ((l = calloc(1, sizeof(*l))) == NULL)
374 abort();
376 l->type = LINE_COMPL;
377 l->meta.data = linedata;
378 if ((l->line = strdup(s)) == NULL)
379 abort();
381 if (TAILQ_EMPTY(&p->head))
382 TAILQ_INSERT_HEAD(&p->head, l, lines);
383 else
384 TAILQ_INSERT_TAIL(&p->head, l, lines);
386 linedata = NULL;
389 if ((l = TAILQ_FIRST(&p->head)) != NULL)
390 l->type = LINE_COMPL_CURRENT;
393 void
394 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
395 void (*abortfn)(void), struct histhead *hist,
396 complfn *complfn, void *compldata)
398 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
399 if (in_minibuffer == MB_COMPREAD) {
400 ui_schedule_redraw();
402 ministate.compl.fn = complfn;
403 ministate.compl.data = compldata;
404 populate_compl_buffer(complfn, compldata);
407 base_map = &minibuffer_map;
408 current_map = &minibuffer_map;
410 base_map->unhandled_input = self_insert_fn;
412 ministate.donefn = donefn;
413 ministate.abortfn = abortfn;
414 memset(ministate.buf, 0, sizeof(ministate.buf));
415 ministate.buffer.current_line = &ministate.vline;
416 ministate.buffer.current_line->line = ministate.buf;
417 ministate.buffer.cpoff = 0;
418 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
420 ministate.history = hist;
421 ministate.hist_cur = NULL;
422 ministate.hist_off = 0;
425 void
426 exit_minibuffer(void)
428 if (in_minibuffer == MB_COMPREAD) {
429 erase_buffer(&ministate.compl.buffer);
430 ui_schedule_redraw();
433 in_minibuffer = 0;
434 base_map = &global_map;
435 current_map = &global_map;
438 void
439 yornp(const char *prompt, void (*fn)(int, struct tab*),
440 struct tab *data)
442 size_t len;
444 if (in_minibuffer) {
445 fn(0, data);
446 return;
449 yornp_cb = fn;
450 yornp_data = data;
451 enter_minibuffer(yornp_self_insert, yornp_self_insert,
452 yornp_abort, NULL, NULL, NULL);
454 len = sizeof(ministate.prompt);
455 strlcpy(ministate.prompt, prompt, len);
456 strlcat(ministate.prompt, " (y or n) ", len);
459 /*
460 * Not yet "completing", but soon maybe...
461 */
462 void
463 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
464 struct tab *data)
466 size_t len;
468 if (in_minibuffer)
469 return;
471 read_cb = fn;
472 read_data = data;
473 enter_minibuffer(read_self_insert, read_select, read_abort,
474 &read_history, NULL, NULL);
476 len = sizeof(ministate.prompt);
477 strlcpy(ministate.prompt, prompt, len);
478 strlcat(ministate.prompt, ": ", len);