2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
25 #include "minibuffer.h"
27 #include "telescope.h"
31 #define GUARD_RECURSIVE_MINIBUFFER() \
33 if (in_minibuffer) { \
34 message("enable-recursive-minibuffers " \
35 "is not yet available."); \
40 #define GUARD_READ_ONLY() \
42 if (!in_minibuffer) { \
43 message("text is read-only"); \
48 /* return 1 if moved, 0 otherwise */
50 forward_line(struct buffer *buffer, int n)
55 if (buffer->current_line == NULL)
57 vl = buffer->current_line;
62 vl = TAILQ_NEXT(vl, vlines);
65 if (vl->parent->flags & L_HIDDEN)
67 buffer->current_line = vl;
70 vl = TAILQ_PREV(vl, vhead, vlines);
73 if (vl->parent->flags & L_HIDDEN)
75 if (buffer->current_line == buffer->top_line) {
77 buffer->top_line = vl;
79 buffer->current_line = vl;
90 cmd_previous_line(struct buffer *buffer)
92 forward_line(buffer, -1);
96 cmd_next_line(struct buffer *buffer)
98 forward_line(buffer, +1);
102 cmd_backward_char(struct buffer *buffer)
104 if (buffer->cpoff != 0)
109 cmd_forward_char(struct buffer *buffer)
113 if (buffer->current_line == NULL)
116 if (buffer->current_line->line != NULL)
117 len = utf8_cplen(buffer->current_line->line);
118 if (++buffer->cpoff > len)
123 cmd_backward_paragraph(struct buffer *buffer)
126 if (!forward_line(buffer, -1)) {
127 message("No previous paragraph");
130 } while (buffer->current_line->line != NULL ||
131 buffer->current_line->parent->type != LINE_TEXT);
135 cmd_forward_paragraph(struct buffer *buffer)
138 if (!forward_line(buffer, +1)) {
139 message("No next paragraph");
142 } while (buffer->current_line->line != NULL ||
143 buffer->current_line->parent->type != LINE_TEXT);
147 cmd_move_beginning_of_line(struct buffer *buffer)
153 cmd_move_end_of_line(struct buffer *buffer)
157 vl = buffer->current_line;
158 if (vl == NULL || vl->line == NULL)
160 buffer->cpoff = utf8_cplen(vl->line);
164 cmd_redraw(struct buffer *buffer)
166 ui_schedule_redraw();
170 cmd_scroll_line_up(struct buffer *buffer)
175 if (buffer->top_line == NULL)
178 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
182 buffer->top_line = vl;
184 if (vl->parent->flags & L_HIDDEN)
192 forward_line(buffer, -1);
196 cmd_scroll_line_down(struct buffer *buffer)
198 if (!forward_line(buffer, +1))
202 if (buffer->top_line == NULL)
205 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
206 if (buffer->top_line->parent->flags & L_HIDDEN)
215 cmd_scroll_up(struct buffer *buffer)
220 if (buffer->top_line == NULL)
223 for (i = 0; i < body_lines; ++i) {
224 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
228 buffer->top_line = vl;
229 forward_line(buffer, -1);
234 cmd_scroll_down(struct buffer *buffer)
238 if (buffer->top_line == NULL)
241 for (i = 0; i < body_lines; ++i) {
242 if (!forward_line(buffer, +1))
245 buffer->top_line = TAILQ_NEXT(buffer->top_line,
252 cmd_beginning_of_buffer(struct buffer *buffer)
254 buffer->current_line = TAILQ_FIRST(&buffer->head);
256 buffer->top_line = buffer->current_line;
257 buffer->line_off = 0;
261 cmd_end_of_buffer(struct buffer *buffer)
263 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
265 if (buffer->current_line == NULL)
268 /* deal with invisible lines */
269 if (buffer->current_line->parent->flags & L_HIDDEN)
270 forward_line(buffer, -1);
272 cmd_move_end_of_line(buffer);
276 cmd_kill_telescope(struct buffer *buffer)
283 cmd_push_button(struct buffer *buffer)
288 vl = buffer->current_line;
293 switch (vl->parent->type) {
295 load_url_in_tab(current_tab, vl->parent->alt, NULL, 0);
298 l = TAILQ_NEXT(vl->parent, lines);
299 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
300 if (l->type == LINE_PRE_END)
302 l->flags ^= L_HIDDEN;
303 if (l->flags & L_HIDDEN)
315 cmd_push_button_new_tab(struct buffer *buffer)
319 vl = buffer->current_line;
320 if (vl == NULL || vl->parent->type != LINE_LINK)
323 new_tab(vl->parent->alt, current_tab->hist_cur->h, current_tab);
327 cmd_previous_button(struct buffer *buffer)
329 struct excursion place;
331 save_excursion(&place, buffer);
334 if (!forward_line(buffer, -1)) {
335 restore_excursion(&place, buffer);
336 message("No previous link");
339 } while (buffer->current_line->parent->type != LINE_LINK);
343 cmd_next_button(struct buffer *buffer)
345 struct excursion place;
347 save_excursion(&place, buffer);
350 if (!forward_line(buffer, +1)){
351 restore_excursion(&place, buffer);
352 message("No next link");
355 } while (buffer->current_line->parent->type != LINE_LINK);
359 is_heading(const struct line *l)
361 return l->type == LINE_TITLE_1 ||
362 l->type == LINE_TITLE_2 ||
363 l->type == LINE_TITLE_3;
367 cmd_previous_heading(struct buffer *buffer)
369 struct excursion place;
371 save_excursion(&place, buffer);
374 if (!forward_line(buffer, -1)) {
375 restore_excursion(&place, buffer);
376 message("No previous heading");
379 } while (!is_heading(buffer->current_line->parent));
383 cmd_next_heading(struct buffer *buffer)
385 struct excursion place;
387 save_excursion(&place, buffer);
390 if (!forward_line(buffer, +1)) {
391 restore_excursion(&place, buffer);
392 message("No next heading");
395 } while (!is_heading(buffer->current_line->parent));
399 cmd_previous_page(struct buffer *buffer)
401 if (!load_previous_page(current_tab))
402 message("No previous page");
404 start_loading_anim(current_tab);
408 cmd_next_page(struct buffer *buffer)
410 if (!load_next_page(current_tab))
411 message("No next page");
413 start_loading_anim(current_tab);
417 cmd_clear_minibuf(struct buffer *buffer)
423 cmd_execute_extended_command(struct buffer *buffer)
427 GUARD_RECURSIVE_MINIBUFFER();
429 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
430 &eecmd_history, compl_eecmd, NULL);
432 len = sizeof(ministate.prompt);
433 strlcpy(ministate.prompt, "", len);
436 strlcat(ministate.prompt, "M-", len);
438 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
441 strlcat(ministate.prompt, " ", len);
445 cmd_tab_close(struct buffer *buffer)
451 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
452 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
456 message("Can't close the only tab.");
461 cmd_tab_close_other(struct buffer *buffer)
465 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
466 if (t == current_tab)
474 cmd_tab_new(struct buffer *buffer)
478 if ((url = new_tab_url) == NULL)
481 new_tab(url, NULL, NULL);
485 cmd_tab_next(struct buffer *buffer)
489 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
490 t = TAILQ_FIRST(&tabshead);
495 cmd_tab_previous(struct buffer *buffer)
499 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
500 t = TAILQ_LAST(&tabshead, tabshead);
505 cmd_tab_move(struct buffer *buffer)
509 t = TAILQ_NEXT(current_tab, tabs);
510 TAILQ_REMOVE(&tabshead, current_tab, tabs);
513 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
515 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
519 cmd_tab_move_to(struct buffer *buffer)
523 t = TAILQ_PREV(current_tab, tabshead, tabs);
524 TAILQ_REMOVE(&tabshead, current_tab, tabs);
527 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
529 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
533 cmd_tab_select(struct buffer *buffer)
535 GUARD_RECURSIVE_MINIBUFFER();
537 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
538 NULL, compl_ts, NULL);
539 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
543 cmd_load_url(struct buffer *buffer)
545 GUARD_RECURSIVE_MINIBUFFER();
547 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
548 &lu_history, NULL, NULL);
549 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
550 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
551 cmd_move_end_of_line(&ministate.buffer);
555 cmd_load_current_url(struct buffer *buffer)
557 GUARD_RECURSIVE_MINIBUFFER();
559 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
560 &lu_history, NULL, NULL);
561 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
562 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
563 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
567 cmd_reload_page(struct buffer *buffer)
569 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL, 1);
573 cmd_bookmark_page(struct buffer *buffer)
575 GUARD_RECURSIVE_MINIBUFFER();
577 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
579 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
580 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
581 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
585 cmd_list_bookmarks(struct buffer *buffer)
587 load_url_in_tab(current_tab, "about:bookmarks", NULL, 0);
591 cmd_toggle_help(struct buffer *buffer)
593 ui_toggle_side_window();
597 cmd_link_select(struct buffer *buffer)
601 GUARD_RECURSIVE_MINIBUFFER();
603 l = TAILQ_FIRST(&buffer->page.head);
604 while (l != NULL && l->type != LINE_LINK)
605 l = TAILQ_NEXT(l, lines);
608 message("No links found");
612 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
614 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
618 cmd_swiper(struct buffer *buffer)
620 GUARD_RECURSIVE_MINIBUFFER();
622 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
623 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
624 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
628 cmd_toc(struct buffer *buffer)
632 GUARD_RECURSIVE_MINIBUFFER();
634 l = TAILQ_FIRST(&buffer->page.head);
636 l->type != LINE_TITLE_1 &&
637 l->type != LINE_TITLE_2 &&
638 l->type != LINE_TITLE_3)
639 l = TAILQ_NEXT(l, lines);
642 message("No headings found");
646 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
648 strlcpy(ministate.prompt, "Select heading: ",
649 sizeof(ministate.prompt));
653 cmd_inc_fill_column(struct buffer *buffer)
655 if (fill_column == INT_MAX)
659 message("fill-column: %d", fill_column);
661 ui_schedule_redraw();
665 cmd_dec_fill_column(struct buffer *buffer)
667 if (fill_column == INT_MAX || fill_column < 8)
671 message("fill-column: %d", fill_column);
673 ui_schedule_redraw();
677 cmd_olivetti_mode(struct buffer *buffer)
679 olivetti_mode = !olivetti_mode;
681 message("olivetti-mode enabled");
683 message("olivetti-mode disabled");
685 ui_schedule_redraw();
689 cmd_mini_delete_char(struct buffer *buffer)
695 minibuffer_taint_hist();
697 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
702 memmove(c, n, strlen(n)+1);
704 recompute_completions(0);
708 cmd_mini_delete_backward_char(struct buffer *buffer)
714 minibuffer_taint_hist();
716 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
717 start = buffer->current_line->line;
720 p = utf8_prev_cp(c-1, start);
722 memmove(p, c, strlen(c)+1);
725 recompute_completions(0);
729 cmd_mini_kill_line(struct buffer *buffer)
735 minibuffer_taint_hist();
736 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
739 recompute_completions(0);
743 cmd_mini_abort(struct buffer *buffer)
752 cmd_mini_complete_and_exit(struct buffer *buffer)
757 minibuffer_taint_hist();
762 cmd_mini_previous_history_element(struct buffer *buffer)
764 if (ministate.history == NULL) {
765 message("No history");
769 if (ministate.hist_cur == NULL ||
770 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
771 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
772 ministate.hist_off = ministate.history->len - 1;
773 if (ministate.hist_cur == NULL)
774 message("No prev history item");
776 ministate.hist_off--;
779 if (ministate.hist_cur != NULL)
780 buffer->current_line->line = ministate.hist_cur->h;
784 cmd_mini_next_history_element(struct buffer *buffer)
786 if (ministate.history == NULL) {
787 message("No history");
791 if (ministate.hist_cur == NULL ||
792 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
793 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
794 ministate.hist_off = 0;
795 if (ministate.hist_cur == NULL)
796 message("No next history item");
798 ministate.hist_off++;
801 if (ministate.hist_cur != NULL)
802 buffer->current_line->line = ministate.hist_cur->h;
806 cmd_previous_completion(struct buffer *buffer)
808 if (in_minibuffer != MB_COMPREAD)
811 buffer = &ministate.compl.buffer;
813 if (buffer->current_line != NULL)
814 buffer->current_line->parent->type = LINE_COMPL;
816 forward_line(buffer, -1);
818 if (buffer->current_line != NULL)
819 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
823 cmd_next_completion(struct buffer *buffer)
825 if (in_minibuffer != MB_COMPREAD)
828 buffer = &ministate.compl.buffer;
830 if (buffer->current_line != NULL)
831 buffer->current_line->parent->type = LINE_COMPL;
833 forward_line(buffer, +1);
835 if (buffer->current_line != NULL)
836 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
840 cmd_insert_current_candidate(struct buffer *buffer)
844 if (in_minibuffer != MB_COMPREAD)
847 buffer = &ministate.compl.buffer;
848 if ((vl = buffer->current_line) == NULL)
851 minibuffer_taint_hist();
852 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
853 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
857 cmd_suspend_telescope(struct buffer *buffer)
864 cmd_toggle_pre_wrap(struct buffer *buffer)
866 dont_wrap_pre = !dont_wrap_pre;
869 message("Don't wrap preformatted blocks");
871 message("Wrap preformatted blocks");
873 ui_schedule_redraw();
877 cmd_mini_goto_beginning(struct buffer *buffer)
884 buffer = &ministate.compl.buffer;
886 if ((vl = buffer->current_line) != NULL)
887 vl->parent->type = LINE_COMPL;
889 vl = TAILQ_FIRST(&buffer->head);
890 while (vl != NULL && vl->parent->flags & L_HIDDEN)
891 vl = TAILQ_NEXT(vl, vlines);
896 vl->parent->type = LINE_COMPL_CURRENT;
897 buffer->top_line = vl;
898 buffer->current_line = vl;
902 cmd_mini_goto_end(struct buffer *buffer)
909 buffer = &ministate.compl.buffer;
911 if ((vl = buffer->current_line) != NULL)
912 vl->parent->type = LINE_COMPL;
914 vl = TAILQ_LAST(&buffer->head, vhead);
915 while (vl != NULL && vl->parent->flags & L_HIDDEN)
916 vl = TAILQ_PREV(vl, vhead, vlines);
921 vl->parent->type = LINE_COMPL_CURRENT;
922 buffer->current_line = vl;
926 cmd_other_window(struct buffer *buffer)
932 cmd_mini_scroll_up(struct buffer *buffer)
937 buffer = &ministate.compl.buffer;
938 if (buffer->current_line == NULL)
941 buffer->current_line->parent->type = LINE_COMPL;
942 cmd_scroll_up(buffer);
943 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
947 cmd_mini_scroll_down(struct buffer *buffer)
952 buffer = &ministate.compl.buffer;
953 if (buffer->current_line == NULL)
956 buffer->current_line->parent->type = LINE_COMPL;
957 cmd_scroll_down(buffer);
958 buffer->current_line->parent->type = LINE_COMPL_CURRENT;