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