]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
* scm/lily.scm (lilypond-all): option read-file-list: substitute
[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               (read-file-list #f "Read files to be processed from command line arguments")
46               )))
47
48
49 ;; need to do this in the beginning. Other parts of the
50 ;; Scheme init depend on these options.
51 ;;
52 (define-scheme-options)
53
54 (if (defined? 'set-debug-cell-accesses!)
55     (set-debug-cell-accesses! #f))
56
57 ;(set-debug-cell-accesses! 1000)
58
59 (use-modules (ice-9 regex)
60              (ice-9 safe)
61              (ice-9 optargs)
62              (oop goops)
63              (srfi srfi-1)
64              (srfi srfi-13)
65              (srfi srfi-14))
66
67
68 ;; my display
69 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
70
71 (define-public (print . args)
72   (apply format (cons (current-output-port) args)))
73
74
75 ;;; General settings
76 ;;; debugging evaluator is slower.  This should
77 ;;; have a more sensible default.
78
79 (if (ly:get-option 'verbose)
80     (begin
81       (debug-enable 'debug)
82       (debug-enable 'backtrace)
83       (read-enable 'positions)))
84
85 (define-public tex-backend?
86   (member (ly:output-backend) '("texstr" "tex")))
87
88 (define-public parser #f)
89
90 (define-public (lilypond-version)
91   (string-join
92    (map (lambda (x) (if (symbol? x)
93                         (symbol->string x)
94                         (number->string x)))
95         (ly:version))
96    "."))
97
98
99 ;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT 
100 (define-public TEX_STRING_HASHLIMIT 10000000)
101
102
103
104 ;; gettext wrapper for guile < 1.7.2
105 (if (defined? 'gettext)
106     (define-public _ gettext)
107     (define-public _ ly:gettext))
108
109 (define-public (ly:load x)
110   (let* ((file-name (%search-load-path x)))
111     (if (ly:get-option 'verbose)
112         (ly:progress "[~A" file-name))
113     (if (not file-name)
114         (ly:error (_ "Can't find ~A" x)))
115     (primitive-load file-name)
116     (if (ly:get-option 'verbose)
117         (ly:progress "]"))))
118
119 ;; Cygwin
120 ;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
121 ;;
122 ;; Debian
123 ;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
124 ;;
125 ;; Mingw
126 ;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
127 ;;
128
129 ;; ugh, code dup.
130 (define-public PLATFORM
131   (string->symbol
132    (string-downcase
133     (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
134
135 (define-public DOS
136   (let ((platform (string-tokenize
137                    (vector-ref (uname) 0) char-set:letter+digit)))
138     (if (null? (cdr platform)) #f
139         (member (string-downcase (cadr platform)) '("95" "98" "me")))))
140
141 (case PLATFORM
142   ((windows)
143    (define native-getcwd getcwd)
144    (define (slashify x)
145      (if (string-index x #\\)
146          x
147          (string-regexp-substitute
148           "//*" "/"
149           (string-regexp-substitute "\\\\" "/" x))))
150    ;; FIXME: this prints a warning.
151   (define-public (ly-getcwd)
152      (slashify (native-getcwd))))
153   (else (define-public ly-getcwd getcwd)))
154
155 (define-public (is-absolute? file-name)
156   (let ((file-name-length (string-length file-name)))
157     (if (= file-name-length 0)
158         #f
159         (or (eq? (string-ref file-name 0) #\/)
160             (and (eq? PLATFORM 'windows)
161                  (> file-name-length 2)
162                  (eq? (string-ref file-name 1) #\:)
163                  (eq? (string-ref file-name 2) #\/))))))
164
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166
167 (define (type-check-list location signature arguments)
168   "Typecheck a list of arguments against a list of type
169 predicates. Print a message at LOCATION if any predicate failed."
170
171   (define (recursion-helper signature arguments count) 
172     (define (helper pred? arg count) 
173       (if (not (pred? arg))
174
175           (begin
176             (ly:input-message
177              location
178              (format
179               #f (_ "wrong type for argument ~a.  Expecting ~a, found ~s")
180               count (type-name pred?) arg))
181             #f)
182           #t))
183
184     (if (null? signature)
185         #t
186         (and (helper (car signature) (car arguments) count)
187              (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
188
189   (recursion-helper signature arguments 1))
190
191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
192 ;;  output
193
194
195 ;;(define-public (output-framework) (write "hello\n"))
196
197 (define output-tex-module
198   (make-module 1021 (list (resolve-interface '(scm output-tex)))))
199 (define output-ps-module
200   (make-module 1021 (list (resolve-interface '(scm output-ps)))))
201
202 (define-public (ps-output-expression expr port)
203   (display (eval expr output-ps-module) port))
204
205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
206 ;; Safe definitions utility
207 (define safe-objects (list))
208
209 (define-macro (define-safe-public arglist . body)
210   "Define a variable, export it, and mark it as safe, ie usable in LilyPond safe mode.
211 The syntax is the same as `define*-public'."
212   (define (get-symbol arg)
213     (if (pair? arg)
214         (get-symbol (car arg))
215         arg))
216   (let ((safe-symbol (get-symbol arglist)))
217     `(begin
218        (define*-public ,arglist
219          ,@body)
220        (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
221                                 safe-objects))
222        ,safe-symbol)))
223
224
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226 ;; other files.
227
228 (for-each ly:load
229           ;; load-from-path
230           '("lily-library.scm"
231             "file-cache.scm"
232             "define-event-classes.scm"
233             "define-music-types.scm"
234             "output-lib.scm"
235             "c++.scm"
236             "chord-ignatzek-names.scm"
237             "chord-entry.scm"
238             "chord-generic-names.scm"
239             "stencil.scm"
240             "markup.scm"
241             "music-functions.scm"
242             "part-combiner.scm"
243             "autochange.scm"
244             "define-music-properties.scm"
245             "auto-beam.scm"
246             "chord-name.scm"
247
248             "parser-ly-from-scheme.scm"
249             "ly-syntax-constructors.scm"
250             
251             "define-context-properties.scm"
252             "translation-functions.scm"
253             "script.scm"
254             "midi.scm"
255             "layout-beam.scm"
256             "parser-clef.scm"
257             "layout-slur.scm"
258             "font.scm"
259             "encoding.scm"
260             
261             "fret-diagrams.scm"
262             "define-markup-commands.scm"
263             "define-grob-properties.scm"
264             "define-grobs.scm"
265             "define-grob-interfaces.scm"
266             "define-stencil-commands.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   (if (ly:get-option 'read-file-list)
395       (set! files
396             (filter (lambda (s)
397                       (> (string-length s) 0))
398                     (apply append
399                            (map (lambda (f) (string-split (ly:gulp-file f) #\nl))
400                                 files)))
401             ))
402
403   (if (ly:get-option 'show-available-fonts)
404       (begin
405         (ly:font-config-display-fonts)
406         (exit 0)
407         ))
408   
409   (let* ((failed '())
410          (handler (lambda (key failed-file)
411                     (set! failed (append (list failed-file) failed)))))
412
413     (for-each
414      (lambda (x)
415        (lilypond-file handler x)
416        (ly:clear-anonymous-modules)
417        (if (ly:get-option 'debug-gc)
418            (dump-gc-protects)))
419      
420      files)
421     failed))
422
423 (define (lilypond-file handler file-name)
424   (catch 'ly-file-failed
425          (lambda () (ly:parse-file file-name))
426          (lambda (x . args) (handler x file-name))))
427
428 (use-modules (scm editor))
429
430 (define-public (gui-main files)
431   (if (null? files)
432       (gui-no-files-handler))
433   (let* ((base (basename (car files) ".ly"))
434          (log-name (string-append base ".log")))
435     (if (not (ly:get-option 'gui))
436         (ly:message (_ "Redirecting output to ~a...") log-name))
437     (ly:stderr-redirect log-name "w")
438     (ly:message "# -*-compilation-*-")
439     
440     (let ((failed (lilypond-all files)))
441       (if (pair? failed)
442           (begin
443             ;; ugh
444             (ly:stderr-redirect "foo" "r")
445             (system (get-editor-command log-name 0 0 0))
446             (ly:error (_ "failed files: ~S") (string-join failed))
447             ;; not reached?
448             (exit 1))
449           (exit 0)))))
450
451 (define (gui-no-files-handler)
452   (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
453          ;; FIXME: soft-code, localize
454          (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
455          (cmd (get-editor-command welcome-ly 0 0 0)))
456     (ly:message (_ "Invoking `~a'...") cmd)
457     (system cmd)
458     (exit 1)))