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));
552 void
553 cmd_load_current_url(struct buffer *buffer)
555 GUARD_RECURSIVE_MINIBUFFER();
557 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
558 &lu_history, NULL, NULL);
559 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
560 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
561 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
564 void
565 cmd_reload_page(struct buffer *buffer)
567 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL, 1);
570 void
571 cmd_bookmark_page(struct buffer *buffer)
573 GUARD_RECURSIVE_MINIBUFFER();
575 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
576 NULL, NULL);
577 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
578 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
579 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
582 void
583 cmd_list_bookmarks(struct buffer *buffer)
585 load_url_in_tab(current_tab, "about:bookmarks", NULL, 0);
588 void
589 cmd_toggle_help(struct buffer *buffer)
591 ui_toggle_side_window();
594 void
595 cmd_link_select(struct buffer *buffer)
597 struct line *l;
599 GUARD_RECURSIVE_MINIBUFFER();
601 l = TAILQ_FIRST(&buffer->page.head);
602 while (l != NULL && l->type != LINE_LINK)
603 l = TAILQ_NEXT(l, lines);
605 if (l == NULL) {
606 message("No links found");
607 return;
610 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
611 NULL, compl_ls, l);
612 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
615 void
616 cmd_swiper(struct buffer *buffer)
618 GUARD_RECURSIVE_MINIBUFFER();
620 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
621 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
622 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
625 void
626 cmd_toc(struct buffer *buffer)
628 struct line *l;
630 GUARD_RECURSIVE_MINIBUFFER();
632 l = TAILQ_FIRST(&buffer->page.head);
633 while (l != NULL &&
634 l->type != LINE_TITLE_1 &&
635 l->type != LINE_TITLE_2 &&
636 l->type != LINE_TITLE_3)
637 l = TAILQ_NEXT(l, lines);
639 if (l == NULL) {
640 message("No headings found");
641 return;
644 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
645 NULL, compl_toc, l);
646 strlcpy(ministate.prompt, "Select heading: ",
647 sizeof(ministate.prompt));
650 void
651 cmd_inc_fill_column(struct buffer *buffer)
653 if (fill_column == INT_MAX)
654 return;
656 fill_column += 2;
657 message("fill-column: %d", fill_column);
659 ui_schedule_redraw();
662 void
663 cmd_dec_fill_column(struct buffer *buffer)
665 if (fill_column == INT_MAX || fill_column < 8)
666 return;
668 fill_column -= 2;
669 message("fill-column: %d", fill_column);
671 ui_schedule_redraw();
674 void
675 cmd_olivetti_mode(struct buffer *buffer)
677 olivetti_mode = !olivetti_mode;
678 if (olivetti_mode)
679 message("olivetti-mode enabled");
680 else
681 message("olivetti-mode disabled");
683 ui_schedule_redraw();
686 void
687 cmd_mini_delete_char(struct buffer *buffer)
689 char *c, *n;
691 GUARD_READ_ONLY();
693 minibuffer_taint_hist();
695 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
696 if (*c == '\0')
697 return;
698 n = utf8_next_cp(c);
700 memmove(c, n, strlen(n)+1);
702 recompute_completions(0);
705 void
706 cmd_mini_delete_backward_char(struct buffer *buffer)
708 char *c, *p, *start;
710 GUARD_READ_ONLY();
712 minibuffer_taint_hist();
714 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
715 start = buffer->current_line->line;
716 if (c == start)
717 return;
718 p = utf8_prev_cp(c-1, start);
720 memmove(p, c, strlen(c)+1);
721 buffer->cpoff--;
723 recompute_completions(0);
726 void
727 cmd_mini_kill_line(struct buffer *buffer)
729 char *c;
731 GUARD_READ_ONLY();
733 minibuffer_taint_hist();
734 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
735 *c = '\0';
737 recompute_completions(0);
740 void
741 cmd_mini_abort(struct buffer *buffer)
743 if (!in_minibuffer)
744 return;
746 ministate.abortfn();
749 void
750 cmd_mini_complete_and_exit(struct buffer *buffer)
752 if (!in_minibuffer)
753 return;
755 minibuffer_taint_hist();
756 ministate.donefn();
759 void
760 cmd_mini_previous_history_element(struct buffer *buffer)
762 if (ministate.history == NULL) {
763 message("No history");
764 return;
767 if (ministate.hist_cur == NULL ||
768 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
769 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
770 ministate.hist_off = ministate.history->len - 1;
771 if (ministate.hist_cur == NULL)
772 message("No prev history item");
773 } else {
774 ministate.hist_off--;
777 if (ministate.hist_cur != NULL)
778 buffer->current_line->line = ministate.hist_cur->h;
781 void
782 cmd_mini_next_history_element(struct buffer *buffer)
784 if (ministate.history == NULL) {
785 message("No history");
786 return;
789 if (ministate.hist_cur == NULL ||
790 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
791 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
792 ministate.hist_off = 0;
793 if (ministate.hist_cur == NULL)
794 message("No next history item");
795 } else {
796 ministate.hist_off++;
799 if (ministate.hist_cur != NULL)
800 buffer->current_line->line = ministate.hist_cur->h;
803 void
804 cmd_previous_completion(struct buffer *buffer)
806 if (in_minibuffer != MB_COMPREAD)
807 return;
809 buffer = &ministate.compl.buffer;
811 if (buffer->current_line != NULL)
812 buffer->current_line->parent->type = LINE_COMPL;
814 forward_line(buffer, -1);
816 if (buffer->current_line != NULL)
817 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
820 void
821 cmd_next_completion(struct buffer *buffer)
823 if (in_minibuffer != MB_COMPREAD)
824 return;
826 buffer = &ministate.compl.buffer;
828 if (buffer->current_line != NULL)
829 buffer->current_line->parent->type = LINE_COMPL;
831 forward_line(buffer, +1);
833 if (buffer->current_line != NULL)
834 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
837 void
838 cmd_insert_current_candidate(struct buffer *buffer)
840 struct vline *vl;
842 if (in_minibuffer != MB_COMPREAD)
843 return;
845 buffer = &ministate.compl.buffer;
846 if ((vl = buffer->current_line) == NULL)
847 return;
849 minibuffer_taint_hist();
850 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
851 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
854 void
855 cmd_suspend_telescope(struct buffer *buffer)
857 message("Zzz...");
858 ui_suspend();
861 void
862 cmd_toggle_pre_wrap(struct buffer *buffer)
864 dont_wrap_pre = !dont_wrap_pre;
866 if (dont_wrap_pre)
867 message("Don't wrap preformatted blocks");
868 else
869 message("Wrap preformatted blocks");
871 ui_schedule_redraw();
874 void
875 cmd_mini_goto_beginning(struct buffer *buffer)
877 struct vline *vl;
879 if (!in_minibuffer)
880 return;
882 buffer = &ministate.compl.buffer;
884 if ((vl = buffer->current_line) != NULL)
885 vl->parent->type = LINE_COMPL;
887 vl = TAILQ_FIRST(&buffer->head);
888 while (vl != NULL && vl->parent->flags & L_HIDDEN)
889 vl = TAILQ_NEXT(vl, vlines);
891 if (vl == NULL)
892 return;
894 vl->parent->type = LINE_COMPL_CURRENT;
895 buffer->top_line = vl;
896 buffer->current_line = vl;
899 void
900 cmd_mini_goto_end(struct buffer *buffer)
902 struct vline *vl;
904 if (!in_minibuffer)
905 return;
907 buffer = &ministate.compl.buffer;
909 if ((vl = buffer->current_line) != NULL)
910 vl->parent->type = LINE_COMPL;
912 vl = TAILQ_LAST(&buffer->head, vhead);
913 while (vl != NULL && vl->parent->flags & L_HIDDEN)
914 vl = TAILQ_PREV(vl, vhead, vlines);
916 if (vl == NULL)
917 return;
919 vl->parent->type = LINE_COMPL_CURRENT;
920 buffer->current_line = vl;
923 void
924 cmd_other_window(struct buffer *buffer)
926 ui_other_window();
929 void
930 cmd_mini_scroll_up(struct buffer *buffer)
932 if (!in_minibuffer)
933 return;
935 buffer = &ministate.compl.buffer;
936 if (buffer->current_line == NULL)
937 return;
939 buffer->current_line->parent->type = LINE_COMPL;
940 cmd_scroll_up(buffer);
941 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
944 void
945 cmd_mini_scroll_down(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_down(buffer);
956 buffer->current_line->parent->type = LINE_COMPL_CURRENT;