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 (internal-type-checking #f "check every property assignment for types")
17 (parse-protect #t "continue when finding errors in inline
18 scheme are caught in the parser. If off, halt
19 on errors, and print a stack trace.")
20 (profile-property-accesses #f "keep statistics of get_property() calls.")
22 "relative for simultaneous music works
23 similar to chord syntax")
25 "experimental mechanism for remembering tweaks")
26 (resolution 101 "resolution for generating bitmaps")
27 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
28 (book-title-preview #t "include book-titles in preview images.")
29 (eps-font-include #f "Include fonts in separate-system EPS files.")
31 "load fonts via Ghostscript.")
32 (gui #f "running from gui; redirect stderr to log file")
33 (delete-intermediate-files #f
34 "delete unusable PostScript files")
35 (safe #f "Run safely")
36 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
37 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
39 "how much verbosity for TTF font embedding?")
41 "dump GC protection info")
42 (show-available-fonts #f
43 "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)
100 ;; gettext wrapper for guile < 1.7.2
101 (if (defined? 'gettext)
102 (define-public _ gettext)
103 (define-public _ ly:gettext))
105 (define-public (ly:load x)
106 (let* ((file-name (%search-load-path x)))
107 (if (ly:get-option 'verbose)
108 (ly:progress "[~A" file-name))
109 (primitive-load file-name)
110 (if (ly:get-option 'verbose)
114 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
117 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
120 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
124 (define-public PLATFORM
127 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
130 (let ((platform (string-tokenize
131 (vector-ref (uname) 0) char-set:letter+digit)))
132 (if (null? (cdr platform)) #f
133 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
137 (define native-getcwd getcwd)
139 (if (string-index x #\\)
141 (string-regexp-substitute
143 (string-regexp-substitute "\\\\" "/" x))))
144 ;; FIXME: this prints a warning.
145 (define-public (ly-getcwd)
146 (slashify (native-getcwd))))
147 (else (define-public ly-getcwd getcwd)))
149 (define-public (is-absolute? file-name)
150 (let ((file-name-length (string-length file-name)))
151 (if (= file-name-length 0)
153 (or (eq? (string-ref file-name 0) #\/)
154 (and (eq? PLATFORM 'windows)
155 (> file-name-length 2)
156 (eq? (string-ref file-name 1) #\:)
157 (eq? (string-ref file-name 2) #\/))))))
159 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
161 (define (type-check-list location signature arguments)
162 "Typecheck a list of arguments against a list of type
163 predicates. Print a message at LOCATION if any predicate failed."
164 (define (recursion-helper signature arguments count)
165 (define (helper pred? arg count)
166 (if (not (pred? arg))
172 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
173 count (type-name pred?) arg))
177 (if (null? signature)
179 (and (helper (car signature) (car arguments) count)
180 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
181 (recursion-helper signature arguments 1))
183 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
187 ;;(define-public (output-framework) (write "hello\n"))
189 (define output-tex-module
190 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
191 (define output-ps-module
192 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
194 (define-public (ps-output-expression expr port)
195 (display (eval expr output-ps-module) port))
197 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
198 ;; Safe definitions utility
199 (define safe-objects (list))
201 (define-macro (define-safe-public arglist . body)
202 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
203 The syntax is the same as `define*-public'."
204 (define (get-symbol arg)
206 (get-symbol (car arg))
208 (let ((safe-symbol (get-symbol arglist)))
210 (define*-public ,arglist
212 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
217 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
224 "define-music-types.scm"
227 "chord-ignatzek-names.scm"
229 "chord-generic-names.scm"
232 "music-functions.scm"
235 "define-music-properties.scm"
239 "parser-ly-from-scheme.scm"
241 "define-context-properties.scm"
242 "translation-functions.scm"
252 "define-markup-commands.scm"
253 "define-grob-properties.scm"
255 "define-grob-interfaces.scm"
256 "define-stencil-commands.scm"
257 "layout-page-layout.scm"
261 "backend-library.scm"
264 ;; must be after everything has been defined
268 (set! type-p-name-alist
270 (,boolean-or-symbol? . "boolean or symbol")
271 (,boolean? . "boolean")
273 (,grob-list? . "list of grobs")
274 (,hash-table? . "hash table")
275 (,input-port? . "input port")
276 (,integer? . "integer")
278 (,ly:context? . "context")
279 (,ly:dimension? . "dimension, in staff space")
280 (,ly:dir? . "direction")
281 (,ly:duration? . "duration")
282 (,ly:grob? . "layout object")
283 (,ly:input-location? . "input location")
284 (,ly:moment? . "moment")
285 (,ly:music? . "music")
286 (,ly:pitch? . "pitch")
287 (,ly:translator? . "translator")
288 (,ly:font-metric? . "font metric")
289 (,ly:simple-closure? . "simple closure")
290 (,markup-list? . "list of markups")
291 (,markup? . "markup")
292 (,ly:music-list? . "list of music")
293 (,number-or-grob? . "number or grob")
294 (,number-or-string? . "number or string")
295 (,number-pair? . "pair of numbers")
296 (,number? . "number")
297 (,output-port? . "output port")
299 (,procedure? . "procedure")
300 (,scheme? . "any type")
301 (,string? . "string")
302 (,symbol? . "symbol")
303 (,vector? . "vector")))
308 (define gc-protect-stat-count 0)
309 (define-public (dump-gc-protects)
310 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
311 (let* ((protects (sort
312 (hash-table->alist (ly:protects))
314 (< (object-address (car a))
315 (object-address (car b))))))
317 (out-file-name (string-append
318 "gcstat-" (number->string gc-protect-stat-count)
320 (outfile (open-file out-file-name "w")))
322 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
330 (map object->string (list (object-address x) c x))
336 (not (symbol? (car x))))
340 ; (display (ly:smob-protects))
342 (if (defined? 'gc-live-object-stats)
344 (display "Live object statistics: GC'ing\n")
348 (set! stats (gc-live-object-stats))
349 (display "Dumping live object statistics.\n")
353 (format outfile "~a: ~a\n" (car x) (cdr x)))
354 (sort (gc-live-object-stats)
356 (string<? (car x) (car y)))))))))
358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
361 (define-public (lilypond-main files)
362 "Entry point for LilyPond."
364 (define (no-files-handler)
368 (if (ly:get-option 'gui)
374 (let ((failed (lilypond-all files)))
377 (ly:error (_ "failed files: ~S") (string-join failed))
380 ;; HACK: be sure to exit with single newline
384 (define-public (lilypond-all files)
386 (if (ly:get-option 'show-available-fonts)
388 (ly:font-config-display-fonts)
393 (handler (lambda (key failed-file)
394 (set! failed (append (list failed-file) failed)))))
398 (lilypond-file handler x)
399 (ly:clear-anonymous-modules)
400 (if (ly:get-option 'debug-gc)
406 (define (lilypond-file handler file-name)
407 (catch 'ly-file-failed
408 (lambda () (ly:parse-file file-name))
409 (lambda (x . args) (handler x file-name))))
411 (use-modules (scm editor))
413 (define-public (gui-main files)
415 (gui-no-files-handler))
416 (let* ((base (basename (car files) ".ly"))
417 (log-name (string-append base ".log")))
418 (if (not (ly:get-option 'gui))
419 (ly:message (_ "Redirecting output to ~a...") log-name))
420 (ly:stderr-redirect log-name "w")
421 (ly:message "# -*-compilation-*-")
423 (let ((failed (lilypond-all files)))
427 (ly:stderr-redirect "foo" "r")
428 (system (get-editor-command log-name 0 0 0))
429 (ly:error (_ "failed files: ~S") (string-join failed))
434 (define (gui-no-files-handler)
435 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
436 ;; FIXME: soft-code, localize
437 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
438 (cmd (get-editor-command welcome-ly 0 0 0)))
439 (ly:message (_ "Invoking `~a'...") cmd)