Controlling the display of a syntax highlighting group

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Controlling the display of a syntax highlighting group

cjsmall
To find non-ascii characters in a file, I have defined the following:

    syntax match nonascii "[^\x00-\x7F]"
    highlight nonascii ctermbg=13

I can disable this with the command:

    syntax off

I would like to create a command that toggles this highlight group on/off and
hopefully do so without affecting the current state of the remainder of the
syntax highlight groups.  I have not found a way to test the state of this
group, and once activated, I cannot seem to clear or edit the highlight
definition non-destructively.

Any pointers?

[P.S.:  I originally sent this to <[hidden email]> which is the address listed
on the official Vim mailing list page, but it never showed up.  Is that still
valid, or must [hidden email] now be used?]

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Controlling the display of a syntax highlighting group

Tony Mechelynck
 On Tue, Aug 29, 2017 at 3:51 AM, Jeffery Small <[hidden email]> wrote:

> To find non-ascii characters in a file, I have defined the following:
>
>     syntax match nonascii "[^\x00-\x7F]"
>     highlight nonascii ctermbg=13
>
> I can disable this with the command:
>
>     syntax off
>
> I would like to create a command that toggles this highlight group on/off
> and
> hopefully do so without affecting the current state of the remainder of the
> syntax highlight groups.  I have not found a way to test the state of this
> group, and once activated, I cannot seem to clear or edit the highlight
> definition non-destructively.
>
> Any pointers?

Try the following (untested):

    function ASCII_on()
        syn match NonAscii "[^\x00-\x7F]"
        hi clear NonAscii
        hi NonAscii term=reverse ctermbg=Magenta guibg=Magenta
    endfunction
    function ASCII_off()
        syn clear NonAscii
        hi clear NonAscii
    endfunction
    function ASCII_toggle()
        if hlexists('NonAscii')
            call ASCII_off()
        else
            call ASCII_on()
        endif
    endfunction
    map <F12> :call ASCII_toggle()<CR>
    imap <F12> <C-O>:call ASCII_toggle()<CR>
    " but Select mode would use the Normal-mode mapping
    " and replace the selection — we don't want that
    sunmap <F12>

>
> [P.S.:  I originally sent this to <[hidden email]> which is the address listed
> on the official Vim mailing list page, but it never showed up.  Is that
> still
> valid, or must [hidden email] now be used?]

I think that [hidden email] is still acceptable, but I'm not sure: it
used to be a robot on a computer somewhere in an out-of-the-way place
in the Math faculty of some German university (Uni-Erlangen IIRC), and
no one knew anymore how to fix anything that went wrong with it. IMHO
the Google Group is more reliable.

Best regards,
Tony.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Controlling the display of a syntax highlighting group

Ben Fritz
In reply to this post by cjsmall
On Monday, August 28, 2017 at 8:52:16 PM UTC-5, cjsmall wrote:

> To find non-ascii characters in a file, I have defined the following:
>
>
>     syntax match nonascii "[^\x00-\x7F]"
>     highlight nonascii ctermbg=13
>
>
>
> I can disable this with the command:
>
>
>     syntax off
>
>
> I would like to create a command that toggles this highlight group on/off and
> hopefully do so without affecting the current state of the remainder of the
> syntax highlight groups. 
I suggest using the matchadd()/matchdelete() functions for this purpose, unless there is a specific reason you need to use syntax matching. Those are more "temporary" and easy to toggle.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Controlling the display of a syntax highlighting group

cjsmall

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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Controlling the display of a syntax highlighting group

cjsmall
In reply to this post by cjsmall
I wrote:

> [P.S.:  I originally sent this to <[hidden email]> which is the address listed
> on the official Vim mailing list page, but it never showed up.  Is that still
> valid, or must [hidden email] now be used?]


As you can see above, after a long delay (3 days), the original message
(duplicte)to [hidden email] did eventually get posted.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Controlling the display of a syntax highlighting group

cjsmall
In reply to this post by cjsmall
One more addendum:

In the external file, I changed the line:

highlight NONASCII ctermbg=13

to:

highlight NONASCII ctermbg=13 guibg=Magenta

so that the command would work in gvim as well as vim.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.