runtime(syntax-tests): Support embeddable Vim configuration for syntax tests

Currently, the very same syntax file for which a test can
be written is the only place where global variables can be
defined so that the file parts guarded with such variables
can be read during screen dump generation.  This approach
would lead to littering the syntax file with test-related
queries.

Instead, we could borrow the idea of comment-based mechanism
for test setup from the indent test runner.  With it, the
first 20 lines of each test file would be ALWAYS scanned in
search of the TEST_SETUP markers and, when found, the part
between the end of the marker and the end of the line would
be treated as a Vim Ex command.

Note that with these changes, runtime/defaults.vim is no
longer sourced for screen dump generation; however, some of
its functionality is reintroduced.

Further details can be found in the discussion thread at
https://github.com/vim/vim/discussions/14117.

related: #14215

Signed-off-by: Aliaksei Budavei <0x000c70@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/runtime/syntax/testdir/runtest.vim b/runtime/syntax/testdir/runtest.vim
index a2cdef3..ffa2be8 100644
--- a/runtime/syntax/testdir/runtest.vim
+++ b/runtime/syntax/testdir/runtest.vim
@@ -114,8 +114,6 @@
       call delete('done/' .. root)
 
       let lines =<< trim END
-	syntax on
-
 	" extra info for shell variables
 	func ShellInfo()
 	  let msg = ''
@@ -145,6 +143,25 @@
 	  redraw!
 	endfunc
 
+	func SetUpVim()
+	  call cursor(1, 1)
+	  " Defend against rogue TEST_SETUP commands.
+	  for _ in range(20)
+	    let lnum = search('\<TEST_SETUP\>', 'eW', 20)
+	    if lnum < 1
+	      break
+	    endif
+	    exe substitute(getline(lnum), '.*TEST_SETUP', '', '')
+	  endfor
+	  call cursor(1, 1)
+	  " BEGIN [runtime/defaults.vim]
+	  set display=truncate ruler scrolloff=5
+	  " Provide pre-TEST_SETUP support for input/*.c.
+	  let g:c_comment_strings = 1
+	  syntax on
+	  " END [runtime/defaults.vim]
+	  redraw!
+	endfunc
       END
       call writefile(lines, 'Xtestscript')
 
@@ -157,9 +174,12 @@
       " for the terminal window.
       redraw
 
-      let buf = RunVimInTerminal('-S Xtestscript', {})
+      " Let "Xtestscript#SetUpVim()" turn the syntax on.
+      let buf = RunVimInTerminal('-Nu NONE -S Xtestscript', {})
       " edit the file only after catching the SwapExists event
       call term_sendkeys(buf, ":edit " .. fname .. "\<CR>")
+      " set up the testing environment
+      call term_sendkeys(buf, ":call SetUpVim()\<CR>")
       " load filetype specific settings
       call term_sendkeys(buf, ":call LoadFiletype('" .. filetype .. "')\<CR>")