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