Based on Rich answer and Drew Neil’s vimcast, one can:
A: To just read the syntax file ~/.vim/after/syntax/<filetype>.vim
.
nnoremap <leader>z :call <SID>SyntaxStack()<CR>
Or B: modify syntax file appending the highlight settings of groups under cursor
nnoremap <leader>z! :call <SID>SyntaxStack(1)<CR>
A picture is worth a thousand words. Up screenshot is previous to executing the map, down the result:
Next more complex mappings to highlight groups. With groups under cursor searched (to search for less groups, just press /
- up-arrow
and modify it). Mappings are more complex because 1st I must open a buffer, and then search in it, so it cannot be all in same function. Actually, cause I cannot achieve getting the return
of this <SID> function
directly in the mapping, my workaround is that this function must add a global variable g:str2Search_SyntaxStack
, which the mapping reads.
A:
nnoremap <leader>z :call <SID>SyntaxStack()<CR>:let g:var02=':silent execute "normal /' . g:str2Search_SyntaxStack . '\' .'<' .'CR' .'>"'<CR>:execute(g:var02)<CR>:echo "Searched: ". substitute(g:str2Search_SyntaxStack, '\\\\\\', '\', 'g')<CR>
B:
nnoremap <leader>z! :call <SID>SyntaxStack(1)<CR>:let g:var02=':silent execute "normal /' . g:str2Search_SyntaxStack . '\' .'<' .'CR' .'>"'<CR>:execute(g:var02)<CR>:echo "Searched: ". substitute(g:str2Search_SyntaxStack, '\\\\\\', '\', 'g')<CR><CR>
where <SID>SyntaxStack()
function:
function! <SID>SyntaxStack(append_to_file_bool=0)
if !exists("*synstack")
return
endif
let l:str2Search=List_to_strings_to_search(map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")'), 1)
let l:file__after_syntax=Get_file__after_syntax()
" read current highlight for <group> under cursor
" and append to syntax file
if (a:append_to_file_bool)
for l:syntaxID in synstack(line("."), col("."))
let l:hi_group=execute('hi ' . synIDattr(l:syntaxID, "name"))
let l:hi_group=substitute(l:hi_group, "\n", '', 'g') " remove new lines
let l:hi_group=substitute(l:hi_group, "xxx", '', 'g') " remove 'xxx' added to show highlight settings default (I think)
let l:hi_group="highlight " . l:hi_group
if l:hi_group =~ "clear"
" if cleared (noone should) from colorscheme plugin, then comment line before appending
let l:hi_group= '" ' . l:hi_group
endif
call writefile([l:hi_group], l:file__after_syntax, "a")
endfor
endif
" 1st I must open a buffer, and then search in it, so it cannot be all in same function
" therefore, I need a global variable to return from an <SID> fcn
let g:str2Search_SyntaxStack=l:str2Search
" open syntax file with last syntaxID highlighted
execute ":tabnew " . l:file__after_syntax
endfunc
And auxiliary functions:
function! List_to_strings_to_search(list01,slash_double_bool=0)
" https://vim.fandom.com/wiki/Search_patterns#Finding_this_or_that
let str01=""
let cnt01=0
let list01_len=len(a:list01)
for l:str02 in a:list01
let l:cnt01+=1
" remove single quotes
let l:str03=substitute(l:str02, "'", '', 'g')
let l:str01=l:str01 . l:str03
if l:cnt01 < l:list01_len
if (a:slash_double_bool)
let l:str01=l:str01 . '\\\' .'|'
else
let l:str01=l:str01 . '\' .'|'
endif
endif
endfor
return l:str01
endfunc
function! Get_filetype()
let l:filetype_str=&filetype " get filetype
let l:filetype_str=substitute(string(l:filetype_str), "'", '', 'g') " remove single quotes
return l:filetype_str
endfunc
function! Get_file__after_syntax()
let l:filetype_str=Get_filetype()
let l:file__after_syntax=expand('~/.vim/after/syntax/' . l:filetype_str . '.vim') " expand() to avoid tilde errors
return l:file__after_syntax
endfunc