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