From 6c03571887cda7c0253c0a28439230cc8fcea3e6 Mon Sep 17 00:00:00 2001
From: Junegunn Choi <junegunn.c@gmail.com>
Date: Fri, 14 Feb 2020 14:01:46 +0900
Subject: [PATCH] [vim] Add fzf#install() for downloading fzf binary

---
 README-VIM.md  | 44 ++++++++++++++++++++++++++++++++++++++++++++
 README.md      | 35 +++++++----------------------------
 plugin/fzf.vim | 29 +++++++++++------------------
 3 files changed, 62 insertions(+), 46 deletions(-)

diff --git a/README-VIM.md b/README-VIM.md
index d178be35..6bf84fa7 100644
--- a/README-VIM.md
+++ b/README-VIM.md
@@ -1,6 +1,50 @@
 FZF Vim integration
 ===================
 
+Installation
+------------
+
+Once you have fzf installed, you can enable it inside Vim simply by adding the
+directory to `&runtimepath` in your Vim configuration file. The path may
+differ depending on the package manager.
+
+```vim
+" If installed using Homebrew
+set rtp+=/usr/local/opt/fzf
+
+" If installed using git
+set rtp+=~/.fzf
+```
+
+If you use [vim-plug](https://github.com/junegunn/vim-plug), the same can be
+written as:
+
+```vim
+" If installed using Homebrew
+Plug '/usr/local/opt/fzf'
+
+" If installed using git
+Plug '~/.fzf'
+```
+
+But if you want the latest Vim plugin file from GitHub rather than the one
+included in the package, write:
+
+```vim
+Plug 'junegunn/fzf'
+```
+
+The Vim plugin will pick up fzf binary available on the system. If fzf is not
+found on `$PATH`, it will ask you if it should download the latest binary for
+you.
+
+To make sure that you have the latest version of the binary, set up
+post-update hook like so:
+
+```vim
+Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
+```
+
 Summary
 -------
 
diff --git a/README.md b/README.md
index 42f15efd..e138ebb6 100644
--- a/README.md
+++ b/README.md
@@ -138,39 +138,18 @@ page][windows-wiki].
 
 ### As Vim plugin
 
-Once you have fzf installed, you can enable it inside Vim simply by adding the
-directory to `&runtimepath` in your Vim configuration file. The path may
-differ depending on the package manager.
+If you use
+[vim-plug](https://github.com/junegunn/vim-plug), add this line to your Vim
+configuration file:
 
 ```vim
-" If installed using Homebrew
-set rtp+=/usr/local/opt/fzf
-
-" If installed using git
-set rtp+=~/.fzf
-```
-
-If you use [vim-plug](https://github.com/junegunn/vim-plug), the same can be
-written as:
-
-```vim
-" If installed using Homebrew
-Plug '/usr/local/opt/fzf'
-
-" If installed using git
-Plug '~/.fzf'
+Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
 ```
 
-But instead of separately installing fzf on your system (using Homebrew or
-"git clone") and enabling it on Vim (adding it to `&runtimepath`), you can use
-vim-plug to do both.
+`fzf#install()` makes sure that you have the latest binary, but it's optional,
+so you can omit it if you use a plugin manager that doesn't support hooks.
 
-```vim
-" PlugInstall and PlugUpdate will clone fzf in ~/.fzf and run the install script
-Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
-  " Both options are optional. You don't have to install fzf in ~/.fzf
-  " and you don't have to run the install script if you use fzf only in Vim.
-```
+For more installation options, see [README-VIM.md](README-VIM.md).
 
 Upgrading fzf
 -------------
diff --git a/plugin/fzf.vim b/plugin/fzf.vim
index 8b88ae4b..2585570a 100644
--- a/plugin/fzf.vim
+++ b/plugin/fzf.vim
@@ -119,40 +119,30 @@ let s:default_layout = { 'down': '~40%' }
 let s:layout_keys = ['window', 'up', 'down', 'left', 'right']
 let s:fzf_go = s:base_dir.'/bin/fzf'
 let s:fzf_tmux = s:base_dir.'/bin/fzf-tmux'
-let s:installed = 0
 
 let s:cpo_save = &cpo
 set cpo&vim
 
-function! s:download_bin()
-  if s:installed
-    return 0
-  endif
-
+function! fzf#install()
   if s:is_win && !has('win32unix')
     let script = s:base_dir.'/install.ps1'
     if !filereadable(script)
-      return 0
+      throw script.' not found'
     endif
     let script = 'powershell -ExecutionPolicy Bypass -file ' . script
   else
     let script = s:base_dir.'/install'
     if !executable(script)
-      return 0
+      throw script.' not found'
     endif
     let script .= ' --bin'
   endif
 
-  if input('fzf executable not found. Download binary? (y/n) ') !~? '^y'
-    return 0
-  end
-
-  redraw
-  echo
-  call s:warn('Downloading fzf binary. Please wait ...')
-  let s:installed = 1
+  call s:warn('Running fzf installer ...')
   call system(script)
-  return v:shell_error == 0
+  if v:shell_error
+    throw 'Failed to download fzf: '.script
+  endif
 endfunction
 
 function! s:fzf_exec()
@@ -161,7 +151,10 @@ function! s:fzf_exec()
       let s:exec = s:fzf_go
     elseif executable('fzf')
       let s:exec = 'fzf'
-    elseif s:download_bin()
+    elseif input('fzf executable not found. Download binary? (y/n) ') =~? '^y'
+      redraw
+      echo
+      call fzf#install()
       return s:fzf_exec()
     else
       redraw
-- 
GitLab