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->alt != NULL && strcasestr(l->alt, ministate.buf) != NULL)) {
68 if (l->flags & L_HIDDEN)
69 b->line_max++;
70 l->flags &= ~L_HIDDEN;
71 } else {
72 if (!(l->flags & L_HIDDEN))
73 b->line_max--;
74 l->flags |= L_HIDDEN;
75 }
76 }
78 if (b->current_line == NULL)
79 b->current_line = TAILQ_FIRST(&b->head);
80 b->current_line = adjust_line(b->current_line, b);
81 vl = b->current_line;
82 if (vl != NULL)
83 vl->parent->type = LINE_COMPL_CURRENT;
84 }
86 static void *
87 minibuffer_metadata(void)
88 {
89 struct vline *vl;
91 vl = ministate.compl.buffer.current_line;
93 if (vl == NULL || vl->parent->flags & L_HIDDEN)
94 return NULL;
96 return vl->parent->data;
97 }
99 static void
100 minibuffer_hist_save_entry(void)
102 struct hist *hist;
104 if (ministate.history == NULL)
105 return;
107 if ((hist = calloc(1, sizeof(*hist))) == NULL)
108 abort();
110 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
112 if (TAILQ_EMPTY(&ministate.history->head))
113 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
114 else
115 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
116 ministate.history->len++;
119 /*
120 * taint the minibuffer cache: if we're currently showing a history
121 * element, copy that to the current buf and reset the "history
122 * navigation" thing.
123 */
124 void
125 minibuffer_taint_hist(void)
127 if (ministate.hist_cur == NULL)
128 return;
130 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
131 ministate.hist_cur = NULL;
132 ministate.buffer.current_line->line = ministate.buf;
135 void
136 minibuffer_self_insert(void)
138 char *c, tmp[5] = {0};
139 size_t len;
141 minibuffer_taint_hist();
143 if (thiskey.cp == 0)
144 return;
146 len = utf8_encode(thiskey.cp, tmp);
147 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
148 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
149 return;
151 memmove(c + len, c, strlen(c)+1);
152 memcpy(c, tmp, len);
153 ministate.buffer.cpoff++;
155 recompute_completions(1);
158 void
159 sensible_self_insert(void)
161 if (thiskey.meta || unicode_isspace(thiskey.key) ||
162 !unicode_isgraph(thiskey.key)) {
163 global_key_unbound();
164 return;
167 minibuffer_self_insert();
170 void
171 eecmd_self_insert(void)
173 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
174 !unicode_isgraph(thiskey.cp)) {
175 global_key_unbound();
176 return;
179 minibuffer_self_insert();
182 void
183 eecmd_select(void)
185 struct cmd *cmd;
187 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
188 if (!strcmp(cmd->cmd, ministate.buf)) {
189 exit_minibuffer();
190 minibuffer_hist_save_entry();
191 cmd->fn(current_buffer());
192 return;
196 message("No match");
199 void
200 ir_self_insert(void)
202 minibuffer_self_insert();
205 void
206 ir_select(void)
208 char buf[1025] = {0};
209 struct phos_uri uri;
210 struct tab *tab = current_tab;
212 exit_minibuffer();
213 minibuffer_hist_save_entry();
215 /* a bit ugly but... */
216 memcpy(&uri, &tab->uri, sizeof(tab->uri));
217 phos_uri_set_query(&uri, ministate.buf);
218 phos_serialize_uri(&uri, buf, sizeof(buf));
219 load_url_in_tab(tab, buf);
222 void
223 lu_select(void)
225 exit_minibuffer();
226 minibuffer_hist_save_entry();
227 load_url_in_tab(current_tab, ministate.buf);
230 void
231 bp_select(void)
233 exit_minibuffer();
234 if (*ministate.buf != '\0')
235 add_to_bookmarks(ministate.buf);
236 else
237 message("Abort.");
240 void
241 ts_select(void)
243 struct tab *tab;
245 if ((tab = minibuffer_metadata()) == NULL) {
246 message("No tab selected");
247 return;
250 exit_minibuffer();
251 switch_to_tab(tab);
254 void
255 ls_select(void)
257 struct line *l;
259 if ((l = minibuffer_metadata()) == NULL) {
260 message("No link selected");
261 return;
264 exit_minibuffer();
265 load_url_in_tab(current_tab, l->alt);
268 static inline void
269 jump_to_line(struct line *l)
271 struct vline *vl;
272 struct tab *tab = current_tab;
274 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
275 if (vl->parent == l)
276 break;
279 if (vl == NULL)
280 message("Ops, %s error! Please report to %s",
281 __func__, PACKAGE_BUGREPORT);
282 else {
283 tab->buffer.top_line = vl;
284 tab->buffer.current_line = vl;
288 void
289 swiper_select(void)
291 struct line *l;
293 if ((l = minibuffer_metadata()) == NULL) {
294 message("No line selected");
295 return;
298 exit_minibuffer();
299 jump_to_line(l);
302 void
303 toc_select(void)
305 struct line *l;
307 if ((l = minibuffer_metadata()) == NULL) {
308 message("No line selected");
309 return;
312 exit_minibuffer();
313 jump_to_line(l);
316 static void
317 yornp_self_insert(void)
319 if (thiskey.key != 'y' && thiskey.key != 'n') {
320 message("Please answer y or n");
321 return;
324 exit_minibuffer();
325 yornp_cb(thiskey.key == 'y', yornp_data);
328 static void
329 yornp_abort(void)
331 exit_minibuffer();
332 yornp_cb(0, yornp_data);
335 static void
336 read_self_insert(void)
338 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
339 global_key_unbound();
340 return;
343 minibuffer_self_insert();
346 static void
347 read_abort(void)
349 exit_minibuffer();
350 read_cb(NULL, read_data);
353 static void
354 read_select(void)
356 exit_minibuffer();
357 minibuffer_hist_save_entry();
358 read_cb(ministate.buf, read_data);
361 /*
362 * TODO: we should collect this asynchronously...
363 */
364 static inline void
365 populate_compl_buffer(complfn *fn, void *data)
367 const char *s, *descr;
368 struct line *l;
369 struct buffer *b;
370 struct parser *p;
371 void *linedata = NULL;
373 b = &ministate.compl.buffer;
374 p = &b->page;
376 while ((s = fn(&data, &linedata, &descr)) != NULL) {
377 if ((l = calloc(1, sizeof(*l))) == NULL)
378 abort();
380 l->type = LINE_COMPL;
381 l->data = linedata;
382 l->alt = (char*)descr;
383 if ((l->line = strdup(s)) == NULL)
384 abort();
386 if (TAILQ_EMPTY(&p->head))
387 TAILQ_INSERT_HEAD(&p->head, l, lines);
388 else
389 TAILQ_INSERT_TAIL(&p->head, l, lines);
391 linedata = NULL;
392 descr = NULL;
395 if ((l = TAILQ_FIRST(&p->head)) != NULL)
396 l->type = LINE_COMPL_CURRENT;
399 void
400 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
401 void (*abortfn)(void), struct histhead *hist,
402 complfn *complfn, void *compldata)
404 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
405 if (in_minibuffer == MB_COMPREAD) {
406 ui_schedule_redraw();
408 ministate.compl.fn = complfn;
409 ministate.compl.data = compldata;
410 populate_compl_buffer(complfn, compldata);
413 base_map = &minibuffer_map;
414 current_map = &minibuffer_map;
416 base_map->unhandled_input = self_insert_fn;
418 ministate.donefn = donefn;
419 ministate.abortfn = abortfn;
420 memset(ministate.buf, 0, sizeof(ministate.buf));
421 ministate.buffer.current_line = &ministate.vline;
422 ministate.buffer.current_line->line = ministate.buf;
423 ministate.buffer.cpoff = 0;
424 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
426 ministate.history = hist;
427 ministate.hist_cur = NULL;
428 ministate.hist_off = 0;
431 void
432 exit_minibuffer(void)
434 if (in_minibuffer == MB_COMPREAD) {
435 erase_buffer(&ministate.compl.buffer);
436 ui_schedule_redraw();
439 in_minibuffer = 0;
440 base_map = &global_map;
441 current_map = &global_map;
444 void
445 yornp(const char *prompt, void (*fn)(int, struct tab*),
446 struct tab *data)
448 size_t len;
450 if (in_minibuffer) {
451 fn(0, data);
452 return;
455 yornp_cb = fn;
456 yornp_data = data;
457 enter_minibuffer(yornp_self_insert, yornp_self_insert,
458 yornp_abort, NULL, NULL, NULL);
460 len = sizeof(ministate.prompt);
461 strlcpy(ministate.prompt, prompt, len);
462 strlcat(ministate.prompt, " (y or n) ", len);
465 /*
466 * Not yet "completing", but soon maybe...
467 */
468 void
469 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
470 struct tab *data)
472 size_t len;
474 if (in_minibuffer)
475 return;
477 read_cb = fn;
478 read_data = data;
479 enter_minibuffer(read_self_insert, read_select, read_abort,
480 &read_history, NULL, NULL);
482 len = sizeof(ministate.prompt);
483 strlcpy(ministate.prompt, prompt, len);
484 strlcat(ministate.prompt, ": ", len);