From f260b1a3b19d545ec7e255af6945ee6e14316fc2 Mon Sep 17 00:00:00 2001 From: NRK Date: Sat, 4 Jan 2025 14:19:28 +0000 Subject: [PATCH] WIP: add support for custom trash command 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: https://github.com/jarun/nnn/issues/1168 Fixes: https://github.com/jarun/nnn/discussions/1963 Fixes: https://github.com/jarun/nnn/discussions/1960 Fixes: https://github.com/jarun/nnn/discussions/1761 --- plugins/.nmv | 4 +++- src/nnn.c | 19 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/plugins/.nmv b/plugins/.nmv index 67abe166c..ffa2021d4 100755 --- a/plugins/.nmv +++ b/plugins/.nmv @@ -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 diff --git a/src/nnn.c b/src/nnn.c index bffc8f0d6..61d603cca 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -392,7 +392,7 @@ 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, 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 */ @@ -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 @@ -2579,7 +2580,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; } @@ -2597,8 +2598,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 */ } @@ -9045,9 +9045,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};