]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
* scm/stencil.scm (annotate-y-interval): robustify: print nan/inf
[lilypond.git] / scm / lily.scm
1 ;;;; lily.scm -- toplevel Scheme stuff
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8
9 (define (define-scheme-options)
10   (for-each (lambda (x)
11               (ly:add-option (car x) (cadr x) (caddr x)))
12           
13             `((point-and-click #t "use point & click")
14               (paper-size "a4" "the default paper size")
15               (midi-debug #f "generate human readable MIDI")
16               (internal-type-checking #f "check every property assignment for types")
17               (parse-protect #t    "continue when finding errors in inline
18 scheme are caught in the parser. If off, halt 
19 on errors, and print a stack trace.")
20               (profile-property-accesses #f "keep statistics of get_property() calls.")
21               (old-relative #f
22                             "relative for simultaneous music works
23 similar to chord syntax")
24               (object-keys #f
25                            "experimental mechanism for remembering tweaks")
26               (resolution 101 "resolution for generating bitmaps")
27               (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
28               (book-title-preview #t "include book-titles in preview images.")
29               (eps-font-include #f "Include fonts in separate-system EPS files.")
30               (gs-font-load #f
31                             "load fonts via Ghostscript.")
32               (gui #f "running from gui; redirect stderr to log file")
33               (delete-intermediate-files #f
34                                          "delete unusable PostScript files")
35               (safe #f "Run safely")
36               (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
37               (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
38               (ttf-verbosity 0
39                            "how much verbosity for TTF font embedding?")
40               (debug-gc #f
41                         "dump GC protection info")
42               (show-available-fonts #f
43                                     "List  font names available.")
44               )
45             ))
46
47 ;; FIXME: stray statement
48 (define-scheme-options)
49
50 (if (defined? 'set-debug-cell-accesses!)
51     (set-debug-cell-accesses! #f))
52
53 ;(set-debug-cell-accesses! 1000)
54
55 (use-modules (ice-9 regex)
56              (ice-9 safe)
57              (ice-9 optargs)
58              (oop goops)
59              (srfi srfi-1)
60              (srfi srfi-13)
61              (srfi srfi-14))
62
63
64 ;; my display
65 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
66
67 (define-public (print . args)
68   (apply format (cons (current-output-port) args)))
69
70
71 ;;; General settings
72 ;;; debugging evaluator is slower.  This should
73 ;;; have a more sensible default.
74
75 (if (ly:get-option 'verbose)
76     (begin
77       (debug-enable 'debug)
78       (debug-enable 'backtrace)
79       (read-enable 'positions)))
80
81 (define-public tex-backend?
82   (member (ly:output-backend) '("texstr" "tex")))
83
84 (define-public parser #f)
85
86 (define-public (lilypond-version)
87   (string-join
88    (map (lambda (x) (if (symbol? x)
89                         (symbol->string x)
90                         (number->string x)))
91         (ly:version))
92    "."))
93
94
95
96
97 ;; gettext wrapper for guile < 1.7.2
98 (if (defined? 'gettext)
99     (define-public _ gettext)
100     (define-public _ ly:gettext))
101
102 (define-public (ly:load x)
103   (let* ((file-name (%search-load-path x)))
104     (if (ly:get-option 'verbose)
105         (ly:progress "[~A" file-name))
106     (primitive-load file-name)
107     (if (ly:get-option 'verbose)
108         (ly:progress "]"))))
109
110 ;; Cygwin
111 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
112 ;;
113 ;; Debian
114 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
115 ;;
116 ;; Mingw
117 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
118 ;;
119
120 ;; ugh, code dup.
121 (define-public PLATFORM
122   (string->symbol
123    (string-downcase
124     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
125
126 (define-public DOS
127   (let ((platform (string-tokenize
128                    (vector-ref (uname) 0) char-set:letter+digit)))
129     (if (null? (cdr platform)) #f
130         (member (string-downcase (cadr platform)) '("95" "98" "me")))))
131
132 (case PLATFORM
133   ((windows)
134    (define native-getcwd getcwd)
135    (define (slashify x)
136      (if (string-index x #\\)
137          x
138          (string-regexp-substitute
139           "//*" "/"
140           (string-regexp-substitute "\\\\" "/" x))))
141    ;; FIXME: this prints a warning.
142   (define-public (ly-getcwd)
143      (slashify (native-getcwd))))
144   (else (define-public ly-getcwd getcwd)))
145
146 (define-public (is-absolute? file-name)
147   (let ((file-name-length (string-length file-name)))
148     (if (= file-name-length 0)
149         #f
150         (or (eq? (string-ref file-name 0) #\/)
151             (and (eq? PLATFORM 'windows)
152                  (> file-name-length 2)
153                  (eq? (string-ref file-name 1) #\:)
154                  (eq? (string-ref file-name 2) #\/))))))
155
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157
158 (define (type-check-list location signature arguments)
159   "Typecheck a list of arguments against a list of type
160 predicates. Print a message at LOCATION if any predicate failed."
161   (define (recursion-helper signature arguments count) 
162     (define (helper pred? arg count) 
163       (if (not (pred? arg))
164
165           (begin
166             (ly:input-message
167              location
168              (format
169               #f (_ "wrong type for argument ~a.  Expecting ~a, found ~s")
170               count (type-name pred?) arg))
171             #f)
172           #t))
173
174     (if (null? signature)
175         #t
176         (and (helper (car signature) (car arguments) count)
177              (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
178   (recursion-helper signature arguments 1))
179
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 ;;  output
182
183
184 ;;(define-public (output-framework) (write "hello\n"))
185
186 (define output-tex-module
187   (make-module 1021 (list (resolve-interface '(scm output-tex)))))
188 (define output-ps-module
189   (make-module 1021 (list (resolve-interface '(scm output-ps)))))
190
191 (define-public (ps-output-expression expr port)
192   (display (eval expr output-ps-module) port))
193
194 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195 ;; Safe definitions utility
196 (define safe-objects (list))
197
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)
202     (if (pair? arg)
203         (get-symbol (car arg))
204         arg))
205   (let ((safe-symbol (get-symbol arglist)))
206     `(begin
207        (define*-public ,arglist
208          ,@body)
209        (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
210                                 safe-objects))
211        ,safe-symbol)))
212
213
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
215 ;; other files.
216
217 (for-each ly:load
218           ;; load-from-path
219           '("lily-library.scm"
220             "file-cache.scm"
221             "define-music-types.scm"
222             "output-lib.scm"
223             "c++.scm"
224             "chord-ignatzek-names.scm"
225             "chord-entry.scm"
226             "chord-generic-names.scm"
227             "stencil.scm"
228             "markup.scm"
229             "music-functions.scm"
230             "part-combiner.scm"
231             "autochange.scm"
232             "define-music-properties.scm"
233             "auto-beam.scm"
234             "chord-name.scm"
235
236             "parser-ly-from-scheme.scm"
237             
238             "define-context-properties.scm"
239             "translation-functions.scm"
240             "script.scm"
241             "midi.scm"
242             "layout-beam.scm"
243             "parser-clef.scm"
244             "layout-slur.scm"
245             "font.scm"
246             "encoding.scm"
247             
248             "fret-diagrams.scm"
249             "define-markup-commands.scm"
250             "define-grob-properties.scm"
251             "define-grobs.scm"
252             "define-grob-interfaces.scm"
253             "define-stencil-commands.scm"
254             "layout-page-layout.scm"
255             "titling.scm"
256             
257             "paper.scm"
258             "backend-library.scm"
259             "x11-color.scm"
260
261             ;; must be after everything has been defined
262             "safe-lily.scm"))
263
264
265 (set! type-p-name-alist
266       `(
267         (,boolean-or-symbol? . "boolean or symbol")
268         (,boolean? . "boolean")
269         (,char? . "char")
270         (,grob-list? . "list of grobs")
271         (,hash-table? . "hash table")
272         (,input-port? . "input port")
273         (,integer? . "integer")
274         (,list? . "list")
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         (,ly:simple-closure? . "simple closure")
287         (,markup-list? . "list of markups")
288         (,markup? . "markup")
289         (,ly:music-list? . "list of music")
290         (,number-or-grob? . "number or grob")
291         (,number-or-string? . "number or string")
292         (,number-pair? . "pair of numbers")
293         (,number? . "number")
294         (,output-port? . "output port")   
295         (,pair? . "pair")
296         (,procedure? . "procedure") 
297         (,scheme? . "any type")
298         (,string? . "string")
299         (,symbol? . "symbol")
300         (,vector? . "vector")))
301
302
303 ;; debug mem leaks
304
305 (define gc-protect-stat-count 0)
306 (define-public (dump-gc-protects)
307   (set! gc-protect-stat-count (1+ gc-protect-stat-count))
308   (let* ((protects (sort
309                     (hash-table->alist (ly:protects))
310                     (lambda (a b)
311                       (< (object-address (car a))
312                          (object-address (car b))))))
313
314          (out-file-name (string-append
315                          "gcstat-" (number->string gc-protect-stat-count)
316                          ".scm"))
317          (outfile    (open-file  out-file-name  "w")))
318
319     (display (format "Dumping gc protected objs to ~a...\n" out-file-name))
320     (display
321      (map (lambda (y)
322             (let ((x (car y))
323                   (c (cdr y)))
324               
325               (string-append
326                (string-join
327                 (map object->string (list (object-address x) c x))
328                 " ")
329                "\n")))
330
331           (filter
332            (lambda (x)
333              (not (symbol? (car x))))
334            protects))
335      outfile)
336
337 ;    (display (ly:smob-protects))
338     (newline outfile)
339     (if (defined? 'gc-live-object-stats)
340         (let* ((stats #f))
341           (display "Live object statistics: GC'ing\n")
342           (gc)
343           (gc)
344           
345           (set! stats (gc-live-object-stats))
346           (display "Dumping live object statistics.\n")
347           
348           (for-each
349            (lambda (x)
350              (format outfile "~a: ~a\n" (car x) (cdr x)))
351            (sort (gc-live-object-stats)
352                  (lambda (x y)
353                    (string<? (car x) (car y)))))))))
354
355 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
356
357
358 (define-public (lilypond-main files)
359   "Entry point for LilyPond."
360   
361   (define (no-files-handler)
362     (ly:usage)
363     (exit 2))
364
365   (if (ly:get-option 'gui)
366       (gui-main files))
367
368   (if (null? files)
369       (no-files-handler))
370
371   (let ((failed (lilypond-all files)))
372     (if (pair? failed)
373         (begin
374           (ly:error (_ "failed files: ~S") (string-join failed))
375           (exit 1))
376         (begin
377           ;; HACK: be sure to exit with single newline
378           (ly:message "")
379           (exit 0)))))
380
381 (define-public (lilypond-all files)
382
383   (if (ly:get-option 'show-available-fonts)
384       (begin
385         (ly:font-config-display-fonts)
386         (exit 0)
387         ))
388   
389   (let* ((failed '())
390          (handler (lambda (key failed-file)
391                     (set! failed (append (list failed-file) failed)))))
392
393     (for-each
394      (lambda (x)
395        (lilypond-file handler x)
396        (ly:clear-anonymous-modules)
397        (if (ly:get-option 'debug-gc)
398            (dump-gc-protects)))
399      
400      files)
401     failed))
402
403 (define (lilypond-file handler file-name)
404   (catch 'ly-file-failed
405          (lambda () (ly:parse-file file-name))
406          (lambda (x . args) (handler x file-name))))
407
408 (use-modules (scm editor))
409
410 (define-public (gui-main files)
411   (if (null? files)
412       (gui-no-files-handler))
413   (let* ((base (basename (car files) ".ly"))
414          (log-name (string-append base ".log")))
415     (if (not (ly:get-option 'gui))
416         (ly:message (_ "Redirecting output to ~a...") log-name))
417     (ly:stderr-redirect log-name "w")
418     (ly:message "# -*-compilation-*-")
419     
420     (let ((failed (lilypond-all files)))
421       (if (pair? failed)
422           (begin
423             ;; ugh
424             (ly:stderr-redirect "foo" "r")
425             (system (get-editor-command log-name 0 0 0))
426             (ly:error (_ "failed files: ~S") (string-join failed))
427             ;; not reached?
428             (exit 1))
429           (exit 0)))))
430
431 (define (gui-no-files-handler)
432   (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
433          ;; FIXME: soft-code, localize
434          (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
435          (cmd (get-editor-command welcome-ly 0 0 0)))
436     (ly:message (_ "Invoking `~a'...") cmd)
437     (system cmd)
438     (exit 1)))