forked from randy3k/LaTeXBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ac.py
executable file
·145 lines (119 loc) · 5.53 KB
/
ac.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import sublime, sublime_plugin
import os
import re
from . misc import *
# sublime wrapper for replacement
class LatexPlusReplaceCommand(sublime_plugin.TextCommand):
def run(self, edit, a, b, replacement):
region = sublime.Region(a, b)
self.view.replace(edit, region, replacement)
self.view.sel().clear()
self.view.sel().add(a+len(replacement))
class LatexPlusAcCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
point = view.sel()[0].end() if view.sel() else 0
if not view.score_selector(point, "text.tex.latex"): return
contentb = view.substr(sublime.Region(view.line(point).begin(), point))
content = view.substr(view.line(point))
m = re.match(r".*\\(?:eq|page)*ref(\{([a-zA-Z0-9_:-]*))?$", contentb)
if m: self.dispatch_ref(m, point); return
m = re.match(r".*\\cite(?:[a-zA-Z_*]*)(\{(?:[a-zA-Z0-9_:-]*\s*,\s*)*([a-zA-Z0-9_:-]*))?$", contentb)
if m: self.dispatch_cite(m, point); return
m = re.match(r".*\\label(\{([a-zA-Z0-9_:-]*))?$", contentb)
if m: self.dispatch_label(m, point); return
m = re.match(r".*\\includegraphics((?:\[[\]]*\])?\{([^\}]*))?$", contentb)
ext = ['.jpg', '.jpeg', '.bmp', '.pdf', '.ps', '.eps']
if m: self.dispatch_listdir(m, point, ext); return
m = re.match(r".*\\(?:input|include)(\{([^\}]*))?$", contentb)
ext = ['.tex']
if m: self.dispatch_listdir(m, point, ext); return
m = re.match(r".*\\bibliography(\{([^\}]*))?$", contentb)
ext = ['.bib']
if m: self.dispatch_listdir(m, point, ext); return
if re.match(r"^\s*$", content):
self.dispatch_closeenv(point); return
sublime.status_message("Nothing to be auto completed.")
def replace(self, i, completions, braces, a, b):
if i<0: return
open_brace = "" if braces else "{"
close_brace = "" if braces else "}"
rept = open_brace + completions[i] + close_brace
self.view.run_command("latex_plus_replace", {"a": a, "b": b, "replacement": rept})
def dispatch_ref(self, m, point):
print("dispatching ref")
view = self.view
tex_root = get_tex_root(view)
tex_dir = os.path.dirname(tex_root)
braces, prefix = m.groups()
results = search_in_tex(r'\\label\{([^\{\}]+)\}', tex_root)
if prefix:
results = [r for r in results if prefix in r['result']]
else:
prefix = ""
if not results:
sublime.status_message("No label matches %s!" % (prefix,))
return
display = [[r['result'], os.path.relpath(r['file'], tex_dir)+":"+str(r['line'])] for r in results]
on_done = lambda i: self.replace(i, [r['result'] for r in results], braces, point - len(prefix), point)
view.window().show_quick_panel(display, on_done)
def dispatch_cite(self, m, point):
print("dispatching cite")
view = self.view
tex_root = get_tex_root(view)
braces, prefix = m.groups()
results = find_bib_records(tex_root, by = 'author')
if prefix:
results = [r for r in results \
if prefix.lower() in ("%s %s %s" % (r['keyword'],r['title'], r['author'])).lower()]
else:
prefix = ""
if not results:
sublime.status_message("No bib record matches %s!" % (prefix,))
return
display = [[ "[" + r['author'] + "] " + r['title'], " (" + r['keyword'] + ") " + r['title'] ] for r in results]
on_done = lambda i: self.replace(i, [r['keyword'] for r in results], braces, point - len(prefix), point)
view.window().show_quick_panel(display, on_done)
def dispatch_label(self, m, point):
print("dispatching label")
view = self.view
tex_root = get_tex_root(view)
tex_dir = os.path.dirname(tex_root)
braces, prefix = m.groups()
print(prefix)
def dispatch_listdir(self, m, point, ext):
print("dispatching listdir")
view = self.view
tex_root = get_tex_root(view)
tex_dir = os.path.dirname(tex_root)
braces, prefix = m.groups()
if not prefix: prefix = ""
dir = os.path.join(tex_dir, os.path.dirname(prefix))
base = os.path.basename(prefix)
def on_done(target):
target_dir = (os.path.splitext(os.path.relpath(target, tex_dir))[0]).replace(os.sep, '/')
self.replace(0, [target_dir], braces, point - len(prefix), point)
listdir(view, dir, base, ext, on_done)
def dispatch_closeenv(self, point):
print("dispatching close env")
view = self.view
pt = 0
env = []
while 1:
r = view.find(r'\\(begin|end)\{[^\}]+\}', pt)
pt = r.end()
if view.scope_name(pt-1).find("comment")>=0:
continue
if pt >= point: break
thisenv = re.match(r'\\(begin|end)\{([^\}]+)\}', view.substr(r)).groups()
if thisenv[0] == 'begin':
env.append(thisenv)
elif thisenv[1] == env[-1][1]:
env.pop()
else:
sublime.error_message('Line %d: {%s} closed with {%s}.' %\
(view.rowcol(pt)[0]+1, env[-1][1], thisenv[1]))
view.run_command("insert_snippet", {'contents': "\\\\end{" + env[-1][1] + "}"})
if view.settings().get('auto_indent'):
view.run_command('reindent', {'force_indent': False})
view.run_command("insert_snippet", {'contents': "\n"})