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")
24 "dump memory debugging statistics")
25 (debug-gc-assert-parsed-dead #f
26 "for memory debugging: ensure that all refs to parsed objects are dead.")
28 (debug-midi #f "generate human readable MIDI")
29 (debug-parser #f "debug the bison parser")
30 (debug-lexer #f "debug the flex lexer")
31 (debug-midi #f "generate human readable MIDI")
32 (debug-parser #f "debug the bison parser")
33 (debug-lexer #f "debug the flex lexer")
34 (debug-midi #f "generate human readable MIDI")
35 (delete-intermediate-files #f
36 "delete unusable PostScript files")
37 (dump-signatures #f "dump output signatures of each system")
38 (dump-tweaks #f "dump page layout and tweaks for each score having the tweak-key layout property set.")
40 "load fonts via Ghostscript.")
41 (include-book-title-preview #t "include book-titles in preview images.")
42 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
44 (eps-box-padding #f "Pad EPS bounding box left edge by this much to guarantee alignment between systems")
46 (gui #f "running from gui; redirect stderr to log file")
49 "relative for simultaneous music works
50 similar to chord syntax")
52 "experimental mechanism for remembering tweaks")
53 (point-and-click #t "use point & click")
54 (paper-size "a4" "the default paper size")
55 (protected-scheme-parsing #t "continue when finding errors in inline
56 scheme are caught in the parser. If off, halt
57 on errors, and print a stack trace.")
58 (profile-property-accesses #f "keep statistics of get_property() calls.")
60 (resolution 101 "resolution for generating bitmaps")
61 (read-file-list #f "Read files to be processed from command line arguments")
63 (safe #f "Run safely")
64 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
67 "how much verbosity for TTF font embedding?")
69 (show-available-fonts #f
70 "List font names available.")
72 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
76 ;; need to do this in the beginning. Other parts of the
77 ;; Scheme init depend on these options.
79 (define-scheme-options)
83 (if (defined? 'set-debug-cell-accesses!)
84 (set-debug-cell-accesses! #f))
86 ;(set-debug-cell-accesses! 1000)
88 (use-modules (ice-9 regex)
101 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
103 (define-public (print . args)
104 (apply format (cons (current-output-port) args)))
108 ;;; debugging evaluator is slower. This should
109 ;;; have a more sensible default.
111 (if (ly:get-option 'verbose)
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 (_ "Can't 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 2 4 5 7 9 11)))
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")))
356 (define gc-protect-stat-count 0)
357 (define-public (dump-gc-protects)
358 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
359 (let* ((protects (sort
360 (hash-table->alist (ly:protects))
362 (< (object-address (car a))
363 (object-address (car b))))))
365 (out-file-name (string-append
366 "gcstat-" (number->string gc-protect-stat-count)
368 (outfile (open-file out-file-name "w")))
370 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
378 (map object->string (list (object-address x) c x))
384 (not (symbol? (car x))))
388 ; (display (ly:smob-protects))
390 (if (defined? 'gc-live-object-stats)
392 (display "Live object statistics: GC'ing\n")
395 (ly:set-option 'debug-gc-assert-parsed-dead #t)
398 (set! stats (gc-live-object-stats))
399 (display "Dumping live object statistics.\n")
403 (format outfile "~a: ~a\n" (car x) (cdr x)))
404 (sort (gc-live-object-stats)
406 (string<? (car x) (car y)))))))))
408 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
411 (define-public (lilypond-main files)
412 "Entry point for LilyPond."
414 (define (no-files-handler)
418 (if (ly:get-option 'gui)
424 (let ((failed (lilypond-all files)))
427 (ly:error (_ "failed files: ~S") (string-join failed))
430 ;; HACK: be sure to exit with single newline
434 (define-public (lilypond-all files)
435 (if (ly:get-option 'read-file-list)
438 (> (string-length s) 0))
440 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
444 (if (ly:get-option 'show-available-fonts)
446 (ly:font-config-display-fonts)
451 (handler (lambda (key failed-file)
452 (set! failed (append (list failed-file) failed)))))
456 (ly:set-option 'debug-gc-assert-parsed-dead #f)
457 (lilypond-file handler x)
458 (ly:clear-anonymous-modules)
459 (if (ly:get-option 'debug-gc)
465 (define (lilypond-file handler file-name)
466 (catch 'ly-file-failed
467 (lambda () (ly:parse-file file-name))
468 (lambda (x . args) (handler x file-name))))
470 (use-modules (scm editor))
472 (define-public (gui-main files)
474 (gui-no-files-handler))
475 (let* ((base (basename (car files) ".ly"))
476 (log-name (string-append base ".log")))
477 (if (not (ly:get-option 'gui))
478 (ly:message (_ "Redirecting output to ~a...") log-name))
479 (ly:stderr-redirect log-name "w")
480 (ly:message "# -*-compilation-*-")
482 (let ((failed (lilypond-all files)))
486 (ly:stderr-redirect "foo" "r")
487 (system (get-editor-command log-name 0 0 0))
488 (ly:error (_ "failed files: ~S") (string-join failed))
493 (define (gui-no-files-handler)
494 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
495 ;; FIXME: soft-code, localize
496 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
497 (cmd (get-editor-command welcome-ly 0 0 0)))
498 (ly:message (_ "Invoking `~a'...") cmd)