]> git.donarmstrong.com Git - lilypond.git/blob - scm/c++.scm
2002-07-17 Han-Wen <hanwen@cs.uu.nl>
[lilypond.git] / scm / c++.scm
1 ;;;; c++.scm -- implement Scheme frontends to C++ functions
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 1998--2001 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8 ;;; Note: this file can't be used without LilyPond executable
9
10 (define (number-pair?  x)
11   (and (pair? x)
12        (number? (car x)) (number? (cdr x))))
13 (define (number-or-grob? x)
14   (or (ly-grob? x) (number? x))
15   )
16
17 (define (grob-list? x)
18   (list? x))
19
20 (define (moment-pair?  x)
21   (and (pair? x)
22        (moment? (car x)) (moment? (cdr x))))
23
24 (define (boolean-or-symbol? x)
25   (or (boolean? x) (symbol? x)))
26
27 (define (number-or-boolean? x)
28   (or (number? x) (boolean? x)))
29
30 (define (number-or-string? x)
31   (or (number? x) (string? x)))
32
33 (define (markup? x)
34   (or (string? x) (list? x)))
35
36 (define (scheme? x) #t)
37
38 (define type-p-name-alist
39   `(
40    (,dir? . "direction")
41    (,scheme? . "any type")
42    (,number-pair? . "pair of numbers")
43    (,ly-input-location? . "input location")   
44    (,ly-grob? . "grob (GRaphical OBject)")
45    (,grob-list? . "list of grobs")
46    (,duration? . "duration")
47    (,pair? . "pair")
48    (,integer? . "integer")
49    (,list? . "list")
50    (,symbol? . "symbol")
51    (,string? . "string")
52    (,boolean? . "boolean")
53    (,moment? . "moment")
54    (,ly-input-location? . "input location")
55    (,music-list? . "list of music")
56    (,music? . "music")
57    (,number? . "number")
58    (,char? . "char")
59    (,input-port? . "input port")
60    (,output-port? . "output port")   
61    (,vector? . "vector")
62    (,procedure? . "procedure") 
63    (,boolean-or-symbol? . "boolean or symbol")
64    (,number-or-string? . "number or string")
65    (,number-or-boolean? . "number or boolean")
66    (,markup? . "markup (list or string)")
67    (,number-or-grob? . "number or grob")
68    ))
69
70
71 (define (match-predicate obj alist)
72   (if (null? alist)
73       "Unknown type"
74       (if (apply (caar alist) obj)
75           (cdar alist)
76           (match-predicate obj (cdr alist))
77           )
78       ))
79
80 (define (object-type obj)
81   (match-predicate obj type-p-name-alist))
82
83 (define (type-name  predicate)
84   (let ((entry (assoc predicate type-p-name-alist)))
85     (if (pair? entry) (cdr entry)
86         "unknown"
87         )))
88
89 (define (uniqued-alist  alist acc)
90   (if (null? alist) acc
91       (if (assoc (caar alist) acc)
92           (uniqued-alist (cdr alist) acc)
93           (uniqued-alist (cdr alist) (cons (car alist) acc)))))
94
95
96 ;; used in denneboom.ly
97 (define (cons-map f x)
98   (cons (f (car x)) (f (cdr x))))
99
100 ;; used where?
101 (define (reduce operator list)
102   "reduce OP [A, B, C, D, ... ] =
103    A op (B op (C ... ))
104 "
105       (if (null? (cdr list)) (car list)
106           (operator (car list) (reduce operator (cdr list)))))
107
108
109
110 (define (take-from-list-until todo gathered crit?)
111   "return (G, T), where (reverse G) + T = GATHERED + TODO, and the last of G
112 is the  first to satisfy CRIT "
113   (if (null? todo)
114       (cons gathered todo)
115       (if (crit? (car todo))
116           (cons (cons (car todo) gathered) (cdr todo))
117           (take-from-list-until (cdr todo) (cons (car todo) gathered) crit?)
118       )
119   ))
120 ; test:
121 ; (take-from-list-until '(1 2 3  4 5) '() (lambda (x) (eq? x 3)))
122 ; ((3 2 1) 4 5)
123
124
125
126 ; Make a function that checks score element for being of a specific type. 
127 (define (make-type-checker symbol)
128   (lambda (elt)
129     ;;(display  symbol)
130     ;;(eq? #t (ly-get-grob-property elt symbol))
131     (not (eq? #f (memq symbol (ly-get-grob-property elt 'interfaces))))))
132
133
134 (define (index-cell cell dir)
135   (if (equal? dir 1)
136       (cdr cell)
137       (car cell)))
138
139 (define (repeat-name-to-ctor name)
140   (let*
141       ((supported-reps
142         `(("volta" . ((iterator-ctor . ,Volta_repeat_iterator::constructor)
143                       (start-moment-function .  ,Repeated_music::first_start)
144                       (length . ,Repeated_music::volta_music_length)))
145           
146             ("unfold" . ((iterator-ctor . ,Unfolded_repeat_iterator::constructor)
147                          (start-moment-function .  ,Repeated_music::first_start)                         
148                          (length . ,Repeated_music::unfolded_music_length)))
149             ("fold" . ((iterator-ctor  . ,Folded_repeat_iterator::constructor)
150                        (start-moment-function .  ,Repeated_music::minimum_start)                         
151                        (length . ,Repeated_music::folded_music_length)))
152             ("percent" . ((iterator-ctor . ,Percent_repeat_iterator::constructor)
153                           (start-moment-function .  ,Repeated_music::first_start)
154                           (length . ,Repeated_music::unfolded_music_length)))
155             ("tremolo" . ((iterator-ctor . ,Chord_tremolo_iterator::constructor)
156                           (start-moment-function .  ,Repeated_music::first_start)
157
158                           ;; the length of the repeat is handled by shifting the note logs
159                           (length . ,Repeated_music::folded_music_length)))))
160           
161        (handle (assoc name supported-reps)))
162
163     (if (pair? handle)
164         (cdr handle)
165         (begin
166           (ly-warn
167            (string-append "Unknown repeat type `" name "'\nSee scm/c++.scm for supported repeats"))
168           '(type . 'repeated-music)))))