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