Skip to content

Commit

Permalink
Rewrite patterns for matching Vim/Emacs modelines
Browse files Browse the repository at this point in the history
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"
  • Loading branch information
Alhadis committed Sep 17, 2016
1 parent f37fb6b commit abffa58
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions grammars/roff.cson
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ firstLineMatch: """(?x)
^'\\\\\"\\x20[tre]+(?=\\s|$)
|
# Emacs modeline
(?i:-\\*-[^*]*(?:mode:\\s*)?\\b[gnt]?roff(?:\\s*;.*?)?\\s*-\\*-
|
# Vim modeline
(?:vim?|ex):\\s*(?:set?.*\\s)?(?:syntax|filetype|ft)=[gnt]?roff\\s?(?:.*:)?)
# Modeline
(?i:
# Emacs
-\\*-(?:\\s*(?=[^:;\\s]+\\s*-\\*-)|(?:.*?[;\\s]|(?<=-\\*-))mode\\s*:\\s*)
[gnt]?roff
(?=[\\s;]|(?<![-*])-\\*-).*?-\\*-
|
# Vim
(?:(?:\\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*=
[gnt]?roff
(?=\\s|:|$)
)
"""
patterns: [{ include: "#main" }]

Expand Down

0 comments on commit abffa58

Please sign in to comment.