]> git.donarmstrong.com Git - lilypond.git/blob - scm/fret-diagrams.scm
Fix internalsrefs
[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 (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 (- (+ 2 (- (cadr fret-range) fret))dot-position) ))
255             (bottom (+  dot-center-y (* barre-vertical-offset scale-dot-radius)))
256             (left (* size (- string-count string1)))
257             (right (* size (- string-count string2)))
258 ;            (bezier-thick (chain-assoc-get 'bezier-thickness props 0.1))
259             (bezier-thick 0.1)
260 ;            (bezier-height (chain-assoc-get 'bezier-height props 0.5))
261             (bezier-height 0.5)
262             (bezier-list (make-bezier-sandwich-list left right bottom (* size bezier-height) (* size bezier-thick)))
263             (barre-stencil (if (eq? barre-type 'straight)
264                               (ly:make-stencil (list 'draw-line (* size dot-radius) left dot-center-y right dot-center-y)
265                                                (cons left right)
266                                                (cons (- dot-center-y scale-dot-radius) (+ dot-center-y scale-dot-radius))) 
267                               (ly:make-stencil (list 'bezier-sandwich `(quote ,bezier-list) (* size bezier-thick) )
268                                   (cons left right)
269                                   (cons bottom (+ bottom (* size bezier-height)))))))
270         (if (not (null? (cdr barre-list)))
271             (ly:stencil-add barre-stencil
272                  (draw-barre layout props string-count fret-range size finger-code 
273                       dot-position dot-radius (cdr barre-list)))
274             barre-stencil ))))
275
276   
277 (define (stepmag mag)
278 "Calculate the font step necessary to get a desired magnification"
279 (* 6 (/ (log mag) (log 2))))
280
281 (define (label-fret layout props string-count fret-range size)
282    "Label the base fret on a fret diagram"
283    (let* ((base-fret (car fret-range))
284 ;          (label-font-mag (chain-assoc-get 'label-font-mag props 0.7))
285           (label-font-mag 0.7)
286 ;          (label-vertical-offset (chain-assoc-get 'fret-label-vertical-offset props -0.2))
287           (label-vertical-offset -0.2)
288           (number-type (chain-assoc-get 'number-type props 'roman-lower))
289           (fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
290            (label-text 
291               (cond
292                ((equal?   number-type  'roman-lower) (format #f "~(~:@r~)" base-fret))
293                ((equal?  number-type 'roman-upper) (format #f "~:@r" base-fret))
294                ((equal? 'arabic number-type)  (format #f "~d" base-fret))
295                (else (format #f  "~(~:@r~)" base-fret)))))
296        (ly:stencil-translate-axis 
297            (sans-serif-stencil layout props (* size label-font-mag) label-text) 
298                        (* size (+ fret-count label-vertical-offset)) Y)))
299  
300 (def-markup-command (fret-diagram-verbose layout props marking-list)
301   (list?)
302   "Make a fret diagram containing the symbols indicated in @var{marking-list}
303   
304   For example,
305   
306 @example
307    \\markup \\fret-diagram #'((mute 6) (mute 5) (open 4)
308         (place-fret 3 2) (place-fret 2 3) (place-fret 1 2))
309 @end example 
310   
311   will produce a standard D chord diagram without fingering indications.
312   
313 Possible elements in @var{marking-list}:
314 @table @asis
315 @item (mute string-number)
316 Place a small 'x' at the top of string @var{string-number}
317
318 @item (open string-number)
319 Place a small 'o' at the top of string @var{string-number}
320
321 @item (barre start-string end-string fret-number)
322 Place a barre indicator (much like a tie) from string @var{start-string}to string @var{end-string} at fret @var{fret-number}
323
324 @item (place-fret string-number fret-number finger-value)
325 Place a fret playing indication on string @var{string-number} at fret @var{fret-number} with an optional 
326 fingering label @var{finger-value}.  By default, the fret playing indicator is a solid dot.  This can be
327 changed by setting the value of the variable @var{dot-color}.  If the @var{finger} 
328 part of the place-fret element is present, @var{finger-value} will be displayed according to the setting of the variable
329 @var{finger-code}.  There is no limit to the number of fret indications per string.
330 @end table
331 "
332    (make-fret-diagram layout props marking-list))
333    
334 (define (make-fret-diagram layout props marking-list)
335 " Make a fret diagram markup"
336   (let* (
337          ; note:  here we get items from props that are needed in this routine, or that are needed in more than one
338          ; of the procedures called from this routine.  If they're only used in one of the sub-procedure, they're 
339          ; obtained in that procedure
340          
341          (size (chain-assoc-get 'size props 1.0)) ; needed for everything
342 ;TODO -- get string-count directly from length of stringTunings; requires FretDiagram engraver, which is not yet available
343 ;TODO -- adjust padding for fret label?  it appears to be too close to dots
344          (string-count (chain-assoc-get 'string-count props 6)) ; needed for everything
345          (fret-count (chain-assoc-get 'fret-count props 4)) ; needed for everything
346          (finger-code (chain-assoc-get 'finger-code props 'none))  ; needed for both draw-dots and draw-barre
347          (default-dot-radius (if (eq? finger-code 'in-dot) 0.425 0.25))  ; bigger dots if labeled
348          (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
349          (dot-radius (chain-assoc-get 'dot-radius props default-dot-radius))  ; needed for both draw-dots and draw-barre
350          (dot-position (chain-assoc-get 'dot-position props default-dot-position)) ; needed for both draw-dots and draw-barre
351          (th (* (ly:output-def-lookup layout 'linethickness)
352                 (chain-assoc-get 'thickness props 0.5))) ; needed for both draw-frets and draw-strings
353                 
354          (alignment (chain-assoc-get 'align-dir props -0.4)) ; needed only here
355 ;         (xo-padding (* th (chain-assoc-get 'padding props 2))) ; needed only here
356          (label-space 0.25)
357          (xo-padding (* th 2))
358          (label-dir (chain-assoc-get 'label-dir props RIGHT))
359          (parameters (fret-parse-marking-list marking-list fret-count))
360          (dot-list (cdr (assoc 'dot-list parameters)))
361          (xo-list (cdr (assoc 'xo-list parameters)))
362          (fret-range (cdr (assoc 'fret-range parameters)))
363          (barre-list (cdr (assoc 'barre-list parameters)))
364          (fret-diagram-stencil (ly:stencil-add
365                             (draw-strings string-count fret-range th size)
366                             (draw-frets layout props fret-range string-count th size))))
367          (if (not (null? barre-list))
368              (set! fret-diagram-stencil (ly:stencil-add
369                                     (draw-barre layout props string-count fret-range size finger-code  
370                                                 dot-position dot-radius barre-list)
371                                     fret-diagram-stencil)))
372          (if (not (null? dot-list))
373              (set! fret-diagram-stencil (ly:stencil-add
374                                     (draw-dots layout props string-count fret-range size finger-code 
375                                           dot-position dot-radius dot-list)
376                                     fret-diagram-stencil)))
377          (if (not (null? xo-list))
378              (set! fret-diagram-stencil (ly:stencil-combine-at-edge
379                                     fret-diagram-stencil Y UP
380                                     (draw-xo layout props string-count fret-range size xo-list) xo-padding 0)))
381          (if (> (car fret-range) 1) 
382              (set! fret-diagram-stencil
383                    (ly:stencil-combine-at-edge fret-diagram-stencil X label-dir
384                                               (label-fret layout props string-count fret-range size) label-space 0)))
385          (ly:stencil-align-to! fret-diagram-stencil X alignment)
386          fret-diagram-stencil))
387          
388 (def-markup-command (fret-diagram layout props definition-string)
389   (string?)
390   "  
391 Example
392
393 @example
394  \\markup \\fret-diagram #\"s:0.75;6-x;5-x;4-o;3-2;2-3;1-2;\"
395 @end example
396
397 for fret spacing 3/4 of staff space, D chord diagram
398
399 Syntax rules for @var{definition-string}:
400 @itemize @minus
401       
402 @item
403 Diagram items are separated by semicolons.
404
405 @item
406 Possible items:
407
408 @itemize @bullet
409 @item
410 s:number -- set the fret spacing of the diagram (in staff spaces). Default 1
411
412 @item
413 t:number -- set the line thickness (in staff spaces).  Default 0.05
414
415 @item
416 h:number -- set the height of the diagram in frets.  Default 4
417
418 @item
419 w:number -- set the width of the diagram in strings.  Default 6
420
421 @item
422 f:number -- set fingering label type (0 = none, 1 = in circle on string, 2 = below string)  Default 0
423
424 @item
425 d:number -- set radius of dot, in terms of fret spacing.  Default 0.25
426
427 @item
428 p:number -- set the position of the dot in the fret space. 0.5 is centered; 1 is on lower fret bar,
429 0 is on upper fret bar.  Default 0.6 
430
431 @item
432 c:string1-string2-fret -- include a barre mark from string1 to string2 on fret
433       
434 @item
435 string-fret -- place a dot on string at fret.  If fret is o, string is identified
436 as open.  If fret is x, string is identified as muted.
437
438 @item
439 string-fret-fingering -- place a dot on string at fret, and label with fingering as 
440 defined by f: code.
441
442 @end itemize
443
444 @item
445 Note:  There is no limit to the number of fret indications per string.
446 @end itemize
447     
448 "
449        (let ((definition-list (fret-parse-definition-string props definition-string)))
450        (make-fret-diagram layout (car definition-list) (cdr definition-list))))
451
452 (define (fret-parse-definition-string props definition-string)
453  "parse a fret diagram string and return a pair containing:
454   props, modified as necessary by the definition-string
455   a fret-indication list with the appropriate values"
456    (let* ((fret-count 4)
457           (string-count 6)
458           (fret-range (list 1 fret-count))
459           (barre-list '())
460           (dot-list '())
461           (xo-list '())
462           (output-list '())
463           (new-props '())
464           (items (string-split definition-string #\;)))
465       (let parse-item ((myitems items))
466           (if (not (null?  (cdr myitems))) 
467               (let ((test-string (car myitems)))
468                  (case (car (string->list (substring test-string 0 1))) 
469                     ((#\s) (let ((size (get-numeric-from-key test-string)))
470                                 (set! new-props (acons 'size size new-props))))
471                     ((#\f) (let* ((finger-code (get-numeric-from-key test-string))
472                                   (finger-id (case finger-code
473                                      ((0) 'none)
474                                      ((1) 'in-dot) 
475                                      ((2) 'below-string))))
476                                 (set! new-props
477                                    (acons 'finger-code finger-id new-props))))
478                     ((#\c) (set! output-list (cons-fret (cons 'barre (numerify (string-split (substring test-string 2) #\-)))
479                                             output-list)))
480                     ((#\h) (let ((fret-count (get-numeric-from-key test-string)))
481                                 (set! new-props (acons 'fret-count fret-count new-props))))
482                     ((#\w) (let ((string-count (get-numeric-from-key test-string)))
483                                 (set! new-props (acons 'string-count string-count new-props))))
484                     ((#\d) (let ((dot-size (get-numeric-from-key test-string)))
485                                 (set! new-props (acons 'dot-radius dot-size new-props))))
486                     ((#\p) (let ((dot-position (get-numeric-from-key test-string)))
487                                 (set! new-props (acons 'dot-position dot-position new-props))))
488                     (else 
489                        (let ((this-list (string-split test-string #\-)))
490                            (if (string->number (cadr this-list))
491                               (set! output-list (cons-fret (cons 'place-fret (numerify this-list)) output-list))
492                               (if (equal? (cadr this-list) "x" )
493                                   (set! output-list (cons-fret (list 'mute (string->number (car this-list))) output-list))
494                                   (set! output-list (cons-fret (list 'open (string->number (car this-list))) output-list)))))))
495                  (parse-item (cdr myitems)))))
496                  (if (eq? new-props '())
497                  `(,props . ,output-list)
498                  `(,(cons new-props props) . ,output-list))))
499
500 (define (cons-fret new-value old-list)
501 "  Put together a fret-list in the format desired by parse-string "
502   (if (eq? old-list '())
503       (list new-value)
504       (cons* new-value old-list)))
505                  
506 (define (get-numeric-from-key keystring)
507  "Get the numeric value from a key  of the form k:val"
508     (string->number (substring keystring 2  (string-length keystring) )))
509   
510 (define (numerify mylist)
511  "Convert string values to numeric or character"
512      (if (null? mylist)
513          '()
514          (let ((numeric-value (string->number (car mylist))))
515              (if numeric-value
516                 (cons* numeric-value (numerify (cdr mylist)))
517                 (cons* (car (string->list (car mylist))) (numerify (cdr mylist)))))))
518            
519 (def-markup-command (fret-diagram-terse layout props definition-string)
520   (string?)
521   "Make a fret diagram markup using terse string-based syntax.
522
523 Example
524 @example
525  \\markup \\fret-diagram-terse #\"x;x;o;2;3;2;\" 
526 @end example
527 for a D chord diagram.
528
529 Syntax rules for @var{definition-string}:
530 @itemize @bullet
531
532 @item    
533 Strings are terminated by semicolons; the number of semicolons 
534 is the number of strings in the diagram.
535
536 @item
537 Mute strings are indicated by \"x\".
538
539 @item
540 Open strings are indicated by \"o\".
541
542 @item
543 A number indicates a fret indication at that fret.
544
545 @item
546 If there are multiple fret indicators desired on a string, they
547 should be separated by spaces.
548
549 @item
550 Fingerings are given by following the fret number with a \"-\",
551 followed by the finger indicator, e.g. 3-2 for playing the third
552 fret with the second finger.
553
554 @item
555 Where a barre indicator is desired, follow the fret (or fingering) symbol
556 with \"-(\" to start a barre and \"-)\" to end the barre.
557 @end itemize"
558 ;TODO -- change syntax to fret\string-finger
559        (let ((definition-list (fret-parse-terse-definition-string props definition-string)))
560        (make-fret-diagram layout (car definition-list) (cdr definition-list))))
561
562 (define (fret-parse-terse-definition-string props definition-string)
563  "parse a fret diagram string that uses terse syntax; return a pair containing:
564     props, modified to include the string-count determined by the definition-string
565     a fret-indication list with the appropriate values"
566 ;TODO -- change syntax to  fret\string-finger
567    (let* ((barre-start-list '())
568           (output-list '())
569           (new-props '())
570           (items (string-split definition-string #\;))
571           (string-count (- (length items) 1)))
572       (let parse-item ((myitems items))
573           (if (not (null?  (cdr myitems))) 
574               (let* ((test-string (car myitems))
575                     (current-string (- (length myitems) 1))
576                     (indicators (string-split test-string #\ )))
577                     (let parse-indicators ((myindicators indicators))
578                        (if (not (eq? '() myindicators))
579                            (let* ((this-list (string-split (car myindicators) #\-))
580                                   (max-element-index (- (length this-list) 1))
581                                   (last-element (car (list-tail this-list max-element-index)))
582                                   (fret (if (string->number (car this-list)) (string->number (car this-list)) (car this-list))))
583                                (if (equal? last-element "(")
584                                    (begin
585                                      (set! barre-start-list (cons-fret (list current-string fret) barre-start-list))
586                                      (set! this-list (list-head this-list max-element-index))))
587                                (if (equal? last-element ")")
588                                    (let* ((this-barre (get-sub-list fret barre-start-list))
589                                           (insert-index (- (length this-barre) 1)))
590                                      (set! output-list (cons-fret (cons* 'barre (car this-barre) current-string (cdr this-barre)) 
591                                                                   output-list))
592                                      (set! this-list (list-head this-list  max-element-index))))
593                                (if (number? fret)
594                                    (set! output-list (cons-fret (cons* 'place-fret current-string (drop-paren (numerify this-list))) output-list))
595                                    (if (equal? (car this-list) "x" )
596                                        (set! output-list (cons-fret (list 'mute current-string) output-list))
597                                        (set! output-list (cons-fret (list 'open current-string) output-list))))
598                                (parse-indicators (cdr myindicators)))))
599                  (parse-item (cdr myitems)))))
600                  (set! new-props (acons 'string-count string-count new-props))
601                  
602                  `(,(cons new-props props) . ,output-list)))
603
604 (define (drop-paren item-list)
605 " drop a final parentheses from a fret indication list resulting from a terse string specification of barre."
606      (if (> (length item-list) 0)
607          (let* ((max-index (- (length item-list) 1))
608             (last-element (car (list-tail item-list max-index))))
609             (if (or (equal? last-element ")") (equal? last-element "("))
610               (list-head item-list max-index) 
611               item-list))
612           item-list))
613           
614 (define (get-sub-list value master-list)
615 " Get a sub-list whose cadr is equal to @var{value} from @var{master-list}"
616     (if (eq? master-list '())
617       #f
618       (let ((sublist (car master-list)))
619            (if (equal? (cadr sublist) value)
620                sublist
621                (get-sub-list value (cdr master-list))))))