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, 0);
304 break;
305 case LINE_PRE_START:
306 l = TAILQ_NEXT(vl->parent, lines);
307 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
308 if (l->type == LINE_PRE_END)
309 break;
310 l->flags ^= L_HIDDEN;
311 if (l->flags & L_HIDDEN)
312 buffer->line_max--;
313 else
314 buffer->line_max++;
316 break;
317 default:
318 break;
322 void
323 cmd_push_button_new_tab(struct buffer *buffer)
325 struct vline *vl;
327 vl = buffer->current_line;
328 if (vl == NULL || vl->parent->type != LINE_LINK)
329 return;
331 new_tab(vl->parent->alt, current_tab->hist_cur->h, current_tab);
334 void
335 cmd_previous_button(struct buffer *buffer)
337 struct excursion place;
339 save_excursion(&place, buffer);
341 do {
342 if (!forward_line(buffer, -1)) {
343 restore_excursion(&place, buffer);
344 message("No previous link");
345 return;
347 } while (buffer->current_line->parent->type != LINE_LINK);
350 void
351 cmd_next_button(struct buffer *buffer)
353 struct excursion place;
355 save_excursion(&place, buffer);
357 do {
358 if (!forward_line(buffer, +1)){
359 restore_excursion(&place, buffer);
360 message("No next link");
361 return;
363 } while (buffer->current_line->parent->type != LINE_LINK);
366 static inline int
367 is_heading(const struct line *l)
369 return l->type == LINE_TITLE_1 ||
370 l->type == LINE_TITLE_2 ||
371 l->type == LINE_TITLE_3;
374 void
375 cmd_previous_heading(struct buffer *buffer)
377 struct excursion place;
379 save_excursion(&place, buffer);
381 do {
382 if (!forward_line(buffer, -1)) {
383 restore_excursion(&place, buffer);
384 message("No previous heading");
385 return;
387 } while (!is_heading(buffer->current_line->parent));
390 void
391 cmd_next_heading(struct buffer *buffer)
393 struct excursion place;
395 save_excursion(&place, buffer);
397 do {
398 if (!forward_line(buffer, +1)) {
399 restore_excursion(&place, buffer);
400 message("No next heading");
401 return;
403 } while (!is_heading(buffer->current_line->parent));
406 void
407 cmd_previous_page(struct buffer *buffer)
409 if (!load_previous_page(current_tab))
410 message("No previous page");
411 else
412 start_loading_anim(current_tab);
415 void
416 cmd_next_page(struct buffer *buffer)
418 if (!load_next_page(current_tab))
419 message("No next page");
420 else
421 start_loading_anim(current_tab);
424 void
425 cmd_clear_minibuf(struct buffer *buffer)
427 message(NULL);
430 void
431 cmd_execute_extended_command(struct buffer *buffer)
433 size_t len;
435 GUARD_RECURSIVE_MINIBUFFER();
437 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
438 &eecmd_history, compl_eecmd, NULL);
440 len = sizeof(ministate.prompt);
441 strlcpy(ministate.prompt, "", len);
443 if (thiskey.meta)
444 strlcat(ministate.prompt, "M-", len);
446 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
448 if (thiskey.meta)
449 strlcat(ministate.prompt, " ", len);
452 void
453 cmd_tab_close(struct buffer *buffer)
455 struct tab *tab, *t;
457 tab = current_tab;
459 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
460 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
461 switch_to_tab(t);
462 kill_tab(tab, 0);
463 } else
464 message("Can't close the only tab.");
468 void
469 cmd_tab_close_other(struct buffer *buffer)
471 struct tab *t, *i;
473 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
474 if (t == current_tab)
475 continue;
477 kill_tab(t, 0);
481 void
482 cmd_tab_undo_close(struct buffer *buffer)
484 struct tab *t;
486 if ((t = unkill_tab()) == NULL) {
487 message("No recently-closed tabs");
488 return;
491 switch_to_tab(t);
494 void
495 cmd_tab_new(struct buffer *buffer)
497 const char *url;
499 if ((url = new_tab_url) == NULL)
500 url = NEW_TAB_URL;
502 new_tab(url, NULL, NULL);
505 void
506 cmd_tab_next(struct buffer *buffer)
508 struct tab *t;
510 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
511 t = TAILQ_FIRST(&tabshead);
512 switch_to_tab(t);
515 void
516 cmd_tab_previous(struct buffer *buffer)
518 struct tab *t;
520 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
521 t = TAILQ_LAST(&tabshead, tabshead);
522 switch_to_tab(t);
525 void
526 cmd_tab_move(struct buffer *buffer)
528 struct tab *t;
530 t = TAILQ_NEXT(current_tab, tabs);
531 TAILQ_REMOVE(&tabshead, current_tab, tabs);
533 if (t == NULL)
534 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
535 else
536 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
539 void
540 cmd_tab_move_to(struct buffer *buffer)
542 struct tab *t;
544 t = TAILQ_PREV(current_tab, tabshead, tabs);
545 TAILQ_REMOVE(&tabshead, current_tab, tabs);
547 if (t == NULL)
548 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
549 else
550 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
553 void
554 cmd_tab_select(struct buffer *buffer)
556 GUARD_RECURSIVE_MINIBUFFER();
558 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
559 NULL, compl_ts, NULL);
560 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
563 void
564 cmd_load_url(struct buffer *buffer)
566 GUARD_RECURSIVE_MINIBUFFER();
568 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
569 &lu_history, NULL, NULL);
570 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
573 void
574 cmd_load_current_url(struct buffer *buffer)
576 GUARD_RECURSIVE_MINIBUFFER();
578 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
579 &lu_history, NULL, NULL);
580 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
581 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
582 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
585 void
586 cmd_reload_page(struct buffer *buffer)
588 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL, 1);
591 void
592 cmd_bookmark_page(struct buffer *buffer)
594 GUARD_RECURSIVE_MINIBUFFER();
596 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
597 NULL, NULL);
598 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
599 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
600 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
603 void
604 cmd_list_bookmarks(struct buffer *buffer)
606 load_url_in_tab(current_tab, "about:bookmarks", NULL, 0);
609 void
610 cmd_toggle_help(struct buffer *buffer)
612 ui_toggle_side_window(SIDE_WINDOW_LEFT);
615 void
616 cmd_link_select(struct buffer *buffer)
618 struct line *l;
620 GUARD_RECURSIVE_MINIBUFFER();
622 l = TAILQ_FIRST(&buffer->page.head);
623 while (l != NULL && l->type != LINE_LINK)
624 l = TAILQ_NEXT(l, lines);
626 if (l == NULL) {
627 message("No links found");
628 return;
631 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
632 NULL, compl_ls, l);
633 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
636 void
637 cmd_swiper(struct buffer *buffer)
639 GUARD_RECURSIVE_MINIBUFFER();
641 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
642 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
643 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
646 void
647 cmd_toc(struct buffer *buffer)
649 struct line *l;
651 GUARD_RECURSIVE_MINIBUFFER();
653 l = TAILQ_FIRST(&buffer->page.head);
654 while (l != NULL &&
655 l->type != LINE_TITLE_1 &&
656 l->type != LINE_TITLE_2 &&
657 l->type != LINE_TITLE_3)
658 l = TAILQ_NEXT(l, lines);
660 if (l == NULL) {
661 message("No headings found");
662 return;
665 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
666 NULL, compl_toc, l);
667 strlcpy(ministate.prompt, "Select heading: ",
668 sizeof(ministate.prompt));
671 void
672 cmd_inc_fill_column(struct buffer *buffer)
674 if (fill_column == INT_MAX)
675 return;
677 fill_column += 2;
678 message("fill-column: %d", fill_column);
680 ui_schedule_redraw();
683 void
684 cmd_dec_fill_column(struct buffer *buffer)
686 if (fill_column == INT_MAX || fill_column < 8)
687 return;
689 fill_column -= 2;
690 message("fill-column: %d", fill_column);
692 ui_schedule_redraw();
695 void
696 cmd_olivetti_mode(struct buffer *buffer)
698 olivetti_mode = !olivetti_mode;
699 if (olivetti_mode)
700 message("olivetti-mode enabled");
701 else
702 message("olivetti-mode disabled");
704 ui_schedule_redraw();
707 void
708 cmd_mini_delete_char(struct buffer *buffer)
710 char *c, *n;
712 GUARD_READ_ONLY();
714 minibuffer_taint_hist();
716 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
717 if (*c == '\0')
718 return;
719 n = utf8_next_cp(c);
721 memmove(c, n, strlen(n)+1);
723 recompute_completions(0);
726 void
727 cmd_mini_delete_backward_char(struct buffer *buffer)
729 char *c, *p, *start;
731 GUARD_READ_ONLY();
733 minibuffer_taint_hist();
735 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
736 start = buffer->current_line->line;
737 if (c == start)
738 return;
739 p = utf8_prev_cp(c-1, start);
741 memmove(p, c, strlen(c)+1);
742 buffer->cpoff--;
744 recompute_completions(0);
747 void
748 cmd_mini_kill_line(struct buffer *buffer)
750 char *c;
752 GUARD_READ_ONLY();
754 minibuffer_taint_hist();
755 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
756 *c = '\0';
758 recompute_completions(0);
761 void
762 cmd_mini_abort(struct buffer *buffer)
764 if (!in_minibuffer)
765 return;
767 ministate.abortfn();
770 void
771 cmd_mini_complete_and_exit(struct buffer *buffer)
773 if (!in_minibuffer)
774 return;
776 minibuffer_taint_hist();
777 ministate.donefn();
780 void
781 cmd_mini_previous_history_element(struct buffer *buffer)
783 if (ministate.history == NULL) {
784 message("No history");
785 return;
788 if (ministate.hist_cur == NULL ||
789 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
790 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
791 ministate.hist_off = ministate.history->len - 1;
792 if (ministate.hist_cur == NULL)
793 message("No prev history item");
794 } else {
795 ministate.hist_off--;
798 if (ministate.hist_cur != NULL)
799 buffer->current_line->line = ministate.hist_cur->h;
802 void
803 cmd_mini_next_history_element(struct buffer *buffer)
805 if (ministate.history == NULL) {
806 message("No history");
807 return;
810 if (ministate.hist_cur == NULL ||
811 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
812 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
813 ministate.hist_off = 0;
814 if (ministate.hist_cur == NULL)
815 message("No next history item");
816 } else {
817 ministate.hist_off++;
820 if (ministate.hist_cur != NULL)
821 buffer->current_line->line = ministate.hist_cur->h;
824 void
825 cmd_previous_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_next_completion(struct buffer *buffer)
844 if (in_minibuffer != MB_COMPREAD)
845 return;
847 buffer = &ministate.compl.buffer;
849 if (buffer->current_line != NULL)
850 buffer->current_line->parent->type = LINE_COMPL;
852 forward_line(buffer, +1);
854 if (buffer->current_line != NULL)
855 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
858 void
859 cmd_insert_current_candidate(struct buffer *buffer)
861 if (in_minibuffer != MB_COMPREAD)
862 return;
864 minibuffer_insert_current_candidate();
867 void
868 cmd_suspend_telescope(struct buffer *buffer)
870 message("Zzz...");
871 ui_suspend();
874 void
875 cmd_toggle_pre_wrap(struct buffer *buffer)
877 dont_wrap_pre = !dont_wrap_pre;
879 if (dont_wrap_pre)
880 message("Don't wrap preformatted blocks");
881 else
882 message("Wrap preformatted blocks");
884 ui_schedule_redraw();
887 void
888 cmd_mini_goto_beginning(struct buffer *buffer)
890 struct vline *vl;
892 if (!in_minibuffer)
893 return;
895 buffer = &ministate.compl.buffer;
897 if ((vl = buffer->current_line) != NULL)
898 vl->parent->type = LINE_COMPL;
900 vl = TAILQ_FIRST(&buffer->head);
901 while (vl != NULL && vl->parent->flags & L_HIDDEN)
902 vl = TAILQ_NEXT(vl, vlines);
904 if (vl == NULL)
905 return;
907 vl->parent->type = LINE_COMPL_CURRENT;
908 buffer->top_line = vl;
909 buffer->current_line = vl;
912 void
913 cmd_mini_goto_end(struct buffer *buffer)
915 struct vline *vl;
917 if (!in_minibuffer)
918 return;
920 buffer = &ministate.compl.buffer;
922 if ((vl = buffer->current_line) != NULL)
923 vl->parent->type = LINE_COMPL;
925 vl = TAILQ_LAST(&buffer->head, vhead);
926 while (vl != NULL && vl->parent->flags & L_HIDDEN)
927 vl = TAILQ_PREV(vl, vhead, vlines);
929 if (vl == NULL)
930 return;
932 vl->parent->type = LINE_COMPL_CURRENT;
933 buffer->current_line = vl;
936 void
937 cmd_other_window(struct buffer *buffer)
939 ui_other_window();
942 void
943 cmd_mini_scroll_up(struct buffer *buffer)
945 if (!in_minibuffer)
946 return;
948 buffer = &ministate.compl.buffer;
949 if (buffer->current_line == NULL)
950 return;
952 buffer->current_line->parent->type = LINE_COMPL;
953 cmd_scroll_up(buffer);
954 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
957 void
958 cmd_mini_scroll_down(struct buffer *buffer)
960 if (!in_minibuffer)
961 return;
963 buffer = &ministate.compl.buffer;
964 if (buffer->current_line == NULL)
965 return;
967 buffer->current_line->parent->type = LINE_COMPL;
968 cmd_scroll_down(buffer);
969 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
972 void
973 cmd_toggle_downloads(struct buffer *buffer)
975 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);