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 "ev.h"
28 #include "hist.h"
29 #include "keymap.h"
30 #include "mcache.h"
31 #include "minibuffer.h"
32 #include "session.h"
33 #include "telescope.h"
34 #include "ui.h"
35 #include "utf8.h"
36 #include "utils.h"
38 #define GUARD_RECURSIVE_MINIBUFFER() \
39 do { \
40 if (in_minibuffer) { \
41 message("enable-recursive-minibuffers " \
42 "is not yet available."); \
43 return; \
44 } \
45 } while(0)
47 #define GUARD_READ_ONLY() \
48 do { \
49 if (!in_minibuffer) { \
50 message("text is read-only"); \
51 return; \
52 } \
53 } while(0)
55 /* return 1 if moved, 0 otherwise */
56 static inline int
57 forward_line(struct buffer *buffer, int n)
58 {
59 struct vline *vl;
60 int did;
62 if (buffer->current_line == NULL)
63 return 0;
64 vl = buffer->current_line;
66 did = 0;
67 while (n != 0) {
68 if (n > 0) {
69 vl = TAILQ_NEXT(vl, vlines);
70 if (vl == NULL)
71 return did;
72 if (vl->parent->flags & L_HIDDEN)
73 continue;
74 buffer->current_line = vl;
75 n--;
76 } else {
77 vl = TAILQ_PREV(vl, vhead, vlines);
78 if (vl == NULL)
79 return did;
80 if (vl->parent->flags & L_HIDDEN)
81 continue;
82 if (buffer->current_line == buffer->top_line) {
83 buffer->line_off--;
84 buffer->top_line = vl;
85 }
86 buffer->current_line = vl;
87 n++;
88 }
90 did = 1;
91 }
93 return did;
94 }
96 void
97 cmd_previous_line(struct buffer *buffer)
98 {
99 forward_line(buffer, -1);
102 void
103 cmd_next_line(struct buffer *buffer)
105 forward_line(buffer, +1);
108 void
109 cmd_backward_char(struct buffer *buffer)
111 if (buffer->cpoff != 0)
112 buffer->cpoff--;
115 void
116 cmd_forward_char(struct buffer *buffer)
118 if (buffer->current_line == NULL)
119 return;
120 if (buffer->current_line->cplen > buffer->cpoff)
121 buffer->cpoff++;
124 void
125 cmd_backward_paragraph(struct buffer *buffer)
127 do {
128 if (!forward_line(buffer, -1)) {
129 message("No previous paragraph");
130 return;
132 } while (buffer->current_line->len != 0 ||
133 buffer->current_line->parent->type != LINE_TEXT);
136 void
137 cmd_forward_paragraph(struct buffer *buffer)
139 do {
140 if (!forward_line(buffer, +1)) {
141 message("No next paragraph");
142 return;
144 } while (buffer->current_line->len != 0 ||
145 buffer->current_line->parent->type != LINE_TEXT);
148 void
149 cmd_move_beginning_of_line(struct buffer *buffer)
151 buffer->cpoff = 0;
154 void
155 cmd_move_end_of_line(struct buffer *buffer)
157 struct vline *vl;
159 vl = buffer->current_line;
160 if (vl == NULL)
161 return;
162 buffer->cpoff = vl->cplen;
165 void
166 cmd_redraw(struct buffer *buffer)
168 ui_schedule_redraw();
171 void
172 cmd_scroll_line_up(struct buffer *buffer)
174 struct vline *vl;
176 for (;;) {
177 if (buffer->top_line == NULL)
178 return;
180 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
181 == NULL)
182 return;
184 buffer->top_line = vl;
186 if (vl->parent->flags & L_HIDDEN)
187 continue;
189 break;
192 buffer->line_off--;
194 forward_line(buffer, -1);
197 void
198 cmd_scroll_line_down(struct buffer *buffer)
200 if (!forward_line(buffer, +1))
201 return;
203 for (;;) {
204 if (buffer->top_line == NULL)
205 return;
207 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
208 if (buffer->top_line->parent->flags & L_HIDDEN)
209 continue;
210 break;
213 buffer->line_off++;
216 void
217 cmd_scroll_up(struct buffer *buffer)
219 struct vline *vl;
220 int i;
222 if (buffer->top_line == NULL)
223 return;
225 for (i = 0; i < body_lines; ++i) {
226 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
227 if (vl == NULL)
228 break;
229 buffer->line_off--;
230 buffer->top_line = vl;
231 forward_line(buffer, -1);
235 void
236 cmd_scroll_down(struct buffer *buffer)
238 int i;
240 if (buffer->top_line == NULL)
241 return;
243 for (i = 0; i < body_lines; ++i) {
244 if (!forward_line(buffer, +1))
245 break;
247 buffer->top_line = TAILQ_NEXT(buffer->top_line,
248 vlines);
249 buffer->line_off++;
253 void
254 cmd_beginning_of_buffer(struct buffer *buffer)
256 buffer->current_line = TAILQ_FIRST(&buffer->head);
257 buffer->cpoff = 0;
258 buffer->top_line = buffer->current_line;
259 buffer->line_off = 0;
262 void
263 cmd_end_of_buffer(struct buffer *buffer)
265 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
267 if (buffer->current_line == NULL)
268 return;
270 /* deal with invisible lines */
271 if (buffer->current_line->parent->flags & L_HIDDEN)
272 forward_line(buffer, -1);
274 cmd_move_end_of_line(buffer);
277 static void
278 kill_telescope_cb(int r, struct tab *tab)
280 if (r) {
281 save_session();
282 ev_break();
286 void
287 cmd_kill_telescope(struct buffer *buffer)
289 yornp("really quit?", kill_telescope_cb, NULL);
292 void
293 cmd_push_button(struct buffer *buffer)
295 struct vline *vl;
296 struct line *l;
298 vl = buffer->current_line;
300 if (vl == NULL)
301 return;
303 switch (vl->parent->type) {
304 case LINE_LINK:
305 load_url_in_tab(current_tab, vl->parent->alt, NULL,
306 LU_MODE_NOCACHE);
307 break;
308 case LINE_PRE_START:
309 l = TAILQ_NEXT(vl->parent, lines);
310 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
311 if (l->type == LINE_PRE_END)
312 break;
313 l->flags ^= L_HIDDEN;
314 if (l->flags & L_HIDDEN)
315 buffer->line_max--;
316 else
317 buffer->line_max++;
319 break;
320 default:
321 break;
325 void
326 cmd_push_button_new_tab(struct buffer *buffer)
328 struct vline *vl;
330 vl = buffer->current_line;
331 if (vl == NULL || vl->parent->type != LINE_LINK)
332 return;
334 new_tab(vl->parent->alt, hist_cur(current_tab->hist), current_tab);
337 void
338 cmd_previous_button(struct buffer *buffer)
340 struct excursion place;
342 save_excursion(&place, buffer);
344 do {
345 if (!forward_line(buffer, -1)) {
346 restore_excursion(&place, buffer);
347 message("No previous link");
348 return;
350 } while (buffer->current_line->parent->type != LINE_LINK);
353 void
354 cmd_next_button(struct buffer *buffer)
356 struct excursion place;
358 save_excursion(&place, buffer);
360 do {
361 if (!forward_line(buffer, +1)){
362 restore_excursion(&place, buffer);
363 message("No next link");
364 return;
366 } while (buffer->current_line->parent->type != LINE_LINK);
369 static inline int
370 is_heading(const struct line *l)
372 return l->type == LINE_TITLE_1 ||
373 l->type == LINE_TITLE_2 ||
374 l->type == LINE_TITLE_3;
377 void
378 cmd_previous_heading(struct buffer *buffer)
380 struct excursion place;
382 save_excursion(&place, buffer);
384 do {
385 if (!forward_line(buffer, -1)) {
386 restore_excursion(&place, buffer);
387 message("No previous heading");
388 return;
390 } while (!is_heading(buffer->current_line->parent));
393 void
394 cmd_next_heading(struct buffer *buffer)
396 struct excursion place;
398 save_excursion(&place, buffer);
400 do {
401 if (!forward_line(buffer, +1)) {
402 restore_excursion(&place, buffer);
403 message("No next heading");
404 return;
406 } while (!is_heading(buffer->current_line->parent));
409 void
410 cmd_previous_page(struct buffer *buffer)
412 if (!load_previous_page(current_tab))
413 message("No previous page");
416 void
417 cmd_next_page(struct buffer *buffer)
419 if (!load_next_page(current_tab))
420 message("No next page");
423 void
424 cmd_clear_minibuf(struct buffer *buffer)
426 message(NULL);
429 void
430 cmd_execute_extended_command(struct buffer *buffer)
432 size_t len;
434 GUARD_RECURSIVE_MINIBUFFER();
436 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
437 eecmd_history, compl_eecmd, NULL, 1);
439 len = sizeof(ministate.prompt);
440 strlcpy(ministate.prompt, "", len);
442 if (thiskey.meta)
443 strlcat(ministate.prompt, "M-", len);
445 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
447 if (thiskey.meta)
448 strlcat(ministate.prompt, " ", len);
451 void
452 cmd_tab_close(struct buffer *buffer)
454 struct tab *tab, *t;
456 tab = current_tab;
458 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
459 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
460 switch_to_tab(t);
461 kill_tab(tab, 0);
462 } else
463 message("Can't close the only tab.");
467 void
468 cmd_tab_close_other(struct buffer *buffer)
470 struct tab *t, *i;
472 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
473 if (t == current_tab)
474 continue;
476 kill_tab(t, 0);
480 void
481 cmd_tab_undo_close(struct buffer *buffer)
483 struct tab *t;
485 if ((t = unkill_tab()) == NULL) {
486 message("No recently-closed tabs");
487 return;
490 switch_to_tab(t);
493 void
494 cmd_tab_new(struct buffer *buffer)
496 const char *url;
498 if ((url = new_tab_url) == NULL)
499 url = NEW_TAB_URL;
501 new_tab(url, NULL, NULL);
504 void
505 cmd_tab_next(struct buffer *buffer)
507 struct tab *t;
509 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
510 t = TAILQ_FIRST(&tabshead);
511 switch_to_tab(t);
514 void
515 cmd_tab_previous(struct buffer *buffer)
517 struct tab *t;
519 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
520 t = TAILQ_LAST(&tabshead, tabshead);
521 switch_to_tab(t);
524 void
525 cmd_tab_move(struct buffer *buffer)
527 struct tab *t;
529 t = TAILQ_NEXT(current_tab, tabs);
530 TAILQ_REMOVE(&tabshead, current_tab, tabs);
532 if (t == NULL)
533 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
534 else
535 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
538 void
539 cmd_tab_move_to(struct buffer *buffer)
541 struct tab *t;
543 t = TAILQ_PREV(current_tab, tabshead, tabs);
544 TAILQ_REMOVE(&tabshead, current_tab, tabs);
546 if (t == NULL)
547 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
548 else
549 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
552 void
553 cmd_tab_select(struct buffer *buffer)
555 GUARD_RECURSIVE_MINIBUFFER();
557 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
558 NULL, compl_ts, NULL, 1);
559 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
562 void
563 cmd_load_url(struct buffer *buffer)
565 GUARD_RECURSIVE_MINIBUFFER();
567 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
568 lu_history, compl_lu, NULL, 0);
569 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
572 void
573 cmd_load_current_url(struct buffer *buffer)
575 GUARD_RECURSIVE_MINIBUFFER();
577 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
578 lu_history, compl_lu, NULL, 0);
579 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
580 strlcpy(ministate.buf, hist_cur(current_tab->hist), sizeof(ministate.buf));
581 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
584 void
585 cmd_reload_page(struct buffer *buffer)
587 load_url_in_tab(current_tab, hist_cur(current_tab->hist), NULL,
588 LU_MODE_NOHIST|LU_MODE_NOCACHE);
591 void
592 cmd_bookmark_page(struct buffer *buffer)
594 GUARD_RECURSIVE_MINIBUFFER();
596 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
597 NULL, NULL, 0);
598 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
599 strlcpy(ministate.buf, hist_cur(current_tab->hist),
600 sizeof(ministate.buf));
601 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
604 void
605 cmd_list_bookmarks(struct buffer *buffer)
607 load_url_in_tab(current_tab, "about:bookmarks", NULL, LU_MODE_NONE);
610 void
611 cmd_toggle_help(struct buffer *buffer)
613 ui_toggle_side_window(SIDE_WINDOW_LEFT);
616 void
617 cmd_link_select(struct buffer *buffer)
619 struct line *l;
621 GUARD_RECURSIVE_MINIBUFFER();
623 l = TAILQ_FIRST(&buffer->page.head);
624 while (l != NULL && l->type != LINE_LINK)
625 l = TAILQ_NEXT(l, lines);
627 if (l == NULL) {
628 message("No links found");
629 return;
632 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
633 NULL, compl_ls, l, 1);
634 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
637 void
638 cmd_swiper(struct buffer *buffer)
640 GUARD_RECURSIVE_MINIBUFFER();
642 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
643 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head), 1);
644 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
647 void
648 cmd_toc(struct buffer *buffer)
650 struct line *l;
652 GUARD_RECURSIVE_MINIBUFFER();
654 l = TAILQ_FIRST(&buffer->page.head);
655 while (l != NULL &&
656 l->type != LINE_TITLE_1 &&
657 l->type != LINE_TITLE_2 &&
658 l->type != LINE_TITLE_3)
659 l = TAILQ_NEXT(l, lines);
661 if (l == NULL) {
662 message("No headings found");
663 return;
666 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
667 NULL, compl_toc, l, 1);
668 strlcpy(ministate.prompt, "Select heading: ",
669 sizeof(ministate.prompt));
672 void
673 cmd_inc_fill_column(struct buffer *buffer)
675 if (fill_column == INT_MAX)
676 return;
678 fill_column += 2;
679 message("fill-column: %d", fill_column);
681 ui_schedule_redraw();
684 void
685 cmd_dec_fill_column(struct buffer *buffer)
687 if (fill_column == INT_MAX || fill_column < 8)
688 return;
690 fill_column -= 2;
691 message("fill-column: %d", fill_column);
693 ui_schedule_redraw();
696 void
697 cmd_olivetti_mode(struct buffer *buffer)
699 olivetti_mode = !olivetti_mode;
700 if (olivetti_mode)
701 message("olivetti-mode enabled");
702 else
703 message("olivetti-mode disabled");
705 ui_schedule_redraw();
708 void
709 cmd_mini_delete_char(struct buffer *buffer)
711 char *line, *c, *n;
713 GUARD_READ_ONLY();
715 minibuffer_taint_hist();
717 line = buffer->current_line->parent->line + buffer->current_line->from;
718 c = utf8_nth(line, buffer->cpoff);
719 if (*c == '\0')
720 return;
721 n = utf8_next_cp(c);
723 memmove(c, n, strlen(n)+1);
725 recompute_completions(0);
728 void
729 cmd_mini_delete_backward_char(struct buffer *buffer)
731 char *line, *c, *p;
733 GUARD_READ_ONLY();
735 minibuffer_taint_hist();
737 line = buffer->current_line->parent->line + buffer->current_line->from;
738 c = utf8_nth(line, buffer->cpoff);
739 if (c == line)
740 return;
741 p = utf8_prev_cp(c-1, line);
743 memmove(p, c, strlen(c)+1);
744 buffer->cpoff--;
746 recompute_completions(0);
749 void
750 cmd_mini_kill_line(struct buffer *buffer)
752 char *line, *c;
754 GUARD_READ_ONLY();
756 minibuffer_taint_hist();
758 line = buffer->current_line->parent->line + buffer->current_line->from;
759 c = utf8_nth(line, buffer->cpoff);
760 *c = '\0';
762 recompute_completions(0);
765 void
766 cmd_mini_kill_whole_line(struct buffer *buffer)
768 GUARD_READ_ONLY();
770 minibuffer_taint_hist();
771 *buffer->current_line->parent->line = '\0';
772 buffer->cpoff = 0;
775 void
776 cmd_mini_abort(struct buffer *buffer)
778 if (!in_minibuffer)
779 return;
781 ministate.abortfn();
784 void
785 cmd_mini_complete_and_exit(struct buffer *buffer)
787 struct vline *vl;
789 if (!in_minibuffer)
790 return;
792 if (ministate.compl.must_select && ministate.hist == NULL) {
793 vl = ministate.compl.buffer.current_line;
794 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
795 vl->parent->type == LINE_COMPL) {
796 message("no match");
797 return;
801 minibuffer_taint_hist();
802 ministate.donefn();
805 void
806 cmd_mini_previous_history_element(struct buffer *buffer)
808 char *text;
810 if (ministate.hist == NULL) {
811 message("No history");
812 return;
815 if (hist_prev(ministate.hist) == NULL) {
816 message("No prev history item");
817 return;
820 ministate.editing = 0;
822 /* XXX the minibuffer line is never modified so this is fine */
823 text = (char *)hist_cur(ministate.hist);
824 buffer->current_line->parent->line = text;
825 recompute_completions(0);
828 void
829 cmd_mini_next_history_element(struct buffer *buffer)
831 char *text;
833 if (ministate.hist == NULL) {
834 message("No history");
835 return;
838 if (hist_next(ministate.hist) == NULL) {
839 message("No next history item");
840 return;
843 ministate.editing = 0;
845 /* XXX the minibuffer line is never modified so this is fine */
846 text = (char *)hist_cur(ministate.hist);
847 buffer->current_line->parent->line = text;
848 recompute_completions(0);
851 void
852 cmd_previous_completion(struct buffer *buffer)
854 if (in_minibuffer != MB_COMPREAD)
855 return;
857 buffer = &ministate.compl.buffer;
858 if (buffer->current_line == NULL)
859 return;
861 buffer->current_line->parent->type = LINE_COMPL;
862 if (!forward_line(buffer, -1))
863 buffer->current_line->parent->type = LINE_COMPL;
864 else
865 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
868 void
869 cmd_next_completion(struct buffer *buffer)
871 if (in_minibuffer != MB_COMPREAD)
872 return;
874 buffer = &ministate.compl.buffer;
875 if (buffer->current_line == NULL)
876 return;
878 if (buffer->current_line->parent->type == LINE_COMPL_CURRENT) {
879 buffer->current_line->parent->type = LINE_COMPL;
880 forward_line(buffer, +1);
883 if (buffer->current_line != NULL)
884 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
887 void
888 cmd_insert_current_candidate(struct buffer *buffer)
890 if (in_minibuffer != MB_COMPREAD)
891 return;
893 minibuffer_insert_current_candidate();
896 void
897 cmd_suspend_telescope(struct buffer *buffer)
899 message("Zzz...");
900 ui_suspend();
903 void
904 cmd_toggle_pre_wrap(struct buffer *buffer)
906 dont_wrap_pre = !dont_wrap_pre;
908 if (dont_wrap_pre)
909 message("Don't wrap preformatted blocks");
910 else
911 message("Wrap preformatted blocks");
913 ui_schedule_redraw();
916 void
917 cmd_mini_goto_beginning(struct buffer *buffer)
919 struct vline *vl;
921 if (!in_minibuffer)
922 return;
924 buffer = &ministate.compl.buffer;
926 if ((vl = buffer->current_line) != NULL)
927 vl->parent->type = LINE_COMPL;
929 vl = TAILQ_FIRST(&buffer->head);
930 while (vl != NULL && vl->parent->flags & L_HIDDEN)
931 vl = TAILQ_NEXT(vl, vlines);
933 if (vl == NULL)
934 return;
936 vl->parent->type = LINE_COMPL_CURRENT;
937 buffer->top_line = vl;
938 buffer->current_line = vl;
941 void
942 cmd_mini_goto_end(struct buffer *buffer)
944 struct vline *vl;
946 if (!in_minibuffer)
947 return;
949 buffer = &ministate.compl.buffer;
951 if ((vl = buffer->current_line) != NULL)
952 vl->parent->type = LINE_COMPL;
954 vl = TAILQ_LAST(&buffer->head, vhead);
955 while (vl != NULL && vl->parent->flags & L_HIDDEN)
956 vl = TAILQ_PREV(vl, vhead, vlines);
958 if (vl == NULL)
959 return;
961 vl->parent->type = LINE_COMPL_CURRENT;
962 buffer->current_line = vl;
965 void
966 cmd_other_window(struct buffer *buffer)
968 ui_other_window();
971 void
972 cmd_mini_scroll_up(struct buffer *buffer)
974 if (!in_minibuffer)
975 return;
977 buffer = &ministate.compl.buffer;
978 if (buffer->current_line == NULL)
979 return;
981 buffer->current_line->parent->type = LINE_COMPL;
982 cmd_scroll_up(buffer);
983 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
986 void
987 cmd_mini_scroll_down(struct buffer *buffer)
989 if (!in_minibuffer)
990 return;
992 buffer = &ministate.compl.buffer;
993 if (buffer->current_line == NULL)
994 return;
996 buffer->current_line->parent->type = LINE_COMPL;
997 cmd_scroll_down(buffer);
998 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
1001 void
1002 cmd_toggle_downloads(struct buffer *buffer)
1004 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1007 void
1008 cmd_cache_info(struct buffer *buffer)
1010 size_t npages, tot;
1011 char fmt[FMT_SCALED_STRSIZE];
1013 mcache_info(&npages, &tot);
1015 if (fmt_scaled(tot, fmt) == 0)
1016 message("pages: %zu, total: %s", npages, fmt);
1017 else
1018 message("pages: %zu, total: %zu", npages, tot);
1021 void
1022 cmd_reply_last_input(struct buffer *buffer)
1024 GUARD_RECURSIVE_MINIBUFFER();
1026 if (current_tab->last_input_url == NULL) {
1027 message("there was no previous input request in this tab");
1028 return;
1031 if (!strncmp(current_tab->last_input_url, "gopher", 6)) {
1032 load_url_in_tab(current_tab, current_tab->last_input_url,
1033 NULL, LU_MODE_NOCACHE);
1034 return;
1037 message("%s", current_tab->last_input_url);
1038 ui_require_input(current_tab, 0, ir_select_reply);
1041 void
1042 cmd_write_buffer(struct buffer *buffer)
1044 const char *f, *url;
1045 char path[PATH_MAX];
1047 GUARD_RECURSIVE_MINIBUFFER();
1049 if (safe_mode) {
1050 message("Can't write buffer in safe-mode.");
1051 return;
1054 url = hist_cur(current_tab->hist);
1056 if ((f = strrchr(url, '/')) != NULL)
1057 f++;
1058 if (f == NULL || *f == '\0') {
1059 /* guess a decent file name based on the protocol used */
1060 if (!strncmp(url, "gemini://", 9))
1061 f = "index.gmi";
1062 else
1063 f = "index.txt";
1066 strlcpy(path, download_path, sizeof(path));
1067 strlcat(path, f, sizeof(path));
1069 ui_read("Write file", write_buffer, current_tab, path);
1072 void
1073 cmd_home(struct buffer *buffer)
1075 char path[GEMINI_URL_LEN];
1076 char *tilde, *t;
1078 strlcpy(path, current_tab->iri.iri_path, sizeof(path));
1080 if ((tilde = strstr(path, "/~")) != NULL &&
1081 tilde[2] != '\0' && tilde[2] != '/') {
1082 if ((t = strchr(tilde + 2, '/')) != NULL)
1083 *++t = '\0';
1084 load_url_in_tab(current_tab, path, NULL, LU_MODE_NOCACHE);
1085 } else
1086 cmd_root(buffer);
1089 void
1090 cmd_root(struct buffer *buffer)
1092 load_url_in_tab(current_tab, "/", NULL, LU_MODE_NOCACHE);
1095 void
1096 cmd_up(struct buffer *buffer)
1098 load_url_in_tab(current_tab, "..", NULL, LU_MODE_NOCACHE);
1101 void
1102 cmd_use_certificate(struct buffer *buffer)
1104 GUARD_RECURSIVE_MINIBUFFER();
1106 enter_minibuffer(sensible_self_insert, uc_select, exit_minibuffer,
1107 NULL, compl_uc, NULL, 1);
1108 strlcpy(ministate.prompt, "Select certificate: ",
1109 sizeof(ministate.prompt));
1112 void
1113 cmd_client_certificate_info(struct buffer *buffer)
1115 if (current_tab->client_cert)
1116 message("Using certificate %s", current_tab->client_cert);
1117 else
1118 message("Not using any client certificate.");
1121 static void
1122 unload_certificate_cb(int r, struct tab *tab)
1124 message("Won't use %s for this site.", tab->client_cert);
1125 cert_delete_for(tab->client_cert, &tab->iri, r);
1128 void
1129 cmd_unload_certificate(struct buffer *buffer)
1131 struct tab *tab = current_tab;
1133 GUARD_RECURSIVE_MINIBUFFER();
1135 if (tab->client_cert == NULL) {
1136 message("No client certificate in use!");
1137 return;
1140 if (tab->client_cert_temp) {
1141 message("Won't use %s for this site.", tab->client_cert);
1142 cert_delete_for(tab->client_cert, &tab->iri, 0);
1143 return;
1146 yornp("Unload only for the current session?", unload_certificate_cb,
1147 current_tab);
1150 void
1151 cmd_search(struct buffer *buffer)
1153 GUARD_RECURSIVE_MINIBUFFER();
1155 if (!strncmp(default_search_engine, "gopher://", 9)) {
1156 load_url_in_tab(current_tab, default_search_engine, NULL,
1157 LU_MODE_NOCACHE);
1158 return;
1161 enter_minibuffer(sensible_self_insert, search_select, exit_minibuffer, NULL,
1162 NULL, NULL, 0);
1163 strlcpy(ministate.prompt, "Search: ", sizeof(ministate.prompt));