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 static void
299 yornp_self_insert(void)
301 if (thiskey.key != 'y' && thiskey.key != 'n') {
302 message("Please answer y or n");
303 return;
306 exit_minibuffer();
307 yornp_cb(thiskey.key == 'y', yornp_data);
310 static void
311 yornp_abort(void)
313 exit_minibuffer();
314 yornp_cb(0, yornp_data);
317 static void
318 read_self_insert(void)
320 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
321 global_key_unbound();
322 return;
325 minibuffer_self_insert();
328 static void
329 read_abort(void)
331 exit_minibuffer();
332 read_cb(NULL, read_data);
335 static void
336 read_select(void)
338 exit_minibuffer();
339 minibuffer_hist_save_entry();
340 read_cb(ministate.buf, read_data);
343 /*
344 * TODO: we should collect this asynchronously...
345 */
346 static inline void
347 populate_compl_buffer(complfn *fn, void *data)
349 const char *s;
350 struct line *l;
351 struct buffer *b;
352 struct parser *p;
353 void *linedata = NULL;
355 b = &ministate.compl.buffer;
356 p = &b->page;
358 while ((s = fn(&data, &linedata)) != NULL) {
359 if ((l = calloc(1, sizeof(*l))) == NULL)
360 abort();
362 l->type = LINE_COMPL;
363 l->meta.data = linedata;
364 if ((l->line = strdup(s)) == NULL)
365 abort();
367 if (TAILQ_EMPTY(&p->head))
368 TAILQ_INSERT_HEAD(&p->head, l, lines);
369 else
370 TAILQ_INSERT_TAIL(&p->head, l, lines);
372 linedata = NULL;
375 if ((l = TAILQ_FIRST(&p->head)) != NULL)
376 l->type = LINE_COMPL_CURRENT;
379 void
380 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
381 void (*abortfn)(void), struct histhead *hist,
382 complfn *complfn, void *compldata)
384 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
385 if (in_minibuffer == MB_COMPREAD) {
386 ui_schedule_redraw();
388 ministate.compl.fn = complfn;
389 ministate.compl.data = compldata;
390 populate_compl_buffer(complfn, compldata);
393 base_map = &minibuffer_map;
394 current_map = &minibuffer_map;
396 base_map->unhandled_input = self_insert_fn;
398 ministate.donefn = donefn;
399 ministate.abortfn = abortfn;
400 memset(ministate.buf, 0, sizeof(ministate.buf));
401 ministate.buffer.current_line = &ministate.vline;
402 ministate.buffer.current_line->line = ministate.buf;
403 ministate.buffer.cpoff = 0;
404 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
406 ministate.history = hist;
407 ministate.hist_cur = NULL;
408 ministate.hist_off = 0;
411 void
412 exit_minibuffer(void)
414 if (in_minibuffer == MB_COMPREAD) {
415 erase_buffer(&ministate.compl.buffer);
416 ui_schedule_redraw();
419 in_minibuffer = 0;
420 base_map = &global_map;
421 current_map = &global_map;
424 void
425 yornp(const char *prompt, void (*fn)(int, struct tab*),
426 struct tab *data)
428 size_t len;
430 if (in_minibuffer) {
431 fn(0, data);
432 return;
435 yornp_cb = fn;
436 yornp_data = data;
437 enter_minibuffer(yornp_self_insert, yornp_self_insert,
438 yornp_abort, NULL, NULL, NULL);
440 len = sizeof(ministate.prompt);
441 strlcpy(ministate.prompt, prompt, len);
442 strlcat(ministate.prompt, " (y or n) ", len);
445 /*
446 * Not yet "completing", but soon maybe...
447 */
448 void
449 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
450 struct tab *data)
452 size_t len;
454 if (in_minibuffer)
455 return;
457 read_cb = fn;
458 read_data = data;
459 enter_minibuffer(read_self_insert, read_select, read_abort,
460 &read_history, NULL, NULL);
462 len = sizeof(ministate.prompt);
463 strlcpy(ministate.prompt, prompt, len);
464 strlcat(ministate.prompt, ": ", len);