2 * Copyright (c) 2021, 2024 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.
30 #include "minibuffer.h"
32 #include "telescope.h"
37 #define GUARD_RECURSIVE_MINIBUFFER() \
39 if (in_minibuffer) { \
40 message("enable-recursive-minibuffers " \
41 "is not yet available."); \
46 #define GUARD_READ_ONLY() \
48 if (!in_minibuffer) { \
49 message("text is read-only"); \
54 /* return 1 if moved, 0 otherwise */
56 forward_line(struct buffer *buffer, int n)
61 if (buffer->current_line == NULL)
63 vl = buffer->current_line;
68 vl = TAILQ_NEXT(vl, vlines);
71 if (vl->parent->flags & L_HIDDEN)
73 buffer->current_line = vl;
76 vl = TAILQ_PREV(vl, vhead, vlines);
79 if (vl->parent->flags & L_HIDDEN)
81 if (buffer->current_line == buffer->top_line) {
83 buffer->top_line = vl;
85 buffer->current_line = vl;
96 cmd_previous_line(struct buffer *buffer)
98 forward_line(buffer, -1);
102 cmd_next_line(struct buffer *buffer)
104 forward_line(buffer, +1);
108 cmd_backward_char(struct buffer *buffer)
110 if (buffer->cpoff != 0)
115 cmd_forward_char(struct buffer *buffer)
117 if (buffer->current_line == NULL)
119 if (buffer->current_line->cplen > buffer->cpoff)
124 cmd_backward_paragraph(struct buffer *buffer)
127 if (!forward_line(buffer, -1)) {
128 message("No previous paragraph");
131 } while (buffer->current_line->len != 0 ||
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->len != 0 ||
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;
161 buffer->cpoff = vl->cplen;
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, hist_cur(current_tab->hist), 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, 1);
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, 1);
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, compl_lu, NULL, 0);
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, compl_lu, NULL, 0);
578 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
579 strlcpy(ministate.buf, hist_cur(current_tab->hist), 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, hist_cur(current_tab->hist), 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, hist_cur(current_tab->hist),
599 sizeof(ministate.buf));
600 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
604 cmd_list_bookmarks(struct buffer *buffer)
606 load_url_in_tab(current_tab, "about:bookmarks", NULL, LU_MODE_NONE);
610 cmd_toggle_help(struct buffer *buffer)
612 ui_toggle_side_window(SIDE_WINDOW_LEFT);
616 cmd_link_select(struct buffer *buffer)
620 GUARD_RECURSIVE_MINIBUFFER();
622 l = TAILQ_FIRST(&buffer->page.head);
623 while (l != NULL && l->type != LINE_LINK)
624 l = TAILQ_NEXT(l, lines);
627 message("No links found");
631 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
632 NULL, compl_ls, l, 1);
633 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
637 cmd_swiper(struct buffer *buffer)
639 GUARD_RECURSIVE_MINIBUFFER();
641 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
642 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head), 1);
643 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
647 cmd_toc(struct buffer *buffer)
651 GUARD_RECURSIVE_MINIBUFFER();
653 l = TAILQ_FIRST(&buffer->page.head);
655 l->type != LINE_TITLE_1 &&
656 l->type != LINE_TITLE_2 &&
657 l->type != LINE_TITLE_3)
658 l = TAILQ_NEXT(l, lines);
661 message("No headings found");
665 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
666 NULL, compl_toc, l, 1);
667 strlcpy(ministate.prompt, "Select heading: ",
668 sizeof(ministate.prompt));
672 cmd_inc_fill_column(struct buffer *buffer)
674 if (fill_column == INT_MAX)
678 message("fill-column: %d", fill_column);
680 ui_schedule_redraw();
684 cmd_dec_fill_column(struct buffer *buffer)
686 if (fill_column == INT_MAX || fill_column < 8)
690 message("fill-column: %d", fill_column);
692 ui_schedule_redraw();
696 cmd_olivetti_mode(struct buffer *buffer)
698 olivetti_mode = !olivetti_mode;
700 message("olivetti-mode enabled");
702 message("olivetti-mode disabled");
704 ui_schedule_redraw();
708 cmd_mini_delete_char(struct buffer *buffer)
714 minibuffer_taint_hist();
716 line = buffer->current_line->parent->line + buffer->current_line->from;
717 c = utf8_nth(line, buffer->cpoff);
722 memmove(c, n, strlen(n)+1);
724 recompute_completions(0);
728 cmd_mini_delete_backward_char(struct buffer *buffer)
734 minibuffer_taint_hist();
736 line = buffer->current_line->parent->line + buffer->current_line->from;
737 c = utf8_nth(line, buffer->cpoff);
740 p = utf8_prev_cp(c-1, line);
742 memmove(p, c, strlen(c)+1);
745 recompute_completions(0);
749 cmd_mini_kill_line(struct buffer *buffer)
755 minibuffer_taint_hist();
757 line = buffer->current_line->parent->line + buffer->current_line->from;
758 c = utf8_nth(line, buffer->cpoff);
761 recompute_completions(0);
765 cmd_mini_kill_whole_line(struct buffer *buffer)
769 minibuffer_taint_hist();
770 *buffer->current_line->parent->line = '\0';
775 cmd_mini_abort(struct buffer *buffer)
784 cmd_mini_complete_and_exit(struct buffer *buffer)
791 if (ministate.compl.must_select && ministate.hist == NULL) {
792 vl = ministate.compl.buffer.current_line;
793 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
794 vl->parent->type == LINE_COMPL) {
800 minibuffer_taint_hist();
805 cmd_mini_previous_history_element(struct buffer *buffer)
809 if (ministate.hist == NULL) {
810 message("No history");
814 if (hist_prev(ministate.hist) == NULL) {
815 message("No prev history item");
819 ministate.editing = 0;
821 /* XXX the minibuffer line is never modified so this is fine */
822 text = (char *)hist_cur(ministate.hist);
823 buffer->current_line->parent->line = text;
824 recompute_completions(0);
828 cmd_mini_next_history_element(struct buffer *buffer)
832 if (ministate.hist == NULL) {
833 message("No history");
837 if (hist_next(ministate.hist) == NULL) {
838 message("No next history item");
842 ministate.editing = 0;
844 /* XXX the minibuffer line is never modified so this is fine */
845 text = (char *)hist_cur(ministate.hist);
846 buffer->current_line->parent->line = text;
847 recompute_completions(0);
851 cmd_previous_completion(struct buffer *buffer)
853 if (in_minibuffer != MB_COMPREAD)
856 buffer = &ministate.compl.buffer;
857 if (buffer->current_line == NULL)
860 buffer->current_line->parent->type = LINE_COMPL;
861 if (!forward_line(buffer, -1))
862 buffer->current_line->parent->type = LINE_COMPL;
864 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
868 cmd_next_completion(struct buffer *buffer)
870 if (in_minibuffer != MB_COMPREAD)
873 buffer = &ministate.compl.buffer;
874 if (buffer->current_line == NULL)
877 if (buffer->current_line->parent->type == LINE_COMPL_CURRENT) {
878 buffer->current_line->parent->type = LINE_COMPL;
879 forward_line(buffer, +1);
882 if (buffer->current_line != NULL)
883 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
887 cmd_insert_current_candidate(struct buffer *buffer)
889 if (in_minibuffer != MB_COMPREAD)
892 minibuffer_insert_current_candidate();
896 cmd_suspend_telescope(struct buffer *buffer)
903 cmd_toggle_pre_wrap(struct buffer *buffer)
905 dont_wrap_pre = !dont_wrap_pre;
908 message("Don't wrap preformatted blocks");
910 message("Wrap preformatted blocks");
912 ui_schedule_redraw();
916 cmd_mini_goto_beginning(struct buffer *buffer)
923 buffer = &ministate.compl.buffer;
925 if ((vl = buffer->current_line) != NULL)
926 vl->parent->type = LINE_COMPL;
928 vl = TAILQ_FIRST(&buffer->head);
929 while (vl != NULL && vl->parent->flags & L_HIDDEN)
930 vl = TAILQ_NEXT(vl, vlines);
935 vl->parent->type = LINE_COMPL_CURRENT;
936 buffer->top_line = vl;
937 buffer->current_line = vl;
941 cmd_mini_goto_end(struct buffer *buffer)
948 buffer = &ministate.compl.buffer;
950 if ((vl = buffer->current_line) != NULL)
951 vl->parent->type = LINE_COMPL;
953 vl = TAILQ_LAST(&buffer->head, vhead);
954 while (vl != NULL && vl->parent->flags & L_HIDDEN)
955 vl = TAILQ_PREV(vl, vhead, vlines);
960 vl->parent->type = LINE_COMPL_CURRENT;
961 buffer->current_line = vl;
965 cmd_other_window(struct buffer *buffer)
971 cmd_mini_scroll_up(struct buffer *buffer)
976 buffer = &ministate.compl.buffer;
977 if (buffer->current_line == NULL)
980 buffer->current_line->parent->type = LINE_COMPL;
981 cmd_scroll_up(buffer);
982 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
986 cmd_mini_scroll_down(struct buffer *buffer)
991 buffer = &ministate.compl.buffer;
992 if (buffer->current_line == NULL)
995 buffer->current_line->parent->type = LINE_COMPL;
996 cmd_scroll_down(buffer);
997 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
1001 cmd_toggle_downloads(struct buffer *buffer)
1003 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1007 cmd_cache_info(struct buffer *buffer)
1010 char fmt[FMT_SCALED_STRSIZE];
1012 mcache_info(&npages, &tot);
1014 if (fmt_scaled(tot, fmt) == 0)
1015 message("pages: %zu, total: %s", npages, fmt);
1017 message("pages: %zu, total: %zu", npages, tot);
1021 cmd_reply_last_input(struct buffer *buffer)
1023 GUARD_RECURSIVE_MINIBUFFER();
1025 if (current_tab->last_input_url == NULL) {
1026 message("there was no previous input request in this tab");
1030 if (!strncmp(current_tab->last_input_url, "gopher", 6)) {
1031 load_url_in_tab(current_tab, current_tab->last_input_url,
1032 NULL, LU_MODE_NOCACHE);
1036 message("%s", current_tab->last_input_url);
1037 ui_require_input(current_tab, 0, ir_select_reply);
1041 cmd_write_buffer(struct buffer *buffer)
1043 const char *f, *url;
1044 char path[PATH_MAX];
1046 GUARD_RECURSIVE_MINIBUFFER();
1049 message("Can't write buffer in safe-mode.");
1053 url = hist_cur(current_tab->hist);
1055 if ((f = strrchr(url, '/')) != NULL)
1057 if (f == NULL || *f == '\0') {
1058 /* guess a decent file name based on the protocol used */
1059 if (!strncmp(url, "gemini://", 9))
1065 strlcpy(path, download_path, sizeof(path));
1066 strlcat(path, f, sizeof(path));
1068 ui_read("Write file", write_buffer, current_tab, path);
1072 cmd_home(struct buffer *buffer)
1074 char path[GEMINI_URL_LEN];
1077 strlcpy(path, current_tab->iri.iri_path, sizeof(path));
1079 if ((tilde = strstr(path, "/~")) != NULL &&
1080 tilde[2] != '\0' && tilde[2] != '/') {
1081 if ((t = strchr(tilde + 2, '/')) != NULL)
1083 load_url_in_tab(current_tab, path, NULL, LU_MODE_NOCACHE);
1089 cmd_root(struct buffer *buffer)
1091 load_url_in_tab(current_tab, "/", NULL, LU_MODE_NOCACHE);
1095 cmd_up(struct buffer *buffer)
1097 load_url_in_tab(current_tab, "..", NULL, LU_MODE_NOCACHE);
1101 cmd_use_certificate(struct buffer *buffer)
1103 GUARD_RECURSIVE_MINIBUFFER();
1105 enter_minibuffer(sensible_self_insert, uc_select, exit_minibuffer,
1106 NULL, compl_uc, NULL, 1);
1107 strlcpy(ministate.prompt, "Select certificate: ",
1108 sizeof(ministate.prompt));
1112 cmd_client_certificate_info(struct buffer *buffer)
1114 if (current_tab->client_cert)
1115 message("Using certificate %s", current_tab->client_cert);
1117 message("Not using any client certificate.");
1121 unload_certificate_cb(int r, struct tab *tab)
1123 message("Won't use %s for this site.", tab->client_cert);
1124 cert_delete_for(tab->client_cert, &tab->iri, r);
1128 cmd_unload_certificate(struct buffer *buffer)
1130 struct tab *tab = current_tab;
1132 GUARD_RECURSIVE_MINIBUFFER();
1134 if (tab->client_cert == NULL) {
1135 message("No client certificate in use!");
1139 if (tab->client_cert_temp) {
1140 message("Won't use %s for this site.", tab->client_cert);
1141 cert_delete_for(tab->client_cert, &tab->iri, 0);
1145 yornp("Unload only for the current session?", unload_certificate_cb,
1150 cmd_search(struct buffer *buffer)
1152 GUARD_RECURSIVE_MINIBUFFER();
1154 if (!strncmp(default_search_engine, "gopher://", 9)) {
1155 load_url_in_tab(current_tab, default_search_engine, NULL,
1160 enter_minibuffer(sensible_self_insert, search_select, exit_minibuffer, NULL,
1162 strlcpy(ministate.prompt, "Search: ", sizeof(ministate.prompt));