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_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,
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;
56 if (in_minibuffer != MB_COMPREAD)
57 return;
59 TAILQ_FOREACH(l, &ministate.compl.buffer.page.head, lines) {
60 if (add && l->flags & L_HIDDEN)
61 continue;
62 if (strstr(l->line, ministate.buf) != NULL)
63 l->flags &= ~L_HIDDEN;
64 else
65 l->flags |= L_HIDDEN;
66 }
67 }
69 static void
70 minibuffer_hist_save_entry(void)
71 {
72 struct hist *hist;
74 if (ministate.history == NULL)
75 return;
77 if ((hist = calloc(1, sizeof(*hist))) == NULL)
78 abort();
80 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
82 if (TAILQ_EMPTY(&ministate.history->head))
83 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
84 else
85 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
86 ministate.history->len++;
87 }
89 /*
90 * taint the minibuffer cache: if we're currently showing a history
91 * element, copy that to the current buf and reset the "history
92 * navigation" thing.
93 */
94 void
95 minibuffer_taint_hist(void)
96 {
97 if (ministate.hist_cur == NULL)
98 return;
100 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
101 ministate.hist_cur = NULL;
102 ministate.buffer.current_line->line = ministate.buf;
105 static void
106 minibuffer_self_insert(void)
108 char *c, tmp[5] = {0};
109 size_t len;
111 minibuffer_taint_hist();
113 if (thiskey.cp == 0)
114 return;
116 len = utf8_encode(thiskey.cp, tmp);
117 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
118 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
119 return;
121 memmove(c + len, c, strlen(c)+1);
122 memcpy(c, tmp, len);
123 ministate.buffer.cpoff++;
125 recompute_completions(1);
128 void
129 eecmd_self_insert(void)
131 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
132 !unicode_isgraph(thiskey.cp)) {
133 global_key_unbound();
134 return;
137 minibuffer_self_insert();
140 void
141 eecmd_select(void)
143 struct cmd *cmd;
145 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
146 if (!strcmp(cmd->cmd, ministate.buf)) {
147 exit_minibuffer();
148 minibuffer_hist_save_entry();
149 cmd->fn(current_buffer());
150 return;
154 message("No match");
157 void
158 ir_self_insert(void)
160 minibuffer_self_insert();
163 void
164 ir_select(void)
166 char buf[1025] = {0};
167 struct phos_uri uri;
168 struct tab *tab;
170 tab = current_tab();
172 exit_minibuffer();
173 minibuffer_hist_save_entry();
175 /* a bit ugly but... */
176 memcpy(&uri, &tab->uri, sizeof(tab->uri));
177 phos_uri_set_query(&uri, ministate.buf);
178 phos_serialize_uri(&uri, buf, sizeof(buf));
179 load_url_in_tab(tab, buf);
182 void
183 lu_self_insert(void)
185 if (thiskey.meta || unicode_isspace(thiskey.key) ||
186 !unicode_isgraph(thiskey.key)) {
187 global_key_unbound();
188 return;
191 minibuffer_self_insert();
194 void
195 lu_select(void)
197 exit_minibuffer();
198 minibuffer_hist_save_entry();
199 load_url_in_tab(current_tab(), ministate.buf);
202 void
203 bp_select(void)
205 exit_minibuffer();
206 if (*ministate.buf != '\0')
207 add_to_bookmarks(ministate.buf);
208 else
209 message("Abort.");
212 static void
213 yornp_self_insert(void)
215 if (thiskey.key != 'y' && thiskey.key != 'n') {
216 message("Please answer y or n");
217 return;
220 exit_minibuffer();
221 yornp_cb(thiskey.key == 'y', yornp_data);
224 static void
225 yornp_abort(void)
227 exit_minibuffer();
228 yornp_cb(0, yornp_data);
231 static void
232 read_self_insert(void)
234 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
235 global_key_unbound();
236 return;
239 minibuffer_self_insert();
242 static void
243 read_abort(void)
245 exit_minibuffer();
246 read_cb(NULL, read_data);
249 static void
250 read_select(void)
252 exit_minibuffer();
253 minibuffer_hist_save_entry();
254 read_cb(ministate.buf, read_data);
257 /*
258 * Just like erase_buffer, but don't call free(l->line);
259 */
260 static inline void
261 erase_compl_buffer(struct buffer *buffer)
263 struct line *l, *lt;
265 empty_vlist(buffer);
267 TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
268 TAILQ_REMOVE(&buffer->page.head, l, lines);
269 /* don't free l->line! */
270 free(l);
274 /*
275 * TODO: we should collect this asynchronously...
276 */
277 static inline void
278 populate_compl_buffer(complfn *fn, void *data)
280 const char *s;
281 struct line *l;
282 struct buffer *b;
283 struct parser *p;
285 b = &ministate.compl.buffer;
286 p = &b->page;
288 while ((s = fn(&data)) != NULL) {
289 if ((l = calloc(1, sizeof(*l))) == NULL)
290 abort();
292 l->type = LINE_TEXT;
293 l->line = s;
295 if (TAILQ_EMPTY(&p->head))
296 TAILQ_INSERT_HEAD(&p->head, l, lines);
297 else
298 TAILQ_INSERT_TAIL(&p->head, l, lines);
302 void
303 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
304 void (*abortfn)(void), struct histhead *hist,
305 complfn *complfn, void *compldata)
307 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
308 if (in_minibuffer == MB_COMPREAD) {
309 ui_schedule_redraw();
311 erase_compl_buffer(&ministate.compl.buffer);
313 ministate.compl.fn = complfn;
314 ministate.compl.data = compldata;
315 populate_compl_buffer(complfn, compldata);
318 base_map = &minibuffer_map;
319 current_map = &minibuffer_map;
321 base_map->unhandled_input = self_insert_fn;
323 ministate.donefn = donefn;
324 ministate.abortfn = abortfn;
325 memset(ministate.buf, 0, sizeof(ministate.buf));
326 ministate.buffer.current_line = &ministate.vline;
327 ministate.buffer.current_line->line = ministate.buf;
328 ministate.buffer.cpoff = 0;
329 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
331 ministate.history = hist;
332 ministate.hist_cur = NULL;
333 ministate.hist_off = 0;
336 void
337 exit_minibuffer(void)
339 if (in_minibuffer == MB_COMPREAD)
340 ui_schedule_redraw();
342 in_minibuffer = 0;
343 base_map = &global_map;
344 current_map = &global_map;
347 void
348 yornp(const char *prompt, void (*fn)(int, struct tab*),
349 struct tab *data)
351 size_t len;
353 if (in_minibuffer) {
354 fn(0, data);
355 return;
358 yornp_cb = fn;
359 yornp_data = data;
360 enter_minibuffer(yornp_self_insert, yornp_self_insert,
361 yornp_abort, NULL, NULL, NULL);
363 len = sizeof(ministate.prompt);
364 strlcpy(ministate.prompt, prompt, len);
365 strlcat(ministate.prompt, " (y or n) ", len);
368 /*
369 * Not yet "completing", but soon maybe...
370 */
371 void
372 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
373 struct tab *data)
375 size_t len;
377 if (in_minibuffer)
378 return;
380 read_cb = fn;
381 read_data = data;
382 enter_minibuffer(read_self_insert, read_select, read_abort,
383 &read_history, NULL, NULL);
385 len = sizeof(ministate.prompt);
386 strlcpy(ministate.prompt, prompt, len);
387 strlcat(ministate.prompt, ": ", len);