commit 8a5b4f616be7a287268614c5bbecb3dd702d0d0e
parent 9cbe6a640c7cc0cd9c2c029083c942aff980b0a3
Author: Rishabh Dave <rishabhddave@gmail.com>
Date: Tue, 10 May 2016 16:40:01 +0200
browser: keep the same file selected when the directory contents change
When refreshing the screen (and thus the file list), use the prev_dir
mechanism to reselect the file that was selected before the refresh,
to prevent the selection from changing when files were added or deleted.
Also, when the selected name has disappeared, move the highlight one step
back, so that it is obvious that the selection has changed. (Decrementing
'selected' will never make it negative, because selected == 0 means the
'..' entry, and every directory has a '..' entry, so it will be found.)
This fixes https://savannah.gnu.org/bugs/?47812.
Signed-off-by: Rishabh Dave <rishabhddave@gmail.com
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
Diffstat:
1 file changed, 18 insertions(+), 3 deletions(-)
diff --git a/src/browser.c b/src/browser.c
@@ -65,7 +65,7 @@ char *do_browser(char *path, DIR *dir)
UNSET(CONST_UPDATE);
change_browser_directory:
- /* We go here after we select a new directory. */
+ /* We come here when we refresh or select a new directory. */
/* Start with no key pressed. */
kbinput = ERR;
@@ -174,8 +174,18 @@ char *do_browser(char *path, DIR *dir)
if (func == total_refresh) {
total_redraw();
- /* Simulate a window resize. */
- kbinput = KEY_WINCH;
+
+ /* Remember the selected file, to be able to reselect it. */
+ prev_dir = strdup(filelist[selected]);
+
+ /* Reopen the current directory. */
+ dir = opendir(path);
+ if (dir == NULL) {
+ statusbar(_("Error reading %s: %s"), path, strerror(errno));
+ beep();
+ continue;
+ }
+ goto change_browser_directory;
} else if (func == do_help_void) {
#ifndef DISABLE_HELP
do_help_void();
@@ -684,6 +694,11 @@ void browser_select_dirname(const char *needle)
break;
}
}
+
+ /* If the sought name isn't found, move the highlight so that the
+ * changed selection will be noticed. */
+ if (looking_at == filelist_len)
+ --selected;
}
/* Set up the system variables for a filename search. Return -1 or -2 if