vim-plug icon indicating copy to clipboard operation
vim-plug copied to clipboard

Is there a megic to preview the files when browse help files and README.md

Open applejwjcat opened this issue 5 years ago • 1 comments

I'm so sorry to botter you, but I go throuch all the man pages of fzf and vim-plug, I did't find the solution. I have saw the extra tips in the vim-plus wiki, I found this:

function! s:plug_help_sink(line)
  let dir = g:plugs[a:line].dir
  for pat in ['doc/*.txt', 'README.md']
    let match = get(split(globpath(dir, pat), "\n"), 0, '')
    if len(match)
      execute 'tabedit' match
      return
    endif
  endfor
  tabnew
  execute 'Explore' dir
endfunction

command! PlugHelp call fzf#run(fzf#wrap({
  \ 'source': sort(keys(g:plugs)),
  \ 'sink':   function('s:plug_help_sink')}))

but I add the preview option to it like this:

command! PlugHelp call fzf#run(fzf#wrap({
  \ 'source': sort(keys(g:plugs)),
  \ 'sink':   function('s:plug_help_sink'),
  \ 'options': '--layout=reverse --info=inline --preview="~/.config/nvim/plugged/fzf.vim/bin/preview.sh {}"'}))

But it didn't work correctly! I thought it may bacause the preview script accept the source lines and it can't open the 'lines'----it's not the actual file. I want to write a function to preview the files, but I don't know how to translate the vimscript function to the --preview argument? Can you help me ? Only once, I may solve it by myself when meet the similar situation. Thanks, I am so sorry to waste you time!

applejwjcat avatar Sep 07 '20 06:09 applejwjcat

This is my temporary solution:

function! s:plug_help_sink(line)
  let dir = g:plugs[a:line].dir
  for pat in ['doc/*.txt', 'README.*']
    let match = get(split(globpath(dir, pat), "\n"), 0, '')
    if len(match)
      execute 'tabedit' match
      if !filereadable(expand($MYNVIM) . '/generate/plugins_path/' . a:line)
          call writefile(add([],match),expand($MYNVIM) . '/generate/plugins_path/' . a:line)
      endif
      return
    endif
  endfor
  tabnew
  execute 'Explore' dir
endfunction

command! PlugHelp call fzf#run(fzf#wrap({
  \ 'source': sort(keys(g:plugs)),
  \ 'sink':   function('s:plug_help_sink'),
  \ 'options': '--layout=reverse --info=inline --preview="cat $MYNVIM/generate/plugins_path/{} | ~/.config/nvim/generate/preview.sh " '}))

I rewrite the preview.sh to make it accept the standard input stream, and write the path of plugins' help-file into some files. Then I throw those to preview.sh. It's so complicated! But I can't genarate the better solution unless rewite the interface of preview to accept reference function! Can you give me some advice? What better if the preview call accept the vimscript function!

applejwjcat avatar Sep 07 '20 11:09 applejwjcat

function! s:plug_help_source()
  let lines = []
  let longest = keys(g:plugs)->map({_, v -> strlen(v)})->max()
  for [name, plug] in items(g:plugs)
    let matches = []
    for pat in ['doc/*.txt', 'README.md']
      let match = get(split(globpath(plug.dir, pat), "\n"), 0, '')
      if len(match)
        call add(lines, printf("%-"..longest.."s\t%s\t%s", name, fnamemodify(match, ':t'), match))
      endif
    endfor
  endfor
  return sort(lines)
endfunction

command! PlugHelp call fzf#run(fzf#wrap({
  \ 'source': s:plug_help_source(),
  \ 'sink':   { line -> execute('tabedit '..split(line)[-1]) },
  \ 'options': ['--reverse', '--delimiter=\t', '--with-nth=1..2', '--preview', 'bat --style=numbers --color=always {-1}']}))

junegunn avatar Mar 30 '24 11:03 junegunn