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.
23 #include "minibuffer.h"
24 #include "telescope.h"
28 #define GUARD_RECURSIVE_MINIBUFFER() \
30 if (in_minibuffer) { \
31 message("enable-recursive-minibuffers " \
32 "is not yet available."); \
37 /* return 1 if moved, 0 otherwise */
39 forward_line(struct buffer *buffer, int n)
44 if (buffer->current_line == NULL)
46 vl = buffer->current_line;
51 vl = TAILQ_NEXT(vl, vlines);
54 if (vl->parent->flags & L_HIDDEN)
56 buffer->current_line = vl;
59 vl = TAILQ_PREV(vl, vhead, vlines);
62 if (vl->parent->flags & L_HIDDEN)
64 if (buffer->current_line == buffer->top_line) {
66 buffer->top_line = vl;
68 buffer->current_line = vl;
79 cmd_previous_line(struct buffer *buffer)
81 forward_line(buffer, -1);
85 cmd_next_line(struct buffer *buffer)
87 forward_line(buffer, +1);
91 cmd_backward_char(struct buffer *buffer)
93 if (buffer->cpoff != 0)
98 cmd_forward_char(struct buffer *buffer)
102 if (buffer->current_line->line != NULL)
103 len = utf8_cplen(buffer->current_line->line);
104 if (++buffer->cpoff > len)
109 cmd_backward_paragraph(struct buffer *buffer)
112 if (!forward_line(buffer, -1)) {
113 message("No previous paragraph");
116 } while (buffer->current_line->line != NULL ||
117 buffer->current_line->parent->type != LINE_TEXT);
121 cmd_forward_paragraph(struct buffer *buffer)
124 if (!forward_line(buffer, +1)) {
125 message("No next paragraph");
128 } while (buffer->current_line->line != NULL ||
129 buffer->current_line->parent->type != LINE_TEXT);
133 cmd_move_beginning_of_line(struct buffer *buffer)
139 cmd_move_end_of_line(struct buffer *buffer)
143 vl = buffer->current_line;
144 if (vl->line == NULL)
146 buffer->cpoff = utf8_cplen(vl->line);
150 cmd_redraw(struct buffer *buffer)
152 ui_schedule_redraw();
156 cmd_scroll_line_up(struct buffer *buffer)
160 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
164 forward_line(buffer, -1);
166 buffer->top_line = vl;
170 cmd_scroll_line_down(struct buffer *buffer)
172 if (!forward_line(buffer, +1))
175 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
180 cmd_scroll_up(struct buffer *buffer)
185 for (i = 0; i < body_lines; ++i) {
186 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
189 buffer->top_line = vl;
190 forward_line(buffer, -1);
195 cmd_scroll_down(struct buffer *buffer)
199 for (i = 0; i < body_lines; ++i) {
200 if (!forward_line(buffer, +1))
203 buffer->top_line = TAILQ_NEXT(buffer->top_line,
209 cmd_beginning_of_buffer(struct buffer *buffer)
211 buffer->current_line = TAILQ_FIRST(&buffer->head);
213 buffer->top_line = buffer->current_line;
214 buffer->line_off = 0;
218 cmd_end_of_buffer(struct buffer *buffer)
220 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
222 /* deal with invisible lines */
223 if (buffer->current_line->parent->flags & L_HIDDEN)
224 forward_line(buffer, -1);
226 cmd_move_end_of_line(buffer);
230 cmd_kill_telescope(struct buffer *buffer)
237 cmd_push_button(struct buffer *buffer)
242 vl = buffer->current_line;
243 switch (vl->parent->type) {
245 load_url_in_tab(current_tab, vl->parent->alt);
248 l = TAILQ_NEXT(vl->parent, lines);
249 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
250 if (l->type == LINE_PRE_END)
252 l->flags ^= L_HIDDEN;
253 if (l->flags & L_HIDDEN)
265 cmd_push_button_new_tab(struct buffer *buffer)
269 vl = buffer->current_line;
270 if (vl->parent->type != LINE_LINK)
273 new_tab(vl->parent->alt);
277 cmd_previous_button(struct buffer *buffer)
279 struct excursion place;
281 save_excursion(&place, buffer);
284 if (!forward_line(buffer, -1)) {
285 restore_excursion(&place, buffer);
286 message("No previous link");
289 } while (buffer->current_line->parent->type != LINE_LINK);
293 cmd_next_button(struct buffer *buffer)
295 struct excursion place;
297 save_excursion(&place, buffer);
300 if (!forward_line(buffer, +1)){
301 restore_excursion(&place, buffer);
302 message("No next link");
305 } while (buffer->current_line->parent->type != LINE_LINK);
309 is_heading(const struct line *l)
311 return l->type == LINE_TITLE_1 ||
312 l->type == LINE_TITLE_2 ||
313 l->type == LINE_TITLE_3;
317 cmd_previous_heading(struct buffer *buffer)
319 struct excursion place;
321 save_excursion(&place, buffer);
324 if (!forward_line(buffer, -1)) {
325 restore_excursion(&place, buffer);
326 message("No previous heading");
329 } while (!is_heading(buffer->current_line->parent));
333 cmd_next_heading(struct buffer *buffer)
335 struct excursion place;
337 save_excursion(&place, buffer);
340 if (!forward_line(buffer, +1)) {
341 restore_excursion(&place, buffer);
342 message("No next heading");
345 } while (!is_heading(buffer->current_line->parent));
349 cmd_previous_page(struct buffer *buffer)
351 if (!load_previous_page(current_tab))
352 message("No previous page");
354 start_loading_anim(current_tab);
358 cmd_next_page(struct buffer *buffer)
360 if (!load_next_page(current_tab))
361 message("No next page");
363 start_loading_anim(current_tab);
367 cmd_clear_minibuf(struct buffer *buffer)
373 cmd_execute_extended_command(struct buffer *buffer)
377 GUARD_RECURSIVE_MINIBUFFER();
379 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
380 &eecmd_history, compl_eecmd, NULL);
382 len = sizeof(ministate.prompt);
383 strlcpy(ministate.prompt, "", len);
386 strlcat(ministate.prompt, "M-", len);
388 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
391 strlcat(ministate.prompt, " ", len);
395 cmd_tab_close(struct buffer *buffer)
400 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
401 TAILQ_NEXT(tab, tabs) == NULL) {
402 message("Can't close the only tab.");
406 if (evtimer_pending(&tab->loadingev, NULL))
407 evtimer_del(&tab->loadingev);
411 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
412 t = TAILQ_NEXT(tab, tabs);
413 TAILQ_REMOVE(&tabshead, tab, tabs);
420 cmd_tab_close_other(struct buffer *buffer)
424 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
425 if (t == current_tab)
429 TAILQ_REMOVE(&tabshead, t, tabs);
435 cmd_tab_new(struct buffer *buffer)
439 if ((url = new_tab_url) == NULL)
446 cmd_tab_next(struct buffer *buffer)
450 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
451 t = TAILQ_FIRST(&tabshead);
456 cmd_tab_previous(struct buffer *buffer)
460 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
461 t = TAILQ_LAST(&tabshead, tabshead);
466 cmd_tab_move(struct buffer *buffer)
470 t = TAILQ_NEXT(current_tab, tabs);
471 TAILQ_REMOVE(&tabshead, current_tab, tabs);
474 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
476 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
480 cmd_tab_move_to(struct buffer *buffer)
484 t = TAILQ_PREV(current_tab, tabshead, tabs);
485 TAILQ_REMOVE(&tabshead, current_tab, tabs);
488 if (TAILQ_EMPTY(&tabshead))
489 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
491 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
493 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
497 cmd_tab_select(struct buffer *buffer)
499 GUARD_RECURSIVE_MINIBUFFER();
501 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
502 NULL, compl_ts, NULL);
503 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
507 cmd_load_url(struct buffer *buffer)
509 GUARD_RECURSIVE_MINIBUFFER();
511 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
512 &lu_history, NULL, NULL);
513 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
514 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
515 cmd_move_end_of_line(&ministate.buffer);
519 cmd_load_current_url(struct buffer *buffer)
521 GUARD_RECURSIVE_MINIBUFFER();
523 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
524 &lu_history, NULL, NULL);
525 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
526 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
527 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
531 cmd_reload_page(struct buffer *buffer)
533 load_url_in_tab(current_tab, current_tab->hist_cur->h);
537 cmd_bookmark_page(struct buffer *buffer)
539 GUARD_RECURSIVE_MINIBUFFER();
541 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
543 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
544 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
545 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
549 cmd_list_bookmarks(struct buffer *buffer)
551 load_url_in_tab(current_tab, "about:bookmarks");
555 cmd_toggle_help(struct buffer *buffer)
557 ui_toggle_side_window();
561 cmd_link_select(struct buffer *buffer)
565 GUARD_RECURSIVE_MINIBUFFER();
567 l = TAILQ_FIRST(&buffer->page.head);
568 while (l != NULL && l->type != LINE_LINK)
569 l = TAILQ_NEXT(l, lines);
572 message("No links found");
576 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
578 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
582 cmd_swiper(struct buffer *buffer)
584 GUARD_RECURSIVE_MINIBUFFER();
586 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
587 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
588 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
592 cmd_toc(struct buffer *buffer)
596 GUARD_RECURSIVE_MINIBUFFER();
598 l = TAILQ_FIRST(&buffer->page.head);
600 l->type != LINE_TITLE_1 &&
601 l->type != LINE_TITLE_2 &&
602 l->type != LINE_TITLE_3)
603 l = TAILQ_NEXT(l, lines);
606 message("No headings found");
610 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
612 strlcpy(ministate.prompt, "Select heading: ",
613 sizeof(ministate.prompt));
617 cmd_inc_fill_column(struct buffer *buffer)
619 if (fill_column == INT_MAX)
623 message("fill-column: %d", fill_column);
625 ui_schedule_redraw();
629 cmd_dec_fill_column(struct buffer *buffer)
631 if (fill_column == INT_MAX || fill_column < 8)
635 message("fill-column: %d", fill_column);
637 ui_schedule_redraw();
641 cmd_olivetti_mode(struct buffer *buffer)
643 olivetti_mode = !olivetti_mode;
645 message("olivetti-mode enabled");
647 message("olivetti-mode disabled");
649 ui_schedule_redraw();
653 cmd_mini_delete_char(struct buffer *buffer)
657 if (!in_minibuffer) {
658 message("text is read-only");
662 minibuffer_taint_hist();
664 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
669 memmove(c, n, strlen(n)+1);
671 recompute_completions(0);
675 cmd_mini_delete_backward_char(struct buffer *buffer)
679 if (!in_minibuffer) {
680 message("text is read-only");
684 minibuffer_taint_hist();
686 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
687 start = buffer->current_line->line;
690 p = utf8_prev_cp(c-1, start);
692 memmove(p, c, strlen(c)+1);
695 recompute_completions(0);
699 cmd_mini_kill_line(struct buffer *buffer)
703 if (!in_minibuffer) {
704 message("text is read-only");
708 minibuffer_taint_hist();
709 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
712 recompute_completions(0);
716 cmd_mini_abort(struct buffer *buffer)
725 cmd_mini_complete_and_exit(struct buffer *buffer)
730 minibuffer_taint_hist();
735 cmd_mini_previous_history_element(struct buffer *buffer)
737 if (ministate.history == NULL) {
738 message("No history");
742 if (ministate.hist_cur == NULL ||
743 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
744 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
745 ministate.hist_off = ministate.history->len - 1;
746 if (ministate.hist_cur == NULL)
747 message("No prev history item");
749 ministate.hist_off--;
752 if (ministate.hist_cur != NULL)
753 buffer->current_line->line = ministate.hist_cur->h;
757 cmd_mini_next_history_element(struct buffer *buffer)
759 if (ministate.history == NULL) {
760 message("No history");
764 if (ministate.hist_cur == NULL ||
765 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
766 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
767 ministate.hist_off = 0;
768 if (ministate.hist_cur == NULL)
769 message("No next history item");
771 ministate.hist_off++;
774 if (ministate.hist_cur != NULL)
775 buffer->current_line->line = ministate.hist_cur->h;
779 cmd_previous_completion(struct buffer *buffer)
781 if (in_minibuffer != MB_COMPREAD)
784 buffer = &ministate.compl.buffer;
786 if (buffer->current_line != NULL)
787 buffer->current_line->parent->type = LINE_COMPL;
789 forward_line(buffer, -1);
791 if (buffer->current_line != NULL)
792 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
796 cmd_next_completion(struct buffer *buffer)
798 if (in_minibuffer != MB_COMPREAD)
801 buffer = &ministate.compl.buffer;
803 if (buffer->current_line != NULL)
804 buffer->current_line->parent->type = LINE_COMPL;
806 forward_line(buffer, +1);
808 if (buffer->current_line != NULL)
809 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
813 cmd_insert_current_candidate(struct buffer *buffer)
817 if (in_minibuffer != MB_COMPREAD)
820 buffer = &ministate.compl.buffer;
821 if ((vl = buffer->current_line) == NULL)
824 minibuffer_taint_hist();
825 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
826 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
830 cmd_suspend_telescope(struct buffer *buffer)
837 cmd_toggle_pre_wrap(struct buffer *buffer)
839 dont_wrap_pre = !dont_wrap_pre;
842 message("Don't wrap preformatted blocks");
844 message("Wrap preformatted blocks");
846 ui_schedule_redraw();
850 cmd_mini_goto_beginning(struct buffer *buffer)
857 buffer = &ministate.compl.buffer;
859 if ((vl = buffer->current_line) != NULL)
860 vl->parent->type = LINE_COMPL;
862 vl = TAILQ_FIRST(&buffer->head);
863 while (vl != NULL && vl->parent->flags & L_HIDDEN)
864 vl = TAILQ_NEXT(vl, vlines);
869 vl->parent->type = LINE_COMPL_CURRENT;
870 buffer->top_line = vl;
871 buffer->current_line = vl;
875 cmd_mini_goto_end(struct buffer *buffer)
882 buffer = &ministate.compl.buffer;
884 if ((vl = buffer->current_line) != NULL)
885 vl->parent->type = LINE_COMPL;
887 vl = TAILQ_LAST(&buffer->head, vhead);
888 while (vl != NULL && vl->parent->flags & L_HIDDEN)
889 vl = TAILQ_PREV(vl, vhead, vlines);
894 vl->parent->type = LINE_COMPL_CURRENT;
895 buffer->current_line = vl;