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)
113 if (buffer->current_line == NULL)
115 if (buffer->current_line->cplen > buffer->cpoff)
120 cmd_backward_paragraph(struct buffer *buffer)
123 if (!forward_line(buffer, -1)) {
124 message("No previous paragraph");
127 } while (buffer->current_line->len != 0 ||
128 buffer->current_line->parent->type != LINE_TEXT);
132 cmd_forward_paragraph(struct buffer *buffer)
135 if (!forward_line(buffer, +1)) {
136 message("No next paragraph");
139 } while (buffer->current_line->len != 0 ||
140 buffer->current_line->parent->type != LINE_TEXT);
144 cmd_move_beginning_of_line(struct buffer *buffer)
150 cmd_move_end_of_line(struct buffer *buffer)
154 vl = buffer->current_line;
157 buffer->cpoff = vl->cplen;
161 cmd_redraw(struct buffer *buffer)
163 ui_schedule_redraw();
167 cmd_scroll_line_up(struct buffer *buffer)
172 if (buffer->top_line == NULL)
175 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
179 buffer->top_line = vl;
181 if (vl->parent->flags & L_HIDDEN)
189 forward_line(buffer, -1);
193 cmd_scroll_line_down(struct buffer *buffer)
195 if (!forward_line(buffer, +1))
199 if (buffer->top_line == NULL)
202 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
203 if (buffer->top_line->parent->flags & L_HIDDEN)
212 cmd_scroll_up(struct buffer *buffer)
217 if (buffer->top_line == NULL)
220 for (i = 0; i < body_lines; ++i) {
221 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
225 buffer->top_line = vl;
226 forward_line(buffer, -1);
231 cmd_scroll_down(struct buffer *buffer)
235 if (buffer->top_line == NULL)
238 for (i = 0; i < body_lines; ++i) {
239 if (!forward_line(buffer, +1))
242 buffer->top_line = TAILQ_NEXT(buffer->top_line,
249 cmd_beginning_of_buffer(struct buffer *buffer)
251 buffer->current_line = TAILQ_FIRST(&buffer->head);
253 buffer->top_line = buffer->current_line;
254 buffer->line_off = 0;
258 cmd_end_of_buffer(struct buffer *buffer)
260 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
262 if (buffer->current_line == NULL)
265 /* deal with invisible lines */
266 if (buffer->current_line->parent->flags & L_HIDDEN)
267 forward_line(buffer, -1);
269 cmd_move_end_of_line(buffer);
273 kill_telescope_cb(int r, struct tab *tab)
282 cmd_kill_telescope(struct buffer *buffer)
284 yornp("really quit?", kill_telescope_cb, NULL);
288 cmd_push_button(struct buffer *buffer)
293 vl = buffer->current_line;
298 switch (vl->parent->type) {
300 load_url_in_tab(current_tab, vl->parent->alt, NULL,
304 l = TAILQ_NEXT(vl->parent, lines);
305 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
306 if (l->type == LINE_PRE_END)
308 l->flags ^= L_HIDDEN;
309 if (l->flags & L_HIDDEN)
321 cmd_push_button_new_tab(struct buffer *buffer)
325 vl = buffer->current_line;
326 if (vl == NULL || vl->parent->type != LINE_LINK)
329 new_tab(vl->parent->alt, current_tab->hist_cur->h, current_tab);
333 cmd_previous_button(struct buffer *buffer)
335 struct excursion place;
337 save_excursion(&place, buffer);
340 if (!forward_line(buffer, -1)) {
341 restore_excursion(&place, buffer);
342 message("No previous link");
345 } while (buffer->current_line->parent->type != LINE_LINK);
349 cmd_next_button(struct buffer *buffer)
351 struct excursion place;
353 save_excursion(&place, buffer);
356 if (!forward_line(buffer, +1)){
357 restore_excursion(&place, buffer);
358 message("No next link");
361 } while (buffer->current_line->parent->type != LINE_LINK);
365 is_heading(const struct line *l)
367 return l->type == LINE_TITLE_1 ||
368 l->type == LINE_TITLE_2 ||
369 l->type == LINE_TITLE_3;
373 cmd_previous_heading(struct buffer *buffer)
375 struct excursion place;
377 save_excursion(&place, buffer);
380 if (!forward_line(buffer, -1)) {
381 restore_excursion(&place, buffer);
382 message("No previous heading");
385 } while (!is_heading(buffer->current_line->parent));
389 cmd_next_heading(struct buffer *buffer)
391 struct excursion place;
393 save_excursion(&place, buffer);
396 if (!forward_line(buffer, +1)) {
397 restore_excursion(&place, buffer);
398 message("No next heading");
401 } while (!is_heading(buffer->current_line->parent));
405 cmd_previous_page(struct buffer *buffer)
407 if (!load_previous_page(current_tab))
408 message("No previous page");
412 cmd_next_page(struct buffer *buffer)
414 if (!load_next_page(current_tab))
415 message("No next page");
419 cmd_clear_minibuf(struct buffer *buffer)
425 cmd_execute_extended_command(struct buffer *buffer)
429 GUARD_RECURSIVE_MINIBUFFER();
431 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
432 &eecmd_history, compl_eecmd, NULL, 1);
434 len = sizeof(ministate.prompt);
435 strlcpy(ministate.prompt, "", len);
438 strlcat(ministate.prompt, "M-", len);
440 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
443 strlcat(ministate.prompt, " ", len);
447 cmd_tab_close(struct buffer *buffer)
453 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
454 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
458 message("Can't close the only tab.");
463 cmd_tab_close_other(struct buffer *buffer)
467 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
468 if (t == current_tab)
476 cmd_tab_undo_close(struct buffer *buffer)
480 if ((t = unkill_tab()) == NULL) {
481 message("No recently-closed tabs");
489 cmd_tab_new(struct buffer *buffer)
493 if ((url = new_tab_url) == NULL)
496 new_tab(url, NULL, NULL);
500 cmd_tab_next(struct buffer *buffer)
504 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
505 t = TAILQ_FIRST(&tabshead);
510 cmd_tab_previous(struct buffer *buffer)
514 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
515 t = TAILQ_LAST(&tabshead, tabshead);
520 cmd_tab_move(struct buffer *buffer)
524 t = TAILQ_NEXT(current_tab, tabs);
525 TAILQ_REMOVE(&tabshead, current_tab, tabs);
528 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
530 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
534 cmd_tab_move_to(struct buffer *buffer)
538 t = TAILQ_PREV(current_tab, tabshead, tabs);
539 TAILQ_REMOVE(&tabshead, current_tab, tabs);
542 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
544 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
548 cmd_tab_select(struct buffer *buffer)
550 GUARD_RECURSIVE_MINIBUFFER();
552 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
553 NULL, compl_ts, NULL, 1);
554 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
558 cmd_load_url(struct buffer *buffer)
560 GUARD_RECURSIVE_MINIBUFFER();
562 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
563 &lu_history, compl_lu, NULL, 0);
564 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
568 cmd_load_current_url(struct buffer *buffer)
570 GUARD_RECURSIVE_MINIBUFFER();
572 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
573 &lu_history, compl_lu, NULL, 0);
574 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
575 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
576 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
580 cmd_reload_page(struct buffer *buffer)
582 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL,
583 LU_MODE_NOHIST|LU_MODE_NOCACHE);
587 cmd_bookmark_page(struct buffer *buffer)
589 GUARD_RECURSIVE_MINIBUFFER();
591 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
593 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
594 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
595 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
599 cmd_list_bookmarks(struct buffer *buffer)
601 load_url_in_tab(current_tab, "about:bookmarks", NULL, LU_MODE_NONE);
605 cmd_toggle_help(struct buffer *buffer)
607 ui_toggle_side_window(SIDE_WINDOW_LEFT);
611 cmd_link_select(struct buffer *buffer)
615 GUARD_RECURSIVE_MINIBUFFER();
617 l = TAILQ_FIRST(&buffer->page.head);
618 while (l != NULL && l->type != LINE_LINK)
619 l = TAILQ_NEXT(l, lines);
622 message("No links found");
626 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
627 NULL, compl_ls, l, 1);
628 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
632 cmd_swiper(struct buffer *buffer)
634 GUARD_RECURSIVE_MINIBUFFER();
636 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
637 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head), 1);
638 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
642 cmd_toc(struct buffer *buffer)
646 GUARD_RECURSIVE_MINIBUFFER();
648 l = TAILQ_FIRST(&buffer->page.head);
650 l->type != LINE_TITLE_1 &&
651 l->type != LINE_TITLE_2 &&
652 l->type != LINE_TITLE_3)
653 l = TAILQ_NEXT(l, lines);
656 message("No headings found");
660 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
661 NULL, compl_toc, l, 1);
662 strlcpy(ministate.prompt, "Select heading: ",
663 sizeof(ministate.prompt));
667 cmd_inc_fill_column(struct buffer *buffer)
669 if (fill_column == INT_MAX)
673 message("fill-column: %d", fill_column);
675 ui_schedule_redraw();
679 cmd_dec_fill_column(struct buffer *buffer)
681 if (fill_column == INT_MAX || fill_column < 8)
685 message("fill-column: %d", fill_column);
687 ui_schedule_redraw();
691 cmd_olivetti_mode(struct buffer *buffer)
693 olivetti_mode = !olivetti_mode;
695 message("olivetti-mode enabled");
697 message("olivetti-mode disabled");
699 ui_schedule_redraw();
703 cmd_mini_delete_char(struct buffer *buffer)
709 minibuffer_taint_hist();
711 line = buffer->current_line->parent->line + buffer->current_line->from;
712 c = utf8_nth(line, buffer->cpoff);
717 memmove(c, n, strlen(n)+1);
719 recompute_completions(0);
723 cmd_mini_delete_backward_char(struct buffer *buffer)
729 minibuffer_taint_hist();
731 line = buffer->current_line->parent->line + buffer->current_line->from;
732 c = utf8_nth(line, buffer->cpoff);
735 p = utf8_prev_cp(c-1, line);
737 memmove(p, c, strlen(c)+1);
740 recompute_completions(0);
744 cmd_mini_kill_line(struct buffer *buffer)
750 minibuffer_taint_hist();
752 line = buffer->current_line->parent->line + buffer->current_line->from;
753 c = utf8_nth(line, buffer->cpoff);
756 recompute_completions(0);
760 cmd_mini_kill_whole_line(struct buffer *buffer)
764 minibuffer_taint_hist();
765 *buffer->current_line->parent->line = '\0';
770 cmd_mini_abort(struct buffer *buffer)
779 cmd_mini_complete_and_exit(struct buffer *buffer)
786 if (ministate.compl.must_select && ministate.hist_cur == NULL) {
787 vl = ministate.compl.buffer.current_line;
788 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
789 vl->parent->type == LINE_COMPL) {
795 minibuffer_taint_hist();
800 cmd_mini_previous_history_element(struct buffer *buffer)
802 if (ministate.history == NULL) {
803 message("No history");
807 if (ministate.hist_cur == NULL ||
808 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
809 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
810 ministate.hist_off = ministate.history->len - 1;
811 if (ministate.hist_cur == NULL)
812 message("No prev history item");
814 ministate.hist_off--;
817 if (ministate.hist_cur != NULL) {
818 buffer->current_line->parent->line = ministate.hist_cur->h;
819 recompute_completions(0);
824 cmd_mini_next_history_element(struct buffer *buffer)
826 if (ministate.history == NULL) {
827 message("No history");
831 if (ministate.hist_cur == NULL ||
832 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
833 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
834 ministate.hist_off = 0;
835 if (ministate.hist_cur == NULL)
836 message("No next history item");
838 ministate.hist_off++;
841 if (ministate.hist_cur != NULL) {
842 buffer->current_line->parent->line = ministate.hist_cur->h;
843 recompute_completions(0);
848 cmd_previous_completion(struct buffer *buffer)
850 if (in_minibuffer != MB_COMPREAD)
853 buffer = &ministate.compl.buffer;
854 if (buffer->current_line == NULL)
857 buffer->current_line->parent->type = LINE_COMPL;
858 if (!forward_line(buffer, -1))
859 buffer->current_line->parent->type = LINE_COMPL;
861 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
865 cmd_next_completion(struct buffer *buffer)
867 if (in_minibuffer != MB_COMPREAD)
870 buffer = &ministate.compl.buffer;
871 if (buffer->current_line == NULL)
874 if (buffer->current_line->parent->type == LINE_COMPL_CURRENT) {
875 buffer->current_line->parent->type = LINE_COMPL;
876 forward_line(buffer, +1);
879 if (buffer->current_line != NULL)
880 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
884 cmd_insert_current_candidate(struct buffer *buffer)
886 if (in_minibuffer != MB_COMPREAD)
889 minibuffer_insert_current_candidate();
893 cmd_suspend_telescope(struct buffer *buffer)
900 cmd_toggle_pre_wrap(struct buffer *buffer)
902 dont_wrap_pre = !dont_wrap_pre;
905 message("Don't wrap preformatted blocks");
907 message("Wrap preformatted blocks");
909 ui_schedule_redraw();
913 cmd_mini_goto_beginning(struct buffer *buffer)
920 buffer = &ministate.compl.buffer;
922 if ((vl = buffer->current_line) != NULL)
923 vl->parent->type = LINE_COMPL;
925 vl = TAILQ_FIRST(&buffer->head);
926 while (vl != NULL && vl->parent->flags & L_HIDDEN)
927 vl = TAILQ_NEXT(vl, vlines);
932 vl->parent->type = LINE_COMPL_CURRENT;
933 buffer->top_line = vl;
934 buffer->current_line = vl;
938 cmd_mini_goto_end(struct buffer *buffer)
945 buffer = &ministate.compl.buffer;
947 if ((vl = buffer->current_line) != NULL)
948 vl->parent->type = LINE_COMPL;
950 vl = TAILQ_LAST(&buffer->head, vhead);
951 while (vl != NULL && vl->parent->flags & L_HIDDEN)
952 vl = TAILQ_PREV(vl, vhead, vlines);
957 vl->parent->type = LINE_COMPL_CURRENT;
958 buffer->current_line = vl;
962 cmd_other_window(struct buffer *buffer)
968 cmd_mini_scroll_up(struct buffer *buffer)
973 buffer = &ministate.compl.buffer;
974 if (buffer->current_line == NULL)
977 buffer->current_line->parent->type = LINE_COMPL;
978 cmd_scroll_up(buffer);
979 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
983 cmd_mini_scroll_down(struct buffer *buffer)
988 buffer = &ministate.compl.buffer;
989 if (buffer->current_line == NULL)
992 buffer->current_line->parent->type = LINE_COMPL;
993 cmd_scroll_down(buffer);
994 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
998 cmd_toggle_downloads(struct buffer *buffer)
1000 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1004 cmd_cache_info(struct buffer *buffer)
1007 char fmt[FMT_SCALED_STRSIZE];
1009 mcache_info(&npages, &tot);
1011 if (fmt_scaled(tot, fmt) == 0)
1012 message("pages: %zu, total: %s", npages, fmt);
1014 message("pages: %zu, total: %zu", npages, tot);
1018 cmd_reply_last_input(struct buffer *buffer)
1020 GUARD_RECURSIVE_MINIBUFFER();
1022 if (current_tab->last_input_url == NULL) {
1023 message("there was no previous input request in this tab");
1027 if (!strncmp(current_tab->last_input_url, "gopher", 6)) {
1028 load_url_in_tab(current_tab, current_tab->last_input_url,
1029 NULL, LU_MODE_NOCACHE);
1033 message("%s", current_tab->last_input_url);
1034 ui_require_input(current_tab, 0, ir_select_reply);
1038 cmd_write_buffer(struct buffer *buffer)
1040 const char *f, *url;
1041 char path[PATH_MAX];
1043 GUARD_RECURSIVE_MINIBUFFER();
1046 message("Can't write buffer in safe-mode.");
1050 url = current_tab->hist_cur->h;
1052 if ((f = strrchr(url, '/')) != NULL)
1054 if (f == NULL || *f == '\0') {
1055 /* guess a decent file name based on the protocol used */
1056 if (!strncmp(url, "gemini://", 9))
1062 strlcpy(path, download_path, sizeof(path));
1063 strlcat(path, f, sizeof(path));
1065 ui_read("Write file", write_buffer, current_tab, path);