Skip to content

Commit d3c1a73

Browse files
committed
Add RangerCD command
Run RangerCD to spawn ranger and change to the last directory encountered.
1 parent e8963d5 commit d3c1a73

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

plugin/ranger.vim

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,42 +45,54 @@ if !exists('s:choice_file_path')
4545
endif
4646

4747
if has('nvim')
48-
function! OpenRangerIn(path, edit_cmd)
48+
function! OpenRangerIn(path, mode, edit_cmd)
4949
let currentPath = expand(a:path)
50-
let rangerCallback = { 'name': 'ranger', 'edit_cmd': a:edit_cmd }
50+
let rangerCallback = { 'name': 'ranger', 'mode': a:mode, 'edit_cmd': a:edit_cmd }
5151
function! rangerCallback.on_exit(job_id, code, event)
5252
if a:code == 0
5353
silent! Bclose!
5454
endif
5555
try
5656
if filereadable(s:choice_file_path)
57-
for f in readfile(s:choice_file_path)
58-
exec self.edit_cmd . f
59-
endfor
57+
if self.mode == "choosefiles"
58+
for f in readfile(s:choice_file_path)
59+
exec self.edit_cmd . f
60+
endfor
61+
elseif self.mode == "choosedir"
62+
for dir in readfile(s:choice_file_path, 1)
63+
exec self.edit_cmd . dir
64+
endfor
65+
end
6066
call delete(s:choice_file_path)
6167
endif
6268
endtry
6369
endfunction
6470
enew
6571
if isdirectory(currentPath)
66-
call termopen(s:ranger_command . ' --choosefiles=' . s:choice_file_path . ' "' . currentPath . '"', rangerCallback)
72+
call termopen(s:ranger_command . ' --' . a:mode . '=' . s:choice_file_path . ' "' . currentPath . '"', rangerCallback)
6773
else
68-
call termopen(s:ranger_command . ' --choosefiles=' . s:choice_file_path . ' --selectfile="' . currentPath . '"', rangerCallback)
74+
call termopen(s:ranger_command . ' --' . a:mode . '=' . s:choice_file_path . ' --selectfile="' . currentPath . '"', rangerCallback)
6975
endif
7076
startinsert
7177
endfunction
7278
else
73-
function! OpenRangerIn(path, edit_cmd)
79+
function! OpenRangerIn(path, mode, edit_cmd)
7480
let currentPath = expand(a:path)
7581
if isdirectory(currentPath)
76-
silent exec '!' . s:ranger_command . ' --choosefiles=' . s:choice_file_path . ' "' . currentPath . '"'
82+
silent exec '!' . s:ranger_command . ' --' . a:mode . '=' . s:choice_file_path . ' "' . currentPath . '"'
7783
else
78-
silent exec '!' . s:ranger_command . ' --choosefiles=' . s:choice_file_path . ' --selectfile="' . currentPath . '"'
84+
silent exec '!' . s:ranger_command . ' --' . a:mode . '=' . s:choice_file_path . ' --selectfile="' . currentPath . '"'
7985
endif
8086
if filereadable(s:choice_file_path)
81-
for f in readfile(s:choice_file_path)
82-
exec a:edit_cmd . f
83-
endfor
87+
if a:mode == "choosefiles"
88+
for f in readfile(s:choice_file_path)
89+
exec a:edit_cmd . f
90+
endfor
91+
elseif a:mode == "choosedir"
92+
for dir in readfile(s:choice_file_path, 1)
93+
exec a:edit_cmd . dir
94+
endfor
95+
end
8496
call delete(s:choice_file_path)
8597
endif
8698
redraw!
@@ -97,15 +109,16 @@ else
97109
let s:default_edit_cmd='edit '
98110
endif
99111

100-
command! RangerCurrentFile call OpenRangerIn("%", s:default_edit_cmd)
101-
command! RangerCurrentDirectory call OpenRangerIn("%:p:h", s:default_edit_cmd)
102-
command! RangerWorkingDirectory call OpenRangerIn(".", s:default_edit_cmd)
112+
command! RangerCD call OpenRangerIn("%", 'choosedir', "cd ")
113+
command! RangerCurrentFile call OpenRangerIn("%", 'choosefiles', s:default_edit_cmd)
114+
command! RangerCurrentDirectory call OpenRangerIn("%:p:h", 'choosefiles', s:default_edit_cmd)
115+
command! RangerWorkingDirectory call OpenRangerIn(".", 'choosefiles', s:default_edit_cmd)
103116
command! Ranger RangerCurrentFile
104117

105118
" To open the selected file in a new tab
106-
command! RangerCurrentFileNewTab call OpenRangerIn("%", 'tabedit ')
107-
command! RangerCurrentDirectoryNewTab call OpenRangerIn("%:p:h", 'tabedit ')
108-
command! RangerWorkingDirectoryNewTab call OpenRangerIn(".", 'tabedit ')
119+
command! RangerCurrentFileNewTab call OpenRangerIn("%", 'choosefiles', tabedit ')
120+
command! RangerCurrentDirectoryNewTab call OpenRangerIn("%:p:h", 'choosefiles', tabedit ')
121+
command! RangerWorkingDirectoryNewTab call OpenRangerIn(".", 'choosefiles', 'tabedit ')
109122
command! RangerNewTab RangerCurrentDirectoryNewTab
110123

111124
" For retro-compatibility
@@ -121,7 +134,7 @@ function! OpenRangerOnVimLoadDir(argv_path)
121134
Bclose!
122135

123136
" Open Ranger
124-
call OpenRangerIn(path, 'edit')
137+
call OpenRangerIn(path, 'choosefiles', 'edit')
125138
endfunction
126139

127140
" To open ranger when vim load a directory

0 commit comments

Comments
 (0)