1 ;;;; lily.scm -- toplevel Scheme stuff
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 (if (defined? 'set-debug-cell-accesses!)
10 (set-debug-cell-accesses! #f))
12 ;(set-debug-cell-accesses! 1000)
14 (use-modules (ice-9 regex)
24 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
26 (define-public (print . args)
27 (apply format (cons (current-output-port) args)))
31 ;;; debugging evaluator is slower. This should
32 ;;; have a more sensible default.
34 (if (ly:get-option 'verbose)
37 (debug-enable 'backtrace)
38 (read-enable 'positions)))
41 (define-public tex-backend?
42 (member (ly:output-backend) '("texstr" "tex")))
44 (define-public parser #f)
46 (define-public (lilypond-version)
48 (map (lambda (x) (if (symbol? x)
56 ;; cpp hack to get useful error message
57 (define ifdef "First run this through cpp.")
58 (define ifndef "First run this through cpp.")
60 ;; gettext wrapper for guile < 1.7.2
61 (if (defined? 'gettext)
62 (define-public _ gettext)
63 (define-public _ ly:gettext))
65 (define-public (ly:load x)
66 (let* ((file-name (%search-load-path x)))
67 (if (ly:get-option 'verbose)
68 (ly:progress "[~A" file-name))
69 (primitive-load file-name)
70 (if (ly:get-option 'verbose)
73 (define-public TEX_STRING_HASHLIMIT 10000000)
76 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
79 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
82 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
86 (define-public PLATFORM
89 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
93 (define native-getcwd getcwd)
95 (if (string-index x #\\)
97 (string-regexp-substitute
99 (string-regexp-substitute "\\\\" "/" x))))
100 ;; FIXME: this prints a warning.
101 (define-public (ly-getcwd)
102 (slashify (native-getcwd))))
103 (else (define-public ly-getcwd getcwd)))
105 (define-public (is-absolute? file-name)
106 (let ((file-name-length (string-length file-name)))
107 (if (= file-name-length 0)
109 (or (eq? (string-ref file-name 0) #\/)
110 (and (eq? PLATFORM 'windows)
111 (> file-name-length 2)
112 (eq? (string-ref file-name 1) #\:)
113 (eq? (string-ref file-name 2) #\/))))))
115 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
117 (define (type-check-list location signature arguments)
118 "Typecheck a list of arguments against a list of type
119 predicates. Print a message at LOCATION if any predicate failed."
120 (define (recursion-helper signature arguments count)
121 (define (helper pred? arg count)
122 (if (not (pred? arg))
128 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
129 count (type-name pred?) arg))
133 (if (null? signature)
135 (and (helper (car signature) (car arguments) count)
136 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
137 (recursion-helper signature arguments 1))
139 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
143 ;;(define-public (output-framework) (write "hello\n"))
145 (define output-tex-module
146 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
147 (define output-ps-module
148 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
150 (define-public (ps-output-expression expr port)
151 (display (eval expr output-ps-module) port))
153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154 ;; Safe definitions utility
155 (define safe-objects (list))
157 (define-macro (define-safe-public arglist . body)
158 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
159 The syntax is the same as `define*-public'."
160 (define (get-symbol arg)
162 (get-symbol (car arg))
164 (let ((safe-symbol (get-symbol arglist)))
166 (define*-public ,arglist
168 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
173 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
180 "define-music-types.scm"
183 "chord-ignatzek-names.scm"
185 "chord-generic-names.scm"
189 "music-functions.scm"
191 "define-music-properties.scm"
197 "define-context-properties.scm"
198 "translation-functions.scm"
208 "define-markup-commands.scm"
209 "define-grob-properties.scm"
211 "define-grob-interfaces.scm"
212 "define-stencil-commands.scm"
217 "backend-library.scm"
220 ;; must be after everything has been defined
224 (set! type-p-name-alist
226 (,boolean-or-symbol? . "boolean or symbol")
227 (,boolean? . "boolean")
229 (,grob-list? . "list of grobs")
230 (,hash-table? . "hash table")
231 (,input-port? . "input port")
232 (,integer? . "integer")
234 (,ly:context? . "context")
235 (,ly:dimension? . "dimension, in staff space")
236 (,ly:dir? . "direction")
237 (,ly:duration? . "duration")
238 (,ly:grob? . "layout object")
239 (,ly:input-location? . "input location")
240 (,ly:moment? . "moment")
241 (,ly:music? . "music")
242 (,ly:pitch? . "pitch")
243 (,ly:translator? . "translator")
244 (,ly:font-metric? . "font metric")
245 (,markup-list? . "list of markups")
246 (,markup? . "markup")
247 (,ly:music-list? . "list of music")
248 (,number-or-grob? . "number or grob")
249 (,number-or-string? . "number or string")
250 (,number-pair? . "pair of numbers")
251 (,number? . "number")
252 (,output-port? . "output port")
254 (,procedure? . "procedure")
255 (,scheme? . "any type")
256 (,string? . "string")
257 (,symbol? . "symbol")
258 (,vector? . "vector")))
263 (define gc-protect-stat-count 0)
264 (define-public (dump-gc-protects)
265 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
266 (let* ((protects (sort
267 (hash-table->alist (ly:protects))
269 (< (object-address (car a))
270 (object-address (car b))))))
272 (out-file-name (string-append
273 "gcstat-" (number->string gc-protect-stat-count)
275 (outfile (open-file out-file-name "w")))
277 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
280 (lambda (x) (not (symbol? x)))
287 (map object->string (list (object-address x) c x))
293 (define-public (tweak-grob-property grob sym val)
294 (set! (ly:grob-property grob sym) val))
296 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
297 (define-public (lilypond-main files)
298 "Entry point for LilyPond."
303 (let ((failed (lilypond-all files)))
306 (ly:error (_ "failed files: ~S") (string-join failed))
309 ;; HACK: be sure to exit with single newline
313 (define (no-files-handler)
317 (define-public (lilypond-all files)
319 (handler (lambda (key failed-file)
320 (set! failed (append (list failed-file) failed)))))
321 ;;(handler (lambda (key . arg) (set! failed (append arg failed)))))
322 (for-each (lambda (x) (lilypond-file handler x)) files)
325 (define (lilypond-file handler file-name)
326 (catch 'ly-file-failed
327 (lambda () (ly:parse-file file-name))
328 (lambda (x . args) (handler x file-name)))
330 (if (ly:get-option 'debug-gc)
333 (use-modules (scm editor))
335 (define-public (running-from-gui?)
336 (let ((have-tty? (isatty? (current-input-port))))
337 ;; If no TTY and not using safe, assume running from GUI.
339 ((eq? PLATFORM 'windows)
340 ;; This only works for i586-mingw32msvc-gcc -mwindows
341 (not (string-match "standard input"
342 (format #f "~S" (current-input-port)))))
343 ((eq? PLATFORM 'darwin) #f)
347 (define-public (gui-main files)
349 (gui-no-files-handler))
350 (let* ((base (basename (car files) ".ly"))
351 (log-name (string-append base ".log")))
352 (if (not (running-from-gui?))
353 (ly:message (_ "Redirecting output to ~a...") log-name))
354 (ly:stderr-redirect log-name "w")
355 (ly:message "# -*-compilation-*-")
356 (let ((failed (lilypond-all files)))
360 (ly:stderr-redirect "foo" "r")
361 (system (get-editor-command log-name 0 0 0))
362 (ly:error (_ "failed files: ~S") (string-join failed))
367 (define (gui-no-files-handler)
368 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
369 ;; FIXME: soft-code, localize
370 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
371 (cmd (get-editor-command welcome-ly 0 0 0)))
372 (ly:message (_ "Invoking `~a'...") cmd)
376 (or (not (running-from-gui?))
377 (ly:get-option 'safe)
378 (define lilypond-main gui-main))