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")
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 memory debugging statistics")
43 (show-available-fonts #f
44 "List font names available.")
45 (read-file-list #f "Read files to be processed from command line arguments")
49 ;; need to do this in the beginning. Other parts of the
50 ;; Scheme init depend on these options.
52 (define-scheme-options)
54 (if (defined? 'set-debug-cell-accesses!)
55 (set-debug-cell-accesses! #f))
57 ;(set-debug-cell-accesses! 1000)
59 (use-modules (ice-9 regex)
69 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
71 (define-public (print . args)
72 (apply format (cons (current-output-port) args)))
76 ;;; debugging evaluator is slower. This should
77 ;;; have a more sensible default.
79 (if (ly:get-option 'verbose)
82 (debug-enable 'backtrace)
83 (read-enable 'positions)))
85 (define-public tex-backend?
86 (member (ly:output-backend) '("texstr" "tex")))
88 (define-public parser #f)
90 (define-public (lilypond-version)
92 (map (lambda (x) (if (symbol? x)
99 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
100 (define-public TEX_STRING_HASHLIMIT 10000000)
104 ;; gettext wrapper for guile < 1.7.2
105 (if (defined? 'gettext)
106 (define-public _ gettext)
107 (define-public _ ly:gettext))
109 (define-public (ly:load x)
110 (let* ((file-name (%search-load-path x)))
111 (if (ly:get-option 'verbose)
112 (ly:progress "[~A" file-name))
114 (ly:error (_ "Can't find ~A" x)))
115 (primitive-load file-name)
116 (if (ly:get-option 'verbose)
120 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
123 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
126 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
130 (define-public PLATFORM
133 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
136 (let ((platform (string-tokenize
137 (vector-ref (uname) 0) char-set:letter+digit)))
138 (if (null? (cdr platform)) #f
139 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
143 (define native-getcwd getcwd)
145 (if (string-index x #\\)
147 (string-regexp-substitute
149 (string-regexp-substitute "\\\\" "/" x))))
150 ;; FIXME: this prints a warning.
151 (define-public (ly-getcwd)
152 (slashify (native-getcwd))))
153 (else (define-public ly-getcwd getcwd)))
155 (define-public (is-absolute? file-name)
156 (let ((file-name-length (string-length file-name)))
157 (if (= file-name-length 0)
159 (or (eq? (string-ref file-name 0) #\/)
160 (and (eq? PLATFORM 'windows)
161 (> file-name-length 2)
162 (eq? (string-ref file-name 1) #\:)
163 (eq? (string-ref file-name 2) #\/))))))
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167 (define (type-check-list location signature arguments)
168 "Typecheck a list of arguments against a list of type
169 predicates. Print a message at LOCATION if any predicate failed."
171 (define (recursion-helper signature arguments count)
172 (define (helper pred? arg count)
173 (if (not (pred? arg))
179 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
180 count (type-name pred?) arg))
184 (if (null? signature)
186 (and (helper (car signature) (car arguments) count)
187 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
189 (recursion-helper signature arguments 1))
191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195 ;;(define-public (output-framework) (write "hello\n"))
197 (define output-tex-module
198 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
199 (define output-ps-module
200 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
202 (define-public (ps-output-expression expr port)
203 (display (eval expr output-ps-module) port))
205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
206 ;; Safe definitions utility
207 (define safe-objects (list))
209 (define-macro (define-safe-public arglist . body)
210 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
211 The syntax is the same as `define*-public'."
212 (define (get-symbol arg)
214 (get-symbol (car arg))
216 (let ((safe-symbol (get-symbol arglist)))
218 (define*-public ,arglist
220 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
232 "define-event-classes.scm"
233 "define-music-types.scm"
236 "chord-ignatzek-names.scm"
238 "chord-generic-names.scm"
241 "music-functions.scm"
244 "define-music-properties.scm"
248 "parser-ly-from-scheme.scm"
249 "ly-syntax-constructors.scm"
251 "define-context-properties.scm"
252 "translation-functions.scm"
262 "define-markup-commands.scm"
263 "define-grob-properties.scm"
265 "define-grob-interfaces.scm"
266 "define-stencil-commands.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)
394 (if (ly:get-option 'read-file-list)
397 (> (string-length s) 0))
399 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
403 (if (ly:get-option 'show-available-fonts)
405 (ly:font-config-display-fonts)
410 (handler (lambda (key failed-file)
411 (set! failed (append (list failed-file) failed)))))
415 (lilypond-file handler x)
416 (ly:clear-anonymous-modules)
417 (if (ly:get-option 'debug-gc)
423 (define (lilypond-file handler file-name)
424 (catch 'ly-file-failed
425 (lambda () (ly:parse-file file-name))
426 (lambda (x . args) (handler x file-name))))
428 (use-modules (scm editor))
430 (define-public (gui-main files)
432 (gui-no-files-handler))
433 (let* ((base (basename (car files) ".ly"))
434 (log-name (string-append base ".log")))
435 (if (not (ly:get-option 'gui))
436 (ly:message (_ "Redirecting output to ~a...") log-name))
437 (ly:stderr-redirect log-name "w")
438 (ly:message "# -*-compilation-*-")
440 (let ((failed (lilypond-all files)))
444 (ly:stderr-redirect "foo" "r")
445 (system (get-editor-command log-name 0 0 0))
446 (ly:error (_ "failed files: ~S") (string-join failed))
451 (define (gui-no-files-handler)
452 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
453 ;; FIXME: soft-code, localize
454 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
455 (cmd (get-editor-command welcome-ly 0 0 0)))
456 (ly:message (_ "Invoking `~a'...") cmd)