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 if (buffer->top_line == NULL)
174 return;
176 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
177 == NULL)
178 return;
180 forward_line(buffer, -1);
181 buffer->line_off--;
182 buffer->top_line = vl;
185 void
186 cmd_scroll_line_down(struct buffer *buffer)
188 if (!forward_line(buffer, +1))
189 return;
191 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
192 buffer->line_off++;
195 void
196 cmd_scroll_up(struct buffer *buffer)
198 struct vline *vl;
199 int i;
201 if (buffer->top_line == NULL)
202 return;
204 for (i = 0; i < body_lines; ++i) {
205 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
206 if (vl == NULL)
207 break;
208 buffer->line_off--;
209 buffer->top_line = vl;
210 forward_line(buffer, -1);
214 void
215 cmd_scroll_down(struct buffer *buffer)
217 int i;
219 if (buffer->top_line == NULL)
220 return;
222 for (i = 0; i < body_lines; ++i) {
223 if (!forward_line(buffer, +1))
224 break;
226 buffer->top_line = TAILQ_NEXT(buffer->top_line,
227 vlines);
228 buffer->line_off++;
232 void
233 cmd_beginning_of_buffer(struct buffer *buffer)
235 buffer->current_line = TAILQ_FIRST(&buffer->head);
236 buffer->cpoff = 0;
237 buffer->top_line = buffer->current_line;
238 buffer->line_off = 0;
241 void
242 cmd_end_of_buffer(struct buffer *buffer)
244 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
246 if (buffer->current_line == NULL)
247 return;
249 /* deal with invisible lines */
250 if (buffer->current_line->parent->flags & L_HIDDEN)
251 forward_line(buffer, -1);
253 cmd_move_end_of_line(buffer);
256 void
257 cmd_kill_telescope(struct buffer *buffer)
259 save_session();
260 event_loopbreak();
263 void
264 cmd_push_button(struct buffer *buffer)
266 struct vline *vl;
267 struct line *l;
269 vl = buffer->current_line;
271 if (vl == NULL)
272 return;
274 switch (vl->parent->type) {
275 case LINE_LINK:
276 load_url_in_tab(current_tab, vl->parent->alt, NULL);
277 break;
278 case LINE_PRE_START:
279 l = TAILQ_NEXT(vl->parent, lines);
280 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
281 if (l->type == LINE_PRE_END)
282 break;
283 l->flags ^= L_HIDDEN;
284 if (l->flags & L_HIDDEN)
285 buffer->line_max--;
286 else
287 buffer->line_max++;
289 break;
290 default:
291 break;
295 void
296 cmd_push_button_new_tab(struct buffer *buffer)
298 struct vline *vl;
300 vl = buffer->current_line;
301 if (vl == NULL || vl->parent->type != LINE_LINK)
302 return;
304 new_tab(vl->parent->alt, current_tab->hist_cur->h);
307 void
308 cmd_previous_button(struct buffer *buffer)
310 struct excursion place;
312 save_excursion(&place, buffer);
314 do {
315 if (!forward_line(buffer, -1)) {
316 restore_excursion(&place, buffer);
317 message("No previous link");
318 return;
320 } while (buffer->current_line->parent->type != LINE_LINK);
323 void
324 cmd_next_button(struct buffer *buffer)
326 struct excursion place;
328 save_excursion(&place, buffer);
330 do {
331 if (!forward_line(buffer, +1)){
332 restore_excursion(&place, buffer);
333 message("No next link");
334 return;
336 } while (buffer->current_line->parent->type != LINE_LINK);
339 static inline int
340 is_heading(const struct line *l)
342 return l->type == LINE_TITLE_1 ||
343 l->type == LINE_TITLE_2 ||
344 l->type == LINE_TITLE_3;
347 void
348 cmd_previous_heading(struct buffer *buffer)
350 struct excursion place;
352 save_excursion(&place, buffer);
354 do {
355 if (!forward_line(buffer, -1)) {
356 restore_excursion(&place, buffer);
357 message("No previous heading");
358 return;
360 } while (!is_heading(buffer->current_line->parent));
363 void
364 cmd_next_heading(struct buffer *buffer)
366 struct excursion place;
368 save_excursion(&place, buffer);
370 do {
371 if (!forward_line(buffer, +1)) {
372 restore_excursion(&place, buffer);
373 message("No next heading");
374 return;
376 } while (!is_heading(buffer->current_line->parent));
379 void
380 cmd_previous_page(struct buffer *buffer)
382 if (!load_previous_page(current_tab))
383 message("No previous page");
384 else
385 start_loading_anim(current_tab);
388 void
389 cmd_next_page(struct buffer *buffer)
391 if (!load_next_page(current_tab))
392 message("No next page");
393 else
394 start_loading_anim(current_tab);
397 void
398 cmd_clear_minibuf(struct buffer *buffer)
400 message(NULL);
403 void
404 cmd_execute_extended_command(struct buffer *buffer)
406 size_t len;
408 GUARD_RECURSIVE_MINIBUFFER();
410 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
411 &eecmd_history, compl_eecmd, NULL);
413 len = sizeof(ministate.prompt);
414 strlcpy(ministate.prompt, "", len);
416 if (thiskey.meta)
417 strlcat(ministate.prompt, "M-", len);
419 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
421 if (thiskey.meta)
422 strlcat(ministate.prompt, " ", len);
425 void
426 cmd_tab_close(struct buffer *buffer)
428 struct tab *tab, *t;
430 tab = current_tab;
432 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
433 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
434 switch_to_tab(t);
435 free_tab(tab);
436 } else
437 message("Can't close the only tab.");
441 void
442 cmd_tab_close_other(struct buffer *buffer)
444 struct tab *t, *i;
446 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
447 if (t == current_tab)
448 continue;
450 free_tab(t);
454 void
455 cmd_tab_new(struct buffer *buffer)
457 const char *url;
459 if ((url = new_tab_url) == NULL)
460 url = NEW_TAB_URL;
462 new_tab(url, NULL);
465 void
466 cmd_tab_next(struct buffer *buffer)
468 struct tab *t;
470 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
471 t = TAILQ_FIRST(&tabshead);
472 switch_to_tab(t);
475 void
476 cmd_tab_previous(struct buffer *buffer)
478 struct tab *t;
480 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
481 t = TAILQ_LAST(&tabshead, tabshead);
482 switch_to_tab(t);
485 void
486 cmd_tab_move(struct buffer *buffer)
488 struct tab *t;
490 t = TAILQ_NEXT(current_tab, tabs);
491 TAILQ_REMOVE(&tabshead, current_tab, tabs);
493 if (t == NULL)
494 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
495 else
496 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
499 void
500 cmd_tab_move_to(struct buffer *buffer)
502 struct tab *t;
504 t = TAILQ_PREV(current_tab, tabshead, tabs);
505 TAILQ_REMOVE(&tabshead, current_tab, tabs);
507 if (t == NULL) {
508 if (TAILQ_EMPTY(&tabshead))
509 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
510 else
511 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
512 } else
513 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
516 void
517 cmd_tab_select(struct buffer *buffer)
519 GUARD_RECURSIVE_MINIBUFFER();
521 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
522 NULL, compl_ts, NULL);
523 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
526 void
527 cmd_load_url(struct buffer *buffer)
529 GUARD_RECURSIVE_MINIBUFFER();
531 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
532 &lu_history, NULL, NULL);
533 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
534 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
535 cmd_move_end_of_line(&ministate.buffer);
538 void
539 cmd_load_current_url(struct buffer *buffer)
541 GUARD_RECURSIVE_MINIBUFFER();
543 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
544 &lu_history, NULL, NULL);
545 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
546 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
547 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
550 void
551 cmd_reload_page(struct buffer *buffer)
553 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL);
556 void
557 cmd_bookmark_page(struct buffer *buffer)
559 GUARD_RECURSIVE_MINIBUFFER();
561 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
562 NULL, NULL);
563 strlcpy(ministate.prompt, "Bookmark 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_list_bookmarks(struct buffer *buffer)
571 load_url_in_tab(current_tab, "about:bookmarks", NULL);
574 void
575 cmd_toggle_help(struct buffer *buffer)
577 ui_toggle_side_window();
580 void
581 cmd_link_select(struct buffer *buffer)
583 struct line *l;
585 GUARD_RECURSIVE_MINIBUFFER();
587 l = TAILQ_FIRST(&buffer->page.head);
588 while (l != NULL && l->type != LINE_LINK)
589 l = TAILQ_NEXT(l, lines);
591 if (l == NULL) {
592 message("No links found");
593 return;
596 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
597 NULL, compl_ls, l);
598 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
601 void
602 cmd_swiper(struct buffer *buffer)
604 GUARD_RECURSIVE_MINIBUFFER();
606 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
607 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
608 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
611 void
612 cmd_toc(struct buffer *buffer)
614 struct line *l;
616 GUARD_RECURSIVE_MINIBUFFER();
618 l = TAILQ_FIRST(&buffer->page.head);
619 while (l != NULL &&
620 l->type != LINE_TITLE_1 &&
621 l->type != LINE_TITLE_2 &&
622 l->type != LINE_TITLE_3)
623 l = TAILQ_NEXT(l, lines);
625 if (l == NULL) {
626 message("No headings found");
627 return;
630 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
631 NULL, compl_toc, l);
632 strlcpy(ministate.prompt, "Select heading: ",
633 sizeof(ministate.prompt));
636 void
637 cmd_inc_fill_column(struct buffer *buffer)
639 if (fill_column == INT_MAX)
640 return;
642 fill_column += 2;
643 message("fill-column: %d", fill_column);
645 ui_schedule_redraw();
648 void
649 cmd_dec_fill_column(struct buffer *buffer)
651 if (fill_column == INT_MAX || fill_column < 8)
652 return;
654 fill_column -= 2;
655 message("fill-column: %d", fill_column);
657 ui_schedule_redraw();
660 void
661 cmd_olivetti_mode(struct buffer *buffer)
663 olivetti_mode = !olivetti_mode;
664 if (olivetti_mode)
665 message("olivetti-mode enabled");
666 else
667 message("olivetti-mode disabled");
669 ui_schedule_redraw();
672 void
673 cmd_mini_delete_char(struct buffer *buffer)
675 char *c, *n;
677 GUARD_READ_ONLY();
679 minibuffer_taint_hist();
681 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
682 if (*c == '\0')
683 return;
684 n = utf8_next_cp(c);
686 memmove(c, n, strlen(n)+1);
688 recompute_completions(0);
691 void
692 cmd_mini_delete_backward_char(struct buffer *buffer)
694 char *c, *p, *start;
696 GUARD_READ_ONLY();
698 minibuffer_taint_hist();
700 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
701 start = buffer->current_line->line;
702 if (c == start)
703 return;
704 p = utf8_prev_cp(c-1, start);
706 memmove(p, c, strlen(c)+1);
707 buffer->cpoff--;
709 recompute_completions(0);
712 void
713 cmd_mini_kill_line(struct buffer *buffer)
715 char *c;
717 GUARD_READ_ONLY();
719 minibuffer_taint_hist();
720 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
721 *c = '\0';
723 recompute_completions(0);
726 void
727 cmd_mini_abort(struct buffer *buffer)
729 if (!in_minibuffer)
730 return;
732 ministate.abortfn();
735 void
736 cmd_mini_complete_and_exit(struct buffer *buffer)
738 if (!in_minibuffer)
739 return;
741 minibuffer_taint_hist();
742 ministate.donefn();
745 void
746 cmd_mini_previous_history_element(struct buffer *buffer)
748 if (ministate.history == NULL) {
749 message("No history");
750 return;
753 if (ministate.hist_cur == NULL ||
754 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
755 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
756 ministate.hist_off = ministate.history->len - 1;
757 if (ministate.hist_cur == NULL)
758 message("No prev history item");
759 } else {
760 ministate.hist_off--;
763 if (ministate.hist_cur != NULL)
764 buffer->current_line->line = ministate.hist_cur->h;
767 void
768 cmd_mini_next_history_element(struct buffer *buffer)
770 if (ministate.history == NULL) {
771 message("No history");
772 return;
775 if (ministate.hist_cur == NULL ||
776 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
777 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
778 ministate.hist_off = 0;
779 if (ministate.hist_cur == NULL)
780 message("No next history item");
781 } else {
782 ministate.hist_off++;
785 if (ministate.hist_cur != NULL)
786 buffer->current_line->line = ministate.hist_cur->h;
789 void
790 cmd_previous_completion(struct buffer *buffer)
792 if (in_minibuffer != MB_COMPREAD)
793 return;
795 buffer = &ministate.compl.buffer;
797 if (buffer->current_line != NULL)
798 buffer->current_line->parent->type = LINE_COMPL;
800 forward_line(buffer, -1);
802 if (buffer->current_line != NULL)
803 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
806 void
807 cmd_next_completion(struct buffer *buffer)
809 if (in_minibuffer != MB_COMPREAD)
810 return;
812 buffer = &ministate.compl.buffer;
814 if (buffer->current_line != NULL)
815 buffer->current_line->parent->type = LINE_COMPL;
817 forward_line(buffer, +1);
819 if (buffer->current_line != NULL)
820 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
823 void
824 cmd_insert_current_candidate(struct buffer *buffer)
826 struct vline *vl;
828 if (in_minibuffer != MB_COMPREAD)
829 return;
831 buffer = &ministate.compl.buffer;
832 if ((vl = buffer->current_line) == NULL)
833 return;
835 minibuffer_taint_hist();
836 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
837 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
840 void
841 cmd_suspend_telescope(struct buffer *buffer)
843 message("Zzz...");
844 ui_suspend();
847 void
848 cmd_toggle_pre_wrap(struct buffer *buffer)
850 dont_wrap_pre = !dont_wrap_pre;
852 if (dont_wrap_pre)
853 message("Don't wrap preformatted blocks");
854 else
855 message("Wrap preformatted blocks");
857 ui_schedule_redraw();
860 void
861 cmd_mini_goto_beginning(struct buffer *buffer)
863 struct vline *vl;
865 if (!in_minibuffer)
866 return;
868 buffer = &ministate.compl.buffer;
870 if ((vl = buffer->current_line) != NULL)
871 vl->parent->type = LINE_COMPL;
873 vl = TAILQ_FIRST(&buffer->head);
874 while (vl != NULL && vl->parent->flags & L_HIDDEN)
875 vl = TAILQ_NEXT(vl, vlines);
877 if (vl == NULL)
878 return;
880 vl->parent->type = LINE_COMPL_CURRENT;
881 buffer->top_line = vl;
882 buffer->current_line = vl;
885 void
886 cmd_mini_goto_end(struct buffer *buffer)
888 struct vline *vl;
890 if (!in_minibuffer)
891 return;
893 buffer = &ministate.compl.buffer;
895 if ((vl = buffer->current_line) != NULL)
896 vl->parent->type = LINE_COMPL;
898 vl = TAILQ_LAST(&buffer->head, vhead);
899 while (vl != NULL && vl->parent->flags & L_HIDDEN)
900 vl = TAILQ_PREV(vl, vhead, vlines);
902 if (vl == NULL)
903 return;
905 vl->parent->type = LINE_COMPL_CURRENT;
906 buffer->current_line = vl;
909 void
910 cmd_other_window(struct buffer *buffer)
912 ui_other_window();
915 void
916 cmd_mini_scroll_up(struct buffer *buffer)
918 if (!in_minibuffer)
919 return;
921 buffer = &ministate.compl.buffer;
922 if (buffer->current_line == NULL)
923 return;
925 buffer->current_line->parent->type = LINE_COMPL;
926 cmd_scroll_up(buffer);
927 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
930 void
931 cmd_mini_scroll_down(struct buffer *buffer)
933 if (!in_minibuffer)
934 return;
936 buffer = &ministate.compl.buffer;
937 if (buffer->current_line == NULL)
938 return;
940 buffer->current_line->parent->type = LINE_COMPL;
941 cmd_scroll_down(buffer);
942 buffer->current_line->parent->type = LINE_COMPL_CURRENT;