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 #define GUARD_READ_ONLY() \
38 do { \
39 if (!in_minibuffer) { \
40 message("text is read-only"); \
41 return; \
42 } \
43 } while(0)
45 /* return 1 if moved, 0 otherwise */
46 static inline int
47 forward_line(struct buffer *buffer, int n)
48 {
49 struct vline *vl;
50 int did;
52 if (buffer->current_line == NULL)
53 return 0;
54 vl = buffer->current_line;
56 did = 0;
57 while (n != 0) {
58 if (n > 0) {
59 vl = TAILQ_NEXT(vl, vlines);
60 if (vl == NULL)
61 return did;
62 if (vl->parent->flags & L_HIDDEN)
63 continue;
64 buffer->current_line = vl;
65 n--;
66 } else {
67 vl = TAILQ_PREV(vl, vhead, vlines);
68 if (vl == NULL)
69 return did;
70 if (vl->parent->flags & L_HIDDEN)
71 continue;
72 if (buffer->current_line == buffer->top_line) {
73 buffer->line_off--;
74 buffer->top_line = vl;
75 }
76 buffer->current_line = vl;
77 n++;
78 }
80 did = 1;
81 }
83 return did;
84 }
86 void
87 cmd_previous_line(struct buffer *buffer)
88 {
89 forward_line(buffer, -1);
90 }
92 void
93 cmd_next_line(struct buffer *buffer)
94 {
95 forward_line(buffer, +1);
96 }
98 void
99 cmd_backward_char(struct buffer *buffer)
101 if (buffer->cpoff != 0)
102 buffer->cpoff--;
105 void
106 cmd_forward_char(struct buffer *buffer)
108 size_t len = 0;
110 if (buffer->current_line == NULL)
111 return;
113 if (buffer->current_line->line != NULL)
114 len = utf8_cplen(buffer->current_line->line);
115 if (++buffer->cpoff > len)
116 buffer->cpoff = len;
119 void
120 cmd_backward_paragraph(struct buffer *buffer)
122 do {
123 if (!forward_line(buffer, -1)) {
124 message("No previous paragraph");
125 return;
127 } while (buffer->current_line->line != NULL ||
128 buffer->current_line->parent->type != LINE_TEXT);
131 void
132 cmd_forward_paragraph(struct buffer *buffer)
134 do {
135 if (!forward_line(buffer, +1)) {
136 message("No next paragraph");
137 return;
139 } while (buffer->current_line->line != NULL ||
140 buffer->current_line->parent->type != LINE_TEXT);
143 void
144 cmd_move_beginning_of_line(struct buffer *buffer)
146 buffer->cpoff = 0;
149 void
150 cmd_move_end_of_line(struct buffer *buffer)
152 struct vline *vl;
154 vl = buffer->current_line;
155 if (vl == NULL || vl->line == NULL)
156 return;
157 buffer->cpoff = utf8_cplen(vl->line);
160 void
161 cmd_redraw(struct buffer *buffer)
163 ui_schedule_redraw();
166 void
167 cmd_scroll_line_up(struct buffer *buffer)
169 struct vline *vl;
171 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
172 == NULL)
173 return;
175 forward_line(buffer, -1);
177 buffer->top_line = vl;
180 void
181 cmd_scroll_line_down(struct buffer *buffer)
183 if (!forward_line(buffer, +1))
184 return;
186 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
187 buffer->line_off++;
190 void
191 cmd_scroll_up(struct buffer *buffer)
193 struct vline *vl;
194 int i;
196 for (i = 0; i < body_lines; ++i) {
197 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
198 if (vl == NULL)
199 break;
200 buffer->top_line = vl;
201 forward_line(buffer, -1);
205 void
206 cmd_scroll_down(struct buffer *buffer)
208 int i;
210 for (i = 0; i < body_lines; ++i) {
211 if (!forward_line(buffer, +1))
212 break;
214 buffer->top_line = TAILQ_NEXT(buffer->top_line,
215 vlines);
219 void
220 cmd_beginning_of_buffer(struct buffer *buffer)
222 buffer->current_line = TAILQ_FIRST(&buffer->head);
223 buffer->cpoff = 0;
224 buffer->top_line = buffer->current_line;
225 buffer->line_off = 0;
228 void
229 cmd_end_of_buffer(struct buffer *buffer)
231 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
233 if (buffer->current_line == NULL)
234 return;
236 /* deal with invisible lines */
237 if (buffer->current_line->parent->flags & L_HIDDEN)
238 forward_line(buffer, -1);
240 cmd_move_end_of_line(buffer);
243 void
244 cmd_kill_telescope(struct buffer *buffer)
246 save_session();
247 event_loopbreak();
250 void
251 cmd_push_button(struct buffer *buffer)
253 struct vline *vl;
254 struct line *l;
256 vl = buffer->current_line;
258 if (vl == NULL)
259 return;
261 switch (vl->parent->type) {
262 case LINE_LINK:
263 load_url_in_tab(current_tab, vl->parent->alt);
264 break;
265 case LINE_PRE_START:
266 l = TAILQ_NEXT(vl->parent, lines);
267 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
268 if (l->type == LINE_PRE_END)
269 break;
270 l->flags ^= L_HIDDEN;
271 if (l->flags & L_HIDDEN)
272 buffer->line_max--;
273 else
274 buffer->line_max++;
276 break;
277 default:
278 break;
282 void
283 cmd_push_button_new_tab(struct buffer *buffer)
285 struct vline *vl;
287 vl = buffer->current_line;
288 if (vl == NULL || vl->parent->type != LINE_LINK)
289 return;
291 new_tab(vl->parent->alt);
294 void
295 cmd_previous_button(struct buffer *buffer)
297 struct excursion place;
299 save_excursion(&place, buffer);
301 do {
302 if (!forward_line(buffer, -1)) {
303 restore_excursion(&place, buffer);
304 message("No previous link");
305 return;
307 } while (buffer->current_line->parent->type != LINE_LINK);
310 void
311 cmd_next_button(struct buffer *buffer)
313 struct excursion place;
315 save_excursion(&place, buffer);
317 do {
318 if (!forward_line(buffer, +1)){
319 restore_excursion(&place, buffer);
320 message("No next link");
321 return;
323 } while (buffer->current_line->parent->type != LINE_LINK);
326 static inline int
327 is_heading(const struct line *l)
329 return l->type == LINE_TITLE_1 ||
330 l->type == LINE_TITLE_2 ||
331 l->type == LINE_TITLE_3;
334 void
335 cmd_previous_heading(struct buffer *buffer)
337 struct excursion place;
339 save_excursion(&place, buffer);
341 do {
342 if (!forward_line(buffer, -1)) {
343 restore_excursion(&place, buffer);
344 message("No previous heading");
345 return;
347 } while (!is_heading(buffer->current_line->parent));
350 void
351 cmd_next_heading(struct buffer *buffer)
353 struct excursion place;
355 save_excursion(&place, buffer);
357 do {
358 if (!forward_line(buffer, +1)) {
359 restore_excursion(&place, buffer);
360 message("No next heading");
361 return;
363 } while (!is_heading(buffer->current_line->parent));
366 void
367 cmd_previous_page(struct buffer *buffer)
369 if (!load_previous_page(current_tab))
370 message("No previous page");
371 else
372 start_loading_anim(current_tab);
375 void
376 cmd_next_page(struct buffer *buffer)
378 if (!load_next_page(current_tab))
379 message("No next page");
380 else
381 start_loading_anim(current_tab);
384 void
385 cmd_clear_minibuf(struct buffer *buffer)
387 message(NULL);
390 void
391 cmd_execute_extended_command(struct buffer *buffer)
393 size_t len;
395 GUARD_RECURSIVE_MINIBUFFER();
397 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
398 &eecmd_history, compl_eecmd, NULL);
400 len = sizeof(ministate.prompt);
401 strlcpy(ministate.prompt, "", len);
403 if (thiskey.meta)
404 strlcat(ministate.prompt, "M-", len);
406 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
408 if (thiskey.meta)
409 strlcat(ministate.prompt, " ", len);
412 void
413 cmd_tab_close(struct buffer *buffer)
415 struct tab *tab, *t;
417 tab = current_tab;
418 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
419 TAILQ_NEXT(tab, tabs) == NULL) {
420 message("Can't close the only tab.");
421 return;
424 if (evtimer_pending(&tab->loadingev, NULL))
425 evtimer_del(&tab->loadingev);
427 stop_tab(tab);
429 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
430 t = TAILQ_NEXT(tab, tabs);
431 TAILQ_REMOVE(&tabshead, tab, tabs);
432 free(tab);
434 switch_to_tab(t);
437 void
438 cmd_tab_close_other(struct buffer *buffer)
440 struct tab *t, *i;
442 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
443 if (t == current_tab)
444 continue;
446 stop_tab(t);
447 TAILQ_REMOVE(&tabshead, t, tabs);
448 free(t);
452 void
453 cmd_tab_new(struct buffer *buffer)
455 const char *url;
457 if ((url = new_tab_url) == NULL)
458 url = NEW_TAB_URL;
460 new_tab(url);
463 void
464 cmd_tab_next(struct buffer *buffer)
466 struct tab *t;
468 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
469 t = TAILQ_FIRST(&tabshead);
470 switch_to_tab(t);
473 void
474 cmd_tab_previous(struct buffer *buffer)
476 struct tab *t;
478 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
479 t = TAILQ_LAST(&tabshead, tabshead);
480 switch_to_tab(t);
483 void
484 cmd_tab_move(struct buffer *buffer)
486 struct tab *t;
488 t = TAILQ_NEXT(current_tab, tabs);
489 TAILQ_REMOVE(&tabshead, current_tab, tabs);
491 if (t == NULL)
492 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
493 else
494 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
497 void
498 cmd_tab_move_to(struct buffer *buffer)
500 struct tab *t;
502 t = TAILQ_PREV(current_tab, tabshead, tabs);
503 TAILQ_REMOVE(&tabshead, current_tab, tabs);
505 if (t == NULL) {
506 if (TAILQ_EMPTY(&tabshead))
507 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
508 else
509 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
510 } else
511 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
514 void
515 cmd_tab_select(struct buffer *buffer)
517 GUARD_RECURSIVE_MINIBUFFER();
519 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
520 NULL, compl_ts, NULL);
521 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
524 void
525 cmd_load_url(struct buffer *buffer)
527 GUARD_RECURSIVE_MINIBUFFER();
529 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
530 &lu_history, NULL, NULL);
531 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
532 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
533 cmd_move_end_of_line(&ministate.buffer);
536 void
537 cmd_load_current_url(struct buffer *buffer)
539 GUARD_RECURSIVE_MINIBUFFER();
541 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
542 &lu_history, NULL, NULL);
543 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
544 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
545 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
548 void
549 cmd_reload_page(struct buffer *buffer)
551 load_url_in_tab(current_tab, current_tab->hist_cur->h);
554 void
555 cmd_bookmark_page(struct buffer *buffer)
557 GUARD_RECURSIVE_MINIBUFFER();
559 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
560 NULL, NULL);
561 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
562 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
563 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
566 void
567 cmd_list_bookmarks(struct buffer *buffer)
569 load_url_in_tab(current_tab, "about:bookmarks");
572 void
573 cmd_toggle_help(struct buffer *buffer)
575 ui_toggle_side_window();
578 void
579 cmd_link_select(struct buffer *buffer)
581 struct line *l;
583 GUARD_RECURSIVE_MINIBUFFER();
585 l = TAILQ_FIRST(&buffer->page.head);
586 while (l != NULL && l->type != LINE_LINK)
587 l = TAILQ_NEXT(l, lines);
589 if (l == NULL) {
590 message("No links found");
591 return;
594 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
595 NULL, compl_ls, l);
596 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
599 void
600 cmd_swiper(struct buffer *buffer)
602 GUARD_RECURSIVE_MINIBUFFER();
604 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
605 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
606 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
609 void
610 cmd_toc(struct buffer *buffer)
612 struct line *l;
614 GUARD_RECURSIVE_MINIBUFFER();
616 l = TAILQ_FIRST(&buffer->page.head);
617 while (l != NULL &&
618 l->type != LINE_TITLE_1 &&
619 l->type != LINE_TITLE_2 &&
620 l->type != LINE_TITLE_3)
621 l = TAILQ_NEXT(l, lines);
623 if (l == NULL) {
624 message("No headings found");
625 return;
628 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
629 NULL, compl_toc, l);
630 strlcpy(ministate.prompt, "Select heading: ",
631 sizeof(ministate.prompt));
634 void
635 cmd_inc_fill_column(struct buffer *buffer)
637 if (fill_column == INT_MAX)
638 return;
640 fill_column += 2;
641 message("fill-column: %d", fill_column);
643 ui_schedule_redraw();
646 void
647 cmd_dec_fill_column(struct buffer *buffer)
649 if (fill_column == INT_MAX || fill_column < 8)
650 return;
652 fill_column -= 2;
653 message("fill-column: %d", fill_column);
655 ui_schedule_redraw();
658 void
659 cmd_olivetti_mode(struct buffer *buffer)
661 olivetti_mode = !olivetti_mode;
662 if (olivetti_mode)
663 message("olivetti-mode enabled");
664 else
665 message("olivetti-mode disabled");
667 ui_schedule_redraw();
670 void
671 cmd_mini_delete_char(struct buffer *buffer)
673 char *c, *n;
675 GUARD_READ_ONLY();
677 minibuffer_taint_hist();
679 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
680 if (*c == '\0')
681 return;
682 n = utf8_next_cp(c);
684 memmove(c, n, strlen(n)+1);
686 recompute_completions(0);
689 void
690 cmd_mini_delete_backward_char(struct buffer *buffer)
692 char *c, *p, *start;
694 GUARD_READ_ONLY();
696 minibuffer_taint_hist();
698 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
699 start = buffer->current_line->line;
700 if (c == start)
701 return;
702 p = utf8_prev_cp(c-1, start);
704 memmove(p, c, strlen(c)+1);
705 buffer->cpoff--;
707 recompute_completions(0);
710 void
711 cmd_mini_kill_line(struct buffer *buffer)
713 char *c;
715 GUARD_READ_ONLY();
717 minibuffer_taint_hist();
718 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
719 *c = '\0';
721 recompute_completions(0);
724 void
725 cmd_mini_abort(struct buffer *buffer)
727 if (!in_minibuffer)
728 return;
730 ministate.abortfn();
733 void
734 cmd_mini_complete_and_exit(struct buffer *buffer)
736 if (!in_minibuffer)
737 return;
739 minibuffer_taint_hist();
740 ministate.donefn();
743 void
744 cmd_mini_previous_history_element(struct buffer *buffer)
746 if (ministate.history == NULL) {
747 message("No history");
748 return;
751 if (ministate.hist_cur == NULL ||
752 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
753 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
754 ministate.hist_off = ministate.history->len - 1;
755 if (ministate.hist_cur == NULL)
756 message("No prev history item");
757 } else {
758 ministate.hist_off--;
761 if (ministate.hist_cur != NULL)
762 buffer->current_line->line = ministate.hist_cur->h;
765 void
766 cmd_mini_next_history_element(struct buffer *buffer)
768 if (ministate.history == NULL) {
769 message("No history");
770 return;
773 if (ministate.hist_cur == NULL ||
774 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
775 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
776 ministate.hist_off = 0;
777 if (ministate.hist_cur == NULL)
778 message("No next history item");
779 } else {
780 ministate.hist_off++;
783 if (ministate.hist_cur != NULL)
784 buffer->current_line->line = ministate.hist_cur->h;
787 void
788 cmd_previous_completion(struct buffer *buffer)
790 if (in_minibuffer != MB_COMPREAD)
791 return;
793 buffer = &ministate.compl.buffer;
795 if (buffer->current_line != NULL)
796 buffer->current_line->parent->type = LINE_COMPL;
798 forward_line(buffer, -1);
800 if (buffer->current_line != NULL)
801 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
804 void
805 cmd_next_completion(struct buffer *buffer)
807 if (in_minibuffer != MB_COMPREAD)
808 return;
810 buffer = &ministate.compl.buffer;
812 if (buffer->current_line != NULL)
813 buffer->current_line->parent->type = LINE_COMPL;
815 forward_line(buffer, +1);
817 if (buffer->current_line != NULL)
818 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
821 void
822 cmd_insert_current_candidate(struct buffer *buffer)
824 struct vline *vl;
826 if (in_minibuffer != MB_COMPREAD)
827 return;
829 buffer = &ministate.compl.buffer;
830 if ((vl = buffer->current_line) == NULL)
831 return;
833 minibuffer_taint_hist();
834 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
835 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
838 void
839 cmd_suspend_telescope(struct buffer *buffer)
841 message("Zzz...");
842 ui_suspend();
845 void
846 cmd_toggle_pre_wrap(struct buffer *buffer)
848 dont_wrap_pre = !dont_wrap_pre;
850 if (dont_wrap_pre)
851 message("Don't wrap preformatted blocks");
852 else
853 message("Wrap preformatted blocks");
855 ui_schedule_redraw();
858 void
859 cmd_mini_goto_beginning(struct buffer *buffer)
861 struct vline *vl;
863 if (!in_minibuffer)
864 return;
866 buffer = &ministate.compl.buffer;
868 if ((vl = buffer->current_line) != NULL)
869 vl->parent->type = LINE_COMPL;
871 vl = TAILQ_FIRST(&buffer->head);
872 while (vl != NULL && vl->parent->flags & L_HIDDEN)
873 vl = TAILQ_NEXT(vl, vlines);
875 if (vl == NULL)
876 return;
878 vl->parent->type = LINE_COMPL_CURRENT;
879 buffer->top_line = vl;
880 buffer->current_line = vl;
883 void
884 cmd_mini_goto_end(struct buffer *buffer)
886 struct vline *vl;
888 if (!in_minibuffer)
889 return;
891 buffer = &ministate.compl.buffer;
893 if ((vl = buffer->current_line) != NULL)
894 vl->parent->type = LINE_COMPL;
896 vl = TAILQ_LAST(&buffer->head, vhead);
897 while (vl != NULL && vl->parent->flags & L_HIDDEN)
898 vl = TAILQ_PREV(vl, vhead, vlines);
900 if (vl == NULL)
901 return;
903 vl->parent->type = LINE_COMPL_CURRENT;
904 buffer->current_line = vl;