[vim] Add fzf#install() for downloading fzf binary

This commit is contained in:
Junegunn Choi 2020-02-14 14:01:46 +09:00
parent 4fb410a93c
commit 6c03571887
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
3 changed files with 62 additions and 46 deletions

View File

@ -1,6 +1,50 @@
FZF Vim integration 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 Summary
------- -------

View File

@ -138,39 +138,18 @@ page][windows-wiki].
### As Vim plugin ### As Vim plugin
Once you have fzf installed, you can enable it inside Vim simply by adding the If you use
directory to `&runtimepath` in your Vim configuration file. The path may [vim-plug](https://github.com/junegunn/vim-plug), add this line to your Vim
differ depending on the package manager. configuration file:
```vim ```vim
" If installed using Homebrew Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
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 `fzf#install()` makes sure that you have the latest binary, but it's optional,
written as: so you can omit it if you use a plugin manager that doesn't support hooks.
```vim For more installation options, see [README-VIM.md](README-VIM.md).
" If installed using Homebrew
Plug '/usr/local/opt/fzf'
" If installed using git
Plug '~/.fzf'
```
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.
```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.
```
Upgrading fzf Upgrading fzf
------------- -------------

View File

@ -119,40 +119,30 @@ let s:default_layout = { 'down': '~40%' }
let s:layout_keys = ['window', 'up', 'down', 'left', 'right'] let s:layout_keys = ['window', 'up', 'down', 'left', 'right']
let s:fzf_go = s:base_dir.'/bin/fzf' let s:fzf_go = s:base_dir.'/bin/fzf'
let s:fzf_tmux = s:base_dir.'/bin/fzf-tmux' let s:fzf_tmux = s:base_dir.'/bin/fzf-tmux'
let s:installed = 0
let s:cpo_save = &cpo let s:cpo_save = &cpo
set cpo&vim set cpo&vim
function! s:download_bin() function! fzf#install()
if s:installed
return 0
endif
if s:is_win && !has('win32unix') if s:is_win && !has('win32unix')
let script = s:base_dir.'/install.ps1' let script = s:base_dir.'/install.ps1'
if !filereadable(script) if !filereadable(script)
return 0 throw script.' not found'
endif endif
let script = 'powershell -ExecutionPolicy Bypass -file ' . script let script = 'powershell -ExecutionPolicy Bypass -file ' . script
else else
let script = s:base_dir.'/install' let script = s:base_dir.'/install'
if !executable(script) if !executable(script)
return 0 throw script.' not found'
endif endif
let script .= ' --bin' let script .= ' --bin'
endif endif
if input('fzf executable not found. Download binary? (y/n) ') !~? '^y' call s:warn('Running fzf installer ...')
return 0
end
redraw
echo
call s:warn('Downloading fzf binary. Please wait ...')
let s:installed = 1
call system(script) call system(script)
return v:shell_error == 0 if v:shell_error
throw 'Failed to download fzf: '.script
endif
endfunction endfunction
function! s:fzf_exec() function! s:fzf_exec()
@ -161,7 +151,10 @@ function! s:fzf_exec()
let s:exec = s:fzf_go let s:exec = s:fzf_go
elseif executable('fzf') elseif executable('fzf')
let s:exec = '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() return s:fzf_exec()
else else
redraw redraw