Fix #1023: support named persistent settions

This commit is contained in:
Arun Prakash Jana 2021-05-20 09:56:55 +05:30
parent 36e1544b32
commit 54d760b9f5
No known key found for this signature in database
GPG key ID: A75979F35C080412
2 changed files with 28 additions and 25 deletions

25
nnn.1
View file

@ -170,23 +170,26 @@ A new context copies the state of the previous context. Each context can have
its own color. See ENVIRONMENT section. its own color. See ENVIRONMENT section.
.Sh SESSIONS .Sh SESSIONS
Sessions are a way to save and restore states of work. A session stores the Sessions are a way to save and restore states of work. A session stores the
settings and contexts. settings and contexts. Sessions can be loaded at runtime or with a program
option.
.Pp .Pp
Sessions can be loaded dynamically at runtime or with a program option. - When a session is loaded at runtime, the last working state is saved
.Pp automatically to a dedicated "auto session" session file. Session option
When a session is loaded dynamically, the last working session is saved \fIrestore\fR would restore the "auto session".
automatically to a dedicated -- "last session" -- session file. The "last .br
session" is also used in persistent session mode. - The persistent session option is global. If it is used, the last active session
.Pp will be updated with the final state at program quit.
Listing input stream has a higher priority to session options (-s/-S). Sessions .br
can be loaded explicitly at runtime. Session option \fIrestore\fR would restore - The "auto session" is used in persistent session mode if no session is active.
the persistent session at runtime. .br
- Listing input stream and opening a bookmark by key have a higher priority to
session options (-s/-S).
.Pp .Pp
All the session files are located by session name in the directory All the session files are located by session name in the directory
.Pp .Pp
\fB${XDG_CONFIG_HOME:-$HOME/.config}/nnn/sessions\fR \fB${XDG_CONFIG_HOME:-$HOME/.config}/nnn/sessions\fR
.Pp .Pp
"@" is the "last session" file. "@" is the "auto session" file.
.Sh FILTERS .Sh FILTERS
Filters are strings (or regex patterns) to find matching entries in the current Filters are strings (or regex patterns) to find matching entries in the current
directory instantly (\fIsearch-as-you-type\fR). Matches are case-insensitive by directory instantly (\fIsearch-as-you-type\fR). Matches are case-insensitive by

View file

@ -335,7 +335,8 @@ typedef struct {
uint_t stayonsel : 1; /* Disable auto-proceed on select */ uint_t stayonsel : 1; /* Disable auto-proceed on select */
uint_t dirctx : 1; /* Show dirs in context color */ uint_t dirctx : 1; /* Show dirs in context color */
uint_t uidgid : 1; /* Show owner and group info */ uint_t uidgid : 1; /* Show owner and group info */
uint_t reserved : 9; /* Adjust when adding/removing a field */ uint_t prstssn : 1; /* Persistent session */
uint_t reserved : 8; /* Adjust when adding/removing a field */
} runstate; } runstate;
/* Contexts or workspaces */ /* Contexts or workspaces */
@ -3851,12 +3852,11 @@ static void savecurctx(settings *curcfg, char *path, char *curname, int nextctx)
} }
#ifndef NOSSN #ifndef NOSSN
static void save_session(bool last_session, int *presel) static void save_session(const char *sname, int *presel)
{ {
int i; int i;
session_header_t header; session_header_t header;
FILE *fsession; FILE *fsession;
char *sname;
bool status = FALSE; bool status = FALSE;
char ssnpath[PATH_MAX]; char ssnpath[PATH_MAX];
char spath[PATH_MAX]; char spath[PATH_MAX];
@ -3877,10 +3877,6 @@ static void save_session(bool last_session, int *presel)
} }
} }
sname = !last_session ? xreadline(NULL, messages[MSG_SSN_NAME]) : "@";
if (!sname[0])
return;
mkpath(cfgpath, toks[TOK_SSN], ssnpath); mkpath(cfgpath, toks[TOK_SSN], ssnpath);
mkpath(ssnpath, sname, spath); mkpath(ssnpath, sname, spath);
@ -3942,7 +3938,7 @@ static bool load_session(const char *sname, char **path, char **lastdir, char **
mkpath(ssnpath, "@", spath); mkpath(ssnpath, "@", spath);
if (has_loaded_dynamically) if (has_loaded_dynamically)
save_session(TRUE, NULL); save_session("@", NULL);
fsession = fopen(spath, "rb"); fsession = fopen(spath, "rb");
if (!fsession) { if (!fsession) {
@ -7128,9 +7124,11 @@ nochange:
case SEL_SESSIONS: case SEL_SESSIONS:
r = get_input(messages[MSG_SSN_OPTS]); r = get_input(messages[MSG_SSN_OPTS]);
if (r == 's') if (r == 's') {
save_session(FALSE, &presel); tmp = xreadline(NULL, messages[MSG_SSN_NAME]);
else if (r == 'l' || r == 'r') { if (tmp && *tmp)
save_session(FALSE, &presel);
} else if (r == 'l' || r == 'r') {
if (load_session(NULL, &path, &lastdir, &lastname, r == 'r')) { if (load_session(NULL, &path, &lastdir, &lastname, r == 'r')) {
setdirwatch(); setdirwatch();
goto begin; goto begin;
@ -7186,8 +7184,8 @@ nochange:
} }
#ifndef NOSSN #ifndef NOSSN
if (session && *session == '@' && !session[1]) if (session && g_state.prstssn)
save_session(TRUE, NULL); save_session(session, NULL);
#endif #endif
/* CD on Quit */ /* CD on Quit */
@ -7768,7 +7766,9 @@ int main(int argc, char *argv[])
session = optarg; session = optarg;
break; break;
case 'S': case 'S':
session = "@"; g_state.prstssn = 1;
if (!session) /* Support named persistent sessions */
session = "@";
break; break;
#endif #endif
case 't': case 't':