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 ;; difficult to put memory amount stats into here.
377 (define (dump-profile name last this)
379 ((outname (format "~a.profile" (basename name ".ly")))
380 (diff (map (lambda (y) (apply - y)) (zip this last))))
382 (ly:progress "\nWriting timing to ~a..." outname)
383 (format (open-file outname "w")
388 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
391 (define gc-dumping #f)
392 (define gc-protect-stat-count 0)
393 (define-public (dump-gc-protects)
394 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
395 (let* ((protects (sort
396 (hash-table->alist (ly:protects))
398 (< (object-address (car a))
399 (object-address (car b))))))
401 (out-file-name (string-append
402 "gcstat-" (number->string gc-protect-stat-count)
404 (outfile (open-file out-file-name "w")))
407 (display (format "Dumping GC statistics ~a...\n" out-file-name))
413 (format "~a (~a) = ~a\n" (object-address x) c x)
417 (not (symbol? (car x))))
421 (format outfile "\nprotected symbols: ~a\n"
422 (length (filter symbol? (map car protects))))
426 ;; (display (ly:smob-protects))
428 (if (defined? 'gc-live-object-stats)
430 (display "Live object statistics: GC'ing\n")
434 (display "Asserting dead objects\n")
435 (ly:set-option 'debug-gc-assert-parsed-dead #t)
437 (ly:set-option 'debug-gc-assert-parsed-dead #f)
439 (set! stats (gc-live-object-stats))
440 (display "Dumping live object statistics.\n")
444 (format outfile "~a: ~a\n" (car x) (cdr x)))
445 (sort (gc-live-object-stats)
447 (string<? (car x) (car y)))))))
458 gc-protect-stat-count
460 (let ((sym-stat (assoc sym stats)))
465 '(protected-objects bytes-malloced cell-heap-size
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
476 (define (multi-fork count)
477 (define (helper count acc)
480 ((pid (primitive-fork)))
483 (helper (1- count) (cons pid acc))))
488 (define-public (lilypond-main files)
489 "Entry point for LilyPond."
491 (define (no-files-handler)
495 (if (ly:get-option 'gui)
501 (if (ly:get-option 'read-file-list)
504 (> (string-length s) 0))
506 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
510 (if (and (number? (ly:get-option 'job-count))
511 (> (length files) (ly:get-option 'job-count)))
514 ((count (ly:get-option 'job-count))
515 (split-todo (split-list files count))
516 (joblist (multi-fork count))
519 (if (not (string-or-symbol? (ly:get-option 'log-file)))
520 (ly:set-option 'log-file "lilypond-multi-run"))
522 (if (number? joblist)
524 (ly:set-option 'log-file (format "~a-~a"
525 (ly:get-option 'log-file) joblist))
526 (set! files (vector-ref split-todo joblist)))
529 (ly:progress "\nForking into jobs: ~a\n" joblist)
532 (let* ((stat (cdr (waitpid pid))))
535 (set! errors (cons (list-element-index joblist pid) errors)))))
540 (let* ((logfile (format "~a-~a.log"
541 (ly:get-option 'log-file) x))
542 (log (ly:gulp-file logfile))
543 (len (string-length log))
544 (tail (substring log (max 0 (- len 1024)))))
546 (display (format "\n\nlogfile ~a:\n\n ~a" logfile tail))))
551 (ly:error "Children ~a exited with errors." errors))
553 (exit (if (null? errors) 0 1))))))
556 (if (string-or-symbol? (ly:get-option 'log-file))
557 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
559 (let ((failed (lilypond-all files)))
562 (ly:error (_ "failed files: ~S") (string-join failed))
565 ;; HACK: be sure to exit with single newline
569 (define-public (lilypond-all files)
572 (if (ly:get-option 'show-available-fonts)
574 (ly:font-config-display-fonts)
579 (start-measurements #f)
580 (handler (lambda (key failed-file)
581 (set! failed (append (list failed-file) failed)))))
587 (set! start-measurements (profile-measurements))
588 (lilypond-file handler x)
589 (if (ly:get-option 'dump-profile)
590 (dump-profile x start-measurements (profile-measurements)))
593 (ly:clear-anonymous-modules)
594 (if (ly:get-option 'debug-gc)
596 (if (= (random 40) 1)
597 (ly:reset-all-fonts))))
602 (define (lilypond-file handler file-name)
603 (catch 'ly-file-failed
604 (lambda () (ly:parse-file file-name))
605 (lambda (x . args) (handler x file-name))))
607 (use-modules (scm editor))
609 (define-public (gui-main files)
611 (gui-no-files-handler))
613 (if (not (string? (ly:get-option 'log-file)))
614 (let* ((base (basename (car files) ".ly"))
615 (log-name (string-append base ".log")))
616 (if (not (ly:get-option 'gui))
617 (ly:message (_ "Redirecting output to ~a...") log-name))
618 (ly:stderr-redirect log-name "w")
619 (ly:message "# -*-compilation-*-"))
621 (let ((failed (lilypond-all files)))
625 (ly:stderr-redirect "foo" "r")
626 (system (get-editor-command log-name 0 0 0))
627 (ly:error (_ "failed files: ~S") (string-join failed))
632 (define (gui-no-files-handler)
633 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
634 ;; FIXME: soft-code, localize
635 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
636 (cmd (get-editor-command welcome-ly 0 0 0)))
637 (ly:message (_ "Invoking `~a'...") cmd)