From 703d34938900bf2413fabdc5f2619c922ef5e3e2 Mon Sep 17 00:00:00 2001 From: blissful Date: Fri, 17 Nov 2023 17:25:16 -0500 Subject: [PATCH 1/2] restore sort function pointers when restoring cfg --- src/nnn.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/nnn.c b/src/nnn.c index 0388b23c..b3c188da 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -4308,6 +4308,9 @@ 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; } #ifndef NOSSN @@ -6248,11 +6251,8 @@ static int set_sort_flags(int r) r = 'd'; } - if (cfg.version) - namecmpfn = &xstrverscasecmp; - - if (cfg.reverse) - entrycmpfn = &reventrycmp; + entrycmpfn = cfg.reverse ? &reventrycmp : &entrycmp; + namecmpfn = cfg.version ? &xstrverscasecmp : &xstricmp; } else if (r == CONTROL('T')) { /* Cycling order: clear -> size -> time -> clear */ if (cfg.timeorder) @@ -8018,6 +8018,9 @@ nochange: 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; cfg.curctx = r; setdirwatch(); From c0b3cc86898cc3f531edc6a28e019a8f0d7cc578 Mon Sep 17 00:00:00 2001 From: blissful Date: Sat, 18 Nov 2023 16:30:40 -0500 Subject: [PATCH 2/2] factor out a `setcfg` function for setting the cfg global --- src/nnn.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/nnn.c b/src/nnn.c index b3c188da..5920797f 100644 --- 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; }