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