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