netrwPlugin.vim 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. " netrwPlugin.vim: Handles file transfer and remote directory listing across a network
  2. " PLUGIN SECTION
  3. " Date: Feb 08, 2016
  4. " Maintainer: Charles E Campbell <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
  5. " GetLatestVimScripts: 1075 1 :AutoInstall: netrw.vim
  6. " Copyright: Copyright (C) 1999-2013 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 = "v156"
  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#FileUrlRead(expand("<amatch>"))
  43. au BufReadCmd ftp://*,rcp://*,scp://*,http://*,file://*,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(<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(expand((exists("g:netrw_gx")? g:netrw_gx : '<cfile>')),netrw#CheckIfRemote())<cr>
  78. endif
  79. if maparg('gx','v') == ""
  80. if !hasmapto('<Plug>NetrwBrowseXVis')
  81. vmap <unique> gx <Plug>NetrwBrowseXVis
  82. endif
  83. vno <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. " call Decho("(s:LocalBrowse) COMBAK#23: buf#".bufnr("%")." file<".expand("%")."> line#".line(".")." col#".col("."))
  121. sil! call netrw#LocalBrowseCheck(a:dirname)
  122. " call Decho("(s:LocalBrowse) COMBAK#24: buf#".bufnr("%")." file<".expand("%")."> line#".line(".")." col#".col("."))
  123. if exists("w:netrw_bannercnt") && line('.') < w:netrw_bannercnt
  124. exe w:netrw_bannercnt
  125. " call Decho("(s:LocalBrowse) COMBAK#25: buf#".bufnr("%")." file<".expand("%")."> line#".line(".")." col#".col("."))
  126. endif
  127. else
  128. " not a directory, ignore it
  129. " call Decho("(LocalBrowse) dirname<".a:dirname."> not a directory, ignoring...")
  130. endif
  131. " call Decho("(s:LocalBrowse) COMBAK#26: buf#".bufnr("%")." file<".expand("%")."> line#".line(".")." col#".col("."))
  132. " call Dret("s:LocalBrowse")
  133. endfun
  134. " ---------------------------------------------------------------------
  135. " s:VimEnter: after all vim startup stuff is done, this function is called. {{{2
  136. " Its purpose: to look over all windows and run s:LocalBrowse() on
  137. " them, which checks if they're directories and will create a directory
  138. " listing when appropriate.
  139. " It also sets s:vimentered, letting s:LocalBrowse() know that s:VimEnter()
  140. " has already been called.
  141. fun! s:VimEnter(dirname)
  142. " call Dfunc("s:VimEnter(dirname<".a:dirname.">) expand(%)<".expand("%").">")
  143. let curwin = winnr()
  144. let s:vimentered = 1
  145. windo call s:LocalBrowse(expand("%:p"))
  146. exe curwin."wincmd w"
  147. " call Dret("s:VimEnter")
  148. endfun
  149. " ---------------------------------------------------------------------
  150. " NetrwStatusLine: {{{1
  151. fun! NetrwStatusLine()
  152. " let g:stlmsg= "Xbufnr=".w:netrw_explore_bufnr." bufnr=".bufnr("%")." Xline#".w:netrw_explore_line." line#".line(".")
  153. 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")
  154. let &stl= s:netrw_explore_stl
  155. if exists("w:netrw_explore_bufnr")|unlet w:netrw_explore_bufnr|endif
  156. if exists("w:netrw_explore_line")|unlet w:netrw_explore_line|endif
  157. return ""
  158. else
  159. return "Match ".w:netrw_explore_mtchcnt." of ".w:netrw_explore_listlen
  160. endif
  161. endfun
  162. " ------------------------------------------------------------------------
  163. " NetUserPass: set username and password for subsequent ftp transfer {{{1
  164. " Usage: :call NetUserPass() -- will prompt for userid and password
  165. " :call NetUserPass("uid") -- will prompt for password
  166. " :call NetUserPass("uid","password") -- sets global userid and password
  167. fun! NetUserPass(...)
  168. " get/set userid
  169. if a:0 == 0
  170. " call Dfunc("NetUserPass(a:0<".a:0.">)")
  171. if !exists("g:netrw_uid") || g:netrw_uid == ""
  172. " via prompt
  173. let g:netrw_uid= input('Enter username: ')
  174. endif
  175. else " from command line
  176. " call Dfunc("NetUserPass(a:1<".a:1.">) {")
  177. let g:netrw_uid= a:1
  178. endif
  179. " get password
  180. if a:0 <= 1 " via prompt
  181. " call Decho("a:0=".a:0." case <=1:")
  182. let g:netrw_passwd= inputsecret("Enter Password: ")
  183. else " from command line
  184. " call Decho("a:0=".a:0." case >1: a:2<".a:2.">")
  185. let g:netrw_passwd=a:2
  186. endif
  187. " call Dret("NetUserPass")
  188. endfun
  189. " ------------------------------------------------------------------------
  190. " Modelines And Restoration: {{{1
  191. let &cpo= s:keepcpo
  192. unlet s:keepcpo
  193. " vim:ts=8 fdm=marker