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