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-midi #f "generate human readable MIDI")
26 (delete-intermediate-files #f
27 "delete unusable PostScript files")
28 (dump-signatures #f "dump output signatures of each system")
29 (dump-tweaks #f "dump page layout and tweaks for each score having the tweak-key layout property set.")
31 "load fonts via Ghostscript.")
32 (include-book-title-preview #t "include book-titles in preview images.")
33 (include-eps-fonts #t "Include fonts in separate-system EPS files.")
35 (pad-eps-boxes #f "Pad EPS bounding boxes to guarantee alignment between systems")
37 (gui #f "running from gui; redirect stderr to log file")
40 "relative for simultaneous music works
41 similar to chord syntax")
43 "experimental mechanism for remembering tweaks")
44 (point-and-click #t "use point & click")
45 (paper-size "a4" "the default paper size")
46 (protected-scheme-parsing #t "continue when finding errors in inline
47 scheme are caught in the parser. If off, halt
48 on errors, and print a stack trace.")
49 (profile-property-accesses #f "keep statistics of get_property() calls.")
51 (resolution 101 "resolution for generating bitmaps")
52 (read-file-list #f "Read files to be processed from command line arguments")
54 (safe #f "Run safely")
55 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
58 "how much verbosity for TTF font embedding?")
60 (show-available-fonts #f
61 "List font names available.")
63 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
67 ;; need to do this in the beginning. Other parts of the
68 ;; Scheme init depend on these options.
70 (define-scheme-options)
72 (if (defined? 'set-debug-cell-accesses!)
73 (set-debug-cell-accesses! #f))
75 ;(set-debug-cell-accesses! 1000)
77 (use-modules (ice-9 regex)
90 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
92 (define-public (print . args)
93 (apply format (cons (current-output-port) args)))
97 ;;; debugging evaluator is slower. This should
98 ;;; have a more sensible default.
100 (if (ly:get-option 'verbose)
102 (debug-enable 'debug)
103 (debug-enable 'backtrace)
104 (read-enable 'positions)))
106 (define-public tex-backend?
107 (member (ly:output-backend) '("texstr" "tex")))
109 (define-public parser #f)
111 (define-public (lilypond-version)
113 (map (lambda (x) (if (symbol? x)
120 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
121 (define-public TEX_STRING_HASHLIMIT 10000000)
125 ;; gettext wrapper for guile < 1.7.2
126 (if (defined? 'gettext)
127 (define-public _ gettext)
128 (define-public _ ly:gettext))
130 (define-public (ly:load x)
131 (let* ((file-name (%search-load-path x)))
132 (if (ly:get-option 'verbose)
133 (ly:progress "[~A" file-name))
135 (ly:error (_ "Can't find ~A" x)))
136 (primitive-load file-name)
137 (if (ly:get-option 'verbose)
141 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
144 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
147 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
151 (define-public PLATFORM
154 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
157 (let ((platform (string-tokenize
158 (vector-ref (uname) 0) char-set:letter+digit)))
159 (if (null? (cdr platform)) #f
160 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
164 (define native-getcwd getcwd)
166 (if (string-index x #\\)
168 (string-regexp-substitute
170 (string-regexp-substitute "\\\\" "/" x))))
171 ;; FIXME: this prints a warning.
172 (define-public (ly-getcwd)
173 (slashify (native-getcwd))))
174 (else (define-public ly-getcwd getcwd)))
176 (define-public (is-absolute? file-name)
177 (let ((file-name-length (string-length file-name)))
178 (if (= file-name-length 0)
180 (or (eq? (string-ref file-name 0) #\/)
181 (and (eq? PLATFORM 'windows)
182 (> file-name-length 2)
183 (eq? (string-ref file-name 1) #\:)
184 (eq? (string-ref file-name 2) #\/))))))
186 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
188 (define (type-check-list location signature arguments)
189 "Typecheck a list of arguments against a list of type
190 predicates. Print a message at LOCATION if any predicate failed."
192 (define (recursion-helper signature arguments count)
193 (define (helper pred? arg count)
194 (if (not (pred? arg))
200 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
201 count (type-name pred?) arg))
205 (if (null? signature)
207 (and (helper (car signature) (car arguments) count)
208 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
210 (recursion-helper signature arguments 1))
212 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
216 ;;(define-public (output-framework) (write "hello\n"))
218 (define output-tex-module
219 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
220 (define output-ps-module
221 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
223 (define-public (ps-output-expression expr port)
224 (display (eval expr output-ps-module) port))
226 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 ;; Safe definitions utility
228 (define safe-objects (list))
230 (define-macro (define-safe-public arglist . body)
231 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
232 The syntax is the same as `define*-public'."
233 (define (get-symbol arg)
235 (get-symbol (car arg))
237 (let ((safe-symbol (get-symbol arglist)))
239 (define*-public ,arglist
241 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
245 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248 (ly:set-default-scale (ly:make-scale #(0 2 4 5 7 9 11)))
252 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
259 "define-event-classes.scm"
260 "define-music-types.scm"
263 "chord-ignatzek-names.scm"
265 "chord-generic-names.scm"
268 "music-functions.scm"
271 "define-music-properties.scm"
275 "parser-ly-from-scheme.scm"
276 "ly-syntax-constructors.scm"
278 "define-context-properties.scm"
279 "translation-functions.scm"
289 "define-markup-commands.scm"
290 "define-grob-properties.scm"
292 "define-grob-interfaces.scm"
293 "define-stencil-commands.scm"
297 "backend-library.scm"
300 ;; must be after everything has been defined
304 (set! type-p-name-alist
306 (,boolean-or-symbol? . "boolean or symbol")
307 (,boolean? . "boolean")
309 (,grob-list? . "list of grobs")
310 (,hash-table? . "hash table")
311 (,input-port? . "input port")
312 (,integer? . "integer")
314 (,ly:context? . "context")
315 (,ly:dimension? . "dimension, in staff space")
316 (,ly:dir? . "direction")
317 (,ly:duration? . "duration")
318 (,ly:grob? . "layout object")
319 (,ly:input-location? . "input location")
320 (,ly:moment? . "moment")
321 (,ly:music? . "music")
322 (,ly:pitch? . "pitch")
323 (,ly:translator? . "translator")
324 (,ly:font-metric? . "font metric")
325 (,ly:simple-closure? . "simple closure")
326 (,markup-list? . "list of markups")
327 (,markup? . "markup")
328 (,ly:music-list? . "list of music")
329 (,number-or-grob? . "number or grob")
330 (,number-or-string? . "number or string")
331 (,number-pair? . "pair of numbers")
332 (,number? . "number")
333 (,output-port? . "output port")
335 (,procedure? . "procedure")
336 (,rhythmic-location? . "rhythmic location")
337 (,scheme? . "any type")
338 (,string? . "string")
339 (,symbol? . "symbol")
340 (,vector? . "vector")))
345 (define gc-protect-stat-count 0)
346 (define-public (dump-gc-protects)
347 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
348 (let* ((protects (sort
349 (hash-table->alist (ly:protects))
351 (< (object-address (car a))
352 (object-address (car b))))))
354 (out-file-name (string-append
355 "gcstat-" (number->string gc-protect-stat-count)
357 (outfile (open-file out-file-name "w")))
359 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
367 (map object->string (list (object-address x) c x))
373 (not (symbol? (car x))))
377 ; (display (ly:smob-protects))
379 (if (defined? 'gc-live-object-stats)
381 (display "Live object statistics: GC'ing\n")
385 (set! stats (gc-live-object-stats))
386 (display "Dumping live object statistics.\n")
390 (format outfile "~a: ~a\n" (car x) (cdr x)))
391 (sort (gc-live-object-stats)
393 (string<? (car x) (car y)))))))))
395 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
398 (define-public (lilypond-main files)
399 "Entry point for LilyPond."
401 (define (no-files-handler)
405 (if (ly:get-option 'gui)
411 (let ((failed (lilypond-all files)))
414 (ly:error (_ "failed files: ~S") (string-join failed))
417 ;; HACK: be sure to exit with single newline
421 (define-public (lilypond-all files)
422 (if (ly:get-option 'read-file-list)
425 (> (string-length s) 0))
427 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
431 (if (ly:get-option 'show-available-fonts)
433 (ly:font-config-display-fonts)
438 (handler (lambda (key failed-file)
439 (set! failed (append (list failed-file) failed)))))
443 (lilypond-file handler x)
444 (ly:clear-anonymous-modules)
445 (if (ly:get-option 'debug-gc)
451 (define (lilypond-file handler file-name)
452 (catch 'ly-file-failed
453 (lambda () (ly:parse-file file-name))
454 (lambda (x . args) (handler x file-name))))
456 (use-modules (scm editor)
459 (define-public (gui-main files)
461 (gui-no-files-handler))
462 (let* ((base (basename (car files) ".ly"))
463 (log-name (string-append base ".log")))
464 (if (not (ly:get-option 'gui))
465 (ly:message (_ "Redirecting output to ~a...") log-name))
466 (ly:stderr-redirect log-name "w")
467 (ly:message "# -*-compilation-*-")
469 (let ((failed (lilypond-all files)))
473 (ly:stderr-redirect "foo" "r")
474 (system (get-editor-command log-name 0 0 0))
475 (ly:error (_ "failed files: ~S") (string-join failed))
480 (define (gui-no-files-handler)
481 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
482 ;; FIXME: soft-code, localize
483 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
484 (cmd (get-editor-command welcome-ly 0 0 0)))
485 (ly:message (_ "Invoking `~a'...") cmd)