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)
185 for (i = 0; i < body_lines; ++i) {
186 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
189 buffer->top_line = vl;
190 forward_line(buffer, -1);
195 cmd_scroll_down(struct buffer *buffer)
199 for (i = 0; i < body_lines; ++i) {
200 if (!forward_line(buffer, +1))
203 buffer->top_line = TAILQ_NEXT(buffer->top_line,
209 cmd_beginning_of_buffer(struct buffer *buffer)
211 buffer->current_line = TAILQ_FIRST(&buffer->head);
213 buffer->top_line = buffer->current_line;
214 buffer->line_off = 0;
218 cmd_end_of_buffer(struct buffer *buffer)
220 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
221 buffer->cpoff = body_cols;
225 cmd_kill_telescope(struct buffer *buffer)
232 cmd_push_button(struct buffer *buffer)
237 vl = buffer->current_line;
238 switch (vl->parent->type) {
240 load_url_in_tab(current_tab, vl->parent->alt);
243 l = TAILQ_NEXT(vl->parent, lines);
244 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
245 if (l->type == LINE_PRE_END)
247 l->flags ^= L_HIDDEN;
248 if (l->flags & L_HIDDEN)
260 cmd_push_button_new_tab(struct buffer *buffer)
264 vl = buffer->current_line;
265 if (vl->parent->type != LINE_LINK)
268 new_tab(vl->parent->alt);
272 cmd_previous_button(struct buffer *buffer)
274 struct excursion place;
276 save_excursion(&place, buffer);
279 if (!forward_line(buffer, -1)) {
280 restore_excursion(&place, buffer);
281 message("No previous link");
284 } while (buffer->current_line->parent->type != LINE_LINK);
288 cmd_next_button(struct buffer *buffer)
290 struct excursion place;
292 save_excursion(&place, buffer);
295 if (!forward_line(buffer, +1)){
296 restore_excursion(&place, buffer);
297 message("No next link");
300 } while (buffer->current_line->parent->type != LINE_LINK);
304 is_heading(const struct line *l)
306 return l->type == LINE_TITLE_1 ||
307 l->type == LINE_TITLE_2 ||
308 l->type == LINE_TITLE_3;
312 cmd_previous_heading(struct buffer *buffer)
314 struct excursion place;
316 save_excursion(&place, buffer);
319 if (!forward_line(buffer, -1)) {
320 restore_excursion(&place, buffer);
321 message("No previous heading");
324 } while (!is_heading(buffer->current_line->parent));
328 cmd_next_heading(struct buffer *buffer)
330 struct excursion place;
332 save_excursion(&place, buffer);
335 if (!forward_line(buffer, +1)) {
336 restore_excursion(&place, buffer);
337 message("No next heading");
340 } while (!is_heading(buffer->current_line->parent));
344 cmd_previous_page(struct buffer *buffer)
346 if (!load_previous_page(current_tab))
347 message("No previous page");
349 start_loading_anim(current_tab);
353 cmd_next_page(struct buffer *buffer)
355 if (!load_next_page(current_tab))
356 message("No next page");
358 start_loading_anim(current_tab);
362 cmd_clear_minibuf(struct buffer *buffer)
368 cmd_execute_extended_command(struct buffer *buffer)
372 GUARD_RECURSIVE_MINIBUFFER();
374 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
375 &eecmd_history, compl_eecmd, NULL);
377 len = sizeof(ministate.prompt);
378 strlcpy(ministate.prompt, "", len);
381 strlcat(ministate.prompt, "M-", len);
383 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
386 strlcat(ministate.prompt, " ", len);
390 cmd_tab_close(struct buffer *buffer)
395 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
396 TAILQ_NEXT(tab, tabs) == NULL) {
397 message("Can't close the only tab.");
401 if (evtimer_pending(&tab->loadingev, NULL))
402 evtimer_del(&tab->loadingev);
406 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
407 t = TAILQ_NEXT(tab, tabs);
408 TAILQ_REMOVE(&tabshead, tab, tabs);
415 cmd_tab_close_other(struct buffer *buffer)
419 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
420 if (t == current_tab)
424 TAILQ_REMOVE(&tabshead, t, tabs);
430 cmd_tab_new(struct buffer *buffer)
434 if ((url = new_tab_url) == NULL)
441 cmd_tab_next(struct buffer *buffer)
445 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
446 t = TAILQ_FIRST(&tabshead);
451 cmd_tab_previous(struct buffer *buffer)
455 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
456 t = TAILQ_LAST(&tabshead, tabshead);
461 cmd_tab_move(struct buffer *buffer)
465 t = TAILQ_NEXT(current_tab, tabs);
466 TAILQ_REMOVE(&tabshead, current_tab, tabs);
469 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
471 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
475 cmd_tab_move_to(struct buffer *buffer)
479 t = TAILQ_PREV(current_tab, tabshead, tabs);
480 TAILQ_REMOVE(&tabshead, current_tab, tabs);
483 if (TAILQ_EMPTY(&tabshead))
484 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
486 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
488 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
492 cmd_tab_select(struct buffer *buffer)
494 GUARD_RECURSIVE_MINIBUFFER();
496 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
497 NULL, compl_ts, NULL);
498 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
502 cmd_load_url(struct buffer *buffer)
504 GUARD_RECURSIVE_MINIBUFFER();
506 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
507 &lu_history, NULL, NULL);
508 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
509 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
510 cmd_move_end_of_line(&ministate.buffer);
514 cmd_load_current_url(struct buffer *buffer)
516 GUARD_RECURSIVE_MINIBUFFER();
518 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
519 &lu_history, NULL, NULL);
520 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
521 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
522 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
526 cmd_reload_page(struct buffer *buffer)
528 load_url_in_tab(current_tab, current_tab->hist_cur->h);
532 cmd_bookmark_page(struct buffer *buffer)
534 GUARD_RECURSIVE_MINIBUFFER();
536 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
538 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
539 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
540 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
544 cmd_list_bookmarks(struct buffer *buffer)
546 load_url_in_tab(current_tab, "about:bookmarks");
550 cmd_toggle_help(struct buffer *buffer)
552 ui_toggle_side_window();
556 cmd_link_select(struct buffer *buffer)
560 GUARD_RECURSIVE_MINIBUFFER();
562 l = TAILQ_FIRST(&buffer->page.head);
563 while (l != NULL && l->type != LINE_LINK)
564 l = TAILQ_NEXT(l, lines);
567 message("No links found");
571 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
573 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
577 cmd_swiper(struct buffer *buffer)
579 GUARD_RECURSIVE_MINIBUFFER();
581 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
582 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
583 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
587 cmd_toc(struct buffer *buffer)
591 GUARD_RECURSIVE_MINIBUFFER();
593 l = TAILQ_FIRST(&buffer->page.head);
595 l->type != LINE_TITLE_1 &&
596 l->type != LINE_TITLE_2 &&
597 l->type != LINE_TITLE_3)
598 l = TAILQ_NEXT(l, lines);
601 message("No headings found");
605 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
607 strlcpy(ministate.prompt, "Select heading: ",
608 sizeof(ministate.prompt));
612 cmd_inc_fill_column(struct buffer *buffer)
614 if (fill_column == INT_MAX)
618 message("fill-column: %d", fill_column);
620 ui_schedule_redraw();
624 cmd_dec_fill_column(struct buffer *buffer)
626 if (fill_column == INT_MAX || fill_column < 8)
630 message("fill-column: %d", fill_column);
632 ui_schedule_redraw();
636 cmd_olivetti_mode(struct buffer *buffer)
638 olivetti_mode = !olivetti_mode;
640 message("olivetti-mode enabled");
642 message("olivetti-mode disabled");
644 ui_schedule_redraw();
648 cmd_mini_delete_char(struct buffer *buffer)
652 if (!in_minibuffer) {
653 message("text is read-only");
657 minibuffer_taint_hist();
659 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
664 memmove(c, n, strlen(n)+1);
666 recompute_completions(0);
670 cmd_mini_delete_backward_char(struct buffer *buffer)
674 if (!in_minibuffer) {
675 message("text is read-only");
679 minibuffer_taint_hist();
681 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
682 start = buffer->current_line->line;
685 p = utf8_prev_cp(c-1, start);
687 memmove(p, c, strlen(c)+1);
690 recompute_completions(0);
694 cmd_mini_kill_line(struct buffer *buffer)
698 if (!in_minibuffer) {
699 message("text is read-only");
703 minibuffer_taint_hist();
704 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
707 recompute_completions(0);
711 cmd_mini_abort(struct buffer *buffer)
720 cmd_mini_complete_and_exit(struct buffer *buffer)
725 minibuffer_taint_hist();
730 cmd_mini_previous_history_element(struct buffer *buffer)
732 if (ministate.history == NULL) {
733 message("No history");
737 if (ministate.hist_cur == NULL ||
738 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
739 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
740 ministate.hist_off = ministate.history->len - 1;
741 if (ministate.hist_cur == NULL)
742 message("No prev history item");
744 ministate.hist_off--;
747 if (ministate.hist_cur != NULL)
748 buffer->current_line->line = ministate.hist_cur->h;
752 cmd_mini_next_history_element(struct buffer *buffer)
754 if (ministate.history == NULL) {
755 message("No history");
759 if (ministate.hist_cur == NULL ||
760 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
761 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
762 ministate.hist_off = 0;
763 if (ministate.hist_cur == NULL)
764 message("No next history item");
766 ministate.hist_off++;
769 if (ministate.hist_cur != NULL)
770 buffer->current_line->line = ministate.hist_cur->h;
774 cmd_previous_completion(struct buffer *buffer)
776 if (in_minibuffer != MB_COMPREAD)
779 buffer = &ministate.compl.buffer;
781 if (buffer->current_line != NULL)
782 buffer->current_line->parent->type = LINE_COMPL;
784 forward_line(buffer, -1);
786 if (buffer->current_line != NULL)
787 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
791 cmd_next_completion(struct buffer *buffer)
793 if (in_minibuffer != MB_COMPREAD)
796 buffer = &ministate.compl.buffer;
798 if (buffer->current_line != NULL)
799 buffer->current_line->parent->type = LINE_COMPL;
801 forward_line(buffer, +1);
803 if (buffer->current_line != NULL)
804 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
808 cmd_insert_current_candidate(struct buffer *buffer)
812 if (in_minibuffer != MB_COMPREAD)
815 buffer = &ministate.compl.buffer;
816 if ((vl = buffer->current_line) == NULL)
819 minibuffer_taint_hist();
820 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
821 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
825 cmd_suspend_telescope(struct buffer *buffer)
832 cmd_toggle_pre_wrap(struct buffer *buffer)
834 dont_wrap_pre = !dont_wrap_pre;
837 message("Don't wrap preformatted blocks");
839 message("Wrap preformatted blocks");
841 ui_schedule_redraw();
845 cmd_mini_goto_beginning(struct buffer *buffer)
852 buffer = &ministate.compl.buffer;
854 if ((vl = buffer->current_line) != NULL)
855 vl->parent->type = LINE_COMPL;
857 vl = TAILQ_FIRST(&buffer->head);
858 while (vl != NULL && vl->parent->flags & L_HIDDEN)
859 vl = TAILQ_NEXT(vl, vlines);
864 vl->parent->type = LINE_COMPL_CURRENT;
865 buffer->top_line = vl;
866 buffer->current_line = vl;
870 cmd_mini_goto_end(struct buffer *buffer)
877 buffer = &ministate.compl.buffer;
879 if ((vl = buffer->current_line) != NULL)
880 vl->parent->type = LINE_COMPL;
882 vl = TAILQ_LAST(&buffer->head, vhead);
883 while (vl != NULL && vl->parent->flags & L_HIDDEN)
884 vl = TAILQ_PREV(vl, vhead, vlines);
889 vl->parent->type = LINE_COMPL_CURRENT;
890 buffer->current_line = vl;