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