]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
patch::: 1.3.95.jcn1
[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     ;;(display  symbol)
185     ;;(eq? #t (ly-get-elt-property elt symbol))
186     (not (eq? #f (memq symbol (ly-get-elt-property elt 'interfaces))))
187     ))
188
189 ;;;;;;;;;;;;;;;;;;; TeX output
190 (define (tex-scm action-name)
191   (define (unknown) 
192     "%\n\\unknown%\n")
193
194
195   (define (select-font font-name-symbol)
196     (let*
197         (
198          (c (assoc font-name-symbol font-name-alist))
199          )
200
201       (if (eq? c #f)
202           (begin
203             (ly-warn (string-append
204                       "Programming error: No such font known " (car font-name-symbol)))
205             "")                         ; issue no command
206           (string-append "\\" (cdr c)))
207       
208       
209       ))
210   
211   (define (beam width slope thick)
212     (embedded-ps ((ps-scm 'beam) width slope thick)))
213
214   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
215     (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
216
217   (define (dashed-slur thick dash l)
218     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
219
220   (define (crescendo thick w h cont)
221     (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
222
223   (define (char i)
224     (string-append "\\char" (inexact->string i 10) " "))
225   
226   (define (dashed-line thick on off w)
227     (embedded-ps ((ps-scm 'dashed-line) thick on off w)))
228
229   (define (decrescendo thick w h cont)
230     (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
231
232   (define (font-load-command name-mag command)
233     (string-append
234      "\\font\\" command "="
235      (symbol->string (car name-mag))
236      " scaled "
237      (number->string (magstep (cdr name-mag)))
238      "\n"))
239
240   (define (embedded-ps s)
241     (string-append "\\embeddedps{" s "}"))
242
243   (define (comment s)
244     (string-append "% " s))
245   
246   (define (end-output) 
247         (begin
248 ; uncomment for some stats about lily memory      
249 ;               (display (gc-stats))
250     (string-append "\n\\EndLilyPondOutput"
251                    ; Put GC stats here.
252                    )))
253   
254   (define (experimental-on)
255     "")
256
257   (define (font-switch i)
258     (string-append
259      "\\" (font i) "\n"))
260
261   (define (font-def i s)
262     (string-append
263      "\\font" (font-switch i) "=" s "\n"))
264
265   (define (header-end)
266     (string-append
267      "\\special{! "
268
269      ;; URG: ly-gulp-file: now we can't use scm output without Lily
270      (if use-regex
271          ;; fixed in 1.3.4 for powerpc -- broken on Windows
272          (regexp-substitute/global #f "\n"
273                                    (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
274          (ly-gulp-file "lily.ps"))
275      "}"
276      "\\input lilyponddefs \\turnOnPostScript"))
277
278   (define (header creator generate) 
279     (string-append
280      "%created by: " creator generate "\n"))
281
282   (define (invoke-char s i)
283     (string-append 
284      "\n\\" s "{" (inexact->string i 10) "}" ))
285
286   (define (invoke-dim1 s d)
287     (string-append
288      "\n\\" s "{" (number->dim d) "}"))
289   (define (pt->sp x)
290     (* 65536 x))
291   
292   ;;
293   ;; need to do something to make this really safe.
294   ;;
295   (define (output-tex-string s)
296       (if security-paranoia
297           (if use-regex
298               (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
299               (begin (display "warning: not paranoid") (newline) s))
300           s))
301       
302   (define (lily-def key val)
303     (string-append
304      "\\def\\"
305      (if use-regex
306          ;; fixed in 1.3.4 for powerpc -- broken on Windows
307          (regexp-substitute/global #f "_"
308                                    (output-tex-string key) 'pre "X" 'post)
309          (output-tex-string key))
310      "{" (output-tex-string val) "}\n"))
311
312   (define (number->dim x)
313     (string-append 
314      (ly-number->string x) " pt "))
315
316   (define (placebox x y s) 
317     (string-append 
318      "\\placebox{"
319      (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
320
321   (define (bezier-sandwich l thick)
322     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
323
324   (define (start-line ht)
325       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
326
327   (define (stop-line) 
328     "}\\vss}\\interscoreline\n")
329   (define (stop-last-line)
330     "}\\vss}")
331   (define (filledbox breapth width depth height) 
332     (string-append 
333      "\\kern" (number->dim (- breapth))
334      "\\vrule width " (number->dim (+ breapth width))
335      "depth " (number->dim depth)
336      "height " (number->dim height) " "))
337
338   (define (text s)
339     (string-append "\\hbox{" (output-tex-string s) "}"))
340   
341   (define (tuplet ht gapx dx dy thick dir)
342     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
343
344   (define (volta h w thick vert_start vert_end)
345     (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
346
347   (define (define-origin file line col)
348     ; use this for column positions
349      (string-append "\\special{src:" (number->string line) ":"
350         (number->string col) " " file "}")
351
352      ; line numbers only:
353     ;(string-append "\\special{src:" (number->string line) " " file "}")
354 )
355     ; no origin  info: return empty string
356     ; ""
357   ; no-origin not yet supported by Xdvi
358   (define (no-origin) "")
359   
360   ;; TeX
361   ;; The procedures listed below form the public interface of TeX-scm.
362   ;; (should merge the 2 lists)
363   (cond ((eq? action-name 'all-definitions)
364          `(begin
365             (define font-load-command ,font-load-command)
366             (define beam ,beam)
367             (define bezier-sandwich ,bezier-sandwich)
368             (define bracket ,bracket)
369             (define char ,char)
370             (define crescendo ,crescendo)
371             (define dashed-line ,dashed-line) 
372             (define dashed-slur ,dashed-slur) 
373             (define decrescendo ,decrescendo) 
374             (define end-output ,end-output)
375             (define experimental-on ,experimental-on)
376             (define filledbox ,filledbox)
377             (define font-def ,font-def)
378             (define font-switch ,font-switch)
379             (define header-end ,header-end)
380             (define lily-def ,lily-def)
381             (define header ,header) 
382             (define invoke-char ,invoke-char) 
383             (define invoke-dim1 ,invoke-dim1)
384             (define placebox ,placebox)
385             (define select-font ,select-font)
386             (define start-line ,start-line)
387             (define stop-line ,stop-line)
388             (define stop-last-line ,stop-last-line)
389             (define text ,text)
390             (define tuplet ,tuplet)
391             (define volta ,volta)
392             (define define-origin ,define-origin)
393             (define no-origin ,no-origin)
394             ))
395
396         ((eq? action-name 'beam) beam)
397         ((eq? action-name 'tuplet) tuplet)
398         ((eq? action-name 'bracket) bracket)
399         ((eq? action-name 'crescendo) crescendo)
400         ((eq? action-name 'dashed-line) dashed-line) 
401         ((eq? action-name 'dashed-slur) dashed-slur) 
402         ((eq? action-name 'decrescendo) decrescendo) 
403         ((eq? action-name 'end-output) end-output)
404         ((eq? action-name 'experimental-on) experimental-on)
405         ((eq? action-name 'font-def) font-def)
406         ((eq? action-name 'font-switch) font-switch)
407         ((eq? action-name 'header-end) header-end)
408         ((eq? action-name 'lily-def) lily-def)
409         ((eq? action-name 'header) header) 
410         ((eq? action-name 'invoke-char) invoke-char) 
411         ((eq? action-name 'invoke-dim1) invoke-dim1)
412         ((eq? action-name 'placebox) placebox)
413         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
414         ((eq? action-name 'start-line) start-line)
415         ((eq? action-name 'stem) stem)
416         ((eq? action-name 'stop-line) stop-line)
417         ((eq? action-name 'stop-last-line) stop-last-line)
418         ((eq? action-name 'volta) volta)
419         (else (error "unknown tag -- PS-TEX " action-name))
420         )
421   )
422
423
424 ;;;;;;;;;;;; PS
425 (define (ps-scm action-name)
426
427   ;; alist containing fontname -> fontcommand assoc (both strings)
428   (define font-alist '())
429   (define font-count 0)
430   (define current-font "")
431
432   
433   (define (cached-fontname i)
434     (string-append
435      "lilyfont"
436      (make-string 1 (integer->char (+ 65 i)))))
437     
438   (define (mag-to-size m)
439     (number->string (case m 
440                       (0 12)
441                       (1 12)
442                       (2 14) ; really: 14.400
443                       (3 17) ; really: 17.280
444                       (4 21) ; really: 20.736
445                       (5 24) ; really: 24.888
446                       (6 30) ; really: 29.856
447                       )))
448   
449   
450   (define (select-font font-name-symbol)
451     (let*
452         (
453          (c (assoc font-name-symbol font-name-alist))
454          )
455
456       (if (eq? c #f)
457           (begin
458             (ly-warn (string-append
459                       "Programming error: No such font known " (car font-name-symbol)))
460             "")                         ; issue no command
461           (string-append " " (cdr c) " "))
462       
463       
464       ))
465
466     (define (font-load-command name-mag command)
467       (string-append
468        "/" command
469        " { /"
470        (symbol->string (car name-mag))
471        " findfont "
472        (number->string (magstep (cdr name-mag)))
473        " 1000 div 12 mul  scalefont setfont } bind def "
474        "\n"))
475
476
477   (define (beam width slope thick)
478     (string-append
479      (numbers->string (list width slope thick)) " draw_beam" ))
480
481   (define (comment s)
482     (string-append "% " s))
483
484   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
485     (string-append
486      (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
487
488   (define (char i)
489     (invoke-char " show" i))
490
491   (define (crescendo thick w h cont )
492     (string-append 
493      (numbers->string (list w h (inexact->exact cont) thick))
494      " draw_crescendo"))
495
496   ;; what the heck is this interface ?
497   (define (dashed-slur thick dash l)
498     (string-append 
499      (apply string-append (map control->string l)) 
500      (number->string thick) 
501      " [ "
502      (number->string dash)
503      " "
504      (number->string (* 10 thick))      ;UGH.  10 ?
505      " ] 0 draw_dashed_slur"))
506
507   (define (dashed-line thick on off width)
508     (string-append 
509      (number->string width) 
510      " "
511      (number->string thick) 
512      " [ "
513      (number->string on)
514      " "
515      (number->string off)
516      " ] 0 draw_dashed_line"))
517
518   (define (decrescendo thick w h cont)
519     (string-append 
520      (numbers->string (list w h (inexact->exact cont) thick))
521      " draw_decrescendo"))
522
523
524   (define (end-output)
525     "\nshowpage\n")
526   
527   (define (experimental-on) "")
528   
529   (define (filledbox breapth width depth height) 
530     (string-append (numbers->string (list breapth width depth height))
531                    " draw_box" ))
532
533   ;; obsolete?
534   (define (font-def i s)
535     (string-append
536      "\n/" (font i) " {/" 
537      (substring s 0 (- (string-length s) 4))
538      " findfont 12 scalefont setfont} bind def \n"))
539
540   (define (font-switch i)
541     (string-append (font i) " "))
542
543   (define (header-end)
544     (string-append
545      ;; URG: now we can't use scm output without Lily
546      (ly-gulp-file "lilyponddefs.ps")
547      " {exch pop //systemdict /run get exec} "
548      (ly-gulp-file "lily.ps")
549      "{ exch pop //systemdict /run get exec } "
550     ))
551   
552   (define (lily-def key val)
553
554      (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
555          (string-append "/" key " {" val "} bind def\n")
556          (string-append "/" key " (" val ") def\n")
557          )
558      )
559
560   (define (header creator generate) 
561     (string-append
562      "%!PS-Adobe-3.0\n"
563      "%%Creator: " creator generate "\n"))
564   
565   (define (invoke-char s i)
566     (string-append 
567      "(\\" (inexact->string i 8) ") " s " " ))
568   
569   (define (invoke-dim1 s d) 
570     (string-append
571      (number->string (* d  (/ 72.27 72))) " " s ))
572
573   (define (placebox x y s) 
574     (string-append 
575      (number->string x) " " (number->string y) " {" s "} placebox "))
576
577   (define (bezier-sandwich l thick)
578     (string-append 
579      (apply string-append (map control->string l))
580      (number->string  thick)
581      " draw_bezier_sandwich"))
582
583   (define (start-line height)
584           "\nstart_line {\n")
585   
586   (define (stem breapth width depth height) 
587     (string-append (numbers->string (list breapth width depth height))
588                    " draw_box" ))
589
590   (define (stop-line)
591       "}\nstop_line\n")
592
593   (define (text s)
594     (string-append "(" s ") show  "))
595
596
597   (define (volta h w thick vert_start vert_end)
598     (string-append 
599      (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
600      " draw_volta"))
601
602   (define (tuplet ht gap dx dy thick dir)
603     (string-append 
604      (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
605      " draw_tuplet"))
606
607
608   (define (unknown) 
609     "\n unknown\n")
610
611
612   (define (define-origin a b c ) "")
613   (define (no-origin) "")
614   
615   ;; PS
616   (cond ((eq? action-name 'all-definitions)
617          `(begin
618             (define beam ,beam)
619             (define tuplet ,tuplet)
620             (define bracket ,bracket)
621             (define char ,char)
622             (define crescendo ,crescendo)
623             (define volta ,volta)
624             (define bezier-sandwich ,bezier-sandwich)
625             (define dashed-line ,dashed-line) 
626             (define dashed-slur ,dashed-slur) 
627             (define decrescendo ,decrescendo) 
628             (define end-output ,end-output)
629             (define experimental-on ,experimental-on)
630             (define filledbox ,filledbox)
631             (define font-def ,font-def)
632             (define font-switch ,font-switch)
633             (define header-end ,header-end)
634             (define lily-def ,lily-def)
635             (define font-load-command ,font-load-command)
636             (define header ,header) 
637             (define invoke-char ,invoke-char) 
638             (define invoke-dim1 ,invoke-dim1)
639             (define placebox ,placebox)
640             (define select-font ,select-font)
641             (define start-line ,start-line)
642             (define stem ,stem)
643             (define stop-line ,stop-line)
644             (define stop-last-line ,stop-line)
645             (define text ,text)
646             (define no-origin ,no-origin)
647             (define define-origin ,define-origin)
648             ))
649         ((eq? action-name 'tuplet) tuplet)
650         ((eq? action-name 'beam) beam)
651         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
652         ((eq? action-name 'bracket) bracket)
653         ((eq? action-name 'char) char)
654         ((eq? action-name 'crescendo) crescendo)
655         ((eq? action-name 'dashed-line) dashed-line) 
656         ((eq? action-name 'dashed-slur) dashed-slur) 
657         ((eq? action-name 'decrescendo) decrescendo)
658         ((eq? action-name 'experimental-on) experimental-on)
659         ((eq? action-name 'filledbox) filledbox)
660         ((eq? action-name 'select-font) select-font)
661         ((eq? action-name 'volta) volta)
662         (else (error "unknown tag -- PS-SCM " action-name))
663         )
664   )
665
666
667 (define (arg->string arg)
668   (cond ((number? arg) (inexact->string arg 10))
669         ((string? arg) (string-append "\"" arg "\""))
670         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
671
672 (define (func name . args)
673   (string-append 
674    "(" name 
675    (if (null? args) 
676        ""
677        (apply string-append 
678               (map (lambda (x) (string-append " " (arg->string x))) args)))
679    ")\n"))
680
681 (define (sign x)
682   (if (= x 0)
683       1
684       (if (< x 0) -1 1)))
685
686 ;;;; AsciiScript as
687 (define (as-scm action-name)
688
689   (define (beam width slope thick)
690           (string-append
691            (func "set-line-char" "#")
692            (func "rline-to" width (* width slope))
693            ))
694
695   ; simple flat slurs
696   (define (bezier-sandwich l thick)
697           (let (
698                 (c0 (cadddr l))
699                 (c1 (cadr l))
700                 (c3 (caddr l)))
701                (let* ((x (car c0))
702                       (dx (- (car c3) x))
703                       (dy (- (cdr c3) (cdr c0)))
704                       (rc (/ dy dx))
705                       (c1-dx (- (car c1) x))
706                       (c1-line-y (+ (cdr c0) (* c1-dx rc)))
707                       (dir (if (< c1-line-y (cdr c1)) 1 -1))
708                       (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
709                      (string-append
710                       (func "rmove-to" x y)
711                       (func "put" (if (< 0 dir) "/" "\\\\"))
712                       (func "rmove-to" 1 (if (< 0 dir) 1 0))
713                       (func "set-line-char" "_")
714                       (func "h-line" (- dx 1))
715                       (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
716                       (func "put" (if (< 0 dir) "\\\\" "/"))))))
717
718   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
719           (string-append
720            (func "rmove-to" (+ width 1) (- (/ height -2) 1))
721            (func "put" "\\\\")
722            (func "set-line-char" "|")
723            (func "rmove-to" 0 1)
724            (func "v-line" (+ height 1))
725            (func "rmove-to" 0 (+ height 1))
726            (func "put" "/")
727            ))
728
729   (define (char i)
730     (func "char" i))
731
732   (define (define-origin a b c ) "")
733
734   (define (end-output) 
735     (func "end-output"))
736   
737   (define (experimental-on)
738           "")
739
740   (define (filledbox breapth width depth height)
741           (let ((dx (+ width breapth))
742                 (dy (+ depth height)))
743                (string-append 
744                 (func "rmove-to" (* -1 breapth) (* -1 depth))
745                 (if (< dx dy)
746                     (string-append
747                      (func "set-line-char" 
748                            (if (<= dx 1) "|" "#"))
749                      (func "v-line" dy))
750                     (string-append
751                      (func "set-line-char" 
752                            (if (<= dy 1) "-" "="))
753                     (func "h-line" dx))))))
754
755   (define (font-load-command name-mag command)
756     (func "load-font" (car name-mag) (magstep (cdr name-mag))))
757
758   (define (header creator generate) 
759     (func "header" creator generate))
760
761   (define (header-end) 
762     (func "header-end"))
763
764   ;; urg: this is good for half of as2text's execution time
765   (define (xlily-def key val)
766           (string-append "(define " key " " (arg->string val) ")\n"))
767
768   (define (lily-def key val)
769           (if 
770            (or (equal? key "mudelapaperlinewidth")
771                (equal? key "mudelapaperstaffheight"))
772            (string-append "(define " key " " (arg->string val) ")\n")
773            ""))
774
775   (define (no-origin) "")
776   
777   (define (placebox x y s) 
778     (let ((ey (inexact->exact y)))
779           (string-append "(move-to " (number->string (inexact->exact x)) " "
780                          (if (= 0.5 (- (abs y) (abs ey)))
781                              (number->string y)
782                              (number->string ey))
783                          ")\n" s)))
784                        
785   (define (select-font font-name-symbol)
786     (let* ((c (assoc font-name-symbol font-name-alist)))
787       (if (eq? c #f)
788           (begin
789             (ly-warn 
790              (string-append 
791               "Programming error: No such font known " 
792               (car font-name-symbol)))
793             "")                         ; issue no command
794           (func "select-font" (car font-name-symbol)))))
795
796   (define (start-line height)
797           (func "start-line" height))
798
799   (define (stop-line)
800           (func "stop-line"))
801
802   (define (text s)
803           (func "text" s))
804
805   (define (tuplet ht gap dx dy thick dir) "")
806
807   (define (volta h w thick vert-start vert-end)
808           ;; urg
809           (string-append
810            (func "set-line-char" "|")
811            (func "rmove-to" 0 -4)
812            ;; definition strange-way around
813            (if (= 0 vert-start)
814               (func "v-line" h)
815                "")
816            (func "rmove-to" 1 h)
817            (func "set-line-char" "_")
818            (func "h-line" (- w 1))
819            (func "set-line-char" "|")
820            (if (= 0 vert-end)
821                (string-append
822                 (func "rmove-to" (- w 1) (* -1 h))
823                 (func "v-line" (* -1 h)))
824                "")))
825
826   (cond ((eq? action-name 'all-definitions)
827          `(begin
828             (define beam ,beam)
829             (define bracket ,bracket)
830             (define char ,char)
831             (define define-origin ,define-origin)
832             ;;(define crescendo ,crescendo)
833             (define bezier-sandwich ,bezier-sandwich)
834             ;;(define dashed-slur ,dashed-slur) 
835             ;;(define decrescendo ,decrescendo) 
836             (define end-output ,end-output)
837             (define experimental-on ,experimental-on)
838             (define filledbox ,filledbox)
839             ;;(define font-def ,font-def)
840             (define font-load-command ,font-load-command)
841             ;;(define font-switch ,font-switch)
842             (define header ,header) 
843             (define header-end ,header-end)
844             (define lily-def ,lily-def)
845             ;;(define invoke-char ,invoke-char) 
846             ;;(define invoke-dim1 ,invoke-dim1)
847             (define no-origin ,no-origin)
848             (define placebox ,placebox)
849             (define select-font ,select-font)
850             (define start-line ,start-line)
851             ;;(define stem ,stem)
852             (define stop-line ,stop-line)
853             (define stop-last-line ,stop-line)
854             (define text ,text)
855             (define tuplet ,tuplet)
856             (define volta ,volta)
857             ))
858         ((eq? action-name 'tuplet) tuplet)
859         ;;((eq? action-name 'beam) beam)
860         ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
861         ;;((eq? action-name 'bracket) bracket)
862         ((eq? action-name 'char) char)
863         ;;((eq? action-name 'crescendo) crescendo)
864         ;;((eq? action-name 'dashed-slur) dashed-slur) 
865         ;;((eq? action-name 'decrescendo) decrescendo)
866         ;;((eq? action-name 'experimental-on) experimental-on)
867         ((eq? action-name 'filledbox) filledbox)
868         ((eq? action-name 'select-font) select-font)
869         ;;((eq? action-name 'volta) volta)
870         (else (error "unknown tag -- MUSA-SCM " action-name))
871         )
872   )
873
874
875 (define (gulp-file name)
876   (let* ((port (open-file name "r"))
877          (content (let loop ((text ""))
878                        (let ((line (read-line port)))
879                             (if (or (eof-object? line)
880                                     (not line)) 
881                                 text
882                                 (loop (string-append text line "\n")))))))
883         (close port)
884         content))
885
886 ;; urg: Use when standalone, do:
887 ;; (define ly-gulp-file scm-gulp-file)
888 (define (scm-gulp-file name)
889   (set! %load-path 
890         (cons (string-append (getenv 'LILYPONDPREFIX) "/ly")
891               (cons (string-append (getenv 'LILYPONDPREFIX) "/ps")
892                     %load-path)))
893   (let ((path (%search-load-path name)))
894        (if path
895            (gulp-file path)
896            (gulp-file name))))
897
898 (define (scm-tex-output)
899   (ly-eval (tex-scm 'all-definitions)))
900                                 
901 (define (scm-ps-output)
902   (ly-eval (ps-scm 'all-definitions)))
903
904 (define (scm-as-output)
905   (ly-eval (as-scm 'all-definitions)))
906         
907 (define (index-cell cell dir)
908   (if (equal? dir 1)
909       (cdr cell)
910       (car cell)))
911
912 (define major-scale
913   '(
914     (0 . 0)
915     (1 . 0)
916     (2 . 0)
917     (3 . 0)
918     (4 . 0)
919     (5 . 0)
920     (6 . 0)
921     )
922   )