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 "compat.h"
19 #include <limits.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "compl.h"
24 #include "defaults.h"
25 #include "minibuffer.h"
26 #include "session.h"
27 #include "telescope.h"
28 #include "ui.h"
29 #include "utf8.h"
31 #define GUARD_RECURSIVE_MINIBUFFER() \
32 do { \
33 if (in_minibuffer) { \
34 message("enable-recursive-minibuffers " \
35 "is not yet available."); \
36 return; \
37 } \
38 } while(0)
40 #define GUARD_READ_ONLY() \
41 do { \
42 if (!in_minibuffer) { \
43 message("text is read-only"); \
44 return; \
45 } \
46 } while(0)
48 /* return 1 if moved, 0 otherwise */
49 static inline int
50 forward_line(struct buffer *buffer, int n)
51 {
52 struct vline *vl;
53 int did;
55 if (buffer->current_line == NULL)
56 return 0;
57 vl = buffer->current_line;
59 did = 0;
60 while (n != 0) {
61 if (n > 0) {
62 vl = TAILQ_NEXT(vl, vlines);
63 if (vl == NULL)
64 return did;
65 if (vl->parent->flags & L_HIDDEN)
66 continue;
67 buffer->current_line = vl;
68 n--;
69 } else {
70 vl = TAILQ_PREV(vl, vhead, vlines);
71 if (vl == NULL)
72 return did;
73 if (vl->parent->flags & L_HIDDEN)
74 continue;
75 if (buffer->current_line == buffer->top_line) {
76 buffer->line_off--;
77 buffer->top_line = vl;
78 }
79 buffer->current_line = vl;
80 n++;
81 }
83 did = 1;
84 }
86 return did;
87 }
89 void
90 cmd_previous_line(struct buffer *buffer)
91 {
92 forward_line(buffer, -1);
93 }
95 void
96 cmd_next_line(struct buffer *buffer)
97 {
98 forward_line(buffer, +1);
99 }
101 void
102 cmd_backward_char(struct buffer *buffer)
104 if (buffer->cpoff != 0)
105 buffer->cpoff--;
108 void
109 cmd_forward_char(struct buffer *buffer)
111 size_t len = 0;
113 if (buffer->current_line == NULL)
114 return;
116 if (buffer->current_line->line != NULL)
117 len = utf8_cplen(buffer->current_line->line);
118 if (++buffer->cpoff > len)
119 buffer->cpoff = len;
122 void
123 cmd_backward_paragraph(struct buffer *buffer)
125 do {
126 if (!forward_line(buffer, -1)) {
127 message("No previous paragraph");
128 return;
130 } while (buffer->current_line->line != NULL ||
131 buffer->current_line->parent->type != LINE_TEXT);
134 void
135 cmd_forward_paragraph(struct buffer *buffer)
137 do {
138 if (!forward_line(buffer, +1)) {
139 message("No next paragraph");
140 return;
142 } while (buffer->current_line->line != NULL ||
143 buffer->current_line->parent->type != LINE_TEXT);
146 void
147 cmd_move_beginning_of_line(struct buffer *buffer)
149 buffer->cpoff = 0;
152 void
153 cmd_move_end_of_line(struct buffer *buffer)
155 struct vline *vl;
157 vl = buffer->current_line;
158 if (vl == NULL || vl->line == NULL)
159 return;
160 buffer->cpoff = utf8_cplen(vl->line);
163 void
164 cmd_redraw(struct buffer *buffer)
166 ui_schedule_redraw();
169 void
170 cmd_scroll_line_up(struct buffer *buffer)
172 struct vline *vl;
174 for (;;) {
175 if (buffer->top_line == NULL)
176 return;
178 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
179 == NULL)
180 return;
182 buffer->top_line = vl;
184 if (vl->parent->flags & L_HIDDEN)
185 continue;
187 break;
190 buffer->line_off--;
192 forward_line(buffer, -1);
195 void
196 cmd_scroll_line_down(struct buffer *buffer)
198 if (!forward_line(buffer, +1))
199 return;
201 for (;;) {
202 if (buffer->top_line == NULL)
203 return;
205 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
206 if (buffer->top_line->parent->flags & L_HIDDEN)
207 continue;
208 break;
211 buffer->line_off++;
214 void
215 cmd_scroll_up(struct buffer *buffer)
217 struct vline *vl;
218 int i;
220 if (buffer->top_line == NULL)
221 return;
223 for (i = 0; i < body_lines; ++i) {
224 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
225 if (vl == NULL)
226 break;
227 buffer->line_off--;
228 buffer->top_line = vl;
229 forward_line(buffer, -1);
233 void
234 cmd_scroll_down(struct buffer *buffer)
236 int i;
238 if (buffer->top_line == NULL)
239 return;
241 for (i = 0; i < body_lines; ++i) {
242 if (!forward_line(buffer, +1))
243 break;
245 buffer->top_line = TAILQ_NEXT(buffer->top_line,
246 vlines);
247 buffer->line_off++;
251 void
252 cmd_beginning_of_buffer(struct buffer *buffer)
254 buffer->current_line = TAILQ_FIRST(&buffer->head);
255 buffer->cpoff = 0;
256 buffer->top_line = buffer->current_line;
257 buffer->line_off = 0;
260 void
261 cmd_end_of_buffer(struct buffer *buffer)
263 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
265 if (buffer->current_line == NULL)
266 return;
268 /* deal with invisible lines */
269 if (buffer->current_line->parent->flags & L_HIDDEN)
270 forward_line(buffer, -1);
272 cmd_move_end_of_line(buffer);
275 void
276 cmd_kill_telescope(struct buffer *buffer)
278 save_session();
279 event_loopbreak();
282 void
283 cmd_push_button(struct buffer *buffer)
285 struct vline *vl;
286 struct line *l;
288 vl = buffer->current_line;
290 if (vl == NULL)
291 return;
293 switch (vl->parent->type) {
294 case LINE_LINK:
295 load_url_in_tab(current_tab, vl->parent->alt, NULL, 0);
296 break;
297 case LINE_PRE_START:
298 l = TAILQ_NEXT(vl->parent, lines);
299 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
300 if (l->type == LINE_PRE_END)
301 break;
302 l->flags ^= L_HIDDEN;
303 if (l->flags & L_HIDDEN)
304 buffer->line_max--;
305 else
306 buffer->line_max++;
308 break;
309 default:
310 break;
314 void
315 cmd_push_button_new_tab(struct buffer *buffer)
317 struct vline *vl;
319 vl = buffer->current_line;
320 if (vl == NULL || vl->parent->type != LINE_LINK)
321 return;
323 new_tab(vl->parent->alt, current_tab->hist_cur->h, current_tab);
326 void
327 cmd_previous_button(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 link");
337 return;
339 } while (buffer->current_line->parent->type != LINE_LINK);
342 void
343 cmd_next_button(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 link");
353 return;
355 } while (buffer->current_line->parent->type != LINE_LINK);
358 static inline int
359 is_heading(const struct line *l)
361 return l->type == LINE_TITLE_1 ||
362 l->type == LINE_TITLE_2 ||
363 l->type == LINE_TITLE_3;
366 void
367 cmd_previous_heading(struct buffer *buffer)
369 struct excursion place;
371 save_excursion(&place, buffer);
373 do {
374 if (!forward_line(buffer, -1)) {
375 restore_excursion(&place, buffer);
376 message("No previous heading");
377 return;
379 } while (!is_heading(buffer->current_line->parent));
382 void
383 cmd_next_heading(struct buffer *buffer)
385 struct excursion place;
387 save_excursion(&place, buffer);
389 do {
390 if (!forward_line(buffer, +1)) {
391 restore_excursion(&place, buffer);
392 message("No next heading");
393 return;
395 } while (!is_heading(buffer->current_line->parent));
398 void
399 cmd_previous_page(struct buffer *buffer)
401 if (!load_previous_page(current_tab))
402 message("No previous page");
403 else
404 start_loading_anim(current_tab);
407 void
408 cmd_next_page(struct buffer *buffer)
410 if (!load_next_page(current_tab))
411 message("No next page");
412 else
413 start_loading_anim(current_tab);
416 void
417 cmd_clear_minibuf(struct buffer *buffer)
419 message(NULL);
422 void
423 cmd_execute_extended_command(struct buffer *buffer)
425 size_t len;
427 GUARD_RECURSIVE_MINIBUFFER();
429 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
430 &eecmd_history, compl_eecmd, NULL);
432 len = sizeof(ministate.prompt);
433 strlcpy(ministate.prompt, "", len);
435 if (thiskey.meta)
436 strlcat(ministate.prompt, "M-", len);
438 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
440 if (thiskey.meta)
441 strlcat(ministate.prompt, " ", len);
444 void
445 cmd_tab_close(struct buffer *buffer)
447 struct tab *tab, *t;
449 tab = current_tab;
451 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
452 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
453 switch_to_tab(t);
454 free_tab(tab);
455 } else
456 message("Can't close the only tab.");
460 void
461 cmd_tab_close_other(struct buffer *buffer)
463 struct tab *t, *i;
465 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
466 if (t == current_tab)
467 continue;
469 free_tab(t);
473 void
474 cmd_tab_new(struct buffer *buffer)
476 const char *url;
478 if ((url = new_tab_url) == NULL)
479 url = NEW_TAB_URL;
481 new_tab(url, NULL, NULL);
484 void
485 cmd_tab_next(struct buffer *buffer)
487 struct tab *t;
489 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
490 t = TAILQ_FIRST(&tabshead);
491 switch_to_tab(t);
494 void
495 cmd_tab_previous(struct buffer *buffer)
497 struct tab *t;
499 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
500 t = TAILQ_LAST(&tabshead, tabshead);
501 switch_to_tab(t);
504 void
505 cmd_tab_move(struct buffer *buffer)
507 struct tab *t;
509 t = TAILQ_NEXT(current_tab, tabs);
510 TAILQ_REMOVE(&tabshead, current_tab, tabs);
512 if (t == NULL)
513 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
514 else
515 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
518 void
519 cmd_tab_move_to(struct buffer *buffer)
521 struct tab *t;
523 t = TAILQ_PREV(current_tab, tabshead, tabs);
524 TAILQ_REMOVE(&tabshead, current_tab, tabs);
526 if (t == NULL)
527 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
528 else
529 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
532 void
533 cmd_tab_select(struct buffer *buffer)
535 GUARD_RECURSIVE_MINIBUFFER();
537 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
538 NULL, compl_ts, NULL);
539 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
542 void
543 cmd_load_url(struct buffer *buffer)
545 GUARD_RECURSIVE_MINIBUFFER();
547 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
548 &lu_history, NULL, NULL);
549 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
550 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
551 cmd_move_end_of_line(&ministate.buffer);
554 void
555 cmd_load_current_url(struct buffer *buffer)
557 GUARD_RECURSIVE_MINIBUFFER();
559 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
560 &lu_history, NULL, NULL);
561 strlcpy(ministate.prompt, "Load 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_reload_page(struct buffer *buffer)
569 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL, 1);
572 void
573 cmd_bookmark_page(struct buffer *buffer)
575 GUARD_RECURSIVE_MINIBUFFER();
577 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
578 NULL, NULL);
579 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
580 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
581 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
584 void
585 cmd_list_bookmarks(struct buffer *buffer)
587 load_url_in_tab(current_tab, "about:bookmarks", NULL, 0);
590 void
591 cmd_toggle_help(struct buffer *buffer)
593 ui_toggle_side_window();
596 void
597 cmd_link_select(struct buffer *buffer)
599 struct line *l;
601 GUARD_RECURSIVE_MINIBUFFER();
603 l = TAILQ_FIRST(&buffer->page.head);
604 while (l != NULL && l->type != LINE_LINK)
605 l = TAILQ_NEXT(l, lines);
607 if (l == NULL) {
608 message("No links found");
609 return;
612 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
613 NULL, compl_ls, l);
614 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
617 void
618 cmd_swiper(struct buffer *buffer)
620 GUARD_RECURSIVE_MINIBUFFER();
622 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
623 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
624 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
627 void
628 cmd_toc(struct buffer *buffer)
630 struct line *l;
632 GUARD_RECURSIVE_MINIBUFFER();
634 l = TAILQ_FIRST(&buffer->page.head);
635 while (l != NULL &&
636 l->type != LINE_TITLE_1 &&
637 l->type != LINE_TITLE_2 &&
638 l->type != LINE_TITLE_3)
639 l = TAILQ_NEXT(l, lines);
641 if (l == NULL) {
642 message("No headings found");
643 return;
646 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
647 NULL, compl_toc, l);
648 strlcpy(ministate.prompt, "Select heading: ",
649 sizeof(ministate.prompt));
652 void
653 cmd_inc_fill_column(struct buffer *buffer)
655 if (fill_column == INT_MAX)
656 return;
658 fill_column += 2;
659 message("fill-column: %d", fill_column);
661 ui_schedule_redraw();
664 void
665 cmd_dec_fill_column(struct buffer *buffer)
667 if (fill_column == INT_MAX || fill_column < 8)
668 return;
670 fill_column -= 2;
671 message("fill-column: %d", fill_column);
673 ui_schedule_redraw();
676 void
677 cmd_olivetti_mode(struct buffer *buffer)
679 olivetti_mode = !olivetti_mode;
680 if (olivetti_mode)
681 message("olivetti-mode enabled");
682 else
683 message("olivetti-mode disabled");
685 ui_schedule_redraw();
688 void
689 cmd_mini_delete_char(struct buffer *buffer)
691 char *c, *n;
693 GUARD_READ_ONLY();
695 minibuffer_taint_hist();
697 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
698 if (*c == '\0')
699 return;
700 n = utf8_next_cp(c);
702 memmove(c, n, strlen(n)+1);
704 recompute_completions(0);
707 void
708 cmd_mini_delete_backward_char(struct buffer *buffer)
710 char *c, *p, *start;
712 GUARD_READ_ONLY();
714 minibuffer_taint_hist();
716 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
717 start = buffer->current_line->line;
718 if (c == start)
719 return;
720 p = utf8_prev_cp(c-1, start);
722 memmove(p, c, strlen(c)+1);
723 buffer->cpoff--;
725 recompute_completions(0);
728 void
729 cmd_mini_kill_line(struct buffer *buffer)
731 char *c;
733 GUARD_READ_ONLY();
735 minibuffer_taint_hist();
736 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
737 *c = '\0';
739 recompute_completions(0);
742 void
743 cmd_mini_abort(struct buffer *buffer)
745 if (!in_minibuffer)
746 return;
748 ministate.abortfn();
751 void
752 cmd_mini_complete_and_exit(struct buffer *buffer)
754 if (!in_minibuffer)
755 return;
757 minibuffer_taint_hist();
758 ministate.donefn();
761 void
762 cmd_mini_previous_history_element(struct buffer *buffer)
764 if (ministate.history == NULL) {
765 message("No history");
766 return;
769 if (ministate.hist_cur == NULL ||
770 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
771 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
772 ministate.hist_off = ministate.history->len - 1;
773 if (ministate.hist_cur == NULL)
774 message("No prev history item");
775 } else {
776 ministate.hist_off--;
779 if (ministate.hist_cur != NULL)
780 buffer->current_line->line = ministate.hist_cur->h;
783 void
784 cmd_mini_next_history_element(struct buffer *buffer)
786 if (ministate.history == NULL) {
787 message("No history");
788 return;
791 if (ministate.hist_cur == NULL ||
792 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
793 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
794 ministate.hist_off = 0;
795 if (ministate.hist_cur == NULL)
796 message("No next history item");
797 } else {
798 ministate.hist_off++;
801 if (ministate.hist_cur != NULL)
802 buffer->current_line->line = ministate.hist_cur->h;
805 void
806 cmd_previous_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_next_completion(struct buffer *buffer)
825 if (in_minibuffer != MB_COMPREAD)
826 return;
828 buffer = &ministate.compl.buffer;
830 if (buffer->current_line != NULL)
831 buffer->current_line->parent->type = LINE_COMPL;
833 forward_line(buffer, +1);
835 if (buffer->current_line != NULL)
836 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
839 void
840 cmd_insert_current_candidate(struct buffer *buffer)
842 struct vline *vl;
844 if (in_minibuffer != MB_COMPREAD)
845 return;
847 buffer = &ministate.compl.buffer;
848 if ((vl = buffer->current_line) == NULL)
849 return;
851 minibuffer_taint_hist();
852 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
853 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
856 void
857 cmd_suspend_telescope(struct buffer *buffer)
859 message("Zzz...");
860 ui_suspend();
863 void
864 cmd_toggle_pre_wrap(struct buffer *buffer)
866 dont_wrap_pre = !dont_wrap_pre;
868 if (dont_wrap_pre)
869 message("Don't wrap preformatted blocks");
870 else
871 message("Wrap preformatted blocks");
873 ui_schedule_redraw();
876 void
877 cmd_mini_goto_beginning(struct buffer *buffer)
879 struct vline *vl;
881 if (!in_minibuffer)
882 return;
884 buffer = &ministate.compl.buffer;
886 if ((vl = buffer->current_line) != NULL)
887 vl->parent->type = LINE_COMPL;
889 vl = TAILQ_FIRST(&buffer->head);
890 while (vl != NULL && vl->parent->flags & L_HIDDEN)
891 vl = TAILQ_NEXT(vl, vlines);
893 if (vl == NULL)
894 return;
896 vl->parent->type = LINE_COMPL_CURRENT;
897 buffer->top_line = vl;
898 buffer->current_line = vl;
901 void
902 cmd_mini_goto_end(struct buffer *buffer)
904 struct vline *vl;
906 if (!in_minibuffer)
907 return;
909 buffer = &ministate.compl.buffer;
911 if ((vl = buffer->current_line) != NULL)
912 vl->parent->type = LINE_COMPL;
914 vl = TAILQ_LAST(&buffer->head, vhead);
915 while (vl != NULL && vl->parent->flags & L_HIDDEN)
916 vl = TAILQ_PREV(vl, vhead, vlines);
918 if (vl == NULL)
919 return;
921 vl->parent->type = LINE_COMPL_CURRENT;
922 buffer->current_line = vl;
925 void
926 cmd_other_window(struct buffer *buffer)
928 ui_other_window();
931 void
932 cmd_mini_scroll_up(struct buffer *buffer)
934 if (!in_minibuffer)
935 return;
937 buffer = &ministate.compl.buffer;
938 if (buffer->current_line == NULL)
939 return;
941 buffer->current_line->parent->type = LINE_COMPL;
942 cmd_scroll_up(buffer);
943 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
946 void
947 cmd_mini_scroll_down(struct buffer *buffer)
949 if (!in_minibuffer)
950 return;
952 buffer = &ministate.compl.buffer;
953 if (buffer->current_line == NULL)
954 return;
956 buffer->current_line->parent->type = LINE_COMPL;
957 cmd_scroll_down(buffer);
958 buffer->current_line->parent->type = LINE_COMPL_CURRENT;