forked from SublimeText/LaTeXTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzathura_viewer.py
129 lines (102 loc) · 3.62 KB
/
zathura_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
from base_viewer import BaseViewer
from latextools_utils import get_setting
from latextools_utils.external_command import (
check_output, external_command
)
from latextools_utils.sublime_utils import get_sublime_exe
from latextools_utils.system import which
class ZathuraViewer(BaseViewer):
def _run_zathura(self, pdf_file):
return external_command([
'zathura',
self._get_synctex_editor(),
pdf_file
], use_texpath=False).pid
def _get_synctex_editor(self):
st_binary = get_sublime_exe()
if st_binary is None:
st_binary = get_setting('linux', {}).get('sublime', 'sublime_text')
return '--synctex-editor-command={0} %{{input}}:%{{line}}'.format(
st_binary
)
def _get_zathura_pid(self, pdf_file):
try:
running_apps = check_output(['ps', 'xv'], use_texpath=False)
for app in running_apps.splitlines():
if 'zathura' not in app:
continue
if pdf_file in app:
return app.lstrip().split(' ', 1)[0]
except:
pass
return None
def _focus_zathura(self, pid):
if which('xdotool') is not None:
try:
self._focus_xdotool(pid)
return
except:
pass
if which('wmctrl') is not None:
try:
self._focus_wmctrl(pid)
except:
pass
def _focus_wmctrl(self, pid):
window_id = None
try:
windows = check_output(
['wmctrl', '-l', '-p'], use_texpath=False
)
except:
pass
else:
pid = ' {0} '.format(pid)
for window in windows.splitlines():
if pid in window:
window_id = window.split(' ', 1)[0]
break
if window_id is None:
raise Exception('Cannot find window for Zathura')
external_command(['wmctrl', '-a', window_id, '-i'], use_texpath=False)
def _focus_xdotool(self, pid):
external_command(
['xdotool', 'search', '--pid', pid,
'--class', 'Zathura', 'windowactivate', '%2'],
use_texpath=False
)
def forward_sync(self, pdf_file, tex_file, line, col, **kwargs):
keep_focus = kwargs.pop('keep_focus', True)
# we check for the pid here as this is only necessary if
# we aren't otherwise creating the window
pid = self._get_zathura_pid(pdf_file)
if pid is None:
pid = self._run_zathura(pdf_file)
if keep_focus:
self.focus_st()
command = [
'zathura', '--synctex-forward',
'{line}:{col}:{tex_file}'.format(**locals()),
]
if pid is None:
command.append(self._get_synctex_editor())
else:
command.append('--synctex-pid={pid}'.format(pid=pid))
command.append(pdf_file)
external_command(command, use_texpath=False)
if pid is not None and not keep_focus:
self._focus_zathura(pid)
def view_file(self, pdf_file, **kwargs):
keep_focus = kwargs.pop('keep_focus', True)
pid = self._get_zathura_pid(pdf_file)
if pid is None:
pid = self._run_zathura(pdf_file)
if keep_focus:
self.focus_st()
elif not keep_focus:
self._focus_zathura(pid)
return pid
def supports_platform(self, platform):
return platform == 'linux'
def supports_keep_focus(self):
return True