Blob


1 /*
2 * Copyright (c) 2021, 2024 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 "compat.h"
19 #include <limits.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "certs.h"
24 #include "cmd.h"
25 #include "compl.h"
26 #include "defaults.h"
27 #include "hist.h"
28 #include "keymap.h"
29 #include "mcache.h"
30 #include "minibuffer.h"
31 #include "session.h"
32 #include "telescope.h"
33 #include "ui.h"
34 #include "utf8.h"
35 #include "utils.h"
37 #define GUARD_RECURSIVE_MINIBUFFER() \
38 do { \
39 if (in_minibuffer) { \
40 message("enable-recursive-minibuffers " \
41 "is not yet available."); \
42 return; \
43 } \
44 } while(0)
46 #define GUARD_READ_ONLY() \
47 do { \
48 if (!in_minibuffer) { \
49 message("text is read-only"); \
50 return; \
51 } \
52 } while(0)
54 /* return 1 if moved, 0 otherwise */
55 static inline int
56 forward_line(struct buffer *buffer, int n)
57 {
58 struct vline *vl;
59 int did;
61 if (buffer->current_line == NULL)
62 return 0;
63 vl = buffer->current_line;
65 did = 0;
66 while (n != 0) {
67 if (n > 0) {
68 vl = TAILQ_NEXT(vl, vlines);
69 if (vl == NULL)
70 return did;
71 if (vl->parent->flags & L_HIDDEN)
72 continue;
73 buffer->current_line = vl;
74 n--;
75 } else {
76 vl = TAILQ_PREV(vl, vhead, vlines);
77 if (vl == NULL)
78 return did;
79 if (vl->parent->flags & L_HIDDEN)
80 continue;
81 if (buffer->current_line == buffer->top_line) {
82 buffer->line_off--;
83 buffer->top_line = vl;
84 }
85 buffer->current_line = vl;
86 n++;
87 }
89 did = 1;
90 }
92 return did;
93 }
95 void
96 cmd_previous_line(struct buffer *buffer)
97 {
98 forward_line(buffer, -1);
99 }
101 void
102 cmd_next_line(struct buffer *buffer)
104 forward_line(buffer, +1);
107 void
108 cmd_backward_char(struct buffer *buffer)
110 if (buffer->cpoff != 0)
111 buffer->cpoff--;
114 void
115 cmd_forward_char(struct buffer *buffer)
117 if (buffer->current_line == NULL)
118 return;
119 if (buffer->current_line->cplen > buffer->cpoff)
120 buffer->cpoff++;
123 void
124 cmd_backward_paragraph(struct buffer *buffer)
126 do {
127 if (!forward_line(buffer, -1)) {
128 message("No previous paragraph");
129 return;
131 } while (buffer->current_line->len != 0 ||
132 buffer->current_line->parent->type != LINE_TEXT);
135 void
136 cmd_forward_paragraph(struct buffer *buffer)
138 do {
139 if (!forward_line(buffer, +1)) {
140 message("No next paragraph");
141 return;
143 } while (buffer->current_line->len != 0 ||
144 buffer->current_line->parent->type != LINE_TEXT);
147 void
148 cmd_move_beginning_of_line(struct buffer *buffer)
150 buffer->cpoff = 0;
153 void
154 cmd_move_end_of_line(struct buffer *buffer)
156 struct vline *vl;
158 vl = buffer->current_line;
159 if (vl == NULL)
160 return;
161 buffer->cpoff = vl->cplen;
164 void
165 cmd_redraw(struct buffer *buffer)
167 ui_schedule_redraw();
170 void
171 cmd_scroll_line_up(struct buffer *buffer)
173 struct vline *vl;
175 for (;;) {
176 if (buffer->top_line == NULL)
177 return;
179 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
180 == NULL)
181 return;
183 buffer->top_line = vl;
185 if (vl->parent->flags & L_HIDDEN)
186 continue;
188 break;
191 buffer->line_off--;
193 forward_line(buffer, -1);
196 void
197 cmd_scroll_line_down(struct buffer *buffer)
199 if (!forward_line(buffer, +1))
200 return;
202 for (;;) {
203 if (buffer->top_line == NULL)
204 return;
206 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
207 if (buffer->top_line->parent->flags & L_HIDDEN)
208 continue;
209 break;
212 buffer->line_off++;
215 void
216 cmd_scroll_up(struct buffer *buffer)
218 struct vline *vl;
219 int i;
221 if (buffer->top_line == NULL)
222 return;
224 for (i = 0; i < body_lines; ++i) {
225 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
226 if (vl == NULL)
227 break;
228 buffer->line_off--;
229 buffer->top_line = vl;
230 forward_line(buffer, -1);
234 void
235 cmd_scroll_down(struct buffer *buffer)
237 int i;
239 if (buffer->top_line == NULL)
240 return;
242 for (i = 0; i < body_lines; ++i) {
243 if (!forward_line(buffer, +1))
244 break;
246 buffer->top_line = TAILQ_NEXT(buffer->top_line,
247 vlines);
248 buffer->line_off++;
252 void
253 cmd_beginning_of_buffer(struct buffer *buffer)
255 buffer->current_line = TAILQ_FIRST(&buffer->head);
256 buffer->cpoff = 0;
257 buffer->top_line = buffer->current_line;
258 buffer->line_off = 0;
261 void
262 cmd_end_of_buffer(struct buffer *buffer)
264 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
266 if (buffer->current_line == NULL)
267 return;
269 /* deal with invisible lines */
270 if (buffer->current_line->parent->flags & L_HIDDEN)
271 forward_line(buffer, -1);
273 cmd_move_end_of_line(buffer);
276 static void
277 kill_telescope_cb(int r, struct tab *tab)
279 if (r) {
280 save_session();
281 event_loopbreak();
285 void
286 cmd_kill_telescope(struct buffer *buffer)
288 yornp("really quit?", kill_telescope_cb, NULL);
291 void
292 cmd_push_button(struct buffer *buffer)
294 struct vline *vl;
295 struct line *l;
297 vl = buffer->current_line;
299 if (vl == NULL)
300 return;
302 switch (vl->parent->type) {
303 case LINE_LINK:
304 load_url_in_tab(current_tab, vl->parent->alt, NULL,
305 LU_MODE_NOCACHE);
306 break;
307 case LINE_PRE_START:
308 l = TAILQ_NEXT(vl->parent, lines);
309 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
310 if (l->type == LINE_PRE_END)
311 break;
312 l->flags ^= L_HIDDEN;
313 if (l->flags & L_HIDDEN)
314 buffer->line_max--;
315 else
316 buffer->line_max++;
318 break;
319 default:
320 break;
324 void
325 cmd_push_button_new_tab(struct buffer *buffer)
327 struct vline *vl;
329 vl = buffer->current_line;
330 if (vl == NULL || vl->parent->type != LINE_LINK)
331 return;
333 new_tab(vl->parent->alt, hist_cur(current_tab->hist), current_tab);
336 void
337 cmd_previous_button(struct buffer *buffer)
339 struct excursion place;
341 save_excursion(&place, buffer);
343 do {
344 if (!forward_line(buffer, -1)) {
345 restore_excursion(&place, buffer);
346 message("No previous link");
347 return;
349 } while (buffer->current_line->parent->type != LINE_LINK);
352 void
353 cmd_next_button(struct buffer *buffer)
355 struct excursion place;
357 save_excursion(&place, buffer);
359 do {
360 if (!forward_line(buffer, +1)){
361 restore_excursion(&place, buffer);
362 message("No next link");
363 return;
365 } while (buffer->current_line->parent->type != LINE_LINK);
368 static inline int
369 is_heading(const struct line *l)
371 return l->type == LINE_TITLE_1 ||
372 l->type == LINE_TITLE_2 ||
373 l->type == LINE_TITLE_3;
376 void
377 cmd_previous_heading(struct buffer *buffer)
379 struct excursion place;
381 save_excursion(&place, buffer);
383 do {
384 if (!forward_line(buffer, -1)) {
385 restore_excursion(&place, buffer);
386 message("No previous heading");
387 return;
389 } while (!is_heading(buffer->current_line->parent));
392 void
393 cmd_next_heading(struct buffer *buffer)
395 struct excursion place;
397 save_excursion(&place, buffer);
399 do {
400 if (!forward_line(buffer, +1)) {
401 restore_excursion(&place, buffer);
402 message("No next heading");
403 return;
405 } while (!is_heading(buffer->current_line->parent));
408 void
409 cmd_previous_page(struct buffer *buffer)
411 if (!load_previous_page(current_tab))
412 message("No previous page");
415 void
416 cmd_next_page(struct buffer *buffer)
418 if (!load_next_page(current_tab))
419 message("No next page");
422 void
423 cmd_clear_minibuf(struct buffer *buffer)
425 message(NULL);
428 void
429 cmd_execute_extended_command(struct buffer *buffer)
431 size_t len;
433 GUARD_RECURSIVE_MINIBUFFER();
435 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
436 eecmd_history, compl_eecmd, NULL, 1);
438 len = sizeof(ministate.prompt);
439 strlcpy(ministate.prompt, "", len);
441 if (thiskey.meta)
442 strlcat(ministate.prompt, "M-", len);
444 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
446 if (thiskey.meta)
447 strlcat(ministate.prompt, " ", len);
450 void
451 cmd_tab_close(struct buffer *buffer)
453 struct tab *tab, *t;
455 tab = current_tab;
457 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
458 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
459 switch_to_tab(t);
460 kill_tab(tab, 0);
461 } else
462 message("Can't close the only tab.");
466 void
467 cmd_tab_close_other(struct buffer *buffer)
469 struct tab *t, *i;
471 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
472 if (t == current_tab)
473 continue;
475 kill_tab(t, 0);
479 void
480 cmd_tab_undo_close(struct buffer *buffer)
482 struct tab *t;
484 if ((t = unkill_tab()) == NULL) {
485 message("No recently-closed tabs");
486 return;
489 switch_to_tab(t);
492 void
493 cmd_tab_new(struct buffer *buffer)
495 const char *url;
497 if ((url = new_tab_url) == NULL)
498 url = NEW_TAB_URL;
500 new_tab(url, NULL, NULL);
503 void
504 cmd_tab_next(struct buffer *buffer)
506 struct tab *t;
508 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
509 t = TAILQ_FIRST(&tabshead);
510 switch_to_tab(t);
513 void
514 cmd_tab_previous(struct buffer *buffer)
516 struct tab *t;
518 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
519 t = TAILQ_LAST(&tabshead, tabshead);
520 switch_to_tab(t);
523 void
524 cmd_tab_move(struct buffer *buffer)
526 struct tab *t;
528 t = TAILQ_NEXT(current_tab, tabs);
529 TAILQ_REMOVE(&tabshead, current_tab, tabs);
531 if (t == NULL)
532 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
533 else
534 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
537 void
538 cmd_tab_move_to(struct buffer *buffer)
540 struct tab *t;
542 t = TAILQ_PREV(current_tab, tabshead, tabs);
543 TAILQ_REMOVE(&tabshead, current_tab, tabs);
545 if (t == NULL)
546 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
547 else
548 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
551 void
552 cmd_tab_select(struct buffer *buffer)
554 GUARD_RECURSIVE_MINIBUFFER();
556 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
557 NULL, compl_ts, NULL, 1);
558 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
561 void
562 cmd_load_url(struct buffer *buffer)
564 GUARD_RECURSIVE_MINIBUFFER();
566 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
567 lu_history, compl_lu, NULL, 0);
568 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
571 void
572 cmd_load_current_url(struct buffer *buffer)
574 GUARD_RECURSIVE_MINIBUFFER();
576 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
577 lu_history, compl_lu, NULL, 0);
578 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
579 strlcpy(ministate.buf, hist_cur(current_tab->hist), sizeof(ministate.buf));
580 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
583 void
584 cmd_reload_page(struct buffer *buffer)
586 load_url_in_tab(current_tab, hist_cur(current_tab->hist), NULL,
587 LU_MODE_NOHIST|LU_MODE_NOCACHE);
590 void
591 cmd_bookmark_page(struct buffer *buffer)
593 GUARD_RECURSIVE_MINIBUFFER();
595 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
596 NULL, NULL, 0);
597 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
598 strlcpy(ministate.buf, hist_cur(current_tab->hist),
599 sizeof(ministate.buf));
600 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
603 void
604 cmd_list_bookmarks(struct buffer *buffer)
606 load_url_in_tab(current_tab, "about:bookmarks", NULL, LU_MODE_NONE);
609 void
610 cmd_toggle_help(struct buffer *buffer)
612 ui_toggle_side_window(SIDE_WINDOW_LEFT);
615 void
616 cmd_link_select(struct buffer *buffer)
618 struct line *l;
620 GUARD_RECURSIVE_MINIBUFFER();
622 l = TAILQ_FIRST(&buffer->page.head);
623 while (l != NULL && l->type != LINE_LINK)
624 l = TAILQ_NEXT(l, lines);
626 if (l == NULL) {
627 message("No links found");
628 return;
631 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
632 NULL, compl_ls, l, 1);
633 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
636 void
637 cmd_swiper(struct buffer *buffer)
639 GUARD_RECURSIVE_MINIBUFFER();
641 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
642 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head), 1);
643 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
646 void
647 cmd_toc(struct buffer *buffer)
649 struct line *l;
651 GUARD_RECURSIVE_MINIBUFFER();
653 l = TAILQ_FIRST(&buffer->page.head);
654 while (l != NULL &&
655 l->type != LINE_TITLE_1 &&
656 l->type != LINE_TITLE_2 &&
657 l->type != LINE_TITLE_3)
658 l = TAILQ_NEXT(l, lines);
660 if (l == NULL) {
661 message("No headings found");
662 return;
665 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
666 NULL, compl_toc, l, 1);
667 strlcpy(ministate.prompt, "Select heading: ",
668 sizeof(ministate.prompt));
671 void
672 cmd_inc_fill_column(struct buffer *buffer)
674 if (fill_column == INT_MAX)
675 return;
677 fill_column += 2;
678 message("fill-column: %d", fill_column);
680 ui_schedule_redraw();
683 void
684 cmd_dec_fill_column(struct buffer *buffer)
686 if (fill_column == INT_MAX || fill_column < 8)
687 return;
689 fill_column -= 2;
690 message("fill-column: %d", fill_column);
692 ui_schedule_redraw();
695 void
696 cmd_olivetti_mode(struct buffer *buffer)
698 olivetti_mode = !olivetti_mode;
699 if (olivetti_mode)
700 message("olivetti-mode enabled");
701 else
702 message("olivetti-mode disabled");
704 ui_schedule_redraw();
707 void
708 cmd_mini_delete_char(struct buffer *buffer)
710 char *line, *c, *n;
712 GUARD_READ_ONLY();
714 minibuffer_taint_hist();
716 line = buffer->current_line->parent->line + buffer->current_line->from;
717 c = utf8_nth(line, buffer->cpoff);
718 if (*c == '\0')
719 return;
720 n = utf8_next_cp(c);
722 memmove(c, n, strlen(n)+1);
724 recompute_completions(0);
727 void
728 cmd_mini_delete_backward_char(struct buffer *buffer)
730 char *line, *c, *p;
732 GUARD_READ_ONLY();
734 minibuffer_taint_hist();
736 line = buffer->current_line->parent->line + buffer->current_line->from;
737 c = utf8_nth(line, buffer->cpoff);
738 if (c == line)
739 return;
740 p = utf8_prev_cp(c-1, line);
742 memmove(p, c, strlen(c)+1);
743 buffer->cpoff--;
745 recompute_completions(0);
748 void
749 cmd_mini_kill_line(struct buffer *buffer)
751 char *line, *c;
753 GUARD_READ_ONLY();
755 minibuffer_taint_hist();
757 line = buffer->current_line->parent->line + buffer->current_line->from;
758 c = utf8_nth(line, buffer->cpoff);
759 *c = '\0';
761 recompute_completions(0);
764 void
765 cmd_mini_kill_whole_line(struct buffer *buffer)
767 GUARD_READ_ONLY();
769 minibuffer_taint_hist();
770 *buffer->current_line->parent->line = '\0';
771 buffer->cpoff = 0;
774 void
775 cmd_mini_abort(struct buffer *buffer)
777 if (!in_minibuffer)
778 return;
780 ministate.abortfn();
783 void
784 cmd_mini_complete_and_exit(struct buffer *buffer)
786 struct vline *vl;
788 if (!in_minibuffer)
789 return;
791 if (ministate.compl.must_select && ministate.hist == NULL) {
792 vl = ministate.compl.buffer.current_line;
793 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
794 vl->parent->type == LINE_COMPL) {
795 message("no match");
796 return;
800 minibuffer_taint_hist();
801 ministate.donefn();
804 void
805 cmd_mini_previous_history_element(struct buffer *buffer)
807 char *text;
809 if (ministate.hist == NULL) {
810 message("No history");
811 return;
814 if (hist_prev(ministate.hist) == NULL) {
815 message("No prev history item");
816 return;
819 ministate.editing = 0;
821 /* XXX the minibuffer line is never modified so this is fine */
822 text = (char *)hist_cur(ministate.hist);
823 buffer->current_line->parent->line = text;
824 recompute_completions(0);
827 void
828 cmd_mini_next_history_element(struct buffer *buffer)
830 char *text;
832 if (ministate.hist == NULL) {
833 message("No history");
834 return;
837 if (hist_next(ministate.hist) == NULL) {
838 message("No next history item");
839 return;
842 ministate.editing = 0;
844 /* XXX the minibuffer line is never modified so this is fine */
845 text = (char *)hist_cur(ministate.hist);
846 buffer->current_line->parent->line = text;
847 recompute_completions(0);
850 void
851 cmd_previous_completion(struct buffer *buffer)
853 if (in_minibuffer != MB_COMPREAD)
854 return;
856 buffer = &ministate.compl.buffer;
857 if (buffer->current_line == NULL)
858 return;
860 buffer->current_line->parent->type = LINE_COMPL;
861 if (!forward_line(buffer, -1))
862 buffer->current_line->parent->type = LINE_COMPL;
863 else
864 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
867 void
868 cmd_next_completion(struct buffer *buffer)
870 if (in_minibuffer != MB_COMPREAD)
871 return;
873 buffer = &ministate.compl.buffer;
874 if (buffer->current_line == NULL)
875 return;
877 if (buffer->current_line->parent->type == LINE_COMPL_CURRENT) {
878 buffer->current_line->parent->type = LINE_COMPL;
879 forward_line(buffer, +1);
882 if (buffer->current_line != NULL)
883 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
886 void
887 cmd_insert_current_candidate(struct buffer *buffer)
889 if (in_minibuffer != MB_COMPREAD)
890 return;
892 minibuffer_insert_current_candidate();
895 void
896 cmd_suspend_telescope(struct buffer *buffer)
898 message("Zzz...");
899 ui_suspend();
902 void
903 cmd_toggle_pre_wrap(struct buffer *buffer)
905 dont_wrap_pre = !dont_wrap_pre;
907 if (dont_wrap_pre)
908 message("Don't wrap preformatted blocks");
909 else
910 message("Wrap preformatted blocks");
912 ui_schedule_redraw();
915 void
916 cmd_mini_goto_beginning(struct buffer *buffer)
918 struct vline *vl;
920 if (!in_minibuffer)
921 return;
923 buffer = &ministate.compl.buffer;
925 if ((vl = buffer->current_line) != NULL)
926 vl->parent->type = LINE_COMPL;
928 vl = TAILQ_FIRST(&buffer->head);
929 while (vl != NULL && vl->parent->flags & L_HIDDEN)
930 vl = TAILQ_NEXT(vl, vlines);
932 if (vl == NULL)
933 return;
935 vl->parent->type = LINE_COMPL_CURRENT;
936 buffer->top_line = vl;
937 buffer->current_line = vl;
940 void
941 cmd_mini_goto_end(struct buffer *buffer)
943 struct vline *vl;
945 if (!in_minibuffer)
946 return;
948 buffer = &ministate.compl.buffer;
950 if ((vl = buffer->current_line) != NULL)
951 vl->parent->type = LINE_COMPL;
953 vl = TAILQ_LAST(&buffer->head, vhead);
954 while (vl != NULL && vl->parent->flags & L_HIDDEN)
955 vl = TAILQ_PREV(vl, vhead, vlines);
957 if (vl == NULL)
958 return;
960 vl->parent->type = LINE_COMPL_CURRENT;
961 buffer->current_line = vl;
964 void
965 cmd_other_window(struct buffer *buffer)
967 ui_other_window();
970 void
971 cmd_mini_scroll_up(struct buffer *buffer)
973 if (!in_minibuffer)
974 return;
976 buffer = &ministate.compl.buffer;
977 if (buffer->current_line == NULL)
978 return;
980 buffer->current_line->parent->type = LINE_COMPL;
981 cmd_scroll_up(buffer);
982 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
985 void
986 cmd_mini_scroll_down(struct buffer *buffer)
988 if (!in_minibuffer)
989 return;
991 buffer = &ministate.compl.buffer;
992 if (buffer->current_line == NULL)
993 return;
995 buffer->current_line->parent->type = LINE_COMPL;
996 cmd_scroll_down(buffer);
997 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
1000 void
1001 cmd_toggle_downloads(struct buffer *buffer)
1003 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1006 void
1007 cmd_cache_info(struct buffer *buffer)
1009 size_t npages, tot;
1010 char fmt[FMT_SCALED_STRSIZE];
1012 mcache_info(&npages, &tot);
1014 if (fmt_scaled(tot, fmt) == 0)
1015 message("pages: %zu, total: %s", npages, fmt);
1016 else
1017 message("pages: %zu, total: %zu", npages, tot);
1020 void
1021 cmd_reply_last_input(struct buffer *buffer)
1023 GUARD_RECURSIVE_MINIBUFFER();
1025 if (current_tab->last_input_url == NULL) {
1026 message("there was no previous input request in this tab");
1027 return;
1030 if (!strncmp(current_tab->last_input_url, "gopher", 6)) {
1031 load_url_in_tab(current_tab, current_tab->last_input_url,
1032 NULL, LU_MODE_NOCACHE);
1033 return;
1036 message("%s", current_tab->last_input_url);
1037 ui_require_input(current_tab, 0, ir_select_reply);
1040 void
1041 cmd_write_buffer(struct buffer *buffer)
1043 const char *f, *url;
1044 char path[PATH_MAX];
1046 GUARD_RECURSIVE_MINIBUFFER();
1048 if (safe_mode) {
1049 message("Can't write buffer in safe-mode.");
1050 return;
1053 url = hist_cur(current_tab->hist);
1055 if ((f = strrchr(url, '/')) != NULL)
1056 f++;
1057 if (f == NULL || *f == '\0') {
1058 /* guess a decent file name based on the protocol used */
1059 if (!strncmp(url, "gemini://", 9))
1060 f = "index.gmi";
1061 else
1062 f = "index.txt";
1065 strlcpy(path, download_path, sizeof(path));
1066 strlcat(path, f, sizeof(path));
1068 ui_read("Write file", write_buffer, current_tab, path);
1071 void
1072 cmd_home(struct buffer *buffer)
1074 char path[GEMINI_URL_LEN];
1075 char *tilde, *t;
1077 strlcpy(path, current_tab->iri.iri_path, sizeof(path));
1079 if ((tilde = strstr(path, "/~")) != NULL &&
1080 tilde[2] != '\0' && tilde[2] != '/') {
1081 if ((t = strchr(tilde + 2, '/')) != NULL)
1082 *++t = '\0';
1083 load_url_in_tab(current_tab, path, NULL, LU_MODE_NOCACHE);
1084 } else
1085 cmd_root(buffer);
1088 void
1089 cmd_root(struct buffer *buffer)
1091 load_url_in_tab(current_tab, "/", NULL, LU_MODE_NOCACHE);
1094 void
1095 cmd_up(struct buffer *buffer)
1097 load_url_in_tab(current_tab, "..", NULL, LU_MODE_NOCACHE);
1100 void
1101 cmd_use_certificate(struct buffer *buffer)
1103 GUARD_RECURSIVE_MINIBUFFER();
1105 enter_minibuffer(sensible_self_insert, uc_select, exit_minibuffer,
1106 NULL, compl_uc, NULL, 1);
1107 strlcpy(ministate.prompt, "Select certificate: ",
1108 sizeof(ministate.prompt));
1111 void
1112 cmd_client_certificate_info(struct buffer *buffer)
1114 if (current_tab->client_cert)
1115 message("Using certificate %s", current_tab->client_cert);
1116 else
1117 message("Not using any client certificate.");
1120 static void
1121 unload_certificate_cb(int r, struct tab *tab)
1123 message("Won't use %s for this site.", tab->client_cert);
1124 cert_delete_for(tab->client_cert, &tab->iri, r);
1127 void
1128 cmd_unload_certificate(struct buffer *buffer)
1130 struct tab *tab = current_tab;
1132 GUARD_RECURSIVE_MINIBUFFER();
1134 if (tab->client_cert == NULL) {
1135 message("No client certificate in use!");
1136 return;
1139 if (tab->client_cert_temp) {
1140 message("Won't use %s for this site.", tab->client_cert);
1141 cert_delete_for(tab->client_cert, &tab->iri, 0);
1142 return;
1145 yornp("Unload only for the current session?", unload_certificate_cb,
1146 current_tab);
1149 void
1150 cmd_search(struct buffer *buffer)
1152 GUARD_RECURSIVE_MINIBUFFER();
1154 if (!strncmp(default_search_engine, "gopher://", 9)) {
1155 load_url_in_tab(current_tab, default_search_engine, NULL,
1156 LU_MODE_NOCACHE);
1157 return;
1160 enter_minibuffer(sensible_self_insert, search_select, exit_minibuffer, NULL,
1161 NULL, NULL, 0);
1162 strlcpy(ministate.prompt, "Search: ", sizeof(ministate.prompt));