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 #f "for memory debugging:
25 ensure that all refs to parsed objects are dead. This is an internal option, and is switched on automatically for -ddebug-gc.")
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 (debug-skylines #f "debug skylines")
30 (delete-intermediate-files #f
31 "delete unusable PostScript files")
32 (dump-signatures #f "dump output signatures of each system")
33 (dump-tweaks #f "dump page layout and tweaks for each score having the tweak-key layout property set.")
35 (eps-box-padding #f "Pad EPS bounding box left edge by this much to guarantee alignment between systems")
38 "load fonts via Ghostscript.")
39 (gui #f "running from gui; redirect stderr to log file")
41 (include-book-title-preview #t "include book-titles in preview images.")
42 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
43 (job-count #f "Process in parallel")
44 (log-file #f "redirect output to log FILE.log")
47 "relative for simultaneous music works
48 similar to chord syntax")
50 "experimental mechanism for remembering tweaks")
51 (point-and-click #t "use point & click")
52 (paper-size "a4" "the default paper size")
53 (pixmap-format "png16m" "GS format to use for pixel images")
54 (protected-scheme-parsing #t "continue when finding errors in inline
55 scheme are caught in the parser. If off, halt
56 on errors, and print a stack trace.")
57 (profile-property-accesses #f "keep statistics of get_property() calls.")
59 (resolution 101 "resolution for generating PNG bitmaps")
60 (read-file-list #f "Read files to be processed from command line arguments")
62 (safe #f "Run safely")
63 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
66 "how much verbosity for TTF font embedding?")
68 (show-available-fonts #f
69 "List font names available.")
71 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
75 ;; need to do this in the beginning. Other parts of the
76 ;; Scheme init depend on these options.
78 (define-scheme-options)
82 (if (defined? 'set-debug-cell-accesses!)
83 (set-debug-cell-accesses! #f))
85 ;(set-debug-cell-accesses! 1000)
87 (use-modules (ice-9 regex)
100 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
102 (define-public (print . args)
103 (apply format (cons (current-output-port) args)))
107 ;;; debugging evaluator is slower. This should
108 ;;; have a more sensible default.
110 (if (ly:get-option 'verbose)
112 (ly:set-option 'protected-scheme-parsing #f)
113 (debug-enable 'debug)
114 (debug-enable 'backtrace)
115 (read-enable 'positions)))
117 (define-public tex-backend?
118 (member (ly:output-backend) '("texstr" "tex")))
120 (define-public parser #f)
122 (define-public (lilypond-version)
124 (map (lambda (x) (if (symbol? x)
131 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
132 (define-public TEX_STRING_HASHLIMIT 10000000)
136 ;; gettext wrapper for guile < 1.7.2
137 (if (defined? 'gettext)
138 (define-public _ gettext)
139 (define-public _ ly:gettext))
141 (define-public (ly:load x)
142 (let* ((file-name (%search-load-path x)))
143 (if (ly:get-option 'verbose)
144 (ly:progress "[~A" file-name))
146 (ly:error (_ "cannot find: ~A") x))
147 (primitive-load file-name)
148 (if (ly:get-option 'verbose)
152 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
155 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
158 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
162 (define-public PLATFORM
165 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
168 (let ((platform (string-tokenize
169 (vector-ref (uname) 0) char-set:letter+digit)))
170 (if (null? (cdr platform)) #f
171 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
175 (define native-getcwd getcwd)
177 (if (string-index x #\\)
179 (string-regexp-substitute
181 (string-regexp-substitute "\\\\" "/" x))))
182 ;; FIXME: this prints a warning.
183 (define-public (ly-getcwd)
184 (slashify (native-getcwd))))
185 (else (define-public ly-getcwd getcwd)))
187 (define-public (is-absolute? file-name)
188 (let ((file-name-length (string-length file-name)))
189 (if (= file-name-length 0)
191 (or (eq? (string-ref file-name 0) #\/)
192 (and (eq? PLATFORM 'windows)
193 (> file-name-length 2)
194 (eq? (string-ref file-name 1) #\:)
195 (eq? (string-ref file-name 2) #\/))))))
197 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
199 (define (type-check-list location signature arguments)
200 "Typecheck a list of arguments against a list of type
201 predicates. Print a message at LOCATION if any predicate failed."
203 (define (recursion-helper signature arguments count)
204 (define (helper pred? arg count)
205 (if (not (pred? arg))
211 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
212 count (type-name pred?) arg))
216 (if (null? signature)
218 (and (helper (car signature) (car arguments) count)
219 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
221 (recursion-helper signature arguments 1))
223 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 ;;(define-public (output-framework) (write "hello\n"))
229 (define output-tex-module
230 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
231 (define output-ps-module
232 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
234 (define-public (ps-output-expression expr port)
235 (display (eval expr output-ps-module) port))
237 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
238 ;; Safe definitions utility
239 (define safe-objects (list))
241 (define-macro (define-safe-public arglist . body)
242 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
243 The syntax is the same as `define*-public'."
244 (define (get-symbol arg)
246 (get-symbol (car arg))
248 (let ((safe-symbol (get-symbol arglist)))
250 (define*-public ,arglist
252 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
256 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
259 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 "define-event-classes.scm"
271 "define-music-types.scm"
274 "chord-ignatzek-names.scm"
276 "chord-generic-names.scm"
279 "music-functions.scm"
282 "define-music-properties.scm"
286 "parser-ly-from-scheme.scm"
287 "ly-syntax-constructors.scm"
289 "define-context-properties.scm"
290 "translation-functions.scm"
300 "define-markup-commands.scm"
301 "define-grob-properties.scm"
303 "define-grob-interfaces.scm"
304 "define-stencil-commands.scm"
308 "backend-library.scm"
311 ;; must be after everything has been defined
315 (set! type-p-name-alist
317 (,boolean-or-symbol? . "boolean or symbol")
318 (,boolean? . "boolean")
320 (,grob-list? . "list of grobs")
321 (,hash-table? . "hash table")
322 (,input-port? . "input port")
323 (,integer? . "integer")
325 (,ly:context? . "context")
326 (,ly:dimension? . "dimension, in staff space")
327 (,ly:dir? . "direction")
328 (,ly:duration? . "duration")
329 (,ly:grob? . "layout object")
330 (,ly:input-location? . "input location")
331 (,ly:moment? . "moment")
332 (,ly:music? . "music")
333 (,ly:pitch? . "pitch")
334 (,ly:translator? . "translator")
335 (,ly:font-metric? . "font metric")
336 (,ly:simple-closure? . "simple closure")
337 (,markup-list? . "list of markups")
338 (,markup? . "markup")
339 (,ly:music-list? . "list of music")
340 (,number-or-grob? . "number or grob")
341 (,number-or-string? . "number or string")
342 (,number-pair? . "pair of numbers")
343 (,number? . "number")
344 (,output-port? . "output port")
346 (,procedure? . "procedure")
347 (,rhythmic-location? . "rhythmic location")
348 (,scheme? . "any type")
349 (,string? . "string")
350 (,symbol? . "symbol")
351 (,vector? . "vector")))
354 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
357 (define gc-dumping #f)
358 (define gc-protect-stat-count 0)
359 (define-public (dump-gc-protects)
360 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
361 (let* ((protects (sort
362 (hash-table->alist (ly:protects))
364 (< (object-address (car a))
365 (object-address (car b))))))
367 (out-file-name (string-append
368 "gcstat-" (number->string gc-protect-stat-count)
370 (outfile (open-file out-file-name "w")))
373 (display (format "Dumping GC statistics ~a...\n" out-file-name))
379 (format "~a (~a) = ~a\n" (object-address x) c x)
383 (not (symbol? (car x))))
387 (format outfile "\nprotected symbols: ~a\n"
388 (length (filter symbol? (map car protects))))
392 ;; (display (ly:smob-protects))
394 (if (defined? 'gc-live-object-stats)
396 (display "Live object statistics: GC'ing\n")
400 (display "Asserting dead objects\n")
401 (ly:set-option 'debug-gc-assert-parsed-dead #t)
403 (ly:set-option 'debug-gc-assert-parsed-dead #f)
405 (set! stats (gc-live-object-stats))
406 (display "Dumping live object statistics.\n")
410 (format outfile "~a: ~a\n" (car x) (cdr x)))
411 (sort (gc-live-object-stats)
413 (string<? (car x) (car y)))))))
424 gc-protect-stat-count
426 (let ((sym-stat (assoc sym stats)))
431 '(protected-objects bytes-malloced cell-heap-size
439 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
442 (define (multi-fork count)
443 (define (helper count acc)
446 ((pid (primitive-fork)))
449 (helper (1- count) (cons pid acc))))
454 (define-public (lilypond-main files)
455 "Entry point for LilyPond."
457 (define (no-files-handler)
461 (if (ly:get-option 'gui)
467 (if (ly:get-option 'read-file-list)
470 (> (string-length s) 0))
472 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
476 (if (and (number? (ly:get-option 'job-count))
477 (> (length files) (ly:get-option 'job-count)))
480 ((count (ly:get-option 'job-count))
481 (split-todo (split-list files count))
482 (joblist (multi-fork count))
485 (if (not (string-or-symbol? (ly:get-option 'log-file)))
486 (ly:set-option 'log-file "lilypond-multi-run"))
488 (if (number? joblist)
490 (ly:set-option 'log-file (format "~a-~a"
491 (ly:get-option 'log-file) joblist))
492 (set! files (vector-ref split-todo joblist)))
495 (ly:progress "\nForking into jobs: ~a\n" joblist)
498 (let* ((stat (cdr (waitpid pid))))
501 (set! errors (cons (list-element-index joblist pid) errors)))))
506 (let* ((logfile (format "~a-~a.log"
507 (ly:get-option 'log-file) x))
508 (log (ly:gulp-file logfile))
509 (len (string-length log))
510 (tail (substring log (max 0 (- len 1024)))))
512 (display (format "\n\nlogfile ~a:\n\n ~a" logfile tail))))
517 (ly:error "Children ~a exited with errors." errors))
519 (exit (if (null? errors) 0 1))))))
522 (if (string-or-symbol? (ly:get-option 'log-file))
523 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
525 (let ((failed (lilypond-all files)))
528 (ly:error (_ "failed files: ~S") (string-join failed))
531 ;; HACK: be sure to exit with single newline
535 (define-public (lilypond-all files)
538 (if (ly:get-option 'show-available-fonts)
540 (ly:font-config-display-fonts)
546 (handler (lambda (key failed-file)
547 (set! failed (append (list failed-file) failed)))))
552 ;; We don't carry info across file boundaries
557 (lilypond-file handler x)
558 (ly:clear-anonymous-modules)
559 (if (ly:get-option 'debug-gc)
561 (if (= (random 40) 1)
562 (ly:reset-all-fonts))))
567 (define (lilypond-file handler file-name)
568 (catch 'ly-file-failed
569 (lambda () (ly:parse-file file-name))
570 (lambda (x . args) (handler x file-name))))
572 (use-modules (scm editor))
574 (define-public (gui-main files)
576 (gui-no-files-handler))
578 (if (not (string? (ly:get-option 'log-file)))
579 (let* ((base (basename (car files) ".ly"))
580 (log-name (string-append base ".log")))
581 (if (not (ly:get-option 'gui))
582 (ly:message (_ "Redirecting output to ~a...") log-name))
583 (ly:stderr-redirect log-name "w")
584 (ly:message "# -*-compilation-*-"))
586 (let ((failed (lilypond-all files)))
590 (ly:stderr-redirect "foo" "r")
591 (system (get-editor-command log-name 0 0 0))
592 (ly:error (_ "failed files: ~S") (string-join failed))
597 (define (gui-no-files-handler)
598 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
599 ;; FIXME: soft-code, localize
600 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
601 (cmd (get-editor-command welcome-ly 0 0 0)))
602 (ly:message (_ "Invoking `~a'...") cmd)