Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Last active September 18, 2024 09:09
Show Gist options
  • Save AndrewRadev/c7c80810cacccd5a9329c3e8b82e2d20 to your computer and use it in GitHub Desktop.
Save AndrewRadev/c7c80810cacccd5a9329c3e8b82e2d20 to your computer and use it in GitHub Desktop.
Open Vim help in a resizable, movable popup window
" Save as ~/.vim/plugin/pophelp.vim
let s:popup = 0
command! -range=0 -nargs=* -complete=help
\ Pophelp call s:Open(<q-args>, <count>)
command! Popclose call s:Close()
function! s:Open(topic, count) abort
if a:topic == ''
let topic = expand('<cword>')
elseif a:count > 0
let topic = s:GetSelection()
else
let topic = a:topic
endif
if &filetype != 'help'
let saved_view = winsaveview()
defer winrestview(saved_view)
endif
" Open/focus a help buffer
help
set buflisted
let help_bufnr = bufnr()
hide
if s:popup > 0
let position = popup_getpos(s:popup)
call popup_close(s:popup)
" We remove 2 to account for the borders:
if has_key(position, 'height')
let position['maxheight'] = position['height'] - 2
endif
if has_key(position, 'width')
let position['maxwidth'] = position['width'] - 2
endif
else
let position = {
\ 'pos': 'topright',
\ 'line': 1,
\ 'col': &columns,
\ 'maxheight': float2nr(floor(&lines / 3)),
\ 'maxwidth': float2nr(floor(&columns / 2)),
\ }
endif
let s:popup = popup_create(help_bufnr, {
\ 'drag': v:true,
\ 'resize': v:true,
\ 'close': 'button',
\ 'border': [],
\ })
call popup_move(s:popup, position)
" Jump the particular help result in the popup window
call win_execute(s:popup, 'tag /'..topic)
endfunction
function! s:Close() abort
if s:popup > 0
call popup_close(s:popup)
let s:popup = 0
endif
endfunction
function! s:GetSelection(motion) abort
let saved_register = getreg('a')
defer setreg('a', saved_register)
exe 'normal! gv"ay'
return @a
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment