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, NULL);
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, current_tab->hist_cur->h);
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)
421 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
422 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
426 message("Can't close the only tab.");
431 cmd_tab_close_other(struct buffer *buffer)
435 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
436 if (t == current_tab)
444 cmd_tab_new(struct buffer *buffer)
448 if ((url = new_tab_url) == NULL)
455 cmd_tab_next(struct buffer *buffer)
459 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
460 t = TAILQ_FIRST(&tabshead);
465 cmd_tab_previous(struct buffer *buffer)
469 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
470 t = TAILQ_LAST(&tabshead, tabshead);
475 cmd_tab_move(struct buffer *buffer)
479 t = TAILQ_NEXT(current_tab, tabs);
480 TAILQ_REMOVE(&tabshead, current_tab, tabs);
483 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
485 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
489 cmd_tab_move_to(struct buffer *buffer)
493 t = TAILQ_PREV(current_tab, tabshead, tabs);
494 TAILQ_REMOVE(&tabshead, current_tab, tabs);
497 if (TAILQ_EMPTY(&tabshead))
498 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
500 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
502 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
506 cmd_tab_select(struct buffer *buffer)
508 GUARD_RECURSIVE_MINIBUFFER();
510 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
511 NULL, compl_ts, NULL);
512 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
516 cmd_load_url(struct buffer *buffer)
518 GUARD_RECURSIVE_MINIBUFFER();
520 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
521 &lu_history, NULL, NULL);
522 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
523 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
524 cmd_move_end_of_line(&ministate.buffer);
528 cmd_load_current_url(struct buffer *buffer)
530 GUARD_RECURSIVE_MINIBUFFER();
532 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
533 &lu_history, NULL, NULL);
534 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
535 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
536 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
540 cmd_reload_page(struct buffer *buffer)
542 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL);
546 cmd_bookmark_page(struct buffer *buffer)
548 GUARD_RECURSIVE_MINIBUFFER();
550 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
552 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
553 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
554 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
558 cmd_list_bookmarks(struct buffer *buffer)
560 load_url_in_tab(current_tab, "about:bookmarks", NULL);
564 cmd_toggle_help(struct buffer *buffer)
566 ui_toggle_side_window();
570 cmd_link_select(struct buffer *buffer)
574 GUARD_RECURSIVE_MINIBUFFER();
576 l = TAILQ_FIRST(&buffer->page.head);
577 while (l != NULL && l->type != LINE_LINK)
578 l = TAILQ_NEXT(l, lines);
581 message("No links found");
585 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
587 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
591 cmd_swiper(struct buffer *buffer)
593 GUARD_RECURSIVE_MINIBUFFER();
595 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
596 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
597 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
601 cmd_toc(struct buffer *buffer)
605 GUARD_RECURSIVE_MINIBUFFER();
607 l = TAILQ_FIRST(&buffer->page.head);
609 l->type != LINE_TITLE_1 &&
610 l->type != LINE_TITLE_2 &&
611 l->type != LINE_TITLE_3)
612 l = TAILQ_NEXT(l, lines);
615 message("No headings found");
619 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
621 strlcpy(ministate.prompt, "Select heading: ",
622 sizeof(ministate.prompt));
626 cmd_inc_fill_column(struct buffer *buffer)
628 if (fill_column == INT_MAX)
632 message("fill-column: %d", fill_column);
634 ui_schedule_redraw();
638 cmd_dec_fill_column(struct buffer *buffer)
640 if (fill_column == INT_MAX || fill_column < 8)
644 message("fill-column: %d", fill_column);
646 ui_schedule_redraw();
650 cmd_olivetti_mode(struct buffer *buffer)
652 olivetti_mode = !olivetti_mode;
654 message("olivetti-mode enabled");
656 message("olivetti-mode disabled");
658 ui_schedule_redraw();
662 cmd_mini_delete_char(struct buffer *buffer)
668 minibuffer_taint_hist();
670 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
675 memmove(c, n, strlen(n)+1);
677 recompute_completions(0);
681 cmd_mini_delete_backward_char(struct buffer *buffer)
687 minibuffer_taint_hist();
689 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
690 start = buffer->current_line->line;
693 p = utf8_prev_cp(c-1, start);
695 memmove(p, c, strlen(c)+1);
698 recompute_completions(0);
702 cmd_mini_kill_line(struct buffer *buffer)
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;