def label_vertical_moments_in_expr_with_pitch_numbers(
    expr, markup_direction=Down):
    r'''Label pitch numbers of every vertical moment in `expr`:

    ::

        >>> score = Score([])
        >>> staff = Staff("c'8 d'8 e'8 f'8")
        >>> score.append(staff)
        >>> staff = Staff(r"""\clef "alto" g4 f4""")
        >>> score.append(staff)
        >>> staff = Staff(r"""\clef "bass" c,2""")
        >>> score.append(staff)

    ::

        >>> labeltools.label_vertical_moments_in_expr_with_pitch_numbers(score)

    ..  doctest::

        >>> f(score)
        \new Score <<
            \new Staff {
                c'8
                d'8
                    _ \markup {
                        \small
                            \column
                                {
                                    2
                                    -5
                                    -24
                                }
                        }
                e'8
                f'8
                    _ \markup {
                        \small
                            \column
                                {
                                    5
                                    -7
                                    -24
                                }
                        }
            }
            \new Staff {
                \clef "alto"
                g4
                f4
                    _ \markup {
                        \small
                            \column
                                {
                                    4
                                    -7
                                    -24
                                }
                        }
            }
            \new Staff {
                \clef "bass"
                c,2
                    _ \markup {
                        \small
                            \column
                                {
                                    0
                                    -5
                                    -24
                                }
                        }
            }
        >>

    ::

        >>> show(score) # doctest: +SKIP

    Returns none.
    '''

    for vertical_moment in iterationtools.iterate_vertical_moments_in_expr(expr):
        leaves = vertical_moment.leaves
        pitches = pitchtools.PitchSegment.from_selection(leaves)
        if not pitches:
            continue
        pitch_numbers = [abs(pitch.numbered_pitch) for pitch in pitches]
        pitch_numbers = ' '.join([str(x) for x in pitch_numbers])
        pitch_numbers = r'\small \column { %s }' % pitch_numbers
        markup = markuptools.Markup(pitch_numbers, markup_direction)
        attach(markup, vertical_moment.start_leaves[-1])
def label_vertical_moments_in_expr_with_numbered_intervals(
    expr, markup_direction=Down):
    r'''Label numbered intervals of every vertical moment in `expr`:

    ::

        >>> score = Score([])
        >>> staff = Staff("c'8 d'8 e'8 f'8")
        >>> score.append(staff)
        >>> staff = Staff(r"""\clef "alto" g4 f4""")
        >>> score.append(staff)
        >>> staff = Staff(r"""\clef "bass" c,2""")
        >>> score.append(staff)

    ::

        >>> labeltools.label_vertical_moments_in_expr_with_numbered_intervals(
        ...     score)

    ..  doctest::

        >>> f(score)
        \new Score <<
            \new Staff {
                c'8
                d'8
                    _ \markup {
                        \small
                            \column
                                {
                                    +26
                                    +19
                                }
                        }
                e'8
                f'8
                    _ \markup {
                        \small
                            \column
                                {
                                    +29
                                    +17
                                }
                        }
            }
            \new Staff {
                \clef "alto"
                g4
                f4
                    _ \markup {
                        \small
                            \column
                                {
                                    +28
                                    +17
                                }
                        }
            }
            \new Staff {
                \clef "bass"
                c,2
                    _ \markup {
                        \small
                            \column
                                {
                                    +24
                                    +19
                                }
                        }
            }
        >>

    ::

        >>> show(score) # doctest: +SKIP

    Returns none.
    '''

    for vertical_moment in \
        iterationtools.iterate_vertical_moments_in_expr(expr):
        leaves = vertical_moment.leaves
        notes = [leaf for leaf in leaves if isinstance(leaf, notetools.Note)]
        if not notes:
            continue
        notes.sort(key=lambda x: x.written_pitch.numbered_pitch)
        notes.reverse()
        bass_note = notes[-1]
        upper_notes = notes[:-1]
        intervals = []
        for upper_note in upper_notes:
            interval = pitchtools.NumberedInterval.from_pitch_carriers(
                bass_note, upper_note)
            intervals.append(interval)
        markup = markuptools.Markup(
            r'\small \column {{ {} }}'.format(
                ' '.join(str(interval) 
                    for interval in intervals)),
            markup_direction,
            )
        attach(markup, vertical_moment.start_leaves[-1])
def iterate_leaf_pairs_in_expr(expr):
    r'''Iterate leaf pairs forward in `expr`:

    ::

        >>> score = Score([])
        >>> notes = [Note("c'8"), Note("d'8"), Note("e'8"), Note("f'8"), Note("g'4")]
        >>> score.append(Staff(notes))
        >>> notes = [Note(x, (1, 4)) for x in [-12, -15, -17]]
        >>> score.append(Staff(notes))
        >>> clef = contexttools.ClefMark('bass')
        >>> attach(clef, score[1])
        ClefMark('bass')(Staff{3})
        >>> show(score) # doctest: +SKIP

    ..  doctest::

        >>> f(score)
        \new Score <<
            \new Staff {
                c'8
                d'8
                e'8
                f'8
                g'4
            }
            \new Staff {
                \clef "bass"
                c4
                a,4
                g,4
            }
        >>

    ::

        >>> for pair in iterationtools.iterate_leaf_pairs_in_expr(score):
        ...        pair
        (Note("c'8"), Note('c4'))
        (Note("c'8"), Note("d'8"))
        (Note('c4'), Note("d'8"))
        (Note("d'8"), Note("e'8"))
        (Note("d'8"), Note('a,4'))
        (Note('c4'), Note("e'8"))
        (Note('c4'), Note('a,4'))
        (Note("e'8"), Note('a,4'))
        (Note("e'8"), Note("f'8"))
        (Note('a,4'), Note("f'8"))
        (Note("f'8"), Note("g'4"))
        (Note("f'8"), Note('g,4'))
        (Note('a,4'), Note("g'4"))
        (Note('a,4'), Note('g,4'))
        (Note("g'4"), Note('g,4'))

    Iterate leaf pairs left-to-right and top-to-bottom.

    Returns generator.
    '''
    from abjad.tools import iterationtools

    vertical_moments = iterationtools.iterate_vertical_moments_in_expr(expr)
    for moment_1, moment_2 in \
        sequencetools.iterate_sequence_pairwise_strict(vertical_moments):
        for pair in sequencetools.yield_all_unordered_pairs_of_sequence(
            moment_1.start_leaves):
            yield pair
        pairs = sequencetools.yield_all_pairs_between_sequences(
            moment_1.leaves, moment_2.start_leaves)
        for pair in pairs:
            yield pair
    else:
        for pair in sequencetools.yield_all_unordered_pairs_of_sequence(
            moment_2.start_leaves):
            yield pair
def label_vertical_moments_in_expr_with_named_intervals(
    expr, markup_direction=Down):
    r'''Label named intervals of every vertical moment in `expr`:

    ::

        >>> score = Score([])
        >>> staff = Staff("c'8 d'8 e'8 f'8")
        >>> score.append(staff)
        >>> staff = Staff(r"""\clef "alto" g4 f4""")
        >>> score.append(staff)
        >>> staff = Staff(r"""\clef "bass" c,2""")
        >>> score.append(staff)

    ::

        >>> labeltools.label_vertical_moments_in_expr_with_named_intervals(score)

    ..  doctest::

        >>> f(score)
        \new Score <<
            \new Staff {
                c'8
                d'8
                    _ \markup {
                        \small
                            \column
                                {
                                    16
                                    12
                                }
                        }
                e'8
                f'8
                    _ \markup {
                        \small
                            \column
                                {
                                    18
                                    11
                                }
                        }
            }
            \new Staff {
                \clef "alto"
                g4
                f4
                    _ \markup {
                        \small
                            \column
                                {
                                    17
                                    11
                                }
                        }
            }
            \new Staff {
                \clef "bass"
                c,2
                    _ \markup {
                        \small
                            \column
                                {
                                    15
                                    12
                                }
                        }
            }
        >>

    ::

        >>> show(score) # doctest: +SKIP

    Returns none.
    '''

    for vertical_moment in \
        iterationtools.iterate_vertical_moments_in_expr(expr):
        leaves = vertical_moment.leaves
        notes = [leaf for leaf in leaves if isinstance(leaf, notetools.Note)]
        if not notes:
            continue
        notes.sort(key=lambda x: x.written_pitch.numbered_pitch)
        notes.reverse()
        bass_note = notes[-1]
        upper_notes = notes[:-1]
        named_intervals = []
        for upper_note in upper_notes:
            named_interval = pitchtools.NamedInterval.from_pitch_carriers(
                bass_note.written_pitch, upper_note.written_pitch)
            named_intervals.append(named_interval)
        intervals = [x.number for x in named_intervals]
        intervals = ' '.join([str(x) for x in intervals])
        intervals = r'\small \column { %s }' % intervals
        markup = markuptools.Markup(intervals, markup_direction)
        attach(markup, vertical_moment.start_leaves[-1])