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 "compat.h"
19 #include <limits.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "compl.h"
24 #include "defaults.h"
25 #include "mcache.h"
26 #include "minibuffer.h"
27 #include "session.h"
28 #include "telescope.h"
29 #include "ui.h"
30 #include "utf8.h"
31 #include "utils.h"
33 #define GUARD_RECURSIVE_MINIBUFFER() \
34 do { \
35 if (in_minibuffer) { \
36 message("enable-recursive-minibuffers " \
37 "is not yet available."); \
38 return; \
39 } \
40 } while(0)
42 #define GUARD_READ_ONLY() \
43 do { \
44 if (!in_minibuffer) { \
45 message("text is read-only"); \
46 return; \
47 } \
48 } while(0)
50 /* return 1 if moved, 0 otherwise */
51 static inline int
52 forward_line(struct buffer *buffer, int n)
53 {
54 struct vline *vl;
55 int did;
57 if (buffer->current_line == NULL)
58 return 0;
59 vl = buffer->current_line;
61 did = 0;
62 while (n != 0) {
63 if (n > 0) {
64 vl = TAILQ_NEXT(vl, vlines);
65 if (vl == NULL)
66 return did;
67 if (vl->parent->flags & L_HIDDEN)
68 continue;
69 buffer->current_line = vl;
70 n--;
71 } else {
72 vl = TAILQ_PREV(vl, vhead, vlines);
73 if (vl == NULL)
74 return did;
75 if (vl->parent->flags & L_HIDDEN)
76 continue;
77 if (buffer->current_line == buffer->top_line) {
78 buffer->line_off--;
79 buffer->top_line = vl;
80 }
81 buffer->current_line = vl;
82 n++;
83 }
85 did = 1;
86 }
88 return did;
89 }
91 void
92 cmd_previous_line(struct buffer *buffer)
93 {
94 forward_line(buffer, -1);
95 }
97 void
98 cmd_next_line(struct buffer *buffer)
99 {
100 forward_line(buffer, +1);
103 void
104 cmd_backward_char(struct buffer *buffer)
106 if (buffer->cpoff != 0)
107 buffer->cpoff--;
110 void
111 cmd_forward_char(struct buffer *buffer)
113 if (buffer->current_line == NULL)
114 return;
115 if (buffer->current_line->cplen > buffer->cpoff)
116 buffer->cpoff++;
119 void
120 cmd_backward_paragraph(struct buffer *buffer)
122 do {
123 if (!forward_line(buffer, -1)) {
124 message("No previous paragraph");
125 return;
127 } while (buffer->current_line->len != 0 ||
128 buffer->current_line->parent->type != LINE_TEXT);
131 void
132 cmd_forward_paragraph(struct buffer *buffer)
134 do {
135 if (!forward_line(buffer, +1)) {
136 message("No next paragraph");
137 return;
139 } while (buffer->current_line->len != 0 ||
140 buffer->current_line->parent->type != LINE_TEXT);
143 void
144 cmd_move_beginning_of_line(struct buffer *buffer)
146 buffer->cpoff = 0;
149 void
150 cmd_move_end_of_line(struct buffer *buffer)
152 struct vline *vl;
154 vl = buffer->current_line;
155 if (vl == NULL)
156 return;
157 buffer->cpoff = vl->cplen;
160 void
161 cmd_redraw(struct buffer *buffer)
163 ui_schedule_redraw();
166 void
167 cmd_scroll_line_up(struct buffer *buffer)
169 struct vline *vl;
171 for (;;) {
172 if (buffer->top_line == NULL)
173 return;
175 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
176 == NULL)
177 return;
179 buffer->top_line = vl;
181 if (vl->parent->flags & L_HIDDEN)
182 continue;
184 break;
187 buffer->line_off--;
189 forward_line(buffer, -1);
192 void
193 cmd_scroll_line_down(struct buffer *buffer)
195 if (!forward_line(buffer, +1))
196 return;
198 for (;;) {
199 if (buffer->top_line == NULL)
200 return;
202 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
203 if (buffer->top_line->parent->flags & L_HIDDEN)
204 continue;
205 break;
208 buffer->line_off++;
211 void
212 cmd_scroll_up(struct buffer *buffer)
214 struct vline *vl;
215 int i;
217 if (buffer->top_line == NULL)
218 return;
220 for (i = 0; i < body_lines; ++i) {
221 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
222 if (vl == NULL)
223 break;
224 buffer->line_off--;
225 buffer->top_line = vl;
226 forward_line(buffer, -1);
230 void
231 cmd_scroll_down(struct buffer *buffer)
233 int i;
235 if (buffer->top_line == NULL)
236 return;
238 for (i = 0; i < body_lines; ++i) {
239 if (!forward_line(buffer, +1))
240 break;
242 buffer->top_line = TAILQ_NEXT(buffer->top_line,
243 vlines);
244 buffer->line_off++;
248 void
249 cmd_beginning_of_buffer(struct buffer *buffer)
251 buffer->current_line = TAILQ_FIRST(&buffer->head);
252 buffer->cpoff = 0;
253 buffer->top_line = buffer->current_line;
254 buffer->line_off = 0;
257 void
258 cmd_end_of_buffer(struct buffer *buffer)
260 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
262 if (buffer->current_line == NULL)
263 return;
265 /* deal with invisible lines */
266 if (buffer->current_line->parent->flags & L_HIDDEN)
267 forward_line(buffer, -1);
269 cmd_move_end_of_line(buffer);
272 static void
273 kill_telescope_cb(int r, struct tab *tab)
275 if (r) {
276 save_session();
277 event_loopbreak();
281 void
282 cmd_kill_telescope(struct buffer *buffer)
284 yornp("really quit?", kill_telescope_cb, NULL);
287 void
288 cmd_push_button(struct buffer *buffer)
290 struct vline *vl;
291 struct line *l;
293 vl = buffer->current_line;
295 if (vl == NULL)
296 return;
298 switch (vl->parent->type) {
299 case LINE_LINK:
300 load_url_in_tab(current_tab, vl->parent->alt, NULL,
301 LU_MODE_NOCACHE);
302 break;
303 case LINE_PRE_START:
304 l = TAILQ_NEXT(vl->parent, lines);
305 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
306 if (l->type == LINE_PRE_END)
307 break;
308 l->flags ^= L_HIDDEN;
309 if (l->flags & L_HIDDEN)
310 buffer->line_max--;
311 else
312 buffer->line_max++;
314 break;
315 default:
316 break;
320 void
321 cmd_push_button_new_tab(struct buffer *buffer)
323 struct vline *vl;
325 vl = buffer->current_line;
326 if (vl == NULL || vl->parent->type != LINE_LINK)
327 return;
329 new_tab(vl->parent->alt, current_tab->hist_cur->h, current_tab);
332 void
333 cmd_previous_button(struct buffer *buffer)
335 struct excursion place;
337 save_excursion(&place, buffer);
339 do {
340 if (!forward_line(buffer, -1)) {
341 restore_excursion(&place, buffer);
342 message("No previous link");
343 return;
345 } while (buffer->current_line->parent->type != LINE_LINK);
348 void
349 cmd_next_button(struct buffer *buffer)
351 struct excursion place;
353 save_excursion(&place, buffer);
355 do {
356 if (!forward_line(buffer, +1)){
357 restore_excursion(&place, buffer);
358 message("No next link");
359 return;
361 } while (buffer->current_line->parent->type != LINE_LINK);
364 static inline int
365 is_heading(const struct line *l)
367 return l->type == LINE_TITLE_1 ||
368 l->type == LINE_TITLE_2 ||
369 l->type == LINE_TITLE_3;
372 void
373 cmd_previous_heading(struct buffer *buffer)
375 struct excursion place;
377 save_excursion(&place, buffer);
379 do {
380 if (!forward_line(buffer, -1)) {
381 restore_excursion(&place, buffer);
382 message("No previous heading");
383 return;
385 } while (!is_heading(buffer->current_line->parent));
388 void
389 cmd_next_heading(struct buffer *buffer)
391 struct excursion place;
393 save_excursion(&place, buffer);
395 do {
396 if (!forward_line(buffer, +1)) {
397 restore_excursion(&place, buffer);
398 message("No next heading");
399 return;
401 } while (!is_heading(buffer->current_line->parent));
404 void
405 cmd_previous_page(struct buffer *buffer)
407 if (!load_previous_page(current_tab))
408 message("No previous page");
411 void
412 cmd_next_page(struct buffer *buffer)
414 if (!load_next_page(current_tab))
415 message("No next page");
418 void
419 cmd_clear_minibuf(struct buffer *buffer)
421 message(NULL);
424 void
425 cmd_execute_extended_command(struct buffer *buffer)
427 size_t len;
429 GUARD_RECURSIVE_MINIBUFFER();
431 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
432 &eecmd_history, compl_eecmd, NULL, 1);
434 len = sizeof(ministate.prompt);
435 strlcpy(ministate.prompt, "", len);
437 if (thiskey.meta)
438 strlcat(ministate.prompt, "M-", len);
440 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
442 if (thiskey.meta)
443 strlcat(ministate.prompt, " ", len);
446 void
447 cmd_tab_close(struct buffer *buffer)
449 struct tab *tab, *t;
451 tab = current_tab;
453 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
454 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
455 switch_to_tab(t);
456 kill_tab(tab, 0);
457 } else
458 message("Can't close the only tab.");
462 void
463 cmd_tab_close_other(struct buffer *buffer)
465 struct tab *t, *i;
467 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
468 if (t == current_tab)
469 continue;
471 kill_tab(t, 0);
475 void
476 cmd_tab_undo_close(struct buffer *buffer)
478 struct tab *t;
480 if ((t = unkill_tab()) == NULL) {
481 message("No recently-closed tabs");
482 return;
485 switch_to_tab(t);
488 void
489 cmd_tab_new(struct buffer *buffer)
491 const char *url;
493 if ((url = new_tab_url) == NULL)
494 url = NEW_TAB_URL;
496 new_tab(url, NULL, NULL);
499 void
500 cmd_tab_next(struct buffer *buffer)
502 struct tab *t;
504 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
505 t = TAILQ_FIRST(&tabshead);
506 switch_to_tab(t);
509 void
510 cmd_tab_previous(struct buffer *buffer)
512 struct tab *t;
514 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
515 t = TAILQ_LAST(&tabshead, tabshead);
516 switch_to_tab(t);
519 void
520 cmd_tab_move(struct buffer *buffer)
522 struct tab *t;
524 t = TAILQ_NEXT(current_tab, tabs);
525 TAILQ_REMOVE(&tabshead, current_tab, tabs);
527 if (t == NULL)
528 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
529 else
530 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
533 void
534 cmd_tab_move_to(struct buffer *buffer)
536 struct tab *t;
538 t = TAILQ_PREV(current_tab, tabshead, tabs);
539 TAILQ_REMOVE(&tabshead, current_tab, tabs);
541 if (t == NULL)
542 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
543 else
544 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
547 void
548 cmd_tab_select(struct buffer *buffer)
550 GUARD_RECURSIVE_MINIBUFFER();
552 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
553 NULL, compl_ts, NULL, 1);
554 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
557 void
558 cmd_load_url(struct buffer *buffer)
560 GUARD_RECURSIVE_MINIBUFFER();
562 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
563 &lu_history, compl_lu, NULL, 0);
564 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
567 void
568 cmd_load_current_url(struct buffer *buffer)
570 GUARD_RECURSIVE_MINIBUFFER();
572 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
573 &lu_history, compl_lu, NULL, 0);
574 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
575 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
576 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
579 void
580 cmd_reload_page(struct buffer *buffer)
582 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL,
583 LU_MODE_NOHIST|LU_MODE_NOCACHE);
586 void
587 cmd_bookmark_page(struct buffer *buffer)
589 GUARD_RECURSIVE_MINIBUFFER();
591 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
592 NULL, NULL, 0);
593 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
594 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
595 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
598 void
599 cmd_list_bookmarks(struct buffer *buffer)
601 load_url_in_tab(current_tab, "about:bookmarks", NULL, LU_MODE_NONE);
604 void
605 cmd_toggle_help(struct buffer *buffer)
607 ui_toggle_side_window(SIDE_WINDOW_LEFT);
610 void
611 cmd_link_select(struct buffer *buffer)
613 struct line *l;
615 GUARD_RECURSIVE_MINIBUFFER();
617 l = TAILQ_FIRST(&buffer->page.head);
618 while (l != NULL && l->type != LINE_LINK)
619 l = TAILQ_NEXT(l, lines);
621 if (l == NULL) {
622 message("No links found");
623 return;
626 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
627 NULL, compl_ls, l, 1);
628 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
631 void
632 cmd_swiper(struct buffer *buffer)
634 GUARD_RECURSIVE_MINIBUFFER();
636 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
637 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head), 1);
638 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
641 void
642 cmd_toc(struct buffer *buffer)
644 struct line *l;
646 GUARD_RECURSIVE_MINIBUFFER();
648 l = TAILQ_FIRST(&buffer->page.head);
649 while (l != NULL &&
650 l->type != LINE_TITLE_1 &&
651 l->type != LINE_TITLE_2 &&
652 l->type != LINE_TITLE_3)
653 l = TAILQ_NEXT(l, lines);
655 if (l == NULL) {
656 message("No headings found");
657 return;
660 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
661 NULL, compl_toc, l, 1);
662 strlcpy(ministate.prompt, "Select heading: ",
663 sizeof(ministate.prompt));
666 void
667 cmd_inc_fill_column(struct buffer *buffer)
669 if (fill_column == INT_MAX)
670 return;
672 fill_column += 2;
673 message("fill-column: %d", fill_column);
675 ui_schedule_redraw();
678 void
679 cmd_dec_fill_column(struct buffer *buffer)
681 if (fill_column == INT_MAX || fill_column < 8)
682 return;
684 fill_column -= 2;
685 message("fill-column: %d", fill_column);
687 ui_schedule_redraw();
690 void
691 cmd_olivetti_mode(struct buffer *buffer)
693 olivetti_mode = !olivetti_mode;
694 if (olivetti_mode)
695 message("olivetti-mode enabled");
696 else
697 message("olivetti-mode disabled");
699 ui_schedule_redraw();
702 void
703 cmd_mini_delete_char(struct buffer *buffer)
705 char *line, *c, *n;
707 GUARD_READ_ONLY();
709 minibuffer_taint_hist();
711 line = buffer->current_line->parent->line + buffer->current_line->from;
712 c = utf8_nth(line, buffer->cpoff);
713 if (*c == '\0')
714 return;
715 n = utf8_next_cp(c);
717 memmove(c, n, strlen(n)+1);
719 recompute_completions(0);
722 void
723 cmd_mini_delete_backward_char(struct buffer *buffer)
725 char *line, *c, *p;
727 GUARD_READ_ONLY();
729 minibuffer_taint_hist();
731 line = buffer->current_line->parent->line + buffer->current_line->from;
732 c = utf8_nth(line, buffer->cpoff);
733 if (c == line)
734 return;
735 p = utf8_prev_cp(c-1, line);
737 memmove(p, c, strlen(c)+1);
738 buffer->cpoff--;
740 recompute_completions(0);
743 void
744 cmd_mini_kill_line(struct buffer *buffer)
746 char *line, *c;
748 GUARD_READ_ONLY();
750 minibuffer_taint_hist();
752 line = buffer->current_line->parent->line + buffer->current_line->from;
753 c = utf8_nth(line, buffer->cpoff);
754 *c = '\0';
756 recompute_completions(0);
759 void
760 cmd_mini_kill_whole_line(struct buffer *buffer)
762 GUARD_READ_ONLY();
764 minibuffer_taint_hist();
765 *buffer->current_line->parent->line = '\0';
766 buffer->cpoff = 0;
769 void
770 cmd_mini_abort(struct buffer *buffer)
772 if (!in_minibuffer)
773 return;
775 ministate.abortfn();
778 void
779 cmd_mini_complete_and_exit(struct buffer *buffer)
781 struct vline *vl;
783 if (!in_minibuffer)
784 return;
786 if (ministate.compl.must_select && ministate.hist_cur == NULL) {
787 vl = ministate.compl.buffer.current_line;
788 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
789 vl->parent->type == LINE_COMPL) {
790 message("no match");
791 return;
795 minibuffer_taint_hist();
796 ministate.donefn();
799 void
800 cmd_mini_previous_history_element(struct buffer *buffer)
802 if (ministate.history == NULL) {
803 message("No history");
804 return;
807 if (ministate.hist_cur == NULL ||
808 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
809 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
810 ministate.hist_off = ministate.history->len - 1;
811 if (ministate.hist_cur == NULL)
812 message("No prev history item");
813 } else {
814 ministate.hist_off--;
817 if (ministate.hist_cur != NULL) {
818 buffer->current_line->parent->line = ministate.hist_cur->h;
819 recompute_completions(0);
823 void
824 cmd_mini_next_history_element(struct buffer *buffer)
826 if (ministate.history == NULL) {
827 message("No history");
828 return;
831 if (ministate.hist_cur == NULL ||
832 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
833 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
834 ministate.hist_off = 0;
835 if (ministate.hist_cur == NULL)
836 message("No next history item");
837 } else {
838 ministate.hist_off++;
841 if (ministate.hist_cur != NULL) {
842 buffer->current_line->parent->line = ministate.hist_cur->h;
843 recompute_completions(0);
847 void
848 cmd_previous_completion(struct buffer *buffer)
850 if (in_minibuffer != MB_COMPREAD)
851 return;
853 buffer = &ministate.compl.buffer;
854 if (buffer->current_line == NULL)
855 return;
857 buffer->current_line->parent->type = LINE_COMPL;
858 if (!forward_line(buffer, -1))
859 buffer->current_line->parent->type = LINE_COMPL;
860 else
861 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
864 void
865 cmd_next_completion(struct buffer *buffer)
867 if (in_minibuffer != MB_COMPREAD)
868 return;
870 buffer = &ministate.compl.buffer;
871 if (buffer->current_line == NULL)
872 return;
874 if (buffer->current_line->parent->type == LINE_COMPL_CURRENT) {
875 buffer->current_line->parent->type = LINE_COMPL;
876 forward_line(buffer, +1);
879 if (buffer->current_line != NULL)
880 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
883 void
884 cmd_insert_current_candidate(struct buffer *buffer)
886 if (in_minibuffer != MB_COMPREAD)
887 return;
889 minibuffer_insert_current_candidate();
892 void
893 cmd_suspend_telescope(struct buffer *buffer)
895 message("Zzz...");
896 ui_suspend();
899 void
900 cmd_toggle_pre_wrap(struct buffer *buffer)
902 dont_wrap_pre = !dont_wrap_pre;
904 if (dont_wrap_pre)
905 message("Don't wrap preformatted blocks");
906 else
907 message("Wrap preformatted blocks");
909 ui_schedule_redraw();
912 void
913 cmd_mini_goto_beginning(struct buffer *buffer)
915 struct vline *vl;
917 if (!in_minibuffer)
918 return;
920 buffer = &ministate.compl.buffer;
922 if ((vl = buffer->current_line) != NULL)
923 vl->parent->type = LINE_COMPL;
925 vl = TAILQ_FIRST(&buffer->head);
926 while (vl != NULL && vl->parent->flags & L_HIDDEN)
927 vl = TAILQ_NEXT(vl, vlines);
929 if (vl == NULL)
930 return;
932 vl->parent->type = LINE_COMPL_CURRENT;
933 buffer->top_line = vl;
934 buffer->current_line = vl;
937 void
938 cmd_mini_goto_end(struct buffer *buffer)
940 struct vline *vl;
942 if (!in_minibuffer)
943 return;
945 buffer = &ministate.compl.buffer;
947 if ((vl = buffer->current_line) != NULL)
948 vl->parent->type = LINE_COMPL;
950 vl = TAILQ_LAST(&buffer->head, vhead);
951 while (vl != NULL && vl->parent->flags & L_HIDDEN)
952 vl = TAILQ_PREV(vl, vhead, vlines);
954 if (vl == NULL)
955 return;
957 vl->parent->type = LINE_COMPL_CURRENT;
958 buffer->current_line = vl;
961 void
962 cmd_other_window(struct buffer *buffer)
964 ui_other_window();
967 void
968 cmd_mini_scroll_up(struct buffer *buffer)
970 if (!in_minibuffer)
971 return;
973 buffer = &ministate.compl.buffer;
974 if (buffer->current_line == NULL)
975 return;
977 buffer->current_line->parent->type = LINE_COMPL;
978 cmd_scroll_up(buffer);
979 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
982 void
983 cmd_mini_scroll_down(struct buffer *buffer)
985 if (!in_minibuffer)
986 return;
988 buffer = &ministate.compl.buffer;
989 if (buffer->current_line == NULL)
990 return;
992 buffer->current_line->parent->type = LINE_COMPL;
993 cmd_scroll_down(buffer);
994 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
997 void
998 cmd_toggle_downloads(struct buffer *buffer)
1000 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1003 void
1004 cmd_cache_info(struct buffer *buffer)
1006 size_t npages, tot;
1007 char fmt[FMT_SCALED_STRSIZE];
1009 mcache_info(&npages, &tot);
1011 if (fmt_scaled(tot, fmt) == 0)
1012 message("pages: %zu, total: %s", npages, fmt);
1013 else
1014 message("pages: %zu, total: %zu", npages, tot);
1017 void
1018 cmd_reply_last_input(struct buffer *buffer)
1020 GUARD_RECURSIVE_MINIBUFFER();
1022 if (current_tab->last_input_url == NULL) {
1023 message("there was no previous input request in this tab");
1024 return;
1027 if (!strncmp(current_tab->last_input_url, "gopher", 6)) {
1028 load_url_in_tab(current_tab, current_tab->last_input_url,
1029 NULL, LU_MODE_NOCACHE);
1030 return;
1033 message("%s", current_tab->last_input_url);
1034 ui_require_input(current_tab, 0, ir_select_reply);
1037 void
1038 cmd_write_buffer(struct buffer *buffer)
1040 const char *f, *url;
1041 char path[PATH_MAX];
1043 GUARD_RECURSIVE_MINIBUFFER();
1045 if (safe_mode) {
1046 message("Can't write buffer in safe-mode.");
1047 return;
1050 url = current_tab->hist_cur->h;
1052 if ((f = strrchr(url, '/')) != NULL)
1053 f++;
1054 if (f == NULL || *f == '\0') {
1055 /* guess a decent file name based on the protocol used */
1056 if (!strncmp(url, "gemini://", 9))
1057 f = "index.gmi";
1058 else
1059 f = "index.txt";
1062 strlcpy(path, download_path, sizeof(path));
1063 strlcat(path, f, sizeof(path));
1065 ui_read("Write file", write_buffer, current_tab, path);