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@cs.uu.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")
23 "dump memory debugging statistics")
24 (debug-midi #f "generate human readable MIDI")
25 (delete-intermediate-files #f
26 "delete unusable PostScript files")
27 (dump-signatures #f "dump output signatures of each system")
29 "load fonts via Ghostscript.")
30 (include-book-title-preview #t "include book-titles in preview images.")
31 (include-eps-fonts #f "Include fonts in separate-system EPS files.")
33 (pad-eps-boxes #f "Pad EPS bounding boxes to guarantee alignment between systems")
35 (gui #f "running from gui; redirect stderr to log file")
38 "relative for simultaneous music works
39 similar to chord syntax")
41 "experimental mechanism for remembering tweaks")
42 (point-and-click #t "use point & click")
43 (paper-size "a4" "the default paper size")
44 (protected-scheme-parsing #t "continue when finding errors in inline
45 scheme are caught in the parser. If off, halt
46 on errors, and print a stack trace.")
47 (profile-property-accesses #f "keep statistics of get_property() calls.")
49 (resolution 101 "resolution for generating bitmaps")
50 (read-file-list #f "Read files to be processed from command line arguments")
52 (safe #f "Run safely")
53 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
56 "how much verbosity for TTF font embedding?")
58 (show-available-fonts #f
59 "List font names available.")
61 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
65 ;; need to do this in the beginning. Other parts of the
66 ;; Scheme init depend on these options.
68 (define-scheme-options)
70 (if (defined? 'set-debug-cell-accesses!)
71 (set-debug-cell-accesses! #f))
73 ;(set-debug-cell-accesses! 1000)
75 (use-modules (ice-9 regex)
85 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
87 (define-public (print . args)
88 (apply format (cons (current-output-port) args)))
92 ;;; debugging evaluator is slower. This should
93 ;;; have a more sensible default.
95 (if (ly:get-option 'verbose)
98 (debug-enable 'backtrace)
99 (read-enable 'positions)))
101 (define-public tex-backend?
102 (member (ly:output-backend) '("texstr" "tex")))
104 (define-public parser #f)
106 (define-public (lilypond-version)
108 (map (lambda (x) (if (symbol? x)
115 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
116 (define-public TEX_STRING_HASHLIMIT 10000000)
120 ;; gettext wrapper for guile < 1.7.2
121 (if (defined? 'gettext)
122 (define-public _ gettext)
123 (define-public _ ly:gettext))
125 (define-public (ly:load x)
126 (let* ((file-name (%search-load-path x)))
127 (if (ly:get-option 'verbose)
128 (ly:progress "[~A" file-name))
130 (ly:error (_ "Can't find ~A" x)))
131 (primitive-load file-name)
132 (if (ly:get-option 'verbose)
136 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
139 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
142 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
146 (define-public PLATFORM
149 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
152 (let ((platform (string-tokenize
153 (vector-ref (uname) 0) char-set:letter+digit)))
154 (if (null? (cdr platform)) #f
155 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
159 (define native-getcwd getcwd)
161 (if (string-index x #\\)
163 (string-regexp-substitute
165 (string-regexp-substitute "\\\\" "/" x))))
166 ;; FIXME: this prints a warning.
167 (define-public (ly-getcwd)
168 (slashify (native-getcwd))))
169 (else (define-public ly-getcwd getcwd)))
171 (define-public (is-absolute? file-name)
172 (let ((file-name-length (string-length file-name)))
173 (if (= file-name-length 0)
175 (or (eq? (string-ref file-name 0) #\/)
176 (and (eq? PLATFORM 'windows)
177 (> file-name-length 2)
178 (eq? (string-ref file-name 1) #\:)
179 (eq? (string-ref file-name 2) #\/))))))
181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
183 (define (type-check-list location signature arguments)
184 "Typecheck a list of arguments against a list of type
185 predicates. Print a message at LOCATION if any predicate failed."
187 (define (recursion-helper signature arguments count)
188 (define (helper pred? arg count)
189 (if (not (pred? arg))
195 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
196 count (type-name pred?) arg))
200 (if (null? signature)
202 (and (helper (car signature) (car arguments) count)
203 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
205 (recursion-helper signature arguments 1))
207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
211 ;;(define-public (output-framework) (write "hello\n"))
213 (define output-tex-module
214 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
215 (define output-ps-module
216 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
218 (define-public (ps-output-expression expr port)
219 (display (eval expr output-ps-module) port))
221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
222 ;; Safe definitions utility
223 (define safe-objects (list))
225 (define-macro (define-safe-public arglist . body)
226 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
227 The syntax is the same as `define*-public'."
228 (define (get-symbol arg)
230 (get-symbol (car arg))
232 (let ((safe-symbol (get-symbol arglist)))
234 (define*-public ,arglist
236 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
241 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248 "define-event-classes.scm"
249 "define-music-types.scm"
252 "chord-ignatzek-names.scm"
254 "chord-generic-names.scm"
257 "music-functions.scm"
260 "define-music-properties.scm"
264 "parser-ly-from-scheme.scm"
265 "ly-syntax-constructors.scm"
267 "define-context-properties.scm"
268 "translation-functions.scm"
278 "define-markup-commands.scm"
279 "define-grob-properties.scm"
281 "define-grob-interfaces.scm"
282 "define-stencil-commands.scm"
286 "backend-library.scm"
289 ;; must be after everything has been defined
293 (set! type-p-name-alist
295 (,boolean-or-symbol? . "boolean or symbol")
296 (,boolean? . "boolean")
298 (,grob-list? . "list of grobs")
299 (,hash-table? . "hash table")
300 (,input-port? . "input port")
301 (,integer? . "integer")
303 (,ly:context? . "context")
304 (,ly:dimension? . "dimension, in staff space")
305 (,ly:dir? . "direction")
306 (,ly:duration? . "duration")
307 (,ly:grob? . "layout object")
308 (,ly:input-location? . "input location")
309 (,ly:moment? . "moment")
310 (,ly:music? . "music")
311 (,ly:pitch? . "pitch")
312 (,ly:translator? . "translator")
313 (,ly:font-metric? . "font metric")
314 (,ly:simple-closure? . "simple closure")
315 (,markup-list? . "list of markups")
316 (,markup? . "markup")
317 (,ly:music-list? . "list of music")
318 (,number-or-grob? . "number or grob")
319 (,number-or-string? . "number or string")
320 (,number-pair? . "pair of numbers")
321 (,number? . "number")
322 (,output-port? . "output port")
324 (,procedure? . "procedure")
325 (,scheme? . "any type")
326 (,string? . "string")
327 (,symbol? . "symbol")
328 (,vector? . "vector")))
333 (define gc-protect-stat-count 0)
334 (define-public (dump-gc-protects)
335 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
336 (let* ((protects (sort
337 (hash-table->alist (ly:protects))
339 (< (object-address (car a))
340 (object-address (car b))))))
342 (out-file-name (string-append
343 "gcstat-" (number->string gc-protect-stat-count)
345 (outfile (open-file out-file-name "w")))
347 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
355 (map object->string (list (object-address x) c x))
361 (not (symbol? (car x))))
365 ; (display (ly:smob-protects))
367 (if (defined? 'gc-live-object-stats)
369 (display "Live object statistics: GC'ing\n")
373 (set! stats (gc-live-object-stats))
374 (display "Dumping live object statistics.\n")
378 (format outfile "~a: ~a\n" (car x) (cdr x)))
379 (sort (gc-live-object-stats)
381 (string<? (car x) (car y)))))))))
383 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 (define-public (lilypond-main files)
387 "Entry point for LilyPond."
389 (define (no-files-handler)
393 (if (ly:get-option 'gui)
399 (let ((failed (lilypond-all files)))
402 (ly:error (_ "failed files: ~S") (string-join failed))
405 ;; HACK: be sure to exit with single newline
409 (define-public (lilypond-all files)
410 (if (ly:get-option 'read-file-list)
413 (> (string-length s) 0))
415 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
419 (if (ly:get-option 'show-available-fonts)
421 (ly:font-config-display-fonts)
426 (handler (lambda (key failed-file)
427 (set! failed (append (list failed-file) failed)))))
431 (lilypond-file handler x)
432 (ly:clear-anonymous-modules)
433 (if (ly:get-option 'debug-gc)
439 (define (lilypond-file handler file-name)
440 (catch 'ly-file-failed
441 (lambda () (ly:parse-file file-name))
442 (lambda (x . args) (handler x file-name))))
444 (use-modules (scm editor))
446 (define-public (gui-main files)
448 (gui-no-files-handler))
449 (let* ((base (basename (car files) ".ly"))
450 (log-name (string-append base ".log")))
451 (if (not (ly:get-option 'gui))
452 (ly:message (_ "Redirecting output to ~a...") log-name))
453 (ly:stderr-redirect log-name "w")
454 (ly:message "# -*-compilation-*-")
456 (let ((failed (lilypond-all files)))
460 (ly:stderr-redirect "foo" "r")
461 (system (get-editor-command log-name 0 0 0))
462 (ly:error (_ "failed files: ~S") (string-join failed))
467 (define (gui-no-files-handler)
468 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
469 ;; FIXME: soft-code, localize
470 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
471 (cmd (get-editor-command welcome-ly 0 0 0)))
472 (ly:message (_ "Invoking `~a'...") cmd)