Skip to content

Commit abffa58

Browse files
committed
Rewrite patterns for matching Vim/Emacs modelines
Both of the previous patterns had flawed handling of less obvious cases, such as wrongly accepting invalid modelines, or failing to match grammar that would otherwise be understood by Vim or Emacs. Emacs: * Boundaries now matched correctly to avoid matching "not-mode: nroff;" * Invalid modelines like "-*- nroff; -*-" are now ignored * Preceding variables containing asterisks are permitted * Semicolons are no longer required to separate modes from variables * Handling of malformed modelines better resembles Emacs's own behaviour Vim: * Version-specific modelines are now supported: "vim<200: ft=roff" * Preceding whitespace is required before "vim/ex" * Boolean options may now be listed before filetypes: "vi: noet ft=roff" * Escaped backslashes are handled properly; "vim: s=foo\ ft=cpp ft=roff"
1 parent f37fb6b commit abffa58

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

grammars/roff.cson

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@ firstLineMatch: """(?x)
3737
^'\\\\\"\\x20[tre]+(?=\\s|$)
3838
|
3939
40-
# Emacs modeline
41-
(?i:-\\*-[^*]*(?:mode:\\s*)?\\b[gnt]?roff(?:\\s*;.*?)?\\s*-\\*-
42-
|
43-
44-
# Vim modeline
45-
(?:vim?|ex):\\s*(?:set?.*\\s)?(?:syntax|filetype|ft)=[gnt]?roff\\s?(?:.*:)?)
40+
# Modeline
41+
(?i:
42+
# Emacs
43+
-\\*-(?:\\s*(?=[^:;\\s]+\\s*-\\*-)|(?:.*?[;\\s]|(?<=-\\*-))mode\\s*:\\s*)
44+
[gnt]?roff
45+
(?=[\\s;]|(?<![-*])-\\*-).*?-\\*-
46+
47+
|
48+
49+
# Vim
50+
(?:(?:\\s|^)vi(?:m[<=>]?\\d+|m)?|\\sex)(?=:(?=\\s*set?\\s[^\\n:]+:)|:(?!\\s* set?\\s))(?:(?:\\s|\\s*:\\s*)\\w*(?:\\s*=(?:[^\\n\\\\\\s]|\\\\.)*)?)*[\\s:](?:filetype|ft|syntax)\\s*=
51+
[gnt]?roff
52+
(?=\\s|:|$)
53+
)
4654
"""
4755
patterns: [{ include: "#main" }]
4856

0 commit comments

Comments
 (0)