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.
21 #include "minibuffer.h"
22 #include "telescope.h"
25 /* return 1 if moved, 0 otherwise */
27 forward_line(struct buffer *buffer, int n)
32 if (buffer->current_line == NULL)
34 vl = buffer->current_line;
39 vl = TAILQ_NEXT(vl, vlines);
42 if (vl->parent->flags & L_HIDDEN)
44 buffer->current_line = vl;
47 vl = TAILQ_PREV(vl, vhead, vlines);
50 if (vl->parent->flags & L_HIDDEN)
52 if (buffer->current_line == buffer->top_line)
53 buffer->top_line = vl;
54 buffer->current_line = vl;
65 cmd_previous_line(struct buffer *buffer)
67 forward_line(buffer, -1);
71 cmd_next_line(struct buffer *buffer)
73 forward_line(buffer, +1);
77 cmd_backward_char(struct buffer *buffer)
79 if (buffer->cpoff != 0)
84 cmd_forward_char(struct buffer *buffer)
88 if (buffer->current_line->line != NULL)
89 len = utf8_cplen(buffer->current_line->line);
90 if (++buffer->cpoff > len)
95 cmd_backward_paragraph(struct buffer *buffer)
98 if (!forward_line(buffer, -1)) {
99 message("No previous paragraph");
102 } while (buffer->current_line->line != NULL ||
103 buffer->current_line->parent->type != LINE_TEXT);
107 cmd_forward_paragraph(struct buffer *buffer)
110 if (!forward_line(buffer, +1)) {
111 message("No next paragraph");
114 } while (buffer->current_line->line != NULL ||
115 buffer->current_line->parent->type != LINE_TEXT);
119 cmd_move_beginning_of_line(struct buffer *buffer)
125 cmd_move_end_of_line(struct buffer *buffer)
129 vl = buffer->current_line;
130 if (vl->line == NULL)
132 buffer->cpoff = utf8_cplen(vl->line);
136 cmd_redraw(struct buffer *buffer)
138 ui_schedule_redraw();
142 cmd_scroll_line_up(struct buffer *buffer)
146 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
150 forward_line(buffer, -1);
153 buffer->top_line = vl;
157 cmd_scroll_line_down(struct buffer *buffer)
159 if (!forward_line(buffer, +1))
162 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
167 cmd_scroll_up(struct buffer *buffer)
169 forward_line(buffer, -1*body_lines);
173 cmd_scroll_down(struct buffer *buffer)
175 forward_line(buffer, body_lines);
179 cmd_beginning_of_buffer(struct buffer *buffer)
181 buffer->current_line = TAILQ_FIRST(&buffer->head);
183 buffer->top_line = buffer->current_line;
184 buffer->line_off = 0;
188 cmd_end_of_buffer(struct buffer *buffer)
190 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
191 buffer->cpoff = body_cols;
195 cmd_kill_telescope(struct buffer *buffer)
202 cmd_push_button(struct buffer *buffer)
207 vl = buffer->current_line;
208 switch (vl->parent->type) {
210 load_url_in_tab(current_tab(), vl->parent->alt);
213 l = TAILQ_NEXT(vl->parent, lines);
214 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
215 if (l->type == LINE_PRE_END)
217 l->flags ^= L_HIDDEN;
226 cmd_push_button_new_tab(struct buffer *buffer)
230 vl = buffer->current_line;
231 if (vl->parent->type != LINE_LINK)
234 new_tab(vl->parent->alt);
238 cmd_previous_button(struct buffer *buffer)
240 struct excursion place;
242 save_excursion(&place, buffer);
245 if (!forward_line(buffer, -1)) {
246 restore_excursion(&place, buffer);
247 message("No previous link");
250 } while (buffer->current_line->parent->type != LINE_LINK);
254 cmd_next_button(struct buffer *buffer)
256 struct excursion place;
258 save_excursion(&place, buffer);
261 if (!forward_line(buffer, +1)){
262 restore_excursion(&place, buffer);
263 message("No next link");
266 } while (buffer->current_line->parent->type != LINE_LINK);
270 is_heading(const struct line *l)
272 return l->type == LINE_TITLE_1 ||
273 l->type == LINE_TITLE_2 ||
274 l->type == LINE_TITLE_3;
278 cmd_previous_heading(struct buffer *buffer)
280 struct excursion place;
282 save_excursion(&place, buffer);
285 if (!forward_line(buffer, -1)) {
286 restore_excursion(&place, buffer);
287 message("No previous heading");
290 } while (!is_heading(buffer->current_line->parent));
294 cmd_next_heading(struct buffer *buffer)
296 struct excursion place;
298 save_excursion(&place, buffer);
301 if (!forward_line(buffer, +1)) {
302 restore_excursion(&place, buffer);
303 message("No next heading");
306 } while (!is_heading(buffer->current_line->parent));
310 cmd_previous_page(struct buffer *buffer)
312 struct tab *tab = current_tab();
314 if (!load_previous_page(tab))
315 message("No previous page");
317 start_loading_anim(tab);
321 cmd_next_page(struct buffer *buffer)
323 struct tab *tab = current_tab();
325 if (!load_next_page(tab))
326 message("No next page");
328 start_loading_anim(tab);
332 cmd_clear_minibuf(struct buffer *buffer)
338 cmd_execute_extended_command(struct buffer *buffer)
343 message("We don't have enable-recursive-minibuffers");
347 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
350 len = sizeof(ministate.prompt);
351 strlcpy(ministate.prompt, "", len);
354 strlcat(ministate.prompt, "M-", len);
356 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
359 strlcat(ministate.prompt, " ", len);
363 cmd_tab_close(struct buffer *buffer)
368 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
369 TAILQ_NEXT(tab, tabs) == NULL) {
370 message("Can't close the only tab.");
374 if (evtimer_pending(&tab->loadingev, NULL))
375 evtimer_del(&tab->loadingev);
379 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
380 t = TAILQ_NEXT(tab, tabs);
381 TAILQ_REMOVE(&tabshead, tab, tabs);
388 cmd_tab_close_other(struct buffer *buffer)
392 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
393 if (t->flags & TAB_CURRENT)
397 TAILQ_REMOVE(&tabshead, t, tabs);
403 cmd_tab_new(struct buffer *buffer)
407 if ((url = new_tab_url) == NULL)
414 cmd_tab_next(struct buffer *buffer)
419 tab->flags &= ~TAB_CURRENT;
421 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
422 t = TAILQ_FIRST(&tabshead);
423 t->flags |= TAB_CURRENT;
424 t->flags &= ~TAB_URGENT;
428 cmd_tab_previous(struct buffer *buffer)
433 tab->flags &= ~TAB_CURRENT;
435 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
436 t = TAILQ_LAST(&tabshead, tabshead);
437 t->flags |= TAB_CURRENT;
438 t->flags &= ~TAB_URGENT;
442 cmd_tab_move(struct buffer *buffer)
447 t = TAILQ_NEXT(tab, tabs);
448 TAILQ_REMOVE(&tabshead, tab, tabs);
451 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
453 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
457 cmd_tab_move_to(struct buffer *buffer)
462 t = TAILQ_PREV(tab, tabshead, tabs);
463 TAILQ_REMOVE(&tabshead, tab, tabs);
466 if (TAILQ_EMPTY(&tabshead))
467 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
469 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
471 TAILQ_INSERT_BEFORE(t, tab, tabs);
475 cmd_load_url(struct buffer *buffer)
478 message("We don't have enable-recursive-minibuffers");
482 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
484 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
485 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
486 cmd_move_end_of_line(&ministate.buffer);
490 cmd_load_current_url(struct buffer *buffer)
492 struct tab *tab = current_tab();
495 message("We don't have enable-recursive-minibuffers");
499 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
501 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
502 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
503 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
507 cmd_bookmark_page(struct buffer *buffer)
509 struct tab *tab = current_tab();
511 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
512 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
513 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
514 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
518 cmd_list_bookmarks(struct buffer *buffer)
520 load_url_in_tab(current_tab(), "about:bookmarks");
524 cmd_toggle_help(struct buffer *buffer)
526 ui_toggle_side_window();
530 cmd_inc_fill_column(struct buffer *buffer)
532 if (fill_column == INT_MAX)
536 message("fill-column: %d", fill_column);
538 ui_schedule_redraw();
542 cmd_dec_fill_column(struct buffer *buffer)
544 if (fill_column == INT_MAX || fill_column < 8)
548 message("fill-column: %d", fill_column);
550 ui_schedule_redraw();
554 cmd_olivetti_mode(struct buffer *buffer)
556 olivetti_mode = !olivetti_mode;
558 message("olivetti-mode enabled");
560 message("olivetti-mode disabled");
562 ui_schedule_redraw();
566 cmd_mini_delete_char(struct buffer *buffer)
570 if (!in_minibuffer) {
571 message("text is read-only");
575 minibuffer_taint_hist();
577 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
582 memmove(c, n, strlen(n)+1);
586 cmd_mini_delete_backward_char(struct buffer *buffer)
590 if (!in_minibuffer) {
591 message("text is read-only");
595 minibuffer_taint_hist();
597 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
598 start = buffer->current_line->line;
601 p = utf8_prev_cp(c-1, start);
603 memmove(p, c, strlen(c)+1);
608 cmd_mini_kill_line(struct buffer *buffer)
612 if (!in_minibuffer) {
613 message("text is read-only");
617 minibuffer_taint_hist();
618 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
623 cmd_mini_abort(struct buffer *buffer)
632 cmd_mini_complete_and_exit(struct buffer *buffer)
637 minibuffer_taint_hist();
642 cmd_mini_previous_history_element(struct buffer *buffer)
644 if (ministate.history == NULL) {
645 message("No history");
649 if (ministate.hist_cur == NULL ||
650 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
651 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
652 ministate.hist_off = ministate.history->len - 1;
653 if (ministate.hist_cur == NULL)
654 message("No prev item");
656 ministate.hist_off--;
659 if (ministate.hist_cur != NULL)
660 buffer->current_line->line = ministate.hist_cur->h;
664 cmd_mini_next_history_element(struct buffer *buffer)
666 if (ministate.history == NULL) {
667 message("No history");
671 if (ministate.hist_cur == NULL ||
672 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
673 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
674 ministate.hist_off = 0;
675 if (ministate.hist_cur == NULL)
676 message("No next item");
678 ministate.hist_off++;
681 if (ministate.hist_cur != NULL)
682 buffer->current_line->line = ministate.hist_cur->h;