script_util.vim 2.3 KB

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