def fold_toplevel(self): """Folds all toplevel regions, without touching inner regions.""" for block in cursortools.all_blocks(self.document()): if not block.isVisible(): continue elif self.fold_level(block).start: self.fold(block)
def blocks_gen(): """Yield depth (before block), block and fold_level per block.""" depth = 0 for b in cursortools.all_blocks(self.document()): l = self.fold_level(b) yield depth, b, l depth += sum(l)
def fold_all(self): """Folds all regions.""" for block in cursortools.all_blocks(self.document()): if self.fold_level(block).start: if block.isVisible(): self.fold(block) else: self.mark(block, True)
def globalStaffSize(self, default=20): """Returns the global staff size, if set, else the default value.""" for block in cursortools.all_blocks(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 unfold_all(self): """Fully unfolds the document.""" first = None for block in cursortools.all_blocks(self.document()): if self.fold_level(block).start: self.mark(block, False) if not block.isVisible(): block.setVisible(True) if first is None: first = block last = block if first: self.document().markContentsDirty(first.position(), last.position())
def re_indent(cursor): """Re-indents the selected region or the whole document.""" if cursor.hasSelection(): blocks = cursortools.blocks(cursor) else: blocks = cursortools.all_blocks(cursor.document()) with cursortools.compress_undo(cursor): for block in blocks: tokeniter.update(block) if tokeniter.state(block).mode() in ('lilypond', 'scheme'): indent = compute_indent(block) else: indent = get_indent(block) if set_indent(block, indent): tokeniter.update(block) # force token update if changed
def unfold_all(self): """Fully unfolds the document.""" first = None for block in cursortools.all_blocks(self.document()): if self.fold_level(block).start: self.mark(block, False) if not block.isVisible(): block.setVisible(True) if first is None: first = block last = block if first: self.document().markContentsDirty(first.position(), last.position() - first.position()) # no need to check consistency self._all_visible = True self._timer.isActive() and self._timer.stop()
def unfold_all(self): """Fully unfolds the document.""" first = None for block in cursortools.all_blocks(self.document()): if self.fold_level(block).start: self.mark(block, False) if not block.isVisible(): block.setVisible(True) if first is None: first = block last = block if first: self.document().markContentsDirty(first.position(), last.position()) # no need to check consistency self._all_visible = True self._timer.isActive() and self._timer.stop()
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.all_blocks(textedit.document()): if block.text().startswith('-*- '): lineno += 1 else: break block = textedit.document().findBlockByNumber(lineno - 1) if block.isValid(): textedit.setTextCursor(QTextCursor(block))
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.all_blocks(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.all_blocks(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.all_blocks(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.all_blocks(cursor.document())
def all_tokens(document): """Yields all tokens of a document.""" return (token for block in cursortools.all_blocks(document) for token in tokens(block))