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