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