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