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