Skip to content

Commit

Permalink
Allow specifying output file in NNN_TMPFILE for cd on quit
Browse files Browse the repository at this point in the history
  • Loading branch information
jarun committed Jul 25, 2021
1 parent f1dbb96 commit f6856f6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
1 change: 0 additions & 1 deletion misc/quitcd/quitcd.bash_zsh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ n ()
# 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
Expand Down
1 change: 0 additions & 1 deletion misc/quitcd/quitcd.csh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# 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
Expand Down
1 change: 0 additions & 1 deletion misc/quitcd/quitcd.fish
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ function n --wraps nnn --description 'support nnn quit and change directory'
# 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
Expand Down
5 changes: 5 additions & 0 deletions nnn.1
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@ separated by \fI;\fR:
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'
Expand Down
43 changes: 24 additions & 19 deletions src/nnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,17 @@ static void reset_tilde_in_path(char *path)
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);
Expand Down Expand Up @@ -2676,21 +2687,21 @@ static void archive_selection(const char *cmd, const char *archive, const char *
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;
}

/*
Expand Down Expand Up @@ -3732,15 +3743,7 @@ static char *get_kv_val(kv *kvarr, char *buf, int key, uchar_t max, uchar_t id)
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);
}
}
Expand Down Expand Up @@ -7581,8 +7584,10 @@ static bool browse(char *ipath, const char *session, int pkey)
#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;
}
Expand Down

0 comments on commit f6856f6

Please sign in to comment.