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.")
19 (profile-property-accesses #f "keep statistics of get_property() calls.")
21 "relative for simultaneous music works
22 similar to chord syntax")
24 "experimental mechanism for remembering tweaks")
25 (resolution 101 "resolution for generating bitmaps")
26 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
27 (preview-include-book-title #t "include book-titles in preview images.")
29 "load fonts via Ghostscript.")
30 (gui #f "running from gui; redirect stderr to log file")
31 (delete-intermediate-files #f
32 "delete unusable PostScript files")
33 (verbose #f "value for the --verbose flag")
35 "how much verbosity for TTF font embedding?")
37 "dump GC protection info"))))
39 ;; FIXME: stray statement
40 (define-scheme-options)
42 (if (defined? 'set-debug-cell-accesses!)
43 (set-debug-cell-accesses! #f))
45 ;(set-debug-cell-accesses! 1000)
47 (use-modules (ice-9 regex)
57 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
59 (define-public (print . args)
60 (apply format (cons (current-output-port) args)))
64 ;;; debugging evaluator is slower. This should
65 ;;; have a more sensible default.
67 (if (ly:get-option 'verbose)
70 (debug-enable 'backtrace)
71 (read-enable 'positions)))
74 (define-public tex-backend?
75 (member (ly:output-backend) '("texstr" "tex")))
77 (define-public parser #f)
79 (define-public (lilypond-version)
81 (map (lambda (x) (if (symbol? x)
89 ;; cpp hack to get useful error message
90 (define ifdef "First run this through cpp.")
91 (define ifndef "First run this through cpp.")
93 ;; gettext wrapper for guile < 1.7.2
94 (if (defined? 'gettext)
95 (define-public _ gettext)
96 (define-public _ ly:gettext))
98 (define-public (ly:load x)
99 (let* ((file-name (%search-load-path x)))
100 (if (ly:get-option 'verbose)
101 (ly:progress "[~A" file-name))
102 (primitive-load file-name)
103 (if (ly:get-option 'verbose)
106 (define-public TEX_STRING_HASHLIMIT 10000000)
109 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
112 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
115 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
119 (define-public PLATFORM
122 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
125 (let ((platform (string-tokenize
126 (vector-ref (uname) 0) char-set:letter+digit)))
127 (if (null? (cdr platform)) #f
128 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
132 (define native-getcwd getcwd)
134 (if (string-index x #\\)
136 (string-regexp-substitute
138 (string-regexp-substitute "\\\\" "/" x))))
139 ;; FIXME: this prints a warning.
140 (define-public (ly-getcwd)
141 (slashify (native-getcwd))))
142 (else (define-public ly-getcwd getcwd)))
144 (define-public (is-absolute? file-name)
145 (let ((file-name-length (string-length file-name)))
146 (if (= file-name-length 0)
148 (or (eq? (string-ref file-name 0) #\/)
149 (and (eq? PLATFORM 'windows)
150 (> file-name-length 2)
151 (eq? (string-ref file-name 1) #\:)
152 (eq? (string-ref file-name 2) #\/))))))
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
156 (define (type-check-list location signature arguments)
157 "Typecheck a list of arguments against a list of type
158 predicates. Print a message at LOCATION if any predicate failed."
159 (define (recursion-helper signature arguments count)
160 (define (helper pred? arg count)
161 (if (not (pred? arg))
167 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
168 count (type-name pred?) arg))
172 (if (null? signature)
174 (and (helper (car signature) (car arguments) count)
175 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
176 (recursion-helper signature arguments 1))
178 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
182 ;;(define-public (output-framework) (write "hello\n"))
184 (define output-tex-module
185 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
186 (define output-ps-module
187 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
189 (define-public (ps-output-expression expr port)
190 (display (eval expr output-ps-module) port))
192 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 ;; Safe definitions utility
194 (define safe-objects (list))
196 (define-macro (define-safe-public arglist . body)
197 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
198 The syntax is the same as `define*-public'."
199 (define (get-symbol arg)
201 (get-symbol (car arg))
203 (let ((safe-symbol (get-symbol arglist)))
205 (define*-public ,arglist
207 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
212 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 "define-music-types.scm"
222 "chord-ignatzek-names.scm"
224 "chord-generic-names.scm"
227 "music-functions.scm"
230 "define-music-properties.scm"
234 "parser-ly-from-scheme.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"
252 "layout-page-layout.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))
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)
364 (let ((failed (lilypond-all files)))
367 (ly:error (_ "failed files: ~S") (string-join failed))
370 ;; HACK: be sure to exit with single newline
374 (define-public (lilypond-all files)
376 (handler (lambda (key failed-file)
377 (set! failed (append (list failed-file) failed)))))
380 (lilypond-file handler x)
381 (ly:clear-anonymous-modules)
382 (if (ly:get-option 'debug-gc)
388 (define (lilypond-file handler file-name)
389 (catch 'ly-file-failed
390 (lambda () (ly:parse-file file-name))
391 (lambda (x . args) (handler x file-name))))
393 (use-modules (scm editor))
395 (define-public (running-from-gui?)
396 (let ((have-tty? (isatty? (current-input-port))))
397 ;; If no TTY and not using safe, assume running from GUI.
399 ((eq? PLATFORM 'windows)
400 ;; Always write to .log file.
402 ;; This only works for i586-mingw32msvc-gcc -mwindows
403 (not (string-match "standard input"
404 (format #f "~S" (current-input-port))))))
405 ;; FIXME: using -dgui would be nice, but it does not work
406 ((eq? PLATFORM 'foo-windows)
407 (ly:get-option 'gui))
408 ((eq? PLATFORM 'darwin) #f)
412 (define-public (gui-main files)
414 (gui-no-files-handler))
415 (let* ((base (basename (car files) ".ly"))
416 (log-name (string-append base ".log")))
417 (if (not (running-from-gui?))
418 (ly:message (_ "Redirecting output to ~a...") log-name))
419 (ly:stderr-redirect log-name "w")
420 (ly:message "# -*-compilation-*-")
421 (let ((failed (lilypond-all files)))
425 (ly:stderr-redirect "foo" "r")
426 (system (get-editor-command log-name 0 0 0))
427 (ly:error (_ "failed files: ~S") (string-join failed))
432 (define (gui-no-files-handler)
433 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
434 ;; FIXME: soft-code, localize
435 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
436 (cmd (get-editor-command welcome-ly 0 0 0)))
437 (ly:message (_ "Invoking `~a'...") cmd)
441 (or (not (running-from-gui?))
442 (ly:get-option 'safe)
443 (define lilypond-main gui-main))