On 2021-04-12 09:30, Peng Yu wrote:
> It seems that modeline is quite flexible. Could anybody let me know
> the regex used by vim to determine what is a modeline and what is
> not a modeline? Thanks.
How loose is acceptable? Do you want to accept the most common
forms, or do you want the version-specific versions described at `help
modeline-version`? And do you only want to allow legit modeline
options, or can it be sloppy, allowing non-sandbox settings?
A simple pass might be
\S\@<!\%(vim\=\|ex\|Vim\):\s*
gets the basics. If you want to allow for modeline-version, perhaps
\S\@<!\%(vi\%(m\%([<>=]\d\+\)\=\)\=\|ex\|Vim\):\s*
capture the options and, you can append
\%(set\=\s\+\)\=\(.*\)
but if you want to limit it to just allowed options, you'd have to
either read the source or scrape `:help options.txt` for all
available options and remove those that are disallowed in the sandbox
:help options.txt
:%y
:enew
:put
:g/This option cannot be set from a .*modeline.*sandbox/?^'?d
:v/^'.*'.*(default/d
:%s/\s.*
:%s/'//g
should get you a good starting list that you can then join and assert
that only these options are included.
-tim