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 size_t len = 0;
115 if (buffer->current_line == NULL)
116 return;
118 if (buffer->current_line->line != NULL)
119 len = utf8_cplen(buffer->current_line->line);
120 if (++buffer->cpoff > len)
121 buffer->cpoff = len;
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->line != NULL ||
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->line != NULL ||
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 || vl->line == NULL)
161 return;
162 buffer->cpoff = utf8_cplen(vl->line);
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 event_loopbreak();
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, current_tab->hist_cur->h, 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, current_tab->hist_cur->h, 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, current_tab->hist_cur->h, 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, current_tab->hist_cur->h, 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 *c, *n;
712 GUARD_READ_ONLY();
714 minibuffer_taint_hist();
716 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
717 if (*c == '\0')
718 return;
719 n = utf8_next_cp(c);
721 memmove(c, n, strlen(n)+1);
723 recompute_completions(0);
726 void
727 cmd_mini_delete_backward_char(struct buffer *buffer)
729 char *c, *p, *start;
731 GUARD_READ_ONLY();
733 minibuffer_taint_hist();
735 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
736 start = buffer->current_line->line;
737 if (c == start)
738 return;
739 p = utf8_prev_cp(c-1, start);
741 memmove(p, c, strlen(c)+1);
742 buffer->cpoff--;
744 recompute_completions(0);
747 void
748 cmd_mini_kill_line(struct buffer *buffer)
750 char *c;
752 GUARD_READ_ONLY();
754 minibuffer_taint_hist();
755 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
756 *c = '\0';
758 recompute_completions(0);
761 void
762 cmd_mini_kill_whole_line(struct buffer *buffer)
764 GUARD_READ_ONLY();
766 minibuffer_taint_hist();
767 *buffer->current_line->line = '\0';
768 buffer->cpoff = 0;
771 void
772 cmd_mini_abort(struct buffer *buffer)
774 if (!in_minibuffer)
775 return;
777 ministate.abortfn();
780 void
781 cmd_mini_complete_and_exit(struct buffer *buffer)
783 struct vline *vl;
785 if (!in_minibuffer)
786 return;
788 if (ministate.compl.must_select && ministate.hist_cur == NULL) {
789 vl = ministate.compl.buffer.current_line;
790 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
791 vl->parent->type == LINE_COMPL) {
792 message("no match");
793 return;
797 minibuffer_taint_hist();
798 ministate.donefn();
801 void
802 cmd_mini_previous_history_element(struct buffer *buffer)
804 if (ministate.history == NULL) {
805 message("No history");
806 return;
809 if (ministate.hist_cur == NULL ||
810 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
811 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
812 ministate.hist_off = ministate.history->len - 1;
813 if (ministate.hist_cur == NULL)
814 message("No prev history item");
815 } else {
816 ministate.hist_off--;
819 if (ministate.hist_cur != NULL) {
820 buffer->current_line->line = ministate.hist_cur->h;
821 recompute_completions(0);
825 void
826 cmd_mini_next_history_element(struct buffer *buffer)
828 if (ministate.history == NULL) {
829 message("No history");
830 return;
833 if (ministate.hist_cur == NULL ||
834 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
835 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
836 ministate.hist_off = 0;
837 if (ministate.hist_cur == NULL)
838 message("No next history item");
839 } else {
840 ministate.hist_off++;
843 if (ministate.hist_cur != NULL) {
844 buffer->current_line->line = ministate.hist_cur->h;
845 recompute_completions(0);
849 void
850 cmd_previous_completion(struct buffer *buffer)
852 if (in_minibuffer != MB_COMPREAD)
853 return;
855 buffer = &ministate.compl.buffer;
856 if (buffer->current_line == NULL)
857 return;
859 buffer->current_line->parent->type = LINE_COMPL;
860 if (!forward_line(buffer, -1))
861 buffer->current_line->parent->type = LINE_COMPL;
862 else
863 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
866 void
867 cmd_next_completion(struct buffer *buffer)
869 if (in_minibuffer != MB_COMPREAD)
870 return;
872 buffer = &ministate.compl.buffer;
873 if (buffer->current_line == NULL)
874 return;
876 if (buffer->current_line->parent->type == LINE_COMPL_CURRENT) {
877 buffer->current_line->parent->type = LINE_COMPL;
878 forward_line(buffer, +1);
881 if (buffer->current_line != NULL)
882 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
885 void
886 cmd_insert_current_candidate(struct buffer *buffer)
888 if (in_minibuffer != MB_COMPREAD)
889 return;
891 minibuffer_insert_current_candidate();
894 void
895 cmd_suspend_telescope(struct buffer *buffer)
897 message("Zzz...");
898 ui_suspend();
901 void
902 cmd_toggle_pre_wrap(struct buffer *buffer)
904 dont_wrap_pre = !dont_wrap_pre;
906 if (dont_wrap_pre)
907 message("Don't wrap preformatted blocks");
908 else
909 message("Wrap preformatted blocks");
911 ui_schedule_redraw();
914 void
915 cmd_mini_goto_beginning(struct buffer *buffer)
917 struct vline *vl;
919 if (!in_minibuffer)
920 return;
922 buffer = &ministate.compl.buffer;
924 if ((vl = buffer->current_line) != NULL)
925 vl->parent->type = LINE_COMPL;
927 vl = TAILQ_FIRST(&buffer->head);
928 while (vl != NULL && vl->parent->flags & L_HIDDEN)
929 vl = TAILQ_NEXT(vl, vlines);
931 if (vl == NULL)
932 return;
934 vl->parent->type = LINE_COMPL_CURRENT;
935 buffer->top_line = vl;
936 buffer->current_line = vl;
939 void
940 cmd_mini_goto_end(struct buffer *buffer)
942 struct vline *vl;
944 if (!in_minibuffer)
945 return;
947 buffer = &ministate.compl.buffer;
949 if ((vl = buffer->current_line) != NULL)
950 vl->parent->type = LINE_COMPL;
952 vl = TAILQ_LAST(&buffer->head, vhead);
953 while (vl != NULL && vl->parent->flags & L_HIDDEN)
954 vl = TAILQ_PREV(vl, vhead, vlines);
956 if (vl == NULL)
957 return;
959 vl->parent->type = LINE_COMPL_CURRENT;
960 buffer->current_line = vl;
963 void
964 cmd_other_window(struct buffer *buffer)
966 ui_other_window();
969 void
970 cmd_mini_scroll_up(struct buffer *buffer)
972 if (!in_minibuffer)
973 return;
975 buffer = &ministate.compl.buffer;
976 if (buffer->current_line == NULL)
977 return;
979 buffer->current_line->parent->type = LINE_COMPL;
980 cmd_scroll_up(buffer);
981 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
984 void
985 cmd_mini_scroll_down(struct buffer *buffer)
987 if (!in_minibuffer)
988 return;
990 buffer = &ministate.compl.buffer;
991 if (buffer->current_line == NULL)
992 return;
994 buffer->current_line->parent->type = LINE_COMPL;
995 cmd_scroll_down(buffer);
996 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
999 void
1000 cmd_toggle_downloads(struct buffer *buffer)
1002 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1005 void
1006 cmd_cache_info(struct buffer *buffer)
1008 size_t npages, tot;
1009 char fmt[FMT_SCALED_STRSIZE];
1011 mcache_info(&npages, &tot);
1013 if (fmt_scaled(tot, fmt) == 0)
1014 message("pages: %zu, total: %s", npages, fmt);
1015 else
1016 message("pages: %zu, total: %zu", npages, tot);
1019 void
1020 cmd_reply_last_input(struct buffer *buffer)
1022 GUARD_RECURSIVE_MINIBUFFER();
1024 if (current_tab->last_input_url == NULL) {
1025 message("there was no previous input request in this tab");
1026 return;
1029 if (!strncmp(current_tab->last_input_url, "gopher", 6)) {
1030 load_url_in_tab(current_tab, current_tab->last_input_url,
1031 NULL, LU_MODE_NOCACHE);
1032 return;
1035 message("%s", current_tab->last_input_url);
1036 ui_require_input(current_tab, 0, ir_select_reply);
1039 void
1040 cmd_write_buffer(struct buffer *buffer)
1042 const char *f, *url;
1043 char path[PATH_MAX];
1045 GUARD_RECURSIVE_MINIBUFFER();
1047 if (safe_mode) {
1048 message("Can't write buffer in safe-mode.");
1049 return;
1052 url = current_tab->hist_cur->h;
1054 if ((f = strrchr(url, '/')) != NULL)
1055 f++;
1056 if (f == NULL || *f == '\0') {
1057 /* guess a decent file name based on the protocol used */
1058 if (!strncmp(url, "gemini://", 9))
1059 f = "index.gmi";
1060 else
1061 f = "index.txt";
1064 strlcpy(path, download_path, sizeof(path));
1065 strlcat(path, f, sizeof(path));
1067 ui_read("Write file", write_buffer, current_tab, path);