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