1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2013 - 2014 by Wilbert Berendsen
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# See http://www.gnu.org/licenses/ for more information.
"""
Remove certain types of LilyPond input from selected regions.
The functions are called by actions defined in the documentactions.py module.
"""
import functools
import lydocument
import ly.document
import ly.words
import ly.lex.lilypond
def remove(func):
"""Decorator turning a function yielding ranges into removing the ranges.
Note that you should call the function with a QTextCursor as the first
argument. The returned decorator converts the QTextCursor to a
ly.document.Cursor, calls the function and removes the ranges returned
by the function.
"""
@functools.wraps(func)
def decorator(cursor, *args):
c = lydocument.cursor(cursor)
remove = func(c, *args)
with c.document as d:
for start, end in remove:
del d[start:end]
return decorator
def is_articulation(token):
"""Return True if token is an articulation."""
return (isinstance(token, ly.lex.lilypond.Articulation)
and token[1:] in ly.words.articulations)
def is_ornament(token):
"""Return True if token is an ornament."""
return (isinstance(token, ly.lex.lilypond.Articulation)
and token[1:] in ly.words.ornaments)
def is_instrument_script(token):
"""Return True if token is an instrument script."""
return (isinstance(token, ly.lex.lilypond.Articulation)
and token[1:] in ly.words.instrument_scripts)
def find_positions(cursor, predicate, predicate_dir=None):
"""Yields positions (start, end) for tokens predicate returns True for.
The tokens (gotten from the cursor's selection) may be preceded by a
ly.lex.lilypond.Direction token.
If predicate_dir is specified, it is used for the items following a
Direction tokens, otherwise predicate is also used for that case.
"""
if predicate_dir is None:
predicate_dir = predicate
source = ly.document.Source(cursor, None, ly.document.PARTIAL, True)
for t in source:
if isinstance(t, ly.lex.lilypond.Direction):
start = t.pos
for t in source.tokens:
if isinstance(t, ly.lex.Space):
continue
elif predicate_dir(t):
yield start, t.end
break
elif predicate(t):
yield t.pos, t.end
@remove
def comments(cursor):
"""Remove all comments from the cursor's selection."""
source = ly.document.Source(cursor, True, tokens_with_position=True)
for token in source:
if isinstance(token, ly.lex.Comment):
yield token.pos, token.end
@remove
def articulations(cursor):
"""Remove articulations from the cursor's selection."""
return find_positions(cursor, is_articulation,
lambda t: isinstance(t, ly.lex.lilypond.ScriptAbbreviation) or is_articulation(t))
@remove
def ornaments(cursor):
"""Remove ornaments from the cursor's selection."""
return find_positions(cursor, is_ornament)
@remove
def instrument_scripts(cursor):
"""Remove instrument_scripts from the cursor's selection."""
return find_positions(cursor, is_instrument_script)
@remove
def slurs(cursor):
"""Remove slurs from the cursor's selection."""
return find_positions(cursor, lambda t: isinstance(t, ly.lex.lilypond.Slur))
@remove
def dynamics(cursor):
"""Remove dynamics from the cursor's selection."""
return find_positions(cursor, lambda t: isinstance(t, ly.lex.lilypond.Dynamic))
@remove
def fingerings(cursor):
"""Remove fingerings from the cursor's selection."""
return find_positions(cursor, lambda t: isinstance(t, ly.lex.lilypond.Fingering))
@remove
def markup(cursor):
"""Remove (postfix) markup texts from the cursor's selection."""
source = ly.document.Source(cursor, True, tokens_with_position=True)
for token in source:
if isinstance(token, ly.lex.lilypond.Direction):
start = token.pos
for token in source:
if token == '\\markup':
# find the end of the markup expression
depth = source.state.depth()
for token in source:
if source.state.depth() < depth:
yield start, token.end
break
elif token == '"':
# find the end of the string
for token in source:
if isinstance(token, ly.lex.StringEnd):
yield start, token.end
break
elif token.isalpha():
yield start, token.end
elif isinstance(token, ly.lex.Space):
continue
break
@remove
def smart_delete(cursor, backspace=False):
r"""This function intelligently deletes an item the cursor is at.
Basically it behaves like normal Delete (cursor.deleteChar()) or BackSpace
(cursor.deletePreviousChar()), but it performs the following:
- if the item is a matching object (, ), [, ], \[, \] etc, the other item is
deleted as well
- if the item is an articulation it is deleted completely with direction
specifier if present
- if the cursor is on a note, the whole notename is deleted including
postfix stuff
- if the cursor is on the '<' of a chord, the whole chord is deleted
TODO: implement
"""
pass
|