Skip to content

Commit 98e3e73

Browse files
committed
edit files using a relative path when possible
1 parent 118afa2 commit 98e3e73

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

plugin/ranger.vim

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,45 +44,40 @@ if !exists('s:choice_file_path')
4444
let s:choice_file_path = '/tmp/chosenfile'
4545
endif
4646

47+
function! s:EditFiles(cmd)
48+
try
49+
if filereadable(s:choice_file_path)
50+
let cwd = getcwd()
51+
for f in readfile(s:choice_file_path)
52+
exec a:cmd . (f[0:len(cwd)-1] ==# cwd ? f[len(cwd)+1:-1] : f)
53+
endfor
54+
call delete(s:choice_file_path)
55+
endif
56+
endtry
57+
endfunction
58+
59+
function! s:GetRangerCmd(path)
60+
return s:ranger_command . ' --choosefiles=' . s:choice_file_path .
61+
\ (isdirectory(a:path) ? ' "' : ' --selectfile="') . expand(a:path) . '"'
62+
endfunction
63+
4764
if has('nvim')
4865
function! OpenRangerIn(path, edit_cmd)
49-
let currentPath = expand(a:path)
5066
let rangerCallback = { 'name': 'ranger', 'edit_cmd': a:edit_cmd }
5167
function! rangerCallback.on_exit(job_id, code, event)
5268
if a:code == 0
5369
silent! Bclose!
5470
endif
55-
try
56-
if filereadable(s:choice_file_path)
57-
for f in readfile(s:choice_file_path)
58-
exec self.edit_cmd . f
59-
endfor
60-
call delete(s:choice_file_path)
61-
endif
62-
endtry
71+
call s:EditFiles(self.edit_cmd)
6372
endfunction
6473
enew
65-
if isdirectory(currentPath)
66-
call termopen(s:ranger_command . ' --choosefiles=' . s:choice_file_path . ' "' . currentPath . '"', rangerCallback)
67-
else
68-
call termopen(s:ranger_command . ' --choosefiles=' . s:choice_file_path . ' --selectfile="' . currentPath . '"', rangerCallback)
69-
endif
74+
call termopen(s:GetRangerCmd(a:path), rangerCallback)
7075
startinsert
7176
endfunction
7277
else
7378
function! OpenRangerIn(path, edit_cmd)
74-
let currentPath = expand(a:path)
75-
if isdirectory(currentPath)
76-
silent exec '!' . s:ranger_command . ' --choosefiles=' . s:choice_file_path . ' "' . currentPath . '"'
77-
else
78-
silent exec '!' . s:ranger_command . ' --choosefiles=' . s:choice_file_path . ' --selectfile="' . currentPath . '"'
79-
endif
80-
if filereadable(s:choice_file_path)
81-
for f in readfile(s:choice_file_path)
82-
exec a:edit_cmd . f
83-
endfor
84-
call delete(s:choice_file_path)
85-
endif
79+
silent exec '!' . s:GetRangerCmd(a:path)
80+
call s:EditFiles(a:edit_cmd)
8681
redraw!
8782
" reset the filetype to fix the issue that happens
8883
" when opening ranger on VimEnter (with `vim .`)
@@ -121,7 +116,7 @@ function! OpenRangerOnVimLoadDir(argv_path)
121116
Bclose!
122117

123118
" Open Ranger
124-
call OpenRangerIn(path, 'edit')
119+
call OpenRangerIn(path, 'edit ')
125120
endfunction
126121

127122
" To open ranger when vim load a directory

0 commit comments

Comments
 (0)