Programmatically detect a current "highlight" setting?

38 views
Skip to first unread message

Wincent Colaiuta

unread,
Mar 1, 2010, 2:42:19 AM3/1/10
to vim_use
I'm writing a plug-in and want to temporarily hide the cursor.

It would be really neat if I could do "highlight Cursor NONE" and
later restore the highlight setting to whatever it was before.

But I am not sure how to capture the old setting. Evidently "highlight
Cursor" _echoes_ the current setting, but doesn't actually return
anything that I could capture programmatically.

Anything I can do?

Cheers,
Wincent

Tony Mechelynck

unread,
Mar 1, 2010, 5:46:34 AM3/1/10
to vim...@googlegroups.com, Wincent Colaiuta

see
:help :redir

You should reformat the captured string(s) to remove the xxx and add
:hi! at the start.

A couple of caveats:
- a different reformat will be needed for a linked group, to replace
"foo xxx links to bar" by "hi! link foo bar"
- You may need to prefix the capturing :hi command by 0verbose in order
to make sure that your screen capture does _not_ include where the
highlight was defined (see :help :verbose).


Best regards,
Tony.
--
To understand this important story, you have to understand how the
telephone company works. Your telephone is connected to a local
computer, which is in turn connected to a regional computer, which is
in turn connected to a loudspeaker the size of a garbage truck on the
lawn of Edna A. Bargewater of Lawrence, Kan.

Whenever you talk on the phone, your local computer listens in. If it
suspects you're going to discuss an intimate topic, it notifies the
computer above it, which listens in and decides whether to alert the
one above it, until finally, if you really humiliate yourself, maybe
break down in tears and tell your closest friend about a sordid
incident from your past involving a seedy motel, a neighbor's spouse,
an entire religious order, a garden hose and six quarts of tapioca
pudding, the top computer feeds your conversation into Edna's
loudspeaker, and she and her friends come out on the porch to listen
and drink gin and laugh themselves silly.
-- Dave Barry, "Won't It Be Just Great Owning Our Own
Phones?"

Wincent Colaiuta

unread,
Mar 1, 2010, 6:09:06 AM3/1/10
to vim_use
On 1 mar, 11:46, Tony Mechelynck <antoine.mechely...@gmail.com> wrote:
> On 01/03/10 08:42, Wincent Colaiuta wrote:
>
> > I'm writing a plug-in and want to temporarily hide the cursor.
>
> > It would be really neat if I could do "highlight Cursor NONE" and
> > later restore the highlight setting to whatever it was before.
>
> > But I am not sure how to capture the old setting. Evidently "highlight
> > Cursor" _echoes_ the current setting, but doesn't actually return
> > anything that I could capture programmatically.
>
> > Anything I can do?
>
> > Cheers,
> > Wincent
>
> see
>         :help :redir

Thanks, Tony. Read the first paragraph and thought , "ugh, redirecting
to a file, ugly but at least it will work...", and was very pleased to
see in the next paragraph how to redirect to a variable. Sweet.

Cheers,
Wincent

Christian Brabandt

unread,
Mar 1, 2010, 11:13:31 AM3/1/10
to vim_use
Hi Wincent!

On So, 28 Feb 2010, Wincent Colaiuta wrote:

> It would be really neat if I could do "highlight Cursor NONE" and
> later restore the highlight setting to whatever it was before.
>
> But I am not sure how to capture the old setting. Evidently "highlight
> Cursor" _echoes_ the current setting, but doesn't actually return
> anything that I could capture programmatically.

This is actually quite hard, to do it right[™]. You would need to do
something like synIDattr(synIDtrans(hlID("Group")) and put the result
together. A quick test, resulted in this messy function:

fun! <sid>Hi(...)
let s:attr = {}
let s:attr1 = {'bold' : 0, 'italic': 0, 'reverse': 0, 'inverse': 0, 'underline': 0, 'undercurl': 0 }
let s:query = ['fg', 'bg', 'sp', 'bold', 'italic', 'reverse', 'underline', 'undercurl']

for key in s:query
for mode in ['term', 'cterm', 'gui']
if !empty(synIDattr(synIDtrans(hlID(a:1)), key, mode))
if key =~ 'fg\|bg' || (key=='sp' && mode=='gui')
let s:attr[mode . key]=synIDattr(synIDtrans(hlID(a:1)), key, mode)
else
let s:attr1[mode . key] = synIDattr(synIDtrans(hlID(a:1)), key, mode)
endif
endif
endfor
endfor

if has("gui_running")
let cmode='gui'
else
let cmode='cterm'
endif

for mode in ['term', 'cterm', 'gui']
if get(s:attr1, mode . 'italic') ||
\ get(s:attr1, mode . 'bold') ||
\ get(s:attr1, mode . 'reverse') ||
\ get(s:attr1, mode . 'underline') ||
\ get(s:attr1, mode . 'undercurl')

let s:attr[mode] = get(s:attr1, mode . 'italic', '') ? 'italic,' : ''
let s:attr[mode] .= get(s:attr1, mode . 'bold', '') ? 'bold,' : ''
let s:attr[mode] .= get(s:attr1, mode . 'reverse', '') ? 'reverse,' : ''
let s:attr[mode] .= get(s:attr1, mode . 'underline', '') ? 'underline,' : ''
let s:attr[mode] .= get(s:attr1, mode . 'undercurl', '') ? 'undercurl' : ''
endif
endfor

let out='hi ' . a:1
for item in items(s:attr)
if item[1] >=0
let out .= printf(" %s=%s", item[0], item[1])
endif
endfor
let out=substitute(out, ',\(\s\|$\)', ' ' , 'g')
if len(split(out, '\s\+')) == 2
let out=substitute(out, '^hi', '& clear', '')
endif

return out
endfun

and still you wouldn't capture all attributes. I notice, that it is not
possible, to neither capture the standout attribute nor the font
attribute. That is quite surprising. May be this was forgetten, when
these attributes were added.

Anyhow, it's probably easier to parse these values by hand using redir,
as Tony suggested. You need to follow the linked attributes then
manually, though. But this shouldn't be a problem with the hilight group
Cursor, I guess.

regards,
Christian

Christian Brabandt

unread,
Mar 1, 2010, 11:20:40 AM3/1/10
to vim_use
On Mo, 01 M�r 2010, Christian Brabandt wrote:

> and still you wouldn't capture all attributes. I notice, that it is not
> possible, to neither capture the standout attribute nor the font
> attribute. That is quite surprising. May be this was forgetten, when
> these attributes were added.

Err, nevermind, standout seems to be supported, though it is not
mentioned at :h synIDattr()

regards,
Christian

Reply all
Reply to author
Forward
0 new messages