ThirdParty/Pygments/pygments/modeline.py

changeset 4172
4f20dba37ab6
child 4697
c2e9bf425554
equal deleted inserted replaced
4170:8bc578136279 4172:4f20dba37ab6
1 # -*- coding: utf-8 -*-
2 """
3 pygments.modeline
4 ~~~~~~~~~~~~~~~~~
5
6 A simple modeline parser (based on pymodeline).
7
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 import re
13
14 __all__ = ['get_filetype_from_buffer']
15
16 modeline_re = re.compile(r'''
17 (?: vi | vim | ex ) (?: [<=>]? \d* )? :
18 .* (?: ft | filetype | syn | syntax ) = ( [^:\s]+ )
19 ''', re.VERBOSE)
20
21 def get_filetype_from_line(l):
22 m = modeline_re.search(l)
23 if m:
24 return m.group(1)
25
26 def get_filetype_from_buffer(buf, max_lines=5):
27 """
28 Scan the buffer for modelines and return filetype if one is found.
29 """
30 lines = buf.splitlines()
31 for l in lines[-1:-max_lines-1:-1]:
32 ret = get_filetype_from_line(l)
33 if ret:
34 return ret
35 for l in lines[max_lines:0:-1]:
36 ret = get_filetype_from_line(l)
37 if ret:
38 return ret
39
40 return None

eric ide

mercurial