2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
20 #include "minibuffer.h"
24 static void minibuffer_hist_save_entry(void);
25 static void minibuffer_self_insert(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,
43 struct ministate ministate;
45 struct buffer minibufferwin;
48 * Recompute the visible completions. If add is 1, don't consider the
49 * ones already hidden.
52 recompute_completions(int add)
58 if (in_minibuffer != MB_COMPREAD)
61 b = &ministate.compl.buffer;
62 TAILQ_FOREACH(l, &b->page.head, lines) {
64 if (add && l->flags & L_HIDDEN)
66 if (strstr(l->line, ministate.buf) != NULL)
67 l->flags &= ~L_HIDDEN;
72 if (b->current_line == NULL)
73 b->current_line = TAILQ_FIRST(&b->head);
74 b->current_line = adjust_line(b->current_line, b);
77 vl->parent->type = LINE_COMPL_CURRENT;
81 minibuffer_hist_save_entry(void)
85 if (ministate.history == NULL)
88 if ((hist = calloc(1, sizeof(*hist))) == NULL)
91 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
93 if (TAILQ_EMPTY(&ministate.history->head))
94 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
96 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
97 ministate.history->len++;
101 * taint the minibuffer cache: if we're currently showing a history
102 * element, copy that to the current buf and reset the "history
106 minibuffer_taint_hist(void)
108 if (ministate.hist_cur == NULL)
111 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
112 ministate.hist_cur = NULL;
113 ministate.buffer.current_line->line = ministate.buf;
117 minibuffer_self_insert(void)
119 char *c, tmp[5] = {0};
122 minibuffer_taint_hist();
127 len = utf8_encode(thiskey.cp, tmp);
128 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
129 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
132 memmove(c + len, c, strlen(c)+1);
134 ministate.buffer.cpoff++;
136 recompute_completions(1);
140 eecmd_self_insert(void)
142 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
143 !unicode_isgraph(thiskey.cp)) {
144 global_key_unbound();
148 minibuffer_self_insert();
156 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
157 if (!strcmp(cmd->cmd, ministate.buf)) {
159 minibuffer_hist_save_entry();
160 cmd->fn(current_buffer());
171 minibuffer_self_insert();
177 char buf[1025] = {0};
184 minibuffer_hist_save_entry();
186 /* a bit ugly but... */
187 memcpy(&uri, &tab->uri, sizeof(tab->uri));
188 phos_uri_set_query(&uri, ministate.buf);
189 phos_serialize_uri(&uri, buf, sizeof(buf));
190 load_url_in_tab(tab, buf);
196 if (thiskey.meta || unicode_isspace(thiskey.key) ||
197 !unicode_isgraph(thiskey.key)) {
198 global_key_unbound();
202 minibuffer_self_insert();
209 minibuffer_hist_save_entry();
210 load_url_in_tab(current_tab(), ministate.buf);
217 if (*ministate.buf != '\0')
218 add_to_bookmarks(ministate.buf);
224 yornp_self_insert(void)
226 if (thiskey.key != 'y' && thiskey.key != 'n') {
227 message("Please answer y or n");
232 yornp_cb(thiskey.key == 'y', yornp_data);
239 yornp_cb(0, yornp_data);
243 read_self_insert(void)
245 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
246 global_key_unbound();
250 minibuffer_self_insert();
257 read_cb(NULL, read_data);
264 minibuffer_hist_save_entry();
265 read_cb(ministate.buf, read_data);
269 * TODO: we should collect this asynchronously...
272 populate_compl_buffer(complfn *fn, void *data)
279 b = &ministate.compl.buffer;
282 while ((s = fn(&data)) != NULL) {
283 if ((l = calloc(1, sizeof(*l))) == NULL)
286 l->type = LINE_COMPL;
287 if ((l->line = strdup(s)) == NULL)
290 if (TAILQ_EMPTY(&p->head))
291 TAILQ_INSERT_HEAD(&p->head, l, lines);
293 TAILQ_INSERT_TAIL(&p->head, l, lines);
296 if ((l = TAILQ_FIRST(&p->head)) != NULL)
297 l->type = LINE_COMPL_CURRENT;
301 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
302 void (*abortfn)(void), struct histhead *hist,
303 complfn *complfn, void *compldata)
305 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
306 if (in_minibuffer == MB_COMPREAD) {
307 ui_schedule_redraw();
309 ministate.compl.fn = complfn;
310 ministate.compl.data = compldata;
311 populate_compl_buffer(complfn, compldata);
314 base_map = &minibuffer_map;
315 current_map = &minibuffer_map;
317 base_map->unhandled_input = self_insert_fn;
319 ministate.donefn = donefn;
320 ministate.abortfn = abortfn;
321 memset(ministate.buf, 0, sizeof(ministate.buf));
322 ministate.buffer.current_line = &ministate.vline;
323 ministate.buffer.current_line->line = ministate.buf;
324 ministate.buffer.cpoff = 0;
325 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
327 ministate.history = hist;
328 ministate.hist_cur = NULL;
329 ministate.hist_off = 0;
333 exit_minibuffer(void)
335 if (in_minibuffer == MB_COMPREAD) {
336 erase_buffer(&ministate.compl.buffer);
337 ui_schedule_redraw();
341 base_map = &global_map;
342 current_map = &global_map;
346 yornp(const char *prompt, void (*fn)(int, struct tab*),
358 enter_minibuffer(yornp_self_insert, yornp_self_insert,
359 yornp_abort, NULL, NULL, NULL);
361 len = sizeof(ministate.prompt);
362 strlcpy(ministate.prompt, prompt, len);
363 strlcat(ministate.prompt, " (y or n) ", len);
367 * Not yet "completing", but soon maybe...
370 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
380 enter_minibuffer(read_self_insert, read_select, read_abort,
381 &read_history, NULL, NULL);
383 len = sizeof(ministate.prompt);
384 strlcpy(ministate.prompt, prompt, len);
385 strlcat(ministate.prompt, ": ", len);