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")
21 (resolution 90 "resolution for generating bitmaps")
22 (preview-include-book-title #t "include book-titles in preview images.")
24 "load fonts via Ghostscript.")
25 (delete-intermediate-files #f
26 "delete unusable PostScript files")
27 (verbose #f "value for the --verbose flag")
29 "how much verbosity for TTF font embedding?")
31 "dump GC protection info")))
34 (if (defined? 'set-debug-cell-accesses!)
35 (set-debug-cell-accesses! #f))
37 ;(set-debug-cell-accesses! 1000)
39 (use-modules (ice-9 regex)
49 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
51 (define-public (print . args)
52 (apply format (cons (current-output-port) args)))
56 ;;; debugging evaluator is slower. This should
57 ;;; have a more sensible default.
59 (if (ly:get-option 'verbose)
62 (debug-enable 'backtrace)
63 (read-enable 'positions)))
66 (define-public tex-backend?
67 (member (ly:output-backend) '("texstr" "tex")))
69 (define-public parser #f)
71 (define-public (lilypond-version)
73 (map (lambda (x) (if (symbol? x)
81 ;; cpp hack to get useful error message
82 (define ifdef "First run this through cpp.")
83 (define ifndef "First run this through cpp.")
85 ;; gettext wrapper for guile < 1.7.2
86 (if (defined? 'gettext)
87 (define-public _ gettext)
88 (define-public _ ly:gettext))
90 (define-public (ly:load x)
91 (let* ((file-name (%search-load-path x)))
92 (if (ly:get-option 'verbose)
93 (ly:progress "[~A" file-name))
94 (primitive-load file-name)
95 (if (ly:get-option 'verbose)
98 (define-public TEX_STRING_HASHLIMIT 10000000)
101 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
104 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
107 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
111 (define-public PLATFORM
114 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
118 (define native-getcwd getcwd)
120 (if (string-index x #\\)
122 (string-regexp-substitute
124 (string-regexp-substitute "\\\\" "/" x))))
125 ;; FIXME: this prints a warning.
126 (define-public (ly-getcwd)
127 (slashify (native-getcwd))))
128 (else (define-public ly-getcwd getcwd)))
130 (define-public (is-absolute? file-name)
131 (let ((file-name-length (string-length file-name)))
132 (if (= file-name-length 0)
134 (or (eq? (string-ref file-name 0) #\/)
135 (and (eq? PLATFORM 'windows)
136 (> file-name-length 2)
137 (eq? (string-ref file-name 1) #\:)
138 (eq? (string-ref file-name 2) #\/))))))
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 (define (type-check-list location signature arguments)
143 "Typecheck a list of arguments against a list of type
144 predicates. Print a message at LOCATION if any predicate failed."
145 (define (recursion-helper signature arguments count)
146 (define (helper pred? arg count)
147 (if (not (pred? arg))
153 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
154 count (type-name pred?) arg))
158 (if (null? signature)
160 (and (helper (car signature) (car arguments) count)
161 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
162 (recursion-helper signature arguments 1))
164 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
168 ;;(define-public (output-framework) (write "hello\n"))
170 (define output-tex-module
171 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
172 (define output-ps-module
173 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
175 (define-public (ps-output-expression expr port)
176 (display (eval expr output-ps-module) port))
178 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
179 ;; Safe definitions utility
180 (define safe-objects (list))
182 (define-macro (define-safe-public arglist . body)
183 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
184 The syntax is the same as `define*-public'."
185 (define (get-symbol arg)
187 (get-symbol (car arg))
189 (let ((safe-symbol (get-symbol arglist)))
191 (define*-public ,arglist
193 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
205 "define-music-types.scm"
208 "chord-ignatzek-names.scm"
210 "chord-generic-names.scm"
214 "music-functions.scm"
216 "define-music-properties.scm"
222 "define-context-properties.scm"
223 "translation-functions.scm"
233 "define-markup-commands.scm"
234 "define-grob-properties.scm"
236 "define-grob-interfaces.scm"
237 "define-stencil-commands.scm"
242 "backend-library.scm"
245 ;; must be after everything has been defined
249 (set! type-p-name-alist
251 (,boolean-or-symbol? . "boolean or symbol")
252 (,boolean? . "boolean")
254 (,grob-list? . "list of grobs")
255 (,hash-table? . "hash table")
256 (,input-port? . "input port")
257 (,integer? . "integer")
259 (,ly:context? . "context")
260 (,ly:dimension? . "dimension, in staff space")
261 (,ly:dir? . "direction")
262 (,ly:duration? . "duration")
263 (,ly:grob? . "layout object")
264 (,ly:input-location? . "input location")
265 (,ly:moment? . "moment")
266 (,ly:music? . "music")
267 (,ly:pitch? . "pitch")
268 (,ly:translator? . "translator")
269 (,ly:font-metric? . "font metric")
270 (,markup-list? . "list of markups")
271 (,markup? . "markup")
272 (,ly:music-list? . "list of music")
273 (,number-or-grob? . "number or grob")
274 (,number-or-string? . "number or string")
275 (,number-pair? . "pair of numbers")
276 (,number? . "number")
277 (,output-port? . "output port")
279 (,procedure? . "procedure")
280 (,scheme? . "any type")
281 (,string? . "string")
282 (,symbol? . "symbol")
283 (,vector? . "vector")))
288 (define gc-protect-stat-count 0)
289 (define-public (dump-gc-protects)
290 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
291 (let* ((protects (sort
292 (hash-table->alist (ly:protects))
294 (< (object-address (car a))
295 (object-address (car b))))))
297 (out-file-name (string-append
298 "gcstat-" (number->string gc-protect-stat-count)
300 (outfile (open-file out-file-name "w")))
302 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
305 (lambda (x) (not (symbol? x)))
312 (map object->string (list (object-address x) c x))
318 (if (defined? 'gc-live-object-stats)
322 (stats (gc-live-object-stats))
327 (format outfile "~a: ~a\n" (car x) (cdr x)))
328 (sort (gc-live-object-stats)
330 (string<? (car x) (car y)))))
335 (define-public (tweak-grob-property grob sym val)
336 (set! (ly:grob-property grob sym) val))
338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
339 (define-public (lilypond-main files)
340 "Entry point for LilyPond."
345 (let ((failed (lilypond-all files)))
348 (ly:error (_ "failed files: ~S") (string-join failed))
351 ;; HACK: be sure to exit with single newline
355 (define (no-files-handler)
359 (define-public (lilypond-all files)
361 (handler (lambda (key failed-file)
362 (set! failed (append (list failed-file) failed)))))
363 ;;(handler (lambda (key . arg) (set! failed (append arg failed)))))
364 (for-each (lambda (x) (lilypond-file handler x)) files)
367 (define (lilypond-file handler file-name)
368 (catch 'ly-file-failed
369 (lambda () (ly:parse-file file-name))
370 (lambda (x . args) (handler x file-name)))
372 (if (ly:get-option 'debug-gc)
375 (use-modules (scm editor))
377 (define-public (running-from-gui?)
378 (let ((have-tty? (isatty? (current-input-port))))
379 ;; If no TTY and not using safe, assume running from GUI.
381 ((eq? PLATFORM 'windows)
382 ;; This only works for i586-mingw32msvc-gcc -mwindows
383 (not (string-match "standard input"
384 (format #f "~S" (current-input-port)))))
385 ((eq? PLATFORM 'darwin) #f)
389 (define-public (gui-main files)
391 (gui-no-files-handler))
392 (let* ((base (basename (car files) ".ly"))
393 (log-name (string-append base ".log")))
394 (if (not (running-from-gui?))
395 (ly:message (_ "Redirecting output to ~a...") log-name))
396 (ly:stderr-redirect log-name "w")
397 (ly:message "# -*-compilation-*-")
398 (let ((failed (lilypond-all files)))
402 (ly:stderr-redirect "foo" "r")
403 (system (get-editor-command log-name 0 0 0))
404 (ly:error (_ "failed files: ~S") (string-join failed))
409 (define (gui-no-files-handler)
410 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
411 ;; FIXME: soft-code, localize
412 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
413 (cmd (get-editor-command welcome-ly 0 0 0)))
414 (ly:message (_ "Invoking `~a'...") cmd)
418 (or (not (running-from-gui?))
419 (ly:get-option 'safe)
420 (define lilypond-main gui-main))