]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
release: 1.3.93
[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 Jan Nieuwenhuizen <janneke@gnu.org>
6
7
8 ;
9 ; This file contains various routines in Scheme that are easier to 
10 ; do here than in C++.  At present it is an unorganised mess. Sorry. 
11 ;
12
13
14 ;(debug-enable 'backtrace)
15
16 ;;; library funtions
17
18 (use-modules (ice-9 regex))
19
20 ;; The regex module may not be available, or may be broken.
21 (define use-regex
22   (let ((os (string-downcase (vector-ref (uname) 0))))
23     (not (equal? "cygwin" (substring os 0 (min 6 (string-length os)))))))
24
25 ;; If you have trouble with regex, define #f
26 (define use-regex #t)
27 ;;(define use-regex #f)
28
29 ;; do nothing in .scm output
30 (define (comment s) "")
31
32 ;; URG guile-1.3/1.4 compatibility
33 (define (ly-eval x) (eval2 x #f))
34
35 (define (comment s) "")
36
37 (define (mm-to-pt x)
38   (* (/ 72.27 25.40) x)
39   )
40
41 (define (cons-map f x)
42   (cons (f (car x)) (f (cdr x))))
43
44 (define (reduce operator list)
45       (if (null? (cdr list)) (car list)
46           (operator (car list) (reduce operator (cdr list)))
47           )
48       )
49
50
51 (define (numbers->string l)
52   (apply string-append (map ly-number->string l)))
53
54 ; (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
55
56 (define (number->octal-string x)
57   (let* ((n (inexact->exact x))
58          (n64 (quotient n 64))
59          (n8 (quotient (- n (* n64 64)) 8)))
60     (string-append
61      (number->string n64)
62      (number->string n8)
63      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
64
65 (define (inexact->string x radix)
66   (let ((n (inexact->exact x)))
67     (number->string n radix)))
68
69
70 (define (control->string c)
71   (string-append (number->string (car c)) " "
72                  (number->string (cdr c)) " "))
73
74
75 (define (font i)
76   (string-append
77    "font"
78    (make-string 1 (integer->char (+ (char->integer #\A) i)))
79    ))
80
81
82
83 (define (scm-scm action-name)
84   1)
85
86 (define security-paranoia #f)
87
88
89 ;; silly, use alist? 
90 (define (find-notehead-symbol duration style)
91   (case style
92    ((cross) "2cross")
93    ((harmonic) "0mensural")
94    ((baroque) 
95     (string-append (number->string duration)
96                    (if (< duration 0) "mensural" "")))
97    ((default) (number->string duration))
98    (else
99     (string-append (number->string duration) (symbol->string style)))))
100
101
102 ;;;;;;;; TeX
103
104 ;; this is silly, can't we use something like
105 ;; roman-0, roman-1 roman+1 ?
106 (define cmr-alist 
107   '(("bold" . "cmbx") 
108     ("brace" . "feta-braces")
109     ("default" . "cmr10")
110     ("dynamic" . "feta-din") 
111     ("feta" . "feta") 
112     ("feta-1" . "feta") 
113     ("feta-2" . "feta") 
114     ("typewriter" . "cmtt") 
115     ("italic" . "cmti") 
116     ("msam" . "msam") 
117     ("roman" . "cmr") 
118     ("script" . "cmr") 
119     ("large" . "cmbx") 
120     ("Large" . "cmbx") 
121     ("mark" . "feta-nummer") 
122     ("finger" . "feta-nummer")
123     ("timesig" . "feta-nummer")
124     ("number" . "feta-nummer") 
125     ("volta" . "feta-nummer"))
126 )
127
128 (define (string-encode-integer i)
129   (cond
130    ((= i  0) "o")
131    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
132    (else (string-append
133           (make-string 1 (integer->char (+ 65 (modulo i 26))))
134           (string-encode-integer (quotient i 26))
135          )
136    )
137   )
138   )
139
140 (define (magstep i)
141   (cdr (assoc i '((-4 . 482)
142                   (-3 . 579)
143                   (-2 . 694)
144                   (-1 . 833)
145                   (0 . 1000)
146                   (1 . 1200) 
147                   (2 . 1440)
148                   (3 . 1728)
149                   (4 . 2074))
150               )
151        )
152   )
153              
154 (define default-script-alist '())
155
156 (define font-name-alist  '())
157 (define (font-command name-mag)
158     (cons name-mag
159           (string-append  "magfont"
160                           (string-encode-integer (hashq (car name-mag) 1000000))
161                           "m"
162                           (string-encode-integer (cdr name-mag)))
163
164           )
165     )
166 (define (define-fonts names)
167   (set! font-name-alist (map font-command names))
168   (apply string-append
169          (map (lambda (x)
170                 (font-load-command (car x) (cdr x))) font-name-alist)
171   ))
172
173 (define (fontify name exp)
174   (string-append (select-font name)
175                  exp)
176   )
177
178 ;;;;;;;;;;;;;;;;;;;;
179
180
181 ; Make a function that checks score element for being of a specific type. 
182 (define (make-type-checker symbol)
183   (lambda (elt)
184     (not (eq? #f (memq symbol (ly-get-elt-property elt 'interfaces))))
185     ))
186
187 ;;;;;;;;;;;;;;;;;;; TeX output
188 (define (tex-scm action-name)
189   (define (unknown) 
190     "%\n\\unknown%\n")
191
192
193   (define (select-font font-name-symbol)
194     (let*
195         (
196          (c (assoc font-name-symbol font-name-alist))
197          )
198
199       (if (eq? c #f)
200           (begin
201             (ly-warn (string-append
202                       "Programming error: No such font known " (car font-name-symbol)))
203             "")                         ; issue no command
204           (string-append "\\" (cdr c)))
205       
206       
207       ))
208   
209   (define (beam width slope thick)
210     (embedded-ps ((ps-scm 'beam) width slope thick)))
211
212   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
213     (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
214
215   (define (dashed-slur thick dash l)
216     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
217
218   (define (crescendo thick w h cont)
219     (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
220
221   (define (char i)
222     (string-append "\\char" (inexact->string i 10) " "))
223   
224   (define (dashed-line thick dash w)
225     (embedded-ps ((ps-scm 'dashed-line) thick dash w)))
226
227   (define (decrescendo thick w h cont)
228     (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
229
230   (define (font-load-command name-mag command)
231     (string-append
232      "\\font\\" command "="
233      (symbol->string (car name-mag))
234      " scaled "
235      (number->string (magstep (cdr name-mag)))
236      "\n"))
237
238   (define (embedded-ps s)
239     (string-append "\\embeddedps{" s "}"))
240
241   (define (comment s)
242     (string-append "% " s))
243   
244   (define (end-output) 
245         (begin
246 ; uncomment for some stats about lily memory      
247 ;               (display (gc-stats))
248     (string-append "\n\\EndLilyPondOutput"
249                    ; Put GC stats here.
250                    )))
251   
252   (define (experimental-on)
253     "")
254
255   (define (font-switch i)
256     (string-append
257      "\\" (font i) "\n"))
258
259   (define (font-def i s)
260     (string-append
261      "\\font" (font-switch i) "=" s "\n"))
262
263   (define (header-end)
264     (string-append
265      "\\special{! "
266
267      ;; URG: ly-gulp-file: now we can't use scm output without Lily
268      (if use-regex
269          ;; fixed in 1.3.4 for powerpc -- broken on Windows
270          (regexp-substitute/global #f "\n"
271                                    (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
272          (ly-gulp-file "lily.ps"))
273      "}"
274      "\\input lilyponddefs \\turnOnPostScript"))
275
276   (define (header creator generate) 
277     (string-append
278      "%created by: " creator generate "\n"))
279
280   (define (invoke-char s i)
281     (string-append 
282      "\n\\" s "{" (inexact->string i 10) "}" ))
283
284   (define (invoke-dim1 s d)
285     (string-append
286      "\n\\" s "{" (number->dim d) "}"))
287   (define (pt->sp x)
288     (* 65536 x))
289   
290   ;;
291   ;; need to do something to make this really safe.
292   ;;
293   (define (output-tex-string s)
294       (if security-paranoia
295           (if use-regex
296               (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
297               (begin (display "warning: not paranoid") (newline) s))
298           s))
299       
300   (define (lily-def key val)
301     (string-append
302      "\\def\\"
303      (if use-regex
304          ;; fixed in 1.3.4 for powerpc -- broken on Windows
305          (regexp-substitute/global #f "_"
306                                    (output-tex-string key) 'pre "X" 'post)
307          (output-tex-string key))
308      "{" (output-tex-string val) "}\n"))
309
310   (define (number->dim x)
311     (string-append 
312      (ly-number->string x) " pt "))
313
314   (define (placebox x y s) 
315     (string-append 
316      "\\placebox{"
317      (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
318
319   (define (bezier-sandwich l thick)
320     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
321
322   (define (start-line ht)
323       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
324
325   (define (stop-line) 
326     "}\\vss}\\interscoreline\n")
327   (define (stop-last-line)
328     "}\\vss}")
329   (define (filledbox breapth width depth height) 
330     (string-append 
331      "\\kern" (number->dim (- breapth))
332      "\\vrule width " (number->dim (+ breapth width))
333      "depth " (number->dim depth)
334      "height " (number->dim height) " "))
335
336   (define (text s)
337     (string-append "\\hbox{" (output-tex-string s) "}"))
338   
339   (define (tuplet ht gapx dx dy thick dir)
340     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
341
342   (define (volta h w thick vert_start vert_end)
343     (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
344
345   (define (define-origin file line col)
346     ; use this for column positions
347      (string-append "\\special{src:" (number->string line) ":"
348         (number->string col) " " file "}")
349
350      ; line numbers only:
351     ;(string-append "\\special{src:" (number->string line) " " file "}")
352 )
353     ; no origin  info: return empty string
354     ; ""
355   ; no-origin not yet supported by Xdvi
356   (define (no-origin) "")
357   
358   ;; TeX
359   ;; The procedures listed below form the public interface of TeX-scm.
360   ;; (should merge the 2 lists)
361   (cond ((eq? action-name 'all-definitions)
362          `(begin
363             (define font-load-command ,font-load-command)
364             (define beam ,beam)
365             (define bezier-sandwich ,bezier-sandwich)
366             (define bracket ,bracket)
367             (define char ,char)
368             (define crescendo ,crescendo)
369             (define dashed-line ,dashed-line) 
370             (define dashed-slur ,dashed-slur) 
371             (define decrescendo ,decrescendo) 
372             (define end-output ,end-output)
373             (define experimental-on ,experimental-on)
374             (define filledbox ,filledbox)
375             (define font-def ,font-def)
376             (define font-switch ,font-switch)
377             (define header-end ,header-end)
378             (define lily-def ,lily-def)
379             (define header ,header) 
380             (define invoke-char ,invoke-char) 
381             (define invoke-dim1 ,invoke-dim1)
382             (define placebox ,placebox)
383             (define select-font ,select-font)
384             (define start-line ,start-line)
385             (define stop-line ,stop-line)
386             (define stop-last-line ,stop-last-line)
387             (define text ,text)
388             (define tuplet ,tuplet)
389             (define volta ,volta)
390             (define define-origin ,define-origin)
391             (define no-origin ,no-origin)
392             ))
393
394         ((eq? action-name 'beam) beam)
395         ((eq? action-name 'tuplet) tuplet)
396         ((eq? action-name 'bracket) bracket)
397         ((eq? action-name 'crescendo) crescendo)
398         ((eq? action-name 'dashed-line) dashed-line) 
399         ((eq? action-name 'dashed-slur) dashed-slur) 
400         ((eq? action-name 'decrescendo) decrescendo) 
401         ((eq? action-name 'end-output) end-output)
402         ((eq? action-name 'experimental-on) experimental-on)
403         ((eq? action-name 'font-def) font-def)
404         ((eq? action-name 'font-switch) font-switch)
405         ((eq? action-name 'header-end) header-end)
406         ((eq? action-name 'lily-def) lily-def)
407         ((eq? action-name 'header) header) 
408         ((eq? action-name 'invoke-char) invoke-char) 
409         ((eq? action-name 'invoke-dim1) invoke-dim1)
410         ((eq? action-name 'placebox) placebox)
411         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
412         ((eq? action-name 'start-line) start-line)
413         ((eq? action-name 'stem) stem)
414         ((eq? action-name 'stop-line) stop-line)
415         ((eq? action-name 'stop-last-line) stop-last-line)
416         ((eq? action-name 'volta) volta)
417         (else (error "unknown tag -- PS-TEX " action-name))
418         )
419   )
420
421
422 ;;;;;;;;;;;; PS
423 (define (ps-scm action-name)
424
425   ;; alist containing fontname -> fontcommand assoc (both strings)
426   (define font-alist '())
427   (define font-count 0)
428   (define current-font "")
429
430   
431   (define (cached-fontname i)
432     (string-append
433      "lilyfont"
434      (make-string 1 (integer->char (+ 65 i)))))
435     
436   (define (mag-to-size m)
437     (number->string (case m 
438                       (0 12)
439                       (1 12)
440                       (2 14) ; really: 14.400
441                       (3 17) ; really: 17.280
442                       (4 21) ; really: 20.736
443                       (5 24) ; really: 24.888
444                       (6 30) ; really: 29.856
445                       )))
446   
447   
448   (define (select-font font-name-symbol)
449     (let*
450         (
451          (c (assoc font-name-symbol font-name-alist))
452          )
453
454       (if (eq? c #f)
455           (begin
456             (ly-warn (string-append
457                       "Programming error: No such font known " (car font-name-symbol)))
458             "")                         ; issue no command
459           (string-append " " (cdr c) " "))
460       
461       
462       ))
463
464     (define (font-load-command name-mag command)
465       (string-append
466        "/" command
467        " { /"
468        (symbol->string (car name-mag))
469        " findfont "
470        (number->string (magstep (cdr name-mag)))
471        " 1000 div 12 mul  scalefont setfont } bind def "
472        "\n"))
473
474
475   (define (beam width slope thick)
476     (string-append
477      (numbers->string (list width slope thick)) " draw_beam" ))
478
479   (define (comment s)
480     (string-append "% " s))
481
482   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
483     (string-append
484      (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
485
486   (define (char i)
487     (invoke-char " show" i))
488
489   (define (crescendo thick w h cont )
490     (string-append 
491      (numbers->string (list w h (inexact->exact cont) thick))
492      " draw_crescendo"))
493
494   ;; what the heck is this interface ?
495   (define (dashed-slur thick dash l)
496     (string-append 
497      (apply string-append (map control->string l)) 
498      (number->string thick) 
499      " [ "
500      (number->string dash)
501      " "
502      (number->string (* 10 thick))      ;UGH.  10 ?
503      " ] 0 draw_dashed_slur"))
504
505   (define (dashed-line thick dash width)
506     (string-append 
507      (number->string width) 
508      " "
509      (number->string thick) 
510      " [ "
511      (number->string dash)
512      " "
513      (number->string dash)
514      " ] 0 draw_dashed_line"))
515
516   (define (decrescendo thick w h cont)
517     (string-append 
518      (numbers->string (list w h (inexact->exact cont) thick))
519      " draw_decrescendo"))
520
521
522   (define (end-output)
523     "\nshowpage\n")
524   
525   (define (experimental-on) "")
526   
527   (define (filledbox breapth width depth height) 
528     (string-append (numbers->string (list breapth width depth height))
529                    " draw_box" ))
530
531   ;; obsolete?
532   (define (font-def i s)
533     (string-append
534      "\n/" (font i) " {/" 
535      (substring s 0 (- (string-length s) 4))
536      " findfont 12 scalefont setfont} bind def \n"))
537
538   (define (font-switch i)
539     (string-append (font i) " "))
540
541   (define (header-end)
542     (string-append
543      ;; URG: now we can't use scm output without Lily
544      (ly-gulp-file "lilyponddefs.ps")
545      " {exch pop //systemdict /run get exec} "
546      (ly-gulp-file "lily.ps")
547      "{ exch pop //systemdict /run get exec } "
548     ))
549   
550   (define (lily-def key val)
551
552      (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
553          (string-append "/" key " {" val "} bind def\n")
554          (string-append "/" key " (" val ") def\n")
555          )
556      )
557
558   (define (header creator generate) 
559     (string-append
560      "%!PS-Adobe-3.0\n"
561      "%%Creator: " creator generate "\n"))
562   
563   (define (invoke-char s i)
564     (string-append 
565      "(\\" (inexact->string i 8) ") " s " " ))
566   
567   (define (invoke-dim1 s d) 
568     (string-append
569      (number->string (* d  (/ 72.27 72))) " " s ))
570
571   (define (placebox x y s) 
572     (string-append 
573      (number->string x) " " (number->string y) " {" s "} placebox "))
574
575   (define (bezier-sandwich l thick)
576     (string-append 
577      (apply string-append (map control->string l))
578      (number->string  thick)
579      " draw_bezier_sandwich"))
580
581   (define (start-line height)
582           "\nstart_line {\n")
583   
584   (define (stem breapth width depth height) 
585     (string-append (numbers->string (list breapth width depth height))
586                    " draw_box" ))
587
588   (define (stop-line)
589       "}\nstop_line\n")
590
591   (define (text s)
592     (string-append "(" s ") show  "))
593
594
595   (define (volta h w thick vert_start vert_end)
596     (string-append 
597      (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
598      " draw_volta"))
599
600   (define (tuplet ht gap dx dy thick dir)
601     (string-append 
602      (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
603      " draw_tuplet"))
604
605
606   (define (unknown) 
607     "\n unknown\n")
608
609
610   (define (define-origin a b c ) "")
611   (define (no-origin) "")
612   
613   ;; PS
614   (cond ((eq? action-name 'all-definitions)
615          `(begin
616             (define beam ,beam)
617             (define tuplet ,tuplet)
618             (define bracket ,bracket)
619             (define char ,char)
620             (define crescendo ,crescendo)
621             (define volta ,volta)
622             (define bezier-sandwich ,bezier-sandwich)
623             (define dashed-line ,dashed-line) 
624             (define dashed-slur ,dashed-slur) 
625             (define decrescendo ,decrescendo) 
626             (define end-output ,end-output)
627             (define experimental-on ,experimental-on)
628             (define filledbox ,filledbox)
629             (define font-def ,font-def)
630             (define font-switch ,font-switch)
631             (define header-end ,header-end)
632             (define lily-def ,lily-def)
633             (define font-load-command ,font-load-command)
634             (define header ,header) 
635             (define invoke-char ,invoke-char) 
636             (define invoke-dim1 ,invoke-dim1)
637             (define placebox ,placebox)
638             (define select-font ,select-font)
639             (define start-line ,start-line)
640             (define stem ,stem)
641             (define stop-line ,stop-line)
642             (define stop-last-line ,stop-line)
643             (define text ,text)
644             (define no-origin ,no-origin)
645             (define define-origin ,define-origin)
646             ))
647         ((eq? action-name 'tuplet) tuplet)
648         ((eq? action-name 'beam) beam)
649         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
650         ((eq? action-name 'bracket) bracket)
651         ((eq? action-name 'char) char)
652         ((eq? action-name 'crescendo) crescendo)
653         ((eq? action-name 'dashed-line) dashed-line) 
654         ((eq? action-name 'dashed-slur) dashed-slur) 
655         ((eq? action-name 'decrescendo) decrescendo)
656         ((eq? action-name 'experimental-on) experimental-on)
657         ((eq? action-name 'filledbox) filledbox)
658         ((eq? action-name 'select-font) select-font)
659         ((eq? action-name 'volta) volta)
660         (else (error "unknown tag -- PS-SCM " action-name))
661         )
662   )
663
664
665 (define (arg->string arg)
666   (cond ((number? arg) (inexact->string arg 10))
667         ((string? arg) (string-append "\"" arg "\""))
668         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
669
670 (define (func name . args)
671   (string-append 
672    "(" name 
673    (if (null? args) 
674        ""
675        (apply string-append 
676               (map (lambda (x) (string-append " " (arg->string x))) args)))
677    ")\n"))
678
679 (define (sign x)
680   (if (= x 0)
681       1
682       (if (< x 0) -1 1)))
683
684 ;;;; AsciiScript as
685 (define (as-scm action-name)
686
687   (define (beam width slope thick)
688           (string-append
689            (func "set-line-char" "#")
690            (func "rline-to" width (* width slope))
691            ))
692
693   ; simple flat slurs
694   (define (bezier-sandwich l thick)
695           (let (
696                 (c0 (cadddr l))
697                 (c1 (cadr l))
698                 (c3 (caddr l)))
699                (let* ((x (car c0))
700                       (dx (- (car c3) x))
701                       (dy (- (cdr c3) (cdr c0)))
702                       (rc (/ dy dx))
703                       (c1-dx (- (car c1) x))
704                       (c1-line-y (+ (cdr c0) (* c1-dx rc)))
705                       (dir (if (< c1-line-y (cdr c1)) 1 -1))
706                       (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
707                      (string-append
708                       (func "rmove-to" x y)
709                       (func "put" (if (< 0 dir) "/" "\\\\"))
710                       (func "rmove-to" 1 (if (< 0 dir) 1 0))
711                       (func "set-line-char" "_")
712                       (func "h-line" (- dx 1))
713                       (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
714                       (func "put" (if (< 0 dir) "\\\\" "/"))))))
715
716   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
717           (string-append
718            (func "rmove-to" (+ width 1) (- (/ height -2) 1))
719            (func "put" "\\\\")
720            (func "set-line-char" "|")
721            (func "rmove-to" 0 1)
722            (func "v-line" (+ height 1))
723            (func "rmove-to" 0 (+ height 1))
724            (func "put" "/")
725            ))
726
727   (define (char i)
728     (func "char" i))
729
730   (define (define-origin a b c ) "")
731
732   (define (end-output) 
733     (func "end-output"))
734   
735   (define (experimental-on)
736           "")
737
738   (define (filledbox breapth width depth height)
739           (let ((dx (+ width breapth))
740                 (dy (+ depth height)))
741                (string-append 
742                 (func "rmove-to" (* -1 breapth) (* -1 depth))
743                 (if (< dx dy)
744                     (string-append
745                      (func "set-line-char" 
746                            (if (<= dx 1) "|" "#"))
747                      (func "v-line" dy))
748                     (string-append
749                      (func "set-line-char" 
750                            (if (<= dy 1) "-" "="))
751                     (func "h-line" dx))))))
752
753   (define (font-load-command name-mag command)
754     (func "load-font" (car name-mag) (magstep (cdr name-mag))))
755
756   (define (header creator generate) 
757     (func "header" creator generate))
758
759   (define (header-end) 
760     (func "header-end"))
761
762   ;; urg: this is good for half of as2text's execution time
763   (define (xlily-def key val)
764           (string-append "(define " key " " (arg->string val) ")\n"))
765
766   (define (lily-def key val)
767           (if 
768            (or (equal? key "mudelapaperlinewidth")
769                (equal? key "mudelapaperstaffheight"))
770            (string-append "(define " key " " (arg->string val) ")\n")
771            ""))
772
773   (define (no-origin) "")
774   
775   (define (placebox x y s) 
776     (let ((ey (inexact->exact y)))
777           (string-append "(move-to " (number->string (inexact->exact x)) " "
778                          (if (= 0.5 (- (abs y) (abs ey)))
779                              (number->string y)
780                              (number->string ey))
781                          ")\n" s)))
782                        
783   (define (select-font font-name-symbol)
784     (let* ((c (assoc font-name-symbol font-name-alist)))
785       (if (eq? c #f)
786           (begin
787             (ly-warn 
788              (string-append 
789               "Programming error: No such font known " 
790               (car font-name-symbol)))
791             "")                         ; issue no command
792           (func "select-font" (car font-name-symbol)))))
793
794   (define (start-line height)
795           (func "start-line" height))
796
797   (define (stop-line)
798           (func "stop-line"))
799
800   (define (text s)
801           (func "text" s))
802
803   (define (tuplet ht gap dx dy thick dir) "")
804
805   (define (volta h w thick vert-start vert-end)
806           ;; urg
807           (string-append
808            (func "set-line-char" "|")
809            (func "rmove-to" 0 -4)
810            ;; definition strange-way around
811            (if (= 0 vert-start)
812               (func "v-line" h)
813                "")
814            (func "rmove-to" 1 h)
815            (func "set-line-char" "_")
816            (func "h-line" (- w 1))
817            (func "set-line-char" "|")
818            (if (= 0 vert-end)
819                (string-append
820                 (func "rmove-to" (- w 1) (* -1 h))
821                 (func "v-line" (* -1 h)))
822                "")))
823
824   (cond ((eq? action-name 'all-definitions)
825          `(begin
826             (define beam ,beam)
827             (define bracket ,bracket)
828             (define char ,char)
829             (define define-origin ,define-origin)
830             ;;(define crescendo ,crescendo)
831             (define bezier-sandwich ,bezier-sandwich)
832             ;;(define dashed-slur ,dashed-slur) 
833             ;;(define decrescendo ,decrescendo) 
834             (define end-output ,end-output)
835             (define experimental-on ,experimental-on)
836             (define filledbox ,filledbox)
837             ;;(define font-def ,font-def)
838             (define font-load-command ,font-load-command)
839             ;;(define font-switch ,font-switch)
840             (define header ,header) 
841             (define header-end ,header-end)
842             (define lily-def ,lily-def)
843             ;;(define invoke-char ,invoke-char) 
844             ;;(define invoke-dim1 ,invoke-dim1)
845             (define no-origin ,no-origin)
846             (define placebox ,placebox)
847             (define select-font ,select-font)
848             (define start-line ,start-line)
849             ;;(define stem ,stem)
850             (define stop-line ,stop-line)
851             (define stop-last-line ,stop-line)
852             (define text ,text)
853             (define tuplet ,tuplet)
854             (define volta ,volta)
855             ))
856         ((eq? action-name 'tuplet) tuplet)
857         ;;((eq? action-name 'beam) beam)
858         ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
859         ;;((eq? action-name 'bracket) bracket)
860         ((eq? action-name 'char) char)
861         ;;((eq? action-name 'crescendo) crescendo)
862         ;;((eq? action-name 'dashed-slur) dashed-slur) 
863         ;;((eq? action-name 'decrescendo) decrescendo)
864         ;;((eq? action-name 'experimental-on) experimental-on)
865         ((eq? action-name 'filledbox) filledbox)
866         ((eq? action-name 'select-font) select-font)
867         ;;((eq? action-name 'volta) volta)
868         (else (error "unknown tag -- MUSA-SCM " action-name))
869         )
870   )
871
872
873 (define (gulp-file name)
874   (let* ((port (open-file name "r"))
875          (content (let loop ((text ""))
876                        (let ((line (read-line port)))
877                             (if (or (eof-object? line)
878                                     (not line)) 
879                                 text
880                                 (loop (string-append text line "\n")))))))
881         (close port)
882         content))
883
884 ;; urg: Use when standalone, do:
885 ;; (define ly-gulp-file scm-gulp-file)
886 (define (scm-gulp-file name)
887   (set! %load-path 
888         (cons (string-append (getenv 'LILYPONDPREFIX) "/ly")
889               (cons (string-append (getenv 'LILYPONDPREFIX) "/ps")
890                     %load-path)))
891   (let ((path (%search-load-path name)))
892        (if path
893            (gulp-file path)
894            (gulp-file name))))
895
896 (define (scm-tex-output)
897   (ly-eval (tex-scm 'all-definitions)))
898                                 
899 (define (scm-ps-output)
900   (ly-eval (ps-scm 'all-definitions)))
901
902 (define (scm-as-output)
903   (ly-eval (as-scm 'all-definitions)))
904         
905 (define (index-cell cell dir)
906   (if (equal? dir 1)
907       (cdr cell)
908       (car cell)))
909
910 (define major-scale
911   '(
912     (0 . 0)
913     (1 . 0)
914     (2 . 0)
915     (3 . 0)
916     (4 . 0)
917     (5 . 0)
918     (6 . 0)
919     )
920   )