Tuesday, August 29, 2017

Re: Controlling the display of a syntax highlighting group


I previously asked how I could set up a toggle for syntax highlighting
of non-ASCII characters.  I was having problems clearing a
highlighting group and then never being able to reactivate it.

Ben Fritz suggested using the matchadd()/matchdelete() functions
for the job. That looks like a good idea and would probably be cleaner
than what I did, but I solved the problem another way before seeing his comments.

Tony Mechelynck suggested using some functions (see above).
I tried these and they exhibit the same problem I was finding:  they
work exactly once and then not again.  There is some strange
interaction happening with the highlight group that I have not yet been able to identify.

I did manage to resolve the problem and integrate it into other syntax highlighting with some work.  Here is my solution.


First, I assigned a command to the L9 key that would toggle basic
syntax highlighting on/off:

" L9 Key                                       [Toggle syntax hilight on/off]
nnoremap  <Esc>[55~ :if exists("g:syntax_on") <Bar>
                                   \     syntax off <Bar>
                                   \     endif <Bar>
                                   \ else <Bar>
                                   \     syntax enable <Bar>
                                   \ endif<CR><CR>


Next, I assigned the shifted L9 key a command to toggle non-ASCII highlighting on/off.  The trick here was to clear the syntax but NOT
the highlighting group.

" L9 (Shifted)                             [Toggle nonascii highlighting]
nnoremap  <Esc>[55;2~ :source /u/jeff/lib/vi/vimrc.nonascii<CR>

Where the vimrc.nonascii file contains:

if (nonascii == "on")
    syntax clear NONASCII
    let nonascii = "off"
else
    syntax match NONASCII "[^\x00-\x7F]"
    highlight NONASCII ctermbg=13
    let nonascii = "on"
endif


Now you ask, why is this in an external file rather than directly
mapped as in the previous case.  Well, I tried that and found that
while it sort of worked, not only did all non-ASCII characters get
highlighted, but so did all white space (space and tabs) between
them!  This does not happen when the command is read from the
external file.  [I'd be happy to hear any ideas as to why that is
happening and what to do about it!]

OK so far.  However, if non-ASCII highlighting was active and syntax
highlighting was toggled, it forced the non-ASCII highlight off too and
left the "nonascii" variable in the wrong state.

I wanted these to operate independently, so I modified the previous
L9 Key mapping as follows, to force non-ASCII highlight back on if
it was currently on.

" L9 Key                                       [Toggle syntax hilight on/off]
nnoremap  <Esc>[55~ :if exists("g:syntax_on") <Bar>
                   \     syntax off <Bar>
                   \     if (nonascii == "on") <Bar>
                   \         let nonascii = "off" <Bar>
                   \         :source /u/jeff/lib/vi/vimrc.nonascii <Bar>
                   \     endif <Bar>
                   \ else <Bar>
                   \     syntax enable <Bar>
                   \     if (nonascii == "on") <Bar>
                   \         let nonascii = "off" <Bar>
                   \         :source /u/jeff/lib/vi/vimrc.nonascii <Bar>
                   \     endif <Bar>
                   \ endif<CR><CR>

Why two <CR> are required here is another minor mystery.  It's
probably because of the :source command which gobbles one
<CR> leaving the second for the overall command.  However,
adding a <CR> to the end of the :source lines and removing one
from the end does not work!

I hope someone finds this instructive.  Continue the discussion if
there are more insights as to what is going on with these perceived
glitches I was experiencing.

Much thanks to Ben and Tony for their very helpful feedback.  I
learned a few things and got pushed in the right direction.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: