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-2009 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")
17 let g:loaded_vimball = "v30"
20 echo "***warning*** this version of vimball needs vim 7.2"
28 " =====================================================================
35 " determine if cygwin is in use or not
36 if !exists("g:netrw_cygwin")
37 if has("win32") || has("win95") || has("win64") || has("win16")
38 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
48 " set up g:vimball_mkdir if the mkdir() call isn't defined
50 if exists("g:netrw_local_mkdir")
51 let g:vimball_mkdir= g:netrw_local_mkdir
52 elseif executable("mkdir")
53 let g:vimball_mkdir= "mkdir"
54 elseif executable("makedir")
55 let g:vimball_mkdir= "makedir"
57 if !exists(g:vimball_mkdir)
58 call vimball#ShowMesg(s:WARNING,"(vimball) g:vimball_mkdir undefined")
63 " =====================================================================
66 " ---------------------------------------------------------------------
67 " vimball#MkVimball: creates a vimball given a list of paths to files {{{2
69 " line1,line2: a range of lines containing paths to files to be included in the vimball
70 " writelevel : if true, force a write to filename.vba, even if it exists
71 " (usually accomplished with :MkVimball! ...
72 " filename : base name of file to be created (ie. filename.vba)
73 " Output: a filename.vba using vimball format:
80 fun! vimball#MkVimball(line1,line2,writelevel,...) range
81 " call Dfunc("MkVimball(line1=".a:line1." line2=".a:line2." writelevel=".a:writelevel." vimballname<".a:1.">) a:0=".a:0)
82 if a:1 =~ '\.vim$' || a:1 =~ '\.txt$'
83 let vbname= substitute(a:1,'\.\a\{3}$','.vba','')
88 let vbname= vbname.'.vba'
90 " call Decho("vbname<".vbname.">")
91 if !a:writelevel && a:1 =~ '[\/]'
92 call vimball#ShowMesg(s:ERROR,"(MkVimball) vimball name<".a:1."> should not include slashes; use ! to insist")
93 " call Dret("MkVimball : vimball name<".a:1."> should not include slashes")
96 if !a:writelevel && filereadable(vbname)
97 call vimball#ShowMesg(s:ERROR,"(MkVimball) file<".vbname."> exists; use ! to insist")
98 " call Dret("MkVimball : file<".vbname."> already exists; use ! to insist")
103 call vimball#SaveSettings()
106 " allow user to specify where to get the files
107 let home= expand(a:2)
109 " use first existing directory from rtp
110 let home= vimball#VimballHome()
113 " save current directory
114 let curdir = getcwd()
117 " record current tab, initialize while loop index
118 let curtabnr = tabpagenr()
120 " call Decho("curtabnr=".curtabnr)
122 while linenr <= a:line2
123 let svfile = getline(linenr)
124 " call Decho("svfile<".svfile.">")
126 if !filereadable(svfile)
127 call vimball#ShowMesg(s:ERROR,"unable to read file<".svfile.">")
128 call s:ChgDir(curdir)
129 call vimball#RestoreSettings()
130 " call Dret("MkVimball")
134 " create/switch to mkvimball tab
135 if !exists("vbtabnr")
138 let vbtabnr= tabpagenr()
143 let lastline= line("$") + 1
144 if lastline == 2 && getline("$") == ""
145 call setline(1,'" Vimball Archiver by Charles E. Campbell, Jr., Ph.D.')
146 call setline(2,'UseVimball')
147 call setline(3,'finish')
148 let lastline= line("$") + 1
150 call setline(lastline ,substitute(svfile,'$',' [[[1',''))
151 call setline(lastline+1,0)
153 " write the file from the tab
154 " call Decho("exe $r ".fnameescape(svfile))
155 exe "$r ".fnameescape(svfile)
157 call setline(lastline+1,line("$") - lastline - 1)
158 " call Decho("lastline=".lastline." line$=".line("$"))
160 " restore to normal tab
162 let linenr= linenr + 1
167 call s:ChgDir(curdir)
170 " call Decho("exe w! ".fnameescape(vbname))
171 exe "w! ".fnameescape(vbname)
173 " call Decho("exe w ".fnameescape(vbname))
174 exe "w ".fnameescape(vbname)
176 " call Decho("Vimball<".vbname."> created")
177 echo "Vimball<".vbname."> created"
179 " remove the evidence
180 setlocal nomod bh=wipe
185 call vimball#RestoreSettings()
187 " call Dret("MkVimball")
190 " ---------------------------------------------------------------------
191 " vimball#Vimball: extract and distribute contents from a vimball {{{2
192 " (invoked the the UseVimball command embedded in
193 " vimballs' prologue)
194 fun! vimball#Vimball(really,...)
195 " call Dfunc("vimball#Vimball(really=".a:really.") a:0=".a:0)
197 if v:version < 701 || (v:version == 701 && !exists('*fnameescape'))
198 echoerr "your vim is missing the fnameescape() function"
199 " call Dret("vimball#Vimball : needs 7.1 with patch 299")
203 if getline(1) !~ '^" Vimball Archiver'
204 echoerr "(Vimball) The current file does not appear to be a Vimball!"
205 " call Dret("vimball#Vimball")
209 " set up standard settings
210 call vimball#SaveSettings()
211 let curtabnr = tabpagenr()
212 let vimballfile = expand("%:tr")
215 " call Decho("setting up vimball tab")
218 let vbtabnr= tabpagenr()
221 " go to vim plugin home
223 let home= expand(a:1)
225 let home= vimball#VimballHome()
227 " call Decho("home<".home.">")
229 " save current directory and remove older same-named vimball, if any
230 let curdir = getcwd()
231 " call Decho("home<".home.">")
232 " call Decho("curdir<".curdir.">")
235 let s:ok_unablefind= 1
236 call vimball#RmVimball(vimballfile)
237 unlet s:ok_unablefind
242 " give title to listing of (extracted) files from Vimball Archive
244 echohl Title | echomsg "Vimball Archive" | echohl None
246 echohl Title | echomsg "Vimball Archive Listing" | echohl None
247 echohl Statement | echomsg "files would be placed under: ".home | echohl None
250 " apportion vimball contents to various files
251 " call Decho("exe tabn ".curtabnr)
253 " call Decho("linenr=".linenr." line$=".line("$"))
254 while 1 < linenr && linenr < line("$")
255 let fname = substitute(getline(linenr),'\t\[\[\[1$','','')
256 let fname = substitute(fname,'\\','/','g')
257 " let fsize = getline(linenr+1)+0
258 let fsize = substitute(getline(linenr+1),'^\(\d\+\).\{-}$','\1','')+0
259 let fenc = substitute(getline(linenr+1),'^\d\+\s*\(\S\+\)$','\1','')
260 let filecnt = filecnt + 1
261 " call Decho("fname<".fname."> fsize=".fsize." filecnt=".filecnt)
264 echomsg "extracted <".fname.">: ".fsize." lines"
266 echomsg "would extract <".fname.">: ".fsize." lines"
268 " call Decho("using L#".linenr.": will extract file<".fname.">")
269 " call Decho("using L#".(linenr+1).": fsize=".fsize)
271 " Allow AsNeeded/ directory to take place of plugin/ directory
272 " when AsNeeded/filename is filereadable or was present in VimballRecord
273 if fname =~ '\<plugin/'
274 let anfname= substitute(fname,'\<plugin/','AsNeeded/','')
275 if filereadable(anfname) || (exists("s:VBRstring") && s:VBRstring =~ anfname)
276 " call Decho("using anfname<".anfname."> instead of <".fname.">")
281 " make directories if they don't exist yet
283 " call Decho("making directories if they don't exist yet (fname<".fname.">)")
284 let fnamebuf= substitute(fname,'\\','/','g')
285 let dirpath = substitute(home,'\\','/','g')
286 while fnamebuf =~ '/'
287 let dirname = dirpath."/".substitute(fnamebuf,'/.*$','','')
288 let dirpath = dirname
289 let fnamebuf = substitute(fnamebuf,'^.\{-}/\(.*\)$','\1','')
290 " call Decho("dirname<".dirname.">")
291 if !isdirectory(dirname)
292 " call Decho("making <".dirname.">")
293 if exists("g:vimball_mkdir")
294 call system(g:vimball_mkdir." ".shellescape(dirname))
298 call s:RecordInVar(home,"rmdir('".dirname."')")
304 " grab specified qty of lines and place into "a" buffer
305 " (skip over path/filename and qty-lines)
306 let linenr = linenr + 2
307 let lastline = linenr + fsize - 1
308 " call Decho("exe ".linenr.",".lastline."yank a")
309 exe "silent ".linenr.",".lastline."yank a"
311 " copy "a" buffer into tab
312 " call Decho('copy "a buffer into tab#'.vbtabnr)
322 let fnamepath= home."/".fname
323 " call Decho("exe w! ".fnameescape(fnamepath))
325 exe "silent w! ++enc=".fnameescape(fenc)." ".fnameescape(fnamepath)
327 exe "silent w! ".fnameescape(fnamepath)
329 echo "wrote ".fnameescape(fnamepath)
330 call s:RecordInVar(home,"call delete('".fnamepath."')")
333 " return to tab with vimball
334 " call Decho("exe tabn ".curtabnr)
337 " set up help if its a doc/*.txt file
338 " call Decho("didhelp<".didhelp."> fname<".fname.">")
339 if a:really && didhelp == "" && fname =~ 'doc/[^/]\+\.\(txt\|..x\)$'
340 let didhelp= substitute(fname,'^\(.*\<doc\)[/\\][^.]*\.\(txt\|..x\)$','\1','')
341 " call Decho("didhelp<".didhelp.">")
344 " update for next file
345 " call Decho("update linenr= [linenr=".linenr."] + [fsize=".fsize."] = ".(linenr+fsize))
346 let linenr= linenr + fsize
350 " call Decho("about to set up help: didhelp<".didhelp.">")
352 let htpath= home."/".didhelp
353 " call Decho("exe helptags ".htpath)
354 exe "helptags ".fnameescape(htpath)
358 " make sure a "Press ENTER..." prompt appears to keep the messages showing!
361 let filecnt= filecnt + 1
364 " record actions in <.VimballRecord>
365 call s:RecordInFile(home)
367 " restore events, delete tab and buffer
369 setlocal nomod bh=wipe
372 call vimball#RestoreSettings()
373 call s:ChgDir(curdir)
375 " call Dret("vimball#Vimball")
378 " ---------------------------------------------------------------------
379 " vimball#RmVimball: remove any files, remove any directories made by any {{{2
380 " previous vimball extraction based on a file of the current
382 " Usage: RmVimball (assume current file is a vimball; remove)
383 " RmVimball vimballname
384 fun! vimball#RmVimball(...)
385 " call Dfunc("vimball#RmVimball() a:0=".a:0)
386 if exists("g:vimball_norecord")
387 " call Dret("vimball#RmVimball : (g:vimball_norecord)")
392 let curfile= expand("%:tr")
393 " call Decho("case a:0=0: curfile<".curfile."> (used expand(%:tr))")
396 call vimball#ShowMesg(s:USAGE,"RmVimball vimballname [path]")
397 " call Dret("vimball#RmVimball : suspect a:1<".a:1.">")
401 " call Decho("case a:0=".a:0.": curfile<".curfile.">")
403 if curfile =~ '\.vba$'
404 let curfile= substitute(curfile,'\.vba','','')
407 let home= expand(a:2)
409 let home= vimball#VimballHome()
411 let curdir = getcwd()
412 " call Decho("home <".home.">")
413 " call Decho("curfile<".curfile.">")
414 " call Decho("curdir <".curdir.">")
417 if filereadable(".VimballRecord")
418 " call Decho(".VimballRecord is readable")
419 " call Decho("curfile<".curfile.">")
420 keepalt keepjumps 1split
421 silent! keepalt keepjumps e .VimballRecord
423 " call Decho('search for ^\M'.curfile.'.\m: ')
424 " call Decho('search for ^\M'.curfile.'.\mvba: ')
425 " call Decho('search for ^\M'.curfile.'\m[-0-9.]*\.vba: ')
426 if search('^\M'.curfile."\m: ".'cw')
428 elseif search('^\M'.curfile.".\mvba: ",'cw')
430 elseif search('^\M'.curfile.'\m[-0-9.]*\.vba: ','cw')
436 let exestring = substitute(getline("."),'^\M'.curfile.'\m\S\{-}\.vba: ','','')
437 let s:VBRstring= substitute(exestring,'call delete(','','g')
438 let s:VBRstring= substitute(s:VBRstring,"[')]",'','g')
439 " call Decho("exe ".exestring)
440 silent! keepalt keepjumps exe exestring
441 silent! keepalt keepjumps d
442 let exestring= strlen(substitute(exestring,'call delete(.\{-})|\=',"D","g"))
443 " call Decho("exestring<".exestring.">")
444 echomsg "removed ".exestring." files"
447 let curfile = substitute(curfile,'\.vba','','')
448 " call Decho("unable to find <".curfile."> in .VimballRecord")
449 if !exists("s:ok_unablefind")
450 call vimball#ShowMesg(s:WARNING,"(RmVimball) unable to find <".curfile."> in .VimballRecord")
453 silent! keepalt keepjumps g/^\s*$/d
454 silent! keepalt keepjumps wq!
457 call s:ChgDir(curdir)
459 " call Dret("vimball#RmVimball")
462 " ---------------------------------------------------------------------
463 " vimball#Decompress: attempts to automatically decompress vimballs {{{2
464 fun! vimball#Decompress(fname,...)
465 " call Dfunc("Decompress(fname<".a:fname.">) a:0=".a:0)
468 if expand("%") =~ '.*\.gz' && executable("gunzip")
469 " handle *.gz with gunzip
470 silent exe "!gunzip ".shellescape(a:fname)
471 if v:shell_error != 0
472 call vimball#ShowMesg(s:WARNING,"(vimball#Decompress) gunzip may have failed with <".a:fname.">")
474 let fname= substitute(a:fname,'\.gz$','','')
475 exe "e ".escape(fname,' \')
476 if a:0 == 0| call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)") | endif
478 elseif expand("%") =~ '.*\.gz' && executable("gzip")
479 " handle *.gz with gzip -d
480 silent exe "!gzip -d ".shellescape(a:fname)
481 if v:shell_error != 0
482 call vimball#ShowMesg(s:WARNING,'(vimball#Decompress) "gzip -d" may have failed with <'.a:fname.">")
484 let fname= substitute(a:fname,'\.gz$','','')
485 exe "e ".escape(fname,' \')
486 if a:0 == 0| call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)") | endif
488 elseif expand("%") =~ '.*\.bz2' && executable("bunzip2")
489 " handle *.bz2 with bunzip2
490 silent exe "!bunzip2 ".shellescape(a:fname)
491 if v:shell_error != 0
492 call vimball#ShowMesg(s:WARNING,"(vimball#Decompress) bunzip2 may have failed with <".a:fname.">")
494 let fname= substitute(a:fname,'\.bz2$','','')
495 exe "e ".escape(fname,' \')
496 if a:0 == 0| call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)") | endif
498 elseif expand("%") =~ '.*\.bz2' && executable("bzip2")
499 " handle *.bz2 with bzip2 -d
500 silent exe "!bzip2 -d ".shellescape(a:fname)
501 if v:shell_error != 0
502 call vimball#ShowMesg(s:WARNING,'(vimball#Decompress) "bzip2 -d" may have failed with <'.a:fname.">")
504 let fname= substitute(a:fname,'\.bz2$','','')
505 exe "e ".escape(fname,' \')
506 if a:0 == 0| call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)") | endif
508 elseif expand("%") =~ '.*\.zip' && executable("unzip")
509 " handle *.zip with unzip
510 silent exe "!unzip ".shellescape(a:fname)
511 if v:shell_error != 0
512 call vimball#ShowMesg(s:WARNING,"(vimball#Decompress) unzip may have failed with <".a:fname.">")
514 let fname= substitute(a:fname,'\.zip$','','')
515 exe "e ".escape(fname,' \')
516 if a:0 == 0| call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)") | endif
519 if a:0 == 0| setlocal noma bt=nofile fmr=[[[,]]] fdm=marker | endif
521 " call Dret("Decompress")
524 " ---------------------------------------------------------------------
525 " vimball#ShowMesg: {{{2
526 fun! vimball#ShowMesg(level,msg)
527 " call Dfunc("vimball#ShowMesg(level=".a:level." msg<".a:msg.">)")
529 let rulerkeep = &ruler
530 let showcmdkeep = &showcmd
531 set noruler noshowcmd
535 echomsg "***vimball*** ".a:msg
537 if a:level == s:WARNING || a:level == s:USAGE
539 elseif a:level == s:ERROR
542 echomsg "***vimball*** ".a:msg
546 if a:level != s:USAGE
547 call inputsave()|let ok= input("Press <cr> to continue")|call inputrestore()
550 let &ruler = rulerkeep
551 let &showcmd = showcmdkeep
553 " call Dret("vimball#ShowMesg")
555 " =====================================================================
556 " s:ChgDir: change directory (in spite of Windoze) {{{2
557 fun! s:ChgDir(newdir)
558 " call Dfunc("ChgDir(newdir<".a:newdir.">)")
559 if (has("win32") || has("win95") || has("win64") || has("win16"))
560 exe 'silent cd '.fnameescape(substitute(a:newdir,'/','\\','g'))
562 exe 'silent cd '.fnameescape(a:newdir)
564 " call Dret("ChgDir : curdir<".getcwd().">")
567 " ---------------------------------------------------------------------
568 " s:RecordInVar: record a un-vimball command in the .VimballRecord file {{{2
569 fun! s:RecordInVar(home,cmd)
570 " call Dfunc("RecordInVar(home<".a:home."> cmd<".a:cmd.">)")
572 " if !exists("s:recorddir")
573 " let s:recorddir= substitute(a:cmd,'^rmdir',"call s:Rmdir",'')
575 " let s:recorddir= s:recorddir."|".substitute(a:cmd,'^rmdir',"call s:Rmdir",'')
577 elseif !exists("s:recordfile")
578 let s:recordfile= a:cmd
580 let s:recordfile= s:recordfile."|".a:cmd
582 " call Dret("RecordInVar : s:recordfile<".(exists("s:recordfile")? s:recordfile : "")."> s:recorddir<".(exists("s:recorddir")? s:recorddir : "").">")
585 " ---------------------------------------------------------------------
586 " s:RecordInFile: {{{2
587 fun! s:RecordInFile(home)
588 " call Dfunc("s:RecordInFile()")
589 if exists("g:vimball_norecord")
590 " call Dret("s:RecordInFile : g:vimball_norecord")
594 if exists("s:recordfile") || exists("s:recorddir")
596 call s:ChgDir(a:home)
597 keepalt keepjumps 1split
599 let cmd= expand("%:tr").": "
600 " call Decho("cmd<".cmd.">")
602 silent! keepalt keepjumps e .VimballRecord
605 if exists("s:recordfile") && exists("s:recorddir")
606 let cmd= cmd.s:recordfile."|".s:recorddir
607 elseif exists("s:recorddir")
608 let cmd= cmd.s:recorddir
609 elseif exists("s:recordfile")
610 let cmd= cmd.s:recordfile
612 " call Dret("s:RecordInFile : neither recordfile nor recorddir exist")
615 " call Decho("cmd<".cmd.">")
617 " put command into buffer, write .VimballRecord `file
618 keepalt keepjumps put=cmd
619 silent! keepalt keepjumps g/^\s*$/d
620 silent! keepalt keepjumps wq!
621 call s:ChgDir(curdir)
623 if exists("s:recorddir")
624 " call Decho("unlet s:recorddir<".s:recorddir.">")
627 if exists("s:recordfile")
628 " call Decho("unlet s:recordfile<".s:recordfile.">")
632 " call Decho("s:record[file|dir] doesn't exist")
635 " call Dret("s:RecordInFile")
638 " ---------------------------------------------------------------------
639 " vimball#VimballHome: determine/get home directory path (usually from rtp) {{{2
640 fun! vimball#VimballHome()
641 " call Dfunc("vimball#VimballHome()")
642 if exists("g:vimball_home")
643 let home= g:vimball_home
645 " go to vim plugin home
646 for home in split(&rtp,',') + ['']
647 if isdirectory(home) && filewritable(home) | break | endif
648 let basehome= substitute(home,'[/\\]\.vim$','','')
649 if isdirectory(basehome) && filewritable(basehome)
650 let home= basehome."/.vim"
655 " just pick the first directory
656 let home= substitute(&rtp,',.*$','','')
658 if (has("win32") || has("win95") || has("win64") || has("win16"))
659 let home= substitute(home,'/','\\','g')
662 " insure that the home directory exists
663 " call Decho("picked home<".home.">")
664 if !isdirectory(home)
665 if exists("g:vimball_mkdir")
666 " call Decho("home<".home."> isn't a directory -- making it now with g:vimball_mkdir<".g:vimball_mkdir.">")
667 " call Decho("system(".g:vimball_mkdir." ".shellescape(home).")")
668 call system(g:vimball_mkdir." ".shellescape(home))
670 " call Decho("home<".home."> isn't a directory -- making it now with mkdir()")
674 " call Dret("vimball#VimballHome <".home.">")
678 " ---------------------------------------------------------------------
679 " vimball#SaveSettings: {{{2
680 fun! vimball#SaveSettings()
681 " call Dfunc("SaveSettings()")
682 let s:makeep = getpos("'a")
688 let s:fenkeep = &l:fen
689 let s:hidkeep = &hidden
693 let s:repkeep = &report
697 setlocal ei=all ve=all noacd nofen noic report=999 nohid bt= ma lz pm= ff=unix
699 setlocal ei=all ve=all nofen noic report=999 nohid bt= ma lz pm= ff=unix
701 " vimballs should be in unix format
703 " call Dret("SaveSettings")
706 " ---------------------------------------------------------------------
707 " vimball#RestoreSettings: {{{2
708 fun! vimball#RestoreSettings()
709 " call Dfunc("RestoreSettings()")
714 let &l:fen = s:fenkeep
715 let &hidden = s:hidkeep
719 let &report = s:repkeep
725 " call Decho("restore mark-a: makeep=".string(makeep))
726 call setpos("'a",s:makeep)
731 unlet s:regakeep s:eikeep s:fenkeep s:hidkeep s:ickeep s:repkeep s:vekeep s:makeep s:lzkeep s:pmkeep s:ffkeep
732 " call Dret("RestoreSettings")
735 " ---------------------------------------------------------------------