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 (preview-include-book-title #t "include book-titles in preview images.")
30 "load fonts via Ghostscript.")
31 (gui #f "running from gui; redirect stderr to log file")
32 (delete-intermediate-files #f
33 "delete unusable PostScript files")
34 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
36 "how much verbosity for TTF font embedding?")
38 "dump page layout settings.")
40 "dump GC protection info"))))
42 ;; FIXME: stray statement
43 (define-scheme-options)
45 (if (defined? 'set-debug-cell-accesses!)
46 (set-debug-cell-accesses! #f))
48 ;(set-debug-cell-accesses! 1000)
50 (use-modules (ice-9 regex)
60 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
62 (define-public (print . args)
63 (apply format (cons (current-output-port) args)))
67 ;;; debugging evaluator is slower. This should
68 ;;; have a more sensible default.
70 (if (ly:get-option 'verbose)
73 (debug-enable 'backtrace)
74 (read-enable 'positions)))
76 (define-public tex-backend?
77 (member (ly:output-backend) '("texstr" "tex")))
79 (define-public parser #f)
81 (define-public (lilypond-version)
83 (map (lambda (x) (if (symbol? x)
91 ;; cpp hack to get useful error message
92 (define ifdef "First run this through cpp.")
93 (define ifndef "First run this through cpp.")
95 ;; gettext wrapper for guile < 1.7.2
96 (if (defined? 'gettext)
97 (define-public _ gettext)
98 (define-public _ ly:gettext))
100 (define-public (ly:load x)
101 (let* ((file-name (%search-load-path x)))
102 (if (ly:get-option 'verbose)
103 (ly:progress "[~A" file-name))
104 (primitive-load file-name)
105 (if (ly:get-option 'verbose)
108 (define-public TEX_STRING_HASHLIMIT 10000000)
111 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
114 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
117 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
121 (define-public PLATFORM
124 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
127 (let ((platform (string-tokenize
128 (vector-ref (uname) 0) char-set:letter+digit)))
129 (if (null? (cdr platform)) #f
130 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
134 (define native-getcwd getcwd)
136 (if (string-index x #\\)
138 (string-regexp-substitute
140 (string-regexp-substitute "\\\\" "/" x))))
141 ;; FIXME: this prints a warning.
142 (define-public (ly-getcwd)
143 (slashify (native-getcwd))))
144 (else (define-public ly-getcwd getcwd)))
146 (define-public (is-absolute? file-name)
147 (let ((file-name-length (string-length file-name)))
148 (if (= file-name-length 0)
150 (or (eq? (string-ref file-name 0) #\/)
151 (and (eq? PLATFORM 'windows)
152 (> file-name-length 2)
153 (eq? (string-ref file-name 1) #\:)
154 (eq? (string-ref file-name 2) #\/))))))
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 (define (type-check-list location signature arguments)
159 "Typecheck a list of arguments against a list of type
160 predicates. Print a message at LOCATION if any predicate failed."
161 (define (recursion-helper signature arguments count)
162 (define (helper pred? arg count)
163 (if (not (pred? arg))
169 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
170 count (type-name pred?) arg))
174 (if (null? signature)
176 (and (helper (car signature) (car arguments) count)
177 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
178 (recursion-helper signature arguments 1))
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
184 ;;(define-public (output-framework) (write "hello\n"))
186 (define output-tex-module
187 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
188 (define output-ps-module
189 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
191 (define-public (ps-output-expression expr port)
192 (display (eval expr output-ps-module) port))
194 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195 ;; Safe definitions utility
196 (define safe-objects (list))
198 (define-macro (define-safe-public arglist . body)
199 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
200 The syntax is the same as `define*-public'."
201 (define (get-symbol arg)
203 (get-symbol (car arg))
205 (let ((safe-symbol (get-symbol arglist)))
207 (define*-public ,arglist
209 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 "define-music-types.scm"
224 "chord-ignatzek-names.scm"
226 "chord-generic-names.scm"
229 "music-functions.scm"
232 "define-music-properties.scm"
236 "parser-ly-from-scheme.scm"
238 "define-context-properties.scm"
239 "translation-functions.scm"
249 "define-markup-commands.scm"
250 "define-grob-properties.scm"
252 "define-grob-interfaces.scm"
253 "define-stencil-commands.scm"
254 "layout-page-layout.scm"
258 "backend-library.scm"
261 ;; must be after everything has been defined
265 (set! type-p-name-alist
267 (,boolean-or-symbol? . "boolean or symbol")
268 (,boolean? . "boolean")
270 (,grob-list? . "list of grobs")
271 (,hash-table? . "hash table")
272 (,input-port? . "input port")
273 (,integer? . "integer")
275 (,ly:context? . "context")
276 (,ly:dimension? . "dimension, in staff space")
277 (,ly:dir? . "direction")
278 (,ly:duration? . "duration")
279 (,ly:grob? . "layout object")
280 (,ly:input-location? . "input location")
281 (,ly:moment? . "moment")
282 (,ly:music? . "music")
283 (,ly:pitch? . "pitch")
284 (,ly:translator? . "translator")
285 (,ly:font-metric? . "font metric")
286 (,ly:simple-closure? . "simple closure")
287 (,markup-list? . "list of markups")
288 (,markup? . "markup")
289 (,ly:music-list? . "list of music")
290 (,number-or-grob? . "number or grob")
291 (,number-or-string? . "number or string")
292 (,number-pair? . "pair of numbers")
293 (,number? . "number")
294 (,output-port? . "output port")
296 (,procedure? . "procedure")
297 (,scheme? . "any type")
298 (,string? . "string")
299 (,symbol? . "symbol")
300 (,vector? . "vector")))
305 (define gc-protect-stat-count 0)
306 (define-public (dump-gc-protects)
307 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
308 (let* ((protects (sort
309 (hash-table->alist (ly:protects))
311 (< (object-address (car a))
312 (object-address (car b))))))
314 (out-file-name (string-append
315 "gcstat-" (number->string gc-protect-stat-count)
317 (outfile (open-file out-file-name "w")))
319 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
327 (map object->string (list (object-address x) c x))
333 (not (symbol? (car x))))
337 ; (display (ly:smob-protects))
339 (if (defined? 'gc-live-object-stats)
341 (display "Live object statistics: GC'ing\n")
345 (set! stats (gc-live-object-stats))
346 (display "Dumping live object statistics.\n")
350 (format outfile "~a: ~a\n" (car x) (cdr x)))
351 (sort (gc-live-object-stats)
353 (string<? (car x) (car y)))))))))
355 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
356 (define-public (lilypond-main files)
357 "Entry point for LilyPond."
359 (define (no-files-handler)
363 (if (ly:get-option 'gui)
369 (let ((failed (lilypond-all files)))
372 (ly:error (_ "failed files: ~S") (string-join failed))
375 ;; HACK: be sure to exit with single newline
379 (define-public (lilypond-all files)
382 (handler (lambda (key failed-file)
383 (set! failed (append (list failed-file) failed)))))
387 (lilypond-file handler x)
388 (ly:clear-anonymous-modules)
389 (if (ly:get-option 'debug-gc)
395 (define (lilypond-file handler file-name)
396 (catch 'ly-file-failed
397 (lambda () (ly:parse-file file-name))
398 (lambda (x . args) (handler x file-name))))
400 (use-modules (scm editor))
402 (define-public (gui-main files)
404 (gui-no-files-handler))
405 (let* ((base (basename (car files) ".ly"))
406 (log-name (string-append base ".log")))
407 (if (not (ly:get-option 'gui))
408 (ly:message (_ "Redirecting output to ~a...") log-name))
409 (ly:stderr-redirect log-name "w")
410 (ly:message "# -*-compilation-*-")
412 (let ((failed (lilypond-all files)))
416 (ly:stderr-redirect "foo" "r")
417 (system (get-editor-command log-name 0 0 0))
418 (ly:error (_ "failed files: ~S") (string-join failed))
423 (define (gui-no-files-handler)
424 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
425 ;; FIXME: soft-code, localize
426 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
427 (cmd (get-editor-command welcome-ly 0 0 0)))
428 (ly:message (_ "Invoking `~a'...") cmd)