-
Notifications
You must be signed in to change notification settings - Fork 367
/
latexEnvCloser.py
44 lines (39 loc) · 1.19 KB
/
latexEnvCloser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import sublime
import sublime_plugin
from .deprecated_command import deprecate
# Insert environment closer
# this only looks at the LAST \begin{...}
# need to extend to allow for nested \begin's
class LatextoolsLatexEnvCloserCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
view = self.view
pattern = r'\\(begin|end)\{[^\}]+\}'
b = []
currpoint = view.sel()[0].b
point = 0
r = view.find(pattern, point)
while r and r.end() <= currpoint:
be = view.substr(r)
point = r.end()
if "\\begin" == be[0:6]:
b.append(be[6:])
else:
if be[4:] == b[-1]:
b.pop()
else:
sublime.error_message(
"\\begin%s closed with %s on line %d" %
(b[-1], be, view.rowcol(point)[0]))
return
r = view.find(pattern, point)
# now either b = [] or b[-1] is unmatched
if b == []:
sublime.error_message("Every environment is closed")
else:
# note the double escaping of \end
print("now we insert")
# for some reason insert does not work
view.run_command(
"insert_snippet",
{'contents': "\\\\end" + b[-1] + "\n"})
deprecate(globals(), 'latex_env_closerCommand', LatextoolsLatexEnvCloserCommand)