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 static void
276 kill_telescope_cb(int r, struct tab *tab)
278 if (r) {
279 save_session();
280 event_loopbreak();
284 void
285 cmd_kill_telescope(struct buffer *buffer)
287 yornp("really quit?", kill_telescope_cb, NULL);
290 void
291 cmd_push_button(struct buffer *buffer)
293 struct vline *vl;
294 struct line *l;
296 vl = buffer->current_line;
298 if (vl == NULL)
299 return;
301 switch (vl->parent->type) {
302 case LINE_LINK:
303 load_url_in_tab(current_tab, vl->parent->alt, NULL,
304 LU_MODE_NOCACHE);
305 break;
306 case LINE_PRE_START:
307 l = TAILQ_NEXT(vl->parent, lines);
308 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
309 if (l->type == LINE_PRE_END)
310 break;
311 l->flags ^= L_HIDDEN;
312 if (l->flags & L_HIDDEN)
313 buffer->line_max--;
314 else
315 buffer->line_max++;
317 break;
318 default:
319 break;
323 void
324 cmd_push_button_new_tab(struct buffer *buffer)
326 struct vline *vl;
328 vl = buffer->current_line;
329 if (vl == NULL || vl->parent->type != LINE_LINK)
330 return;
332 new_tab(vl->parent->alt, current_tab->hist_cur->h, current_tab);
335 void
336 cmd_previous_button(struct buffer *buffer)
338 struct excursion place;
340 save_excursion(&place, buffer);
342 do {
343 if (!forward_line(buffer, -1)) {
344 restore_excursion(&place, buffer);
345 message("No previous link");
346 return;
348 } while (buffer->current_line->parent->type != LINE_LINK);
351 void
352 cmd_next_button(struct buffer *buffer)
354 struct excursion place;
356 save_excursion(&place, buffer);
358 do {
359 if (!forward_line(buffer, +1)){
360 restore_excursion(&place, buffer);
361 message("No next link");
362 return;
364 } while (buffer->current_line->parent->type != LINE_LINK);
367 static inline int
368 is_heading(const struct line *l)
370 return l->type == LINE_TITLE_1 ||
371 l->type == LINE_TITLE_2 ||
372 l->type == LINE_TITLE_3;
375 void
376 cmd_previous_heading(struct buffer *buffer)
378 struct excursion place;
380 save_excursion(&place, buffer);
382 do {
383 if (!forward_line(buffer, -1)) {
384 restore_excursion(&place, buffer);
385 message("No previous heading");
386 return;
388 } while (!is_heading(buffer->current_line->parent));
391 void
392 cmd_next_heading(struct buffer *buffer)
394 struct excursion place;
396 save_excursion(&place, buffer);
398 do {
399 if (!forward_line(buffer, +1)) {
400 restore_excursion(&place, buffer);
401 message("No next heading");
402 return;
404 } while (!is_heading(buffer->current_line->parent));
407 void
408 cmd_previous_page(struct buffer *buffer)
410 if (!load_previous_page(current_tab))
411 message("No previous page");
412 else
413 start_loading_anim(current_tab);
416 void
417 cmd_next_page(struct buffer *buffer)
419 if (!load_next_page(current_tab))
420 message("No next page");
421 else
422 start_loading_anim(current_tab);
425 void
426 cmd_clear_minibuf(struct buffer *buffer)
428 message(NULL);
431 void
432 cmd_execute_extended_command(struct buffer *buffer)
434 size_t len;
436 GUARD_RECURSIVE_MINIBUFFER();
438 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
439 &eecmd_history, compl_eecmd, NULL);
441 len = sizeof(ministate.prompt);
442 strlcpy(ministate.prompt, "", len);
444 if (thiskey.meta)
445 strlcat(ministate.prompt, "M-", len);
447 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
449 if (thiskey.meta)
450 strlcat(ministate.prompt, " ", len);
453 void
454 cmd_tab_close(struct buffer *buffer)
456 struct tab *tab, *t;
458 tab = current_tab;
460 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
461 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
462 switch_to_tab(t);
463 kill_tab(tab, 0);
464 } else
465 message("Can't close the only tab.");
469 void
470 cmd_tab_close_other(struct buffer *buffer)
472 struct tab *t, *i;
474 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
475 if (t == current_tab)
476 continue;
478 kill_tab(t, 0);
482 void
483 cmd_tab_undo_close(struct buffer *buffer)
485 struct tab *t;
487 if ((t = unkill_tab()) == NULL) {
488 message("No recently-closed tabs");
489 return;
492 switch_to_tab(t);
495 void
496 cmd_tab_new(struct buffer *buffer)
498 const char *url;
500 if ((url = new_tab_url) == NULL)
501 url = NEW_TAB_URL;
503 new_tab(url, NULL, NULL);
506 void
507 cmd_tab_next(struct buffer *buffer)
509 struct tab *t;
511 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
512 t = TAILQ_FIRST(&tabshead);
513 switch_to_tab(t);
516 void
517 cmd_tab_previous(struct buffer *buffer)
519 struct tab *t;
521 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
522 t = TAILQ_LAST(&tabshead, tabshead);
523 switch_to_tab(t);
526 void
527 cmd_tab_move(struct buffer *buffer)
529 struct tab *t;
531 t = TAILQ_NEXT(current_tab, tabs);
532 TAILQ_REMOVE(&tabshead, current_tab, tabs);
534 if (t == NULL)
535 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
536 else
537 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
540 void
541 cmd_tab_move_to(struct buffer *buffer)
543 struct tab *t;
545 t = TAILQ_PREV(current_tab, tabshead, tabs);
546 TAILQ_REMOVE(&tabshead, current_tab, tabs);
548 if (t == NULL)
549 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
550 else
551 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
554 void
555 cmd_tab_select(struct buffer *buffer)
557 GUARD_RECURSIVE_MINIBUFFER();
559 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
560 NULL, compl_ts, NULL);
561 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
564 void
565 cmd_load_url(struct buffer *buffer)
567 GUARD_RECURSIVE_MINIBUFFER();
569 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
570 &lu_history, NULL, NULL);
571 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
574 void
575 cmd_load_current_url(struct buffer *buffer)
577 GUARD_RECURSIVE_MINIBUFFER();
579 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
580 &lu_history, NULL, NULL);
581 strlcpy(ministate.prompt, "Load 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_reload_page(struct buffer *buffer)
589 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL,
590 LU_MODE_NOHIST|LU_MODE_NOCACHE);
593 void
594 cmd_bookmark_page(struct buffer *buffer)
596 GUARD_RECURSIVE_MINIBUFFER();
598 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
599 NULL, NULL);
600 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
601 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
602 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
605 void
606 cmd_list_bookmarks(struct buffer *buffer)
608 load_url_in_tab(current_tab, "about:bookmarks", NULL, LU_MODE_NONE);
611 void
612 cmd_toggle_help(struct buffer *buffer)
614 ui_toggle_side_window(SIDE_WINDOW_LEFT);
617 void
618 cmd_link_select(struct buffer *buffer)
620 struct line *l;
622 GUARD_RECURSIVE_MINIBUFFER();
624 l = TAILQ_FIRST(&buffer->page.head);
625 while (l != NULL && l->type != LINE_LINK)
626 l = TAILQ_NEXT(l, lines);
628 if (l == NULL) {
629 message("No links found");
630 return;
633 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
634 NULL, compl_ls, l);
635 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
638 void
639 cmd_swiper(struct buffer *buffer)
641 GUARD_RECURSIVE_MINIBUFFER();
643 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
644 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
645 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
648 void
649 cmd_toc(struct buffer *buffer)
651 struct line *l;
653 GUARD_RECURSIVE_MINIBUFFER();
655 l = TAILQ_FIRST(&buffer->page.head);
656 while (l != NULL &&
657 l->type != LINE_TITLE_1 &&
658 l->type != LINE_TITLE_2 &&
659 l->type != LINE_TITLE_3)
660 l = TAILQ_NEXT(l, lines);
662 if (l == NULL) {
663 message("No headings found");
664 return;
667 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
668 NULL, compl_toc, l);
669 strlcpy(ministate.prompt, "Select heading: ",
670 sizeof(ministate.prompt));
673 void
674 cmd_inc_fill_column(struct buffer *buffer)
676 if (fill_column == INT_MAX)
677 return;
679 fill_column += 2;
680 message("fill-column: %d", fill_column);
682 ui_schedule_redraw();
685 void
686 cmd_dec_fill_column(struct buffer *buffer)
688 if (fill_column == INT_MAX || fill_column < 8)
689 return;
691 fill_column -= 2;
692 message("fill-column: %d", fill_column);
694 ui_schedule_redraw();
697 void
698 cmd_olivetti_mode(struct buffer *buffer)
700 olivetti_mode = !olivetti_mode;
701 if (olivetti_mode)
702 message("olivetti-mode enabled");
703 else
704 message("olivetti-mode disabled");
706 ui_schedule_redraw();
709 void
710 cmd_mini_delete_char(struct buffer *buffer)
712 char *c, *n;
714 GUARD_READ_ONLY();
716 minibuffer_taint_hist();
718 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
719 if (*c == '\0')
720 return;
721 n = utf8_next_cp(c);
723 memmove(c, n, strlen(n)+1);
725 recompute_completions(0);
728 void
729 cmd_mini_delete_backward_char(struct buffer *buffer)
731 char *c, *p, *start;
733 GUARD_READ_ONLY();
735 minibuffer_taint_hist();
737 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
738 start = buffer->current_line->line;
739 if (c == start)
740 return;
741 p = utf8_prev_cp(c-1, start);
743 memmove(p, c, strlen(c)+1);
744 buffer->cpoff--;
746 recompute_completions(0);
749 void
750 cmd_mini_kill_line(struct buffer *buffer)
752 char *c;
754 GUARD_READ_ONLY();
756 minibuffer_taint_hist();
757 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
758 *c = '\0';
760 recompute_completions(0);
763 void
764 cmd_mini_abort(struct buffer *buffer)
766 if (!in_minibuffer)
767 return;
769 ministate.abortfn();
772 void
773 cmd_mini_complete_and_exit(struct buffer *buffer)
775 if (!in_minibuffer)
776 return;
778 minibuffer_taint_hist();
779 ministate.donefn();
782 void
783 cmd_mini_previous_history_element(struct buffer *buffer)
785 if (ministate.history == NULL) {
786 message("No history");
787 return;
790 if (ministate.hist_cur == NULL ||
791 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
792 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
793 ministate.hist_off = ministate.history->len - 1;
794 if (ministate.hist_cur == NULL)
795 message("No prev history item");
796 } else {
797 ministate.hist_off--;
800 if (ministate.hist_cur != NULL)
801 buffer->current_line->line = ministate.hist_cur->h;
804 void
805 cmd_mini_next_history_element(struct buffer *buffer)
807 if (ministate.history == NULL) {
808 message("No history");
809 return;
812 if (ministate.hist_cur == NULL ||
813 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
814 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
815 ministate.hist_off = 0;
816 if (ministate.hist_cur == NULL)
817 message("No next history item");
818 } else {
819 ministate.hist_off++;
822 if (ministate.hist_cur != NULL)
823 buffer->current_line->line = ministate.hist_cur->h;
826 void
827 cmd_previous_completion(struct buffer *buffer)
829 if (in_minibuffer != MB_COMPREAD)
830 return;
832 buffer = &ministate.compl.buffer;
834 if (buffer->current_line != NULL)
835 buffer->current_line->parent->type = LINE_COMPL;
837 forward_line(buffer, -1);
839 if (buffer->current_line != NULL)
840 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
843 void
844 cmd_next_completion(struct buffer *buffer)
846 if (in_minibuffer != MB_COMPREAD)
847 return;
849 buffer = &ministate.compl.buffer;
851 if (buffer->current_line != NULL)
852 buffer->current_line->parent->type = LINE_COMPL;
854 forward_line(buffer, +1);
856 if (buffer->current_line != NULL)
857 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
860 void
861 cmd_insert_current_candidate(struct buffer *buffer)
863 if (in_minibuffer != MB_COMPREAD)
864 return;
866 minibuffer_insert_current_candidate();
869 void
870 cmd_suspend_telescope(struct buffer *buffer)
872 message("Zzz...");
873 ui_suspend();
876 void
877 cmd_toggle_pre_wrap(struct buffer *buffer)
879 dont_wrap_pre = !dont_wrap_pre;
881 if (dont_wrap_pre)
882 message("Don't wrap preformatted blocks");
883 else
884 message("Wrap preformatted blocks");
886 ui_schedule_redraw();
889 void
890 cmd_mini_goto_beginning(struct buffer *buffer)
892 struct vline *vl;
894 if (!in_minibuffer)
895 return;
897 buffer = &ministate.compl.buffer;
899 if ((vl = buffer->current_line) != NULL)
900 vl->parent->type = LINE_COMPL;
902 vl = TAILQ_FIRST(&buffer->head);
903 while (vl != NULL && vl->parent->flags & L_HIDDEN)
904 vl = TAILQ_NEXT(vl, vlines);
906 if (vl == NULL)
907 return;
909 vl->parent->type = LINE_COMPL_CURRENT;
910 buffer->top_line = vl;
911 buffer->current_line = vl;
914 void
915 cmd_mini_goto_end(struct buffer *buffer)
917 struct vline *vl;
919 if (!in_minibuffer)
920 return;
922 buffer = &ministate.compl.buffer;
924 if ((vl = buffer->current_line) != NULL)
925 vl->parent->type = LINE_COMPL;
927 vl = TAILQ_LAST(&buffer->head, vhead);
928 while (vl != NULL && vl->parent->flags & L_HIDDEN)
929 vl = TAILQ_PREV(vl, vhead, vlines);
931 if (vl == NULL)
932 return;
934 vl->parent->type = LINE_COMPL_CURRENT;
935 buffer->current_line = vl;
938 void
939 cmd_other_window(struct buffer *buffer)
941 ui_other_window();
944 void
945 cmd_mini_scroll_up(struct buffer *buffer)
947 if (!in_minibuffer)
948 return;
950 buffer = &ministate.compl.buffer;
951 if (buffer->current_line == NULL)
952 return;
954 buffer->current_line->parent->type = LINE_COMPL;
955 cmd_scroll_up(buffer);
956 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
959 void
960 cmd_mini_scroll_down(struct buffer *buffer)
962 if (!in_minibuffer)
963 return;
965 buffer = &ministate.compl.buffer;
966 if (buffer->current_line == NULL)
967 return;
969 buffer->current_line->parent->type = LINE_COMPL;
970 cmd_scroll_down(buffer);
971 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
974 void
975 cmd_toggle_downloads(struct buffer *buffer)
977 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);