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 <limits.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "compl.h"
22 #include "defaults.h"
23 #include "minibuffer.h"
24 #include "telescope.h"
25 #include "ui.h"
26 #include "utf8.h"
28 #define GUARD_RECURSIVE_MINIBUFFER() \
29 do { \
30 if (in_minibuffer) { \
31 message("enable-recursive-minibuffers " \
32 "is not yet available."); \
33 return; \
34 } \
35 } while(0)
37 /* return 1 if moved, 0 otherwise */
38 static inline int
39 forward_line(struct buffer *buffer, int n)
40 {
41 struct vline *vl;
42 int did;
44 if (buffer->current_line == NULL)
45 return 0;
46 vl = buffer->current_line;
48 did = 0;
49 while (n != 0) {
50 if (n > 0) {
51 vl = TAILQ_NEXT(vl, vlines);
52 if (vl == NULL)
53 return did;
54 if (vl->parent->flags & L_HIDDEN)
55 continue;
56 buffer->current_line = vl;
57 n--;
58 } else {
59 vl = TAILQ_PREV(vl, vhead, vlines);
60 if (vl == NULL)
61 return did;
62 if (vl->parent->flags & L_HIDDEN)
63 continue;
64 if (buffer->current_line == buffer->top_line) {
65 buffer->line_off--;
66 buffer->top_line = vl;
67 }
68 buffer->current_line = vl;
69 n++;
70 }
72 did = 1;
73 }
75 return did;
76 }
78 void
79 cmd_previous_line(struct buffer *buffer)
80 {
81 forward_line(buffer, -1);
82 }
84 void
85 cmd_next_line(struct buffer *buffer)
86 {
87 forward_line(buffer, +1);
88 }
90 void
91 cmd_backward_char(struct buffer *buffer)
92 {
93 if (buffer->cpoff != 0)
94 buffer->cpoff--;
95 }
97 void
98 cmd_forward_char(struct buffer *buffer)
99 {
100 size_t len = 0;
102 if (buffer->current_line->line != NULL)
103 len = utf8_cplen(buffer->current_line->line);
104 if (++buffer->cpoff > len)
105 buffer->cpoff = len;
108 void
109 cmd_backward_paragraph(struct buffer *buffer)
111 do {
112 if (!forward_line(buffer, -1)) {
113 message("No previous paragraph");
114 return;
116 } while (buffer->current_line->line != NULL ||
117 buffer->current_line->parent->type != LINE_TEXT);
120 void
121 cmd_forward_paragraph(struct buffer *buffer)
123 do {
124 if (!forward_line(buffer, +1)) {
125 message("No next paragraph");
126 return;
128 } while (buffer->current_line->line != NULL ||
129 buffer->current_line->parent->type != LINE_TEXT);
132 void
133 cmd_move_beginning_of_line(struct buffer *buffer)
135 buffer->cpoff = 0;
138 void
139 cmd_move_end_of_line(struct buffer *buffer)
141 struct vline *vl;
143 vl = buffer->current_line;
144 if (vl->line == NULL)
145 return;
146 buffer->cpoff = utf8_cplen(vl->line);
149 void
150 cmd_redraw(struct buffer *buffer)
152 ui_schedule_redraw();
155 void
156 cmd_scroll_line_up(struct buffer *buffer)
158 struct vline *vl;
160 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
161 == NULL)
162 return;
164 forward_line(buffer, -1);
166 buffer->top_line = vl;
169 void
170 cmd_scroll_line_down(struct buffer *buffer)
172 if (!forward_line(buffer, +1))
173 return;
175 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
176 buffer->line_off++;
179 void
180 cmd_scroll_up(struct buffer *buffer)
182 struct vline *vl;
183 int i;
185 for (i = 0; i < body_lines; ++i) {
186 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
187 if (vl == NULL)
188 break;
189 buffer->top_line = vl;
190 forward_line(buffer, -1);
194 void
195 cmd_scroll_down(struct buffer *buffer)
197 int i;
199 for (i = 0; i < body_lines; ++i) {
200 if (!forward_line(buffer, +1))
201 break;
203 buffer->top_line = TAILQ_NEXT(buffer->top_line,
204 vlines);
208 void
209 cmd_beginning_of_buffer(struct buffer *buffer)
211 buffer->current_line = TAILQ_FIRST(&buffer->head);
212 buffer->cpoff = 0;
213 buffer->top_line = buffer->current_line;
214 buffer->line_off = 0;
217 void
218 cmd_end_of_buffer(struct buffer *buffer)
220 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
222 /* deal with invisible lines */
223 if (buffer->current_line->parent->flags & L_HIDDEN)
224 forward_line(buffer, -1);
226 cmd_move_end_of_line(buffer);
229 void
230 cmd_kill_telescope(struct buffer *buffer)
232 save_session();
233 event_loopbreak();
236 void
237 cmd_push_button(struct buffer *buffer)
239 struct vline *vl;
240 struct line *l;
242 vl = buffer->current_line;
243 switch (vl->parent->type) {
244 case LINE_LINK:
245 load_url_in_tab(current_tab, vl->parent->alt);
246 break;
247 case LINE_PRE_START:
248 l = TAILQ_NEXT(vl->parent, lines);
249 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
250 if (l->type == LINE_PRE_END)
251 break;
252 l->flags ^= L_HIDDEN;
253 if (l->flags & L_HIDDEN)
254 buffer->line_max--;
255 else
256 buffer->line_max++;
258 break;
259 default:
260 break;
264 void
265 cmd_push_button_new_tab(struct buffer *buffer)
267 struct vline *vl;
269 vl = buffer->current_line;
270 if (vl->parent->type != LINE_LINK)
271 return;
273 new_tab(vl->parent->alt);
276 void
277 cmd_previous_button(struct buffer *buffer)
279 struct excursion place;
281 save_excursion(&place, buffer);
283 do {
284 if (!forward_line(buffer, -1)) {
285 restore_excursion(&place, buffer);
286 message("No previous link");
287 return;
289 } while (buffer->current_line->parent->type != LINE_LINK);
292 void
293 cmd_next_button(struct buffer *buffer)
295 struct excursion place;
297 save_excursion(&place, buffer);
299 do {
300 if (!forward_line(buffer, +1)){
301 restore_excursion(&place, buffer);
302 message("No next link");
303 return;
305 } while (buffer->current_line->parent->type != LINE_LINK);
308 static inline int
309 is_heading(const struct line *l)
311 return l->type == LINE_TITLE_1 ||
312 l->type == LINE_TITLE_2 ||
313 l->type == LINE_TITLE_3;
316 void
317 cmd_previous_heading(struct buffer *buffer)
319 struct excursion place;
321 save_excursion(&place, buffer);
323 do {
324 if (!forward_line(buffer, -1)) {
325 restore_excursion(&place, buffer);
326 message("No previous heading");
327 return;
329 } while (!is_heading(buffer->current_line->parent));
332 void
333 cmd_next_heading(struct buffer *buffer)
335 struct excursion place;
337 save_excursion(&place, buffer);
339 do {
340 if (!forward_line(buffer, +1)) {
341 restore_excursion(&place, buffer);
342 message("No next heading");
343 return;
345 } while (!is_heading(buffer->current_line->parent));
348 void
349 cmd_previous_page(struct buffer *buffer)
351 if (!load_previous_page(current_tab))
352 message("No previous page");
353 else
354 start_loading_anim(current_tab);
357 void
358 cmd_next_page(struct buffer *buffer)
360 if (!load_next_page(current_tab))
361 message("No next page");
362 else
363 start_loading_anim(current_tab);
366 void
367 cmd_clear_minibuf(struct buffer *buffer)
369 message(NULL);
372 void
373 cmd_execute_extended_command(struct buffer *buffer)
375 size_t len;
377 GUARD_RECURSIVE_MINIBUFFER();
379 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
380 &eecmd_history, compl_eecmd, NULL);
382 len = sizeof(ministate.prompt);
383 strlcpy(ministate.prompt, "", len);
385 if (thiskey.meta)
386 strlcat(ministate.prompt, "M-", len);
388 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
390 if (thiskey.meta)
391 strlcat(ministate.prompt, " ", len);
394 void
395 cmd_tab_close(struct buffer *buffer)
397 struct tab *tab, *t;
399 tab = current_tab;
400 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
401 TAILQ_NEXT(tab, tabs) == NULL) {
402 message("Can't close the only tab.");
403 return;
406 if (evtimer_pending(&tab->loadingev, NULL))
407 evtimer_del(&tab->loadingev);
409 stop_tab(tab);
411 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
412 t = TAILQ_NEXT(tab, tabs);
413 TAILQ_REMOVE(&tabshead, tab, tabs);
414 free(tab);
416 switch_to_tab(t);
419 void
420 cmd_tab_close_other(struct buffer *buffer)
422 struct tab *t, *i;
424 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
425 if (t == current_tab)
426 continue;
428 stop_tab(t);
429 TAILQ_REMOVE(&tabshead, t, tabs);
430 free(t);
434 void
435 cmd_tab_new(struct buffer *buffer)
437 const char *url;
439 if ((url = new_tab_url) == NULL)
440 url = NEW_TAB_URL;
442 new_tab(url);
445 void
446 cmd_tab_next(struct buffer *buffer)
448 struct tab *t;
450 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
451 t = TAILQ_FIRST(&tabshead);
452 switch_to_tab(t);
455 void
456 cmd_tab_previous(struct buffer *buffer)
458 struct tab *t;
460 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
461 t = TAILQ_LAST(&tabshead, tabshead);
462 switch_to_tab(t);
465 void
466 cmd_tab_move(struct buffer *buffer)
468 struct tab *t;
470 t = TAILQ_NEXT(current_tab, tabs);
471 TAILQ_REMOVE(&tabshead, current_tab, tabs);
473 if (t == NULL)
474 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
475 else
476 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
479 void
480 cmd_tab_move_to(struct buffer *buffer)
482 struct tab *t;
484 t = TAILQ_PREV(current_tab, tabshead, tabs);
485 TAILQ_REMOVE(&tabshead, current_tab, tabs);
487 if (t == NULL) {
488 if (TAILQ_EMPTY(&tabshead))
489 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
490 else
491 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
492 } else
493 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
496 void
497 cmd_tab_select(struct buffer *buffer)
499 GUARD_RECURSIVE_MINIBUFFER();
501 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
502 NULL, compl_ts, NULL);
503 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
506 void
507 cmd_load_url(struct buffer *buffer)
509 GUARD_RECURSIVE_MINIBUFFER();
511 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
512 &lu_history, NULL, NULL);
513 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
514 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
515 cmd_move_end_of_line(&ministate.buffer);
518 void
519 cmd_load_current_url(struct buffer *buffer)
521 GUARD_RECURSIVE_MINIBUFFER();
523 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
524 &lu_history, NULL, NULL);
525 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
526 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
527 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
530 void
531 cmd_reload_page(struct buffer *buffer)
533 load_url_in_tab(current_tab, current_tab->hist_cur->h);
536 void
537 cmd_bookmark_page(struct buffer *buffer)
539 GUARD_RECURSIVE_MINIBUFFER();
541 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
542 NULL, NULL);
543 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
544 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
545 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
548 void
549 cmd_list_bookmarks(struct buffer *buffer)
551 load_url_in_tab(current_tab, "about:bookmarks");
554 void
555 cmd_toggle_help(struct buffer *buffer)
557 ui_toggle_side_window();
560 void
561 cmd_link_select(struct buffer *buffer)
563 struct line *l;
565 GUARD_RECURSIVE_MINIBUFFER();
567 l = TAILQ_FIRST(&buffer->page.head);
568 while (l != NULL && l->type != LINE_LINK)
569 l = TAILQ_NEXT(l, lines);
571 if (l == NULL) {
572 message("No links found");
573 return;
576 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
577 NULL, compl_ls, l);
578 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
581 void
582 cmd_swiper(struct buffer *buffer)
584 GUARD_RECURSIVE_MINIBUFFER();
586 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
587 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
588 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
591 void
592 cmd_toc(struct buffer *buffer)
594 struct line *l;
596 GUARD_RECURSIVE_MINIBUFFER();
598 l = TAILQ_FIRST(&buffer->page.head);
599 while (l != NULL &&
600 l->type != LINE_TITLE_1 &&
601 l->type != LINE_TITLE_2 &&
602 l->type != LINE_TITLE_3)
603 l = TAILQ_NEXT(l, lines);
605 if (l == NULL) {
606 message("No headings found");
607 return;
610 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
611 NULL, compl_toc, l);
612 strlcpy(ministate.prompt, "Select heading: ",
613 sizeof(ministate.prompt));
616 void
617 cmd_inc_fill_column(struct buffer *buffer)
619 if (fill_column == INT_MAX)
620 return;
622 fill_column += 2;
623 message("fill-column: %d", fill_column);
625 ui_schedule_redraw();
628 void
629 cmd_dec_fill_column(struct buffer *buffer)
631 if (fill_column == INT_MAX || fill_column < 8)
632 return;
634 fill_column -= 2;
635 message("fill-column: %d", fill_column);
637 ui_schedule_redraw();
640 void
641 cmd_olivetti_mode(struct buffer *buffer)
643 olivetti_mode = !olivetti_mode;
644 if (olivetti_mode)
645 message("olivetti-mode enabled");
646 else
647 message("olivetti-mode disabled");
649 ui_schedule_redraw();
652 void
653 cmd_mini_delete_char(struct buffer *buffer)
655 char *c, *n;
657 if (!in_minibuffer) {
658 message("text is read-only");
659 return;
662 minibuffer_taint_hist();
664 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
665 if (*c == '\0')
666 return;
667 n = utf8_next_cp(c);
669 memmove(c, n, strlen(n)+1);
671 recompute_completions(0);
674 void
675 cmd_mini_delete_backward_char(struct buffer *buffer)
677 char *c, *p, *start;
679 if (!in_minibuffer) {
680 message("text is read-only");
681 return;
684 minibuffer_taint_hist();
686 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
687 start = buffer->current_line->line;
688 if (c == start)
689 return;
690 p = utf8_prev_cp(c-1, start);
692 memmove(p, c, strlen(c)+1);
693 buffer->cpoff--;
695 recompute_completions(0);
698 void
699 cmd_mini_kill_line(struct buffer *buffer)
701 char *c;
703 if (!in_minibuffer) {
704 message("text is read-only");
705 return;
708 minibuffer_taint_hist();
709 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
710 *c = '\0';
712 recompute_completions(0);
715 void
716 cmd_mini_abort(struct buffer *buffer)
718 if (!in_minibuffer)
719 return;
721 ministate.abortfn();
724 void
725 cmd_mini_complete_and_exit(struct buffer *buffer)
727 if (!in_minibuffer)
728 return;
730 minibuffer_taint_hist();
731 ministate.donefn();
734 void
735 cmd_mini_previous_history_element(struct buffer *buffer)
737 if (ministate.history == NULL) {
738 message("No history");
739 return;
742 if (ministate.hist_cur == NULL ||
743 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
744 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
745 ministate.hist_off = ministate.history->len - 1;
746 if (ministate.hist_cur == NULL)
747 message("No prev history item");
748 } else {
749 ministate.hist_off--;
752 if (ministate.hist_cur != NULL)
753 buffer->current_line->line = ministate.hist_cur->h;
756 void
757 cmd_mini_next_history_element(struct buffer *buffer)
759 if (ministate.history == NULL) {
760 message("No history");
761 return;
764 if (ministate.hist_cur == NULL ||
765 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
766 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
767 ministate.hist_off = 0;
768 if (ministate.hist_cur == NULL)
769 message("No next history item");
770 } else {
771 ministate.hist_off++;
774 if (ministate.hist_cur != NULL)
775 buffer->current_line->line = ministate.hist_cur->h;
778 void
779 cmd_previous_completion(struct buffer *buffer)
781 if (in_minibuffer != MB_COMPREAD)
782 return;
784 buffer = &ministate.compl.buffer;
786 if (buffer->current_line != NULL)
787 buffer->current_line->parent->type = LINE_COMPL;
789 forward_line(buffer, -1);
791 if (buffer->current_line != NULL)
792 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
795 void
796 cmd_next_completion(struct buffer *buffer)
798 if (in_minibuffer != MB_COMPREAD)
799 return;
801 buffer = &ministate.compl.buffer;
803 if (buffer->current_line != NULL)
804 buffer->current_line->parent->type = LINE_COMPL;
806 forward_line(buffer, +1);
808 if (buffer->current_line != NULL)
809 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
812 void
813 cmd_insert_current_candidate(struct buffer *buffer)
815 struct vline *vl;
817 if (in_minibuffer != MB_COMPREAD)
818 return;
820 buffer = &ministate.compl.buffer;
821 if ((vl = buffer->current_line) == NULL)
822 return;
824 minibuffer_taint_hist();
825 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
826 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
829 void
830 cmd_suspend_telescope(struct buffer *buffer)
832 message("Zzz...");
833 ui_suspend();
836 void
837 cmd_toggle_pre_wrap(struct buffer *buffer)
839 dont_wrap_pre = !dont_wrap_pre;
841 if (dont_wrap_pre)
842 message("Don't wrap preformatted blocks");
843 else
844 message("Wrap preformatted blocks");
846 ui_schedule_redraw();
849 void
850 cmd_mini_goto_beginning(struct buffer *buffer)
852 struct vline *vl;
854 if (!in_minibuffer)
855 return;
857 buffer = &ministate.compl.buffer;
859 if ((vl = buffer->current_line) != NULL)
860 vl->parent->type = LINE_COMPL;
862 vl = TAILQ_FIRST(&buffer->head);
863 while (vl != NULL && vl->parent->flags & L_HIDDEN)
864 vl = TAILQ_NEXT(vl, vlines);
866 if (vl == NULL)
867 return;
869 vl->parent->type = LINE_COMPL_CURRENT;
870 buffer->top_line = vl;
871 buffer->current_line = vl;
874 void
875 cmd_mini_goto_end(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_LAST(&buffer->head, vhead);
888 while (vl != NULL && vl->parent->flags & L_HIDDEN)
889 vl = TAILQ_PREV(vl, vhead, vlines);
891 if (vl == NULL)
892 return;
894 vl->parent->type = LINE_COMPL_CURRENT;
895 buffer->current_line = vl;