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.
23 #include "minibuffer.h"
24 #include "telescope.h"
28 /* return 1 if moved, 0 otherwise */
30 forward_line(struct buffer *buffer, int n)
35 if (buffer->current_line == NULL)
37 vl = buffer->current_line;
42 vl = TAILQ_NEXT(vl, vlines);
45 if (vl->parent->flags & L_HIDDEN)
47 buffer->current_line = vl;
50 vl = TAILQ_PREV(vl, vhead, vlines);
53 if (vl->parent->flags & L_HIDDEN)
55 if (buffer->current_line == buffer->top_line)
56 buffer->top_line = vl;
57 buffer->current_line = vl;
68 cmd_previous_line(struct buffer *buffer)
70 forward_line(buffer, -1);
74 cmd_next_line(struct buffer *buffer)
76 forward_line(buffer, +1);
80 cmd_backward_char(struct buffer *buffer)
82 if (buffer->cpoff != 0)
87 cmd_forward_char(struct buffer *buffer)
91 if (buffer->current_line->line != NULL)
92 len = utf8_cplen(buffer->current_line->line);
93 if (++buffer->cpoff > len)
98 cmd_backward_paragraph(struct buffer *buffer)
101 if (!forward_line(buffer, -1)) {
102 message("No previous paragraph");
105 } while (buffer->current_line->line != NULL ||
106 buffer->current_line->parent->type != LINE_TEXT);
110 cmd_forward_paragraph(struct buffer *buffer)
113 if (!forward_line(buffer, +1)) {
114 message("No next paragraph");
117 } while (buffer->current_line->line != NULL ||
118 buffer->current_line->parent->type != LINE_TEXT);
122 cmd_move_beginning_of_line(struct buffer *buffer)
128 cmd_move_end_of_line(struct buffer *buffer)
132 vl = buffer->current_line;
133 if (vl->line == NULL)
135 buffer->cpoff = utf8_cplen(vl->line);
139 cmd_redraw(struct buffer *buffer)
141 ui_schedule_redraw();
145 cmd_scroll_line_up(struct buffer *buffer)
149 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
153 forward_line(buffer, -1);
156 buffer->top_line = vl;
160 cmd_scroll_line_down(struct buffer *buffer)
162 if (!forward_line(buffer, +1))
165 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
170 cmd_scroll_up(struct buffer *buffer)
172 forward_line(buffer, -1*body_lines);
176 cmd_scroll_down(struct buffer *buffer)
178 forward_line(buffer, body_lines);
182 cmd_beginning_of_buffer(struct buffer *buffer)
184 buffer->current_line = TAILQ_FIRST(&buffer->head);
186 buffer->top_line = buffer->current_line;
187 buffer->line_off = 0;
191 cmd_end_of_buffer(struct buffer *buffer)
193 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
194 buffer->cpoff = body_cols;
198 cmd_kill_telescope(struct buffer *buffer)
205 cmd_push_button(struct buffer *buffer)
210 vl = buffer->current_line;
211 switch (vl->parent->type) {
213 load_url_in_tab(current_tab(), vl->parent->alt);
216 l = TAILQ_NEXT(vl->parent, lines);
217 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
218 if (l->type == LINE_PRE_END)
220 l->flags ^= L_HIDDEN;
229 cmd_push_button_new_tab(struct buffer *buffer)
233 vl = buffer->current_line;
234 if (vl->parent->type != LINE_LINK)
237 new_tab(vl->parent->alt);
241 cmd_previous_button(struct buffer *buffer)
243 struct excursion place;
245 save_excursion(&place, buffer);
248 if (!forward_line(buffer, -1)) {
249 restore_excursion(&place, buffer);
250 message("No previous link");
253 } while (buffer->current_line->parent->type != LINE_LINK);
257 cmd_next_button(struct buffer *buffer)
259 struct excursion place;
261 save_excursion(&place, buffer);
264 if (!forward_line(buffer, +1)){
265 restore_excursion(&place, buffer);
266 message("No next link");
269 } while (buffer->current_line->parent->type != LINE_LINK);
273 is_heading(const struct line *l)
275 return l->type == LINE_TITLE_1 ||
276 l->type == LINE_TITLE_2 ||
277 l->type == LINE_TITLE_3;
281 cmd_previous_heading(struct buffer *buffer)
283 struct excursion place;
285 save_excursion(&place, buffer);
288 if (!forward_line(buffer, -1)) {
289 restore_excursion(&place, buffer);
290 message("No previous heading");
293 } while (!is_heading(buffer->current_line->parent));
297 cmd_next_heading(struct buffer *buffer)
299 struct excursion place;
301 save_excursion(&place, buffer);
304 if (!forward_line(buffer, +1)) {
305 restore_excursion(&place, buffer);
306 message("No next heading");
309 } while (!is_heading(buffer->current_line->parent));
313 cmd_previous_page(struct buffer *buffer)
315 struct tab *tab = current_tab();
317 if (!load_previous_page(tab))
318 message("No previous page");
320 start_loading_anim(tab);
324 cmd_next_page(struct buffer *buffer)
326 struct tab *tab = current_tab();
328 if (!load_next_page(tab))
329 message("No next page");
331 start_loading_anim(tab);
335 cmd_clear_minibuf(struct buffer *buffer)
341 cmd_execute_extended_command(struct buffer *buffer)
346 message("We don't have enable-recursive-minibuffers");
350 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
351 &eecmd_history, compl_eecmd, NULL);
353 len = sizeof(ministate.prompt);
354 strlcpy(ministate.prompt, "", len);
357 strlcat(ministate.prompt, "M-", len);
359 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
362 strlcat(ministate.prompt, " ", len);
366 cmd_tab_close(struct buffer *buffer)
371 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
372 TAILQ_NEXT(tab, tabs) == NULL) {
373 message("Can't close the only tab.");
377 if (evtimer_pending(&tab->loadingev, NULL))
378 evtimer_del(&tab->loadingev);
382 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
383 t = TAILQ_NEXT(tab, tabs);
384 TAILQ_REMOVE(&tabshead, tab, tabs);
391 cmd_tab_close_other(struct buffer *buffer)
395 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
396 if (t->flags & TAB_CURRENT)
400 TAILQ_REMOVE(&tabshead, t, tabs);
406 cmd_tab_new(struct buffer *buffer)
410 if ((url = new_tab_url) == NULL)
417 cmd_tab_next(struct buffer *buffer)
422 tab->flags &= ~TAB_CURRENT;
424 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
425 t = TAILQ_FIRST(&tabshead);
426 t->flags |= TAB_CURRENT;
427 t->flags &= ~TAB_URGENT;
431 cmd_tab_previous(struct buffer *buffer)
436 tab->flags &= ~TAB_CURRENT;
438 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
439 t = TAILQ_LAST(&tabshead, tabshead);
440 t->flags |= TAB_CURRENT;
441 t->flags &= ~TAB_URGENT;
445 cmd_tab_move(struct buffer *buffer)
450 t = TAILQ_NEXT(tab, tabs);
451 TAILQ_REMOVE(&tabshead, tab, tabs);
454 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
456 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
460 cmd_tab_move_to(struct buffer *buffer)
465 t = TAILQ_PREV(tab, tabshead, tabs);
466 TAILQ_REMOVE(&tabshead, tab, tabs);
469 if (TAILQ_EMPTY(&tabshead))
470 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
472 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
474 TAILQ_INSERT_BEFORE(t, tab, tabs);
478 cmd_load_url(struct buffer *buffer)
481 message("We don't have enable-recursive-minibuffers");
485 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
486 &lu_history, NULL, NULL);
487 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
488 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
489 cmd_move_end_of_line(&ministate.buffer);
493 cmd_load_current_url(struct buffer *buffer)
495 struct tab *tab = current_tab();
498 message("We don't have enable-recursive-minibuffers");
502 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
503 &lu_history, NULL, NULL);
504 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
505 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
506 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
510 cmd_reload_page(struct buffer *buffer)
515 load_url_in_tab(tab, tab->hist_cur->h);
519 cmd_bookmark_page(struct buffer *buffer)
521 struct tab *tab = current_tab();
523 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL,
525 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
526 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
527 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
531 cmd_list_bookmarks(struct buffer *buffer)
533 load_url_in_tab(current_tab(), "about:bookmarks");
537 cmd_toggle_help(struct buffer *buffer)
539 ui_toggle_side_window();
543 cmd_inc_fill_column(struct buffer *buffer)
545 if (fill_column == INT_MAX)
549 message("fill-column: %d", fill_column);
551 ui_schedule_redraw();
555 cmd_dec_fill_column(struct buffer *buffer)
557 if (fill_column == INT_MAX || fill_column < 8)
561 message("fill-column: %d", fill_column);
563 ui_schedule_redraw();
567 cmd_olivetti_mode(struct buffer *buffer)
569 olivetti_mode = !olivetti_mode;
571 message("olivetti-mode enabled");
573 message("olivetti-mode disabled");
575 ui_schedule_redraw();
579 cmd_mini_delete_char(struct buffer *buffer)
583 if (!in_minibuffer) {
584 message("text is read-only");
588 minibuffer_taint_hist();
590 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
595 memmove(c, n, strlen(n)+1);
597 recompute_completions(0);
601 cmd_mini_delete_backward_char(struct buffer *buffer)
605 if (!in_minibuffer) {
606 message("text is read-only");
610 minibuffer_taint_hist();
612 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
613 start = buffer->current_line->line;
616 p = utf8_prev_cp(c-1, start);
618 memmove(p, c, strlen(c)+1);
621 recompute_completions(0);
625 cmd_mini_kill_line(struct buffer *buffer)
629 if (!in_minibuffer) {
630 message("text is read-only");
634 minibuffer_taint_hist();
635 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
638 recompute_completions(0);
642 cmd_mini_abort(struct buffer *buffer)
651 cmd_mini_complete_and_exit(struct buffer *buffer)
656 minibuffer_taint_hist();
661 cmd_mini_previous_history_element(struct buffer *buffer)
663 if (ministate.history == NULL) {
664 message("No history");
668 if (ministate.hist_cur == NULL ||
669 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
670 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
671 ministate.hist_off = ministate.history->len - 1;
672 if (ministate.hist_cur == NULL)
673 message("No prev history item");
675 ministate.hist_off--;
678 if (ministate.hist_cur != NULL)
679 buffer->current_line->line = ministate.hist_cur->h;
683 cmd_mini_next_history_element(struct buffer *buffer)
685 if (ministate.history == NULL) {
686 message("No history");
690 if (ministate.hist_cur == NULL ||
691 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
692 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
693 ministate.hist_off = 0;
694 if (ministate.hist_cur == NULL)
695 message("No next history item");
697 ministate.hist_off++;
700 if (ministate.hist_cur != NULL)
701 buffer->current_line->line = ministate.hist_cur->h;
705 cmd_previous_completion(struct buffer *buffer)
707 if (in_minibuffer != MB_COMPREAD)
710 buffer = &ministate.compl.buffer;
712 if (buffer->current_line != NULL)
713 buffer->current_line->parent->type = LINE_COMPL;
715 forward_line(buffer, -1);
717 if (buffer->current_line != NULL)
718 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
722 cmd_next_completion(struct buffer *buffer)
724 if (in_minibuffer != MB_COMPREAD)
727 buffer = &ministate.compl.buffer;
729 if (buffer->current_line != NULL)
730 buffer->current_line->parent->type = LINE_COMPL;
732 forward_line(buffer, +1);
734 if (buffer->current_line != NULL)
735 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
739 cmd_insert_current_candidate(struct buffer *buffer)
743 if (in_minibuffer != MB_COMPREAD)
746 buffer = &ministate.compl.buffer;
747 if ((vl = buffer->current_line) == NULL)
750 minibuffer_taint_hist();
751 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
752 ministate.buffer.cpoff = utf8_cplen(ministate.buf);