]> git.donarmstrong.com Git - lilypond.git/blob - scm/fret-diagrams.scm
(class New_slur): new file. Score based slur
[lilypond.git] / scm / fret-diagrams.scm
1 ;;;; fret-diagrams.scm -- 
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2004 Carl D. Sorensen <c_sorensen@byu.edu>
6
7 (define ly:paper-lookup ly:output-def-lookup) ; compat for 2.3, remove  when using 2.2
8 (define my-font-encoding 'TeX-text )
9
10 (define (fret-parse-marking-list marking-list fret-count)
11    (let* ((fret-range (list 1 fret-count))
12           (barre-list '())
13           (dot-list '())
14           (xo-list '())
15           (output-alist '()))
16       (let parse-item ((mylist marking-list))
17           (if (not (null? mylist))
18               (let* ((my-item (car mylist)) (my-code (car my-item)))
19                  (cond
20                      ((or (eq? my-code 'open)(eq? my-code 'mute))
21                         (set! xo-list (cons* my-item xo-list)))
22                      ((eq? my-code 'barre)
23                         (set! barre-list (cons* (cdr my-item) barre-list)))
24                      ((eq? my-code 'place-fret)
25                         (set! dot-list (cons* (cdr my-item) dot-list))))
26                  (parse-item (cdr mylist)))))
27                ; calculate fret-range
28                (let ((maxfret 0) (minfret 99))
29                     (let updatemax ((fret-list dot-list))
30                         (if (null?  fret-list)
31                            '()
32                            (let ((fretval (second (car fret-list))))
33                                (if (> fretval maxfret) (set! maxfret fretval))
34                                (if (< fretval minfret) (set! minfret fretval))
35                                (updatemax (cdr fret-list)))))
36                     (if (> maxfret fret-count)
37                         (set! fret-range (list minfret
38                              (let ((upfret (- (+ minfret fret-count) 1)))
39                                   (if (> maxfret upfret) maxfret upfret)))))
40                     ; subtract fret from dots
41                     (set! dot-list (subtract-base-fret (- (car fret-range) 1) dot-list)))
42                (acons 'fret-range fret-range
43                (acons 'barre-list barre-list
44                (acons 'dot-list dot-list
45                (acons 'xo-list xo-list '()))))))
46    
47 (define (subtract-base-fret base-fret dot-list)
48 "Subtract @var{base-fret} from every fret in @var{dot-list}"  
49   (if (null? dot-list)
50       '()
51       (let ((this-list (car dot-list)))
52       (cons* (list (car this-list) (- (second this-list) base-fret) (if (null? (cddr this-list))
53                                                                     '()
54                                                                     (third this-list)))
55              (subtract-base-fret base-fret (cdr dot-list))))))
56
57 (define (draw-strings string-count fret-range th size)
58 "Draw the strings (vertical lines) for a fret diagram with @var{string-count} strings and frets as indicated
59    in @var{fret-range}.  Line thickness is given by @var{th}, fret & string spacing by @var{size}. "
60   (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
61          (sl (* (+ fret-count 1) size))
62          (sth (* size th))
63          (half-thickness (* sth 0.5))
64          (gap (- size sth))
65          (string-stencil (ly:make-stencil (list 'draw-line sth 0 0 0 sl)
66                          (cons (- half-thickness) half-thickness)
67                          (cons (- half-thickness) (+ sl half-thickness)))))
68     (if (= string-count 1)
69          string-stencil
70         (ly:stencil-combine-at-edge
71          (draw-strings (- string-count 1) fret-range th size) X RIGHT
72          string-stencil
73          gap 0))))
74
75 (define (draw-fret-lines fret-count string-count th size)
76  "Draw @var{fret-count} frets (horizontal lines) for a fret diagram with @var{string-count} strings.
77    Line thickness is given by @var{th}, fret & string spacing by @var{size}. "
78    (let* ((fret-length (* (- string-count 1) size))
79           (sth (* size th))
80           (half-thickness (* sth 0.5))
81           (gap (- size sth))
82           (fret-line (ly:make-stencil (list 'draw-line sth half-thickness size (- fret-length half-thickness) size)
83                           (cons 0 fret-length)
84                           (cons (- size half-thickness) (+  size half-thickness)))))
85        (if (= fret-count 1)
86          fret-line
87          (ly:stencil-combine-at-edge fret-line Y UP
88           (draw-fret-lines (- fret-count 1) string-count th size)
89           gap 0))))
90           
91 (define (draw-thick-top-fret props string-count th size)
92  "Draw a thick top fret for a fret diagram whose base fret is not 1."
93    (let* ((sth (* th size))
94 ;          (top-fret-thick (* sth (chain-assoc-get 'top-fret-thickness props 3.0)))
95           (top-fret-thick (* sth 3.0))
96 ;          (top-half-thick (* top-fret-thick 0.5))
97           (half-thick (* sth 0.5))
98           (x1 half-thick)
99           (x2 (+ half-thick (* size (- string-count 1))))
100           (y1 (- half-thick))
101           (y2 (+ top-fret-thick half-thick))
102           (x-extent (cons (- x1) x2))
103           (y-extent (cons 0 y2)))
104           (ly:make-stencil (list 'round-filled-box x1 x2 y1 y2 sth)
105                             x-extent y-extent)))           
106  
107  
108 (define (draw-frets paper props fret-range string-count th size)
109  "Draw the frets (horizontal lines) for a fret diagram with @var{string-count} strings and frets as indicated
110    in @var{fret-range}.  Line thickness is given by @var{th}, fret & string spacing by @var{size}. "
111   (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
112          (fret-length (* (- string-count 1) size))
113          (half-thickness (* th 0.5))
114          (base-fret (car fret-range)))
115        (ly:stencil-combine-at-edge
116           (draw-fret-lines fret-count string-count th size) Y UP
117              (if (= base-fret 1)
118                  (draw-thick-top-fret props string-count th size)
119                  (draw-fret-lines 1 string-count th size)) 
120                  (- size th) 0))) 
121                  
122
123 (define (centered-stencil stencil)
124  "Center stencil @var{stencil} in both the X and Y directions"
125  (let* ((output-stencil stencil))
126 ;     (if (= (cadr (ly:version)) 3)
127          (begin
128            (ly:stencil-align-to! output-stencil Y 0)
129            (ly:stencil-align-to! output-stencil X 0)
130            output-stencil)))
131 ;        (ly:stencil-align-to (ly:stencil-align-to text-stencil X 0) Y 0))))
132
133 (define (draw-dots paper props string-count fret-range size finger-code dot-position dot-radius dot-list)
134   "Make dots for fret diagram."
135   (let* ((scale-dot-radius (* size dot-radius))
136          (dot-color (chain-assoc-get 'dot-color props 'black))
137 ;         (finger-xoffset (chain-assoc-get 'finger-xoffset props -0.25))
138 ;         (finger-yoffset (chain-assoc-get 'finger-yoffset props (- size)))
139          (finger-xoffset -0.25)
140          (finger-yoffset (- size))
141 ;         (dot-label-font-mag (* scale-dot-radius (chain-assoc-get 'dot-label-font-mag props 1.0)))
142          (dot-label-font-mag scale-dot-radius)
143 ;         (string-label-font-mag (* size (chain-assoc-get 'label-font-mag props 0.7)))
144          (string-label-font-mag (* size 0.7))
145          (fret-count (+ (- (cadr fret-range) (car fret-range) 1)))
146          (mypair (car dot-list))
147          (restlist (cdr dot-list))
148          (xpos (* size (- string-count (car mypair))))
149 ;TODO -- figure out what 4 is and get rid of it
150 ;UGH -- 4?
151          (ypos (* size (+ 4 (- fret-count (cadr mypair) dot-position ))))
152          (extent (cons (- scale-dot-radius) scale-dot-radius))
153          (finger (caddr mypair))
154          (finger (if (number? finger) (number->string finger) finger))
155           (string-label-font (ly:paper-get-font paper `(((font-family . sans)(font-encoding . ,my-font-encoding)(font-series . medium) (font-shape . upright)
156                                         (font-size . ,(stepmag  string-label-font-mag))))))
157           (dot-label-font (ly:paper-get-font paper `(((font-family . sans)(font-encoding . ,my-font-encoding)(font-series . medium) (font-shape . upright)
158                                         (font-size . ,(stepmag  dot-label-font-mag))))))
159          (dotstencil  (if (eq? dot-color 'white)
160                           (begin
161                           (ly:make-stencil (list 'white-dot 0 0 scale-dot-radius) extent extent))
162                           (ly:make-stencil (list 'dot 0 0 scale-dot-radius ) extent extent)))
163          (positioned-dot (ly:stencil-translate-axis
164                            (ly:stencil-translate-axis dotstencil xpos X)
165                            ypos Y))
166          (labeled-dot-stencil 
167                  (if (or (eq? finger '())(eq? finger-code 'none))
168                      positioned-dot
169                  (if (eq? finger-code 'in-dot)
170                     (let*  ((dot-proc (if (eq? dot-color 'white) 'white-dot 'dot)))
171                      (ly:stencil-add 
172                         (ly:stencil-translate-axis 
173                           (ly:stencil-translate-axis 
174                               (if (eq? dot-color 'white)
175                               (centered-stencil (fontify-text dot-label-font finger))
176                               (centered-stencil (fontify-text-white dot-label-font-mag 
177                                                                     dot-label-font  finger)))
178                                xpos X)
179                               ypos Y)
180                         (ly:stencil-translate-axis
181                            (ly:stencil-translate-axis 
182                               (ly:make-stencil (list dot-proc 0 0 scale-dot-radius) extent extent)
183                                xpos X)
184                            ypos Y)))
185                  (if (eq? finger-code 'below-string) 
186                      (ly:stencil-add 
187                          positioned-dot
188                          (ly:stencil-translate-axis 
189                              (ly:stencil-translate-axis 
190                                  (centered-stencil (fontify-text string-label-font finger)) xpos  X)
191                              (* size finger-yoffset) Y))
192                      ;unknown finger-code
193                      positioned-dot)))))
194     (if (null? restlist) 
195         labeled-dot-stencil
196         (ly:stencil-add 
197             (draw-dots paper props string-count fret-range size finger-code 
198                           dot-position dot-radius restlist)
199             labeled-dot-stencil))))
200
201 (define (draw-xo paper props string-count fret-range size xo-list) 
202 "Put open and mute string indications on diagram, as contained in @var{xo-list}."
203     (let* ((fret-count (+ (- (cadr fret-range) (car fret-range) 1)))
204 ;           (xo-font-mag (* size (chain-assoc-get 'xo-font-magnification props 0.5)))
205            (xo-font-mag (* size 0.5))
206 ;           (xo-horizontal-offset (* size (chain-assoc-get 'xo-horizontal-offset props -0.35)))
207            (xo-horizontal-offset (* size -0.35))
208            (font (ly:paper-get-font paper `(((font-encoding . ,my-font-encoding)(font-family . sans)
209                                         (font-series . medium) (font-shape . upright)
210                                         (font-size . ,(stepmag (* size xo-font-mag)))))))
211            (mypair (car xo-list))
212            (restlist (cdr xo-list))
213            (glyph-string (if (eq? (car mypair) 'mute) "X" "O"))
214            (xpos (+ (* (- string-count (cadr mypair)) size) xo-horizontal-offset ))
215            (glyph-stencil (ly:stencil-translate-axis (fontify-text font glyph-string) xpos X)))
216       (if (null? restlist)
217           glyph-stencil
218           (ly:stencil-add
219             (draw-xo paper props string-count fret-range size restlist)
220             glyph-stencil))))
221
222 (define (make-bezier-sandwich-list left right bottom height thickness)
223 " Make the argument list for a horizontal bezier sandwich from @var{left} to @var{right} with a bottom at @var{bottom}, 
224   a height of @var{height}, and a thickness of @var{thickness}."
225    (let* ((width (+ (- right left) 1))
226           (x1 (+ (* width thickness) left))
227           (x2 (- right (* width thickness)))
228           (bottom-control-point-height (+ bottom (- height thickness)))
229           (top-control-point-height (+ bottom height)))
230            ; order of points is: left cp low, right cp low, right end low, left end low
231            ;                     right cp high, left cp high, left end high, right end high.
232        (list (cons x1 bottom-control-point-height) (cons x2 bottom-control-point-height) (cons right bottom) (cons left bottom)
233              (cons x2 top-control-point-height) (cons x1 top-control-point-height) (cons left bottom) (cons right bottom))))
234
235 (define (draw-barre paper props string-count fret-range size finger-code dot-position dot-radius barre-list)
236    "Create barre indications for a fret diagram"
237    (if (not (null? barre-list))
238      (let* ((string1 (caar barre-list))
239             (string2 (cadar barre-list))
240             (fret    (caddar barre-list))
241             (barre-type (chain-assoc-get 'barre-type props 'curved))
242             (scale-dot-radius (* size dot-radius))
243             (barre-vertical-offset (chain-assoc-get 'barre-vertical-offset props 0.5))
244             ; 2 is 1 for empty fret at bottom of figure + 1 for interval (top-fret - fret + 1) -- not an arbitrary constant
245             (dot-center-y (* size (- (+ 2 (- (cadr fret-range) fret))dot-position) ))
246             (bottom (+  dot-center-y (* barre-vertical-offset scale-dot-radius)))
247             (left (* size (- string-count string1)))
248             (right (* size (- string-count string2)))
249 ;            (bezier-thick (chain-assoc-get 'bezier-thickness props 0.1))
250             (bezier-thick 0.1)
251 ;            (bezier-height (chain-assoc-get 'bezier-height props 0.5))
252             (bezier-height 0.5)
253             (bezier-list (make-bezier-sandwich-list left right bottom (* size bezier-height) (* size bezier-thick)))
254             (barre-stencil (if (eq? barre-type 'straight)
255                               (ly:make-stencil (list 'draw-line (* size dot-radius) left dot-center-y right dot-center-y)
256                                                (cons left right)
257                                                (cons (- dot-center-y scale-dot-radius) (+ dot-center-y scale-dot-radius))) 
258                               (ly:make-stencil (list 'bezier-sandwich `(quote ,bezier-list) (* size bezier-thick) )
259                                   (cons left right)
260                                   (cons bottom (+ bottom (* size bezier-height)))))))
261         (if (not (null? (cdr barre-list)))
262             (ly:stencil-add barre-stencil
263                  (draw-barre paper props string-count fret-range size finger-code 
264                       dot-position dot-radius (cdr barre-list)))
265             barre-stencil ))))
266
267   
268 (define (stepmag mag)
269 "Calculate the font step necessary to get a desired magnification"
270 (* 6 (/ (log mag) (log 2))))
271
272 (define (label-fret paper props string-count fret-range size)
273    "Label the base fret on a fret diagram"
274    (let* ((base-fret (car fret-range))
275 ;          (label-font-mag (chain-assoc-get 'label-font-mag props 0.7))
276           (label-font-mag 0.7)
277 ;          (label-vertical-offset (chain-assoc-get 'fret-label-vertical-offset props -0.2))
278           (label-vertical-offset -0.2)
279           (fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
280           (font (ly:paper-get-font paper `(((font-encoding . ,my-font-encoding)(font-family . sans)
281                                             (font-series . medium) (font-shape . upright)
282                                             (font-size . ,(stepmag (* size label-font-mag))))))))
283        (ly:stencil-translate-axis (fontify-text font (format #f "~(~:@r~)" base-fret)) 
284                        (* size (+ fret-count label-vertical-offset)) Y)))
285  
286 (def-markup-command (fret-diagram-verbose paper props marking-list)
287   (list?)
288   "Make a fret diagram containing the symbols indicated in @var{marking-list}
289   
290   Syntax: \\fret-diagram   marking-list
291   
292   For example,
293   
294 @example
295    \\markup \\fret-diagram #'((mute 6) (mute 5) (open 4) (place-fret 3 2) (place-fret 2 3) (place-fret 1 2))
296 @end example 
297   
298   will produce a standard D chord diagram without fingering indications.
299   
300 Possible elements in @var{marking-list}:
301 @table @asis
302 @item (mute string-number)
303 Place a small 'x' at the top of string @var{string-number}
304
305 @item (open string-number)
306 Place a small 'o' at the top of string @var{string-number}
307
308 @item (barre start-string end-string fret-number)
309 Place a barre indicator (much like a tie) from string @var{start-string}to string @var{end-string} at fret @var{fret-number}
310
311 @item (place-fret string-number fret-number finger-value)
312 Place a fret playing indication on string @var{string-number} at fret @var{fret-number} with an optional 
313 fingering label @var{finger-value}.  By default, the fret playing indicator is a solid dot.  This can be
314 changed by setting the value of the variable @var{dot-color}.  If the @var{finger} 
315 part of the place-fret element is present, @var{finger-value} will be displayed according to the setting of the variable
316 @var{finger-code}.  There is no limit to the number of fret indications per string.
317 @end table
318 "
319    (make-fret-diagram paper props marking-list))
320    
321 (define (make-fret-diagram paper props marking-list)
322 " Make a fret diagram markup"
323   (let* (
324          ; note:  here we get items from props that are needed in this routine, or that are needed in more than one
325          ; of the procedures called from this routine.  If they're only used in one of the sub-procedure, they're 
326          ; obtained in that procedure
327          
328          (size (chain-assoc-get 'size props 1.0)) ; needed for everything
329 ;TODO -- get string-count directly from length of stringTunings; requires FretDiagram engraver, which is not yet available
330 ;TODO -- adjust padding for fret label?  it appears to be too close to dots
331          (string-count (chain-assoc-get 'string-count props 6)) ; needed for everything
332          (fret-count (chain-assoc-get 'fret-count props 4)) ; needed for everything
333          (finger-code (chain-assoc-get 'finger-code props 'none))  ; needed for both draw-dots and draw-barre
334          (default-dot-radius (if (eq? finger-code 'in-dot) 0.45 0.25))  ; bigger dots if labeled
335          (default-dot-position (if (eq? finger-code 'in-dot) (- 1 default-dot-radius) 0.6))  ; move up to make room for bigger if labeled
336          (dot-radius (chain-assoc-get 'dot-radius props default-dot-radius))  ; needed for both draw-dots and draw-barre
337          (dot-position (chain-assoc-get 'dot-position props default-dot-position)) ; needed for both draw-dots and draw-barre
338          (th (* (ly:paper-lookup paper 'linethickness)
339                 (chain-assoc-get 'thickness props 0.5))) ; needed for both draw-frets and draw-strings
340                 
341          (alignment (chain-assoc-get 'align-dir props -0.4)) ; needed only here
342 ;         (xo-padding (* th (chain-assoc-get 'padding props 2))) ; needed only here
343          (xo-padding (* th 2))
344
345          (parameters (fret-parse-marking-list marking-list fret-count))
346          (dot-list (cdr (assoc 'dot-list parameters)))
347          (xo-list (cdr (assoc 'xo-list parameters)))
348          (fret-range (cdr (assoc 'fret-range parameters)))
349          (barre-list (cdr (assoc 'barre-list parameters)))
350          (fret-diagram-stencil (ly:stencil-add
351                             (draw-strings string-count fret-range th size)
352                             (draw-frets paper props fret-range string-count th size))))
353          (if (not (null? barre-list))
354              (set! fret-diagram-stencil (ly:stencil-add
355                                     (draw-barre paper props string-count fret-range size finger-code  
356                                                 dot-position dot-radius barre-list)
357                                     fret-diagram-stencil)))
358          (if (not (null? dot-list))
359              (set! fret-diagram-stencil (ly:stencil-add
360                                     (draw-dots paper props string-count fret-range size finger-code 
361                                           dot-position dot-radius dot-list)
362                                     fret-diagram-stencil)))
363          (if (not (null? xo-list))
364              (set! fret-diagram-stencil (ly:stencil-combine-at-edge
365                                     fret-diagram-stencil Y UP
366                                     (draw-xo paper props string-count fret-range size xo-list) xo-padding 0)))
367          (if (> (car fret-range) 1) 
368              (set! fret-diagram-stencil
369                    (ly:stencil-combine-at-edge fret-diagram-stencil X RIGHT
370                                               (label-fret paper props string-count fret-range size) 0 0)))
371          (ly:stencil-align-to! fret-diagram-stencil X alignment)
372          fret-diagram-stencil))
373          
374 (def-markup-command (fret-diagram paper props definition-string)
375   (string?)
376   "Syntax: \\fret-diagram definition-string
377   
378 eg: \\markup \\fret-diagram #\"s:0.75;6-x;5-x;4-o;3-2;2-3;1-2;\"
379
380 for fret spacing 3/4 of staff space, D chord diagram
381
382 Syntax rules for @var{definition-string}:
383 @itemize @minus
384       
385 @item
386 Diagram items are separated by semicolons.
387
388 @item
389 Possible items:
390
391 @itemize @bullet
392 @item
393 s:number -- set the fret spacing of the diagram (in staff spaces). Default 1
394
395 @item
396 t:number -- set the line thickness (in staff spaces).  Default 0.05
397
398 @item
399 h:number -- set the height of the diagram in frets.  Default 4
400
401 @item
402 w:number -- set the width of the diagram in strings.  Default 6
403
404 @item
405 f:number -- set fingering label type (0 = none, 1 = in circle on string, 2 = below string)  Default 0
406
407 @item
408 d:number -- set radius of dot, in terms of fret spacing.  Default 0.25
409
410 @item
411 p:number -- set the position of the dot in the fret space. 0.5 is centered; 1 is on lower fret bar,
412 0 is on upper fret bar.  Default 0.6 
413
414 @item
415 c:string1-string2-fret -- include a barre mark from string1 to string2 on fret
416       
417 @item
418 string-fret -- place a dot on string at fret.  If fret is o, string is identified
419 as open.  If fret is x, string is identified as muted.
420
421 @item
422 string-fret-fingering -- place a dot on string at fret, and label with fingering as 
423 defined by f: code.
424
425 @end itemize
426
427 @item
428 Note:  There is no limit to the number of fret indications per string.
429 @end itemize
430     
431 "
432        (let ((definition-list (fret-parse-definition-string props definition-string)))
433        (make-fret-diagram paper (car definition-list) (cdr definition-list))))
434
435 (define (fret-parse-definition-string props definition-string)
436  "parse a fret diagram string and return a pair containing:
437   props, modified as necessary by the definition-string
438   a fret-indication list with the appropriate values"
439    (let* ((fret-count 4)
440           (string-count 6)
441           (fret-range (list 1 fret-count))
442           (barre-list '())
443           (dot-list '())
444           (xo-list '())
445           (output-list '())
446           (new-props '())
447           (items (string-split definition-string #\;)))
448       (let parse-item ((myitems items))
449           (if (not (null?  (cdr myitems))) 
450               (let ((test-string (car myitems)))
451                  (case (car (string->list (substring test-string 0 1))) 
452                     ((#\s) (let ((size (get-numeric-from-key test-string)))
453                                 (set! new-props (acons 'size size new-props))))
454                     ((#\f) (let* ((finger-code (get-numeric-from-key test-string))
455                                   (finger-id (case finger-code
456                                      ((0) 'none)
457                                      ((1) 'in-dot) 
458                                      ((2) 'below-string))))
459                                 (set! new-props
460                                    (acons 'finger-code finger-id new-props))))
461                     ((#\c) (set! output-list (cons-fret (cons 'barre (numerify (string-split (substring test-string 2) #\-)))
462                                             output-list)))
463                     ((#\h) (let ((fret-count (get-numeric-from-key test-string)))
464                                 (set! new-props (acons 'fret-count fret-count new-props))))
465                     ((#\w) (let ((string-count (get-numeric-from-key test-string)))
466                                 (set! new-props (acons 'string-count string-count new-props))))
467                     ((#\d) (let ((dot-size (get-numeric-from-key test-string)))
468                                 (set! new-props (acons 'dot-radius dot-size new-props))))
469                     ((#\p) (let ((dot-position (get-numeric-from-key test-string)))
470                                 (set! new-props (acons 'dot-position dot-position new-props))))
471                     (else 
472                        (let ((this-list (string-split test-string #\-)))
473                            (if (string->number (cadr this-list))
474                               (set! output-list (cons-fret (cons 'place-fret (numerify this-list)) output-list))
475                               (if (equal? (cadr this-list) "x" )
476                                   (set! output-list (cons-fret (list 'mute (string->number (car this-list))) output-list))
477                                   (set! output-list (cons-fret (list 'open (string->number (car this-list))) output-list)))))))
478                  (parse-item (cdr myitems)))))
479                  (if (eq? new-props '())
480                  `(,props . ,output-list)
481                  `(,(cons new-props props) . ,output-list))))
482
483 (define (cons-fret new-value old-list)
484 "  Put together a fret-list in the format desired by parse-string "
485   (if (eq? old-list '())
486       (list new-value)
487       (cons* new-value old-list)))
488                  
489 (define (get-numeric-from-key keystring)
490  "Get the numeric value from a key  of the form k:val"
491     (string->number (substring keystring 2  (string-length keystring) )))
492   
493 (define (numerify mylist)
494  "Convert string values to numeric or character"
495      (if (null? mylist)
496          '()
497          (let ((numeric-value (string->number (car mylist))))
498              (if numeric-value
499                 (cons* numeric-value (numerify (cdr mylist)))
500                 (cons* (car (string->list (car mylist))) (numerify (cdr mylist)))))))
501            
502 (def-markup-command (fret-diagram-terse paper props definition-string)
503   (string?)
504   "Make a fret diagram markup using terse string-based syntax.
505
506 Syntax: \\fret-diagram-terse definition-string
507
508 eg: \\markup \\fret-diagram #\"x;x;o;2;3;2;\" for a D chord diagram.
509
510 Syntax rules for @var{definition-string}:
511 @itemize @bullet
512
513 @item    
514 Strings are terminated by semicolons; the number of semicolons 
515 is the number of strings in the diagram.
516
517 @item
518 Mute strings are indicated by \"x\".
519
520 @item
521 Open strings are indicated by \"o\".
522
523 @item
524 A number indicates a fret indication at that fret.
525
526 @item
527 If there are multiple fret indicators desired on a string, they
528 should be separated by spaces.
529
530 @item
531 Fingerings are given by following the fret number with a \"-\",
532 followed by the finger indicator, e.g. 3-2 for playing the third
533 fret with the second finger.
534
535 @item
536 Where a barre indicator is desired, follow the fret (or fingering) symbol
537 with \"-(\" to start a barre and \"-)\" to end the barre.
538 @end itemize"
539 ;TODO -- change syntax to fret\string-finger
540        (let ((definition-list (fret-parse-terse-definition-string props definition-string)))
541        (make-fret-diagram paper (car definition-list) (cdr definition-list))))
542
543 (define (fret-parse-terse-definition-string props definition-string)
544  "parse a fret diagram string that uses terse syntax; return a pair containing:
545     props, modified to include the string-count determined by the definition-string
546     a fret-indication list with the appropriate values"
547 ;TODO -- change syntax to  fret\string-finger
548    (let* ((barre-start-list '())
549           (output-list '())
550           (new-props '())
551           (items (string-split definition-string #\;))
552           (string-count (- (length items) 1)))
553       (let parse-item ((myitems items))
554           (if (not (null?  (cdr myitems))) 
555               (let* ((test-string (car myitems))
556                     (current-string (- (length myitems) 1))
557                     (indicators (string-split test-string #\ )))
558                     (let parse-indicators ((myindicators indicators))
559                        (if (not (eq? '() myindicators))
560                            (let* ((this-list (string-split (car myindicators) #\-))
561                                   (max-element-index (- (length this-list) 1))
562                                   (last-element (car (list-tail this-list max-element-index)))
563                                   (fret (if (string->number (car this-list)) (string->number (car this-list)) (car this-list))))
564                                (if (equal? last-element "(")
565                                    (begin
566                                      (set! barre-start-list (cons-fret (list current-string fret) barre-start-list))
567                                      (set! this-list (list-head this-list max-element-index))))
568                                (if (equal? last-element ")")
569                                    (let* ((this-barre (get-sub-list fret barre-start-list))
570                                           (insert-index (- (length this-barre) 1)))
571                                      (set! output-list (cons-fret (cons* 'barre (car this-barre) current-string (cdr this-barre)) 
572                                                                   output-list))
573                                      (set! this-list (list-head this-list  max-element-index))))
574                                (if (number? fret)
575                                    (set! output-list (cons-fret (cons* 'place-fret current-string (drop-paren (numerify this-list))) output-list))
576                                    (if (equal? (car this-list) "x" )
577                                        (set! output-list (cons-fret (list 'mute current-string) output-list))
578                                        (set! output-list (cons-fret (list 'open current-string) output-list))))
579                                (parse-indicators (cdr myindicators)))))
580                  (parse-item (cdr myitems)))))
581                  (set! new-props (acons 'string-count string-count new-props))
582                  
583                  `(,(cons new-props props) . ,output-list)))
584
585 (define (drop-paren item-list)
586 " drop a final parentheses from a fret indication list resulting from a terse string specification of barre."
587      (if (> (length item-list) 0)
588          (let* ((max-index (- (length item-list) 1))
589             (last-element (car (list-tail item-list max-index))))
590             (if (or (equal? last-element ")") (equal? last-element "("))
591               (list-head item-list max-index) 
592               item-list))
593           item-list))
594           
595 (define (get-sub-list value master-list)
596 " Get a sub-list whose cadr is equal to @var{value} from @var{master-list}"
597     (if (eq? master-list '())
598       #f
599       (let ((sublist (car master-list)))
600            (if (equal? (cadr sublist) value)
601                sublist
602                (get-sub-list value (cdr master-list))))))