+ (let* ((base-fret (car fret-range))
+; (label-font-mag (chain-assoc-get 'label-font-mag props 0.7))
+ (label-font-mag 0.5)
+; (label-vertical-offset (chain-assoc-get 'fret-label-vertical-offset props -0.2))
+ (label-vertical-offset -0.2)
+ (number-type (chain-assoc-get 'number-type props 'roman-lower))
+ (fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
+ (label-text
+ (cond
+ ((equal? number-type 'roman-lower) (format #f "~(~:@r~)" base-fret))
+ ((equal? number-type 'roman-upper) (format #f "~:@r" base-fret))
+ ((equal? 'arabic number-type) (format #f "~d" base-fret))
+ (else (format #f "~(~:@r~)" base-fret)))))
+ (ly:stencil-translate-axis
+ (sans-serif-stencil layout props (* size label-font-mag) label-text)
+ (* size (+ fret-count label-vertical-offset)) Y)))
+
+(def-markup-command (fret-diagram-verbose layout props marking-list)
+ (list?)
+ "Make a fret diagram containing the symbols indicated in @var{marking-list}
+
+ For example,
+
+@example
+ \\markup \\fret-diagram #'((mute 6) (mute 5) (open 4)
+ (place-fret 3 2) (place-fret 2 3) (place-fret 1 2))
+@end example
+
+ will produce a standard D chord diagram without fingering indications.
+
+Possible elements in @var{marking-list}:
+@table @asis
+@item (mute string-number)
+Place a small 'x' at the top of string @var{string-number}
+
+@item (open string-number)
+Place a small 'o' at the top of string @var{string-number}
+
+@item (barre start-string end-string fret-number)
+Place a barre indicator (much like a tie) from string @var{start-string}to string @var{end-string} at fret @var{fret-number}
+
+@item (place-fret string-number fret-number finger-value)
+
+Place a fret playing indication on string @var{string-number} at fret
+@var{fret-number} with an optional fingering label @var{finger-value}.
+By default, the fret playing indicator is a solid dot. This can be
+changed by setting the value of the variable @var{dot-color}. If the
+@var{finger} part of the place-fret element is present,
+@var{finger-value} will be displayed according to the setting of the
+variable @var{finger-code}. There is no limit to the number of fret
+indications per string.
+
+@end table
+"
+ (make-fret-diagram layout props marking-list))
+
+(define (make-fret-diagram layout props marking-list)
+" Make a fret diagram markup"
+ (let* (
+ ; note: here we get items from props that are needed in this routine, or that are needed in more than one
+ ; of the procedures called from this routine. If they're only used in one of the sub-procedure, they're
+ ; obtained in that procedure
+
+ (size (chain-assoc-get 'size props 1.0)) ; needed for everything
+;TODO -- get string-count directly from length of stringTunings; requires FretDiagram engraver, which is not yet available
+;TODO -- adjust padding for fret label? it appears to be too close to dots
+ (string-count (chain-assoc-get 'string-count props 6)) ; needed for everything
+ (fret-count (chain-assoc-get 'fret-count props 4)) ; needed for everything
+ (finger-code (chain-assoc-get 'finger-code props 'none)) ; needed for both draw-dots and draw-barre
+ (default-dot-radius (if (eq? finger-code 'in-dot) 0.425 0.25)) ; bigger dots if labeled
+ (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
+ (dot-radius (chain-assoc-get 'dot-radius props default-dot-radius)) ; needed for both draw-dots and draw-barre
+ (dot-position (chain-assoc-get 'dot-position props default-dot-position)) ; needed for both draw-dots and draw-barre
+ (th (* (ly:output-def-lookup layout 'linethickness)
+ (chain-assoc-get 'thickness props 0.5))) ; needed for both draw-frets and draw-strings
+
+ (alignment (chain-assoc-get 'align-dir props -0.4)) ; needed only here
+; (xo-padding (* th (chain-assoc-get 'padding props 2))) ; needed only here
+ (label-space (* 0.25 size))
+ (xo-padding (* th size 5))
+ (label-dir (chain-assoc-get 'label-dir props RIGHT))
+ (parameters (fret-parse-marking-list marking-list fret-count))
+ (dot-list (cdr (assoc 'dot-list parameters)))
+ (xo-list (cdr (assoc 'xo-list parameters)))
+ (fret-range (cdr (assoc 'fret-range parameters)))
+ (barre-list (cdr (assoc 'barre-list parameters)))
+ (fret-diagram-stencil (ly:stencil-add
+ (draw-strings string-count fret-range th size)
+ (draw-frets layout props fret-range string-count th size))))
+ (if (not (null? barre-list))
+ (set! fret-diagram-stencil (ly:stencil-add
+ (draw-barre layout props string-count fret-range size finger-code
+ dot-position dot-radius barre-list)
+ fret-diagram-stencil)))
+ (if (not (null? dot-list))
+ (set! fret-diagram-stencil (ly:stencil-add
+ fret-diagram-stencil
+ (draw-dots layout props string-count fret-range size finger-code
+ dot-position dot-radius th dot-list))))
+ (if (not (null? xo-list))
+ (set! fret-diagram-stencil (ly:stencil-combine-at-edge
+ fret-diagram-stencil Y UP
+ (draw-xo layout props string-count fret-range size xo-list) xo-padding 0)))
+ (if (> (car fret-range) 1)
+ (set! fret-diagram-stencil
+ (ly:stencil-combine-at-edge fret-diagram-stencil X label-dir
+ (label-fret layout props string-count fret-range size) label-space 0)))
+ (ly:stencil-aligned-to fret-diagram-stencil X alignment)
+ ))
+
+(def-markup-command (fret-diagram layout props definition-string)
+ (string?)
+ "
+Example
+
+@example
+ \\markup \\fret-diagram #\"s:0.75;6-x;5-x;4-o;3-2;2-3;1-2;\"
+@end example
+
+for fret spacing 3/4 of staff space, D chord diagram
+
+Syntax rules for @var{definition-string}:
+@itemize @minus
+
+@item
+Diagram items are separated by semicolons.
+
+@item
+Possible items:
+
+@itemize @bullet
+@item
+s:number -- set the fret spacing of the diagram (in staff spaces). Default 1
+
+@item
+t:number -- set the line thickness (in staff spaces). Default 0.05
+
+@item
+h:number -- set the height of the diagram in frets. Default 4
+
+@item
+w:number -- set the width of the diagram in strings. Default 6
+
+@item
+f:number -- set fingering label type (0 = none, 1 = in circle on string, 2 = below string) Default 0
+
+@item
+d:number -- set radius of dot, in terms of fret spacing. Default 0.25
+
+@item
+p:number -- set the position of the dot in the fret space. 0.5 is centered; 1 is on lower fret bar,
+0 is on upper fret bar. Default 0.6
+
+@item
+c:string1-string2-fret -- include a barre mark from string1 to string2 on fret
+
+@item
+string-fret -- place a dot on string at fret. If fret is o, string is identified
+as open. If fret is x, string is identified as muted.
+
+@item
+string-fret-fingering -- place a dot on string at fret, and label with fingering as
+defined by f: code.
+
+@end itemize
+
+@item
+Note: There is no limit to the number of fret indications per string.
+@end itemize
+
+"
+ (let ((definition-list (fret-parse-definition-string props definition-string)))
+ (make-fret-diagram layout (car definition-list) (cdr definition-list))))
+
+(define (fret-parse-definition-string props definition-string)
+ "parse a fret diagram string and return a pair containing:
+ props, modified as necessary by the definition-string
+ a fret-indication list with the appropriate values"
+ (let* ((fret-count 4)
+ (string-count 6)
+ (fret-range (list 1 fret-count))
+ (barre-list '())
+ (dot-list '())
+ (xo-list '())
+ (output-list '())
+ (new-props '())
+ (items (string-split definition-string #\;)))
+ (let parse-item ((myitems items))
+ (if (not (null? (cdr myitems)))
+ (let ((test-string (car myitems)))
+ (case (car (string->list (substring test-string 0 1)))
+ ((#\s) (let ((size (get-numeric-from-key test-string)))
+ (set! new-props (acons 'size size new-props))))
+ ((#\f) (let* ((finger-code (get-numeric-from-key test-string))
+ (finger-id (case finger-code
+ ((0) 'none)
+ ((1) 'in-dot)
+ ((2) 'below-string))))
+ (set! new-props
+ (acons 'finger-code finger-id new-props))))
+ ((#\c) (set! output-list (cons-fret (cons 'barre (numerify (string-split (substring test-string 2) #\-)))
+ output-list)))
+ ((#\h) (let ((fret-count (get-numeric-from-key test-string)))
+ (set! new-props (acons 'fret-count fret-count new-props))))
+ ((#\w) (let ((string-count (get-numeric-from-key test-string)))
+ (set! new-props (acons 'string-count string-count new-props))))
+ ((#\d) (let ((dot-size (get-numeric-from-key test-string)))
+ (set! new-props (acons 'dot-radius dot-size new-props))))
+ ((#\p) (let ((dot-position (get-numeric-from-key test-string)))
+ (set! new-props (acons 'dot-position dot-position new-props))))
+ (else
+ (let ((this-list (string-split test-string #\-)))
+ (if (string->number (cadr this-list))
+ (set! output-list (cons-fret (cons 'place-fret (numerify this-list)) output-list))
+ (if (equal? (cadr this-list) "x" )
+ (set! output-list (cons-fret (list 'mute (string->number (car this-list))) output-list))
+ (set! output-list (cons-fret (list 'open (string->number (car this-list))) output-list)))))))
+ (parse-item (cdr myitems)))))
+ (if (eq? new-props '())
+ `(,props . ,output-list)
+ `(,(cons new-props props) . ,output-list))))
+
+(define (cons-fret new-value old-list)
+" Put together a fret-list in the format desired by parse-string "
+ (if (eq? old-list '())
+ (list new-value)
+ (cons* new-value old-list)))
+