avoid cursor jump when file under cursor is renamed by plugin/`r`

This commit is contained in:
blissful 2023-11-19 10:28:58 -05:00
parent ad04944bdf
commit fbed2f12e1
No known key found for this signature in database
GPG Key ID: 3FC5AA97AA6A4D78
1 changed files with 21 additions and 7 deletions

View File

@ -5394,7 +5394,7 @@ static char *readpipe(int fd, char *ctxnum, char **path)
return nextpath; return nextpath;
} }
static bool run_plugin(char **path, const char *file, char *runfile, char **lastname, char **lastdir) static bool run_plugin(char **path, const char *file, char *runfile, char **lastname, char **lastdir, bool *cd)
{ {
pid_t p; pid_t p;
char ctx = 0; char ctx = 0;
@ -5477,6 +5477,8 @@ static bool run_plugin(char **path, const char *file, char *runfile, char **last
nextpath = readpipe(rfd, &ctx, path); nextpath = readpipe(rfd, &ctx, path);
if (nextpath) if (nextpath)
set_smart_ctx(ctx, nextpath, path, runfile, lastname, lastdir); set_smart_ctx(ctx, nextpath, path, runfile, lastname, lastdir);
else
*cd = FALSE;
close(rfd); close(rfd);
@ -5991,7 +5993,7 @@ exit:
return ndents; return ndents;
} }
static void populate(char *path, char *lastname) static void populate(char *path, char *lastname, bool cd)
{ {
#ifdef DEBUG #ifdef DEBUG
struct timespec ts1, ts2; struct timespec ts1, ts2;
@ -6014,7 +6016,18 @@ static void populate(char *path, char *lastname)
/* Find cur from history */ /* Find cur from history */
/* No NULL check for lastname, always points to an array */ /* No NULL check for lastname, always points to an array */
move_cursor(*lastname ? dentfind(lastname, ndents) : 0, 0); int target;
if (*lastname) {
target = dentfind(lastname, ndents);
/* If we failed to find the previous filename, and we didn't change directories, */
/* default to the previous cursor position. This way, when a file is renamed by */
/* a plugin, the cursor does not jump to the top of the directory. */
if (target == 0 && !cd)
target = cur;
} else
target = 0;
move_cursor(target, 0);
// Force full redraw // Force full redraw
last_curscroll = -1; last_curscroll = -1;
@ -6887,15 +6900,16 @@ begin:
} else } else
cfgsort[cfg.curctx] = cfgsort[CTX_MAX]; cfgsort[cfg.curctx] = cfgsort[CTX_MAX];
} }
cd = TRUE;
populate(path, lastname); populate(path, lastname, cd);
if (g_state.interrupt) { if (g_state.interrupt) {
g_state.interrupt = cfg.apparentsz = cfg.blkorder = 0; g_state.interrupt = cfg.apparentsz = cfg.blkorder = 0;
blk_shift = BLK_SHIFT_512; blk_shift = BLK_SHIFT_512;
presel = CONTROL('L'); presel = CONTROL('L');
} }
cd = TRUE;
#ifdef LINUX_INOTIFY #ifdef LINUX_INOTIFY
if (presel != FILTER && inotify_wd == -1) if (presel != FILTER && inotify_wd == -1)
inotify_wd = inotify_add_watch(inotify_fd, path, INOTIFY_MASK); inotify_wd = inotify_add_watch(inotify_fd, path, INOTIFY_MASK);
@ -7122,7 +7136,7 @@ nochange:
clearfilter(); clearfilter();
if (chdir(path) == -1 if (chdir(path) == -1
|| !run_plugin(&path, pent->name, runfile, &lastname, &lastdir)) { || !run_plugin(&path, pent->name, runfile, &lastname, &lastdir, &cd)) {
DPRINTF_S("plugin failed!"); DPRINTF_S("plugin failed!");
} }
@ -7940,7 +7954,7 @@ nochange:
r = TRUE; r = TRUE;
if (!run_plugin(&path, tmp, (ndents ? pdents[cur].name : NULL), if (!run_plugin(&path, tmp, (ndents ? pdents[cur].name : NULL),
&lastname, &lastdir)) { &lastname, &lastdir, &cd)) {
printwait(messages[MSG_FAILED], &presel); printwait(messages[MSG_FAILED], &presel);
goto nochange; goto nochange;
} }