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 #define GUARD_READ_ONLY() \
38 do { \
39 if (!in_minibuffer) { \
40 message("text is read-only"); \
41 return; \
42 } \
43 } while(0)
45 /* return 1 if moved, 0 otherwise */
46 static inline int
47 forward_line(struct buffer *buffer, int n)
48 {
49 struct vline *vl;
50 int did;
52 if (buffer->current_line == NULL)
53 return 0;
54 vl = buffer->current_line;
56 did = 0;
57 while (n != 0) {
58 if (n > 0) {
59 vl = TAILQ_NEXT(vl, vlines);
60 if (vl == NULL)
61 return did;
62 if (vl->parent->flags & L_HIDDEN)
63 continue;
64 buffer->current_line = vl;
65 n--;
66 } else {
67 vl = TAILQ_PREV(vl, vhead, vlines);
68 if (vl == NULL)
69 return did;
70 if (vl->parent->flags & L_HIDDEN)
71 continue;
72 if (buffer->current_line == buffer->top_line) {
73 buffer->line_off--;
74 buffer->top_line = vl;
75 }
76 buffer->current_line = vl;
77 n++;
78 }
80 did = 1;
81 }
83 return did;
84 }
86 void
87 cmd_previous_line(struct buffer *buffer)
88 {
89 forward_line(buffer, -1);
90 }
92 void
93 cmd_next_line(struct buffer *buffer)
94 {
95 forward_line(buffer, +1);
96 }
98 void
99 cmd_backward_char(struct buffer *buffer)
101 if (buffer->cpoff != 0)
102 buffer->cpoff--;
105 void
106 cmd_forward_char(struct buffer *buffer)
108 size_t len = 0;
110 if (buffer->current_line == NULL)
111 return;
113 if (buffer->current_line->line != NULL)
114 len = utf8_cplen(buffer->current_line->line);
115 if (++buffer->cpoff > len)
116 buffer->cpoff = len;
119 void
120 cmd_backward_paragraph(struct buffer *buffer)
122 do {
123 if (!forward_line(buffer, -1)) {
124 message("No previous paragraph");
125 return;
127 } while (buffer->current_line->line != NULL ||
128 buffer->current_line->parent->type != LINE_TEXT);
131 void
132 cmd_forward_paragraph(struct buffer *buffer)
134 do {
135 if (!forward_line(buffer, +1)) {
136 message("No next paragraph");
137 return;
139 } while (buffer->current_line->line != NULL ||
140 buffer->current_line->parent->type != LINE_TEXT);
143 void
144 cmd_move_beginning_of_line(struct buffer *buffer)
146 buffer->cpoff = 0;
149 void
150 cmd_move_end_of_line(struct buffer *buffer)
152 struct vline *vl;
154 vl = buffer->current_line;
155 if (vl == NULL || vl->line == NULL)
156 return;
157 buffer->cpoff = utf8_cplen(vl->line);
160 void
161 cmd_redraw(struct buffer *buffer)
163 ui_schedule_redraw();
166 void
167 cmd_scroll_line_up(struct buffer *buffer)
169 struct vline *vl;
171 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
172 == NULL)
173 return;
175 forward_line(buffer, -1);
177 buffer->top_line = vl;
180 void
181 cmd_scroll_line_down(struct buffer *buffer)
183 if (!forward_line(buffer, +1))
184 return;
186 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
187 buffer->line_off++;
190 void
191 cmd_scroll_up(struct buffer *buffer)
193 struct vline *vl;
194 int i;
196 for (i = 0; i < body_lines; ++i) {
197 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
198 if (vl == NULL)
199 break;
200 buffer->line_off--;
201 buffer->top_line = vl;
202 forward_line(buffer, -1);
206 void
207 cmd_scroll_down(struct buffer *buffer)
209 int i;
211 for (i = 0; i < body_lines; ++i) {
212 if (!forward_line(buffer, +1))
213 break;
215 buffer->top_line = TAILQ_NEXT(buffer->top_line,
216 vlines);
217 buffer->line_off++;
221 void
222 cmd_beginning_of_buffer(struct buffer *buffer)
224 buffer->current_line = TAILQ_FIRST(&buffer->head);
225 buffer->cpoff = 0;
226 buffer->top_line = buffer->current_line;
227 buffer->line_off = 0;
230 void
231 cmd_end_of_buffer(struct buffer *buffer)
233 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
235 if (buffer->current_line == NULL)
236 return;
238 /* deal with invisible lines */
239 if (buffer->current_line->parent->flags & L_HIDDEN)
240 forward_line(buffer, -1);
242 cmd_move_end_of_line(buffer);
245 void
246 cmd_kill_telescope(struct buffer *buffer)
248 save_session();
249 event_loopbreak();
252 void
253 cmd_push_button(struct buffer *buffer)
255 struct vline *vl;
256 struct line *l;
258 vl = buffer->current_line;
260 if (vl == NULL)
261 return;
263 switch (vl->parent->type) {
264 case LINE_LINK:
265 load_url_in_tab(current_tab, vl->parent->alt, NULL);
266 break;
267 case LINE_PRE_START:
268 l = TAILQ_NEXT(vl->parent, lines);
269 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
270 if (l->type == LINE_PRE_END)
271 break;
272 l->flags ^= L_HIDDEN;
273 if (l->flags & L_HIDDEN)
274 buffer->line_max--;
275 else
276 buffer->line_max++;
278 break;
279 default:
280 break;
284 void
285 cmd_push_button_new_tab(struct buffer *buffer)
287 struct vline *vl;
289 vl = buffer->current_line;
290 if (vl == NULL || vl->parent->type != LINE_LINK)
291 return;
293 new_tab(vl->parent->alt, current_tab->hist_cur->h);
296 void
297 cmd_previous_button(struct buffer *buffer)
299 struct excursion place;
301 save_excursion(&place, buffer);
303 do {
304 if (!forward_line(buffer, -1)) {
305 restore_excursion(&place, buffer);
306 message("No previous link");
307 return;
309 } while (buffer->current_line->parent->type != LINE_LINK);
312 void
313 cmd_next_button(struct buffer *buffer)
315 struct excursion place;
317 save_excursion(&place, buffer);
319 do {
320 if (!forward_line(buffer, +1)){
321 restore_excursion(&place, buffer);
322 message("No next link");
323 return;
325 } while (buffer->current_line->parent->type != LINE_LINK);
328 static inline int
329 is_heading(const struct line *l)
331 return l->type == LINE_TITLE_1 ||
332 l->type == LINE_TITLE_2 ||
333 l->type == LINE_TITLE_3;
336 void
337 cmd_previous_heading(struct buffer *buffer)
339 struct excursion place;
341 save_excursion(&place, buffer);
343 do {
344 if (!forward_line(buffer, -1)) {
345 restore_excursion(&place, buffer);
346 message("No previous heading");
347 return;
349 } while (!is_heading(buffer->current_line->parent));
352 void
353 cmd_next_heading(struct buffer *buffer)
355 struct excursion place;
357 save_excursion(&place, buffer);
359 do {
360 if (!forward_line(buffer, +1)) {
361 restore_excursion(&place, buffer);
362 message("No next heading");
363 return;
365 } while (!is_heading(buffer->current_line->parent));
368 void
369 cmd_previous_page(struct buffer *buffer)
371 if (!load_previous_page(current_tab))
372 message("No previous page");
373 else
374 start_loading_anim(current_tab);
377 void
378 cmd_next_page(struct buffer *buffer)
380 if (!load_next_page(current_tab))
381 message("No next page");
382 else
383 start_loading_anim(current_tab);
386 void
387 cmd_clear_minibuf(struct buffer *buffer)
389 message(NULL);
392 void
393 cmd_execute_extended_command(struct buffer *buffer)
395 size_t len;
397 GUARD_RECURSIVE_MINIBUFFER();
399 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
400 &eecmd_history, compl_eecmd, NULL);
402 len = sizeof(ministate.prompt);
403 strlcpy(ministate.prompt, "", len);
405 if (thiskey.meta)
406 strlcat(ministate.prompt, "M-", len);
408 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
410 if (thiskey.meta)
411 strlcat(ministate.prompt, " ", len);
414 void
415 cmd_tab_close(struct buffer *buffer)
417 struct tab *tab, *t;
419 tab = current_tab;
421 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
422 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
423 switch_to_tab(t);
424 free_tab(tab);
425 } else
426 message("Can't close the only tab.");
430 void
431 cmd_tab_close_other(struct buffer *buffer)
433 struct tab *t, *i;
435 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
436 if (t == current_tab)
437 continue;
439 free_tab(t);
443 void
444 cmd_tab_new(struct buffer *buffer)
446 const char *url;
448 if ((url = new_tab_url) == NULL)
449 url = NEW_TAB_URL;
451 new_tab(url, NULL);
454 void
455 cmd_tab_next(struct buffer *buffer)
457 struct tab *t;
459 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
460 t = TAILQ_FIRST(&tabshead);
461 switch_to_tab(t);
464 void
465 cmd_tab_previous(struct buffer *buffer)
467 struct tab *t;
469 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
470 t = TAILQ_LAST(&tabshead, tabshead);
471 switch_to_tab(t);
474 void
475 cmd_tab_move(struct buffer *buffer)
477 struct tab *t;
479 t = TAILQ_NEXT(current_tab, tabs);
480 TAILQ_REMOVE(&tabshead, current_tab, tabs);
482 if (t == NULL)
483 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
484 else
485 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
488 void
489 cmd_tab_move_to(struct buffer *buffer)
491 struct tab *t;
493 t = TAILQ_PREV(current_tab, tabshead, tabs);
494 TAILQ_REMOVE(&tabshead, current_tab, tabs);
496 if (t == NULL) {
497 if (TAILQ_EMPTY(&tabshead))
498 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
499 else
500 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
501 } else
502 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
505 void
506 cmd_tab_select(struct buffer *buffer)
508 GUARD_RECURSIVE_MINIBUFFER();
510 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
511 NULL, compl_ts, NULL);
512 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
515 void
516 cmd_load_url(struct buffer *buffer)
518 GUARD_RECURSIVE_MINIBUFFER();
520 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
521 &lu_history, NULL, NULL);
522 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
523 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
524 cmd_move_end_of_line(&ministate.buffer);
527 void
528 cmd_load_current_url(struct buffer *buffer)
530 GUARD_RECURSIVE_MINIBUFFER();
532 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
533 &lu_history, NULL, NULL);
534 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
535 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
536 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
539 void
540 cmd_reload_page(struct buffer *buffer)
542 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL);
545 void
546 cmd_bookmark_page(struct buffer *buffer)
548 GUARD_RECURSIVE_MINIBUFFER();
550 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
551 NULL, NULL);
552 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
553 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
554 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
557 void
558 cmd_list_bookmarks(struct buffer *buffer)
560 load_url_in_tab(current_tab, "about:bookmarks", NULL);
563 void
564 cmd_toggle_help(struct buffer *buffer)
566 ui_toggle_side_window();
569 void
570 cmd_link_select(struct buffer *buffer)
572 struct line *l;
574 GUARD_RECURSIVE_MINIBUFFER();
576 l = TAILQ_FIRST(&buffer->page.head);
577 while (l != NULL && l->type != LINE_LINK)
578 l = TAILQ_NEXT(l, lines);
580 if (l == NULL) {
581 message("No links found");
582 return;
585 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
586 NULL, compl_ls, l);
587 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
590 void
591 cmd_swiper(struct buffer *buffer)
593 GUARD_RECURSIVE_MINIBUFFER();
595 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
596 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
597 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
600 void
601 cmd_toc(struct buffer *buffer)
603 struct line *l;
605 GUARD_RECURSIVE_MINIBUFFER();
607 l = TAILQ_FIRST(&buffer->page.head);
608 while (l != NULL &&
609 l->type != LINE_TITLE_1 &&
610 l->type != LINE_TITLE_2 &&
611 l->type != LINE_TITLE_3)
612 l = TAILQ_NEXT(l, lines);
614 if (l == NULL) {
615 message("No headings found");
616 return;
619 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
620 NULL, compl_toc, l);
621 strlcpy(ministate.prompt, "Select heading: ",
622 sizeof(ministate.prompt));
625 void
626 cmd_inc_fill_column(struct buffer *buffer)
628 if (fill_column == INT_MAX)
629 return;
631 fill_column += 2;
632 message("fill-column: %d", fill_column);
634 ui_schedule_redraw();
637 void
638 cmd_dec_fill_column(struct buffer *buffer)
640 if (fill_column == INT_MAX || fill_column < 8)
641 return;
643 fill_column -= 2;
644 message("fill-column: %d", fill_column);
646 ui_schedule_redraw();
649 void
650 cmd_olivetti_mode(struct buffer *buffer)
652 olivetti_mode = !olivetti_mode;
653 if (olivetti_mode)
654 message("olivetti-mode enabled");
655 else
656 message("olivetti-mode disabled");
658 ui_schedule_redraw();
661 void
662 cmd_mini_delete_char(struct buffer *buffer)
664 char *c, *n;
666 GUARD_READ_ONLY();
668 minibuffer_taint_hist();
670 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
671 if (*c == '\0')
672 return;
673 n = utf8_next_cp(c);
675 memmove(c, n, strlen(n)+1);
677 recompute_completions(0);
680 void
681 cmd_mini_delete_backward_char(struct buffer *buffer)
683 char *c, *p, *start;
685 GUARD_READ_ONLY();
687 minibuffer_taint_hist();
689 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
690 start = buffer->current_line->line;
691 if (c == start)
692 return;
693 p = utf8_prev_cp(c-1, start);
695 memmove(p, c, strlen(c)+1);
696 buffer->cpoff--;
698 recompute_completions(0);
701 void
702 cmd_mini_kill_line(struct buffer *buffer)
704 char *c;
706 GUARD_READ_ONLY();
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;