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 #define GUARD_READ_ONLY() \
39 if (!in_minibuffer) { \
40 message("text is read-only"); \
45 /* return 1 if moved, 0 otherwise */
47 forward_line(struct buffer *buffer, int n)
52 if (buffer->current_line == NULL)
54 vl = buffer->current_line;
59 vl = TAILQ_NEXT(vl, vlines);
62 if (vl->parent->flags & L_HIDDEN)
64 buffer->current_line = vl;
67 vl = TAILQ_PREV(vl, vhead, vlines);
70 if (vl->parent->flags & L_HIDDEN)
72 if (buffer->current_line == buffer->top_line) {
74 buffer->top_line = vl;
76 buffer->current_line = vl;
87 cmd_previous_line(struct buffer *buffer)
89 forward_line(buffer, -1);
93 cmd_next_line(struct buffer *buffer)
95 forward_line(buffer, +1);
99 cmd_backward_char(struct buffer *buffer)
101 if (buffer->cpoff != 0)
106 cmd_forward_char(struct buffer *buffer)
110 if (buffer->current_line == NULL)
113 if (buffer->current_line->line != NULL)
114 len = utf8_cplen(buffer->current_line->line);
115 if (++buffer->cpoff > len)
120 cmd_backward_paragraph(struct buffer *buffer)
123 if (!forward_line(buffer, -1)) {
124 message("No previous paragraph");
127 } while (buffer->current_line->line != NULL ||
128 buffer->current_line->parent->type != LINE_TEXT);
132 cmd_forward_paragraph(struct buffer *buffer)
135 if (!forward_line(buffer, +1)) {
136 message("No next paragraph");
139 } while (buffer->current_line->line != NULL ||
140 buffer->current_line->parent->type != LINE_TEXT);
144 cmd_move_beginning_of_line(struct buffer *buffer)
150 cmd_move_end_of_line(struct buffer *buffer)
154 vl = buffer->current_line;
155 if (vl == NULL || vl->line == NULL)
157 buffer->cpoff = utf8_cplen(vl->line);
161 cmd_redraw(struct buffer *buffer)
163 ui_schedule_redraw();
167 cmd_scroll_line_up(struct buffer *buffer)
171 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
175 forward_line(buffer, -1);
177 buffer->top_line = vl;
181 cmd_scroll_line_down(struct buffer *buffer)
183 if (!forward_line(buffer, +1))
186 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
191 cmd_scroll_up(struct buffer *buffer)
196 for (i = 0; i < body_lines; ++i) {
197 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
200 buffer->top_line = vl;
201 forward_line(buffer, -1);
206 cmd_scroll_down(struct buffer *buffer)
210 for (i = 0; i < body_lines; ++i) {
211 if (!forward_line(buffer, +1))
214 buffer->top_line = TAILQ_NEXT(buffer->top_line,
220 cmd_beginning_of_buffer(struct buffer *buffer)
222 buffer->current_line = TAILQ_FIRST(&buffer->head);
224 buffer->top_line = buffer->current_line;
225 buffer->line_off = 0;
229 cmd_end_of_buffer(struct buffer *buffer)
231 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
233 if (buffer->current_line == NULL)
236 /* deal with invisible lines */
237 if (buffer->current_line->parent->flags & L_HIDDEN)
238 forward_line(buffer, -1);
240 cmd_move_end_of_line(buffer);
244 cmd_kill_telescope(struct buffer *buffer)
251 cmd_push_button(struct buffer *buffer)
256 vl = buffer->current_line;
261 switch (vl->parent->type) {
263 load_url_in_tab(current_tab, vl->parent->alt);
266 l = TAILQ_NEXT(vl->parent, lines);
267 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
268 if (l->type == LINE_PRE_END)
270 l->flags ^= L_HIDDEN;
271 if (l->flags & L_HIDDEN)
283 cmd_push_button_new_tab(struct buffer *buffer)
287 vl = buffer->current_line;
288 if (vl == NULL || vl->parent->type != LINE_LINK)
291 new_tab(vl->parent->alt);
295 cmd_previous_button(struct buffer *buffer)
297 struct excursion place;
299 save_excursion(&place, buffer);
302 if (!forward_line(buffer, -1)) {
303 restore_excursion(&place, buffer);
304 message("No previous link");
307 } while (buffer->current_line->parent->type != LINE_LINK);
311 cmd_next_button(struct buffer *buffer)
313 struct excursion place;
315 save_excursion(&place, buffer);
318 if (!forward_line(buffer, +1)){
319 restore_excursion(&place, buffer);
320 message("No next link");
323 } while (buffer->current_line->parent->type != LINE_LINK);
327 is_heading(const struct line *l)
329 return l->type == LINE_TITLE_1 ||
330 l->type == LINE_TITLE_2 ||
331 l->type == LINE_TITLE_3;
335 cmd_previous_heading(struct buffer *buffer)
337 struct excursion place;
339 save_excursion(&place, buffer);
342 if (!forward_line(buffer, -1)) {
343 restore_excursion(&place, buffer);
344 message("No previous heading");
347 } while (!is_heading(buffer->current_line->parent));
351 cmd_next_heading(struct buffer *buffer)
353 struct excursion place;
355 save_excursion(&place, buffer);
358 if (!forward_line(buffer, +1)) {
359 restore_excursion(&place, buffer);
360 message("No next heading");
363 } while (!is_heading(buffer->current_line->parent));
367 cmd_previous_page(struct buffer *buffer)
369 if (!load_previous_page(current_tab))
370 message("No previous page");
372 start_loading_anim(current_tab);
376 cmd_next_page(struct buffer *buffer)
378 if (!load_next_page(current_tab))
379 message("No next page");
381 start_loading_anim(current_tab);
385 cmd_clear_minibuf(struct buffer *buffer)
391 cmd_execute_extended_command(struct buffer *buffer)
395 GUARD_RECURSIVE_MINIBUFFER();
397 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
398 &eecmd_history, compl_eecmd, NULL);
400 len = sizeof(ministate.prompt);
401 strlcpy(ministate.prompt, "", len);
404 strlcat(ministate.prompt, "M-", len);
406 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
409 strlcat(ministate.prompt, " ", len);
413 cmd_tab_close(struct buffer *buffer)
418 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
419 TAILQ_NEXT(tab, tabs) == NULL) {
420 message("Can't close the only tab.");
424 if (evtimer_pending(&tab->loadingev, NULL))
425 evtimer_del(&tab->loadingev);
429 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
430 t = TAILQ_NEXT(tab, tabs);
431 TAILQ_REMOVE(&tabshead, tab, tabs);
438 cmd_tab_close_other(struct buffer *buffer)
442 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
443 if (t == current_tab)
447 TAILQ_REMOVE(&tabshead, t, tabs);
453 cmd_tab_new(struct buffer *buffer)
457 if ((url = new_tab_url) == NULL)
464 cmd_tab_next(struct buffer *buffer)
468 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
469 t = TAILQ_FIRST(&tabshead);
474 cmd_tab_previous(struct buffer *buffer)
478 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
479 t = TAILQ_LAST(&tabshead, tabshead);
484 cmd_tab_move(struct buffer *buffer)
488 t = TAILQ_NEXT(current_tab, tabs);
489 TAILQ_REMOVE(&tabshead, current_tab, tabs);
492 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
494 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
498 cmd_tab_move_to(struct buffer *buffer)
502 t = TAILQ_PREV(current_tab, tabshead, tabs);
503 TAILQ_REMOVE(&tabshead, current_tab, tabs);
506 if (TAILQ_EMPTY(&tabshead))
507 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
509 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
511 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
515 cmd_tab_select(struct buffer *buffer)
517 GUARD_RECURSIVE_MINIBUFFER();
519 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
520 NULL, compl_ts, NULL);
521 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
525 cmd_load_url(struct buffer *buffer)
527 GUARD_RECURSIVE_MINIBUFFER();
529 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
530 &lu_history, NULL, NULL);
531 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
532 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
533 cmd_move_end_of_line(&ministate.buffer);
537 cmd_load_current_url(struct buffer *buffer)
539 GUARD_RECURSIVE_MINIBUFFER();
541 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
542 &lu_history, NULL, NULL);
543 strlcpy(ministate.prompt, "Load 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_reload_page(struct buffer *buffer)
551 load_url_in_tab(current_tab, current_tab->hist_cur->h);
555 cmd_bookmark_page(struct buffer *buffer)
557 GUARD_RECURSIVE_MINIBUFFER();
559 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
561 strlcpy(ministate.prompt, "Bookmark 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_list_bookmarks(struct buffer *buffer)
569 load_url_in_tab(current_tab, "about:bookmarks");
573 cmd_toggle_help(struct buffer *buffer)
575 ui_toggle_side_window();
579 cmd_link_select(struct buffer *buffer)
583 GUARD_RECURSIVE_MINIBUFFER();
585 l = TAILQ_FIRST(&buffer->page.head);
586 while (l != NULL && l->type != LINE_LINK)
587 l = TAILQ_NEXT(l, lines);
590 message("No links found");
594 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
596 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
600 cmd_swiper(struct buffer *buffer)
602 GUARD_RECURSIVE_MINIBUFFER();
604 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
605 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
606 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
610 cmd_toc(struct buffer *buffer)
614 GUARD_RECURSIVE_MINIBUFFER();
616 l = TAILQ_FIRST(&buffer->page.head);
618 l->type != LINE_TITLE_1 &&
619 l->type != LINE_TITLE_2 &&
620 l->type != LINE_TITLE_3)
621 l = TAILQ_NEXT(l, lines);
624 message("No headings found");
628 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
630 strlcpy(ministate.prompt, "Select heading: ",
631 sizeof(ministate.prompt));
635 cmd_inc_fill_column(struct buffer *buffer)
637 if (fill_column == INT_MAX)
641 message("fill-column: %d", fill_column);
643 ui_schedule_redraw();
647 cmd_dec_fill_column(struct buffer *buffer)
649 if (fill_column == INT_MAX || fill_column < 8)
653 message("fill-column: %d", fill_column);
655 ui_schedule_redraw();
659 cmd_olivetti_mode(struct buffer *buffer)
661 olivetti_mode = !olivetti_mode;
663 message("olivetti-mode enabled");
665 message("olivetti-mode disabled");
667 ui_schedule_redraw();
671 cmd_mini_delete_char(struct buffer *buffer)
677 minibuffer_taint_hist();
679 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
684 memmove(c, n, strlen(n)+1);
686 recompute_completions(0);
690 cmd_mini_delete_backward_char(struct buffer *buffer)
696 minibuffer_taint_hist();
698 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
699 start = buffer->current_line->line;
702 p = utf8_prev_cp(c-1, start);
704 memmove(p, c, strlen(c)+1);
707 recompute_completions(0);
711 cmd_mini_kill_line(struct buffer *buffer)
717 minibuffer_taint_hist();
718 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
721 recompute_completions(0);
725 cmd_mini_abort(struct buffer *buffer)
734 cmd_mini_complete_and_exit(struct buffer *buffer)
739 minibuffer_taint_hist();
744 cmd_mini_previous_history_element(struct buffer *buffer)
746 if (ministate.history == NULL) {
747 message("No history");
751 if (ministate.hist_cur == NULL ||
752 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
753 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
754 ministate.hist_off = ministate.history->len - 1;
755 if (ministate.hist_cur == NULL)
756 message("No prev history item");
758 ministate.hist_off--;
761 if (ministate.hist_cur != NULL)
762 buffer->current_line->line = ministate.hist_cur->h;
766 cmd_mini_next_history_element(struct buffer *buffer)
768 if (ministate.history == NULL) {
769 message("No history");
773 if (ministate.hist_cur == NULL ||
774 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
775 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
776 ministate.hist_off = 0;
777 if (ministate.hist_cur == NULL)
778 message("No next history item");
780 ministate.hist_off++;
783 if (ministate.hist_cur != NULL)
784 buffer->current_line->line = ministate.hist_cur->h;
788 cmd_previous_completion(struct buffer *buffer)
790 if (in_minibuffer != MB_COMPREAD)
793 buffer = &ministate.compl.buffer;
795 if (buffer->current_line != NULL)
796 buffer->current_line->parent->type = LINE_COMPL;
798 forward_line(buffer, -1);
800 if (buffer->current_line != NULL)
801 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
805 cmd_next_completion(struct buffer *buffer)
807 if (in_minibuffer != MB_COMPREAD)
810 buffer = &ministate.compl.buffer;
812 if (buffer->current_line != NULL)
813 buffer->current_line->parent->type = LINE_COMPL;
815 forward_line(buffer, +1);
817 if (buffer->current_line != NULL)
818 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
822 cmd_insert_current_candidate(struct buffer *buffer)
826 if (in_minibuffer != MB_COMPREAD)
829 buffer = &ministate.compl.buffer;
830 if ((vl = buffer->current_line) == NULL)
833 minibuffer_taint_hist();
834 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
835 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
839 cmd_suspend_telescope(struct buffer *buffer)
846 cmd_toggle_pre_wrap(struct buffer *buffer)
848 dont_wrap_pre = !dont_wrap_pre;
851 message("Don't wrap preformatted blocks");
853 message("Wrap preformatted blocks");
855 ui_schedule_redraw();
859 cmd_mini_goto_beginning(struct buffer *buffer)
866 buffer = &ministate.compl.buffer;
868 if ((vl = buffer->current_line) != NULL)
869 vl->parent->type = LINE_COMPL;
871 vl = TAILQ_FIRST(&buffer->head);
872 while (vl != NULL && vl->parent->flags & L_HIDDEN)
873 vl = TAILQ_NEXT(vl, vlines);
878 vl->parent->type = LINE_COMPL_CURRENT;
879 buffer->top_line = vl;
880 buffer->current_line = vl;
884 cmd_mini_goto_end(struct buffer *buffer)
891 buffer = &ministate.compl.buffer;
893 if ((vl = buffer->current_line) != NULL)
894 vl->parent->type = LINE_COMPL;
896 vl = TAILQ_LAST(&buffer->head, vhead);
897 while (vl != NULL && vl->parent->flags & L_HIDDEN)
898 vl = TAILQ_PREV(vl, vhead, vlines);
903 vl->parent->type = LINE_COMPL_CURRENT;
904 buffer->current_line = vl;