002
2021-07-12
op
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
004
2021-07-12
op
* Permission to use, copy, modify, and distribute this software for any
005
2021-07-12
op
* purpose with or without fee is hereby granted, provided that the above
006
2021-07-12
op
* copyright notice and this permission notice appear in all copies.
008
2021-07-12
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2021-07-12
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2021-07-12
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2021-07-12
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2021-07-12
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2021-07-12
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2021-07-12
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017
2021-07-21
op
#include "compat.h"
019
2022-04-24
op
#include <errno.h>
020
2022-04-24
op
#include <limits.h>
021
2021-07-21
op
#include <stdio.h>
022
2021-07-12
op
#include <stdlib.h>
023
2021-07-12
op
#include <string.h>
025
2022-04-24
op
#include "fs.h"
026
2021-07-12
op
#include "minibuffer.h"
027
2021-08-14
op
#include "session.h"
028
2021-07-12
op
#include "ui.h"
029
2021-07-12
op
#include "utf8.h"
030
2022-01-11
op
#include "utils.h"
032
2022-02-26
op
#define nitems(x) (sizeof(x)/sizeof(x[0]))
034
2021-07-15
op
static void *minibuffer_metadata(void);
035
2022-04-13
op
static const char *minibuffer_compl_text(void);
036
2021-07-12
op
static void minibuffer_hist_save_entry(void);
037
2021-07-12
op
static void yornp_self_insert(void);
038
2021-07-12
op
static void yornp_abort(void);
039
2021-07-12
op
static void read_self_insert(void);
040
2021-07-12
op
static void read_abort(void);
041
2021-07-12
op
static void read_select(void);
042
2021-07-21
op
static void handle_clear_echoarea(int, short, void *);
044
2021-07-21
op
static struct event clechoev;
045
2021-07-21
op
static struct timeval clechoev_timer = { 5, 0 };
047
2021-07-12
op
static void (*yornp_cb)(int, struct tab *);
048
2021-07-12
op
static struct tab *yornp_data;
050
2021-07-13
op
static void (*read_cb)(const char*, struct tab *);
051
2021-07-13
op
static struct tab *read_data;
053
2021-08-12
op
/* XXX: don't forget to init these in minibuffer_init */
054
2021-07-12
op
struct histhead eecmd_history,
055
2021-07-12
op
ir_history,
056
2021-07-12
op
lu_history,
057
2021-07-12
op
read_history;
059
2021-07-12
op
struct ministate ministate;
061
2021-07-14
op
struct buffer minibufferwin;
063
2021-07-21
op
int in_minibuffer;
065
2022-02-26
op
static inline int
066
2022-02-26
op
matches(char **words, size_t len, struct line *l)
068
2022-02-26
op
size_t i;
069
2022-02-26
op
int lm, am;
071
2022-02-26
op
for (i = 0; i < len; ++i) {
072
2022-02-26
op
lm = am = 0;
074
2022-02-26
op
if (strcasestr(l->line, words[i]) != NULL)
076
2022-02-26
op
if (l->alt != NULL &&
077
2022-02-26
op
strcasestr(l->alt, words[i]) != NULL)
080
2022-02-26
op
if (!lm && !am)
081
2022-02-26
op
return 0;
084
2022-02-26
op
return 1;
088
2021-07-14
op
* Recompute the visible completions. If add is 1, don't consider the
089
2021-07-14
op
* ones already hidden.
092
2021-07-14
op
recompute_completions(int add)
094
2022-02-26
op
static char buf[GEMINI_URL_LEN];
095
2022-04-15
op
const char *text;
096
2022-02-26
op
char *input, **ap, *words[10];
097
2022-02-26
op
size_t len = 0;
098
2021-07-14
op
struct line *l;
099
2022-04-13
op
struct vline *vl;
100
2021-07-14
op
struct buffer *b;
102
2021-07-14
op
if (in_minibuffer != MB_COMPREAD)
105
2022-04-15
op
if (ministate.hist_cur != NULL)
106
2022-04-15
op
text = ministate.hist_cur->h;
108
2022-04-15
op
text = ministate.buf;
110
2022-04-15
op
strlcpy(buf, text, sizeof(buf));
111
2022-02-26
op
input = buf;
113
2022-02-26
op
/* tokenize the input */
114
2022-02-26
op
for (ap = words; ap < words + nitems(words) &&
115
2022-02-26
op
(*ap = strsep(&input, " ")) != NULL;) {
116
2022-02-26
op
if (**ap != '\0')
117
2022-02-26
op
ap++, len++;
120
2021-07-14
op
b = &ministate.compl.buffer;
121
2021-07-14
op
TAILQ_FOREACH(l, &b->page.head, lines) {
122
2021-07-14
op
l->type = LINE_COMPL;
123
2021-07-14
op
if (add && l->flags & L_HIDDEN)
124
2021-07-14
op
continue;
125
2022-02-26
op
if (matches(words, len, l)) {
126
2021-07-15
op
if (l->flags & L_HIDDEN)
127
2021-07-15
op
b->line_max++;
128
2021-07-14
op
l->flags &= ~L_HIDDEN;
130
2021-07-15
op
if (!(l->flags & L_HIDDEN))
131
2021-07-15
op
b->line_max--;
132
2021-07-14
op
l->flags |= L_HIDDEN;
136
2021-07-14
op
if (b->current_line == NULL)
137
2021-07-14
op
b->current_line = TAILQ_FIRST(&b->head);
138
2021-07-14
op
b->current_line = adjust_line(b->current_line, b);
139
2022-04-13
op
vl = b->current_line;
140
2022-04-14
op
if (ministate.compl.must_select && vl != NULL)
141
2022-04-13
op
vl->parent->type = LINE_COMPL_CURRENT;
145
2021-01-02
op
minibuffer_insert_current_candidate(void)
147
2021-01-02
op
struct vline *vl;
149
2021-01-02
op
vl = ministate.compl.buffer.current_line;
150
2021-01-02
op
if (vl == NULL || vl->parent->flags & L_HIDDEN)
151
2021-01-02
op
return -1;
153
2021-01-02
op
minibuffer_taint_hist();
154
2021-01-02
op
strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
155
2021-01-02
op
ministate.buffer.cpoff = utf8_cplen(ministate.buf);
157
2021-01-02
op
return 0;
160
2021-07-15
op
static void *
161
2021-07-15
op
minibuffer_metadata(void)
163
2021-07-15
op
struct vline *vl;
165
2021-07-15
op
vl = ministate.compl.buffer.current_line;
167
2021-07-15
op
if (vl == NULL || vl->parent->flags & L_HIDDEN)
168
2021-07-15
op
return NULL;
170
2021-07-16
op
return vl->parent->data;
173
2022-04-13
op
static const char *
174
2022-04-13
op
minibuffer_compl_text(void)
176
2022-04-13
op
struct vline *vl;
178
2022-04-15
op
if (ministate.hist_cur != NULL)
179
2022-04-15
op
return ministate.hist_cur->h;
181
2022-04-13
op
vl = ministate.compl.buffer.current_line;
182
2022-04-13
op
if (vl == NULL || vl->parent->flags & L_HIDDEN ||
183
2022-04-13
op
vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
184
2022-04-13
op
return ministate.buf;
185
2022-04-13
op
return vl->parent->line;
188
2021-07-12
op
static void
189
2021-07-12
op
minibuffer_hist_save_entry(void)
191
2021-07-12
op
struct hist *hist;
192
2022-04-13
op
const char *t;
194
2021-07-12
op
if (ministate.history == NULL)
197
2021-07-12
op
if ((hist = calloc(1, sizeof(*hist))) == NULL)
200
2022-04-13
op
t = minibuffer_compl_text();
201
2022-04-13
op
strlcpy(hist->h, t, sizeof(hist->h));
203
2021-08-12
op
TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
204
2021-07-12
op
ministate.history->len++;
208
2021-07-12
op
* taint the minibuffer cache: if we're currently showing a history
209
2021-07-12
op
* element, copy that to the current buf and reset the "history
210
2021-07-12
op
* navigation" thing.
213
2021-07-12
op
minibuffer_taint_hist(void)
215
2021-07-12
op
if (ministate.hist_cur == NULL)
218
2021-07-12
op
strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
219
2021-07-12
op
ministate.hist_cur = NULL;
220
2021-07-12
op
ministate.buffer.current_line->line = ministate.buf;
224
2021-07-12
op
minibuffer_self_insert(void)
226
2021-07-12
op
char *c, tmp[5] = {0};
227
2021-07-12
op
size_t len;
229
2021-07-12
op
minibuffer_taint_hist();
231
2021-07-12
op
if (thiskey.cp == 0)
234
2021-07-12
op
len = utf8_encode(thiskey.cp, tmp);
235
2021-07-12
op
c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
236
2021-07-12
op
if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
239
2021-07-12
op
memmove(c + len, c, strlen(c)+1);
240
2021-07-12
op
memcpy(c, tmp, len);
241
2021-07-12
op
ministate.buffer.cpoff++;
243
2021-07-14
op
recompute_completions(1);
247
2021-07-14
op
sensible_self_insert(void)
249
2021-07-20
op
if (thiskey.meta ||
250
2021-07-24
op
(!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
251
2021-07-12
op
global_key_unbound();
255
2021-07-12
op
minibuffer_self_insert();
259
2021-07-12
op
eecmd_select(void)
261
2021-01-02
op
struct cmd *cmd;
262
2021-01-02
op
const char *t;
264
2022-04-13
op
t = minibuffer_compl_text();
265
2021-07-12
op
for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
266
2021-01-02
op
if (!strcmp(cmd->cmd, t)) {
267
2021-07-12
op
minibuffer_hist_save_entry();
268
2022-04-13
op
exit_minibuffer();
269
2021-07-12
op
cmd->fn(current_buffer());
274
2021-07-12
op
message("No match");
278
2021-08-03
op
ir_select_gemini(void)
280
2021-07-12
op
char buf[1025] = {0};
281
2021-07-12
op
struct phos_uri uri;
282
2021-07-17
op
struct tab *tab = current_tab;
284
2021-07-12
op
minibuffer_hist_save_entry();
286
2021-07-12
op
/* a bit ugly but... */
287
2021-07-12
op
memcpy(&uri, &tab->uri, sizeof(tab->uri));
288
2022-04-13
op
phos_uri_set_query(&uri, minibuffer_compl_text());
289
2022-02-07
op
phos_serialize_uri(&uri, buf, sizeof(buf));
291
2022-04-13
op
exit_minibuffer();
292
2022-02-07
op
load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
296
2022-02-07
op
ir_select_reply(void)
298
2022-02-07
op
char buf[1025] = {0};
299
2022-02-07
op
struct phos_uri uri;
300
2022-02-07
op
struct tab *tab = current_tab;
302
2022-02-07
op
minibuffer_hist_save_entry();
304
2022-02-07
op
/* a bit ugly but... */
305
2022-02-07
op
strlcpy(buf, tab->last_input_url, sizeof(buf));
306
2022-02-07
op
phos_parse_absolute_uri(buf, &uri);
307
2022-04-13
op
phos_uri_set_query(&uri, minibuffer_compl_text());
308
2021-07-12
op
phos_serialize_uri(&uri, buf, sizeof(buf));
310
2022-04-13
op
exit_minibuffer();
311
2022-01-11
op
load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
315
2021-08-03
op
ir_select_gopher(void)
317
2021-08-03
op
minibuffer_hist_save_entry();
318
2022-04-13
op
gopher_send_search_req(current_tab, minibuffer_compl_text());
319
2022-04-13
op
exit_minibuffer();
323
2021-07-12
op
lu_select(void)
325
2021-08-16
op
char url[GEMINI_URL_LEN+1];
327
2021-07-12
op
minibuffer_hist_save_entry();
328
2022-04-13
op
humanify_url(minibuffer_compl_text(), url, sizeof(url));
329
2022-04-13
op
exit_minibuffer();
330
2022-01-11
op
load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
334
2021-07-12
op
bp_select(void)
336
2021-07-12
op
exit_minibuffer();
337
2022-04-24
op
if (*ministate.buf != '\0') {
338
2022-04-24
op
if (!bookmark_page(ministate.buf))
339
2022-04-24
op
message("failed to bookmark page: %s",
340
2022-04-24
op
strerror(errno));
342
2021-07-12
op
message("Abort.");
346
2021-07-14
op
ts_select(void)
348
2021-07-14
op
struct tab *tab;
350
2021-07-15
op
if ((tab = minibuffer_metadata()) == NULL) {
351
2021-07-14
op
message("No tab selected");
355
2021-07-14
op
exit_minibuffer();
356
2021-07-14
op
switch_to_tab(tab);
360
2021-07-14
op
ls_select(void)
362
2021-07-14
op
struct line *l;
364
2021-07-15
op
if ((l = minibuffer_metadata()) == NULL) {
365
2021-07-14
op
message("No link selected");
369
2021-07-14
op
exit_minibuffer();
370
2022-01-11
op
load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
373
2021-07-15
op
static inline void
374
2021-07-15
op
jump_to_line(struct line *l)
376
2021-07-14
op
struct vline *vl;
377
2021-07-21
op
struct buffer *buffer;
379
2021-07-21
op
buffer = current_buffer();
381
2021-07-21
op
TAILQ_FOREACH(vl, &buffer->head, vlines) {
382
2021-07-14
op
if (vl->parent == l)
386
2021-07-14
op
if (vl == NULL)
387
2021-07-15
op
message("Ops, %s error! Please report to %s",
388
2021-07-15
op
__func__, PACKAGE_BUGREPORT);
390
2021-07-21
op
buffer->top_line = vl;
391
2021-07-21
op
buffer->current_line = vl;
396
2021-07-15
op
swiper_select(void)
398
2021-07-15
op
struct line *l;
400
2021-07-15
op
if ((l = minibuffer_metadata()) == NULL) {
401
2021-07-15
op
message("No line selected");
405
2021-07-15
op
exit_minibuffer();
406
2021-07-15
op
jump_to_line(l);
410
2021-07-15
op
toc_select(void)
412
2021-07-15
op
struct line *l;
414
2021-07-15
op
if ((l = minibuffer_metadata()) == NULL) {
415
2021-07-15
op
message("No line selected");
419
2021-07-15
op
exit_minibuffer();
420
2021-07-15
op
jump_to_line(l);
423
2021-07-12
op
static void
424
2021-07-12
op
yornp_self_insert(void)
426
2021-07-12
op
if (thiskey.key != 'y' && thiskey.key != 'n') {
427
2021-07-12
op
message("Please answer y or n");
431
2021-07-12
op
exit_minibuffer();
432
2021-07-12
op
yornp_cb(thiskey.key == 'y', yornp_data);
435
2021-07-12
op
static void
436
2021-07-12
op
yornp_abort(void)
438
2021-07-12
op
exit_minibuffer();
439
2021-07-12
op
yornp_cb(0, yornp_data);
442
2021-07-12
op
static void
443
2021-07-12
op
read_self_insert(void)
445
2021-07-12
op
if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
446
2021-07-12
op
global_key_unbound();
450
2021-07-12
op
minibuffer_self_insert();
453
2021-07-12
op
static void
454
2021-07-12
op
read_abort(void)
456
2021-07-12
op
exit_minibuffer();
457
2021-07-12
op
read_cb(NULL, read_data);
460
2021-07-12
op
static void
461
2021-07-12
op
read_select(void)
463
2021-08-26
op
exit_minibuffer();
464
2021-07-12
op
minibuffer_hist_save_entry();
465
2021-07-12
op
read_cb(ministate.buf, read_data);
469
2021-07-14
op
* TODO: we should collect this asynchronously...
471
2021-07-14
op
static inline void
472
2021-07-14
op
populate_compl_buffer(complfn *fn, void *data)
474
2021-07-18
op
const char *s, *descr;
475
2021-07-14
op
struct line *l;
476
2021-07-14
op
struct buffer *b;
477
2021-07-14
op
struct parser *p;
478
2021-07-18
op
void *linedata;
480
2021-07-14
op
b = &ministate.compl.buffer;
481
2021-07-14
op
p = &b->page;
483
2021-07-18
op
linedata = NULL;
484
2021-07-18
op
descr = NULL;
485
2021-07-18
op
while ((s = fn(&data, &linedata, &descr)) != NULL) {
486
2021-07-14
op
if ((l = calloc(1, sizeof(*l))) == NULL)
489
2021-07-14
op
l->type = LINE_COMPL;
490
2021-07-16
op
l->data = linedata;
491
2021-07-18
op
l->alt = (char*)descr;
492
2021-07-14
op
if ((l->line = strdup(s)) == NULL)
495
2021-08-12
op
TAILQ_INSERT_TAIL(&p->head, l, lines);
497
2021-07-14
op
linedata = NULL;
498
2021-07-18
op
descr = NULL;
501
2022-04-13
op
if ((l = TAILQ_FIRST(&p->head)) != NULL &&
502
2022-04-13
op
ministate.compl.must_select)
503
2022-04-13
op
l->type = LINE_COMPL_CURRENT;
507
2021-07-12
op
enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
508
2021-07-13
op
void (*abortfn)(void), struct histhead *hist,
509
2022-04-13
op
complfn *complfn, void *compldata, int must_select)
511
2022-05-05
op
ministate.compl.must_select = must_select;
512
2022-05-05
op
ministate.compl.fn = complfn;
513
2022-05-05
op
ministate.compl.data = compldata;
515
2021-07-14
op
in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
516
2021-07-14
op
if (in_minibuffer == MB_COMPREAD) {
517
2022-05-05
op
populate_compl_buffer(complfn, compldata);
518
2021-07-14
op
ui_schedule_redraw();
521
2021-07-12
op
base_map = &minibuffer_map;
522
2021-07-12
op
current_map = &minibuffer_map;
524
2021-07-12
op
base_map->unhandled_input = self_insert_fn;
526
2021-07-12
op
ministate.donefn = donefn;
527
2021-07-12
op
ministate.abortfn = abortfn;
528
2021-07-12
op
memset(ministate.buf, 0, sizeof(ministate.buf));
529
2021-07-12
op
ministate.buffer.current_line = &ministate.vline;
530
2021-07-12
op
ministate.buffer.current_line->line = ministate.buf;
531
2021-07-12
op
ministate.buffer.cpoff = 0;
532
2021-07-12
op
strlcpy(ministate.buf, "", sizeof(ministate.prompt));
534
2021-07-12
op
ministate.history = hist;
535
2021-07-12
op
ministate.hist_cur = NULL;
536
2021-07-12
op
ministate.hist_off = 0;
540
2021-07-12
op
exit_minibuffer(void)
542
2021-07-14
op
if (in_minibuffer == MB_COMPREAD) {
543
2021-07-14
op
erase_buffer(&ministate.compl.buffer);
544
2021-07-14
op
ui_schedule_redraw();
547
2021-07-12
op
in_minibuffer = 0;
548
2021-07-12
op
base_map = &global_map;
549
2021-07-12
op
current_map = &global_map;
553
2021-07-12
op
yornp(const char *prompt, void (*fn)(int, struct tab*),
554
2021-07-12
op
struct tab *data)
556
2021-07-12
op
size_t len;
558
2021-07-12
op
if (in_minibuffer) {
559
2021-07-12
op
fn(0, data);
563
2021-07-12
op
yornp_cb = fn;
564
2021-07-12
op
yornp_data = data;
565
2021-07-12
op
enter_minibuffer(yornp_self_insert, yornp_self_insert,
566
2022-04-13
op
yornp_abort, NULL, NULL, NULL, 0);
568
2021-07-12
op
len = sizeof(ministate.prompt);
569
2021-07-12
op
strlcpy(ministate.prompt, prompt, len);
570
2021-07-12
op
strlcat(ministate.prompt, " (y or n) ", len);
574
2021-07-21
op
minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
575
2021-07-14
op
struct tab *data)
577
2021-07-12
op
size_t len;
579
2021-07-12
op
if (in_minibuffer)
582
2021-07-12
op
read_cb = fn;
583
2021-07-12
op
read_data = data;
584
2021-07-12
op
enter_minibuffer(read_self_insert, read_select, read_abort,
585
2022-04-13
op
&read_history, NULL, NULL, 0);
587
2021-07-12
op
len = sizeof(ministate.prompt);
588
2021-07-12
op
strlcpy(ministate.prompt, prompt, len);
589
2021-07-12
op
strlcat(ministate.prompt, ": ", len);
592
2021-07-21
op
static void
593
2021-07-21
op
handle_clear_echoarea(int fd, short ev, void *d)
595
2021-07-21
op
free(ministate.curmesg);
596
2021-07-21
op
ministate.curmesg = NULL;
598
2021-07-21
op
ui_after_message_hook();
602
2021-07-21
op
vmessage(const char *fmt, va_list ap)
604
2021-07-21
op
if (evtimer_pending(&clechoev, NULL))
605
2021-07-21
op
evtimer_del(&clechoev);
607
2021-07-21
op
free(ministate.curmesg);
608
2021-07-21
op
ministate.curmesg = NULL;
610
2021-07-21
op
if (fmt != NULL) {
611
2021-07-21
op
evtimer_set(&clechoev, handle_clear_echoarea, NULL);
612
2021-07-21
op
evtimer_add(&clechoev, &clechoev_timer);
614
2021-07-21
op
/* TODO: what to do if the allocation fails here? */
615
2021-07-21
op
if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
616
2021-07-21
op
ministate.curmesg = NULL;
619
2021-07-21
op
ui_after_message_hook();
623
2021-07-21
op
message(const char *fmt, ...)
625
2021-07-21
op
va_list ap;
627
2021-07-21
op
va_start(ap, fmt);
628
2021-07-21
op
vmessage(fmt, ap);
629
2021-07-21
op
va_end(ap);
633
2021-07-21
op
minibuffer_init(void)
635
2021-07-21
op
TAILQ_INIT(&eecmd_history.head);
636
2021-07-21
op
TAILQ_INIT(&ir_history.head);
637
2021-07-21
op
TAILQ_INIT(&lu_history.head);
638
2021-08-12
op
TAILQ_INIT(&read_history.head);
640
2021-08-12
op
TAILQ_INIT(&ministate.compl.buffer.head);
641
2021-08-12
op
TAILQ_INIT(&ministate.compl.buffer.page.head);
643
2021-07-21
op
ministate.line.type = LINE_TEXT;
644
2021-07-21
op
ministate.vline.parent = &ministate.line;
645
2021-07-25
op
ministate.buffer.page.name = "*minibuffer*";
646
2021-07-21
op
ministate.buffer.current_line = &ministate.vline;
648
2021-07-21
op
evtimer_set(&clechoev, handle_clear_echoarea, NULL);