def globalStaffSize(self, default=20): """Returns the global staff size, if set, else the default value.""" for block in cursortools.allBlocks(self.document()): tokens = tokeniter.tokens(block) try: i = tokens.index('set-global-staff-size') except ValueError: continue try: return int(tokens[i+2], 10) except (IndexError, ValueError): pass return default
def reIndent(cursor): """Re-indents the selected region or the whole document.""" if cursor.hasSelection(): blocks = cursortools.blocks(cursor) else: blocks = cursortools.allBlocks(cursor.document()) with cursortools.editBlock(cursor): for block in blocks: tokeniter.update(block) if tokeniter.state(block).mode() in ('lilypond', 'scheme'): indent = computeIndent(block) else: indent = getIndent(block) if setIndent(block, indent): tokeniter.update(block) # force token update if changed
def handle_exception(name, view): """Called when a snippet raises a Python exception. Shows the error message and offers the option to edit the offending snippet. """ import sys, traceback exc_type, exc_value, exc_traceback = sys.exc_info() tb = traceback.extract_tb(exc_traceback) while tb and tb[0][0] != "<snippet>": del tb[0] msg = ''.join(traceback.format_list(tb) + traceback.format_exception_only(exc_type, exc_value)) dlg = QMessageBox(QMessageBox.Critical, _("Snippet error"), msg, QMessageBox.Ok | QMessageBox.Cancel) dlg.button(QMessageBox.Ok).setText(_("Edit Snippet")) dlg.setDefaultButton(QMessageBox.Cancel) dlg.setEscapeButton(QMessageBox.Cancel) if dlg.exec_() != QMessageBox.Ok: return # determine line number if exc_type is SyntaxError: lineno = exc_value.lineno elif tb: lineno = tb[0][1] else: lineno = None import panelmanager from . import edit widget = panelmanager.manager(view.window()).snippettool.widget() textedit = edit.Edit(widget, name).text if lineno is not None: # convert to line number in full snippet text for block in cursortools.allBlocks(textedit.document()): if block.text().startswith('-*- '): lineno += 1 else: break block = textedit.document().findBlockByNumber(lineno-1) if block.isValid(): textedit.setTextCursor(QTextCursor(block))
def pitchLanguage(self): """Returns the pitchname language used in the document, if defined.""" languages = ly.pitch.pitchInfo.keys() for block in cursortools.allBlocks(self.document()): tokens = tokeniter.tokens(block) try: i = tokens.index('\\language') except ValueError: try: i = tokens.index('\\include') except ValueError: continue if isinstance(tokens[i], ly.lex.lilypond.Keyword): for t in tokens[i+1:]: if isinstance(t, ly.lex.Space): continue elif t == '"': continue lang = t[:-3] if t.endswith('.ly') else t[:] if lang in languages: return lang
def document(cursor, state=None): """Yields block, tokens for all blocks in the document. Usage: for block, tokens in document(cursor): for token in tokens: do_something() ... If state is given, it should be the state at the start of the document. """ if state: def gen(source): for t in source: state.follow(t) yield t else: gen = iter for block in cursortools.allBlocks(cursor.document()): yield block, gen(tokens(block))
def get_blocks(cursor): """Yields all or the selected blocks.""" if cursor.hasSelection(): return cursortools.blocks(cursor) else: return cursortools.allBlocks(cursor.document())
def allTokens(document): """Yields all tokens of a document.""" return (token for block in cursortools.allBlocks(document) for token in tokens(block))