blob: d534b36966aec44993b241c8f18bbd98bbdab305 [file] [log] [blame]
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001" netrwPlugin.vim: Handles file transfer and remote directory listing across a network
Bram Moolenaardd2a3cd2007-05-05 17:10:09 +00002" PLUGIN SECTION
Luca Saccarolac363ca12024-12-19 20:16:18 +01003" Maintainer: This runtime file is looking for a new maintainer.
4" Date: Sep 09, 2021
Nir Lichtman1e34b952024-05-08 19:19:34 +02005" Last Change:
6" 2024 May 08 by Vim Project: cleanup legacy Win9X checks
Konfekt3d7e5672024-10-27 22:16:49 +01007" 2024 Oct 27 by Vim Project: cleanup gx mapping
Christian Brabandtd69ffbe2024-10-28 21:23:54 +01008" 2024 Oct 28 by Vim Project: further improvements
Christian Brabandt8b0fa7a2024-10-31 09:21:23 +01009" 2024 Oct 31 by Vim Project: use autoloaded functions
Luca Saccarolac363ca12024-12-19 20:16:18 +010010" 2024 Dec 19 by Vim Project: change style (#16248)
Christian Brabandtf9ca1392024-02-19 20:37:11 +010011" Former Maintainer: Charles E Campbell
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012" GetLatestVimScripts: 1075 1 :AutoInstall: netrw.vim
Bram Moolenaar89a9c152021-08-29 21:55:35 +020013" Copyright: Copyright (C) 1999-2021 Charles E. Campbell {{{1
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014" Permission is hereby granted to use and distribute this code,
15" with or without modifications, provided that this copyright
16" notice is copied with it. Like anything else that's free,
17" netrw.vim, netrwPlugin.vim, and netrwSettings.vim are provided
18" *as is* and comes with no warranty of any kind, either
19" expressed or implied. By using this plugin, you agree that
20" in no event will the copyright holder be liable for any damages
21" resulting from the use of this software.
22"
23" But be doers of the Word, and not only hearers, deluding your own selves {{{1
24" (James 1:22 RSV)
25" =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000026" Load Once: {{{1
Bram Moolenaardd2a3cd2007-05-05 17:10:09 +000027if &cp || exists("g:loaded_netrwPlugin")
Luca Saccarolac363ca12024-12-19 20:16:18 +010028 finish
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000029endif
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010030let g:loaded_netrwPlugin = "v173"
Bram Moolenaar5c736222010-01-06 20:54:52 +010031let s:keepcpo = &cpo
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000032set cpo&vim
Bram Moolenaar8d043172014-01-23 14:24:41 +010033"DechoRemOn
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000034
35" ---------------------------------------------------------------------
36" Public Interface: {{{1
37
Konfekt3d7e5672024-10-27 22:16:49 +010038" Commands Launch/URL {{{2
Christian Brabandt8b0fa7a2024-10-31 09:21:23 +010039command -complete=shellcmd -nargs=1 Launch call netrw#Launch(trim(<q-args>))
40command -complete=file -nargs=1 Open call netrw#Open(trim(<q-args>))
Konfekt3d7e5672024-10-27 22:16:49 +010041" " }}}
Bram Moolenaar8d043172014-01-23 14:24:41 +010042" Local Browsing Autocmds: {{{2
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000043augroup FileExplorer
Luca Saccarolac363ca12024-12-19 20:16:18 +010044 au!
45 au BufLeave * if &ft != "netrw"|let w:netrw_prvfile= expand("%:p")|endif
46 au BufEnter * sil call s:LocalBrowse(expand("<amatch>"))
47 au VimEnter * sil call s:VimEnter(expand("<amatch>"))
48 if has("win32")
49 au BufEnter .* sil call s:LocalBrowse(expand("<amatch>"))
50 endif
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000051augroup END
52
53" Network Browsing Reading Writing: {{{2
54augroup Network
Luca Saccarolac363ca12024-12-19 20:16:18 +010055 au!
56 au BufReadCmd file://* call netrw#FileUrlEdit(expand("<amatch>"))
57 au BufReadCmd ftp://*,rcp://*,scp://*,http://*,https://*,dav://*,davs://*,rsync://*,sftp://* exe "sil doau BufReadPre ".fnameescape(expand("<amatch>"))|call netrw#Nread(2,expand("<amatch>"))|exe "sil doau BufReadPost ".fnameescape(expand("<amatch>"))
58 au FileReadCmd ftp://*,rcp://*,scp://*,http://*,file://*,https://*,dav://*,davs://*,rsync://*,sftp://* exe "sil doau FileReadPre ".fnameescape(expand("<amatch>"))|call netrw#Nread(1,expand("<amatch>"))|exe "sil doau FileReadPost ".fnameescape(expand("<amatch>"))
59 au BufWriteCmd ftp://*,rcp://*,scp://*,http://*,file://*,dav://*,davs://*,rsync://*,sftp://* exe "sil doau BufWritePre ".fnameescape(expand("<amatch>"))|exe 'Nwrite '.fnameescape(expand("<amatch>"))|exe "sil doau BufWritePost ".fnameescape(expand("<amatch>"))
60 au FileWriteCmd ftp://*,rcp://*,scp://*,http://*,file://*,dav://*,davs://*,rsync://*,sftp://* exe "sil doau FileWritePre ".fnameescape(expand("<amatch>"))|exe "'[,']".'Nwrite '.fnameescape(expand("<amatch>"))|exe "sil doau FileWritePost ".fnameescape(expand("<amatch>"))
61 try
62 au SourceCmd ftp://*,rcp://*,scp://*,http://*,file://*,https://*,dav://*,davs://*,rsync://*,sftp://* exe 'Nsource '.fnameescape(expand("<amatch>"))
63 catch /^Vim\%((\a\+)\)\=:E216/
64 au SourcePre ftp://*,rcp://*,scp://*,http://*,file://*,https://*,dav://*,davs://*,rsync://*,sftp://* exe 'Nsource '.fnameescape(expand("<amatch>"))
65 endtry
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000066augroup END
67
68" Commands: :Nread, :Nwrite, :NetUserPass {{{2
Luca Saccarolac363ca12024-12-19 20:16:18 +010069com! -count=1 -nargs=* Nread let s:svpos= winsaveview()<bar>call netrw#NetRead(<count>,<f-args>)<bar>call winrestview(s:svpos)
70com! -range=% -nargs=* Nwrite let s:svpos= winsaveview()<bar><line1>,<line2>call netrw#NetWrite(<f-args>)<bar>call winrestview(s:svpos)
71com! -nargs=* NetUserPass call NetUserPass(<f-args>)
72com! -nargs=* Nsource let s:svpos= winsaveview()<bar>call netrw#NetSource(<f-args>)<bar>call winrestview(s:svpos)
73com! -nargs=? Ntree call netrw#SetTreetop(1,<q-args>)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000074
Bram Moolenaar8d043172014-01-23 14:24:41 +010075" Commands: :Explore, :Sexplore, Hexplore, Vexplore, Lexplore {{{2
Luca Saccarolac363ca12024-12-19 20:16:18 +010076com! -nargs=* -bar -bang -count=0 -complete=dir Explore call netrw#Explore(<count>,0,0+<bang>0,<q-args>)
77com! -nargs=* -bar -bang -count=0 -complete=dir Sexplore call netrw#Explore(<count>,1,0+<bang>0,<q-args>)
78com! -nargs=* -bar -bang -count=0 -complete=dir Hexplore call netrw#Explore(<count>,1,2+<bang>0,<q-args>)
79com! -nargs=* -bar -bang -count=0 -complete=dir Vexplore call netrw#Explore(<count>,1,4+<bang>0,<q-args>)
80com! -nargs=* -bar -count=0 -complete=dir Texplore call netrw#Explore(<count>,0,6 ,<q-args>)
81com! -nargs=* -bar -bang Nexplore call netrw#Explore(-1,0,0,<q-args>)
82com! -nargs=* -bar -bang Pexplore call netrw#Explore(-2,0,0,<q-args>)
83com! -nargs=* -bar -bang -count=0 -complete=dir Lexplore call netrw#Lexplore(<count>,<bang>0,<q-args>)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000084
85" Commands: NetrwSettings {{{2
Luca Saccarolac363ca12024-12-19 20:16:18 +010086com! -nargs=0 NetrwSettings call netrwSettings#NetrwSettings()
87com! -bang NetrwClean call netrw#Clean(<bang>0)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000088
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000089" Maps:
Bram Moolenaar8feef4f2015-01-07 16:57:10 +010090if !exists("g:netrw_nogx")
Luca Saccarolac363ca12024-12-19 20:16:18 +010091 if maparg('gx','n') == ""
92 if !hasmapto('<Plug>NetrwBrowseX')
93 nmap <unique> gx <Plug>NetrwBrowseX
94 endif
95 nno <silent> <Plug>NetrwBrowseX :call netrw#BrowseX(netrw#GX(),netrw#CheckIfRemote(netrw#GX()))<cr>
Bram Moolenaar8feef4f2015-01-07 16:57:10 +010096 endif
Luca Saccarolac363ca12024-12-19 20:16:18 +010097 if maparg('gx','x') == ""
98 if !hasmapto('<Plug>NetrwBrowseXVis')
99 xmap <unique> gx <Plug>NetrwBrowseXVis
100 endif
101 xno <silent> <Plug>NetrwBrowseXVis :<c-u>call netrw#BrowseXVis()<cr>
Bram Moolenaar8feef4f2015-01-07 16:57:10 +0100102 endif
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000103endif
Bram Moolenaara0f849e2015-10-30 14:37:44 +0100104if exists("g:netrw_usetab") && g:netrw_usetab
Luca Saccarolac363ca12024-12-19 20:16:18 +0100105 if maparg('<c-tab>','n') == ""
106 nmap <unique> <c-tab> <Plug>NetrwShrink
107 endif
108 nno <silent> <Plug>NetrwShrink :call netrw#Shrink()<cr>
Bram Moolenaara0f849e2015-10-30 14:37:44 +0100109endif
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000110
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000111" ---------------------------------------------------------------------
Bram Moolenaar8d043172014-01-23 14:24:41 +0100112" LocalBrowse: invokes netrw#LocalBrowseCheck() on directory buffers {{{2
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000113fun! s:LocalBrowse(dirname)
Bram Moolenaar8d043172014-01-23 14:24:41 +0100114 " Unfortunate interaction -- only DechoMsg debugging calls can be safely used here.
115 " Otherwise, the BufEnter event gets triggered when attempts to write to
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000116 " the DBG buffer are made.
Bram Moolenaar589edb32019-09-20 14:38:13 +0200117
Bram Moolenaare6ae6222013-05-21 21:01:10 +0200118 if !exists("s:vimentered")
Luca Saccarolac363ca12024-12-19 20:16:18 +0100119 " If s:vimentered doesn't exist, then the VimEnter event hasn't fired. It will,
120 " and so s:VimEnter() will then be calling this routine, but this time with s:vimentered defined.
121 " call Dfunc("s:LocalBrowse(dirname<".a:dirname.">) (s:vimentered doesn't exist)")
122 " call Dret("s:LocalBrowse")
123 return
Bram Moolenaare6ae6222013-05-21 21:01:10 +0200124 endif
Bram Moolenaar8d043172014-01-23 14:24:41 +0100125
Luca Saccarolac363ca12024-12-19 20:16:18 +0100126 " call Dfunc("s:LocalBrowse(dirname<".a:dirname.">) (s:vimentered=".s:vimentered.")")
Bram Moolenaar8d043172014-01-23 14:24:41 +0100127
Bram Moolenaardd2a3cd2007-05-05 17:10:09 +0000128 if has("amiga")
Luca Saccarolac363ca12024-12-19 20:16:18 +0100129 " The check against '' is made for the Amiga, where the empty
130 " string is the current directory and not checking would break
131 " things such as the help command.
132 " call Decho("(LocalBrowse) dirname<".a:dirname."> (isdirectory, amiga)")
133 if a:dirname != '' && isdirectory(a:dirname)
134 sil! call netrw#LocalBrowseCheck(a:dirname)
135 if exists("w:netrw_bannercnt") && line('.') < w:netrw_bannercnt
136 exe w:netrw_bannercnt
137 endif
Bram Moolenaara6878372014-03-22 21:02:50 +0100138 endif
Bram Moolenaar8d043172014-01-23 14:24:41 +0100139
Bram Moolenaardd2a3cd2007-05-05 17:10:09 +0000140 elseif isdirectory(a:dirname)
Luca Saccarolac363ca12024-12-19 20:16:18 +0100141 " call Decho("(LocalBrowse) dirname<".a:dirname."> ft=".&ft." (isdirectory, not amiga)")
142 " call Dredir("LocalBrowse ft last set: ","verbose set ft")
143 " Jul 13, 2021: for whatever reason, preceding the following call with
144 " a sil! causes an unbalanced if-endif vim error
145 call netrw#LocalBrowseCheck(a:dirname)
146 if exists("w:netrw_bannercnt") && line('.') < w:netrw_bannercnt
147 exe w:netrw_bannercnt
148 endif
Bram Moolenaar8d043172014-01-23 14:24:41 +0100149
150 else
Luca Saccarolac363ca12024-12-19 20:16:18 +0100151 " not a directory, ignore it
152 " call Decho("(LocalBrowse) dirname<".a:dirname."> not a directory, ignoring...")
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000153 endif
Bram Moolenaar8d043172014-01-23 14:24:41 +0100154
Luca Saccarolac363ca12024-12-19 20:16:18 +0100155 " call Dret("s:LocalBrowse")
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000156endfun
157
158" ---------------------------------------------------------------------
Bram Moolenaar8d043172014-01-23 14:24:41 +0100159" s:VimEnter: after all vim startup stuff is done, this function is called. {{{2
160" Its purpose: to look over all windows and run s:LocalBrowse() on
161" them, which checks if they're directories and will create a directory
162" listing when appropriate.
163" It also sets s:vimentered, letting s:LocalBrowse() know that s:VimEnter()
164" has already been called.
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200165fun! s:VimEnter(dirname)
Luca Saccarolac363ca12024-12-19 20:16:18 +0100166 " call Dfunc("s:VimEnter(dirname<".a:dirname.">) expand(%)<".expand("%").">")
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200167 if has('nvim') || v:version < 802
Luca Saccarolac363ca12024-12-19 20:16:18 +0100168 " Johann Höchtl: reported that the call range... line causes an E488: Trailing characters
169 " error with neovim. I suspect its because neovim hasn't updated with recent
170 " vim patches. As is, this code will have problems with popup terminals
171 " instantiated before the VimEnter event runs.
172 " Ingo Karkat : E488 also in Vim 8.1.1602
173 let curwin = winnr()
174 let s:vimentered = 1
175 windo call s:LocalBrowse(expand("%:p"))
176 exe curwin."wincmd w"
177 else
178 " the following complicated expression comes courtesy of lacygoill; largely does the same thing as the windo and
179 " wincmd which are commented out, but avoids some side effects. Allows popup terminal before VimEnter.
180 let s:vimentered = 1
181 call range(1, winnr('$'))->map({_, v -> win_execute(win_getid(v), 'call expand("%:p")->s:LocalBrowse()')})
182 endif
183 " call Dret("s:VimEnter")
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200184endfun
185
186" ---------------------------------------------------------------------
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000187" NetrwStatusLine: {{{1
188fun! NetrwStatusLine()
Luca Saccarolac363ca12024-12-19 20:16:18 +0100189 " let g:stlmsg= "Xbufnr=".w:netrw_explore_bufnr." bufnr=".bufnr("%")." Xline#".w:netrw_explore_line." line#".line(".")
Bram Moolenaaraf48b092006-03-14 22:51:38 +0000190 if !exists("w:netrw_explore_bufnr") || w:netrw_explore_bufnr != bufnr("%") || !exists("w:netrw_explore_line") || w:netrw_explore_line != line(".") || !exists("w:netrw_explore_list")
Luca Saccarolac363ca12024-12-19 20:16:18 +0100191 let &stl= s:netrw_explore_stl
192 if exists("w:netrw_explore_bufnr")|unlet w:netrw_explore_bufnr|endif
193 if exists("w:netrw_explore_line")|unlet w:netrw_explore_line|endif
194 return ""
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000195 else
Luca Saccarolac363ca12024-12-19 20:16:18 +0100196 return "Match ".w:netrw_explore_mtchcnt." of ".w:netrw_explore_listlen
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000197 endif
198endfun
199
200" ------------------------------------------------------------------------
201" NetUserPass: set username and password for subsequent ftp transfer {{{1
Luca Saccarolac363ca12024-12-19 20:16:18 +0100202" Usage: :call NetUserPass() -- will prompt for userid and password
203" :call NetUserPass("uid") -- will prompt for password
204" :call NetUserPass("uid","password") -- sets global userid and password
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000205fun! NetUserPass(...)
206
Luca Saccarolac363ca12024-12-19 20:16:18 +0100207 " get/set userid
208 if a:0 == 0
209 " call Dfunc("NetUserPass(a:0<".a:0.">)")
210 if !exists("g:netrw_uid") || g:netrw_uid == ""
211 " via prompt
212 let g:netrw_uid= input('Enter username: ')
213 endif
214 else " from command line
215 " call Dfunc("NetUserPass(a:1<".a:1.">) {")
216 let g:netrw_uid= a:1
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000217 endif
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000218
Luca Saccarolac363ca12024-12-19 20:16:18 +0100219 " get password
220 if a:0 <= 1 " via prompt
221 " call Decho("a:0=".a:0." case <=1:")
222 let g:netrw_passwd= inputsecret("Enter Password: ")
223 else " from command line
224 " call Decho("a:0=".a:0." case >1: a:2<".a:2.">")
225 let g:netrw_passwd=a:2
226 endif
227 " call Dret("NetUserPass")
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000228endfun
229
230" ------------------------------------------------------------------------
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000231" Modelines And Restoration: {{{1
232let &cpo= s:keepcpo
233unlet s:keepcpo
Luca Saccarolac363ca12024-12-19 20:16:18 +0100234" vim:ts=8 sts=2 sw=2 et fdm=marker