]> git.donarmstrong.com Git - lilypond.git/blob - ly/chord-repetition-init.ly
Docs: run convert-ly for 2.14.0.
[lilypond.git] / ly / chord-repetition-init.ly
1 %%% -*- Mode: Scheme -*-
2 \version "2.14.0"
3 %{
4
5 The following functions define the chord repetition behavior, and may
6 be invoked by the user to customize it.
7
8 ly:parser-set-repetition-symbol
9   set the chord repetition shortcut.
10   `q' is the default value set in this file.
11
12 ly:parser-set-repetition-function
13
14   set the function that is invoked when a chord repetition symbol
15   is encountered by the parser: a four argument function
16   (previous-chord, location, duration, list of articulations) which is
17   supposed to return a new chord.
18   `default-repeat-chord' is the default function set in this file.
19   `tab-repeat-chord' may be used in tablatures to preserve the string information.
20 %}
21
22 #(define-public ((make-repeat-chord-function chord-element-types note-articulation-types)
23                  previous-chord location duration articulations)
24    "Make a chord repetition function.
25 The returned functions copies the notes from @var{previous-chord} into a new chord.
26 Chord elements, which type is found in @var{chord-element-types}, are copied into
27 the new chord. Note articulations, which type is found in @var{note-articulation-types},
28 are also copied. All other events are not copied into the new chord."
29    (define (filter-events events event-types)
30      (filter (lambda (event)
31                (and (memq (ly:music-property event 'name) event-types) event))
32              events))
33    ;; If previous-chord has an length property, then it means that it
34    ;; has been processed by a music iterator.  In other words, the chord
35    ;; has been memorized from an other music block, which is certainly not
36    ;; what the user has intended, as anywy the result will be buggy.
37    ;; In that case, raise a warning.
38    (if (not (and (ly:music? previous-chord)
39                  (null? (ly:music-property previous-chord 'length))))
40        (ly:input-message location
41                          (_ "No memorized chord in music block before chord repetition")))
42    ;; Instead of copying the previous chord, then removing the
43    ;; undesired elements (like articulations), a new empty chord is built.
44    ;; Then, the pitch found in the previous chord are added to the new
45    ;; chord, without any "decoration" (e.g. cautionary accidentals,
46    ;; fingerings, text scripts, articulations).  Only the events of types
47    ;; given in `chord-elements-types' and `note-articulation-types' are
48    ;; copied from the original chord elements and note articulations,
49    ;; respectively.
50    (let ((elements (ly:music-property (ly:music-deep-copy previous-chord) 'elements)))
51      (make-music
52       'EventChord
53       'origin location
54       'elements (append!
55                  (map (lambda (note)
56                         (let ((new-note (make-music 'NoteEvent
57                                                     'origin location
58                                                     'pitch (ly:music-property note 'pitch)
59                                                     'duration duration))
60                               (articulations
61                                (filter-events (ly:music-property note 'articulations)
62                                               note-articulation-types)))
63                           (if (not (null? articulations))
64                               (set! (ly:music-property new-note 'articulations)
65                                     articulations))
66                           new-note))
67                       (filter-events elements '(NoteEvent)))
68                  (filter-events elements chord-element-types)
69                  articulations))))
70
71 #(define-public default-repeat-chord
72    (make-repeat-chord-function '() '()))
73
74 #(define-public tab-repeat-chord
75    (make-repeat-chord-function '(StringNumberEvent) '(StringNumberEvent)))
76
77 % default settings
78 #(ly:parser-set-repetition-symbol parser 'q)
79 #(ly:parser-set-repetition-function parser default-repeat-chord)