Last active
January 3, 2021 13:36
-
-
Save metalelf0/8cdd2cea7d759a62dd5899226e144423 to your computer and use it in GitHub Desktop.
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
" 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