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);
201 buffer->top_line = vl;
202 forward_line(buffer, -1);
207 cmd_scroll_down(struct buffer *buffer)
211 for (i = 0; i < body_lines; ++i) {
212 if (!forward_line(buffer, +1))
215 buffer->top_line = TAILQ_NEXT(buffer->top_line,
222 cmd_beginning_of_buffer(struct buffer *buffer)
224 buffer->current_line = TAILQ_FIRST(&buffer->head);
226 buffer->top_line = buffer->current_line;
227 buffer->line_off = 0;
231 cmd_end_of_buffer(struct buffer *buffer)
233 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
235 if (buffer->current_line == NULL)
238 /* deal with invisible lines */
239 if (buffer->current_line->parent->flags & L_HIDDEN)
240 forward_line(buffer, -1);
242 cmd_move_end_of_line(buffer);
246 cmd_kill_telescope(struct buffer *buffer)
253 cmd_push_button(struct buffer *buffer)
258 vl = buffer->current_line;
263 switch (vl->parent->type) {
265 load_url_in_tab(current_tab, vl->parent->alt);
268 l = TAILQ_NEXT(vl->parent, lines);
269 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
270 if (l->type == LINE_PRE_END)
272 l->flags ^= L_HIDDEN;
273 if (l->flags & L_HIDDEN)
285 cmd_push_button_new_tab(struct buffer *buffer)
289 vl = buffer->current_line;
290 if (vl == NULL || vl->parent->type != LINE_LINK)
293 new_tab(vl->parent->alt);
297 cmd_previous_button(struct buffer *buffer)
299 struct excursion place;
301 save_excursion(&place, buffer);
304 if (!forward_line(buffer, -1)) {
305 restore_excursion(&place, buffer);
306 message("No previous link");
309 } while (buffer->current_line->parent->type != LINE_LINK);
313 cmd_next_button(struct buffer *buffer)
315 struct excursion place;
317 save_excursion(&place, buffer);
320 if (!forward_line(buffer, +1)){
321 restore_excursion(&place, buffer);
322 message("No next link");
325 } while (buffer->current_line->parent->type != LINE_LINK);
329 is_heading(const struct line *l)
331 return l->type == LINE_TITLE_1 ||
332 l->type == LINE_TITLE_2 ||
333 l->type == LINE_TITLE_3;
337 cmd_previous_heading(struct buffer *buffer)
339 struct excursion place;
341 save_excursion(&place, buffer);
344 if (!forward_line(buffer, -1)) {
345 restore_excursion(&place, buffer);
346 message("No previous heading");
349 } while (!is_heading(buffer->current_line->parent));
353 cmd_next_heading(struct buffer *buffer)
355 struct excursion place;
357 save_excursion(&place, buffer);
360 if (!forward_line(buffer, +1)) {
361 restore_excursion(&place, buffer);
362 message("No next heading");
365 } while (!is_heading(buffer->current_line->parent));
369 cmd_previous_page(struct buffer *buffer)
371 if (!load_previous_page(current_tab))
372 message("No previous page");
374 start_loading_anim(current_tab);
378 cmd_next_page(struct buffer *buffer)
380 if (!load_next_page(current_tab))
381 message("No next page");
383 start_loading_anim(current_tab);
387 cmd_clear_minibuf(struct buffer *buffer)
393 cmd_execute_extended_command(struct buffer *buffer)
397 GUARD_RECURSIVE_MINIBUFFER();
399 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
400 &eecmd_history, compl_eecmd, NULL);
402 len = sizeof(ministate.prompt);
403 strlcpy(ministate.prompt, "", len);
406 strlcat(ministate.prompt, "M-", len);
408 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
411 strlcat(ministate.prompt, " ", len);
415 cmd_tab_close(struct buffer *buffer)
420 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
421 TAILQ_NEXT(tab, tabs) == NULL) {
422 message("Can't close the only tab.");
426 if (evtimer_pending(&tab->loadingev, NULL))
427 evtimer_del(&tab->loadingev);
431 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
432 t = TAILQ_NEXT(tab, tabs);
433 TAILQ_REMOVE(&tabshead, tab, tabs);
440 cmd_tab_close_other(struct buffer *buffer)
444 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
445 if (t == current_tab)
449 TAILQ_REMOVE(&tabshead, t, tabs);
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);
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");
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;