Commit Diff


commit - 430cd7d8a26300453a7617806e1e4ed1a5e15558
commit + 00ecdc1e9fb94ee869a04bb3789c80f9e0a7c04d
blob - fa83d0bc7b3be73b8676a63cc90e313fbc9e4f03
blob + b1ed2221373b5bdbd53c804a49ae619caa161b74
--- rover.c
+++ rover.c
@@ -432,7 +432,7 @@ static void
 try_to_sel(const char *target)
 {
     ESEL = 0;
-    while (strcoll(ENAME(ESEL), target) < 0)
+    while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
         ESEL++;
     if (rover.nfiles > HEIGHT) {
         SCROLL = ESEL - (HEIGHT >> 1);
@@ -440,6 +440,19 @@ try_to_sel(const char *target)
     }
 }
 
+/* Reload CWD, but try to keep selection. */
+static void
+reload()
+{
+    if (rover.nfiles) {
+        strcpy(INPUT, ENAME(ESEL));
+        cd(1);
+        try_to_sel(INPUT);
+        update_view();
+    }
+    else cd(1);
+}
+
 /* Recursively process a source directory using CWD as destination root.
  * For each node (i.e. directory), do the following:
  *  1. call pre(destination);
@@ -509,10 +522,7 @@ process_marked(PROCESS pre, PROCESS proc, PROCESS pos)
             else ret = proc(path);
             if (!ret) del_mark(&rover.marks, rover.marks.entries[i]);
         }
-    strcpy(INPUT, ENAME(ESEL));
-    cd(1);
-    try_to_sel(INPUT);
-    update_view();
+    reload();
     if (!rover.marks.nentries)
         message("Done.", GREEN);
     else
@@ -749,7 +759,7 @@ main(int argc, char *argv[])
                 ARGS[0] = program;
                 ARGS[1] = NULL;
                 spawn();
-                cd(1);
+                reload();
             }
         }
         else if (!strcmp(key, RVK_VIEW)) {
@@ -814,15 +824,15 @@ main(int argc, char *argv[])
         }
         else if (!strcmp(key, RVK_TG_FILES)) {
             FLAGS ^= SHOW_FILES;
-            cd(1);
+            reload();
         }
         else if (!strcmp(key, RVK_TG_DIRS)) {
             FLAGS ^= SHOW_DIRS;
-            cd(1);
+            reload();
         }
         else if (!strcmp(key, RVK_TG_HIDDEN)) {
             FLAGS ^= SHOW_HIDDEN;
-            cd(1);
+            reload();
         }
         else if (!strcmp(key, RVK_NEW_FILE)) {
             int ok = 0;
@@ -908,7 +918,12 @@ main(int argc, char *argv[])
             }
             clear_message();
             if (strlen(INPUT)) {
-                if (ok) { rename(ENAME(ESEL), INPUT); cd(1); }
+                if (ok) {
+                    rename(ENAME(ESEL), INPUT);
+                    cd(1);
+                    try_to_sel(INPUT);
+                    update_view();
+                }
                 else message("File already exists.", RED);
             }
         }