Skip to content

Commit

Permalink
add support for custom trash command
Browse files Browse the repository at this point in the history
this makes it so that if $NNN_TRASH is set to a string other
than "1" or "2" then it is accepted as the trash command to run.

this allows us to support arbritary trashing utilities while
also maintaining backwards compatibility for older "1" & "2"
values.

Fixes: #1168
Fixes: #1963
Fixes: #1960
Fixes: #1761
  • Loading branch information
N-R-K committed Jan 4, 2025
1 parent 5522292 commit 066eb3a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
10 changes: 8 additions & 2 deletions nnn.1
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,14 @@ separated by \fI;\fR:
.Pp
\fBNNN_TRASH:\fR trash (instead of \fIrm -rf\fR) files to desktop Trash.
.Bd -literal
export NNN_TRASH=n
# n=1: trash-cli, n=2: gio trash
export NNN_TRASH=cmd

NOTES:
1. \fBcmd\fR is the name/path of the binary that nnn will call for
trashing files. E.g to use macOS's native `trash' command:
export NNN_TRASH="trash"
2. Special value "1" and "2" for cmd will use trash-cli and
gio trash respectively.
.Ed
.Pp
\fBNNN_SEL:\fR absolute path to custom selection file.
Expand Down
4 changes: 3 additions & 1 deletion plugins/.nmv
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ case "$NNN_TRASH" in
RM_UTIL="trash-put" ;;
2)
RM_UTIL="gio trash" ;;
*)
"")
RM_UTIL="rm -ri --" ;;
*)
RM_UTIL="$NNN_TRASH" ;;
esac

exit_status=0
Expand Down
32 changes: 13 additions & 19 deletions src/nnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,12 @@ typedef struct {
uint_t selbm : 1; /* Select a bookmark from bookmarks directory */
uint_t selmode : 1; /* Set when selecting files */
uint_t stayonsel : 1; /* Disable auto-advance on selection */
uint_t trash : 2; /* Trash method 0: rm -rf, 1: trash-cli, 2: gio trash */
uint_t trash : 1; /* 0: rm -rf, 1: trashcmd */
uint_t uidgid : 1; /* Show owner and group info */
uint_t usebsdtar : 1; /* Use bsdtar as default archive utility */
uint_t xprompt : 1; /* Use native prompt instead of readline prompt */
uint_t showlines : 1; /* Show line numbers */
uint_t reserved : 3; /* Adjust when adding/removing a field */
uint_t reserved : 4; /* Adjust when adding/removing a field */
} runstate;

/* Contexts or workspaces */
Expand Down Expand Up @@ -462,6 +462,7 @@ static char *listroot;
static char *plgpath;
static char *pnamebuf, *pselbuf, *findselpos;
static char *mark;
static char *trashcmd;
#ifndef NOX11
static char hostname[_POSIX_HOST_NAME_MAX + 1];
#endif
Expand Down Expand Up @@ -2527,17 +2528,6 @@ static char *xgetenv(const char * const name, char *fallback)
return value && value[0] ? value : fallback;
}

/* Checks if an env variable is set to 1 */
static inline uint_t xgetenv_val(const char *name)
{
char *str = getenv(name);

if (str && str[0])
return atoi(str);

return 0;
}

/* Check if a dir exists, IS a dir, and is readable */
static bool xdiraccess(const char *path)
{
Expand Down Expand Up @@ -2579,7 +2569,7 @@ static bool rmmulstr(char *buf, bool use_trash)
r, selpath);
else
snprintf(buf, CMD_LEN_MAX, "xargs -0 %s < %s",
utils[(g_state.trash == 1) ? UTIL_TRASH_CLI : UTIL_GIO_TRASH], selpath);
trashcmd, selpath);

return TRUE;
}
Expand All @@ -2597,8 +2587,7 @@ static bool xrm(char * const fpath, bool use_trash)
rm_opts[3] = r;
spawn("rm", rm_opts, "--", fpath, F_NORMAL | F_CHKRTN);
} else
spawn(utils[(g_state.trash == 1) ? UTIL_TRASH_CLI : UTIL_GIO_TRASH],
fpath, NULL, NULL, F_NORMAL | F_MULTI);
spawn(trashcmd, fpath, NULL, NULL, F_NORMAL | F_MULTI);

return (access(fpath, F_OK) == -1); /* File is removed */
}
Expand Down Expand Up @@ -9045,9 +9034,14 @@ int main(int argc, char *argv[])
#endif

/* Configure trash preference */
opt = xgetenv_val(env_cfg[NNN_TRASH]);
if (opt && opt <= 2)
g_state.trash = opt;
trashcmd = getenv(env_cfg[NNN_TRASH]);
if (trashcmd) {
g_state.trash = TRUE;
if (strcmp(trashcmd, "1") == 0)
trashcmd = utils[UTIL_TRASH_CLI];
else if (strcmp(trashcmd, "2") == 0)
trashcmd = utils[UTIL_GIO_TRASH];
}

/* Ignore/handle certain signals */
struct sigaction act = {.sa_handler = sigint_handler};
Expand Down

0 comments on commit 066eb3a

Please sign in to comment.