xaizek / vifm (License: GPLv2+) (since 2018-12-07)
Vifm is a file manager with curses interface, which provides Vi[m]-like environment for managing objects within file systems, extended with some useful ideas from mutt.
Commit cab43d83327a51b2cc1fbe5de21be536eeaa5f95

Fix undesired scrolling up on file removal
This has started with commit cc97740ad291d48caae8bc3989493514b3dad8ad:

Avoid views scrolling unnecessarily

That happened in response to file-system changes that add or remove
files before the first visible line.

Reimplement handling of top line from there.
Author: xaizek
Author date (UTC): 2026-11-17 16:32
Committer name: xaizek
Committer date (UTC): 2026-11-17 16:32
Parent(s): 4e9aeda705e2a1f51ec8760672b298748a92be25
Signing key: 99DC5E4DB05F6BE2
Tree: 24c070a19d23b04505f97603209ff7da70d69c99
File Lines added Lines deleted
src/filelist.c 4 7
tests/misc/flist_reload.c 43 1
File src/filelist.c changed (mode: 100644) (index a7db76ab9..738b8b43c)
... ... merge_lists(view_t *view, dir_entry_t *entries, int len)
2447 2447 { {
2448 2448 int i; int i;
2449 2449 const int prev_pos = view->list_pos; const int prev_pos = view->list_pos;
2450 const int top_delta = view->list_pos - view->top_line;
2450 const int prev_top = view->top_line;
2451 2451 trie_t *prev_names = trie_create(/*free_func=*/NULL); trie_t *prev_names = trie_create(/*free_func=*/NULL);
2452 2452
2453 2453 for(i = 0; i < len; ++i) for(i = 0; i < len; ++i)
 
... ... merge_lists(view_t *view, dir_entry_t *entries, int len)
2459 2459 } }
2460 2460
2461 2461 int best_pos_offset = INT_MIN; int best_pos_offset = INT_MIN;
2462 int best_top_offset = INT_MIN;
2462 2463 for(i = 0; i < view->list_rows; ++i) for(i = 0; i < view->list_rows; ++i)
2463 2464 { {
2464 2465 void *data; void *data;
 
... ... merge_lists(view_t *view, dir_entry_t *entries, int len)
2474 2475 /* Update number of selected files (should have been zeroed beforehand). */ /* Update number of selected files (should have been zeroed beforehand). */
2475 2476 view->selected_files += (entry->selected != 0); view->selected_files += (entry->selected != 0);
2476 2477
2477 /* Update cursor position in a smart way. */
2478 /* Update top and cursor positions in a smart way. */
2478 2479 int old_i = (dir_entry_t *)data - entries; int old_i = (dir_entry_t *)data - entries;
2479 2480 pick_position(&view->list_pos, &best_pos_offset, i, old_i - prev_pos); pick_position(&view->list_pos, &best_pos_offset, i, old_i - prev_pos);
2481 pick_position(&view->top_line, &best_top_offset, i, old_i - prev_top);
2480 2482 } }
2481 2483
2482 /* When possible, preserve relative placement of the current entry in the
2483 * view. Drawing code should handle out-of-range values (e.g., negative ones)
2484 * fine. */
2485 view->top_line = view->list_pos - top_delta;
2486
2487 2484 trie_free(prev_names); trie_free(prev_names);
2488 2485 } }
2489 2486
File tests/misc/flist_reload.c changed (mode: 100644) (index db6df5dda..2e690ca4f)
... ... TEST(selection_is_preserved)
86 86 assert_int_equal(2, view->selected_files); assert_int_equal(2, view->selected_files);
87 87 } }
88 88
89 TEST(top_line_is_not_shifted_unnecessarily)
89 /*
90 * Original Updated
91 *
92 * | 0 | del
93 * | 1 | del
94 * top -> | 2 | ==> | 2 | <- top
95 * cursor -> | 3 | | 3 | <- cursor
96 * | 4 | | 4 |
97 * bottom -> | 5 | | 5 | <- bottom
98 */
99 TEST(top_line_is_preserved_when_prefix_shortens)
90 100 { {
91 101 view->window_rows = 4; view->window_rows = 4;
92 102 view->top_line = 2; view->top_line = 2;
 
... ... TEST(top_line_is_not_shifted_unnecessarily)
105 115 assert_string_equal("3", view->dir_entry[view->list_pos].name); assert_string_equal("3", view->dir_entry[view->list_pos].name);
106 116 } }
107 117
118 /*
119 * Original Updated
120 *
121 * | 0 | | 0 |
122 * top -> | 1 | | 1 | <- top
123 * | 2 | ==> | 2 |
124 * cursor v | 3 | del | 4 | <- cursor
125 * bottom -> | 4 | | 5 | <- bottom
126 * | 5 |
127 *
128 * Despite the cursor not on the file being deleted, this is a test for deletion
129 * of the current file. This is because file removal moves the cursor to the
130 * next file (if it exists) to get predictable cursor movement.
131 */
132 TEST(top_line_is_preserved_when_middle_shortens)
133 {
134 view->window_rows = 4;
135 view->top_line = 1;
136 view->list_pos = 4;
137 remove_dir("3");
138
139 assert_string_equal("1", view->dir_entry[view->top_line].name);
140 assert_string_equal("4", view->dir_entry[view->list_pos].name);
141
142 populate_dir_list(view, /*reload=*/1);
143
144 assert_int_equal(1, view->top_line);
145 assert_int_equal(3, view->list_pos);
146 assert_string_equal("1", view->dir_entry[view->top_line].name);
147 assert_string_equal("4", view->dir_entry[view->list_pos].name);
148 }
149
108 150 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */ /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
109 151 /* vim: set cinoptions+=t0 filetype=c : */ /* vim: set cinoptions+=t0 filetype=c : */
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master