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