summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinnnus <[email protected]>2023-10-22 11:34:22 +0200
committerLinnnus <[email protected]>2023-10-22 11:34:22 +0200
commitcad9fbeb827a9bc55a34162d2fcb7acd4e81489f (patch)
treeeab15851aae9fd16a2410d039139cb4e11e2dc21
parent4c017d6fdc6e991b60ff4fba1d5d6b7da1d5bd69 (diff)
home/nvim: Update sus whitespace highlighting
-rw-r--r--home/neovim/init.vim46
1 files changed, 40 insertions, 6 deletions
diff --git a/home/neovim/init.vim b/home/neovim/init.vim
index b2da2aa..b0eb388 100644
--- a/home/neovim/init.vim
+++ b/home/neovim/init.vim
@@ -190,13 +190,47 @@ au BufReadPost *
\ exe "normal! g'\"" |
\ endif
-" TODO: Ignore in term:// buffers.
augroup Sus
au!
+
+ " Add syntax groups if relevant. This conditional is compensating for
+ " the lack of negative matches in :au.
+ "
+ " See: https://vim.fandom.com/wiki/Highlight_unwanted_spaces
+ " See: https://stackoverflow.com/questions/6496778/vim-run-autocmd-on-all-filetypes-except
+ fun! s:AddSyntax()
+ if bufname() !~ 'term://\|man://'
+ " Any trailing whitespace at the end of lines.
+ syn match SusWhitespace /\s\+$/ containedin=ALL
+
+ " Any non-breaking spaces. These are generated by
+ " CMD+SPACE and deeply annoying.
+ syn match SusWhitespace /\%u00A0/ containedin=ALL
+
+ " Any characters beyond the maximum width of the text.
+ if &tw > 0
+ exe 'syn match SusWhitespace /\%' . &tw . 'v.\+/ containedin=ALL'
+ endif
+ endif
+ endfun
+
+ " Remove highligt group.
+ "
+ " Note that we have to do abit more work since the the syntax rules
+ " have changed under Vim's nose. Hopefully the perfomance
+ " characteristics don't come back to haunt us.
+ fun! s:RemoveSyntax()
+ syn clear SusWhitespace
+ syn sync fromstart
+ endfun
+
+ " Add a persistent highligt group, which matches are going to use.
au VimEnter,ColorScheme * hi SusWhitespace ctermbg=red guibg=red
- au BufWinEnter * match SusWhitespace /\s\+$/
- \ | 2match SusWhitespace /\%u00A0/
- au InsertEnter * match SusWhitespace /\s\+\%#\@<!$/
- au InsertLeave * match SusWhitespace /\s\+$/
- au BufWinLeave * call clearmatches()
+
+ " Create some persistent syntax highlighting groups.
+ au Syntax * call s:AddSyntax()
+
+ " Temporarily remove the groups when in insert mode.
+ au InsertEnter * call s:RemoveSyntax()
+ au InsertLeave * call s:AddSyntax()
augroup END