]> git.donarmstrong.com Git - lilypond.git/blob - scripts/lilypond-invoke-editor.scm
* lily/main.cc (setup_paths)[__MINGW32__]: Normalize LILYPONDPREFIX.
[lilypond.git] / scripts / lilypond-invoke-editor.scm
1 #!@GUILE@ \
2 -e main -s
3 !#
4 ;;;; lilypond-invoke-editor.scm -- Invoke an editor in file:line:column mode
5 ;;;;
6 ;;;; source file of the GNU LilyPond music typesetter
7 ;;;;
8 ;;;; (c)  2005 Jan Nieuwenhuizen <janneke@gnu.org>
9
10 ;; gui debug helper
11 ;; (define (exit x) (system "sleep 10"))
12
13 (use-modules
14  (ice-9 getopt-long)
15  (ice-9 regex)
16  (srfi srfi-13)
17  (srfi srfi-14))
18
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))
24
25 ;; argv0 relocation -- do in wrapper?
26 (define LILYPONDPREFIX
27   (or (getenv "LILYPONDPREFIX")
28       (let* ((bindir (dirname (car (command-line))))
29              (prefix (dirname bindir))
30              (lilypond-prefix
31               (if (eq? prefix (dirname DATADIR)) COMPILE-TIME-PREFIX
32                   (format #f "~a/share/lilypond/~a"
33                           prefix TOPLEVEL-VERSION))))
34         lilypond-prefix)))
35
36 ;; gettext wrapper for guile < 1.7.2
37 (if (defined? 'gettext)
38     (define-public _ gettext)
39     (define-public (_ x) x))
40
41 (define (show-version port)
42   (format port "~a (GNU LilyPond) ~a \n" PROGRAM-NAME TOPLEVEL-VERSION))
43
44 (define (show-help port)
45   (format port (_ "Usage: lilypond-invoke-editor [textedit://]FILE:LINE:COLUMN
46
47 Visit a file and position the cursor
48
49 Options:
50   -h,--help          show this help
51   -v,--version       show version
52 ")))
53
54 (define (parse-options args)
55   (let* ((options (getopt-long args
56                                '((help (single-char #\h))
57                                  (version (single-char #\v)))))
58          (files (cdr (assq '() options))))
59     (if (assq 'help options)
60         (begin
61           (show-version (current-output-port))
62           (show-help (current-output-port))
63         (exit 0)))
64     (if (assq 'version options)
65         (begin (show-version (current-output-port)) (exit 0)))
66     (show-version (current-error-port))
67     files))
68
69 ;;(define (re-sub re sub string)
70 ;;  (let ((sub-string (if (string? sub) sub (sub re))))
71 ;;    (regexp-substitute/global #f re string 'pre sub-string 'post)))
72 (define (re-sub re sub string)
73   (regexp-substitute/global #f re string 'pre sub 'post))
74
75 ;; FIXME: I'm going slowly but certainly mad, I really cannot find the
76 ;; scm library function for this.
77 (define (unquote-uri uri)
78   (re-sub "%([A-Fa-f0-9]{2})"
79           (lambda (m)
80             (string (integer->char (string->number (match:substring m 1) 16))))
81           uri))
82   
83 (define (dissect-uri uri)
84   (let* ((ri "textedit://")
85          (file-name:line:column (re-sub ri "" uri))
86          (match (string-match "(.*):([^:]+):(.*)$" file-name:line:column)))
87     (if match
88         (list (unquote-uri (match:substring match 1))
89               (match:substring match 2)
90               (match:substring match 3))
91         (begin
92           ;; FIXME: why be so strict wrt :LINE:COLUMN,
93           ;; esp. considering omitting textedit:// is explicitly
94           ;; allowed.
95           (format (current-error-port) (_ "invalid URI: ~a") uri)
96           (newline (current-error-port))
97           (format (current-error-port) (_ "expect: ~aFILE:LINE:COLUMN") ri)
98           (newline (current-error-port))
99           (exit 1)))))
100
101 (define PLATFORM
102   (string->symbol
103    (string-downcase
104     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
105
106 (define (running-from-gui?)
107   (let ((have-tty? (isatty? (current-input-port))))
108     ;; If no TTY and not using safe, assume running from GUI.
109     (not have-tty?)))
110
111 (define (main args)
112   (let ((files (parse-options args)))
113     (if (running-from-gui?)
114         (redirect-port (current-error-port)
115                        (open-file (string-append
116                                    (or (getenv "TMP")
117                                        (getenv "TEMP")
118                                        "/tmp")
119                                    "/lilypond-invoke-editor.log") "a")))
120     (if (not (= (length files) 1))
121         (begin
122           (show-help (current-error-port))
123           (exit 1)))
124     (set! %load-path (cons LILYPONDPREFIX %load-path))
125     (primitive-eval '(use-modules (scm editor)))
126     (let* ((uri (car files))
127            (command (apply get-editor-command (dissect-uri uri)))
128            (status (system command)))
129       (if (not (= status 0))
130           (begin
131             (format (current-error-port)
132                     (_ "failed to invoke editor: ~a") command)
133             (exit 1))))))