1 " vimball.vim : construct a file containing both paths and files
2 " Author: Charles E. Campbell, Jr.
5 " GetLatestVimScripts: 1502 1 :AutoInstall: vimball.vim
6 " Copyright: (c) 2004-2007 by Charles E. Campbell, Jr.
7 " The VIM LICENSE applies to Vimball.vim, and Vimball.txt
8 " (see |copyright|) except use "Vimball" instead of "Vim".
9 " No warranty, express or implied.
10 " *** *** Use At-Your-Own-Risk! *** ***
12 " ---------------------------------------------------------------------
14 if &cp || exists("g:loaded_vimball") || v:version < 700
18 let g:loaded_vimball = "v24"
22 " =====================================================================
28 if exists("g:vimball_shq") && !exists("g:netrw_shq")
29 let g:netrw_shq= g:vimball_shq
31 if !exists("g:netrw_cygwin")
32 if has("win32") || has("win95") || has("win64") || has("win16")
33 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
42 if !exists("g:netrw_shq")
43 if exists("&shq") && &shq != ""
45 elseif has("win32") || has("win95") || has("win64") || has("win16")
54 " call Decho("g:netrw_shq<".g:netrw_shq.">")
56 if !exists("g:vimball_path_escape")
57 let g:vimball_path_escape= ' ;#%'
61 " =====================================================================
64 " ---------------------------------------------------------------------
65 " vimball#MkVimball: creates a vimball given a list of paths to files {{{2
67 " line1,line2: a range of lines containing paths to files to be included in the vimball
68 " writelevel : if true, force a write to filename.vba, even if it exists
69 " (usually accomplished with :MkVimball! ...
70 " filename : base name of file to be created (ie. filename.vba)
71 " Output: a filename.vba using vimball format:
78 fun! vimball#MkVimball(line1,line2,writelevel,...) range
79 " call Dfunc("MkVimball(line1=".a:line1." line2=".a:line2." writelevel=".a:writelevel." vimballname<".a:1.">) a:0=".a:0)
80 if a:1 =~ '.vim' || a:1 =~ '.txt'
81 let vbname= substitute(a:1,'\.\a\{3}$','.vba','')
86 let vbname= vbname.'.vba'
88 " call Decho("vbname<".vbname.">")
90 call vimball#ShowMesg(s:ERROR,"(MkVimball) vimball name<".a:1."> should not include slashes")
91 " call Dret("MkVimball : vimball name<".a:1."> should not include slashes")
94 if !a:writelevel && filereadable(vbname)
95 call vimball#ShowMesg(s:ERROR,"(MkVimball) file<".vbname."> exists; use ! to insist")
96 " call Dret("MkVimball : file<".vbname."> already exists; use ! to insist")
101 call s:SaveSettings()
104 " allow user to specify where to get the files
105 let home= expand(a:2)
107 " use first existing directory from rtp
108 let home= s:VimballHome()
111 " save current directory
112 let curdir = getcwd()
115 " record current tab, initialize while loop index
116 let curtabnr = tabpagenr()
118 " call Decho("curtabnr=".curtabnr)
120 while linenr <= a:line2
121 let svfile = getline(linenr)
122 " call Decho("svfile<".svfile.">")
124 if !filereadable(svfile)
125 call vimball#ShowMesg(s:ERROR,"unable to read file<".svfile.">")
126 call s:ChgDir(curdir)
127 call s:RestoreSettings()
128 " call Dret("MkVimball")
132 " create/switch to mkvimball tab
133 if !exists("vbtabnr")
136 let vbtabnr= tabpagenr()
141 let lastline= line("$") + 1
142 if lastline == 2 && getline("$") == ""
143 call setline(1,'" Vimball Archiver by Charles E. Campbell, Jr., Ph.D.')
144 call setline(2,'UseVimball')
145 call setline(3,'finish')
146 let lastline= line("$") + 1
148 call setline(lastline ,substitute(svfile,'$',' [[[1',''))
149 call setline(lastline+1,0)
151 " write the file from the tab
152 let svfilepath= s:Path(svfile,'')
153 " call Decho("exe $r ".svfilepath)
156 call setline(lastline+1,line("$") - lastline - 1)
157 " call Decho("lastline=".lastline." line$=".line("$"))
159 " restore to normal tab
161 let linenr= linenr + 1
166 call s:ChgDir(curdir)
169 let vbnamepath= s:Path(vbname,'')
170 " call Decho("exe w! ".vbnamepath)
173 let vbnamepath= s:Path(vbname,'')
174 " call Decho("exe w ".vbnamepath)
177 " call Decho("Vimball<".vbname."> created")
178 echo "Vimball<".vbname."> created"
180 " remove the evidence
181 setlocal nomod bh=wipe
186 call s:RestoreSettings()
188 " call Dret("MkVimball")
191 " ---------------------------------------------------------------------
192 " vimball#Vimball: extract and distribute contents from a vimball {{{2
193 " (invoked the the UseVimball command embedded in
194 " vimballs' prologue)
195 fun! vimball#Vimball(really,...)
196 " call Dfunc("vimball#Vimball(really=".a:really.") a:0=".a:0)
198 if getline(1) !~ '^" Vimball Archiver by Charles E. Campbell, Jr., Ph.D.$'
199 echoerr "(Vimball) The current file does not appear to be a Vimball!"
200 " call Dret("vimball#Vimball")
204 " set up standard settings
205 call s:SaveSettings()
206 let curtabnr = tabpagenr()
209 " call Decho("setting up vimball tab")
212 let vbtabnr= tabpagenr()
215 " go to vim plugin home
217 let home= expand(a:1)
219 let home= s:VimballHome()
221 " call Decho("home<".home.">")
223 " save current directory and remove older same-named vimball, if any
224 let curdir = getcwd()
225 " call Decho("home<".home.">")
226 " call Decho("curdir<".curdir.">")
229 call vimball#RmVimball()
234 " give title to listing of (extracted) files from Vimball Archive
236 echohl Title | echomsg "Vimball Archive" | echohl None
238 echohl Title | echomsg "Vimball Archive Listing" | echohl None
239 echohl Statement | echomsg "files would be placed under: ".home | echohl None
242 " apportion vimball contents to various files
243 " call Decho("exe tabn ".curtabnr)
245 " call Decho("linenr=".linenr." line$=".line("$"))
246 while 1 < linenr && linenr < line("$")
247 let fname = substitute(getline(linenr),'\t\[\[\[1$','','')
248 let fname = substitute(fname,'\\','/','g')
249 let fsize = getline(linenr+1)
250 let filecnt = filecnt + 1
251 " call Decho("fname<".fname."> fsize=".fsize." filecnt=".filecnt)
254 echomsg "extracted <".fname.">: ".fsize." lines"
256 echomsg "would extract <".fname.">: ".fsize." lines"
258 " call Decho("using L#".linenr.": will extract file<".fname.">")
259 " call Decho("using L#".(linenr+1).": fsize=".fsize)
261 " Allow AsNeeded/ directory to take place of plugin/ directory
262 " when AsNeeded/filename is filereadable
263 if fname =~ '\<plugin/'
264 let anfname= substitute(fname,'\<plugin/','AsNeeded/','')
265 if filereadable(anfname)
266 " call Decho("using anfname<".anfname."> instead of <".fname.">")
271 " make directories if they don't exist yet
273 " call Decho("making directories if they don't exist yet (fname<".fname.">)")
274 let fnamebuf= substitute(fname,'\\','/','g')
275 let dirpath = substitute(home,'\\','/','g')
276 while fnamebuf =~ '/'
277 let dirname = dirpath."/".substitute(fnamebuf,'/.*$','','')
278 let dirpath = dirname
279 let fnamebuf = substitute(fnamebuf,'^.\{-}/\(.*\)$','\1','')
280 " call Decho("dirname<".dirname.">")
281 if !isdirectory(dirname)
282 " call Decho("making <".dirname.">")
284 call s:RecordInVar(home,"rmdir('".dirname."')")
290 " grab specified qty of lines and place into "a" buffer
291 " (skip over path/filename and qty-lines)
292 let linenr = linenr + 2
293 let lastline = linenr + fsize - 1
294 " call Decho("exe ".linenr.",".lastline."yank a")
295 exe "silent ".linenr.",".lastline."yank a"
297 " copy "a" buffer into tab
298 " call Decho('copy "a buffer into tab#'.vbtabnr)
308 let fnamepath= s:Path(home."/".fname,'')
309 " call Decho("exe w! ".fnamepath)
310 exe "silent w! ".fnamepath
311 echo "wrote ".fnamepath
312 call s:RecordInVar(home,"call delete('".fnamepath."')")
315 " return to tab with vimball
316 " call Decho("exe tabn ".curtabnr)
319 " set up help if its a doc/*.txt file
320 " call Decho("didhelp<".didhelp."> fname<".fname.">")
321 if a:really && didhelp == "" && fname =~ 'doc/[^/]\+\.txt$'
322 let didhelp= substitute(fname,'^\(.*\<doc\)[/\\][^.]*\.txt$','\1','')
323 " call Decho("didhelp<".didhelp.">")
326 " update for next file
327 " let oldlinenr = linenr " Decho
328 let linenr = linenr + fsize
329 " call Decho("update linenr= [linenr=".oldlinenr."] + [fsize=".fsize."] = ".linenr)
333 " call Decho("about to set up help: didhelp<".didhelp.">")
335 let htpath= s:Path(home."/".didhelp,"")
336 "let htpath= escape(substitute(s:Path(home."/".didhelp,'"'),'"','','g'),' ')
337 " call Decho("exe helptags ".htpath)
338 exe "helptags ".htpath
342 " make sure a "Press ENTER..." prompt appears to keep the messages showing!
345 let filecnt= filecnt + 1
348 " record actions in <.VimballRecord>
349 call s:RecordInFile(home)
351 " restore events, delete tab and buffer
353 setlocal nomod bh=wipe
356 call s:RestoreSettings()
357 call s:ChgDir(curdir)
359 " call Dret("vimball#Vimball")
362 " ---------------------------------------------------------------------
363 " vimball#RmVimball: remove any files, remove any directories made by any {{{2
364 " previous vimball extraction based on a file of the current
366 " Usage: RmVimball (assume current file is a vimball; remove)
367 " RmVimball vimballname
368 fun! vimball#RmVimball(...)
369 " call Dfunc("vimball#RmVimball() a:0=".a:0)
370 if exists("g:vimball_norecord")
371 " call Dret("vimball#RmVimball : (g:vimball_norecord)")
376 " call Decho("turned off all events")
379 let curfile= '^'.expand("%:tr")
382 call vimball#ShowMesg(s:USAGE,"RmVimball vimballname [path]")
383 " call Dret("vimball#RmVimball : suspect a:1<".a:1.">")
388 if curfile !~ '.vba$'
389 let curfile= curfile.".vba: "
391 let curfile= curfile.": "
394 let home= expand(a:2)
396 let home= s:VimballHome()
398 let curdir = getcwd()
399 " call Decho("home <".home.">")
400 " call Decho("curfile<".curfile.">")
401 " call Decho("curdir <".curdir.">")
404 if filereadable(".VimballRecord")
405 " call Decho(".VimballRecord is readable")
406 " call Decho("curfile<".curfile.">")
407 keepalt keepjumps 1split
408 silent! keepalt keepjumps e .VimballRecord
410 if search(curfile,'cw')
411 let exestring= substitute(getline("."),curfile,'','')
412 " call Decho("exe ".exestring)
413 silent! keepalt keepjumps exe exestring
414 silent! keepalt keepjumps d
416 " call Decho("unable to find <".curfile."> in .VimballRecord")
418 silent! keepalt keepjumps g/^\s*$/d
419 silent! keepalt keepjumps wq!
422 call s:ChgDir(curdir)
425 " call Decho("restoring events")
428 " call Dret("vimball#RmVimball")
431 " ---------------------------------------------------------------------
432 " vimball#Decompress: attempts to automatically decompress vimballs {{{2
433 fun! vimball#Decompress(fname)
434 " call Dfunc("Decompress(fname<".a:fname.">)")
437 if expand("%") =~ '.*\.gz' && executable("gunzip")
438 silent exe "!gunzip ".s:Escape(a:fname)
439 if v:shell_error != 0
440 call vimball#ShowMesg(s:WARNING,"(vimball#Decompress) gunzip may have failed with <".a:fname.">")
442 let fname= substitute(a:fname,'\.gz$','','')
443 exe "e ".escape(fname,' \')
444 call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)")
445 elseif expand("%") =~ '.*\.bz2' && executable("bunzip2")
446 silent exe "!bunzip2 ".s:Escape(a:fname)
447 if v:shell_error != 0
448 call vimball#ShowMesg(s:WARNING,"(vimball#Decompress) bunzip2 may have failed with <".a:fname.">")
450 let fname= substitute(a:fname,'\.bz2$','','')
451 exe "e ".escape(fname,' \')
452 call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)")
453 elseif expand("%") =~ '.*\.zip' && executable("unzip")
454 silent exe "!unzip ".s:Escape(a:fname)
455 if v:shell_error != 0
456 call vimball#ShowMesg(s:WARNING,"(vimball#Decompress) unzip may have failed with <".a:fname.">")
458 let fname= substitute(a:fname,'\.zip$','','')
459 exe "e ".escape(fname,' \')
460 call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)")
462 set noma bt=nofile fmr=[[[,]]] fdm=marker
464 " call Dret("Decompress")
467 " ---------------------------------------------------------------------
468 " vimball#ShowMesg: {{{2
469 fun! vimball#ShowMesg(level,msg)
470 " call Dfunc("vimball#ShowMesg(level=".a:level." msg<".a:msg.">)")
471 let rulerkeep = &ruler
472 let showcmdkeep = &showcmd
473 set noruler noshowcmd
477 echomsg "***vimball*** " a:msg
479 if a:level == s:WARNING || a:level == s:USAGE
481 elseif a:level == s:ERROR
484 echomsg "***vimball*** " a:msg
488 if a:level != s:USAGE
489 call inputsave()|let ok= input("Press <cr> to continue")|call inputrestore()
492 let &ruler = rulerkeep
493 let &showcmd = showcmdkeep
495 " call Dret("vimball#ShowMesg")
498 " ---------------------------------------------------------------------
501 " =====================================================================
502 " s:ChgDir: change directory (in spite of Windoze) {{{2
503 fun! s:ChgDir(newdir)
504 " call Dfunc("ChgDir(newdir<".a:newdir.">)")
505 if (has("win32") || has("win95") || has("win64") || has("win16"))
506 exe 'silent cd '.escape(substitute(a:newdir,'/','\\','g'),' ')
508 exe 'silent cd '.escape(a:newdir,' ')
510 " call Dret("ChgDir : curdir<".getcwd().">")
513 " ---------------------------------------------------------------------
514 " s:Path: prepend and append quotes and do escaping {{{2
515 fun! s:Path(cmd,quote)
516 " call Dfunc("Path(cmd<".a:cmd."> quote<".a:quote.">) vimball_path_escape<".g:vimball_path_escape.">")
517 if (has("win32") || has("win95") || has("win64") || has("win16"))
518 " let cmdpath= a:quote.substitute(a:cmd,'/','\\','g').a:quote
519 let cmdpath= a:quote.substitute(a:cmd,'\\','/','g').a:quote
520 " call Decho("cmdpath<".cmdpath."> (win32 mod)")
522 let cmdpath= a:quote.a:cmd.a:quote
523 " call Decho("cmdpath<".cmdpath."> (not-win32 mod)")
525 if a:quote == "" && g:vimball_path_escape !~ ' '
526 let cmdpath= escape(cmdpath,' ')
527 " call Decho("cmdpath<".cmdpath."> (empty quote case)")
529 let cmdpath= escape(cmdpath,g:vimball_path_escape)
530 " call Dret("Path <".cmdpath.">")
534 " ---------------------------------------------------------------------
535 " s:RecordInVar: record a un-vimball command in the .VimballRecord file {{{2
536 fun! s:RecordInVar(home,cmd)
537 " call Dfunc("RecordInVar(home<".a:home."> cmd<".a:cmd.">)")
539 " if !exists("s:recorddir")
540 " let s:recorddir= substitute(a:cmd,'^rmdir',"call s:Rmdir",'')
542 " let s:recorddir= s:recorddir."|".substitute(a:cmd,'^rmdir',"call s:Rmdir",'')
544 elseif !exists("s:recordfile")
545 let s:recordfile= a:cmd
547 let s:recordfile= s:recordfile."|".a:cmd
549 " call Dret("RecordInVar : s:recordfile<".(exists("s:recordfile")? s:recordfile : "")."> s:recorddir<".(exists("s:recorddir")? s:recorddir : "").">")
552 " ---------------------------------------------------------------------
553 " s:RecordInFile: {{{2
554 fun! s:RecordInFile(home)
555 " call Dfunc("s:RecordInFile()")
556 if exists("g:vimball_norecord")
557 " call Dret("s:RecordInFile : g:vimball_norecord")
561 if exists("s:recordfile") || exists("s:recorddir")
563 call s:ChgDir(a:home)
564 keepalt keepjumps 1split
566 let cmd= expand("%:tr").": "
567 " call Decho("cmd<".cmd.">")
569 silent! keepalt keepjumps e .VimballRecord
572 if exists("s:recordfile") && exists("s:recorddir")
573 let cmd= cmd.s:recordfile."|".s:recorddir
574 elseif exists("s:recorddir")
575 let cmd= cmd.s:recorddir
576 elseif exists("s:recordfile")
577 let cmd= cmd.s:recordfile
579 " call Dret("s:RecordInFile : neither recordfile nor recorddir exist")
582 " call Decho("cmd<".cmd.">")
584 " put command into buffer, write .VimballRecord `file
585 keepalt keepjumps put=cmd
586 silent! keepalt keepjumps g/^\s*$/d
587 silent! keepalt keepjumps wq!
588 call s:ChgDir(curdir)
590 if exists("s:recorddir")
591 " call Decho("unlet s:recorddir<".s:recorddir.">")
594 if exists("s:recordfile")
595 " call Decho("unlet s:recordfile<".s:recordfile.">")
599 " call Decho("s:record[file|dir] doesn't exist")
602 " call Dret("s:RecordInFile")
605 " ---------------------------------------------------------------------
606 " s:VimballHome: determine/get home directory path (usually from rtp) {{{2
608 " call Dfunc("VimballHome()")
609 if exists("g:vimball_home")
610 let home= g:vimball_home
612 " go to vim plugin home
613 for home in split(&rtp,',') + ['']
614 if isdirectory(home) && filewritable(home) | break | endif
617 " just pick the first directory
618 let home= substitute(&rtp,',.*$','','')
620 if (has("win32") || has("win95") || has("win64") || has("win16"))
621 let home= substitute(home,'/','\\','g')
624 " call Dret("VimballHome <".home.">")
628 " ---------------------------------------------------------------------
629 " s:SaveSettings: {{{2
630 fun! s:SaveSettings()
631 " call Dfunc("SaveSettings()")
632 let s:makeep = getpos("'a")
639 let s:hidkeep = &hidden
643 let s:repkeep = &report
647 setlocal ei=all ve=all noacd nofen noic report=999 nohid bt= ma lz pm= ff=unix
649 setlocal ei=all ve=all nofen noic report=999 nohid bt= ma lz pm= ff=unix
651 " vimballs should be in unix format
653 " call Dret("SaveSettings")
656 " ---------------------------------------------------------------------
657 " s:RestoreSettings: {{{2
658 fun! s:RestoreSettings()
659 " call Dfunc("RestoreSettings()")
665 let &hidden = s:hidkeep
669 let &report = s:repkeep
675 " call Decho("restore mark-a: makeep=".string(makeep))
676 call setpos("'a",s:makeep)
681 unlet s:regakeep s:eikeep s:fenkeep s:hidkeep s:ickeep s:repkeep s:vekeep s:makeep s:lzkeep s:pmkeep s:ffkeep
682 " call Dret("RestoreSettings")
685 " ---------------------------------------------------------------------
688 " shellescape() was added by patch 7.0.111
689 if exists("*shellescape")
690 return shellescape(a:name)
692 return g:netrw_shq . a:name . g:netrw_shq
695 " ---------------------------------------------------------------------