summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMicha? G?rny <mgorny@gentoo.org>2012-08-19 18:42:36 +0200
committerMicha? G?rny <mgorny@gentoo.org>2012-08-19 18:42:36 +0200
commitd9b7bd9b1a31b92c5dae2cbbd7db0aea93497b36 (patch)
tree1490830539344088f0918677a65ab95aa5034048
parent210d44add129caf389cc9b1c5c8850ed7e601643 (diff)
downloadpygments-d9b7bd9b1a31b92c5dae2cbbd7db0aea93497b36.tar.gz
Try vim modelines in guess_lexer().
-rw-r--r--pygments/lexers/__init__.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/pygments/lexers/__init__.py b/pygments/lexers/__init__.py
index eb8609d4..2f3bdbc4 100644
--- a/pygments/lexers/__init__.py
+++ b/pygments/lexers/__init__.py
@@ -15,6 +15,7 @@ import fnmatch
from os.path import basename
from pygments.lexers._mapping import LEXERS
+from pygments.modeline import get_filetype_from_buffer
from pygments.plugin import find_plugin_lexers
from pygments.util import ClassNotFound, bytes
@@ -197,6 +198,16 @@ def guess_lexer(_text, **options):
"""
Guess a lexer by strong distinctions in the text (eg, shebang).
"""
+
+ # try to get a vim modeline first
+ ft = get_filetype_from_buffer(_text)
+
+ if ft is not None:
+ try:
+ return get_lexer_by_name(ft, **options)
+ except ClassNotFound:
+ pass
+
best_lexer = [0.0, None]
for lexer in _iter_lexerclasses():
rv = lexer.analyse_text(_text)