1" vimball : construct a file containing both paths and files 2" Author: Charles E. Campbell, Jr. 3" Date: Mar 22, 2006 4" Version: 5 5" GetLatestVimScripts: 1502 1 :AutoInstall: vimball.vim 6" Copyright: (c) 2004-2006 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! *** *** 11 12" --------------------------------------------------------------------- 13" Load Once: {{{1 14if &cp || exists("g:loaded_vimball") 15 finish 16endif 17let s:keepcpo = &cpo 18let g:loaded_vimball = "v5" 19set cpo&vim 20 21" ===================================================================== 22" Functions: {{{1 23 24" --------------------------------------------------------------------- 25" MkVimball: creates a vimball given a list of paths to files {{{2 26" Vimball Format: 27" path 28" filesize 29" [file] 30" path 31" filesize 32" [file] 33fun! vimball#MkVimball(line1,line2,writelevel,vimballname) range 34" call Dfunc("MkVimball(line1=".a:line1." line2=".a:line2." writelevel=".a:writelevel." vimballname<".a:vimballname.">") 35 let vbname= substitute(a:vimballname,'\.[^.]*$','','e').'.vba' 36 if !a:writelevel && filereadable(vbname) 37 echohl Error | echoerr "(MkVimball) file<".vbname."> exists; use ! to insist" | echohl None 38" call Dret("MkVimball : file<".vbname."> already exists; use ! to insist") 39 return 40 endif 41 42 " user option bypass 43 let eikeep= &ei 44 set ei=all 45 46 let home = substitute(&rtp,',.*$','','') 47 let curdir = getcwd() 48 exe "cd ".home 49 50 " record current tab, initialize while loop index 51 let curtabnr = tabpagenr() 52 let linenr = a:line1 53" call Decho("curtabnr=".curtabnr) 54 55 while linenr <= a:line2 56 let svfile = getline(linenr) 57" call Decho("svfile<".svfile.">") 58 59 if !filereadable(svfile) 60 echohl Error | echo "unable to read file<".svfile.">" | echohl None 61 let &ei= eikeep 62 exe "cd ".curdir 63" call Dret("MkVimball") 64 return 65 endif 66 67 " create/switch to mkvimball tab 68 if !exists("vbtabnr") 69 tabnew 70 silent! file Vimball 71 let vbtabnr= tabpagenr() 72 else 73 exe "tabn ".vbtabnr 74 endif 75 76 let lastline= line("$") + 1 77 if lastline == 2 && getline("$") == "" 78 call setline(1,'" Vimball Archiver by Charles E. Campbell, Jr., Ph.D.') 79 call setline(2,'UseVimball') 80 call setline(3,'finish') 81 let lastline= 4 82 endif 83 call setline(lastline ,svfile) 84 call setline(lastline+1,0) 85 exe "$r ".svfile 86 call setline(lastline+1,line("$") - lastline - 1) 87" call Decho("lastline=".lastline." line$=".line("$")) 88 89 " restore to normal tab 90 exe "tabn ".curtabnr 91 let linenr= linenr + 1 92 endwhile 93 94 " write the vimball 95 exe "tabn ".vbtabnr 96 exe "cd ".curdir 97 if a:writelevel 98 exe "w! ".vbname 99 else 100 exe "w ".vbname 101 endif 102" call Decho("Vimball<".vbname."> created") 103 echo "Vimball<".vbname."> created" 104 105 " remove the evidence 106 setlocal nomod bh=wipe 107 exe "tabn ".curtabnr 108 exe "tabc ".vbtabnr 109 110 " restore options 111 let &ei= eikeep 112 113" call Dret("MkVimball") 114endfun 115 116" --------------------------------------------------------------------- 117" Vimball: {{{2 118fun! vimball#Vimball(really) 119" call Dfunc("Vimball(really=".a:really.")") 120 121 if getline(1) !~ '^" Vimball Archiver by Charles E. Campbell, Jr., Ph.D.$' 122 echoerr "(Vimball) The current file does not appear to be a Vimball!" 123" call Dret("Vimball") 124 return 125 endif 126 127 " initialize 128 let regakeep = @a 129 let eikeep = &ei 130 let vekeep = &ve 131 let makeep = getpos("'a") 132 let curtabnr = tabpagenr() 133 set ei=all ve=all 134 135 " set up vimball tab 136 tabnew 137 silent! file Vimball 138 let vbtabnr= tabpagenr() 139 let didhelp= "" 140 141 " go to vim plugin home 142 let home = substitute(&rtp,',.*$','','') 143 let curdir = getcwd() 144" call Decho("exe cd ".home) 145 exe "cd ".home 146 let linenr = 4 147 let filecnt = 0 148 149 " give title to listing of (extracted) files from Vimball Archive 150 if a:really 151 echohl Title | echomsg "Vimball Archive" | echohl None 152 else 153 echohl Title | echomsg "Vimball Archive Listing" | echohl None 154 endif 155 156 " apportion vimball contents to various files 157" call Decho("exe tabn ".curtabnr) 158 exe "tabn ".curtabnr 159" call Decho("linenr=".linenr." line$=".line("$")) 160 while 1 < linenr && linenr < line("$") 161 let fname = getline(linenr) 162 let fsize = getline(linenr+1) 163 let filecnt = filecnt + 1 164 if a:really 165 echomsg "extracted <".fname.">: ".fsize." lines" 166 else 167 echomsg "would extract <".fname.">: ".fsize." lines" 168 endif 169" call Decho(linenr.": will extract file<".fname.">") 170" call Decho((linenr+1).": fsize=".fsize) 171 172 " make directories if they don't exist yet 173 let fnamebuf= fname 174 while fnamebuf =~ '/' 175 let dirname = substitute(fnamebuf,'/.*$','','e') 176 let fnamebuf = substitute(fnamebuf,'^.\{-}/\(.*\)$','\1','e') 177 if !isdirectory(dirname) 178" call Decho("making <".dirname.">") 179 call mkdir(dirname) 180 endif 181 exe "cd ".dirname 182 endwhile 183 exe "cd ".home 184 185 " grab specified qty of lines and place into "a" buffer 186 exe linenr 187 norm! jjma 188 exe (linenr + fsize + 1) 189 silent norm! "ay'a 190" call Decho("yanked ".fsize." lines into register-a") 191 192" call Decho("didhelp<".didhelp."> fname<".fname.">") 193 if didhelp == "" && fname =~ 'doc/[^/]\+\.txt$' 194 let didhelp= substitute(fname,'^\(.*\<doc\)[/\\][^.]*\.txt$','\1','e') 195" call Decho("didhelp<".didhelp.">") 196 endif 197 198 " copy "a" buffer into tab 199" call Decho('copy "a buffer into tab#'.vbtabnr) 200 exe "tabn ".vbtabnr 201 silent! %d 202 silent norm! "aPGdd1G 203" call Decho("rega<".@a.">") 204 205 " write tab to file 206 if a:really 207" call Decho("exe w! ".fname) 208 exe "silent w! ".fname 209 endif 210 211" call Decho("exe tabn ".curtabnr) 212 exe "tabn ".curtabnr 213" let oldlinenr = linenr " Decho 214 let linenr = linenr + fsize + 2 215" call Decho("update linenr= [linenr=".oldlinenr."] + [fsize=".fsize."] + 2 = ".linenr) 216 endwhile 217 218 " set up help 219" call Decho("about to set up help: didhelp<".didhelp.">") 220 if didhelp != "" 221" call Decho("exe helptags ".home."/".didhelp) 222 exe "helptags ".home."/".didhelp 223 echomsg "did helptags" 224 endif 225 226 " make sure a "Press ENTER..." prompt appears to keep the messages showing! 227 while filecnt <= &ch 228 echomsg " " 229 let filecnt= filecnt + 1 230 endwhile 231 232 " restore events, delete tab and buffer 233 exe "tabn ".vbtabnr 234 setlocal nomod bh=wipe 235 exe "tabn ".curtabnr 236 exe "tabc ".vbtabnr 237 let &ei= eikeep 238 let @a = regakeep 239 if makeep[0] != 0 240 " restore mark a 241" call Decho("restore mark-a: makeep=".string(makeep)) 242 call setpos("'a",makeep) 243 ka 244 endif 245 exe "cd ".curdir 246 247" call Dret("Vimball") 248endfun 249 250let &cpo= s:keepcpo 251unlet s:keepcpo 252" ===================================================================== 253" Modelines: {{{1 254" vim: fdm=marker 255