Wednesday, March 3, 2010

Re: How to temporarily change Cursor color and change back


fun! <sid>HiList()
redir => a | sil hi | redir end

let a=substitute(a, '\n\s\+', ' ', 'g')
let b=split(a,'\n')
call map(b, 'split(v:val, ''\s\+'')[0]')
if exists("g:items")
unlet g:items
endif
let g:items=[]
for val in b
call add(g:items, <sid>Hi(val))
endfor
endfun

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

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)
elseif key=='font' && mode=='gui'
let s:attr[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 . 'standout') ||
\ 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 . 'standout', '') ? 'standout,' : ''
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
" echo out

return out
endfun

fun! <sid>SortLines()
" %s/^hi //ge
" %s/\s\+xxx//ge
"sort
let lines=getline(1,'$')
let i=1
for line in lines
let word=split(line, '\s\+')
let sort=[ word[0] ] + sort(word[1:])
call setline(i, join(sort))
let i+=1
endfor
endfun

"com! -nargs=1 -complete=highlight HI :call <sid>Hi(<q-args>)
"com! HIList :call <sid>HiList()
"com! CleanUp :call <sid>SortLines()
Hi pansz!

On Do, 04 Mär 2010, pansz wrote:

> Script can use highlight command to change color, however, if a script
> meant to change one color temporarily, it has no knowledge about the
> previous setting.
>
> 1. the :hi Cursor is defined by my color scheme.
> 2. now in some case a script change the cursor color to indicate a
> special mode.
> 3. when the mode ends, the script want to change the Cursor back but it
> has no knowledge about what is the highlight of Cursor defined by user's
> color scheme.
[…]
>
> Any work around? Thanks for all.
>

Try the attached script. I was written quick and dirty fr a similar
issue on this list. It already queries the font attribute, though this
only works with a patched vim, currently.

To return the Cursor highlight, you can then issue:
:let a=Hi("Cursor")
and have all attributes of Cursor in the variable a. To restore the
attributes simply exe the variable a.

The other functions are there to return all available hi groups
(HiLIst()) and SortLines is a debuging function for me to be able to
compare the output from HiList() with a manual list I created using
redir|hi|redir end
You probably won't need it, but I leave it there anyway.

regards,
Christian
--
:wq

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