misc/quitcd/quitcd.bash_zsh | 1 - misc/quitcd/quitcd.csh | 1 - misc/quitcd/quitcd.fish | 1 - nnn.1 | 5 +++++ src/nnn.c | 43 ++++++++++++++++++++++++------------------- diff --git a/misc/quitcd/quitcd.bash_zsh b/misc/quitcd/quitcd.bash_zsh index edeb2db943a2bfc1a09cf989bcfced04b7d61f12..40d94a60837877f6003ad0d3968d3f1b0ce9adc8 100644 --- a/misc/quitcd/quitcd.bash_zsh +++ b/misc/quitcd/quitcd.bash_zsh @@ -9,7 +9,6 @@ # The default behaviour is to cd on quit (nnn checks if NNN_TMPFILE is set) # To cd on quit only on ^G, remove the "export" as in: # NNN_TMPFILE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd" - # NOTE: NNN_TMPFILE is fixed, should not be modified export NNN_TMPFILE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd" # Unmask ^Q (, ^V etc.) (if required, see `stty -a`) to Quit nnn diff --git a/misc/quitcd/quitcd.csh b/misc/quitcd/quitcd.csh index 6e6730fe4ed89df8d220e8e44e65d98eae34c902..d0427430774fb9190a9c0d1de54cd0302979d257 100644 --- a/misc/quitcd/quitcd.csh +++ b/misc/quitcd/quitcd.csh @@ -2,7 +2,6 @@ # NOTE: set NNN_TMPFILE correctly if you use 'XDG_CONFIG_HOME' # The default behaviour is to cd on quit (nnn checks if NNN_TMPFILE is set) # To cd on quit only on ^G, export NNN_TMPFILE after the call to nnn -# NOTE: NNN_TMPFILE is fixed, should not be modified set NNN_TMPFILE=~/.config/nnn/.lastd # Unmask ^Q (, ^V etc.) (if required, see `stty -a`) to Quit nnn diff --git a/misc/quitcd/quitcd.fish b/misc/quitcd/quitcd.fish index 47cda35b331d7cc40b959ea0119f66b1791d3c07..33fb991beadc1c04a663d1ed119f53212832fcbe 100644 --- a/misc/quitcd/quitcd.fish +++ b/misc/quitcd/quitcd.fish @@ -14,7 +14,6 @@ # The default behaviour is to cd on quit (nnn checks if NNN_TMPFILE is set) # To cd on quit only on ^G, remove the "-x" as in: # set NNN_TMPFILE "$XDG_CONFIG_HOME/nnn/.lastd" - # NOTE: NNN_TMPFILE is fixed, should not be modified if test -n "$XDG_CONFIG_HOME" set -x NNN_TMPFILE "$XDG_CONFIG_HOME/nnn/.lastd" else diff --git a/nnn.1 b/nnn.1 index 06b554a343c9f5fa1bd381784fc20923aecac96a..0c79f5f04ac0245e7f46120e493c7fcc5a3dd93e 100644 --- a/nnn.1 +++ b/nnn.1 @@ -520,6 +520,11 @@ export NNN_LOCKER='bmon -p wlp1s0' export NNN_LOCKER='cmatrix' .Ed .Pp +\fBNNN_TMPFILE:\fR \fIalways\fR cd on quit and write the command in the file specified. +.Bd -literal + export NNN_TMPFILE='/tmp/.lastd' +.Ed +.Pp \fBNNN_HELP:\fR run a program and show the output on top of the program help page. .Bd -literal export NNN_HELP='fortune' diff --git a/src/nnn.c b/src/nnn.c index cae77ef82f70da9ca781a9ceaea5c45d3d0ebf24..5912066a0f9e915f40da263b4f61ee5b7d9ac1ee 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -1237,6 +1237,17 @@ path[homelen - 1] = home[homelen]; home[homelen] = '\0'; } +static void convert_tilde(const char *path, char *buf) +{ + if (path[0] == '~') { + ssize_t len = xstrlen(home); + ssize_t loclen = xstrlen(path); + + xstrsncpy(buf, home, len + 1); + xstrsncpy(buf + len, path + 1, loclen); + } +} + static int create_tmp_file(void) { xstrsncpy(g_tmpfpath + tmpfplen - 1, messages[STR_TMPFILE], TMP_LEN_MAX - tmpfplen); @@ -2676,21 +2687,21 @@ spawn(utils[UTIL_SH_EXEC], buf, NULL, NULL, F_CLI | F_CONFIRM); free(buf); } -static bool write_lastdir(const char *curpath) +static void write_lastdir(const char *curpath, const char *outfile) { - bool ret = FALSE; - size_t len = xstrlen(cfgpath); - - xstrsncpy(cfgpath + len, "/.lastd", 8); + if (!outfile) + xstrsncpy(cfgpath + xstrlen(cfgpath), "/.lastd", 8); + else + convert_tilde(outfile, g_buf); - int fd = open(cfgpath, O_CREAT | O_WRONLY | O_TRUNC, 0666); + int fd = open(outfile + ? (outfile[0] == '~' ? g_buf : outfile) + : cfgpath, O_CREAT | O_WRONLY | O_TRUNC, 0666); if (fd != -1) { dprintf(fd, "cd \"%s\"", curpath); close(fd); - ret = TRUE; } - return ret; } /* @@ -3732,15 +3743,7 @@ if (id == NNN_PLUG) return pluginstr + kvarr[r].off; val = bmstr + kvarr[r].off; - - if (val[0] == '~') { - ssize_t len = xstrlen(home); - ssize_t loclen = xstrlen(val); - - xstrsncpy(g_buf, home, len + 1); - xstrsncpy(g_buf + len, val + 1, loclen); - } - + convert_tilde(val, g_buf); return realpath(((val[0] == '~') ? g_buf : val), buf); } } @@ -7581,8 +7584,10 @@ save_session(session, NULL); #endif /* CD on Quit */ - if ((sel == SEL_QUITCD) || getenv("NNN_TMPFILE")) { - write_lastdir(path); + tmp = getenv("NNN_TMPFILE"); + if ((sel == SEL_QUITCD) || tmp) { + write_lastdir(path, tmp); + /* ^G is a way to quit picker mode without picking anything */ if ((sel == SEL_QUITCD) && g_state.picker) selbufpos = 0; }