netrwPlugin.vim 10 KB

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