From d7da4acde04e8c340ddf4ba95c73c6c90f3ad78d Mon Sep 17 00:00:00 2001
From: Arun Prakash Jana <engineerarun@gmail.com>
Date: Sat, 1 Dec 2018 07:42:37 +0530
Subject: [PATCH] Split EDITOR & PAGER options, concise NNN_USE_EDITOR logic.

---
 src/nnn.c | 43 +++++++++++++++----------------------------
 src/nnn.h |  7 ++++---
 2 files changed, 19 insertions(+), 31 deletions(-)

diff --git a/src/nnn.c b/src/nnn.c
index 8a4c3f36..5751dfbc 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -883,7 +883,7 @@ static void getprogarg(char *prog, char **arg)
 		/* Make sure there are no more args */
 		while (*argptr) {
 			if (*argptr == ' ') {
-				fprintf(stderr, "Too many %s args\n", prog);
+				fprintf(stderr, "Too many args [%s]\n", prog);
 				exit(1);
 			}
 			++argptr;
@@ -2092,7 +2092,7 @@ static int show_help(char *path)
 	}
 
 	if (cfg.useeditor)
-		dprintf(fd, "NNN_USE_EDITOR: %s\n", editor);
+		dprintf(fd, "NNN_USE_EDITOR: 1\n");
 	if (idletimeout)
 		dprintf(fd, "NNN_IDLE_TIMEOUT: %d secs\n", idletimeout);
 	if (copier)
@@ -2694,24 +2694,14 @@ nochange:
 					continue;
 
 				/* If NNN_USE_EDITOR is set, open text in EDITOR */
-				if (cfg.useeditor) {
-					if (getmime(dents[cur].name)) {
+				if (cfg.useeditor)
+					if (getmime(dents[cur].name) ||
+					    (get_output(g_buf, MAX_CMD_LEN, "file", FILE_OPTS, newpath, 0) &&
+					     strstr(g_buf, "text/") == g_buf)) {
 						spawn(editor, newpath, editor_arg, path, F_NORMAL);
 						continue;
 					}
 
-					/* Recognize and open plain
-					 * text files with vi
-					 */
-					if (get_output(g_buf, MAX_CMD_LEN, "file", FILE_OPTS, newpath, 0) == NULL)
-						continue;
-
-					if (strstr(g_buf, "text/") == g_buf) {
-						spawn(editor, newpath, editor_arg, path, F_NORMAL);
-						continue;
-					}
-				}
-
 				/* Invoke desktop opener as last resort */
 				spawn(utils[OPENER], newpath, NULL, NULL, F_NOWAIT | F_NOTRACE);
 				continue;
@@ -3398,14 +3388,11 @@ nochange:
 
 			/* Repopulate as directory content may have changed */
 			goto begin;
-		case SEL_RUNARG:
-			tmp = NULL;
-			run = xgetenv(env, run);
-			if ((!run || !run[0]) && (strcmp("VISUAL", env) == 0)) {
-				run = editor;
-				tmp = editor_arg;
-			}
-			spawn(run, dents[cur].name, tmp, path, F_NORMAL);
+		case SEL_RUNEDIT:
+			spawn(editor, dents[cur].name, editor_arg, path, F_NORMAL);
+			break;
+		case SEL_RUNPAGE:
+			spawn(pager, dents[cur].name, pager_arg, path, F_NORMAL);
 			break;
 		case SEL_LOCK:
 			spawn(utils[LOCKER], NULL, NULL, NULL, F_NORMAL | F_SIGINT);
@@ -3601,6 +3588,10 @@ int main(int argc, char *argv[])
 	if (getuid() == 0 || getenv("NNN_SHOW_HIDDEN"))
 		cfg.showhidden = 1;
 
+	/* Edit text in EDITOR, if opted */
+	if (getenv("NNN_USE_EDITOR"))
+		cfg.useeditor = 1;
+
 	/* Get VISUAL/EDITOR */
 	editor = xgetenv("VISUAL", xgetenv("EDITOR", "vi"));
 	getprogarg(editor, &editor_arg);
@@ -3609,10 +3600,6 @@ int main(int argc, char *argv[])
 	pager = xgetenv("PAGER", "less");
 	getprogarg(pager, &pager_arg);
 
-	/* Edit text in EDITOR, if opted */
-	if (getenv("NNN_USE_EDITOR"))
-		cfg.useeditor = 1;
-
 #ifdef LINUX_INOTIFY
 	/* Initialize inotify */
 	inotify_fd = inotify_init1(IN_NONBLOCK);
diff --git a/src/nnn.h b/src/nnn.h
index c635e4d6..9befd59e 100644
--- a/src/nnn.h
+++ b/src/nnn.h
@@ -82,7 +82,8 @@ enum action {
 	SEL_HELP,
 	SEL_RUN,
 	SEL_RUNSCRIPT,
-	SEL_RUNARG,
+	SEL_RUNEDIT,
+	SEL_RUNPAGE,
 	SEL_LOCK,
 	SEL_QUITCTX,
 	SEL_QUITCD,
@@ -226,8 +227,8 @@ static struct key bindings[] = {
 	/* Run a custom script */
 	{ 'R',            SEL_RUNSCRIPT, "sh",   "SHELL" },
 	/* Run command with argument */
-	{ 'e',            SEL_RUNARG,    "",     "VISUAL" },
-	{ 'p',            SEL_RUNARG,    "",     "PAGER" },
+	{ 'e',            SEL_RUNEDIT,   "",     "" },
+	{ 'p',            SEL_RUNPAGE,   "",     "" },
 	/* Lock screen */
 	{ 'L',            SEL_LOCK,      "",     "" },
 	/* Quit a context */
-- 
2.51.0