1 ;;;; lily.scm -- toplevel Scheme stuff
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 (ly:add-option (car x) (cadr x) (caddr x)))
12 '((point-and-click #t "use point & click")
13 (midi-debug #f "generate human readable MIDI")
14 (internal-type-checking #f "check every property assignment for types")
15 (parse-protect #t "continue when finding errors in inline
16 scheme are caught in the parser. If off, halt
17 on errors, and print a stack trace.")
19 "relative for simultaneous music works
20 similar to chord syntax")
22 ;; at 101.178, a staff space is exactly 7 pixels.
25 (resolution 90 "resolution for generating bitmaps")
26 (anti-alias-factor 1 "blow up resolution and scale to prevent jaggies in PNG")
27 (preview-include-book-title #t "include book-titles in preview images.")
29 "load fonts via Ghostscript.")
30 (delete-intermediate-files #f
31 "delete unusable PostScript files")
32 (verbose #f "value for the --verbose flag")
34 "how much verbosity for TTF font embedding?")
36 "dump GC protection info")))
39 (if (defined? 'set-debug-cell-accesses!)
40 (set-debug-cell-accesses! #f))
42 ;(set-debug-cell-accesses! 1000)
44 (use-modules (ice-9 regex)
54 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
56 (define-public (print . args)
57 (apply format (cons (current-output-port) args)))
61 ;;; debugging evaluator is slower. This should
62 ;;; have a more sensible default.
64 (if (ly:get-option 'verbose)
67 (debug-enable 'backtrace)
68 (read-enable 'positions)))
71 (define-public tex-backend?
72 (member (ly:output-backend) '("texstr" "tex")))
74 (define-public parser #f)
76 (define-public (lilypond-version)
78 (map (lambda (x) (if (symbol? x)
86 ;; cpp hack to get useful error message
87 (define ifdef "First run this through cpp.")
88 (define ifndef "First run this through cpp.")
90 ;; gettext wrapper for guile < 1.7.2
91 (if (defined? 'gettext)
92 (define-public _ gettext)
93 (define-public _ ly:gettext))
95 (define-public (ly:load x)
96 (let* ((file-name (%search-load-path x)))
97 (if (ly:get-option 'verbose)
98 (ly:progress "[~A" file-name))
99 (primitive-load file-name)
100 (if (ly:get-option 'verbose)
103 (define-public TEX_STRING_HASHLIMIT 10000000)
106 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
109 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
112 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
116 (define-public PLATFORM
119 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
123 (define native-getcwd getcwd)
125 (if (string-index x #\\)
127 (string-regexp-substitute
129 (string-regexp-substitute "\\\\" "/" x))))
130 ;; FIXME: this prints a warning.
131 (define-public (ly-getcwd)
132 (slashify (native-getcwd))))
133 (else (define-public ly-getcwd getcwd)))
135 (define-public (is-absolute? file-name)
136 (let ((file-name-length (string-length file-name)))
137 (if (= file-name-length 0)
139 (or (eq? (string-ref file-name 0) #\/)
140 (and (eq? PLATFORM 'windows)
141 (> file-name-length 2)
142 (eq? (string-ref file-name 1) #\:)
143 (eq? (string-ref file-name 2) #\/))))))
145 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
147 (define (type-check-list location signature arguments)
148 "Typecheck a list of arguments against a list of type
149 predicates. Print a message at LOCATION if any predicate failed."
150 (define (recursion-helper signature arguments count)
151 (define (helper pred? arg count)
152 (if (not (pred? arg))
158 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
159 count (type-name pred?) arg))
163 (if (null? signature)
165 (and (helper (car signature) (car arguments) count)
166 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
167 (recursion-helper signature arguments 1))
169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
173 ;;(define-public (output-framework) (write "hello\n"))
175 (define output-tex-module
176 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
177 (define output-ps-module
178 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
180 (define-public (ps-output-expression expr port)
181 (display (eval expr output-ps-module) port))
183 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
184 ;; Safe definitions utility
185 (define safe-objects (list))
187 (define-macro (define-safe-public arglist . body)
188 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
189 The syntax is the same as `define*-public'."
190 (define (get-symbol arg)
192 (get-symbol (car arg))
194 (let ((safe-symbol (get-symbol arglist)))
196 (define*-public ,arglist
198 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
210 "define-music-types.scm"
213 "chord-ignatzek-names.scm"
215 "chord-generic-names.scm"
219 "music-functions.scm"
221 "define-music-properties.scm"
227 "define-context-properties.scm"
228 "translation-functions.scm"
238 "define-markup-commands.scm"
239 "define-grob-properties.scm"
241 "define-grob-interfaces.scm"
242 "define-stencil-commands.scm"
247 "backend-library.scm"
250 ;; must be after everything has been defined
254 (set! type-p-name-alist
256 (,boolean-or-symbol? . "boolean or symbol")
257 (,boolean? . "boolean")
259 (,grob-list? . "list of grobs")
260 (,hash-table? . "hash table")
261 (,input-port? . "input port")
262 (,integer? . "integer")
264 (,ly:context? . "context")
265 (,ly:dimension? . "dimension, in staff space")
266 (,ly:dir? . "direction")
267 (,ly:duration? . "duration")
268 (,ly:grob? . "layout object")
269 (,ly:input-location? . "input location")
270 (,ly:moment? . "moment")
271 (,ly:music? . "music")
272 (,ly:pitch? . "pitch")
273 (,ly:translator? . "translator")
274 (,ly:font-metric? . "font metric")
275 (,markup-list? . "list of markups")
276 (,markup? . "markup")
277 (,ly:music-list? . "list of music")
278 (,number-or-grob? . "number or grob")
279 (,number-or-string? . "number or string")
280 (,number-pair? . "pair of numbers")
281 (,number? . "number")
282 (,output-port? . "output port")
284 (,procedure? . "procedure")
285 (,scheme? . "any type")
286 (,string? . "string")
287 (,symbol? . "symbol")
288 (,vector? . "vector")))
293 (define gc-protect-stat-count 0)
294 (define-public (dump-gc-protects)
295 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
296 (let* ((protects (sort
297 (hash-table->alist (ly:protects))
299 (< (object-address (car a))
300 (object-address (car b))))))
302 (out-file-name (string-append
303 "gcstat-" (number->string gc-protect-stat-count)
305 (outfile (open-file out-file-name "w")))
307 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
310 (lambda (x) (not (symbol? x)))
317 (map object->string (list (object-address x) c x))
323 (if (defined? 'gc-live-object-stats)
327 (stats (gc-live-object-stats))
332 (format outfile "~a: ~a\n" (car x) (cdr x)))
333 (sort (gc-live-object-stats)
335 (string<? (car x) (car y)))))
340 (define-public (tweak-grob-property grob sym val)
341 (set! (ly:grob-property grob sym) val))
343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
344 (define-public (lilypond-main files)
345 "Entry point for LilyPond."
350 (let ((failed (lilypond-all files)))
353 (ly:error (_ "failed files: ~S") (string-join failed))
356 ;; HACK: be sure to exit with single newline
360 (define (no-files-handler)
364 (define-public (lilypond-all files)
366 (handler (lambda (key failed-file)
367 (set! failed (append (list failed-file) failed)))))
368 ;;(handler (lambda (key . arg) (set! failed (append arg failed)))))
369 (for-each (lambda (x) (lilypond-file handler x)) files)
372 (define (lilypond-file handler file-name)
373 (catch 'ly-file-failed
374 (lambda () (ly:parse-file file-name))
375 (lambda (x . args) (handler x file-name)))
377 (if (ly:get-option 'debug-gc)
380 (use-modules (scm editor))
382 (define-public (running-from-gui?)
383 (let ((have-tty? (isatty? (current-input-port))))
384 ;; If no TTY and not using safe, assume running from GUI.
386 ((eq? PLATFORM 'windows)
387 ;; This only works for i586-mingw32msvc-gcc -mwindows
388 (not (string-match "standard input"
389 (format #f "~S" (current-input-port)))))
390 ((eq? PLATFORM 'darwin) #f)
394 (define-public (gui-main files)
396 (gui-no-files-handler))
397 (let* ((base (basename (car files) ".ly"))
398 (log-name (string-append base ".log")))
399 (if (not (running-from-gui?))
400 (ly:message (_ "Redirecting output to ~a...") log-name))
401 (ly:stderr-redirect log-name "w")
402 (ly:message "# -*-compilation-*-")
403 (let ((failed (lilypond-all files)))
407 (ly:stderr-redirect "foo" "r")
408 (system (get-editor-command log-name 0 0 0))
409 (ly:error (_ "failed files: ~S") (string-join failed))
414 (define (gui-no-files-handler)
415 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
416 ;; FIXME: soft-code, localize
417 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
418 (cmd (get-editor-command welcome-ly 0 0 0)))
419 (ly:message (_ "Invoking `~a'...") cmd)
423 (or (not (running-from-gui?))
424 (ly:get-option 'safe)
425 (define lilypond-main gui-main))