vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

vimball.vim (21491B)


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