Monday, April 12, 2021

Re: regex that matches a modeline line

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




--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20210412110405.65bd3dd5%40bigbox.attlocal.net.

No comments:

Post a Comment