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.
25 #include "minibuffer.h"
27 #include "telescope.h"
31 #define GUARD_RECURSIVE_MINIBUFFER() \
33 if (in_minibuffer) { \
34 message("enable-recursive-minibuffers " \
35 "is not yet available."); \
40 #define GUARD_READ_ONLY() \
42 if (!in_minibuffer) { \
43 message("text is read-only"); \
48 /* return 1 if moved, 0 otherwise */
50 forward_line(struct buffer *buffer, int n)
55 if (buffer->current_line == NULL)
57 vl = buffer->current_line;
62 vl = TAILQ_NEXT(vl, vlines);
65 if (vl->parent->flags & L_HIDDEN)
67 buffer->current_line = vl;
70 vl = TAILQ_PREV(vl, vhead, vlines);
73 if (vl->parent->flags & L_HIDDEN)
75 if (buffer->current_line == buffer->top_line) {
77 buffer->top_line = vl;
79 buffer->current_line = vl;
90 cmd_previous_line(struct buffer *buffer)
92 forward_line(buffer, -1);
96 cmd_next_line(struct buffer *buffer)
98 forward_line(buffer, +1);
102 cmd_backward_char(struct buffer *buffer)
104 if (buffer->cpoff != 0)
109 cmd_forward_char(struct buffer *buffer)
113 if (buffer->current_line == NULL)
116 if (buffer->current_line->line != NULL)
117 len = utf8_cplen(buffer->current_line->line);
118 if (++buffer->cpoff > len)
123 cmd_backward_paragraph(struct buffer *buffer)
126 if (!forward_line(buffer, -1)) {
127 message("No previous paragraph");
130 } while (buffer->current_line->line != NULL ||
131 buffer->current_line->parent->type != LINE_TEXT);
135 cmd_forward_paragraph(struct buffer *buffer)
138 if (!forward_line(buffer, +1)) {
139 message("No next paragraph");
142 } while (buffer->current_line->line != NULL ||
143 buffer->current_line->parent->type != LINE_TEXT);
147 cmd_move_beginning_of_line(struct buffer *buffer)
153 cmd_move_end_of_line(struct buffer *buffer)
157 vl = buffer->current_line;
158 if (vl == NULL || vl->line == NULL)
160 buffer->cpoff = utf8_cplen(vl->line);
164 cmd_redraw(struct buffer *buffer)
166 ui_schedule_redraw();
170 cmd_scroll_line_up(struct buffer *buffer)
175 if (buffer->top_line == NULL)
178 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
182 buffer->top_line = vl;
184 if (vl->parent->flags & L_HIDDEN)
192 forward_line(buffer, -1);
196 cmd_scroll_line_down(struct buffer *buffer)
198 if (!forward_line(buffer, +1))
202 if (buffer->top_line == NULL)
205 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
206 if (buffer->top_line->parent->flags & L_HIDDEN)
215 cmd_scroll_up(struct buffer *buffer)
220 if (buffer->top_line == NULL)
223 for (i = 0; i < body_lines; ++i) {
224 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
228 buffer->top_line = vl;
229 forward_line(buffer, -1);
234 cmd_scroll_down(struct buffer *buffer)
238 if (buffer->top_line == NULL)
241 for (i = 0; i < body_lines; ++i) {
242 if (!forward_line(buffer, +1))
245 buffer->top_line = TAILQ_NEXT(buffer->top_line,
252 cmd_beginning_of_buffer(struct buffer *buffer)
254 buffer->current_line = TAILQ_FIRST(&buffer->head);
256 buffer->top_line = buffer->current_line;
257 buffer->line_off = 0;
261 cmd_end_of_buffer(struct buffer *buffer)
263 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
265 if (buffer->current_line == NULL)
268 /* deal with invisible lines */
269 if (buffer->current_line->parent->flags & L_HIDDEN)
270 forward_line(buffer, -1);
272 cmd_move_end_of_line(buffer);
276 kill_telescope_cb(int r, struct tab *tab)
285 cmd_kill_telescope(struct buffer *buffer)
287 yornp("really quit?", kill_telescope_cb, NULL);
291 cmd_push_button(struct buffer *buffer)
296 vl = buffer->current_line;
301 switch (vl->parent->type) {
303 load_url_in_tab(current_tab, vl->parent->alt, NULL, 0);
306 l = TAILQ_NEXT(vl->parent, lines);
307 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
308 if (l->type == LINE_PRE_END)
310 l->flags ^= L_HIDDEN;
311 if (l->flags & L_HIDDEN)
323 cmd_push_button_new_tab(struct buffer *buffer)
327 vl = buffer->current_line;
328 if (vl == NULL || vl->parent->type != LINE_LINK)
331 new_tab(vl->parent->alt, current_tab->hist_cur->h, current_tab);
335 cmd_previous_button(struct buffer *buffer)
337 struct excursion place;
339 save_excursion(&place, buffer);
342 if (!forward_line(buffer, -1)) {
343 restore_excursion(&place, buffer);
344 message("No previous link");
347 } while (buffer->current_line->parent->type != LINE_LINK);
351 cmd_next_button(struct buffer *buffer)
353 struct excursion place;
355 save_excursion(&place, buffer);
358 if (!forward_line(buffer, +1)){
359 restore_excursion(&place, buffer);
360 message("No next link");
363 } while (buffer->current_line->parent->type != LINE_LINK);
367 is_heading(const struct line *l)
369 return l->type == LINE_TITLE_1 ||
370 l->type == LINE_TITLE_2 ||
371 l->type == LINE_TITLE_3;
375 cmd_previous_heading(struct buffer *buffer)
377 struct excursion place;
379 save_excursion(&place, buffer);
382 if (!forward_line(buffer, -1)) {
383 restore_excursion(&place, buffer);
384 message("No previous heading");
387 } while (!is_heading(buffer->current_line->parent));
391 cmd_next_heading(struct buffer *buffer)
393 struct excursion place;
395 save_excursion(&place, buffer);
398 if (!forward_line(buffer, +1)) {
399 restore_excursion(&place, buffer);
400 message("No next heading");
403 } while (!is_heading(buffer->current_line->parent));
407 cmd_previous_page(struct buffer *buffer)
409 if (!load_previous_page(current_tab))
410 message("No previous page");
412 start_loading_anim(current_tab);
416 cmd_next_page(struct buffer *buffer)
418 if (!load_next_page(current_tab))
419 message("No next page");
421 start_loading_anim(current_tab);
425 cmd_clear_minibuf(struct buffer *buffer)
431 cmd_execute_extended_command(struct buffer *buffer)
435 GUARD_RECURSIVE_MINIBUFFER();
437 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
438 &eecmd_history, compl_eecmd, NULL);
440 len = sizeof(ministate.prompt);
441 strlcpy(ministate.prompt, "", len);
444 strlcat(ministate.prompt, "M-", len);
446 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
449 strlcat(ministate.prompt, " ", len);
453 cmd_tab_close(struct buffer *buffer)
459 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
460 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
464 message("Can't close the only tab.");
469 cmd_tab_close_other(struct buffer *buffer)
473 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
474 if (t == current_tab)
482 cmd_tab_new(struct buffer *buffer)
486 if ((url = new_tab_url) == NULL)
489 new_tab(url, NULL, NULL);
493 cmd_tab_next(struct buffer *buffer)
497 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
498 t = TAILQ_FIRST(&tabshead);
503 cmd_tab_previous(struct buffer *buffer)
507 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
508 t = TAILQ_LAST(&tabshead, tabshead);
513 cmd_tab_move(struct buffer *buffer)
517 t = TAILQ_NEXT(current_tab, tabs);
518 TAILQ_REMOVE(&tabshead, current_tab, tabs);
521 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
523 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
527 cmd_tab_move_to(struct buffer *buffer)
531 t = TAILQ_PREV(current_tab, tabshead, tabs);
532 TAILQ_REMOVE(&tabshead, current_tab, tabs);
535 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
537 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
541 cmd_tab_select(struct buffer *buffer)
543 GUARD_RECURSIVE_MINIBUFFER();
545 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
546 NULL, compl_ts, NULL);
547 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
551 cmd_load_url(struct buffer *buffer)
553 GUARD_RECURSIVE_MINIBUFFER();
555 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
556 &lu_history, NULL, NULL);
557 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
561 cmd_load_current_url(struct buffer *buffer)
563 GUARD_RECURSIVE_MINIBUFFER();
565 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
566 &lu_history, NULL, NULL);
567 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
568 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
569 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
573 cmd_reload_page(struct buffer *buffer)
575 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL, 1);
579 cmd_bookmark_page(struct buffer *buffer)
581 GUARD_RECURSIVE_MINIBUFFER();
583 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
585 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
586 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
587 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
591 cmd_list_bookmarks(struct buffer *buffer)
593 load_url_in_tab(current_tab, "about:bookmarks", NULL, 0);
597 cmd_toggle_help(struct buffer *buffer)
599 ui_toggle_side_window(SIDE_WINDOW_LEFT);
603 cmd_link_select(struct buffer *buffer)
607 GUARD_RECURSIVE_MINIBUFFER();
609 l = TAILQ_FIRST(&buffer->page.head);
610 while (l != NULL && l->type != LINE_LINK)
611 l = TAILQ_NEXT(l, lines);
614 message("No links found");
618 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
620 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
624 cmd_swiper(struct buffer *buffer)
626 GUARD_RECURSIVE_MINIBUFFER();
628 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
629 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
630 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
634 cmd_toc(struct buffer *buffer)
638 GUARD_RECURSIVE_MINIBUFFER();
640 l = TAILQ_FIRST(&buffer->page.head);
642 l->type != LINE_TITLE_1 &&
643 l->type != LINE_TITLE_2 &&
644 l->type != LINE_TITLE_3)
645 l = TAILQ_NEXT(l, lines);
648 message("No headings found");
652 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
654 strlcpy(ministate.prompt, "Select heading: ",
655 sizeof(ministate.prompt));
659 cmd_inc_fill_column(struct buffer *buffer)
661 if (fill_column == INT_MAX)
665 message("fill-column: %d", fill_column);
667 ui_schedule_redraw();
671 cmd_dec_fill_column(struct buffer *buffer)
673 if (fill_column == INT_MAX || fill_column < 8)
677 message("fill-column: %d", fill_column);
679 ui_schedule_redraw();
683 cmd_olivetti_mode(struct buffer *buffer)
685 olivetti_mode = !olivetti_mode;
687 message("olivetti-mode enabled");
689 message("olivetti-mode disabled");
691 ui_schedule_redraw();
695 cmd_mini_delete_char(struct buffer *buffer)
701 minibuffer_taint_hist();
703 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
708 memmove(c, n, strlen(n)+1);
710 recompute_completions(0);
714 cmd_mini_delete_backward_char(struct buffer *buffer)
720 minibuffer_taint_hist();
722 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
723 start = buffer->current_line->line;
726 p = utf8_prev_cp(c-1, start);
728 memmove(p, c, strlen(c)+1);
731 recompute_completions(0);
735 cmd_mini_kill_line(struct buffer *buffer)
741 minibuffer_taint_hist();
742 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
745 recompute_completions(0);
749 cmd_mini_abort(struct buffer *buffer)
758 cmd_mini_complete_and_exit(struct buffer *buffer)
763 minibuffer_taint_hist();
768 cmd_mini_previous_history_element(struct buffer *buffer)
770 if (ministate.history == NULL) {
771 message("No history");
775 if (ministate.hist_cur == NULL ||
776 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
777 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
778 ministate.hist_off = ministate.history->len - 1;
779 if (ministate.hist_cur == NULL)
780 message("No prev history item");
782 ministate.hist_off--;
785 if (ministate.hist_cur != NULL)
786 buffer->current_line->line = ministate.hist_cur->h;
790 cmd_mini_next_history_element(struct buffer *buffer)
792 if (ministate.history == NULL) {
793 message("No history");
797 if (ministate.hist_cur == NULL ||
798 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
799 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
800 ministate.hist_off = 0;
801 if (ministate.hist_cur == NULL)
802 message("No next history item");
804 ministate.hist_off++;
807 if (ministate.hist_cur != NULL)
808 buffer->current_line->line = ministate.hist_cur->h;
812 cmd_previous_completion(struct buffer *buffer)
814 if (in_minibuffer != MB_COMPREAD)
817 buffer = &ministate.compl.buffer;
819 if (buffer->current_line != NULL)
820 buffer->current_line->parent->type = LINE_COMPL;
822 forward_line(buffer, -1);
824 if (buffer->current_line != NULL)
825 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
829 cmd_next_completion(struct buffer *buffer)
831 if (in_minibuffer != MB_COMPREAD)
834 buffer = &ministate.compl.buffer;
836 if (buffer->current_line != NULL)
837 buffer->current_line->parent->type = LINE_COMPL;
839 forward_line(buffer, +1);
841 if (buffer->current_line != NULL)
842 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
846 cmd_insert_current_candidate(struct buffer *buffer)
848 if (in_minibuffer != MB_COMPREAD)
851 minibuffer_insert_current_candidate();
855 cmd_suspend_telescope(struct buffer *buffer)
862 cmd_toggle_pre_wrap(struct buffer *buffer)
864 dont_wrap_pre = !dont_wrap_pre;
867 message("Don't wrap preformatted blocks");
869 message("Wrap preformatted blocks");
871 ui_schedule_redraw();
875 cmd_mini_goto_beginning(struct buffer *buffer)
882 buffer = &ministate.compl.buffer;
884 if ((vl = buffer->current_line) != NULL)
885 vl->parent->type = LINE_COMPL;
887 vl = TAILQ_FIRST(&buffer->head);
888 while (vl != NULL && vl->parent->flags & L_HIDDEN)
889 vl = TAILQ_NEXT(vl, vlines);
894 vl->parent->type = LINE_COMPL_CURRENT;
895 buffer->top_line = vl;
896 buffer->current_line = vl;
900 cmd_mini_goto_end(struct buffer *buffer)
907 buffer = &ministate.compl.buffer;
909 if ((vl = buffer->current_line) != NULL)
910 vl->parent->type = LINE_COMPL;
912 vl = TAILQ_LAST(&buffer->head, vhead);
913 while (vl != NULL && vl->parent->flags & L_HIDDEN)
914 vl = TAILQ_PREV(vl, vhead, vlines);
919 vl->parent->type = LINE_COMPL_CURRENT;
920 buffer->current_line = vl;
924 cmd_other_window(struct buffer *buffer)
930 cmd_mini_scroll_up(struct buffer *buffer)
935 buffer = &ministate.compl.buffer;
936 if (buffer->current_line == NULL)
939 buffer->current_line->parent->type = LINE_COMPL;
940 cmd_scroll_up(buffer);
941 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
945 cmd_mini_scroll_down(struct buffer *buffer)
950 buffer = &ministate.compl.buffer;
951 if (buffer->current_line == NULL)
954 buffer->current_line->parent->type = LINE_COMPL;
955 cmd_scroll_down(buffer);
956 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
960 cmd_toggle_downloads(struct buffer *buffer)
962 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);