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 "experimental mechanism for remembering tweaks")
23 (resolution 101 "resolution for generating bitmaps")
24 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
25 (preview-include-book-title #t "include book-titles in preview images.")
27 "load fonts via Ghostscript.")
28 (delete-intermediate-files #f
29 "delete unusable PostScript files")
30 (verbose #f "value for the --verbose flag")
32 "how much verbosity for TTF font embedding?")
34 "dump GC protection info")))
37 (if (defined? 'set-debug-cell-accesses!)
38 (set-debug-cell-accesses! #f))
40 ;(set-debug-cell-accesses! 1000)
42 (use-modules (ice-9 regex)
52 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
54 (define-public (print . args)
55 (apply format (cons (current-output-port) args)))
59 ;;; debugging evaluator is slower. This should
60 ;;; have a more sensible default.
62 (if (ly:get-option 'verbose)
65 (debug-enable 'backtrace)
66 (read-enable 'positions)))
69 (define-public tex-backend?
70 (member (ly:output-backend) '("texstr" "tex")))
72 (define-public parser #f)
74 (define-public (lilypond-version)
76 (map (lambda (x) (if (symbol? x)
84 ;; cpp hack to get useful error message
85 (define ifdef "First run this through cpp.")
86 (define ifndef "First run this through cpp.")
88 ;; gettext wrapper for guile < 1.7.2
89 (if (defined? 'gettext)
90 (define-public _ gettext)
91 (define-public _ ly:gettext))
93 (define-public (ly:load x)
94 (let* ((file-name (%search-load-path x)))
95 (if (ly:get-option 'verbose)
96 (ly:progress "[~A" file-name))
97 (primitive-load file-name)
98 (if (ly:get-option 'verbose)
101 (define-public TEX_STRING_HASHLIMIT 10000000)
104 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
107 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
110 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
114 (define-public PLATFORM
117 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
121 (define native-getcwd getcwd)
123 (if (string-index x #\\)
125 (string-regexp-substitute
127 (string-regexp-substitute "\\\\" "/" x))))
128 ;; FIXME: this prints a warning.
129 (define-public (ly-getcwd)
130 (slashify (native-getcwd))))
131 (else (define-public ly-getcwd getcwd)))
133 (define-public (is-absolute? file-name)
134 (let ((file-name-length (string-length file-name)))
135 (if (= file-name-length 0)
137 (or (eq? (string-ref file-name 0) #\/)
138 (and (eq? PLATFORM 'windows)
139 (> file-name-length 2)
140 (eq? (string-ref file-name 1) #\:)
141 (eq? (string-ref file-name 2) #\/))))))
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
145 (define (type-check-list location signature arguments)
146 "Typecheck a list of arguments against a list of type
147 predicates. Print a message at LOCATION if any predicate failed."
148 (define (recursion-helper signature arguments count)
149 (define (helper pred? arg count)
150 (if (not (pred? arg))
156 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
157 count (type-name pred?) arg))
161 (if (null? signature)
163 (and (helper (car signature) (car arguments) count)
164 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
165 (recursion-helper signature arguments 1))
167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
171 ;;(define-public (output-framework) (write "hello\n"))
173 (define output-tex-module
174 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
175 (define output-ps-module
176 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
178 (define-public (ps-output-expression expr port)
179 (display (eval expr output-ps-module) port))
181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
182 ;; Safe definitions utility
183 (define safe-objects (list))
185 (define-macro (define-safe-public arglist . body)
186 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
187 The syntax is the same as `define*-public'."
188 (define (get-symbol arg)
190 (get-symbol (car arg))
192 (let ((safe-symbol (get-symbol arglist)))
194 (define*-public ,arglist
196 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 "define-music-types.scm"
211 "chord-ignatzek-names.scm"
213 "chord-generic-names.scm"
217 "music-functions.scm"
220 "define-music-properties.scm"
226 "define-context-properties.scm"
227 "translation-functions.scm"
237 "define-markup-commands.scm"
238 "define-grob-properties.scm"
240 "define-grob-interfaces.scm"
241 "define-stencil-commands.scm"
246 "backend-library.scm"
249 ;; must be after everything has been defined
253 (set! type-p-name-alist
255 (,boolean-or-symbol? . "boolean or symbol")
256 (,boolean? . "boolean")
258 (,grob-list? . "list of grobs")
259 (,hash-table? . "hash table")
260 (,input-port? . "input port")
261 (,integer? . "integer")
263 (,ly:context? . "context")
264 (,ly:dimension? . "dimension, in staff space")
265 (,ly:dir? . "direction")
266 (,ly:duration? . "duration")
267 (,ly:grob? . "layout object")
268 (,ly:input-location? . "input location")
269 (,ly:moment? . "moment")
270 (,ly:music? . "music")
271 (,ly:pitch? . "pitch")
272 (,ly:translator? . "translator")
273 (,ly:font-metric? . "font metric")
274 (,markup-list? . "list of markups")
275 (,markup? . "markup")
276 (,ly:music-list? . "list of music")
277 (,number-or-grob? . "number or grob")
278 (,number-or-string? . "number or string")
279 (,number-pair? . "pair of numbers")
280 (,number? . "number")
281 (,output-port? . "output port")
283 (,procedure? . "procedure")
284 (,scheme? . "any type")
285 (,string? . "string")
286 (,symbol? . "symbol")
287 (,vector? . "vector")))
292 (define gc-protect-stat-count 0)
293 (define-public (dump-gc-protects)
294 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
295 (let* ((protects (sort
296 (hash-table->alist (ly:protects))
298 (< (object-address (car a))
299 (object-address (car b))))))
301 (out-file-name (string-append
302 "gcstat-" (number->string gc-protect-stat-count)
304 (outfile (open-file out-file-name "w")))
306 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
309 (lambda (x) (not (symbol? x)))
316 (map object->string (list (object-address x) c x))
322 (if (defined? 'gc-live-object-stats)
326 (display "Live object statistics: GC'ing\n")
330 (set! stats (gc-live-object-stats))
331 (display "Dumping live object statistics.\n")
335 (format outfile "~a: ~a\n" (car x) (cdr x)))
336 (sort (gc-live-object-stats)
338 (string<? (car x) (car y)))))
343 (define-public (tweak-grob-property grob sym val)
344 (set! (ly:grob-property grob sym) val))
346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347 (define-public (lilypond-main files)
348 "Entry point for LilyPond."
353 (let ((failed (lilypond-all files)))
356 (ly:error (_ "failed files: ~S") (string-join failed))
359 ;; HACK: be sure to exit with single newline
363 (define (no-files-handler)
367 (define-public (lilypond-all files)
369 (handler (lambda (key failed-file)
370 (set! failed (append (list failed-file) failed)))))
371 ;;(handler (lambda (key . arg) (set! failed (append arg failed)))))
372 (for-each (lambda (x) (lilypond-file handler x)) files)
375 (define (lilypond-file handler file-name)
376 (catch 'ly-file-failed
377 (lambda () (ly:parse-file file-name))
378 (lambda (x . args) (handler x file-name)))
380 (if (ly:get-option 'debug-gc)
383 (use-modules (scm editor))
385 (define-public (running-from-gui?)
386 (let ((have-tty? (isatty? (current-input-port))))
387 ;; If no TTY and not using safe, assume running from GUI.
389 ((eq? PLATFORM 'windows)
390 ;; This only works for i586-mingw32msvc-gcc -mwindows
391 (not (string-match "standard input"
392 (format #f "~S" (current-input-port)))))
393 ((eq? PLATFORM 'darwin) #f)
397 (define-public (gui-main files)
399 (gui-no-files-handler))
400 (let* ((base (basename (car files) ".ly"))
401 (log-name (string-append base ".log")))
402 (if (not (running-from-gui?))
403 (ly:message (_ "Redirecting output to ~a...") log-name))
404 (ly:stderr-redirect log-name "w")
405 (ly:message "# -*-compilation-*-")
406 (let ((failed (lilypond-all files)))
410 (ly:stderr-redirect "foo" "r")
411 (system (get-editor-command log-name 0 0 0))
412 (ly:error (_ "failed files: ~S") (string-join failed))
417 (define (gui-no-files-handler)
418 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
419 ;; FIXME: soft-code, localize
420 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
421 (cmd (get-editor-command welcome-ly 0 0 0)))
422 (ly:message (_ "Invoking `~a'...") cmd)
426 (or (not (running-from-gui?))
427 (ly:get-option 'safe)
428 (define lilypond-main gui-main))