Skip to content

Commit

Permalink
quitcd: fix bugs and feat. for modular export and selective quit
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondMagic committed Feb 10, 2024
1 parent ea40f91 commit 2767538
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
52 changes: 35 additions & 17 deletions misc/quitcd/quitcd.nu
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
# The behaviour is set to cd on quit (nnn checks if NNN_TMPFILE is set)
let cfgHome = ($env | default $"($env.HOME)/.config" XDG_CONFIG_HOME | get XDG_CONFIG_HOME)
$env.NNN_TMPFILE = $"($cfgHome)/nnn/.lastd"
# Run nnn with dynamic changing directory to the environment.
#
# $env.XDG_CONFIG_HOME sets the home folder for `nnn` folder and its $env.NNN_TMPFILE variable.
# See manual NNN(1) for more information.
#
# Import module using `use quitcd.nu n` to have `n` command in your context.
export def --env n [
...args : string # Extra flags to launch nnn with.
--selective = false # Change directory only when exiting via ^G.
] -> nothing {

def --env n [...x] {
# Launch nnn. Add desired flags after `^nnn`, ex: `^nnn -eda ($x | str join)`
^nnn ($x | str join)
let newpath = (
if ($env.NNN_TMPFILE | path exists) {
# FIXME: fails if path contains single-quote
let newpath = (open $env.NNN_TMPFILE | parse "cd '{nnnpath}'").0.nnnpath
^rm -f $env.NNN_TMPFILE
echo $newpath
} else {
pwd
}
)
cd $newpath
# The behaviour is set to cd on quit (nnn checks if $env.NNN_TMPFILE is set).
# Hard-coded to its respective behaviour in `nnn` source-code.
let nnn_tmpfile = $env
| default '~/.config/' 'XDG_CONFIG_HOME'
| get 'XDG_CONFIG_HOME'
| path join 'nnn/.lastd'
| path expand

# Launch nnn. Add desired flags after `^nnn`, ex: `^nnn -eda ...$args`,
# or make an alias `alias n = n -eda`.
if $selective {
^nnn ...$args
} else {
NNN_TMPFILE=$nnn_tmpfile ^nnn ...$args
}

if ($nnn_tmpfile | path exists) {
# Remove <cd '> from the first part of the string and the last single quote <'>.
# Fix post-processing of nnn's given path that escapes its single quotes with POSIX syntax.
let path = open $nnn_tmpfile | str substring 4..-1 | str replace --all `'\''` `'`

^rm $nnn_tmpfile

cd $path
}
}
2 changes: 1 addition & 1 deletion patches/restorepreview/mainline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ index 0388b23c..66d3316a 100644
case SEL_PROMPT:
+ if (g_state.previewer)
+ notify_fifo(FALSE, TRUE);
r = handle_cmd(sel, path, newpath);
r = handle_cmd(sel, newpath);
+ if (g_state.previewer) {
+ pkey = previewkey;
+ goto run_plugin;
Expand Down
13 changes: 7 additions & 6 deletions src/nnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -5237,7 +5237,7 @@ static void show_help(const char *path)
unlink(g_tmpfpath);
}

static void setexports(const char *path)
static void setexports(void)
{
char dvar[] = "d0";
char fvar[] = "f0";
Expand All @@ -5261,7 +5261,6 @@ static void setexports(const char *path)
}
setenv("NNN_INCLUDE_HIDDEN", xitoa(cfg.showhidden), 1);
setenv("NNN_PREFER_SELECTION", xitoa(cfg.prefersel), 1);
setenv("PWD", path, 1);
}

static void run_cmd_as_plugin(const char *file, uchar_t flags)
Expand Down Expand Up @@ -5392,7 +5391,7 @@ static bool run_plugin(char **path, const char *file, char *runfile, char **last
g_state.pluginit = 1;
}

setexports(*path);
setexports();

/* Check for run-cmd-as-plugin mode */
if (*file == '!') {
Expand Down Expand Up @@ -5566,14 +5565,14 @@ static bool prompt_run(void)
return ret;
}

static bool handle_cmd(enum action sel, char *path, char *newpath)
static bool handle_cmd(enum action sel, char *newpath)
{
endselection(FALSE);

if (sel == SEL_LAUNCH)
return launch_app(newpath);

setexports(path);
setexports();

if (sel == SEL_PROMPT)
return prompt_run();
Expand Down Expand Up @@ -6836,6 +6835,8 @@ static bool browse(char *ipath, const char *session, int pkey)
setdirwatch();
}

setenv("PWD", path, TRUE);

#ifndef NOX11
xterm_cfg(path);
#endif
Expand Down Expand Up @@ -7968,7 +7969,7 @@ static bool browse(char *ipath, const char *session, int pkey)
case SEL_SHELL: // fallthrough
case SEL_LAUNCH: // fallthrough
case SEL_PROMPT:
r = handle_cmd(sel, path, newpath);
r = handle_cmd(sel, newpath);

/* Continue in type-to-nav mode, if enabled */
if (cfg.filtermode)
Expand Down

0 comments on commit 2767538

Please sign in to comment.