Ejemplo n.º 1
0
    def all_in_marked(self, pat, template=None):
        if self.current_search_mark is None:
            return 0
        c = self.current_search_mark.cursor
        raw = unicode(c.selectedText()).replace(PARAGRAPH_SEPARATOR, "\n").rstrip("\0")
        if template is None:
            count = len(pat.findall(raw))
        else:
            from calibre.gui2.tweak_book.function_replace import Function

            repl_is_func = isinstance(template, Function)
            if repl_is_func:
                template.init_env()
            raw, count = pat.subn(template, raw)
            if repl_is_func:
                from calibre.gui2.tweak_book.search import show_function_debug_output

                template.end()
                show_function_debug_output(template)
            if count > 0:
                start_pos = min(c.anchor(), c.position())
                c.insertText(raw)
                end_pos = max(c.anchor(), c.position())
                c.setPosition(start_pos), c.setPosition(end_pos, c.KeepAnchor)
                self.update_extra_selections()
        return count
Ejemplo n.º 2
0
 def all_in_marked(self, pat, template=None):
     if self.current_search_mark is None:
         return 0
     c = self.current_search_mark.cursor
     raw = unicode(c.selectedText()).replace(PARAGRAPH_SEPARATOR,
                                             '\n').rstrip('\0')
     if template is None:
         count = len(pat.findall(raw))
     else:
         from calibre.gui2.tweak_book.function_replace import Function
         repl_is_func = isinstance(template, Function)
         if repl_is_func:
             template.init_env()
         raw, count = pat.subn(template, raw)
         if repl_is_func:
             from calibre.gui2.tweak_book.search import show_function_debug_output
             if getattr(template.func, 'append_final_output_to_marked',
                        False):
                 retval = template.end()
                 if retval:
                     raw += unicode(retval)
             else:
                 template.end()
             show_function_debug_output(template)
         if count > 0:
             start_pos = min(c.anchor(), c.position())
             c.insertText(raw)
             end_pos = max(c.anchor(), c.position())
             c.setPosition(start_pos), c.setPosition(end_pos, c.KeepAnchor)
             self.update_extra_selections()
     return count