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);
221 buffer->cpoff = body_cols;
224 void
225 cmd_kill_telescope(struct buffer *buffer)
227 save_session();
228 event_loopbreak();
231 void
232 cmd_push_button(struct buffer *buffer)
234 struct vline *vl;
235 struct line *l;
237 vl = buffer->current_line;
238 switch (vl->parent->type) {
239 case LINE_LINK:
240 load_url_in_tab(current_tab, vl->parent->alt);
241 break;
242 case LINE_PRE_START:
243 l = TAILQ_NEXT(vl->parent, lines);
244 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
245 if (l->type == LINE_PRE_END)
246 break;
247 l->flags ^= L_HIDDEN;
248 if (l->flags & L_HIDDEN)
249 buffer->line_max--;
250 else
251 buffer->line_max++;
253 break;
254 default:
255 break;
259 void
260 cmd_push_button_new_tab(struct buffer *buffer)
262 struct vline *vl;
264 vl = buffer->current_line;
265 if (vl->parent->type != LINE_LINK)
266 return;
268 new_tab(vl->parent->alt);
271 void
272 cmd_previous_button(struct buffer *buffer)
274 struct excursion place;
276 save_excursion(&place, buffer);
278 do {
279 if (!forward_line(buffer, -1)) {
280 restore_excursion(&place, buffer);
281 message("No previous link");
282 return;
284 } while (buffer->current_line->parent->type != LINE_LINK);
287 void
288 cmd_next_button(struct buffer *buffer)
290 struct excursion place;
292 save_excursion(&place, buffer);
294 do {
295 if (!forward_line(buffer, +1)){
296 restore_excursion(&place, buffer);
297 message("No next link");
298 return;
300 } while (buffer->current_line->parent->type != LINE_LINK);
303 static inline int
304 is_heading(const struct line *l)
306 return l->type == LINE_TITLE_1 ||
307 l->type == LINE_TITLE_2 ||
308 l->type == LINE_TITLE_3;
311 void
312 cmd_previous_heading(struct buffer *buffer)
314 struct excursion place;
316 save_excursion(&place, buffer);
318 do {
319 if (!forward_line(buffer, -1)) {
320 restore_excursion(&place, buffer);
321 message("No previous heading");
322 return;
324 } while (!is_heading(buffer->current_line->parent));
327 void
328 cmd_next_heading(struct buffer *buffer)
330 struct excursion place;
332 save_excursion(&place, buffer);
334 do {
335 if (!forward_line(buffer, +1)) {
336 restore_excursion(&place, buffer);
337 message("No next heading");
338 return;
340 } while (!is_heading(buffer->current_line->parent));
343 void
344 cmd_previous_page(struct buffer *buffer)
346 if (!load_previous_page(current_tab))
347 message("No previous page");
348 else
349 start_loading_anim(current_tab);
352 void
353 cmd_next_page(struct buffer *buffer)
355 if (!load_next_page(current_tab))
356 message("No next page");
357 else
358 start_loading_anim(current_tab);
361 void
362 cmd_clear_minibuf(struct buffer *buffer)
364 message(NULL);
367 void
368 cmd_execute_extended_command(struct buffer *buffer)
370 size_t len;
372 GUARD_RECURSIVE_MINIBUFFER();
374 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
375 &eecmd_history, compl_eecmd, NULL);
377 len = sizeof(ministate.prompt);
378 strlcpy(ministate.prompt, "", len);
380 if (thiskey.meta)
381 strlcat(ministate.prompt, "M-", len);
383 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
385 if (thiskey.meta)
386 strlcat(ministate.prompt, " ", len);
389 void
390 cmd_tab_close(struct buffer *buffer)
392 struct tab *tab, *t;
394 tab = current_tab;
395 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
396 TAILQ_NEXT(tab, tabs) == NULL) {
397 message("Can't close the only tab.");
398 return;
401 if (evtimer_pending(&tab->loadingev, NULL))
402 evtimer_del(&tab->loadingev);
404 stop_tab(tab);
406 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
407 t = TAILQ_NEXT(tab, tabs);
408 TAILQ_REMOVE(&tabshead, tab, tabs);
409 free(tab);
411 switch_to_tab(t);
414 void
415 cmd_tab_close_other(struct buffer *buffer)
417 struct tab *t, *i;
419 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
420 if (t == current_tab)
421 continue;
423 stop_tab(t);
424 TAILQ_REMOVE(&tabshead, t, tabs);
425 free(t);
429 void
430 cmd_tab_new(struct buffer *buffer)
432 const char *url;
434 if ((url = new_tab_url) == NULL)
435 url = NEW_TAB_URL;
437 new_tab(url);
440 void
441 cmd_tab_next(struct buffer *buffer)
443 struct tab *t;
445 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
446 t = TAILQ_FIRST(&tabshead);
447 t->flags &= ~TAB_URGENT;
448 current_tab = t;
451 void
452 cmd_tab_previous(struct buffer *buffer)
454 struct tab *t;
456 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
457 t = TAILQ_LAST(&tabshead, tabshead);
458 t->flags &= ~TAB_URGENT;
459 current_tab = t;
462 void
463 cmd_tab_move(struct buffer *buffer)
465 struct tab *t;
467 t = TAILQ_NEXT(current_tab, tabs);
468 TAILQ_REMOVE(&tabshead, current_tab, tabs);
470 if (t == NULL)
471 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
472 else
473 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
476 void
477 cmd_tab_move_to(struct buffer *buffer)
479 struct tab *t;
481 t = TAILQ_PREV(current_tab, tabshead, tabs);
482 TAILQ_REMOVE(&tabshead, current_tab, tabs);
484 if (t == NULL) {
485 if (TAILQ_EMPTY(&tabshead))
486 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
487 else
488 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
489 } else
490 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
493 void
494 cmd_tab_select(struct buffer *buffer)
496 GUARD_RECURSIVE_MINIBUFFER();
498 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
499 NULL, compl_ts, NULL);
500 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
503 void
504 cmd_load_url(struct buffer *buffer)
506 GUARD_RECURSIVE_MINIBUFFER();
508 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
509 &lu_history, NULL, NULL);
510 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
511 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
512 cmd_move_end_of_line(&ministate.buffer);
515 void
516 cmd_load_current_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, current_tab->hist_cur->h, sizeof(ministate.buf));
524 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
527 void
528 cmd_reload_page(struct buffer *buffer)
530 load_url_in_tab(current_tab, current_tab->hist_cur->h);
533 void
534 cmd_bookmark_page(struct buffer *buffer)
536 GUARD_RECURSIVE_MINIBUFFER();
538 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
539 NULL, NULL);
540 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
541 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
542 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
545 void
546 cmd_list_bookmarks(struct buffer *buffer)
548 load_url_in_tab(current_tab, "about:bookmarks");
551 void
552 cmd_toggle_help(struct buffer *buffer)
554 ui_toggle_side_window();
557 void
558 cmd_link_select(struct buffer *buffer)
560 struct line *l;
562 GUARD_RECURSIVE_MINIBUFFER();
564 l = TAILQ_FIRST(&buffer->page.head);
565 while (l != NULL && l->type != LINE_LINK)
566 l = TAILQ_NEXT(l, lines);
568 if (l == NULL) {
569 message("No links found");
570 return;
573 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
574 NULL, compl_ls, l);
575 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
578 void
579 cmd_swiper(struct buffer *buffer)
581 GUARD_RECURSIVE_MINIBUFFER();
583 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
584 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
585 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
588 void
589 cmd_toc(struct buffer *buffer)
591 struct line *l;
593 GUARD_RECURSIVE_MINIBUFFER();
595 l = TAILQ_FIRST(&buffer->page.head);
596 while (l != NULL &&
597 l->type != LINE_TITLE_1 &&
598 l->type != LINE_TITLE_2 &&
599 l->type != LINE_TITLE_3)
600 l = TAILQ_NEXT(l, lines);
602 if (l == NULL) {
603 message("No headings found");
604 return;
607 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
608 NULL, compl_toc, l);
609 strlcpy(ministate.prompt, "Select heading: ",
610 sizeof(ministate.prompt));
613 void
614 cmd_inc_fill_column(struct buffer *buffer)
616 if (fill_column == INT_MAX)
617 return;
619 fill_column += 2;
620 message("fill-column: %d", fill_column);
622 ui_schedule_redraw();
625 void
626 cmd_dec_fill_column(struct buffer *buffer)
628 if (fill_column == INT_MAX || fill_column < 8)
629 return;
631 fill_column -= 2;
632 message("fill-column: %d", fill_column);
634 ui_schedule_redraw();
637 void
638 cmd_olivetti_mode(struct buffer *buffer)
640 olivetti_mode = !olivetti_mode;
641 if (olivetti_mode)
642 message("olivetti-mode enabled");
643 else
644 message("olivetti-mode disabled");
646 ui_schedule_redraw();
649 void
650 cmd_mini_delete_char(struct buffer *buffer)
652 char *c, *n;
654 if (!in_minibuffer) {
655 message("text is read-only");
656 return;
659 minibuffer_taint_hist();
661 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
662 if (*c == '\0')
663 return;
664 n = utf8_next_cp(c);
666 memmove(c, n, strlen(n)+1);
668 recompute_completions(0);
671 void
672 cmd_mini_delete_backward_char(struct buffer *buffer)
674 char *c, *p, *start;
676 if (!in_minibuffer) {
677 message("text is read-only");
678 return;
681 minibuffer_taint_hist();
683 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
684 start = buffer->current_line->line;
685 if (c == start)
686 return;
687 p = utf8_prev_cp(c-1, start);
689 memmove(p, c, strlen(c)+1);
690 buffer->cpoff--;
692 recompute_completions(0);
695 void
696 cmd_mini_kill_line(struct buffer *buffer)
698 char *c;
700 if (!in_minibuffer) {
701 message("text is read-only");
702 return;
705 minibuffer_taint_hist();
706 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
707 *c = '\0';
709 recompute_completions(0);
712 void
713 cmd_mini_abort(struct buffer *buffer)
715 if (!in_minibuffer)
716 return;
718 ministate.abortfn();
721 void
722 cmd_mini_complete_and_exit(struct buffer *buffer)
724 if (!in_minibuffer)
725 return;
727 minibuffer_taint_hist();
728 ministate.donefn();
731 void
732 cmd_mini_previous_history_element(struct buffer *buffer)
734 if (ministate.history == NULL) {
735 message("No history");
736 return;
739 if (ministate.hist_cur == NULL ||
740 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
741 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
742 ministate.hist_off = ministate.history->len - 1;
743 if (ministate.hist_cur == NULL)
744 message("No prev history item");
745 } else {
746 ministate.hist_off--;
749 if (ministate.hist_cur != NULL)
750 buffer->current_line->line = ministate.hist_cur->h;
753 void
754 cmd_mini_next_history_element(struct buffer *buffer)
756 if (ministate.history == NULL) {
757 message("No history");
758 return;
761 if (ministate.hist_cur == NULL ||
762 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
763 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
764 ministate.hist_off = 0;
765 if (ministate.hist_cur == NULL)
766 message("No next history item");
767 } else {
768 ministate.hist_off++;
771 if (ministate.hist_cur != NULL)
772 buffer->current_line->line = ministate.hist_cur->h;
775 void
776 cmd_previous_completion(struct buffer *buffer)
778 if (in_minibuffer != MB_COMPREAD)
779 return;
781 buffer = &ministate.compl.buffer;
783 if (buffer->current_line != NULL)
784 buffer->current_line->parent->type = LINE_COMPL;
786 forward_line(buffer, -1);
788 if (buffer->current_line != NULL)
789 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
792 void
793 cmd_next_completion(struct buffer *buffer)
795 if (in_minibuffer != MB_COMPREAD)
796 return;
798 buffer = &ministate.compl.buffer;
800 if (buffer->current_line != NULL)
801 buffer->current_line->parent->type = LINE_COMPL;
803 forward_line(buffer, +1);
805 if (buffer->current_line != NULL)
806 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
809 void
810 cmd_insert_current_candidate(struct buffer *buffer)
812 struct vline *vl;
814 if (in_minibuffer != MB_COMPREAD)
815 return;
817 buffer = &ministate.compl.buffer;
818 if ((vl = buffer->current_line) == NULL)
819 return;
821 minibuffer_taint_hist();
822 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
823 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
826 void
827 cmd_suspend_telescope(struct buffer *buffer)
829 message("Zzz...");
830 ui_suspend();
833 void
834 cmd_toggle_pre_wrap(struct buffer *buffer)
836 dont_wrap_pre = !dont_wrap_pre;
838 if (dont_wrap_pre)
839 message("Don't wrap preformatted blocks");
840 else
841 message("Wrap preformatted blocks");
843 ui_schedule_redraw();
846 void
847 cmd_mini_goto_beginning(struct buffer *buffer)
849 struct vline *vl;
851 if (!in_minibuffer)
852 return;
854 buffer = &ministate.compl.buffer;
856 if ((vl = buffer->current_line) != NULL)
857 vl->parent->type = LINE_COMPL;
859 vl = TAILQ_FIRST(&buffer->head);
860 while (vl != NULL && vl->parent->flags & L_HIDDEN)
861 vl = TAILQ_NEXT(vl, vlines);
863 if (vl == NULL)
864 return;
866 vl->parent->type = LINE_COMPL_CURRENT;
867 buffer->top_line = vl;
868 buffer->current_line = vl;
871 void
872 cmd_mini_goto_end(struct buffer *buffer)
874 struct vline *vl;
876 if (!in_minibuffer)
877 return;
879 buffer = &ministate.compl.buffer;
881 if ((vl = buffer->current_line) != NULL)
882 vl->parent->type = LINE_COMPL;
884 vl = TAILQ_LAST(&buffer->head, vhead);
885 while (vl != NULL && vl->parent->flags & L_HIDDEN)
886 vl = TAILQ_PREV(vl, vhead, vlines);
888 if (vl == NULL)
889 return;
891 vl->parent->type = LINE_COMPL_CURRENT;
892 buffer->current_line = vl;