forked from krlmlr/evince_eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evince_backward_search.py
executable file
·173 lines (142 loc) · 6.29 KB
/
evince_backward_search.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2010 Jose Aliste <[email protected]>
# 2011 Benjamin Kellermann <[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
import dbus, subprocess, time, re, sys, os.path
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 = None, on_closed = None):
self._log = logger
self.uri = uri
self.editor = editor
self.status = CLOSED
self.source_handler = None
self.dbus_name = ''
self.on_closed = on_closed
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()
except dbus.DBusException:
if self._log:
self._log.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 = False
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):
if self._log:
self._log.debug("FindDocument DBus call has failed")
def handle_find_document_reply(self, evince_name):
if self._handler is not None:
handler = self._handler
else:
handler = self.handle_get_window_list_reply
if evince_name != '':
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):
if self._log:
self._log.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)
if self.on_closed:
self.window.connect_to_signal("Closed", self.on_closed)
else:
#That should never happen.
if self._log:
self._log.debug("GetWindowList returned empty list")
def on_sync_source(self, input_file, source_link, *args, **kwargs):
m = re.match("^file\://+(/.*)$", input_file)
if m is not None:
input_file = m.group(1)
print input_file + ":" + str(source_link[0])
sys.stdout.flush()
if self.editor:
cmd = re.sub("%f",input_file,self.editor)
cmd = re.sub("%l",str(source_link[0]), cmd)
print cmd
subprocess.call(cmd, shell=True)
if self.source_handler is not None:
self.source_handler(input_file, source_link, *args, **kwargs)
## This file offers backward search in any editor.
## evince_dbus pdf_file line_source input_file
if __name__ == '__main__':
import dbus.mainloop.glib, sys, os
def print_usage():
print """Usage:
evince_backward_search pdf_file ["editorcmd %f %l"]
%f ... TeX-file to load
%l ... line to jump to
E.g.:
evince_backward_search somepdf.pdf "gvim --servername somepdf --remote-silent '+%l<Enter>' %f"
evince_backward_search somepdf.pdf "emacsclient -a emacs --no-wait +%l %f"
evince_backward_search somepdf.pdf "scite %f '-goto:%l'"
evince_backward_search somepdf.pdf "lyxclient -g %f %l"
evince_backward_search somepdf.pdf "kate --use --line %l"
evince_backward_search somepdf.pdf "kile --line %l"
If no command is given, 'file:line' is printed to stdout for each reverse search, e.g., for
integration into eclipse.
"""
sys.exit(1)
if len(sys.argv) < 2:
print_usage()
pdf_file = os.path.abspath(sys.argv[1])
editor_cmd = None
if len(sys.argv >= 3):
editor_cmd = sys.argv[2]
if not os.path.isfile(pdf_file):
print_usage()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
run(pdf_file, editor_cmd)
def run(pdf_file, editor_cmd):
import gobject
def on_closed():
loop.quit()
a = EvinceWindowProxy('file://' + pdf_file, editor_cmd, True, on_closed = on_closed)
loop = gobject.MainLoop()
loop.run()
# ex:ts=4:et: