[vim] Popup window support for both Vim and Neovim
Before adoption: this commit came in with the history the project adopted, so no landing records it.
e.g. let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } } Based on the code from https://github.com/junegunn/fzf.vim/issues/821#issuecomment-581273191 by @lacygoill.
authored by Junegunn Choi · committed by Junegunn Choi · parent 293dd76 · Browse files · 3 files·+111 added−61 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+111 added−61 removed
- other:+111 added−61 removed
Provenance
- Commit
7ceb58b- Tree
814a058- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
293dd76
3 files changed+111 added−61 removed
modified:doc/fzf.txt+23 added-24 removed
1-fzf.txt fzf Last change: November 23 20191+fzf.txt fzf Last change: February 3 20202 FZF - TABLE OF CONTENTS *fzf* *fzf-toc*3 ==============================================================================4 5 FZF Vim integration4 unmodified lines10 fzf#run11 fzf#wrap12 Tips13 fzf inside terminal buffer14- Starting fzf in Neovim floating window14+ Starting fzf in a popup window15 Hide statusline16 License17 18 FZF VIM INTEGRATION *fzf-vim-integration*184 unmodified lines203 `options` | string/list | Options to fzf204 `dir` | string | Working directory205 `up` / `down` / `left` / `right` | number/string | (Layout) Window position and size (e.g. `20` , `50%` )206 `window` (Vim 8 / Neovim) | string | (Layout) Command to open fzf window (e.g. `vertical aboveleft 30new` )207+ `window` (Vim 8 / Neovim) | dict | (Layout) Popup window settings (e.g. `{'width': 0.9, 'height': 0.6}` )208 ---------------------------+---------------+----------------------------------------------------------------------209 210 `options` entry can be either a string or a list. For simple cases, string211 should suffice, but prefer to use list type to avoid escaping issues.212 >213 call fzf#run({'options': '--reverse --prompt "C:\\Program Files\\"'})214 call fzf#run({'options': ['--reverse', '--prompt', 'C:\Program Files\']})215 <216+When `window` entry is a dictionary, fzf will start in a popup window. The217+following options are allowed:218 219+ - Required:220+ - `width` [float]221+ - `height` [float]222+ - Optional:223+ - `highlight` [string default `'Comment'`]: Highlight group for border224+ - `rounded` [boolean default `v:true`]: Use rounded border225+226+227 FZF#WRAP228 ==============================================================================229 230 *fzf#wrap*70 unmodified lines301 - On Terminal Vim with a non-default layout302 - `call fzf#run({'left': '30%'})` or `let g:fzf_layout = {'left': '30%'}`303 304 294-Starting fzf in Neovim floating window~295- *fzf-starting-fzf-in-neovim-floating-window*305+Starting fzf in a popup window~306+ *fzf-starting-fzf-in-a-popup-window*307 >297- " Using floating windows of Neovim to start fzf298- if has('nvim')299- let $FZF_DEFAULT_OPTS .= ' --border --margin=0,2'300-301- function! FloatingFZF()302- let width = float2nr(&columns * 0.9)303- let height = float2nr(&lines * 0.6)304- let opts = { 'relative': 'editor',305- \ 'row': (&lines - height) / 2,306- \ 'col': (&columns - width) / 2,307- \ 'width': width,308- \ 'height': height }309-310- let win = nvim_open_win(nvim_create_buf(v:false, v:true), v:true, opts)311- call setwinvar(win, '&winhighlight', 'NormalFloat:Normal')312- endfunction313-314- let g:fzf_layout = { 'window': 'call FloatingFZF()' }315- endif316-308+ " Required:309+ " - width [float]310+ " - height [float]311+ "312+ " Optional:313+ " - highlight [string default 'Comment']: Highlight group for border314+ " - rounded [boolean default v:true]: Use rounded border315+ let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }316 <317 318 Hide statusline~319 *fzf-hide-statusline*24 unmodified linesmodified:plugin/fzf.vim+68 added-1 removed
648 unmodified lines649 \ 'right': ['vertical botright', 'vertical resize', &columns] }650 let ppos = s:getpos()651 try652 if s:present(a:dict, 'window')653+ if type(a:dict.window) == type({})654+ call s:popup(a:dict.window)655+ else653- execute 'keepalt' a:dict.window656+ execute 'keepalt' a:dict.window657+ endif658 elseif !s:splittable(a:dict)659 execute (tabpagenr()-1).'tabnew'660 else661 for [dir, triple] in items(directions)139 unmodified lines801 call s:dopopd()802 endif803 endfunction804 805+if has('nvim')806+ function s:create_popup(hl, opts) abort807+ let buf = nvim_create_buf(v:false, v:true)808+ let opts = extend({'relative': 'editor', 'style': 'minimal'}, a:opts)809+ let border = has_key(opts, 'border') ? remove(opts, 'border') : []810+ let win = nvim_open_win(buf, v:true, opts)811+ call setwinvar(win, '&winhighlight', 'NormalFloat:'..a:hl)812+ call setwinvar(win, '&colorcolumn', '')813+ if !empty(border)814+ call nvim_buf_set_lines(buf, 0, -1, v:true, border)815+ endif816+ return buf817+ endfunction818+else819+ function! s:create_popup(hl, opts) abort820+ let is_frame = has_key(a:opts, 'border')821+ let buf = is_frame ? '' : term_start(&shell, #{hidden: 1})822+ let id = popup_create(buf, #{823+ \ line: a:opts.row,824+ \ col: a:opts.col,825+ \ minwidth: a:opts.width,826+ \ minheight: a:opts.height,827+ \ zindex: 50 - is_frame,828+ \ })829+830+ if is_frame831+ call setwinvar(id, '&wincolor', a:hl)832+ call setbufline(winbufnr(id), 1, a:opts.border)833+ execute 'autocmd BufWipeout * ++once call popup_close('..id..')'834+ else835+ execute 'autocmd BufWipeout * ++once bwipeout! '..buf836+ endif837+ return winbufnr(id)838+ endfunction839+endif840+841+function! s:popup(opts) abort842+ " Size and position843+ let width = float2nr(&columns * a:opts.width)844+ let height = float2nr(&lines * a:opts.height)845+ let row = float2nr((&lines - height) / 2)846+ let col = float2nr((&columns - width) / 2)847+848+ " Border849+ let edges = get(a:opts, 'rounded', 1) ? ['╭', '╮', '╰', '╯'] : ['┌', '┐', '└', '┘']850+ let bar = repeat('─', width - 2)851+ let top = edges[0] .. bar .. edges[1]852+ let mid = '│' .. repeat(' ', width - 2) .. '│'853+ let bot = edges[2] .. bar .. edges[3]854+ let border = [top] + repeat([mid], height - 2) + [bot]855+856+ let highlight = get(a:opts, 'highlight', 'Comment')857+ let frame = s:create_popup(highlight, {858+ \ 'row': row, 'col': col, 'width': width, 'height': height, 'border': border859+ \ })860+ call s:create_popup('Normal', {861+ \ 'row': row + 1, 'col': col + 2, 'width': width - 4, 'height': height - 2862+ \ })863+ if has('nvim')864+ execute 'autocmd BufWipeout <buffer> bwipeout '..frame865+ endif866+endfunction867+868 let s:default_action = {869 \ 'ctrl-t': 'tab split',870 \ 'ctrl-x': 'split',871 \ 'ctrl-v': 'vsplit' }32 unmodified linesmodified:README-VIM.md+20 added-36 removed
182 unmodified lines183 | `options` | string/list | Options to fzf |184 | `dir` | string | Working directory |185 | `up`/`down`/`left`/`right` | number/string | (Layout) Window position and size (e.g. `20`, `50%`) |186 | `window` (Vim 8 / Neovim) | string | (Layout) Command to open fzf window (e.g. `vertical aboveleft 30new`) |187+| `window` (Vim 8 / Neovim) | dict | (Layout) Popup window settings (e.g. `{'width': 0.9, 'height': 0.6}`) |188 189 `options` entry can be either a string or a list. For simple cases, string190 should suffice, but prefer to use list type to avoid escaping issues.191 1 unmodified line193 call fzf#run({'options': '--reverse --prompt "C:\\Program Files\\"'})194 call fzf#run({'options': ['--reverse', '--prompt', 'C:\Program Files\']})195 ```196 197+When `window` entry is a dictionary, fzf will start in a popup window. The198+following options are allowed:199+200+- Required:201+ - `width` [float]202+ - `height` [float]203+- Optional:204+ - `highlight` [string default `'Comment'`]: Highlight group for border205+ - `rounded` [boolean default `v:true`]: Use rounded border206+207 `fzf#wrap`208 ----------209 210 We have seen that several aspects of `:FZF` command can be configured with75 unmodified lines286 - On GVim287 - On Terminal Vim with a non-default layout288 - `call fzf#run({'left': '30%'})` or `let g:fzf_layout = {'left': '30%'}`289 279-#### Starting fzf in Neovim floating window290+#### Starting fzf in a popup window291 292 ```vim282-" Using floating windows of Neovim to start fzf283-if has('nvim')284- function! FloatingFZF(width, height, border_highlight)285- function! s:create_float(hl, opts)286- let buf = nvim_create_buf(v:false, v:true)287- let opts = extend({'relative': 'editor', 'style': 'minimal'}, a:opts)288- let win = nvim_open_win(buf, v:true, opts)289- call setwinvar(win, '&winhighlight', 'NormalFloat:'.a:hl)290- call setwinvar(win, '&colorcolumn', '')291- return buf292- endfunction293-294- " Size and position295- let width = float2nr(&columns * a:width)296- let height = float2nr(&lines * a:height)297- let row = float2nr((&lines - height) / 2)298- let col = float2nr((&columns - width) / 2)299-300- " Border301- let top = '╭' . repeat('─', width - 2) . '╮'302- let mid = '│' . repeat(' ', width - 2) . '│'303- let bot = '╰' . repeat('─', width - 2) . '╯'304- let border = [top] + repeat([mid], height - 2) + [bot]305-306- " Draw frame307- let s:frame = s:create_float(a:border_highlight, {'row': row, 'col': col, 'width': width, 'height': height})308- call nvim_buf_set_lines(s:frame, 0, -1, v:true, border)309-310- " Draw viewport311- call s:create_float('Normal', {'row': row + 1, 'col': col + 2, 'width': width - 4, 'height': height - 2})312- autocmd BufWipeout <buffer> execute 'bwipeout' s:frame313- endfunction314-315- let g:fzf_layout = { 'window': 'call FloatingFZF(0.9, 0.6, "Comment")' }316-endif293+" Required:294+" - width [float]295+" - height [float]296+"297+" Optional:298+" - highlight [string default 'Comment']: Highlight group for border299+" - rounded [boolean default v:true]: Use rounded border300+let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }301 ```302 303 #### Hide statusline304 21 unmodified lines