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
102 (string-regexp-substitute "\\\\" "/" x))))
103 ;; FIXME: this prints a warning.
104 (define-public (ly-getcwd)
105 (slashify (native-getcwd))))
106 (else (define-public ly-getcwd getcwd)))
108 (define-public (is-absolute? file-name)
109 (let ((file-name-length (string-length file-name)))
110 (if (= file-name-length 0)
112 (or (eq? (string-ref file-name 0) #\/)
113 (and (eq? PLATFORM 'windows)
114 (> file-name-length 2)
115 (eq? (string-ref file-name 1) #\:)
116 (eq? (string-ref file-name 2) #\/))))))
118 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
120 (define (type-check-list location signature arguments)
121 "Typecheck a list of arguments against a list of type
122 predicates. Print a message at LOCATION if any predicate failed."
123 (define (recursion-helper signature arguments count)
124 (define (helper pred? arg count)
125 (if (not (pred? arg))
131 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
132 count (type-name pred?) arg))
136 (if (null? signature)
138 (and (helper (car signature) (car arguments) count)
139 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
140 (recursion-helper signature arguments 1))
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
146 ;;(define-public (output-framework) (write "hello\n"))
148 (define output-tex-module
149 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
150 (define output-ps-module
151 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
153 (define-public (ps-output-expression expr port)
154 (display (eval expr output-ps-module) port))
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 ;; Safe definitions utility
158 (define safe-objects (list))
160 (define-macro (define-safe-public arglist . body)
161 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
162 The syntax is the same as `define*-public'."
163 (define (get-symbol arg)
165 (get-symbol (car arg))
167 (let ((safe-symbol (get-symbol arglist)))
169 (define*-public ,arglist
171 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
176 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
183 "define-music-types.scm"
186 "chord-ignatzek-names.scm"
188 "chord-generic-names.scm"
192 "music-functions.scm"
194 "define-music-properties.scm"
200 "define-context-properties.scm"
201 "translation-functions.scm"
211 "define-markup-commands.scm"
212 "define-grob-properties.scm"
214 "define-grob-interfaces.scm"
215 "define-stencil-commands.scm"
220 "backend-library.scm"
223 ;; must be after everything has been defined
227 (set! type-p-name-alist
229 (,boolean-or-symbol? . "boolean or symbol")
230 (,boolean? . "boolean")
232 (,grob-list? . "list of grobs")
233 (,hash-table? . "hash table")
234 (,input-port? . "input port")
235 (,integer? . "integer")
237 (,ly:context? . "context")
238 (,ly:dimension? . "dimension, in staff space")
239 (,ly:dir? . "direction")
240 (,ly:duration? . "duration")
241 (,ly:grob? . "layout object")
242 (,ly:input-location? . "input location")
243 (,ly:moment? . "moment")
244 (,ly:music? . "music")
245 (,ly:pitch? . "pitch")
246 (,ly:translator? . "translator")
247 (,ly:font-metric? . "font metric")
248 (,markup-list? . "list of markups")
249 (,markup? . "markup")
250 (,ly:music-list? . "list of music")
251 (,number-or-grob? . "number or grob")
252 (,number-or-string? . "number or string")
253 (,number-pair? . "pair of numbers")
254 (,number? . "number")
255 (,output-port? . "output port")
257 (,procedure? . "procedure")
258 (,scheme? . "any type")
259 (,string? . "string")
260 (,symbol? . "symbol")
261 (,vector? . "vector")))
266 (define gc-protect-stat-count 0)
267 (define-public (dump-gc-protects)
268 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
269 (let* ((protects (sort
270 (hash-table->alist (ly:protects))
272 (< (object-address (car a))
273 (object-address (car b))))))
274 (out-file-name (string-append
275 "gcstat-" (number->string gc-protect-stat-count)
277 (outfile (open-file out-file-name "w")))
279 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
282 (lambda (x) (not (symbol? x)))
289 (map object->string (list (object-address x) c x))
295 (define-public (tweak-grob-property grob sym val)
296 (set! (ly:grob-property grob sym) val))
298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299 (define-public (lilypond-main files)
300 "Entry point for LilyPond."
305 (let ((failed (lilypond-all files)))
308 (ly:error (_ "failed files: ~S") (string-join failed))
311 ;; HACK: be sure to exit with single newline
315 (define (no-files-handler)
319 (define-public (lilypond-all files)
321 (handler (lambda (key failed-file)
322 (set! failed (append (list failed-file) failed)))))
323 ;;(handler (lambda (key . arg) (set! failed (append arg failed)))))
324 (for-each (lambda (x) (lilypond-file handler x)) files)
327 (define (lilypond-file handler file-name)
328 (catch 'ly-file-failed
329 (lambda () (ly:parse-file file-name))
330 (lambda (x . args) (handler x file-name)))
335 (use-modules (scm editor))
337 (define (running-from-gui?)
338 (let ((have-tty? (isatty? (current-input-port))))
339 ;; If no TTY and not using safe, assume running from GUI.
341 ((eq? PLATFORM 'windows)
342 ;; This only works for i586-mingw32msvc-gcc -mwindows
343 (not (string-match "standard input"
344 (format #f "~S" (current-input-port)))))
345 ((eq? PLATFORM 'darwin) #f)
349 (define-public (gui-main files)
350 (if (null? files) (gui-no-files-handler))
351 (let* ((base (basename (car files) ".ly"))
352 (log-name (string-append base ".log")))
353 (if (not (running-from-gui?))
354 (ly:message (_ "Redirecting output to ~a...") log-name))
355 (ly:stderr-redirect log-name "w")
356 (ly:message "# -*-compilation-*-")
357 (let ((failed (lilypond-all files)))
361 (ly:stderr-redirect "foo" "r")
362 (system (get-editor-command log-name 0 0))
363 (ly:error (_ "failed files: ~S") (string-join failed))
368 (define (gui-no-files-handler)
369 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
370 ;; FIXME: soft-code, localize
371 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
372 (cmd (get-editor-command welcome-ly 0 0)))
373 (ly:message (_ "Invoking `~a'...") cmd)
377 (or (not (running-from-gui?))
378 (ly:get-option 'safe)
379 (define lilypond-main gui-main))