ntpeters / vim-better-whitespace

Better whitespace highlighting for Vim

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error generated when force saving file opened in read-only mode

mcrmonkey opened this issue · comments

When opening a file in vim in read only mode ( either via view <filename> or vi -R <filename> or vim -R <filename> ) and then attempting to write the file with the force option ( :w! ) an error is generated:

Error detected while processing function <SNR>StripWhitespaceCommand:
line    2:

I've traced this to line 206 in plugin/better-whitespace.vim

When the echoerr function is used the error is generated. If its switched for echo or echohl there is no error generated.

Issue looks to have appeared in commit: f5726c4

Vim version: VIM - Vi IMproved 8.0
OS: Debian stretch
Loading via pathogen

Calling the StripWhitespaceCommand in a read only file intentionally causes an E45 error message.

You can use StripWhitespace! to force removing white space in such a file. Then saving (probably with StripWhitespaceOnSave disabled).

The :w! should also force stripping white space on top of force saving.

Given how the file is opened I'd expect the error that the echoerr is trying to give, from vim itself, but the echoerr is throwing an explicit vim error.
...I'd think the same error would be thrown with the other echoerr on 215 too

If I forced the write with an :w! I'd expect it to follow the same process for :w as if the file was opened in a non-readonly state.

Hmmm, I might have initially misunderstood your bug report.

Anyway, after having a quick look at this, I can’t reproduce the bug (using the version at commit f5726c4 and vim 8.1.1017).

  • Saving readonly files with :w returns the E45 error as intended.
  • Saving readonly files with :w! writes the the buffer to the file without an error, and correctly stripping whitespace.

I did a recording to show what I'm getting: https://asciinema.org/a/rF64uGMr0Dp8FjnzIkoeLMIjM

its all done in a docker container and I used this Dockerfile to create that container:

FROM debian:buster
RUN apt-get update -qq && apt-get install -qq vim git wget
WORKDIR /root
RUN mkdir -p ./.vim/autoload ./.vim/bundle
RUN wget -O /root/.vim/autoload/pathogen.vim https://raw.githubusercontent.com/tpope/vim-pathogen/master/autoload/pathogen.vim
RUN git clone https://github.com/ntpeters/vim-better-whitespace ./.vim/bundle/vim-better-whitespace
RUN echo "execute pathogen#infect()\ncall pathogen#helptags()\nautocmd BufWritePre * StripWhitespace\n" > ./.vim/vimrc

I used debian:buster because it gave me a more up to date version of vim and set the vimrc at the bare minimum to reproduce

I also found mention of echoerr in https://google.github.io/styleguide/vimscriptfull.xml#Forbidden_Commands but couldn't find the full reasoning behind the statements for the use of echoerr though

So it seems the error message is printed, but prefixed with some sort of backtrace, when you force write (:w!). You should be able to confirm this using :messages.

The underlying problem seems to be that you are using StripWhitespaceCommand instead of StripWhitespaceOnSave, which correctly captures the force option (!, called cmdbang) when enabled through the plugin:

if <SID>ShouldStripWhitespaceOnSave()
autocmd BufWritePre * call <SID>StripWhitespaceOnSave(v:cmdbang)
endif

Instead of your manual autocmd, try using let g:strip_whitespace_on_save = 1 (or if you use the autocmd to restrict e.g. to some file types, try autocmd FileType <desired_filetypes> EnableStripWhitespaceOnSave).

ah-ha, my mistake!

The autocmd was a copy from someones vimrc from a while back I'm going to switch to using let g:strip_whitespace_on_save = 1 as suggested.

prefixed with some sort of backtrace

I think that's the source of the confusion here - I was seeing the correct error message flash up and remain in :messages but wasn't expecting the debug stuff

Thank you for your help ⭐