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 (resolution 101 "resolution for generating bitmaps")
23 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
24 (preview-include-book-title #t "include book-titles in preview images.")
26 "load fonts via Ghostscript.")
27 (delete-intermediate-files #f
28 "delete unusable PostScript files")
29 (verbose #f "value for the --verbose flag")
31 "how much verbosity for TTF font embedding?")
33 "dump GC protection info")))
36 (if (defined? 'set-debug-cell-accesses!)
37 (set-debug-cell-accesses! #f))
39 ;(set-debug-cell-accesses! 1000)
41 (use-modules (ice-9 regex)
51 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
53 (define-public (print . args)
54 (apply format (cons (current-output-port) args)))
58 ;;; debugging evaluator is slower. This should
59 ;;; have a more sensible default.
61 (if (ly:get-option 'verbose)
64 (debug-enable 'backtrace)
65 (read-enable 'positions)))
68 (define-public tex-backend?
69 (member (ly:output-backend) '("texstr" "tex")))
71 (define-public parser #f)
73 (define-public (lilypond-version)
75 (map (lambda (x) (if (symbol? x)
83 ;; cpp hack to get useful error message
84 (define ifdef "First run this through cpp.")
85 (define ifndef "First run this through cpp.")
87 ;; gettext wrapper for guile < 1.7.2
88 (if (defined? 'gettext)
89 (define-public _ gettext)
90 (define-public _ ly:gettext))
92 (define-public (ly:load x)
93 (let* ((file-name (%search-load-path x)))
94 (if (ly:get-option 'verbose)
95 (ly:progress "[~A" file-name))
96 (primitive-load file-name)
97 (if (ly:get-option 'verbose)
100 (define-public TEX_STRING_HASHLIMIT 10000000)
103 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
106 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
109 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
113 (define-public PLATFORM
116 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
120 (define native-getcwd getcwd)
122 (if (string-index x #\\)
124 (string-regexp-substitute
126 (string-regexp-substitute "\\\\" "/" x))))
127 ;; FIXME: this prints a warning.
128 (define-public (ly-getcwd)
129 (slashify (native-getcwd))))
130 (else (define-public ly-getcwd getcwd)))
132 (define-public (is-absolute? file-name)
133 (let ((file-name-length (string-length file-name)))
134 (if (= file-name-length 0)
136 (or (eq? (string-ref file-name 0) #\/)
137 (and (eq? PLATFORM 'windows)
138 (> file-name-length 2)
139 (eq? (string-ref file-name 1) #\:)
140 (eq? (string-ref file-name 2) #\/))))))
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144 (define (type-check-list location signature arguments)
145 "Typecheck a list of arguments against a list of type
146 predicates. Print a message at LOCATION if any predicate failed."
147 (define (recursion-helper signature arguments count)
148 (define (helper pred? arg count)
149 (if (not (pred? arg))
155 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
156 count (type-name pred?) arg))
160 (if (null? signature)
162 (and (helper (car signature) (car arguments) count)
163 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
164 (recursion-helper signature arguments 1))
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
170 ;;(define-public (output-framework) (write "hello\n"))
172 (define output-tex-module
173 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
174 (define output-ps-module
175 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
177 (define-public (ps-output-expression expr port)
178 (display (eval expr output-ps-module) port))
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 ;; Safe definitions utility
182 (define safe-objects (list))
184 (define-macro (define-safe-public arglist . body)
185 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
186 The syntax is the same as `define*-public'."
187 (define (get-symbol arg)
189 (get-symbol (car arg))
191 (let ((safe-symbol (get-symbol arglist)))
193 (define*-public ,arglist
195 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
200 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
207 "define-music-types.scm"
210 "chord-ignatzek-names.scm"
212 "chord-generic-names.scm"
216 "music-functions.scm"
218 "define-music-properties.scm"
224 "define-context-properties.scm"
225 "translation-functions.scm"
235 "define-markup-commands.scm"
236 "define-grob-properties.scm"
238 "define-grob-interfaces.scm"
239 "define-stencil-commands.scm"
244 "backend-library.scm"
247 ;; must be after everything has been defined
251 (set! type-p-name-alist
253 (,boolean-or-symbol? . "boolean or symbol")
254 (,boolean? . "boolean")
256 (,grob-list? . "list of grobs")
257 (,hash-table? . "hash table")
258 (,input-port? . "input port")
259 (,integer? . "integer")
261 (,ly:context? . "context")
262 (,ly:dimension? . "dimension, in staff space")
263 (,ly:dir? . "direction")
264 (,ly:duration? . "duration")
265 (,ly:grob? . "layout object")
266 (,ly:input-location? . "input location")
267 (,ly:moment? . "moment")
268 (,ly:music? . "music")
269 (,ly:pitch? . "pitch")
270 (,ly:translator? . "translator")
271 (,ly:font-metric? . "font metric")
272 (,markup-list? . "list of markups")
273 (,markup? . "markup")
274 (,ly:music-list? . "list of music")
275 (,number-or-grob? . "number or grob")
276 (,number-or-string? . "number or string")
277 (,number-pair? . "pair of numbers")
278 (,number? . "number")
279 (,output-port? . "output port")
281 (,procedure? . "procedure")
282 (,scheme? . "any type")
283 (,string? . "string")
284 (,symbol? . "symbol")
285 (,vector? . "vector")))
290 (define gc-protect-stat-count 0)
291 (define-public (dump-gc-protects)
292 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
293 (let* ((protects (sort
294 (hash-table->alist (ly:protects))
296 (< (object-address (car a))
297 (object-address (car b))))))
299 (out-file-name (string-append
300 "gcstat-" (number->string gc-protect-stat-count)
302 (outfile (open-file out-file-name "w")))
304 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
307 (lambda (x) (not (symbol? x)))
314 (map object->string (list (object-address x) c x))
320 (if (defined? 'gc-live-object-stats)
324 (stats (gc-live-object-stats))
329 (format outfile "~a: ~a\n" (car x) (cdr x)))
330 (sort (gc-live-object-stats)
332 (string<? (car x) (car y)))))
337 (define-public (tweak-grob-property grob sym val)
338 (set! (ly:grob-property grob sym) val))
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 (define-public (lilypond-main files)
342 "Entry point for LilyPond."
347 (let ((failed (lilypond-all files)))
350 (ly:error (_ "failed files: ~S") (string-join failed))
353 ;; HACK: be sure to exit with single newline
357 (define (no-files-handler)
361 (define-public (lilypond-all files)
363 (handler (lambda (key failed-file)
364 (set! failed (append (list failed-file) failed)))))
365 ;;(handler (lambda (key . arg) (set! failed (append arg failed)))))
366 (for-each (lambda (x) (lilypond-file handler x)) files)
369 (define (lilypond-file handler file-name)
370 (catch 'ly-file-failed
371 (lambda () (ly:parse-file file-name))
372 (lambda (x . args) (handler x file-name)))
374 (if (ly:get-option 'debug-gc)
377 (use-modules (scm editor))
379 (define-public (running-from-gui?)
380 (let ((have-tty? (isatty? (current-input-port))))
381 ;; If no TTY and not using safe, assume running from GUI.
383 ((eq? PLATFORM 'windows)
384 ;; This only works for i586-mingw32msvc-gcc -mwindows
385 (not (string-match "standard input"
386 (format #f "~S" (current-input-port)))))
387 ((eq? PLATFORM 'darwin) #f)
391 (define-public (gui-main files)
393 (gui-no-files-handler))
394 (let* ((base (basename (car files) ".ly"))
395 (log-name (string-append base ".log")))
396 (if (not (running-from-gui?))
397 (ly:message (_ "Redirecting output to ~a...") log-name))
398 (ly:stderr-redirect log-name "w")
399 (ly:message "# -*-compilation-*-")
400 (let ((failed (lilypond-all files)))
404 (ly:stderr-redirect "foo" "r")
405 (system (get-editor-command log-name 0 0 0))
406 (ly:error (_ "failed files: ~S") (string-join failed))
411 (define (gui-no-files-handler)
412 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
413 ;; FIXME: soft-code, localize
414 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
415 (cmd (get-editor-command welcome-ly 0 0 0)))
416 (ly:message (_ "Invoking `~a'...") cmd)
420 (or (not (running-from-gui?))
421 (ly:get-option 'safe)
422 (define lilypond-main gui-main))