Created
April 13, 2020 16:24
-
-
Save bfrg/23376e80f7709c729589b4399edd3ea0 to your computer and use it in GitHub Desktop.
Display the output of :highlight in a popup window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" ============================================================================== | |
" Display the output of :highlight in a popup window | |
" | |
" Command: | |
" | |
" :Highlight [{args}...] | |
" | |
" {args} is an optional list of highlight groups (partial names are supported) | |
" | |
" Examples: | |
" | |
" Display all highlight groups (this is like running :hi): | |
" | |
" :Highlight | |
" | |
" Display only specific highlight groups (this is like running :hi Pmenu | | |
" hi Visual | hi StatusLine | hi StatusLineNC): | |
" | |
" :Highlight Pmenu Visual StatusLine StatusLineNC | |
" | |
" Display all highlight groups containing 'diff' (&wildignorecase applies): | |
" | |
" :Highlight diff | |
" | |
" Popup Mappings: | |
" j/k - scroll down/up one line | |
" d/u - scroll down/up one half page | |
" f/b - scroll down/up one full page | |
" g/G - scroll to top/bottom | |
" q - close popup window | |
" ============================================================================== | |
function! s:popup_filter(winid, key) abort | |
if a:key ==# 'j' | |
call win_execute(a:winid, "normal! \<c-e>") | |
elseif a:key ==# 'k' | |
call win_execute(a:winid, "normal! \<c-y>") | |
elseif a:key ==# 'g' | |
call win_execute(a:winid, 'normal! gg') | |
elseif a:key ==# 'G' | |
call win_execute(a:winid, 'normal! G') | |
elseif a:key ==# 'd' | |
call win_execute(a:winid, "normal! \<c-d>") | |
elseif a:key ==# 'u' | |
call win_execute(a:winid, "normal! \<c-u>") | |
elseif a:key ==# 'f' | |
call win_execute(a:winid, "normal! \<c-f>") | |
elseif a:key ==# 'b' | |
call win_execute(a:winid, "normal! \<c-b>") | |
elseif a:key ==# 'q' | |
call popup_close(a:winid) | |
else | |
return v:false | |
endif | |
return v:true | |
endfunction | |
function! s:popup_highlight(args) abort | |
if empty(a:args) | |
let output = execute('hi')->split('\n') | |
else | |
let grouplist = [] | |
call split(a:args)->map({_,i -> extend(grouplist, getcompletion(i, 'highlight'))}) | |
let output = execute('hi ' .. join(grouplist, ' | hi '))->split('\n') | |
endif | |
let winid = popup_create(output, { | |
\ 'pos': 'botleft', | |
\ 'line': &lines - &cmdheight, | |
\ 'pos2': 'topleft', | |
\ 'line2': 1, | |
\ 'minheight': 2, | |
\ 'maxheight': 20, | |
\ 'minwidth': &columns - 2, | |
\ 'maxwidth': &columns - 1, | |
\ 'padding': [], | |
\ 'scrollbar': v:true, | |
\ 'wrap': v:true, | |
\ 'mapping': v:false, | |
\ 'filtermode': 'n', | |
\ 'filter': funcref('s:popup_filter') | |
\ }) | |
call matchadd('Directory', 'links to', 10, -1, {'window': winid}) | |
call matchadd('Directory', '\s\zs\%(term\|cterm\|ctermfg\|ctermbg\|gui\|guifg\|guibg\|guisp\)=', 10, -1, {'window': winid}) | |
let i = 0 | |
for line in winbufnr(winid)->getbufline(1, '$') | |
let i += 1 | |
let group = matchstr(line, '^\w\+') | |
if !empty(group) | |
call matchadd(group, printf('\%%%dl\<xxx\>', i), 10, -1, {'window': winid}) | |
endif | |
endfor | |
return winid | |
endfunction | |
command! -nargs=* -complete=highlight Highlight call s:popup_highlight(<q-args>) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment