bronson / vim-trailing-whitespace

Highlights trailing whitespace in red and provides :FixWhitespace to fix it.
452 stars 62 forks source link

Branch autocmd setup depending on vim|nvim #28

Closed PabloScolpino closed 1 year ago

PabloScolpino commented 1 year ago

I have not found how to interpolate a variable with event names into autocmd, I don't know if this is possible or not

superjamie commented 1 year ago

I have not found how to interpolate a variable with event names into autocmd, I don't know if this is possible or not

You can do this with execute of a string, and substitute a variable into the string, like:

let term_open_event = (has('nvim') ? 'TermOpen' : 'TerminalOpen')
exe 'autocmd BufRead,BufNew,FileType,' term_open_event '* if ShouldMatchWhitespace() | match ExtraWhitespace /\\\@<![\u3000[:space:]]\+$/ | else | match ExtraWhitespace /^^/ | endif'

Both your patch and the above syntax work for me in Vim 8.2.4919 and Neovim 0.8.3

Yours keeps the syntax highlighting of the autocmd which is nice.

However, it means the autocmd is repeated in the source twice, so future changes to the autocmd would need be manually done on both lines.

I think it's likely a future person will make the mistake of updating one autocmd but not the other.

So it's probably better to use the syntax with exe of a built autocmd string with term_open_event variable.

PabloScolpino commented 1 year ago

Thanks for the feedback and explanation. Yes, I agree with the duplication being a likely landmine, that's what I was referring to with this "not being elegant".

I'll rework this to incorporate your suggestion and test on both editors.

gcapizzi commented 1 year ago

Can this fix be merged in the meantime, and explore cleaner solutions later? It would be super helpful to us NeoVim users 🙂

superjamie commented 1 year ago

If Pablo isn't able to follow up, I'm happy to send a PR.

nusendra commented 1 year ago

Seems Pablo isn't responsive, I created PR to fix this here https://github.com/bronson/vim-trailing-whitespace/pull/30

PabloScolpino commented 1 year ago

Superseeded by PR #30 sorry for pacing out.