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"
24 /* return 1 if moved, 0 otherwise */
26 forward_line(struct buffer *buffer, int n)
31 if (buffer->current_line == NULL)
33 vl = buffer->current_line;
38 vl = TAILQ_NEXT(vl, vlines);
41 if (vl->parent->flags & L_HIDDEN)
43 buffer->current_line = vl;
46 vl = TAILQ_PREV(vl, vhead, vlines);
49 if (vl->parent->flags & L_HIDDEN)
51 if (buffer->current_line == buffer->top_line)
52 buffer->top_line = vl;
53 buffer->current_line = vl;
64 cmd_previous_line(struct buffer *buffer)
66 forward_line(buffer, -1);
70 cmd_next_line(struct buffer *buffer)
72 forward_line(buffer, +1);
76 cmd_backward_char(struct buffer *buffer)
78 if (buffer->cpoff != 0)
83 cmd_forward_char(struct buffer *buffer)
87 if (buffer->current_line->line != NULL)
88 len = utf8_cplen(buffer->current_line->line);
89 if (++buffer->cpoff > len)
94 cmd_backward_paragraph(struct buffer *buffer)
97 if (!forward_line(buffer, -1)) {
98 message("No previous paragraph");
101 } while (buffer->current_line->line != NULL ||
102 buffer->current_line->parent->type != LINE_TEXT);
106 cmd_forward_paragraph(struct buffer *buffer)
109 if (!forward_line(buffer, +1)) {
110 message("No next paragraph");
113 } while (buffer->current_line->line != NULL ||
114 buffer->current_line->parent->type != LINE_TEXT);
118 cmd_move_beginning_of_line(struct buffer *buffer)
124 cmd_move_end_of_line(struct buffer *buffer)
128 vl = buffer->current_line;
129 if (vl->line == NULL)
131 buffer->cpoff = utf8_cplen(vl->line);
135 cmd_redraw(struct buffer *buffer)
137 ui_schedule_redraw();
141 cmd_scroll_line_up(struct buffer *buffer)
145 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
149 forward_line(buffer, -1);
152 buffer->top_line = vl;
156 cmd_scroll_line_down(struct buffer *buffer)
158 if (!forward_line(buffer, +1))
161 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
166 cmd_scroll_up(struct buffer *buffer)
168 forward_line(buffer, -1*body_lines);
172 cmd_scroll_down(struct buffer *buffer)
174 forward_line(buffer, body_lines);
178 cmd_beginning_of_buffer(struct buffer *buffer)
180 buffer->current_line = TAILQ_FIRST(&buffer->head);
182 buffer->top_line = buffer->current_line;
183 buffer->line_off = 0;
187 cmd_end_of_buffer(struct buffer *buffer)
189 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
190 buffer->cpoff = body_cols;
194 cmd_kill_telescope(struct buffer *buffer)
201 cmd_push_button(struct buffer *buffer)
206 vl = buffer->current_line;
207 switch (vl->parent->type) {
209 load_url_in_tab(current_tab(), vl->parent->alt);
212 l = TAILQ_NEXT(vl->parent, lines);
213 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
214 if (l->type == LINE_PRE_END)
216 l->flags ^= L_HIDDEN;
225 cmd_push_button_new_tab(struct buffer *buffer)
229 vl = buffer->current_line;
230 if (vl->parent->type != LINE_LINK)
233 new_tab(vl->parent->alt);
237 cmd_previous_button(struct buffer *buffer)
239 struct excursion place;
241 save_excursion(&place, buffer);
244 if (!forward_line(buffer, -1)) {
245 restore_excursion(&place, buffer);
246 message("No previous link");
249 } while (buffer->current_line->parent->type != LINE_LINK);
253 cmd_next_button(struct buffer *buffer)
255 struct excursion place;
257 save_excursion(&place, buffer);
260 if (!forward_line(buffer, +1)){
261 restore_excursion(&place, buffer);
262 message("No next link");
265 } while (buffer->current_line->parent->type != LINE_LINK);
269 is_heading(const struct line *l)
271 return l->type == LINE_TITLE_1 ||
272 l->type == LINE_TITLE_2 ||
273 l->type == LINE_TITLE_3;
277 cmd_previous_heading(struct buffer *buffer)
279 struct excursion place;
281 save_excursion(&place, buffer);
284 if (!forward_line(buffer, -1)) {
285 restore_excursion(&place, buffer);
286 message("No previous heading");
289 } while (!is_heading(buffer->current_line->parent));
293 cmd_next_heading(struct buffer *buffer)
295 struct excursion place;
297 save_excursion(&place, buffer);
300 if (!forward_line(buffer, +1)) {
301 restore_excursion(&place, buffer);
302 message("No next heading");
305 } while (!is_heading(buffer->current_line->parent));
309 cmd_previous_page(struct buffer *buffer)
311 struct tab *tab = current_tab();
313 if (!load_previous_page(tab))
314 message("No previous page");
316 start_loading_anim(tab);
320 cmd_next_page(struct buffer *buffer)
322 struct tab *tab = current_tab();
324 if (!load_next_page(tab))
325 message("No next page");
327 start_loading_anim(tab);
331 cmd_clear_minibuf(struct buffer *buffer)
337 cmd_execute_extended_command(struct buffer *buffer)
342 message("We don't have enable-recursive-minibuffers");
346 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
349 len = sizeof(ministate.prompt);
350 strlcpy(ministate.prompt, "", len);
353 strlcat(ministate.prompt, "M-", len);
355 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
358 strlcat(ministate.prompt, " ", len);
362 cmd_tab_close(struct buffer *buffer)
367 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
368 TAILQ_NEXT(tab, tabs) == NULL) {
369 message("Can't close the only tab.");
373 if (evtimer_pending(&tab->loadingev, NULL))
374 evtimer_del(&tab->loadingev);
378 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
379 t = TAILQ_NEXT(tab, tabs);
380 TAILQ_REMOVE(&tabshead, tab, tabs);
387 cmd_tab_close_other(struct buffer *buffer)
391 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
392 if (t->flags & TAB_CURRENT)
396 TAILQ_REMOVE(&tabshead, t, tabs);
402 cmd_tab_new(struct buffer *buffer)
406 if ((url = new_tab_url) == NULL)
413 cmd_tab_next(struct buffer *buffer)
418 tab->flags &= ~TAB_CURRENT;
420 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
421 t = TAILQ_FIRST(&tabshead);
422 t->flags |= TAB_CURRENT;
423 t->flags &= ~TAB_URGENT;
427 cmd_tab_previous(struct buffer *buffer)
432 tab->flags &= ~TAB_CURRENT;
434 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
435 t = TAILQ_LAST(&tabshead, tabshead);
436 t->flags |= TAB_CURRENT;
437 t->flags &= ~TAB_URGENT;
441 cmd_tab_move(struct buffer *buffer)
446 t = TAILQ_NEXT(tab, tabs);
447 TAILQ_REMOVE(&tabshead, tab, tabs);
450 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
452 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
456 cmd_tab_move_to(struct buffer *buffer)
461 t = TAILQ_PREV(tab, tabshead, tabs);
462 TAILQ_REMOVE(&tabshead, tab, tabs);
465 if (TAILQ_EMPTY(&tabshead))
466 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
468 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
470 TAILQ_INSERT_BEFORE(t, tab, tabs);
474 cmd_load_url(struct buffer *buffer)
477 message("We don't have enable-recursive-minibuffers");
481 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
483 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
484 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
485 cmd_move_end_of_line(&ministate.buffer);
489 cmd_load_current_url(struct buffer *buffer)
491 struct tab *tab = current_tab();
494 message("We don't have enable-recursive-minibuffers");
498 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
500 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
501 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
502 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
506 cmd_bookmark_page(struct buffer *buffer)
508 struct tab *tab = current_tab();
510 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
511 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
512 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
513 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
517 cmd_list_bookmarks(struct buffer *buffer)
519 load_url_in_tab(current_tab(), "about:bookmarks");
523 cmd_toggle_help(struct buffer *buffer)
525 ui_toggle_side_window();
529 cmd_inc_fill_column(struct buffer *buffer)
531 if (fill_column == INT_MAX)
535 message("fill-column: %d", fill_column);
537 ui_schedule_redraw();
541 cmd_dec_fill_column(struct buffer *buffer)
543 if (fill_column == INT_MAX || fill_column < 8)
547 message("fill-column: %d", fill_column);
549 ui_schedule_redraw();
553 cmd_olivetti_mode(struct buffer *buffer)
555 olivetti_mode = !olivetti_mode;
557 message("olivetti-mode enabled");
559 message("olivetti-mode disabled");
561 ui_schedule_redraw();
565 cmd_mini_delete_char(struct buffer *buffer)
569 if (!in_minibuffer) {
570 message("text is read-only");
574 minibuffer_taint_hist();
576 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
581 memmove(c, n, strlen(n)+1);
585 cmd_mini_delete_backward_char(struct buffer *buffer)
589 if (!in_minibuffer) {
590 message("text is read-only");
594 minibuffer_taint_hist();
596 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
597 start = buffer->current_line->line;
600 p = utf8_prev_cp(c-1, start);
602 memmove(p, c, strlen(c)+1);
607 cmd_mini_kill_line(struct buffer *buffer)
611 if (!in_minibuffer) {
612 message("text is read-only");
616 minibuffer_taint_hist();
617 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
622 cmd_mini_abort(struct buffer *buffer)
631 cmd_mini_complete_and_exit(struct buffer *buffer)
636 minibuffer_taint_hist();
641 cmd_mini_previous_history_element(struct buffer *buffer)
643 if (ministate.history == NULL) {
644 message("No history");
648 if (ministate.hist_cur == NULL ||
649 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
650 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
651 ministate.hist_off = ministate.history->len - 1;
652 if (ministate.hist_cur == NULL)
653 message("No prev item");
655 ministate.hist_off--;
658 if (ministate.hist_cur != NULL)
659 buffer->current_line->line = ministate.hist_cur->h;
663 cmd_mini_next_history_element(struct buffer *buffer)
665 if (ministate.history == NULL) {
666 message("No history");
670 if (ministate.hist_cur == NULL ||
671 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
672 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
673 ministate.hist_off = 0;
674 if (ministate.hist_cur == NULL)
675 message("No next item");
677 ministate.hist_off++;
680 if (ministate.hist_cur != NULL)
681 buffer->current_line->line = ministate.hist_cur->h;