From 068c8a341d40b6b8c18e5f8cf7ee1fa08fc0c165 Mon Sep 17 00:00:00 2001 From: Michael Tesch Date: Sat, 14 Mar 2015 18:35:57 +0100 Subject: [PATCH] better regex for matching emacs modeline the emacs modeline is actually a per-file variable setting mechanism, which means it can have other flags in it. this regex extracts the part that corresponds to the file's language ("mode:" - ie emacs major mode) http://ergoemacs.org/emacs_manual/emacs/Specifying-File-Variables.html --- lib/linguist/strategy/modeline.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/linguist/strategy/modeline.rb b/lib/linguist/strategy/modeline.rb index e77597f4..ded1102c 100644 --- a/lib/linguist/strategy/modeline.rb +++ b/lib/linguist/strategy/modeline.rb @@ -1,7 +1,7 @@ module Linguist module Strategy class Modeline - EmacsModeline = /-\*-\s*mode:\s*(\w+);?\s*-\*-/i + EmacsModeline = /-\*-\s*((?!mode)[\w-]+\s*:\s*([\w+-]+)\s*;?\s*)?(mode\s*:)?\s*([\w+-]+)\s*;?\s*((?!mode)[\w-]+\s*:\s*([\w+-]+)\s*;?\s*)?-\*-/i VimModeline = /\/\*\s*vim:\s*set\s*(?:ft|filetype)=(\w+):\s*\*\//i # Public: Detects language based on Vim and Emacs modelines @@ -22,8 +22,13 @@ module Linguist # # Returns a String or nil def self.modeline(data) - match = data.match(EmacsModeline) || data.match(VimModeline) - match[1] if match + match = data.match(EmacsModeline) + if match + match[4] + else + match = data.match(VimModeline) + match[1] if match + end end end end