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"
26 #include "telescope.h"
30 #define GUARD_RECURSIVE_MINIBUFFER() \
32 if (in_minibuffer) { \
33 message("enable-recursive-minibuffers " \
34 "is not yet available."); \
39 #define GUARD_READ_ONLY() \
41 if (!in_minibuffer) { \
42 message("text is read-only"); \
47 /* return 1 if moved, 0 otherwise */
49 forward_line(struct buffer *buffer, int n)
54 if (buffer->current_line == NULL)
56 vl = buffer->current_line;
61 vl = TAILQ_NEXT(vl, vlines);
64 if (vl->parent->flags & L_HIDDEN)
66 buffer->current_line = vl;
69 vl = TAILQ_PREV(vl, vhead, vlines);
72 if (vl->parent->flags & L_HIDDEN)
74 if (buffer->current_line == buffer->top_line) {
76 buffer->top_line = vl;
78 buffer->current_line = vl;
89 cmd_previous_line(struct buffer *buffer)
91 forward_line(buffer, -1);
95 cmd_next_line(struct buffer *buffer)
97 forward_line(buffer, +1);
101 cmd_backward_char(struct buffer *buffer)
103 if (buffer->cpoff != 0)
108 cmd_forward_char(struct buffer *buffer)
112 if (buffer->current_line == NULL)
115 if (buffer->current_line->line != NULL)
116 len = utf8_cplen(buffer->current_line->line);
117 if (++buffer->cpoff > len)
122 cmd_backward_paragraph(struct buffer *buffer)
125 if (!forward_line(buffer, -1)) {
126 message("No previous paragraph");
129 } while (buffer->current_line->line != NULL ||
130 buffer->current_line->parent->type != LINE_TEXT);
134 cmd_forward_paragraph(struct buffer *buffer)
137 if (!forward_line(buffer, +1)) {
138 message("No next paragraph");
141 } while (buffer->current_line->line != NULL ||
142 buffer->current_line->parent->type != LINE_TEXT);
146 cmd_move_beginning_of_line(struct buffer *buffer)
152 cmd_move_end_of_line(struct buffer *buffer)
156 vl = buffer->current_line;
157 if (vl == NULL || vl->line == NULL)
159 buffer->cpoff = utf8_cplen(vl->line);
163 cmd_redraw(struct buffer *buffer)
165 ui_schedule_redraw();
169 cmd_scroll_line_up(struct buffer *buffer)
173 if (buffer->top_line == NULL)
176 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
180 forward_line(buffer, -1);
182 buffer->top_line = vl;
186 cmd_scroll_line_down(struct buffer *buffer)
188 if (!forward_line(buffer, +1))
191 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
196 cmd_scroll_up(struct buffer *buffer)
201 if (buffer->top_line == NULL)
204 for (i = 0; i < body_lines; ++i) {
205 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
209 buffer->top_line = vl;
210 forward_line(buffer, -1);
215 cmd_scroll_down(struct buffer *buffer)
219 if (buffer->top_line == NULL)
222 for (i = 0; i < body_lines; ++i) {
223 if (!forward_line(buffer, +1))
226 buffer->top_line = TAILQ_NEXT(buffer->top_line,
233 cmd_beginning_of_buffer(struct buffer *buffer)
235 buffer->current_line = TAILQ_FIRST(&buffer->head);
237 buffer->top_line = buffer->current_line;
238 buffer->line_off = 0;
242 cmd_end_of_buffer(struct buffer *buffer)
244 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
246 if (buffer->current_line == NULL)
249 /* deal with invisible lines */
250 if (buffer->current_line->parent->flags & L_HIDDEN)
251 forward_line(buffer, -1);
253 cmd_move_end_of_line(buffer);
257 cmd_kill_telescope(struct buffer *buffer)
264 cmd_push_button(struct buffer *buffer)
269 vl = buffer->current_line;
274 switch (vl->parent->type) {
276 load_url_in_tab(current_tab, vl->parent->alt, NULL);
279 l = TAILQ_NEXT(vl->parent, lines);
280 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
281 if (l->type == LINE_PRE_END)
283 l->flags ^= L_HIDDEN;
284 if (l->flags & L_HIDDEN)
296 cmd_push_button_new_tab(struct buffer *buffer)
300 vl = buffer->current_line;
301 if (vl == NULL || vl->parent->type != LINE_LINK)
304 new_tab(vl->parent->alt, current_tab->hist_cur->h);
308 cmd_previous_button(struct buffer *buffer)
310 struct excursion place;
312 save_excursion(&place, buffer);
315 if (!forward_line(buffer, -1)) {
316 restore_excursion(&place, buffer);
317 message("No previous link");
320 } while (buffer->current_line->parent->type != LINE_LINK);
324 cmd_next_button(struct buffer *buffer)
326 struct excursion place;
328 save_excursion(&place, buffer);
331 if (!forward_line(buffer, +1)){
332 restore_excursion(&place, buffer);
333 message("No next link");
336 } while (buffer->current_line->parent->type != LINE_LINK);
340 is_heading(const struct line *l)
342 return l->type == LINE_TITLE_1 ||
343 l->type == LINE_TITLE_2 ||
344 l->type == LINE_TITLE_3;
348 cmd_previous_heading(struct buffer *buffer)
350 struct excursion place;
352 save_excursion(&place, buffer);
355 if (!forward_line(buffer, -1)) {
356 restore_excursion(&place, buffer);
357 message("No previous heading");
360 } while (!is_heading(buffer->current_line->parent));
364 cmd_next_heading(struct buffer *buffer)
366 struct excursion place;
368 save_excursion(&place, buffer);
371 if (!forward_line(buffer, +1)) {
372 restore_excursion(&place, buffer);
373 message("No next heading");
376 } while (!is_heading(buffer->current_line->parent));
380 cmd_previous_page(struct buffer *buffer)
382 if (!load_previous_page(current_tab))
383 message("No previous page");
385 start_loading_anim(current_tab);
389 cmd_next_page(struct buffer *buffer)
391 if (!load_next_page(current_tab))
392 message("No next page");
394 start_loading_anim(current_tab);
398 cmd_clear_minibuf(struct buffer *buffer)
404 cmd_execute_extended_command(struct buffer *buffer)
408 GUARD_RECURSIVE_MINIBUFFER();
410 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
411 &eecmd_history, compl_eecmd, NULL);
413 len = sizeof(ministate.prompt);
414 strlcpy(ministate.prompt, "", len);
417 strlcat(ministate.prompt, "M-", len);
419 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
422 strlcat(ministate.prompt, " ", len);
426 cmd_tab_close(struct buffer *buffer)
432 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
433 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
437 message("Can't close the only tab.");
442 cmd_tab_close_other(struct buffer *buffer)
446 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
447 if (t == current_tab)
455 cmd_tab_new(struct buffer *buffer)
459 if ((url = new_tab_url) == NULL)
466 cmd_tab_next(struct buffer *buffer)
470 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
471 t = TAILQ_FIRST(&tabshead);
476 cmd_tab_previous(struct buffer *buffer)
480 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
481 t = TAILQ_LAST(&tabshead, tabshead);
486 cmd_tab_move(struct buffer *buffer)
490 t = TAILQ_NEXT(current_tab, tabs);
491 TAILQ_REMOVE(&tabshead, current_tab, tabs);
494 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
496 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
500 cmd_tab_move_to(struct buffer *buffer)
504 t = TAILQ_PREV(current_tab, tabshead, tabs);
505 TAILQ_REMOVE(&tabshead, current_tab, tabs);
508 if (TAILQ_EMPTY(&tabshead))
509 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
511 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
513 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
517 cmd_tab_select(struct buffer *buffer)
519 GUARD_RECURSIVE_MINIBUFFER();
521 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
522 NULL, compl_ts, NULL);
523 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
527 cmd_load_url(struct buffer *buffer)
529 GUARD_RECURSIVE_MINIBUFFER();
531 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
532 &lu_history, NULL, NULL);
533 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
534 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
535 cmd_move_end_of_line(&ministate.buffer);
539 cmd_load_current_url(struct buffer *buffer)
541 GUARD_RECURSIVE_MINIBUFFER();
543 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
544 &lu_history, NULL, NULL);
545 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
546 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
547 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
551 cmd_reload_page(struct buffer *buffer)
553 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL);
557 cmd_bookmark_page(struct buffer *buffer)
559 GUARD_RECURSIVE_MINIBUFFER();
561 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
563 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
564 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
565 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
569 cmd_list_bookmarks(struct buffer *buffer)
571 load_url_in_tab(current_tab, "about:bookmarks", NULL);
575 cmd_toggle_help(struct buffer *buffer)
577 ui_toggle_side_window();
581 cmd_link_select(struct buffer *buffer)
585 GUARD_RECURSIVE_MINIBUFFER();
587 l = TAILQ_FIRST(&buffer->page.head);
588 while (l != NULL && l->type != LINE_LINK)
589 l = TAILQ_NEXT(l, lines);
592 message("No links found");
596 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
598 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
602 cmd_swiper(struct buffer *buffer)
604 GUARD_RECURSIVE_MINIBUFFER();
606 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
607 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
608 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
612 cmd_toc(struct buffer *buffer)
616 GUARD_RECURSIVE_MINIBUFFER();
618 l = TAILQ_FIRST(&buffer->page.head);
620 l->type != LINE_TITLE_1 &&
621 l->type != LINE_TITLE_2 &&
622 l->type != LINE_TITLE_3)
623 l = TAILQ_NEXT(l, lines);
626 message("No headings found");
630 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
632 strlcpy(ministate.prompt, "Select heading: ",
633 sizeof(ministate.prompt));
637 cmd_inc_fill_column(struct buffer *buffer)
639 if (fill_column == INT_MAX)
643 message("fill-column: %d", fill_column);
645 ui_schedule_redraw();
649 cmd_dec_fill_column(struct buffer *buffer)
651 if (fill_column == INT_MAX || fill_column < 8)
655 message("fill-column: %d", fill_column);
657 ui_schedule_redraw();
661 cmd_olivetti_mode(struct buffer *buffer)
663 olivetti_mode = !olivetti_mode;
665 message("olivetti-mode enabled");
667 message("olivetti-mode disabled");
669 ui_schedule_redraw();
673 cmd_mini_delete_char(struct buffer *buffer)
679 minibuffer_taint_hist();
681 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
686 memmove(c, n, strlen(n)+1);
688 recompute_completions(0);
692 cmd_mini_delete_backward_char(struct buffer *buffer)
698 minibuffer_taint_hist();
700 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
701 start = buffer->current_line->line;
704 p = utf8_prev_cp(c-1, start);
706 memmove(p, c, strlen(c)+1);
709 recompute_completions(0);
713 cmd_mini_kill_line(struct buffer *buffer)
719 minibuffer_taint_hist();
720 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
723 recompute_completions(0);
727 cmd_mini_abort(struct buffer *buffer)
736 cmd_mini_complete_and_exit(struct buffer *buffer)
741 minibuffer_taint_hist();
746 cmd_mini_previous_history_element(struct buffer *buffer)
748 if (ministate.history == NULL) {
749 message("No history");
753 if (ministate.hist_cur == NULL ||
754 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
755 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
756 ministate.hist_off = ministate.history->len - 1;
757 if (ministate.hist_cur == NULL)
758 message("No prev history item");
760 ministate.hist_off--;
763 if (ministate.hist_cur != NULL)
764 buffer->current_line->line = ministate.hist_cur->h;
768 cmd_mini_next_history_element(struct buffer *buffer)
770 if (ministate.history == NULL) {
771 message("No history");
775 if (ministate.hist_cur == NULL ||
776 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
777 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
778 ministate.hist_off = 0;
779 if (ministate.hist_cur == NULL)
780 message("No next history item");
782 ministate.hist_off++;
785 if (ministate.hist_cur != NULL)
786 buffer->current_line->line = ministate.hist_cur->h;
790 cmd_previous_completion(struct buffer *buffer)
792 if (in_minibuffer != MB_COMPREAD)
795 buffer = &ministate.compl.buffer;
797 if (buffer->current_line != NULL)
798 buffer->current_line->parent->type = LINE_COMPL;
800 forward_line(buffer, -1);
802 if (buffer->current_line != NULL)
803 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
807 cmd_next_completion(struct buffer *buffer)
809 if (in_minibuffer != MB_COMPREAD)
812 buffer = &ministate.compl.buffer;
814 if (buffer->current_line != NULL)
815 buffer->current_line->parent->type = LINE_COMPL;
817 forward_line(buffer, +1);
819 if (buffer->current_line != NULL)
820 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
824 cmd_insert_current_candidate(struct buffer *buffer)
828 if (in_minibuffer != MB_COMPREAD)
831 buffer = &ministate.compl.buffer;
832 if ((vl = buffer->current_line) == NULL)
835 minibuffer_taint_hist();
836 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
837 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
841 cmd_suspend_telescope(struct buffer *buffer)
848 cmd_toggle_pre_wrap(struct buffer *buffer)
850 dont_wrap_pre = !dont_wrap_pre;
853 message("Don't wrap preformatted blocks");
855 message("Wrap preformatted blocks");
857 ui_schedule_redraw();
861 cmd_mini_goto_beginning(struct buffer *buffer)
868 buffer = &ministate.compl.buffer;
870 if ((vl = buffer->current_line) != NULL)
871 vl->parent->type = LINE_COMPL;
873 vl = TAILQ_FIRST(&buffer->head);
874 while (vl != NULL && vl->parent->flags & L_HIDDEN)
875 vl = TAILQ_NEXT(vl, vlines);
880 vl->parent->type = LINE_COMPL_CURRENT;
881 buffer->top_line = vl;
882 buffer->current_line = vl;
886 cmd_mini_goto_end(struct buffer *buffer)
893 buffer = &ministate.compl.buffer;
895 if ((vl = buffer->current_line) != NULL)
896 vl->parent->type = LINE_COMPL;
898 vl = TAILQ_LAST(&buffer->head, vhead);
899 while (vl != NULL && vl->parent->flags & L_HIDDEN)
900 vl = TAILQ_PREV(vl, vhead, vlines);
905 vl->parent->type = LINE_COMPL_CURRENT;
906 buffer->current_line = vl;
910 cmd_other_window(struct buffer *buffer)