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."
194 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195 ;; Safe definitions utility
196 (define safe-objects (list))
198 (define-macro (define-safe-public arglist . body)
199 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
200 The syntax is the same as `define*-public'."
201 (define (get-symbol arg)
203 (get-symbol (car arg))
205 (let ((safe-symbol (get-symbol arglist)))
207 (define*-public ,arglist
209 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 "define-music-types.scm"
224 "chord-ignatzek-names.scm"
226 "chord-generic-names.scm"
230 "music-functions.scm"
232 "define-music-properties.scm"
238 "define-context-properties.scm"
239 "translation-functions.scm"
249 "define-markup-commands.scm"
250 "define-grob-properties.scm"
252 "define-grob-interfaces.scm"
257 "backend-library.scm"
260 ;; must be after everything has been defined
264 (set! type-p-name-alist
266 (,boolean-or-symbol? . "boolean or symbol")
267 (,boolean? . "boolean")
269 (,grob-list? . "list of grobs")
270 (,hash-table? . "hash table")
271 (,input-port? . "input port")
272 (,integer? . "integer")
274 (,ly:context? . "context")
275 (,ly:dimension? . "dimension, in staff space")
276 (,ly:dir? . "direction")
277 (,ly:duration? . "duration")
278 (,ly:grob? . "layout object")
279 (,ly:input-location? . "input location")
280 (,ly:moment? . "moment")
281 (,ly:music? . "music")
282 (,ly:pitch? . "pitch")
283 (,ly:translator? . "translator")
284 (,ly:font-metric? . "font metric")
285 (,markup-list? . "list of markups")
286 (,markup? . "markup")
287 (,ly:music-list? . "list of music")
288 (,number-or-grob? . "number or grob")
289 (,number-or-string? . "number or string")
290 (,number-pair? . "pair of numbers")
291 (,number? . "number")
292 (,output-port? . "output port")
294 (,procedure? . "procedure")
295 (,scheme? . "any type")
296 (,string? . "string")
297 (,symbol? . "symbol")
298 (,vector? . "vector")))
303 (define gc-protect-stat-count 0)
304 (define-public (dump-gc-protects)
305 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
306 (let* ((protects (sort
307 (hash-table->alist (ly:protects))
309 (< (object-address (car a))
310 (object-address (car b))))))
311 (out-file-name (string-append
312 "gcstat-" (number->string gc-protect-stat-count)
314 (outfile (open-file out-file-name "w")))
316 (display "Dumping gc protected objs to ...\n")
319 (lambda (x) (not (symbol? x)))
326 (map object->string (list (object-address x) c x))
332 (define-public (tweak-grob-property grob sym val)
333 (set! (ly:grob-property grob sym) val))
335 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
336 (define-public (lilypond-main files)
337 "Entry point for LilyPond."
342 (let ((failed (lilypond-all files)))
345 (ly:error (_ "failed files: ~S") (string-join failed))
348 ;; HACK: be sure to exit with single newline
352 (define (no-files-handler)
356 (define-public (lilypond-all files)
358 (handler (lambda (key failed-file)
359 (set! failed (append (list failed-file) failed)))))
360 ;;(handler (lambda (key . arg) (set! failed (append arg failed)))))
361 (for-each (lambda (x) (lilypond-file handler x)) files)
364 (define (lilypond-file handler file-name)
365 (catch 'ly-file-failed
366 (lambda () (ly:parse-file file-name))
367 (lambda (x . args) (handler x file-name)))
368 ;;(lambda (x) (handler x f)))
372 (use-modules (scm editor))
374 (define (running-from-gui?)
375 (let ((have-tty? (isatty? (current-input-port))))
376 ;; If no TTY and not using safe, assume running from GUI.
378 ((eq? PLATFORM 'windows)
379 ;; This only works for i586-mingw32msvc-gcc -mwindows
380 (not (string-match "standard input"
381 (format #f "~S" (current-input-port)))))
382 ((eq? PLATFORM 'darwin) #f)
386 (define-public (gui-main files)
387 (if (null? files) (gui-no-files-handler))
388 (let* ((base (basename (car files) ".ly"))
389 (log-name (string-append base ".log")))
390 (if (not (running-from-gui?))
391 (ly:message (_ "Redirecting output to ~a...") log-name))
392 (ly:stderr-redirect log-name "w")
393 (ly:message "# -*-compilation-*-")
394 (let ((failed (lilypond-all files)))
398 (ly:stderr-redirect "foo" "r")
399 (system (get-editor-command log-name 0 0))
400 (ly:error (_ "failed files: ~S") (string-join failed))
405 (define (gui-no-files-handler)
406 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
407 ;; FIXME: soft-code, localize
408 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
409 (cmd (get-editor-command welcome-ly 0 0)))
410 (ly:message (_ "Invoking `~a'...") cmd)
414 (or (not (running-from-gui?))
415 (ly:get-option 'safe)
416 (define lilypond-main gui-main))