forked from SublimeText/LaTeXTools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
latexCommand.py
28 lines (23 loc) · 958 Bytes
/
latexCommand.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
import sublime, sublime_plugin
import re
# Insert LaTeX command based on current word
# Position cursor inside braces
class latexcmdCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
view = self.view
# Workaround: env* and friends trip ST2 up because * is a word boundary,
# so we search for a word boundary
# Code is similar to latex_cite_completions.py (should prbly factor out)
point = view.sel()[0].b
line = view.substr(sublime.Region(view.line(point).a, point))
line = line[::-1]
rex = re.compile(r"([^\s\{]*)\s?\{?")
expr = re.match(rex, line)
if expr:
command = expr.group(1)[::-1]
command_region = sublime.Region(point-len(command),point)
view.erase(edit, command_region)
snippet = "\\\\" + command + "{$1} $0"
view.run_command("insert_snippet", {'contents': snippet})
else:
sublime.status_message("LATEXTOOLS INTERNAL ERROR: could not find command to expand")