1 ;;;; lily.scm -- toplevel Scheme stuff
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
9 (define (define-scheme-options)
11 (ly:add-option (car x) (cadr x) (caddr x)))
17 ;; - [subject-]object-object-verb +"ing"
18 ;; - [subject-]-verb-object-object
20 (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
21 (check-internal-types #f "check every property assignment for types")
23 "dump memory debugging statistics")
24 (debug-midi #f "generate human readable MIDI")
25 (delete-intermediate-files #f
26 "delete unusable PostScript files")
27 (dump-signatures #f "dump output signatures of each system")
28 (dump-tweaks #f "dump page layout and tweaks for each score having the tweak-key layout property set.")
30 "load fonts via Ghostscript.")
31 (include-book-title-preview #t "include book-titles in preview images.")
32 (include-eps-fonts #f "Include fonts in separate-system EPS files.")
34 (pad-eps-boxes #f "Pad EPS bounding boxes to guarantee alignment between systems")
36 (gui #f "running from gui; redirect stderr to log file")
39 "relative for simultaneous music works
40 similar to chord syntax")
42 "experimental mechanism for remembering tweaks")
43 (point-and-click #t "use point & click")
44 (paper-size "a4" "the default paper size")
45 (protected-scheme-parsing #t "continue when finding errors in inline
46 scheme are caught in the parser. If off, halt
47 on errors, and print a stack trace.")
48 (profile-property-accesses #f "keep statistics of get_property() calls.")
50 (resolution 101 "resolution for generating bitmaps")
51 (read-file-list #f "Read files to be processed from command line arguments")
53 (safe #f "Run safely")
54 (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
57 "how much verbosity for TTF font embedding?")
59 (show-available-fonts #f
60 "List font names available.")
62 (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
66 ;; need to do this in the beginning. Other parts of the
67 ;; Scheme init depend on these options.
69 (define-scheme-options)
71 (if (defined? 'set-debug-cell-accesses!)
72 (set-debug-cell-accesses! #f))
74 ;(set-debug-cell-accesses! 1000)
76 (use-modules (ice-9 regex)
86 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
88 (define-public (print . args)
89 (apply format (cons (current-output-port) args)))
93 ;;; debugging evaluator is slower. This should
94 ;;; have a more sensible default.
96 (if (ly:get-option 'verbose)
99 (debug-enable 'backtrace)
100 (read-enable 'positions)))
102 (define-public tex-backend?
103 (member (ly:output-backend) '("texstr" "tex")))
105 (define-public parser #f)
107 (define-public (lilypond-version)
109 (map (lambda (x) (if (symbol? x)
116 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT
117 (define-public TEX_STRING_HASHLIMIT 10000000)
121 ;; gettext wrapper for guile < 1.7.2
122 (if (defined? 'gettext)
123 (define-public _ gettext)
124 (define-public _ ly:gettext))
126 (define-public (ly:load x)
127 (let* ((file-name (%search-load-path x)))
128 (if (ly:get-option 'verbose)
129 (ly:progress "[~A" file-name))
131 (ly:error (_ "Can't find ~A" x)))
132 (primitive-load file-name)
133 (if (ly:get-option 'verbose)
137 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
140 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
143 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
147 (define-public PLATFORM
150 (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
153 (let ((platform (string-tokenize
154 (vector-ref (uname) 0) char-set:letter+digit)))
155 (if (null? (cdr platform)) #f
156 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
160 (define native-getcwd getcwd)
162 (if (string-index x #\\)
164 (string-regexp-substitute
166 (string-regexp-substitute "\\\\" "/" x))))
167 ;; FIXME: this prints a warning.
168 (define-public (ly-getcwd)
169 (slashify (native-getcwd))))
170 (else (define-public ly-getcwd getcwd)))
172 (define-public (is-absolute? file-name)
173 (let ((file-name-length (string-length file-name)))
174 (if (= file-name-length 0)
176 (or (eq? (string-ref file-name 0) #\/)
177 (and (eq? PLATFORM 'windows)
178 (> file-name-length 2)
179 (eq? (string-ref file-name 1) #\:)
180 (eq? (string-ref file-name 2) #\/))))))
182 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
184 (define (type-check-list location signature arguments)
185 "Typecheck a list of arguments against a list of type
186 predicates. Print a message at LOCATION if any predicate failed."
188 (define (recursion-helper signature arguments count)
189 (define (helper pred? arg count)
190 (if (not (pred? arg))
196 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
197 count (type-name pred?) arg))
201 (if (null? signature)
203 (and (helper (car signature) (car arguments) count)
204 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
206 (recursion-helper signature arguments 1))
208 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
212 ;;(define-public (output-framework) (write "hello\n"))
214 (define output-tex-module
215 (make-module 1021 (list (resolve-interface '(scm output-tex)))))
216 (define output-ps-module
217 (make-module 1021 (list (resolve-interface '(scm output-ps)))))
219 (define-public (ps-output-expression expr port)
220 (display (eval expr output-ps-module) port))
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;; Safe definitions utility
224 (define safe-objects (list))
226 (define-macro (define-safe-public arglist . body)
227 "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
228 The syntax is the same as `define*-public'."
229 (define (get-symbol arg)
231 (get-symbol (car arg))
233 (let ((safe-symbol (get-symbol arglist)))
235 (define*-public ,arglist
237 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
241 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
244 (ly:set-default-scale (ly:make-scale #(0 2 4 5 7 9 11)))
248 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255 "define-event-classes.scm"
256 "define-music-types.scm"
259 "chord-ignatzek-names.scm"
261 "chord-generic-names.scm"
264 "music-functions.scm"
267 "define-music-properties.scm"
271 "parser-ly-from-scheme.scm"
272 "ly-syntax-constructors.scm"
274 "define-context-properties.scm"
275 "translation-functions.scm"
285 "define-markup-commands.scm"
286 "define-grob-properties.scm"
288 "define-grob-interfaces.scm"
289 "define-stencil-commands.scm"
293 "backend-library.scm"
296 ;; must be after everything has been defined
300 (set! type-p-name-alist
302 (,boolean-or-symbol? . "boolean or symbol")
303 (,boolean? . "boolean")
305 (,grob-list? . "list of grobs")
306 (,hash-table? . "hash table")
307 (,input-port? . "input port")
308 (,integer? . "integer")
310 (,ly:context? . "context")
311 (,ly:dimension? . "dimension, in staff space")
312 (,ly:dir? . "direction")
313 (,ly:duration? . "duration")
314 (,ly:grob? . "layout object")
315 (,ly:input-location? . "input location")
316 (,ly:moment? . "moment")
317 (,ly:music? . "music")
318 (,ly:pitch? . "pitch")
319 (,ly:translator? . "translator")
320 (,ly:font-metric? . "font metric")
321 (,ly:simple-closure? . "simple closure")
322 (,markup-list? . "list of markups")
323 (,markup? . "markup")
324 (,ly:music-list? . "list of music")
325 (,number-or-grob? . "number or grob")
326 (,number-or-string? . "number or string")
327 (,number-pair? . "pair of numbers")
328 (,number? . "number")
329 (,output-port? . "output port")
331 (,procedure? . "procedure")
332 (,scheme? . "any type")
333 (,string? . "string")
334 (,symbol? . "symbol")
335 (,vector? . "vector")))
340 (define gc-protect-stat-count 0)
341 (define-public (dump-gc-protects)
342 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
343 (let* ((protects (sort
344 (hash-table->alist (ly:protects))
346 (< (object-address (car a))
347 (object-address (car b))))))
349 (out-file-name (string-append
350 "gcstat-" (number->string gc-protect-stat-count)
352 (outfile (open-file out-file-name "w")))
354 (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
362 (map object->string (list (object-address x) c x))
368 (not (symbol? (car x))))
372 ; (display (ly:smob-protects))
374 (if (defined? 'gc-live-object-stats)
376 (display "Live object statistics: GC'ing\n")
380 (set! stats (gc-live-object-stats))
381 (display "Dumping live object statistics.\n")
385 (format outfile "~a: ~a\n" (car x) (cdr x)))
386 (sort (gc-live-object-stats)
388 (string<? (car x) (car y)))))))))
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
393 (define-public (lilypond-main files)
394 "Entry point for LilyPond."
396 (define (no-files-handler)
400 (if (ly:get-option 'gui)
406 (let ((failed (lilypond-all files)))
409 (ly:error (_ "failed files: ~S") (string-join failed))
412 ;; HACK: be sure to exit with single newline
416 (define-public (lilypond-all files)
417 (if (ly:get-option 'read-file-list)
420 (> (string-length s) 0))
422 (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
426 (if (ly:get-option 'show-available-fonts)
428 (ly:font-config-display-fonts)
433 (handler (lambda (key failed-file)
434 (set! failed (append (list failed-file) failed)))))
438 (lilypond-file handler x)
439 (ly:clear-anonymous-modules)
440 (if (ly:get-option 'debug-gc)
446 (define (lilypond-file handler file-name)
447 (catch 'ly-file-failed
448 (lambda () (ly:parse-file file-name))
449 (lambda (x . args) (handler x file-name))))
451 (use-modules (scm editor))
453 (define-public (gui-main files)
455 (gui-no-files-handler))
456 (let* ((base (basename (car files) ".ly"))
457 (log-name (string-append base ".log")))
458 (if (not (ly:get-option 'gui))
459 (ly:message (_ "Redirecting output to ~a...") log-name))
460 (ly:stderr-redirect log-name "w")
461 (ly:message "# -*-compilation-*-")
463 (let ((failed (lilypond-all files)))
467 (ly:stderr-redirect "foo" "r")
468 (system (get-editor-command log-name 0 0 0))
469 (ly:error (_ "failed files: ~S") (string-join failed))
474 (define (gui-no-files-handler)
475 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
476 ;; FIXME: soft-code, localize
477 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
478 (cmd (get-editor-command welcome-ly 0 0 0)))
479 (ly:message (_ "Invoking `~a'...") cmd)