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"
26 #include "telescope.h"
30 #define GUARD_RECURSIVE_MINIBUFFER() \
32 if (in_minibuffer) { \
33 message("enable-recursive-minibuffers " \
34 "is not yet available."); \
39 #define GUARD_READ_ONLY() \
41 if (!in_minibuffer) { \
42 message("text is read-only"); \
47 /* return 1 if moved, 0 otherwise */
49 forward_line(struct buffer *buffer, int n)
54 if (buffer->current_line == NULL)
56 vl = buffer->current_line;
61 vl = TAILQ_NEXT(vl, vlines);
64 if (vl->parent->flags & L_HIDDEN)
66 buffer->current_line = vl;
69 vl = TAILQ_PREV(vl, vhead, vlines);
72 if (vl->parent->flags & L_HIDDEN)
74 if (buffer->current_line == buffer->top_line) {
76 buffer->top_line = vl;
78 buffer->current_line = vl;
89 cmd_previous_line(struct buffer *buffer)
91 forward_line(buffer, -1);
95 cmd_next_line(struct buffer *buffer)
97 forward_line(buffer, +1);
101 cmd_backward_char(struct buffer *buffer)
103 if (buffer->cpoff != 0)
108 cmd_forward_char(struct buffer *buffer)
112 if (buffer->current_line == NULL)
115 if (buffer->current_line->line != NULL)
116 len = utf8_cplen(buffer->current_line->line);
117 if (++buffer->cpoff > len)
122 cmd_backward_paragraph(struct buffer *buffer)
125 if (!forward_line(buffer, -1)) {
126 message("No previous paragraph");
129 } while (buffer->current_line->line != NULL ||
130 buffer->current_line->parent->type != LINE_TEXT);
134 cmd_forward_paragraph(struct buffer *buffer)
137 if (!forward_line(buffer, +1)) {
138 message("No next paragraph");
141 } while (buffer->current_line->line != NULL ||
142 buffer->current_line->parent->type != LINE_TEXT);
146 cmd_move_beginning_of_line(struct buffer *buffer)
152 cmd_move_end_of_line(struct buffer *buffer)
156 vl = buffer->current_line;
157 if (vl == NULL || vl->line == NULL)
159 buffer->cpoff = utf8_cplen(vl->line);
163 cmd_redraw(struct buffer *buffer)
165 ui_schedule_redraw();
169 cmd_scroll_line_up(struct buffer *buffer)
174 if (buffer->top_line == NULL)
177 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
181 buffer->top_line = vl;
183 if (vl->parent->flags & L_HIDDEN)
191 forward_line(buffer, -1);
195 cmd_scroll_line_down(struct buffer *buffer)
197 if (!forward_line(buffer, +1))
201 if (buffer->top_line == NULL)
204 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
205 if (buffer->top_line->parent->flags & L_HIDDEN)
214 cmd_scroll_up(struct buffer *buffer)
219 if (buffer->top_line == NULL)
222 for (i = 0; i < body_lines; ++i) {
223 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
227 buffer->top_line = vl;
228 forward_line(buffer, -1);
233 cmd_scroll_down(struct buffer *buffer)
237 if (buffer->top_line == NULL)
240 for (i = 0; i < body_lines; ++i) {
241 if (!forward_line(buffer, +1))
244 buffer->top_line = TAILQ_NEXT(buffer->top_line,
251 cmd_beginning_of_buffer(struct buffer *buffer)
253 buffer->current_line = TAILQ_FIRST(&buffer->head);
255 buffer->top_line = buffer->current_line;
256 buffer->line_off = 0;
260 cmd_end_of_buffer(struct buffer *buffer)
262 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
264 if (buffer->current_line == NULL)
267 /* deal with invisible lines */
268 if (buffer->current_line->parent->flags & L_HIDDEN)
269 forward_line(buffer, -1);
271 cmd_move_end_of_line(buffer);
275 cmd_kill_telescope(struct buffer *buffer)
282 cmd_push_button(struct buffer *buffer)
287 vl = buffer->current_line;
292 switch (vl->parent->type) {
294 load_url_in_tab(current_tab, vl->parent->alt, NULL, 0);
297 l = TAILQ_NEXT(vl->parent, lines);
298 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
299 if (l->type == LINE_PRE_END)
301 l->flags ^= L_HIDDEN;
302 if (l->flags & L_HIDDEN)
314 cmd_push_button_new_tab(struct buffer *buffer)
318 vl = buffer->current_line;
319 if (vl == NULL || vl->parent->type != LINE_LINK)
322 new_tab(vl->parent->alt, current_tab->hist_cur->h, current_tab);
326 cmd_previous_button(struct buffer *buffer)
328 struct excursion place;
330 save_excursion(&place, buffer);
333 if (!forward_line(buffer, -1)) {
334 restore_excursion(&place, buffer);
335 message("No previous link");
338 } while (buffer->current_line->parent->type != LINE_LINK);
342 cmd_next_button(struct buffer *buffer)
344 struct excursion place;
346 save_excursion(&place, buffer);
349 if (!forward_line(buffer, +1)){
350 restore_excursion(&place, buffer);
351 message("No next link");
354 } while (buffer->current_line->parent->type != LINE_LINK);
358 is_heading(const struct line *l)
360 return l->type == LINE_TITLE_1 ||
361 l->type == LINE_TITLE_2 ||
362 l->type == LINE_TITLE_3;
366 cmd_previous_heading(struct buffer *buffer)
368 struct excursion place;
370 save_excursion(&place, buffer);
373 if (!forward_line(buffer, -1)) {
374 restore_excursion(&place, buffer);
375 message("No previous heading");
378 } while (!is_heading(buffer->current_line->parent));
382 cmd_next_heading(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 next heading");
394 } while (!is_heading(buffer->current_line->parent));
398 cmd_previous_page(struct buffer *buffer)
400 if (!load_previous_page(current_tab))
401 message("No previous page");
403 start_loading_anim(current_tab);
407 cmd_next_page(struct buffer *buffer)
409 if (!load_next_page(current_tab))
410 message("No next page");
412 start_loading_anim(current_tab);
416 cmd_clear_minibuf(struct buffer *buffer)
422 cmd_execute_extended_command(struct buffer *buffer)
426 GUARD_RECURSIVE_MINIBUFFER();
428 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
429 &eecmd_history, compl_eecmd, NULL);
431 len = sizeof(ministate.prompt);
432 strlcpy(ministate.prompt, "", len);
435 strlcat(ministate.prompt, "M-", len);
437 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
440 strlcat(ministate.prompt, " ", len);
444 cmd_tab_close(struct buffer *buffer)
450 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
451 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
455 message("Can't close the only tab.");
460 cmd_tab_close_other(struct buffer *buffer)
464 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
465 if (t == current_tab)
473 cmd_tab_new(struct buffer *buffer)
477 if ((url = new_tab_url) == NULL)
480 new_tab(url, NULL, NULL);
484 cmd_tab_next(struct buffer *buffer)
488 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
489 t = TAILQ_FIRST(&tabshead);
494 cmd_tab_previous(struct buffer *buffer)
498 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
499 t = TAILQ_LAST(&tabshead, tabshead);
504 cmd_tab_move(struct buffer *buffer)
508 t = TAILQ_NEXT(current_tab, tabs);
509 TAILQ_REMOVE(&tabshead, current_tab, tabs);
512 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
514 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
518 cmd_tab_move_to(struct buffer *buffer)
522 t = TAILQ_PREV(current_tab, tabshead, tabs);
523 TAILQ_REMOVE(&tabshead, current_tab, tabs);
526 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
528 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
532 cmd_tab_select(struct buffer *buffer)
534 GUARD_RECURSIVE_MINIBUFFER();
536 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
537 NULL, compl_ts, NULL);
538 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
542 cmd_load_url(struct buffer *buffer)
544 GUARD_RECURSIVE_MINIBUFFER();
546 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
547 &lu_history, NULL, NULL);
548 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
549 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
550 cmd_move_end_of_line(&ministate.buffer);
554 cmd_load_current_url(struct buffer *buffer)
556 GUARD_RECURSIVE_MINIBUFFER();
558 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
559 &lu_history, NULL, NULL);
560 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
561 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
562 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
566 cmd_reload_page(struct buffer *buffer)
568 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL, 1);
572 cmd_bookmark_page(struct buffer *buffer)
574 GUARD_RECURSIVE_MINIBUFFER();
576 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
578 strlcpy(ministate.prompt, "Bookmark 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_list_bookmarks(struct buffer *buffer)
586 load_url_in_tab(current_tab, "about:bookmarks", NULL, 0);
590 cmd_toggle_help(struct buffer *buffer)
592 ui_toggle_side_window();
596 cmd_link_select(struct buffer *buffer)
600 GUARD_RECURSIVE_MINIBUFFER();
602 l = TAILQ_FIRST(&buffer->page.head);
603 while (l != NULL && l->type != LINE_LINK)
604 l = TAILQ_NEXT(l, lines);
607 message("No links found");
611 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
613 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
617 cmd_swiper(struct buffer *buffer)
619 GUARD_RECURSIVE_MINIBUFFER();
621 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
622 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
623 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
627 cmd_toc(struct buffer *buffer)
631 GUARD_RECURSIVE_MINIBUFFER();
633 l = TAILQ_FIRST(&buffer->page.head);
635 l->type != LINE_TITLE_1 &&
636 l->type != LINE_TITLE_2 &&
637 l->type != LINE_TITLE_3)
638 l = TAILQ_NEXT(l, lines);
641 message("No headings found");
645 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
647 strlcpy(ministate.prompt, "Select heading: ",
648 sizeof(ministate.prompt));
652 cmd_inc_fill_column(struct buffer *buffer)
654 if (fill_column == INT_MAX)
658 message("fill-column: %d", fill_column);
660 ui_schedule_redraw();
664 cmd_dec_fill_column(struct buffer *buffer)
666 if (fill_column == INT_MAX || fill_column < 8)
670 message("fill-column: %d", fill_column);
672 ui_schedule_redraw();
676 cmd_olivetti_mode(struct buffer *buffer)
678 olivetti_mode = !olivetti_mode;
680 message("olivetti-mode enabled");
682 message("olivetti-mode disabled");
684 ui_schedule_redraw();
688 cmd_mini_delete_char(struct buffer *buffer)
694 minibuffer_taint_hist();
696 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
701 memmove(c, n, strlen(n)+1);
703 recompute_completions(0);
707 cmd_mini_delete_backward_char(struct buffer *buffer)
713 minibuffer_taint_hist();
715 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
716 start = buffer->current_line->line;
719 p = utf8_prev_cp(c-1, start);
721 memmove(p, c, strlen(c)+1);
724 recompute_completions(0);
728 cmd_mini_kill_line(struct buffer *buffer)
734 minibuffer_taint_hist();
735 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
738 recompute_completions(0);
742 cmd_mini_abort(struct buffer *buffer)
751 cmd_mini_complete_and_exit(struct buffer *buffer)
756 minibuffer_taint_hist();
761 cmd_mini_previous_history_element(struct buffer *buffer)
763 if (ministate.history == NULL) {
764 message("No history");
768 if (ministate.hist_cur == NULL ||
769 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
770 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
771 ministate.hist_off = ministate.history->len - 1;
772 if (ministate.hist_cur == NULL)
773 message("No prev history item");
775 ministate.hist_off--;
778 if (ministate.hist_cur != NULL)
779 buffer->current_line->line = ministate.hist_cur->h;
783 cmd_mini_next_history_element(struct buffer *buffer)
785 if (ministate.history == NULL) {
786 message("No history");
790 if (ministate.hist_cur == NULL ||
791 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
792 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
793 ministate.hist_off = 0;
794 if (ministate.hist_cur == NULL)
795 message("No next history item");
797 ministate.hist_off++;
800 if (ministate.hist_cur != NULL)
801 buffer->current_line->line = ministate.hist_cur->h;
805 cmd_previous_completion(struct buffer *buffer)
807 if (in_minibuffer != MB_COMPREAD)
810 buffer = &ministate.compl.buffer;
812 if (buffer->current_line != NULL)
813 buffer->current_line->parent->type = LINE_COMPL;
815 forward_line(buffer, -1);
817 if (buffer->current_line != NULL)
818 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
822 cmd_next_completion(struct buffer *buffer)
824 if (in_minibuffer != MB_COMPREAD)
827 buffer = &ministate.compl.buffer;
829 if (buffer->current_line != NULL)
830 buffer->current_line->parent->type = LINE_COMPL;
832 forward_line(buffer, +1);
834 if (buffer->current_line != NULL)
835 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
839 cmd_insert_current_candidate(struct buffer *buffer)
843 if (in_minibuffer != MB_COMPREAD)
846 buffer = &ministate.compl.buffer;
847 if ((vl = buffer->current_line) == NULL)
850 minibuffer_taint_hist();
851 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
852 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
856 cmd_suspend_telescope(struct buffer *buffer)
863 cmd_toggle_pre_wrap(struct buffer *buffer)
865 dont_wrap_pre = !dont_wrap_pre;
868 message("Don't wrap preformatted blocks");
870 message("Wrap preformatted blocks");
872 ui_schedule_redraw();
876 cmd_mini_goto_beginning(struct buffer *buffer)
883 buffer = &ministate.compl.buffer;
885 if ((vl = buffer->current_line) != NULL)
886 vl->parent->type = LINE_COMPL;
888 vl = TAILQ_FIRST(&buffer->head);
889 while (vl != NULL && vl->parent->flags & L_HIDDEN)
890 vl = TAILQ_NEXT(vl, vlines);
895 vl->parent->type = LINE_COMPL_CURRENT;
896 buffer->top_line = vl;
897 buffer->current_line = vl;
901 cmd_mini_goto_end(struct buffer *buffer)
908 buffer = &ministate.compl.buffer;
910 if ((vl = buffer->current_line) != NULL)
911 vl->parent->type = LINE_COMPL;
913 vl = TAILQ_LAST(&buffer->head, vhead);
914 while (vl != NULL && vl->parent->flags & L_HIDDEN)
915 vl = TAILQ_PREV(vl, vhead, vlines);
920 vl->parent->type = LINE_COMPL_CURRENT;
921 buffer->current_line = vl;
925 cmd_other_window(struct buffer *buffer)
931 cmd_mini_scroll_up(struct buffer *buffer)
936 buffer = &ministate.compl.buffer;
937 if (buffer->current_line == NULL)
940 buffer->current_line->parent->type = LINE_COMPL;
941 cmd_scroll_up(buffer);
942 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
946 cmd_mini_scroll_down(struct buffer *buffer)
951 buffer = &ministate.compl.buffer;
952 if (buffer->current_line == NULL)
955 buffer->current_line->parent->type = LINE_COMPL;
956 cmd_scroll_down(buffer);
957 buffer->current_line->parent->type = LINE_COMPL_CURRENT;