robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
a6d5e6b105
master
1" Functions shared by the tests for Vim script 2 3" Commands to track the execution path of a script 4com ! XpathINIT let g:Xpath = '' 5com ! -nargs = 1 -bar Xpath let g:Xpath ..= <args> 6com ! XloopINIT let g:Xloop = 1 7com ! -nargs = 1 -bar Xloop let g:Xpath ..= <args> .. g:Xloop 8com ! 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. 18func MakeScript ( funcname , ...) 19let script = tempname () 20execute "redir! >" . script 21execute "function" a: funcname 22redir END 23execute "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. 28exec '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 33normal gg0 34let cnt = 0 35while search ( '\<call\s*\%(\u\|s:\)\w*\s*(.*)' , 'W' ) > 0 36let cnt = cnt + 1 37s /\<call\s * \%(\u\|s:\)\w * \s * (. * )/\='source ' . a:{cnt}/ 38endwhile 39g /^\s * $ /d 40write 41bwipeout 42return script 43endfunc 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. 50let s: script_name = '' 51func ExecAsScript ( funcname ) 52" Make a script from the function passed as argument. 53let s: script_name = MakeScript ( a: funcname ) 54 55" Source and delete the script. 56exec "source" s: script_name 57call delete ( s: script_name ) 58let s: script_name = '' 59endfunc 60 61func DeleteTheScript () 62if s: script_name 63call delete ( s: script_name ) 64let s: script_name = '' 65endif 66endfunc 67 68com ! -nargs = 1 -bar ExecAsScript call ExecAsScript(<f-args>) 69