]> git.donarmstrong.com Git - lilypond.git/blob - scm/fret-diagrams.scm
* scm/framework-tex.scm (output-preview-framework)
[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 (define ss-font-encoding 'ec )
9
10 (define (fret-parse-marking-list marking-list fret-count)
11    (let* ((fret-range (list 1 fret-count))
12           (barre-list '())
13           (dot-list '())
14           (xo-list '())
15           (output-alist '()))
16       (let parse-item ((mylist marking-list))
17           (if (not (null? mylist))
18               (let* ((my-item (car mylist)) (my-code (car my-item)))
19                  (cond
20                      ((or (eq? my-code 'open)(eq? my-code 'mute))
21                         (set! xo-list (cons* my-item xo-list)))
22                      ((eq? my-code 'barre)
23                         (set! barre-list (cons* (cdr my-item) barre-list)))
24                      ((eq? my-code 'place-fret)
25                         (set! dot-list (cons* (cdr my-item) dot-list))))
26                  (parse-item (cdr mylist)))))
27                ; calculate fret-range
28                (let ((maxfret 0) (minfret 99))
29                     (let updatemax ((fret-list dot-list))
30                         (if (null?  fret-list)
31                            '()
32                            (let ((fretval (second (car fret-list))))
33                                (if (> fretval maxfret) (set! maxfret fretval))
34                                (if (< fretval minfret) (set! minfret fretval))
35                                (updatemax (cdr fret-list)))))
36                     (if (> maxfret fret-count)
37                         (set! fret-range (list minfret
38                              (let ((upfret (- (+ minfret fret-count) 1)))
39                                   (if (> maxfret upfret) maxfret upfret)))))
40                     ; subtract fret from dots
41                     (set! dot-list (subtract-base-fret (- (car fret-range) 1) dot-list)))
42                (acons 'fret-range fret-range
43                (acons 'barre-list barre-list
44                (acons 'dot-list dot-list
45                (acons 'xo-list xo-list '()))))))
46    
47 (define (subtract-base-fret base-fret dot-list)
48 "Subtract @var{base-fret} from every fret in @var{dot-list}"  
49   (if (null? dot-list)
50       '()
51       (let ((this-list (car dot-list)))
52       (cons* (list (car this-list) (- (second this-list) base-fret) (if (null? (cddr this-list))
53                                                                     '()
54                                                                     (third this-list)))
55              (subtract-base-fret base-fret (cdr dot-list))))))
56
57 (define (draw-strings string-count fret-range th size)
58 "Draw the strings (vertical lines) for a fret diagram with @var{string-count} strings and frets as indicated
59    in @var{fret-range}.  Line thickness is given by @var{th}, fret & string spacing by @var{size}. "
60   (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
61          (sl (* (+ fret-count 1) size))
62          (sth (* size th))
63          (half-thickness (* sth 0.5))
64          (gap (- size sth))
65          (string-stencil (ly:make-stencil (list 'draw-line sth 0 0 0 sl)
66                          (cons (- half-thickness) half-thickness)
67                          (cons (- half-thickness) (+ sl half-thickness)))))
68     (if (= string-count 1)
69          string-stencil
70         (ly:stencil-combine-at-edge
71          (draw-strings (- string-count 1) fret-range th size) X RIGHT
72          string-stencil
73          gap 0))))
74
75 (define (draw-fret-lines fret-count string-count th size)
76  "Draw @var{fret-count} frets (horizontal lines) for a fret diagram with @var{string-count} strings.
77    Line thickness is given by @var{th}, fret & string spacing by @var{size}. "
78    (let* ((fret-length (* (- string-count 1) size))
79           (sth (* size th))
80           (half-thickness (* sth 0.5))
81           (gap (- size sth))
82           (fret-line (ly:make-stencil (list 'draw-line sth half-thickness size (- fret-length half-thickness) size)
83                           (cons 0 fret-length)
84                           (cons (- size half-thickness) (+  size half-thickness)))))
85        (if (= fret-count 1)
86          fret-line
87          (ly:stencil-combine-at-edge fret-line Y UP
88           (draw-fret-lines (- fret-count 1) string-count th size)
89           gap 0))))
90           
91 (define (draw-thick-top-fret props string-count th size)
92  "Draw a thick top fret for a fret diagram whose base fret is not 1."
93    (let* ((sth (* th size))
94 ;          (top-fret-thick (* sth (chain-assoc-get 'top-fret-thickness props 3.0)))
95           (top-fret-thick (* sth 3.0))
96 ;          (top-half-thick (* top-fret-thick 0.5))
97           (half-thick (* sth 0.5))
98           (x1 half-thick)
99           (x2 (+ half-thick (* size (- string-count 1))))
100           (y1 (- half-thick))
101           (y2 (+ top-fret-thick half-thick))
102           (x-extent (cons (- x1) x2))
103           (y-extent (cons 0 y2)))
104           (ly:make-stencil (list 'round-filled-box x1 x2 y1 y2 sth)
105                             x-extent y-extent)))           
106  
107  
108 (define (draw-frets paper props fret-range string-count th size)
109  "Draw the frets (horizontal lines) for a fret diagram with @var{string-count} strings and frets as indicated
110    in @var{fret-range}.  Line thickness is given by @var{th}, fret & string spacing by @var{size}. "
111   (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
112          (fret-length (* (- string-count 1) size))
113          (half-thickness (* th 0.5))
114          (base-fret (car fret-range)))
115        (ly:stencil-combine-at-edge
116           (draw-fret-lines fret-count string-count th size) Y UP
117              (if (= base-fret 1)
118                  (draw-thick-top-fret props string-count th size)
119                  (draw-fret-lines 1 string-count th size)) 
120                  (- size th) 0))) 
121                  
122
123 (define (centered-stencil stencil)
124  "Center stencil @var{stencil} in both the X and Y directions"
125  (let* ((output-stencil stencil))
126 ;     (if (= (cadr (ly:version)) 3)
127          (begin
128            (ly:stencil-align-to! output-stencil Y 0)
129            (ly:stencil-align-to! output-stencil X 0)
130            output-stencil)))
131 ;        (ly:stencil-align-to (ly:stencil-align-to text-stencil X 0) Y 0))))
132
133 (define (draw-dots paper props string-count fret-range size finger-code dot-position dot-radius dot-list)
134   "Make dots for fret diagram."
135   (let* ((scale-dot-radius (* size dot-radius))
136          (dot-color (chain-assoc-get 'dot-color props 'black))
137 ;         (finger-xoffset (chain-assoc-get 'finger-xoffset props -0.25))
138 ;         (finger-yoffset (chain-assoc-get 'finger-yoffset props (- size)))
139          (finger-xoffset -0.25)
140          (finger-yoffset (- size))
141 ;         (dot-label-font-mag (* scale-dot-radius (chain-assoc-get 'dot-label-font-mag props 1.0)))
142          (dot-label-font-mag scale-dot-radius)
143 ;         (string-label-font-mag (* size (chain-assoc-get 'label-font-mag props 0.7)))
144          (string-label-font-mag (* size 0.7))
145          (fret-count (+ (- (cadr fret-range) (car fret-range) 1)))
146          (mypair (car dot-list))
147          (restlist (cdr dot-list))
148          (xpos (* size (- string-count (car mypair))))
149 ;TODO -- figure out what 4 is and get rid of it
150 ;UGH -- 4?
151          (ypos (* size (+ 4 (- fret-count (cadr mypair) dot-position ))))
152          (extent (cons (- scale-dot-radius) scale-dot-radius))
153          (finger (caddr mypair))
154          (finger (if (number? finger) (number->string finger) finger))
155           (string-label-font 
156                (ly:paper-get-font paper `(((font-family . sans)
157                                            (font-encoding . ,ss-font-encoding)
158                                            (font-series . medium) 
159                                            (font-shape . upright)
160                                            (font-size . ,(stepmag  string-label-font-mag))))))
161           (dot-label-font 
162                (ly:paper-get-font paper `(((font-family . sans)
163                                            (font-encoding . ,ss-font-encoding)
164                                            (font-series . medium) 
165                                            (font-shape . upright)
166                                            (font-size . ,(stepmag  dot-label-font-mag))))))
167          (dotstencil  (if (eq? dot-color 'white)
168                           (begin
169                           (ly:make-stencil (list 'white-dot 0 0 scale-dot-radius) extent extent))
170                           (ly:make-stencil (list 'dot 0 0 scale-dot-radius ) extent extent)))
171          (positioned-dot (ly:stencil-translate-axis
172                            (ly:stencil-translate-axis dotstencil xpos X)
173                            ypos Y))
174          (labeled-dot-stencil 
175                  (if (or (eq? finger '())(eq? finger-code 'none))
176                      positioned-dot
177                  (if (eq? finger-code 'in-dot)
178                     (let*  ((dot-proc (if (eq? dot-color 'white) 'white-dot 'dot)))
179                      (ly:stencil-add 
180                         (ly:stencil-translate-axis 
181                           (ly:stencil-translate-axis 
182                               (if (eq? dot-color 'white)
183                               (centered-stencil (fontify-text dot-label-font finger))
184                               (centered-stencil (fontify-text-white dot-label-font-mag 
185                                                                     dot-label-font  finger)))
186                                xpos X)
187                               ypos Y)
188                         (ly:stencil-translate-axis
189                            (ly:stencil-translate-axis 
190                               (ly:make-stencil (list dot-proc 0 0 scale-dot-radius) extent extent)
191                                xpos X)
192                            ypos Y)))
193                  (if (eq? finger-code 'below-string) 
194                      (ly:stencil-add 
195                          positioned-dot
196                          (ly:stencil-translate-axis 
197                              (ly:stencil-translate-axis 
198                                  (centered-stencil (fontify-text string-label-font finger)) xpos  X)
199                              (* size finger-yoffset) Y))
200                      ;unknown finger-code
201                      positioned-dot)))))
202     (if (null? restlist) 
203         labeled-dot-stencil
204         (ly:stencil-add 
205             (draw-dots paper props string-count fret-range size finger-code 
206                           dot-position dot-radius restlist)
207             labeled-dot-stencil))))
208
209 (define (draw-xo paper props string-count fret-range size xo-list) 
210 "Put open and mute string indications on diagram, as contained in @var{xo-list}."
211     (let* ((fret-count (+ (- (cadr fret-range) (car fret-range) 1)))
212 ;           (xo-font-mag (* size (chain-assoc-get 'xo-font-magnification props 0.5)))
213            (xo-font-mag (* size 0.5))
214 ;           (xo-horizontal-offset (* size (chain-assoc-get 'xo-horizontal-offset props -0.35)))
215            (xo-horizontal-offset (* size -0.35))
216            (font (ly:paper-get-font paper `(((font-encoding . ,ss-font-encoding)(font-family . sans)
217                                         (font-series . medium) (font-shape . upright)
218                                         (font-size . ,(stepmag (* size xo-font-mag)))))))
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 (fontify-text font glyph-string) xpos X)))
224       (if (null? restlist)
225           glyph-stencil
226           (ly:stencil-add
227             (draw-xo paper props string-count fret-range size restlist)
228             glyph-stencil))))
229
230 (define (make-bezier-sandwich-list left right bottom height thickness)
231 " Make the argument list for a horizontal bezier sandwich from @var{left} to @var{right} with a bottom at @var{bottom}, 
232   a height of @var{height}, and a thickness of @var{thickness}."
233    (let* ((width (+ (- right left) 1))
234           (x1 (+ (* width thickness) left))
235           (x2 (- right (* width thickness)))
236           (bottom-control-point-height (+ bottom (- height thickness)))
237           (top-control-point-height (+ bottom height)))
238            ; order of points is: left cp low, right cp low, right end low, left end low
239            ;                     right cp high, left cp high, left end high, right end high.
240        (list (cons x1 bottom-control-point-height) (cons x2 bottom-control-point-height) (cons right bottom) (cons left bottom)
241              (cons x2 top-control-point-height) (cons x1 top-control-point-height) (cons left bottom) (cons right bottom))))
242
243 (define (draw-barre paper props string-count fret-range size finger-code dot-position dot-radius barre-list)
244    "Create barre indications for a fret diagram"
245    (if (not (null? barre-list))
246      (let* ((string1 (caar barre-list))
247             (string2 (cadar barre-list))
248             (fret    (caddar barre-list))
249             (barre-type (chain-assoc-get 'barre-type props 'curved))
250             (scale-dot-radius (* size dot-radius))
251             (barre-vertical-offset (chain-assoc-get 'barre-vertical-offset props 0.5))
252             ; 2 is 1 for empty fret at bottom of figure + 1 for interval (top-fret - fret + 1) -- not an arbitrary constant
253             (dot-center-y (* size (- (+ 2 (- (cadr fret-range) fret))dot-position) ))
254             (bottom (+  dot-center-y (* barre-vertical-offset scale-dot-radius)))
255             (left (* size (- string-count string1)))
256             (right (* size (- string-count string2)))
257 ;            (bezier-thick (chain-assoc-get 'bezier-thickness props 0.1))
258             (bezier-thick 0.1)
259 ;            (bezier-height (chain-assoc-get 'bezier-height props 0.5))
260             (bezier-height 0.5)
261             (bezier-list (make-bezier-sandwich-list left right bottom (* size bezier-height) (* size bezier-thick)))
262             (barre-stencil (if (eq? barre-type 'straight)
263                               (ly:make-stencil (list 'draw-line (* size dot-radius) left dot-center-y right dot-center-y)
264                                                (cons left right)
265                                                (cons (- dot-center-y scale-dot-radius) (+ dot-center-y scale-dot-radius))) 
266                               (ly:make-stencil (list 'bezier-sandwich `(quote ,bezier-list) (* size bezier-thick) )
267                                   (cons left right)
268                                   (cons bottom (+ bottom (* size bezier-height)))))))
269         (if (not (null? (cdr barre-list)))
270             (ly:stencil-add barre-stencil
271                  (draw-barre paper props string-count fret-range size finger-code 
272                       dot-position dot-radius (cdr barre-list)))
273             barre-stencil ))))
274
275   
276 (define (stepmag mag)
277 "Calculate the font step necessary to get a desired magnification"
278 (* 6 (/ (log mag) (log 2))))
279
280 (define (label-fret paper props string-count fret-range size)
281    "Label the base fret on a fret diagram"
282    (let* ((base-fret (car fret-range))
283 ;          (label-font-mag (chain-assoc-get 'label-font-mag props 0.7))
284           (label-font-mag 0.7)
285 ;          (label-vertical-offset (chain-assoc-get 'fret-label-vertical-offset props -0.2))
286           (label-vertical-offset -0.2)
287           (number-type (chain-assoc-get 'number-type props 'roman-lower))
288           (fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
289           (font (ly:paper-get-font paper `(((font-encoding . ,ss-font-encoding)
290                                             (font-family . sans)
291                                             (font-series . medium) 
292                                             (font-shape . upright)
293                                             (font-size . ,(stepmag (* size label-font-mag)))))))
294            (label-text 
295               (case number-type 
296                   ('roman-lower (format #f "~(~:@r~)" base-fret))
297                   ('roman-upper (format #f "~:@r" base-fret))
298                   ('arabic  (format #f "~d" base-fret))
299                   (else (format #f  "~(~:@r~)" base-fret)))))
300        (ly:stencil-translate-axis 
301            (fontify-text font label-text) 
302                        (* size (+ fret-count label-vertical-offset)) Y)))
303  
304 (def-markup-command (fret-diagram-verbose paper props marking-list)
305   (list?)
306   "Make a fret diagram containing the symbols indicated in @var{marking-list}
307   
308   Syntax: \\fret-diagram   marking-list
309   
310   For example,
311   
312 @example
313    \\markup \\fret-diagram #'((mute 6) (mute 5) (open 4)
314         (place-fret 3 2) (place-fret 2 3) (place-fret 1 2))
315 @end example 
316   
317   will produce a standard D chord diagram without fingering indications.
318   
319 Possible elements in @var{marking-list}:
320 @table @asis
321 @item (mute string-number)
322 Place a small 'x' at the top of string @var{string-number}
323
324 @item (open string-number)
325 Place a small 'o' at the top of string @var{string-number}
326
327 @item (barre start-string end-string fret-number)
328 Place a barre indicator (much like a tie) from string @var{start-string}to string @var{end-string} at fret @var{fret-number}
329
330 @item (place-fret string-number fret-number finger-value)
331 Place a fret playing indication on string @var{string-number} at fret @var{fret-number} with an optional 
332 fingering label @var{finger-value}.  By default, the fret playing indicator is a solid dot.  This can be
333 changed by setting the value of the variable @var{dot-color}.  If the @var{finger} 
334 part of the place-fret element is present, @var{finger-value} will be displayed according to the setting of the variable
335 @var{finger-code}.  There is no limit to the number of fret indications per string.
336 @end table
337 "
338    (make-fret-diagram paper props marking-list))
339    
340 (define (make-fret-diagram paper props marking-list)
341 " Make a fret diagram markup"
342   (let* (
343          ; note:  here we get items from props that are needed in this routine, or that are needed in more than one
344          ; of the procedures called from this routine.  If they're only used in one of the sub-procedure, they're 
345          ; obtained in that procedure
346          
347          (size (chain-assoc-get 'size props 1.0)) ; needed for everything
348 ;TODO -- get string-count directly from length of stringTunings; requires FretDiagram engraver, which is not yet available
349 ;TODO -- adjust padding for fret label?  it appears to be too close to dots
350          (string-count (chain-assoc-get 'string-count props 6)) ; needed for everything
351          (fret-count (chain-assoc-get 'fret-count props 4)) ; needed for everything
352          (finger-code (chain-assoc-get 'finger-code props 'none))  ; needed for both draw-dots and draw-barre
353          (default-dot-radius (if (eq? finger-code 'in-dot) 0.425 0.25))  ; bigger dots if labeled
354          (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
355          (dot-radius (chain-assoc-get 'dot-radius props default-dot-radius))  ; needed for both draw-dots and draw-barre
356          (dot-position (chain-assoc-get 'dot-position props default-dot-position)) ; needed for both draw-dots and draw-barre
357          (th (* (ly:paper-lookup paper 'linethickness)
358                 (chain-assoc-get 'thickness props 0.5))) ; needed for both draw-frets and draw-strings
359                 
360          (alignment (chain-assoc-get 'align-dir props -0.4)) ; needed only here
361 ;         (xo-padding (* th (chain-assoc-get 'padding props 2))) ; needed only here
362          (label-space 0.25)
363          (xo-padding (* th 2))
364          (label-dir (chain-assoc-get 'label-dir props RIGHT))
365          (parameters (fret-parse-marking-list marking-list fret-count))
366          (dot-list (cdr (assoc 'dot-list parameters)))
367          (xo-list (cdr (assoc 'xo-list parameters)))
368          (fret-range (cdr (assoc 'fret-range parameters)))
369          (barre-list (cdr (assoc 'barre-list parameters)))
370          (fret-diagram-stencil (ly:stencil-add
371                             (draw-strings string-count fret-range th size)
372                             (draw-frets paper props fret-range string-count th size))))
373          (if (not (null? barre-list))
374              (set! fret-diagram-stencil (ly:stencil-add
375                                     (draw-barre paper props string-count fret-range size finger-code  
376                                                 dot-position dot-radius barre-list)
377                                     fret-diagram-stencil)))
378          (if (not (null? dot-list))
379              (set! fret-diagram-stencil (ly:stencil-add
380                                     (draw-dots paper props string-count fret-range size finger-code 
381                                           dot-position dot-radius dot-list)
382                                     fret-diagram-stencil)))
383          (if (not (null? xo-list))
384              (set! fret-diagram-stencil (ly:stencil-combine-at-edge
385                                     fret-diagram-stencil Y UP
386                                     (draw-xo paper props string-count fret-range size xo-list) xo-padding 0)))
387          (if (> (car fret-range) 1) 
388              (set! fret-diagram-stencil
389                    (ly:stencil-combine-at-edge fret-diagram-stencil X label-dir
390                                               (label-fret paper props string-count fret-range size) label-space 0)))
391          (ly:stencil-align-to! fret-diagram-stencil X alignment)
392          fret-diagram-stencil))
393          
394 (def-markup-command (fret-diagram paper props definition-string)
395   (string?)
396   "Syntax: \\fret-diagram definition-string
397   
398 eg: \\markup \\fret-diagram #\"s:0.75;6-x;5-x;4-o;3-2;2-3;1-2;\"
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 paper (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 paper props definition-string)
523   (string?)
524   "Make a fret diagram markup using terse string-based syntax.
525
526 Syntax: \\fret-diagram-terse definition-string
527
528 eg: \\markup \\fret-diagram #\"x;x;o;2;3;2;\" for a D chord diagram.
529
530 Syntax rules for @var{definition-string}:
531 @itemize @bullet
532
533 @item    
534 Strings are terminated by semicolons; the number of semicolons 
535 is the number of strings in the diagram.
536
537 @item
538 Mute strings are indicated by \"x\".
539
540 @item
541 Open strings are indicated by \"o\".
542
543 @item
544 A number indicates a fret indication at that fret.
545
546 @item
547 If there are multiple fret indicators desired on a string, they
548 should be separated by spaces.
549
550 @item
551 Fingerings are given by following the fret number with a \"-\",
552 followed by the finger indicator, e.g. 3-2 for playing the third
553 fret with the second finger.
554
555 @item
556 Where a barre indicator is desired, follow the fret (or fingering) symbol
557 with \"-(\" to start a barre and \"-)\" to end the barre.
558 @end itemize"
559 ;TODO -- change syntax to fret\string-finger
560        (let ((definition-list (fret-parse-terse-definition-string props definition-string)))
561        (make-fret-diagram paper (car definition-list) (cdr definition-list))))
562
563 (define (fret-parse-terse-definition-string props definition-string)
564  "parse a fret diagram string that uses terse syntax; return a pair containing:
565     props, modified to include the string-count determined by the definition-string
566     a fret-indication list with the appropriate values"
567 ;TODO -- change syntax to  fret\string-finger
568    (let* ((barre-start-list '())
569           (output-list '())
570           (new-props '())
571           (items (string-split definition-string #\;))
572           (string-count (- (length items) 1)))
573       (let parse-item ((myitems items))
574           (if (not (null?  (cdr myitems))) 
575               (let* ((test-string (car myitems))
576                     (current-string (- (length myitems) 1))
577                     (indicators (string-split test-string #\ )))
578                     (let parse-indicators ((myindicators indicators))
579                        (if (not (eq? '() myindicators))
580                            (let* ((this-list (string-split (car myindicators) #\-))
581                                   (max-element-index (- (length this-list) 1))
582                                   (last-element (car (list-tail this-list max-element-index)))
583                                   (fret (if (string->number (car this-list)) (string->number (car this-list)) (car this-list))))
584                                (if (equal? last-element "(")
585                                    (begin
586                                      (set! barre-start-list (cons-fret (list current-string fret) barre-start-list))
587                                      (set! this-list (list-head this-list max-element-index))))
588                                (if (equal? last-element ")")
589                                    (let* ((this-barre (get-sub-list fret barre-start-list))
590                                           (insert-index (- (length this-barre) 1)))
591                                      (set! output-list (cons-fret (cons* 'barre (car this-barre) current-string (cdr this-barre)) 
592                                                                   output-list))
593                                      (set! this-list (list-head this-list  max-element-index))))
594                                (if (number? fret)
595                                    (set! output-list (cons-fret (cons* 'place-fret current-string (drop-paren (numerify this-list))) output-list))
596                                    (if (equal? (car this-list) "x" )
597                                        (set! output-list (cons-fret (list 'mute current-string) output-list))
598                                        (set! output-list (cons-fret (list 'open current-string) output-list))))
599                                (parse-indicators (cdr myindicators)))))
600                  (parse-item (cdr myitems)))))
601                  (set! new-props (acons 'string-count string-count new-props))
602                  
603                  `(,(cons new-props props) . ,output-list)))
604
605 (define (drop-paren item-list)
606 " drop a final parentheses from a fret indication list resulting from a terse string specification of barre."
607      (if (> (length item-list) 0)
608          (let* ((max-index (- (length item-list) 1))
609             (last-element (car (list-tail item-list max-index))))
610             (if (or (equal? last-element ")") (equal? last-element "("))
611               (list-head item-list max-index) 
612               item-list))
613           item-list))
614           
615 (define (get-sub-list value master-list)
616 " Get a sub-list whose cadr is equal to @var{value} from @var{master-list}"
617     (if (eq? master-list '())
618       #f
619       (let ((sublist (car master-list)))
620            (if (equal? (cadr sublist) value)
621                sublist
622                (get-sub-list value (cdr master-list))))))