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 == NULL)
103 return;
105 if (buffer->current_line->line != NULL)
106 len = utf8_cplen(buffer->current_line->line);
107 if (++buffer->cpoff > len)
108 buffer->cpoff = len;
111 void
112 cmd_backward_paragraph(struct buffer *buffer)
114 do {
115 if (!forward_line(buffer, -1)) {
116 message("No previous paragraph");
117 return;
119 } while (buffer->current_line->line != NULL ||
120 buffer->current_line->parent->type != LINE_TEXT);
123 void
124 cmd_forward_paragraph(struct buffer *buffer)
126 do {
127 if (!forward_line(buffer, +1)) {
128 message("No next paragraph");
129 return;
131 } while (buffer->current_line->line != NULL ||
132 buffer->current_line->parent->type != LINE_TEXT);
135 void
136 cmd_move_beginning_of_line(struct buffer *buffer)
138 buffer->cpoff = 0;
141 void
142 cmd_move_end_of_line(struct buffer *buffer)
144 struct vline *vl;
146 vl = buffer->current_line;
147 if (vl->line == NULL)
148 return;
149 buffer->cpoff = utf8_cplen(vl->line);
152 void
153 cmd_redraw(struct buffer *buffer)
155 ui_schedule_redraw();
158 void
159 cmd_scroll_line_up(struct buffer *buffer)
161 struct vline *vl;
163 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
164 == NULL)
165 return;
167 forward_line(buffer, -1);
169 buffer->top_line = vl;
172 void
173 cmd_scroll_line_down(struct buffer *buffer)
175 if (!forward_line(buffer, +1))
176 return;
178 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
179 buffer->line_off++;
182 void
183 cmd_scroll_up(struct buffer *buffer)
185 struct vline *vl;
186 int i;
188 for (i = 0; i < body_lines; ++i) {
189 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
190 if (vl == NULL)
191 break;
192 buffer->top_line = vl;
193 forward_line(buffer, -1);
197 void
198 cmd_scroll_down(struct buffer *buffer)
200 int i;
202 for (i = 0; i < body_lines; ++i) {
203 if (!forward_line(buffer, +1))
204 break;
206 buffer->top_line = TAILQ_NEXT(buffer->top_line,
207 vlines);
211 void
212 cmd_beginning_of_buffer(struct buffer *buffer)
214 buffer->current_line = TAILQ_FIRST(&buffer->head);
215 buffer->cpoff = 0;
216 buffer->top_line = buffer->current_line;
217 buffer->line_off = 0;
220 void
221 cmd_end_of_buffer(struct buffer *buffer)
223 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
225 /* deal with invisible lines */
226 if (buffer->current_line->parent->flags & L_HIDDEN)
227 forward_line(buffer, -1);
229 cmd_move_end_of_line(buffer);
232 void
233 cmd_kill_telescope(struct buffer *buffer)
235 save_session();
236 event_loopbreak();
239 void
240 cmd_push_button(struct buffer *buffer)
242 struct vline *vl;
243 struct line *l;
245 vl = buffer->current_line;
246 switch (vl->parent->type) {
247 case LINE_LINK:
248 load_url_in_tab(current_tab, vl->parent->alt);
249 break;
250 case LINE_PRE_START:
251 l = TAILQ_NEXT(vl->parent, lines);
252 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
253 if (l->type == LINE_PRE_END)
254 break;
255 l->flags ^= L_HIDDEN;
256 if (l->flags & L_HIDDEN)
257 buffer->line_max--;
258 else
259 buffer->line_max++;
261 break;
262 default:
263 break;
267 void
268 cmd_push_button_new_tab(struct buffer *buffer)
270 struct vline *vl;
272 vl = buffer->current_line;
273 if (vl->parent->type != LINE_LINK)
274 return;
276 new_tab(vl->parent->alt);
279 void
280 cmd_previous_button(struct buffer *buffer)
282 struct excursion place;
284 save_excursion(&place, buffer);
286 do {
287 if (!forward_line(buffer, -1)) {
288 restore_excursion(&place, buffer);
289 message("No previous link");
290 return;
292 } while (buffer->current_line->parent->type != LINE_LINK);
295 void
296 cmd_next_button(struct buffer *buffer)
298 struct excursion place;
300 save_excursion(&place, buffer);
302 do {
303 if (!forward_line(buffer, +1)){
304 restore_excursion(&place, buffer);
305 message("No next link");
306 return;
308 } while (buffer->current_line->parent->type != LINE_LINK);
311 static inline int
312 is_heading(const struct line *l)
314 return l->type == LINE_TITLE_1 ||
315 l->type == LINE_TITLE_2 ||
316 l->type == LINE_TITLE_3;
319 void
320 cmd_previous_heading(struct buffer *buffer)
322 struct excursion place;
324 save_excursion(&place, buffer);
326 do {
327 if (!forward_line(buffer, -1)) {
328 restore_excursion(&place, buffer);
329 message("No previous heading");
330 return;
332 } while (!is_heading(buffer->current_line->parent));
335 void
336 cmd_next_heading(struct buffer *buffer)
338 struct excursion place;
340 save_excursion(&place, buffer);
342 do {
343 if (!forward_line(buffer, +1)) {
344 restore_excursion(&place, buffer);
345 message("No next heading");
346 return;
348 } while (!is_heading(buffer->current_line->parent));
351 void
352 cmd_previous_page(struct buffer *buffer)
354 if (!load_previous_page(current_tab))
355 message("No previous page");
356 else
357 start_loading_anim(current_tab);
360 void
361 cmd_next_page(struct buffer *buffer)
363 if (!load_next_page(current_tab))
364 message("No next page");
365 else
366 start_loading_anim(current_tab);
369 void
370 cmd_clear_minibuf(struct buffer *buffer)
372 message(NULL);
375 void
376 cmd_execute_extended_command(struct buffer *buffer)
378 size_t len;
380 GUARD_RECURSIVE_MINIBUFFER();
382 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
383 &eecmd_history, compl_eecmd, NULL);
385 len = sizeof(ministate.prompt);
386 strlcpy(ministate.prompt, "", len);
388 if (thiskey.meta)
389 strlcat(ministate.prompt, "M-", len);
391 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
393 if (thiskey.meta)
394 strlcat(ministate.prompt, " ", len);
397 void
398 cmd_tab_close(struct buffer *buffer)
400 struct tab *tab, *t;
402 tab = current_tab;
403 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
404 TAILQ_NEXT(tab, tabs) == NULL) {
405 message("Can't close the only tab.");
406 return;
409 if (evtimer_pending(&tab->loadingev, NULL))
410 evtimer_del(&tab->loadingev);
412 stop_tab(tab);
414 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
415 t = TAILQ_NEXT(tab, tabs);
416 TAILQ_REMOVE(&tabshead, tab, tabs);
417 free(tab);
419 switch_to_tab(t);
422 void
423 cmd_tab_close_other(struct buffer *buffer)
425 struct tab *t, *i;
427 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
428 if (t == current_tab)
429 continue;
431 stop_tab(t);
432 TAILQ_REMOVE(&tabshead, t, tabs);
433 free(t);
437 void
438 cmd_tab_new(struct buffer *buffer)
440 const char *url;
442 if ((url = new_tab_url) == NULL)
443 url = NEW_TAB_URL;
445 new_tab(url);
448 void
449 cmd_tab_next(struct buffer *buffer)
451 struct tab *t;
453 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
454 t = TAILQ_FIRST(&tabshead);
455 switch_to_tab(t);
458 void
459 cmd_tab_previous(struct buffer *buffer)
461 struct tab *t;
463 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
464 t = TAILQ_LAST(&tabshead, tabshead);
465 switch_to_tab(t);
468 void
469 cmd_tab_move(struct buffer *buffer)
471 struct tab *t;
473 t = TAILQ_NEXT(current_tab, tabs);
474 TAILQ_REMOVE(&tabshead, current_tab, tabs);
476 if (t == NULL)
477 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
478 else
479 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
482 void
483 cmd_tab_move_to(struct buffer *buffer)
485 struct tab *t;
487 t = TAILQ_PREV(current_tab, tabshead, tabs);
488 TAILQ_REMOVE(&tabshead, current_tab, tabs);
490 if (t == NULL) {
491 if (TAILQ_EMPTY(&tabshead))
492 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
493 else
494 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
495 } else
496 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
499 void
500 cmd_tab_select(struct buffer *buffer)
502 GUARD_RECURSIVE_MINIBUFFER();
504 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
505 NULL, compl_ts, NULL);
506 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
509 void
510 cmd_load_url(struct buffer *buffer)
512 GUARD_RECURSIVE_MINIBUFFER();
514 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
515 &lu_history, NULL, NULL);
516 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
517 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
518 cmd_move_end_of_line(&ministate.buffer);
521 void
522 cmd_load_current_url(struct buffer *buffer)
524 GUARD_RECURSIVE_MINIBUFFER();
526 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
527 &lu_history, NULL, NULL);
528 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
529 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
530 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
533 void
534 cmd_reload_page(struct buffer *buffer)
536 load_url_in_tab(current_tab, current_tab->hist_cur->h);
539 void
540 cmd_bookmark_page(struct buffer *buffer)
542 GUARD_RECURSIVE_MINIBUFFER();
544 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
545 NULL, NULL);
546 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
547 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
548 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
551 void
552 cmd_list_bookmarks(struct buffer *buffer)
554 load_url_in_tab(current_tab, "about:bookmarks");
557 void
558 cmd_toggle_help(struct buffer *buffer)
560 ui_toggle_side_window();
563 void
564 cmd_link_select(struct buffer *buffer)
566 struct line *l;
568 GUARD_RECURSIVE_MINIBUFFER();
570 l = TAILQ_FIRST(&buffer->page.head);
571 while (l != NULL && l->type != LINE_LINK)
572 l = TAILQ_NEXT(l, lines);
574 if (l == NULL) {
575 message("No links found");
576 return;
579 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
580 NULL, compl_ls, l);
581 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
584 void
585 cmd_swiper(struct buffer *buffer)
587 GUARD_RECURSIVE_MINIBUFFER();
589 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
590 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
591 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
594 void
595 cmd_toc(struct buffer *buffer)
597 struct line *l;
599 GUARD_RECURSIVE_MINIBUFFER();
601 l = TAILQ_FIRST(&buffer->page.head);
602 while (l != NULL &&
603 l->type != LINE_TITLE_1 &&
604 l->type != LINE_TITLE_2 &&
605 l->type != LINE_TITLE_3)
606 l = TAILQ_NEXT(l, lines);
608 if (l == NULL) {
609 message("No headings found");
610 return;
613 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
614 NULL, compl_toc, l);
615 strlcpy(ministate.prompt, "Select heading: ",
616 sizeof(ministate.prompt));
619 void
620 cmd_inc_fill_column(struct buffer *buffer)
622 if (fill_column == INT_MAX)
623 return;
625 fill_column += 2;
626 message("fill-column: %d", fill_column);
628 ui_schedule_redraw();
631 void
632 cmd_dec_fill_column(struct buffer *buffer)
634 if (fill_column == INT_MAX || fill_column < 8)
635 return;
637 fill_column -= 2;
638 message("fill-column: %d", fill_column);
640 ui_schedule_redraw();
643 void
644 cmd_olivetti_mode(struct buffer *buffer)
646 olivetti_mode = !olivetti_mode;
647 if (olivetti_mode)
648 message("olivetti-mode enabled");
649 else
650 message("olivetti-mode disabled");
652 ui_schedule_redraw();
655 void
656 cmd_mini_delete_char(struct buffer *buffer)
658 char *c, *n;
660 if (!in_minibuffer) {
661 message("text is read-only");
662 return;
665 minibuffer_taint_hist();
667 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
668 if (*c == '\0')
669 return;
670 n = utf8_next_cp(c);
672 memmove(c, n, strlen(n)+1);
674 recompute_completions(0);
677 void
678 cmd_mini_delete_backward_char(struct buffer *buffer)
680 char *c, *p, *start;
682 if (!in_minibuffer) {
683 message("text is read-only");
684 return;
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 if (!in_minibuffer) {
707 message("text is read-only");
708 return;
711 minibuffer_taint_hist();
712 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
713 *c = '\0';
715 recompute_completions(0);
718 void
719 cmd_mini_abort(struct buffer *buffer)
721 if (!in_minibuffer)
722 return;
724 ministate.abortfn();
727 void
728 cmd_mini_complete_and_exit(struct buffer *buffer)
730 if (!in_minibuffer)
731 return;
733 minibuffer_taint_hist();
734 ministate.donefn();
737 void
738 cmd_mini_previous_history_element(struct buffer *buffer)
740 if (ministate.history == NULL) {
741 message("No history");
742 return;
745 if (ministate.hist_cur == NULL ||
746 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
747 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
748 ministate.hist_off = ministate.history->len - 1;
749 if (ministate.hist_cur == NULL)
750 message("No prev history item");
751 } else {
752 ministate.hist_off--;
755 if (ministate.hist_cur != NULL)
756 buffer->current_line->line = ministate.hist_cur->h;
759 void
760 cmd_mini_next_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_NEXT(ministate.hist_cur, entries)) == NULL) {
769 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
770 ministate.hist_off = 0;
771 if (ministate.hist_cur == NULL)
772 message("No next 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_previous_completion(struct buffer *buffer)
784 if (in_minibuffer != MB_COMPREAD)
785 return;
787 buffer = &ministate.compl.buffer;
789 if (buffer->current_line != NULL)
790 buffer->current_line->parent->type = LINE_COMPL;
792 forward_line(buffer, -1);
794 if (buffer->current_line != NULL)
795 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
798 void
799 cmd_next_completion(struct buffer *buffer)
801 if (in_minibuffer != MB_COMPREAD)
802 return;
804 buffer = &ministate.compl.buffer;
806 if (buffer->current_line != NULL)
807 buffer->current_line->parent->type = LINE_COMPL;
809 forward_line(buffer, +1);
811 if (buffer->current_line != NULL)
812 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
815 void
816 cmd_insert_current_candidate(struct buffer *buffer)
818 struct vline *vl;
820 if (in_minibuffer != MB_COMPREAD)
821 return;
823 buffer = &ministate.compl.buffer;
824 if ((vl = buffer->current_line) == NULL)
825 return;
827 minibuffer_taint_hist();
828 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
829 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
832 void
833 cmd_suspend_telescope(struct buffer *buffer)
835 message("Zzz...");
836 ui_suspend();
839 void
840 cmd_toggle_pre_wrap(struct buffer *buffer)
842 dont_wrap_pre = !dont_wrap_pre;
844 if (dont_wrap_pre)
845 message("Don't wrap preformatted blocks");
846 else
847 message("Wrap preformatted blocks");
849 ui_schedule_redraw();
852 void
853 cmd_mini_goto_beginning(struct buffer *buffer)
855 struct vline *vl;
857 if (!in_minibuffer)
858 return;
860 buffer = &ministate.compl.buffer;
862 if ((vl = buffer->current_line) != NULL)
863 vl->parent->type = LINE_COMPL;
865 vl = TAILQ_FIRST(&buffer->head);
866 while (vl != NULL && vl->parent->flags & L_HIDDEN)
867 vl = TAILQ_NEXT(vl, vlines);
869 if (vl == NULL)
870 return;
872 vl->parent->type = LINE_COMPL_CURRENT;
873 buffer->top_line = vl;
874 buffer->current_line = vl;
877 void
878 cmd_mini_goto_end(struct buffer *buffer)
880 struct vline *vl;
882 if (!in_minibuffer)
883 return;
885 buffer = &ministate.compl.buffer;
887 if ((vl = buffer->current_line) != NULL)
888 vl->parent->type = LINE_COMPL;
890 vl = TAILQ_LAST(&buffer->head, vhead);
891 while (vl != NULL && vl->parent->flags & L_HIDDEN)
892 vl = TAILQ_PREV(vl, vhead, vlines);
894 if (vl == NULL)
895 return;
897 vl->parent->type = LINE_COMPL_CURRENT;
898 buffer->current_line = vl;