-
Notifications
You must be signed in to change notification settings - Fork 367
/
jumpToPDF.py
272 lines (224 loc) · 7.35 KB
/
jumpToPDF.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import sublime
import sublime_plugin
import os
import traceback
import re
from . import getTeXRoot
from .latextools_utils.is_tex_file import is_tex_file
from .latextools_utils import get_setting
from .latextools_utils.output_directory import (
get_output_directory, get_jobname
)
from .latextools_utils.sublime_utils import focus_st
from .latextools_plugin import (
get_plugin, add_plugin_path, NoSuchPluginException,
add_whitelist_module
)
from .deprecated_command import deprecate
SUBLIME_VERSION = re.compile(r'Build (\d{4})', re.UNICODE)
DEFAULT_VIEWERS = {
'linux': 'evince',
'osx': 'skim',
'windows': 'sumatra'
}
class NoViewerException(Exception):
pass
# common viewer logic
def get_viewer():
default_viewer = DEFAULT_VIEWERS.get(sublime.platform(), None)
viewer_name = get_setting('viewer', default_viewer)
if viewer_name in ['', 'default']:
viewer_name = default_viewer
if viewer_name is None:
sublime.error_message(
'No viewer could be found for your platform. '
'Please configure the "viewer" setting in your LaTeXTools '
'Preferences')
raise NoViewerException()
try:
viewer = get_plugin(viewer_name + '_viewer')
except NoSuchPluginException:
try:
viewer = get_plugin(viewer_name)
except NoSuchPluginException:
sublime.error_message(
'Cannot find viewer ' + viewer_name + '.\n' +
'Please check your LaTeXTools Preferences.')
raise NoViewerException()
print(repr(viewer))
# assume no-args constructor
viewer = viewer()
if not viewer.supports_platform(sublime.platform()):
sublime.error_message(
viewer_name + ' does not support the current platform. '
'Please change the viewer in your LaTeXTools Preferences.')
raise NoViewerException()
return viewer
# Jump to current line in PDF file
# NOTE: must be called with {"from_keybinding": <boolean>} as arg
class LatextoolsJumpToPdfCommand(sublime_plugin.TextCommand):
def is_visible(self, *args):
view = sublime.active_window().active_view()
return bool(view.score_selector(0, "text.tex"))
def run(self, edit, **args):
# Check prefs for PDF focus and sync
keep_focus = args.get('keep_focus', get_setting('keep_focus', True))
forward_sync = args.get('forward_sync', get_setting('forward_sync', True))
# If invoked from keybinding, we sync
# Rationale: if the user invokes the jump command, s/he wants to see
# the result of the compilation.
# If the PDF viewer window is already visible, s/he probably wants to
# sync, or s/he would have no need to invoke the command. And if it is
# not visible, the natural way to just bring up the window without
# syncing is by using the system's window management shortcuts.
# As for focusing, we honor the toggles / prefs.
from_keybinding = args.pop("from_keybinding", False)
if from_keybinding:
forward_sync = True
print(from_keybinding, keep_focus, forward_sync)
view = self.view
file_name = view.file_name()
if not is_tex_file(file_name):
if from_keybinding:
sublime.error_message(
"%s is not a TeX source file: cannot jump." %
(os.path.basename(file_name),))
return
# If not invoked from keybinding, it is invoked by the
# compilation process. Then, we should go forward with
# viewing the newly compiled PDF, but not try to sync
# forward (since that would be impossible).
else:
forward_sync = False
root = getTeXRoot.get_tex_root(view)
file_name = get_jobname(view)
output_directory = get_output_directory(view)
if output_directory is None:
pdffile = os.path.join(
os.path.dirname(root),
file_name + u'.pdf'
)
else:
pdffile = os.path.join(
output_directory,
file_name + u'.pdf'
)
if not os.path.exists(pdffile):
pdffile = os.path.join(
os.path.dirname(root),
file_name + u'.pdf'
)
if not os.path.exists(pdffile):
print("Expected PDF file {0} not found".format(pdffile))
return
pdffile = os.path.realpath(pdffile)
(line, col) = self.view.rowcol(self.view.sel()[0].end())
print("Jump to: ", line, col)
# column is actually ignored up to 0.94
# HACK? It seems we get better results incrementing line
line += 1
# issue #625: we need to pass the path to the file to the viewer when
# there are files in subfolders of the main folder.
# Thanks rstein and arahlin for this code!
srcfile = self.view.file_name()
try:
viewer = get_viewer()
except NoViewerException:
return
if forward_sync:
try:
viewer.forward_sync(pdffile, srcfile, line, col, keep_focus=keep_focus)
except (AttributeError, NotImplementedError):
try:
viewer.view_file(pdffile, keep_focus=keep_focus)
except (AttributeError, NotImplementedError):
traceback.print_exc()
sublime.error_message(
'Your viewer does not appear to be a proper'
'LaTeXTools viewer plugin. '
'Please contact the plugin author.')
return
else:
try:
viewer.view_file(pdffile, keep_focus=keep_focus)
except (AttributeError, NotImplementedError):
traceback.print_exc()
sublime.error_message(
'Your viewer does not appear to be a proper'
'LaTeXTools viewer plugin. '
'Please contact the plugin author.')
return
if keep_focus:
try:
if viewer.supports_keep_focus():
return
except (AttributeError, NotImplementedError):
pass
focus_st()
class LatextoolsViewPdfCommand(sublime_plugin.WindowCommand):
def is_visible(self, *args):
view = self.window.active_view()
return bool(view.score_selector(0, "text.tex"))
def run(self, **args):
pdffile = None
if 'file' in args:
pdffile = args.pop('file', None)
else:
view = self.window.active_view()
root = getTeXRoot.get_tex_root(view)
file_name = get_jobname(view)
output_directory = get_output_directory(view)
if output_directory is None:
root = getTeXRoot.get_tex_root(view)
pdffile = os.path.join(
os.path.dirname(root),
file_name + u'.pdf'
)
else:
pdffile = os.path.join(
output_directory,
file_name + u'.pdf'
)
if not os.path.exists(pdffile):
pdffile = os.path.join(
os.path.dirname(root),
file_name + u'.pdf'
)
pdffile = os.path.normpath(pdffile)
if not os.path.exists(pdffile):
print("Expected PDF file {0} not found".format(pdffile))
return
pdffile = os.path.realpath(pdffile)
# since we potentially accept an argument, add some extra
# safety checks
if pdffile is None:
print('No PDF file found.')
return
elif not os.path.exists(pdffile):
print(u'PDF file "{0}" does not exist.'.format(pdffile))
sublime.error_message(
u'PDF file "{0}" does not exist.'.format(pdffile)
)
return
try:
viewer = get_viewer()
except NoViewerException:
return
try:
viewer.view_file(pdffile, keep_focus=False)
except (AttributeError, NotImplementedError):
traceback.print_exception()
sublime.error_message(
'Your viewer does not appear to be a proper'
'LaTeXTools viewer plugin. '
'Please contact the plugin author.')
return
def plugin_loaded():
add_whitelist_module('latextools_utils')
viewers_path = os.path.join(sublime.packages_path(), 'LaTeXTools', 'viewers')
# ensure that base_viewer is loaded first so that other viewers are registered
# as plugins
add_plugin_path(os.path.join(viewers_path, 'base_viewer.py'))
add_plugin_path(viewers_path)
deprecate(globals(), 'JumpToPdf', LatextoolsJumpToPdfCommand)
deprecate(globals(), 'ViewPdf', LatextoolsViewPdfCommand)