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")
38 "how much verbosity for TTF font embedding?")
40 "dump GC protection info")
41 (show-available-fonts #f
42 "List font names available.")
46 ;; FIXME: stray statement
47 (define-scheme-options)
49 (if (defined? 'set-debug-cell-accesses!)
50 (set-debug-cell-accesses! #f))
52 ;(set-debug-cell-accesses! 1000)
54 (use-modules (ice-9 regex)
64 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
66 (define-public (print . args)
67 (apply format (cons (current-output-port) args)))
71 ;;; debugging evaluator is slower. This should
72 ;;; have a more sensible default.
74 (if (ly:get-option 'verbose)
77 (debug-enable 'backtrace)
78 (read-enable 'positions)))
80 (define-public tex-backend?
81 (member (ly:output-backend) '("texstr" "tex")))
83 (define-public parser #f)
85 (define-public (lilypond-version)
87 (map (lambda (x) (if (symbol? x)
96 ;; gettext wrapper for guile < 1.7.2
97 (if (defined? 'gettext)
98 (define-public _ gettext)
99 (define-public _ ly:gettext))
101 (define-public (ly:load x)
102 (let* ((file-name (%search-load-path x)))
103 (if (ly:get-option 'verbose)
104 (ly:progress "[~A" file-name))
105 (primitive-load file-name)
106 (if (ly:get-option 'verbose)
110 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
113 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
116 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
120 (define-public PLATFORM
123 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
126 (let ((platform (string-tokenize
127 (vector-ref (uname) 0) char-set:letter+digit)))
128 (if (null? (cdr platform)) #f
129 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
133 (define native-getcwd getcwd)
135 (if (string-index x #\\)
137 (string-regexp-substitute
139 (string-regexp-substitute "\\\\" "/" x))))
140 ;; FIXME: this prints a warning.
141 (define-public (ly-getcwd)
142 (slashify (native-getcwd))))
143 (else (define-public ly-getcwd getcwd)))
145 (define-public (is-absolute? file-name)
146 (let ((file-name-length (string-length file-name)))
147 (if (= file-name-length 0)
149 (or (eq? (string-ref file-name 0) #\/)
150 (and (eq? PLATFORM 'windows)
151 (> file-name-length 2)
152 (eq? (string-ref file-name 1) #\:)
153 (eq? (string-ref file-name 2) #\/))))))
155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 (define (type-check-list location signature arguments)
158 "Typecheck a list of arguments against a list of type
159 predicates. Print a message at LOCATION if any predicate failed."
160 (define (recursion-helper signature arguments count)
161 (define (helper pred? arg count)
162 (if (not (pred? arg))
168 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
169 count (type-name pred?) arg))
173 (if (null? signature)
175 (and (helper (car signature) (car arguments) count)
176 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
177 (recursion-helper signature arguments 1))
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
183 ;;(define-public (output-framework) (write "hello\n"))
185 (define output-tex-module
186 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
187 (define output-ps-module
188 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
190 (define-public (ps-output-expression expr port)
191 (display (eval expr output-ps-module) port))
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194 ;; Safe definitions utility
195 (define safe-objects (list))
197 (define-macro (define-safe-public arglist . body)
198 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
199 The syntax is the same as `define*-public'."
200 (define (get-symbol arg)
202 (get-symbol (car arg))
204 (let ((safe-symbol (get-symbol arglist)))
206 (define*-public ,arglist
208 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
213 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
220 "define-music-types.scm"
223 "chord-ignatzek-names.scm"
225 "chord-generic-names.scm"
228 "music-functions.scm"
231 "define-music-properties.scm"
235 "parser-ly-from-scheme.scm"
237 "define-context-properties.scm"
238 "translation-functions.scm"
248 "define-markup-commands.scm"
249 "define-grob-properties.scm"
251 "define-grob-interfaces.scm"
252 "define-stencil-commands.scm"
253 "layout-page-layout.scm"
257 "backend-library.scm"
260 ;; must be after everything has been defined
264 (set! type-p-name-alist
266 (,boolean-or-symbol? . "boolean or symbol")
267 (,boolean? . "boolean")
269 (,grob-list? . "list of grobs")
270 (,hash-table? . "hash table")
271 (,input-port? . "input port")
272 (,integer? . "integer")
274 (,ly:context? . "context")
275 (,ly:dimension? . "dimension, in staff space")
276 (,ly:dir? . "direction")
277 (,ly:duration? . "duration")
278 (,ly:grob? . "layout object")
279 (,ly:input-location? . "input location")
280 (,ly:moment? . "moment")
281 (,ly:music? . "music")
282 (,ly:pitch? . "pitch")
283 (,ly:translator? . "translator")
284 (,ly:font-metric? . "font metric")
285 (,ly:simple-closure? . "simple closure")
286 (,markup-list? . "list of markups")
287 (,markup? . "markup")
288 (,ly:music-list? . "list of music")
289 (,number-or-grob? . "number or grob")
290 (,number-or-string? . "number or string")
291 (,number-pair? . "pair of numbers")
292 (,number? . "number")
293 (,output-port? . "output port")
295 (,procedure? . "procedure")
296 (,scheme? . "any type")
297 (,string? . "string")
298 (,symbol? . "symbol")
299 (,vector? . "vector")))
304 (define gc-protect-stat-count 0)
305 (define-public (dump-gc-protects)
306 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
307 (let* ((protects (sort
308 (hash-table->alist (ly:protects))
310 (< (object-address (car a))
311 (object-address (car b))))))
313 (out-file-name (string-append
314 "gcstat-" (number->string gc-protect-stat-count)
316 (outfile (open-file out-file-name "w")))
318 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
326 (map object->string (list (object-address x) c x))
332 (not (symbol? (car x))))
336 ; (display (ly:smob-protects))
338 (if (defined? 'gc-live-object-stats)
340 (display "Live object statistics: GC'ing\n")
344 (set! stats (gc-live-object-stats))
345 (display "Dumping live object statistics.\n")
349 (format outfile "~a: ~a\n" (car x) (cdr x)))
350 (sort (gc-live-object-stats)
352 (string<? (car x) (car y)))))))))
354 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
357 (define-public (lilypond-main files)
358 "Entry point for LilyPond."
360 (define (no-files-handler)
364 (if (ly:get-option 'gui)
370 (let ((failed (lilypond-all files)))
373 (ly:error (_ "failed files: ~S") (string-join failed))
376 ;; HACK: be sure to exit with single newline
380 (define-public (lilypond-all files)
382 (if (ly:get-option 'show-available-fonts)
384 (ly:font-config-display-fonts)
389 (handler (lambda (key failed-file)
390 (set! failed (append (list failed-file) failed)))))
394 (lilypond-file handler x)
395 (ly:clear-anonymous-modules)
396 (if (ly:get-option 'debug-gc)
402 (define (lilypond-file handler file-name)
403 (catch 'ly-file-failed
404 (lambda () (ly:parse-file file-name))
405 (lambda (x . args) (handler x file-name))))
407 (use-modules (scm editor))
409 (define-public (gui-main files)
411 (gui-no-files-handler))
412 (let* ((base (basename (car files) ".ly"))
413 (log-name (string-append base ".log")))
414 (if (not (ly:get-option 'gui))
415 (ly:message (_ "Redirecting output to ~a...") log-name))
416 (ly:stderr-redirect log-name "w")
417 (ly:message "# -*-compilation-*-")
419 (let ((failed (lilypond-all files)))
423 (ly:stderr-redirect "foo" "r")
424 (system (get-editor-command log-name 0 0 0))
425 (ly:error (_ "failed files: ~S") (string-join failed))
430 (define (gui-no-files-handler)
431 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
432 ;; FIXME: soft-code, localize
433 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
434 (cmd (get-editor-command welcome-ly 0 0 0)))
435 (ly:message (_ "Invoking `~a'...") cmd)