netrwPlugin.vim 11 KB

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