]> Sergey Matveev's repositories - nnn.git/commitdiff
Support file path quote on copy
authorArun Prakash Jana <engineerarun@gmail.com>
Sun, 28 Jan 2018 06:03:12 +0000 (11:33 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Sun, 28 Jan 2018 06:04:18 +0000 (11:34 +0530)
README.md
nnn.1
nnn.c
nnn.h

index 52c188f069b966c0cda8d127b544e4c320a49e62..4ff24059ecd83e1ff4cda4a072383e9332886f6a 100644 (file)
--- 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 b5be9fc213a2d8fa2005e58f963a680e78b3b703..98931843814d955c5ab07d12c517a7765537fdd6 100644 (file)
--- 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 097e1c0855286712528e198ab8545551071b0a0a..d69ab3b1249079f2d13b8df054e96d1667e41837 100644 (file)
--- 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 26379f8bccb0ff8d64c4fcc46f0e3954f1c6997c..bb757f975ff509cefd96a8b9124c9d5c7b2d4b0b 100644 (file)
--- 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 */