Monday, March 1, 2010

Re: Programmatically detect a current "highlight" setting?

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

--
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

No comments:

Post a Comment