diff --git a/src/browser.c b/src/browser.c index e688e4d..6f82c5b 100644 --- a/src/browser.c +++ b/src/browser.c @@ -108,6 +108,7 @@ char *do_browser(char *path, DIR *dir) char *new_path; /* The path we switch to at the "Go to Directory" * prompt. */ + size_t filelistlen_old = filelist_len; /* Make sure that the cursor is off. */ curs_set(0); @@ -118,9 +119,14 @@ char *do_browser(char *path, DIR *dir) browser_init(present_path, opendir(present_path)); qsort(filelist, filelist_len, sizeof(char *), diralphasort); - /* Make sure the selected file is within range. */ - if (selected >= filelist_len) - selected = filelist_len - 1; + /* Keep selection even if number of file in cwd changes. */ + if (filelistlen_old < filelist_len) + selected += filelist_len - filelistlen_old; + else if (filelistlen_old > filelist_len) + selected -= filelistlen_old - filelist_len; + else { + /* There is nothing to be done. */ + } kbinput = ERR; }