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")
23 (debug-gc #f "dump memory debugging statistics")
24 (debug-gc-assert-parsed-dead
25 #f "for memory debugging: ensure that all refs to parsed objects are dead.")
26 (debug-lexer #f "debug the flex lexer")
27 (debug-midi #f "generate human readable MIDI")
28 (debug-parser #f "debug the bison parser")
29 (delete-intermediate-files #f
30 "delete unusable PostScript files")
31 (dump-signatures #f "dump output signatures of each system")
32 (dump-tweaks #f "dump page layout and tweaks for each score having the tweak-key layout property set.")
34 "load fonts via Ghostscript.")
35 (include-book-title-preview #t "include book-titles in preview images.")
36 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
37 (job-count #f "Process in parallel")
39 (eps-box-padding #f "Pad EPS bounding box left edge by this much to guarantee alignment between systems")
41 (gui #f "running from gui; redirect stderr to log file")
42 (log-file #f "redirect output to log FILE.log")
44 "relative for simultaneous music works
45 similar to chord syntax")
47 "experimental mechanism for remembering tweaks")
48 (point-and-click #t "use point & click")
49 (paper-size "a4" "the default paper size")
50 (protected-scheme-parsing #t "continue when finding errors in inline
51 scheme are caught in the parser. If off, halt
52 on errors, and print a stack trace.")
53 (profile-property-accesses #f "keep statistics of get_property() calls.")
55 (resolution 101 "resolution for generating PNG bitmaps")
56 (read-file-list #f "Read files to be processed from command line arguments")
58 (safe #f "Run safely")
59 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
62 "how much verbosity for TTF font embedding?")
64 (show-available-fonts #f
65 "List font names available.")
67 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
71 ;; need to do this in the beginning. Other parts of the
72 ;; Scheme init depend on these options.
74 (define-scheme-options)
78 (if (defined? 'set-debug-cell-accesses!)
79 (set-debug-cell-accesses! #f))
81 ;(set-debug-cell-accesses! 1000)
83 (use-modules (ice-9 regex)
96 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
98 (define-public (print . args)
99 (apply format (cons (current-output-port) args)))
103 ;;; debugging evaluator is slower. This should
104 ;;; have a more sensible default.
106 (if (ly:get-option 'verbose)
108 (debug-enable 'debug)
109 (debug-enable 'backtrace)
110 (read-enable 'positions)))
112 (define-public tex-backend?
113 (member (ly:output-backend) '("texstr" "tex")))
115 (define-public parser #f)
117 (define-public (lilypond-version)
119 (map (lambda (x) (if (symbol? x)
126 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
127 (define-public TEX_STRING_HASHLIMIT 10000000)
131 ;; gettext wrapper for guile < 1.7.2
132 (if (defined? 'gettext)
133 (define-public _ gettext)
134 (define-public _ ly:gettext))
136 (define-public (ly:load x)
137 (let* ((file-name (%search-load-path x)))
138 (if (ly:get-option 'verbose)
139 (ly:progress "[~A" file-name))
141 (ly:error (_ "Can't find ~A" x)))
142 (primitive-load file-name)
143 (if (ly:get-option 'verbose)
147 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
150 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
153 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
157 (define-public PLATFORM
160 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
163 (let ((platform (string-tokenize
164 (vector-ref (uname) 0) char-set:letter+digit)))
165 (if (null? (cdr platform)) #f
166 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
170 (define native-getcwd getcwd)
172 (if (string-index x #\\)
174 (string-regexp-substitute
176 (string-regexp-substitute "\\\\" "/" x))))
177 ;; FIXME: this prints a warning.
178 (define-public (ly-getcwd)
179 (slashify (native-getcwd))))
180 (else (define-public ly-getcwd getcwd)))
182 (define-public (is-absolute? file-name)
183 (let ((file-name-length (string-length file-name)))
184 (if (= file-name-length 0)
186 (or (eq? (string-ref file-name 0) #\/)
187 (and (eq? PLATFORM 'windows)
188 (> file-name-length 2)
189 (eq? (string-ref file-name 1) #\:)
190 (eq? (string-ref file-name 2) #\/))))))
192 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194 (define (type-check-list location signature arguments)
195 "Typecheck a list of arguments against a list of type
196 predicates. Print a message at LOCATION if any predicate failed."
198 (define (recursion-helper signature arguments count)
199 (define (helper pred? arg count)
200 (if (not (pred? arg))
206 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
207 count (type-name pred?) arg))
211 (if (null? signature)
213 (and (helper (car signature) (car arguments) count)
214 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
216 (recursion-helper signature arguments 1))
218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
222 ;;(define-public (output-framework) (write "hello\n"))
224 (define output-tex-module
225 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
226 (define output-ps-module
227 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
229 (define-public (ps-output-expression expr port)
230 (display (eval expr output-ps-module) port))
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233 ;; Safe definitions utility
234 (define safe-objects (list))
236 (define-macro (define-safe-public arglist . body)
237 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
238 The syntax is the same as `define*-public'."
239 (define (get-symbol arg)
241 (get-symbol (car arg))
243 (let ((safe-symbol (get-symbol arglist)))
245 (define*-public ,arglist
247 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254 (ly:set-default-scale (ly:make-scale #(0 2 4 5 7 9 11)))
258 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 "define-event-classes.scm"
266 "define-music-types.scm"
269 "chord-ignatzek-names.scm"
271 "chord-generic-names.scm"
274 "music-functions.scm"
277 "define-music-properties.scm"
281 "parser-ly-from-scheme.scm"
282 "ly-syntax-constructors.scm"
284 "define-context-properties.scm"
285 "translation-functions.scm"
295 "define-markup-commands.scm"
296 "define-grob-properties.scm"
298 "define-grob-interfaces.scm"
299 "define-stencil-commands.scm"
303 "backend-library.scm"
306 ;; must be after everything has been defined
310 (set! type-p-name-alist
312 (,boolean-or-symbol? . "boolean or symbol")
313 (,boolean? . "boolean")
315 (,grob-list? . "list of grobs")
316 (,hash-table? . "hash table")
317 (,input-port? . "input port")
318 (,integer? . "integer")
320 (,ly:context? . "context")
321 (,ly:dimension? . "dimension, in staff space")
322 (,ly:dir? . "direction")
323 (,ly:duration? . "duration")
324 (,ly:grob? . "layout object")
325 (,ly:input-location? . "input location")
326 (,ly:moment? . "moment")
327 (,ly:music? . "music")
328 (,ly:pitch? . "pitch")
329 (,ly:translator? . "translator")
330 (,ly:font-metric? . "font metric")
331 (,ly:simple-closure? . "simple closure")
332 (,markup-list? . "list of markups")
333 (,markup? . "markup")
334 (,ly:music-list? . "list of music")
335 (,number-or-grob? . "number or grob")
336 (,number-or-string? . "number or string")
337 (,number-pair? . "pair of numbers")
338 (,number? . "number")
339 (,output-port? . "output port")
341 (,procedure? . "procedure")
342 (,rhythmic-location? . "rhythmic location")
343 (,scheme? . "any type")
344 (,string? . "string")
345 (,symbol? . "symbol")
346 (,vector? . "vector")))
351 (define gc-protect-stat-count 0)
352 (define-public (dump-gc-protects)
353 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
354 (let* ((protects (sort
355 (hash-table->alist (ly:protects))
357 (< (object-address (car a))
358 (object-address (car b))))))
360 (out-file-name (string-append
361 "gcstat-" (number->string gc-protect-stat-count)
363 (outfile (open-file out-file-name "w")))
365 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
373 (map object->string (list (object-address x) c x))
379 (not (symbol? (car x))))
383 ; (display (ly:smob-protects))
385 (if (defined? 'gc-live-object-stats)
387 (display "Live object statistics: GC'ing\n")
390 (ly:set-option 'debug-gc-assert-parsed-dead #t)
393 (set! stats (gc-live-object-stats))
394 (display "Dumping live object statistics.\n")
398 (format outfile "~a: ~a\n" (car x) (cdr x)))
399 (sort (gc-live-object-stats)
401 (string<? (car x) (car y)))))))))
403 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
406 (define (multi-fork count)
407 (define (helper count acc)
410 ((pid (primitive-fork)))
413 (helper (1- count) (cons pid acc))))
418 (define-public (lilypond-main files)
419 "Entry point for LilyPond."
421 (define (no-files-handler)
425 (if (ly:get-option 'gui)
431 (if (and (number? (ly:get-option 'job-count))
432 (> (length files) (ly:get-option 'job-count)))
435 ((count (ly:get-option 'job-count))
436 (split-todo (split-list files count))
437 (joblist (multi-fork count))
440 (if (not (string-or-symbol? (ly:get-option 'log-file)))
441 (ly:set-option 'log-file "lilypond-multi-run"))
443 (if (number? joblist)
445 (ly:set-option 'log-file (format "~a-~a"
446 (ly:get-option 'log-file) joblist))
447 (set! files (vector-ref split-todo joblist)))
452 (let* ((stat (cdr (waitpid pid))))
455 (set! errors (cons (list-element-index joblist pid) errors)))))
460 (let* ((logfile (format "~a-~a.log"
461 (ly:get-option 'log-file) x))
462 (log (ly:gulp-file logfile))
463 (len (string-length log))
464 (tail (substring log (- len 1024))))
466 (display (format "\n\nlogfile ~a:\n\n ~a" logfile tail))))
471 (ly:error "Children ~a exited with errors." errors))
473 (exit (if (null? errors) 0 1))))))
476 (if (string-or-symbol? (ly:get-option 'log-file))
477 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
479 (let ((failed (lilypond-all files)))
482 (ly:error (_ "failed files: ~S") (string-join failed))
485 ;; HACK: be sure to exit with single newline
489 (define-public (lilypond-all files)
490 (if (ly:get-option 'read-file-list)
493 (> (string-length s) 0))
495 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
499 (if (ly:get-option 'show-available-fonts)
501 (ly:font-config-display-fonts)
506 (handler (lambda (key failed-file)
507 (set! failed (append (list failed-file) failed)))))
511 (ly:set-option 'debug-gc-assert-parsed-dead #f)
512 (lilypond-file handler x)
513 (ly:clear-anonymous-modules)
514 (if (ly:get-option 'debug-gc)
520 (define (lilypond-file handler file-name)
521 (catch 'ly-file-failed
522 (lambda () (ly:parse-file file-name))
523 (lambda (x . args) (handler x file-name))))
525 (use-modules (scm editor))
527 (define-public (gui-main files)
529 (gui-no-files-handler))
531 (if (not (string? (ly:get-option 'log-file)))
532 (let* ((base (basename (car files) ".ly"))
533 (log-name (string-append base ".log")))
534 (if (not (ly:get-option 'gui))
535 (ly:message (_ "Redirecting output to ~a...") log-name))
536 (ly:stderr-redirect log-name "w")
537 (ly:message "# -*-compilation-*-"))
539 (let ((failed (lilypond-all files)))
543 (ly:stderr-redirect "foo" "r")
544 (system (get-editor-command log-name 0 0 0))
545 (ly:error (_ "failed files: ~S") (string-join failed))
550 (define (gui-no-files-handler)
551 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
552 ;; FIXME: soft-code, localize
553 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
554 (cmd (get-editor-command welcome-ly 0 0 0)))
555 (ly:message (_ "Invoking `~a'...") cmd)