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 "telescope.h"
27 #include "ui.h"
28 #include "utf8.h"
30 #define GUARD_RECURSIVE_MINIBUFFER() \
31 do { \
32 if (in_minibuffer) { \
33 message("enable-recursive-minibuffers " \
34 "is not yet available."); \
35 return; \
36 } \
37 } while(0)
39 #define GUARD_READ_ONLY() \
40 do { \
41 if (!in_minibuffer) { \
42 message("text is read-only"); \
43 return; \
44 } \
45 } while(0)
47 /* return 1 if moved, 0 otherwise */
48 static inline int
49 forward_line(struct buffer *buffer, int n)
50 {
51 struct vline *vl;
52 int did;
54 if (buffer->current_line == NULL)
55 return 0;
56 vl = buffer->current_line;
58 did = 0;
59 while (n != 0) {
60 if (n > 0) {
61 vl = TAILQ_NEXT(vl, vlines);
62 if (vl == NULL)
63 return did;
64 if (vl->parent->flags & L_HIDDEN)
65 continue;
66 buffer->current_line = vl;
67 n--;
68 } else {
69 vl = TAILQ_PREV(vl, vhead, vlines);
70 if (vl == NULL)
71 return did;
72 if (vl->parent->flags & L_HIDDEN)
73 continue;
74 if (buffer->current_line == buffer->top_line) {
75 buffer->line_off--;
76 buffer->top_line = vl;
77 }
78 buffer->current_line = vl;
79 n++;
80 }
82 did = 1;
83 }
85 return did;
86 }
88 void
89 cmd_previous_line(struct buffer *buffer)
90 {
91 forward_line(buffer, -1);
92 }
94 void
95 cmd_next_line(struct buffer *buffer)
96 {
97 forward_line(buffer, +1);
98 }
100 void
101 cmd_backward_char(struct buffer *buffer)
103 if (buffer->cpoff != 0)
104 buffer->cpoff--;
107 void
108 cmd_forward_char(struct buffer *buffer)
110 size_t len = 0;
112 if (buffer->current_line == NULL)
113 return;
115 if (buffer->current_line->line != NULL)
116 len = utf8_cplen(buffer->current_line->line);
117 if (++buffer->cpoff > len)
118 buffer->cpoff = len;
121 void
122 cmd_backward_paragraph(struct buffer *buffer)
124 do {
125 if (!forward_line(buffer, -1)) {
126 message("No previous paragraph");
127 return;
129 } while (buffer->current_line->line != NULL ||
130 buffer->current_line->parent->type != LINE_TEXT);
133 void
134 cmd_forward_paragraph(struct buffer *buffer)
136 do {
137 if (!forward_line(buffer, +1)) {
138 message("No next paragraph");
139 return;
141 } while (buffer->current_line->line != NULL ||
142 buffer->current_line->parent->type != LINE_TEXT);
145 void
146 cmd_move_beginning_of_line(struct buffer *buffer)
148 buffer->cpoff = 0;
151 void
152 cmd_move_end_of_line(struct buffer *buffer)
154 struct vline *vl;
156 vl = buffer->current_line;
157 if (vl == NULL || vl->line == NULL)
158 return;
159 buffer->cpoff = utf8_cplen(vl->line);
162 void
163 cmd_redraw(struct buffer *buffer)
165 ui_schedule_redraw();
168 void
169 cmd_scroll_line_up(struct buffer *buffer)
171 struct vline *vl;
173 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
174 == NULL)
175 return;
177 forward_line(buffer, -1);
178 buffer->line_off--;
179 buffer->top_line = vl;
182 void
183 cmd_scroll_line_down(struct buffer *buffer)
185 if (!forward_line(buffer, +1))
186 return;
188 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
189 buffer->line_off++;
192 void
193 cmd_scroll_up(struct buffer *buffer)
195 struct vline *vl;
196 int i;
198 for (i = 0; i < body_lines; ++i) {
199 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
200 if (vl == NULL)
201 break;
202 buffer->line_off--;
203 buffer->top_line = vl;
204 forward_line(buffer, -1);
208 void
209 cmd_scroll_down(struct buffer *buffer)
211 int i;
213 for (i = 0; i < body_lines; ++i) {
214 if (!forward_line(buffer, +1))
215 break;
217 buffer->top_line = TAILQ_NEXT(buffer->top_line,
218 vlines);
219 buffer->line_off++;
223 void
224 cmd_beginning_of_buffer(struct buffer *buffer)
226 buffer->current_line = TAILQ_FIRST(&buffer->head);
227 buffer->cpoff = 0;
228 buffer->top_line = buffer->current_line;
229 buffer->line_off = 0;
232 void
233 cmd_end_of_buffer(struct buffer *buffer)
235 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
237 if (buffer->current_line == NULL)
238 return;
240 /* deal with invisible lines */
241 if (buffer->current_line->parent->flags & L_HIDDEN)
242 forward_line(buffer, -1);
244 cmd_move_end_of_line(buffer);
247 void
248 cmd_kill_telescope(struct buffer *buffer)
250 save_session();
251 event_loopbreak();
254 void
255 cmd_push_button(struct buffer *buffer)
257 struct vline *vl;
258 struct line *l;
260 vl = buffer->current_line;
262 if (vl == NULL)
263 return;
265 switch (vl->parent->type) {
266 case LINE_LINK:
267 load_url_in_tab(current_tab, vl->parent->alt, NULL);
268 break;
269 case LINE_PRE_START:
270 l = TAILQ_NEXT(vl->parent, lines);
271 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
272 if (l->type == LINE_PRE_END)
273 break;
274 l->flags ^= L_HIDDEN;
275 if (l->flags & L_HIDDEN)
276 buffer->line_max--;
277 else
278 buffer->line_max++;
280 break;
281 default:
282 break;
286 void
287 cmd_push_button_new_tab(struct buffer *buffer)
289 struct vline *vl;
291 vl = buffer->current_line;
292 if (vl == NULL || vl->parent->type != LINE_LINK)
293 return;
295 new_tab(vl->parent->alt, current_tab->hist_cur->h);
298 void
299 cmd_previous_button(struct buffer *buffer)
301 struct excursion place;
303 save_excursion(&place, buffer);
305 do {
306 if (!forward_line(buffer, -1)) {
307 restore_excursion(&place, buffer);
308 message("No previous link");
309 return;
311 } while (buffer->current_line->parent->type != LINE_LINK);
314 void
315 cmd_next_button(struct buffer *buffer)
317 struct excursion place;
319 save_excursion(&place, buffer);
321 do {
322 if (!forward_line(buffer, +1)){
323 restore_excursion(&place, buffer);
324 message("No next link");
325 return;
327 } while (buffer->current_line->parent->type != LINE_LINK);
330 static inline int
331 is_heading(const struct line *l)
333 return l->type == LINE_TITLE_1 ||
334 l->type == LINE_TITLE_2 ||
335 l->type == LINE_TITLE_3;
338 void
339 cmd_previous_heading(struct buffer *buffer)
341 struct excursion place;
343 save_excursion(&place, buffer);
345 do {
346 if (!forward_line(buffer, -1)) {
347 restore_excursion(&place, buffer);
348 message("No previous heading");
349 return;
351 } while (!is_heading(buffer->current_line->parent));
354 void
355 cmd_next_heading(struct buffer *buffer)
357 struct excursion place;
359 save_excursion(&place, buffer);
361 do {
362 if (!forward_line(buffer, +1)) {
363 restore_excursion(&place, buffer);
364 message("No next heading");
365 return;
367 } while (!is_heading(buffer->current_line->parent));
370 void
371 cmd_previous_page(struct buffer *buffer)
373 if (!load_previous_page(current_tab))
374 message("No previous page");
375 else
376 start_loading_anim(current_tab);
379 void
380 cmd_next_page(struct buffer *buffer)
382 if (!load_next_page(current_tab))
383 message("No next page");
384 else
385 start_loading_anim(current_tab);
388 void
389 cmd_clear_minibuf(struct buffer *buffer)
391 message(NULL);
394 void
395 cmd_execute_extended_command(struct buffer *buffer)
397 size_t len;
399 GUARD_RECURSIVE_MINIBUFFER();
401 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
402 &eecmd_history, compl_eecmd, NULL);
404 len = sizeof(ministate.prompt);
405 strlcpy(ministate.prompt, "", len);
407 if (thiskey.meta)
408 strlcat(ministate.prompt, "M-", len);
410 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
412 if (thiskey.meta)
413 strlcat(ministate.prompt, " ", len);
416 void
417 cmd_tab_close(struct buffer *buffer)
419 struct tab *tab, *t;
421 tab = current_tab;
423 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
424 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
425 switch_to_tab(t);
426 free_tab(tab);
427 } else
428 message("Can't close the only tab.");
432 void
433 cmd_tab_close_other(struct buffer *buffer)
435 struct tab *t, *i;
437 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
438 if (t == current_tab)
439 continue;
441 free_tab(t);
445 void
446 cmd_tab_new(struct buffer *buffer)
448 const char *url;
450 if ((url = new_tab_url) == NULL)
451 url = NEW_TAB_URL;
453 new_tab(url, NULL);
456 void
457 cmd_tab_next(struct buffer *buffer)
459 struct tab *t;
461 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
462 t = TAILQ_FIRST(&tabshead);
463 switch_to_tab(t);
466 void
467 cmd_tab_previous(struct buffer *buffer)
469 struct tab *t;
471 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
472 t = TAILQ_LAST(&tabshead, tabshead);
473 switch_to_tab(t);
476 void
477 cmd_tab_move(struct buffer *buffer)
479 struct tab *t;
481 t = TAILQ_NEXT(current_tab, tabs);
482 TAILQ_REMOVE(&tabshead, current_tab, tabs);
484 if (t == NULL)
485 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
486 else
487 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
490 void
491 cmd_tab_move_to(struct buffer *buffer)
493 struct tab *t;
495 t = TAILQ_PREV(current_tab, tabshead, tabs);
496 TAILQ_REMOVE(&tabshead, current_tab, tabs);
498 if (t == NULL) {
499 if (TAILQ_EMPTY(&tabshead))
500 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
501 else
502 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
503 } else
504 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
507 void
508 cmd_tab_select(struct buffer *buffer)
510 GUARD_RECURSIVE_MINIBUFFER();
512 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
513 NULL, compl_ts, NULL);
514 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
517 void
518 cmd_load_url(struct buffer *buffer)
520 GUARD_RECURSIVE_MINIBUFFER();
522 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
523 &lu_history, NULL, NULL);
524 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
525 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
526 cmd_move_end_of_line(&ministate.buffer);
529 void
530 cmd_load_current_url(struct buffer *buffer)
532 GUARD_RECURSIVE_MINIBUFFER();
534 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
535 &lu_history, NULL, NULL);
536 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
537 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
538 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
541 void
542 cmd_reload_page(struct buffer *buffer)
544 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL);
547 void
548 cmd_bookmark_page(struct buffer *buffer)
550 GUARD_RECURSIVE_MINIBUFFER();
552 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
553 NULL, NULL);
554 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
555 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
556 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
559 void
560 cmd_list_bookmarks(struct buffer *buffer)
562 load_url_in_tab(current_tab, "about:bookmarks", NULL);
565 void
566 cmd_toggle_help(struct buffer *buffer)
568 ui_toggle_side_window();
571 void
572 cmd_link_select(struct buffer *buffer)
574 struct line *l;
576 GUARD_RECURSIVE_MINIBUFFER();
578 l = TAILQ_FIRST(&buffer->page.head);
579 while (l != NULL && l->type != LINE_LINK)
580 l = TAILQ_NEXT(l, lines);
582 if (l == NULL) {
583 message("No links found");
584 return;
587 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
588 NULL, compl_ls, l);
589 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
592 void
593 cmd_swiper(struct buffer *buffer)
595 GUARD_RECURSIVE_MINIBUFFER();
597 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
598 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
599 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
602 void
603 cmd_toc(struct buffer *buffer)
605 struct line *l;
607 GUARD_RECURSIVE_MINIBUFFER();
609 l = TAILQ_FIRST(&buffer->page.head);
610 while (l != NULL &&
611 l->type != LINE_TITLE_1 &&
612 l->type != LINE_TITLE_2 &&
613 l->type != LINE_TITLE_3)
614 l = TAILQ_NEXT(l, lines);
616 if (l == NULL) {
617 message("No headings found");
618 return;
621 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
622 NULL, compl_toc, l);
623 strlcpy(ministate.prompt, "Select heading: ",
624 sizeof(ministate.prompt));
627 void
628 cmd_inc_fill_column(struct buffer *buffer)
630 if (fill_column == INT_MAX)
631 return;
633 fill_column += 2;
634 message("fill-column: %d", fill_column);
636 ui_schedule_redraw();
639 void
640 cmd_dec_fill_column(struct buffer *buffer)
642 if (fill_column == INT_MAX || fill_column < 8)
643 return;
645 fill_column -= 2;
646 message("fill-column: %d", fill_column);
648 ui_schedule_redraw();
651 void
652 cmd_olivetti_mode(struct buffer *buffer)
654 olivetti_mode = !olivetti_mode;
655 if (olivetti_mode)
656 message("olivetti-mode enabled");
657 else
658 message("olivetti-mode disabled");
660 ui_schedule_redraw();
663 void
664 cmd_mini_delete_char(struct buffer *buffer)
666 char *c, *n;
668 GUARD_READ_ONLY();
670 minibuffer_taint_hist();
672 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
673 if (*c == '\0')
674 return;
675 n = utf8_next_cp(c);
677 memmove(c, n, strlen(n)+1);
679 recompute_completions(0);
682 void
683 cmd_mini_delete_backward_char(struct buffer *buffer)
685 char *c, *p, *start;
687 GUARD_READ_ONLY();
689 minibuffer_taint_hist();
691 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
692 start = buffer->current_line->line;
693 if (c == start)
694 return;
695 p = utf8_prev_cp(c-1, start);
697 memmove(p, c, strlen(c)+1);
698 buffer->cpoff--;
700 recompute_completions(0);
703 void
704 cmd_mini_kill_line(struct buffer *buffer)
706 char *c;
708 GUARD_READ_ONLY();
710 minibuffer_taint_hist();
711 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
712 *c = '\0';
714 recompute_completions(0);
717 void
718 cmd_mini_abort(struct buffer *buffer)
720 if (!in_minibuffer)
721 return;
723 ministate.abortfn();
726 void
727 cmd_mini_complete_and_exit(struct buffer *buffer)
729 if (!in_minibuffer)
730 return;
732 minibuffer_taint_hist();
733 ministate.donefn();
736 void
737 cmd_mini_previous_history_element(struct buffer *buffer)
739 if (ministate.history == NULL) {
740 message("No history");
741 return;
744 if (ministate.hist_cur == NULL ||
745 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
746 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
747 ministate.hist_off = ministate.history->len - 1;
748 if (ministate.hist_cur == NULL)
749 message("No prev history item");
750 } else {
751 ministate.hist_off--;
754 if (ministate.hist_cur != NULL)
755 buffer->current_line->line = ministate.hist_cur->h;
758 void
759 cmd_mini_next_history_element(struct buffer *buffer)
761 if (ministate.history == NULL) {
762 message("No history");
763 return;
766 if (ministate.hist_cur == NULL ||
767 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
768 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
769 ministate.hist_off = 0;
770 if (ministate.hist_cur == NULL)
771 message("No next history item");
772 } else {
773 ministate.hist_off++;
776 if (ministate.hist_cur != NULL)
777 buffer->current_line->line = ministate.hist_cur->h;
780 void
781 cmd_previous_completion(struct buffer *buffer)
783 if (in_minibuffer != MB_COMPREAD)
784 return;
786 buffer = &ministate.compl.buffer;
788 if (buffer->current_line != NULL)
789 buffer->current_line->parent->type = LINE_COMPL;
791 forward_line(buffer, -1);
793 if (buffer->current_line != NULL)
794 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
797 void
798 cmd_next_completion(struct buffer *buffer)
800 if (in_minibuffer != MB_COMPREAD)
801 return;
803 buffer = &ministate.compl.buffer;
805 if (buffer->current_line != NULL)
806 buffer->current_line->parent->type = LINE_COMPL;
808 forward_line(buffer, +1);
810 if (buffer->current_line != NULL)
811 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
814 void
815 cmd_insert_current_candidate(struct buffer *buffer)
817 struct vline *vl;
819 if (in_minibuffer != MB_COMPREAD)
820 return;
822 buffer = &ministate.compl.buffer;
823 if ((vl = buffer->current_line) == NULL)
824 return;
826 minibuffer_taint_hist();
827 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
828 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
831 void
832 cmd_suspend_telescope(struct buffer *buffer)
834 message("Zzz...");
835 ui_suspend();
838 void
839 cmd_toggle_pre_wrap(struct buffer *buffer)
841 dont_wrap_pre = !dont_wrap_pre;
843 if (dont_wrap_pre)
844 message("Don't wrap preformatted blocks");
845 else
846 message("Wrap preformatted blocks");
848 ui_schedule_redraw();
851 void
852 cmd_mini_goto_beginning(struct buffer *buffer)
854 struct vline *vl;
856 if (!in_minibuffer)
857 return;
859 buffer = &ministate.compl.buffer;
861 if ((vl = buffer->current_line) != NULL)
862 vl->parent->type = LINE_COMPL;
864 vl = TAILQ_FIRST(&buffer->head);
865 while (vl != NULL && vl->parent->flags & L_HIDDEN)
866 vl = TAILQ_NEXT(vl, vlines);
868 if (vl == NULL)
869 return;
871 vl->parent->type = LINE_COMPL_CURRENT;
872 buffer->top_line = vl;
873 buffer->current_line = vl;
876 void
877 cmd_mini_goto_end(struct buffer *buffer)
879 struct vline *vl;
881 if (!in_minibuffer)
882 return;
884 buffer = &ministate.compl.buffer;
886 if ((vl = buffer->current_line) != NULL)
887 vl->parent->type = LINE_COMPL;
889 vl = TAILQ_LAST(&buffer->head, vhead);
890 while (vl != NULL && vl->parent->flags & L_HIDDEN)
891 vl = TAILQ_PREV(vl, vhead, vlines);
893 if (vl == NULL)
894 return;
896 vl->parent->type = LINE_COMPL_CURRENT;
897 buffer->current_line = vl;
900 void
901 cmd_other_window(struct buffer *buffer)
903 ui_other_window();