]> Sergey Matveev's repositories - nnn.git/commitdiff
Use existing buffer to (un)select file names
authorArun Prakash Jana <engineerarun@gmail.com>
Mon, 10 May 2021 21:37:55 +0000 (03:07 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Mon, 10 May 2021 21:37:55 +0000 (03:07 +0530)
src/.clang-tidy
src/nnn.c

index a3d12927c5d631bbd6b7d1e2a69e89c93ac39563..6a8141ab15af697053758be8c456fab3acb9694c 100644 (file)
@@ -11,5 +11,5 @@ CheckOptions:
   - key:             fuchsia-restrict-system-includes.Includes
     value:           '*,-stdint.h,-stdbool.h'
   - key:             readability-function-size.StatementThreshold
-    value:           '925'
+    value:           '900'
 ...
index 43038f36a66356bb83f497a8602e1df3a4c28320..f67c42ac720e863f1c521be7d5fca54111d4cc1c 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -1458,15 +1458,68 @@ static void startselection(void)
        }
 }
 
-static void updateselbuf(const char *path, char *newpath)
+static size_t appendslash(char *path)
 {
-       size_t r;
 
-       for (int i = 0; i < ndents; ++i)
-               if (pdents[i].flags & FILE_SELECTED) {
-                       r = mkpath(path, pdents[i].name, newpath);
-                       appendfpath(newpath, r);
+       size_t len = 1;
+
+       if (path[1] != '\0') {
+               len = xstrlen(path);
+               path[len] = '/';
+               ++len;
+       }
+
+       return len;
+}
+
+static void invertselbuf(char *path, bool toggle)
+{
+       selbufpos = lastappendpos;
+
+       if (!nselected) {
+               writesel(NULL, 0);
+               return;
+       }
+
+       size_t len = appendslash(path);
+
+       for (int i = 0; i < ndents; ++i) {
+               if (toggle) { /* Toggle selection status */
+                       pdents[i].flags ^= FILE_SELECTED;
+                       pdents[i].flags & FILE_SELECTED ? ++nselected : --nselected;
+               }
+
+               if (pdents[i].flags & FILE_SELECTED)
+                       appendfpath(path, len + xstrsncpy(path + len, pdents[i].name, PATH_MAX - len));
+       }
+
+       if (len > 1)
+               --len;
+       path[len] = '\0';
+
+       writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
+}
+
+static void addtoselbuf(char *path, int startid, int endid)
+{
+       size_t len = appendslash(path);
+
+       /* Remember current selection buffer position */
+       for (int i = startid; i <= endid; ++i) {
+               if (!(pdents[i].flags & FILE_SELECTED)) {
+                       /* Write the path to selection file to avoid flush */
+                       appendfpath(path, len + xstrsncpy(path + len, pdents[i].name, PATH_MAX - len));
+
+                       pdents[i].flags |= FILE_SELECTED;
+                       ++nselected;
                }
+       }
+
+       if (len > 1)
+               --len;
+       path[len] = '\0';
+
+       writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
 }
 
 /* Finish selection procedure before an operation */
@@ -6617,12 +6670,8 @@ nochange:
                                appendfpath(newpath, mkpath(path, pdents[cur].name, newpath));
                                writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
                        } else {
-                               selbufpos = lastappendpos;
-                               if (--nselected) {
-                                       updateselbuf(path, newpath);
-                                       writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
-                               } else
-                                       writesel(NULL, 0);
+                               --nselected;
+                               invertselbuf(path, FALSE);
                        }
 
 #ifndef NOX11
@@ -6693,33 +6742,7 @@ nochange:
                                selendid = ndents - 1;
                        }
 
-                       if (sel == SEL_SELINV) {
-                               /* Toggle selection status */
-                               for (r = selstartid; r <= selendid; ++r) {
-                                       pdents[r].flags ^= FILE_SELECTED;
-                                       pdents[r].flags & FILE_SELECTED ? ++nselected : --nselected;
-                               }
-
-                               selbufpos = lastappendpos;
-                               if (nselected) {
-                                       updateselbuf(path, newpath);
-                                       writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
-                               } else
-                                       writesel(NULL, 0);
-                       } else {
-                               /* Remember current selection buffer position */
-                               for (r = selstartid; r <= selendid; ++r) {
-                                       if (!(pdents[r].flags & FILE_SELECTED)) {
-                                               /* Write the path to selection file to avoid flush */
-                                               appendfpath(newpath, mkpath(path, pdents[r].name, newpath));
-
-                                               pdents[r].flags |= FILE_SELECTED;
-                                               ++nselected;
-                                       }
-                               }
-
-                               writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
-                       }
+                       (sel == SEL_SELINV) ? invertselbuf(path, TRUE) : addtoselbuf(path, selstartid, selendid);
 
 #ifndef NOX11
                        if (cfg.x11)