Skip to content

Commit

Permalink
factor out a setcfg function for setting the cfg global
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Nov 18, 2023
1 parent 703d349 commit c0b3cc8
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/nnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -8017,12 +8027,9 @@ static bool browse(char *ipath, const char *session, int pkey)
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;
}
Expand Down

0 comments on commit c0b3cc8

Please sign in to comment.