Skip to content

Instantly share code, notes, and snippets.

@metalelf0
Last active January 3, 2021 13:36
Show Gist options
  • Save metalelf0/8cdd2cea7d759a62dd5899226e144423 to your computer and use it in GitHub Desktop.
Save metalelf0/8cdd2cea7d759a62dd5899226e144423 to your computer and use it in GitHub Desktop.
" This function originates from https://www.reddit.com/r/neovim/comments/eq1xpt/how_open_help_in_floating_windows/; it isn't mine!
function! CreateCenteredFloatingWindow(title) abort
let width = min([&columns - 4, max([80, &columns - 20])])
let height = min([&lines - 4, max([20, &lines - 10])])
let top = ((&lines - height) / 2) - 1
let left = (&columns - width) / 2
let opts = {'relative': 'editor', 'row': top, 'col': left, 'width': width, 'height': height, 'style': 'minimal'}
let top = "╭" . repeat("─", width - 2) . "╮"
let mid = "│" . repeat(" ", width - 2) . "│"
let bot = "╰─" . a:title . repeat("─", width - strlen(a:title) - 3) . "╯"
let lines = [top] + repeat([mid], height - 2) + [bot]
let s:buf = nvim_create_buf(v:false, v:true)
call nvim_buf_set_lines(s:buf, 0, -1, v:true, lines)
call nvim_open_win(s:buf, v:true, opts)
set winhl=Normal:Floating
let opts.row += 1
let opts.height -= 2
let opts.col += 2
let opts.width -= 4
let l:textbuf = nvim_create_buf(v:false, v:true)
call nvim_open_win(l:textbuf, v:true, opts)
au BufWipeout <buffer> exe 'bw '.s:buf
return l:textbuf
endfunction
function! FloatingWindowEdit(query) abort
let l:buf = CreateCenteredFloatingWindow(a:query)
call nvim_set_current_buf(l:buf)
execute 'edit ' . a:query
endfunction
command! -complete=file -nargs=? FWO call FloatingWindowEdit(<q-args>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment