blob: 61d5fa662ad10ecf68395e26ae4d5743be32f98a [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 Moolenaar551dbcc2006-04-25 22:13:59 +00003" Date: Apr 25, 2006
4" Version: 8
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 Moolenaar551dbcc2006-04-25 22:13:59 +000018let g:loaded_vimball = "v8"
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
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
Bram Moolenaar8ab561d2006-03-23 22:44:10 +000097 if a:writelevel
98 exe "w! ".vbname
99 else
100 exe "w ".vbname
Bram Moolenaareee697b2006-03-21 21:20:39 +0000101 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
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000128 let fenkeep = &fen
Bram Moolenaareee697b2006-03-21 21:20:39 +0000129 let regakeep = @a
130 let eikeep = &ei
131 let vekeep = &ve
132 let makeep = getpos("'a")
133 let curtabnr = tabpagenr()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000134 set ei=all ve=all nofen
Bram Moolenaareee697b2006-03-21 21:20:39 +0000135
136 " set up vimball tab
137 tabnew
138 silent! file Vimball
139 let vbtabnr= tabpagenr()
140 let didhelp= ""
141
142 " go to vim plugin home
143 let home = substitute(&rtp,',.*$','','')
144 let curdir = getcwd()
145" call Decho("exe cd ".home)
146 exe "cd ".home
147 let linenr = 4
148 let filecnt = 0
149
150 " give title to listing of (extracted) files from Vimball Archive
151 if a:really
152 echohl Title | echomsg "Vimball Archive" | echohl None
153 else
154 echohl Title | echomsg "Vimball Archive Listing" | echohl None
155 endif
156
157 " apportion vimball contents to various files
158" call Decho("exe tabn ".curtabnr)
159 exe "tabn ".curtabnr
160" call Decho("linenr=".linenr." line$=".line("$"))
161 while 1 < linenr && linenr < line("$")
162 let fname = getline(linenr)
163 let fsize = getline(linenr+1)
164 let filecnt = filecnt + 1
165 if a:really
166 echomsg "extracted <".fname.">: ".fsize." lines"
167 else
168 echomsg "would extract <".fname.">: ".fsize." lines"
169 endif
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000170" call Decho("using L#".linenr.": will extract file<".fname.">")
171" call Decho("using L#".(linenr+1).": fsize=".fsize)
Bram Moolenaareee697b2006-03-21 21:20:39 +0000172
173 " make directories if they don't exist yet
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000174" call Decho("making directories if they don't exist yet")
Bram Moolenaareee697b2006-03-21 21:20:39 +0000175 let fnamebuf= fname
176 while fnamebuf =~ '/'
177 let dirname = substitute(fnamebuf,'/.*$','','e')
178 let fnamebuf = substitute(fnamebuf,'^.\{-}/\(.*\)$','\1','e')
179 if !isdirectory(dirname)
180" call Decho("making <".dirname.">")
181 call mkdir(dirname)
182 endif
183 exe "cd ".dirname
184 endwhile
185 exe "cd ".home
186
187 " grab specified qty of lines and place into "a" buffer
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000188 " (skip over path/filename and qty-lines)
189 let linenr = linenr + 2
190 let lastline = linenr + fsize - 1
191" call Decho("exe ".linenr.",".lastline."yank a")
192 exe linenr.",".lastline."yank a"
Bram Moolenaareee697b2006-03-21 21:20:39 +0000193
194 " copy "a" buffer into tab
195" call Decho('copy "a buffer into tab#'.vbtabnr)
196 exe "tabn ".vbtabnr
197 silent! %d
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000198 put a
199 1
200 d
Bram Moolenaareee697b2006-03-21 21:20:39 +0000201
202 " write tab to file
Bram Moolenaar8ab561d2006-03-23 22:44:10 +0000203 if a:really
204" call Decho("exe w! ".fname)
205 exe "silent w! ".fname
206 endif
Bram Moolenaareee697b2006-03-21 21:20:39 +0000207
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000208 " return to tab with vimball
Bram Moolenaareee697b2006-03-21 21:20:39 +0000209" call Decho("exe tabn ".curtabnr)
210 exe "tabn ".curtabnr
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000211
212 " set up help if its a doc/*.txt file
213" call Decho("didhelp<".didhelp."> fname<".fname.">")
214 if a:really && didhelp == "" && fname =~ 'doc/[^/]\+\.txt$'
215 let didhelp= substitute(fname,'^\(.*\<doc\)[/\\][^.]*\.txt$','\1','e')
216" call Decho("didhelp<".didhelp.">")
217 endif
218
219 " update for next file
Bram Moolenaareee697b2006-03-21 21:20:39 +0000220" let oldlinenr = linenr " Decho
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000221 let linenr = linenr + fsize
222" call Decho("update linenr= [linenr=".oldlinenr."] + [fsize=".fsize."] = ".linenr)
Bram Moolenaareee697b2006-03-21 21:20:39 +0000223 endwhile
224
225 " set up help
226" call Decho("about to set up help: didhelp<".didhelp.">")
227 if didhelp != ""
228" call Decho("exe helptags ".home."/".didhelp)
229 exe "helptags ".home."/".didhelp
230 echomsg "did helptags"
231 endif
232
233 " make sure a "Press ENTER..." prompt appears to keep the messages showing!
Bram Moolenaar8ab561d2006-03-23 22:44:10 +0000234 while filecnt <= &ch
Bram Moolenaareee697b2006-03-21 21:20:39 +0000235 echomsg " "
236 let filecnt= filecnt + 1
237 endwhile
238
239 " restore events, delete tab and buffer
240 exe "tabn ".vbtabnr
241 setlocal nomod bh=wipe
242 exe "tabn ".curtabnr
243 exe "tabc ".vbtabnr
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000244 let &ei = eikeep
245 let @a = regakeep
246 let &fen = fenkeep
Bram Moolenaareee697b2006-03-21 21:20:39 +0000247 if makeep[0] != 0
248 " restore mark a
249" call Decho("restore mark-a: makeep=".string(makeep))
250 call setpos("'a",makeep)
251 ka
252 endif
253 exe "cd ".curdir
254
255" call Dret("Vimball")
256endfun
257
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000258" ---------------------------------------------------------------------
259" vimball#Decompress: attempts to automatically decompress vimballs {{{2
260fun! vimball#Decompress(fname)
261" call Dfunc("Decompress(fname<".a:fname.">)")
262
263 " decompression:
264 if expand("%") =~ '.*\.gz' && executable("gunzip")
265 exe "!gunzip ".a:fname
266 let fname= substitute(a:fname,'\.gz$','','')
267 exe "e ".fname
268 echohl WarningMsg | echo "Source this file to extract it! (:so ".fname.")" | echohl None
269 elseif expand("%") =~ '.*\.bz2' && executable("bunzip2")
270 exe "!bunzip2 ".a:fname
271 let fname= substitute(a:fname,'\.bz2$','','')
272 exe "e ".fname
273 echohl WarningMsg | echo "Source this file to extract it! (:so ".fname.")" | echohl None
274 elseif expand("%") =~ '.*\.zip' && executable("unzip")
275 exe "!unzip ".a:fname
276 let fname= substitute(a:fname,'\.zip$','','')
277 exe "e ".fname
278 echohl WarningMsg | echo "Source this file to extract it! (:so ".fname.")" | echohl None
279 endif
280
281" call Dret("Decompress")
282endfun
283
Bram Moolenaareee697b2006-03-21 21:20:39 +0000284let &cpo= s:keepcpo
285unlet s:keepcpo
286" =====================================================================
287" Modelines: {{{1
288" vim: fdm=marker