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>
8 ;; Internationalisation: (_i "to be translated") gets an entry in the
9 ;; POT file (gettext ) must be invoked explicitely to do the actual
11 ;;(define-macro (_i x) x)
12 ;;(define-macro-public _i (x) x)
13 ;;(define-public-macro _i (x) x)
15 (defmacro-public _i (x) x)
17 (define (define-scheme-options)
19 (ly:add-option (car x) (cadr x) (caddr x)))
25 ;; - [subject-]object-object-verb +"ing"
26 ;; - [subject-]-verb-object-object
28 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
29 (check-internal-types #f "check every property assignment for types")
30 (clip-systems #f "Generate cut-out snippets of a score")
31 (debug-gc #f "dump memory debugging statistics")
32 (debug-gc-assert-parsed-dead #f "for memory debugging:
33 ensure that all refs to parsed objects are dead. This is an internal option, and is switched on automatically for -ddebug-gc.")
34 (debug-lexer #f "debug the flex lexer")
35 (debug-midi #f "generate human readable MIDI")
36 (debug-parser #f "debug the bison parser")
37 (debug-skylines #f "debug skylines")
38 (delete-intermediate-files #f
39 "delete unusable PostScript files")
40 (dump-profile #f "dump timing information for each file")
41 (dump-tweaks #f "dump page layout and tweaks for each score having the tweak-key layout property set.")
42 (dump-signatures #f "dump output signatures of each system")
44 (eps-box-padding #f "Pad EPS bounding box left edge by this much to guarantee alignment between systems")
47 "load fonts via Ghostscript.")
48 (gui #f "running from gui; redirect stderr to log file")
50 (include-book-title-preview #t "include book-titles in preview images.")
51 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
52 (job-count #f "Process in parallel")
53 (log-file #f "redirect output to log FILE.log")
56 "relative for simultaneous music works
57 similar to chord syntax")
59 "experimental mechanism for remembering tweaks")
60 (point-and-click #t "use point & click")
61 (paper-size "a4" "the default paper size")
62 (pixmap-format "png16m" "GS format to use for pixel images")
63 (protected-scheme-parsing #t "continue when finding errors in inline
64 scheme are caught in the parser. If off, halt
65 on errors, and print a stack trace.")
66 (profile-property-accesses #f "keep statistics of get_property() calls.")
68 (resolution 101 "resolution for generating PNG bitmaps")
69 (read-file-list #f "Read files to be processed from command line arguments")
71 (safe #f "Run safely")
72 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
75 "how much verbosity for TTF font embedding?")
77 (show-available-fonts #f
78 "List font names available.")
80 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
84 ;; need to do this in the beginning. Other parts of the
85 ;; Scheme init depend on these options.
87 (define-scheme-options)
91 (if (defined? 'set-debug-cell-accesses!)
92 (set-debug-cell-accesses! #f))
94 ;(set-debug-cell-accesses! 1000)
96 (use-modules (ice-9 regex)
109 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
111 (define-public (print . args)
112 (apply format (cons (current-output-port) args)))
116 ;;; debugging evaluator is slower. This should
117 ;;; have a more sensible default.
119 (if (ly:get-option 'verbose)
121 (ly:set-option 'protected-scheme-parsing #f)
122 (debug-enable 'debug)
123 (debug-enable 'backtrace)
124 (read-enable 'positions)))
126 (define-public tex-backend?
127 (member (ly:output-backend) '("texstr" "tex")))
129 (define-public parser #f)
131 (define-public (lilypond-version)
133 (map (lambda (x) (if (symbol? x)
140 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
141 (define-public TEX_STRING_HASHLIMIT 10000000)
145 ;; gettext wrapper for guile < 1.7.2
146 (if (defined? 'gettext)
147 (define-public _ gettext)
148 (define-public _ ly:gettext))
150 (define-public (ly:load x)
151 (let* ((file-name (%search-load-path x)))
152 (if (ly:get-option 'verbose)
153 (ly:progress "[~A" file-name))
155 (ly:error (_ "cannot find: ~A") x))
156 (primitive-load file-name)
157 (if (ly:get-option 'verbose)
161 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
164 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
167 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
171 (define-public PLATFORM
174 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
177 (let ((platform (string-tokenize
178 (vector-ref (uname) 0) char-set:letter+digit)))
179 (if (null? (cdr platform)) #f
180 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
184 (define native-getcwd getcwd)
186 (if (string-index x #\\)
188 (string-regexp-substitute
190 (string-regexp-substitute "\\\\" "/" x))))
191 ;; FIXME: this prints a warning.
192 (define-public (ly-getcwd)
193 (slashify (native-getcwd))))
194 (else (define-public ly-getcwd getcwd)))
196 (define-public (is-absolute? file-name)
197 (let ((file-name-length (string-length file-name)))
198 (if (= file-name-length 0)
200 (or (eq? (string-ref file-name 0) #\/)
201 (and (eq? PLATFORM 'windows)
202 (> file-name-length 2)
203 (eq? (string-ref file-name 1) #\:)
204 (eq? (string-ref file-name 2) #\/))))))
206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 (define (type-check-list location signature arguments)
209 "Typecheck a list of arguments against a list of type
210 predicates. Print a message at LOCATION if any predicate failed."
212 (define (recursion-helper signature arguments count)
213 (define (helper pred? arg count)
214 (if (not (pred? arg))
220 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
221 count (type-name pred?) arg))
225 (if (null? signature)
227 (and (helper (car signature) (car arguments) count)
228 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
230 (recursion-helper signature arguments 1))
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
236 ;;(define-public (output-framework) (write "hello\n"))
238 (define output-tex-module
239 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
240 (define output-ps-module
241 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
243 (define-public (ps-output-expression expr port)
244 (display (eval expr output-ps-module) port))
246 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
247 ;; Safe definitions utility
248 (define safe-objects (list))
250 (define-macro (define-safe-public arglist . body)
251 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
252 The syntax is the same as `define*-public'."
253 (define (get-symbol arg)
255 (get-symbol (car arg))
257 (let ((safe-symbol (get-symbol arglist)))
259 (define*-public ,arglist
261 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
265 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
268 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
272 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
279 "define-event-classes.scm"
280 "define-music-types.scm"
283 "chord-ignatzek-names.scm"
285 "chord-generic-names.scm"
288 "music-functions.scm"
291 "define-music-properties.scm"
295 "parser-ly-from-scheme.scm"
296 "ly-syntax-constructors.scm"
298 "define-context-properties.scm"
299 "translation-functions.scm"
309 "define-markup-commands.scm"
310 "define-grob-properties.scm"
312 "define-grob-interfaces.scm"
313 "define-stencil-commands.scm"
317 "backend-library.scm"
320 ;; must be after everything has been defined
324 (set! type-p-name-alist
326 (,boolean-or-symbol? . "boolean or symbol")
327 (,boolean? . "boolean")
329 (,grob-list? . "list of grobs")
330 (,hash-table? . "hash table")
331 (,input-port? . "input port")
332 (,integer? . "integer")
334 (,ly:context? . "context")
335 (,ly:dimension? . "dimension, in staff space")
336 (,ly:dir? . "direction")
337 (,ly:duration? . "duration")
338 (,ly:grob? . "layout object")
339 (,ly:input-location? . "input location")
340 (,ly:moment? . "moment")
341 (,ly:music? . "music")
342 (,ly:pitch? . "pitch")
343 (,ly:translator? . "translator")
344 (,ly:font-metric? . "font metric")
345 (,ly:simple-closure? . "simple closure")
346 (,markup-list? . "list of markups")
347 (,markup? . "markup")
348 (,ly:music-list? . "list of music")
349 (,number-or-grob? . "number or grob")
350 (,number-or-string? . "number or string")
351 (,number-pair? . "pair of numbers")
352 (,number? . "number")
353 (,output-port? . "output port")
355 (,procedure? . "procedure")
356 (,rhythmic-location? . "rhythmic location")
357 (,scheme? . "any type")
358 (,string? . "string")
359 (,symbol? . "symbol")
360 (,vector? . "vector")))
362 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
365 (define (profile-measurements)
371 (ly:assoc-get 'gc-time-taken stats))
373 (ly:assoc-get 'total-cells-allocated stats 0)
374 ;; difficult to put memory amount stats into here.
378 (define (dump-profile name last this)
380 ((outname (format "~a.profile" (basename name ".ly")))
381 (diff (map (lambda (y) (apply - y)) (zip this last))))
383 (ly:progress "\nWriting timing to ~a..." outname)
384 (format (open-file outname "w")
385 "time: ~a\ncells: ~a\n"
391 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
394 (define gc-dumping #f)
395 (define gc-protect-stat-count 0)
396 (define-public (dump-gc-protects)
397 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
398 (let* ((protects (sort
399 (hash-table->alist (ly:protects))
401 (< (object-address (car a))
402 (object-address (car b))))))
404 (out-file-name (string-append
405 "gcstat-" (number->string gc-protect-stat-count)
407 (outfile (open-file out-file-name "w")))
410 (display (format "Dumping GC statistics ~a...\n" out-file-name))
416 (format "~a (~a) = ~a\n" (object-address x) c x)
420 (not (symbol? (car x))))
424 (format outfile "\nprotected symbols: ~a\n"
425 (length (filter symbol? (map car protects))))
429 ;; (display (ly:smob-protects))
431 (if (defined? 'gc-live-object-stats)
433 (display "Live object statistics: GC'ing\n")
437 (display "Asserting dead objects\n")
438 (ly:set-option 'debug-gc-assert-parsed-dead #t)
440 (ly:set-option 'debug-gc-assert-parsed-dead #f)
442 (set! stats (gc-live-object-stats))
443 (display "Dumping live object statistics.\n")
447 (format outfile "~a: ~a\n" (car x) (cdr x)))
448 (sort (gc-live-object-stats)
450 (string<? (car x) (car y)))))))
461 gc-protect-stat-count
463 (let ((sym-stat (assoc sym stats)))
468 '(protected-objects bytes-malloced cell-heap-size
476 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
479 (define (multi-fork count)
480 (define (helper count acc)
483 ((pid (primitive-fork)))
486 (helper (1- count) (cons pid acc))))
491 (define-public (lilypond-main files)
492 "Entry point for LilyPond."
494 (define (no-files-handler)
498 (if (ly:get-option 'gui)
504 (if (ly:get-option 'read-file-list)
507 (> (string-length s) 0))
509 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
513 (if (and (number? (ly:get-option 'job-count))
514 (> (length files) (ly:get-option 'job-count)))
517 ((count (ly:get-option 'job-count))
518 (split-todo (split-list files count))
519 (joblist (multi-fork count))
522 (if (not (string-or-symbol? (ly:get-option 'log-file)))
523 (ly:set-option 'log-file "lilypond-multi-run"))
525 (if (number? joblist)
527 (ly:set-option 'log-file (format "~a-~a"
528 (ly:get-option 'log-file) joblist))
529 (set! files (vector-ref split-todo joblist)))
532 (ly:progress "\nForking into jobs: ~a\n" joblist)
535 (let* ((stat (cdr (waitpid pid))))
538 (set! errors (cons (list-element-index joblist pid) errors)))))
543 (let* ((logfile (format "~a-~a.log"
544 (ly:get-option 'log-file) x))
545 (log (ly:gulp-file logfile))
546 (len (string-length log))
547 (tail (substring log (max 0 (- len 1024)))))
549 (display (format "\n\nlogfile ~a:\n\n ~a" logfile tail))))
554 (ly:error "Children ~a exited with errors." errors))
556 (exit (if (null? errors) 0 1))))))
559 (if (string-or-symbol? (ly:get-option 'log-file))
560 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
562 (let ((failed (lilypond-all files)))
565 (ly:error (_ "failed files: ~S") (string-join failed))
568 ;; HACK: be sure to exit with single newline
572 (define-public (lilypond-all files)
575 (if (ly:get-option 'show-available-fonts)
577 (ly:font-config-display-fonts)
582 (start-measurements (ly:get-option 'dump-profile))
583 (handler (lambda (key failed-file)
584 (set! failed (append (list failed-file) failed)))))
590 (if start-measurements
591 (set! start-measurements (profile-measurements)))
592 (lilypond-file handler x)
593 (if start-measurements
594 (dump-profile x start-measurements (profile-measurements)))
597 (ly:clear-anonymous-modules)
598 (if (ly:get-option 'debug-gc)
600 (if (= (random 40) 1)
601 (ly:reset-all-fonts))))
606 (define (lilypond-file handler file-name)
607 (catch 'ly-file-failed
608 (lambda () (ly:parse-file file-name))
609 (lambda (x . args) (handler x file-name))))
611 (use-modules (scm editor))
613 (define-public (gui-main files)
615 (gui-no-files-handler))
617 (if (not (string? (ly:get-option 'log-file)))
618 (let* ((base (basename (car files) ".ly"))
619 (log-name (string-append base ".log")))
620 (if (not (ly:get-option 'gui))
621 (ly:message (_ "Redirecting output to ~a...") log-name))
622 (ly:stderr-redirect log-name "w")
623 (ly:message "# -*-compilation-*-"))
625 (let ((failed (lilypond-all files)))
629 (ly:stderr-redirect "foo" "r")
630 (system (get-editor-command log-name 0 0 0))
631 (ly:error (_ "failed files: ~S") (string-join failed))
636 (define (gui-no-files-handler)
637 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
638 ;; FIXME: soft-code, localize
639 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
640 (cmd (get-editor-command welcome-ly 0 0 0)))
641 (ly:message (_ "Invoking `~a'...") cmd)