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"
33 #define GUARD_RECURSIVE_MINIBUFFER() \
35 if (in_minibuffer) { \
36 message("enable-recursive-minibuffers " \
37 "is not yet available."); \
42 #define GUARD_READ_ONLY() \
44 if (!in_minibuffer) { \
45 message("text is read-only"); \
50 /* return 1 if moved, 0 otherwise */
52 forward_line(struct buffer *buffer, int n)
57 if (buffer->current_line == NULL)
59 vl = buffer->current_line;
64 vl = TAILQ_NEXT(vl, vlines);
67 if (vl->parent->flags & L_HIDDEN)
69 buffer->current_line = vl;
72 vl = TAILQ_PREV(vl, vhead, vlines);
75 if (vl->parent->flags & L_HIDDEN)
77 if (buffer->current_line == buffer->top_line) {
79 buffer->top_line = vl;
81 buffer->current_line = vl;
92 cmd_previous_line(struct buffer *buffer)
94 forward_line(buffer, -1);
98 cmd_next_line(struct buffer *buffer)
100 forward_line(buffer, +1);
104 cmd_backward_char(struct buffer *buffer)
106 if (buffer->cpoff != 0)
111 cmd_forward_char(struct buffer *buffer)
115 if (buffer->current_line == NULL)
118 if (buffer->current_line->line != NULL)
119 len = utf8_cplen(buffer->current_line->line);
120 if (++buffer->cpoff > len)
125 cmd_backward_paragraph(struct buffer *buffer)
128 if (!forward_line(buffer, -1)) {
129 message("No previous paragraph");
132 } while (buffer->current_line->line != NULL ||
133 buffer->current_line->parent->type != LINE_TEXT);
137 cmd_forward_paragraph(struct buffer *buffer)
140 if (!forward_line(buffer, +1)) {
141 message("No next paragraph");
144 } while (buffer->current_line->line != NULL ||
145 buffer->current_line->parent->type != LINE_TEXT);
149 cmd_move_beginning_of_line(struct buffer *buffer)
155 cmd_move_end_of_line(struct buffer *buffer)
159 vl = buffer->current_line;
160 if (vl == NULL || vl->line == NULL)
162 buffer->cpoff = utf8_cplen(vl->line);
166 cmd_redraw(struct buffer *buffer)
168 ui_schedule_redraw();
172 cmd_scroll_line_up(struct buffer *buffer)
177 if (buffer->top_line == NULL)
180 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
184 buffer->top_line = vl;
186 if (vl->parent->flags & L_HIDDEN)
194 forward_line(buffer, -1);
198 cmd_scroll_line_down(struct buffer *buffer)
200 if (!forward_line(buffer, +1))
204 if (buffer->top_line == NULL)
207 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
208 if (buffer->top_line->parent->flags & L_HIDDEN)
217 cmd_scroll_up(struct buffer *buffer)
222 if (buffer->top_line == NULL)
225 for (i = 0; i < body_lines; ++i) {
226 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
230 buffer->top_line = vl;
231 forward_line(buffer, -1);
236 cmd_scroll_down(struct buffer *buffer)
240 if (buffer->top_line == NULL)
243 for (i = 0; i < body_lines; ++i) {
244 if (!forward_line(buffer, +1))
247 buffer->top_line = TAILQ_NEXT(buffer->top_line,
254 cmd_beginning_of_buffer(struct buffer *buffer)
256 buffer->current_line = TAILQ_FIRST(&buffer->head);
258 buffer->top_line = buffer->current_line;
259 buffer->line_off = 0;
263 cmd_end_of_buffer(struct buffer *buffer)
265 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
267 if (buffer->current_line == NULL)
270 /* deal with invisible lines */
271 if (buffer->current_line->parent->flags & L_HIDDEN)
272 forward_line(buffer, -1);
274 cmd_move_end_of_line(buffer);
278 kill_telescope_cb(int r, struct tab *tab)
287 cmd_kill_telescope(struct buffer *buffer)
289 yornp("really quit?", kill_telescope_cb, NULL);
294 #include <sys/wait.h>
297 do_exec_command(const char *cmd, struct tab *t)
307 switch (p = fork()) {
309 message("failed to fork: %s", strerror(errno));
312 execl("/bin/sh", "sh", "-c", cmd, NULL);
313 warn("exec \"%s\" failed", cmd);
318 if (waitpid(p, &s, 0) == -1) {
325 ui_schedule_redraw();
326 /* rearrange_windows(); */
330 cmd_push_button(struct buffer *buffer)
335 vl = buffer->current_line;
340 switch (vl->parent->type) {
342 load_url_in_tab(current_tab, vl->parent->alt, NULL,
346 l = TAILQ_NEXT(vl->parent, lines);
347 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
348 if (l->type == LINE_PRE_END)
350 l->flags ^= L_HIDDEN;
351 if (l->flags & L_HIDDEN)
358 case LINE_DOWNLOAD_DONE:
359 minibuffer_read("Execute: ", do_exec_command,
361 snprintf(ministate.buf, sizeof(ministate.buf),
362 "xdg-open %s", vl->parent->alt);
370 cmd_push_button_new_tab(struct buffer *buffer)
374 vl = buffer->current_line;
375 if (vl == NULL || vl->parent->type != LINE_LINK)
378 new_tab(vl->parent->alt, current_tab->hist_cur->h, current_tab);
382 cmd_previous_button(struct buffer *buffer)
384 struct excursion place;
386 save_excursion(&place, buffer);
389 if (!forward_line(buffer, -1)) {
390 restore_excursion(&place, buffer);
391 message("No previous link");
394 } while (buffer->current_line->parent->type != LINE_LINK);
398 cmd_next_button(struct buffer *buffer)
400 struct excursion place;
402 save_excursion(&place, buffer);
405 if (!forward_line(buffer, +1)){
406 restore_excursion(&place, buffer);
407 message("No next link");
410 } while (buffer->current_line->parent->type != LINE_LINK);
414 is_heading(const struct line *l)
416 return l->type == LINE_TITLE_1 ||
417 l->type == LINE_TITLE_2 ||
418 l->type == LINE_TITLE_3;
422 cmd_previous_heading(struct buffer *buffer)
424 struct excursion place;
426 save_excursion(&place, buffer);
429 if (!forward_line(buffer, -1)) {
430 restore_excursion(&place, buffer);
431 message("No previous heading");
434 } while (!is_heading(buffer->current_line->parent));
438 cmd_next_heading(struct buffer *buffer)
440 struct excursion place;
442 save_excursion(&place, buffer);
445 if (!forward_line(buffer, +1)) {
446 restore_excursion(&place, buffer);
447 message("No next heading");
450 } while (!is_heading(buffer->current_line->parent));
454 cmd_previous_page(struct buffer *buffer)
456 if (!load_previous_page(current_tab))
457 message("No previous page");
461 cmd_next_page(struct buffer *buffer)
463 if (!load_next_page(current_tab))
464 message("No next page");
468 cmd_clear_minibuf(struct buffer *buffer)
474 cmd_execute_extended_command(struct buffer *buffer)
478 GUARD_RECURSIVE_MINIBUFFER();
480 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
481 &eecmd_history, compl_eecmd, NULL, 1);
483 len = sizeof(ministate.prompt);
484 strlcpy(ministate.prompt, "", len);
487 strlcat(ministate.prompt, "M-", len);
489 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
492 strlcat(ministate.prompt, " ", len);
496 cmd_tab_close(struct buffer *buffer)
502 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
503 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
507 message("Can't close the only tab.");
512 cmd_tab_close_other(struct buffer *buffer)
516 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
517 if (t == current_tab)
525 cmd_tab_undo_close(struct buffer *buffer)
529 if ((t = unkill_tab()) == NULL) {
530 message("No recently-closed tabs");
538 cmd_tab_new(struct buffer *buffer)
542 if ((url = new_tab_url) == NULL)
545 new_tab(url, NULL, NULL);
549 cmd_tab_next(struct buffer *buffer)
553 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
554 t = TAILQ_FIRST(&tabshead);
559 cmd_tab_previous(struct buffer *buffer)
563 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
564 t = TAILQ_LAST(&tabshead, tabshead);
569 cmd_tab_move(struct buffer *buffer)
573 t = TAILQ_NEXT(current_tab, tabs);
574 TAILQ_REMOVE(&tabshead, current_tab, tabs);
577 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
579 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
583 cmd_tab_move_to(struct buffer *buffer)
587 t = TAILQ_PREV(current_tab, tabshead, tabs);
588 TAILQ_REMOVE(&tabshead, current_tab, tabs);
591 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
593 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
597 cmd_tab_select(struct buffer *buffer)
599 GUARD_RECURSIVE_MINIBUFFER();
601 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
602 NULL, compl_ts, NULL, 1);
603 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
607 cmd_load_url(struct buffer *buffer)
609 GUARD_RECURSIVE_MINIBUFFER();
611 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
612 &lu_history, compl_lu, NULL, 0);
613 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
617 cmd_load_current_url(struct buffer *buffer)
619 GUARD_RECURSIVE_MINIBUFFER();
621 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
622 &lu_history, compl_lu, NULL, 0);
623 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
624 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
625 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
629 cmd_reload_page(struct buffer *buffer)
631 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL,
632 LU_MODE_NOHIST|LU_MODE_NOCACHE);
636 cmd_bookmark_page(struct buffer *buffer)
638 GUARD_RECURSIVE_MINIBUFFER();
640 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
642 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
643 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
644 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
648 cmd_list_bookmarks(struct buffer *buffer)
650 load_url_in_tab(current_tab, "about:bookmarks", NULL, LU_MODE_NONE);
654 cmd_toggle_help(struct buffer *buffer)
656 ui_toggle_side_window(SIDE_WINDOW_LEFT);
660 cmd_link_select(struct buffer *buffer)
664 GUARD_RECURSIVE_MINIBUFFER();
666 l = TAILQ_FIRST(&buffer->page.head);
667 while (l != NULL && l->type != LINE_LINK)
668 l = TAILQ_NEXT(l, lines);
671 message("No links found");
675 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
676 NULL, compl_ls, l, 1);
677 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
681 cmd_swiper(struct buffer *buffer)
683 GUARD_RECURSIVE_MINIBUFFER();
685 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
686 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head), 1);
687 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
691 cmd_toc(struct buffer *buffer)
695 GUARD_RECURSIVE_MINIBUFFER();
697 l = TAILQ_FIRST(&buffer->page.head);
699 l->type != LINE_TITLE_1 &&
700 l->type != LINE_TITLE_2 &&
701 l->type != LINE_TITLE_3)
702 l = TAILQ_NEXT(l, lines);
705 message("No headings found");
709 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
710 NULL, compl_toc, l, 1);
711 strlcpy(ministate.prompt, "Select heading: ",
712 sizeof(ministate.prompt));
716 cmd_inc_fill_column(struct buffer *buffer)
718 if (fill_column == INT_MAX)
722 message("fill-column: %d", fill_column);
724 ui_schedule_redraw();
728 cmd_dec_fill_column(struct buffer *buffer)
730 if (fill_column == INT_MAX || fill_column < 8)
734 message("fill-column: %d", fill_column);
736 ui_schedule_redraw();
740 cmd_olivetti_mode(struct buffer *buffer)
742 olivetti_mode = !olivetti_mode;
744 message("olivetti-mode enabled");
746 message("olivetti-mode disabled");
748 ui_schedule_redraw();
752 cmd_mini_delete_char(struct buffer *buffer)
758 minibuffer_taint_hist();
760 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
765 memmove(c, n, strlen(n)+1);
767 recompute_completions(0);
771 cmd_mini_delete_backward_char(struct buffer *buffer)
777 minibuffer_taint_hist();
779 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
780 start = buffer->current_line->line;
783 p = utf8_prev_cp(c-1, start);
785 memmove(p, c, strlen(c)+1);
788 recompute_completions(0);
792 cmd_mini_kill_line(struct buffer *buffer)
798 minibuffer_taint_hist();
799 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
802 recompute_completions(0);
806 cmd_mini_kill_whole_line(struct buffer *buffer)
810 minibuffer_taint_hist();
811 *buffer->current_line->line = '\0';
816 cmd_mini_abort(struct buffer *buffer)
825 cmd_mini_complete_and_exit(struct buffer *buffer)
832 if (ministate.compl.must_select && ministate.hist_cur == NULL) {
833 vl = ministate.compl.buffer.current_line;
834 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
835 vl->parent->type == LINE_COMPL) {
841 minibuffer_taint_hist();
846 cmd_mini_previous_history_element(struct buffer *buffer)
848 if (ministate.history == NULL) {
849 message("No history");
853 if (ministate.hist_cur == NULL ||
854 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
855 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
856 ministate.hist_off = ministate.history->len - 1;
857 if (ministate.hist_cur == NULL)
858 message("No prev history item");
860 ministate.hist_off--;
863 if (ministate.hist_cur != NULL) {
864 buffer->current_line->line = ministate.hist_cur->h;
865 recompute_completions(0);
870 cmd_mini_next_history_element(struct buffer *buffer)
872 if (ministate.history == NULL) {
873 message("No history");
877 if (ministate.hist_cur == NULL ||
878 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
879 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
880 ministate.hist_off = 0;
881 if (ministate.hist_cur == NULL)
882 message("No next history item");
884 ministate.hist_off++;
887 if (ministate.hist_cur != NULL) {
888 buffer->current_line->line = ministate.hist_cur->h;
889 recompute_completions(0);
894 cmd_previous_completion(struct buffer *buffer)
896 if (in_minibuffer != MB_COMPREAD)
899 buffer = &ministate.compl.buffer;
900 if (buffer->current_line == NULL)
903 buffer->current_line->parent->type = LINE_COMPL;
904 if (!forward_line(buffer, -1))
905 buffer->current_line->parent->type = LINE_COMPL;
907 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
911 cmd_next_completion(struct buffer *buffer)
913 if (in_minibuffer != MB_COMPREAD)
916 buffer = &ministate.compl.buffer;
917 if (buffer->current_line == NULL)
920 if (buffer->current_line->parent->type == LINE_COMPL_CURRENT) {
921 buffer->current_line->parent->type = LINE_COMPL;
922 forward_line(buffer, +1);
925 if (buffer->current_line != NULL)
926 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
930 cmd_insert_current_candidate(struct buffer *buffer)
932 if (in_minibuffer != MB_COMPREAD)
935 minibuffer_insert_current_candidate();
939 cmd_suspend_telescope(struct buffer *buffer)
946 cmd_toggle_pre_wrap(struct buffer *buffer)
948 dont_wrap_pre = !dont_wrap_pre;
951 message("Don't wrap preformatted blocks");
953 message("Wrap preformatted blocks");
955 ui_schedule_redraw();
959 cmd_mini_goto_beginning(struct buffer *buffer)
966 buffer = &ministate.compl.buffer;
968 if ((vl = buffer->current_line) != NULL)
969 vl->parent->type = LINE_COMPL;
971 vl = TAILQ_FIRST(&buffer->head);
972 while (vl != NULL && vl->parent->flags & L_HIDDEN)
973 vl = TAILQ_NEXT(vl, vlines);
978 vl->parent->type = LINE_COMPL_CURRENT;
979 buffer->top_line = vl;
980 buffer->current_line = vl;
984 cmd_mini_goto_end(struct buffer *buffer)
991 buffer = &ministate.compl.buffer;
993 if ((vl = buffer->current_line) != NULL)
994 vl->parent->type = LINE_COMPL;
996 vl = TAILQ_LAST(&buffer->head, vhead);
997 while (vl != NULL && vl->parent->flags & L_HIDDEN)
998 vl = TAILQ_PREV(vl, vhead, vlines);
1003 vl->parent->type = LINE_COMPL_CURRENT;
1004 buffer->current_line = vl;
1008 cmd_other_window(struct buffer *buffer)
1014 cmd_mini_scroll_up(struct buffer *buffer)
1019 buffer = &ministate.compl.buffer;
1020 if (buffer->current_line == NULL)
1023 buffer->current_line->parent->type = LINE_COMPL;
1024 cmd_scroll_up(buffer);
1025 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
1029 cmd_mini_scroll_down(struct buffer *buffer)
1034 buffer = &ministate.compl.buffer;
1035 if (buffer->current_line == NULL)
1038 buffer->current_line->parent->type = LINE_COMPL;
1039 cmd_scroll_down(buffer);
1040 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
1044 cmd_toggle_downloads(struct buffer *buffer)
1046 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1050 cmd_cache_info(struct buffer *buffer)
1053 char fmt[FMT_SCALED_STRSIZE];
1055 mcache_info(&npages, &tot);
1057 if (fmt_scaled(tot, fmt) == 0)
1058 message("pages: %zu, total: %s", npages, fmt);
1060 message("pages: %zu, total: %zu", npages, tot);
1064 cmd_reply_last_input(struct buffer *buffer)
1066 GUARD_RECURSIVE_MINIBUFFER();
1068 if (current_tab->last_input_url == NULL) {
1069 message("there was no previous input request in this tab");
1073 if (!strncmp(current_tab->last_input_url, "gopher", 6)) {
1074 load_url_in_tab(current_tab, current_tab->last_input_url,
1075 NULL, LU_MODE_NOCACHE);
1079 message("%s", current_tab->last_input_url);
1080 ui_require_input(current_tab, 0, ir_select_reply);
1084 cmd_write_buffer(struct buffer *buffer)
1086 const char *f, *url;
1087 char path[PATH_MAX];
1089 GUARD_RECURSIVE_MINIBUFFER();
1092 message("Can't write buffer in safe-mode.");
1096 url = current_tab->hist_cur->h;
1098 if ((f = strrchr(url, '/')) != NULL)
1100 if (f == NULL || *f == '\0') {
1101 /* guess a decent file name based on the protocol used */
1102 if (!strncmp(url, "gemini://", 9))
1108 strlcpy(path, download_path, sizeof(path));
1109 strlcat(path, f, sizeof(path));
1111 ui_read("Write file", write_buffer, current_tab, path);