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 (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 "load fonts via Ghostscript.")
36 (include-book-title-preview #t "include book-titles in preview images.")
37 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
38 (job-count #f "Process in parallel")
40 (eps-box-padding #f "Pad EPS bounding box left edge by this much to guarantee alignment between systems")
42 (gui #f "running from gui; redirect stderr to log file")
43 (log-file #f "redirect output to log FILE.log")
45 "relative for simultaneous music works
46 similar to chord syntax")
48 "experimental mechanism for remembering tweaks")
49 (point-and-click #t "use point & click")
50 (paper-size "a4" "the default paper size")
51 (protected-scheme-parsing #t "continue when finding errors in inline
52 scheme are caught in the parser. If off, halt
53 on errors, and print a stack trace.")
54 (profile-property-accesses #f "keep statistics of get_property() calls.")
56 (resolution 101 "resolution for generating PNG bitmaps")
57 (read-file-list #f "Read files to be processed from command line arguments")
59 (safe #f "Run safely")
60 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
63 "how much verbosity for TTF font embedding?")
65 (show-available-fonts #f
66 "List font names available.")
68 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
72 ;; need to do this in the beginning. Other parts of the
73 ;; Scheme init depend on these options.
75 (define-scheme-options)
79 (if (defined? 'set-debug-cell-accesses!)
80 (set-debug-cell-accesses! #f))
82 ;(set-debug-cell-accesses! 1000)
84 (use-modules (ice-9 regex)
97 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
99 (define-public (print . args)
100 (apply format (cons (current-output-port) args)))
104 ;;; debugging evaluator is slower. This should
105 ;;; have a more sensible default.
107 (if (ly:get-option 'verbose)
109 (debug-enable 'debug)
110 (debug-enable 'backtrace)
111 (read-enable 'positions)))
113 (define-public tex-backend?
114 (member (ly:output-backend) '("texstr" "tex")))
116 (define-public parser #f)
118 (define-public (lilypond-version)
120 (map (lambda (x) (if (symbol? x)
127 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
128 (define-public TEX_STRING_HASHLIMIT 10000000)
132 ;; gettext wrapper for guile < 1.7.2
133 (if (defined? 'gettext)
134 (define-public _ gettext)
135 (define-public _ ly:gettext))
137 (define-public (ly:load x)
138 (let* ((file-name (%search-load-path x)))
139 (if (ly:get-option 'verbose)
140 (ly:progress "[~A" file-name))
142 (ly:error (_ "Can't find ~A" x)))
143 (primitive-load file-name)
144 (if (ly:get-option 'verbose)
148 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
151 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
154 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
158 (define-public PLATFORM
161 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
164 (let ((platform (string-tokenize
165 (vector-ref (uname) 0) char-set:letter+digit)))
166 (if (null? (cdr platform)) #f
167 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
171 (define native-getcwd getcwd)
173 (if (string-index x #\\)
175 (string-regexp-substitute
177 (string-regexp-substitute "\\\\" "/" x))))
178 ;; FIXME: this prints a warning.
179 (define-public (ly-getcwd)
180 (slashify (native-getcwd))))
181 (else (define-public ly-getcwd getcwd)))
183 (define-public (is-absolute? file-name)
184 (let ((file-name-length (string-length file-name)))
185 (if (= file-name-length 0)
187 (or (eq? (string-ref file-name 0) #\/)
188 (and (eq? PLATFORM 'windows)
189 (> file-name-length 2)
190 (eq? (string-ref file-name 1) #\:)
191 (eq? (string-ref file-name 2) #\/))))))
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195 (define (type-check-list location signature arguments)
196 "Typecheck a list of arguments against a list of type
197 predicates. Print a message at LOCATION if any predicate failed."
199 (define (recursion-helper signature arguments count)
200 (define (helper pred? arg count)
201 (if (not (pred? arg))
207 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
208 count (type-name pred?) arg))
212 (if (null? signature)
214 (and (helper (car signature) (car arguments) count)
215 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
217 (recursion-helper signature arguments 1))
219 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;;(define-public (output-framework) (write "hello\n"))
225 (define output-tex-module
226 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
227 (define output-ps-module
228 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
230 (define-public (ps-output-expression expr port)
231 (display (eval expr output-ps-module) port))
233 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234 ;; Safe definitions utility
235 (define safe-objects (list))
237 (define-macro (define-safe-public arglist . body)
238 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
239 The syntax is the same as `define*-public'."
240 (define (get-symbol arg)
242 (get-symbol (car arg))
244 (let ((safe-symbol (get-symbol arglist)))
246 (define*-public ,arglist
248 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
252 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255 (ly:set-default-scale (ly:make-scale #(0 2 4 5 7 9 11)))
259 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266 "define-event-classes.scm"
267 "define-music-types.scm"
270 "chord-ignatzek-names.scm"
272 "chord-generic-names.scm"
275 "music-functions.scm"
278 "define-music-properties.scm"
282 "parser-ly-from-scheme.scm"
283 "ly-syntax-constructors.scm"
285 "define-context-properties.scm"
286 "translation-functions.scm"
296 "define-markup-commands.scm"
297 "define-grob-properties.scm"
299 "define-grob-interfaces.scm"
300 "define-stencil-commands.scm"
304 "backend-library.scm"
307 ;; must be after everything has been defined
311 (set! type-p-name-alist
313 (,boolean-or-symbol? . "boolean or symbol")
314 (,boolean? . "boolean")
316 (,grob-list? . "list of grobs")
317 (,hash-table? . "hash table")
318 (,input-port? . "input port")
319 (,integer? . "integer")
321 (,ly:context? . "context")
322 (,ly:dimension? . "dimension, in staff space")
323 (,ly:dir? . "direction")
324 (,ly:duration? . "duration")
325 (,ly:grob? . "layout object")
326 (,ly:input-location? . "input location")
327 (,ly:moment? . "moment")
328 (,ly:music? . "music")
329 (,ly:pitch? . "pitch")
330 (,ly:translator? . "translator")
331 (,ly:font-metric? . "font metric")
332 (,ly:simple-closure? . "simple closure")
333 (,markup-list? . "list of markups")
334 (,markup? . "markup")
335 (,ly:music-list? . "list of music")
336 (,number-or-grob? . "number or grob")
337 (,number-or-string? . "number or string")
338 (,number-pair? . "pair of numbers")
339 (,number? . "number")
340 (,output-port? . "output port")
342 (,procedure? . "procedure")
343 (,rhythmic-location? . "rhythmic location")
344 (,scheme? . "any type")
345 (,string? . "string")
346 (,symbol? . "symbol")
347 (,vector? . "vector")))
352 (define gc-protect-stat-count 0)
353 (define-public (dump-gc-protects)
354 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
355 (let* ((protects (sort
356 (hash-table->alist (ly:protects))
358 (< (object-address (car a))
359 (object-address (car b))))))
361 (out-file-name (string-append
362 "gcstat-" (number->string gc-protect-stat-count)
364 (outfile (open-file out-file-name "w")))
366 (display (format "Dumping GC statistics ~a...\n" out-file-name))
372 (format "~a (~a) = ~a\n" (object-address x) c x)
376 (not (symbol? (car x))))
380 (format outfile "\nprotected symbols: ~a\n"
381 (length (filter symbol? (map car protects))))
385 ;; (display (ly:smob-protects))
387 (if (defined? 'gc-live-object-stats)
389 (display "Live object statistics: GC'ing\n")
393 (ly:set-option 'debug-gc-assert-parsed-dead #t)
395 (ly:set-option 'debug-gc-assert-parsed-dead #f)
397 (set! stats (gc-live-object-stats))
398 (display "Dumping live object statistics.\n")
402 (format outfile "~a: ~a\n" (car x) (cdr x)))
403 (sort (gc-live-object-stats)
405 (string<? (car x) (car y)))))))
416 gc-protect-stat-count
418 (cdr (assoc sym stats)))
420 '(protected-objects bytes-malloced cell-heap-size
426 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429 (define (multi-fork count)
430 (define (helper count acc)
433 ((pid (primitive-fork)))
436 (helper (1- count) (cons pid acc))))
441 (define-public (lilypond-main files)
442 "Entry point for LilyPond."
444 (define (no-files-handler)
448 (if (ly:get-option 'gui)
454 (if (ly:get-option 'read-file-list)
457 (> (string-length s) 0))
459 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
463 (if (and (number? (ly:get-option 'job-count))
464 (> (length files) (ly:get-option 'job-count)))
467 ((count (ly:get-option 'job-count))
468 (split-todo (split-list files count))
469 (joblist (multi-fork count))
472 (if (not (string-or-symbol? (ly:get-option 'log-file)))
473 (ly:set-option 'log-file "lilypond-multi-run"))
475 (if (number? joblist)
477 (ly:set-option 'log-file (format "~a-~a"
478 (ly:get-option 'log-file) joblist))
479 (set! files (vector-ref split-todo joblist)))
482 (ly:progress "\nForking into jobs: ~a\n" joblist)
485 (let* ((stat (cdr (waitpid pid))))
488 (set! errors (cons (list-element-index joblist pid) errors)))))
493 (let* ((logfile (format "~a-~a.log"
494 (ly:get-option 'log-file) x))
495 (log (ly:gulp-file logfile))
496 (len (string-length log))
497 (tail (substring log (max 0 (- len 1024)))))
499 (display (format "\n\nlogfile ~a:\n\n ~a" logfile tail))))
504 (ly:error "Children ~a exited with errors." errors))
506 (exit (if (null? errors) 0 1))))))
509 (if (string-or-symbol? (ly:get-option 'log-file))
510 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
512 (let ((failed (lilypond-all files)))
515 (ly:error (_ "failed files: ~S") (string-join failed))
518 ;; HACK: be sure to exit with single newline
522 (define-public (lilypond-all files)
525 (if (ly:get-option 'show-available-fonts)
527 (ly:font-config-display-fonts)
532 (handler (lambda (key failed-file)
533 (set! failed (append (list failed-file) failed)))))
537 (lilypond-file handler x)
538 (ly:clear-anonymous-modules)
539 (if (ly:get-option 'debug-gc)
542 (ly:reset-all-fonts))))
547 (define (lilypond-file handler file-name)
548 (catch 'ly-file-failed
549 (lambda () (ly:parse-file file-name))
550 (lambda (x . args) (handler x file-name))))
552 (use-modules (scm editor))
554 (define-public (gui-main files)
556 (gui-no-files-handler))
558 (if (not (string? (ly:get-option 'log-file)))
559 (let* ((base (basename (car files) ".ly"))
560 (log-name (string-append base ".log")))
561 (if (not (ly:get-option 'gui))
562 (ly:message (_ "Redirecting output to ~a...") log-name))
563 (ly:stderr-redirect log-name "w")
564 (ly:message "# -*-compilation-*-"))
566 (let ((failed (lilypond-all files)))
570 (ly:stderr-redirect "foo" "r")
571 (system (get-editor-command log-name 0 0 0))
572 (ly:error (_ "failed files: ~S") (string-join failed))
577 (define (gui-no-files-handler)
578 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
579 ;; FIXME: soft-code, localize
580 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
581 (cmd (get-editor-command welcome-ly 0 0 0)))
582 (ly:message (_ "Invoking `~a'...") cmd)