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>
9 (define (define-scheme-options)
11 (ly:add-option (car x) (cadr x) (caddr x)))
13 '((point-and-click #t "use point & click")
14 (midi-debug #f "generate human readable MIDI")
15 (internal-type-checking #f "check every property assignment for types")
16 (parse-protect #t "continue when finding errors in inline
17 scheme are caught in the parser. If off, halt
18 on errors, and print a stack trace.")
20 "relative for simultaneous music works
21 similar to chord syntax")
23 "experimental mechanism for remembering tweaks")
24 (resolution 101 "resolution for generating bitmaps")
25 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
26 (preview-include-book-title #t "include book-titles in preview images.")
28 "load fonts via Ghostscript.")
29 (gui #f "running from gui; redirect stderr to log file")
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"))))
38 ;; FIXME: stray statement
39 (define-scheme-options)
41 (if (defined? 'set-debug-cell-accesses!)
42 (set-debug-cell-accesses! #f))
44 ;(set-debug-cell-accesses! 1000)
46 (use-modules (ice-9 regex)
56 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
58 (define-public (print . args)
59 (apply format (cons (current-output-port) args)))
63 ;;; debugging evaluator is slower. This should
64 ;;; have a more sensible default.
66 (if (ly:get-option 'verbose)
69 (debug-enable 'backtrace)
70 (read-enable 'positions)))
73 (define-public tex-backend?
74 (member (ly:output-backend) '("texstr" "tex")))
76 (define-public parser #f)
78 (define-public (lilypond-version)
80 (map (lambda (x) (if (symbol? x)
88 ;; cpp hack to get useful error message
89 (define ifdef "First run this through cpp.")
90 (define ifndef "First run this through cpp.")
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"
227 "music-functions.scm"
230 "define-music-properties.scm"
236 "define-context-properties.scm"
237 "translation-functions.scm"
247 "define-markup-commands.scm"
248 "define-grob-properties.scm"
250 "define-grob-interfaces.scm"
251 "define-stencil-commands.scm"
256 "backend-library.scm"
259 ;; must be after everything has been defined
263 (set! type-p-name-alist
265 (,boolean-or-symbol? . "boolean or symbol")
266 (,boolean? . "boolean")
268 (,grob-list? . "list of grobs")
269 (,hash-table? . "hash table")
270 (,input-port? . "input port")
271 (,integer? . "integer")
273 (,ly:context? . "context")
274 (,ly:dimension? . "dimension, in staff space")
275 (,ly:dir? . "direction")
276 (,ly:duration? . "duration")
277 (,ly:grob? . "layout object")
278 (,ly:input-location? . "input location")
279 (,ly:moment? . "moment")
280 (,ly:music? . "music")
281 (,ly:pitch? . "pitch")
282 (,ly:translator? . "translator")
283 (,ly:font-metric? . "font metric")
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))
319 (lambda (x) (not (symbol? x)))
326 (map object->string (list (object-address x) c x))
332 (if (defined? 'gc-live-object-stats)
334 (display "Live object statistics: GC'ing\n")
338 (set! stats (gc-live-object-stats))
339 (display "Dumping live object statistics.\n")
343 (format outfile "~a: ~a\n" (car x) (cdr x)))
344 (sort (gc-live-object-stats)
346 (string<? (car x) (car y)))))))))
348 (define-public (tweak-grob-property grob sym val)
349 (set! (ly:grob-property grob sym) val))
351 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
352 (define-public (lilypond-main files)
353 "Entry point for LilyPond."
358 (let ((failed (lilypond-all files)))
361 (ly:error (_ "failed files: ~S") (string-join failed))
364 ;; HACK: be sure to exit with single newline
368 (define (no-files-handler)
372 (define-public (lilypond-all files)
374 (handler (lambda (key failed-file)
375 (set! failed (append (list failed-file) failed)))))
376 ;;(handler (lambda (key . arg) (set! failed (append arg failed)))))
377 (for-each (lambda (x) (lilypond-file handler x)) files)
380 (define (lilypond-file handler file-name)
381 (catch 'ly-file-failed
382 (lambda () (ly:parse-file file-name))
383 (lambda (x . args) (handler x file-name)))
385 (if (ly:get-option 'debug-gc)
388 (use-modules (scm editor))
390 (define-public (running-from-gui?)
391 (let ((have-tty? (isatty? (current-input-port))))
392 ;; If no TTY and not using safe, assume running from GUI.
394 ((eq? PLATFORM 'windows)
395 ;; Always write to .log file.
397 ;; This only works for i586-mingw32msvc-gcc -mwindows
398 (not (string-match "standard input"
399 (format #f "~S" (current-input-port))))))
400 ;; FIXME: using -dgui would be nice, but it does not work
401 ((eq? PLATFORM 'foo-windows)
402 (ly:get-option 'gui))
403 ((eq? PLATFORM 'darwin) #f)
407 (define-public (gui-main files)
409 (gui-no-files-handler))
410 (let* ((base (basename (car files) ".ly"))
411 (log-name (string-append base ".log")))
412 (if (not (running-from-gui?))
413 (ly:message (_ "Redirecting output to ~a...") log-name))
414 (ly:stderr-redirect log-name "w")
415 (ly:message "# -*-compilation-*-")
416 (let ((failed (lilypond-all files)))
420 (ly:stderr-redirect "foo" "r")
421 (system (get-editor-command log-name 0 0 0))
422 (ly:error (_ "failed files: ~S") (string-join failed))
427 (define (gui-no-files-handler)
428 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
429 ;; FIXME: soft-code, localize
430 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
431 (cmd (get-editor-command welcome-ly 0 0 0)))
432 (ly:message (_ "Invoking `~a'...") cmd)
436 (or (not (running-from-gui?))
437 (ly:get-option 'safe)
438 (define lilypond-main gui-main))