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 if (l->flags & L_HIDDEN)
68 b->line_max++;
69 l->flags &= ~L_HIDDEN;
70 } else {
71 if (!(l->flags & L_HIDDEN))
72 b->line_max--;
73 l->flags |= L_HIDDEN;
74 }
75 }
77 if (b->current_line == NULL)
78 b->current_line = TAILQ_FIRST(&b->head);
79 b->current_line = adjust_line(b->current_line, b);
80 vl = b->current_line;
81 if (vl != NULL)
82 vl->parent->type = LINE_COMPL_CURRENT;
83 }
85 static void *
86 minibuffer_metadata(void)
87 {
88 struct vline *vl;
90 vl = ministate.compl.buffer.current_line;
92 if (vl == NULL || vl->parent->flags & L_HIDDEN)
93 return NULL;
95 return vl->parent->data;
96 }
98 static void
99 minibuffer_hist_save_entry(void)
101 struct hist *hist;
103 if (ministate.history == NULL)
104 return;
106 if ((hist = calloc(1, sizeof(*hist))) == NULL)
107 abort();
109 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
111 if (TAILQ_EMPTY(&ministate.history->head))
112 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
113 else
114 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
115 ministate.history->len++;
118 /*
119 * taint the minibuffer cache: if we're currently showing a history
120 * element, copy that to the current buf and reset the "history
121 * navigation" thing.
122 */
123 void
124 minibuffer_taint_hist(void)
126 if (ministate.hist_cur == NULL)
127 return;
129 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
130 ministate.hist_cur = NULL;
131 ministate.buffer.current_line->line = ministate.buf;
134 void
135 minibuffer_self_insert(void)
137 char *c, tmp[5] = {0};
138 size_t len;
140 minibuffer_taint_hist();
142 if (thiskey.cp == 0)
143 return;
145 len = utf8_encode(thiskey.cp, tmp);
146 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
147 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
148 return;
150 memmove(c + len, c, strlen(c)+1);
151 memcpy(c, tmp, len);
152 ministate.buffer.cpoff++;
154 recompute_completions(1);
157 void
158 sensible_self_insert(void)
160 if (thiskey.meta || unicode_isspace(thiskey.key) ||
161 !unicode_isgraph(thiskey.key)) {
162 global_key_unbound();
163 return;
166 minibuffer_self_insert();
169 void
170 eecmd_self_insert(void)
172 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
173 !unicode_isgraph(thiskey.cp)) {
174 global_key_unbound();
175 return;
178 minibuffer_self_insert();
181 void
182 eecmd_select(void)
184 struct cmd *cmd;
186 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
187 if (!strcmp(cmd->cmd, ministate.buf)) {
188 exit_minibuffer();
189 minibuffer_hist_save_entry();
190 cmd->fn(current_buffer());
191 return;
195 message("No match");
198 void
199 ir_self_insert(void)
201 minibuffer_self_insert();
204 void
205 ir_select(void)
207 char buf[1025] = {0};
208 struct phos_uri uri;
209 struct tab *tab;
211 tab = current_tab();
213 exit_minibuffer();
214 minibuffer_hist_save_entry();
216 /* a bit ugly but... */
217 memcpy(&uri, &tab->uri, sizeof(tab->uri));
218 phos_uri_set_query(&uri, ministate.buf);
219 phos_serialize_uri(&uri, buf, sizeof(buf));
220 load_url_in_tab(tab, buf);
223 void
224 lu_select(void)
226 exit_minibuffer();
227 minibuffer_hist_save_entry();
228 load_url_in_tab(current_tab(), ministate.buf);
231 void
232 bp_select(void)
234 exit_minibuffer();
235 if (*ministate.buf != '\0')
236 add_to_bookmarks(ministate.buf);
237 else
238 message("Abort.");
241 void
242 ts_select(void)
244 struct tab *tab;
246 if ((tab = minibuffer_metadata()) == NULL) {
247 message("No tab selected");
248 return;
251 exit_minibuffer();
252 switch_to_tab(tab);
255 void
256 ls_select(void)
258 struct line *l;
260 if ((l = minibuffer_metadata()) == NULL) {
261 message("No link selected");
262 return;
265 exit_minibuffer();
266 load_url_in_tab(current_tab(), l->alt);
269 static inline void
270 jump_to_line(struct line *l)
272 struct vline *vl;
273 struct tab *tab;
275 tab = current_tab();
277 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
278 if (vl->parent == l)
279 break;
282 if (vl == NULL)
283 message("Ops, %s error! Please report to %s",
284 __func__, PACKAGE_BUGREPORT);
285 else
286 tab->buffer.current_line = vl;
289 void
290 swiper_select(void)
292 struct line *l;
294 if ((l = minibuffer_metadata()) == NULL) {
295 message("No line selected");
296 return;
299 exit_minibuffer();
300 jump_to_line(l);
303 void
304 toc_select(void)
306 struct line *l;
308 if ((l = minibuffer_metadata()) == NULL) {
309 message("No line selected");
310 return;
313 exit_minibuffer();
314 jump_to_line(l);
317 static void
318 yornp_self_insert(void)
320 if (thiskey.key != 'y' && thiskey.key != 'n') {
321 message("Please answer y or n");
322 return;
325 exit_minibuffer();
326 yornp_cb(thiskey.key == 'y', yornp_data);
329 static void
330 yornp_abort(void)
332 exit_minibuffer();
333 yornp_cb(0, yornp_data);
336 static void
337 read_self_insert(void)
339 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
340 global_key_unbound();
341 return;
344 minibuffer_self_insert();
347 static void
348 read_abort(void)
350 exit_minibuffer();
351 read_cb(NULL, read_data);
354 static void
355 read_select(void)
357 exit_minibuffer();
358 minibuffer_hist_save_entry();
359 read_cb(ministate.buf, read_data);
362 /*
363 * TODO: we should collect this asynchronously...
364 */
365 static inline void
366 populate_compl_buffer(complfn *fn, void *data)
368 const char *s;
369 struct line *l;
370 struct buffer *b;
371 struct parser *p;
372 void *linedata = NULL;
374 b = &ministate.compl.buffer;
375 p = &b->page;
377 while ((s = fn(&data, &linedata)) != NULL) {
378 if ((l = calloc(1, sizeof(*l))) == NULL)
379 abort();
381 l->type = LINE_COMPL;
382 l->data = linedata;
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;
394 if ((l = TAILQ_FIRST(&p->head)) != NULL)
395 l->type = LINE_COMPL_CURRENT;
398 void
399 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
400 void (*abortfn)(void), struct histhead *hist,
401 complfn *complfn, void *compldata)
403 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
404 if (in_minibuffer == MB_COMPREAD) {
405 ui_schedule_redraw();
407 ministate.compl.fn = complfn;
408 ministate.compl.data = compldata;
409 populate_compl_buffer(complfn, compldata);
412 base_map = &minibuffer_map;
413 current_map = &minibuffer_map;
415 base_map->unhandled_input = self_insert_fn;
417 ministate.donefn = donefn;
418 ministate.abortfn = abortfn;
419 memset(ministate.buf, 0, sizeof(ministate.buf));
420 ministate.buffer.current_line = &ministate.vline;
421 ministate.buffer.current_line->line = ministate.buf;
422 ministate.buffer.cpoff = 0;
423 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
425 ministate.history = hist;
426 ministate.hist_cur = NULL;
427 ministate.hist_off = 0;
430 void
431 exit_minibuffer(void)
433 if (in_minibuffer == MB_COMPREAD) {
434 erase_buffer(&ministate.compl.buffer);
435 ui_schedule_redraw();
438 in_minibuffer = 0;
439 base_map = &global_map;
440 current_map = &global_map;
443 void
444 yornp(const char *prompt, void (*fn)(int, struct tab*),
445 struct tab *data)
447 size_t len;
449 if (in_minibuffer) {
450 fn(0, data);
451 return;
454 yornp_cb = fn;
455 yornp_data = data;
456 enter_minibuffer(yornp_self_insert, yornp_self_insert,
457 yornp_abort, NULL, NULL, NULL);
459 len = sizeof(ministate.prompt);
460 strlcpy(ministate.prompt, prompt, len);
461 strlcat(ministate.prompt, " (y or n) ", len);
464 /*
465 * Not yet "completing", but soon maybe...
466 */
467 void
468 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
469 struct tab *data)
471 size_t len;
473 if (in_minibuffer)
474 return;
476 read_cb = fn;
477 read_data = data;
478 enter_minibuffer(read_self_insert, read_select, read_abort,
479 &read_history, NULL, NULL);
481 len = sizeof(ministate.prompt);
482 strlcpy(ministate.prompt, prompt, len);
483 strlcat(ministate.prompt, ": ", len);