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 == NULL)
103 return;
105 if (buffer->current_line->line != NULL)
106 len = utf8_cplen(buffer->current_line->line);
107 if (++buffer->cpoff > len)
108 buffer->cpoff = len;
111 void
112 cmd_backward_paragraph(struct buffer *buffer)
114 do {
115 if (!forward_line(buffer, -1)) {
116 message("No previous paragraph");
117 return;
119 } while (buffer->current_line->line != NULL ||
120 buffer->current_line->parent->type != LINE_TEXT);
123 void
124 cmd_forward_paragraph(struct buffer *buffer)
126 do {
127 if (!forward_line(buffer, +1)) {
128 message("No next paragraph");
129 return;
131 } while (buffer->current_line->line != NULL ||
132 buffer->current_line->parent->type != LINE_TEXT);
135 void
136 cmd_move_beginning_of_line(struct buffer *buffer)
138 buffer->cpoff = 0;
141 void
142 cmd_move_end_of_line(struct buffer *buffer)
144 struct vline *vl;
146 vl = buffer->current_line;
147 if (vl == NULL || vl->line == NULL)
148 return;
149 buffer->cpoff = utf8_cplen(vl->line);
152 void
153 cmd_redraw(struct buffer *buffer)
155 ui_schedule_redraw();
158 void
159 cmd_scroll_line_up(struct buffer *buffer)
161 struct vline *vl;
163 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
164 == NULL)
165 return;
167 forward_line(buffer, -1);
169 buffer->top_line = vl;
172 void
173 cmd_scroll_line_down(struct buffer *buffer)
175 if (!forward_line(buffer, +1))
176 return;
178 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
179 buffer->line_off++;
182 void
183 cmd_scroll_up(struct buffer *buffer)
185 struct vline *vl;
186 int i;
188 for (i = 0; i < body_lines; ++i) {
189 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
190 if (vl == NULL)
191 break;
192 buffer->top_line = vl;
193 forward_line(buffer, -1);
197 void
198 cmd_scroll_down(struct buffer *buffer)
200 int i;
202 for (i = 0; i < body_lines; ++i) {
203 if (!forward_line(buffer, +1))
204 break;
206 buffer->top_line = TAILQ_NEXT(buffer->top_line,
207 vlines);
211 void
212 cmd_beginning_of_buffer(struct buffer *buffer)
214 buffer->current_line = TAILQ_FIRST(&buffer->head);
215 buffer->cpoff = 0;
216 buffer->top_line = buffer->current_line;
217 buffer->line_off = 0;
220 void
221 cmd_end_of_buffer(struct buffer *buffer)
223 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
225 if (buffer->current_line == NULL)
226 return;
228 /* deal with invisible lines */
229 if (buffer->current_line->parent->flags & L_HIDDEN)
230 forward_line(buffer, -1);
232 cmd_move_end_of_line(buffer);
235 void
236 cmd_kill_telescope(struct buffer *buffer)
238 save_session();
239 event_loopbreak();
242 void
243 cmd_push_button(struct buffer *buffer)
245 struct vline *vl;
246 struct line *l;
248 vl = buffer->current_line;
250 if (vl == NULL)
251 return;
253 switch (vl->parent->type) {
254 case LINE_LINK:
255 load_url_in_tab(current_tab, vl->parent->alt);
256 break;
257 case LINE_PRE_START:
258 l = TAILQ_NEXT(vl->parent, lines);
259 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
260 if (l->type == LINE_PRE_END)
261 break;
262 l->flags ^= L_HIDDEN;
263 if (l->flags & L_HIDDEN)
264 buffer->line_max--;
265 else
266 buffer->line_max++;
268 break;
269 default:
270 break;
274 void
275 cmd_push_button_new_tab(struct buffer *buffer)
277 struct vline *vl;
279 vl = buffer->current_line;
280 if (vl == NULL || vl->parent->type != LINE_LINK)
281 return;
283 new_tab(vl->parent->alt);
286 void
287 cmd_previous_button(struct buffer *buffer)
289 struct excursion place;
291 save_excursion(&place, buffer);
293 do {
294 if (!forward_line(buffer, -1)) {
295 restore_excursion(&place, buffer);
296 message("No previous link");
297 return;
299 } while (buffer->current_line->parent->type != LINE_LINK);
302 void
303 cmd_next_button(struct buffer *buffer)
305 struct excursion place;
307 save_excursion(&place, buffer);
309 do {
310 if (!forward_line(buffer, +1)){
311 restore_excursion(&place, buffer);
312 message("No next link");
313 return;
315 } while (buffer->current_line->parent->type != LINE_LINK);
318 static inline int
319 is_heading(const struct line *l)
321 return l->type == LINE_TITLE_1 ||
322 l->type == LINE_TITLE_2 ||
323 l->type == LINE_TITLE_3;
326 void
327 cmd_previous_heading(struct buffer *buffer)
329 struct excursion place;
331 save_excursion(&place, buffer);
333 do {
334 if (!forward_line(buffer, -1)) {
335 restore_excursion(&place, buffer);
336 message("No previous heading");
337 return;
339 } while (!is_heading(buffer->current_line->parent));
342 void
343 cmd_next_heading(struct buffer *buffer)
345 struct excursion place;
347 save_excursion(&place, buffer);
349 do {
350 if (!forward_line(buffer, +1)) {
351 restore_excursion(&place, buffer);
352 message("No next heading");
353 return;
355 } while (!is_heading(buffer->current_line->parent));
358 void
359 cmd_previous_page(struct buffer *buffer)
361 if (!load_previous_page(current_tab))
362 message("No previous page");
363 else
364 start_loading_anim(current_tab);
367 void
368 cmd_next_page(struct buffer *buffer)
370 if (!load_next_page(current_tab))
371 message("No next page");
372 else
373 start_loading_anim(current_tab);
376 void
377 cmd_clear_minibuf(struct buffer *buffer)
379 message(NULL);
382 void
383 cmd_execute_extended_command(struct buffer *buffer)
385 size_t len;
387 GUARD_RECURSIVE_MINIBUFFER();
389 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
390 &eecmd_history, compl_eecmd, NULL);
392 len = sizeof(ministate.prompt);
393 strlcpy(ministate.prompt, "", len);
395 if (thiskey.meta)
396 strlcat(ministate.prompt, "M-", len);
398 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
400 if (thiskey.meta)
401 strlcat(ministate.prompt, " ", len);
404 void
405 cmd_tab_close(struct buffer *buffer)
407 struct tab *tab, *t;
409 tab = current_tab;
410 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
411 TAILQ_NEXT(tab, tabs) == NULL) {
412 message("Can't close the only tab.");
413 return;
416 if (evtimer_pending(&tab->loadingev, NULL))
417 evtimer_del(&tab->loadingev);
419 stop_tab(tab);
421 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
422 t = TAILQ_NEXT(tab, tabs);
423 TAILQ_REMOVE(&tabshead, tab, tabs);
424 free(tab);
426 switch_to_tab(t);
429 void
430 cmd_tab_close_other(struct buffer *buffer)
432 struct tab *t, *i;
434 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
435 if (t == current_tab)
436 continue;
438 stop_tab(t);
439 TAILQ_REMOVE(&tabshead, t, tabs);
440 free(t);
444 void
445 cmd_tab_new(struct buffer *buffer)
447 const char *url;
449 if ((url = new_tab_url) == NULL)
450 url = NEW_TAB_URL;
452 new_tab(url);
455 void
456 cmd_tab_next(struct buffer *buffer)
458 struct tab *t;
460 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
461 t = TAILQ_FIRST(&tabshead);
462 switch_to_tab(t);
465 void
466 cmd_tab_previous(struct buffer *buffer)
468 struct tab *t;
470 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
471 t = TAILQ_LAST(&tabshead, tabshead);
472 switch_to_tab(t);
475 void
476 cmd_tab_move(struct buffer *buffer)
478 struct tab *t;
480 t = TAILQ_NEXT(current_tab, tabs);
481 TAILQ_REMOVE(&tabshead, current_tab, tabs);
483 if (t == NULL)
484 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
485 else
486 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
489 void
490 cmd_tab_move_to(struct buffer *buffer)
492 struct tab *t;
494 t = TAILQ_PREV(current_tab, tabshead, tabs);
495 TAILQ_REMOVE(&tabshead, current_tab, tabs);
497 if (t == NULL) {
498 if (TAILQ_EMPTY(&tabshead))
499 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
500 else
501 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
502 } else
503 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
506 void
507 cmd_tab_select(struct buffer *buffer)
509 GUARD_RECURSIVE_MINIBUFFER();
511 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
512 NULL, compl_ts, NULL);
513 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
516 void
517 cmd_load_url(struct buffer *buffer)
519 GUARD_RECURSIVE_MINIBUFFER();
521 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
522 &lu_history, NULL, NULL);
523 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
524 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
525 cmd_move_end_of_line(&ministate.buffer);
528 void
529 cmd_load_current_url(struct buffer *buffer)
531 GUARD_RECURSIVE_MINIBUFFER();
533 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
534 &lu_history, NULL, NULL);
535 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
536 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
537 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
540 void
541 cmd_reload_page(struct buffer *buffer)
543 load_url_in_tab(current_tab, current_tab->hist_cur->h);
546 void
547 cmd_bookmark_page(struct buffer *buffer)
549 GUARD_RECURSIVE_MINIBUFFER();
551 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
552 NULL, NULL);
553 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
554 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
555 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
558 void
559 cmd_list_bookmarks(struct buffer *buffer)
561 load_url_in_tab(current_tab, "about:bookmarks");
564 void
565 cmd_toggle_help(struct buffer *buffer)
567 ui_toggle_side_window();
570 void
571 cmd_link_select(struct buffer *buffer)
573 struct line *l;
575 GUARD_RECURSIVE_MINIBUFFER();
577 l = TAILQ_FIRST(&buffer->page.head);
578 while (l != NULL && l->type != LINE_LINK)
579 l = TAILQ_NEXT(l, lines);
581 if (l == NULL) {
582 message("No links found");
583 return;
586 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
587 NULL, compl_ls, l);
588 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
591 void
592 cmd_swiper(struct buffer *buffer)
594 GUARD_RECURSIVE_MINIBUFFER();
596 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
597 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
598 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
601 void
602 cmd_toc(struct buffer *buffer)
604 struct line *l;
606 GUARD_RECURSIVE_MINIBUFFER();
608 l = TAILQ_FIRST(&buffer->page.head);
609 while (l != NULL &&
610 l->type != LINE_TITLE_1 &&
611 l->type != LINE_TITLE_2 &&
612 l->type != LINE_TITLE_3)
613 l = TAILQ_NEXT(l, lines);
615 if (l == NULL) {
616 message("No headings found");
617 return;
620 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
621 NULL, compl_toc, l);
622 strlcpy(ministate.prompt, "Select heading: ",
623 sizeof(ministate.prompt));
626 void
627 cmd_inc_fill_column(struct buffer *buffer)
629 if (fill_column == INT_MAX)
630 return;
632 fill_column += 2;
633 message("fill-column: %d", fill_column);
635 ui_schedule_redraw();
638 void
639 cmd_dec_fill_column(struct buffer *buffer)
641 if (fill_column == INT_MAX || fill_column < 8)
642 return;
644 fill_column -= 2;
645 message("fill-column: %d", fill_column);
647 ui_schedule_redraw();
650 void
651 cmd_olivetti_mode(struct buffer *buffer)
653 olivetti_mode = !olivetti_mode;
654 if (olivetti_mode)
655 message("olivetti-mode enabled");
656 else
657 message("olivetti-mode disabled");
659 ui_schedule_redraw();
662 void
663 cmd_mini_delete_char(struct buffer *buffer)
665 char *c, *n;
667 if (!in_minibuffer) {
668 message("text is read-only");
669 return;
672 minibuffer_taint_hist();
674 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
675 if (*c == '\0')
676 return;
677 n = utf8_next_cp(c);
679 memmove(c, n, strlen(n)+1);
681 recompute_completions(0);
684 void
685 cmd_mini_delete_backward_char(struct buffer *buffer)
687 char *c, *p, *start;
689 if (!in_minibuffer) {
690 message("text is read-only");
691 return;
694 minibuffer_taint_hist();
696 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
697 start = buffer->current_line->line;
698 if (c == start)
699 return;
700 p = utf8_prev_cp(c-1, start);
702 memmove(p, c, strlen(c)+1);
703 buffer->cpoff--;
705 recompute_completions(0);
708 void
709 cmd_mini_kill_line(struct buffer *buffer)
711 char *c;
713 if (!in_minibuffer) {
714 message("text is read-only");
715 return;
718 minibuffer_taint_hist();
719 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
720 *c = '\0';
722 recompute_completions(0);
725 void
726 cmd_mini_abort(struct buffer *buffer)
728 if (!in_minibuffer)
729 return;
731 ministate.abortfn();
734 void
735 cmd_mini_complete_and_exit(struct buffer *buffer)
737 if (!in_minibuffer)
738 return;
740 minibuffer_taint_hist();
741 ministate.donefn();
744 void
745 cmd_mini_previous_history_element(struct buffer *buffer)
747 if (ministate.history == NULL) {
748 message("No history");
749 return;
752 if (ministate.hist_cur == NULL ||
753 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
754 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
755 ministate.hist_off = ministate.history->len - 1;
756 if (ministate.hist_cur == NULL)
757 message("No prev history item");
758 } else {
759 ministate.hist_off--;
762 if (ministate.hist_cur != NULL)
763 buffer->current_line->line = ministate.hist_cur->h;
766 void
767 cmd_mini_next_history_element(struct buffer *buffer)
769 if (ministate.history == NULL) {
770 message("No history");
771 return;
774 if (ministate.hist_cur == NULL ||
775 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
776 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
777 ministate.hist_off = 0;
778 if (ministate.hist_cur == NULL)
779 message("No next history item");
780 } else {
781 ministate.hist_off++;
784 if (ministate.hist_cur != NULL)
785 buffer->current_line->line = ministate.hist_cur->h;
788 void
789 cmd_previous_completion(struct buffer *buffer)
791 if (in_minibuffer != MB_COMPREAD)
792 return;
794 buffer = &ministate.compl.buffer;
796 if (buffer->current_line != NULL)
797 buffer->current_line->parent->type = LINE_COMPL;
799 forward_line(buffer, -1);
801 if (buffer->current_line != NULL)
802 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
805 void
806 cmd_next_completion(struct buffer *buffer)
808 if (in_minibuffer != MB_COMPREAD)
809 return;
811 buffer = &ministate.compl.buffer;
813 if (buffer->current_line != NULL)
814 buffer->current_line->parent->type = LINE_COMPL;
816 forward_line(buffer, +1);
818 if (buffer->current_line != NULL)
819 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
822 void
823 cmd_insert_current_candidate(struct buffer *buffer)
825 struct vline *vl;
827 if (in_minibuffer != MB_COMPREAD)
828 return;
830 buffer = &ministate.compl.buffer;
831 if ((vl = buffer->current_line) == NULL)
832 return;
834 minibuffer_taint_hist();
835 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
836 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
839 void
840 cmd_suspend_telescope(struct buffer *buffer)
842 message("Zzz...");
843 ui_suspend();
846 void
847 cmd_toggle_pre_wrap(struct buffer *buffer)
849 dont_wrap_pre = !dont_wrap_pre;
851 if (dont_wrap_pre)
852 message("Don't wrap preformatted blocks");
853 else
854 message("Wrap preformatted blocks");
856 ui_schedule_redraw();
859 void
860 cmd_mini_goto_beginning(struct buffer *buffer)
862 struct vline *vl;
864 if (!in_minibuffer)
865 return;
867 buffer = &ministate.compl.buffer;
869 if ((vl = buffer->current_line) != NULL)
870 vl->parent->type = LINE_COMPL;
872 vl = TAILQ_FIRST(&buffer->head);
873 while (vl != NULL && vl->parent->flags & L_HIDDEN)
874 vl = TAILQ_NEXT(vl, vlines);
876 if (vl == NULL)
877 return;
879 vl->parent->type = LINE_COMPL_CURRENT;
880 buffer->top_line = vl;
881 buffer->current_line = vl;
884 void
885 cmd_mini_goto_end(struct buffer *buffer)
887 struct vline *vl;
889 if (!in_minibuffer)
890 return;
892 buffer = &ministate.compl.buffer;
894 if ((vl = buffer->current_line) != NULL)
895 vl->parent->type = LINE_COMPL;
897 vl = TAILQ_LAST(&buffer->head, vhead);
898 while (vl != NULL && vl->parent->flags & L_HIDDEN)
899 vl = TAILQ_PREV(vl, vhead, vlines);
901 if (vl == NULL)
902 return;
904 vl->parent->type = LINE_COMPL_CURRENT;
905 buffer->current_line = vl;