1 /* needed for some ncurses stuff */
2 #define _XOPEN_SOURCE_EXTENDED
3 #define _FILE_OFFSET_BITS 64
31 #define FM_SHELL "/bin/sh"
35 #define FM_PAGER "/usr/bin/less"
39 #define FM_ED "/bin/ed"
43 #define FM_OPENER "xdg-open"
48 struct option opts[] = {
49 {"help", no_argument, NULL, 'h'},
50 {"version", no_argument, NULL, 'v'},
54 static char clipboard[PATH_MAX];
57 #define BUFLEN PATH_MAX
58 static char BUF1[BUFLEN];
59 static char BUF2[BUFLEN];
60 static char INPUT[BUFLEN];
61 static wchar_t WBUF[BUFLEN];
63 /* Paths to external programs. */
64 static char *user_shell;
65 static char *user_pager;
66 static char *user_editor;
67 static char *user_open;
69 /* Listing view parameters. */
70 #define HEIGHT (LINES-4)
71 #define STATUSPOS (COLS-16)
73 /* Listing view flags. */
74 #define SHOW_FILES 0x01u
75 #define SHOW_DIRS 0x02u
76 #define SHOW_HIDDEN 0x04u
78 /* Marks parameters. */
80 #define BULK_THRESH 256
82 /* Information associated to each entry in listing. */
91 /* Dynamic array of marked entries. */
93 char dirpath[PATH_MAX];
99 /* Line editing state. */
101 wchar_t buffer[BUFLEN + 1];
105 /* Each tab only stores the following information. */
120 static struct state {
128 volatile sig_atomic_t pending_usr1;
129 volatile sig_atomic_t pending_winch;
134 /* Macros for accessing global state. */
135 #define ENAME(I) fm.rows[I].name
136 #define ESIZE(I) fm.rows[I].size
137 #define EMODE(I) fm.rows[I].mode
138 #define ISLINK(I) fm.rows[I].islink
139 #define MARKED(I) fm.rows[I].marked
140 #define SCROLL fm.tabs[fm.tab].scroll
141 #define ESEL fm.tabs[fm.tab].esel
142 #define FLAGS fm.tabs[fm.tab].flags
143 #define CWD fm.tabs[fm.tab].cwd
146 #define MIN(A, B) ((A) < (B) ? (A) : (B))
147 #define MAX(A, B) ((A) > (B) ? (A) : (B))
148 #define ISDIR(E) (strchr((E), '/') != NULL)
149 #define CTRL(x) ((x) & 0x1f)
150 #define nitems(a) (sizeof(a)/sizeof(a[0]))
152 /* Line Editing Macros. */
153 #define EDIT_FULL(E) ((E).left == (E).right)
154 #define EDIT_CAN_LEFT(E) ((E).left)
155 #define EDIT_CAN_RIGHT(E) ((E).right < BUFLEN-1)
156 #define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
157 #define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
158 #define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
159 #define EDIT_BACKSPACE(E) (E).left--
160 #define EDIT_DELETE(E) (E).right++
161 #define EDIT_CLEAR(E) do { (E).left = 0; (E).right = BUFLEN-1; } while(0)
163 enum editstate { CONTINUE, CONFIRM, CANCEL };
164 enum color { DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE, BLACK };
166 typedef int (*PROCESS)(const char *path);
169 #define __dead __attribute__((noreturn))
172 static inline __dead void
173 quit(const char *reason)
182 err(1, "%s", reason);
190 if ((d = malloc(size)) == NULL)
196 xcalloc(size_t nmemb, size_t size)
200 if ((d = calloc(nmemb, size)) == NULL)
206 xrealloc(void *p, size_t size)
210 if ((d = realloc(p, size)) == NULL)
216 init_marks(struct marks *marks)
218 strcpy(marks->dirpath, "");
219 marks->bulk = BULK_INIT;
221 marks->entries = xcalloc(marks->bulk, sizeof(*marks->entries));
224 /* Unmark all entries. */
226 mark_none(struct marks *marks)
230 strcpy(marks->dirpath, "");
231 for (i = 0; i < marks->bulk && marks->nentries; i++)
232 if (marks->entries[i]) {
233 free(marks->entries[i]);
234 marks->entries[i] = NULL;
237 if (marks->bulk > BULK_THRESH) {
238 /* Reset bulk to free some memory. */
239 free(marks->entries);
240 marks->bulk = BULK_INIT;
241 marks->entries = xcalloc(marks->bulk, sizeof(*marks->entries));
246 add_mark(struct marks *marks, char *dirpath, char *entry)
250 if (!strcmp(marks->dirpath, dirpath)) {
251 /* Append mark to directory. */
252 if (marks->nentries == marks->bulk) {
253 /* Expand bulk to accomodate new entry. */
254 int extra = marks->bulk / 2;
255 marks->bulk += extra; /* bulk *= 1.5; */
256 marks->entries = xrealloc(marks->entries,
257 marks->bulk * sizeof(*marks->entries));
258 memset(&marks->entries[marks->nentries], 0,
259 extra * sizeof(*marks->entries));
262 /* Search for empty slot (there must be one). */
263 for (i = 0; i < marks->bulk; i++)
264 if (!marks->entries[i])
268 /* Directory changed. Discard old marks. */
270 strcpy(marks->dirpath, dirpath);
273 marks->entries[i] = xmalloc(strlen(entry) + 1);
274 strcpy(marks->entries[i], entry);
279 del_mark(struct marks *marks, char *entry)
283 if (marks->nentries > 1) {
284 for (i = 0; i < marks->bulk; i++)
285 if (marks->entries[i] &&
286 !strcmp(marks->entries[i], entry))
288 free(marks->entries[i]);
289 marks->entries[i] = NULL;
296 free_marks(struct marks *marks)
300 for (i = 0; i < marks->bulk && marks->nentries; i++)
301 if (marks->entries[i]) {
302 free(marks->entries[i]);
305 free(marks->entries);
315 handle_winch(int sig)
317 fm.pending_winch = 1;
321 enable_handlers(void)
325 memset(&sa, 0, sizeof(struct sigaction));
326 sa.sa_handler = handle_usr1;
327 sigaction(SIGUSR1, &sa, NULL);
328 sa.sa_handler = handle_winch;
329 sigaction(SIGWINCH, &sa, NULL);
333 disable_handlers(void)
337 memset(&sa, 0, sizeof(struct sigaction));
338 sa.sa_handler = SIG_DFL;
339 sigaction(SIGUSR1, &sa, NULL);
340 sigaction(SIGWINCH, &sa, NULL);
343 static void reload(void);
344 static void update_view(void);
346 /* Handle any signals received since last call. */
350 if (fm.pending_usr1) {
351 /* SIGUSR1 received: refresh directory listing. */
355 if (fm.pending_winch) {
356 /* SIGWINCH received: resize application accordingly. */
361 fm.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
362 if (HEIGHT < fm.nfiles && SCROLL + HEIGHT > fm.nfiles)
363 SCROLL = ESEL - HEIGHT;
365 fm.pending_winch = 0;
370 * This function must be used in place of getch(). It handles signals
371 * while waiting for user input.
378 while ((ch = getch()) == ERR)
384 * This function must be used in place of get_wch(). It handles
385 * signals while waiting for user input.
388 fm_get_wch(wint_t *wch)
392 while ((ret = get_wch(wch)) == (wint_t)ERR)
397 /* Get user programs from the environment. */
399 #define FM_ENV(dst, src) if ((dst = getenv("FM_" #src)) == NULL) \
405 FM_ENV(user_shell, SHELL);
406 FM_ENV(user_pager, PAGER);
407 FM_ENV(user_editor, VISUAL);
409 FM_ENV(user_editor, EDITOR);
410 FM_ENV(user_open, OPEN);
413 /* Do a fork-exec to external program (e.g. $EDITOR). */
415 spawn(const char *argv0, ...)
420 const char *argv[16], *last;
423 memset(argv, 0, sizeof(argv));
427 for (i = 1; i < nitems(argv); ++i) {
428 last = va_arg(ap, const char *);
441 switch (pid = fork()) {
445 setenv("RVSEL", fm.nfiles ? ENAME(ESEL) : "", 1);
446 execvp(argv[0], (char *const *)argv);
449 waitpid(pid, &status, 0);
451 kill(getpid(), SIGWINCH);
456 shell_escaped_cat(char *buf, char *str, size_t n)
458 char *p = buf + strlen(buf);
460 for (n--; n; n--, str++) {
481 open_with_env(char *program, char *path)
485 strncpy(BUF1, program, BUFLEN - 1);
486 strncat(BUF1, " ", BUFLEN - strlen(program) - 1);
487 shell_escaped_cat(BUF1, path, BUFLEN - strlen(program) - 2);
488 spawn(RV_SHELL, "-c", BUF1, NULL );
490 spawn(program, path, NULL);
501 setlocale(LC_ALL, "");
504 timeout(100); /* For getch(). */
506 nonl(); /* No NL->CR/NL on output. */
507 intrflush(stdscr, FALSE);
508 keypad(stdscr, TRUE);
509 curs_set(FALSE); /* Hide blinking cursor. */
513 #ifdef NCURSES_EXT_FUNCS
514 use_default_colors();
519 init_pair(RED, COLOR_RED, bg);
520 init_pair(GREEN, COLOR_GREEN, bg);
521 init_pair(YELLOW, COLOR_YELLOW, bg);
522 init_pair(BLUE, COLOR_BLUE, bg);
523 init_pair(CYAN, COLOR_CYAN, bg);
524 init_pair(MAGENTA, COLOR_MAGENTA, bg);
525 init_pair(WHITE, COLOR_WHITE, bg);
526 init_pair(BLACK, COLOR_BLACK, bg);
528 atexit((void (*)(void))endwin);
532 /* Update the listing view. */
541 mvhline(0, 0, ' ', COLS);
542 attr_on(A_BOLD, NULL);
543 color_set(RVC_TABNUM, NULL);
544 mvaddch(0, COLS - 2, fm.tab + '0');
545 attr_off(A_BOLD, NULL);
546 if (fm.marks.nentries) {
547 numsize = snprintf(BUF1, BUFLEN, "%d", fm.marks.nentries);
548 color_set(RVC_MARKS, NULL);
549 mvaddstr(0, COLS - 3 - numsize, BUF1);
552 color_set(RVC_CWD, NULL);
553 mbstowcs(WBUF, CWD, PATH_MAX);
554 mvaddnwstr(0, 0, WBUF, COLS - 4 - numsize);
555 wcolor_set(fm.window, RVC_BORDER, NULL);
556 wborder(fm.window, 0, 0, 0, 0, 0, 0, 0, 0);
557 ESEL = MAX(MIN(ESEL, fm.nfiles - 1), 0);
560 * Selection might not be visible, due to cursor wrapping or
561 * window shrinking. In that case, the scroll must be moved to
564 if (fm.nfiles > HEIGHT) {
565 SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
566 SCROLL = MIN(MAX(SCROLL, 0), fm.nfiles - HEIGHT);
569 marking = !strcmp(CWD, fm.marks.dirpath);
570 for (i = 0, j = SCROLL; i < HEIGHT && j < fm.nfiles; i++, j++) {
571 ishidden = ENAME(j)[0] == '.';
573 wattr_on(fm.window, A_REVERSE, NULL);
575 wcolor_set(fm.window, RVC_LINK, NULL);
577 wcolor_set(fm.window, RVC_HIDDEN, NULL);
578 else if (S_ISREG(EMODE(j))) {
579 if (EMODE(j) & (S_IXUSR | S_IXGRP | S_IXOTH))
580 wcolor_set(fm.window, RVC_EXEC, NULL);
582 wcolor_set(fm.window, RVC_REG, NULL);
583 } else if (S_ISDIR(EMODE(j)))
584 wcolor_set(fm.window, RVC_DIR, NULL);
585 else if (S_ISCHR(EMODE(j)))
586 wcolor_set(fm.window, RVC_CHR, NULL);
587 else if (S_ISBLK(EMODE(j)))
588 wcolor_set(fm.window, RVC_BLK, NULL);
589 else if (S_ISFIFO(EMODE(j)))
590 wcolor_set(fm.window, RVC_FIFO, NULL);
591 else if (S_ISSOCK(EMODE(j)))
592 wcolor_set(fm.window, RVC_SOCK, NULL);
593 if (S_ISDIR(EMODE(j))) {
594 mbstowcs(WBUF, ENAME(j), PATH_MAX);
598 const char *suffix, *suffixes = "BKMGTPEZY";
599 off_t human_size = ESIZE(j) * 10;
600 int length = mbstowcs(WBUF, ENAME(j), PATH_MAX);
601 int namecols = wcswidth(WBUF, length);
602 for (suffix = suffixes; human_size >= 10240; suffix++)
603 human_size = (human_size + 512) / 1024;
605 swprintf(WBUF + length, PATH_MAX - length,
607 (int)(COLS - namecols - 6),
608 (int)human_size / 10, *suffix);
610 swprintf(WBUF + length, PATH_MAX - length,
612 (int)(COLS - namecols - 8),
613 (int)human_size / 10,
614 (int)human_size % 10, *suffix);
616 mvwhline(fm.window, i + 1, 1, ' ', COLS - 2);
617 mvwaddnwstr(fm.window, i + 1, 2, WBUF, COLS - 4);
618 if (marking && MARKED(j)) {
619 wcolor_set(fm.window, RVC_MARKS, NULL);
620 mvwaddch(fm.window, i + 1, 1, RVS_MARK);
622 mvwaddch(fm.window, i + 1, 1, ' ');
624 wattr_off(fm.window, A_REVERSE, NULL);
626 for (; i < HEIGHT; i++)
627 mvwhline(fm.window, i + 1, 1, ' ', COLS - 2);
628 if (fm.nfiles > HEIGHT) {
630 center = (SCROLL + HEIGHT / 2) * HEIGHT / fm.nfiles;
631 height = (HEIGHT - 1) * HEIGHT / fm.nfiles;
634 wcolor_set(fm.window, RVC_SCROLLBAR, NULL);
635 mvwvline(fm.window, center - height/2 + 1, COLS - 1,
636 RVS_SCROLLBAR, height);
638 BUF1[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
639 BUF1[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
640 BUF1[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
644 snprintf(BUF2, BUFLEN, "%d/%d", ESEL + 1, fm.nfiles);
645 snprintf(BUF1 + 3, BUFLEN - 3, "%12s", BUF2);
646 color_set(RVC_STATUS, NULL);
647 mvaddstr(LINES - 1, STATUSPOS, BUF1);
651 /* Show a message on the status bar. */
652 static void __attribute__((format(printf, 2, 3)))
653 message(enum color c, const char *fmt, ...)
659 vsnprintf(BUF1, MIN(BUFLEN, STATUSPOS), fmt, args);
662 pos = (STATUSPOS - len) / 2;
663 attr_on(A_BOLD, NULL);
665 mvaddstr(LINES - 1, pos, BUF1);
666 color_set(DEFAULT, NULL);
667 attr_off(A_BOLD, NULL);
670 /* Clear message area, leaving only status info. */
674 mvhline(LINES - 1, 0, ' ', STATUSPOS);
677 /* Comparison used to sort listing entries. */
679 rowcmp(const void *a, const void *b)
681 int isdir1, isdir2, cmpdir;
682 const struct row *r1 = a;
683 const struct row *r2 = b;
684 isdir1 = S_ISDIR(r1->mode);
685 isdir2 = S_ISDIR(r2->mode);
686 cmpdir = isdir2 - isdir1;
687 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
690 /* Get all entries in current working directory. */
692 ls(struct row **rowsp, uint8_t flags)
700 if (!(dp = opendir(".")))
702 n = -2; /* We don't want the entries "." and "..". */
710 rows = xmalloc(n * sizeof(*rows));
712 while ((ep = readdir(dp))) {
713 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
715 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
717 lstat(ep->d_name, &statbuf);
718 rows[i].islink = S_ISLNK(statbuf.st_mode);
719 stat(ep->d_name, &statbuf);
720 if (S_ISDIR(statbuf.st_mode)) {
721 if (flags & SHOW_DIRS) {
722 rows[i].name = xmalloc(strlen(ep->d_name) + 2);
723 strcpy(rows[i].name, ep->d_name);
725 strcat(rows[i].name, "/");
726 rows[i].mode = statbuf.st_mode;
729 } else if (flags & SHOW_FILES) {
730 rows[i].name = xmalloc(strlen(ep->d_name) + 1);
731 strcpy(rows[i].name, ep->d_name);
732 rows[i].size = statbuf.st_size;
733 rows[i].mode = statbuf.st_mode;
737 n = i; /* Ignore unused space in array caused by filters. */
738 qsort(rows, n, sizeof(*rows), rowcmp);
745 free_rows(struct row **rowsp, int nfiles)
749 for (i = 0; i < nfiles; i++)
750 free((*rowsp)[i].name);
755 /* Change working directory to the path in CWD. */
761 message(CYAN, "Loading \"%s\"...", CWD);
763 if (chdir(CWD) == -1) {
764 getcwd(CWD, PATH_MAX - 1);
765 if (CWD[strlen(CWD) - 1] != '/')
772 free_rows(&fm.rows, fm.nfiles);
773 fm.nfiles = ls(&fm.rows, FLAGS);
774 if (!strcmp(CWD, fm.marks.dirpath)) {
775 for (i = 0; i < fm.nfiles; i++) {
776 for (j = 0; j < fm.marks.bulk; j++)
777 if (fm.marks.entries[j] &&
778 !strcmp(fm.marks.entries[j], ENAME(i)))
780 MARKED(i) = j < fm.marks.bulk;
783 for (i = 0; i < fm.nfiles; i++)
790 /* Select a target entry, if it is present. */
792 try_to_sel(const char *target)
796 while ((ESEL + 1) < fm.nfiles && S_ISDIR(EMODE(ESEL)))
798 while ((ESEL + 1) < fm.nfiles && strcoll(ENAME(ESEL), target) < 0)
802 /* Reload CWD, but try to keep selection. */
807 strcpy(INPUT, ENAME(ESEL));
816 count_dir(const char *path)
821 char subpath[PATH_MAX];
824 if (!(dp = opendir(path)))
827 while ((ep = readdir(dp))) {
828 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
830 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
831 lstat(subpath, &statbuf);
832 if (S_ISDIR(statbuf.st_mode)) {
833 strcat(subpath, "/");
834 total += count_dir(subpath);
836 total += statbuf.st_size;
851 chdir(fm.marks.dirpath);
852 for (i = 0; i < fm.marks.bulk; i++) {
853 entry = fm.marks.entries[i];
856 total += count_dir(entry);
858 lstat(entry, &statbuf);
859 total += statbuf.st_size;
868 * Recursively process a source directory using CWD as destination
869 * root. For each node (i.e. directory), do the following:
871 * 1. call pre(destination);
872 * 2. call proc() on every child leaf (i.e. files);
873 * 3. recurse into every child node;
874 * 4. call pos(source).
876 * E.g. to move directory /src/ (and all its contents) inside /dst/:
877 * strcpy(CWD, "/dst/");
878 * process_dir(adddir, movfile, deldir, "/src/");
881 process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
887 char subpath[PATH_MAX];
891 char dstpath[PATH_MAX];
892 strcpy(dstpath, CWD);
893 strcat(dstpath, path + strlen(fm . marks . dirpath));
896 if (!(dp = opendir(path)))
898 while ((ep = readdir(dp))) {
899 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
901 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
902 lstat(subpath, &statbuf);
903 if (S_ISDIR(statbuf.st_mode)) {
904 strcat(subpath, "/");
905 ret |= process_dir(pre, proc, pos, subpath);
907 ret |= proc(subpath);
916 * Process all marked entries using CWD as destination root. All
917 * marked entries that are directories will be recursively processed.
918 * See process_dir() for details on the parameters.
921 process_marked(PROCESS pre, PROCESS proc, PROCESS pos, const char *msg_doing,
922 const char *msg_done)
929 message(CYAN, "%s...", msg_doing);
931 fm.prog = (struct prog){0, count_marked(), msg_doing};
932 for (i = 0; i < fm.marks.bulk; i++) {
933 entry = fm.marks.entries[i];
936 snprintf(path, PATH_MAX, "%s%s", fm.marks.dirpath,
939 if (!strncmp(path, CWD, strlen(path)))
942 ret = process_dir(pre, proc, pos, path);
946 del_mark(&fm.marks, entry);
953 if (!fm.marks.nentries)
954 message(GREEN, "%s all marked entries.", msg_done);
956 message(RED, "Some errors occured while %s.", msg_doing);
961 update_progress(off_t delta)
967 fm.prog.partial += delta;
968 percent = (int)(fm.prog.partial * 100 / fm.prog.total);
969 message(CYAN, "%s...%d%%", fm.prog.msg, percent);
973 /* Wrappers for file operations. */
975 delfile(const char *path)
980 ret = lstat(path, &st);
983 update_progress(st.st_size);
988 addfile(const char *path)
990 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
993 ret = creat(path, 0644);
1000 cpyfile(const char *srcpath)
1006 char dstpath[PATH_MAX];
1008 strcpy(dstpath, CWD);
1009 strcat(dstpath, srcpath + strlen(fm.marks.dirpath));
1010 ret = lstat(srcpath, &st);
1013 if (S_ISLNK(st.st_mode)) {
1014 ret = readlink(srcpath, BUF1, BUFLEN - 1);
1018 ret = symlink(BUF1, dstpath);
1020 ret = src = open(srcpath, O_RDONLY);
1023 ret = dst = creat(dstpath, st.st_mode);
1026 while ((size = read(src, buf, BUFSIZ)) > 0) {
1027 write(dst, buf, size);
1028 update_progress(size);
1039 adddir(const char *path)
1044 ret = stat(CWD, &st);
1047 return mkdir(path, st.st_mode);
1051 movfile(const char *srcpath)
1055 char dstpath[PATH_MAX];
1057 strcpy(dstpath, CWD);
1058 strcat(dstpath, srcpath + strlen(fm.marks.dirpath));
1059 ret = rename(srcpath, dstpath);
1061 ret = lstat(dstpath, &st);
1064 update_progress(st.st_size);
1065 } else if (errno == EXDEV) {
1066 ret = cpyfile(srcpath);
1069 ret = unlink(srcpath);
1075 start_line_edit(const char *init_input)
1078 strncpy(INPUT, init_input, BUFLEN);
1079 fm.edit.left = mbstowcs(fm.edit.buffer, init_input, BUFLEN);
1080 fm.edit.right = BUFLEN - 1;
1081 fm.edit.buffer[BUFLEN] = L'\0';
1085 /* Read input and change editing state accordingly. */
1086 static enum editstate
1089 wchar_t eraser, killer, wch;
1092 ret = fm_get_wch((wint_t *)&wch);
1093 erasewchar(&eraser);
1095 if (ret == KEY_CODE_YES) {
1096 if (wch == KEY_ENTER) {
1099 } else if (wch == KEY_LEFT) {
1100 if (EDIT_CAN_LEFT(fm.edit))
1102 } else if (wch == KEY_RIGHT) {
1103 if (EDIT_CAN_RIGHT(fm.edit))
1104 EDIT_RIGHT(fm.edit);
1105 } else if (wch == KEY_UP) {
1106 while (EDIT_CAN_LEFT(fm.edit))
1108 } else if (wch == KEY_DOWN) {
1109 while (EDIT_CAN_RIGHT(fm.edit))
1110 EDIT_RIGHT(fm.edit);
1111 } else if (wch == KEY_BACKSPACE) {
1112 if (EDIT_CAN_LEFT(fm.edit))
1113 EDIT_BACKSPACE(fm.edit);
1114 } else if (wch == KEY_DC) {
1115 if (EDIT_CAN_RIGHT(fm.edit))
1116 EDIT_DELETE(fm.edit);
1119 if (wch == L'\r' || wch == L'\n') {
1122 } else if (wch == L'\t') {
1125 } else if (wch == eraser) {
1126 if (EDIT_CAN_LEFT(fm.edit))
1127 EDIT_BACKSPACE(fm.edit);
1128 } else if (wch == killer) {
1129 EDIT_CLEAR(fm.edit);
1131 } else if (iswprint(wch)) {
1132 if (!EDIT_FULL(fm.edit))
1133 EDIT_INSERT(fm.edit, wch);
1136 /* Encode edit contents in INPUT. */
1137 fm.edit.buffer[fm.edit.left] = L'\0';
1138 length = wcstombs(INPUT, fm.edit.buffer, BUFLEN);
1139 wcstombs(&INPUT[length], &fm.edit.buffer[fm.edit.right + 1],
1144 /* Update line input on the screen. */
1146 update_input(const char *prompt, enum color c)
1148 int plen, ilen, maxlen;
1150 plen = strlen(prompt);
1151 ilen = mbstowcs(NULL, INPUT, 0);
1152 maxlen = STATUSPOS - plen - 2;
1153 if (ilen - fm.edit_scroll < maxlen)
1154 fm.edit_scroll = MAX(ilen - maxlen, 0);
1155 else if (fm.edit.left > fm.edit_scroll + maxlen - 1)
1156 fm.edit_scroll = fm.edit.left - maxlen;
1157 else if (fm.edit.left < fm.edit_scroll)
1158 fm.edit_scroll = MAX(fm.edit.left - maxlen, 0);
1159 color_set(RVC_PROMPT, NULL);
1160 mvaddstr(LINES - 1, 0, prompt);
1162 mbstowcs(WBUF, INPUT, COLS);
1163 mvaddnwstr(LINES - 1, plen, &WBUF[fm.edit_scroll], maxlen);
1164 mvaddch(LINES - 1, plen + MIN(ilen - fm.edit_scroll, maxlen + 1),
1166 color_set(DEFAULT, NULL);
1168 mvaddch(LINES - 1, plen - 1, '<');
1169 if (ilen > fm.edit_scroll + maxlen)
1170 mvaddch(LINES - 1, plen + maxlen, '>');
1171 move(LINES - 1, plen + fm.edit.left - fm.edit_scroll);
1178 ESEL = MIN(ESEL + 1, fm.nfiles - 1);
1185 ESEL = MAX(ESEL - 1, 0);
1189 cmd_scroll_down(void)
1193 ESEL = MIN(ESEL + HEIGHT, fm.nfiles - 1);
1194 if (fm.nfiles > HEIGHT)
1195 SCROLL = MIN(SCROLL + HEIGHT, fm.nfiles - HEIGHT);
1203 ESEL = MAX(ESEL - HEIGHT, 0);
1204 SCROLL = MAX(SCROLL - HEIGHT, 0);
1210 spawn("man", "fm", NULL);
1221 cmd_jump_bottom(void)
1224 ESEL = fm.nfiles - 1;
1230 if (!fm.nfiles || !S_ISDIR(EMODE(ESEL)))
1232 if (chdir(ENAME(ESEL)) == -1) {
1233 message(RED, "cd: %s: %s", ENAME(ESEL), strerror(errno));
1236 strlcat(CWD, ENAME(ESEL), sizeof(CWD));
1243 char *dirname, first;
1245 if (!strcmp(CWD, "/"))
1248 /* dirname(3) basically */
1249 dirname = strrchr(CWD, '/');
1251 dirname = strrchr(CWD, '/') + 1;
1257 strlcat(dirname, "/", sizeof(dirname));
1258 try_to_sel(dirname);
1260 if (fm.nfiles > HEIGHT)
1261 SCROLL = ESEL - HEIGHT / 2;
1269 if ((home = getenv("HOME")) == NULL) {
1270 message(RED, "HOME is not defined!");
1274 strlcpy(CWD, home, sizeof(CWD));
1275 if (*CWD != '\0' && CWD[strlen(CWD) - 1] != '/')
1276 strlcat(CWD, "/", sizeof(CWD));
1286 if ((path = getenv("CLIP")) == NULL ||
1287 (f = fopen(path, "w")) == NULL) {
1288 /* use internal clipboard */
1289 strlcpy(clipboard, CWD, sizeof(clipboard));
1290 strlcat(clipboard, ENAME(ESEL), sizeof(clipboard));
1294 fprintf(f, "%s%s", CWD, ENAME(ESEL));
1300 cmd_paste_path(void)
1308 if ((path = getenv("CLIP")) != NULL &&
1309 (f = fopen(path, "w")) != NULL) {
1310 r = fread(clipboard, 1, sizeof(clipboard)-1, f);
1312 if (r == -1 || r == 0)
1314 if ((nl = memmem(clipboard, r, "", 1)) == NULL)
1318 strlcpy(p, clipboard, sizeof(p));
1319 strlcpy(CWD, clipboard, sizeof(CWD));
1320 if ((nl = strrchr(CWD, '/')) != NULL) {
1322 nl = strrchr(p, '/');
1325 if (strcmp(CWD, "/") != 0)
1326 strlcat(CWD, "/", sizeof(CWD));
1332 memset(clipboard, 0, sizeof(clipboard));
1346 if ((shell = getenv("SHELL")) == NULL)
1356 if (!fm.nfiles || S_ISDIR(EMODE(ESEL)))
1359 if ((pager = getenv("PAGER")) == NULL)
1361 spawn(pager, ENAME(ESEL), NULL);
1369 if (!fm.nfiles || S_ISDIR(EMODE(ESEL)))
1372 if ((editor = getenv("VISUAL")) == NULL ||
1373 (editor = getenv("EDITOR")) == NULL)
1376 spawn(editor, ENAME(ESEL), NULL);
1384 if (!fm.nfiles || S_ISDIR(EMODE(ESEL)))
1387 if ((opener = getenv("OPENER")) == NULL)
1390 spawn(opener, ENAME(ESEL), NULL);
1397 del_mark(&fm.marks, ENAME(ESEL));
1399 add_mark(&fm.marks, CWD, ENAME(ESEL));
1401 MARKED(ESEL) = !MARKED(ESEL);
1402 ESEL = (ESEL + 1) % fm.nfiles;
1406 cmd_toggle_mark(void)
1410 for (i = 0; i < fm.nfiles; ++i) {
1412 del_mark(&fm.marks, ENAME(i));
1414 add_mark(&fm.marks, CWD, ENAME(ESEL));
1415 MARKED(i) = !MARKED(i);
1424 for (i = 0; i < fm.nfiles; ++i)
1426 add_mark(&fm.marks, CWD, ENAME(ESEL));
1445 {'<', K_META, cmd_jump_top, X_UPDV},
1446 {'>', K_META, cmd_jump_bottom, X_UPDV},
1447 {'?', 0, cmd_man, 0},
1448 {'G', 0, cmd_jump_bottom, X_UPDV},
1449 {'H', 0, cmd_home, X_UPDV},
1450 {'J', 0, cmd_scroll_down, X_UPDV},
1451 {'K', 0, cmd_scroll_up, X_UPDV},
1452 {'M', 0, cmd_mark_all, X_UPDV},
1453 {'P', 0, cmd_paste_path, X_UPDV},
1454 {'V', K_CTRL, cmd_scroll_down, X_UPDV},
1455 {'Y', 0, cmd_copy_path, X_UPDV},
1456 {'^', 0, cmd_cd_up, X_UPDV},
1457 {'b', 0, cmd_cd_up, X_UPDV},
1458 {'e', 0, cmd_edit, X_UPDV},
1459 {'f', 0, cmd_cd_down, X_UPDV},
1460 {'g', 0, cmd_jump_top, X_UPDV},
1461 {'g', K_CTRL, NULL, X_UPDV},
1462 {'h', 0, cmd_cd_up, X_UPDV},
1463 {'j', 0, cmd_down, X_UPDV},
1464 {'k', 0, cmd_up, X_UPDV},
1465 {'l', 0, cmd_cd_down, X_UPDV},
1466 {'l', K_CTRL, cmd_reload, X_UPDV},
1467 {'n', 0, cmd_down, X_UPDV},
1468 {'n', K_CTRL, cmd_down, X_UPDV},
1469 {'m', 0, cmd_mark, X_UPDV},
1470 {'m', K_CTRL, cmd_shell, X_UPDV},
1471 {'o', 0, cmd_open, X_UPDV},
1472 {'p', 0, cmd_up, X_UPDV},
1473 {'p', K_CTRL, cmd_up, X_UPDV},
1474 {'q', 0, NULL, X_QUIT},
1475 {'t', 0, cmd_toggle_mark, X_UPDV},
1476 {'v', 0, cmd_view, X_UPDV},
1477 {'v', K_META, cmd_scroll_up, X_UPDV},
1478 {KEY_DOWN, 0, cmd_scroll_down, X_UPDV},
1479 {KEY_NPAGE, 0, cmd_scroll_down, X_UPDV},
1480 {KEY_PPAGE, 0, cmd_scroll_up, X_UPDV},
1481 {KEY_RESIZE, 0, NULL, X_UPDV},
1482 {KEY_RESIZE, K_META, NULL, X_UPDV},
1483 {KEY_UP, 0, cmd_scroll_up, X_UPDV},
1493 if ((ch = fm_getch()) == '\e') {
1501 for (i = 0; i < nitems(bindings); ++i) {
1504 if (b->chflags & K_CTRL)
1506 if ((!meta && b->chflags & K_META) || ch != c)
1509 if (b->flags & X_QUIT)
1513 if (b->flags & X_UPDV)
1519 message(RED, "%s%s is undefined",
1520 meta ? "M-": "", keyname(ch));
1528 fprintf(stderr, "usage: %s [-hv] [-d file] [-m file] [dirs ...]\n",
1530 fprintf(stderr, "version: %s\n", RV_VERSION);
1535 main(int argc, char *argv[])
1540 FILE *save_cwd_file = NULL;
1541 FILE *save_marks_file = NULL;
1543 while ((ch = getopt_long(argc, argv, "d:hm:v", opts, NULL)) != -1) {
1546 if ((save_cwd_file = fopen(optarg, "w")) == NULL)
1547 err(1, "open %s", optarg);
1553 if ((save_marks_file = fopen(optarg, "a")) == NULL)
1554 err(1, "open %s", optarg);
1557 printf("version: fm %s\n", RV_VERSION);
1564 if (pledge("stdio rpath wpath cpath tty proc exec", NULL) == -1)
1567 get_user_programs();
1570 for (i = 0; i < 10; i++) {
1571 fm.tabs[i].esel = fm.tabs[i].scroll = 0;
1572 fm.tabs[i].flags = RV_FLAGS;
1574 strcpy(fm.tabs[0].cwd, getenv("HOME"));
1575 for (i = 1; i < argc && i < 10; i++) {
1576 if ((d = opendir(argv[i]))) {
1577 realpath(argv[i], fm.tabs[i].cwd);
1580 strcpy(fm.tabs[i].cwd, fm.tabs[0].cwd);
1582 getcwd(fm.tabs[i].cwd, PATH_MAX);
1583 for (i++; i < 10; i++)
1584 strcpy(fm.tabs[i].cwd, fm.tabs[i - 1].cwd);
1585 for (i = 0; i < 10; i++)
1586 if (fm.tabs[i].cwd[strlen(fm.tabs[i].cwd) - 1] != '/')
1587 strcat(fm.tabs[i].cwd, "/");
1589 fm.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
1590 init_marks(&fm.marks);
1592 strlcpy(clipboard, CWD, sizeof(clipboard));
1594 strlcat(clipboard, ENAME(ESEL), sizeof(clipboard));
1599 free_rows(&fm.rows, fm.nfiles);
1601 if (save_cwd_file != NULL) {
1602 fputs(CWD, save_cwd_file);
1603 fclose(save_cwd_file);
1605 if (save_marks_file != NULL) {
1606 for (i = 0; i < fm.marks.bulk; i++) {
1607 entry = fm.marks.entries[i];
1609 fprintf(save_marks_file, "%s%s\n",
1610 fm.marks.dirpath, entry);
1612 fclose(save_marks_file);
1614 free_marks(&fm.marks);