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