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