1 ;;;; lily.scm -- toplevel Scheme stuff
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
9 (define (define-scheme-options)
11 (ly:add-option (car x) (cadr x) (caddr x)))
17 ;; - [subject-]object-object-verb +"ing"
18 ;; - [subject-]-verb-object-object
20 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
21 (check-internal-types #f "check every property assignment for types")
22 (clip-systems #f "Generate cut-out snippets of a score")
24 "dump memory debugging statistics")
25 (debug-midi #f "generate human readable MIDI")
26 (delete-intermediate-files #f
27 "delete unusable PostScript files")
28 (dump-signatures #f "dump output signatures of each system")
29 (dump-tweaks #f "dump page layout and tweaks for each score having the tweak-key layout property set.")
31 "load fonts via Ghostscript.")
32 (include-book-title-preview #t "include book-titles in preview images.")
33 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
35 (pad-eps-boxes #f "Pad EPS bounding boxes to guarantee alignment between systems")
37 (gui #f "running from gui; redirect stderr to log file")
40 "relative for simultaneous music works
41 similar to chord syntax")
43 "experimental mechanism for remembering tweaks")
44 (point-and-click #t "use point & click")
45 (paper-size "a4" "the default paper size")
46 (protected-scheme-parsing #t "continue when finding errors in inline
47 scheme are caught in the parser. If off, halt
48 on errors, and print a stack trace.")
49 (profile-property-accesses #f "keep statistics of get_property() calls.")
51 (resolution 101 "resolution for generating bitmaps")
52 (read-file-list #f "Read files to be processed from command line arguments")
54 (safe #f "Run safely")
55 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
58 "how much verbosity for TTF font embedding?")
60 (show-available-fonts #f
61 "List font names available.")
63 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
67 ;; need to do this in the beginning. Other parts of the
68 ;; Scheme init depend on these options.
70 (define-scheme-options)
74 (if (defined? 'set-debug-cell-accesses!)
75 (set-debug-cell-accesses! #f))
77 ;(set-debug-cell-accesses! 1000)
79 (use-modules (ice-9 regex)
92 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
94 (define-public (print . args)
95 (apply format (cons (current-output-port) args)))
99 ;;; debugging evaluator is slower. This should
100 ;;; have a more sensible default.
102 (if (ly:get-option 'verbose)
104 (debug-enable 'debug)
105 (debug-enable 'backtrace)
106 (read-enable 'positions)))
108 (define-public tex-backend?
109 (member (ly:output-backend) '("texstr" "tex")))
111 (define-public parser #f)
113 (define-public (lilypond-version)
115 (map (lambda (x) (if (symbol? x)
122 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
123 (define-public TEX_STRING_HASHLIMIT 10000000)
127 ;; gettext wrapper for guile < 1.7.2
128 (if (defined? 'gettext)
129 (define-public _ gettext)
130 (define-public _ ly:gettext))
132 (define-public (ly:load x)
133 (let* ((file-name (%search-load-path x)))
134 (if (ly:get-option 'verbose)
135 (ly:progress "[~A" file-name))
137 (ly:error (_ "Can't find ~A" x)))
138 (primitive-load file-name)
139 (if (ly:get-option 'verbose)
143 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
146 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
149 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
153 (define-public PLATFORM
156 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
159 (let ((platform (string-tokenize
160 (vector-ref (uname) 0) char-set:letter+digit)))
161 (if (null? (cdr platform)) #f
162 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
166 (define native-getcwd getcwd)
168 (if (string-index x #\\)
170 (string-regexp-substitute
172 (string-regexp-substitute "\\\\" "/" x))))
173 ;; FIXME: this prints a warning.
174 (define-public (ly-getcwd)
175 (slashify (native-getcwd))))
176 (else (define-public ly-getcwd getcwd)))
178 (define-public (is-absolute? file-name)
179 (let ((file-name-length (string-length file-name)))
180 (if (= file-name-length 0)
182 (or (eq? (string-ref file-name 0) #\/)
183 (and (eq? PLATFORM 'windows)
184 (> file-name-length 2)
185 (eq? (string-ref file-name 1) #\:)
186 (eq? (string-ref file-name 2) #\/))))))
188 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190 (define (type-check-list location signature arguments)
191 "Typecheck a list of arguments against a list of type
192 predicates. Print a message at LOCATION if any predicate failed."
194 (define (recursion-helper signature arguments count)
195 (define (helper pred? arg count)
196 (if (not (pred? arg))
202 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
203 count (type-name pred?) arg))
207 (if (null? signature)
209 (and (helper (car signature) (car arguments) count)
210 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
212 (recursion-helper signature arguments 1))
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218 ;;(define-public (output-framework) (write "hello\n"))
220 (define output-tex-module
221 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
222 (define output-ps-module
223 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
225 (define-public (ps-output-expression expr port)
226 (display (eval expr output-ps-module) port))
228 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
229 ;; Safe definitions utility
230 (define safe-objects (list))
232 (define-macro (define-safe-public arglist . body)
233 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
234 The syntax is the same as `define*-public'."
235 (define (get-symbol arg)
237 (get-symbol (car arg))
239 (let ((safe-symbol (get-symbol arglist)))
241 (define*-public ,arglist
243 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
247 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250 (ly:set-default-scale (ly:make-scale #(0 2 4 5 7 9 11)))
254 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
261 "define-event-classes.scm"
262 "define-music-types.scm"
265 "chord-ignatzek-names.scm"
267 "chord-generic-names.scm"
270 "music-functions.scm"
273 "define-music-properties.scm"
277 "parser-ly-from-scheme.scm"
278 "ly-syntax-constructors.scm"
280 "define-context-properties.scm"
281 "translation-functions.scm"
291 "define-markup-commands.scm"
292 "define-grob-properties.scm"
294 "define-grob-interfaces.scm"
295 "define-stencil-commands.scm"
299 "backend-library.scm"
302 ;; must be after everything has been defined
306 (set! type-p-name-alist
308 (,boolean-or-symbol? . "boolean or symbol")
309 (,boolean? . "boolean")
311 (,grob-list? . "list of grobs")
312 (,hash-table? . "hash table")
313 (,input-port? . "input port")
314 (,integer? . "integer")
316 (,ly:context? . "context")
317 (,ly:dimension? . "dimension, in staff space")
318 (,ly:dir? . "direction")
319 (,ly:duration? . "duration")
320 (,ly:grob? . "layout object")
321 (,ly:input-location? . "input location")
322 (,ly:moment? . "moment")
323 (,ly:music? . "music")
324 (,ly:pitch? . "pitch")
325 (,ly:translator? . "translator")
326 (,ly:font-metric? . "font metric")
327 (,ly:simple-closure? . "simple closure")
328 (,markup-list? . "list of markups")
329 (,markup? . "markup")
330 (,ly:music-list? . "list of music")
331 (,number-or-grob? . "number or grob")
332 (,number-or-string? . "number or string")
333 (,number-pair? . "pair of numbers")
334 (,number? . "number")
335 (,output-port? . "output port")
337 (,procedure? . "procedure")
338 (,rhythmic-location? . "rhythmic location")
339 (,scheme? . "any type")
340 (,string? . "string")
341 (,symbol? . "symbol")
342 (,vector? . "vector")))
347 (define gc-protect-stat-count 0)
348 (define-public (dump-gc-protects)
349 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
350 (let* ((protects (sort
351 (hash-table->alist (ly:protects))
353 (< (object-address (car a))
354 (object-address (car b))))))
356 (out-file-name (string-append
357 "gcstat-" (number->string gc-protect-stat-count)
359 (outfile (open-file out-file-name "w")))
361 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
369 (map object->string (list (object-address x) c x))
375 (not (symbol? (car x))))
379 ; (display (ly:smob-protects))
381 (if (defined? 'gc-live-object-stats)
383 (display "Live object statistics: GC'ing\n")
387 (set! stats (gc-live-object-stats))
388 (display "Dumping live object statistics.\n")
392 (format outfile "~a: ~a\n" (car x) (cdr x)))
393 (sort (gc-live-object-stats)
395 (string<? (car x) (car y)))))))))
397 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
400 (define-public (lilypond-main files)
401 "Entry point for LilyPond."
403 (define (no-files-handler)
407 (if (ly:get-option 'gui)
413 (let ((failed (lilypond-all files)))
416 (ly:error (_ "failed files: ~S") (string-join failed))
419 ;; HACK: be sure to exit with single newline
423 (define-public (lilypond-all files)
424 (if (ly:get-option 'read-file-list)
427 (> (string-length s) 0))
429 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
433 (if (ly:get-option 'show-available-fonts)
435 (ly:font-config-display-fonts)
440 (handler (lambda (key failed-file)
441 (set! failed (append (list failed-file) failed)))))
445 (lilypond-file handler x)
446 (ly:clear-anonymous-modules)
447 (if (ly:get-option 'debug-gc)
453 (define (lilypond-file handler file-name)
454 (catch 'ly-file-failed
455 (lambda () (ly:parse-file file-name))
456 (lambda (x . args) (handler x file-name))))
458 (use-modules (scm editor)
461 (define-public (gui-main files)
463 (gui-no-files-handler))
464 (let* ((base (basename (car files) ".ly"))
465 (log-name (string-append base ".log")))
466 (if (not (ly:get-option 'gui))
467 (ly:message (_ "Redirecting output to ~a...") log-name))
468 (ly:stderr-redirect log-name "w")
469 (ly:message "# -*-compilation-*-")
471 (let ((failed (lilypond-all files)))
475 (ly:stderr-redirect "foo" "r")
476 (system (get-editor-command log-name 0 0 0))
477 (ly:error (_ "failed files: ~S") (string-join failed))
482 (define (gui-no-files-handler)
483 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
484 ;; FIXME: soft-code, localize
485 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
486 (cmd (get-editor-command welcome-ly 0 0 0)))
487 (ly:message (_ "Invoking `~a'...") cmd)