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 "2.5.25")
21 (define DATADIR "@DATADIR@")
22 (define COMPILE-TIME-PREFIX
23 (format #f "~a/lilypond/~a" DATADIR TOPLEVEL-VERSION))
24 (define LILYPONDPREFIX (or (getenv "LILYPONDPREFIX") COMPILE-TIME-PREFIX))
26 ;; gettext wrapper for guile < 1.7.2
27 (if (defined? 'gettext)
28 (define-public _ gettext)
29 (define-public (_ x) x))
31 (define (show-version port)
32 (format port "~a (GNU LilyPond) ~a \n" PROGRAM-NAME TOPLEVEL-VERSION))
34 (define (show-help port)
35 (format port (_ "Usage: lilypond-invoke-editor [textedit://]FILE:LINE:COLUMN
37 Visit a file and position the cursor
40 -h,--help show this help
41 -v,--version show version
44 (define (parse-options args)
45 (let* ((options (getopt-long args
46 '((help (single-char #\h))
47 (version (single-char #\v)))))
48 (files (cdr (assq '() options))))
49 (if (assq 'help options)
51 (show-version (current-output-port))
52 (show-help (current-output-port))
54 (if (assq 'version options)
55 (begin (show-version (current-output-port)) (exit 0)))
56 (show-version (current-error-port))
59 ;;(define (re-sub re sub string)
60 ;; (let ((sub-string (if (string? sub) sub (sub re))))
61 ;; (regexp-substitute/global #f re string 'pre sub-string 'post)))
62 (define (re-sub re sub string)
63 (regexp-substitute/global #f re string 'pre sub 'post))
65 ;; FIXME: I'm going slowly but certainly mad, I really cannot find the
66 ;; scm library function for this.
67 (define (unquote-uri uri)
68 (re-sub "%([A-Fa-f0-9]{2})"
70 (string (integer->char (string->number (match:substring m 1) 16))))
73 (define (dissect-uri uri)
74 (let* ((ri "textedit://")
75 (file-name:line:column (re-sub ri "" uri))
76 (match (string-match "(.*):([^:]+):(.*)$" file-name:line:column)))
78 (list (unquote-uri (match:substring match 1))
79 (match:substring match 2)
80 (match:substring match 3))
82 ;; FIXME: why be so strict wrt :LINE:COLUMN,
83 ;; esp. considering omitting textedit:// is explicitly
85 (format (current-error-port) (_ "invalid URI: ~a") uri)
86 (newline (current-error-port))
87 (format (current-error-port) (_ "expect: ~aFILE:LINE:COLUMN") ri)
88 (newline (current-error-port))
94 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
96 (define (running-from-gui?)
97 (let ((have-tty? (isatty? (current-input-port))))
98 ;; If no TTY and not using safe, assume running from GUI.
99 ;; for mingw, the test must be inverted.
100 (if (eq? PLATFORM 'windows)
101 have-tty? (not have-tty?))))
104 (let ((files (parse-options args)))
105 (if (running-from-gui?)
106 (redirect-port (current-error-port)
107 (open-file (string-append
111 "/lilypond-invoke-editor.log") "a")))
112 (if (not (= (length files) 1))
114 (show-help (current-error-port))
116 (set! %load-path (cons LILYPONDPREFIX %load-path))
117 (primitive-eval '(use-modules (scm editor)))
118 (let* ((uri (car files))
119 (command (apply get-editor-command (dissect-uri uri)))
120 (status (system command)))
121 (if (not (= status 0))
123 (format (current-error-port)
124 (_ "failed to invoke editor: ~a") command)