Example #1
0
void FindDialog::replaceAll() {
   TextEditor *current = m_parent->m_activeEditor;
   QTextDocument *doc  = current->getTextDocument();
   QTextCursor start   = current->textCursor();
   QTextCursor result, last;
   int noOccurrences = 0, startPos = -1, endPos = -1;
   m_searching = false;
   
   // search within a selection
   if (start.hasSelection() && searchSelection()) {
      startPos = start.selectionStart();
      endPos = start.selectionEnd();
      
      // get rid of selection
      if (findBackwards())
         start.setPosition(endPos);
      else start.setPosition(startPos);
   } else start.movePosition(QTextCursor::Start);
   
   start.beginEditBlock();
   result = start;
   
   do {
      last   = result;
      result = replace(result, doc, startPos, endPos);
      if (result.isNull())
         break;
      
      // disallow infinite replacements of similar terms ('life' w/ 'LIFE')
      result.setPosition(result.selectionEnd());
      ++noOccurrences;
   } while(true);
   
   start.endEditBlock();
   if (noOccurrences <= 0) {
      STATUS_BAR->showMessage(QString("%1 '%2' not found with the Options given").arg((regularExpression() ? QString("RegExp") : QString("String")), getSearchString()));
   } else {
      current->changeTextCursor(last);
      STATUS_BAR->showMessage(QString("Replaced %1 occurence%2.").arg(QString::number(noOccurrences), (noOccurrences != 1 ? QString("s") : QString(""))));
   }
   
   updateReplace(true);
}
Example #2
0
void FindDialog::accept() {
   TextEditor *current = m_parent->m_activeEditor;
   QTextDocument *doc  = current->getTextDocument();
   QTextCursor start   = current->textCursor();
   QTextCursor result;
   int startPos = -1, endPos = -1;
   
   // see if we're supposed to search within a selection
   if (start.hasSelection() && searchSelection()) {
      startPos = start.selectionStart();
      endPos = start.selectionEnd();
      
      // get rid of selection
      if (findBackwards())
         start.setPosition(endPos);
      else start.setPosition(startPos);
   } else if (!fromCursor() && !m_searching)
      start.movePosition(QTextCursor::Start);
   
   if (isFind()) {
      result = find(start, doc);
      
      // test if result was outside of selection
      if (!result.isNull() && 
          ((endPos > 0 && result.selectionEnd() > endPos) || 
          (startPos > 0 && result.selectionStart() < startPos)))
         result = QTextCursor(); // null cursor
   } else result = replace(start, doc, endPos);
   
   if (result.isNull()) {
      STATUS_BAR->showMessage(QString("%1 '%2' not found with the Options given").arg((regularExpression() ? QString("RegExp") : QString("String")), getSearchString()));
      
      m_searching = false;
      updateReplace(isReplace());
   } else {
      current->changeTextCursor(result);
      STATUS_BAR->clearMessage();
      
      m_searching = true;
      updateReplace(isReplace());
   }
}