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.
26 #include "minibuffer.h"
28 #include "telescope.h"
32 #define GUARD_RECURSIVE_MINIBUFFER() \
34 if (in_minibuffer) { \
35 message("enable-recursive-minibuffers " \
36 "is not yet available."); \
41 #define GUARD_READ_ONLY() \
43 if (!in_minibuffer) { \
44 message("text is read-only"); \
49 /* return 1 if moved, 0 otherwise */
51 forward_line(struct buffer *buffer, int n)
56 if (buffer->current_line == NULL)
58 vl = buffer->current_line;
63 vl = TAILQ_NEXT(vl, vlines);
66 if (vl->parent->flags & L_HIDDEN)
68 buffer->current_line = vl;
71 vl = TAILQ_PREV(vl, vhead, vlines);
74 if (vl->parent->flags & L_HIDDEN)
76 if (buffer->current_line == buffer->top_line) {
78 buffer->top_line = vl;
80 buffer->current_line = vl;
91 cmd_previous_line(struct buffer *buffer)
93 forward_line(buffer, -1);
97 cmd_next_line(struct buffer *buffer)
99 forward_line(buffer, +1);
103 cmd_backward_char(struct buffer *buffer)
105 if (buffer->cpoff != 0)
110 cmd_forward_char(struct buffer *buffer)
114 if (buffer->current_line == NULL)
117 if (buffer->current_line->line != NULL)
118 len = utf8_cplen(buffer->current_line->line);
119 if (++buffer->cpoff > len)
124 cmd_backward_paragraph(struct buffer *buffer)
127 if (!forward_line(buffer, -1)) {
128 message("No previous paragraph");
131 } while (buffer->current_line->line != NULL ||
132 buffer->current_line->parent->type != LINE_TEXT);
136 cmd_forward_paragraph(struct buffer *buffer)
139 if (!forward_line(buffer, +1)) {
140 message("No next paragraph");
143 } while (buffer->current_line->line != NULL ||
144 buffer->current_line->parent->type != LINE_TEXT);
148 cmd_move_beginning_of_line(struct buffer *buffer)
154 cmd_move_end_of_line(struct buffer *buffer)
158 vl = buffer->current_line;
159 if (vl == NULL || vl->line == NULL)
161 buffer->cpoff = utf8_cplen(vl->line);
165 cmd_redraw(struct buffer *buffer)
167 ui_schedule_redraw();
171 cmd_scroll_line_up(struct buffer *buffer)
176 if (buffer->top_line == NULL)
179 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
183 buffer->top_line = vl;
185 if (vl->parent->flags & L_HIDDEN)
193 forward_line(buffer, -1);
197 cmd_scroll_line_down(struct buffer *buffer)
199 if (!forward_line(buffer, +1))
203 if (buffer->top_line == NULL)
206 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
207 if (buffer->top_line->parent->flags & L_HIDDEN)
216 cmd_scroll_up(struct buffer *buffer)
221 if (buffer->top_line == NULL)
224 for (i = 0; i < body_lines; ++i) {
225 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
229 buffer->top_line = vl;
230 forward_line(buffer, -1);
235 cmd_scroll_down(struct buffer *buffer)
239 if (buffer->top_line == NULL)
242 for (i = 0; i < body_lines; ++i) {
243 if (!forward_line(buffer, +1))
246 buffer->top_line = TAILQ_NEXT(buffer->top_line,
253 cmd_beginning_of_buffer(struct buffer *buffer)
255 buffer->current_line = TAILQ_FIRST(&buffer->head);
257 buffer->top_line = buffer->current_line;
258 buffer->line_off = 0;
262 cmd_end_of_buffer(struct buffer *buffer)
264 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
266 if (buffer->current_line == NULL)
269 /* deal with invisible lines */
270 if (buffer->current_line->parent->flags & L_HIDDEN)
271 forward_line(buffer, -1);
273 cmd_move_end_of_line(buffer);
277 kill_telescope_cb(int r, struct tab *tab)
286 cmd_kill_telescope(struct buffer *buffer)
288 yornp("really quit?", kill_telescope_cb, NULL);
292 cmd_push_button(struct buffer *buffer)
297 vl = buffer->current_line;
302 switch (vl->parent->type) {
304 load_url_in_tab(current_tab, vl->parent->alt, NULL,
308 l = TAILQ_NEXT(vl->parent, lines);
309 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
310 if (l->type == LINE_PRE_END)
312 l->flags ^= L_HIDDEN;
313 if (l->flags & L_HIDDEN)
325 cmd_push_button_new_tab(struct buffer *buffer)
329 vl = buffer->current_line;
330 if (vl == NULL || vl->parent->type != LINE_LINK)
333 new_tab(vl->parent->alt, current_tab->hist_cur->h, current_tab);
337 cmd_previous_button(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 link");
349 } while (buffer->current_line->parent->type != LINE_LINK);
353 cmd_next_button(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 link");
365 } while (buffer->current_line->parent->type != LINE_LINK);
369 is_heading(const struct line *l)
371 return l->type == LINE_TITLE_1 ||
372 l->type == LINE_TITLE_2 ||
373 l->type == LINE_TITLE_3;
377 cmd_previous_heading(struct buffer *buffer)
379 struct excursion place;
381 save_excursion(&place, buffer);
384 if (!forward_line(buffer, -1)) {
385 restore_excursion(&place, buffer);
386 message("No previous heading");
389 } while (!is_heading(buffer->current_line->parent));
393 cmd_next_heading(struct buffer *buffer)
395 struct excursion place;
397 save_excursion(&place, buffer);
400 if (!forward_line(buffer, +1)) {
401 restore_excursion(&place, buffer);
402 message("No next heading");
405 } while (!is_heading(buffer->current_line->parent));
409 cmd_previous_page(struct buffer *buffer)
411 if (!load_previous_page(current_tab))
412 message("No previous page");
416 cmd_next_page(struct buffer *buffer)
418 if (!load_next_page(current_tab))
419 message("No next page");
423 cmd_clear_minibuf(struct buffer *buffer)
429 cmd_execute_extended_command(struct buffer *buffer)
433 GUARD_RECURSIVE_MINIBUFFER();
435 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
436 &eecmd_history, compl_eecmd, NULL);
438 len = sizeof(ministate.prompt);
439 strlcpy(ministate.prompt, "", len);
442 strlcat(ministate.prompt, "M-", len);
444 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
447 strlcat(ministate.prompt, " ", len);
451 cmd_tab_close(struct buffer *buffer)
457 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
458 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
462 message("Can't close the only tab.");
467 cmd_tab_close_other(struct buffer *buffer)
471 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
472 if (t == current_tab)
480 cmd_tab_undo_close(struct buffer *buffer)
484 if ((t = unkill_tab()) == NULL) {
485 message("No recently-closed tabs");
493 cmd_tab_new(struct buffer *buffer)
497 if ((url = new_tab_url) == NULL)
500 new_tab(url, NULL, NULL);
504 cmd_tab_next(struct buffer *buffer)
508 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
509 t = TAILQ_FIRST(&tabshead);
514 cmd_tab_previous(struct buffer *buffer)
518 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
519 t = TAILQ_LAST(&tabshead, tabshead);
524 cmd_tab_move(struct buffer *buffer)
528 t = TAILQ_NEXT(current_tab, tabs);
529 TAILQ_REMOVE(&tabshead, current_tab, tabs);
532 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
534 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
538 cmd_tab_move_to(struct buffer *buffer)
542 t = TAILQ_PREV(current_tab, tabshead, tabs);
543 TAILQ_REMOVE(&tabshead, current_tab, tabs);
546 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
548 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
552 cmd_tab_select(struct buffer *buffer)
554 GUARD_RECURSIVE_MINIBUFFER();
556 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
557 NULL, compl_ts, NULL);
558 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
562 cmd_load_url(struct buffer *buffer)
564 GUARD_RECURSIVE_MINIBUFFER();
566 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
567 &lu_history, NULL, NULL);
568 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
572 cmd_load_current_url(struct buffer *buffer)
574 GUARD_RECURSIVE_MINIBUFFER();
576 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
577 &lu_history, NULL, NULL);
578 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
579 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
580 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
584 cmd_reload_page(struct buffer *buffer)
586 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL,
587 LU_MODE_NOHIST|LU_MODE_NOCACHE);
591 cmd_bookmark_page(struct buffer *buffer)
593 GUARD_RECURSIVE_MINIBUFFER();
595 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
597 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
598 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
599 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
603 cmd_list_bookmarks(struct buffer *buffer)
605 load_url_in_tab(current_tab, "about:bookmarks", NULL, LU_MODE_NONE);
609 cmd_toggle_help(struct buffer *buffer)
611 ui_toggle_side_window(SIDE_WINDOW_LEFT);
615 cmd_link_select(struct buffer *buffer)
619 GUARD_RECURSIVE_MINIBUFFER();
621 l = TAILQ_FIRST(&buffer->page.head);
622 while (l != NULL && l->type != LINE_LINK)
623 l = TAILQ_NEXT(l, lines);
626 message("No links found");
630 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
632 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
636 cmd_swiper(struct buffer *buffer)
638 GUARD_RECURSIVE_MINIBUFFER();
640 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
641 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
642 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
646 cmd_toc(struct buffer *buffer)
650 GUARD_RECURSIVE_MINIBUFFER();
652 l = TAILQ_FIRST(&buffer->page.head);
654 l->type != LINE_TITLE_1 &&
655 l->type != LINE_TITLE_2 &&
656 l->type != LINE_TITLE_3)
657 l = TAILQ_NEXT(l, lines);
660 message("No headings found");
664 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
666 strlcpy(ministate.prompt, "Select heading: ",
667 sizeof(ministate.prompt));
671 cmd_inc_fill_column(struct buffer *buffer)
673 if (fill_column == INT_MAX)
677 message("fill-column: %d", fill_column);
679 ui_schedule_redraw();
683 cmd_dec_fill_column(struct buffer *buffer)
685 if (fill_column == INT_MAX || fill_column < 8)
689 message("fill-column: %d", fill_column);
691 ui_schedule_redraw();
695 cmd_olivetti_mode(struct buffer *buffer)
697 olivetti_mode = !olivetti_mode;
699 message("olivetti-mode enabled");
701 message("olivetti-mode disabled");
703 ui_schedule_redraw();
707 cmd_mini_delete_char(struct buffer *buffer)
713 minibuffer_taint_hist();
715 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
720 memmove(c, n, strlen(n)+1);
722 recompute_completions(0);
726 cmd_mini_delete_backward_char(struct buffer *buffer)
732 minibuffer_taint_hist();
734 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
735 start = buffer->current_line->line;
738 p = utf8_prev_cp(c-1, start);
740 memmove(p, c, strlen(c)+1);
743 recompute_completions(0);
747 cmd_mini_kill_line(struct buffer *buffer)
753 minibuffer_taint_hist();
754 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
757 recompute_completions(0);
761 cmd_mini_abort(struct buffer *buffer)
770 cmd_mini_complete_and_exit(struct buffer *buffer)
775 minibuffer_taint_hist();
780 cmd_mini_previous_history_element(struct buffer *buffer)
782 if (ministate.history == NULL) {
783 message("No history");
787 if (ministate.hist_cur == NULL ||
788 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
789 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
790 ministate.hist_off = ministate.history->len - 1;
791 if (ministate.hist_cur == NULL)
792 message("No prev history item");
794 ministate.hist_off--;
797 if (ministate.hist_cur != NULL)
798 buffer->current_line->line = ministate.hist_cur->h;
802 cmd_mini_next_history_element(struct buffer *buffer)
804 if (ministate.history == NULL) {
805 message("No history");
809 if (ministate.hist_cur == NULL ||
810 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
811 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
812 ministate.hist_off = 0;
813 if (ministate.hist_cur == NULL)
814 message("No next history item");
816 ministate.hist_off++;
819 if (ministate.hist_cur != NULL)
820 buffer->current_line->line = ministate.hist_cur->h;
824 cmd_previous_completion(struct buffer *buffer)
826 if (in_minibuffer != MB_COMPREAD)
829 buffer = &ministate.compl.buffer;
831 if (buffer->current_line != NULL)
832 buffer->current_line->parent->type = LINE_COMPL;
834 forward_line(buffer, -1);
836 if (buffer->current_line != NULL)
837 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
841 cmd_next_completion(struct buffer *buffer)
843 if (in_minibuffer != MB_COMPREAD)
846 buffer = &ministate.compl.buffer;
848 if (buffer->current_line != NULL)
849 buffer->current_line->parent->type = LINE_COMPL;
851 forward_line(buffer, +1);
853 if (buffer->current_line != NULL)
854 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
858 cmd_insert_current_candidate(struct buffer *buffer)
860 if (in_minibuffer != MB_COMPREAD)
863 minibuffer_insert_current_candidate();
867 cmd_suspend_telescope(struct buffer *buffer)
874 cmd_toggle_pre_wrap(struct buffer *buffer)
876 dont_wrap_pre = !dont_wrap_pre;
879 message("Don't wrap preformatted blocks");
881 message("Wrap preformatted blocks");
883 ui_schedule_redraw();
887 cmd_mini_goto_beginning(struct buffer *buffer)
894 buffer = &ministate.compl.buffer;
896 if ((vl = buffer->current_line) != NULL)
897 vl->parent->type = LINE_COMPL;
899 vl = TAILQ_FIRST(&buffer->head);
900 while (vl != NULL && vl->parent->flags & L_HIDDEN)
901 vl = TAILQ_NEXT(vl, vlines);
906 vl->parent->type = LINE_COMPL_CURRENT;
907 buffer->top_line = vl;
908 buffer->current_line = vl;
912 cmd_mini_goto_end(struct buffer *buffer)
919 buffer = &ministate.compl.buffer;
921 if ((vl = buffer->current_line) != NULL)
922 vl->parent->type = LINE_COMPL;
924 vl = TAILQ_LAST(&buffer->head, vhead);
925 while (vl != NULL && vl->parent->flags & L_HIDDEN)
926 vl = TAILQ_PREV(vl, vhead, vlines);
931 vl->parent->type = LINE_COMPL_CURRENT;
932 buffer->current_line = vl;
936 cmd_other_window(struct buffer *buffer)
942 cmd_mini_scroll_up(struct buffer *buffer)
947 buffer = &ministate.compl.buffer;
948 if (buffer->current_line == NULL)
951 buffer->current_line->parent->type = LINE_COMPL;
952 cmd_scroll_up(buffer);
953 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
957 cmd_mini_scroll_down(struct buffer *buffer)
962 buffer = &ministate.compl.buffer;
963 if (buffer->current_line == NULL)
966 buffer->current_line->parent->type = LINE_COMPL;
967 cmd_scroll_down(buffer);
968 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
972 cmd_toggle_downloads(struct buffer *buffer)
974 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
978 cmd_cache_info(struct buffer *buffer)
981 char fmt[FMT_SCALED_STRSIZE];
983 mcache_info(&npages, &tot);
985 if (fmt_scaled(tot, fmt) == 0)
986 message("pages: %zu, total: %s", npages, fmt);
988 message("pages: %zu, total: %zu", npages, tot);