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