Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid cursor jump when file under cursor is renamed by plugin/r #1869

Closed
wants to merge 1 commit into from

Conversation

azuline
Copy link
Contributor

@azuline azuline commented Apr 16, 2024

This PR prevents the cursor from jumping back to the top of the directory when the active file (file underneath the cursor) is renamed by the r renamer tool or an external plugin.

nojump.mp4

My use case:

I use nnn to navigate my music library, with a custom plugin to operate on music directories. Several of these commands rename the directory. Currently, once the directory is renamed (often the change of a single character), the cursor jumps back to the top of the working directory, requiring me to navigate back to where I was in a fairly large directory. After this change, the cursor no longer jumps.

@azuline azuline force-pushed the no-jump branch 4 times, most recently from d15749a to e3be631 Compare April 16, 2024 04:17
Comment on lines 5478 to +5481
if (nextpath)
set_smart_ctx(ctx, nextpath, path, runfile, lastname, lastdir);
else
*cd = FALSE;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prior, there was an assumption that every plugin execution could change the directory, and so cd = TRUE for every plugin run. We now accurately set cd based on whether the plugin changed the directory or not.

I believe this is the only way for a plugin to change the directory, would like confirmation.

Comment on lines +6019 to +6030
int target;
if (*lastname) {
target = dentfind(lastname, ndents);
/* If we failed to find the previous filename, and we didn't change directories, */
/* default to the previous cursor position. This way, when a file is renamed by */
/* a plugin, the cursor does not jump to the top of the directory. */
if (target == 0 && !cd)
target = cur;
} else
target = 0;

move_cursor(target, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if now lastname is on position 0 and we didn't cd? You set target = cur which is probably not what we want.

Also, we generally don't know if cur will be a valid position after running external actions. Any number of entries may be added or removed or renamed, in which case you might as well place the cursor on a random line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I didn't consider these cases. I will fix them in my fork.

@jarun
Copy link
Owner

jarun commented Apr 17, 2024

Currently, once the directory is renamed (often the change of a single character)

From the nnn help page:

^R Rename/dup r Batch rename

If you are changing only 1 file/dir, use ^R.

Alternatively, use filters to reach a file quickly. Since you have just renamed it, you should be knowing the name.

@jarun jarun closed this Apr 17, 2024
@azuline
Copy link
Contributor Author

azuline commented Apr 18, 2024

I am neither using ^R or r, but a custom plugin in a very large directory. I will keep this behavior in a fork since it is only important to my use case. 👍🏼

@github-actions github-actions bot locked and limited conversation to collaborators May 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants