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