]> git.donarmstrong.com Git - lilypond.git/blob - scripts/lilypond-pdfpc-helper.py
cfd086fe2fac259be428994a3e861c59b4f503ef
[lilypond.git] / scripts / lilypond-pdfpc-helper.py
1 #!@PYTHON@
2 import re
3 import getopt
4 import sys
5 import os
6 import string
7
8 version = '@TOPLEVEL_VERSION@'
9
10 localedir = '@localedir@'
11 try:
12         import gettext
13         gettext.bindtextdomain ('lilypond', localedir)
14         gettext.textdomain ('lilypond')
15         _ = gettext.gettext
16 except:
17         def _ (s):
18                 return s
19         
20 program_name = os.path.basename (sys.argv[0])
21 print os.getcwd()
22 def usage():
23         sys.stdout.write ('Usage: %s FILE:LINE:COLUMN' % program_name)
24         sys.stdout.write ('\n\n')
25         sys.stdout.write ('''PDF Point & Click helper.
26 Start a text-editor when given textedit:///path/to/file:line:column URI.''')
27         sys.stdout.write ('\n\n')
28         sys.stdout.write (_ ("Report bugs to %s.") % "bug-lilypond@gnu.org")
29         sys.stdout.write ('\n')
30
31 def print_version ():
32         sys.stdout.write (program_id ())
33         sys.stdout.write ('\n')
34         sys.stdout.write (_ ("""\
35 This program is free software.  It is covered by the GNU General Public
36 License and you are welcome to change it and/or distribute copies of it
37 under certain conditions.  Invoke as `%s --warranty' for more
38 information.
39 """) % "lilypond")
40
41 def program_id ():
42         return '%s (GNU LilyPond) %s' % (program_name, version)
43
44 ################################################################
45 (options, files) = getopt.getopt (sys.argv[1:], 'hv', ['help','version'])
46
47 for opt in options:
48         o = opt[0]
49         a = opt[1]
50         if o == '--help' or o == '-h':
51                 usage ()
52                 sys.exit (0)
53         if o == '--version' or o == '-v':
54                 print_version ()
55                 sys.exit (0)
56
57
58 if not files:
59         usage()
60         sys.exit (1)
61         
62
63 ################################################################
64
65
66 arg = files[0]
67
68 arg = re.sub ('textedit://','', arg)
69
70 match = re.match ('([^:]+):([^:]+):(.*)', arg)
71 if not match:
72         sys.stderr.write (_("Not in FILE:LINE:COL format: ")
73                           + files[0])
74         sys.exit (1)
75
76 (file, line, column) = tuple (match.groups())
77
78
79 editor = ''
80 try:
81         editor = os.environ['EDITOR']
82 except KeyError:
83         pass
84 ly_pc_editor = None
85 try:
86         ly_pc_editor = os.environ['LYEDITOR']
87 except KeyError:
88         pass
89
90
91 if ly_pc_editor == None:
92         if  re.search ("emacs", editor):
93                 ly_pc_editor = 'emacsclient --no-wait +%(line)s:%(column)s %(file)s'
94         elif re.search ('gvim', editor):
95                 ly_pc_editor = 'gvim --remote +:%(line)s:norm%(column)s %(file)s'
96         elif re.search ('nedit', editor):
97                 ly_pc_editor = 'nc -noask +%(line)s %(file)s'
98
99 if not ly_pc_editor:
100         sys.stderr.write ("Need to set EDITOR or LYEDITOR")
101         sys.exit (1)
102
103 command = ly_pc_editor % vars()
104
105 print 'invoked with %s\ncalling %s\n' % (string.join (sys.argv),command)
106
107 status = os.system (command)
108 if status:
109         sys.stderr.write (_("Command failed: `%s' (status %d)") % (command, status) + '\n')
110
111
112