/
M4Expand.py
40 lines (33 loc) · 1.18 KB
/
M4Expand.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
import sublime_plugin, os, subprocess
from tempfile import NamedTemporaryFile
class M4Expand(sublime_plugin.WindowCommand):
def run(self):
active_view = self.window.active_view()
text = "\n\n".join(getSelectedText(active_view)).strip()
tf = NamedTemporaryFile(mode="w", delete=False)
try:
tf.write(text)
tf.close()
res = subprocess.check_output(["m4", tf.name],
stderr=subprocess.STDOUT,
cwd=os.path.dirname(os.path.abspath(active_view.file_name())))
res = res.decode('utf-8').replace('\r', '').strip()
panel_name = "m4expand.results"
panel = self.window.create_output_panel(panel_name)
self.window.run_command("show_panel", {"panel": "output." + panel_name})
panel.set_read_only(False)
panel.set_syntax_file(active_view.settings().get("syntax"))
panel.run_command("append", {"characters": res})
panel.set_read_only(True)
except Exception as e:
print("M4Expand - An error occurred: ", e)
finally:
os.unlink(tf.name)
def getSelectedText(view):
text = []
for s in view.sel():
if s.empty():
text.append(view.substr(view.line(s)))
else:
text.append(view.substr(s))
return text