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 116 "resolution for generating bitmaps")
26 (preview-include-book-title #t "include book-titles in preview images.")
28 "load fonts via Ghostscript.")
29 (delete-intermediate-files #f
30 "delete unusable PostScript files")
31 (verbose #f "value for the --verbose flag")
33 "how much verbosity for TTF font embedding?")
35 "dump GC protection info")))
38 (if (defined? 'set-debug-cell-accesses!)
39 (set-debug-cell-accesses! #f))
41 ;(set-debug-cell-accesses! 1000)
43 (use-modules (ice-9 regex)
53 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
55 (define-public (print . args)
56 (apply format (cons (current-output-port) args)))
60 ;;; debugging evaluator is slower. This should
61 ;;; have a more sensible default.
63 (if (ly:get-option 'verbose)
66 (debug-enable 'backtrace)
67 (read-enable 'positions)))
70 (define-public tex-backend?
71 (member (ly:output-backend) '("texstr" "tex")))
73 (define-public parser #f)
75 (define-public (lilypond-version)
77 (map (lambda (x) (if (symbol? x)
85 ;; cpp hack to get useful error message
86 (define ifdef "First run this through cpp.")
87 (define ifndef "First run this through cpp.")
89 ;; gettext wrapper for guile < 1.7.2
90 (if (defined? 'gettext)
91 (define-public _ gettext)
92 (define-public _ ly:gettext))
94 (define-public (ly:load x)
95 (let* ((file-name (%search-load-path x)))
96 (if (ly:get-option 'verbose)
97 (ly:progress "[~A" file-name))
98 (primitive-load file-name)
99 (if (ly:get-option 'verbose)
102 (define-public TEX_STRING_HASHLIMIT 10000000)
105 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
108 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
111 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
115 (define-public PLATFORM
118 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
122 (define native-getcwd getcwd)
124 (if (string-index x #\\)
126 (string-regexp-substitute
128 (string-regexp-substitute "\\\\" "/" x))))
129 ;; FIXME: this prints a warning.
130 (define-public (ly-getcwd)
131 (slashify (native-getcwd))))
132 (else (define-public ly-getcwd getcwd)))
134 (define-public (is-absolute? file-name)
135 (let ((file-name-length (string-length file-name)))
136 (if (= file-name-length 0)
138 (or (eq? (string-ref file-name 0) #\/)
139 (and (eq? PLATFORM 'windows)
140 (> file-name-length 2)
141 (eq? (string-ref file-name 1) #\:)
142 (eq? (string-ref file-name 2) #\/))))))
144 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
146 (define (type-check-list location signature arguments)
147 "Typecheck a list of arguments against a list of type
148 predicates. Print a message at LOCATION if any predicate failed."
149 (define (recursion-helper signature arguments count)
150 (define (helper pred? arg count)
151 (if (not (pred? arg))
157 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
158 count (type-name pred?) arg))
162 (if (null? signature)
164 (and (helper (car signature) (car arguments) count)
165 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
166 (recursion-helper signature arguments 1))
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 ;;(define-public (output-framework) (write "hello\n"))
174 (define output-tex-module
175 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
176 (define output-ps-module
177 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
179 (define-public (ps-output-expression expr port)
180 (display (eval expr output-ps-module) port))
182 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
183 ;; Safe definitions utility
184 (define safe-objects (list))
186 (define-macro (define-safe-public arglist . body)
187 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
188 The syntax is the same as `define*-public'."
189 (define (get-symbol arg)
191 (get-symbol (car arg))
193 (let ((safe-symbol (get-symbol arglist)))
195 (define*-public ,arglist
197 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
202 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
209 "define-music-types.scm"
212 "chord-ignatzek-names.scm"
214 "chord-generic-names.scm"
218 "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 (stats (gc-live-object-stats))
331 (format outfile "~a: ~a\n" (car x) (cdr x)))
332 (sort (gc-live-object-stats)
334 (string<? (car x) (car y)))))
339 (define-public (tweak-grob-property grob sym val)
340 (set! (ly:grob-property grob sym) val))
342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343 (define-public (lilypond-main files)
344 "Entry point for LilyPond."
349 (let ((failed (lilypond-all files)))
352 (ly:error (_ "failed files: ~S") (string-join failed))
355 ;; HACK: be sure to exit with single newline
359 (define (no-files-handler)
363 (define-public (lilypond-all files)
365 (handler (lambda (key failed-file)
366 (set! failed (append (list failed-file) failed)))))
367 ;;(handler (lambda (key . arg) (set! failed (append arg failed)))))
368 (for-each (lambda (x) (lilypond-file handler x)) files)
371 (define (lilypond-file handler file-name)
372 (catch 'ly-file-failed
373 (lambda () (ly:parse-file file-name))
374 (lambda (x . args) (handler x file-name)))
376 (if (ly:get-option 'debug-gc)
379 (use-modules (scm editor))
381 (define-public (running-from-gui?)
382 (let ((have-tty? (isatty? (current-input-port))))
383 ;; If no TTY and not using safe, assume running from GUI.
385 ((eq? PLATFORM 'windows)
386 ;; This only works for i586-mingw32msvc-gcc -mwindows
387 (not (string-match "standard input"
388 (format #f "~S" (current-input-port)))))
389 ((eq? PLATFORM 'darwin) #f)
393 (define-public (gui-main files)
395 (gui-no-files-handler))
396 (let* ((base (basename (car files) ".ly"))
397 (log-name (string-append base ".log")))
398 (if (not (running-from-gui?))
399 (ly:message (_ "Redirecting output to ~a...") log-name))
400 (ly:stderr-redirect log-name "w")
401 (ly:message "# -*-compilation-*-")
402 (let ((failed (lilypond-all files)))
406 (ly:stderr-redirect "foo" "r")
407 (system (get-editor-command log-name 0 0 0))
408 (ly:error (_ "failed files: ~S") (string-join failed))
413 (define (gui-no-files-handler)
414 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
415 ;; FIXME: soft-code, localize
416 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
417 (cmd (get-editor-command welcome-ly 0 0 0)))
418 (ly:message (_ "Invoking `~a'...") cmd)
422 (or (not (running-from-gui?))
423 (ly:get-option 'safe)
424 (define lilypond-main gui-main))