Skip to content
Myr

[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

3 files+111 added−61 removed

modified:doc/fzf.txt+23 added-24 removed
1fzf.txt fzf Last change: November 23 20191fzf.txt fzf Last change: February 3 20202FZF - TABLE OF CONTENTS *fzf* *fzf-toc*3==============================================================================45 FZF Vim integration
4 unmodified lines
10 fzf#run11 fzf#wrap12 Tips13 fzf inside terminal buffer14 Starting fzf in Neovim floating window14 Starting fzf in a popup window15 Hide statusline16 License1718FZF VIM INTEGRATION *fzf-vim-integration*
184 unmodified lines
203 `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 ---------------------------+---------------+----------------------------------------------------------------------209210`options` entry can be either a string or a list. For simple cases, string211should 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<216When `window` entry is a dictionary, fzf will start in a popup window. The217following options are allowed:218219 - 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 border225226227FZF#WRAP228==============================================================================229230 *fzf#wrap*
70 unmodified lines
301 - On Terminal Vim with a non-default layout302 - `call fzf#run({'left': '30%'})` or `let g:fzf_layout = {'left': '30%'}`303304294Starting fzf in Neovim floating window~295 *fzf-starting-fzf-in-neovim-floating-window*305Starting 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'300301 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 }309310 let win = nvim_open_win(nvim_create_buf(v:false, v:true), v:true, opts)311 call setwinvar(win, '&winhighlight', 'NormalFloat:Normal')312 endfunction313314 let g:fzf_layout = { 'window': 'call FloatingFZF()' }315 endif316308 " 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<317318Hide statusline~319 *fzf-hide-statusline*
24 unmodified lines
modified:plugin/fzf.vim+68 added-1 removed
648 unmodified lines
649 \ '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 lines
801 call s:dopopd()802 endif803endfunction804805if 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 endfunction818else819 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 \ })829830 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 endfunction839endif840841function! 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)847848 " 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]855856 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 endif866endfunction867868let s:default_action = {869 \ 'ctrl-t': 'tab split',870 \ 'ctrl-x': 'split',871 \ 'ctrl-v': 'vsplit' }
32 unmodified lines
modified:README-VIM.md+20 added-36 removed
182 unmodified lines
183| `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}`) |188189`options` entry can be either a string or a list. For simple cases, string190should suffice, but prefer to use list type to avoid escaping issues.191
1 unmodified line
193call fzf#run({'options': '--reverse --prompt "C:\\Program Files\\"'})194call fzf#run({'options': ['--reverse', '--prompt', 'C:\Program Files\']})195```196197When `window` entry is a dictionary, fzf will start in a popup window. The198following options are allowed:199200- 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 border206207`fzf#wrap`208----------209210We have seen that several aspects of `:FZF` command can be configured with
75 unmodified lines
286- On GVim287- On Terminal Vim with a non-default layout288 - `call fzf#run({'left': '30%'})` or `let g:fzf_layout = {'left': '30%'}`289279#### Starting fzf in Neovim floating window290#### Starting fzf in a popup window291292```vim282" Using floating windows of Neovim to start fzf283if 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 endfunction293294 " 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)299300 " 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]305306 " 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)309310 " 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 endfunction314315 let g:fzf_layout = { 'window': 'call FloatingFZF(0.9, 0.6, "Comment")' }316endif293" 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 border300let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }301```302303#### Hide statusline304
21 unmodified lines