Пример #1
0
 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)
Пример #2
0
 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)
Пример #3
0
 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)
Пример #4
0
 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)
Пример #5
0
 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)
Пример #6
0
 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)
Пример #7
0
 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
Пример #8
0
 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())
Пример #9
0
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
Пример #10
0
 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()
Пример #11
0
 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()
Пример #12
0
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))
Пример #13
0
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))
Пример #14
0
 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
Пример #15
0
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))
Пример #16
0
def get_blocks(cursor):
    """Yields all or the selected blocks."""
    if cursor.hasSelection():
        return cursortools.blocks(cursor) 
    else:
        return cursortools.all_blocks(cursor.document())
Пример #17
0
def all_tokens(document):
    """Yields all tokens of a document."""
    return (token for block in cursortools.all_blocks(document)
            for token in tokens(block))
Пример #18
0
def all_tokens(document):
    """Yields all tokens of a document."""
    return (token for block in cursortools.all_blocks(document) for token in tokens(block))