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 (safe #f "Run safely")
35 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
37 "how much verbosity for TTF font embedding?")
39 "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)
92 ;; gettext wrapper for guile < 1.7.2
93 (if (defined? 'gettext)
94 (define-public _ gettext)
95 (define-public _ ly:gettext))
97 (define-public (ly:load x)
98 (let* ((file-name (%search-load-path x)))
99 (if (ly:get-option 'verbose)
100 (ly:progress "[~A" file-name))
101 (primitive-load file-name)
102 (if (ly:get-option 'verbose)
105 (define-public TEX_STRING_HASHLIMIT 10000000)
108 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
111 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
114 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
118 (define-public PLATFORM
121 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
124 (let ((platform (string-tokenize
125 (vector-ref (uname) 0) char-set:letter+digit)))
126 (if (null? (cdr platform)) #f
127 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
131 (define native-getcwd getcwd)
133 (if (string-index x #\\)
135 (string-regexp-substitute
137 (string-regexp-substitute "\\\\" "/" x))))
138 ;; FIXME: this prints a warning.
139 (define-public (ly-getcwd)
140 (slashify (native-getcwd))))
141 (else (define-public ly-getcwd getcwd)))
143 (define-public (is-absolute? file-name)
144 (let ((file-name-length (string-length file-name)))
145 (if (= file-name-length 0)
147 (or (eq? (string-ref file-name 0) #\/)
148 (and (eq? PLATFORM 'windows)
149 (> file-name-length 2)
150 (eq? (string-ref file-name 1) #\:)
151 (eq? (string-ref file-name 2) #\/))))))
153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 (define (type-check-list location signature arguments)
156 "Typecheck a list of arguments against a list of type
157 predicates. Print a message at LOCATION if any predicate failed."
158 (define (recursion-helper signature arguments count)
159 (define (helper pred? arg count)
160 (if (not (pred? arg))
166 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
167 count (type-name pred?) arg))
171 (if (null? signature)
173 (and (helper (car signature) (car arguments) count)
174 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
175 (recursion-helper signature arguments 1))
177 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 ;;(define-public (output-framework) (write "hello\n"))
183 (define output-tex-module
184 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
185 (define output-ps-module
186 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
188 (define-public (ps-output-expression expr port)
189 (display (eval expr output-ps-module) port))
191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
192 ;; Safe definitions utility
193 (define safe-objects (list))
195 (define-macro (define-safe-public arglist . body)
196 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
197 The syntax is the same as `define*-public'."
198 (define (get-symbol arg)
200 (get-symbol (car arg))
202 (let ((safe-symbol (get-symbol arglist)))
204 (define*-public ,arglist
206 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218 "define-music-types.scm"
221 "chord-ignatzek-names.scm"
223 "chord-generic-names.scm"
226 "music-functions.scm"
229 "define-music-properties.scm"
233 "parser-ly-from-scheme.scm"
235 "define-context-properties.scm"
236 "translation-functions.scm"
246 "define-markup-commands.scm"
247 "define-grob-properties.scm"
249 "define-grob-interfaces.scm"
250 "define-stencil-commands.scm"
251 "layout-page-layout.scm"
255 "backend-library.scm"
258 ;; must be after everything has been defined
262 (set! type-p-name-alist
264 (,boolean-or-symbol? . "boolean or symbol")
265 (,boolean? . "boolean")
267 (,grob-list? . "list of grobs")
268 (,hash-table? . "hash table")
269 (,input-port? . "input port")
270 (,integer? . "integer")
272 (,ly:context? . "context")
273 (,ly:dimension? . "dimension, in staff space")
274 (,ly:dir? . "direction")
275 (,ly:duration? . "duration")
276 (,ly:grob? . "layout object")
277 (,ly:input-location? . "input location")
278 (,ly:moment? . "moment")
279 (,ly:music? . "music")
280 (,ly:pitch? . "pitch")
281 (,ly:translator? . "translator")
282 (,ly:font-metric? . "font metric")
283 (,ly:simple-closure? . "simple closure")
284 (,markup-list? . "list of markups")
285 (,markup? . "markup")
286 (,ly:music-list? . "list of music")
287 (,number-or-grob? . "number or grob")
288 (,number-or-string? . "number or string")
289 (,number-pair? . "pair of numbers")
290 (,number? . "number")
291 (,output-port? . "output port")
293 (,procedure? . "procedure")
294 (,scheme? . "any type")
295 (,string? . "string")
296 (,symbol? . "symbol")
297 (,vector? . "vector")))
302 (define gc-protect-stat-count 0)
303 (define-public (dump-gc-protects)
304 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
305 (let* ((protects (sort
306 (hash-table->alist (ly:protects))
308 (< (object-address (car a))
309 (object-address (car b))))))
311 (out-file-name (string-append
312 "gcstat-" (number->string gc-protect-stat-count)
314 (outfile (open-file out-file-name "w")))
316 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
324 (map object->string (list (object-address x) c x))
330 (not (symbol? (car x))))
334 ; (display (ly:smob-protects))
336 (if (defined? 'gc-live-object-stats)
338 (display "Live object statistics: GC'ing\n")
342 (set! stats (gc-live-object-stats))
343 (display "Dumping live object statistics.\n")
347 (format outfile "~a: ~a\n" (car x) (cdr x)))
348 (sort (gc-live-object-stats)
350 (string<? (car x) (car y)))))))))
352 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
353 (define-public (lilypond-main files)
354 "Entry point for LilyPond."
356 (define (no-files-handler)
360 (if (ly:get-option 'gui)
366 (let ((failed (lilypond-all files)))
369 (ly:error (_ "failed files: ~S") (string-join failed))
372 ;; HACK: be sure to exit with single newline
376 (define-public (lilypond-all files)
379 (handler (lambda (key failed-file)
380 (set! failed (append (list failed-file) failed)))))
384 (lilypond-file handler x)
385 (ly:clear-anonymous-modules)
386 (if (ly:get-option 'debug-gc)
392 (define (lilypond-file handler file-name)
393 (catch 'ly-file-failed
394 (lambda () (ly:parse-file file-name))
395 (lambda (x . args) (handler x file-name))))
397 (use-modules (scm editor))
399 (define-public (gui-main files)
401 (gui-no-files-handler))
402 (let* ((base (basename (car files) ".ly"))
403 (log-name (string-append base ".log")))
404 (if (not (ly:get-option 'gui))
405 (ly:message (_ "Redirecting output to ~a...") log-name))
406 (ly:stderr-redirect log-name "w")
407 (ly:message "# -*-compilation-*-")
409 (let ((failed (lilypond-all files)))
413 (ly:stderr-redirect "foo" "r")
414 (system (get-editor-command log-name 0 0 0))
415 (ly:error (_ "failed files: ~S") (string-join failed))
420 (define (gui-no-files-handler)
421 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
422 ;; FIXME: soft-code, localize
423 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
424 (cmd (get-editor-command welcome-ly 0 0 0)))
425 (ly:message (_ "Invoking `~a'...") cmd)