Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floating Windows Support From Neovim #664

Closed
Piping opened this issue Jul 1, 2018 · 29 comments
Closed

Floating Windows Support From Neovim #664

Piping opened this issue Jul 1, 2018 · 29 comments

Comments

@Piping
Copy link

Piping commented Jul 1, 2018

Hi @junegunn

Neovim patch from @bfredl has a working version of floating windows implementation now.

Is it possible to add a split direction like float and then open fzf inside floating windows.

Here is a reference

@junegunn
Copy link
Owner

junegunn commented Jul 3, 2018

You can pass an arbitrary command as window option.

function! Foo()
  tabnew
  " or whatever
endfunction

call fzf#run({'window': 'call Foo()'})

See https://github.com/junegunn/fzf/blob/master/README-VIM.md#fzfrun for details.

@Piping
Copy link
Author

Piping commented Jul 3, 2018

@junegunn Thanks a bunch

@Piping Piping closed this as completed Jul 3, 2018
@Piping Piping reopened this Jul 3, 2018
@Piping
Copy link
Author

Piping commented Jul 3, 2018

function! Foo()
     if exists('*nvim_open_float_win')
         let b = nvim_create_buf(v:false)
         call nvim_buf_set_option(b, "buftype", "nofile")
         call nvim_buf_set_lines(b,0,-1,v:true,["test"])
         let opts = {'x':100, 'y':5, 'anchor': 'NE'}
         let w =  nvim_open_float_win(b,v:false,60,20,opts)
         hi Floating guibg=#000000
         call setwinvar(w, '&winhl', 'Normal:Floating')
         call setwinvar(w, '&number', 0)
     endif
 endfunction
 call fzf#run({'window': 'call Foo()'})

I tried the following script, but fzf windows is not opened inside the floating windows, any idea?

How does fzf window option locate the window?

@Piping Piping closed this as completed Jul 3, 2018
@Piping Piping reopened this Jul 3, 2018
@junegunn
Copy link
Owner

junegunn commented Jul 3, 2018

It opens in the "current" window where your cursor is positioned.

@bfredl
Copy link

bfredl commented Jul 3, 2018

@Piping change v:false to v:true in the open float call.

@Piping
Copy link
Author

Piping commented Jul 4, 2018

@bfredl Thanks!

This is what I got!

root local-vm - secure shell extension 0 8 43 7_3_2018 11_04_24 pm

root local-vm - secure shell extension 0 8 43 7_3_2018 11_14_02 pm

@junegunn
Awesome.

Although if I adjust the gui terminal size, the nvim crashed.

nvim: ../src/nvim/ui_compositor.c:226: compose_line: Assertion `until <= default_grid.Columns' failed.

@bfredl

@kassio
Copy link

kassio commented Mar 25, 2019

I got a nice result with the following:

let g:fzf_layout = { 'window': 'call FloatingFZF()' }
function! FloatingFZF()
  let buf = nvim_create_buf(v:false, v:true)
  call setbufvar(buf, '&signcolumn', 'no')

  let winheight = winheight(0)
  let winwidth = winwidth(0)

  let width = float2nr(winwidth-(winwidth*2/10))

  let opts = {
        \ 'relative': 'editor',
        \ 'row': &lines - 3,
        \ 'col': float2nr((winwidth-width)/2),
        \ 'width': width,
        \ 'height': &lines - 3
        \ }

  call nvim_open_win(buf, v:true, opts)
endfunction

_o-height____var_folders_kl_p5p1p0gd0lx7zjcvmn59b8s00000gp_T_nvimPW0pDR_3__FZF

I was trying to achieve a different visual with the "input" in the top and the list of files in a top-down layout, but with the current fzf customization tools I didn't find a way to do it.

@kassio
Copy link

kassio commented Mar 25, 2019

little change to keep the floating window centralized horizontally, regardless of any vertical splits.

function! FloatingFZF()
  let buf = nvim_create_buf(v:false, v:true)
  call setbufvar(buf, '&signcolumn', 'no')

  let width = float2nr(&columns - (&columns * 2 / 10))
  let height = &lines - 3
  let y = &lines - 3
  let x = float2nr((&columns - width) / 2)

  let opts = {
        \ 'relative': 'editor',
        \ 'row': y,
        \ 'col': x,
        \ 'width': width,
        \ 'height': height
        \ }

  call nvim_open_win(buf, v:true, opts)
endfunction

@junegunn
Copy link
Owner

a different visual with the "input" in the top and the list of files in a top-down layout

See Layout section in the man page.

@kassio
Copy link

kassio commented Mar 26, 2019

Thanks @junegunn, I got the visual that I wanted:

_folders_kl_p5p1p0gd0lx7zjcvmn59b8s00000gp_T_nvime4Ibru_3__FZF

let $FZF_DEFAULT_OPTS='--layout=reverse'
let g:fzf_layout = { 'window': 'call FloatingFZF()' }

function! FloatingFZF()
  let buf = nvim_create_buf(v:false, v:true)
  call setbufvar(buf, '&signcolumn', 'no')

  let height = &lines - 3
  let width = float2nr(&columns - (&columns * 2 / 10))
  let col = float2nr((&columns - width) / 2)

  let opts = {
        \ 'relative': 'editor',
        \ 'row': 1,
        \ 'col': col,
        \ 'width': width,
        \ 'height': height
        \ }

  call nvim_open_win(buf, v:true, opts)
endfunction

@glepnir
Copy link

glepnir commented Mar 27, 2019

@kassio hi i use this function, how to hide line number in float window?setlocal nonumber ? where it should be write

@sassanh
Copy link

sassanh commented Mar 27, 2019

change

call nvim_open_win(buf, v:true, opts)

to

let win = nvim_open_win(buf, v:true, opts)
call setwinvar(win, '&number', 0)

@glepnir
Copy link

glepnir commented Mar 27, 2019

@sassanh nothing change.

@sassanh
Copy link

sassanh commented Mar 27, 2019

It's working for me:

    let win = nvim_open_win(l:buf, v:true, l:opts)
    call timer_start(0, {-> execute(':call setwinvar(' . l:win . ', "&number", 1)')})

And this (not changing anything in the function):

autocmd! FileType fzf
autocmd  FileType fzf setlocal nonumber

@kassio
Copy link

kassio commented Mar 27, 2019

You can use:

  au FileType fzf set nonu nornu

Just saw that @sassanh already mention that, sorry for the noise.

@glepnir
Copy link

glepnir commented Mar 27, 2019

@kassio @sassanh add this call setwinvar(win, '&relativenumber', 0),works well,by the way how to change fzf backgournd highlight.

@sassanh
Copy link

sassanh commented Mar 27, 2019

Put this at the end of your init.vim (or just after color scheme is set in your init.vim)

highlight NormalFloat cterm=NONE ctermfg=14 ctermbg=0 gui=NONE guifg=#93a1a1 guibg=#002931

MaryHal added a commit to MaryHal/nvim_config that referenced this issue May 26, 2019
elentok added a commit to elentok/dotfiles that referenced this issue Jul 6, 2019
@AhmedAbdulrahman
Copy link

Hi @kassio Thanks for the cool tips, do you know how can I get ride of that annoying Flashing background color when I open fzf ?
Kapture 2019-10-13 at 7 12 46

@dagadbm
Copy link

dagadbm commented Dec 4, 2019

" floating fzf
if has('nvim')
  let $FZF_DEFAULT_OPTS .= ' --layout=reverse'

  function! FloatingFZF()
    let buf = nvim_create_buf(v:false, v:true)

   " here be dragoons
    let height = &lines
    let width = float2nr(&columns - (&columns * 2 / 10))
    let col = float2nr((&columns - width) / 2)
    let col_offset = &columns / 10
    let opts = {
          \ 'relative': 'editor',
          \ 'row': 1,
          \ 'col': col + col_offset,
          \ 'width': width * 2 / 1,
          \ 'height': height / 2
          \ }

    let win = nvim_open_win(buf, v:true, opts)
    call setwinvar(win, '&winhl', 'NormalFloat:TabLine')

  " this is to remove all line numbers and so on from the window
    setlocal
          \ buftype=nofile
          \ nobuflisted
          \ bufhidden=hide
          \ nonumber
          \ norelativenumber
          \ signcolumn=no
  endfunction

  let g:fzf_layout = { 'window': 'call FloatingFZF()' }
endif

this is a mix from junegunn dotfiles, comment from this thread and space-vim config
although i have no idea what this does exactly but it seems to work.

if you guys have better alternatives do share here !

@Karmenzind
Copy link

call setwinvar(win, '&winhl', 'NormalFloat:TabLine') will be better @dagadbm

@kassio
Copy link

kassio commented Dec 10, 2019

@AhmedAbdulrahman is that still happening? For me, it's not happening. 🤷‍♂

@dagadbm
Copy link

dagadbm commented Dec 10, 2019

call setwinvar(win, '&winhl', 'NormalFloat:TabLine') will be better @dagadbm

changed it!

why is it better?

@alfunx
Copy link

alfunx commented Dec 10, 2019

You usually don't need the setlocal stuff anymore if you set the minimal stile in opts, e.g.:

" floating fzf
if has('nvim')
  let $FZF_DEFAULT_OPTS .= ' --layout=reverse'

  function! FloatingFZF()
    let height = &lines
    let width = float2nr(&columns - (&columns * 2 / 10))
    let col = float2nr((&columns - width) / 2)
    let col_offset = &columns / 10
    let opts = {
          \ 'relative': 'editor',
          \ 'row': 1,
          \ 'col': col + col_offset,
          \ 'width': width * 2 / 1,
          \ 'height': height / 2,
          \ 'style': 'minimal'
          \ }
    let buf = nvim_create_buf(v:false, v:true)
    let win = nvim_open_win(buf, v:true, opts)
    call setwinvar(win, '&winhl', 'NormalFloat:TabLine')
  endfunction

  let g:fzf_layout = { 'window': 'call FloatingFZF()' }
endif

@nstetter
Copy link

How would I change the background color for the FloatWindow in @alfunx example?

@marcov
Copy link

marcov commented Dec 17, 2019

How would I change the background color for the FloatWindow in @alfunx example?

@nstetter use highlight TabLine ctermbg=<code>

@alfunx
Copy link

alfunx commented Dec 17, 2019

@nstetter If you want to change the color for all floating windows and just use that color, set the NormalFloat group and remove the following line:

call setwinvar(win, '&winhl', 'NormalFloat:TabLine')

If you want to use a different colors for that specific floating window (while leaving the other ones as they are now), exchange Tabline with some other highlighting group, e.g.:

call setwinvar(win, '&winhl', 'NormalFloat:Error')

You can also define a highlighting group yourself and then use that, e.g.

highlight SpecificFloat ctermbg=... ctermfg=...

@nstetter
Copy link

Thanks for the explanation.
I found it more applicable to change the background color using the minimal style option and the FZF_DEFAULT_OPTS variable.

One thing I am still having issues with is the layout option. I set --layout=reverse
But it does not get respected, as can be seen in the screenshot:
image

My whole config is:

    " Using floating windows of Neovim to start fzf
    if has('nvim-0.4.0')
      let $FZF_DEFAULT_OPTS .= '--color=bg:#20242C --border --layout=reverse'
      function! FloatingFZF()
	let width = float2nr(&columns * 0.9)
	let height = float2nr(&lines * 0.6)
	let opts = { 'relative': 'editor',
		   \ 'row': (&lines - height) / 2,
		   \ 'col': (&columns - width) / 2,
		   \ 'width': width,
		   \ 'height': height,
		   \ 'style': 'minimal'
		   \}

	let win = nvim_open_win(nvim_create_buf(v:false, v:true), v:true, opts)
	call setwinvar(win, '&winhighlight', 'NormalFloat:TabLine')
      endfunction

      let g:fzf_layout = { 'window': 'call FloatingFZF()' }
    endif

When echoing $FZF_DEFAULT_OPTS inside vim I get:

--multi
--color=dark
--color=fg:-1,bg:-1,hl:#c678dd,fg+:#ffffff,bg+:#4b5263,hl+:#d858fe
--color=info:#98c379,prompt:#61afef,pointer:#be5046,marker:#e5c07b,spinner:#61afef,header:#61afef
--color=bg:#20242C --border --layout=reverse

Any idea why the reverse layout does not work like that?

@junegunn
Copy link
Owner

@nstetter 👉 #916

@yutkat
Copy link

yutkat commented Feb 20, 2020

https://github.com/yuki-ycino/fzf-preview.vim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests