neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

script_util.vim (2375B)


      1 " Functions shared by the tests for Vim script
      2 
      3 " Commands to track the execution path of a script
      4 com!		   XpathINIT  let g:Xpath = ''
      5 com! -nargs=1 -bar Xpath      let g:Xpath ..= <args>
      6 com!               XloopINIT  let g:Xloop = 1
      7 com! -nargs=1 -bar Xloop      let g:Xpath ..= <args> .. g:Xloop
      8 com!               XloopNEXT  let g:Xloop += 1
      9 
     10 " MakeScript() - Make a script file from a function.			    {{{2
     11 "
     12 " Create a script that consists of the body of the function a:funcname.
     13 " Replace any ":return" by a ":finish", any argument variable by a global
     14 " variable, and every ":call" by a ":source" for the next following argument
     15 " in the variable argument list.  This function is useful if similar tests are
     16 " to be made for a ":return" from a function call or a ":finish" in a script
     17 " file.
     18 func MakeScript(funcname, ...)
     19    let script = tempname()
     20    execute "redir! >" . script
     21    execute "function" a:funcname
     22    redir END
     23    execute "edit" script
     24    " Delete the "function" and the "endfunction" lines.  Do not include the
     25    " word "function" in the pattern since it might be translated if LANG is
     26    " set.  When MakeScript() is being debugged, this deletes also the debugging
     27    " output of its line 3 and 4.
     28    exec '1,/.*' . a:funcname . '(.*)/d'
     29    /^\d*\s*endfunction\>/,$d
     30    %s/^\d*//e
     31    %s/return/finish/e
     32    %s/\<a:\(\h\w*\)/g:\1/ge
     33    normal gg0
     34    let cnt = 0
     35    while search('\<call\s*\%(\u\|s:\)\w*\s*(.*)', 'W') > 0
     36 let cnt = cnt + 1
     37 s/\<call\s*\%(\u\|s:\)\w*\s*(.*)/\='source ' . a:{cnt}/
     38    endwhile
     39    g/^\s*$/d
     40    write
     41    bwipeout
     42    return script
     43 endfunc
     44 
     45 " ExecAsScript - Source a temporary script made from a function.	    {{{2
     46 "
     47 " Make a temporary script file from the function a:funcname, ":source" it, and
     48 " delete it afterwards.  However, if an exception is thrown the file may remain,
     49 " the caller should call DeleteTheScript() afterwards.
     50 let s:script_name = ''
     51 func ExecAsScript(funcname)
     52    " Make a script from the function passed as argument.
     53    let s:script_name = MakeScript(a:funcname)
     54 
     55    " Source and delete the script.
     56    exec "source" s:script_name
     57    call delete(s:script_name)
     58    let s:script_name = ''
     59 endfunc
     60 
     61 func DeleteTheScript()
     62    if s:script_name
     63 call delete(s:script_name)
     64 let s:script_name = ''
     65    endif
     66 endfunc
     67 
     68 com! -nargs=1 -bar ExecAsScript call ExecAsScript(<f-args>)