From ad4ecb19cb9f398b3e7f1aea2de57eace15e10e8 Mon Sep 17 00:00:00 2001
From: Arun Prakash Jana <engineerarun@gmail.com>
Date: Sat, 28 Jan 2023 09:11:15 +0530
Subject: [PATCH] Simplify paged and GUI commands run as plugin

---
 nnn.1             | 14 ++++++--------
 plugins/README.md |  8 ++++----
 src/nnn.c         | 18 ++++--------------
 3 files changed, 14 insertions(+), 26 deletions(-)

diff --git a/nnn.1 b/nnn.1
index 61532ab5..7025d6a4 100644
--- a/nnn.1
+++ b/nnn.1
@@ -423,14 +423,14 @@ separated by \fI;\fR:
     To assign keys to arbitrary non-background cli commands and invoke like
     plugins, add \fB!\fR before the command.
 .Bd -literal
-    export NNN_PLUG='x:!chmod +x $nnn;g:!git log;s:!smplayer $nnn'
+    export NNN_PLUG='x:!chmod +x "$nnn";g:!git log;s:!smplayer "$nnn"'
 
     To pick and run an unassigned plugin, press \fBEnter\fR at the plugin prompt.
     To run a plugin at startup, use the option `-P` followed by the plugin key.
 
     NOTES:
-    1. Use single quotes for $NNN_PLUG so $nnn is not interpreted
-    2. $nnn must be the last argument (if used) to run a \fIcommand as plugin\fR
+    1. Place $nnn in double quotes (\fB"$nnn"\fR)
+    2. Use single quotes for $NNN_PLUG so "$nnn" is not interpreted
     3. (Again) add \fB!\fR before the command
     4. To disable directory refresh after running a \fIcommand as plugin\fR,
        prefix with \fB-!\fR
@@ -439,18 +439,16 @@ separated by \fI;\fR:
 
         export NNN_PLUG='y:-!sync*'
 
-    6. To run a \fIGUI app as plugin\fR, add a \fB&\fR after \fB!\fR
-       \fI$nnn\fR must be the last argument in this case.
+    6. To run a \fIGUI app as plugin\fR, add a \fB&\fR after \fB!\fR.
 
-        export NNN_PLUG='m:-!&mousepad $nnn'
+        export NNN_PLUG='m:-!&mousepad "$nnn"'
 
     7. To show the output of run-and-exit commands which do not need user input,
        add \fB|\fR (pipe) after \fB!\fR
        Note: This option is incompatible with \fB&\fR (terminal output is masked
        for GUI programs) and ignores \fB*\fR (output is already paged for user).
-       \fI$nnn\fR must be the last argument in this case as well.
 
-        export NNN_PLUG='m:-!|mediainfo $nnn;t:-!|tree -ps;l:-!|ls -lah --group-directories-first'
+        export NNN_PLUG='m:-!|mediainfo "$nnn";t:-!|tree -ps;l:-!|ls -lah --group-directories-first'
 
     EXAMPLES:
     ------------------------------------ + -------------------------------------------------
diff --git a/plugins/README.md b/plugins/README.md
index 2a7aeef9..5d749c17 100644
--- a/plugins/README.md
+++ b/plugins/README.md
@@ -180,10 +180,10 @@ export NNN_PLUG='m:-!|mediainfo "$nnn";t:-!|tree -ps;l:-!|ls -lah --group-direct
 This option is incompatible with `&` (terminal output is masked for GUI programs) and ignores `*` (output is already paged for user).
 
 Notes:
-
-1. Use single quotes for `$NNN_PLUG` so `"$nnn"` is not interpreted
-2. (_Again_) add `!` before the command
-3. To disable directory refresh after running a _command as plugin_, prefix with `-!`
+1. Place `$nnn` in double quotes (**`"$nnn"`**)
+2. Use single quotes for `$NNN_PLUG` so `"$nnn"` is not interpreted
+3. (_Again_) add `!` before the command
+4. To disable directory refresh after running a _command as plugin_, prefix with `-!`
 
 #### Some useful key-command examples
 
diff --git a/src/nnn.c b/src/nnn.c
index 6be52e3f..17e97da9 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -5175,7 +5175,7 @@ static void setexports(void)
 	setenv("NNN_INCLUDE_HIDDEN", xitoa(cfg.showhidden), 1);
 }
 
-static void run_cmd_as_plugin(const char *file, char *runfile, uchar_t flags)
+static void run_cmd_as_plugin(const char *file, uchar_t flags)
 {
 	size_t len;
 
@@ -5188,16 +5188,6 @@ static void run_cmd_as_plugin(const char *file, char *runfile, uchar_t flags)
 		--len;
 	}
 
-	/* This is to catch the old way of doing things so we don't break users' configs */
-	if ((flags & (F_PAGE | F_NOTRACE)) && is_suffix(g_buf, " $nnn")) {
-		g_buf[len - 5] = '\0';
-
-		if (flags & F_PAGE)
-			get_output(g_buf, runfile, NULL, -1, TRUE);
-		else // F_NOTRACE
-			spawn(g_buf, runfile, NULL, NULL, flags);
-	}
-
 	if (flags & F_PAGE)
 		get_output(utils[UTIL_SH_EXEC], g_buf, NULL, -1, TRUE);
 	else
@@ -5324,7 +5314,7 @@ static bool run_plugin(char **path, const char *file, char *runfile, char **last
 			flags |= F_PAGE;
 			++file;
 		} else if (*file == '&') { /* Check if GUI flags are to be used */
-			flags = F_NOTRACE | F_NOWAIT;
+			flags = F_MULTI | F_NOTRACE | F_NOWAIT;
 			++file;
 		}
 
@@ -5332,7 +5322,7 @@ static bool run_plugin(char **path, const char *file, char *runfile, char **last
 			return FALSE;
 
 		if ((flags & F_NOTRACE) || (flags & F_PAGE)) {
-			run_cmd_as_plugin(file, runfile, flags);
+			run_cmd_as_plugin(file, flags);
 			return TRUE;
 		}
 
@@ -5368,7 +5358,7 @@ static bool run_plugin(char **path, const char *file, char *runfile, char **last
 			} else
 				spawn(g_buf, NULL, *path, sel, 0);
 		} else
-			run_cmd_as_plugin(file, runfile, flags);
+			run_cmd_as_plugin(file, flags);
 
 		close(wfd);
 		_exit(EXIT_SUCCESS);
-- 
2.51.0