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)))
13 `((point-and-click #t "use point & click")
14 (paper-size "a4" "the default paper size")
15 (midi-debug #f "generate human readable MIDI")
16 (dump-signatures #f "dump output signatures of each system (EPS backend)")
17 (internal-type-checking #f "check every property assignment for types")
18 (parse-protect #t "continue when finding errors in inline
19 scheme are caught in the parser. If off, halt
20 on errors, and print a stack trace.")
21 (profile-property-accesses #f "keep statistics of get_property() calls.")
23 "relative for simultaneous music works
24 similar to chord syntax")
26 "experimental mechanism for remembering tweaks")
27 (resolution 101 "resolution for generating bitmaps")
28 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
29 (book-title-preview #t "include book-titles in preview images.")
30 (eps-font-include #f "Include fonts in separate-system EPS files.")
32 "load fonts via Ghostscript.")
33 (gui #f "running from gui; redirect stderr to log file")
34 (delete-intermediate-files #f
35 "delete unusable PostScript files")
36 (safe #f "Run safely")
37 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
38 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
40 "how much verbosity for TTF font embedding?")
42 "dump GC protection info")
43 (show-available-fonts #f
44 "List font names available.")
48 ;; need to do this in the beginning. Other parts of the
49 ;; Scheme init depend on these options.
51 (define-scheme-options)
53 (if (defined? 'set-debug-cell-accesses!)
54 (set-debug-cell-accesses! #f))
56 ;(set-debug-cell-accesses! 1000)
58 (use-modules (ice-9 regex)
68 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
70 (define-public (print . args)
71 (apply format (cons (current-output-port) args)))
75 ;;; debugging evaluator is slower. This should
76 ;;; have a more sensible default.
78 (if (ly:get-option 'verbose)
81 (debug-enable 'backtrace)
82 (read-enable 'positions)))
84 (define-public tex-backend?
85 (member (ly:output-backend) '("texstr" "tex")))
87 (define-public parser #f)
89 (define-public (lilypond-version)
91 (map (lambda (x) (if (symbol? x)
98 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
99 (define-public TEX_STRING_HASHLIMIT 10000000)
103 ;; gettext wrapper for guile < 1.7.2
104 (if (defined? 'gettext)
105 (define-public _ gettext)
106 (define-public _ ly:gettext))
108 (define-public (ly:load x)
109 (let* ((file-name (%search-load-path x)))
110 (if (ly:get-option 'verbose)
111 (ly:progress "[~A" file-name))
113 (ly:error (_ "Can't find ~A" x)))
114 (primitive-load file-name)
115 (if (ly:get-option 'verbose)
119 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
122 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
125 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
129 (define-public PLATFORM
132 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
135 (let ((platform (string-tokenize
136 (vector-ref (uname) 0) char-set:letter+digit)))
137 (if (null? (cdr platform)) #f
138 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
142 (define native-getcwd getcwd)
144 (if (string-index x #\\)
146 (string-regexp-substitute
148 (string-regexp-substitute "\\\\" "/" x))))
149 ;; FIXME: this prints a warning.
150 (define-public (ly-getcwd)
151 (slashify (native-getcwd))))
152 (else (define-public ly-getcwd getcwd)))
154 (define-public (is-absolute? file-name)
155 (let ((file-name-length (string-length file-name)))
156 (if (= file-name-length 0)
158 (or (eq? (string-ref file-name 0) #\/)
159 (and (eq? PLATFORM 'windows)
160 (> file-name-length 2)
161 (eq? (string-ref file-name 1) #\:)
162 (eq? (string-ref file-name 2) #\/))))))
164 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 (define (type-check-list location signature arguments)
167 "Typecheck a list of arguments against a list of type
168 predicates. Print a message at LOCATION if any predicate failed."
170 (define (recursion-helper signature arguments count)
171 (define (helper pred? arg count)
172 (if (not (pred? arg))
178 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
179 count (type-name pred?) arg))
183 (if (null? signature)
185 (and (helper (car signature) (car arguments) count)
186 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
188 (recursion-helper signature arguments 1))
190 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194 ;;(define-public (output-framework) (write "hello\n"))
196 (define output-tex-module
197 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
198 (define output-ps-module
199 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
201 (define-public (ps-output-expression expr port)
202 (display (eval expr output-ps-module) port))
204 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
205 ;; Safe definitions utility
206 (define safe-objects (list))
208 (define-macro (define-safe-public arglist . body)
209 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
210 The syntax is the same as `define*-public'."
211 (define (get-symbol arg)
213 (get-symbol (car arg))
215 (let ((safe-symbol (get-symbol arglist)))
217 (define*-public ,arglist
219 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
224 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231 "define-event-classes.scm"
232 "define-music-types.scm"
235 "chord-ignatzek-names.scm"
237 "chord-generic-names.scm"
240 "music-functions.scm"
243 "define-music-properties.scm"
247 "parser-ly-from-scheme.scm"
248 "ly-syntax-constructors.scm"
250 "define-context-properties.scm"
251 "translation-functions.scm"
261 "define-markup-commands.scm"
262 "define-grob-properties.scm"
264 "define-grob-interfaces.scm"
265 "define-stencil-commands.scm"
266 "layout-page-layout.scm"
270 "backend-library.scm"
273 ;; must be after everything has been defined
277 (set! type-p-name-alist
279 (,boolean-or-symbol? . "boolean or symbol")
280 (,boolean? . "boolean")
282 (,grob-list? . "list of grobs")
283 (,hash-table? . "hash table")
284 (,input-port? . "input port")
285 (,integer? . "integer")
287 (,ly:context? . "context")
288 (,ly:dimension? . "dimension, in staff space")
289 (,ly:dir? . "direction")
290 (,ly:duration? . "duration")
291 (,ly:grob? . "layout object")
292 (,ly:input-location? . "input location")
293 (,ly:moment? . "moment")
294 (,ly:music? . "music")
295 (,ly:pitch? . "pitch")
296 (,ly:translator? . "translator")
297 (,ly:font-metric? . "font metric")
298 (,ly:simple-closure? . "simple closure")
299 (,markup-list? . "list of markups")
300 (,markup? . "markup")
301 (,ly:music-list? . "list of music")
302 (,number-or-grob? . "number or grob")
303 (,number-or-string? . "number or string")
304 (,number-pair? . "pair of numbers")
305 (,number? . "number")
306 (,output-port? . "output port")
308 (,procedure? . "procedure")
309 (,scheme? . "any type")
310 (,string? . "string")
311 (,symbol? . "symbol")
312 (,vector? . "vector")))
317 (define gc-protect-stat-count 0)
318 (define-public (dump-gc-protects)
319 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
320 (let* ((protects (sort
321 (hash-table->alist (ly:protects))
323 (< (object-address (car a))
324 (object-address (car b))))))
326 (out-file-name (string-append
327 "gcstat-" (number->string gc-protect-stat-count)
329 (outfile (open-file out-file-name "w")))
331 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
339 (map object->string (list (object-address x) c x))
345 (not (symbol? (car x))))
349 ; (display (ly:smob-protects))
351 (if (defined? 'gc-live-object-stats)
353 (display "Live object statistics: GC'ing\n")
357 (set! stats (gc-live-object-stats))
358 (display "Dumping live object statistics.\n")
362 (format outfile "~a: ~a\n" (car x) (cdr x)))
363 (sort (gc-live-object-stats)
365 (string<? (car x) (car y)))))))))
367 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
370 (define-public (lilypond-main files)
371 "Entry point for LilyPond."
373 (define (no-files-handler)
377 (if (ly:get-option 'gui)
383 (let ((failed (lilypond-all files)))
386 (ly:error (_ "failed files: ~S") (string-join failed))
389 ;; HACK: be sure to exit with single newline
393 (define-public (lilypond-all files)
395 (if (ly:get-option 'show-available-fonts)
397 (ly:font-config-display-fonts)
402 (handler (lambda (key failed-file)
403 (set! failed (append (list failed-file) failed)))))
407 (lilypond-file handler x)
408 (ly:clear-anonymous-modules)
409 (if (ly:get-option 'debug-gc)
415 (define (lilypond-file handler file-name)
416 (catch 'ly-file-failed
417 (lambda () (ly:parse-file file-name))
418 (lambda (x . args) (handler x file-name))))
420 (use-modules (scm editor))
422 (define-public (gui-main files)
424 (gui-no-files-handler))
425 (let* ((base (basename (car files) ".ly"))
426 (log-name (string-append base ".log")))
427 (if (not (ly:get-option 'gui))
428 (ly:message (_ "Redirecting output to ~a...") log-name))
429 (ly:stderr-redirect log-name "w")
430 (ly:message "# -*-compilation-*-")
432 (let ((failed (lilypond-all files)))
436 (ly:stderr-redirect "foo" "r")
437 (system (get-editor-command log-name 0 0 0))
438 (ly:error (_ "failed files: ~S") (string-join failed))
443 (define (gui-no-files-handler)
444 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
445 ;; FIXME: soft-code, localize
446 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
447 (cmd (get-editor-command welcome-ly 0 0 0)))
448 (ly:message (_ "Invoking `~a'...") cmd)