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 #include <curses.h>
293 #include <unistd.h>
294 #include <sys/wait.h>
295 #include <errno.h>
296 static void
297 do_exec_command(const char *cmd, struct tab *t)
299 int s;
300 pid_t p;
302 if (cmd == NULL)
303 return;
305 endwin();
307 switch (p = fork()) {
308 case -1:
309 message("failed to fork: %s", strerror(errno));
310 return;
311 case 0:
312 execl("/bin/sh", "sh", "-c", cmd, NULL);
313 warn("exec \"%s\" failed", cmd);
314 _exit(1);
317 again:
318 if (waitpid(p, &s, 0) == -1) {
319 if (errno == EINTR)
320 goto again;
323 refresh();
324 clear();
325 ui_schedule_redraw();
326 /* rearrange_windows(); */
329 void
330 cmd_push_button(struct buffer *buffer)
332 struct vline *vl;
333 struct line *l;
335 vl = buffer->current_line;
337 if (vl == NULL)
338 return;
340 switch (vl->parent->type) {
341 case LINE_LINK:
342 load_url_in_tab(current_tab, vl->parent->alt, NULL,
343 LU_MODE_NOCACHE);
344 break;
345 case LINE_PRE_START:
346 l = TAILQ_NEXT(vl->parent, lines);
347 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
348 if (l->type == LINE_PRE_END)
349 break;
350 l->flags ^= L_HIDDEN;
351 if (l->flags & L_HIDDEN)
352 buffer->line_max--;
353 else
354 buffer->line_max++;
356 break;
357 case LINE_DOWNLOAD:
358 case LINE_DOWNLOAD_DONE:
359 minibuffer_read("Execute: ", do_exec_command,
360 NULL);
361 snprintf(ministate.buf, sizeof(ministate.buf),
362 "xdg-open %s", vl->parent->alt);
363 break;
364 default:
365 break;
369 void
370 cmd_push_button_new_tab(struct buffer *buffer)
372 struct vline *vl;
374 vl = buffer->current_line;
375 if (vl == NULL || vl->parent->type != LINE_LINK)
376 return;
378 new_tab(vl->parent->alt, current_tab->hist_cur->h, current_tab);
381 void
382 cmd_previous_button(struct buffer *buffer)
384 struct excursion place;
386 save_excursion(&place, buffer);
388 do {
389 if (!forward_line(buffer, -1)) {
390 restore_excursion(&place, buffer);
391 message("No previous link");
392 return;
394 } while (buffer->current_line->parent->type != LINE_LINK);
397 void
398 cmd_next_button(struct buffer *buffer)
400 struct excursion place;
402 save_excursion(&place, buffer);
404 do {
405 if (!forward_line(buffer, +1)){
406 restore_excursion(&place, buffer);
407 message("No next link");
408 return;
410 } while (buffer->current_line->parent->type != LINE_LINK);
413 static inline int
414 is_heading(const struct line *l)
416 return l->type == LINE_TITLE_1 ||
417 l->type == LINE_TITLE_2 ||
418 l->type == LINE_TITLE_3;
421 void
422 cmd_previous_heading(struct buffer *buffer)
424 struct excursion place;
426 save_excursion(&place, buffer);
428 do {
429 if (!forward_line(buffer, -1)) {
430 restore_excursion(&place, buffer);
431 message("No previous heading");
432 return;
434 } while (!is_heading(buffer->current_line->parent));
437 void
438 cmd_next_heading(struct buffer *buffer)
440 struct excursion place;
442 save_excursion(&place, buffer);
444 do {
445 if (!forward_line(buffer, +1)) {
446 restore_excursion(&place, buffer);
447 message("No next heading");
448 return;
450 } while (!is_heading(buffer->current_line->parent));
453 void
454 cmd_previous_page(struct buffer *buffer)
456 if (!load_previous_page(current_tab))
457 message("No previous page");
460 void
461 cmd_next_page(struct buffer *buffer)
463 if (!load_next_page(current_tab))
464 message("No next page");
467 void
468 cmd_clear_minibuf(struct buffer *buffer)
470 message(NULL);
473 void
474 cmd_execute_extended_command(struct buffer *buffer)
476 size_t len;
478 GUARD_RECURSIVE_MINIBUFFER();
480 enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,
481 &eecmd_history, compl_eecmd, NULL, 1);
483 len = sizeof(ministate.prompt);
484 strlcpy(ministate.prompt, "", len);
486 if (thiskey.meta)
487 strlcat(ministate.prompt, "M-", len);
489 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
491 if (thiskey.meta)
492 strlcat(ministate.prompt, " ", len);
495 void
496 cmd_tab_close(struct buffer *buffer)
498 struct tab *tab, *t;
500 tab = current_tab;
502 if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
503 (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
504 switch_to_tab(t);
505 kill_tab(tab, 0);
506 } else
507 message("Can't close the only tab.");
511 void
512 cmd_tab_close_other(struct buffer *buffer)
514 struct tab *t, *i;
516 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
517 if (t == current_tab)
518 continue;
520 kill_tab(t, 0);
524 void
525 cmd_tab_undo_close(struct buffer *buffer)
527 struct tab *t;
529 if ((t = unkill_tab()) == NULL) {
530 message("No recently-closed tabs");
531 return;
534 switch_to_tab(t);
537 void
538 cmd_tab_new(struct buffer *buffer)
540 const char *url;
542 if ((url = new_tab_url) == NULL)
543 url = NEW_TAB_URL;
545 new_tab(url, NULL, NULL);
548 void
549 cmd_tab_next(struct buffer *buffer)
551 struct tab *t;
553 if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
554 t = TAILQ_FIRST(&tabshead);
555 switch_to_tab(t);
558 void
559 cmd_tab_previous(struct buffer *buffer)
561 struct tab *t;
563 if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
564 t = TAILQ_LAST(&tabshead, tabshead);
565 switch_to_tab(t);
568 void
569 cmd_tab_move(struct buffer *buffer)
571 struct tab *t;
573 t = TAILQ_NEXT(current_tab, tabs);
574 TAILQ_REMOVE(&tabshead, current_tab, tabs);
576 if (t == NULL)
577 TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
578 else
579 TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
582 void
583 cmd_tab_move_to(struct buffer *buffer)
585 struct tab *t;
587 t = TAILQ_PREV(current_tab, tabshead, tabs);
588 TAILQ_REMOVE(&tabshead, current_tab, tabs);
590 if (t == NULL)
591 TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
592 else
593 TAILQ_INSERT_BEFORE(t, current_tab, tabs);
596 void
597 cmd_tab_select(struct buffer *buffer)
599 GUARD_RECURSIVE_MINIBUFFER();
601 enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
602 NULL, compl_ts, NULL, 1);
603 strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
606 void
607 cmd_load_url(struct buffer *buffer)
609 GUARD_RECURSIVE_MINIBUFFER();
611 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
612 &lu_history, compl_lu, NULL, 0);
613 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
616 void
617 cmd_load_current_url(struct buffer *buffer)
619 GUARD_RECURSIVE_MINIBUFFER();
621 enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
622 &lu_history, compl_lu, NULL, 0);
623 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
624 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
625 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
628 void
629 cmd_reload_page(struct buffer *buffer)
631 load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL,
632 LU_MODE_NOHIST|LU_MODE_NOCACHE);
635 void
636 cmd_bookmark_page(struct buffer *buffer)
638 GUARD_RECURSIVE_MINIBUFFER();
640 enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
641 NULL, NULL, 0);
642 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
643 strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
644 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
647 void
648 cmd_list_bookmarks(struct buffer *buffer)
650 load_url_in_tab(current_tab, "about:bookmarks", NULL, LU_MODE_NONE);
653 void
654 cmd_toggle_help(struct buffer *buffer)
656 ui_toggle_side_window(SIDE_WINDOW_LEFT);
659 void
660 cmd_link_select(struct buffer *buffer)
662 struct line *l;
664 GUARD_RECURSIVE_MINIBUFFER();
666 l = TAILQ_FIRST(&buffer->page.head);
667 while (l != NULL && l->type != LINE_LINK)
668 l = TAILQ_NEXT(l, lines);
670 if (l == NULL) {
671 message("No links found");
672 return;
675 enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
676 NULL, compl_ls, l, 1);
677 strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
680 void
681 cmd_swiper(struct buffer *buffer)
683 GUARD_RECURSIVE_MINIBUFFER();
685 enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
686 NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head), 1);
687 strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
690 void
691 cmd_toc(struct buffer *buffer)
693 struct line *l;
695 GUARD_RECURSIVE_MINIBUFFER();
697 l = TAILQ_FIRST(&buffer->page.head);
698 while (l != NULL &&
699 l->type != LINE_TITLE_1 &&
700 l->type != LINE_TITLE_2 &&
701 l->type != LINE_TITLE_3)
702 l = TAILQ_NEXT(l, lines);
704 if (l == NULL) {
705 message("No headings found");
706 return;
709 enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
710 NULL, compl_toc, l, 1);
711 strlcpy(ministate.prompt, "Select heading: ",
712 sizeof(ministate.prompt));
715 void
716 cmd_inc_fill_column(struct buffer *buffer)
718 if (fill_column == INT_MAX)
719 return;
721 fill_column += 2;
722 message("fill-column: %d", fill_column);
724 ui_schedule_redraw();
727 void
728 cmd_dec_fill_column(struct buffer *buffer)
730 if (fill_column == INT_MAX || fill_column < 8)
731 return;
733 fill_column -= 2;
734 message("fill-column: %d", fill_column);
736 ui_schedule_redraw();
739 void
740 cmd_olivetti_mode(struct buffer *buffer)
742 olivetti_mode = !olivetti_mode;
743 if (olivetti_mode)
744 message("olivetti-mode enabled");
745 else
746 message("olivetti-mode disabled");
748 ui_schedule_redraw();
751 void
752 cmd_mini_delete_char(struct buffer *buffer)
754 char *c, *n;
756 GUARD_READ_ONLY();
758 minibuffer_taint_hist();
760 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
761 if (*c == '\0')
762 return;
763 n = utf8_next_cp(c);
765 memmove(c, n, strlen(n)+1);
767 recompute_completions(0);
770 void
771 cmd_mini_delete_backward_char(struct buffer *buffer)
773 char *c, *p, *start;
775 GUARD_READ_ONLY();
777 minibuffer_taint_hist();
779 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
780 start = buffer->current_line->line;
781 if (c == start)
782 return;
783 p = utf8_prev_cp(c-1, start);
785 memmove(p, c, strlen(c)+1);
786 buffer->cpoff--;
788 recompute_completions(0);
791 void
792 cmd_mini_kill_line(struct buffer *buffer)
794 char *c;
796 GUARD_READ_ONLY();
798 minibuffer_taint_hist();
799 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
800 *c = '\0';
802 recompute_completions(0);
805 void
806 cmd_mini_kill_whole_line(struct buffer *buffer)
808 GUARD_READ_ONLY();
810 minibuffer_taint_hist();
811 *buffer->current_line->line = '\0';
812 buffer->cpoff = 0;
815 void
816 cmd_mini_abort(struct buffer *buffer)
818 if (!in_minibuffer)
819 return;
821 ministate.abortfn();
824 void
825 cmd_mini_complete_and_exit(struct buffer *buffer)
827 struct vline *vl;
829 if (!in_minibuffer)
830 return;
832 if (ministate.compl.must_select && ministate.hist_cur == NULL) {
833 vl = ministate.compl.buffer.current_line;
834 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
835 vl->parent->type == LINE_COMPL) {
836 message("no match");
837 return;
841 minibuffer_taint_hist();
842 ministate.donefn();
845 void
846 cmd_mini_previous_history_element(struct buffer *buffer)
848 if (ministate.history == NULL) {
849 message("No history");
850 return;
853 if (ministate.hist_cur == NULL ||
854 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
855 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
856 ministate.hist_off = ministate.history->len - 1;
857 if (ministate.hist_cur == NULL)
858 message("No prev history item");
859 } else {
860 ministate.hist_off--;
863 if (ministate.hist_cur != NULL) {
864 buffer->current_line->line = ministate.hist_cur->h;
865 recompute_completions(0);
869 void
870 cmd_mini_next_history_element(struct buffer *buffer)
872 if (ministate.history == NULL) {
873 message("No history");
874 return;
877 if (ministate.hist_cur == NULL ||
878 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
879 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
880 ministate.hist_off = 0;
881 if (ministate.hist_cur == NULL)
882 message("No next history item");
883 } else {
884 ministate.hist_off++;
887 if (ministate.hist_cur != NULL) {
888 buffer->current_line->line = ministate.hist_cur->h;
889 recompute_completions(0);
893 void
894 cmd_previous_completion(struct buffer *buffer)
896 if (in_minibuffer != MB_COMPREAD)
897 return;
899 buffer = &ministate.compl.buffer;
900 if (buffer->current_line == NULL)
901 return;
903 buffer->current_line->parent->type = LINE_COMPL;
904 if (!forward_line(buffer, -1))
905 buffer->current_line->parent->type = LINE_COMPL;
906 else
907 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
910 void
911 cmd_next_completion(struct buffer *buffer)
913 if (in_minibuffer != MB_COMPREAD)
914 return;
916 buffer = &ministate.compl.buffer;
917 if (buffer->current_line == NULL)
918 return;
920 if (buffer->current_line->parent->type == LINE_COMPL_CURRENT) {
921 buffer->current_line->parent->type = LINE_COMPL;
922 forward_line(buffer, +1);
925 if (buffer->current_line != NULL)
926 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
929 void
930 cmd_insert_current_candidate(struct buffer *buffer)
932 if (in_minibuffer != MB_COMPREAD)
933 return;
935 minibuffer_insert_current_candidate();
938 void
939 cmd_suspend_telescope(struct buffer *buffer)
941 message("Zzz...");
942 ui_suspend();
945 void
946 cmd_toggle_pre_wrap(struct buffer *buffer)
948 dont_wrap_pre = !dont_wrap_pre;
950 if (dont_wrap_pre)
951 message("Don't wrap preformatted blocks");
952 else
953 message("Wrap preformatted blocks");
955 ui_schedule_redraw();
958 void
959 cmd_mini_goto_beginning(struct buffer *buffer)
961 struct vline *vl;
963 if (!in_minibuffer)
964 return;
966 buffer = &ministate.compl.buffer;
968 if ((vl = buffer->current_line) != NULL)
969 vl->parent->type = LINE_COMPL;
971 vl = TAILQ_FIRST(&buffer->head);
972 while (vl != NULL && vl->parent->flags & L_HIDDEN)
973 vl = TAILQ_NEXT(vl, vlines);
975 if (vl == NULL)
976 return;
978 vl->parent->type = LINE_COMPL_CURRENT;
979 buffer->top_line = vl;
980 buffer->current_line = vl;
983 void
984 cmd_mini_goto_end(struct buffer *buffer)
986 struct vline *vl;
988 if (!in_minibuffer)
989 return;
991 buffer = &ministate.compl.buffer;
993 if ((vl = buffer->current_line) != NULL)
994 vl->parent->type = LINE_COMPL;
996 vl = TAILQ_LAST(&buffer->head, vhead);
997 while (vl != NULL && vl->parent->flags & L_HIDDEN)
998 vl = TAILQ_PREV(vl, vhead, vlines);
1000 if (vl == NULL)
1001 return;
1003 vl->parent->type = LINE_COMPL_CURRENT;
1004 buffer->current_line = vl;
1007 void
1008 cmd_other_window(struct buffer *buffer)
1010 ui_other_window();
1013 void
1014 cmd_mini_scroll_up(struct buffer *buffer)
1016 if (!in_minibuffer)
1017 return;
1019 buffer = &ministate.compl.buffer;
1020 if (buffer->current_line == NULL)
1021 return;
1023 buffer->current_line->parent->type = LINE_COMPL;
1024 cmd_scroll_up(buffer);
1025 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
1028 void
1029 cmd_mini_scroll_down(struct buffer *buffer)
1031 if (!in_minibuffer)
1032 return;
1034 buffer = &ministate.compl.buffer;
1035 if (buffer->current_line == NULL)
1036 return;
1038 buffer->current_line->parent->type = LINE_COMPL;
1039 cmd_scroll_down(buffer);
1040 buffer->current_line->parent->type = LINE_COMPL_CURRENT;
1043 void
1044 cmd_toggle_downloads(struct buffer *buffer)
1046 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1049 void
1050 cmd_cache_info(struct buffer *buffer)
1052 size_t npages, tot;
1053 char fmt[FMT_SCALED_STRSIZE];
1055 mcache_info(&npages, &tot);
1057 if (fmt_scaled(tot, fmt) == 0)
1058 message("pages: %zu, total: %s", npages, fmt);
1059 else
1060 message("pages: %zu, total: %zu", npages, tot);
1063 void
1064 cmd_reply_last_input(struct buffer *buffer)
1066 GUARD_RECURSIVE_MINIBUFFER();
1068 if (current_tab->last_input_url == NULL) {
1069 message("there was no previous input request in this tab");
1070 return;
1073 if (!strncmp(current_tab->last_input_url, "gopher", 6)) {
1074 load_url_in_tab(current_tab, current_tab->last_input_url,
1075 NULL, LU_MODE_NOCACHE);
1076 return;
1079 message("%s", current_tab->last_input_url);
1080 ui_require_input(current_tab, 0, ir_select_reply);
1083 void
1084 cmd_write_buffer(struct buffer *buffer)
1086 const char *f, *url;
1087 char path[PATH_MAX];
1089 GUARD_RECURSIVE_MINIBUFFER();
1091 if (safe_mode) {
1092 message("Can't write buffer in safe-mode.");
1093 return;
1096 url = current_tab->hist_cur->h;
1098 if ((f = strrchr(url, '/')) != NULL)
1099 f++;
1100 if (f == NULL || *f == '\0') {
1101 /* guess a decent file name based on the protocol used */
1102 if (!strncmp(url, "gemini://", 9))
1103 f = "index.gmi";
1104 else
1105 f = "index.txt";
1108 strlcpy(path, download_path, sizeof(path));
1109 strlcat(path, f, sizeof(path));
1111 ui_read("Write file", write_buffer, current_tab, path);