]> git.lyx.org Git - lyx.git/blob - 3rdparty/evince_sync/evince_forward_search
Some minor polishment and comments.
[lyx.git] / 3rdparty / evince_sync / evince_forward_search
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (C) 2010 Jose Aliste <jose.aliste@gmail.com>
5 #               2011 Benjamin Kellermann <Benjamin.Kellermann@tu-dresden.de>
6 #
7 # This program is free software; you can redistribute it and/or modify it under
8 # the terms of the GNU General Public Licence as published by the Free Software
9 # Foundation; either version 2 of the Licence, or (at your option) any later
10 # version.
11 #
12 # This program is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE.  See the GNU General Public Licence for more 
15 # details.
16 #
17 # You should have received a copy of the GNU General Public Licence along with
18 # this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
19 # Street, Fifth Floor, Boston, MA  02110-1301, USA
20
21 # This file offers forward search for evince.
22 from __future__ import print_function
23
24 import dbus
25 import sys
26 import os
27 import traceback
28
29 if sys.version_info < (3,):
30     from urllib import quote
31 else:
32     from urllib.parse import quote
33
34 if __name__ == '__main__':
35     def print_usage():
36         print('Usage: evince_forward_search pdf_file line_number tex_file')
37         sys.exit(1)
38
39     if len(sys.argv) != 4:
40         print_usage()
41     try:
42         line_number = int(sys.argv[2])
43     except ValueError:
44         print_usage()
45
46     pdf_file = os.path.abspath(sys.argv[1])
47     line = int(sys.argv[2])
48     tex_file = os.path.abspath(sys.argv[3])
49
50     try:
51         bus = dbus.SessionBus()
52         daemon = bus.get_object(
53             'org.gnome.evince.Daemon', '/org/gnome/evince/Daemon'
54         )
55         dbus_name = daemon.FindDocument(
56             # The PDF file name MUST be URI-encoded
57             # RFC 1738: unreserved = alpha | digit | safe | extra
58             #           safe       = "$" | "-" | "_" | "." | "+"
59             #           extra      = "!" | "*" | "'" | "(" | ")" | ","
60             'file://' + quote(pdf_file, "/$+!*'(),@=~"),
61             True,
62             dbus_interface="org.gnome.evince.Daemon"
63         )
64         window = bus.get_object(dbus_name, '/org/gnome/evince/Window/0')
65         window.SyncView(
66             tex_file,
67             (line_number, 1),
68             0,  # GDK_CURRENT_TIME constant
69             dbus_interface="org.gnome.evince.Window"
70         )
71     except dbus.DBusException:
72         traceback.print_exc()