]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
($(outdir)/%.pdf): add DVIPS_FLAGS. This will
[lilypond.git] / scm / lily.scm
1 ;;;; lily.scm -- implement Scheme output routines for TeX and PostScript
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c)  1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8 ;;; Library functions
9
10
11 (use-modules (ice-9 regex)
12              (ice-9 safe)
13              (oop goops)
14              (srfi srfi-1)  ; lists
15              (srfi srfi-13)) ; strings
16
17 (define-public safe-module (make-safe-module))
18
19 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
20
21 ;;; General settings
22 ;;; debugging evaluator is slower.  This should
23 ;;; have a more sensible default.
24
25
26 (if (ly:get-option 'verbose)
27     (begin
28       (debug-enable 'debug)
29       (debug-enable 'backtrace)
30       (read-enable 'positions) ))
31
32
33 (define-public (line-column-location line col file)
34   "Print an input location, including column number ."
35   (string-append (number->string line) ":"
36                  (number->string col) " " file)
37   )
38
39 (define-public (line-location line col file)
40   "Print an input location, without column number ."
41   (string-append (number->string line) " " file)
42   )
43
44 (define-public point-and-click #f)
45
46 (define-public (lilypond-version)
47   (string-join
48    (map (lambda (x) (if (symbol? x)
49                         (symbol->string x)
50                         (number->string x)))
51                 (ly:version))
52    "."))
53
54
55
56 ;; cpp hack to get useful error message
57 (define ifdef "First run this through cpp.")
58 (define ifndef "First run this through cpp.")
59
60
61
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63
64 (define-public X 0)
65 (define-public Y 1)
66 (define-public START -1)
67 (define-public STOP 1)
68 (define-public LEFT -1)
69 (define-public RIGHT 1)
70 (define-public UP 1)
71 (define-public DOWN -1)
72 (define-public CENTER 0)
73
74 (define-public DOUBLE-FLAT -4)
75 (define-public THREE-Q-FLAT -3)
76 (define-public FLAT -2)
77 (define-public SEMI-FLAT -1)
78 (define-public NATURAL 0)
79 (define-public SEMI-SHARP 1)
80 (define-public SHARP 2)
81 (define-public THREE-Q-SHARP 3)
82 (define-public DOUBLE-SHARP 4)
83 (define-public SEMI-TONE 2)
84
85 (define-public ZERO-MOMENT (ly:make-moment 0 1)) 
86
87 (define-public (moment-min a b)
88   (if (ly:moment<? a b) a b))
89
90 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 ;; lily specific variables.
92 (define-public default-script-alist '())
93
94 (define-public security-paranoia #f)
95
96 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
97 ;;; Unassorted utility functions.
98
99
100 ;;;;;;;;;;;;;;;;
101 ; alist
102 (define (uniqued-alist  alist acc)
103   (if (null? alist) acc
104       (if (assoc (caar alist) acc)
105           (uniqued-alist (cdr alist) acc)
106           (uniqued-alist (cdr alist) (cons (car alist) acc)))))
107
108
109 (define (assoc-get key alist)
110   "Return value if KEY in ALIST, else #f."
111   (let ((entry (assoc key alist)))
112     (if entry (cdr entry) #f)))
113   
114 (define (assoc-get-default key alist default)
115   "Return value if KEY in ALIST, else DEFAULT."
116   (let ((entry (assoc key alist)))
117     (if entry (cdr entry) default)))
118
119
120 (define-public (uniqued-alist  alist acc)
121   (if (null? alist) acc
122       (if (assoc (caar alist) acc)
123           (uniqued-alist (cdr alist) acc)
124           (uniqued-alist (cdr alist) (cons (car alist) acc)))))
125
126 (define-public (alist<? x y)
127   (string<? (symbol->string (car x))
128             (symbol->string (car y))))
129
130
131
132 (define (chain-assoc x alist-list)
133   (if (null? alist-list)
134       #f
135       (let* ((handle (assoc x (car alist-list))))
136         (if (pair? handle)
137             handle
138             (chain-assoc x (cdr alist-list))))))
139
140 (define (chain-assoc-get x alist-list default)
141   (if (null? alist-list)
142       default
143       (let* ((handle (assoc x (car alist-list))))
144         (if (pair? handle)
145             (cdr handle)
146             (chain-assoc-get x (cdr alist-list) default)))))
147
148
149 (define (map-alist-vals func list)
150   "map FUNC over the vals of  LIST, leaving the keys."
151   (if (null?  list)
152       '()
153       (cons (cons  (caar list) (func (cdar list)))
154             (map-alist-vals func (cdr list)))
155       ))
156
157 (define (map-alist-keys func list)
158   "map FUNC over the keys of an alist LIST, leaving the vals. "
159   (if (null?  list)
160       '()
161       (cons (cons (func (caar list)) (cdar list))
162             (map-alist-keys func (cdr list)))
163       ))
164  
165 ;;;;;;;;;;;;;;;;
166 ;; hash
167
168
169
170 (if (not (defined? 'hash-table?))       ; guile 1.6 compat
171     (begin
172       (define hash-table? vector?)
173
174       (define-public (hash-table->alist t)
175         "Convert table t to list"
176         (apply append
177                (vector->list t)
178                )))
179
180     ;; native hashtabs.
181     (begin
182       (define-public (hash-table->alist t)
183
184         (hash-fold (lambda (k v acc) (acons  k v  acc))
185                    '() t)
186         )
187       ))
188
189 ;; todo: code dup with C++. 
190 (define-public (alist->hash-table l)
191   "Convert alist to table"
192   (let
193       ((m (make-hash-table (length l))))
194
195     (map (lambda (k-v)
196            (hashq-set! m (car k-v) (cdr k-v)))
197          l)
198
199     m))
200        
201
202
203 ;;;;;;;;;;;;;;;;
204 ; list
205
206 (define (flatten-list lst)
207   "Unnest LST" 
208   (if (null? lst)
209       '()
210       (if (pair? (car lst))
211           (append (flatten-list (car lst)) (flatten-list  (cdr lst)))
212           (cons (car lst) (flatten-list (cdr lst))))
213   ))
214
215 (define (list-minus a b)
216   "Return list of elements in A that are not in B."
217   (lset-difference eq? a b))
218
219
220 ;; TODO: use the srfi-1 partition function.
221 (define-public (uniq-list l)
222   
223   "Uniq LIST, assuming that it is sorted"
224   (define (helper acc l) 
225     (if (null? l)
226         acc
227         (if (null? (cdr l))
228             (cons (car l) acc)
229             (if (equal? (car l) (cadr l))
230                 (helper acc (cdr l))
231                 (helper (cons (car l) acc)  (cdr l)))
232             )))
233   (reverse! (helper '() l) '()))
234
235
236 (define (split-at-predicate predicate l)
237  "Split L = (a_1 a_2 ... a_k b_1 ... b_k)
238 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k) 
239 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
240 L1 is copied, L2 not.
241
242 (split-at-predicate (lambda (x y) (= (- y x) 2))  '(1 3 5 9 11) (cons '() '()))"
243 ;; "
244
245 ;; KUT EMACS MODE.
246
247   (define (inner-split predicate l acc)
248   (cond
249    ((null? l) acc)
250    ((null? (cdr l))
251     (set-car! acc (cons (car l) (car acc)))
252     acc)
253    ((predicate (car l) (cadr l))
254     (set-car! acc (cons (car l) (car acc)))
255     (inner-split predicate (cdr l) acc))
256    (else
257     (set-car! acc (cons (car l) (car acc)))
258     (set-cdr! acc (cdr l))
259     acc)
260
261   ))
262  (let*
263     ((c (cons '() '()))
264      )
265   (inner-split predicate l  c)
266   (set-car! c (reverse! (car c))) 
267   c)
268 )
269
270
271 (define-public (split-list l sep?)
272 "
273 (display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/))) )
274 =>
275 ((a b c) (d e f) (g))
276
277 "
278 ;; " KUT EMACS.
279
280 (define (split-one sep?  l acc)
281   "Split off the first parts before separator and return both parts."
282   (if (null? l)
283       (cons acc '())
284       (if (sep? (car l))
285           (cons acc (cdr l))
286           (split-one sep? (cdr l) (cons (car l) acc))
287           )
288       ))
289
290 (if (null? l)
291     '()
292     (let* ((c (split-one sep? l '())))
293       (cons (reverse! (car c) '()) (split-list (cdr c) sep?))
294       )))
295
296
297 (define-public (interval-length x)
298   "Length of the number-pair X, when an interval"
299   (max 0 (- (cdr x) (car x)))
300   )
301   
302
303 (define (other-axis a)
304   (remainder (+ a 1) 2))
305   
306
307 (define-public (interval-widen iv amount)
308    (cons (- (car iv) amount)
309          (+ (cdr iv) amount)))
310
311 (define-public (interval-union i1 i2)
312    (cons (min (car i1) (car i2))
313          (max (cdr i1) (cdr i2))))
314
315
316 (define-public (write-me message x)
317   "Return X.  Display MESSAGE and write X.  Handy for debugging, possibly turned off."
318   (display message) (write x) (newline) x)
319 ;;  x)
320
321 (define (index-cell cell dir)
322   (if (equal? dir 1)
323       (cdr cell)
324       (car cell)))
325
326 (define (cons-map f x)
327   "map F to contents of X"
328   (cons (f (car x)) (f (cdr x))))
329
330
331 (define-public (list-insert-separator lst between)
332   "Create new list, inserting BETWEEN between elements of LIST"
333   (define (conc x y )
334     (if (eq? y #f)
335         (list x)
336         (cons x  (cons between y))
337         ))
338   (fold-right conc #f lst))
339
340 ;;;;;;;;;;;;;;;;
341 ; other
342 (define (sign x)
343   (if (= x 0)
344       0
345       (if (< x 0) -1 1)))
346
347 (define-public (symbol<? l r)
348   (string<? (symbol->string l) (symbol->string r)))
349
350 (define-public (!= l r)
351   (not (= l r)))
352
353 (define-public (ly:load x)
354   (let* (
355          (fn (%search-load-path x))
356
357          )
358     (if (ly:get-option 'verbose)
359         (format (current-error-port) "[~A]" fn))
360     (primitive-load fn)))
361
362
363 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
364 ;;  output
365 (use-modules (scm output-tex)
366              (scm output-ps)
367              (scm output-sketch)
368              (scm output-sodipodi)
369              (scm output-pdftex)
370              )
371
372 (define output-alist
373   `(
374     ("tex" . ("TeX output. The default output form." ,tex-output-expression))
375     ("ps" . ("Direct postscript. Requires setting GS_LIB and GS_FONTPATH" ,ps-output-expression))
376     ("scm" . ("Scheme dump: debug scheme stencil expressions" ,write))
377     ("sketch" . ("Bare bones Sketch output." ,sketch-output-expression))
378     ("sodipodi" . ("Bare bones Sodipodi output." ,sodipodi-output-expression))
379     ("pdftex" . ("PDFTeX output. Was last seen nonfunctioning." ,pdftex-output-expression))
380     ))
381
382
383 (define (document-format-dumpers)
384   (map
385    (lambda (x)
386      (display (string-append  (pad-string-to 5 (car x)) (cadr x) "\n"))
387      output-alist)
388    ))
389
390 (define-public (find-dumper format )
391   (let*
392       ((d (assoc format output-alist)))
393     
394     (if (pair? d)
395         (caddr d)
396         (scm-error "Could not find dumper for format ~s" format))
397     ))
398
399 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
400 ;; other files.
401
402 (map ly:load
403                                         ; load-from-path
404      '("define-music-types.scm"
405        "output-lib.scm"
406        "c++.scm"
407        "chord-ignatzek-names.scm"
408        "chord-entry.scm"
409        "chord-generic-names.scm"
410        "stencil.scm"
411        "new-markup.scm"
412        "bass-figure.scm"
413        "music-functions.scm"
414        "part-combiner.scm"
415        "define-music-properties.scm"
416        "auto-beam.scm"
417        "chord-name.scm"
418        
419        "define-translator-properties.scm"
420        "translation-functions.scm"
421        "script.scm"
422        "midi.scm"
423
424        "beam.scm"
425        "clef.scm"
426        "slur.scm"
427        "font.scm"
428        
429        "define-markup-commands.scm"
430        "define-grob-properties.scm"
431        "define-grobs.scm"
432        "define-grob-interfaces.scm"
433
434        "paper.scm"
435        ))
436
437
438 (set! type-p-name-alist
439   `(
440    (,boolean-or-symbol? . "boolean or symbol")
441    (,boolean? . "boolean")
442    (,char? . "char")
443    (,grob-list? . "list of grobs")
444    (,hash-table? . "hash table")
445    (,input-port? . "input port")
446    (,integer? . "integer")
447    (,list? . "list")
448    (,ly:context? . "context")
449    (,ly:dimension? . "dimension, in staff space")
450    (,ly:dir? . "direction")
451    (,ly:duration? . "duration")
452    (,ly:grob? . "layout object")
453    (,ly:input-location? . "input location")
454    (,ly:input-location? . "input location")   
455    (,ly:moment? . "moment")
456    (,ly:music? . "music")
457    (,ly:pitch? . "pitch")
458    (,ly:translator? . "translator")
459    (,markup-list? . "list of markups")
460    (,markup? . "markup")
461    (,music-list? . "list of music")
462    (,number-or-grob? . "number or grob")
463    (,number-or-string? . "number or string")
464    (,number-pair? . "pair of numbers")
465    (,number? . "number")
466    (,output-port? . "output port")   
467    (,pair? . "pair")
468    (,procedure? . "procedure") 
469    (,scheme? . "any type")
470    (,string? . "string")
471    (,symbol? . "symbol")
472    (,vector? . "vector")
473    ))
474
475
476 ;; debug mem leaks
477
478 (define gc-protect-stat-count 0)
479 (define-public (dump-gc-protects)
480   (set! gc-protect-stat-count (1+ gc-protect-stat-count) )
481   
482   (display 
483    (map (lambda (y)
484           (let
485               ((x (car y))
486                (c (cdr y)))
487
488             (string-append
489              (string-join
490               (map object->string (list (object-address x) c x))
491               " ")
492              "\n")))
493
494           (sort
495            (hash-table->alist (ly:protects))
496            (lambda (a b)
497              (< (object-address (car a))
498                 (object-address (car b)))))
499           
500           )
501       (open-file (string-append
502                "gcstat-" (number->string gc-protect-stat-count)
503                ".scm"
504                ) "w")))
505