forked from SublimeText/LaTeXTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumatra_viewer.py
138 lines (114 loc) · 4.04 KB
/
sumatra_viewer.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
from base_viewer import BaseViewer
from latextools_utils import get_setting
from latextools_utils.external_command import external_command
import os
import sublime
import sys
import traceback
if sys.version_info < (3, 0):
try:
import _winreg as winreg
except:
# not on Windows
pass
exec("""def reraise(tp, value, tb=None):
raise tp, value, tb
""")
else:
try:
import winreg
except:
# not on Windows
pass
def reraise(tp, value, tb=None):
if value is None:
value = tp()
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
raise value
class SumatraViewer(BaseViewer):
def __init__(self, *args, **kwargs):
super(SumatraViewer, self).__init__(*args, **kwargs)
def _find_sumatra_exe(self):
if hasattr(SumatraViewer, '_sumatra_exe'):
return SumatraViewer._sumatra_exe
# Sumatra's installer writes the location of the exe to the
# App Paths registry key, which we can access using the winreg
# module.
try:
with winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
'\\App Paths\\SumatraPDF.exe'
) as hndl:
SumatraViewer._sumatra_exe = winreg.QueryValue(hndl, '')
return SumatraViewer._sumatra_exe
except WindowsError:
pass
paths = [
os.path.expandvars("%PROGRAMFILES%\\SumatraPDF"),
os.path.expandvars("%ProgramW6432%\\SumatraPDF"),
os.path.expandvars("%PROGRAMFILES(x86)%\\SumatraPDF")
]
for path in paths:
if os.path.exists(path):
exe = os.path.join(path, 'SumatraPDF.exe')
if os.path.exists(exe):
SumatraViewer._sumatra_exe = exe
return exe
return None
def _run_with_sumatra_exe(self, commands):
def _no_binary():
message = (
'Could not find SumatraPDF.exe. '
'Please ensure the "sumatra" setting in your '
'LaTeXTools settings is set and points to the location '
'of Sumatra on your computer.'
)
def _error_msg():
sublime.error_message(message)
sublime.set_timeout(_error_msg, 1)
print(message)
# paranoia
if not isinstance(commands, list):
commands = [commands]
# favour 'sumatra' setting under viewer_settings if
# it exists, otherwise, use the platform setting
sumatra_binary = get_setting('viewer_settings', {}).\
get('sumatra', get_setting('windows', {}).
get('sumatra', 'SumatraPDF.exe')) or 'SumatraPDF.exe'
try:
external_command(
[sumatra_binary] + commands,
use_texpath=False, show_window=True
)
except OSError:
exc_info = sys.exc_info()
sumatra_exe = self._find_sumatra_exe()
if sumatra_exe is not None and sumatra_exe != sumatra_binary:
try:
external_command(
[sumatra_exe] + commands,
use_texpath=False, show_window=True
)
except OSError:
traceback.print_exc()
_no_binary()
return
else:
traceback.print_exception(*exc_info)
_no_binary()
return
def forward_sync(self, pdf_file, tex_file, line, col, **kwargs):
src_file = tex_file
self._run_with_sumatra_exe([
'-reuse-instance',
'-forward-search',
src_file,
str(line),
pdf_file
])
def view_file(self, pdf_file, **kwargs):
self._run_with_sumatra_exe(['-reuse-instance', pdf_file])
def supports_platform(self, platform):
return platform == 'windows'