Skip to content

Commit

Permalink
Merge pull request #44 from enthudave/master
Browse files Browse the repository at this point in the history
add a config variable for the 'chosenfile'
  • Loading branch information
francoiscabrol authored Feb 5, 2018
2 parents 4f4ff45 + 7777a81 commit 5f71be7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ If you want to see vim opening ranger when you open a directory (ex: nvim ./dir)
let g:NERDTreeHijackNetrw = 0 // add this line if you use NERDTree
let g:ranger_replace_netrw = 1 // open ranger when vim open a directory
```
Ranger.vim uses a temporary file to store the path that was chosen, `/tmp/chosenfile` by default.
This can be a problem if you do not have write permissions for the `/tmp` directory, for example on Android.
There is a configuration variable for this called `g:ranger_choice_file`, this must be set to the
path for a file that doesn't yet exist (this file is created when choosing a file and removed afterwards).


43 changes: 29 additions & 14 deletions plugin/ranger.vim
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,53 @@


" ================ Ranger =======================
if exists('g:ranger_choice_file')
if empty(glob(g:ranger_choice_file))
let s:choice_file_path = g:ranger_choice_file
else
echom "Message from *Ranger.vim* :"
echom "You've set the g:ranger_choice_file variable."
echom "Please use the path for a file that does not already exist."
echom "Using /tmp/chosenfile for now..."
endif
endif

if !exists('s:choice_file_path')
let s:choice_file_path = '/tmp/chosenfile'
endif

if has('nvim')
function! OpenRangerIn(path, edit_cmd)
let currentPath = expand(a:path)
let rangerCallback = { 'name': 'ranger', 'edit_cmd': a:edit_cmd }
function! rangerCallback.on_exit(id, code, _event)
silent! Bclose!
try
if filereadable('/tmp/chosenfile')
exec system('sed -ie "s/ /\\\ /g" /tmp/chosenfile')
exec 'argadd ' . system('cat /tmp/chosenfile | tr "\\n" " "')
exec self.edit_cmd . system('head -n1 /tmp/chosenfile')
call system('rm /tmp/chosenfile')
if filereadable(s:choice_file_path)
exec system('sed -ie "s/ /\\\ /g" ' . s:choice_file_path)
exec 'argadd ' . system('cat ' . s:choice_file_path . ' | tr "\\n" " "')
exec self.edit_cmd . system('head -n1 ' . s:choice_file_path)
call system('rm ' . s:choice_file_path)
endif
endtry
endfunction
enew
if isdirectory(currentPath)
call termopen('ranger --choosefiles=/tmp/chosenfile "' . currentPath . '"', rangerCallback)
call termopen('ranger --choosefiles=' . s:choice_file_path . ' "' . currentPath . '"', rangerCallback)
else
call termopen('ranger --choosefiles=/tmp/chosenfile --selectfile="' . currentPath . '"', rangerCallback)
call termopen('ranger --choosefiles=' . s:choice_file_path . ' --selectfile="' . currentPath . '"', rangerCallback)
endif
startinsert
endfunction
else
function! OpenRangerIn(path, edit_cmd)
let currentPath = expand(a:path)
exec 'silent !ranger --choosefiles=/tmp/chosenfile --selectfile="' . currentPath . '"'
if filereadable('/tmp/chosenfile')
exec system('sed -ie "s/ /\\\ /g" /tmp/chosenfile')
exec 'argadd ' . system('cat /tmp/chosenfile | tr "\\n" " "')
exec a:edit_cmd . system('head -n1 /tmp/chosenfile')
call system('rm /tmp/chosenfile')
exec 'silent !ranger --choosefiles=' . s:choice_file_path . ' --selectfile="' . currentPath . '"'
if filereadable(s:choice_file_path)
exec system('sed -ie "s/ /\\\ /g" ' . s:choice_file_path)
exec 'argadd ' . system('cat ' . s:choice_file_path . ' | tr "\\n" " "')
exec a:edit_cmd . system('head -n1 ' . s:choice_file_path)
call system('rm ' . s:choice_file_path)
endif
redraw!
endfun
Expand Down Expand Up @@ -86,7 +101,7 @@ endfunction
" Open Ranger in the directory passed by argument
function! OpenRangerOnVimLoadDir(argv_path)
" Open Ranger
let path = expand(a:argv_path)
let path = expand(a:argv_path)
call OpenRangerIn(path, "edit")

" Delete the empty buffer created by vim
Expand Down

0 comments on commit 5f71be7

Please sign in to comment.