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