Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include <limits.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "compl.h"
22 #include "defaults.h"
23 #include "minibuffer.h"
24 #include "telescope.h"
25 #include "ui.h"
26 #include "utf8.h"
28 /* return 1 if moved, 0 otherwise */
29 static inline int
30 forward_line(struct buffer *buffer, int n)
31 {
32 struct vline *vl;
33 int did;
35 if (buffer->current_line == NULL)
36 return 0;
37 vl = buffer->current_line;
39 did = 0;
40 while (n != 0) {
41 if (n > 0) {
42 vl = TAILQ_NEXT(vl, vlines);
43 if (vl == NULL)
44 return did;
45 if (vl->parent->flags & L_HIDDEN)
46 continue;
47 buffer->current_line = vl;
48 n--;
49 } else {
50 vl = TAILQ_PREV(vl, vhead, vlines);
51 if (vl == NULL)
52 return did;
53 if (vl->parent->flags & L_HIDDEN)
54 continue;
55 if (buffer->current_line == buffer->top_line) {
56 buffer->line_off--;
57 buffer->top_line = vl;
58 }
59 buffer->current_line = vl;
60 n++;
61 }
63 did = 1;
64 }
66 return did;
67 }
69 void
70 cmd_previous_line(struct buffer *buffer)
71 {
72 forward_line(buffer, -1);
73 }
75 void
76 cmd_next_line(struct buffer *buffer)
77 {
78 forward_line(buffer, +1);
79 }
81 void
82 cmd_backward_char(struct buffer *buffer)
83 {
84 if (buffer->cpoff != 0)
85 buffer->cpoff--;
86 }
88 void
89 cmd_forward_char(struct buffer *buffer)
90 {
91 size_t len = 0;
93 if (buffer->current_line->line != NULL)
94 len = utf8_cplen(buffer->current_line->line);
95 if (++buffer->cpoff > len)
96 buffer->cpoff = len;
97 }
99 void
100 cmd_backward_paragraph(struct buffer *buffer)
102 do {
103 if (!forward_line(buffer, -1)) {
104 message("No previous paragraph");
105 return;
107 } while (buffer->current_line->line != NULL ||
108 buffer->current_line->parent->type != LINE_TEXT);
111 void
112 cmd_forward_paragraph(struct buffer *buffer)
114 do {
115 if (!forward_line(buffer, +1)) {
116 message("No next paragraph");
117 return;
119 } while (buffer->current_line->line != NULL ||
120 buffer->current_line->parent->type != LINE_TEXT);
123 void
124 cmd_move_beginning_of_line(struct buffer *buffer)
126 buffer->cpoff = 0;
129 void
130 cmd_move_end_of_line(struct buffer *buffer)
132 struct vline *vl;
134 vl = buffer->current_line;
135 if (vl->line == NULL)
136 return;
137 buffer->cpoff = utf8_cplen(vl->line);
140 void
141 cmd_redraw(struct buffer *buffer)
143 ui_schedule_redraw();
146 void
147 cmd_scroll_line_up(struct buffer *buffer)
149 struct vline *vl;
151 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
152 == NULL)
153 return;
155 forward_line(buffer, -1);
157 buffer->top_line = vl;
160 void
161 cmd_scroll_line_down(struct buffer *buffer)
163 if (!forward_line(buffer, +1))
164 return;
166 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
167 buffer->line_off++;
170 void
171 cmd_scroll_up(struct buffer *buffer)
173 forward_line(buffer, -1*body_lines);
176 void
177 cmd_scroll_down(struct buffer *buffer)
179 forward_line(buffer, body_lines);
182 void
183 cmd_beginning_of_buffer(struct buffer *buffer)
185 buffer->current_line = TAILQ_FIRST(&buffer->head);
186 buffer->cpoff = 0;
187 buffer->top_line = buffer->current_line;
188 buffer->line_off = 0;
191 void
192 cmd_end_of_buffer(struct buffer *buffer)
194 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
195 buffer->cpoff = body_cols;
198 void
199 cmd_kill_telescope(struct buffer *buffer)
201 save_session();
202 event_loopbreak();
205 void
206 cmd_push_button(struct buffer *buffer)
208 struct vline *vl;
209 struct line *l;
211 vl = buffer->current_line;
212 switch (vl->parent->type) {
213 case LINE_LINK:
214 load_url_in_tab(current_tab(), vl->parent->meta.alt);
215 break;
216 case LINE_PRE_START:
217 l = TAILQ_NEXT(vl->parent, lines);
218 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
219 if (l->type == LINE_PRE_END)
220 break;
221 l->flags ^= L_HIDDEN;
222 if (l->flags & L_HIDDEN)
223 buffer->line_max--;
224 else
225 buffer->line_max++;
227 break;
228 default:
229 break;
233 void
234 cmd_push_button_new_tab(struct buffer *buffer)
236 struct vline *vl;
238 vl = buffer->current_line;
239 if (vl->parent->type != LINE_LINK)
240 return;
242 new_tab(vl->parent->meta.alt);
245 void
246 cmd_previous_button(struct buffer *buffer)
248 struct excursion place;
250 save_excursion(&place, buffer);
252 do {
253 if (!forward_line(buffer, -1)) {
254 restore_excursion(&place, buffer);
255 message("No previous link");
256 return;
258 } while (buffer->current_line->parent->type != LINE_LINK);
261 void
262 cmd_next_button(struct buffer *buffer)
264 struct excursion place;
266 save_excursion(&place, buffer);
268 do {
269 if (!forward_line(buffer, +1)){
270 restore_excursion(&place, buffer);
271 message("No next link");
272 return;
274 } while (buffer->current_line->parent->type != LINE_LINK);
277 static inline int
278 is_heading(const struct line *l)
280 return l->type == LINE_TITLE_1 ||
281 l->type == LINE_TITLE_2 ||
282 l->type == LINE_TITLE_3;
285 void
286 cmd_previous_heading(struct buffer *buffer)
288 struct excursion place;
290 save_excursion(&place, buffer);
292 do {
293 if (!forward_line(buffer, -1)) {
294 restore_excursion(&place, buffer);
295 message("No previous heading");
296 return;
298 } while (!is_heading(buffer->current_line->parent));
301 void
302 cmd_next_heading(struct buffer *buffer)
304 struct excursion place;
306 save_excursion(&place, buffer);
308 do {
309 if (!forward_line(buffer, +1)) {
310 restore_excursion(&place, buffer);
311 message("No next heading");
312 return;
314 } while (!is_heading(buffer->current_line->parent));
317 void
318 cmd_previous_page(struct buffer *buffer)
320 struct tab *tab = current_tab();
322 if (!load_previous_page(tab))
323 message("No previous page");
324 else
325 start_loading_anim(tab);
328 void
329 cmd_next_page(struct buffer *buffer)
331 struct tab *tab = current_tab();
333 if (!load_next_page(tab))
334 message("No next page");
335 else
336 start_loading_anim(tab);
339 void
340 cmd_clear_minibuf(struct buffer *buffer)
342 message(NULL);
345 void
346 cmd_execute_extended_command(struct buffer *buffer)
348 size_t len;
350 if (in_minibuffer) {
351 message("We don't have enable-recursive-minibuffers");
352 return;
355 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
356 &eecmd_history, compl_eecmd, NULL);
358 len = sizeof(ministate.prompt);
359 strlcpy(ministate.prompt, "", len);
361 if (thiskey.meta)
362 strlcat(ministate.prompt, "M-", len);
364 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
366 if (thiskey.meta)
367 strlcat(ministate.prompt, " ", len);
370 void
371 cmd_tab_close(struct buffer *buffer)
373 struct tab *tab, *t;
375 tab = current_tab();
376 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
377 TAILQ_NEXT(tab, tabs) == NULL) {
378 message("Can't close the only tab.");
379 return;
382 if (evtimer_pending(&tab->loadingev, NULL))
383 evtimer_del(&tab->loadingev);
385 stop_tab(tab);
387 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
388 t = TAILQ_NEXT(tab, tabs);
389 TAILQ_REMOVE(&tabshead, tab, tabs);
390 free(tab);
392 switch_to_tab(t);
395 void
396 cmd_tab_close_other(struct buffer *buffer)
398 struct tab *t, *i;
400 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
401 if (t->flags & TAB_CURRENT)
402 continue;
404 stop_tab(t);
405 TAILQ_REMOVE(&tabshead, t, tabs);
406 free(t);
410 void
411 cmd_tab_new(struct buffer *buffer)
413 const char *url;
415 if ((url = new_tab_url) == NULL)
416 url = NEW_TAB_URL;
418 new_tab(url);
421 void
422 cmd_tab_next(struct buffer *buffer)
424 struct tab *tab, *t;
426 tab = current_tab();
427 tab->flags &= ~TAB_CURRENT;
429 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
430 t = TAILQ_FIRST(&tabshead);
431 t->flags |= TAB_CURRENT;
432 t->flags &= ~TAB_URGENT;
435 void
436 cmd_tab_previous(struct buffer *buffer)
438 struct tab *tab, *t;
440 tab = current_tab();
441 tab->flags &= ~TAB_CURRENT;
443 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
444 t = TAILQ_LAST(&tabshead, tabshead);
445 t->flags |= TAB_CURRENT;
446 t->flags &= ~TAB_URGENT;
449 void
450 cmd_tab_move(struct buffer *buffer)
452 struct tab *tab, *t;
454 tab = current_tab();
455 t = TAILQ_NEXT(tab, tabs);
456 TAILQ_REMOVE(&tabshead, tab, tabs);
458 if (t == NULL)
459 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
460 else
461 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
464 void
465 cmd_tab_move_to(struct buffer *buffer)
467 struct tab *tab, *t;
469 tab = current_tab();
470 t = TAILQ_PREV(tab, tabshead, tabs);
471 TAILQ_REMOVE(&tabshead, tab, tabs);
473 if (t == NULL) {
474 if (TAILQ_EMPTY(&tabshead))
475 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
476 else
477 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
478 } else
479 TAILQ_INSERT_BEFORE(t, tab, tabs);
482 void
483 cmd_tab_select(struct buffer *buffer)
485 if (in_minibuffer) {
486 message("We don't have enable-recursive-minibuffers");
487 return;
490 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
491 NULL, compl_ts, NULL);
492 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
495 void
496 cmd_load_url(struct buffer *buffer)
498 if (in_minibuffer) {
499 message("We don't have enable-recursive-minibuffers");
500 return;
503 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
504 &lu_history, NULL, NULL);
505 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
506 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
507 cmd_move_end_of_line(&ministate.buffer);
510 void
511 cmd_load_current_url(struct buffer *buffer)
513 struct tab *tab = current_tab();
515 if (in_minibuffer) {
516 message("We don't have enable-recursive-minibuffers");
517 return;
520 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
521 &lu_history, NULL, NULL);
522 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
523 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
524 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
527 void
528 cmd_reload_page(struct buffer *buffer)
530 struct tab *tab;
532 tab = current_tab();
533 load_url_in_tab(tab, tab->hist_cur->h);
536 void
537 cmd_bookmark_page(struct buffer *buffer)
539 struct tab *tab = current_tab();
541 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
542 NULL, NULL);
543 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
544 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
545 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
548 void
549 cmd_list_bookmarks(struct buffer *buffer)
551 load_url_in_tab(current_tab(), "about:bookmarks");
554 void
555 cmd_toggle_help(struct buffer *buffer)
557 ui_toggle_side_window();
560 void
561 cmd_link_select(struct buffer *buffer)
563 if (in_minibuffer) {
564 message("We don't have enable-recursive-minibuffers");
565 return;
568 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
569 NULL, compl_ls, TAILQ_FIRST(&buffer->page.head));
570 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
573 void
574 cmd_swiper(struct buffer *buffer)
576 if (in_minibuffer) {
577 message("We don't have enable-recursive-minibuffers");
578 return;
581 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
582 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
583 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
586 void
587 cmd_toc(struct buffer *buffer)
589 if (in_minibuffer) {
590 message("We don't have enable-recursive-minibuffers");
591 return;
594 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
595 NULL, compl_toc, TAILQ_FIRST(&buffer->page.head));
596 strlcpy(ministate.prompt, "Select heading: ",
597 sizeof(ministate.prompt));
600 void
601 cmd_inc_fill_column(struct buffer *buffer)
603 if (fill_column == INT_MAX)
604 return;
606 fill_column += 2;
607 message("fill-column: %d", fill_column);
609 ui_schedule_redraw();
612 void
613 cmd_dec_fill_column(struct buffer *buffer)
615 if (fill_column == INT_MAX || fill_column < 8)
616 return;
618 fill_column -= 2;
619 message("fill-column: %d", fill_column);
621 ui_schedule_redraw();
624 void
625 cmd_olivetti_mode(struct buffer *buffer)
627 olivetti_mode = !olivetti_mode;
628 if (olivetti_mode)
629 message("olivetti-mode enabled");
630 else
631 message("olivetti-mode disabled");
633 ui_schedule_redraw();
636 void
637 cmd_mini_delete_char(struct buffer *buffer)
639 char *c, *n;
641 if (!in_minibuffer) {
642 message("text is read-only");
643 return;
646 minibuffer_taint_hist();
648 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
649 if (*c == '\0')
650 return;
651 n = utf8_next_cp(c);
653 memmove(c, n, strlen(n)+1);
655 recompute_completions(0);
658 void
659 cmd_mini_delete_backward_char(struct buffer *buffer)
661 char *c, *p, *start;
663 if (!in_minibuffer) {
664 message("text is read-only");
665 return;
668 minibuffer_taint_hist();
670 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
671 start = buffer->current_line->line;
672 if (c == start)
673 return;
674 p = utf8_prev_cp(c-1, start);
676 memmove(p, c, strlen(c)+1);
677 buffer->cpoff--;
679 recompute_completions(0);
682 void
683 cmd_mini_kill_line(struct buffer *buffer)
685 char *c;
687 if (!in_minibuffer) {
688 message("text is read-only");
689 return;
692 minibuffer_taint_hist();
693 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
694 *c = '\0';
696 recompute_completions(0);
699 void
700 cmd_mini_abort(struct buffer *buffer)
702 if (!in_minibuffer)
703 return;
705 ministate.abortfn();
708 void
709 cmd_mini_complete_and_exit(struct buffer *buffer)
711 if (!in_minibuffer)
712 return;
714 minibuffer_taint_hist();
715 ministate.donefn();
718 void
719 cmd_mini_previous_history_element(struct buffer *buffer)
721 if (ministate.history == NULL) {
722 message("No history");
723 return;
726 if (ministate.hist_cur == NULL ||
727 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
728 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
729 ministate.hist_off = ministate.history->len - 1;
730 if (ministate.hist_cur == NULL)
731 message("No prev history item");
732 } else {
733 ministate.hist_off--;
736 if (ministate.hist_cur != NULL)
737 buffer->current_line->line = ministate.hist_cur->h;
740 void
741 cmd_mini_next_history_element(struct buffer *buffer)
743 if (ministate.history == NULL) {
744 message("No history");
745 return;
748 if (ministate.hist_cur == NULL ||
749 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
750 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
751 ministate.hist_off = 0;
752 if (ministate.hist_cur == NULL)
753 message("No next history item");
754 } else {
755 ministate.hist_off++;
758 if (ministate.hist_cur != NULL)
759 buffer->current_line->line = ministate.hist_cur->h;
762 void
763 cmd_previous_completion(struct buffer *buffer)
765 if (in_minibuffer != MB_COMPREAD)
766 return;
768 buffer = &ministate.compl.buffer;
770 if (buffer->current_line != NULL)
771 buffer->current_line->parent->type = LINE_COMPL;
773 forward_line(buffer, -1);
775 if (buffer->current_line != NULL)
776 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
779 void
780 cmd_next_completion(struct buffer *buffer)
782 if (in_minibuffer != MB_COMPREAD)
783 return;
785 buffer = &ministate.compl.buffer;
787 if (buffer->current_line != NULL)
788 buffer->current_line->parent->type = LINE_COMPL;
790 forward_line(buffer, +1);
792 if (buffer->current_line != NULL)
793 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
796 void
797 cmd_insert_current_candidate(struct buffer *buffer)
799 struct vline *vl;
801 if (in_minibuffer != MB_COMPREAD)
802 return;
804 buffer = &ministate.compl.buffer;
805 if ((vl = buffer->current_line) == NULL)
806 return;
808 minibuffer_taint_hist();
809 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
810 ministate.buffer.cpoff = utf8_cplen(ministate.buf);