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->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 /* deal with invisible lines */
226 if (buffer->current_line->parent->flags & L_HIDDEN)
227 forward_line(buffer, -1);
229 cmd_move_end_of_line(buffer);
233 cmd_kill_telescope(struct buffer *buffer)
240 cmd_push_button(struct buffer *buffer)
245 vl = buffer->current_line;
246 switch (vl->parent->type) {
248 load_url_in_tab(current_tab, vl->parent->alt);
251 l = TAILQ_NEXT(vl->parent, lines);
252 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
253 if (l->type == LINE_PRE_END)
255 l->flags ^= L_HIDDEN;
256 if (l->flags & L_HIDDEN)
268 cmd_push_button_new_tab(struct buffer *buffer)
272 vl = buffer->current_line;
273 if (vl->parent->type != LINE_LINK)
276 new_tab(vl->parent->alt);
280 cmd_previous_button(struct buffer *buffer)
282 struct excursion place;
284 save_excursion(&place, buffer);
287 if (!forward_line(buffer, -1)) {
288 restore_excursion(&place, buffer);
289 message("No previous link");
292 } while (buffer->current_line->parent->type != LINE_LINK);
296 cmd_next_button(struct buffer *buffer)
298 struct excursion place;
300 save_excursion(&place, buffer);
303 if (!forward_line(buffer, +1)){
304 restore_excursion(&place, buffer);
305 message("No next link");
308 } while (buffer->current_line->parent->type != LINE_LINK);
312 is_heading(const struct line *l)
314 return l->type == LINE_TITLE_1 ||
315 l->type == LINE_TITLE_2 ||
316 l->type == LINE_TITLE_3;
320 cmd_previous_heading(struct buffer *buffer)
322 struct excursion place;
324 save_excursion(&place, buffer);
327 if (!forward_line(buffer, -1)) {
328 restore_excursion(&place, buffer);
329 message("No previous heading");
332 } while (!is_heading(buffer->current_line->parent));
336 cmd_next_heading(struct buffer *buffer)
338 struct excursion place;
340 save_excursion(&place, buffer);
343 if (!forward_line(buffer, +1)) {
344 restore_excursion(&place, buffer);
345 message("No next heading");
348 } while (!is_heading(buffer->current_line->parent));
352 cmd_previous_page(struct buffer *buffer)
354 if (!load_previous_page(current_tab))
355 message("No previous page");
357 start_loading_anim(current_tab);
361 cmd_next_page(struct buffer *buffer)
363 if (!load_next_page(current_tab))
364 message("No next page");
366 start_loading_anim(current_tab);
370 cmd_clear_minibuf(struct buffer *buffer)
376 cmd_execute_extended_command(struct buffer *buffer)
380 GUARD_RECURSIVE_MINIBUFFER();
382 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
383 &eecmd_history, compl_eecmd, NULL);
385 len = sizeof(ministate.prompt);
386 strlcpy(ministate.prompt, "", len);
389 strlcat(ministate.prompt, "M-", len);
391 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
394 strlcat(ministate.prompt, " ", len);
398 cmd_tab_close(struct buffer *buffer)
403 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
404 TAILQ_NEXT(tab, tabs) == NULL) {
405 message("Can't close the only tab.");
409 if (evtimer_pending(&tab->loadingev, NULL))
410 evtimer_del(&tab->loadingev);
414 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
415 t = TAILQ_NEXT(tab, tabs);
416 TAILQ_REMOVE(&tabshead, tab, tabs);
423 cmd_tab_close_other(struct buffer *buffer)
427 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
428 if (t == current_tab)
432 TAILQ_REMOVE(&tabshead, t, tabs);
438 cmd_tab_new(struct buffer *buffer)
442 if ((url = new_tab_url) == NULL)
449 cmd_tab_next(struct buffer *buffer)
453 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
454 t = TAILQ_FIRST(&tabshead);
459 cmd_tab_previous(struct buffer *buffer)
463 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
464 t = TAILQ_LAST(&tabshead, tabshead);
469 cmd_tab_move(struct buffer *buffer)
473 t = TAILQ_NEXT(current_tab, tabs);
474 TAILQ_REMOVE(&tabshead, current_tab, tabs);
477 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
479 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
483 cmd_tab_move_to(struct buffer *buffer)
487 t = TAILQ_PREV(current_tab, tabshead, tabs);
488 TAILQ_REMOVE(&tabshead, current_tab, tabs);
491 if (TAILQ_EMPTY(&tabshead))
492 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
494 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
496 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
500 cmd_tab_select(struct buffer *buffer)
502 GUARD_RECURSIVE_MINIBUFFER();
504 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
505 NULL, compl_ts, NULL);
506 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
510 cmd_load_url(struct buffer *buffer)
512 GUARD_RECURSIVE_MINIBUFFER();
514 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
515 &lu_history, NULL, NULL);
516 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
517 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
518 cmd_move_end_of_line(&ministate.buffer);
522 cmd_load_current_url(struct buffer *buffer)
524 GUARD_RECURSIVE_MINIBUFFER();
526 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
527 &lu_history, NULL, NULL);
528 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
529 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
530 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
534 cmd_reload_page(struct buffer *buffer)
536 load_url_in_tab(current_tab, current_tab->hist_cur->h);
540 cmd_bookmark_page(struct buffer *buffer)
542 GUARD_RECURSIVE_MINIBUFFER();
544 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
546 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
547 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
548 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
552 cmd_list_bookmarks(struct buffer *buffer)
554 load_url_in_tab(current_tab, "about:bookmarks");
558 cmd_toggle_help(struct buffer *buffer)
560 ui_toggle_side_window();
564 cmd_link_select(struct buffer *buffer)
568 GUARD_RECURSIVE_MINIBUFFER();
570 l = TAILQ_FIRST(&buffer->page.head);
571 while (l != NULL && l->type != LINE_LINK)
572 l = TAILQ_NEXT(l, lines);
575 message("No links found");
579 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
581 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
585 cmd_swiper(struct buffer *buffer)
587 GUARD_RECURSIVE_MINIBUFFER();
589 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
590 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
591 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
595 cmd_toc(struct buffer *buffer)
599 GUARD_RECURSIVE_MINIBUFFER();
601 l = TAILQ_FIRST(&buffer->page.head);
603 l->type != LINE_TITLE_1 &&
604 l->type != LINE_TITLE_2 &&
605 l->type != LINE_TITLE_3)
606 l = TAILQ_NEXT(l, lines);
609 message("No headings found");
613 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
615 strlcpy(ministate.prompt, "Select heading: ",
616 sizeof(ministate.prompt));
620 cmd_inc_fill_column(struct buffer *buffer)
622 if (fill_column == INT_MAX)
626 message("fill-column: %d", fill_column);
628 ui_schedule_redraw();
632 cmd_dec_fill_column(struct buffer *buffer)
634 if (fill_column == INT_MAX || fill_column < 8)
638 message("fill-column: %d", fill_column);
640 ui_schedule_redraw();
644 cmd_olivetti_mode(struct buffer *buffer)
646 olivetti_mode = !olivetti_mode;
648 message("olivetti-mode enabled");
650 message("olivetti-mode disabled");
652 ui_schedule_redraw();
656 cmd_mini_delete_char(struct buffer *buffer)
660 if (!in_minibuffer) {
661 message("text is read-only");
665 minibuffer_taint_hist();
667 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
672 memmove(c, n, strlen(n)+1);
674 recompute_completions(0);
678 cmd_mini_delete_backward_char(struct buffer *buffer)
682 if (!in_minibuffer) {
683 message("text is read-only");
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)
706 if (!in_minibuffer) {
707 message("text is read-only");
711 minibuffer_taint_hist();
712 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
715 recompute_completions(0);
719 cmd_mini_abort(struct buffer *buffer)
728 cmd_mini_complete_and_exit(struct buffer *buffer)
733 minibuffer_taint_hist();
738 cmd_mini_previous_history_element(struct buffer *buffer)
740 if (ministate.history == NULL) {
741 message("No history");
745 if (ministate.hist_cur == NULL ||
746 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
747 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
748 ministate.hist_off = ministate.history->len - 1;
749 if (ministate.hist_cur == NULL)
750 message("No prev history item");
752 ministate.hist_off--;
755 if (ministate.hist_cur != NULL)
756 buffer->current_line->line = ministate.hist_cur->h;
760 cmd_mini_next_history_element(struct buffer *buffer)
762 if (ministate.history == NULL) {
763 message("No history");
767 if (ministate.hist_cur == NULL ||
768 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
769 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
770 ministate.hist_off = 0;
771 if (ministate.hist_cur == NULL)
772 message("No next history item");
774 ministate.hist_off++;
777 if (ministate.hist_cur != NULL)
778 buffer->current_line->line = ministate.hist_cur->h;
782 cmd_previous_completion(struct buffer *buffer)
784 if (in_minibuffer != MB_COMPREAD)
787 buffer = &ministate.compl.buffer;
789 if (buffer->current_line != NULL)
790 buffer->current_line->parent->type = LINE_COMPL;
792 forward_line(buffer, -1);
794 if (buffer->current_line != NULL)
795 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
799 cmd_next_completion(struct buffer *buffer)
801 if (in_minibuffer != MB_COMPREAD)
804 buffer = &ministate.compl.buffer;
806 if (buffer->current_line != NULL)
807 buffer->current_line->parent->type = LINE_COMPL;
809 forward_line(buffer, +1);
811 if (buffer->current_line != NULL)
812 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
816 cmd_insert_current_candidate(struct buffer *buffer)
820 if (in_minibuffer != MB_COMPREAD)
823 buffer = &ministate.compl.buffer;
824 if ((vl = buffer->current_line) == NULL)
827 minibuffer_taint_hist();
828 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
829 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
833 cmd_suspend_telescope(struct buffer *buffer)
840 cmd_toggle_pre_wrap(struct buffer *buffer)
842 dont_wrap_pre = !dont_wrap_pre;
845 message("Don't wrap preformatted blocks");
847 message("Wrap preformatted blocks");
849 ui_schedule_redraw();
853 cmd_mini_goto_beginning(struct buffer *buffer)
860 buffer = &ministate.compl.buffer;
862 if ((vl = buffer->current_line) != NULL)
863 vl->parent->type = LINE_COMPL;
865 vl = TAILQ_FIRST(&buffer->head);
866 while (vl != NULL && vl->parent->flags & L_HIDDEN)
867 vl = TAILQ_NEXT(vl, vlines);
872 vl->parent->type = LINE_COMPL_CURRENT;
873 buffer->top_line = vl;
874 buffer->current_line = vl;
878 cmd_mini_goto_end(struct buffer *buffer)
885 buffer = &ministate.compl.buffer;
887 if ((vl = buffer->current_line) != NULL)
888 vl->parent->type = LINE_COMPL;
890 vl = TAILQ_LAST(&buffer->head, vhead);
891 while (vl != NULL && vl->parent->flags & L_HIDDEN)
892 vl = TAILQ_PREV(vl, vhead, vlines);
897 vl->parent->type = LINE_COMPL_CURRENT;
898 buffer->current_line = vl;