netrwPlugin.vim 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. " netrwPlugin.vim: Handles file transfer and remote directory listing across a network
  2. " PLUGIN SECTION
  3. " Maintainer: This runtime file is looking for a new maintainer.
  4. " Date: Sep 09, 2021
  5. " Last Change:
  6. " 2024 May 08 by Vim Project: cleanup legacy Win9X checks
  7. " 2024 Oct 27 by Vim Project: cleanup gx mapping
  8. " 2024 Oct 28 by Vim Project: further improvements
  9. " 2024 Oct 31 by Vim Project: use autoloaded functions
  10. " 2024 Dec 19 by Vim Project: change style (#16248)
  11. " Former Maintainer: Charles E Campbell
  12. " GetLatestVimScripts: 1075 1 :AutoInstall: netrw.vim
  13. " Copyright: Copyright (C) 1999-2021 Charles E. Campbell {{{1
  14. " 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. " =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  26. " Load Once: {{{1
  27. if &cp || exists("g:loaded_netrwPlugin")
  28. finish
  29. endif
  30. let g:loaded_netrwPlugin = "v173"
  31. let s:keepcpo = &cpo
  32. set cpo&vim
  33. "DechoRemOn
  34. " ---------------------------------------------------------------------
  35. " Public Interface: {{{1
  36. " Commands Launch/URL {{{2
  37. command -complete=shellcmd -nargs=1 Launch call netrw#Launch(trim(<q-args>))
  38. command -complete=file -nargs=1 Open call netrw#Open(trim(<q-args>))
  39. " " }}}
  40. " Local Browsing Autocmds: {{{2
  41. augroup FileExplorer
  42. au!
  43. au BufLeave * if &ft != "netrw"|let w:netrw_prvfile= expand("%:p")|endif
  44. au BufEnter * sil call s:LocalBrowse(expand("<amatch>"))
  45. au VimEnter * sil call s:VimEnter(expand("<amatch>"))
  46. if has("win32")
  47. au BufEnter .* sil call s:LocalBrowse(expand("<amatch>"))
  48. endif
  49. augroup END
  50. " Network Browsing Reading Writing: {{{2
  51. augroup Network
  52. au!
  53. au BufReadCmd file://* call netrw#FileUrlEdit(expand("<amatch>"))
  54. 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>"))
  55. 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>"))
  56. 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>"))
  57. 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>"))
  58. try
  59. au SourceCmd ftp://*,rcp://*,scp://*,http://*,file://*,https://*,dav://*,davs://*,rsync://*,sftp://* exe 'Nsource '.fnameescape(expand("<amatch>"))
  60. catch /^Vim\%((\a\+)\)\=:E216/
  61. au SourcePre ftp://*,rcp://*,scp://*,http://*,file://*,https://*,dav://*,davs://*,rsync://*,sftp://* exe 'Nsource '.fnameescape(expand("<amatch>"))
  62. endtry
  63. augroup END
  64. " Commands: :Nread, :Nwrite, :NetUserPass {{{2
  65. com! -count=1 -nargs=* Nread let s:svpos= winsaveview()<bar>call netrw#NetRead(<count>,<f-args>)<bar>call winrestview(s:svpos)
  66. com! -range=% -nargs=* Nwrite let s:svpos= winsaveview()<bar><line1>,<line2>call netrw#NetWrite(<f-args>)<bar>call winrestview(s:svpos)
  67. com! -nargs=* NetUserPass call NetUserPass(<f-args>)
  68. com! -nargs=* Nsource let s:svpos= winsaveview()<bar>call netrw#NetSource(<f-args>)<bar>call winrestview(s:svpos)
  69. com! -nargs=? Ntree call netrw#SetTreetop(1,<q-args>)
  70. " Commands: :Explore, :Sexplore, Hexplore, Vexplore, Lexplore {{{2
  71. com! -nargs=* -bar -bang -count=0 -complete=dir Explore call netrw#Explore(<count>,0,0+<bang>0,<q-args>)
  72. com! -nargs=* -bar -bang -count=0 -complete=dir Sexplore call netrw#Explore(<count>,1,0+<bang>0,<q-args>)
  73. com! -nargs=* -bar -bang -count=0 -complete=dir Hexplore call netrw#Explore(<count>,1,2+<bang>0,<q-args>)
  74. com! -nargs=* -bar -bang -count=0 -complete=dir Vexplore call netrw#Explore(<count>,1,4+<bang>0,<q-args>)
  75. com! -nargs=* -bar -count=0 -complete=dir Texplore call netrw#Explore(<count>,0,6 ,<q-args>)
  76. com! -nargs=* -bar -bang Nexplore call netrw#Explore(-1,0,0,<q-args>)
  77. com! -nargs=* -bar -bang Pexplore call netrw#Explore(-2,0,0,<q-args>)
  78. com! -nargs=* -bar -bang -count=0 -complete=dir Lexplore call netrw#Lexplore(<count>,<bang>0,<q-args>)
  79. " Commands: NetrwSettings {{{2
  80. com! -nargs=0 NetrwSettings call netrwSettings#NetrwSettings()
  81. com! -bang NetrwClean call netrw#Clean(<bang>0)
  82. " Maps:
  83. if !exists("g:netrw_nogx")
  84. if maparg('gx','n') == ""
  85. if !hasmapto('<Plug>NetrwBrowseX')
  86. nmap <unique> gx <Plug>NetrwBrowseX
  87. endif
  88. nno <silent> <Plug>NetrwBrowseX :call netrw#BrowseX(netrw#GX(),netrw#CheckIfRemote(netrw#GX()))<cr>
  89. endif
  90. if maparg('gx','x') == ""
  91. if !hasmapto('<Plug>NetrwBrowseXVis')
  92. xmap <unique> gx <Plug>NetrwBrowseXVis
  93. endif
  94. xno <silent> <Plug>NetrwBrowseXVis :<c-u>call netrw#BrowseXVis()<cr>
  95. endif
  96. endif
  97. if exists("g:netrw_usetab") && g:netrw_usetab
  98. if maparg('<c-tab>','n') == ""
  99. nmap <unique> <c-tab> <Plug>NetrwShrink
  100. endif
  101. nno <silent> <Plug>NetrwShrink :call netrw#Shrink()<cr>
  102. endif
  103. " ---------------------------------------------------------------------
  104. " LocalBrowse: invokes netrw#LocalBrowseCheck() on directory buffers {{{2
  105. fun! s:LocalBrowse(dirname)
  106. " Unfortunate interaction -- only DechoMsg debugging calls can be safely used here.
  107. " Otherwise, the BufEnter event gets triggered when attempts to write to
  108. " the DBG buffer are made.
  109. if !exists("s:vimentered")
  110. " If s:vimentered doesn't exist, then the VimEnter event hasn't fired. It will,
  111. " and so s:VimEnter() will then be calling this routine, but this time with s:vimentered defined.
  112. " call Dfunc("s:LocalBrowse(dirname<".a:dirname.">) (s:vimentered doesn't exist)")
  113. " call Dret("s:LocalBrowse")
  114. return
  115. endif
  116. " call Dfunc("s:LocalBrowse(dirname<".a:dirname.">) (s:vimentered=".s:vimentered.")")
  117. if has("amiga")
  118. " The check against '' is made for the Amiga, where the empty
  119. " string is the current directory and not checking would break
  120. " things such as the help command.
  121. " call Decho("(LocalBrowse) dirname<".a:dirname."> (isdirectory, amiga)")
  122. if a:dirname != '' && isdirectory(a:dirname)
  123. sil! call netrw#LocalBrowseCheck(a:dirname)
  124. if exists("w:netrw_bannercnt") && line('.') < w:netrw_bannercnt
  125. exe w:netrw_bannercnt
  126. endif
  127. endif
  128. elseif isdirectory(a:dirname)
  129. " call Decho("(LocalBrowse) dirname<".a:dirname."> ft=".&ft." (isdirectory, not amiga)")
  130. " call Dredir("LocalBrowse ft last set: ","verbose set ft")
  131. " Jul 13, 2021: for whatever reason, preceding the following call with
  132. " a sil! causes an unbalanced if-endif vim error
  133. call netrw#LocalBrowseCheck(a:dirname)
  134. if exists("w:netrw_bannercnt") && line('.') < w:netrw_bannercnt
  135. exe w:netrw_bannercnt
  136. endif
  137. else
  138. " not a directory, ignore it
  139. " call Decho("(LocalBrowse) dirname<".a:dirname."> not a directory, ignoring...")
  140. endif
  141. " call Dret("s:LocalBrowse")
  142. endfun
  143. " ---------------------------------------------------------------------
  144. " s:VimEnter: after all vim startup stuff is done, this function is called. {{{2
  145. " Its purpose: to look over all windows and run s:LocalBrowse() on
  146. " them, which checks if they're directories and will create a directory
  147. " listing when appropriate.
  148. " It also sets s:vimentered, letting s:LocalBrowse() know that s:VimEnter()
  149. " has already been called.
  150. fun! s:VimEnter(dirname)
  151. " call Dfunc("s:VimEnter(dirname<".a:dirname.">) expand(%)<".expand("%").">")
  152. if has('nvim') || v:version < 802
  153. " Johann Höchtl: reported that the call range... line causes an E488: Trailing characters
  154. " error with neovim. I suspect its because neovim hasn't updated with recent
  155. " vim patches. As is, this code will have problems with popup terminals
  156. " instantiated before the VimEnter event runs.
  157. " Ingo Karkat : E488 also in Vim 8.1.1602
  158. let curwin = winnr()
  159. let s:vimentered = 1
  160. windo call s:LocalBrowse(expand("%:p"))
  161. exe curwin."wincmd w"
  162. else
  163. " the following complicated expression comes courtesy of lacygoill; largely does the same thing as the windo and
  164. " wincmd which are commented out, but avoids some side effects. Allows popup terminal before VimEnter.
  165. let s:vimentered = 1
  166. call range(1, winnr('$'))->map({_, v -> win_execute(win_getid(v), 'call expand("%:p")->s:LocalBrowse()')})
  167. endif
  168. " call Dret("s:VimEnter")
  169. endfun
  170. " ---------------------------------------------------------------------
  171. " NetrwStatusLine: {{{1
  172. fun! NetrwStatusLine()
  173. " let g:stlmsg= "Xbufnr=".w:netrw_explore_bufnr." bufnr=".bufnr("%")." Xline#".w:netrw_explore_line." line#".line(".")
  174. 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")
  175. let &stl= s:netrw_explore_stl
  176. if exists("w:netrw_explore_bufnr")|unlet w:netrw_explore_bufnr|endif
  177. if exists("w:netrw_explore_line")|unlet w:netrw_explore_line|endif
  178. return ""
  179. else
  180. return "Match ".w:netrw_explore_mtchcnt." of ".w:netrw_explore_listlen
  181. endif
  182. endfun
  183. " ------------------------------------------------------------------------
  184. " NetUserPass: set username and password for subsequent ftp transfer {{{1
  185. " Usage: :call NetUserPass() -- will prompt for userid and password
  186. " :call NetUserPass("uid") -- will prompt for password
  187. " :call NetUserPass("uid","password") -- sets global userid and password
  188. fun! NetUserPass(...)
  189. " get/set userid
  190. if a:0 == 0
  191. " call Dfunc("NetUserPass(a:0<".a:0.">)")
  192. if !exists("g:netrw_uid") || g:netrw_uid == ""
  193. " via prompt
  194. let g:netrw_uid= input('Enter username: ')
  195. endif
  196. else " from command line
  197. " call Dfunc("NetUserPass(a:1<".a:1.">) {")
  198. let g:netrw_uid= a:1
  199. endif
  200. " get password
  201. if a:0 <= 1 " via prompt
  202. " call Decho("a:0=".a:0." case <=1:")
  203. let g:netrw_passwd= inputsecret("Enter Password: ")
  204. else " from command line
  205. " call Decho("a:0=".a:0." case >1: a:2<".a:2.">")
  206. let g:netrw_passwd=a:2
  207. endif
  208. " call Dret("NetUserPass")
  209. endfun
  210. " ------------------------------------------------------------------------
  211. " Modelines And Restoration: {{{1
  212. let &cpo= s:keepcpo
  213. unlet s:keepcpo
  214. " vim:ts=8 sts=2 sw=2 et fdm=marker