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>
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?
26 (define LILYPONDPREFIX
27 (or (getenv "LILYPONDPREFIX")
28 (let* ((bindir (dirname (car (command-line))))
29 (prefix (dirname bindir))
31 (if (eq? prefix (dirname DATADIR)) COMPILE-TIME-PREFIX
32 (format #f "~a/share/lilypond/~a"
33 prefix TOPLEVEL-VERSION))))
36 ;; gettext wrapper for guile < 1.7.2
37 (if (defined? 'gettext)
38 (define-public _ gettext)
39 (define-public (_ x) x))
41 (define (show-version port)
42 (format port "~a (GNU LilyPond) ~a \n" PROGRAM-NAME TOPLEVEL-VERSION))
44 (define (show-help port)
45 (format port (_ "Usage: lilypond-invoke-editor [textedit://]FILE:LINE:COLUMN
47 Visit a file and position the cursor
50 -h,--help show this help
51 -v,--version show version
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)
61 (show-version (current-output-port))
62 (show-help (current-output-port))
64 (if (assq 'version options)
65 (begin (show-version (current-output-port)) (exit 0)))
66 (show-version (current-error-port))
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))
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})"
80 (string (integer->char (string->number (match:substring m 1) 16))))
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)))
88 (list (unquote-uri (match:substring match 1))
89 (match:substring match 2)
90 (match:substring match 3))
92 ;; FIXME: why be so strict wrt :LINE:COLUMN,
93 ;; esp. considering omitting textedit:// is explicitly
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))
104 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
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.
112 (let ((files (parse-options args)))
113 (if (running-from-gui?)
114 (redirect-port (current-error-port)
115 (open-file (string-append
119 "/lilypond-invoke-editor.log") "a")))
120 (if (not (= (length files) 1))
122 (show-help (current-error-port))
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))
131 (format (current-error-port)
132 (_ "failed to invoke editor: ~a") command)