From: Arun Prakash Jana <engineerarun@gmail.com>
Date: Sun, 28 Jan 2018 06:03:12 +0000 (+0530)
Subject: Support file path quote on copy
X-Git-Tag: v1.7~23
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=6da9d5f499e133ef693ab743d65e2fc79cc08d24;p=nnn.git

Support file path quote on copy
---

diff --git a/README.md b/README.md
index 52c188f0..4ff24059 100644
--- a/README.md
+++ b/README.md
@@ -247,6 +247,7 @@ optional arguments:
              ^F | Extract archive
              ^K | Invoke file path copier
              ^Y | Toggle multi-copy mode
+             ^T | Toggle path quote
              ^L | Redraw, clear prompt
               ? | Help, settings
               Q | Quit and cd
@@ -369,6 +370,11 @@ To copy multiple file paths, switch to the multi-copy mode using <kbd>^Y</kbd>.
 
 Pressing <kbd>^Y</kbd> again copies the paths to clipboard and exits the multi-copy mode.
 
+To wrap each file path within single quotes, export `NNN_QUOTE_ON`:
+
+    export NNN_QUOTE_ON=1
+This is particularly useful if you are planning to copy the whole string to the shell to run a command. Quotes can be toggled at runtime using <kbd>^T</kbd>.
+
 #### change dir color
 
 The default color for directories is blue. Option `-c` accepts color codes from 0 to 7 to use a different color:
diff --git a/nnn.1 b/nnn.1
index b5be9fc2..98931843 100644
--- a/nnn.1
+++ b/nnn.1
@@ -104,6 +104,8 @@ Extract archive in current directory
 Invoke file path copier
 .It Ic ^Y
 Toggle multiple file path copy mode
+.It Ic ^T
+Toggle path quote
 .It Ic ^L
 Force a redraw, clear rename or filter prompt
 .It Ic \&?
@@ -247,6 +249,9 @@ screensaver.
 .Bd -literal
     export NNN_NOWAIT=1
 .Ed
+.Pp
+\fBNNN_QUOTE_ON:\fR wrap copied paths within single quotes. Useful for pasting
+names in the shell.
 .Sh KNOWN ISSUES
 If you are using urxvt you might have to set backspacekey to DEC.
 .Sh AUTHORS
diff --git a/nnn.c b/nnn.c
index 097e1c08..d69ab3b1 100644
--- a/nnn.c
+++ b/nnn.c
@@ -229,13 +229,14 @@ typedef struct {
 	ushort showcolor  : 1;  /* Set to show dirs in blue */
 	ushort dircolor   : 1;  /* Current status of dir color */
 	ushort metaviewer : 1;  /* Index of metadata viewer in utils[] */
+	ushort quote      : 1;  /* Copy paths within quotes */
 	ushort color      : 3;  /* Color code for directories */
 } settings;
 
 /* GLOBALS */
 
 /* Configuration */
-static settings cfg = {0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 4};
+static settings cfg = {0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 4};
 
 static struct entry *dents;
 static char *pnamebuf, *pcopybuf;
@@ -619,7 +620,7 @@ xbasename(char *path)
 static bool
 appendfilepath(const char *path, const size_t len)
 {
-	if ((copybufpos >= copybuflen) || (len > (copybuflen - (copybufpos + 1)))) {
+	if ((copybufpos >= copybuflen) || (len > (copybuflen - (copybufpos + 3)))) {
 		copybuflen += PATH_MAX;
 		pcopybuf = xrealloc(pcopybuf, copybuflen);
 		if (!pcopybuf) {
@@ -628,10 +629,24 @@ appendfilepath(const char *path, const size_t len)
 		}
 	}
 
-	if (copybufpos)
+	if (copybufpos) {
 		pcopybuf[copybufpos - 1] = '\n';
+		if (cfg.quote) {
+			pcopybuf[copybufpos] = '\'';
+			++copybufpos;
+		}
+	} else if (cfg.quote) {
+		pcopybuf[copybufpos] = '\'';
+		++copybufpos;
+	}
 
 	copybufpos += xstrlcpy(pcopybuf + copybufpos, path, len);
+	if (cfg.quote) {
+		pcopybuf[copybufpos - 1] = '\'';
+		pcopybuf[copybufpos] = '\0';
+		++copybufpos;
+	}
+
 	return TRUE;
 }
 
@@ -1844,6 +1859,7 @@ show_help(char *path)
 	    "d^F | Extract archive\n"
 	    "d^K | Invoke file path copier\n"
 	    "d^Y | Toggle multi-copy mode\n"
+	    "d^T | Toggle path quote\n"
 	    "d^L | Redraw, clear prompt\n"
 	     "e? | Help, settings\n"
 	     "eQ | Quit and cd\n"
@@ -2880,6 +2896,14 @@ nochange:
 				}
 			}
 			goto nochange;
+		case SEL_QUOTE:
+			cfg.quote ^= 1;
+			DPRINTF_D(cfg.quote);
+			if (cfg.quote)
+				printmsg("quotes on");
+			else
+				printmsg("quotes off");
+			goto nochange;
 		case SEL_OPEN:
 			printprompt("open with: "); // fallthrough
 		case SEL_NEW:
@@ -3184,6 +3208,10 @@ main(int argc, char *argv[])
 	/* Get nowait flag */
 	nowait |= getenv("NNN_NOWAIT") ? F_NOWAIT : 0;
 
+	/* Enable quotes if opted */
+	if (getenv("NNN_QUOTE_ON"))
+		cfg.quote = 1;
+
 	signal(SIGINT, SIG_IGN);
 
 	/* Test initial path */
diff --git a/nnn.h b/nnn.h
index 26379f8b..bb757f97 100644
--- a/nnn.h
+++ b/nnn.h
@@ -35,6 +35,7 @@ enum action {
 	SEL_REDRAW,
 	SEL_COPY,
 	SEL_COPYMUL,
+	SEL_QUOTE,
 	SEL_OPEN,
 	SEL_NEW,
 	SEL_RENAME,
@@ -148,6 +149,8 @@ static struct key bindings[] = {
 	{ CONTROL('K'),   SEL_COPY,      "",     "" },
 	/* Toggle copy multiple file paths */
 	{ CONTROL('Y'),   SEL_COPYMUL,   "",     "" },
+	/* Toggle quote on while copy */
+	{ CONTROL('T'),   SEL_QUOTE,     "",     "" },
 	/* Open in a custom application */
 	{ CONTROL('O'),   SEL_OPEN,      "",     "" },
 	/* Create a new file */