Refactor change directory

This commit is contained in:
Arun Prakash Jana 2020-03-29 17:25:11 +05:30
parent 6ea33f1db3
commit d2e83b2cfe
No known key found for this signature in database
GPG key ID: A75979F35C080412
2 changed files with 57 additions and 92 deletions

143
src/nnn.c
View file

@ -675,7 +675,7 @@ static haiku_nm_h haiku_hnd;
#define settimeout() timeout(1000) #define settimeout() timeout(1000)
#define cleartimeout() timeout(-1) #define cleartimeout() timeout(-1)
#define errexit() printerr(__LINE__) #define errexit() printerr(__LINE__)
#define setdirwatch() (cfg.filtermode ? (presel = FILTER) : (dir_changed = TRUE)) #define setdirwatch() (cfg.filtermode ? (presel = FILTER) : (watch = TRUE))
#define filterset() (g_ctx[cfg.curctx].c_fltr[1]) #define filterset() (g_ctx[cfg.curctx].c_fltr[1])
/* We don't care about the return value from strcmp() */ /* We don't care about the return value from strcmp() */
#define xstrcmp(a, b) (*(a) != *(b) ? -1 : strcmp((a), (b))) #define xstrcmp(a, b) (*(a) != *(b) ? -1 : strcmp((a), (b)))
@ -5082,6 +5082,22 @@ static void redraw(char *path)
statusbar(path); statusbar(path);
} }
static bool cdprep(char *lastdir, char *lastname, char *path, char *newpath)
{
if (lastname)
lastname[0] = '\0';
/* Save last working directory */
xstrlcpy(lastdir, path, PATH_MAX);
/* Save the newly opted dir in path */
xstrlcpy(path, newpath, PATH_MAX);
DPRINTF_S(path);
clearfilter();
return cfg.filtermode;
}
static bool browse(char *ipath, const char *session) static bool browse(char *ipath, const char *session)
{ {
char newpath[PATH_MAX] __attribute__ ((aligned)); char newpath[PATH_MAX] __attribute__ ((aligned));
@ -5092,7 +5108,7 @@ static bool browse(char *ipath, const char *session)
struct stat sb; struct stat sb;
int r = -1, presel, selstartid = 0, selendid = 0; int r = -1, presel, selstartid = 0, selendid = 0;
const uchar opener_flags = (cfg.cliopener ? F_CLI : (F_NOTRACE | F_NOWAIT)); const uchar opener_flags = (cfg.cliopener ? F_CLI : (F_NOTRACE | F_NOWAIT));
bool dir_changed = FALSE; bool watch = FALSE;
#ifndef NOMOUSE #ifndef NOMOUSE
MEVENT event; MEVENT event;
@ -5148,22 +5164,22 @@ begin:
lastappendpos = selbufpos; lastappendpos = selbufpos;
#ifdef LINUX_INOTIFY #ifdef LINUX_INOTIFY
if ((presel == FILTER || dir_changed) && inotify_wd >= 0) { if ((presel == FILTER || watch) && inotify_wd >= 0) {
inotify_rm_watch(inotify_fd, inotify_wd); inotify_rm_watch(inotify_fd, inotify_wd);
inotify_wd = -1; inotify_wd = -1;
dir_changed = FALSE; watch = FALSE;
} }
#elif defined(BSD_KQUEUE) #elif defined(BSD_KQUEUE)
if ((presel == FILTER || dir_changed) && event_fd >= 0) { if ((presel == FILTER || watch) && event_fd >= 0) {
close(event_fd); close(event_fd);
event_fd = -1; event_fd = -1;
dir_changed = FALSE; watch = FALSE;
} }
#elif defined(HAIKU_NM) #elif defined(HAIKU_NM)
if ((presel == FILTER || dir_changed) && haiku_hnd != NULL) { if ((presel == FILTER || watch) && haiku_hnd != NULL) {
haiku_stop_watch(haiku_hnd); haiku_stop_watch(haiku_hnd);
haiku_nm_active = FALSE; haiku_nm_active = FALSE;
dir_changed = FALSE; watch = FALSE;
} }
#endif #endif
@ -5266,16 +5282,10 @@ nochange:
if (!dir) if (!dir)
goto nochange; goto nochange;
/* Save last working directory */
xstrlcpy(lastdir, path, PATH_MAX);
/* Save history */ /* Save history */
xstrlcpy(lastname, xbasename(path), NAME_MAX + 1); xstrlcpy(lastname, xbasename(path), NAME_MAX + 1);
clearfilter();
xstrlcpy(path, dir, PATH_MAX); cdprep(lastdir, NULL, path, dir) ? (presel = FILTER) : (watch = TRUE);
setdirwatch();
goto begin; goto begin;
#ifndef NOMOUSE #ifndef NOMOUSE
} }
@ -5312,7 +5322,7 @@ nochange:
} }
/* Start watching the directory */ /* Start watching the directory */
dir_changed = TRUE; watch = TRUE;
if (ndents) if (ndents)
copycurname(); copycurname();
@ -5382,13 +5392,8 @@ nochange:
goto nochange; goto nochange;
} }
/* Save last working directory */ cdprep(lastdir, lastname, path, newpath)
xstrlcpy(lastdir, path, PATH_MAX); ? (presel = FILTER) : (watch = TRUE);
xstrlcpy(path, newpath, PATH_MAX);
lastname[0] = '\0';
clearfilter();
setdirwatch();
goto begin; goto begin;
case S_IFREG: case S_IFREG:
{ {
@ -5423,7 +5428,6 @@ nochange:
if (runfile[0]) if (runfile[0])
runfile[0] = '\0'; runfile[0] = '\0';
clearfilter(); clearfilter();
setdirwatch(); setdirwatch();
goto begin; goto begin;
} }
@ -5457,26 +5461,20 @@ nochange:
if (r == 'l' || r == 'x') { if (r == 'l' || r == 'x') {
mkpath(path, dents[cur].name, newpath); mkpath(path, dents[cur].name, newpath);
handle_archive(newpath, path, r); handle_archive(newpath, path, r);
copycurname();
if (r == 'l') { if (r == 'l') {
statusbar(path); statusbar(path);
goto nochange; goto nochange;
} }
copycurname();
clearfilter();
goto begin; goto begin;
} }
if (r == 'm') { if (r == 'm') {
if (archive_mount(dents[cur].name, if (archive_mount(dents[cur].name,
path, newpath, &presel)) { path, newpath, &presel)) {
lastname[0] = '\0'; cdprep(lastdir, lastname, path, newpath)
? (presel = FILTER) : (watch = TRUE);
/* Save last working directory */
xstrlcpy(lastdir, path, PATH_MAX);
/* Switch to mount point */
xstrlcpy(path, newpath, PATH_MAX);
setdirwatch();
goto begin; goto begin;
} }
@ -5549,38 +5547,23 @@ nochange:
} }
/* SEL_CDLAST: dir pointing to lastdir */ /* SEL_CDLAST: dir pointing to lastdir */
xstrlcpy(newpath, dir, PATH_MAX); xstrlcpy(newpath, dir, PATH_MAX); // fallthrough
/* Save last working directory */
xstrlcpy(lastdir, path, PATH_MAX);
xstrlcpy(path, newpath, PATH_MAX);
lastname[0] = '\0';
clearfilter();
DPRINTF_S(path);
setdirwatch();
goto begin;
case SEL_BOOKMARK: case SEL_BOOKMARK:
r = (int)handle_bookmark(mark, newpath); if (sel == SEL_BOOKMARK) {
if (r) { r = (int)handle_bookmark(mark, newpath);
printwait(messages[r], &presel); if (r) {
printwait(messages[r], &presel);
goto nochange;
}
if (strcmp(path, newpath) == 0)
break;
} // fallthrough
case SEL_REMOTE:
if (sel == SEL_REMOTE && !remote_mount(newpath, &presel))
goto nochange; goto nochange;
}
if (strcmp(path, newpath) == 0) cdprep(lastdir, lastname, path, newpath) ? (presel = FILTER) : (watch = TRUE);
break;
lastname[0] = '\0';
clearfilter();
/* Save last working directory */
xstrlcpy(lastdir, path, PATH_MAX);
/* Save the newly opted dir in path */
xstrlcpy(path, newpath, PATH_MAX);
DPRINTF_S(path);
setdirwatch();
goto begin; goto begin;
case SEL_CYCLE: // fallthrough case SEL_CYCLE: // fallthrough
case SEL_CYCLER: // fallthrough case SEL_CYCLER: // fallthrough
@ -5602,7 +5585,7 @@ nochange:
if (cfg.filtermode || ((tmp[0] == FILTER || tmp[0] == RFILTER) && tmp[1])) if (cfg.filtermode || ((tmp[0] == FILTER || tmp[0] == RFILTER) && tmp[1]))
presel = FILTER; presel = FILTER;
else else
dir_changed = TRUE; watch = TRUE;
goto begin; goto begin;
case SEL_PIN: case SEL_PIN:
@ -5648,20 +5631,16 @@ nochange:
goto nochange; goto nochange;
} }
/* Start watching the directory */ watch = TRUE; // fallthrough
dir_changed = TRUE;
/* Save current */
if (ndents)
copycurname();
goto begin;
case SEL_HIDDEN: case SEL_HIDDEN:
cfg.showhidden ^= 1; if (sel == SEL_HIDDEN) {
cfg.showhidden ^= 1;
if (cfg.filtermode)
presel = FILTER;
clearfilter();
}
if (ndents) if (ndents)
copycurname(); copycurname();
if (cfg.filtermode)
presel = FILTER;
clearfilter();
goto begin; goto begin;
case SEL_DETAIL: case SEL_DETAIL:
cfg.showdetail ^= 1; cfg.showdetail ^= 1;
@ -6247,20 +6226,6 @@ nochange:
/* Repopulate as directory content may have changed */ /* Repopulate as directory content may have changed */
goto begin; goto begin;
case SEL_REMOTE:
if (sel == SEL_REMOTE && !remote_mount(newpath, &presel))
goto nochange;
lastname[0] = '\0';
/* Save last working directory */
xstrlcpy(lastdir, path, PATH_MAX);
/* Switch to mount point */
xstrlcpy(path, newpath, PATH_MAX);
setdirwatch();
goto begin;
case SEL_UMOUNT: case SEL_UMOUNT:
tmp = ndents ? dents[cur].name : NULL; tmp = ndents ? dents[cur].name : NULL;
unmount(tmp, newpath, &presel, path); unmount(tmp, newpath, &presel, path);

View file

@ -53,6 +53,7 @@ enum action {
SEL_CDLAST, SEL_CDLAST,
SEL_CDROOT, SEL_CDROOT,
SEL_BOOKMARK, SEL_BOOKMARK,
SEL_REMOTE,
SEL_CYCLE, SEL_CYCLE,
SEL_CYCLER, SEL_CYCLER,
SEL_CTX1, SEL_CTX1,
@ -81,7 +82,6 @@ enum action {
SEL_NEW, SEL_NEW,
SEL_RENAME, SEL_RENAME,
SEL_RENAMEMUL, SEL_RENAMEMUL,
SEL_REMOTE,
SEL_UMOUNT, SEL_UMOUNT,
SEL_HELP, SEL_HELP,
SEL_AUTONEXT, SEL_AUTONEXT,
@ -154,6 +154,8 @@ static struct key bindings[] = {
/* Leader key */ /* Leader key */
{ 'b', SEL_BOOKMARK }, { 'b', SEL_BOOKMARK },
{ CONTROL('_'), SEL_BOOKMARK }, { CONTROL('_'), SEL_BOOKMARK },
/* Connect to server over SSHFS */
{ 'c', SEL_REMOTE },
/* Cycle contexts in forward direction */ /* Cycle contexts in forward direction */
{ '\t', SEL_CYCLE }, { '\t', SEL_CYCLE },
/* Cycle contexts in reverse direction */ /* Cycle contexts in reverse direction */
@ -217,8 +219,6 @@ static struct key bindings[] = {
{ CONTROL('R'), SEL_RENAME }, { CONTROL('R'), SEL_RENAME },
/* Rename contents of current dir */ /* Rename contents of current dir */
{ 'r', SEL_RENAMEMUL }, { 'r', SEL_RENAMEMUL },
/* Connect to server over SSHFS */
{ 'c', SEL_REMOTE },
/* Disconnect a SSHFS mount point */ /* Disconnect a SSHFS mount point */
{ 'u', SEL_UMOUNT }, { 'u', SEL_UMOUNT },
/* Show help */ /* Show help */