forked from gauteh/vim-evince-synctex
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevince_synctex.py
executable file
·217 lines (178 loc) · 7.3 KB
/
evince_synctex.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
#!/usr/bin/python3
# Copyright (C) 2010 Jose Aliste <[email protected]>
# 2011 Benjamin Kellermann <[email protected]>
# 2018 Mathias Rav <[email protected]>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public Licence as published by the Free Software
# Foundation; either version 2 of the Licence, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public Licence for more
# details.
#
# You should have received a copy of the GNU General Public Licence along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
Run Evince in SyncTeX mode while continuously building a TeX file.
"""
import os
import re
import sys
import dbus
import shlex
import logging
import argparse
import subprocess
import urllib.parse
CONSOLE_SCRIPTS = [
'latexedit = evince_synctex:latexedit',
'evince-synctex = evince_synctex:main_parse',
]
parser = argparse.ArgumentParser(
description=__doc__.lstrip(),
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument('-s', '--build-source', metavar='FILE',
dest='source_file',
help='Run latexmk to continuously build FILE')
parser.add_argument('-v', '--view-file', metavar='FILE', required=True,
dest='pdf_file',
help='Open Evince on FILE in synctex mode')
parser.add_argument('cmdline', nargs='+',
help='Run command upon Ctrl+Click in Evince')
latexedit_parser = argparse.ArgumentParser()
latexedit_parser.add_argument('source_file')
latexedit_cmdline = ['gnome-terminal', '--window', '--', 'vim', '%f', '+%l', '+norm zz']
RUNNING, CLOSED = range(2)
EV_DAEMON_PATH = "/org/gnome/evince/Daemon"
EV_DAEMON_NAME = "org.gnome.evince.Daemon"
EV_DAEMON_IFACE = "org.gnome.evince.Daemon"
EVINCE_PATH = "/org/gnome/evince/Evince"
EVINCE_IFACE = "org.gnome.evince.Application"
EV_WINDOW_IFACE = "org.gnome.evince.Window"
class EvinceWindowProxy:
"""A DBUS proxy for an Evince Window."""
daemon = None
bus = None
def __init__(self, uri, editor, logger):
assert uri is not None
assert editor is not None
assert logger is not None
self.logger = logger
self.uri = uri
self.editor = editor
self.status = CLOSED
self.dbus_name = ''
self._handler = None
try:
if EvinceWindowProxy.bus is None:
EvinceWindowProxy.bus = dbus.SessionBus()
if EvinceWindowProxy.daemon is None:
EvinceWindowProxy.daemon = EvinceWindowProxy.bus.get_object(
EV_DAEMON_NAME,
EV_DAEMON_PATH,
follow_name_owner_changes=True)
EvinceWindowProxy.bus.add_signal_receiver(
self._on_doc_loaded,
signal_name="DocumentLoaded",
dbus_interface=EV_WINDOW_IFACE,
sender_keyword='sender')
self._get_dbus_name(False)
except dbus.DBusException:
self.logger.debug("Could not connect to the Evince Daemon")
def _on_doc_loaded(self, uri, **keyargs):
if uri == self.uri and self._handler is None:
self.handle_find_document_reply(keyargs['sender'])
def _get_dbus_name(self, spawn):
EvinceWindowProxy.daemon.FindDocument(
self.uri, spawn,
reply_handler=self.handle_find_document_reply,
error_handler=self.handle_find_document_error,
dbus_interface=EV_DAEMON_IFACE)
def handle_find_document_error(self, error):
self.logger.debug("FindDocument DBus call has failed")
def handle_find_document_reply(self, evince_name):
if evince_name == '':
self.logger.debug("Did not find an Evince with our document")
return
self.logger.debug("Found Evince with our document: %r", evince_name)
if self._handler is not None:
handler = self._handler
else:
handler = self.handle_get_window_list_reply
self.dbus_name = evince_name
self.status = RUNNING
self.evince = EvinceWindowProxy.bus.get_object(
self.dbus_name, EVINCE_PATH)
self.evince.GetWindowList(
dbus_interface=EVINCE_IFACE,
reply_handler=handler,
error_handler=self.handle_get_window_list_error)
def handle_get_window_list_error(self, e):
self.logger.debug("GetWindowList DBus call has failed")
def handle_get_window_list_reply(self, window_list):
if len(window_list) > 0:
window_obj = EvinceWindowProxy.bus.get_object(
self.dbus_name, window_list[0])
self.window = dbus.Interface(window_obj, EV_WINDOW_IFACE)
self.window.connect_to_signal("SyncSource", self.on_sync_source)
else:
self.logger.debug("GetWindowList returned empty list")
def on_sync_source(self, input_file, source_link, timestamp):
path = urllib.parse.unquote(input_file.split("file://")[1])
line = source_link[0]
self.logger.debug("Go to %s:%s", path, line)
cmd = re.sub("%f", shlex.quote(path), self.editor)
cmd = re.sub("%l", str(line), cmd)
subprocess.call(cmd, shell=True)
def main(source_file=None, pdf_file=None, cmdline=None,
configure_logging=True):
logger = logging.getLogger('evince_synctex')
if configure_logging:
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
if not source_file and not pdf_file:
raise ValueError('Either source_file or pdf_file must be provided')
if not pdf_file:
pdf_file = os.path.splitext(source_file)[0] + '.pdf'
if not cmdline:
cmdline = 'gvim %f +%l'.split()
import dbus.mainloop.glib
from gi.repository import GObject as gobject
pdf_url = 'file://%s' % (
urllib.parse.quote(os.path.abspath(pdf_file),
safe="%/:=&?~#+!$,;'@()*[]"))
cmdline_string = ' '.join(map(shlex.quote, cmdline))
build_source_process = None
if source_file:
subprocess.check_call(
('latexmk', '--synctex=1', '-pdf', source_file))
build_source_process = subprocess.Popen(
('latexmk', '--synctex=1', '-pvc', '-view=none', '-pdf',
source_file))
view_process = subprocess.Popen(('evince', pdf_file))
try:
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
EvinceWindowProxy.instance = EvinceWindowProxy(
pdf_url, cmdline_string, logger)
try:
gobject.MainLoop().run()
except KeyboardInterrupt:
pass
del EvinceWindowProxy.instance
finally:
if build_source_process:
build_source_process.terminate()
build_source_process.wait()
view_process.terminate()
view_process.wait()
def latexedit():
main(cmdline=latexedit_cmdline, **vars(latexedit_parser.parse_args()))
def main_parse():
main(**vars(parser.parse_args()))
if __name__ == '__main__':
main_parse()