4 ;;;; lilypond-invoke-editor.scm -- Invoke an editor in file:line:column mode
6 ;;;; source file of the GNU LilyPond music typesetter
8 ;;;; (c) 2005--2006 Jan Nieuwenhuizen <janneke@gnu.org>
11 ;; (define (exit x) (system "sleep 10"))
19 (define PROGRAM-NAME "lilypond-invoke-editor")
20 (define TOPLEVEL-VERSION "@TOPLEVEL_VERSION@")
21 (define DATADIR "@datadir@")
22 (define COMPILE-TIME-PREFIX
23 (format #f "~a/lilypond/~a" DATADIR TOPLEVEL-VERSION))
25 ;; argv0 relocation -- do in wrapper?
28 (define LILYPONDPREFIX
30 (or (getenv "LILYPONDPREFIX")
31 (dirname (dirname (car (command-line)))))))
34 (if (eq? prefix (dirname DATADIR)) COMPILE-TIME-PREFIX
35 (format #f "~a/share/lilypond/~a"
36 prefix TOPLEVEL-VERSION))))
40 ;; gettext wrapper for guile < 1.7.2
41 (if (defined? 'gettext)
42 (define-public _ gettext)
43 (define-public (_ x) x))
45 (define (show-version port)
46 (format port "~a (GNU LilyPond) ~a \n" PROGRAM-NAME TOPLEVEL-VERSION))
48 (define (show-help port)
49 (format port (_ "Usage: lilypond-invoke-editor [textedit://]FILE:LINE:CHAR:COLUMN
51 Visit a file and position the cursor.
54 -h, --help show this help
55 -v, --version show version
58 (define (parse-options args)
59 (let* ((options (getopt-long args
60 '((help (single-char #\h))
61 (version (single-char #\v)))))
62 (files (cdr (assq '() options))))
63 (if (assq 'help options)
65 (show-version (current-output-port))
66 (show-help (current-output-port))
68 (if (assq 'version options)
69 (begin (show-version (current-output-port)) (exit 0)))
70 (show-version (current-error-port))
73 (define (re-sub re sub string)
74 (regexp-substitute/global #f re string 'pre sub 'post))
76 ;; FIXME: I'm going slowly but certainly mad; I really cannot find the
77 ;; scm library function for this.
78 (define (unquote-uri uri)
79 (re-sub "%([A-Fa-f0-9]{2})"
81 (string (integer->char (string->number (match:substring m 1) 16))))
84 (define (is-textedit-uri? uri)
85 (string-match "^textedit://" uri))
88 (define (dissect-uri uri)
89 (let* ((match (string-match "textedit://(.*):([^:]+):([^:]+):(.*)$" uri)))
91 (list (unquote-uri (match:substring match 1))
92 (match:substring match 2)
93 (match:substring match 3)
94 (match:substring match 4))
96 (format (current-error-port) (_ "invalid textedit URI: ~a") uri)
97 (newline (current-error-port))
98 (format (current-error-port) (_ "expect: textedit://FILE:LINE:CHAR:COLUMN"))
99 (newline (current-error-port))
105 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
107 (define (running-from-gui?)
108 (let ((have-tty? (isatty? (current-input-port))))
109 ;; If no TTY and not using safe, assume running from GUI.
112 (define (run-editor uri)
114 ((command (apply get-editor-command (dissect-uri uri)))
115 (status (system command)))
116 (if (not (= status 0))
118 (format (current-error-port)
119 (_ "failed to invoke editor: ~a") command)
122 (define (run-browser uri)
124 (if (getenv "BROWSER")
125 (format "~a ~a" (getenv "BROWSER") uri)
126 (format #f "firefox -remote 'OpenURL(~a,new-tab)'" uri))))
129 (let ((files (parse-options args)))
130 (if (running-from-gui?)
131 (redirect-port (current-error-port)
132 (open-file (string-append
136 "/lilypond-invoke-editor.log") "a")))
137 (if (not (= (length files) 1))
139 (show-help (current-error-port))
141 (set! %load-path (cons LILYPONDPREFIX %load-path))
142 (primitive-eval '(use-modules (scm editor)))
144 (let* ((uri (car files)))
145 (if (is-textedit-uri? uri)
147 (run-browser uri)))))