+;;;; This file is part of LilyPond, the GNU music typesetter.
+;;;;
+;;;; Copyright (C) 2004--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
+;;;;
+;;;; LilyPond is free software: you can redistribute it and/or modify
+;;;; it under the terms of the GNU General Public License as published by
+;;;; the Free Software Foundation, either version 3 of the License, or
+;;;; (at your option) any later version.
+;;;;
+;;;; LilyPond is distributed in the hope that it will be useful,
+;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;;;; GNU General Public License for more details.
+;;;;
+;;;; You should have received a copy of the GNU General Public License
+;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
+
+; for define-safe-public when byte-compiling using Guile V2
+(use-modules (scm safe-utility-defs))
+
+(define-public (construct-chord-elements root duration modifications)
+ "Build a chord on root using modifiers in @var{modifications}.
+@code{NoteEvents} have duration @var{duration}.
+
+Notes: Natural 11 is left from chord if not explicitly specified.
+
+Entry point for the parser."
+ (let* ((flat-mods (flatten-list modifications))
+ (base-chord (stack-thirds (ly:make-pitch 0 4 0) the-canonical-chord))
+ (complete-chord '())
+ (bass #f)
+ (inversion #f)
+ (lead-mod #f)
+ (explicit-11 #f)
+ (start-additions #t))
+
+ (define (interpret-inversion chord mods)
+ "Read /FOO part. Side effect: INVERSION is set."
+ (if (and (> (length mods) 1) (eq? (car mods) 'chord-slash))
+ (begin
+ (set! inversion (cadr mods))
+ (set! mods (cddr mods))))
+ (interpret-bass chord mods))
+
+ (define (interpret-bass chord mods)
+ "Read /+FOO part. Side effect: BASS is set."
+ (if (and (> (length mods) 1) (eq? (car mods) 'chord-bass))
+ (begin
+ (set! bass (cadr mods))
+ (set! mods (cddr mods))))
+ (if (pair? mods)
+ (ly:warning (_ "Spurious garbage following chord: ~A") mods))
+ chord)
+
+ (define (interpret-removals chord mods)
+ (define (inner-interpret chord mods)
+ (if (and (pair? mods) (ly:pitch? (car mods)))
+ (inner-interpret (remove-step (+ 1 (ly:pitch-steps (car mods))) chord)
+ (cdr mods))
+ (interpret-inversion chord mods)))
+ (if (and (pair? mods) (eq? (car mods) 'chord-caret))
+ (inner-interpret chord (cdr mods))
+ (interpret-inversion chord mods)))
+
+ (define (interpret-additions chord mods)
+ "Interpret additions. TODO: should restrict modifier use?"
+ (cond ((null? mods) chord)
+ ((ly:pitch? (car mods))
+ (if (= (pitch-step (car mods)) 11)
+ (set! explicit-11 #t))
+ (interpret-additions (cons (car mods) (remove-step (pitch-step (car mods)) chord))
+ (cdr mods)))
+ ((procedure? (car mods))
+ (interpret-additions ((car mods) chord)
+ (cdr mods)))
+ (else (interpret-removals chord mods))))
+
+ (define (pitch-octavated-strictly-below p root)
+ "return P, but octavated, so it is below ROOT"
+ (ly:make-pitch (+ (ly:pitch-octave root)
+ (if (> (ly:pitch-notename root)
+ (ly:pitch-notename p))
+ 0 -1))
+ (ly:pitch-notename p)
+ (ly:pitch-alteration p)))
+
+ (define (process-inversion complete-chord)
+ "Take out inversion from COMPLETE-CHORD, and put it at the bottom.
+Return (INVERSION . REST-OF-CHORD).
+
+Side effect: put original pitch in INVERSION.
+If INVERSION is not in COMPLETE-CHORD, it will be set as a BASS, overriding
+the bass specified.