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