blob: e8042b54372901f5b8523e392c0b8951f6ad8bfd [file] [log] [blame]
Bram Moolenaareee697b2006-03-21 21:20:39 +00001" vimball : construct a file containing both paths and files
2" Author: Charles E. Campbell, Jr.
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +00003" Date: Apr 27, 2006
4" Version: 11
Bram Moolenaareee697b2006-03-21 21:20:39 +00005" 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
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +000018let g:loaded_vimball = "v11"
Bram Moolenaareee697b2006-03-21 21:20:39 +000019set 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
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +000043 call s:SaveSettings()
Bram Moolenaareee697b2006-03-21 21:20:39 +000044
Bram Moolenaarf193fff2006-04-27 00:02:13 +000045 " go to vim plugin home
46 for home in split(&rtp,',') + ['']
47 if isdirectory(home) | break | endif
48 endfor
49 if home == ""
50 let home= substitute(&rtp,',.*$','','')
51 endif
52 if (has("win32") || has("win95") || has("win64") || has("win16"))
53 let home= substitute(home,'/','\\','ge')
54 endif
55" call Decho("home<".home.">")
56
57 " save current directory
Bram Moolenaareee697b2006-03-21 21:20:39 +000058 let curdir = getcwd()
Bram Moolenaarf193fff2006-04-27 00:02:13 +000059 call s:ChgDir(home)
Bram Moolenaareee697b2006-03-21 21:20:39 +000060
61 " record current tab, initialize while loop index
62 let curtabnr = tabpagenr()
63 let linenr = a:line1
64" call Decho("curtabnr=".curtabnr)
65
66 while linenr <= a:line2
67 let svfile = getline(linenr)
68" call Decho("svfile<".svfile.">")
69
70 if !filereadable(svfile)
71 echohl Error | echo "unable to read file<".svfile.">" | echohl None
Bram Moolenaarf193fff2006-04-27 00:02:13 +000072 call s:ChgDir(curdir)
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +000073 call s:RestoreSettings()
Bram Moolenaareee697b2006-03-21 21:20:39 +000074" call Dret("MkVimball")
75 return
76 endif
77
78 " create/switch to mkvimball tab
79 if !exists("vbtabnr")
80 tabnew
81 silent! file Vimball
82 let vbtabnr= tabpagenr()
83 else
84 exe "tabn ".vbtabnr
85 endif
86
87 let lastline= line("$") + 1
88 if lastline == 2 && getline("$") == ""
89 call setline(1,'" Vimball Archiver by Charles E. Campbell, Jr., Ph.D.')
90 call setline(2,'UseVimball')
91 call setline(3,'finish')
92 let lastline= 4
93 endif
94 call setline(lastline ,svfile)
95 call setline(lastline+1,0)
Bram Moolenaarf193fff2006-04-27 00:02:13 +000096
97 " write the file from the tab
98 let svfilepath= s:Path(svfile,'')
99" call Decho("exe $r ".svfilepath)
100 exe "$r ".svfilepath
101
Bram Moolenaareee697b2006-03-21 21:20:39 +0000102 call setline(lastline+1,line("$") - lastline - 1)
103" call Decho("lastline=".lastline." line$=".line("$"))
104
105 " restore to normal tab
106 exe "tabn ".curtabnr
107 let linenr= linenr + 1
108 endwhile
109
110 " write the vimball
111 exe "tabn ".vbtabnr
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000112 call s:ChgDir(curdir)
Bram Moolenaar8ab561d2006-03-23 22:44:10 +0000113 if a:writelevel
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000114 let vbnamepath= s:Path(vbname,'')
115" call Decho("exe w! ".vbnamepath)
116 exe "w! ".vbnamepath
Bram Moolenaar8ab561d2006-03-23 22:44:10 +0000117 else
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000118 let vbnamepath= s:Path(vbname,'')
119" call Decho("exe w ".vbnamepath)
120 exe "w ".vbnamepath
Bram Moolenaareee697b2006-03-21 21:20:39 +0000121 endif
122" call Decho("Vimball<".vbname."> created")
123 echo "Vimball<".vbname."> created"
124
125 " remove the evidence
126 setlocal nomod bh=wipe
127 exe "tabn ".curtabnr
128 exe "tabc ".vbtabnr
129
130 " restore options
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000131 call s:RestoreSettings()
Bram Moolenaareee697b2006-03-21 21:20:39 +0000132
133" call Dret("MkVimball")
134endfun
135
136" ---------------------------------------------------------------------
137" Vimball: {{{2
138fun! vimball#Vimball(really)
139" call Dfunc("Vimball(really=".a:really.")")
140
141 if getline(1) !~ '^" Vimball Archiver by Charles E. Campbell, Jr., Ph.D.$'
142 echoerr "(Vimball) The current file does not appear to be a Vimball!"
143" call Dret("Vimball")
144 return
145 endif
146
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000147 " set up standard settings
148 call s:SaveSettings()
Bram Moolenaareee697b2006-03-21 21:20:39 +0000149 let curtabnr = tabpagenr()
Bram Moolenaareee697b2006-03-21 21:20:39 +0000150
151 " set up vimball tab
152 tabnew
153 silent! file Vimball
154 let vbtabnr= tabpagenr()
155 let didhelp= ""
156
157 " go to vim plugin home
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000158 for home in split(&rtp,',') + ['']
159 if isdirectory(home) | break | endif
160 endfor
161 if home == ""
162 let home= substitute(&rtp,',.*$','','')
163 endif
164 if (has("win32") || has("win95") || has("win64") || has("win16"))
165 let home= substitute(home,'/','\\','ge')
166 endif
167" call Decho("home<".home.">")
168
169 " save current directory
Bram Moolenaareee697b2006-03-21 21:20:39 +0000170 let curdir = getcwd()
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000171 call s:ChgDir(home)
172
Bram Moolenaareee697b2006-03-21 21:20:39 +0000173 let linenr = 4
174 let filecnt = 0
175
176 " give title to listing of (extracted) files from Vimball Archive
177 if a:really
178 echohl Title | echomsg "Vimball Archive" | echohl None
179 else
180 echohl Title | echomsg "Vimball Archive Listing" | echohl None
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000181 echohl Statement | echomsg "files would be placed under: ".home | echohl None
Bram Moolenaareee697b2006-03-21 21:20:39 +0000182 endif
183
184 " apportion vimball contents to various files
185" call Decho("exe tabn ".curtabnr)
186 exe "tabn ".curtabnr
187" call Decho("linenr=".linenr." line$=".line("$"))
188 while 1 < linenr && linenr < line("$")
189 let fname = getline(linenr)
190 let fsize = getline(linenr+1)
191 let filecnt = filecnt + 1
192 if a:really
193 echomsg "extracted <".fname.">: ".fsize." lines"
194 else
195 echomsg "would extract <".fname.">: ".fsize." lines"
196 endif
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000197" call Decho("using L#".linenr.": will extract file<".fname.">")
198" call Decho("using L#".(linenr+1).": fsize=".fsize)
Bram Moolenaareee697b2006-03-21 21:20:39 +0000199
200 " make directories if they don't exist yet
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000201" call Decho("making directories if they don't exist yet")
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000202 if a:really
203 let fnamebuf= fname
204 while fnamebuf =~ '/'
205 let dirname = home."/".substitute(fnamebuf,'/.*$','','e')
206 let fnamebuf = substitute(fnamebuf,'^.\{-}/\(.*\)$','\1','e')
207 if !isdirectory(dirname)
208" call Decho("making <".dirname.">")
209 call mkdir(dirname)
210 endif
211 endwhile
212 endif
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000213 call s:ChgDir(home)
Bram Moolenaareee697b2006-03-21 21:20:39 +0000214
215 " grab specified qty of lines and place into "a" buffer
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000216 " (skip over path/filename and qty-lines)
217 let linenr = linenr + 2
218 let lastline = linenr + fsize - 1
219" call Decho("exe ".linenr.",".lastline."yank a")
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000220 exe "silent ".linenr.",".lastline."yank a"
Bram Moolenaareee697b2006-03-21 21:20:39 +0000221
222 " copy "a" buffer into tab
223" call Decho('copy "a buffer into tab#'.vbtabnr)
224 exe "tabn ".vbtabnr
225 silent! %d
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000226 silent put a
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000227 1
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000228 silent d
Bram Moolenaareee697b2006-03-21 21:20:39 +0000229
230 " write tab to file
Bram Moolenaar8ab561d2006-03-23 22:44:10 +0000231 if a:really
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000232 let fnamepath= s:Path(home."/".fname,'')
233" call Decho("exe w! ".fnamepath)
234 exe "silent w! ".fnamepath
235 echo "wrote ".fnamepath
Bram Moolenaar8ab561d2006-03-23 22:44:10 +0000236 endif
Bram Moolenaareee697b2006-03-21 21:20:39 +0000237
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000238 " return to tab with vimball
Bram Moolenaareee697b2006-03-21 21:20:39 +0000239" call Decho("exe tabn ".curtabnr)
240 exe "tabn ".curtabnr
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000241
242 " set up help if its a doc/*.txt file
243" call Decho("didhelp<".didhelp."> fname<".fname.">")
244 if a:really && didhelp == "" && fname =~ 'doc/[^/]\+\.txt$'
245 let didhelp= substitute(fname,'^\(.*\<doc\)[/\\][^.]*\.txt$','\1','e')
246" call Decho("didhelp<".didhelp.">")
247 endif
248
249 " update for next file
Bram Moolenaareee697b2006-03-21 21:20:39 +0000250" let oldlinenr = linenr " Decho
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000251 let linenr = linenr + fsize
252" call Decho("update linenr= [linenr=".oldlinenr."] + [fsize=".fsize."] = ".linenr)
Bram Moolenaareee697b2006-03-21 21:20:39 +0000253 endwhile
254
255 " set up help
256" call Decho("about to set up help: didhelp<".didhelp.">")
257 if didhelp != ""
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000258 let htpath= escape(substitute(s:Path(home."/".didhelp,'"'),'"','','ge'),' ')
259" call Decho("exe helptags ".htpath)
260 exe "helptags ".htpath
261 echo "did helptags"
Bram Moolenaareee697b2006-03-21 21:20:39 +0000262 endif
263
264 " make sure a "Press ENTER..." prompt appears to keep the messages showing!
Bram Moolenaar8ab561d2006-03-23 22:44:10 +0000265 while filecnt <= &ch
Bram Moolenaareee697b2006-03-21 21:20:39 +0000266 echomsg " "
267 let filecnt= filecnt + 1
268 endwhile
269
270 " restore events, delete tab and buffer
271 exe "tabn ".vbtabnr
272 setlocal nomod bh=wipe
273 exe "tabn ".curtabnr
274 exe "tabc ".vbtabnr
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000275 call s:RestoreSettings()
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000276 call s:ChgDir(curdir)
Bram Moolenaareee697b2006-03-21 21:20:39 +0000277
278" call Dret("Vimball")
279endfun
280
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000281" ---------------------------------------------------------------------
282" vimball#Decompress: attempts to automatically decompress vimballs {{{2
283fun! vimball#Decompress(fname)
284" call Dfunc("Decompress(fname<".a:fname.">)")
285
286 " decompression:
287 if expand("%") =~ '.*\.gz' && executable("gunzip")
288 exe "!gunzip ".a:fname
289 let fname= substitute(a:fname,'\.gz$','','')
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000290 exe "e ".escape(fname,' \')
291 call vimball#ShowMesg("Source this file to extract it! (:so %)")
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000292 elseif expand("%") =~ '.*\.bz2' && executable("bunzip2")
293 exe "!bunzip2 ".a:fname
294 let fname= substitute(a:fname,'\.bz2$','','')
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000295 exe "e ".escape(fname,' \')
296 call vimball#ShowMesg("Source this file to extract it! (:so %)")
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000297 elseif expand("%") =~ '.*\.zip' && executable("unzip")
298 exe "!unzip ".a:fname
299 let fname= substitute(a:fname,'\.zip$','','')
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000300 exe "e ".escape(fname,' \')
301 call vimball#ShowMesg("Source this file to extract it! (:so %)")
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000302 endif
303
304" call Dret("Decompress")
305endfun
306
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000307" ---------------------------------------------------------------------
308" ChgDir: change directory (in spite of Windoze) {{{2
309fun! s:ChgDir(newdir)
310" call Dfunc("ChgDir(newdir<".a:newdir.">)")
311 if (has("win32") || has("win95") || has("win64") || has("win16"))
312 exe 'silent cd '.escape(substitute(a:newdir,'/','\\','g'),' ')
313 else
314 exe 'silent cd '.escape(a:newdir,' ')
315 endif
316" call Dret("ChgDir")
317endfun
318
319" ---------------------------------------------------------------------
320" Path: {{{2
321fun! s:Path(cmd,quote)
322" call Dfunc("Path(cmd<".a:cmd."> quote<".a:quote.">)")
323 if (has("win32") || has("win95") || has("win64") || has("win16"))
324 let cmdpath= a:quote.substitute(a:cmd,'/','\\','ge').a:quote
325 else
326 let cmdpath= a:quote.a:cmd.a:quote
327 endif
328 if a:quote == ""
329 let cmdpath= escape(cmdpath,' ')
330 endif
331" call Dret("Path <".cmdpath.">")
332 return cmdpath
333endfun
334
335" ---------------------------------------------------------------------
336" vimball#ShowMesg: {{{2
337fun! vimball#ShowMesg(msg)
338" call Dfunc("vimball#ShowMesg(msg<".a:msg.">)")
339 let ich= 1
340 echohl WarningMsg | echo a:msg | echohl None
341 while ich < &ch
342 echo " "
343 let ich= ich + 1
344 endwhile
345" call Dret("vimball#ShowMesg")
346endfun
347
348" ---------------------------------------------------------------------
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000349" s:SaveSettings: {{{2
350fun! s:SaveSettings()
351" call Dfunc("SaveSettings()")
352 let s:makeep = getpos("'a")
353 let s:regakeep= @a
354 let s:acdkeep = &acd
355 let s:eikeep = &ei
356 let s:fenkeep = &fen
357 let s:hidkeep = &hidden
358 let s:ickeep = &ic
359 let s:repkeep = &report
360 let s:vekeep = &ve
361 set ei=all ve=all noacd nofen noic report=999 nohid
362" call Dret("SaveSettings")
363endfun
364
365" ---------------------------------------------------------------------
366" s:RestoreSettings: {{{2
367fun! s:RestoreSettings()
368" call Dfunc("RestoreSettings()")
369 let @a = s:regakeep
370 let &acd = s:acdkeep
371 let &ei = s:eikeep
372 let &fen = s:fenkeep
373 let &hidden = s:hidkeep
374 let &ic = s:ickeep
375 let &report = s:repkeep
376 let &ve = s:vekeep
377 if s:makeep[0] != 0
378 " restore mark a
379" call Decho("restore mark-a: makeep=".string(makeep))
380 call setpos("'a",s:makeep)
381 endif
382 unlet s:regakeep s:acdkeep s:eikeep s:fenkeep s:hidkeep s:ickeep s:repkeep s:vekeep s:makeep
383" call Dret("RestoreSettings")
384endfun
385
386" ---------------------------------------------------------------------
Bram Moolenaareee697b2006-03-21 21:20:39 +0000387let &cpo= s:keepcpo
388unlet s:keepcpo
389" =====================================================================
390" Modelines: {{{1
391" vim: fdm=marker