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 == NULL)
105 if (buffer->current_line->line != NULL)
106 len = utf8_cplen(buffer->current_line->line);
107 if (++buffer->cpoff > len)
112 cmd_backward_paragraph(struct buffer *buffer)
115 if (!forward_line(buffer, -1)) {
116 message("No previous paragraph");
119 } while (buffer->current_line->line != NULL ||
120 buffer->current_line->parent->type != LINE_TEXT);
124 cmd_forward_paragraph(struct buffer *buffer)
127 if (!forward_line(buffer, +1)) {
128 message("No next paragraph");
131 } while (buffer->current_line->line != NULL ||
132 buffer->current_line->parent->type != LINE_TEXT);
136 cmd_move_beginning_of_line(struct buffer *buffer)
142 cmd_move_end_of_line(struct buffer *buffer)
146 vl = buffer->current_line;
147 if (vl == NULL || vl->line == NULL)
149 buffer->cpoff = utf8_cplen(vl->line);
153 cmd_redraw(struct buffer *buffer)
155 ui_schedule_redraw();
159 cmd_scroll_line_up(struct buffer *buffer)
163 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
167 forward_line(buffer, -1);
169 buffer->top_line = vl;
173 cmd_scroll_line_down(struct buffer *buffer)
175 if (!forward_line(buffer, +1))
178 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
183 cmd_scroll_up(struct buffer *buffer)
188 for (i = 0; i < body_lines; ++i) {
189 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
192 buffer->top_line = vl;
193 forward_line(buffer, -1);
198 cmd_scroll_down(struct buffer *buffer)
202 for (i = 0; i < body_lines; ++i) {
203 if (!forward_line(buffer, +1))
206 buffer->top_line = TAILQ_NEXT(buffer->top_line,
212 cmd_beginning_of_buffer(struct buffer *buffer)
214 buffer->current_line = TAILQ_FIRST(&buffer->head);
216 buffer->top_line = buffer->current_line;
217 buffer->line_off = 0;
221 cmd_end_of_buffer(struct buffer *buffer)
223 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
225 if (buffer->current_line == NULL)
228 /* deal with invisible lines */
229 if (buffer->current_line->parent->flags & L_HIDDEN)
230 forward_line(buffer, -1);
232 cmd_move_end_of_line(buffer);
236 cmd_kill_telescope(struct buffer *buffer)
243 cmd_push_button(struct buffer *buffer)
248 vl = buffer->current_line;
253 switch (vl->parent->type) {
255 load_url_in_tab(current_tab, vl->parent->alt);
258 l = TAILQ_NEXT(vl->parent, lines);
259 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
260 if (l->type == LINE_PRE_END)
262 l->flags ^= L_HIDDEN;
263 if (l->flags & L_HIDDEN)
275 cmd_push_button_new_tab(struct buffer *buffer)
279 vl = buffer->current_line;
280 if (vl == NULL || vl->parent->type != LINE_LINK)
283 new_tab(vl->parent->alt);
287 cmd_previous_button(struct buffer *buffer)
289 struct excursion place;
291 save_excursion(&place, buffer);
294 if (!forward_line(buffer, -1)) {
295 restore_excursion(&place, buffer);
296 message("No previous link");
299 } while (buffer->current_line->parent->type != LINE_LINK);
303 cmd_next_button(struct buffer *buffer)
305 struct excursion place;
307 save_excursion(&place, buffer);
310 if (!forward_line(buffer, +1)){
311 restore_excursion(&place, buffer);
312 message("No next link");
315 } while (buffer->current_line->parent->type != LINE_LINK);
319 is_heading(const struct line *l)
321 return l->type == LINE_TITLE_1 ||
322 l->type == LINE_TITLE_2 ||
323 l->type == LINE_TITLE_3;
327 cmd_previous_heading(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 heading");
339 } while (!is_heading(buffer->current_line->parent));
343 cmd_next_heading(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 heading");
355 } while (!is_heading(buffer->current_line->parent));
359 cmd_previous_page(struct buffer *buffer)
361 if (!load_previous_page(current_tab))
362 message("No previous page");
364 start_loading_anim(current_tab);
368 cmd_next_page(struct buffer *buffer)
370 if (!load_next_page(current_tab))
371 message("No next page");
373 start_loading_anim(current_tab);
377 cmd_clear_minibuf(struct buffer *buffer)
383 cmd_execute_extended_command(struct buffer *buffer)
387 GUARD_RECURSIVE_MINIBUFFER();
389 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
390 &eecmd_history, compl_eecmd, NULL);
392 len = sizeof(ministate.prompt);
393 strlcpy(ministate.prompt, "", len);
396 strlcat(ministate.prompt, "M-", len);
398 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
401 strlcat(ministate.prompt, " ", len);
405 cmd_tab_close(struct buffer *buffer)
410 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
411 TAILQ_NEXT(tab, tabs) == NULL) {
412 message("Can't close the only tab.");
416 if (evtimer_pending(&tab->loadingev, NULL))
417 evtimer_del(&tab->loadingev);
421 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
422 t = TAILQ_NEXT(tab, tabs);
423 TAILQ_REMOVE(&tabshead, tab, tabs);
430 cmd_tab_close_other(struct buffer *buffer)
434 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
435 if (t == current_tab)
439 TAILQ_REMOVE(&tabshead, t, tabs);
445 cmd_tab_new(struct buffer *buffer)
449 if ((url = new_tab_url) == NULL)
456 cmd_tab_next(struct buffer *buffer)
460 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
461 t = TAILQ_FIRST(&tabshead);
466 cmd_tab_previous(struct buffer *buffer)
470 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
471 t = TAILQ_LAST(&tabshead, tabshead);
476 cmd_tab_move(struct buffer *buffer)
480 t = TAILQ_NEXT(current_tab, tabs);
481 TAILQ_REMOVE(&tabshead, current_tab, tabs);
484 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
486 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
490 cmd_tab_move_to(struct buffer *buffer)
494 t = TAILQ_PREV(current_tab, tabshead, tabs);
495 TAILQ_REMOVE(&tabshead, current_tab, tabs);
498 if (TAILQ_EMPTY(&tabshead))
499 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
501 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
503 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
507 cmd_tab_select(struct buffer *buffer)
509 GUARD_RECURSIVE_MINIBUFFER();
511 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
512 NULL, compl_ts, NULL);
513 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
517 cmd_load_url(struct buffer *buffer)
519 GUARD_RECURSIVE_MINIBUFFER();
521 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
522 &lu_history, NULL, NULL);
523 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
524 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
525 cmd_move_end_of_line(&ministate.buffer);
529 cmd_load_current_url(struct buffer *buffer)
531 GUARD_RECURSIVE_MINIBUFFER();
533 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
534 &lu_history, NULL, NULL);
535 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
536 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
537 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
541 cmd_reload_page(struct buffer *buffer)
543 load_url_in_tab(current_tab, current_tab->hist_cur->h);
547 cmd_bookmark_page(struct buffer *buffer)
549 GUARD_RECURSIVE_MINIBUFFER();
551 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
553 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
554 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
555 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
559 cmd_list_bookmarks(struct buffer *buffer)
561 load_url_in_tab(current_tab, "about:bookmarks");
565 cmd_toggle_help(struct buffer *buffer)
567 ui_toggle_side_window();
571 cmd_link_select(struct buffer *buffer)
575 GUARD_RECURSIVE_MINIBUFFER();
577 l = TAILQ_FIRST(&buffer->page.head);
578 while (l != NULL && l->type != LINE_LINK)
579 l = TAILQ_NEXT(l, lines);
582 message("No links found");
586 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
588 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
592 cmd_swiper(struct buffer *buffer)
594 GUARD_RECURSIVE_MINIBUFFER();
596 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
597 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
598 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
602 cmd_toc(struct buffer *buffer)
606 GUARD_RECURSIVE_MINIBUFFER();
608 l = TAILQ_FIRST(&buffer->page.head);
610 l->type != LINE_TITLE_1 &&
611 l->type != LINE_TITLE_2 &&
612 l->type != LINE_TITLE_3)
613 l = TAILQ_NEXT(l, lines);
616 message("No headings found");
620 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
622 strlcpy(ministate.prompt, "Select heading: ",
623 sizeof(ministate.prompt));
627 cmd_inc_fill_column(struct buffer *buffer)
629 if (fill_column == INT_MAX)
633 message("fill-column: %d", fill_column);
635 ui_schedule_redraw();
639 cmd_dec_fill_column(struct buffer *buffer)
641 if (fill_column == INT_MAX || fill_column < 8)
645 message("fill-column: %d", fill_column);
647 ui_schedule_redraw();
651 cmd_olivetti_mode(struct buffer *buffer)
653 olivetti_mode = !olivetti_mode;
655 message("olivetti-mode enabled");
657 message("olivetti-mode disabled");
659 ui_schedule_redraw();
663 cmd_mini_delete_char(struct buffer *buffer)
667 if (!in_minibuffer) {
668 message("text is read-only");
672 minibuffer_taint_hist();
674 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
679 memmove(c, n, strlen(n)+1);
681 recompute_completions(0);
685 cmd_mini_delete_backward_char(struct buffer *buffer)
689 if (!in_minibuffer) {
690 message("text is read-only");
694 minibuffer_taint_hist();
696 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
697 start = buffer->current_line->line;
700 p = utf8_prev_cp(c-1, start);
702 memmove(p, c, strlen(c)+1);
705 recompute_completions(0);
709 cmd_mini_kill_line(struct buffer *buffer)
713 if (!in_minibuffer) {
714 message("text is read-only");
718 minibuffer_taint_hist();
719 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
722 recompute_completions(0);
726 cmd_mini_abort(struct buffer *buffer)
735 cmd_mini_complete_and_exit(struct buffer *buffer)
740 minibuffer_taint_hist();
745 cmd_mini_previous_history_element(struct buffer *buffer)
747 if (ministate.history == NULL) {
748 message("No history");
752 if (ministate.hist_cur == NULL ||
753 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
754 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
755 ministate.hist_off = ministate.history->len - 1;
756 if (ministate.hist_cur == NULL)
757 message("No prev history item");
759 ministate.hist_off--;
762 if (ministate.hist_cur != NULL)
763 buffer->current_line->line = ministate.hist_cur->h;
767 cmd_mini_next_history_element(struct buffer *buffer)
769 if (ministate.history == NULL) {
770 message("No history");
774 if (ministate.hist_cur == NULL ||
775 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
776 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
777 ministate.hist_off = 0;
778 if (ministate.hist_cur == NULL)
779 message("No next history item");
781 ministate.hist_off++;
784 if (ministate.hist_cur != NULL)
785 buffer->current_line->line = ministate.hist_cur->h;
789 cmd_previous_completion(struct buffer *buffer)
791 if (in_minibuffer != MB_COMPREAD)
794 buffer = &ministate.compl.buffer;
796 if (buffer->current_line != NULL)
797 buffer->current_line->parent->type = LINE_COMPL;
799 forward_line(buffer, -1);
801 if (buffer->current_line != NULL)
802 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
806 cmd_next_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_insert_current_candidate(struct buffer *buffer)
827 if (in_minibuffer != MB_COMPREAD)
830 buffer = &ministate.compl.buffer;
831 if ((vl = buffer->current_line) == NULL)
834 minibuffer_taint_hist();
835 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
836 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
840 cmd_suspend_telescope(struct buffer *buffer)
847 cmd_toggle_pre_wrap(struct buffer *buffer)
849 dont_wrap_pre = !dont_wrap_pre;
852 message("Don't wrap preformatted blocks");
854 message("Wrap preformatted blocks");
856 ui_schedule_redraw();
860 cmd_mini_goto_beginning(struct buffer *buffer)
867 buffer = &ministate.compl.buffer;
869 if ((vl = buffer->current_line) != NULL)
870 vl->parent->type = LINE_COMPL;
872 vl = TAILQ_FIRST(&buffer->head);
873 while (vl != NULL && vl->parent->flags & L_HIDDEN)
874 vl = TAILQ_NEXT(vl, vlines);
879 vl->parent->type = LINE_COMPL_CURRENT;
880 buffer->top_line = vl;
881 buffer->current_line = vl;
885 cmd_mini_goto_end(struct buffer *buffer)
892 buffer = &ministate.compl.buffer;
894 if ((vl = buffer->current_line) != NULL)
895 vl->parent->type = LINE_COMPL;
897 vl = TAILQ_LAST(&buffer->head, vhead);
898 while (vl != NULL && vl->parent->flags & L_HIDDEN)
899 vl = TAILQ_PREV(vl, vhead, vlines);
904 vl->parent->type = LINE_COMPL_CURRENT;
905 buffer->current_line = vl;