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