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