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"
26 /* return 1 if moved, 0 otherwise */
28 forward_line(struct buffer *buffer, int n)
33 if (buffer->current_line == NULL)
35 vl = buffer->current_line;
40 vl = TAILQ_NEXT(vl, vlines);
43 if (vl->parent->flags & L_HIDDEN)
45 buffer->current_line = vl;
48 vl = TAILQ_PREV(vl, vhead, vlines);
51 if (vl->parent->flags & L_HIDDEN)
53 if (buffer->current_line == buffer->top_line)
54 buffer->top_line = vl;
55 buffer->current_line = vl;
66 cmd_previous_line(struct buffer *buffer)
68 forward_line(buffer, -1);
72 cmd_next_line(struct buffer *buffer)
74 forward_line(buffer, +1);
78 cmd_backward_char(struct buffer *buffer)
80 if (buffer->cpoff != 0)
85 cmd_forward_char(struct buffer *buffer)
89 if (buffer->current_line->line != NULL)
90 len = utf8_cplen(buffer->current_line->line);
91 if (++buffer->cpoff > len)
96 cmd_backward_paragraph(struct buffer *buffer)
99 if (!forward_line(buffer, -1)) {
100 message("No previous paragraph");
103 } while (buffer->current_line->line != NULL ||
104 buffer->current_line->parent->type != LINE_TEXT);
108 cmd_forward_paragraph(struct buffer *buffer)
111 if (!forward_line(buffer, +1)) {
112 message("No next paragraph");
115 } while (buffer->current_line->line != NULL ||
116 buffer->current_line->parent->type != LINE_TEXT);
120 cmd_move_beginning_of_line(struct buffer *buffer)
126 cmd_move_end_of_line(struct buffer *buffer)
130 vl = buffer->current_line;
131 if (vl->line == NULL)
133 buffer->cpoff = utf8_cplen(vl->line);
137 cmd_redraw(struct buffer *buffer)
139 ui_schedule_redraw();
143 cmd_scroll_line_up(struct buffer *buffer)
147 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
151 forward_line(buffer, -1);
154 buffer->top_line = vl;
158 cmd_scroll_line_down(struct buffer *buffer)
160 if (!forward_line(buffer, +1))
163 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
168 cmd_scroll_up(struct buffer *buffer)
170 forward_line(buffer, -1*body_lines);
174 cmd_scroll_down(struct buffer *buffer)
176 forward_line(buffer, body_lines);
180 cmd_beginning_of_buffer(struct buffer *buffer)
182 buffer->current_line = TAILQ_FIRST(&buffer->head);
184 buffer->top_line = buffer->current_line;
185 buffer->line_off = 0;
189 cmd_end_of_buffer(struct buffer *buffer)
191 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
192 buffer->cpoff = body_cols;
196 cmd_kill_telescope(struct buffer *buffer)
203 cmd_push_button(struct buffer *buffer)
208 vl = buffer->current_line;
209 switch (vl->parent->type) {
211 load_url_in_tab(current_tab(), vl->parent->alt);
214 l = TAILQ_NEXT(vl->parent, lines);
215 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
216 if (l->type == LINE_PRE_END)
218 l->flags ^= L_HIDDEN;
227 cmd_push_button_new_tab(struct buffer *buffer)
231 vl = buffer->current_line;
232 if (vl->parent->type != LINE_LINK)
235 new_tab(vl->parent->alt);
239 cmd_previous_button(struct buffer *buffer)
241 struct excursion place;
243 save_excursion(&place, buffer);
246 if (!forward_line(buffer, -1)) {
247 restore_excursion(&place, buffer);
248 message("No previous link");
251 } while (buffer->current_line->parent->type != LINE_LINK);
255 cmd_next_button(struct buffer *buffer)
257 struct excursion place;
259 save_excursion(&place, buffer);
262 if (!forward_line(buffer, +1)){
263 restore_excursion(&place, buffer);
264 message("No next link");
267 } while (buffer->current_line->parent->type != LINE_LINK);
271 is_heading(const struct line *l)
273 return l->type == LINE_TITLE_1 ||
274 l->type == LINE_TITLE_2 ||
275 l->type == LINE_TITLE_3;
279 cmd_previous_heading(struct buffer *buffer)
281 struct excursion place;
283 save_excursion(&place, buffer);
286 if (!forward_line(buffer, -1)) {
287 restore_excursion(&place, buffer);
288 message("No previous heading");
291 } while (!is_heading(buffer->current_line->parent));
295 cmd_next_heading(struct buffer *buffer)
297 struct excursion place;
299 save_excursion(&place, buffer);
302 if (!forward_line(buffer, +1)) {
303 restore_excursion(&place, buffer);
304 message("No next heading");
307 } while (!is_heading(buffer->current_line->parent));
311 cmd_previous_page(struct buffer *buffer)
313 struct tab *tab = current_tab();
315 if (!load_previous_page(tab))
316 message("No previous page");
318 start_loading_anim(tab);
322 cmd_next_page(struct buffer *buffer)
324 struct tab *tab = current_tab();
326 if (!load_next_page(tab))
327 message("No next page");
329 start_loading_anim(tab);
333 cmd_clear_minibuf(struct buffer *buffer)
339 cmd_execute_extended_command(struct buffer *buffer)
344 message("We don't have enable-recursive-minibuffers");
348 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
351 len = sizeof(ministate.prompt);
352 strlcpy(ministate.prompt, "", len);
355 strlcat(ministate.prompt, "M-", len);
357 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
360 strlcat(ministate.prompt, " ", len);
364 cmd_tab_close(struct buffer *buffer)
369 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
370 TAILQ_NEXT(tab, tabs) == NULL) {
371 message("Can't close the only tab.");
375 if (evtimer_pending(&tab->loadingev, NULL))
376 evtimer_del(&tab->loadingev);
380 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
381 t = TAILQ_NEXT(tab, tabs);
382 TAILQ_REMOVE(&tabshead, tab, tabs);
389 cmd_tab_close_other(struct buffer *buffer)
393 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
394 if (t->flags & TAB_CURRENT)
398 TAILQ_REMOVE(&tabshead, t, tabs);
404 cmd_tab_new(struct buffer *buffer)
408 if ((url = new_tab_url) == NULL)
415 cmd_tab_next(struct buffer *buffer)
420 tab->flags &= ~TAB_CURRENT;
422 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
423 t = TAILQ_FIRST(&tabshead);
424 t->flags |= TAB_CURRENT;
425 t->flags &= ~TAB_URGENT;
429 cmd_tab_previous(struct buffer *buffer)
434 tab->flags &= ~TAB_CURRENT;
436 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
437 t = TAILQ_LAST(&tabshead, tabshead);
438 t->flags |= TAB_CURRENT;
439 t->flags &= ~TAB_URGENT;
443 cmd_tab_move(struct buffer *buffer)
448 t = TAILQ_NEXT(tab, tabs);
449 TAILQ_REMOVE(&tabshead, tab, tabs);
452 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
454 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
458 cmd_tab_move_to(struct buffer *buffer)
463 t = TAILQ_PREV(tab, tabshead, tabs);
464 TAILQ_REMOVE(&tabshead, tab, tabs);
467 if (TAILQ_EMPTY(&tabshead))
468 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
470 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
472 TAILQ_INSERT_BEFORE(t, tab, tabs);
476 cmd_load_url(struct buffer *buffer)
479 message("We don't have enable-recursive-minibuffers");
483 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
485 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
486 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
487 cmd_move_end_of_line(&ministate.buffer);
491 cmd_load_current_url(struct buffer *buffer)
493 struct tab *tab = current_tab();
496 message("We don't have enable-recursive-minibuffers");
500 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
502 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
503 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
504 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
508 cmd_bookmark_page(struct buffer *buffer)
510 struct tab *tab = current_tab();
512 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
513 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
514 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
515 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
519 cmd_list_bookmarks(struct buffer *buffer)
521 load_url_in_tab(current_tab(), "about:bookmarks");
525 cmd_toggle_help(struct buffer *buffer)
527 ui_toggle_side_window();
531 cmd_inc_fill_column(struct buffer *buffer)
533 if (fill_column == INT_MAX)
537 message("fill-column: %d", fill_column);
539 ui_schedule_redraw();
543 cmd_dec_fill_column(struct buffer *buffer)
545 if (fill_column == INT_MAX || fill_column < 8)
549 message("fill-column: %d", fill_column);
551 ui_schedule_redraw();
555 cmd_olivetti_mode(struct buffer *buffer)
557 olivetti_mode = !olivetti_mode;
559 message("olivetti-mode enabled");
561 message("olivetti-mode disabled");
563 ui_schedule_redraw();
567 cmd_mini_delete_char(struct buffer *buffer)
571 if (!in_minibuffer) {
572 message("text is read-only");
576 minibuffer_taint_hist();
578 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
583 memmove(c, n, strlen(n)+1);
587 cmd_mini_delete_backward_char(struct buffer *buffer)
591 if (!in_minibuffer) {
592 message("text is read-only");
596 minibuffer_taint_hist();
598 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
599 start = buffer->current_line->line;
602 p = utf8_prev_cp(c-1, start);
604 memmove(p, c, strlen(c)+1);
609 cmd_mini_kill_line(struct buffer *buffer)
613 if (!in_minibuffer) {
614 message("text is read-only");
618 minibuffer_taint_hist();
619 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
624 cmd_mini_abort(struct buffer *buffer)
633 cmd_mini_complete_and_exit(struct buffer *buffer)
638 minibuffer_taint_hist();
643 cmd_mini_previous_history_element(struct buffer *buffer)
645 if (ministate.history == NULL) {
646 message("No history");
650 if (ministate.hist_cur == NULL ||
651 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
652 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
653 ministate.hist_off = ministate.history->len - 1;
654 if (ministate.hist_cur == NULL)
655 message("No prev item");
657 ministate.hist_off--;
660 if (ministate.hist_cur != NULL)
661 buffer->current_line->line = ministate.hist_cur->h;
665 cmd_mini_next_history_element(struct buffer *buffer)
667 if (ministate.history == NULL) {
668 message("No history");
672 if (ministate.hist_cur == NULL ||
673 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
674 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
675 ministate.hist_off = 0;
676 if (ministate.hist_cur == NULL)
677 message("No next item");
679 ministate.hist_off++;
682 if (ministate.hist_cur != NULL)
683 buffer->current_line->line = ministate.hist_cur->h;