Update the vimscript code for restoring cursor position

Using xxd(1) to filter and edit binary files causes the input files
to have dual nature, so to speak, which effectively makes restoring
the cursor position broken.  Fix that by ignoring the "xxd" file type
in the code that restores the cursor position.

Interactive rebasing in git causes files to be edited in vim, which,
similarly to commit messages, are rarely the same as the last one
edited.  Thus, also add "gitrebase" to the list of file types for
which the cursor position isn't restored.

While there, refactor the code a bit to possibly save a few CPU cycles
and to keep the line lengths in check, and use the long form of the
commands and variables, to make the code slightly more consistent and
more understandable to newcomers.

Update the relevant comments in the code and the associated parts of
the documentation, to keep them in sync with the updated code.

Remove some redundant trailing whitespace as well, as spotted.
This commit is contained in:
Dragan Simic' via vim_dev
2023-08-09 17:23:58 +02:00
committed by Christian Brabandt
parent 6a500661a9
commit 81b8bf5b4a
2 changed files with 28 additions and 14 deletions

View File

@ -99,15 +99,19 @@ if 1
" Put these in an autocmd group, so that you can revert them with: " Put these in an autocmd group, so that you can revert them with:
" ":autocmd! vimStartup" " ":autocmd! vimStartup"
augroup vimStartup augroup vimStartup
au! autocmd!
" When editing a file, always jump to the last known cursor position. " When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid, when inside an event handler " Don't do it when the position is invalid, when inside an event handler
" (happens when dropping a file on gvim) and for a commit message (it's " (happens when dropping a file on gvim), for a commit or rebase message
" likely a different one than last time). " (likely a different one than last time), and when using xxd(1) to filter
" and edit binary files (it transforms input files back and forth, causing
" them to have dual nature, so to speak)
autocmd BufReadPost * autocmd BufReadPost *
\ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit' \ let line = line("'\"")
\ | exe "normal! g`\"" \ | if line >= 1 && line <= line("$") && &filetype !~# 'commit'
\ && index(['xxd', 'gitrebase'], &filetype) == -1
\ | execute "normal! g`\""
\ | endif \ | endif
augroup END augroup END
@ -119,7 +123,7 @@ if 1
augroup vimHints augroup vimHints
au! au!
autocmd CmdwinEnter * autocmd CmdwinEnter *
\ echohl Todo | \ echohl Todo |
\ echo gettext('You discovered the command-line window! You can close it with ":q".') | \ echo gettext('You discovered the command-line window! You can close it with ":q".') |
\ echohl None \ echohl None
augroup END augroup END

View File

@ -308,17 +308,27 @@ This switches on three very clever mechanisms:
*restore-cursor* *last-position-jump* > *restore-cursor* *last-position-jump* >
autocmd BufReadPost * augroup RestoreCursor
\ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit' autocmd!
\ | exe "normal! g`\"" autocmd BufReadPost *
\ | endif \ let line = line("'\"")
\ | if line >= 1 && line <= line("$") && &filetype !~# 'commit'
\ && index(['xxd', 'gitrebase'], &filetype) == -1
\ | execute "normal! g`\""
\ | endif
augroup END
Another autocommand. This time it is used after reading any file. The Another autocommand. This time it is used after reading any file. The
complicated stuff after it checks if the '" mark is defined, and jumps to it complicated stuff after it checks if the '" mark is defined, and jumps to it
if so. The backslash at the start of a line is used to continue the command if so. It doesn't do that for a commit or rebase message, which are likely
from the previous line. That avoids a line getting very long. a different one than last time, and when using xxd(1) to filter and edit
See |line-continuation|. This only works in a Vim script file, not when binary files, which transforms input files back and forth, causing them to
typing commands at the command-line. have dual nature, so to speak. See also |using-xxd|.
The backslash at the start of a line is used to continue the command from the
previous line. That avoids a line getting very long. See |line-continuation|.
This only works in a Vim script file, not when typing commands at the
command line.
> >
command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis