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)
81 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
84 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
87 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
89 (define-public PLATFORM
92 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
96 (define native-getcwd getcwd)
98 (if (string-index x #\/)
100 (string-regexp-substitute "\\\\" "/" x)))
101 ;; FIXME: this prints a warning.
102 (define-public (ly-getcwd)
103 (slashify (native-getcwd))))
104 (else (define-public ly-getcwd getcwd)))
106 (define-public (is-absolute? file-name)
107 (let ((file-name-length (string-length file-name)))
108 (if (= file-name-length 0)
110 (or (eq? (string-ref file-name 0) #\/)
111 (and (eq? PLATFORM 'windows)
112 (> file-name-length 2)
113 (eq? (string-ref file-name 1) #\:)
114 (eq? (string-ref file-name 2) #\/))))))
116 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 (define (type-check-list location signature arguments)
119 "Typecheck a list of arguments against a list of type
120 predicates. Print a message at LOCATION if any predicate failed."
121 (define (recursion-helper signature arguments count)
122 (define (helper pred? arg count)
123 (if (not (pred? arg))
129 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
130 count (type-name pred?) arg))
134 (if (null? signature)
136 (and (helper (car signature) (car arguments) count)
137 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
138 (recursion-helper signature arguments 1))
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144 ;;(define-public (output-framework) (write "hello\n"))
146 (define output-tex-module
147 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
148 (define output-ps-module
149 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
151 (define-public (ps-output-expression expr port)
152 (display (eval expr output-ps-module) port))
154 ;; TODO: generate this list by registering the stencil expressions
155 ;; stencil expressions should have docstrings.
156 (define-public (ly:all-stencil-expressions)
157 "Return list of stencil expressions."
183 ;; - generate this list by registering the output-backend-commands
184 ;; output-backend-commands should have docstrings.
185 ;; - remove hard copies in output-ps output-tex
186 (define-public (ly:all-output-backend-commands)
187 "Return list of output backend commands."
195 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 ;; Safe definitions utility
197 (define safe-objects (list))
199 (define-macro (define-safe-public arglist . body)
200 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
201 The syntax is the same as `define*-public'."
202 (define (get-symbol arg)
204 (get-symbol (car arg))
206 (let ((safe-symbol (get-symbol arglist)))
208 (define*-public ,arglist
210 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
222 "define-music-types.scm"
225 "chord-ignatzek-names.scm"
227 "chord-generic-names.scm"
231 "music-functions.scm"
233 "define-music-properties.scm"
239 "define-context-properties.scm"
240 "translation-functions.scm"
250 "define-markup-commands.scm"
251 "define-grob-properties.scm"
253 "define-grob-interfaces.scm"
258 "backend-library.scm"
261 ;; must be after everything has been defined
265 (set! type-p-name-alist
267 (,boolean-or-symbol? . "boolean or symbol")
268 (,boolean? . "boolean")
270 (,grob-list? . "list of grobs")
271 (,hash-table? . "hash table")
272 (,input-port? . "input port")
273 (,integer? . "integer")
275 (,ly:context? . "context")
276 (,ly:dimension? . "dimension, in staff space")
277 (,ly:dir? . "direction")
278 (,ly:duration? . "duration")
279 (,ly:grob? . "layout object")
280 (,ly:input-location? . "input location")
281 (,ly:moment? . "moment")
282 (,ly:music? . "music")
283 (,ly:pitch? . "pitch")
284 (,ly:translator? . "translator")
285 (,ly:font-metric? . "font metric")
286 (,markup-list? . "list of markups")
287 (,markup? . "markup")
288 (,ly:music-list? . "list of music")
289 (,number-or-grob? . "number or grob")
290 (,number-or-string? . "number or string")
291 (,number-pair? . "pair of numbers")
292 (,number? . "number")
293 (,output-port? . "output port")
295 (,procedure? . "procedure")
296 (,scheme? . "any type")
297 (,string? . "string")
298 (,symbol? . "symbol")
299 (,vector? . "vector")))
304 (define gc-protect-stat-count 0)
305 (define-public (dump-gc-protects)
306 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
307 (let* ((protects (sort
308 (hash-table->alist (ly:protects))
310 (< (object-address (car a))
311 (object-address (car b))))))
312 (out-file-name (string-append
313 "gcstat-" (number->string gc-protect-stat-count)
315 (outfile (open-file out-file-name "w")))
317 (display "Dumping gc protected objs to ...\n")
320 (lambda (x) (not (symbol? x)))
327 (map object->string (list (object-address x) c x))
333 (define-public (tweak-grob-property grob sym val)
334 (set! (ly:grob-property grob sym) val))
336 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
337 (define-public (lilypond-main files)
338 "Entry point for LilyPond."
343 (let ((failed (lilypond-all files)))
346 (ly:error (_ "failed files: ~S") (string-join failed))
349 ;; HACK: be sure to exit with single newline
353 (define (no-files-handler)
357 (define-public (lilypond-all files)
359 (handler (lambda (key failed-file)
360 (set! failed (append (list failed-file) failed)))))
361 ;;(handler (lambda (key . arg) (set! failed (append arg failed)))))
362 (for-each (lambda (x) (lilypond-file handler x)) files)
365 (define (lilypond-file handler file-name)
366 (catch 'ly-file-failed
367 (lambda () (ly:parse-file file-name))
368 (lambda (x . args) (handler x file-name)))
369 ;;(lambda (x) (handler x f)))
373 (use-modules (scm editor))
375 (define (running-from-gui?)
376 (let ((have-tty? (isatty? (current-input-port))))
377 ;; If no TTY and not using safe, assume running from GUI.
379 ((eq? PLATFORM 'windows)
380 ;; This only works for i586-mingw32msvc-gcc -mwindows
381 (not (string-match "standard input"
382 (format #f "~S" (current-input-port)))))
383 ((eq? PLATFORM 'darwin) #f)
387 (define-public (gui-main files)
388 (if (null? files) (gui-no-files-handler))
389 (let* ((base (basename (car files) ".ly"))
390 (log-name (string-append base ".log")))
391 (if (not (running-from-gui?))
392 (ly:message (_ "Redirecting output to ~a...") log-name))
393 (ly:stderr-redirect log-name "w")
394 (ly:message "# -*-compilation-*-")
395 (let ((failed (lilypond-all files)))
399 (ly:stderr-redirect "foo" "r")
400 (system (get-editor-command log-name 0 0))
401 (ly:error (_ "failed files: ~S") (string-join failed))
406 (define (gui-no-files-handler)
407 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
408 ;; FIXME: soft-code, localize
409 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
410 (cmd (get-editor-command welcome-ly 0 0)))
411 (ly:message (_ "Invoking `~a'...") cmd)
415 (or (not (running-from-gui?))
416 (ly:get-option 'safe)
417 (define lilypond-main gui-main))