code: def __replace_within_document(self, document, occurrences, replacement_pattern):
          
  
          cursor = QTextCursor(document)
          cursor.beginEditBlock()
          offset = count = 0
          for occurence in sorted(occurrences, key=lambda x: x.position):
              cursor.setPosition(offset + occurence.position, QTextCursor.MoveAnchor)
              cursor.setPosition(offset + occurence.position + occurence.length, QTextCursor.KeepAnchor)
              cursor.insertText(replacement_pattern)
              offset += len(replacement_pattern) - occurence.length
              count += 1
          cursor.endEditBlock()
          return count
docs: Replaces given pattern occurrences in given document using given settings.
  
          :param document: Document.
          :type document: QTextDocument
          :param replacement_pattern: Replacement pattern.
          :type replacement_pattern: unicode
          :return: Replaced occurrences count.
          :rtype: int