Skip to content

Commit

Permalink
[vim] Support relative-to-window positioning of popup (#2443)
Browse files Browse the repository at this point in the history
Co-authored-by: Junegunn Choi <junegunn.c@gmail.com>
  • Loading branch information
tpict and junegunn committed Apr 17, 2021
1 parent c7c5e76 commit 8b0e1f9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
10 changes: 9 additions & 1 deletion README-VIM.md
Expand Up @@ -127,9 +127,15 @@ let g:fzf_action = {
\ 'ctrl-v': 'vsplit' }
" Default fzf layout
" - Popup window
" - Popup window (center of the screen)
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }
" - Popup window (center of the current window)
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true } }
" - Popup window (anchored to the bottom of the current window)
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true, 'yoffset': 1.0 } }
" - down / up / left / right
let g:fzf_layout = { 'down': '40%' }
Expand Down Expand Up @@ -302,6 +308,7 @@ following options are allowed:
- Optional:
- `yoffset` [float default 0.5 range [0 ~ 1]]
- `xoffset` [float default 0.5 range [0 ~ 1]]
- `relative` [boolean default v:false]
- `border` [string default `rounded`]: Border style
- `rounded` / `sharp` / `horizontal` / `vertical` / `top` / `bottom` / `left` / `right` / `no[ne]`

Expand Down Expand Up @@ -410,6 +417,7 @@ The latest versions of Vim and Neovim include builtin terminal emulator
" Optional:
" - xoffset [float default 0.5 range [0 ~ 1]]
" - yoffset [float default 0.5 range [0 ~ 1]]
" - relative [boolean default v:false]
" - border [string default 'rounded']: Border style
" - 'rounded' / 'sharp' / 'horizontal' / 'vertical' / 'top' / 'bottom' / 'left' / 'right'
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }
Expand Down
16 changes: 12 additions & 4 deletions doc/fzf.txt
@@ -1,4 +1,4 @@
fzf.txt fzf Last change: January 3 2021
fzf.txt fzf Last change: April 17 2021
FZF - TABLE OF CONTENTS *fzf* *fzf-toc*
==============================================================================

Expand Down Expand Up @@ -155,9 +155,15 @@ Examples~
\ 'ctrl-v': 'vsplit' }
" Default fzf layout
" - Popup window
" - Popup window (center of the screen)
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }
" - Popup window (center of the current window)
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true } }
" - Popup window (anchored to the bottom of the current window)
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true, 'yoffset': 1.0 } }
" - down / up / left / right
let g:fzf_layout = { 'down': '40%' }
Expand Down Expand Up @@ -318,6 +324,7 @@ following options are allowed:
- Optional:
- `yoffset` [float default 0.5 range [0 ~ 1]]
- `xoffset` [float default 0.5 range [0 ~ 1]]
- `relative` [boolean default v:false]
- `border` [string default `rounded`]: Border style
- `rounded` / `sharp` / `horizontal` / `vertical` / `top` / `bottom` / `left` / `right` / `no[ne]`

Expand Down Expand Up @@ -372,15 +379,15 @@ last `fullscreen` argument of `fzf#wrap` (see :help <bang>).
Our `:LS` command will be much more useful if we can pass a directory argument
to it, so that something like `:LS /tmp` is possible.
>
command! -bang -complete=dir -nargs=* LS
command! -bang -complete=dir -nargs=? LS
\ call fzf#run(fzf#wrap({'source': 'ls', 'dir': <q-args>}, <bang>0))
<
Lastly, if you have enabled `g:fzf_history_dir`, you might want to assign a
unique name to our command and pass it as the first argument to `fzf#wrap`.
>
" The query history for this command will be stored as 'ls' inside g:fzf_history_dir.
" The name is ignored if g:fzf_history_dir is not defined.
command! -bang -complete=dir -nargs=* LS
command! -bang -complete=dir -nargs=? LS
\ call fzf#run(fzf#wrap('ls', {'source': 'ls', 'dir': <q-args>}, <bang>0))
<

Expand Down Expand Up @@ -423,6 +430,7 @@ Starting fzf in a popup window~
" Optional:
" - xoffset [float default 0.5 range [0 ~ 1]]
" - yoffset [float default 0.5 range [0 ~ 1]]
" - relative [boolean default v:false]
" - border [string default 'rounded']: Border style
" - 'rounded' / 'sharp' / 'horizontal' / 'vertical' / 'top' / 'bottom' / 'left' / 'right'
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }
Expand Down
16 changes: 12 additions & 4 deletions plugin/fzf.vim
Expand Up @@ -972,11 +972,19 @@ else
endif

function! s:popup(opts) abort
let xoffset = get(a:opts, 'xoffset', 0.5)
let yoffset = get(a:opts, 'yoffset', 0.5)
let relative = get(a:opts, 'relative', 0)

" Use current window size for positioning relatively positioned popups
let columns = relative ? winwidth(0) : &columns
let lines = relative ? winheight(0) : &lines

" Size and position
let width = min([max([8, a:opts.width > 1 ? a:opts.width : float2nr(&columns * a:opts.width)]), &columns])
let height = min([max([4, a:opts.height > 1 ? a:opts.height : float2nr(&lines * a:opts.height)]), &lines - has('nvim')])
let row = float2nr(get(a:opts, 'yoffset', 0.5) * (&lines - height))
let col = float2nr(get(a:opts, 'xoffset', 0.5) * (&columns - width))
let width = min([max([8, a:opts.width > 1 ? a:opts.width : float2nr(columns * a:opts.width)]), columns])
let height = min([max([4, a:opts.height > 1 ? a:opts.height : float2nr(lines * a:opts.height)]), lines - has('nvim')])
let row = float2nr(yoffset * (lines - height)) + (relative ? win_screenpos(0)[0] : 0)
let col = float2nr(xoffset * (columns - width)) + (relative ? win_screenpos(0)[1] : 0)

" Managing the differences
let row = min([max([0, row]), &lines - has('nvim') - height])
Expand Down

0 comments on commit 8b0e1f9

Please sign in to comment.