yuki-yano / fern-preview.vim

Add a file preview window to fern.vim.
MIT License
44 stars 5 forks source link

Only map keys when the preview window is open #19

Open arashm opened 3 years ago

arashm commented 3 years ago

Thanks for the great set of Plugins. I have this piece of config as suggested in the readme:

function! s:fern_settings() abort
  ....
  nmap <silent> <buffer> <C-d> <Plug>(fern-action-preview:scroll:down:half)
  nmap <silent> <buffer> <C-u> <Plug>(fern-action-preview:scroll:up:half)
endfunction

I use <C-d> and <C-u> in the fern itself to jump up and down the list of directories/files as well, but setting it here causes the fern/vim to throw an error:

Error detected while processing function fern_preview#half_down[2]..<SNR>187_info:
line    3:
E716: Key not present in Dictionary: "width,   'height': l:info.height,   'topline': l:info.firstline }"

Is there a way to only map those keys when a preview window is open, and do the regular <C-d>, <C-u> when there's no preview window?

knupska commented 2 years ago

I also wanted this behaviour and was able to cobble this together:

if !exists("b:fern_is_preview")
  let b:fern_is_preview = 0
endif
function! FernPreviewToggle()
  if b:fern_is_preview
    :execute "normal \<Plug>(fern-action-preview:close)"
    :execute "normal \<Plug>(fern-action-preview:auto:disable)"
    nunmap <buffer> <C-d>
    nunmap <buffer> <C-u>
    let b:fern_is_preview = 0
  else
    :execute "normal \<Plug>(fern-action-preview:open)"
    :execute "normal \<Plug>(fern-action-preview:auto:enable)<Plug>(fern-action-preview:open)"
    nmap <silent> <buffer> <C-d> <Plug>(fern-action-preview:scroll:down:half)
    nmap <silent> <buffer> <C-u> <Plug>(fern-action-preview:scroll:up:half)
    let b:fern_is_preview = 1
  endif
endfunction

nmap <silent> <buffer> p :call FernPreviewToggle()<CR>

It should be placed within the init_fern() function as outlined here, so that it acts upon the buffer that fern is in.

It will only work so long as the preview is open/closed via the function.