blob: fdb6d2793995818c5ba97f072ca2eae02e7a21a8 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" netrw.vim: (global plugin) Handles file transfer across a network
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002" Last Change: Jun 18, 2004
Bram Moolenaar071d4272004-06-13 20:20:40 +00003" Maintainer: Charles E. Campbell, Jr. PhD <drchipNOSPAM at campbellfamily.biz>
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004" Version: 46
Bram Moolenaar071d4272004-06-13 20:20:40 +00005" License: Vim License (see vim's :help license)
6"
7" But be doers of the word, and not only hearers, deluding your own selves
8" (James 1:22 RSV)
9" =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
10
11" Exit quickly when already loaded or when 'compatible' is set.
12if exists("loaded_netrw") || &cp
13 finish
14endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000015let loaded_netrw = "v46"
Bram Moolenaar071d4272004-06-13 20:20:40 +000016let s:save_cpo = &cpo
17set cpo&vim
18
19" ---------------------------------------------------------------------
20" Default values for global netrw variables {{{1
21if !exists("g:netrw_ftpmode")
22 let g:netrw_ftpmode= "binary"
23endif
24if !exists("g:netrw_win95ftp")
25 let g:netrw_win95ftp= 1
26endif
27if !exists("g:netrw_cygwin")
28 if has("win32")
29 let g:netrw_cygwin= 1
30 else
31 let g:netrw_cygwin= 0
32 endif
33endif
34
35" ---------------------------------------------------------------------
36" Default values for global protocol variables {{{1
37if !exists("g:netrw_rcp_cmd")
38 let g:netrw_rcp_cmd = "rcp"
39endif
40if !exists("g:netrw_ftp_cmd")
41 let g:netrw_ftp_cmd = "ftp"
42endif
43if !exists("g:netrw_scp_cmd")
44 let g:netrw_scp_cmd = "scp -q"
45endif
46if !exists("g:netrw_sftp_cmd")
47 let g:netrw_sftp_cmd = "sftp"
48endif
49if !exists("g:netrw_http_cmd")
50 if executable("wget")
51 let g:netrw_http_cmd = "wget -q -O"
52 elseif executable("fetch")
53 let g:netrw_http_cmd = "fetch -o"
54 else
55 let g:netrw_http_cmd = ""
56 endif
57endif
58if !exists("g:netrw_dav_cmd")
59 let g:netrw_dav_cmd = "cadaver"
60endif
61if !exists("g:netrw_rsync_cmd")
62 let g:netrw_rsync_cmd = "rsync"
63endif
64if !exists("g:netrw_fetch_cmd")
65 if executable("fetch")
66 let g:netrw_fetch_cmd = "fetch -o"
67 else
68 let g:netrw_fetch_cmd = ""
69 endif
70endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000071if !exists("g:netrw_list_cmd")
72 if executable("ssh")
73 let g:netrw_list_cmd= "ssh HOSTNAME ls -Fa"
74 else
75" call Decho("ssh is not executable, can't do netlist")
76 let g:netrw_list_cmd= ""
77 endif
78endif
79if exists("g:netrw_silent") && g:netrw_silent != 0
80 let g:netrw_silentxfer= "silent "
81else
82 let g:netrw_silentxfer= ""
83endif
84
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
86if has("win32")
87 \ && exists("g:netrw_use_nt_rcp")
88 \ && g:netrw_use_nt_rcp
89 \ && executable( $SystemRoot .'/system32/rcp.exe')
90 let s:netrw_has_nt_rcp = 1
91 let s:netrw_rcpmode = '-b'
92 else
93 let s:netrw_has_nt_rcp = 0
94 let s:netrw_rcpmode = ''
95endif
96
97" ---------------------------------------------------------------------
98" Transparency Support: {{{1
99" Auto-detection for ftp://*, rcp://*, scp://*, sftp://*, http://*, dav://*,
100" and rsync://*
101" Should make file transfers across networks transparent. Currently I haven't
102" supported appends. Hey, gotta leave something for a future <netrw.vim>!
103if version >= 600
104 augroup Network
105 au!
106 if has("win32")
107 au BufReadCmd file://* exe "doau BufReadPre ".expand("<afile>")|exe 'e '.substitute(expand("<afile>"),"file:/*","","")|exe "doau BufReadPost ".expand("<afile>")
108 else
109 au BufReadCmd file:///* exe "doau BufReadPre ".expand("<afile>")|exe 'e /'.substitute(expand("<afile>"),"file:/*","","")|exe "doau BufReadPost ".expand("<afile>")
110 au BufReadCmd file://localhost/* exe "doau BufReadPre ".expand("<afile>")|exe 'e /'.substitute(expand("<afile>"),"file:/*","","")|exe "doau BufReadPost ".expand("<afile>")
111 endif
112 au BufReadCmd ftp://*,rcp://*,scp://*,http://*,dav://*,rsync://*,sftp://* exe "doau BufReadPre ".expand("<afile>")|exe "Nread 0r ".expand("<afile>")|exe "doau BufReadPost ".expand("<afile>")
113 au FileReadCmd ftp://*,rcp://*,scp://*,http://*,dav://*,rsync://*,sftp://* exe "doau BufReadPre ".expand("<afile>")|exe "Nread " .expand("<afile>")|exe "doau BufReadPost ".expand("<afile>")
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000114 au BufWriteCmd ftp://*,rcp://*,scp://*,dav://*,rsync://*,sftp://* exe "Nwrite " .expand("<afile>")|call <SID>NetRestorePosn()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 augroup END
116endif
117
118" ------------------------------------------------------------------------
119" Commands: :Nread, :Nwrite, and :NetUserPass {{{1
120com! -nargs=* Nread call <SID>NetSavePosn()<bar>call <SID>NetRead(<f-args>)<bar>call <SID>NetRestorePosn()
121com! -range=% -nargs=* Nwrite call <SID>NetSavePosn()<bar><line1>,<line2>call <SID>NetWrite(<f-args>)<bar>call <SID>NetRestorePosn()
122com! -nargs=* NetUserPass call NetUserPass(<f-args>)
123
124" ------------------------------------------------------------------------
125" NetSavePosn: saves position of cursor on screen {{{1
126fun! s:NetSavePosn()
127" call Dfunc("NetSavePosn()")
128 " Save current line and column
129 let s:netrw_winnr= winnr()
130 let s:netrw_line = line(".")
131 let s:netrw_col = virtcol(".")
132
133 " Save top-of-screen line
134 norm! H0
135 let s:netrw_hline= line(".")
136
137 call s:NetRestorePosn()
138" call Dret("NetSavePosn : winnr=".s:netrw_winnr." line=".s:netrw_line." col=".s:netrw_col." hline=".s:netrw_hline)
139endfun
140
141" ------------------------------------------------------------------------
142" NetRestorePosn: restores the cursor and file position as saved by NetSavePosn() {{{1
143fun! <SID>NetRestorePosn()
144" call Dfunc("NetRestorePosn() winnr=".s:netrw_winnr." line=".s:netrw_line." col=".s:netrw_col." hline=".s:netrw_hline)
145
146 exe "silent! ".s:netrw_winnr."wincmd w"
147 if v:shell_error == 0
148 " as suggested by Bram M: redraw on no error
149 " allows protocol error messages to remain visible
150 redraw!
151 endif
152 " restore top-of-screen line
153 exe "norm! ".s:netrw_hline."G0z\<CR>"
154 " restore position
155 exe "norm! ".s:netrw_line."G0".s:netrw_col."\<bar>"
156
157" call Dret("NetRestorePosn")
158endfun
159
160" ------------------------------------------------------------------------
161" NetRead: responsible for reading a file over the net {{{1
162fun! s:NetRead(...)
163" call Dfunc("NetRead(a:1<".a:1.">)")
164
165 " save options
166 call s:NetOptionSave()
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 " Special Exception: if a file is named "0r", then
169 " "0r" will be used to read the
170 " following files instead of "r"
171 if a:0 == 0
172 let readcmd= "r"
173 let ichoice= 0
174 elseif a:1 == "0r"
175 let readcmd = "0r"
176 let ichoice = 2
177 else
178 let readcmd = "r"
179 let ichoice = 1
180 endif
181
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000182 " get name of a temporary file
183 let tmpfile= tempname()
184
185" call Decho("ichoice=".ichoice." readcmd<".readcmd.">")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 while ichoice <= a:0
187
188 " attempt to repeat with previous host-file-etc
189 if exists("b:netrw_lastfile") && a:0 == 0
190" call Decho("using b:netrw_lastfile<" . b:netrw_lastfile . ">")
191 let choice = b:netrw_lastfile
192 let ichoice= ichoice + 1
193
194 else
195 exe "let choice= a:" . ichoice
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000196" call Decho("no lastfile: choice<" . choice . ">")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
198 " Reconstruct Choice if choice starts with '"'
199 if match(choice,"?") == 0
200 echo 'NetRead Usage:'
201 echo ':Nread machine:path uses rcp'
202 echo ':Nread "machine path" uses ftp with <.netrc>'
203 echo ':Nread "machine id password path" uses ftp'
204 echo ':Nread dav://machine[:port]/path uses cadaver'
205 echo ':Nread fetch://machine/path uses fetch'
206 echo ':Nread ftp://[user@]machine[:port]/path uses ftp autodetects <.netrc>'
207 echo ':Nread http://[user@]machine/path uses http wget'
208 echo ':Nread rcp://[user@]machine/path uses rcp'
209 echo ':Nread rsync://machine[:port]/path uses rsync'
210 echo ':Nread scp://[user@]machine[[:#]port]/path uses scp'
211 echo ':Nread sftp://[user@]machine[[:#]port]/path uses sftp'
212 break
213 elseif match(choice,"^\"") != -1
214" call Decho("reconstructing choice")
215 if match(choice,"\"$") != -1
216 " case "..."
217 let choice=strpart(choice,1,strlen(choice)-2)
218 else
219 " case "... ... ..."
220 let choice = strpart(choice,1,strlen(choice)-1)
221 let wholechoice = ""
222
223 while match(choice,"\"$") == -1
224 let wholechoice = wholechoice . " " . choice
225 let ichoice = ichoice + 1
226 if ichoice > a:0
227 echoerr "Unbalanced string in filename '". wholechoice ."'"
228" call Dret("NetRead")
229 return
230 endif
231 let choice= a:{ichoice}
232 endwhile
233 let choice= strpart(wholechoice,1,strlen(wholechoice)-1) . " " . strpart(choice,0,strlen(choice)-1)
234 endif
235 endif
236 endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000237" call Decho("choice<" . choice . ">")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 let ichoice= ichoice + 1
239
240 " fix up windows urls
241 if has("win32")
242 let choice = substitute(choice,'\\','/','ge')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000243" call Decho("fixing up windows url to <".choice."> tmpfile<".tmpfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 exe 'lcd ' . fnamemodify(tmpfile,':h')
245 let tmpfile = fnamemodify(tmpfile,':t')
246 endif
247
248 " Determine method of read (ftp, rcp, etc)
249 call s:NetMethod(choice)
250
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000251 " Check if NetList() should be handling this request
252" call Decho("checking if netlist: choice<".choice."> netrw_list_cmd<".g:netrw_list_cmd.">")
253 if choice =~ "^.*/$"
254 if strlen(g:netrw_list_cmd) > 0
255 call s:NetList(choice)
256" call Dret("NetRead")
257 else
258 echoerr "sorry, can't do a remote listing; ssh isn't executable"
259 endif
260 return
261 endif
262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 " ============
264 " Perform Read
265 " ============
266
267 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000268 " rcp: NetRead Method #1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 if b:netrw_method == 1 " read with rcp
270" call Decho("read via rcp (method #1)")
271 " ER: noting done with g:netrw_uid yet?
272 " ER: on Win2K" rcp machine[.user]:file tmpfile
273 " ER: if machine contains '.' adding .user is required (use $USERNAME)
274 " ER: the tmpfile is full path: rcp sees C:\... as host C
275 if s:netrw_has_nt_rcp == 1
276 if exists("g:netrw_uid") && ( g:netrw_uid != "" )
277 let uid_machine = g:netrw_machine .'.'. g:netrw_uid
278 else
279 " Any way needed it machine contains a '.'
280 let uid_machine = g:netrw_machine .'.'. $USERNAME
281 endif
282 else
283 if exists("g:netrw_uid") && ( g:netrw_uid != "" )
284 let uid_machine = g:netrw_uid .'@'. g:netrw_machine
285 else
286 let uid_machine = g:netrw_machine
287 endif
288 endif
289" call Decho("executing: !".g:netrw_rcp_cmd." ".s:netrw_rcpmode." ".uid_machine.":".escape(b:netrw_fname,' ?&')." ".tmpfile)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000290 exe g:netrw_silentxfer."!".g:netrw_rcp_cmd." ".s:netrw_rcpmode." ".uid_machine.":".escape(b:netrw_fname,' ?&')." ".tmpfile
291 let result = s:NetGetFile(readcmd, tmpfile, b:netrw_method)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 let b:netrw_lastfile = choice
293
294 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000295 " ftp + <.netrc>: NetRead Method #2
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 elseif b:netrw_method == 2 " read with ftp + <.netrc>
297" call Decho("read via ftp+.netrc (method #2)")
298 let netrw_fname= b:netrw_fname
299 new
300 set ff=unix
301 exe "put ='".g:netrw_ftpmode."'"
302 exe "put ='get ".netrw_fname." ".tmpfile."'"
303 if exists("g:netrw_port") && g:netrw_port != ""
304" call Decho("executing: %!".g:netrw_ftp_cmd." -i ".g:netrw_machine." ".g:netrw_port)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000305 exe g:netrw_silentxfer."%!".g:netrw_ftp_cmd." -i ".g:netrw_machine." ".g:netrw_port
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 else
307" call Decho("executing: %!".g:netrw_ftp_cmd." -i ".g:netrw_machine)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000308 exe g:netrw_silentxfer."%!".g:netrw_ftp_cmd." -i ".g:netrw_machine
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 endif
310 " If the result of the ftp operation isn't blank, show an error message (tnx to Doug Claar)
311 if getline(1) !~ "^$"
312 echoerr getline(1)
313 endif
314 bd!
315 let result = s:NetGetFile(readcmd, tmpfile, b:netrw_method)
316 let b:netrw_lastfile = choice
317
318 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000319 " ftp + machine,id,passwd,filename: NetRead Method #3
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 elseif b:netrw_method == 3 " read with ftp + machine, id, passwd, and fname
321 " Construct execution string (four lines) which will be passed through filter
322" call Decho("read via ftp+mipf (method #3)")
323 let netrw_fname= b:netrw_fname
324 new
325 set ff=unix
326 if exists("g:netrw_port") && g:netrw_port != ""
327 put ='open '.g:netrw_machine.' '.g:netrw_port
328 else
329 put ='open '.g:netrw_machine
330 endif
331
332 if exists("g:netrw_ftp") && g:netrw_ftp == 1
333 put =g:netrw_uid
334 put =g:netrw_passwd
335 else
336 put ='user '.g:netrw_uid.' '.g:netrw_passwd
337 endif
338
339 if exists("g:netrw_ftpmode") && g:netrw_ftpmode != ""
340 put =g:netrw_ftpmode
341 endif
342 put ='get '.netrw_fname.' '.tmpfile
343
344 " perform ftp:
345 " -i : turns off interactive prompting from ftp
346 " -n unix : DON'T use <.netrc>, even though it exists
347 " -n win32: quit being obnoxious about password
348" call Decho('performing ftp -i -n')
349 norm 1Gdd
350" call Decho("executing: %!".g:netrw_ftp_cmd." -i -n")
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000351 exe g:netrw_silentxfer."%!".g:netrw_ftp_cmd." -i -n"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 " If the result of the ftp operation isn't blank, show an error message (tnx to Doug Claar)
353 if getline(1) !~ "^$"
354 echoerr getline(1)
355 endif
356 bd!
357 let result = s:NetGetFile(readcmd, tmpfile, b:netrw_method)
358 let b:netrw_lastfile = choice
359
360 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000361 " scp: NetRead Method #4
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 elseif b:netrw_method == 4 " read with scp
363" call Decho("read via scp (method #4)")
364 if exists("g:netrw_port") && g:netrw_port != ""
365 let useport= " -P ".g:netrw_port
366 else
367 let useport= ""
368 endif
369 if g:netrw_cygwin == 1
370 let cygtmpfile=substitute(tmpfile,'^\(\a\):','/cygdrive/\1/','e')
371" call Decho("executing: !".g:netrw_scp_cmd.useport." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".cygtmpfile)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000372 exe g:netrw_silentxfer."!".g:netrw_scp_cmd.useport." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".cygtmpfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 else
374" call Decho("executing: !".g:netrw_scp_cmd.useport." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".tmpfile)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000375 exe g:netrw_silentxfer."!".g:netrw_scp_cmd.useport." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".tmpfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 endif
377 let result = s:NetGetFile(readcmd, tmpfile, b:netrw_method)
378 let b:netrw_lastfile = choice
379
380 ".........................................
381 elseif b:netrw_method == 5 " read with http (wget)
382" call Decho("read via http (method #5)")
383 if g:netrw_http_cmd == ""
384 echoerr "neither wget nor fetch command is available"
385 exit
386 endif
387
388 if match(b:netrw_fname,"#") == -1
389 " simple wget
390" call Decho("executing: !".g:netrw_http_cmd." ".tmpfile." http://".g:netrw_machine.escape(b:netrw_fname,' ?&'))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000391 exe g:netrw_silentxfer."!".g:netrw_http_cmd." ".tmpfile." http://".g:netrw_machine.escape(b:netrw_fname,' ?&')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 let result = s:NetGetFile(readcmd, tmpfile, b:netrw_method)
393
394 else
395 " wget plus a jump to an in-page marker (ie. http://abc/def.html#aMarker)
396 let netrw_html= substitute(b:netrw_fname,"#.*$","","")
397 let netrw_tag = substitute(b:netrw_fname,"^.*#","","")
398" call Decho("netrw_html<".netrw_html.">")
399" call Decho("netrw_tag <".netrw_tag.">")
400" call Decho("executing: !".g:netrw_http_cmd." ".tmpfile." http://".g:netrw_machine.netrw_html)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000401 exe g:netrw_silentxfer."!".g:netrw_http_cmd." ".tmpfile." http://".g:netrw_machine.netrw_html
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 let result = s:NetGetFile(readcmd, tmpfile, b:netrw_method)
403" call Decho('<\s*a\s*name=\s*"'.netrw_tag.'"/')
404 exe 'norm! 1G/<\s*a\s*name=\s*"'.netrw_tag.'"/'."\<CR>"
405 endif
406 let b:netrw_lastfile = choice
407
408 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000409 " cadaver: NetRead Method #6
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 elseif b:netrw_method == 6 " read with cadaver
411" call Decho("read via cadaver (method #6)")
412
413 " Construct execution string (four lines) which will be passed through filter
414 let netrw_fname= b:netrw_fname
415 new
416 set ff=unix
417 if exists("g:netrw_port") && g:netrw_port != ""
418 put ='open '.g:netrw_machine.' '.g:netrw_port
419 else
420 put ='open '.g:netrw_machine
421 endif
422 put ='user '.g:netrw_uid.' '.g:netrw_passwd
423
424 if g:netrw_cygwin == 1
425 let cygtmpfile=substitute(tmpfile,'^\(\a\):','/cygdrive/\1/','e')
426 put ='get '.netrw_fname.' '.cygtmpfile
427 else
428 put ='get '.netrw_fname.' '.tmpfile
429 endif
430
431 " perform cadaver operation:
432 norm 1Gdd
433" call Decho("executing: %!".g:netrw_dav_cmd)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000434 exe g:netrw_silentxfer."%!".g:netrw_dav_cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 bd!
436 let result = s:NetGetFile(readcmd, tmpfile, b:netrw_method)
437 let b:netrw_lastfile = choice
438
439 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000440 " rsync: NetRead Method #7
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 elseif b:netrw_method == 7 " read with rsync
442" call Decho("read via rsync (method #7)")
443 if g:netrw_cygwin == 1
444 let cygtmpfile=substitute(tmpfile,'^\(\a\):','/cygdrive/\1/','e')
445" call Decho("executing: !".g:netrw_rsync_cmd." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".cygtmpfile)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000446 exe g:netrw_silentxfer."!".g:netrw_rsync_cmd." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".cygtmpfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 else
448" call Decho("executing: !".g:netrw_rsync_cmd." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".tmpfile)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000449 exe g:netrw_silentxfer."!".g:netrw_rsync_cmd." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".tmpfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 endif
451 let result = s:NetGetFile(readcmd,tmpfile, b:netrw_method)
452 let b:netrw_lastfile = choice
453
454 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000455 " fetch: NetRead Method #8
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 " fetch://[user@]host[:http]/path
457 elseif b:netrw_method == 8 " read with fetch
458 if g:netrw_fetch_cmd == ""
459 echoerr "fetch command not available"
460 exit
461 endif
462 if exists("g:netrw_option") && g:netrw_option == ":http"
463 let netrw_option= "http"
464 else
465 let netrw_option= "ftp"
466 endif
467" call Decho("read via fetch for ".netrw_option)
468
469 if exists("g:netrw_uid") && g:netrw_uid != "" && exists("g:netrw_passwd") && g:netrw_passwd != ""
470" call Decho("executing: !".g:netrw_fetch_cmd." ".tmpfile." ".netrw_option."://".g:netrw_uid.':'.g:netrw_passwd.'@'.g:netrw_machine."/".escape(b:netrw_fname,' ?&'))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000471 exe g:netrw_silentxfer."!".g:netrw_fetch_cmd." ".tmpfile." ".netrw_option."://".g:netrw_uid.':'.g:netrw_passwd.'@'.g:netrw_machine."/".escape(b:netrw_fname,' ?&')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 else
473" call Decho("executing: !".g:netrw_fetch_cmd." ".tmpfile." ".netrw_option."://".g:netrw_machine."/".escape(b:netrw_fname,' ?&'))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000474 exe g:netrw_silentxfer."!".g:netrw_fetch_cmd." ".tmpfile." ".netrw_option."://".g:netrw_machine."/".escape(b:netrw_fname,' ?&')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 endif
476
477 let result = s:NetGetFile(readcmd,tmpfile, b:netrw_method)
478 let b:netrw_lastfile = choice
479
480 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000481 " sftp: NetRead Method #9
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 elseif b:netrw_method == 9 " read with sftp
483" call Decho("read via sftp (method #4)")
484 if g:netrw_cygwin == 1
485 let cygtmpfile=substitute(tmpfile,'^\(\a\):','/cygdrive/\1/','e')
486" call Decho("!".g:netrw_sftp_cmd." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".cygtmpfile)
487" call Decho("executing: !".g:netrw_sftp_cmd." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".cygtmpfile)
488 exe "!".g:netrw_sftp_cmd." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".cygtmpfile
489 else
490" call Decho("executing: !".g:netrw_sftp_cmd." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".tmpfile)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000491 exe g:netrw_silentxfer."!".g:netrw_sftp_cmd." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')." ".tmpfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 endif
493 let result = s:NetGetFile(readcmd, tmpfile, b:netrw_method)
494 let b:netrw_lastfile = choice
495
496 ".........................................
497 else " Complain
498 echo "***warning*** unable to comply with your request<" . choice . ">"
499 endif
500 endwhile
501
502 " cleanup
503" call Decho("cleanup")
504 if exists("b:netrw_method")
505 unlet b:netrw_method
506 unlet g:netrw_machine
507 unlet b:netrw_fname
508 endif
509 call s:NetOptionRestore()
510
511" call Dret("NetRead")
512endfun
513" end of NetRead
514
515" ------------------------------------------------------------------------
516" NetGetFile: Function to read file "fname" with command "readcmd". {{{1
517fun! s:NetGetFile(readcmd, fname, method)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000518"" call Dfunc("NetGetFile(readcmd<".a:readcmd.">,fname<".a:fname."> method<".a:method.">)")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
520 if exists("*NetReadFixup")
521 " for the use of NetReadFixup (not otherwise used internally)
522 let line2= line("$")
523 endif
524
525 " transform paths from / to \ for Windows, unless the shell is bash
526 if &term == "win32"
527 if &shell == "bash"
528 let fname=a:fname
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000529"" call Decho("(win32 && bash) fname<".fname.">")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 else
531 let fname=substitute(a:fname,'/','\\\\','ge')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000532"" call Decho("(win32 && !bash) fname<".fname.">")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 endif
534 else
535 let fname= a:fname
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000536"" call Decho("(copied) fname<".fname.">")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 endif
538
539 " get the file, but disable undo when reading a new buffer
540 if a:readcmd[0] == '0'
541 let use_e_cmd = 0 " 1 when using ':edit'
542 let delline = 0 " 1 when have to delete empty last line
543 if line("$") == 1 && getline(1) == ""
544 " Now being asked to 0r a file into an empty file.
545 " Safe to :e it instead, unless there is another window on the same buffer.
546 let curbufnr = bufnr("%")
547 let use_e_cmd = 1
548 let delline = 1
549 " Loop over all windows,
550 " reset use_e_cmd when another one is editing the current buffer.
551 let i = 1
552 while 1
553 if i != winnr() && winbufnr(i) == curbufnr
554 let use_e_cmd = 0
555 break
556 endif
557 let i = i + 1
558 if winbufnr(i) < 0
559 break
560 endif
561 endwhile
562 endif
563
564 if use_e_cmd > 0
565 " ':edit' the temp file, wipe out the old buffer and rename the buffer
566 let curfilename = expand("%")
567
568 let binlocal = &l:bin
569 let binglobal = &g:bin
570 if binlocal
571 setglobal bin " Need to set 'bin' globally for ":e" command.
572 endif
573 silent exe "e! ".v:cmdarg." ".fname
574 if binlocal && !binglobal
575 setglobal nobin
576 setlocal bin
577 endif
578
579 exe curbufnr . "bwipe!"
580 exe "f ".curfilename
581 " the ":f newname" apparently leaves the temporary file as the alternate
582 " file in the buffer list (see :ls!). The following command wipes it out.
583 exe bufnr("#")."bwipe!"
584 else
585 let oldul= &ul
586 set ul=-1
587 exe a:readcmd." ".v:cmdarg." ".fname
588 if delline > 0
589 " wipe out last line, which should be a blank line anyway
590 $del
591 endif
592 let &ul= oldul
593 endif
594 else
595 exe a:readcmd." ".v:cmdarg." ".fname
596 endif
597
598 " User-provided (ie. optional) fix-it-up command
599 if exists("*NetReadFixup")
600 let line1= line(".")
601 if a:readcmd == "r"
602 let line2= line("$") - line2 + line1
603 else
604 let line2= line("$") - line2
605 endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000606"" call Decho("calling NetReadFixup(method<".a:method."> line1=".line1." line2=".line2.")")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 call NetReadFixup(a:method, line1, line2)
608 endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000609"" call Decho("readcmd<".a:readcmd."> cmdarg<".v:cmdarg."> fname<".a:fname."> readable=".filereadable(a:fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611" insure that we have the right filetype and that its being displayed
612 filetype detect
613 redraw!
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000614"" call Dret("NetGetFile")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615endfun
616
617" ------------------------------------------------------------------------
618" NetWrite: responsible for writing a file over the net {{{1
619fun! s:NetWrite(...) range
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000620"" call Dfunc("NetWrite(a:0=".a:0.")")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622 " option handling
623 let mod= 0
624 call s:NetOptionSave()
625
626 " Get Temporary Filename
627 let tmpfile= tempname()
628
629 if a:0 == 0
630 let ichoice = 0
631 else
632 let ichoice = 1
633 endif
634
635 " write (selected portion of) file to temporary
636 silent exe a:firstline."," . a:lastline . "w! ".v:cmdarg." ".tmpfile
637
638 while ichoice <= a:0
639
640 " attempt to repeat with previous host-file-etc
641 if exists("b:netrw_lastfile") && a:0 == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000642"" call Decho("using b:netrw_lastfile<" . b:netrw_lastfile . ">")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 let choice = b:netrw_lastfile
644 let ichoice= ichoice + 1
645 else
646 exe "let choice= a:" . ichoice
647
648 " Reconstruct Choice if choice starts with '"'
649 if match(choice,"?") == 0
650 echo 'NetWrite Usage:"'
651 echo ':Nwrite machine:path uses rcp'
652 echo ':Nwrite "machine path" uses ftp with <.netrc>'
653 echo ':Nwrite "machine id password path" uses ftp'
654 echo ':Nwrite dav://[user@]machine/path uses cadaver'
655 echo ':Nwrite fetch://[user@]machine/path uses fetch'
656 echo ':Nwrite ftp://machine[#port]/path uses ftp (autodetects <.netrc>)'
657 echo ':Nwrite rcp://machine/path uses rcp'
658 echo ':Nwrite rsync://[user@]machine/path uses rsync'
659 echo ':Nwrite scp://[user@]machine[[:#]port]/path uses scp'
660 echo ':Nwrite sftp://[user@]machine/path uses sftp'
661 break
662
663 elseif match(choice,"^\"") != -1
664 if match(choice,"\"$") != -1
665 " case "..."
666 let choice=strpart(choice,1,strlen(choice)-2)
667 else
668 " case "... ... ..."
669 let choice = strpart(choice,1,strlen(choice)-1)
670 let wholechoice = ""
671
672 while match(choice,"\"$") == -1
673 let wholechoice= wholechoice . " " . choice
674 let ichoice = ichoice + 1
675 if choice > a:0
676 echoerr "Unbalanced string in filename '". wholechoice ."'"
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000677"" call Dret("NetWrite")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return
679 endif
680 let choice= a:{ichoice}
681 endwhile
682 let choice= strpart(wholechoice,1,strlen(wholechoice)-1) . " " . strpart(choice,0,strlen(choice)-1)
683 endif
684 endif
685 endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000686"" call Decho("choice<" . choice . ">")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 let ichoice= ichoice + 1
688
689 " fix up windows urls
690 if has("win32")
691 let choice= substitute(choice,'\\','/','ge')
692 "ER: see NetRead()
693 exe 'lcd ' . fnamemodify(tmpfile,':h')
694 let tmpfile = fnamemodify(tmpfile,':t')
695 endif
696
697 " Determine method of read (ftp, rcp, etc)
698 call s:NetMethod(choice)
699
700 " =============
701 " Perform Write
702 " =============
703
704 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000705 " rcp: NetWrite Method #1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if b:netrw_method == 1 " write with rcp
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000707"" Decho "write via rcp (method #1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if s:netrw_has_nt_rcp == 1
709 if exists("g:netrw_uid") && ( g:netrw_uid != "" )
710 let uid_machine = g:netrw_machine .'.'. g:netrw_uid
711 else
712 let uid_machine = g:netrw_machine .'.'. $USERNAME
713 endif
714 else
715 if exists("g:netrw_uid") && ( g:netrw_uid != "" )
716 let uid_machine = g:netrw_uid .'@'. g:netrw_machine
717 else
718 let uid_machine = g:netrw_machine
719 endif
720 endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000721"" call Decho("executing: !".g:netrw_rcp_cmd." ".s:netrw_rcpmode." ".tmpfile." ".uid_machine.":".escape(b:netrw_fname,' ?&'))
722 exe g:netrw_silentxfer."!".g:netrw_rcp_cmd." ".s:netrw_rcpmode." ".tmpfile." ".uid_machine.":".escape(b:netrw_fname,' ?&')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 let b:netrw_lastfile = choice
724
725 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000726 " ftp + <.netrc>: NetWrite Method #2
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 elseif b:netrw_method == 2 " write with ftp + <.netrc>
728 let netrw_fname = b:netrw_fname
729 new
730 set ff=unix
731 exe "put ='".g:netrw_ftpmode."'"
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000732"" call Decho(" NetWrite: put ='".g:netrw_ftpmode."'")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 exe "put ='put ".tmpfile." ".netrw_fname."'"
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000734"" call Decho("put ='put ".tmpfile." ".netrw_fname."'")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 if exists("g:netrw_port") && g:netrw_port != ""
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000736"" call Decho("executing: %!".g:netrw_ftp_cmd." -i ".g:netrw_machine." ".g:netrw_port)
737 exe g:netrw_silentxfer."%!".g:netrw_ftp_cmd." -i ".g:netrw_machine." ".g:netrw_port
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000739"" call Decho("executing: %!".g:netrw_ftp_cmd." -i ".g:netrw_machine)
740 exe g:netrw_silentxfer."%!".g:netrw_ftp_cmd." -i ".g:netrw_machine
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 endif
742 " If the result of the ftp operation isn't blank, show an error message (tnx to Doug Claar)
743 if getline(1) !~ "^$"
744 echoerr getline(1)
745 let mod=1
746 endif
747 bd!
748 let b:netrw_lastfile = choice
749
750 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000751 " ftp + machine, id, passwd, filename: NetWrite Method #3
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 elseif b:netrw_method == 3 " write with ftp + machine, id, passwd, and fname
753 let netrw_fname= b:netrw_fname
754 new
755 set ff=unix
756 if exists("g:netrw_port") && g:netrw_port != ""
757 put ='open '.g:netrw_machine.' '.g:netrw_port
758 else
759 put ='open '.g:netrw_machine
760 endif
761 if exists("g:netrw_ftp") && g:netrw_ftp == 1
762 put =g:netrw_uid
763 put =g:netrw_passwd
764 else
765 put ='user '.g:netrw_uid.' '.g:netrw_passwd
766 endif
767 put ='put '.tmpfile.' '.netrw_fname
768 " save choice/id/password for future use
769 let b:netrw_lastfile = choice
770
771 " perform ftp:
772 " -i : turns off interactive prompting from ftp
773 " -n unix : DON'T use <.netrc>, even though it exists
774 " -n win32: quit being obnoxious about password
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000775"" call Decho('performing ftp -i -n')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 norm 1Gdd
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000777"" call Decho("executing: %!".g:netrw_ftp_cmd." -i -n")
778 exe g:netrw_silentxfer."%!".g:netrw_ftp_cmd." -i -n"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 " If the result of the ftp operation isn't blank, show an error message (tnx to Doug Claar)
780 if getline(1) !~ "^$"
781 echoerr getline(1)
782 let mod=1
783 endif
784 bd!
785
786 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000787 " scp: NetWrite Method #4
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 elseif b:netrw_method == 4 " write with scp
789 if exists("g:netrw_port") && g:netrw_port != ""
790 let useport= " -P ".g:netrw_port
791 else
792 let useport= ""
793 endif
794 if g:netrw_cygwin == 1
795 let cygtmpfile=substitute(tmpfile,'^\(\a\):','/cygdrive/\1/','e')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000796"" call Decho("executing: !".g:netrw_scp_cmd.useport." ".cygtmpfile." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&'))
797 exe g:netrw_silentxfer."!".g:netrw_scp_cmd.useport." ".cygtmpfile." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000799"" call Decho("executing: !".g:netrw_scp_cmd.useport." ".tmpfile." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&'))
800 exe g:netrw_silentxfer."!".g:netrw_scp_cmd.useport." ".tmpfile." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 endif
802 let b:netrw_lastfile = choice
803
804 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000805 " http: NetWrite Method #5
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 elseif b:netrw_method == 5
807 echoerr "***warning*** currently <netrw.vim> does not support writing using http:"
808
809 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000810 " dav: NetWrite Method #6
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 elseif b:netrw_method == 6 " write with cadaver
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000812"" call Decho("write via cadaver (method #6)")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 " Construct execution string (four lines) which will be passed through filter
815 let netrw_fname= b:netrw_fname
816 new
817 set ff=unix
818 if exists("g:netrw_port") && g:netrw_port != ""
819 put ='open '.g:netrw_machine.' '.g:netrw_port
820 else
821 put ='open '.g:netrw_machine
822 endif
823 put ='user '.g:netrw_uid.' '.g:netrw_passwd
824
825 if g:netrw_cygwin == 1
826 let cygtmpfile=substitute(tmpfile,'^\(\a\):','/cygdrive/\1/','e')
827 put ='put '.cygtmpfile.' '.netrw_fname
828 else
829 put ='put '.tmpfile.' '.netrw_fname
830 endif
831
832 " perform cadaver operation:
833 norm 1Gdd
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000834"" call Decho("executing: %!".g:netrw_dav_cmd)
835 exe g:netrw_silentxfer."%!".g:netrw_dav_cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 bd!
837 let b:netrw_lastfile = choice
838
839 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000840 " rsync: NetWrite Method #7
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 elseif b:netrw_method == 7 " write with rsync
842 if g:netrw_cygwin == 1
843 let cygtmpfile=substitute(tmpfile,'^\(\a\):','/cygdrive/\1/','e')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000844"" call Decho("executing: !".g:netrw_rsync_cmd." ".cygtmpfile." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&'))
845 exe g:netrw_silentxfer."!".g:netrw_rsync_cmd." ".cygtmpfile." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000847"" call Decho("executing: !".g:netrw_rsync_cmd." ".tmpfile." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&'))
848 exe g:netrw_silentxfer."!".g:netrw_rsync_cmd." ".tmpfile." ".g:netrw_machine.":".escape(b:netrw_fname,' ?&')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 endif
850 let b:netrw_lastfile = choice
851
852 ".........................................
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000853 " scp: NetWrite Method #9
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 elseif b:netrw_method == 9 " write with sftp
855 let netrw_fname= b:netrw_fname
856 if exists("g:netrw_uid") && ( g:netrw_uid != "" )
857 let uid_machine = g:netrw_uid .'@'. g:netrw_machine
858 else
859 let uid_machine = g:netrw_machine
860 endif
861 new
862 set ff=unix
863 put ='put '.tmpfile.' '.netrw_fname
864 norm 1Gdd
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000865"" call Decho("executing: %!".g:netrw_sftp_cmd.' '.uid_machine)
866 exe g:netrw_silentxfer."%!".g:netrw_sftp_cmd.' '.uid_machine
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 bd!
868 let b:netrw_lastfile= choice
869
870 ".........................................
871 else " Complain
872 echo "***warning*** unable to comply with your request<" . choice . ">"
873 endif
874 endwhile
875
876 " cleanup
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000877"" call Decho("cleanup")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 let result=delete(tmpfile)
879 call s:NetOptionRestore()
880
881 if a:firstline == 1 && a:lastline == line("$")
882 let &mod= mod " usually equivalent to set nomod
883 endif
884
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000885"" call Dret("NetWrite")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886endfun
887" end of NetWrite
888
889" ------------------------------------------------------------------------
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000890" NetList: This function uses the command in g:netrw_list_cmd to get a list {{{1
891" of the contents of a remote directory. It is assumed that the
892" g:netrw_list_cmd has a string, HOSTNAME, that needs to be substituted
893" with the requested remote hostname first.
894fun! <SID>NetList(dirname)
895"" call Dfunc("NetList(dirname<".a:dirname.">)")
896
897 " sanity maintenance
898 let dirname= a:dirname
899 if dirname !~ '/$' && (!exists("g:netrw_machine") || !exists("b:netlist_method") || !exists("b:netlist_path"))
900 let dirname= dirname."/"
901 endif
902 set ma
903
904 if dirname !~ '/$' && dirname !~ '^"'
905 " looks like a regular file, attempt transfer
906"" call Decho("attempt transfer with regular file<".dirname.">")
907"" call Decho("netlist method<".b:netlist_method."> cmd<".b:netlist_cmd."> path<".b:netlist_path.">")
908 %d
909
910 " remove any filetype indicator from end of dirname, except for the
911 " "this is a directory" indicator (/)
912 let dirname= substitute(dirname,"[*=@|]$","","e")
913"" call Decho("modified dirname<".dirname.">")
914
915 " remote-read the requested file into current buffer
916 let machine= g:netrw_machine
917 let method = b:netlist_method
918 let path = b:netlist_path
919 enew!
920 exe "file ".method."://".machine."/".path.dirname
921 exe "doau BufReadPre ".dirname
922 silent call s:NetRead(method."://".machine."/".path.dirname)
923 exe "doau BufReadPost ".dirname
924 1d
925 set nomod noma
926
927 silent! unlet b:netlist_method
928 silent! unlet b:netlist_cmd
929 silent! unlet b:netlist_path
930"" call Dret("NetList")
931 return
932
933 elseif dirname == './'
934 " refresh the directory list
935"" call Decho("refresh directory listing")
936"" call Decho("netlist method<".b:netlist_method."> cmd<".b:netlist_cmd."> path<".b:netlist_path.">")
937 %d
938
939 elseif dirname == '../'
940 " go up one directory
941
942 if b:netlist_path !~ '/' && strlen(b:netlist_path) > 0
943 " go to top (rltv) directory
944"" call Decho("go up one directory : 1 path<".b:netlist_path.">")
945 let b:netlist_path= ""
946
947 elseif b:netlist_path !~ '^[./]\+$' && strlen(b:netlist_path) > 0
948 " remove a directory from the netlist_path
949"" call Decho("go up one directory : 2 path<".b:netlist_path.">")
950 if b:netlist_path =~ '^[^/]*/$'
951 let b:netlist_path= ""
952 else
953 let b:netlist_path= substitute(b:netlist_path,'^\(.*/\)[^/]*/$','\1','')
954 endif
955
956 else
957 " append ../
958"" call Decho("go up one directory : 3 path<".b:netlist_path.">")
959 let b:netlist_path= b:netlist_path.'../'
960 endif
961"" call Decho("netlist method<".b:netlist_method."> cmd<".b:netlist_cmd."> path<".b:netlist_path.">")
962 %d
963
964 elseif dirname !~ '^\w\+:' && dirname =~ '/$'
965 " go down one directory
966"" call Decho("go down one directory<".dirname.">")
967 let b:netlist_path=b:netlist_path.dirname
968"" call Decho("netlist method<".b:netlist_method."> cmd<".b:netlist_cmd."> path<".b:netlist_path.">")
969 %d
970
971 else
972 " normal initial directory listing
973"" call Decho("normal initial directory listing")
974
975 let listcmd = substitute(g:netrw_list_cmd,'\<HOSTNAME\>',g:netrw_machine,'')
976 let method = substitute(dirname,'^\(\w\+\):.*$','\1','')
977 let fname = b:netrw_fname
978"" call Decho("set up listcmd<".listcmd.">")
979"" call Decho("set up method <".method.">")
980"" call Decho("set up fname <".fname.">")
981
982 " set up window
983 if &mod == 1
984 wincmd s
985 endif
986 enew!
987 if v:version >= 602
988 setlocal bh=wipe
989 endif
990
991 " set up buffer-local variables
992 let b:netlist_method = method
993 let b:netlist_cmd = listcmd
994 let b:netlist_path = fname
995
996 " set up buffer-local map
997 nnoremap <buffer> <cr> :call <SID>NetList(expand("<cWORD>"))<cr>
998
999"" call Decho("netlist method<".b:netlist_method."> cmd<".b:netlist_cmd."> path<".b:netlist_path.">")
1000 endif
1001
1002"" call Decho("executing: r! ".b:netlist_cmd." '".b:netlist_path."'")
1003 put ='\" =============================='
1004 put ='\" Netrw Remote Directory Listing'
1005 put ='\" '.g:netrw_machine.':'.b:netlist_path
1006 put ='\" =============================='
1007 exe "silent r! ".b:netlist_cmd." '".b:netlist_path."'"
1008 1d
1009 set ft=netrwlist
1010 silent 5,$s/^\(.*\)\/$/ \1/e
1011 silent 5,$call s:NetSort()
1012 silent 5,$s/^ \(.*\)$/\1\//e
1013 5
1014 exe 'file ['.g:netrw_machine.':'.b:netlist_path.']'
1015 if v:version >= 602
1016 setlocal nomod bh=wipe bt=nofile nobl noma
1017 else
1018 setlocal nomod
1019 endif
1020
1021"" call Dret("NetList")
1022endfun
1023
1024" ------------------------------------------------------------------------
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025" NetMethod: determine method of transfer {{{1
1026" method == 1: rcp
1027" 2: ftp + <.netrc>
1028" 3: ftp + machine, id, password, and [path]filename
1029" 4: scp
1030" 5: http (wget)
1031" 6: cadaver
1032" 7: rsync
1033" 8: fetch
1034" 9: sftp
1035fun! s:NetMethod(choice) " globals: method machine id passwd fname
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001036"" call Dfunc("NetMethod(a:choice<".a:choice.">)")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
1038 " initialization
1039 let b:netrw_method = 0
1040 let g:netrw_machine = ""
1041 let b:netrw_fname = ""
1042 let g:netrw_port = ""
1043
1044 " Patterns:
1045 " mipf : a:machine a:id password filename Use ftp
1046 " mf : a:machine filename Use ftp + <.netrc> or g:netrw_uid g:netrw_passwd
1047 " ftpurm : ftp://[user@]host[[#:]port]/filename Use ftp + <.netrc> or g:netrw_uid g:netrw_passwd
1048 " rcpurm : rcp://[user@]host/filename Use rcp
1049 " rcphf : [user@]host:filename Use rcp
1050 " scpurm : scp://[user@]host[[#:]port]/filename Use scp
1051 " httpurm : http://[user@]host/filename Use wget
1052 " davurm : dav://host[:port]/path Use cadaver
1053 " rsyncurm : rsync://host[:port]/path Use rsync
1054 " fetchurm : fetch://[user@]host[:http]/filename Use fetch (defaults to ftp, override for http)
1055 " sftpurm : sftp://[user@]host/filename Use scp
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001056 let mipf = '^\(\S\+\)\s\+\(\S\+\)\s\+\(\S\+\)\s\+\(\S\+\)$'
1057 let mf = '^\(\S\+\)\s\+\(\S\+\)$'
1058 let ftpurm = '^ftp://\(\([^/@]\{-}\)@\)\=\([^/#:]\{-}\)\([#:]\d\+\)\=/\(.*\)$'
1059 let rcpurm = '^rcp://\(\([^/@]\{-}\)@\)\=\([^/]\{-}\)/\(.*\)$'
1060 let rcphf = '^\(\(\h\w*\)@\)\=\(\h\w*\):\([^@]\+\)$'
1061 let scpurm = '^scp://\([^/]\{-}\)\([#:]\d\+\)\=/\(.*\)$'
1062 let httpurm = '^http://\([^/]\{-}\)\(/.*\)\=$'
1063 let davurm = '^dav://\([^/]\{-}\)/\(.*\)\=$'
1064 let rsyncurm = '^rsync://\([^/]\{-}\)/\(.*\)\=$'
1065 let fetchurm = '^fetch://\(\([^/@]\{-}\)@\)\=\([^/#:]\{-}\)\(:http\)\=/\(.*\)$'
1066 let sftpurm = '^sftp://\([^/]\{-}\)/\(.*\)\=$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001068"" call Decho("determine method:")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 " Determine Method
1070 " rcp://user@hostname/...path-to-file
1071 if match(a:choice,rcpurm) == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001072"" call Decho("rcp://...")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 let b:netrw_method = 1
1074 let userid = substitute(a:choice,rcpurm,'\2',"")
1075 let g:netrw_machine= substitute(a:choice,rcpurm,'\3',"")
1076 let b:netrw_fname = substitute(a:choice,rcpurm,'\4',"")
1077 if userid != ""
1078 let g:netrw_uid= userid
1079 endif
1080
1081 " scp://user@hostname/...path-to-file
1082 elseif match(a:choice,scpurm) == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001083"" call Decho("scp://...")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 let b:netrw_method = 4
1085 let g:netrw_machine= substitute(a:choice,scpurm,'\1',"")
1086 let b:netrw_port = substitute(a:choice,scpurm,'\2',"")
1087 let b:netrw_fname = substitute(a:choice,scpurm,'\3',"")
1088
1089 " http://user@hostname/...path-to-file
1090 elseif match(a:choice,httpurm) == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001091"" call Decho("http://...")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 let b:netrw_method = 5
1093 let g:netrw_machine= substitute(a:choice,httpurm,'\1',"")
1094 let b:netrw_fname = substitute(a:choice,httpurm,'\2',"")
1095
1096 " dav://hostname[:port]/..path-to-file..
1097 elseif match(a:choice,davurm) == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001098"" call Decho("dav://...")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 let b:netrw_method= 6
1100 let g:netrw_machine= substitute(a:choice,davurm,'\1',"")
1101 let b:netrw_fname = substitute(a:choice,davurm,'\2',"")
1102
1103 " rsync://user@hostname/...path-to-file
1104 elseif match(a:choice,rsyncurm) == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001105"" call Decho("rsync://...")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 let b:netrw_method = 7
1107 let g:netrw_machine= substitute(a:choice,rsyncurm,'\1',"")
1108 let b:netrw_fname = substitute(a:choice,rsyncurm,'\2',"")
1109
1110 " ftp://[user@]hostname[[:#]port]/...path-to-file
1111 elseif match(a:choice,ftpurm) == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001112"" call Decho("ftp://...")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 let userid = substitute(a:choice,ftpurm,'\2',"")
1114 let g:netrw_machine= substitute(a:choice,ftpurm,'\3',"")
1115 let g:netrw_port = substitute(a:choice,ftpurm,'\4',"")
1116 let b:netrw_fname = substitute(a:choice,ftpurm,'\5',"")
1117 if g:netrw_port != ""
1118 let g:netrw_port = substitute(g:netrw_port,"[#:]","","")
1119 endif
1120 if userid != ""
1121 let g:netrw_uid= userid
1122 endif
1123 if exists("g:netrw_uid") && exists("g:netrw_passwd")
1124 let b:netrw_method = 3
1125 else
1126 if filereadable(expand("$HOME/.netrc")) && !exists("g:netrw_ignorenetrc")
1127 let b:netrw_method= 2
1128 else
1129 if !exists("g:netrw_uid") || g:netrw_uid == ""
1130 call NetUserPass()
1131 elseif !exists("g:netrw_passwd") || g:netrw_passwd == ""
1132 call NetUserPass(g:netrw_uid)
1133 " else just use current g:netrw_uid and g:netrw_passwd
1134 endif
1135 let b:netrw_method= 3
1136 endif
1137 endif
1138
1139 elseif match(a:choice,fetchurm) == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001140"" call Decho("fetch://...")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 let b:netrw_method = 8
1142 let g:netrw_userid = substitute(a:choice,fetchurm,'\2',"")
1143 let g:netrw_machine= substitute(a:choice,fetchurm,'\3',"")
1144 let b:netrw_option = substitute(a:choice,fetchurm,'\4',"")
1145 let b:netrw_fname = substitute(a:choice,fetchurm,'\5',"")
1146
1147 " Issue an ftp : "machine id password [path/]filename"
1148 elseif match(a:choice,mipf) == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001149"" call Decho("(ftp) host id pass file")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 let b:netrw_method = 3
1151 let g:netrw_machine = substitute(a:choice,mipf,'\1',"")
1152 let g:netrw_uid = substitute(a:choice,mipf,'\2',"")
1153 let g:netrw_passwd = substitute(a:choice,mipf,'\3',"")
1154 let b:netrw_fname = substitute(a:choice,mipf,'\4',"")
1155
1156 " Issue an ftp: "hostname [path/]filename"
1157 elseif match(a:choice,mf) == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001158"" call Decho("(ftp) host file")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if exists("g:netrw_uid") && exists("g:netrw_passwd")
1160 let b:netrw_method = 3
1161 let g:netrw_machine = substitute(a:choice,mf,'\1',"")
1162 let b:netrw_fname = substitute(a:choice,mf,'\2',"")
1163
1164 elseif filereadable(expand("$HOME/.netrc"))
1165 let b:netrw_method = 2
1166 let g:netrw_machine = substitute(a:choice,mf,'\1',"")
1167 let b:netrw_fname = substitute(a:choice,mf,'\2',"")
1168 endif
1169
1170 " sftp://user@hostname/...path-to-file
1171 elseif match(a:choice,sftpurm) == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001172"" call Decho("sftp://...")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 let b:netrw_method = 9
1174 let g:netrw_machine= substitute(a:choice,sftpurm,'\1',"")
1175 let b:netrw_fname = substitute(a:choice,sftpurm,'\2',"")
1176
1177 " Issue an rcp: hostname:filename" (this one should be last)
1178 elseif match(a:choice,rcphf) == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001179"" call Decho("(rcp) [user@]host:file) rcphf<".rcphf.">")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 let b:netrw_method = 1
1181 let userid = substitute(a:choice,rcphf,'\2',"")
1182 let g:netrw_machine= substitute(a:choice,rcphf,'\3',"")
1183 let b:netrw_fname = substitute(a:choice,rcphf,'\4',"")
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001184"" call Decho('\1<'.substitute(a:choice,rcphf,'\1',"").">")
1185"" call Decho('\2<'.substitute(a:choice,rcphf,'\2',"").">")
1186"" call Decho('\3<'.substitute(a:choice,rcphf,'\3',"").">")
1187"" call Decho('\4<'.substitute(a:choice,rcphf,'\4',"").">")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if userid != ""
1189 let g:netrw_uid= userid
1190 endif
1191 if has("win32")
1192 " don't let PCs try <.netrc>
1193 let b:netrw_method = 3
1194 endif
1195
1196 else
1197 echoerr "***error*** cannot determine method"
1198 let b:netrw_method = -1
1199 endif
1200
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001201"" call Decho("a:choice <".a:choice.">")
1202"" call Decho("b:netrw_method <".b:netrw_method.">")
1203"" call Decho("g:netrw_machine<".g:netrw_machine.">")
1204"" call Decho("g:netrw_port <".g:netrw_port.">")
1205"" if exists("g:netrw_uid") "Decho
1206"" call Decho("g:netrw_uid <".g:netrw_uid.">")
1207"" endif "Decho
1208"" if exists("g:netrw_passwd") "Decho
1209"" call Decho("g:netrw_passwd <".g:netrw_passwd.">")
1210"" endif "Decho
1211"" call Decho("b:netrw_fname <".b:netrw_fname.">")
1212"" call Dret("NetMethod")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213endfun
1214" end of NetMethod
1215
1216" ------------------------------------------------------------------------
1217" NetUserPass: set username and password for subsequent ftp transfer {{{1
1218" Usage: :call NetUserPass() -- will prompt for userid and password
1219" :call NetUserPass("uid") -- will prompt for password
1220" :call NetUserPass("uid","password") -- sets global userid and password
1221fun! NetUserPass(...)
1222
1223 " get/set userid
1224 if a:0 == 0
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001225"" call Dfunc("NetUserPass(a:0<".a:0.">)")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if !exists("g:netrw_uid") || g:netrw_uid == ""
1227 " via prompt
1228 let g:netrw_uid= input('Enter username: ')
1229 endif
1230 else " from command line
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001231"" call Dfunc("NetUserPass(a:1<".a:1.">) {")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 let g:netrw_uid= a:1
1233 endif
1234
1235 " get password
1236 if a:0 <= 1 " via prompt
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001237"" call Decho("a:0=".a:0." case <=1:")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 let g:netrw_passwd= inputsecret("Enter Password: ")
1239 else " from command line
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001240"" call Decho("a:0=".a:0." case >1: a:2<".a:2.">")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 let g:netrw_passwd=a:2
1242 endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001243"" call Dret("NetUserPass")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244endfun
1245" end NetUserPass
1246
1247" ------------------------------------------------------------------------
1248" NetOptionSave: save options and set to "standard" form {{{1
1249fun!s:NetOptionSave()
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001250"" call Dfunc("NetOptionSave()")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
1252 " Get Temporary Filename
1253 let s:aikeep = &ai
1254 let s:cinkeep = &cin
1255 let s:cinokeep = &cino
1256 let s:comkeep = &com
1257 let s:cpokeep = &cpo
1258 let s:dirkeep = getcwd()
1259 let s:gdkeep = &gd
1260 let s:twkeep = &tw
1261 set cino =
1262 set com =
1263 set cpo -=aA
1264 set nocin noai
1265 set tw =0
1266 if has("win32") && !has("win95")
1267 let s:swfkeep= &swf
1268 set noswf
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001269"" call Decho("setting s:swfkeep to <".&swf.">")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 endif
1271
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001272"" call Dret("NetOptionSave")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273endfun
1274
1275" ------------------------------------------------------------------------
1276" NetOptionRestore: restore options {{{1
1277fun! s:NetOptionRestore()
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001278"" call Dfunc("NetOptionRestore()")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
1280 let &ai = s:aikeep
1281 let &cin = s:cinkeep
1282 let &cino = s:cinokeep
1283 let &com = s:comkeep
1284 let &cpo = s:cpokeep
1285 exe "lcd ".s:dirkeep
1286 let &gd = s:gdkeep
1287 let &tw = s:twkeep
1288 if exists("s:swfkeep")
1289 let &swf= s:swfkeep
1290 unlet s:swfkeep
1291 endif
1292 unlet s:aikeep
1293 unlet s:cinkeep
1294 unlet s:cinokeep
1295 unlet s:comkeep
1296 unlet s:cpokeep
1297 unlet s:gdkeep
1298 unlet s:twkeep
1299 unlet s:dirkeep
1300
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001301"" call Dret("NetOptionRestore")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302endfun
1303
1304" ------------------------------------------------------------------------
1305" NetReadFixup: this sort of function is typically written by the user {{{1
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001306" to handle extra junk that their system's ftp dumps
1307" into the transfer. This function is provided as an
1308" example and as a fix for a Windows 95 problem: in my
1309" experience, win95's ftp always dumped four blank lines
1310" at the end of the transfer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311if has("win95") && g:netrw_win95ftp
1312 fun! NetReadFixup(method, line1, line2)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001313"" call Dfunc("NetReadFixup(method<".a:method."> line1=".a:line1." line2=".a:line2.")")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if method == 3 " ftp (no <.netrc>)
1315 let fourblanklines= line2 - 3
1316 silent fourblanklines.",".line2."g/^\s*/d"
1317 endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001318"" call Dret("NetReadFixup")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 endfun
1320endif
1321
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001322" ---------------------------------------------------------------------
1323" NetSort: Piet Delport's BISort2() function, modified to take a range
1324fun! <SID>NetSort() range
1325 let i = a:firstline + 1
1326 while i <= a:lastline
1327 " find insertion point via binary search
1328 let i_val = getline(i)
1329 let lo = a:firstline
1330 let hi = i
1331 while lo < hi
1332 let mid = (lo + hi) / 2
1333 let mid_val = getline(mid)
1334 if i_val < mid_val
1335 let hi = mid
1336 else
1337 let lo = mid + 1
1338 if i_val == mid_val | break | endif
1339 endif
1340 endwhile
1341 " do insert
1342 if lo < i
1343 exec i.'d_'
1344 call append(lo - 1, i_val)
1345 endif
1346 let i = i + 1
1347 endwhile
1348endfun
1349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350" ------------------------------------------------------------------------
1351" Restore {{{1
1352let &cpo= s:save_cpo
1353unlet s:save_cpo
1354" vim:ts=8 fdm=marker