xref: /vim-8.2.3635/runtime/autoload/vimball.vim (revision eee697b7)
1" vimball : construct a file containing both paths and files
2" Author: Charles E. Campbell, Jr.
3" Date:   Mar 20, 2006
4" Version: 4
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 = "v4"
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:really
98   if a:writelevel
99    exe "w! ".vbname
100   else
101    exe "w ".vbname
102   endif
103  endif
104"  call Decho("Vimball<".vbname."> created")
105  echo "Vimball<".vbname."> created"
106
107  " remove the evidence
108  setlocal nomod bh=wipe
109  exe "tabn ".curtabnr
110  exe "tabc ".vbtabnr
111
112  " restore options
113  let &ei= eikeep
114
115"  call Dret("MkVimball")
116endfun
117
118" ---------------------------------------------------------------------
119" Vimball: {{{2
120fun! vimball#Vimball(really)
121"  call Dfunc("Vimball(really=".a:really.")")
122
123  if getline(1) !~ '^" Vimball Archiver by Charles E. Campbell, Jr., Ph.D.$'
124   echoerr "(Vimball) The current file does not appear to be a Vimball!"
125"   call Dret("Vimball")
126   return
127  endif
128
129  " initialize
130  let regakeep = @a
131  let eikeep   = &ei
132  let vekeep   = &ve
133  let makeep   = getpos("'a")
134  let curtabnr = tabpagenr()
135  set ei=all ve=all
136
137  " set up vimball tab
138  tabnew
139  silent! file Vimball
140  let vbtabnr= tabpagenr()
141  let didhelp= ""
142
143  " go to vim plugin home
144  let home   = substitute(&rtp,',.*$','','')
145  let curdir = getcwd()
146"  call Decho("exe cd ".home)
147  exe "cd ".home
148  let linenr  = 4
149  let filecnt = 0
150
151  " give title to listing of (extracted) files from Vimball Archive
152  if a:really
153   echohl Title | echomsg "Vimball Archive" | echohl None
154  else
155   echohl Title | echomsg "Vimball Archive Listing" | echohl None
156  endif
157
158  " apportion vimball contents to various files
159"  call Decho("exe tabn ".curtabnr)
160  exe "tabn ".curtabnr
161"  call Decho("linenr=".linenr." line$=".line("$"))
162  while 1 < linenr && linenr < line("$")
163   let fname   = getline(linenr)
164   let fsize   = getline(linenr+1)
165   let filecnt = filecnt + 1
166   if a:really
167    echomsg "extracted <".fname.">: ".fsize." lines"
168   else
169    echomsg "would extract <".fname.">: ".fsize." lines"
170   endif
171"   call Decho(linenr.": will extract file<".fname.">")
172"   call Decho((linenr+1).": fsize=".fsize)
173
174   " make directories if they don't exist yet
175   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
188   exe linenr
189   norm! jjma
190   exe (linenr + fsize + 1)
191   silent norm! "ay'a
192"   call Decho("yanked ".fsize." lines into register-a")
193
194"   call Decho("didhelp<".didhelp."> fname<".fname.">")
195   if didhelp == "" && fname =~ 'doc/[^/]\+\.txt$'
196   	let didhelp= substitute(fname,'^\(.*\<doc\)[/\\][^.]*\.txt$','\1','e')
197"	call Decho("didhelp<".didhelp.">")
198   endif
199
200   " copy "a" buffer into tab
201"   call Decho('copy "a buffer into tab#'.vbtabnr)
202   exe "tabn ".vbtabnr
203   silent! %d
204   silent norm! "aPGdd1G
205"   call Decho("rega<".@a.">")
206
207   " write tab to file
208"   call Decho("exe w! ".fname)
209   exe "silent w! ".fname
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