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 (if (defined? 'set-debug-cell-accesses!)
10 (set-debug-cell-accesses! #f))
12 ;(set-debug-cell-accesses! 1000)
14 (use-modules (ice-9 regex)
24 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
26 (define-public (print . args)
27 (apply format (cons (current-output-port) args)))
31 ;;; debugging evaluator is slower. This should
32 ;;; have a more sensible default.
34 (if (ly:get-option 'verbose)
37 (debug-enable 'backtrace)
38 (read-enable 'positions)))
40 ;; initialize defaults.
41 (ly:set-option 'command-line-settings
43 (preview-include-book-title . #t)
46 (define-public tex-backend?
47 (member (ly:output-backend) '("texstr" "tex")))
49 (define-public parser #f)
51 (define-public (lilypond-version)
53 (map (lambda (x) (if (symbol? x)
61 ;; cpp hack to get useful error message
62 (define ifdef "First run this through cpp.")
63 (define ifndef "First run this through cpp.")
65 ;; gettext wrapper for guile < 1.7.2
66 (if (defined? 'gettext)
67 (define-public _ gettext)
68 (define-public _ ly:gettext))
70 (define-public (ly:load x)
71 (let* ((file-name (%search-load-path x)))
72 (if (ly:get-option 'verbose)
73 (ly:progress "[~A" file-name))
74 (primitive-load file-name)
75 (if (ly:get-option 'verbose)
78 (define-public TEX_STRING_HASHLIMIT 10000000)
80 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82 (define (type-check-list location signature arguments)
83 "Typecheck a list of arguments against a list of type
84 predicates. Print a message at LOCATION if any predicate failed."
85 (define (recursion-helper signature arguments count)
86 (define (helper pred? arg count)
93 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
94 count (type-name pred?) arg))
100 (and (helper (car signature) (car arguments) count)
101 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
102 (recursion-helper signature arguments 1))
104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
108 ;;(define-public (output-framework) (write "hello\n"))
110 (define output-tex-module
111 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
112 (define output-ps-module
113 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
115 (define-public (ps-output-expression expr port)
116 (display (eval expr output-ps-module) port))
118 ;; TODO: generate this list by registering the stencil expressions
119 ;; stencil expressions should have docstrings.
120 (define-public (ly:all-stencil-expressions)
121 "Return list of stencil expressions."
149 ;; - generate this list by registering the output-backend-commands
150 ;; output-backend-commands should have docstrings.
151 ;; - remove hard copies in output-ps output-tex
152 (define-public (ly:all-output-backend-commands)
153 "Return list of output backend commands."
161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162 ;; Safe definitions utility
163 (define safe-objects (list))
165 (define-macro (define-safe-public arglist . body)
166 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
167 The syntax is the same as `define*-public'."
168 (define (get-symbol arg)
170 (get-symbol (car arg))
172 (let ((safe-symbol (get-symbol arglist)))
174 (define*-public ,arglist
176 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
188 "define-music-types.scm"
191 "chord-ignatzek-names.scm"
193 "chord-generic-names.scm"
197 "music-functions.scm"
199 "define-music-properties.scm"
205 "define-context-properties.scm"
206 "translation-functions.scm"
216 "define-markup-commands.scm"
217 "define-grob-properties.scm"
219 "define-grob-interfaces.scm"
224 "backend-library.scm"
227 ;; must be after everything has been defined
231 (set! type-p-name-alist
233 (,boolean-or-symbol? . "boolean or symbol")
234 (,boolean? . "boolean")
236 (,grob-list? . "list of grobs")
237 (,hash-table? . "hash table")
238 (,input-port? . "input port")
239 (,integer? . "integer")
241 (,ly:context? . "context")
242 (,ly:dimension? . "dimension, in staff space")
243 (,ly:dir? . "direction")
244 (,ly:duration? . "duration")
245 (,ly:grob? . "layout object")
246 (,ly:input-location? . "input location")
247 (,ly:moment? . "moment")
248 (,ly:music? . "music")
249 (,ly:pitch? . "pitch")
250 (,ly:translator? . "translator")
251 (,ly:font-metric? . "font metric")
252 (,markup-list? . "list of markups")
253 (,markup? . "markup")
254 (,ly:music-list? . "list of music")
255 (,number-or-grob? . "number or grob")
256 (,number-or-string? . "number or string")
257 (,number-pair? . "pair of numbers")
258 (,number? . "number")
259 (,output-port? . "output port")
261 (,procedure? . "procedure")
262 (,scheme? . "any type")
263 (,string? . "string")
264 (,symbol? . "symbol")
265 (,vector? . "vector")))
270 (define gc-protect-stat-count 0)
271 (define-public (dump-gc-protects)
272 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
273 (let* ((protects (sort
274 (hash-table->alist (ly:protects))
276 (< (object-address (car a))
277 (object-address (car b))))))
278 (out-file-name (string-append
279 "gcstat-" (number->string gc-protect-stat-count)
281 (outfile (open-file out-file-name "w")))
283 (display "Dumping gc protected objs to ...\n")
286 (lambda (x) (not (symbol? x)))
293 (map object->string (list (object-address x) c x))
299 (define-public (tweak-grob-property grob sym val)
300 (set! (ly:grob-property grob sym) val))
302 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
303 (define-public (lilypond-main files)
304 "Entry point for LilyPond."
309 (let ((failed (lilypond-all files)))
312 (ly:error (_ "failed files: ~S") (string-join failed))
315 ;; HACK: be sure to exit with single newline
319 (define (no-files-handler)
323 (define-public (lilypond-all files)
325 (handler (lambda (key failed-file)
326 (set! failed (append (list failed-file) failed)))))
327 ;;(handler (lambda (key . arg) (set! failed (append arg failed)))))
328 (for-each (lambda (x) (lilypond-file handler x)) files)))
330 (define (lilypond-file handler file-name)
331 (catch 'ly-file-failed
332 (lambda () (ly:parse-file file-name))
333 (lambda (x . args) (handler x file-name)))
334 ;;(lambda (x) (handler x f)))
338 (use-modules (scm editor))
340 (define-public (gui-main files)
341 (if (null? files) (gui-no-files-handler))
342 (let* ((base (basename (car files) ".ly"))
343 (log-name (string-append base ".log"))
344 (log-file (open-file log-name "w")))
345 ;; Ugh, his opens a terminal
346 ;; Do this when invoked using --quiet, --log or something?
347 ;; (ly:message (_ "Redirecting output to ~a...") log-name)
348 (ly:port-move (fileno (current-error-port)) log-file)
349 (ly:message "# -*-compilation-*-")
350 (if (null? (lilypond-all files))
353 (system (get-editor-command log-name 0 0))
356 (define (gui-no-files-handler)
357 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
358 ;; FIXME: soft-code, localize
359 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
360 (cmd (get-editor-command welcome-ly 0 0)))
361 (ly:message (_ "Invoking `~a'...") cmd)
365 ;; If no TTY and not using safe, assume running from GUI.
366 (or (isatty? (current-input-port))
367 (ly:get-option 'safe)
368 (define lilypond-main gui-main))