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 Jan Nieuwenhuizen <janneke@gnu.org>
16 (define PROGRAM-NAME "lilypond-invoke-editor")
17 (define TOPLEVEL-VERSION "@TOPLEVEL_VERSION@")
18 (define DATADIR "@DATADIR@")
19 (define COMPILE-TIME-PREFIX
20 (format #f "~a/lilypond/~a" DATADIR TOPLEVEL-VERSION))
21 (define LILYPONDPREFIX (or (getenv "LILYPONDPREFIX") COMPILE-TIME-PREFIX))
23 ;; gettext wrapper for guile < 1.7.2
24 (if (defined? 'gettext)
25 (define-public _ gettext)
26 (define-public (_ x) x))
28 (define (show-version port)
29 (format port "~a (GNU LilyPond) ~a \n" PROGRAM-NAME TOPLEVEL-VERSION))
31 (define (show-help port)
32 (format port (_ "Usage: lilypond-invoke-editor [textedit://]FILE:LINE:COLUMN
34 Visit a file and position the cursor
37 -h,--help show this help
38 -v,--version show version
41 (define (parse-options args)
42 (let* ((options (getopt-long args
43 '((help (single-char #\h))
44 (version (single-char #\v)))))
45 (files (cdr (assq '() options))))
46 (if (assq 'help options)
48 (show-version (current-output-port))
49 (show-help (current-output-port))
51 (if (assq 'version options)
52 (begin (show-version (current-output-port)) (exit 0)))
53 (show-version (current-error-port))
56 (define (re-sub re sub string)
57 (regexp-substitute/global #f re string 'pre sub 'post))
59 (define (dissect-uri uri)
60 (let* ((ri "textedit://")
61 (file-name:line:column (re-sub ri "" uri))
62 (match (string-match "(.*):([^:]+):(.*)$" file-name:line:column)))
64 (list (match:substring match 1)
65 (match:substring match 2)
66 (match:substring match 3))
68 ;; FIXME: why be so strict wrt :LINE:COLUMN,
69 ;; esp. considering omitting textedit:// is explicitly
71 (format (current-error-port) (_ "invalid URI: ~a") uri)
72 (newline (current-error-port))
73 (format (current-error-port) (_ "expect: ~aFILE:LINE:COLUMN") ri)
74 (newline (current-error-port))
80 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
82 (define (running-from-gui?)
83 (let ((have-tty? (isatty? (current-input-port))))
84 ;; If no TTY and not using safe, assume running from GUI.
85 ;; for mingw, the test must be inverted.
86 (if (eq? PLATFORM 'windows)
87 have-tty? (not have-tty?))))
90 (let ((files (parse-options args)))
91 (if (running-from-gui?)
92 (redirect-port (current-error-port)
93 (open-file (string-append
97 "/lilypond-invoke-editor.log") "a")))
98 (if (not (= (length files) 1))
100 (show-help (current-error-port))
102 (set! %load-path (cons LILYPONDPREFIX %load-path))
103 (primitive-eval '(use-modules (scm editor)))
104 (let* ((uri (car files))
105 (command (apply get-editor-command (dissect-uri uri)))
106 (status (system command)))
107 (if (not (= status 0))
109 (format (current-error-port)
110 (_ "failed to invoke editor: ~a") command)