regex that matches a modeline line

15 views
Skip to first unread message

Peng Yu

unread,
Apr 12, 2021, 10:30:32 AM4/12/21
to vim...@googlegroups.com
Hi,

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.

https://vim.fandom.com/wiki/Modeline_magic

--
Regards,
Peng

Gary Johnson

unread,
Apr 12, 2021, 11:42:34 AM4/12/21
to vim...@googlegroups.com
On 2021-04-12, Peng Yu wrote:
> Hi,
>
> 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.

Vim doesn't use a regular expression to identify modelines; it finds
them algorithmically. The code that identifies modelines is in the
chk_modeline() function in vim/src/buffer.c.

Regards,
Gary

Tim Chase

unread,
Apr 12, 2021, 12:04:15 PM4/12/21
to Peng Yu, vim...@googlegroups.com
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




Reply all
Reply to author
Forward
0 new messages