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->line != NULL)
103 len = utf8_cplen(buffer->current_line->line);
104 if (++buffer->cpoff > len)
109 cmd_backward_paragraph(struct buffer *buffer)
112 if (!forward_line(buffer, -1)) {
113 message("No previous paragraph");
116 } while (buffer->current_line->line != NULL ||
117 buffer->current_line->parent->type != LINE_TEXT);
121 cmd_forward_paragraph(struct buffer *buffer)
124 if (!forward_line(buffer, +1)) {
125 message("No next paragraph");
128 } while (buffer->current_line->line != NULL ||
129 buffer->current_line->parent->type != LINE_TEXT);
133 cmd_move_beginning_of_line(struct buffer *buffer)
139 cmd_move_end_of_line(struct buffer *buffer)
143 vl = buffer->current_line;
144 if (vl->line == NULL)
146 buffer->cpoff = utf8_cplen(vl->line);
150 cmd_redraw(struct buffer *buffer)
152 ui_schedule_redraw();
156 cmd_scroll_line_up(struct buffer *buffer)
160 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
164 forward_line(buffer, -1);
166 buffer->top_line = vl;
170 cmd_scroll_line_down(struct buffer *buffer)
172 if (!forward_line(buffer, +1))
175 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
180 cmd_scroll_up(struct buffer *buffer)
182 forward_line(buffer, -1*body_lines);
186 cmd_scroll_down(struct buffer *buffer)
188 forward_line(buffer, body_lines);
192 cmd_beginning_of_buffer(struct buffer *buffer)
194 buffer->current_line = TAILQ_FIRST(&buffer->head);
196 buffer->top_line = buffer->current_line;
197 buffer->line_off = 0;
201 cmd_end_of_buffer(struct buffer *buffer)
203 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
204 buffer->cpoff = body_cols;
208 cmd_kill_telescope(struct buffer *buffer)
215 cmd_push_button(struct buffer *buffer)
220 vl = buffer->current_line;
221 switch (vl->parent->type) {
223 load_url_in_tab(current_tab(), vl->parent->meta.alt);
226 l = TAILQ_NEXT(vl->parent, lines);
227 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
228 if (l->type == LINE_PRE_END)
230 l->flags ^= L_HIDDEN;
231 if (l->flags & L_HIDDEN)
243 cmd_push_button_new_tab(struct buffer *buffer)
247 vl = buffer->current_line;
248 if (vl->parent->type != LINE_LINK)
251 new_tab(vl->parent->meta.alt);
255 cmd_previous_button(struct buffer *buffer)
257 struct excursion place;
259 save_excursion(&place, buffer);
262 if (!forward_line(buffer, -1)) {
263 restore_excursion(&place, buffer);
264 message("No previous link");
267 } while (buffer->current_line->parent->type != LINE_LINK);
271 cmd_next_button(struct buffer *buffer)
273 struct excursion place;
275 save_excursion(&place, buffer);
278 if (!forward_line(buffer, +1)){
279 restore_excursion(&place, buffer);
280 message("No next link");
283 } while (buffer->current_line->parent->type != LINE_LINK);
287 is_heading(const struct line *l)
289 return l->type == LINE_TITLE_1 ||
290 l->type == LINE_TITLE_2 ||
291 l->type == LINE_TITLE_3;
295 cmd_previous_heading(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 heading");
307 } while (!is_heading(buffer->current_line->parent));
311 cmd_next_heading(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 heading");
323 } while (!is_heading(buffer->current_line->parent));
327 cmd_previous_page(struct buffer *buffer)
329 struct tab *tab = current_tab();
331 if (!load_previous_page(tab))
332 message("No previous page");
334 start_loading_anim(tab);
338 cmd_next_page(struct buffer *buffer)
340 struct tab *tab = current_tab();
342 if (!load_next_page(tab))
343 message("No next page");
345 start_loading_anim(tab);
349 cmd_clear_minibuf(struct buffer *buffer)
355 cmd_execute_extended_command(struct buffer *buffer)
359 GUARD_RECURSIVE_MINIBUFFER();
361 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
362 &eecmd_history, compl_eecmd, NULL);
364 len = sizeof(ministate.prompt);
365 strlcpy(ministate.prompt, "", len);
368 strlcat(ministate.prompt, "M-", len);
370 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
373 strlcat(ministate.prompt, " ", len);
377 cmd_tab_close(struct buffer *buffer)
382 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
383 TAILQ_NEXT(tab, tabs) == NULL) {
384 message("Can't close the only tab.");
388 if (evtimer_pending(&tab->loadingev, NULL))
389 evtimer_del(&tab->loadingev);
393 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
394 t = TAILQ_NEXT(tab, tabs);
395 TAILQ_REMOVE(&tabshead, tab, tabs);
402 cmd_tab_close_other(struct buffer *buffer)
406 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
407 if (t->flags & TAB_CURRENT)
411 TAILQ_REMOVE(&tabshead, t, tabs);
417 cmd_tab_new(struct buffer *buffer)
421 if ((url = new_tab_url) == NULL)
428 cmd_tab_next(struct buffer *buffer)
433 tab->flags &= ~TAB_CURRENT;
435 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
436 t = TAILQ_FIRST(&tabshead);
437 t->flags |= TAB_CURRENT;
438 t->flags &= ~TAB_URGENT;
442 cmd_tab_previous(struct buffer *buffer)
447 tab->flags &= ~TAB_CURRENT;
449 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
450 t = TAILQ_LAST(&tabshead, tabshead);
451 t->flags |= TAB_CURRENT;
452 t->flags &= ~TAB_URGENT;
456 cmd_tab_move(struct buffer *buffer)
461 t = TAILQ_NEXT(tab, tabs);
462 TAILQ_REMOVE(&tabshead, tab, tabs);
465 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
467 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
471 cmd_tab_move_to(struct buffer *buffer)
476 t = TAILQ_PREV(tab, tabshead, tabs);
477 TAILQ_REMOVE(&tabshead, tab, tabs);
480 if (TAILQ_EMPTY(&tabshead))
481 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
483 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
485 TAILQ_INSERT_BEFORE(t, tab, tabs);
489 cmd_tab_select(struct buffer *buffer)
491 GUARD_RECURSIVE_MINIBUFFER();
493 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
494 NULL, compl_ts, NULL);
495 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
499 cmd_load_url(struct buffer *buffer)
501 GUARD_RECURSIVE_MINIBUFFER();
503 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
504 &lu_history, NULL, NULL);
505 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
506 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
507 cmd_move_end_of_line(&ministate.buffer);
511 cmd_load_current_url(struct buffer *buffer)
513 struct tab *tab = current_tab();
515 GUARD_RECURSIVE_MINIBUFFER();
517 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
518 &lu_history, NULL, NULL);
519 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
520 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
521 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
525 cmd_reload_page(struct buffer *buffer)
530 load_url_in_tab(tab, tab->hist_cur->h);
534 cmd_bookmark_page(struct buffer *buffer)
536 struct tab *tab = current_tab();
538 GUARD_RECURSIVE_MINIBUFFER();
540 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
542 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
543 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
544 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
548 cmd_list_bookmarks(struct buffer *buffer)
550 load_url_in_tab(current_tab(), "about:bookmarks");
554 cmd_toggle_help(struct buffer *buffer)
556 ui_toggle_side_window();
560 cmd_link_select(struct buffer *buffer)
564 GUARD_RECURSIVE_MINIBUFFER();
566 l = TAILQ_FIRST(&buffer->page.head);
567 while (l != NULL && l->type != LINE_LINK)
568 l = TAILQ_NEXT(l, lines);
571 message("No links found");
575 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
577 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
581 cmd_swiper(struct buffer *buffer)
583 GUARD_RECURSIVE_MINIBUFFER();
585 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
586 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
587 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
591 cmd_toc(struct buffer *buffer)
595 GUARD_RECURSIVE_MINIBUFFER();
597 l = TAILQ_FIRST(&buffer->page.head);
599 l->type != LINE_TITLE_1 &&
600 l->type != LINE_TITLE_2 &&
601 l->type != LINE_TITLE_3)
602 l = TAILQ_NEXT(l, lines);
605 message("No headings found");
609 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
611 strlcpy(ministate.prompt, "Select heading: ",
612 sizeof(ministate.prompt));
616 cmd_inc_fill_column(struct buffer *buffer)
618 if (fill_column == INT_MAX)
622 message("fill-column: %d", fill_column);
624 ui_schedule_redraw();
628 cmd_dec_fill_column(struct buffer *buffer)
630 if (fill_column == INT_MAX || fill_column < 8)
634 message("fill-column: %d", fill_column);
636 ui_schedule_redraw();
640 cmd_olivetti_mode(struct buffer *buffer)
642 olivetti_mode = !olivetti_mode;
644 message("olivetti-mode enabled");
646 message("olivetti-mode disabled");
648 ui_schedule_redraw();
652 cmd_mini_delete_char(struct buffer *buffer)
656 if (!in_minibuffer) {
657 message("text is read-only");
661 minibuffer_taint_hist();
663 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
668 memmove(c, n, strlen(n)+1);
670 recompute_completions(0);
674 cmd_mini_delete_backward_char(struct buffer *buffer)
678 if (!in_minibuffer) {
679 message("text is read-only");
683 minibuffer_taint_hist();
685 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
686 start = buffer->current_line->line;
689 p = utf8_prev_cp(c-1, start);
691 memmove(p, c, strlen(c)+1);
694 recompute_completions(0);
698 cmd_mini_kill_line(struct buffer *buffer)
702 if (!in_minibuffer) {
703 message("text is read-only");
707 minibuffer_taint_hist();
708 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
711 recompute_completions(0);
715 cmd_mini_abort(struct buffer *buffer)
724 cmd_mini_complete_and_exit(struct buffer *buffer)
729 minibuffer_taint_hist();
734 cmd_mini_previous_history_element(struct buffer *buffer)
736 if (ministate.history == NULL) {
737 message("No history");
741 if (ministate.hist_cur == NULL ||
742 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
743 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
744 ministate.hist_off = ministate.history->len - 1;
745 if (ministate.hist_cur == NULL)
746 message("No prev history item");
748 ministate.hist_off--;
751 if (ministate.hist_cur != NULL)
752 buffer->current_line->line = ministate.hist_cur->h;
756 cmd_mini_next_history_element(struct buffer *buffer)
758 if (ministate.history == NULL) {
759 message("No history");
763 if (ministate.hist_cur == NULL ||
764 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
765 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
766 ministate.hist_off = 0;
767 if (ministate.hist_cur == NULL)
768 message("No next history item");
770 ministate.hist_off++;
773 if (ministate.hist_cur != NULL)
774 buffer->current_line->line = ministate.hist_cur->h;
778 cmd_previous_completion(struct buffer *buffer)
780 if (in_minibuffer != MB_COMPREAD)
783 buffer = &ministate.compl.buffer;
785 if (buffer->current_line != NULL)
786 buffer->current_line->parent->type = LINE_COMPL;
788 forward_line(buffer, -1);
790 if (buffer->current_line != NULL)
791 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
795 cmd_next_completion(struct buffer *buffer)
797 if (in_minibuffer != MB_COMPREAD)
800 buffer = &ministate.compl.buffer;
802 if (buffer->current_line != NULL)
803 buffer->current_line->parent->type = LINE_COMPL;
805 forward_line(buffer, +1);
807 if (buffer->current_line != NULL)
808 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
812 cmd_insert_current_candidate(struct buffer *buffer)
816 if (in_minibuffer != MB_COMPREAD)
819 buffer = &ministate.compl.buffer;
820 if ((vl = buffer->current_line) == NULL)
823 minibuffer_taint_hist();
824 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
825 ministate.buffer.cpoff = utf8_cplen(ministate.buf);