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 <limits.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "compl.h"
22 #include "defaults.h"
23 #include "minibuffer.h"
24 #include "telescope.h"
25 #include "ui.h"
26 #include "utf8.h"
28 #define GUARD_RECURSIVE_MINIBUFFER() \
29 do { \
30 if (in_minibuffer) { \
31 message("enable-recursive-minibuffers " \
32 "is not yet available."); \
33 return; \
34 } \
35 } while(0)
37 /* return 1 if moved, 0 otherwise */
38 static inline int
39 forward_line(struct buffer *buffer, int n)
40 {
41 struct vline *vl;
42 int did;
44 if (buffer->current_line == NULL)
45 return 0;
46 vl = buffer->current_line;
48 did = 0;
49 while (n != 0) {
50 if (n > 0) {
51 vl = TAILQ_NEXT(vl, vlines);
52 if (vl == NULL)
53 return did;
54 if (vl->parent->flags & L_HIDDEN)
55 continue;
56 buffer->current_line = vl;
57 n--;
58 } else {
59 vl = TAILQ_PREV(vl, vhead, vlines);
60 if (vl == NULL)
61 return did;
62 if (vl->parent->flags & L_HIDDEN)
63 continue;
64 if (buffer->current_line == buffer->top_line) {
65 buffer->line_off--;
66 buffer->top_line = vl;
67 }
68 buffer->current_line = vl;
69 n++;
70 }
72 did = 1;
73 }
75 return did;
76 }
78 void
79 cmd_previous_line(struct buffer *buffer)
80 {
81 forward_line(buffer, -1);
82 }
84 void
85 cmd_next_line(struct buffer *buffer)
86 {
87 forward_line(buffer, +1);
88 }
90 void
91 cmd_backward_char(struct buffer *buffer)
92 {
93 if (buffer->cpoff != 0)
94 buffer->cpoff--;
95 }
97 void
98 cmd_forward_char(struct buffer *buffer)
99 {
100 size_t len = 0;
102 if (buffer->current_line->line != NULL)
103 len = utf8_cplen(buffer->current_line->line);
104 if (++buffer->cpoff > len)
105 buffer->cpoff = len;
108 void
109 cmd_backward_paragraph(struct buffer *buffer)
111 do {
112 if (!forward_line(buffer, -1)) {
113 message("No previous paragraph");
114 return;
116 } while (buffer->current_line->line != NULL ||
117 buffer->current_line->parent->type != LINE_TEXT);
120 void
121 cmd_forward_paragraph(struct buffer *buffer)
123 do {
124 if (!forward_line(buffer, +1)) {
125 message("No next paragraph");
126 return;
128 } while (buffer->current_line->line != NULL ||
129 buffer->current_line->parent->type != LINE_TEXT);
132 void
133 cmd_move_beginning_of_line(struct buffer *buffer)
135 buffer->cpoff = 0;
138 void
139 cmd_move_end_of_line(struct buffer *buffer)
141 struct vline *vl;
143 vl = buffer->current_line;
144 if (vl->line == NULL)
145 return;
146 buffer->cpoff = utf8_cplen(vl->line);
149 void
150 cmd_redraw(struct buffer *buffer)
152 ui_schedule_redraw();
155 void
156 cmd_scroll_line_up(struct buffer *buffer)
158 struct vline *vl;
160 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
161 == NULL)
162 return;
164 forward_line(buffer, -1);
166 buffer->top_line = vl;
169 void
170 cmd_scroll_line_down(struct buffer *buffer)
172 if (!forward_line(buffer, +1))
173 return;
175 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
176 buffer->line_off++;
179 void
180 cmd_scroll_up(struct buffer *buffer)
182 struct vline *vl;
183 int i;
185 for (i = 0; i < body_lines; ++i) {
186 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
187 if (vl == NULL)
188 break;
189 buffer->top_line = vl;
190 forward_line(buffer, -1);
194 void
195 cmd_scroll_down(struct buffer *buffer)
197 int i;
199 for (i = 0; i < body_lines; ++i) {
200 if (!forward_line(buffer, +1))
201 break;
203 buffer->top_line = TAILQ_NEXT(buffer->top_line,
204 vlines);
208 void
209 cmd_beginning_of_buffer(struct buffer *buffer)
211 buffer->current_line = TAILQ_FIRST(&buffer->head);
212 buffer->cpoff = 0;
213 buffer->top_line = buffer->current_line;
214 buffer->line_off = 0;
217 void
218 cmd_end_of_buffer(struct buffer *buffer)
220 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
221 buffer->cpoff = body_cols;
224 void
225 cmd_kill_telescope(struct buffer *buffer)
227 save_session();
228 event_loopbreak();
231 void
232 cmd_push_button(struct buffer *buffer)
234 struct vline *vl;
235 struct line *l;
237 vl = buffer->current_line;
238 switch (vl->parent->type) {
239 case LINE_LINK:
240 load_url_in_tab(current_tab(), vl->parent->alt);
241 break;
242 case LINE_PRE_START:
243 l = TAILQ_NEXT(vl->parent, lines);
244 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
245 if (l->type == LINE_PRE_END)
246 break;
247 l->flags ^= L_HIDDEN;
248 if (l->flags & L_HIDDEN)
249 buffer->line_max--;
250 else
251 buffer->line_max++;
253 break;
254 default:
255 break;
259 void
260 cmd_push_button_new_tab(struct buffer *buffer)
262 struct vline *vl;
264 vl = buffer->current_line;
265 if (vl->parent->type != LINE_LINK)
266 return;
268 new_tab(vl->parent->alt);
271 void
272 cmd_previous_button(struct buffer *buffer)
274 struct excursion place;
276 save_excursion(&place, buffer);
278 do {
279 if (!forward_line(buffer, -1)) {
280 restore_excursion(&place, buffer);
281 message("No previous link");
282 return;
284 } while (buffer->current_line->parent->type != LINE_LINK);
287 void
288 cmd_next_button(struct buffer *buffer)
290 struct excursion place;
292 save_excursion(&place, buffer);
294 do {
295 if (!forward_line(buffer, +1)){
296 restore_excursion(&place, buffer);
297 message("No next link");
298 return;
300 } while (buffer->current_line->parent->type != LINE_LINK);
303 static inline int
304 is_heading(const struct line *l)
306 return l->type == LINE_TITLE_1 ||
307 l->type == LINE_TITLE_2 ||
308 l->type == LINE_TITLE_3;
311 void
312 cmd_previous_heading(struct buffer *buffer)
314 struct excursion place;
316 save_excursion(&place, buffer);
318 do {
319 if (!forward_line(buffer, -1)) {
320 restore_excursion(&place, buffer);
321 message("No previous heading");
322 return;
324 } while (!is_heading(buffer->current_line->parent));
327 void
328 cmd_next_heading(struct buffer *buffer)
330 struct excursion place;
332 save_excursion(&place, buffer);
334 do {
335 if (!forward_line(buffer, +1)) {
336 restore_excursion(&place, buffer);
337 message("No next heading");
338 return;
340 } while (!is_heading(buffer->current_line->parent));
343 void
344 cmd_previous_page(struct buffer *buffer)
346 struct tab *tab = current_tab();
348 if (!load_previous_page(tab))
349 message("No previous page");
350 else
351 start_loading_anim(tab);
354 void
355 cmd_next_page(struct buffer *buffer)
357 struct tab *tab = current_tab();
359 if (!load_next_page(tab))
360 message("No next page");
361 else
362 start_loading_anim(tab);
365 void
366 cmd_clear_minibuf(struct buffer *buffer)
368 message(NULL);
371 void
372 cmd_execute_extended_command(struct buffer *buffer)
374 size_t len;
376 GUARD_RECURSIVE_MINIBUFFER();
378 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
379 &eecmd_history, compl_eecmd, NULL);
381 len = sizeof(ministate.prompt);
382 strlcpy(ministate.prompt, "", len);
384 if (thiskey.meta)
385 strlcat(ministate.prompt, "M-", len);
387 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
389 if (thiskey.meta)
390 strlcat(ministate.prompt, " ", len);
393 void
394 cmd_tab_close(struct buffer *buffer)
396 struct tab *tab, *t;
398 tab = current_tab();
399 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
400 TAILQ_NEXT(tab, tabs) == NULL) {
401 message("Can't close the only tab.");
402 return;
405 if (evtimer_pending(&tab->loadingev, NULL))
406 evtimer_del(&tab->loadingev);
408 stop_tab(tab);
410 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
411 t = TAILQ_NEXT(tab, tabs);
412 TAILQ_REMOVE(&tabshead, tab, tabs);
413 free(tab);
415 switch_to_tab(t);
418 void
419 cmd_tab_close_other(struct buffer *buffer)
421 struct tab *t, *i;
423 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
424 if (t->flags & TAB_CURRENT)
425 continue;
427 stop_tab(t);
428 TAILQ_REMOVE(&tabshead, t, tabs);
429 free(t);
433 void
434 cmd_tab_new(struct buffer *buffer)
436 const char *url;
438 if ((url = new_tab_url) == NULL)
439 url = NEW_TAB_URL;
441 new_tab(url);
444 void
445 cmd_tab_next(struct buffer *buffer)
447 struct tab *tab, *t;
449 tab = current_tab();
450 tab->flags &= ~TAB_CURRENT;
452 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
453 t = TAILQ_FIRST(&tabshead);
454 t->flags |= TAB_CURRENT;
455 t->flags &= ~TAB_URGENT;
458 void
459 cmd_tab_previous(struct buffer *buffer)
461 struct tab *tab, *t;
463 tab = current_tab();
464 tab->flags &= ~TAB_CURRENT;
466 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
467 t = TAILQ_LAST(&tabshead, tabshead);
468 t->flags |= TAB_CURRENT;
469 t->flags &= ~TAB_URGENT;
472 void
473 cmd_tab_move(struct buffer *buffer)
475 struct tab *tab, *t;
477 tab = current_tab();
478 t = TAILQ_NEXT(tab, tabs);
479 TAILQ_REMOVE(&tabshead, tab, tabs);
481 if (t == NULL)
482 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
483 else
484 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
487 void
488 cmd_tab_move_to(struct buffer *buffer)
490 struct tab *tab, *t;
492 tab = current_tab();
493 t = TAILQ_PREV(tab, tabshead, tabs);
494 TAILQ_REMOVE(&tabshead, tab, tabs);
496 if (t == NULL) {
497 if (TAILQ_EMPTY(&tabshead))
498 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
499 else
500 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
501 } else
502 TAILQ_INSERT_BEFORE(t, tab, tabs);
505 void
506 cmd_tab_select(struct buffer *buffer)
508 GUARD_RECURSIVE_MINIBUFFER();
510 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
511 NULL, compl_ts, NULL);
512 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
515 void
516 cmd_load_url(struct buffer *buffer)
518 GUARD_RECURSIVE_MINIBUFFER();
520 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
521 &lu_history, NULL, NULL);
522 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
523 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
524 cmd_move_end_of_line(&ministate.buffer);
527 void
528 cmd_load_current_url(struct buffer *buffer)
530 struct tab *tab = current_tab();
532 GUARD_RECURSIVE_MINIBUFFER();
534 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
535 &lu_history, NULL, NULL);
536 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
537 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
538 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
541 void
542 cmd_reload_page(struct buffer *buffer)
544 struct tab *tab;
546 tab = current_tab();
547 load_url_in_tab(tab, tab->hist_cur->h);
550 void
551 cmd_bookmark_page(struct buffer *buffer)
553 struct tab *tab = current_tab();
555 GUARD_RECURSIVE_MINIBUFFER();
557 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
558 NULL, NULL);
559 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
560 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
561 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
564 void
565 cmd_list_bookmarks(struct buffer *buffer)
567 load_url_in_tab(current_tab(), "about:bookmarks");
570 void
571 cmd_toggle_help(struct buffer *buffer)
573 ui_toggle_side_window();
576 void
577 cmd_link_select(struct buffer *buffer)
579 struct line *l;
581 GUARD_RECURSIVE_MINIBUFFER();
583 l = TAILQ_FIRST(&buffer->page.head);
584 while (l != NULL && l->type != LINE_LINK)
585 l = TAILQ_NEXT(l, lines);
587 if (l == NULL) {
588 message("No links found");
589 return;
592 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
593 NULL, compl_ls, l);
594 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
597 void
598 cmd_swiper(struct buffer *buffer)
600 GUARD_RECURSIVE_MINIBUFFER();
602 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
603 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
604 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
607 void
608 cmd_toc(struct buffer *buffer)
610 struct line *l;
612 GUARD_RECURSIVE_MINIBUFFER();
614 l = TAILQ_FIRST(&buffer->page.head);
615 while (l != NULL &&
616 l->type != LINE_TITLE_1 &&
617 l->type != LINE_TITLE_2 &&
618 l->type != LINE_TITLE_3)
619 l = TAILQ_NEXT(l, lines);
621 if (l == NULL) {
622 message("No headings found");
623 return;
626 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
627 NULL, compl_toc, l);
628 strlcpy(ministate.prompt, "Select heading: ",
629 sizeof(ministate.prompt));
632 void
633 cmd_inc_fill_column(struct buffer *buffer)
635 if (fill_column == INT_MAX)
636 return;
638 fill_column += 2;
639 message("fill-column: %d", fill_column);
641 ui_schedule_redraw();
644 void
645 cmd_dec_fill_column(struct buffer *buffer)
647 if (fill_column == INT_MAX || fill_column < 8)
648 return;
650 fill_column -= 2;
651 message("fill-column: %d", fill_column);
653 ui_schedule_redraw();
656 void
657 cmd_olivetti_mode(struct buffer *buffer)
659 olivetti_mode = !olivetti_mode;
660 if (olivetti_mode)
661 message("olivetti-mode enabled");
662 else
663 message("olivetti-mode disabled");
665 ui_schedule_redraw();
668 void
669 cmd_mini_delete_char(struct buffer *buffer)
671 char *c, *n;
673 if (!in_minibuffer) {
674 message("text is read-only");
675 return;
678 minibuffer_taint_hist();
680 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
681 if (*c == '\0')
682 return;
683 n = utf8_next_cp(c);
685 memmove(c, n, strlen(n)+1);
687 recompute_completions(0);
690 void
691 cmd_mini_delete_backward_char(struct buffer *buffer)
693 char *c, *p, *start;
695 if (!in_minibuffer) {
696 message("text is read-only");
697 return;
700 minibuffer_taint_hist();
702 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
703 start = buffer->current_line->line;
704 if (c == start)
705 return;
706 p = utf8_prev_cp(c-1, start);
708 memmove(p, c, strlen(c)+1);
709 buffer->cpoff--;
711 recompute_completions(0);
714 void
715 cmd_mini_kill_line(struct buffer *buffer)
717 char *c;
719 if (!in_minibuffer) {
720 message("text is read-only");
721 return;
724 minibuffer_taint_hist();
725 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
726 *c = '\0';
728 recompute_completions(0);
731 void
732 cmd_mini_abort(struct buffer *buffer)
734 if (!in_minibuffer)
735 return;
737 ministate.abortfn();
740 void
741 cmd_mini_complete_and_exit(struct buffer *buffer)
743 if (!in_minibuffer)
744 return;
746 minibuffer_taint_hist();
747 ministate.donefn();
750 void
751 cmd_mini_previous_history_element(struct buffer *buffer)
753 if (ministate.history == NULL) {
754 message("No history");
755 return;
758 if (ministate.hist_cur == NULL ||
759 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
760 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
761 ministate.hist_off = ministate.history->len - 1;
762 if (ministate.hist_cur == NULL)
763 message("No prev history item");
764 } else {
765 ministate.hist_off--;
768 if (ministate.hist_cur != NULL)
769 buffer->current_line->line = ministate.hist_cur->h;
772 void
773 cmd_mini_next_history_element(struct buffer *buffer)
775 if (ministate.history == NULL) {
776 message("No history");
777 return;
780 if (ministate.hist_cur == NULL ||
781 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
782 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
783 ministate.hist_off = 0;
784 if (ministate.hist_cur == NULL)
785 message("No next history item");
786 } else {
787 ministate.hist_off++;
790 if (ministate.hist_cur != NULL)
791 buffer->current_line->line = ministate.hist_cur->h;
794 void
795 cmd_previous_completion(struct buffer *buffer)
797 if (in_minibuffer != MB_COMPREAD)
798 return;
800 buffer = &ministate.compl.buffer;
802 if (buffer->current_line != NULL)
803 buffer->current_line->parent->type = LINE_COMPL;
805 forward_line(buffer, -1);
807 if (buffer->current_line != NULL)
808 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
811 void
812 cmd_next_completion(struct buffer *buffer)
814 if (in_minibuffer != MB_COMPREAD)
815 return;
817 buffer = &ministate.compl.buffer;
819 if (buffer->current_line != NULL)
820 buffer->current_line->parent->type = LINE_COMPL;
822 forward_line(buffer, +1);
824 if (buffer->current_line != NULL)
825 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
828 void
829 cmd_insert_current_candidate(struct buffer *buffer)
831 struct vline *vl;
833 if (in_minibuffer != MB_COMPREAD)
834 return;
836 buffer = &ministate.compl.buffer;
837 if ((vl = buffer->current_line) == NULL)
838 return;
840 minibuffer_taint_hist();
841 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
842 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
845 void
846 cmd_suspend_telescope(struct buffer *buffer)
848 message("Zzz...");
849 ui_suspend();
852 void
853 cmd_toggle_pre_wrap(struct buffer *buffer)
855 dont_wrap_pre = !dont_wrap_pre;
857 if (dont_wrap_pre)
858 message("Don't wrap preformatted blocks");
859 else
860 message("Wrap preformatted blocks");
862 ui_schedule_redraw();