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 forward_line(buffer, -1*body_lines);
185 void
186 cmd_scroll_down(struct buffer *buffer)
188 forward_line(buffer, body_lines);
191 void
192 cmd_beginning_of_buffer(struct buffer *buffer)
194 buffer->current_line = TAILQ_FIRST(&buffer->head);
195 buffer->cpoff = 0;
196 buffer->top_line = buffer->current_line;
197 buffer->line_off = 0;
200 void
201 cmd_end_of_buffer(struct buffer *buffer)
203 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
204 buffer->cpoff = body_cols;
207 void
208 cmd_kill_telescope(struct buffer *buffer)
210 save_session();
211 event_loopbreak();
214 void
215 cmd_push_button(struct buffer *buffer)
217 struct vline *vl;
218 struct line *l;
220 vl = buffer->current_line;
221 switch (vl->parent->type) {
222 case LINE_LINK:
223 load_url_in_tab(current_tab(), vl->parent->meta.alt);
224 break;
225 case LINE_PRE_START:
226 l = TAILQ_NEXT(vl->parent, lines);
227 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
228 if (l->type == LINE_PRE_END)
229 break;
230 l->flags ^= L_HIDDEN;
231 if (l->flags & L_HIDDEN)
232 buffer->line_max--;
233 else
234 buffer->line_max++;
236 break;
237 default:
238 break;
242 void
243 cmd_push_button_new_tab(struct buffer *buffer)
245 struct vline *vl;
247 vl = buffer->current_line;
248 if (vl->parent->type != LINE_LINK)
249 return;
251 new_tab(vl->parent->meta.alt);
254 void
255 cmd_previous_button(struct buffer *buffer)
257 struct excursion place;
259 save_excursion(&place, buffer);
261 do {
262 if (!forward_line(buffer, -1)) {
263 restore_excursion(&place, buffer);
264 message("No previous link");
265 return;
267 } while (buffer->current_line->parent->type != LINE_LINK);
270 void
271 cmd_next_button(struct buffer *buffer)
273 struct excursion place;
275 save_excursion(&place, buffer);
277 do {
278 if (!forward_line(buffer, +1)){
279 restore_excursion(&place, buffer);
280 message("No next link");
281 return;
283 } while (buffer->current_line->parent->type != LINE_LINK);
286 static inline int
287 is_heading(const struct line *l)
289 return l->type == LINE_TITLE_1 ||
290 l->type == LINE_TITLE_2 ||
291 l->type == LINE_TITLE_3;
294 void
295 cmd_previous_heading(struct buffer *buffer)
297 struct excursion place;
299 save_excursion(&place, buffer);
301 do {
302 if (!forward_line(buffer, -1)) {
303 restore_excursion(&place, buffer);
304 message("No previous heading");
305 return;
307 } while (!is_heading(buffer->current_line->parent));
310 void
311 cmd_next_heading(struct buffer *buffer)
313 struct excursion place;
315 save_excursion(&place, buffer);
317 do {
318 if (!forward_line(buffer, +1)) {
319 restore_excursion(&place, buffer);
320 message("No next heading");
321 return;
323 } while (!is_heading(buffer->current_line->parent));
326 void
327 cmd_previous_page(struct buffer *buffer)
329 struct tab *tab = current_tab();
331 if (!load_previous_page(tab))
332 message("No previous page");
333 else
334 start_loading_anim(tab);
337 void
338 cmd_next_page(struct buffer *buffer)
340 struct tab *tab = current_tab();
342 if (!load_next_page(tab))
343 message("No next page");
344 else
345 start_loading_anim(tab);
348 void
349 cmd_clear_minibuf(struct buffer *buffer)
351 message(NULL);
354 void
355 cmd_execute_extended_command(struct buffer *buffer)
357 size_t len;
359 GUARD_RECURSIVE_MINIBUFFER();
361 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
362 &eecmd_history, compl_eecmd, NULL);
364 len = sizeof(ministate.prompt);
365 strlcpy(ministate.prompt, "", len);
367 if (thiskey.meta)
368 strlcat(ministate.prompt, "M-", len);
370 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
372 if (thiskey.meta)
373 strlcat(ministate.prompt, " ", len);
376 void
377 cmd_tab_close(struct buffer *buffer)
379 struct tab *tab, *t;
381 tab = current_tab();
382 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
383 TAILQ_NEXT(tab, tabs) == NULL) {
384 message("Can't close the only tab.");
385 return;
388 if (evtimer_pending(&tab->loadingev, NULL))
389 evtimer_del(&tab->loadingev);
391 stop_tab(tab);
393 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
394 t = TAILQ_NEXT(tab, tabs);
395 TAILQ_REMOVE(&tabshead, tab, tabs);
396 free(tab);
398 switch_to_tab(t);
401 void
402 cmd_tab_close_other(struct buffer *buffer)
404 struct tab *t, *i;
406 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
407 if (t->flags & TAB_CURRENT)
408 continue;
410 stop_tab(t);
411 TAILQ_REMOVE(&tabshead, t, tabs);
412 free(t);
416 void
417 cmd_tab_new(struct buffer *buffer)
419 const char *url;
421 if ((url = new_tab_url) == NULL)
422 url = NEW_TAB_URL;
424 new_tab(url);
427 void
428 cmd_tab_next(struct buffer *buffer)
430 struct tab *tab, *t;
432 tab = current_tab();
433 tab->flags &= ~TAB_CURRENT;
435 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
436 t = TAILQ_FIRST(&tabshead);
437 t->flags |= TAB_CURRENT;
438 t->flags &= ~TAB_URGENT;
441 void
442 cmd_tab_previous(struct buffer *buffer)
444 struct tab *tab, *t;
446 tab = current_tab();
447 tab->flags &= ~TAB_CURRENT;
449 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
450 t = TAILQ_LAST(&tabshead, tabshead);
451 t->flags |= TAB_CURRENT;
452 t->flags &= ~TAB_URGENT;
455 void
456 cmd_tab_move(struct buffer *buffer)
458 struct tab *tab, *t;
460 tab = current_tab();
461 t = TAILQ_NEXT(tab, tabs);
462 TAILQ_REMOVE(&tabshead, tab, tabs);
464 if (t == NULL)
465 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
466 else
467 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
470 void
471 cmd_tab_move_to(struct buffer *buffer)
473 struct tab *tab, *t;
475 tab = current_tab();
476 t = TAILQ_PREV(tab, tabshead, tabs);
477 TAILQ_REMOVE(&tabshead, tab, tabs);
479 if (t == NULL) {
480 if (TAILQ_EMPTY(&tabshead))
481 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
482 else
483 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
484 } else
485 TAILQ_INSERT_BEFORE(t, tab, tabs);
488 void
489 cmd_tab_select(struct buffer *buffer)
491 GUARD_RECURSIVE_MINIBUFFER();
493 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
494 NULL, compl_ts, NULL);
495 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
498 void
499 cmd_load_url(struct buffer *buffer)
501 GUARD_RECURSIVE_MINIBUFFER();
503 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
504 &lu_history, NULL, NULL);
505 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
506 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
507 cmd_move_end_of_line(&ministate.buffer);
510 void
511 cmd_load_current_url(struct buffer *buffer)
513 struct tab *tab = current_tab();
515 GUARD_RECURSIVE_MINIBUFFER();
517 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
518 &lu_history, NULL, NULL);
519 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
520 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
521 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
524 void
525 cmd_reload_page(struct buffer *buffer)
527 struct tab *tab;
529 tab = current_tab();
530 load_url_in_tab(tab, tab->hist_cur->h);
533 void
534 cmd_bookmark_page(struct buffer *buffer)
536 struct tab *tab = current_tab();
538 GUARD_RECURSIVE_MINIBUFFER();
540 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
541 NULL, NULL);
542 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
543 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
544 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
547 void
548 cmd_list_bookmarks(struct buffer *buffer)
550 load_url_in_tab(current_tab(), "about:bookmarks");
553 void
554 cmd_toggle_help(struct buffer *buffer)
556 ui_toggle_side_window();
559 void
560 cmd_link_select(struct buffer *buffer)
562 struct line *l;
564 GUARD_RECURSIVE_MINIBUFFER();
566 l = TAILQ_FIRST(&buffer->page.head);
567 while (l != NULL && l->type != LINE_LINK)
568 l = TAILQ_NEXT(l, lines);
570 if (l == NULL) {
571 message("No links found");
572 return;
575 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
576 NULL, compl_ls, l);
577 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
580 void
581 cmd_swiper(struct buffer *buffer)
583 GUARD_RECURSIVE_MINIBUFFER();
585 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
586 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
587 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
590 void
591 cmd_toc(struct buffer *buffer)
593 struct line *l;
595 GUARD_RECURSIVE_MINIBUFFER();
597 l = TAILQ_FIRST(&buffer->page.head);
598 while (l != NULL &&
599 l->type != LINE_TITLE_1 &&
600 l->type != LINE_TITLE_2 &&
601 l->type != LINE_TITLE_3)
602 l = TAILQ_NEXT(l, lines);
604 if (l == NULL) {
605 message("No headings found");
606 return;
609 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
610 NULL, compl_toc, l);
611 strlcpy(ministate.prompt, "Select heading: ",
612 sizeof(ministate.prompt));
615 void
616 cmd_inc_fill_column(struct buffer *buffer)
618 if (fill_column == INT_MAX)
619 return;
621 fill_column += 2;
622 message("fill-column: %d", fill_column);
624 ui_schedule_redraw();
627 void
628 cmd_dec_fill_column(struct buffer *buffer)
630 if (fill_column == INT_MAX || fill_column < 8)
631 return;
633 fill_column -= 2;
634 message("fill-column: %d", fill_column);
636 ui_schedule_redraw();
639 void
640 cmd_olivetti_mode(struct buffer *buffer)
642 olivetti_mode = !olivetti_mode;
643 if (olivetti_mode)
644 message("olivetti-mode enabled");
645 else
646 message("olivetti-mode disabled");
648 ui_schedule_redraw();
651 void
652 cmd_mini_delete_char(struct buffer *buffer)
654 char *c, *n;
656 if (!in_minibuffer) {
657 message("text is read-only");
658 return;
661 minibuffer_taint_hist();
663 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
664 if (*c == '\0')
665 return;
666 n = utf8_next_cp(c);
668 memmove(c, n, strlen(n)+1);
670 recompute_completions(0);
673 void
674 cmd_mini_delete_backward_char(struct buffer *buffer)
676 char *c, *p, *start;
678 if (!in_minibuffer) {
679 message("text is read-only");
680 return;
683 minibuffer_taint_hist();
685 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
686 start = buffer->current_line->line;
687 if (c == start)
688 return;
689 p = utf8_prev_cp(c-1, start);
691 memmove(p, c, strlen(c)+1);
692 buffer->cpoff--;
694 recompute_completions(0);
697 void
698 cmd_mini_kill_line(struct buffer *buffer)
700 char *c;
702 if (!in_minibuffer) {
703 message("text is read-only");
704 return;
707 minibuffer_taint_hist();
708 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
709 *c = '\0';
711 recompute_completions(0);
714 void
715 cmd_mini_abort(struct buffer *buffer)
717 if (!in_minibuffer)
718 return;
720 ministate.abortfn();
723 void
724 cmd_mini_complete_and_exit(struct buffer *buffer)
726 if (!in_minibuffer)
727 return;
729 minibuffer_taint_hist();
730 ministate.donefn();
733 void
734 cmd_mini_previous_history_element(struct buffer *buffer)
736 if (ministate.history == NULL) {
737 message("No history");
738 return;
741 if (ministate.hist_cur == NULL ||
742 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
743 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
744 ministate.hist_off = ministate.history->len - 1;
745 if (ministate.hist_cur == NULL)
746 message("No prev history item");
747 } else {
748 ministate.hist_off--;
751 if (ministate.hist_cur != NULL)
752 buffer->current_line->line = ministate.hist_cur->h;
755 void
756 cmd_mini_next_history_element(struct buffer *buffer)
758 if (ministate.history == NULL) {
759 message("No history");
760 return;
763 if (ministate.hist_cur == NULL ||
764 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
765 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
766 ministate.hist_off = 0;
767 if (ministate.hist_cur == NULL)
768 message("No next history item");
769 } else {
770 ministate.hist_off++;
773 if (ministate.hist_cur != NULL)
774 buffer->current_line->line = ministate.hist_cur->h;
777 void
778 cmd_previous_completion(struct buffer *buffer)
780 if (in_minibuffer != MB_COMPREAD)
781 return;
783 buffer = &ministate.compl.buffer;
785 if (buffer->current_line != NULL)
786 buffer->current_line->parent->type = LINE_COMPL;
788 forward_line(buffer, -1);
790 if (buffer->current_line != NULL)
791 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
794 void
795 cmd_next_completion(struct buffer *buffer)
797 if (in_minibuffer != MB_COMPREAD)
798 return;
800 buffer = &ministate.compl.buffer;
802 if (buffer->current_line != NULL)
803 buffer->current_line->parent->type = LINE_COMPL;
805 forward_line(buffer, +1);
807 if (buffer->current_line != NULL)
808 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
811 void
812 cmd_insert_current_candidate(struct buffer *buffer)
814 struct vline *vl;
816 if (in_minibuffer != MB_COMPREAD)
817 return;
819 buffer = &ministate.compl.buffer;
820 if ((vl = buffer->current_line) == NULL)
821 return;
823 minibuffer_taint_hist();
824 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
825 ministate.buffer.cpoff = utf8_cplen(ministate.buf);