Internal states, sessions changes

- replace bools for internal non-persistent binary states with an uchar
- do not store path to session, derive from config path in tmp buffer
- program option to load session is not `-s`
- remove auto-stored session "@" on restore
- minor help update
This commit is contained in:
Arun Prakash Jana 2019-12-19 08:37:17 +05:30
parent 92ac068815
commit cf388649b9
No known key found for this signature in database
GPG key ID: A75979F35C080412
5 changed files with 64 additions and 54 deletions

View file

@ -16,7 +16,6 @@ _nnn ()
-b -b
-c -c
-d -d
-e
-E -E
-g -g
-H -H
@ -28,6 +27,7 @@ _nnn ()
-Q -Q
-r -r
-R -R
-s
-S -S
-t -t
-v -v
@ -39,7 +39,7 @@ _nnn ()
COMPREPLY=( $(compgen -W "$bookmarks" -- "$cur") ) COMPREPLY=( $(compgen -W "$bookmarks" -- "$cur") )
elif [[ $prev == -p ]]; then elif [[ $prev == -p ]]; then
COMPREPLY=( $(compgen -f -d -- "$cur") ) COMPREPLY=( $(compgen -f -d -- "$cur") )
elif [[ $prev == -e ]]; then elif [[ $prev == -s ]]; then
local sessions_dir=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/sessions local sessions_dir=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/sessions
COMPREPLY=( $(cd "$sessions_dir" && compgen -f -d -- "$cur") ) COMPREPLY=( $(cd "$sessions_dir" && compgen -f -d -- "$cur") )
elif [[ $cur == -* ]]; then elif [[ $cur == -* ]]; then

View file

@ -15,7 +15,6 @@ complete -c nnn -s a -d 'use access time'
complete -c nnn -s b -r -d 'bookmark key to open' -x -a '(echo $NNN_BMS | awk -F: -v RS=\; \'{print $1"\t"$2}\')' complete -c nnn -s b -r -d 'bookmark key to open' -x -a '(echo $NNN_BMS | awk -F: -v RS=\; \'{print $1"\t"$2}\')'
complete -c nnn -s c -d 'cli-only opener' complete -c nnn -s c -d 'cli-only opener'
complete -c nnn -s d -d 'start in detail mode' complete -c nnn -s d -d 'start in detail mode'
complete -c nnn -s e -r -d 'load session by name' -x -a '@\t"last session" (ls $sessions_dir)'
complete -c nnn -s E -d 'use EDITOR for undetached edits' complete -c nnn -s E -d 'use EDITOR for undetached edits'
complete -c nnn -s g -d 'regex filters' complete -c nnn -s g -d 'regex filters'
complete -c nnn -s H -d 'show hidden files' complete -c nnn -s H -d 'show hidden files'
@ -27,6 +26,7 @@ complete -c nnn -s p -r -d 'copy selection to file' -a '-\tstdout'
complete -c nnn -s Q -d 'disable quit confirmation' complete -c nnn -s Q -d 'disable quit confirmation'
complete -c nnn -s r -d 'show cp, mv progress (Linux-only)' complete -c nnn -s r -d 'show cp, mv progress (Linux-only)'
complete -c nnn -s R -d 'disable rollover at edges' complete -c nnn -s R -d 'disable rollover at edges'
complete -c nnn -s s -r -d 'load session by name' -x -a '@\t"last session" (ls $sessions_dir)'
complete -c nnn -s S -d 'start in disk usage analyzer mode' complete -c nnn -s S -d 'start in disk usage analyzer mode'
complete -c nnn -s t -d 'disable dir auto-select' complete -c nnn -s t -d 'disable dir auto-select'
complete -c nnn -s v -d 'show program version and exit' complete -c nnn -s v -d 'show program version and exit'

View file

@ -13,7 +13,6 @@ args=(
'(-b)-b[bookmark key to open]:key char' '(-b)-b[bookmark key to open]:key char'
'(-c)-c[cli-only opener]' '(-c)-c[cli-only opener]'
'(-d)-d[start in detail mode]' '(-d)-d[start in detail mode]'
'(-e)-e[load session]:session name'
'(-E)-E[use EDITOR for undetached edits]' '(-E)-E[use EDITOR for undetached edits]'
'(-g)-g[regex filters]' '(-g)-g[regex filters]'
'(-H)-H[show hidden files]' '(-H)-H[show hidden files]'
@ -25,6 +24,7 @@ args=(
'(-Q)-Q[disable quit confirmation]' '(-Q)-Q[disable quit confirmation]'
'(-r)-r[show cp, mv progress (Linux-only)]' '(-r)-r[show cp, mv progress (Linux-only)]'
'(-R)-R[disable rollover at edges]' '(-R)-R[disable rollover at edges]'
'(-s)-s[load session]:session name'
'(-S)-S[start in disk usage analyzer mode]' '(-S)-S[start in disk usage analyzer mode]'
'(-t)-t[disable dir auto-select]' '(-t)-t[disable dir auto-select]'
'(-v)-v[show program version and exit]' '(-v)-v[show program version and exit]'

8
nnn.1
View file

@ -10,7 +10,6 @@
.Op Ar -b key .Op Ar -b key
.Op Ar -c .Op Ar -c
.Op Ar -d .Op Ar -d
.Op Ar -e name
.Op Ar -E .Op Ar -E
.Op Ar -g .Op Ar -g
.Op Ar -H .Op Ar -H
@ -21,6 +20,7 @@
.Op Ar -Q .Op Ar -Q
.Op Ar -r .Op Ar -r
.Op Ar -R .Op Ar -R
.Op Ar -s name
.Op Ar -S .Op Ar -S
.Op Ar -v .Op Ar -v
.Op Ar -x .Op Ar -x
@ -56,9 +56,6 @@ supports the following options:
.Fl d .Fl d
detail mode detail mode
.Pp .Pp
.Fl "e name"
load a session by name
.Pp
.Fl E .Fl E
use $EDITOR for internal undetached edits use $EDITOR for internal undetached edits
.Pp .Pp
@ -92,6 +89,9 @@ supports the following options:
.Fl R .Fl R
disable rollover at edges disable rollover at edges
.Pp .Pp
.Fl "s name"
load a session by name
.Pp
.Fl S .Fl S
start in disk usage analyzer mode start in disk usage analyzer mode
.Pp .Pp

102
src/nnn.c
View file

@ -321,7 +321,6 @@ static char *initpath;
static char *cfgdir; static char *cfgdir;
static char *g_selpath; static char *g_selpath;
static char *plugindir; static char *plugindir;
static char *sessiondir;
static char *pnamebuf, *pselbuf; static char *pnamebuf, *pselbuf;
static struct entry *dents; static struct entry *dents;
static blkcnt_t ent_blocks; static blkcnt_t ent_blocks;
@ -331,8 +330,6 @@ static kv bookmark[BM_MAX];
static kv plug[PLUGIN_MAX]; static kv plug[PLUGIN_MAX];
static uchar g_tmpfplen; static uchar g_tmpfplen;
static uchar blk_shift = BLK_SHIFT_512; static uchar blk_shift = BLK_SHIFT_512;
static bool interrupted = FALSE;
static bool rangesel = FALSE;
/* Retain old signal handlers */ /* Retain old signal handlers */
#ifdef __linux__ #ifdef __linux__
@ -353,8 +350,14 @@ static char g_tmpfpath[TMP_LEN_MAX] __attribute__ ((aligned));
/* Buffer to store plugins control pipe location */ /* Buffer to store plugins control pipe location */
static char g_pipepath[TMP_LEN_MAX] __attribute__ ((aligned)); static char g_pipepath[TMP_LEN_MAX] __attribute__ ((aligned));
/* MISC NON-PERSISTENT INTERNAL BINARY STATES */
/* Plugin control initialization status */ /* Plugin control initialization status */
static bool g_plinit = FALSE; #define STATE_PLUGIN_INIT 0x1
#define STATE_INTERRUPTED 0x2
#define STATE_RANGESEL 0x4
static uchar g_states;
/* Options to identify file mime */ /* Options to identify file mime */
#if defined(__APPLE__) #if defined(__APPLE__)
@ -531,6 +534,7 @@ static const char * const env_cfg[] = {
#define ENV_EDITOR 2 #define ENV_EDITOR 2
#define ENV_PAGER 3 #define ENV_PAGER 3
#define ENV_NCUR 4 #define ENV_NCUR 4
#define DIR_SESSIONS 5
static const char * const envs[] = { static const char * const envs[] = {
"SHELL", "SHELL",
@ -538,6 +542,7 @@ static const char * const envs[] = {
"EDITOR", "EDITOR",
"PAGER", "PAGER",
"nnn", "nnn",
"/sessions",
}; };
#ifdef __linux__ #ifdef __linux__
@ -604,7 +609,7 @@ static void sigint_handler(int sig)
{ {
(void) sig; (void) sig;
interrupted = TRUE; g_states |= STATE_INTERRUPTED;
} }
static uint xatoi(const char *str) static uint xatoi(const char *str)
@ -2824,9 +2829,17 @@ static void savecurctx(settings *curcfg, char *path, char *curname, int r /* nex
*curcfg = cfg; *curcfg = cfg;
} }
static void save_session(bool last_session, int *presel) static void makesessionpath(char *spath, const char *sname)
{
size_t r = mkpath(cfgdir, envs[DIR_SESSIONS] + 1 /* begins with '/' */, spath);
spath[r - 1] = '/';
xstrlcpy(spath + r, sname, PATH_MAX - r);
}
static void save_session(bool last_session, int *presel, char *spath)
{ {
char spath[PATH_MAX];
int i; int i;
session_header_t header; session_header_t header;
FILE *fsession; FILE *fsession;
@ -2850,7 +2863,8 @@ static void save_session(bool last_session, int *presel)
sname = !last_session ? xreadline(NULL, messages[MSG_SSN_NAME]) : "@"; sname = !last_session ? xreadline(NULL, messages[MSG_SSN_NAME]) : "@";
if (!sname[0]) if (!sname[0])
return; return;
mkpath(sessiondir, sname, spath);
makesessionpath(spath, sname);
fsession = fopen(spath, "wb"); fsession = fopen(spath, "wb");
if (!fsession) { if (!fsession) {
@ -2884,9 +2898,8 @@ END:
printwait(messages[MSG_FAILED], presel); printwait(messages[MSG_FAILED], presel);
} }
static bool load_session(const char *sname, char **path, char **lastdir, char **lastname, bool restore) static bool load_session(const char *sname, char **path, char **lastdir, char **lastname, char *spath, bool restore)
{ {
char spath[PATH_MAX];
int i = 0; int i = 0;
session_header_t header; session_header_t header;
FILE *fsession; FILE *fsession;
@ -2897,13 +2910,13 @@ static bool load_session(const char *sname, char **path, char **lastdir, char **
sname = sname ? sname : xreadline(NULL, messages[MSG_SSN_NAME]); sname = sname ? sname : xreadline(NULL, messages[MSG_SSN_NAME]);
if (!sname[0]) if (!sname[0])
return FALSE; return FALSE;
}
mkpath(sessiondir, sname, spath); /* Save current session */
} else
mkpath(sessiondir, "@", spath);
if (has_loaded_dynamically) if (has_loaded_dynamically)
save_session(TRUE, NULL); save_session(TRUE, NULL, spath);
makesessionpath(spath, (!restore ? sname : "@"));
fsession = fopen(spath, "rb"); fsession = fopen(spath, "rb");
if (!fsession) { if (!fsession) {
@ -2945,7 +2958,8 @@ END:
if (!status) { if (!status) {
printmsg(messages[MSG_FAILED]); printmsg(messages[MSG_FAILED]);
xdelay(XDELAY_INTERVAL_MS); xdelay(XDELAY_INTERVAL_MS);
} } else if (restore)
unlink(spath);
return status; return status;
} }
@ -3474,7 +3488,7 @@ static void show_help(const char *path)
"6(Sh)Tab Cycle context%-11cd Detail view toggle\n" "6(Sh)Tab Cycle context%-11cd Detail view toggle\n"
"c/ Filter%-13cIns ^N Nav-as-you-type toggle\n" "c/ Filter%-13cIns ^N Nav-as-you-type toggle\n"
"aEsc Exit prompt%-9c^L F5 Redraw/clear prompt\n" "aEsc Exit prompt%-9c^L F5 Redraw/clear prompt\n"
"c. Hidden toggle%-11c? Help, conf\n" "c. Toggle hidden%-11c? Help, conf\n"
"9Q ^Q Quit%-20cq Quit context\n" "9Q ^Q Quit%-20cq Quit context\n"
"b^G QuitCD%-1c\n" "b^G QuitCD%-1c\n"
"1FILES\n" "1FILES\n"
@ -3606,9 +3620,9 @@ static bool run_selected_plugin(char **path, const char *file, char *newpath, ch
if (*file == '_') if (*file == '_')
return run_cmd_as_plugin(*path, file, newpath, runfile); return run_cmd_as_plugin(*path, file, newpath, runfile);
if (!g_plinit) { if (!(g_states & STATE_PLUGIN_INIT)) {
plctrl_init(); plctrl_init();
g_plinit = TRUE; g_states |= STATE_PLUGIN_INIT;
} }
fd = open(g_pipepath, O_RDONLY | O_NONBLOCK); fd = open(g_pipepath, O_RDONLY | O_NONBLOCK);
@ -3795,7 +3809,7 @@ static int dentfill(char *path, struct entry **dents)
dir_blocks += dirwalk(buf, &sb); dir_blocks += dirwalk(buf, &sb);
if (interrupted) { if (g_states & STATE_INTERRUPTED) {
closedir(dirp); closedir(dirp);
return n; return n;
} }
@ -3892,7 +3906,7 @@ static int dentfill(char *path, struct entry **dents)
else else
num_files = num_saved; num_files = num_saved;
if (interrupted) { if (g_states & STATE_INTERRUPTED) {
closedir(dirp); closedir(dirp);
return n; return n;
} }
@ -4188,7 +4202,7 @@ static void redraw(char *path)
mvprintw(lastln, 0, "%d/%d [%d:%s] %cu:%s free:%s files:%lu %lldB %s", mvprintw(lastln, 0, "%d/%d [%d:%s] %cu:%s free:%s files:%lu %lldB %s",
cur + 1, ndents, cfg.selmode, cur + 1, ndents, cfg.selmode,
(rangesel ? "*" : (nselected ? xitoa(nselected) : "")), ((g_states & STATE_RANGESEL) ? "*" : (nselected ? xitoa(nselected) : "")),
c, buf, coolsize(get_fs_info(path, FREE)), num_files, c, buf, coolsize(get_fs_info(path, FREE)), num_files,
(ll)pent->blocks << blk_shift, ptr); (ll)pent->blocks << blk_shift, ptr);
} else { /* light or detail mode */ } else { /* light or detail mode */
@ -4202,7 +4216,7 @@ static void redraw(char *path)
mvprintw(lastln, 0, "%d/%d [%d:%s] %s%s %s %s %s [%s]", mvprintw(lastln, 0, "%d/%d [%d:%s] %s%s %s %s %s [%s]",
cur + 1, ndents, cfg.selmode, cur + 1, ndents, cfg.selmode,
(rangesel ? "*" : (nselected ? xitoa(nselected) : "")), ((g_states & STATE_RANGESEL) ? "*" : (nselected ? xitoa(nselected) : "")),
sort, buf, get_lsperms(pent->mode), coolsize(pent->size), ptr, base); sort, buf, get_lsperms(pent->mode), coolsize(pent->size), ptr, base);
} }
} else } else
@ -4231,7 +4245,7 @@ static void browse(char *ipath, const char *session)
xcols = COLS; xcols = COLS;
/* setup first context */ /* setup first context */
if (!session || !load_session(session, &path, &lastdir, &lastname, FALSE)) { if (!session || !load_session(session, &path, &lastdir, &lastname, newpath, FALSE)) {
xstrlcpy(g_ctx[0].c_path, ipath, PATH_MAX); /* current directory */ xstrlcpy(g_ctx[0].c_path, ipath, PATH_MAX); /* current directory */
path = g_ctx[0].c_path; path = g_ctx[0].c_path;
g_ctx[0].c_last[0] = g_ctx[0].c_name[0] = '\0'; g_ctx[0].c_last[0] = g_ctx[0].c_name[0] = '\0';
@ -4279,8 +4293,8 @@ begin:
printwarn(&presel); printwarn(&presel);
populate(path, lastname); populate(path, lastname);
if (interrupted) { if (g_states & STATE_INTERRUPTED) {
interrupted = FALSE; g_states &= ~STATE_INTERRUPTED;
cfg.apparentsz = 0; cfg.apparentsz = 0;
cfg.blkorder = 0; cfg.blkorder = 0;
blk_shift = BLK_SHIFT_512; blk_shift = BLK_SHIFT_512;
@ -4869,8 +4883,8 @@ nochange:
goto nochange; goto nochange;
startselection(); startselection();
if (rangesel) if (g_states & STATE_RANGESEL)
rangesel = FALSE; g_states &= ~STATE_RANGESEL;
/* Toggle selection status */ /* Toggle selection status */
dents[cur].flags ^= FILE_SELECTED; dents[cur].flags ^= FILE_SELECTED;
@ -4903,14 +4917,14 @@ nochange:
goto nochange; goto nochange;
startselection(); startselection();
rangesel ^= TRUE; g_states ^= STATE_RANGESEL;
if (stat(path, &sb) == -1) { if (stat(path, &sb) == -1) {
printwarn(&presel); printwarn(&presel);
goto nochange; goto nochange;
} }
if (rangesel) { /* Range selection started */ if (g_states & STATE_RANGESEL) { /* Range selection started */
inode = sb.st_ino; inode = sb.st_ino;
selstartid = cur; selstartid = cur;
continue; continue;
@ -4940,8 +4954,8 @@ nochange:
goto nochange; goto nochange;
startselection(); startselection();
if (rangesel) if (g_states & STATE_RANGESEL)
rangesel = FALSE; g_states &= ~STATE_RANGESEL;
selstartid = 0; selstartid = 0;
selendid = ndents - 1; selendid = ndents - 1;
@ -5348,12 +5362,13 @@ nochange:
r = get_input(messages[MSG_SSN_OPTS]); r = get_input(messages[MSG_SSN_OPTS]);
if (r == 's') { if (r == 's') {
save_session(FALSE, &presel); save_session(FALSE, &presel, newpath);
goto nochange; goto nochange;
} }
if (r == 'l' || r == 'r') { if (r == 'l' || r == 'r') {
if (load_session(NULL, &path, &lastdir, &lastname, r == 'r')) { if (load_session(NULL, &path, &lastdir,
&lastname, newpath, r == 'r')) {
setdirwatch(); setdirwatch();
goto begin; goto begin;
} }
@ -5469,7 +5484,6 @@ static void usage(void)
" -b key open bookmark key\n" " -b key open bookmark key\n"
" -c cli-only opener\n" " -c cli-only opener\n"
" -d detail mode\n" " -d detail mode\n"
" -e name load session by name\n"
" -E use EDITOR for undetached edits\n" " -E use EDITOR for undetached edits\n"
" -g regex filters [default: string]\n" " -g regex filters [default: string]\n"
" -H show hidden files\n" " -H show hidden files\n"
@ -5481,6 +5495,7 @@ static void usage(void)
" -Q no quit confirmation\n" " -Q no quit confirmation\n"
" -r use advcpmv patched cp, mv\n" " -r use advcpmv patched cp, mv\n"
" -R no rollover at edges\n" " -R no rollover at edges\n"
" -s name load session by name\n"
" -S du mode\n" " -S du mode\n"
" -t no dir auto-select\n" " -t no dir auto-select\n"
" -v show version\n" " -v show version\n"
@ -5519,8 +5534,7 @@ static bool setup_config(void)
cfgdir = (char *)malloc(len); cfgdir = (char *)malloc(len);
plugindir = (char *)malloc(len); plugindir = (char *)malloc(len);
sessiondir = (char *)malloc(len); if (!cfgdir || !plugindir) {
if (!cfgdir || !plugindir || !sessiondir) {
xerror(); xerror();
return FALSE; return FALSE;
} }
@ -5554,12 +5568,9 @@ static bool setup_config(void)
} }
/* Create ~/.config/nnn/sessions */ /* Create ~/.config/nnn/sessions */
xstrlcpy(cfgdir + r + 4 - 1, "/sessions", 10); /* subtract length of "/nnn" (4) */ xstrlcpy(cfgdir + r + 4 - 1, envs[DIR_SESSIONS], 10); /* subtract length of "/nnn" (4) */
DPRINTF_S(cfgdir); DPRINTF_S(cfgdir);
xstrlcpy(sessiondir, cfgdir, len);
DPRINTF_S(sessiondir);
if (!xmktree(cfgdir, TRUE)) { if (!xmktree(cfgdir, TRUE)) {
xerror(); xerror();
return FALSE; return FALSE;
@ -5604,7 +5615,6 @@ static void cleanup(void)
{ {
free(g_selpath); free(g_selpath);
free(plugindir); free(plugindir);
free(sessiondir);
free(cfgdir); free(cfgdir);
free(initpath); free(initpath);
free(bmstr); free(bmstr);
@ -5627,7 +5637,7 @@ int main(int argc, char *argv[])
bool progress = FALSE; bool progress = FALSE;
#endif #endif
while ((opt = getopt(argc, argv, "HSKiab:cde:Egnop:QrRtvxh")) != -1) { while ((opt = getopt(argc, argv, "HSKiab:cdEgnop:QrRs:tvxh")) != -1) {
switch (opt) { switch (opt) {
case 'S': case 'S':
cfg.blkorder = 1; cfg.blkorder = 1;
@ -5649,9 +5659,6 @@ int main(int argc, char *argv[])
case 'c': case 'c':
cfg.cliopener = 1; cfg.cliopener = 1;
break; break;
case 'e':
session = optarg;
break;
case 'E': case 'E':
cfg.waitedit = 1; cfg.waitedit = 1;
break; break;
@ -5696,6 +5703,9 @@ int main(int argc, char *argv[])
case 'R': case 'R':
cfg.rollover = 0; cfg.rollover = 0;
break; break;
case 's':
session = optarg;
break;
case 't': case 't':
cfg.autoselect = 0; cfg.autoselect = 0;
break; break;