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>
master
Rishabh Dave 2016-05-10 16:40:01 +02:00 committed by Benno Schulenberg
parent 9cbe6a640c
commit 8a5b4f616b
1 changed files with 18 additions and 3 deletions

View File

@ -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