def save(self): """Called to perform the edits in the document.""" cursor = QTextCursor(self._range) start = cursor.selectionStart() # use cursordiff; don't destroy point and click positions cursordiff.insert_text(cursor, self.view.toPlainText()) cursor.setPosition(start, QTextCursor.KeepAnchor) with cursortools.compress_undo(cursor, True): # re-indent the inserted line(s) indent.re_indent(cursor)
def save(self): """Called to perform the edits in the document.""" cursor = QTextCursor(self._range) start = cursor.selectionStart() # use cursordiff; don't destroy point and click positions cursordiff.insert_text(cursor, self.view.toPlainText()) cursor.setPosition(start, QTextCursor.KeepAnchor) with cursortools.compress_undo(cursor, True): # re-indent the inserted line(s) indent.re_indent(cursor)
def toLowerCase(self): global reWord # the regex \b\w+\b tc = QTextCursor(self.textCursor()) if not tc.hasSelection() : return # no selection, nothing to do startpos = tc.selectionStart() endpos = tc.selectionEnd() qs = QString(tc.selectedText()) # copy of selected text i = reWord.indexIn(qs,0) # index of first word if any if i < 0 : return # no words in selection, exit while i >= 0: w = reWord.cap(0) # found word as QString n = w.size() # its length qs.replace(i,n,w.toLower()) # replace it with UC version i = reWord.indexIn(qs,i+n) # find next word if any # we have changed at least one word, replace selection with altered text tc.insertText(qs) # that wiped the selection, so restore it by "dragging" left to right tc.setPosition(startpos,QTextCursor.MoveAnchor) # click tc.setPosition(endpos,QTextCursor.KeepAnchor) # drag self.setTextCursor(tc)
def toLowerCase(self): global reWord # the regex \b\w+\b tc = QTextCursor(self.textCursor()) if not tc.hasSelection(): return # no selection, nothing to do startpos = tc.selectionStart() endpos = tc.selectionEnd() qs = QString(tc.selectedText()) # copy of selected text i = reWord.indexIn(qs, 0) # index of first word if any if i < 0: return # no words in selection, exit while i >= 0: w = reWord.cap(0) # found word as QString n = w.size() # its length qs.replace(i, n, w.toLower()) # replace it with UC version i = reWord.indexIn(qs, i + n) # find next word if any # we have changed at least one word, replace selection with altered text tc.insertText(qs) # that wiped the selection, so restore it by "dragging" left to right tc.setPosition(startpos, QTextCursor.MoveAnchor) # click tc.setPosition(endpos, QTextCursor.KeepAnchor) # drag self.setTextCursor(tc)
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 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)