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)))
14 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
15 (book-title-preview #t "include book-titles in preview images.")
17 "dump memory debugging statistics")
18 (delete-intermediate-files #f
19 "delete unusable PostScript files")
20 (dump-signatures #f "dump output signatures of each system")
21 (eps-font-include #f "Include fonts in separate-system EPS files.")
22 (eps-pad-boxes #f "Pad EPS bounding boxes to guarantee alignment between systems")
24 "load fonts via Ghostscript.")
25 (gui #f "running from gui; redirect stderr to log file")
26 (internal-type-checking #f "check every property assignment for types")
27 (midi-debug #f "generate human readable MIDI")
29 "relative for simultaneous music works
30 similar to chord syntax")
32 "experimental mechanism for remembering tweaks")
33 (point-and-click #t "use point & click")
34 (paper-size "a4" "the default paper size")
35 (parse-protect #t "continue when finding errors in inline
36 scheme are caught in the parser. If off, halt
37 on errors, and print a stack trace.")
38 (profile-property-accesses #f "keep statistics of get_property() calls.")
39 (resolution 101 "resolution for generating bitmaps")
40 (read-file-list #f "Read files to be processed from command line arguments")
41 (safe #f "Run safely")
42 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
44 "how much verbosity for TTF font embedding?")
45 (show-available-fonts #f
46 "List font names available.")
47 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
51 ;; need to do this in the beginning. Other parts of the
52 ;; Scheme init depend on these options.
54 (define-scheme-options)
56 (if (defined? 'set-debug-cell-accesses!)
57 (set-debug-cell-accesses! #f))
59 ;(set-debug-cell-accesses! 1000)
61 (use-modules (ice-9 regex)
71 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
73 (define-public (print . args)
74 (apply format (cons (current-output-port) args)))
78 ;;; debugging evaluator is slower. This should
79 ;;; have a more sensible default.
81 (if (ly:get-option 'verbose)
84 (debug-enable 'backtrace)
85 (read-enable 'positions)))
87 (define-public tex-backend?
88 (member (ly:output-backend) '("texstr" "tex")))
90 (define-public parser #f)
92 (define-public (lilypond-version)
94 (map (lambda (x) (if (symbol? x)
101 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
102 (define-public TEX_STRING_HASHLIMIT 10000000)
106 ;; gettext wrapper for guile < 1.7.2
107 (if (defined? 'gettext)
108 (define-public _ gettext)
109 (define-public _ ly:gettext))
111 (define-public (ly:load x)
112 (let* ((file-name (%search-load-path x)))
113 (if (ly:get-option 'verbose)
114 (ly:progress "[~A" file-name))
116 (ly:error (_ "Can't find ~A" x)))
117 (primitive-load file-name)
118 (if (ly:get-option 'verbose)
122 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
125 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
128 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
132 (define-public PLATFORM
135 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
138 (let ((platform (string-tokenize
139 (vector-ref (uname) 0) char-set:letter+digit)))
140 (if (null? (cdr platform)) #f
141 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
145 (define native-getcwd getcwd)
147 (if (string-index x #\\)
149 (string-regexp-substitute
151 (string-regexp-substitute "\\\\" "/" x))))
152 ;; FIXME: this prints a warning.
153 (define-public (ly-getcwd)
154 (slashify (native-getcwd))))
155 (else (define-public ly-getcwd getcwd)))
157 (define-public (is-absolute? file-name)
158 (let ((file-name-length (string-length file-name)))
159 (if (= file-name-length 0)
161 (or (eq? (string-ref file-name 0) #\/)
162 (and (eq? PLATFORM 'windows)
163 (> file-name-length 2)
164 (eq? (string-ref file-name 1) #\:)
165 (eq? (string-ref file-name 2) #\/))))))
167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169 (define (type-check-list location signature arguments)
170 "Typecheck a list of arguments against a list of type
171 predicates. Print a message at LOCATION if any predicate failed."
173 (define (recursion-helper signature arguments count)
174 (define (helper pred? arg count)
175 (if (not (pred? arg))
181 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
182 count (type-name pred?) arg))
186 (if (null? signature)
188 (and (helper (car signature) (car arguments) count)
189 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
191 (recursion-helper signature arguments 1))
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
197 ;;(define-public (output-framework) (write "hello\n"))
199 (define output-tex-module
200 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
201 (define output-ps-module
202 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
204 (define-public (ps-output-expression expr port)
205 (display (eval expr output-ps-module) port))
207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 ;; Safe definitions utility
209 (define safe-objects (list))
211 (define-macro (define-safe-public arglist . body)
212 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
213 The syntax is the same as `define*-public'."
214 (define (get-symbol arg)
216 (get-symbol (car arg))
218 (let ((safe-symbol (get-symbol arglist)))
220 (define*-public ,arglist
222 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
227 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234 "define-event-classes.scm"
235 "define-music-types.scm"
238 "chord-ignatzek-names.scm"
240 "chord-generic-names.scm"
243 "music-functions.scm"
246 "define-music-properties.scm"
250 "parser-ly-from-scheme.scm"
251 "ly-syntax-constructors.scm"
253 "define-context-properties.scm"
254 "translation-functions.scm"
264 "define-markup-commands.scm"
265 "define-grob-properties.scm"
267 "define-grob-interfaces.scm"
268 "define-stencil-commands.scm"
272 "backend-library.scm"
275 ;; must be after everything has been defined
279 (set! type-p-name-alist
281 (,boolean-or-symbol? . "boolean or symbol")
282 (,boolean? . "boolean")
284 (,grob-list? . "list of grobs")
285 (,hash-table? . "hash table")
286 (,input-port? . "input port")
287 (,integer? . "integer")
289 (,ly:context? . "context")
290 (,ly:dimension? . "dimension, in staff space")
291 (,ly:dir? . "direction")
292 (,ly:duration? . "duration")
293 (,ly:grob? . "layout object")
294 (,ly:input-location? . "input location")
295 (,ly:moment? . "moment")
296 (,ly:music? . "music")
297 (,ly:pitch? . "pitch")
298 (,ly:translator? . "translator")
299 (,ly:font-metric? . "font metric")
300 (,ly:simple-closure? . "simple closure")
301 (,markup-list? . "list of markups")
302 (,markup? . "markup")
303 (,ly:music-list? . "list of music")
304 (,number-or-grob? . "number or grob")
305 (,number-or-string? . "number or string")
306 (,number-pair? . "pair of numbers")
307 (,number? . "number")
308 (,output-port? . "output port")
310 (,procedure? . "procedure")
311 (,scheme? . "any type")
312 (,string? . "string")
313 (,symbol? . "symbol")
314 (,vector? . "vector")))
319 (define gc-protect-stat-count 0)
320 (define-public (dump-gc-protects)
321 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
322 (let* ((protects (sort
323 (hash-table->alist (ly:protects))
325 (< (object-address (car a))
326 (object-address (car b))))))
328 (out-file-name (string-append
329 "gcstat-" (number->string gc-protect-stat-count)
331 (outfile (open-file out-file-name "w")))
333 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
341 (map object->string (list (object-address x) c x))
347 (not (symbol? (car x))))
351 ; (display (ly:smob-protects))
353 (if (defined? 'gc-live-object-stats)
355 (display "Live object statistics: GC'ing\n")
359 (set! stats (gc-live-object-stats))
360 (display "Dumping live object statistics.\n")
364 (format outfile "~a: ~a\n" (car x) (cdr x)))
365 (sort (gc-live-object-stats)
367 (string<? (car x) (car y)))))))))
369 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
372 (define-public (lilypond-main files)
373 "Entry point for LilyPond."
375 (define (no-files-handler)
379 (if (ly:get-option 'gui)
385 (let ((failed (lilypond-all files)))
388 (ly:error (_ "failed files: ~S") (string-join failed))
391 ;; HACK: be sure to exit with single newline
395 (define-public (lilypond-all files)
396 (if (ly:get-option 'read-file-list)
399 (> (string-length s) 0))
401 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
405 (if (ly:get-option 'show-available-fonts)
407 (ly:font-config-display-fonts)
412 (handler (lambda (key failed-file)
413 (set! failed (append (list failed-file) failed)))))
417 (lilypond-file handler x)
418 (ly:clear-anonymous-modules)
419 (if (ly:get-option 'debug-gc)
425 (define (lilypond-file handler file-name)
426 (catch 'ly-file-failed
427 (lambda () (ly:parse-file file-name))
428 (lambda (x . args) (handler x file-name))))
430 (use-modules (scm editor))
432 (define-public (gui-main files)
434 (gui-no-files-handler))
435 (let* ((base (basename (car files) ".ly"))
436 (log-name (string-append base ".log")))
437 (if (not (ly:get-option 'gui))
438 (ly:message (_ "Redirecting output to ~a...") log-name))
439 (ly:stderr-redirect log-name "w")
440 (ly:message "# -*-compilation-*-")
442 (let ((failed (lilypond-all files)))
446 (ly:stderr-redirect "foo" "r")
447 (system (get-editor-command log-name 0 0 0))
448 (ly:error (_ "failed files: ~S") (string-join failed))
453 (define (gui-no-files-handler)
454 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
455 ;; FIXME: soft-code, localize
456 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
457 (cmd (get-editor-command welcome-ly 0 0 0)))
458 (ly:message (_ "Invoking `~a'...") cmd)