Skip to content

Commit 8b18b49

Browse files
committed
Merge pull request #4 from noct/master
Various improvements
2 parents bdb5671 + 41efef4 commit 8b18b49

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

sublime_modelines.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
MODELINES_REG_SIZE = MAX_LINES_TO_CHECK * LINE_LENGTH
1212

1313

14-
def is_modeline(view, line):
15-
return bool(re.match(build_modeline_prefix(view), view.substr(line)))
14+
def is_modeline(prefix, line):
15+
return bool(re.match(prefix, line))
1616

1717

1818
def gen_modelines(view):
@@ -26,7 +26,10 @@ def gen_modelines(view):
2626
((view.size() - MODELINES_REG_SIZE), 0))[0]
2727
candidates += view.lines(sublime.Region(bottomRegStart, view.size()))
2828

29-
for modeline in (view.substr(c) for c in candidates if is_modeline(view, c)):
29+
prefix = build_modeline_prefix(view)
30+
modelines = (view.substr(c) for c in candidates if is_modeline(prefix, view.substr(c)))
31+
32+
for modeline in modelines:
3033
yield modeline
3134

3235

@@ -44,21 +47,27 @@ def gen_modeline_options(view):
4447
modelines = gen_modelines(view)
4548
for opt in gen_raw_options(modelines):
4649
name, sep, value = opt.partition(' ')
47-
yield view.settings().set, name, value
50+
yield view.settings().set, name.rstrip(':'), value.rstrip(';')
4851

4952

5053
def get_line_comment_char(view):
5154
commentChar = ""
55+
commentChar2 = ""
5256
try:
5357
for pair in view.meta_info("shellVariables", 0):
5458
if pair["name"] == "TM_COMMENT_START":
5559
commentChar = pair["value"]
60+
if pair["name"] == "TM_COMMENT_START_2":
61+
commentChar2 = pair["value"]
62+
if commentChar and commentChar2:
5663
break
5764
except TypeError:
5865
pass
5966

60-
return commentChar.strip()
61-
67+
if not commentChar2:
68+
return re.escape(commentChar.strip())
69+
else:
70+
return "("+re.escape(commentChar.strip())+"|"+re.escape(commentChar2.strip())+")"
6271

6372
def build_modeline_prefix(view):
6473
lineComment = get_line_comment_char(view).lstrip() or DEFAULT_LINE_COMMENT
@@ -91,11 +100,17 @@ class ExecuteSublimeTextModeLinesCommand(sublime_plugin.EventListener):
91100
MAX_LINES_TO_CHECK * LINE_LENGTH defines the size of the regions to be
92101
scanned.
93102
"""
94-
def on_load(self, view):
103+
def do_modelines(self, view):
95104
for setter, name, value in gen_modeline_options(view):
96105
try:
97106
setter(name, to_json_type(value))
98107
except ValueError, e:
99108
sublime.status_message("[SublimeModelines] Bad modeline detected.")
100109
print "[SublimeModelines] Bad option detected: %s, %s" % (name, value)
101-
print "[SublimeModelines] Tip: Keys cannot be empty strings."
110+
print "[SublimeModelines] Tip: Keys cannot be empty strings."
111+
112+
def on_load(self, view):
113+
self.do_modelines(view)
114+
115+
def on_post_save(self, view):
116+
self.do_modelines(view)

0 commit comments

Comments
 (0)