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 "telescope.h"
27 #include "ui.h"
28 #include "utf8.h"
30 #define GUARD_RECURSIVE_MINIBUFFER() \
31 do { \
32 if (in_minibuffer) { \
33 message("enable-recursive-minibuffers " \
34 "is not yet available."); \
35 return; \
36 } \
37 } while(0)
39 #define GUARD_READ_ONLY() \
40 do { \
41 if (!in_minibuffer) { \
42 message("text is read-only"); \
43 return; \
44 } \
45 } while(0)
47 /* return 1 if moved, 0 otherwise */
48 static inline int
49 forward_line(struct buffer *buffer, int n)
50 {
51 struct vline *vl;
52 int did;
54 if (buffer->current_line == NULL)
55 return 0;
56 vl = buffer->current_line;
58 did = 0;
59 while (n != 0) {
60 if (n > 0) {
61 vl = TAILQ_NEXT(vl, vlines);
62 if (vl == NULL)
63 return did;
64 if (vl->parent->flags & L_HIDDEN)
65 continue;
66 buffer->current_line = vl;
67 n--;
68 } else {
69 vl = TAILQ_PREV(vl, vhead, vlines);
70 if (vl == NULL)
71 return did;
72 if (vl->parent->flags & L_HIDDEN)
73 continue;
74 if (buffer->current_line == buffer->top_line) {
75 buffer->line_off--;
76 buffer->top_line = vl;
77 }
78 buffer->current_line = vl;
79 n++;
80 }
82 did = 1;
83 }
85 return did;
86 }
88 void
89 cmd_previous_line(struct buffer *buffer)
90 {
91 forward_line(buffer, -1);
92 }
94 void
95 cmd_next_line(struct buffer *buffer)
96 {
97 forward_line(buffer, +1);
98 }
100 void
101 cmd_backward_char(struct buffer *buffer)
103 if (buffer->cpoff != 0)
104 buffer->cpoff--;
107 void
108 cmd_forward_char(struct buffer *buffer)
110 size_t len = 0;
112 if (buffer->current_line == NULL)
113 return;
115 if (buffer->current_line->line != NULL)
116 len = utf8_cplen(buffer->current_line->line);
117 if (++buffer->cpoff > len)
118 buffer->cpoff = len;
121 void
122 cmd_backward_paragraph(struct buffer *buffer)
124 do {
125 if (!forward_line(buffer, -1)) {
126 message("No previous paragraph");
127 return;
129 } while (buffer->current_line->line != NULL ||
130 buffer->current_line->parent->type != LINE_TEXT);
133 void
134 cmd_forward_paragraph(struct buffer *buffer)
136 do {
137 if (!forward_line(buffer, +1)) {
138 message("No next paragraph");
139 return;
141 } while (buffer->current_line->line != NULL ||
142 buffer->current_line->parent->type != LINE_TEXT);
145 void
146 cmd_move_beginning_of_line(struct buffer *buffer)
148 buffer->cpoff = 0;
151 void
152 cmd_move_end_of_line(struct buffer *buffer)
154 struct vline *vl;
156 vl = buffer->current_line;
157 if (vl == NULL || vl->line == NULL)
158 return;
159 buffer->cpoff = utf8_cplen(vl->line);
162 void
163 cmd_redraw(struct buffer *buffer)
165 ui_schedule_redraw();
168 void
169 cmd_scroll_line_up(struct buffer *buffer)
171 struct vline *vl;
173 for (;;) {
174 if (buffer->top_line == NULL)
175 return;
177 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
178 == NULL)
179 return;
181 buffer->top_line = vl;
183 if (vl->parent->flags & L_HIDDEN)
184 continue;
186 break;
189 buffer->line_off--;
191 forward_line(buffer, -1);
194 void
195 cmd_scroll_line_down(struct buffer *buffer)
197 if (!forward_line(buffer, +1))
198 return;
200 for (;;) {
201 if (buffer->top_line == NULL)
202 return;
204 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
205 if (buffer->top_line->parent->flags & L_HIDDEN)
206 continue;
207 break;
210 buffer->line_off++;
213 void
214 cmd_scroll_up(struct buffer *buffer)
216 struct vline *vl;
217 int i;
219 if (buffer->top_line == NULL)
220 return;
222 for (i = 0; i < body_lines; ++i) {
223 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
224 if (vl == NULL)
225 break;
226 buffer->line_off--;
227 buffer->top_line = vl;
228 forward_line(buffer, -1);
232 void
233 cmd_scroll_down(struct buffer *buffer)
235 int i;
237 if (buffer->top_line == NULL)
238 return;
240 for (i = 0; i < body_lines; ++i) {
241 if (!forward_line(buffer, +1))
242 break;
244 buffer->top_line = TAILQ_NEXT(buffer->top_line,
245 vlines);
246 buffer->line_off++;
250 void
251 cmd_beginning_of_buffer(struct buffer *buffer)
253 buffer->current_line = TAILQ_FIRST(&buffer->head);
254 buffer->cpoff = 0;
255 buffer->top_line = buffer->current_line;
256 buffer->line_off = 0;
259 void
260 cmd_end_of_buffer(struct buffer *buffer)
262 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
264 if (buffer->current_line == NULL)
265 return;
267 /* deal with invisible lines */
268 if (buffer->current_line->parent->flags & L_HIDDEN)
269 forward_line(buffer, -1);
271 cmd_move_end_of_line(buffer);
274 void
275 cmd_kill_telescope(struct buffer *buffer)
277 save_session();
278 event_loopbreak();
281 void
282 cmd_push_button(struct buffer *buffer)
284 struct vline *vl;
285 struct line *l;
287 vl = buffer->current_line;
289 if (vl == NULL)
290 return;
292 switch (vl->parent->type) {
293 case LINE_LINK:
294 load_url_in_tab(current_tab, vl->parent->alt, NULL);
295 break;
296 case LINE_PRE_START:
297 l = TAILQ_NEXT(vl->parent, lines);
298 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
299 if (l->type == LINE_PRE_END)
300 break;
301 l->flags ^= L_HIDDEN;
302 if (l->flags & L_HIDDEN)
303 buffer->line_max--;
304 else
305 buffer->line_max++;
307 break;
308 default:
309 break;
313 void
314 cmd_push_button_new_tab(struct buffer *buffer)
316 struct vline *vl;
318 vl = buffer->current_line;
319 if (vl == NULL || vl->parent->type != LINE_LINK)
320 return;
322 new_tab(vl->parent->alt, current_tab->hist_cur->h);
325 void
326 cmd_previous_button(struct buffer *buffer)
328 struct excursion place;
330 save_excursion(&place, buffer);
332 do {
333 if (!forward_line(buffer, -1)) {
334 restore_excursion(&place, buffer);
335 message("No previous link");
336 return;
338 } while (buffer->current_line->parent->type != LINE_LINK);
341 void
342 cmd_next_button(struct buffer *buffer)
344 struct excursion place;
346 save_excursion(&place, buffer);
348 do {
349 if (!forward_line(buffer, +1)){
350 restore_excursion(&place, buffer);
351 message("No next link");
352 return;
354 } while (buffer->current_line->parent->type != LINE_LINK);
357 static inline int
358 is_heading(const struct line *l)
360 return l->type == LINE_TITLE_1 ||
361 l->type == LINE_TITLE_2 ||
362 l->type == LINE_TITLE_3;
365 void
366 cmd_previous_heading(struct buffer *buffer)
368 struct excursion place;
370 save_excursion(&place, buffer);
372 do {
373 if (!forward_line(buffer, -1)) {
374 restore_excursion(&place, buffer);
375 message("No previous heading");
376 return;
378 } while (!is_heading(buffer->current_line->parent));
381 void
382 cmd_next_heading(struct buffer *buffer)
384 struct excursion place;
386 save_excursion(&place, buffer);
388 do {
389 if (!forward_line(buffer, +1)) {
390 restore_excursion(&place, buffer);
391 message("No next heading");
392 return;
394 } while (!is_heading(buffer->current_line->parent));
397 void
398 cmd_previous_page(struct buffer *buffer)
400 if (!load_previous_page(current_tab))
401 message("No previous page");
402 else
403 start_loading_anim(current_tab);
406 void
407 cmd_next_page(struct buffer *buffer)
409 if (!load_next_page(current_tab))
410 message("No next page");
411 else
412 start_loading_anim(current_tab);
415 void
416 cmd_clear_minibuf(struct buffer *buffer)
418 message(NULL);
421 void
422 cmd_execute_extended_command(struct buffer *buffer)
424 size_t len;
426 GUARD_RECURSIVE_MINIBUFFER();
428 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
429 &eecmd_history, compl_eecmd, NULL);
431 len = sizeof(ministate.prompt);
432 strlcpy(ministate.prompt, "", len);
434 if (thiskey.meta)
435 strlcat(ministate.prompt, "M-", len);
437 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
439 if (thiskey.meta)
440 strlcat(ministate.prompt, " ", len);
443 void
444 cmd_tab_close(struct buffer *buffer)
446 struct tab *tab, *t;
448 tab = current_tab;
450 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
451 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
452 switch_to_tab(t);
453 free_tab(tab);
454 } else
455 message("Can't close the only tab.");
459 void
460 cmd_tab_close_other(struct buffer *buffer)
462 struct tab *t, *i;
464 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
465 if (t == current_tab)
466 continue;
468 free_tab(t);
472 void
473 cmd_tab_new(struct buffer *buffer)
475 const char *url;
477 if ((url = new_tab_url) == NULL)
478 url = NEW_TAB_URL;
480 new_tab(url, NULL);
483 void
484 cmd_tab_next(struct buffer *buffer)
486 struct tab *t;
488 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
489 t = TAILQ_FIRST(&tabshead);
490 switch_to_tab(t);
493 void
494 cmd_tab_previous(struct buffer *buffer)
496 struct tab *t;
498 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
499 t = TAILQ_LAST(&tabshead, tabshead);
500 switch_to_tab(t);
503 void
504 cmd_tab_move(struct buffer *buffer)
506 struct tab *t;
508 t = TAILQ_NEXT(current_tab, tabs);
509 TAILQ_REMOVE(&tabshead, current_tab, tabs);
511 if (t == NULL)
512 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
513 else
514 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
517 void
518 cmd_tab_move_to(struct buffer *buffer)
520 struct tab *t;
522 t = TAILQ_PREV(current_tab, tabshead, tabs);
523 TAILQ_REMOVE(&tabshead, current_tab, tabs);
525 if (t == NULL) {
526 if (TAILQ_EMPTY(&tabshead))
527 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
528 else
529 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
530 } else
531 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
534 void
535 cmd_tab_select(struct buffer *buffer)
537 GUARD_RECURSIVE_MINIBUFFER();
539 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
540 NULL, compl_ts, NULL);
541 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
544 void
545 cmd_load_url(struct buffer *buffer)
547 GUARD_RECURSIVE_MINIBUFFER();
549 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
550 &lu_history, NULL, NULL);
551 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
552 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
553 cmd_move_end_of_line(&ministate.buffer);
556 void
557 cmd_load_current_url(struct buffer *buffer)
559 GUARD_RECURSIVE_MINIBUFFER();
561 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
562 &lu_history, NULL, NULL);
563 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
564 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
565 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
568 void
569 cmd_reload_page(struct buffer *buffer)
571 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL);
574 void
575 cmd_bookmark_page(struct buffer *buffer)
577 GUARD_RECURSIVE_MINIBUFFER();
579 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
580 NULL, NULL);
581 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
582 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
583 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
586 void
587 cmd_list_bookmarks(struct buffer *buffer)
589 load_url_in_tab(current_tab, "about:bookmarks", NULL);
592 void
593 cmd_toggle_help(struct buffer *buffer)
595 ui_toggle_side_window();
598 void
599 cmd_link_select(struct buffer *buffer)
601 struct line *l;
603 GUARD_RECURSIVE_MINIBUFFER();
605 l = TAILQ_FIRST(&buffer->page.head);
606 while (l != NULL && l->type != LINE_LINK)
607 l = TAILQ_NEXT(l, lines);
609 if (l == NULL) {
610 message("No links found");
611 return;
614 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
615 NULL, compl_ls, l);
616 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
619 void
620 cmd_swiper(struct buffer *buffer)
622 GUARD_RECURSIVE_MINIBUFFER();
624 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
625 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
626 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
629 void
630 cmd_toc(struct buffer *buffer)
632 struct line *l;
634 GUARD_RECURSIVE_MINIBUFFER();
636 l = TAILQ_FIRST(&buffer->page.head);
637 while (l != NULL &&
638 l->type != LINE_TITLE_1 &&
639 l->type != LINE_TITLE_2 &&
640 l->type != LINE_TITLE_3)
641 l = TAILQ_NEXT(l, lines);
643 if (l == NULL) {
644 message("No headings found");
645 return;
648 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
649 NULL, compl_toc, l);
650 strlcpy(ministate.prompt, "Select heading: ",
651 sizeof(ministate.prompt));
654 void
655 cmd_inc_fill_column(struct buffer *buffer)
657 if (fill_column == INT_MAX)
658 return;
660 fill_column += 2;
661 message("fill-column: %d", fill_column);
663 ui_schedule_redraw();
666 void
667 cmd_dec_fill_column(struct buffer *buffer)
669 if (fill_column == INT_MAX || fill_column < 8)
670 return;
672 fill_column -= 2;
673 message("fill-column: %d", fill_column);
675 ui_schedule_redraw();
678 void
679 cmd_olivetti_mode(struct buffer *buffer)
681 olivetti_mode = !olivetti_mode;
682 if (olivetti_mode)
683 message("olivetti-mode enabled");
684 else
685 message("olivetti-mode disabled");
687 ui_schedule_redraw();
690 void
691 cmd_mini_delete_char(struct buffer *buffer)
693 char *c, *n;
695 GUARD_READ_ONLY();
697 minibuffer_taint_hist();
699 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
700 if (*c == '\0')
701 return;
702 n = utf8_next_cp(c);
704 memmove(c, n, strlen(n)+1);
706 recompute_completions(0);
709 void
710 cmd_mini_delete_backward_char(struct buffer *buffer)
712 char *c, *p, *start;
714 GUARD_READ_ONLY();
716 minibuffer_taint_hist();
718 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
719 start = buffer->current_line->line;
720 if (c == start)
721 return;
722 p = utf8_prev_cp(c-1, start);
724 memmove(p, c, strlen(c)+1);
725 buffer->cpoff--;
727 recompute_completions(0);
730 void
731 cmd_mini_kill_line(struct buffer *buffer)
733 char *c;
735 GUARD_READ_ONLY();
737 minibuffer_taint_hist();
738 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
739 *c = '\0';
741 recompute_completions(0);
744 void
745 cmd_mini_abort(struct buffer *buffer)
747 if (!in_minibuffer)
748 return;
750 ministate.abortfn();
753 void
754 cmd_mini_complete_and_exit(struct buffer *buffer)
756 if (!in_minibuffer)
757 return;
759 minibuffer_taint_hist();
760 ministate.donefn();
763 void
764 cmd_mini_previous_history_element(struct buffer *buffer)
766 if (ministate.history == NULL) {
767 message("No history");
768 return;
771 if (ministate.hist_cur == NULL ||
772 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
773 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
774 ministate.hist_off = ministate.history->len - 1;
775 if (ministate.hist_cur == NULL)
776 message("No prev history item");
777 } else {
778 ministate.hist_off--;
781 if (ministate.hist_cur != NULL)
782 buffer->current_line->line = ministate.hist_cur->h;
785 void
786 cmd_mini_next_history_element(struct buffer *buffer)
788 if (ministate.history == NULL) {
789 message("No history");
790 return;
793 if (ministate.hist_cur == NULL ||
794 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
795 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
796 ministate.hist_off = 0;
797 if (ministate.hist_cur == NULL)
798 message("No next history item");
799 } else {
800 ministate.hist_off++;
803 if (ministate.hist_cur != NULL)
804 buffer->current_line->line = ministate.hist_cur->h;
807 void
808 cmd_previous_completion(struct buffer *buffer)
810 if (in_minibuffer != MB_COMPREAD)
811 return;
813 buffer = &ministate.compl.buffer;
815 if (buffer->current_line != NULL)
816 buffer->current_line->parent->type = LINE_COMPL;
818 forward_line(buffer, -1);
820 if (buffer->current_line != NULL)
821 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
824 void
825 cmd_next_completion(struct buffer *buffer)
827 if (in_minibuffer != MB_COMPREAD)
828 return;
830 buffer = &ministate.compl.buffer;
832 if (buffer->current_line != NULL)
833 buffer->current_line->parent->type = LINE_COMPL;
835 forward_line(buffer, +1);
837 if (buffer->current_line != NULL)
838 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
841 void
842 cmd_insert_current_candidate(struct buffer *buffer)
844 struct vline *vl;
846 if (in_minibuffer != MB_COMPREAD)
847 return;
849 buffer = &ministate.compl.buffer;
850 if ((vl = buffer->current_line) == NULL)
851 return;
853 minibuffer_taint_hist();
854 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
855 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
858 void
859 cmd_suspend_telescope(struct buffer *buffer)
861 message("Zzz...");
862 ui_suspend();
865 void
866 cmd_toggle_pre_wrap(struct buffer *buffer)
868 dont_wrap_pre = !dont_wrap_pre;
870 if (dont_wrap_pre)
871 message("Don't wrap preformatted blocks");
872 else
873 message("Wrap preformatted blocks");
875 ui_schedule_redraw();
878 void
879 cmd_mini_goto_beginning(struct buffer *buffer)
881 struct vline *vl;
883 if (!in_minibuffer)
884 return;
886 buffer = &ministate.compl.buffer;
888 if ((vl = buffer->current_line) != NULL)
889 vl->parent->type = LINE_COMPL;
891 vl = TAILQ_FIRST(&buffer->head);
892 while (vl != NULL && vl->parent->flags & L_HIDDEN)
893 vl = TAILQ_NEXT(vl, vlines);
895 if (vl == NULL)
896 return;
898 vl->parent->type = LINE_COMPL_CURRENT;
899 buffer->top_line = vl;
900 buffer->current_line = vl;
903 void
904 cmd_mini_goto_end(struct buffer *buffer)
906 struct vline *vl;
908 if (!in_minibuffer)
909 return;
911 buffer = &ministate.compl.buffer;
913 if ((vl = buffer->current_line) != NULL)
914 vl->parent->type = LINE_COMPL;
916 vl = TAILQ_LAST(&buffer->head, vhead);
917 while (vl != NULL && vl->parent->flags & L_HIDDEN)
918 vl = TAILQ_PREV(vl, vhead, vlines);
920 if (vl == NULL)
921 return;
923 vl->parent->type = LINE_COMPL_CURRENT;
924 buffer->current_line = vl;
927 void
928 cmd_other_window(struct buffer *buffer)
930 ui_other_window();
933 void
934 cmd_mini_scroll_up(struct buffer *buffer)
936 if (!in_minibuffer)
937 return;
939 buffer = &ministate.compl.buffer;
940 if (buffer->current_line == NULL)
941 return;
943 buffer->current_line->parent->type = LINE_COMPL;
944 cmd_scroll_up(buffer);
945 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
948 void
949 cmd_mini_scroll_down(struct buffer *buffer)
951 if (!in_minibuffer)
952 return;
954 buffer = &ministate.compl.buffer;
955 if (buffer->current_line == NULL)
956 return;
958 buffer->current_line->parent->type = LINE_COMPL;
959 cmd_scroll_down(buffer);
960 buffer->current_line->parent->type = LINE_COMPL_CURRENT;