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.
22 #include "minibuffer.h"
23 #include "telescope.h"
27 /* return 1 if moved, 0 otherwise */
29 forward_line(struct buffer *buffer, int n)
34 if (buffer->current_line == NULL)
36 vl = buffer->current_line;
41 vl = TAILQ_NEXT(vl, vlines);
44 if (vl->parent->flags & L_HIDDEN)
46 buffer->current_line = vl;
49 vl = TAILQ_PREV(vl, vhead, vlines);
52 if (vl->parent->flags & L_HIDDEN)
54 if (buffer->current_line == buffer->top_line)
55 buffer->top_line = vl;
56 buffer->current_line = vl;
67 cmd_previous_line(struct buffer *buffer)
69 forward_line(buffer, -1);
73 cmd_next_line(struct buffer *buffer)
75 forward_line(buffer, +1);
79 cmd_backward_char(struct buffer *buffer)
81 if (buffer->cpoff != 0)
86 cmd_forward_char(struct buffer *buffer)
90 if (buffer->current_line->line != NULL)
91 len = utf8_cplen(buffer->current_line->line);
92 if (++buffer->cpoff > len)
97 cmd_backward_paragraph(struct buffer *buffer)
100 if (!forward_line(buffer, -1)) {
101 message("No previous paragraph");
104 } while (buffer->current_line->line != NULL ||
105 buffer->current_line->parent->type != LINE_TEXT);
109 cmd_forward_paragraph(struct buffer *buffer)
112 if (!forward_line(buffer, +1)) {
113 message("No next paragraph");
116 } while (buffer->current_line->line != NULL ||
117 buffer->current_line->parent->type != LINE_TEXT);
121 cmd_move_beginning_of_line(struct buffer *buffer)
127 cmd_move_end_of_line(struct buffer *buffer)
131 vl = buffer->current_line;
132 if (vl->line == NULL)
134 buffer->cpoff = utf8_cplen(vl->line);
138 cmd_redraw(struct buffer *buffer)
140 ui_schedule_redraw();
144 cmd_scroll_line_up(struct buffer *buffer)
148 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
152 forward_line(buffer, -1);
155 buffer->top_line = vl;
159 cmd_scroll_line_down(struct buffer *buffer)
161 if (!forward_line(buffer, +1))
164 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
169 cmd_scroll_up(struct buffer *buffer)
171 forward_line(buffer, -1*body_lines);
175 cmd_scroll_down(struct buffer *buffer)
177 forward_line(buffer, body_lines);
181 cmd_beginning_of_buffer(struct buffer *buffer)
183 buffer->current_line = TAILQ_FIRST(&buffer->head);
185 buffer->top_line = buffer->current_line;
186 buffer->line_off = 0;
190 cmd_end_of_buffer(struct buffer *buffer)
192 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
193 buffer->cpoff = body_cols;
197 cmd_kill_telescope(struct buffer *buffer)
204 cmd_push_button(struct buffer *buffer)
209 vl = buffer->current_line;
210 switch (vl->parent->type) {
212 load_url_in_tab(current_tab(), vl->parent->alt);
215 l = TAILQ_NEXT(vl->parent, lines);
216 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
217 if (l->type == LINE_PRE_END)
219 l->flags ^= L_HIDDEN;
228 cmd_push_button_new_tab(struct buffer *buffer)
232 vl = buffer->current_line;
233 if (vl->parent->type != LINE_LINK)
236 new_tab(vl->parent->alt);
240 cmd_previous_button(struct buffer *buffer)
242 struct excursion place;
244 save_excursion(&place, buffer);
247 if (!forward_line(buffer, -1)) {
248 restore_excursion(&place, buffer);
249 message("No previous link");
252 } while (buffer->current_line->parent->type != LINE_LINK);
256 cmd_next_button(struct buffer *buffer)
258 struct excursion place;
260 save_excursion(&place, buffer);
263 if (!forward_line(buffer, +1)){
264 restore_excursion(&place, buffer);
265 message("No next link");
268 } while (buffer->current_line->parent->type != LINE_LINK);
272 is_heading(const struct line *l)
274 return l->type == LINE_TITLE_1 ||
275 l->type == LINE_TITLE_2 ||
276 l->type == LINE_TITLE_3;
280 cmd_previous_heading(struct buffer *buffer)
282 struct excursion place;
284 save_excursion(&place, buffer);
287 if (!forward_line(buffer, -1)) {
288 restore_excursion(&place, buffer);
289 message("No previous heading");
292 } while (!is_heading(buffer->current_line->parent));
296 cmd_next_heading(struct buffer *buffer)
298 struct excursion place;
300 save_excursion(&place, buffer);
303 if (!forward_line(buffer, +1)) {
304 restore_excursion(&place, buffer);
305 message("No next heading");
308 } while (!is_heading(buffer->current_line->parent));
312 cmd_previous_page(struct buffer *buffer)
314 struct tab *tab = current_tab();
316 if (!load_previous_page(tab))
317 message("No previous page");
319 start_loading_anim(tab);
323 cmd_next_page(struct buffer *buffer)
325 struct tab *tab = current_tab();
327 if (!load_next_page(tab))
328 message("No next page");
330 start_loading_anim(tab);
334 cmd_clear_minibuf(struct buffer *buffer)
340 cmd_execute_extended_command(struct buffer *buffer)
345 message("We don't have enable-recursive-minibuffers");
349 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
350 &eecmd_history, NULL, NULL);
352 len = sizeof(ministate.prompt);
353 strlcpy(ministate.prompt, "", len);
356 strlcat(ministate.prompt, "M-", len);
358 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
361 strlcat(ministate.prompt, " ", len);
365 cmd_tab_close(struct buffer *buffer)
370 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
371 TAILQ_NEXT(tab, tabs) == NULL) {
372 message("Can't close the only tab.");
376 if (evtimer_pending(&tab->loadingev, NULL))
377 evtimer_del(&tab->loadingev);
381 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
382 t = TAILQ_NEXT(tab, tabs);
383 TAILQ_REMOVE(&tabshead, tab, tabs);
390 cmd_tab_close_other(struct buffer *buffer)
394 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
395 if (t->flags & TAB_CURRENT)
399 TAILQ_REMOVE(&tabshead, t, tabs);
405 cmd_tab_new(struct buffer *buffer)
409 if ((url = new_tab_url) == NULL)
416 cmd_tab_next(struct buffer *buffer)
421 tab->flags &= ~TAB_CURRENT;
423 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
424 t = TAILQ_FIRST(&tabshead);
425 t->flags |= TAB_CURRENT;
426 t->flags &= ~TAB_URGENT;
430 cmd_tab_previous(struct buffer *buffer)
435 tab->flags &= ~TAB_CURRENT;
437 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
438 t = TAILQ_LAST(&tabshead, tabshead);
439 t->flags |= TAB_CURRENT;
440 t->flags &= ~TAB_URGENT;
444 cmd_tab_move(struct buffer *buffer)
449 t = TAILQ_NEXT(tab, tabs);
450 TAILQ_REMOVE(&tabshead, tab, tabs);
453 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
455 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
459 cmd_tab_move_to(struct buffer *buffer)
464 t = TAILQ_PREV(tab, tabshead, tabs);
465 TAILQ_REMOVE(&tabshead, tab, tabs);
468 if (TAILQ_EMPTY(&tabshead))
469 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
471 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
473 TAILQ_INSERT_BEFORE(t, tab, tabs);
477 cmd_load_url(struct buffer *buffer)
480 message("We don't have enable-recursive-minibuffers");
484 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
485 &lu_history, NULL, NULL);
486 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
487 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
488 cmd_move_end_of_line(&ministate.buffer);
492 cmd_load_current_url(struct buffer *buffer)
494 struct tab *tab = current_tab();
497 message("We don't have enable-recursive-minibuffers");
501 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
502 &lu_history, NULL, NULL);
503 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
504 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
505 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
509 cmd_bookmark_page(struct buffer *buffer)
511 struct tab *tab = current_tab();
513 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL,
515 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
516 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
517 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
521 cmd_list_bookmarks(struct buffer *buffer)
523 load_url_in_tab(current_tab(), "about:bookmarks");
527 cmd_toggle_help(struct buffer *buffer)
529 ui_toggle_side_window();
533 cmd_inc_fill_column(struct buffer *buffer)
535 if (fill_column == INT_MAX)
539 message("fill-column: %d", fill_column);
541 ui_schedule_redraw();
545 cmd_dec_fill_column(struct buffer *buffer)
547 if (fill_column == INT_MAX || fill_column < 8)
551 message("fill-column: %d", fill_column);
553 ui_schedule_redraw();
557 cmd_olivetti_mode(struct buffer *buffer)
559 olivetti_mode = !olivetti_mode;
561 message("olivetti-mode enabled");
563 message("olivetti-mode disabled");
565 ui_schedule_redraw();
569 cmd_mini_delete_char(struct buffer *buffer)
573 if (!in_minibuffer) {
574 message("text is read-only");
578 minibuffer_taint_hist();
580 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
585 memmove(c, n, strlen(n)+1);
589 cmd_mini_delete_backward_char(struct buffer *buffer)
593 if (!in_minibuffer) {
594 message("text is read-only");
598 minibuffer_taint_hist();
600 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
601 start = buffer->current_line->line;
604 p = utf8_prev_cp(c-1, start);
606 memmove(p, c, strlen(c)+1);
611 cmd_mini_kill_line(struct buffer *buffer)
615 if (!in_minibuffer) {
616 message("text is read-only");
620 minibuffer_taint_hist();
621 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
626 cmd_mini_abort(struct buffer *buffer)
635 cmd_mini_complete_and_exit(struct buffer *buffer)
640 minibuffer_taint_hist();
645 cmd_mini_previous_history_element(struct buffer *buffer)
647 if (ministate.history == NULL) {
648 message("No history");
652 if (ministate.hist_cur == NULL ||
653 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
654 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
655 ministate.hist_off = ministate.history->len - 1;
656 if (ministate.hist_cur == NULL)
657 message("No prev item");
659 ministate.hist_off--;
662 if (ministate.hist_cur != NULL)
663 buffer->current_line->line = ministate.hist_cur->h;
667 cmd_mini_next_history_element(struct buffer *buffer)
669 if (ministate.history == NULL) {
670 message("No history");
674 if (ministate.hist_cur == NULL ||
675 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
676 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
677 ministate.hist_off = 0;
678 if (ministate.hist_cur == NULL)
679 message("No next item");
681 ministate.hist_off++;
684 if (ministate.hist_cur != NULL)
685 buffer->current_line->line = ministate.hist_cur->h;