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
|
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008 - 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.
"""
Cut selected text and assign it to a LilyPond variable.
"""
import os.path
from PyQt5.QtGui import QTextCursor
from PyQt5.QtWidgets import QFileDialog, QMessageBox
import app
import util
import fileinfo
import documentinfo
import cursortools
import tokeniter
import ly.lex.lilypond
import metainfo
import indent
import inputdialog
def cut_assign(cursor):
"""Cuts selected text and assigns it to a LilyPond variable."""
# ask the variable name
name = inputdialog.getText(None, _("Cut and Assign"), _(
"Please enter the name for the variable to assign the selected "
"text to:"), regexp="[A-Za-z]+")
if not name:
return
cursortools.strip_selection(cursor)
# determine state at cursor
block = cursortools.block(cursor)
state = tokeniter.state(block)
for t in tokeniter.partition(cursor).left:
state.follow(t)
mode = ""
for p in state.parsers():
if isinstance(p, ly.lex.lilypond.ParseInputMode):
if isinstance(p, ly.lex.lilypond.ParseLyricMode):
mode = " \\lyricmode"
elif isinstance(p, ly.lex.lilypond.ParseChordMode):
mode = " \\chordmode"
elif isinstance(p, ly.lex.lilypond.ParseFigureMode):
mode = " \\figuremode"
elif isinstance(p, ly.lex.lilypond.ParseDrumMode):
mode = " \\drummode"
break
# find insertion place:
found = False
while block.previous().isValid():
block = block.previous()
state = tokeniter.state(block)
if isinstance(state.parser(), ly.lex.lilypond.ParseGlobal):
found = True
break
tokens = tokeniter.tokens(block)
for t in tokens:
if isinstance(t, ly.lex.lilypond.Name):
found = True
break
elif not isinstance(t, (ly.lex.Space, ly.lex.Comment)):
break
if found:
break
insert = QTextCursor(block)
text = cursor.selection().toPlainText()
space = '\n' if '\n' in text else ' '
text = ''.join((name, ' =', mode, ' {', space, text, space, '}\n\n'))
with cursortools.compress_undo(cursor):
cursor.insertText('\\' + name)
pos = insert.selectionStart()
insert.insertText(text)
if metainfo.info(cursor.document()).auto_indent:
insert.setPosition(pos, QTextCursor.KeepAnchor)
with cursortools.compress_undo(insert, True):
indent.re_indent(insert)
def move_to_include_file(cursor, parent_widget=None):
"""Opens a dialog to save the cursor's selection to a file.
The cursor's selection is then replaced with an \\include statement.
This function does its best to supply a good default filename and
use it correctly in a relative \\include statement.
Of course it only works well if the document already has a filename.
"""
doc = cursor.document()
text = cursor.selection().toPlainText()
mode = fileinfo.textmode(text)
caption = app.caption(_("dialog title", "Move to include file"))
filetypes = app.filetypes(ly.lex.extensions[mode])
name, ext = os.path.splitext(os.path.basename(doc.url().path()))
if not ext or mode == "lilypond":
ext = ".ily"
version = documentinfo.docinfo(doc).version_string()
if version:
text = '\\version "{0}"\n\n{1}'.format(version, text)
docname = name + "-include" + ext
dirname = os.path.dirname(doc.url().toLocalFile()) or app.basedir()
filename = os.path.join(dirname, docname)
filename = QFileDialog.getSaveFileName(parent_widget, caption, filename, filetypes)[0]
if not filename:
return # cancelled
data = util.encode(util.platform_newlines(text))
try:
with open(filename, "wb") as f:
f.write(data)
except IOError as e:
msg = _("{message}\n\n{strerror} ({errno})").format(
message = _("Could not write to: {url}").format(url=filename),
strerror = e.strerror,
errno = e.errno)
QMessageBox.critical(self, app.caption(_("Error")), msg)
return
filename = os.path.relpath(filename, dirname)
command = '\\include "{0}"\n'.format(filename)
cursor.insertText(command)
|