]> Sergey Matveev's repositories - nnn.git/commitdiff
factor out a `setcfg` function for setting the cfg global
authorblissful <blissful@sunsetglow.net>
Sat, 18 Nov 2023 21:30:40 +0000 (16:30 -0500)
committerblissful <blissful@sunsetglow.net>
Sat, 18 Nov 2023 21:34:17 +0000 (16:34 -0500)
src/nnn.c

index b3c188da677696afcfe51631e1b0ce5cdb735986..5920797f1faf1e85fbd50100160ea1b6aebd6fc2 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -4276,6 +4276,18 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
                addch(ind);
 }
 
+/**
+ * Sets the global cfg variable and restores related state to match the new
+ * cfg.
+ */
+static void setcfg(settings newcfg)
+{
+       cfg = newcfg;
+       /* Synchronize the global function pointers to match the new cfg. */
+       entrycmpfn = cfg.reverse ? &reventrycmp : &entrycmp;
+       namecmpfn = cfg.version ? &xstrverscasecmp : &xstricmp;
+}
+
 static void savecurctx(char *path, char *curname, int nextctx)
 {
        settings tmpcfg = cfg;
@@ -4307,10 +4319,7 @@ static void savecurctx(char *path, char *curname, int nextctx)
        }
 
        tmpcfg.curctx = nextctx;
-       cfg = tmpcfg;
-       /* Restore the global function pointers alongside the cfg. */
-       entrycmpfn = cfg.reverse ? &reventrycmp : &entrycmp;
-       namecmpfn = cfg.version ? &xstrverscasecmp : &xstricmp;
+       setcfg(tmpcfg);
 }
 
 #ifndef NOSSN
@@ -6251,6 +6260,7 @@ static int set_sort_flags(int r)
                        r = 'd';
                }
 
+               /* Ensure function pointers are in sync with cfg. */
                entrycmpfn = cfg.reverse ? &reventrycmp : &entrycmp;
                namecmpfn = cfg.version ? &xstrverscasecmp : &xstricmp;
        } else if (r == CONTROL('T')) {
@@ -8017,12 +8027,9 @@ nochange:
                                        lastdir = g_ctx[r].c_last;
                                        lastname = g_ctx[r].c_name;
 
-                                       cfg = g_ctx[r].c_cfg;
-                                       /* Restore the global function pointers alongside the cfg. */
-                                       entrycmpfn = cfg.reverse ? &reventrycmp : &entrycmp;
-                                       namecmpfn = cfg.version ? &xstrverscasecmp : &xstricmp;
+                                       g_ctx[r].c_cfg.curctx = r;
+                                       setcfg(g_ctx[r].c_cfg);
 
-                                       cfg.curctx = r;
                                        setdirwatch();
                                        goto begin;
                                }