vimball.vim 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. " vimball.vim : construct a file containing both paths and files
  2. " Author: Charles E. Campbell
  3. " Date: Apr 11, 2016
  4. " Version: 37
  5. " GetLatestVimScripts: 1502 1 :AutoInstall: vimball.vim
  6. " Copyright: (c) 2004-2011 by Charles E. Campbell
  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. " Load Once: {{{1
  13. if &cp || exists("g:loaded_vimball")
  14. finish
  15. endif
  16. let g:loaded_vimball = "v37"
  17. if v:version < 702
  18. echohl WarningMsg
  19. echo "***warning*** this version of vimball needs vim 7.2"
  20. echohl Normal
  21. finish
  22. endif
  23. let s:keepcpo= &cpo
  24. set cpo&vim
  25. "DechoTabOn
  26. " =====================================================================
  27. " Constants: {{{1
  28. if !exists("s:USAGE")
  29. let s:USAGE = 0
  30. let s:WARNING = 1
  31. let s:ERROR = 2
  32. " determine if cygwin is in use or not
  33. if !exists("g:netrw_cygwin")
  34. if has("win32") || has("win95") || has("win64") || has("win16")
  35. if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
  36. let g:netrw_cygwin= 1
  37. else
  38. let g:netrw_cygwin= 0
  39. endif
  40. else
  41. let g:netrw_cygwin= 0
  42. endif
  43. endif
  44. " set up g:vimball_mkdir if the mkdir() call isn't defined
  45. if !exists("*mkdir")
  46. if exists("g:netrw_local_mkdir")
  47. let g:vimball_mkdir= g:netrw_local_mkdir
  48. elseif executable("mkdir")
  49. let g:vimball_mkdir= "mkdir"
  50. elseif executable("makedir")
  51. let g:vimball_mkdir= "makedir"
  52. endif
  53. if !exists(g:vimball_mkdir)
  54. call vimball#ShowMesg(s:WARNING,"(vimball) g:vimball_mkdir undefined")
  55. endif
  56. endif
  57. endif
  58. " =====================================================================
  59. " Functions: {{{1
  60. " ---------------------------------------------------------------------
  61. " vimball#MkVimball: creates a vimball given a list of paths to files {{{2
  62. " Input:
  63. " line1,line2: a range of lines containing paths to files to be included in the vimball
  64. " writelevel : if true, force a write to filename.vmb, even if it exists
  65. " (usually accomplished with :MkVimball! ...
  66. " filename : base name of file to be created (ie. filename.vmb)
  67. " Output: a filename.vmb using vimball format:
  68. " path
  69. " filesize
  70. " [file]
  71. " path
  72. " filesize
  73. " [file]
  74. fun! vimball#MkVimball(line1,line2,writelevel,...) range
  75. " call Dfunc("MkVimball(line1=".a:line1." line2=".a:line2." writelevel=".a:writelevel." vimballname<".a:1.">) a:0=".a:0)
  76. if a:1 =~ '\.vim$' || a:1 =~ '\.txt$'
  77. let vbname= substitute(a:1,'\.\a\{3}$','.vmb','')
  78. else
  79. let vbname= a:1
  80. endif
  81. if vbname !~ '\.vmb$'
  82. let vbname= vbname.'.vmb'
  83. endif
  84. " call Decho("vbname<".vbname.">")
  85. if !a:writelevel && a:1 =~ '[\/]'
  86. call vimball#ShowMesg(s:ERROR,"(MkVimball) vimball name<".a:1."> should not include slashes; use ! to insist")
  87. " call Dret("MkVimball : vimball name<".a:1."> should not include slashes")
  88. return
  89. endif
  90. if !a:writelevel && filereadable(vbname)
  91. call vimball#ShowMesg(s:ERROR,"(MkVimball) file<".vbname."> exists; use ! to insist")
  92. " call Dret("MkVimball : file<".vbname."> already exists; use ! to insist")
  93. return
  94. endif
  95. " user option bypass
  96. call vimball#SaveSettings()
  97. if a:0 >= 2
  98. " allow user to specify where to get the files
  99. let home= expand(a:2)
  100. else
  101. " use first existing directory from rtp
  102. let home= vimball#VimballHome()
  103. endif
  104. " save current directory
  105. let curdir = getcwd()
  106. call s:ChgDir(home)
  107. " record current tab, initialize while loop index
  108. let curtabnr = tabpagenr()
  109. let linenr = a:line1
  110. " call Decho("curtabnr=".curtabnr)
  111. while linenr <= a:line2
  112. let svfile = getline(linenr)
  113. " call Decho("svfile<".svfile.">")
  114. if !filereadable(svfile)
  115. call vimball#ShowMesg(s:ERROR,"unable to read file<".svfile.">")
  116. call s:ChgDir(curdir)
  117. call vimball#RestoreSettings()
  118. " call Dret("MkVimball")
  119. return
  120. endif
  121. " create/switch to mkvimball tab
  122. if !exists("vbtabnr")
  123. tabnew
  124. sil! file Vimball
  125. let vbtabnr= tabpagenr()
  126. else
  127. exe "tabn ".vbtabnr
  128. endif
  129. let lastline= line("$") + 1
  130. if lastline == 2 && getline("$") == ""
  131. call setline(1,'" Vimball Archiver by Charles E. Campbell')
  132. call setline(2,'UseVimball')
  133. call setline(3,'finish')
  134. let lastline= line("$") + 1
  135. endif
  136. call setline(lastline ,substitute(svfile,'$',' [[[1',''))
  137. call setline(lastline+1,0)
  138. " write the file from the tab
  139. " call Decho("exe $r ".fnameescape(svfile))
  140. exe "$r ".fnameescape(svfile)
  141. call setline(lastline+1,line("$") - lastline - 1)
  142. " call Decho("lastline=".lastline." line$=".line("$"))
  143. " restore to normal tab
  144. exe "tabn ".curtabnr
  145. let linenr= linenr + 1
  146. endwhile
  147. " write the vimball
  148. exe "tabn ".vbtabnr
  149. call s:ChgDir(curdir)
  150. setlocal ff=unix
  151. if a:writelevel
  152. " call Decho("exe w! ".fnameescape(vbname))
  153. exe "w! ".fnameescape(vbname)
  154. else
  155. " call Decho("exe w ".fnameescape(vbname))
  156. exe "w ".fnameescape(vbname)
  157. endif
  158. " call Decho("Vimball<".vbname."> created")
  159. echo "Vimball<".vbname."> created"
  160. " remove the evidence
  161. setlocal nomod bh=wipe
  162. exe "tabn ".curtabnr
  163. exe "tabc! ".vbtabnr
  164. " restore options
  165. call vimball#RestoreSettings()
  166. " call Dret("MkVimball")
  167. endfun
  168. " ---------------------------------------------------------------------
  169. " vimball#Vimball: extract and distribute contents from a vimball {{{2
  170. " (invoked the the UseVimball command embedded in
  171. " vimballs' prologue)
  172. fun! vimball#Vimball(really,...)
  173. " call Dfunc("vimball#Vimball(really=".a:really.") a:0=".a:0)
  174. if v:version < 701 || (v:version == 701 && !exists('*fnameescape'))
  175. echoerr "your vim is missing the fnameescape() function (pls upgrade to vim 7.2 or later)"
  176. " call Dret("vimball#Vimball : needs 7.1 with patch 299 or later")
  177. return
  178. endif
  179. if getline(1) !~ '^" Vimball Archiver'
  180. echoerr "(Vimball) The current file does not appear to be a Vimball!"
  181. " call Dret("vimball#Vimball")
  182. return
  183. endif
  184. " set up standard settings
  185. call vimball#SaveSettings()
  186. let curtabnr = tabpagenr()
  187. let vimballfile = expand("%:tr")
  188. " set up vimball tab
  189. " call Decho("setting up vimball tab")
  190. tabnew
  191. sil! file Vimball
  192. let vbtabnr= tabpagenr()
  193. let didhelp= ""
  194. " go to vim plugin home
  195. if a:0 > 0
  196. " let user specify the directory where the vimball is to be unpacked.
  197. " If, however, the user did not specify a full path, set the home to be below the current directory
  198. let home= expand(a:1)
  199. if has("win32") || has("win95") || has("win64") || has("win16")
  200. if home !~ '^\a:[/\\]'
  201. let home= getcwd().'/'.a:1
  202. endif
  203. elseif home !~ '^/'
  204. let home= getcwd().'/'.a:1
  205. endif
  206. else
  207. let home= vimball#VimballHome()
  208. endif
  209. " call Decho("home<".home.">")
  210. " save current directory and remove older same-named vimball, if any
  211. let curdir = getcwd()
  212. " call Decho("home<".home.">")
  213. " call Decho("curdir<".curdir.">")
  214. call s:ChgDir(home)
  215. let s:ok_unablefind= 1
  216. call vimball#RmVimball(vimballfile)
  217. unlet s:ok_unablefind
  218. let linenr = 4
  219. let filecnt = 0
  220. " give title to listing of (extracted) files from Vimball Archive
  221. if a:really
  222. echohl Title | echomsg "Vimball Archive" | echohl None
  223. else
  224. echohl Title | echomsg "Vimball Archive Listing" | echohl None
  225. echohl Statement | echomsg "files would be placed under: ".home | echohl None
  226. endif
  227. " apportion vimball contents to various files
  228. " call Decho("exe tabn ".curtabnr)
  229. exe "tabn ".curtabnr
  230. " call Decho("linenr=".linenr." line$=".line("$"))
  231. while 1 < linenr && linenr < line("$")
  232. let fname = substitute(getline(linenr),'\t\[\[\[1$','','')
  233. let fname = substitute(fname,'\\','/','g')
  234. let fsize = substitute(getline(linenr+1),'^\(\d\+\).\{-}$','\1','')+0
  235. let fenc = substitute(getline(linenr+1),'^\d\+\s*\(\S\{-}\)$','\1','')
  236. let filecnt = filecnt + 1
  237. " call Decho("fname<".fname."> fsize=".fsize." filecnt=".filecnt. " fenc=".fenc)
  238. if a:really
  239. echomsg "extracted <".fname.">: ".fsize." lines"
  240. else
  241. echomsg "would extract <".fname.">: ".fsize." lines"
  242. endif
  243. " call Decho("using L#".linenr.": will extract file<".fname.">")
  244. " call Decho("using L#".(linenr+1).": fsize=".fsize)
  245. " Allow AsNeeded/ directory to take place of plugin/ directory
  246. " when AsNeeded/filename is filereadable or was present in VimballRecord
  247. if fname =~ '\<plugin/'
  248. let anfname= substitute(fname,'\<plugin/','AsNeeded/','')
  249. if filereadable(anfname) || (exists("s:VBRstring") && s:VBRstring =~# anfname)
  250. " call Decho("using anfname<".anfname."> instead of <".fname.">")
  251. let fname= anfname
  252. endif
  253. endif
  254. " make directories if they don't exist yet
  255. if a:really
  256. " call Decho("making directories if they don't exist yet (fname<".fname.">)")
  257. let fnamebuf= substitute(fname,'\\','/','g')
  258. let dirpath = substitute(home,'\\','/','g')
  259. " call Decho("init: fnamebuf<".fnamebuf.">")
  260. " call Decho("init: dirpath <".dirpath.">")
  261. while fnamebuf =~ '/'
  262. let dirname = dirpath."/".substitute(fnamebuf,'/.*$','','')
  263. let dirpath = dirname
  264. let fnamebuf = substitute(fnamebuf,'^.\{-}/\(.*\)$','\1','')
  265. " call Decho("dirname<".dirname.">")
  266. " call Decho("dirpath<".dirpath.">")
  267. if !isdirectory(dirname)
  268. " call Decho("making <".dirname.">")
  269. if exists("g:vimball_mkdir")
  270. call system(g:vimball_mkdir." ".shellescape(dirname))
  271. else
  272. call mkdir(dirname)
  273. endif
  274. call s:RecordInVar(home,"rmdir('".dirname."')")
  275. endif
  276. endwhile
  277. endif
  278. call s:ChgDir(home)
  279. " grab specified qty of lines and place into "a" buffer
  280. " (skip over path/filename and qty-lines)
  281. let linenr = linenr + 2
  282. let lastline = linenr + fsize - 1
  283. " call Decho("exe ".linenr.",".lastline."yank a")
  284. " no point in handling a zero-length file
  285. if lastline >= linenr
  286. exe "silent ".linenr.",".lastline."yank a"
  287. " copy "a" buffer into tab
  288. " call Decho('copy "a buffer into tab#'.vbtabnr)
  289. exe "tabn ".vbtabnr
  290. setlocal ma
  291. sil! %d
  292. silent put a
  293. 1
  294. sil! d
  295. " write tab to file
  296. if a:really
  297. let fnamepath= home."/".fname
  298. " call Decho("exe w! ".fnameescape(fnamepath))
  299. if fenc != ""
  300. exe "silent w! ++enc=".fnameescape(fenc)." ".fnameescape(fnamepath)
  301. else
  302. exe "silent w! ".fnameescape(fnamepath)
  303. endif
  304. echo "wrote ".fnameescape(fnamepath)
  305. call s:RecordInVar(home,"call delete('".fnamepath."')")
  306. endif
  307. " return to tab with vimball
  308. " call Decho("exe tabn ".curtabnr)
  309. exe "tabn ".curtabnr
  310. " set up help if it's a doc/*.txt file
  311. " call Decho("didhelp<".didhelp."> fname<".fname.">")
  312. if a:really && didhelp == "" && fname =~ 'doc/[^/]\+\.\(txt\|..x\)$'
  313. let didhelp= substitute(fname,'^\(.*\<doc\)[/\\][^.]*\.\(txt\|..x\)$','\1','')
  314. " call Decho("didhelp<".didhelp.">")
  315. endif
  316. endif
  317. " update for next file
  318. " call Decho("update linenr= [linenr=".linenr."] + [fsize=".fsize."] = ".(linenr+fsize))
  319. let linenr= linenr + fsize
  320. endwhile
  321. " set up help
  322. " call Decho("about to set up help: didhelp<".didhelp.">")
  323. if didhelp != ""
  324. let htpath= home."/".didhelp
  325. " call Decho("exe helptags ".htpath)
  326. exe "helptags ".fnameescape(htpath)
  327. echo "did helptags"
  328. endif
  329. " make sure a "Press ENTER..." prompt appears to keep the messages showing!
  330. while filecnt <= &ch
  331. echomsg " "
  332. let filecnt= filecnt + 1
  333. endwhile
  334. " record actions in <.VimballRecord>
  335. call s:RecordInFile(home)
  336. " restore events, delete tab and buffer
  337. exe "sil! tabn ".vbtabnr
  338. setlocal nomod bh=wipe
  339. exe "sil! tabn ".curtabnr
  340. exe "sil! tabc! ".vbtabnr
  341. call vimball#RestoreSettings()
  342. call s:ChgDir(curdir)
  343. " call Dret("vimball#Vimball")
  344. endfun
  345. " ---------------------------------------------------------------------
  346. " vimball#RmVimball: remove any files, remove any directories made by any {{{2
  347. " previous vimball extraction based on a file of the current
  348. " name.
  349. " Usage: RmVimball (assume current file is a vimball; remove)
  350. " RmVimball vimballname
  351. fun! vimball#RmVimball(...)
  352. " call Dfunc("vimball#RmVimball() a:0=".a:0)
  353. if exists("g:vimball_norecord")
  354. " call Dret("vimball#RmVimball : (g:vimball_norecord)")
  355. return
  356. endif
  357. if a:0 == 0
  358. let curfile= expand("%:tr")
  359. " call Decho("case a:0=0: curfile<".curfile."> (used expand(%:tr))")
  360. else
  361. if a:1 =~ '[\/]'
  362. call vimball#ShowMesg(s:USAGE,"RmVimball vimballname [path]")
  363. " call Dret("vimball#RmVimball : suspect a:1<".a:1.">")
  364. return
  365. endif
  366. let curfile= a:1
  367. " call Decho("case a:0=".a:0.": curfile<".curfile.">")
  368. endif
  369. if curfile =~ '\.vmb$'
  370. let curfile= substitute(curfile,'\.vmb','','')
  371. elseif curfile =~ '\.vba$'
  372. let curfile= substitute(curfile,'\.vba','','')
  373. endif
  374. if a:0 >= 2
  375. let home= expand(a:2)
  376. else
  377. let home= vimball#VimballHome()
  378. endif
  379. let curdir = getcwd()
  380. " call Decho("home <".home.">")
  381. " call Decho("curfile<".curfile.">")
  382. " call Decho("curdir <".curdir.">")
  383. call s:ChgDir(home)
  384. if filereadable(".VimballRecord")
  385. " call Decho(".VimballRecord is readable")
  386. " call Decho("curfile<".curfile.">")
  387. keepalt keepjumps 1split
  388. sil! keepalt keepjumps e .VimballRecord
  389. let keepsrch= @/
  390. " call Decho('search for ^\M'.curfile.'.\m: ')
  391. " call Decho('search for ^\M'.curfile.'.\m{vba|vmb}: ')
  392. " call Decho('search for ^\M'.curfile.'\m[-0-9.]*\.{vba|vmb}: ')
  393. if search('^\M'.curfile."\m: ".'cw')
  394. let foundit= 1
  395. elseif search('^\M'.curfile.".\mvmb: ",'cw')
  396. let foundit= 2
  397. elseif search('^\M'.curfile.'\m[-0-9.]*\.vmb: ','cw')
  398. let foundit= 2
  399. elseif search('^\M'.curfile.".\mvba: ",'cw')
  400. let foundit= 1
  401. elseif search('^\M'.curfile.'\m[-0-9.]*\.vba: ','cw')
  402. let foundit= 1
  403. else
  404. let foundit = 0
  405. endif
  406. if foundit
  407. if foundit == 1
  408. let exestring = substitute(getline("."),'^\M'.curfile.'\m\S\{-}\.vba: ','','')
  409. else
  410. let exestring = substitute(getline("."),'^\M'.curfile.'\m\S\{-}\.vmb: ','','')
  411. endif
  412. let s:VBRstring= substitute(exestring,'call delete(','','g')
  413. let s:VBRstring= substitute(s:VBRstring,"[')]",'','g')
  414. " call Decho("exe ".exestring)
  415. sil! keepalt keepjumps exe exestring
  416. sil! keepalt keepjumps d
  417. let exestring= strlen(substitute(exestring,'call delete(.\{-})|\=',"D","g"))
  418. " call Decho("exestring<".exestring.">")
  419. echomsg "removed ".exestring." files"
  420. else
  421. let s:VBRstring= ''
  422. let curfile = substitute(curfile,'\.vmb','','')
  423. " call Decho("unable to find <".curfile."> in .VimballRecord")
  424. if !exists("s:ok_unablefind")
  425. call vimball#ShowMesg(s:WARNING,"(RmVimball) unable to find <".curfile."> in .VimballRecord")
  426. endif
  427. endif
  428. sil! keepalt keepjumps g/^\s*$/d
  429. sil! keepalt keepjumps wq!
  430. let @/= keepsrch
  431. endif
  432. call s:ChgDir(curdir)
  433. " call Dret("vimball#RmVimball")
  434. endfun
  435. " ---------------------------------------------------------------------
  436. " vimball#Decompress: attempts to automatically decompress vimballs {{{2
  437. fun! vimball#Decompress(fname,...)
  438. " call Dfunc("Decompress(fname<".a:fname.">) a:0=".a:0)
  439. " decompression:
  440. if expand("%") =~ '.*\.gz' && executable("gunzip")
  441. " handle *.gz with gunzip
  442. silent exe "!gunzip ".shellescape(a:fname)
  443. if v:shell_error != 0
  444. call vimball#ShowMesg(s:WARNING,"(vimball#Decompress) gunzip may have failed with <".a:fname.">")
  445. endif
  446. let fname= substitute(a:fname,'\.gz$','','')
  447. exe "e ".escape(fname,' \')
  448. if a:0 == 0| call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)") | endif
  449. elseif expand("%") =~ '.*\.gz' && executable("gzip")
  450. " handle *.gz with gzip -d
  451. silent exe "!gzip -d ".shellescape(a:fname)
  452. if v:shell_error != 0
  453. call vimball#ShowMesg(s:WARNING,'(vimball#Decompress) "gzip -d" may have failed with <'.a:fname.">")
  454. endif
  455. let fname= substitute(a:fname,'\.gz$','','')
  456. exe "e ".escape(fname,' \')
  457. if a:0 == 0| call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)") | endif
  458. elseif expand("%") =~ '.*\.bz2' && executable("bunzip2")
  459. " handle *.bz2 with bunzip2
  460. silent exe "!bunzip2 ".shellescape(a:fname)
  461. if v:shell_error != 0
  462. call vimball#ShowMesg(s:WARNING,"(vimball#Decompress) bunzip2 may have failed with <".a:fname.">")
  463. endif
  464. let fname= substitute(a:fname,'\.bz2$','','')
  465. exe "e ".escape(fname,' \')
  466. if a:0 == 0| call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)") | endif
  467. elseif expand("%") =~ '.*\.bz2' && executable("bzip2")
  468. " handle *.bz2 with bzip2 -d
  469. silent exe "!bzip2 -d ".shellescape(a:fname)
  470. if v:shell_error != 0
  471. call vimball#ShowMesg(s:WARNING,'(vimball#Decompress) "bzip2 -d" may have failed with <'.a:fname.">")
  472. endif
  473. let fname= substitute(a:fname,'\.bz2$','','')
  474. exe "e ".escape(fname,' \')
  475. if a:0 == 0| call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)") | endif
  476. elseif expand("%") =~ '.*\.zip' && executable("unzip")
  477. " handle *.zip with unzip
  478. silent exe "!unzip ".shellescape(a:fname)
  479. if v:shell_error != 0
  480. call vimball#ShowMesg(s:WARNING,"(vimball#Decompress) unzip may have failed with <".a:fname.">")
  481. endif
  482. let fname= substitute(a:fname,'\.zip$','','')
  483. exe "e ".escape(fname,' \')
  484. if a:0 == 0| call vimball#ShowMesg(s:USAGE,"Source this file to extract it! (:so %)") | endif
  485. endif
  486. if a:0 == 0| setlocal noma bt=nofile fmr=[[[,]]] fdm=marker | endif
  487. " call Dret("Decompress")
  488. endfun
  489. " ---------------------------------------------------------------------
  490. " vimball#ShowMesg: {{{2
  491. fun! vimball#ShowMesg(level,msg)
  492. " call Dfunc("vimball#ShowMesg(level=".a:level." msg<".a:msg.">)")
  493. let rulerkeep = &ruler
  494. let showcmdkeep = &showcmd
  495. set noruler noshowcmd
  496. redraw!
  497. if &fo =~# '[ta]'
  498. echomsg "***vimball*** ".a:msg
  499. else
  500. if a:level == s:WARNING || a:level == s:USAGE
  501. echohl WarningMsg
  502. elseif a:level == s:ERROR
  503. echohl Error
  504. endif
  505. echomsg "***vimball*** ".a:msg
  506. echohl None
  507. endif
  508. if a:level != s:USAGE
  509. call inputsave()|let ok= input("Press <cr> to continue")|call inputrestore()
  510. endif
  511. let &ruler = rulerkeep
  512. let &showcmd = showcmdkeep
  513. " call Dret("vimball#ShowMesg")
  514. endfun
  515. " =====================================================================
  516. " s:ChgDir: change directory (in spite of Windoze) {{{2
  517. fun! s:ChgDir(newdir)
  518. " call Dfunc("ChgDir(newdir<".a:newdir.">)")
  519. if (has("win32") || has("win95") || has("win64") || has("win16"))
  520. try
  521. exe 'silent cd '.fnameescape(substitute(a:newdir,'/','\\','g'))
  522. catch /^Vim\%((\a\+)\)\=:E/
  523. call mkdir(fnameescape(substitute(a:newdir,'/','\\','g')))
  524. exe 'silent cd '.fnameescape(substitute(a:newdir,'/','\\','g'))
  525. endtry
  526. else
  527. try
  528. exe 'silent cd '.fnameescape(a:newdir)
  529. catch /^Vim\%((\a\+)\)\=:E/
  530. call mkdir(fnameescape(a:newdir))
  531. exe 'silent cd '.fnameescape(a:newdir)
  532. endtry
  533. endif
  534. " call Dret("ChgDir : curdir<".getcwd().">")
  535. endfun
  536. " ---------------------------------------------------------------------
  537. " s:RecordInVar: record a un-vimball command in the .VimballRecord file {{{2
  538. fun! s:RecordInVar(home,cmd)
  539. " call Dfunc("RecordInVar(home<".a:home."> cmd<".a:cmd.">)")
  540. if a:cmd =~ '^rmdir'
  541. " if !exists("s:recorddir")
  542. " let s:recorddir= substitute(a:cmd,'^rmdir',"call s:Rmdir",'')
  543. " else
  544. " let s:recorddir= s:recorddir."|".substitute(a:cmd,'^rmdir',"call s:Rmdir",'')
  545. " endif
  546. elseif !exists("s:recordfile")
  547. let s:recordfile= a:cmd
  548. else
  549. let s:recordfile= s:recordfile."|".a:cmd
  550. endif
  551. " call Dret("RecordInVar : s:recordfile<".(exists("s:recordfile")? s:recordfile : "")."> s:recorddir<".(exists("s:recorddir")? s:recorddir : "").">")
  552. endfun
  553. " ---------------------------------------------------------------------
  554. " s:RecordInFile: {{{2
  555. fun! s:RecordInFile(home)
  556. " call Dfunc("s:RecordInFile()")
  557. if exists("g:vimball_norecord")
  558. " call Dret("s:RecordInFile : g:vimball_norecord")
  559. return
  560. endif
  561. if exists("s:recordfile") || exists("s:recorddir")
  562. let curdir= getcwd()
  563. call s:ChgDir(a:home)
  564. keepalt keepjumps 1split
  565. let cmd= expand("%:tr").": "
  566. " call Decho("cmd<".cmd.">")
  567. sil! keepalt keepjumps e .VimballRecord
  568. setlocal ma
  569. $
  570. if exists("s:recordfile") && exists("s:recorddir")
  571. let cmd= cmd.s:recordfile."|".s:recorddir
  572. elseif exists("s:recorddir")
  573. let cmd= cmd.s:recorddir
  574. elseif exists("s:recordfile")
  575. let cmd= cmd.s:recordfile
  576. else
  577. " call Dret("s:RecordInFile : neither recordfile nor recorddir exist")
  578. return
  579. endif
  580. " call Decho("cmd<".cmd.">")
  581. " put command into buffer, write .VimballRecord `file
  582. keepalt keepjumps put=cmd
  583. sil! keepalt keepjumps g/^\s*$/d
  584. sil! keepalt keepjumps wq!
  585. call s:ChgDir(curdir)
  586. if exists("s:recorddir")
  587. " call Decho("unlet s:recorddir<".s:recorddir.">")
  588. unlet s:recorddir
  589. endif
  590. if exists("s:recordfile")
  591. " call Decho("unlet s:recordfile<".s:recordfile.">")
  592. unlet s:recordfile
  593. endif
  594. else
  595. " call Decho("s:record[file|dir] doesn't exist")
  596. endif
  597. " call Dret("s:RecordInFile")
  598. endfun
  599. " ---------------------------------------------------------------------
  600. " vimball#VimballHome: determine/get home directory path (usually from rtp) {{{2
  601. fun! vimball#VimballHome()
  602. " call Dfunc("vimball#VimballHome()")
  603. if exists("g:vimball_home")
  604. let home= g:vimball_home
  605. else
  606. " go to vim plugin home
  607. for home in split(&rtp,',') + ['']
  608. if isdirectory(home) && filewritable(home) | break | endif
  609. let basehome= substitute(home,'[/\\]\.vim$','','')
  610. if isdirectory(basehome) && filewritable(basehome)
  611. let home= basehome."/.vim"
  612. break
  613. endif
  614. endfor
  615. if home == ""
  616. " just pick the first directory
  617. let home= substitute(&rtp,',.*$','','')
  618. endif
  619. if (has("win32") || has("win95") || has("win64") || has("win16"))
  620. let home= substitute(home,'/','\\','g')
  621. endif
  622. endif
  623. " insure that the home directory exists
  624. " call Decho("picked home<".home.">")
  625. if !isdirectory(home)
  626. if exists("g:vimball_mkdir")
  627. " call Decho("home<".home."> isn't a directory -- making it now with g:vimball_mkdir<".g:vimball_mkdir.">")
  628. " call Decho("system(".g:vimball_mkdir." ".shellescape(home).")")
  629. call system(g:vimball_mkdir." ".shellescape(home))
  630. else
  631. " call Decho("home<".home."> isn't a directory -- making it now with mkdir()")
  632. call mkdir(home)
  633. endif
  634. endif
  635. " call Dret("vimball#VimballHome <".home.">")
  636. return home
  637. endfun
  638. " ---------------------------------------------------------------------
  639. " vimball#SaveSettings: {{{2
  640. fun! vimball#SaveSettings()
  641. " call Dfunc("SaveSettings()")
  642. let s:makeep = getpos("'a")
  643. let s:regakeep= @a
  644. if exists("+acd")
  645. let s:acdkeep = &acd
  646. endif
  647. let s:eikeep = &ei
  648. let s:fenkeep = &l:fen
  649. let s:hidkeep = &hidden
  650. let s:ickeep = &ic
  651. let s:lzkeep = &lz
  652. let s:pmkeep = &pm
  653. let s:repkeep = &report
  654. let s:vekeep = &ve
  655. let s:ffkeep = &l:ff
  656. let s:swfkeep = &l:swf
  657. if exists("+acd")
  658. setlocal ei=all ve=all noacd nofen noic report=999 nohid bt= ma lz pm= ff=unix noswf
  659. else
  660. setlocal ei=all ve=all nofen noic report=999 nohid bt= ma lz pm= ff=unix noswf
  661. endif
  662. " vimballs should be in unix format
  663. setlocal ff=unix
  664. " call Dret("SaveSettings")
  665. endfun
  666. " ---------------------------------------------------------------------
  667. " vimball#RestoreSettings: {{{2
  668. fun! vimball#RestoreSettings()
  669. " call Dfunc("RestoreSettings()")
  670. let @a = s:regakeep
  671. if exists("+acd")
  672. let &acd = s:acdkeep
  673. endif
  674. let &l:fen = s:fenkeep
  675. let &hidden = s:hidkeep
  676. let &ic = s:ickeep
  677. let &lz = s:lzkeep
  678. let &pm = s:pmkeep
  679. let &report = s:repkeep
  680. let &ve = s:vekeep
  681. let &ei = s:eikeep
  682. let &l:ff = s:ffkeep
  683. if s:makeep[0] != 0
  684. " restore mark a
  685. " call Decho("restore mark-a: makeep=".string(makeep))
  686. call setpos("'a",s:makeep)
  687. endif
  688. if exists("+acd")
  689. unlet s:acdkeep
  690. endif
  691. unlet s:regakeep s:eikeep s:fenkeep s:hidkeep s:ickeep s:repkeep s:vekeep s:makeep s:lzkeep s:pmkeep s:ffkeep
  692. " call Dret("RestoreSettings")
  693. endfun
  694. let &cpo = s:keepcpo
  695. unlet s:keepcpo
  696. " ---------------------------------------------------------------------
  697. " Modelines: {{{1
  698. " vim: fdm=marker