00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <qcolor.h>
00025 #include <qregexp.h>
00026 #include <qsyntaxhighlighter.h>
00027 #include <qtimer.h>
00028
00029 #include <klocale.h>
00030 #include <kconfig.h>
00031 #include <kdebug.h>
00032 #include <kglobal.h>
00033 #include <kspell.h>
00034 #include <kapplication.h>
00035
00036 #include "ksyntaxhighlighter.h"
00037
00038 static int dummy, dummy2, dummy3, dummy4;
00039 static int *Okay = &dummy;
00040 static int *NotOkay = &dummy2;
00041 static int *Ignore = &dummy3;
00042 static int *Unknown = &dummy4;
00043 static const int tenSeconds = 10*1000;
00044
00045 class KSyntaxHighlighter::KSyntaxHighlighterPrivate
00046 {
00047 public:
00048 QColor col1, col2, col3, col4, col5;
00049 SyntaxMode mode;
00050 bool enabled;
00051 };
00052
00053 class KSpellingHighlighter::KSpellingHighlighterPrivate
00054 {
00055 public:
00056
00057 KSpellingHighlighterPrivate() :
00058 alwaysEndsWithSpace( true ),
00059 intraWordEditing( false ) {}
00060
00061 QString currentWord;
00062 int currentPos;
00063 bool alwaysEndsWithSpace;
00064 QColor color;
00065 bool intraWordEditing;
00066 };
00067
00068 class KDictSpellingHighlighter::KDictSpellingHighlighterPrivate
00069 {
00070 public:
00071 KDictSpellingHighlighterPrivate() :
00072 mDict( 0 ),
00073 spell( 0 ),
00074 mSpellConfig( 0 ),
00075 rehighlightRequest( 0 ),
00076 wordCount( 0 ),
00077 errorCount( 0 ),
00078 autoReady( false ),
00079 globalConfig( true ),
00080 spellReady( false ) {}
00081
00082 ~KDictSpellingHighlighterPrivate() {
00083 delete rehighlightRequest;
00084 delete spell;
00085 }
00086
00087 static QDict<int>* sDict()
00088 {
00089 if (!statDict)
00090 statDict = new QDict<int>(50021);
00091 return statDict;
00092 }
00093
00094 QDict<int>* mDict;
00095 QDict<int> autoDict;
00096 QDict<int> autoIgnoreDict;
00097 static QObject *sDictionaryMonitor;
00098 KSpell *spell;
00099 KSpellConfig *mSpellConfig;
00100 QTimer *rehighlightRequest, *spellTimeout;
00101 QString spellKey;
00102 int wordCount, errorCount;
00103 int checksRequested, checksDone;
00104 int disablePercentage;
00105 int disableWordCount;
00106 bool completeRehighlightRequired;
00107 bool active, automatic, autoReady;
00108 bool globalConfig, spellReady;
00109 private:
00110 static QDict<int>* statDict;
00111
00112 };
00113
00114 QDict<int>* KDictSpellingHighlighter::KDictSpellingHighlighterPrivate::statDict = 0;
00115
00116
00117 KSyntaxHighlighter::KSyntaxHighlighter( QTextEdit *textEdit,
00118 bool colorQuoting,
00119 const QColor& depth0,
00120 const QColor& depth1,
00121 const QColor& depth2,
00122 const QColor& depth3,
00123 SyntaxMode mode )
00124 : QSyntaxHighlighter( textEdit )
00125 {
00126 d = new KSyntaxHighlighterPrivate();
00127
00128 d->enabled = colorQuoting;
00129 d->col1 = depth0;
00130 d->col2 = depth1;
00131 d->col3 = depth2;
00132 d->col4 = depth3;
00133 d->col5 = depth0;
00134
00135 d->mode = mode;
00136 }
00137
00138 KSyntaxHighlighter::~KSyntaxHighlighter()
00139 {
00140 delete d;
00141 }
00142
00143 int KSyntaxHighlighter::highlightParagraph( const QString &text, int )
00144 {
00145 if (!d->enabled) {
00146 setFormat( 0, text.length(), textEdit()->viewport()->paletteForegroundColor() );
00147 return 0;
00148 }
00149
00150 QString simplified = text;
00151 simplified = simplified.replace( QRegExp( "\\s" ), QString::null ).replace( '|', QString::fromLatin1(">") );
00152 while ( simplified.startsWith( QString::fromLatin1(">>>>") ) )
00153 simplified = simplified.mid(3);
00154 if ( simplified.startsWith( QString::fromLatin1(">>>") ) || simplified.startsWith( QString::fromLatin1("> > >") ) )
00155 setFormat( 0, text.length(), d->col2 );
00156 else if ( simplified.startsWith( QString::fromLatin1(">>") ) || simplified.startsWith( QString::fromLatin1("> >") ) )
00157 setFormat( 0, text.length(), d->col3 );
00158 else if ( simplified.startsWith( QString::fromLatin1(">") ) )
00159 setFormat( 0, text.length(), d->col4 );
00160 else
00161 setFormat( 0, text.length(), d->col5 );
00162 return 0;
00163 }
00164
00165 KSpellingHighlighter::KSpellingHighlighter( QTextEdit *textEdit,
00166 const QColor& spellColor,
00167 bool colorQuoting,
00168 const QColor& depth0,
00169 const QColor& depth1,
00170 const QColor& depth2,
00171 const QColor& depth3 )
00172 : KSyntaxHighlighter( textEdit, colorQuoting, depth0, depth1, depth2, depth3 )
00173 {
00174 d = new KSpellingHighlighterPrivate();
00175
00176 d->color = spellColor;
00177 }
00178
00179 KSpellingHighlighter::~KSpellingHighlighter()
00180 {
00181 delete d;
00182 }
00183
00184 int KSpellingHighlighter::highlightParagraph( const QString &text,
00185 int paraNo )
00186 {
00187 if ( paraNo == -2 )
00188 paraNo = 0;
00189
00190 QString diffAndCo( ">|" );
00191
00192 bool isCode = diffAndCo.find(text[0]) != -1;
00193
00194 if ( !text.endsWith(" ") )
00195 d->alwaysEndsWithSpace = false;
00196
00197 KSyntaxHighlighter::highlightParagraph( text, -2 );
00198
00199 if ( !isCode ) {
00200 int para, index;
00201 textEdit()->getCursorPosition( ¶, &index );
00202 int len = text.length();
00203 if ( d->alwaysEndsWithSpace )
00204 len--;
00205
00206 d->currentPos = 0;
00207 d->currentWord = "";
00208 for ( int i = 0; i < len; i++ ) {
00209 if ( !text[i].isLetter() && (!(text[i] == '\'')) ) {
00210 if ( ( para != paraNo ) ||
00211 !intraWordEditing() ||
00212 ( i - d->currentWord.length() > (uint)index ) ||
00213 ( i < index ) ) {
00214 flushCurrentWord();
00215 } else {
00216 d->currentWord = "";
00217 }
00218 d->currentPos = i + 1;
00219 } else {
00220 d->currentWord += text[i];
00221 }
00222 }
00223 if ( !text[len - 1].isLetter() ||
00224 (uint)( index + 1 ) != text.length() ||
00225 para != paraNo )
00226 flushCurrentWord();
00227 }
00228 return ++paraNo;
00229 }
00230
00231 QStringList KSpellingHighlighter::personalWords()
00232 {
00233 QStringList l;
00234 l.append( "KMail" );
00235 l.append( "KOrganizer" );
00236 l.append( "KAddressBook" );
00237 l.append( "KHTML" );
00238 l.append( "KIO" );
00239 l.append( "KJS" );
00240 l.append( "Konqueror" );
00241 l.append( "KSpell" );
00242 l.append( "Kontact" );
00243 l.append( "Qt" );
00244 return l;
00245 }
00246
00247 void KSpellingHighlighter::flushCurrentWord()
00248 {
00249 while ( d->currentWord[0].isPunct() ) {
00250 d->currentWord = d->currentWord.mid( 1 );
00251 d->currentPos++;
00252 }
00253
00254 QChar ch;
00255 while ( ( ch = d->currentWord[(int) d->currentWord.length() - 1] ).isPunct() &&
00256 ch != '(' && ch != '@' )
00257 d->currentWord.truncate( d->currentWord.length() - 1 );
00258
00259 if ( !d->currentWord.isEmpty() ) {
00260 if ( isMisspelled( d->currentWord ) ) {
00261 setFormat( d->currentPos, d->currentWord.length(), d->color );
00262
00263 }
00264 }
00265 d->currentWord = "";
00266 }
00267
00268 QObject *KDictSpellingHighlighter::KDictSpellingHighlighterPrivate::sDictionaryMonitor = 0;
00269
00270 KDictSpellingHighlighter::KDictSpellingHighlighter( QTextEdit *textEdit,
00271 bool spellCheckingActive ,
00272 bool autoEnable,
00273 const QColor& spellColor,
00274 bool colorQuoting,
00275 const QColor& depth0,
00276 const QColor& depth1,
00277 const QColor& depth2,
00278 const QColor& depth3,
00279 KSpellConfig *spellConfig )
00280 : KSpellingHighlighter( textEdit, spellColor,
00281 colorQuoting, depth0, depth1, depth2, depth3 )
00282 {
00283 d = new KDictSpellingHighlighterPrivate();
00284
00285 d->mSpellConfig = spellConfig;
00286 d->globalConfig = ( !spellConfig );
00287 d->automatic = autoEnable;
00288 d->active = spellCheckingActive;
00289 d->checksRequested = 0;
00290 d->checksDone = 0;
00291 d->completeRehighlightRequired = false;
00292
00293 KConfig *config = KGlobal::config();
00294 KConfigGroupSaver cs( config, "KSpell" );
00295 d->disablePercentage = config->readNumEntry( "KSpell_AsYouTypeDisablePercentage", 42 );
00296 d->disablePercentage = QMIN( d->disablePercentage, 101 );
00297 d->disableWordCount = config->readNumEntry( "KSpell_AsYouTypeDisableWordCount", 100 );
00298
00299 textEdit->installEventFilter( this );
00300 textEdit->viewport()->installEventFilter( this );
00301
00302 d->rehighlightRequest = new QTimer(this);
00303 connect( d->rehighlightRequest, SIGNAL( timeout() ),
00304 this, SLOT( slotRehighlight() ));
00305 d->spellTimeout = new QTimer(this);
00306 connect( d->spellTimeout, SIGNAL( timeout() ),
00307 this, SLOT( slotKSpellNotResponding() ));
00308
00309 if ( d->globalConfig ) {
00310 d->spellKey = spellKey();
00311
00312 if ( !d->sDictionaryMonitor )
00313 d->sDictionaryMonitor = new QObject();
00314 }
00315 else {
00316 d->mDict = new QDict<int>(4001);
00317 connect( d->mSpellConfig, SIGNAL( configChanged() ),
00318 this, SLOT( slotLocalSpellConfigChanged() ) );
00319 }
00320
00321 slotDictionaryChanged();
00322 startTimer( 2 * 1000 );
00323 }
00324
00325 KDictSpellingHighlighter::~KDictSpellingHighlighter()
00326 {
00327 delete d->spell;
00328 d->spell = 0;
00329 delete d->mDict;
00330 d->mDict = 0;
00331 delete d;
00332 }
00333
00334 void KDictSpellingHighlighter::slotSpellReady( KSpell *spell )
00335 {
00336 kdDebug(0) << "KDictSpellingHighlighter::slotSpellReady( " << spell << " )" << endl;
00337 if ( d->globalConfig ) {
00338 connect( d->sDictionaryMonitor, SIGNAL( destroyed()),
00339 this, SLOT( slotDictionaryChanged() ));
00340 }
00341 if ( spell != d->spell )
00342 {
00343 delete d->spell;
00344 d->spell = spell;
00345 }
00346 d->spellReady = true;
00347 const QStringList l = KSpellingHighlighter::personalWords();
00348 for ( QStringList::ConstIterator it = l.begin(); it != l.end(); ++it ) {
00349 d->spell->addPersonal( *it );
00350 }
00351 connect( spell, SIGNAL( misspelling( const QString &, const QStringList &, unsigned int )),
00352 this, SLOT( slotMisspelling( const QString &, const QStringList &, unsigned int )));
00353 connect( spell, SIGNAL( corrected( const QString &, const QString &, unsigned int )),
00354 this, SLOT( slotCorrected( const QString &, const QString &, unsigned int )));
00355 d->checksRequested = 0;
00356 d->checksDone = 0;
00357 d->completeRehighlightRequired = true;
00358 d->rehighlightRequest->start( 0, true );
00359 }
00360
00361 bool KDictSpellingHighlighter::isMisspelled( const QString &word )
00362 {
00363 if (!d->spellReady)
00364 return false;
00365
00366
00367
00368
00369
00370
00371
00372 if ( !d->autoReady )
00373 d->autoIgnoreDict.replace( word, Ignore );
00374
00375
00376 QDict<int>* dict = ( d->globalConfig ? d->sDict() : d->mDict );
00377 if ( !dict->isEmpty() && (*dict)[word] == NotOkay ) {
00378 if ( d->autoReady && ( d->autoDict[word] != NotOkay )) {
00379 if ( !d->autoIgnoreDict[word] )
00380 ++d->errorCount;
00381 d->autoDict.replace( word, NotOkay );
00382 }
00383
00384 return d->active;
00385 }
00386 if ( !dict->isEmpty() && (*dict)[word] == Okay ) {
00387 if ( d->autoReady && !d->autoDict[word] ) {
00388 d->autoDict.replace( word, Okay );
00389 }
00390 return false;
00391 }
00392
00393 if ((dict->isEmpty() || !((*dict)[word])) && d->spell ) {
00394 int para, index;
00395 textEdit()->getCursorPosition( ¶, &index );
00396 ++d->wordCount;
00397 dict->replace( word, Unknown );
00398 ++d->checksRequested;
00399 if (currentParagraph() != para)
00400 d->completeRehighlightRequired = true;
00401 d->spellTimeout->start( tenSeconds, true );
00402 d->spell->checkWord( word, false );
00403 }
00404 return false;
00405 }
00406
00407 bool KSpellingHighlighter::intraWordEditing() const
00408 {
00409 return d->intraWordEditing;
00410 }
00411
00412 void KSpellingHighlighter::setIntraWordEditing( bool editing )
00413 {
00414 d->intraWordEditing = editing;
00415 }
00416
00417 void KDictSpellingHighlighter::slotMisspelling (const QString &originalWord, const QStringList &suggestions,
00418 unsigned int pos)
00419 {
00420 Q_UNUSED( suggestions );
00421
00422 if ( d->globalConfig )
00423 d->sDict()->replace( originalWord, NotOkay );
00424 else
00425 d->mDict->replace( originalWord, NotOkay );
00426
00427
00428
00429 emit newSuggestions( originalWord, suggestions, pos );
00430 }
00431
00432 void KDictSpellingHighlighter::slotCorrected(const QString &word,
00433 const QString &,
00434 unsigned int)
00435
00436 {
00437 QDict<int>* dict = ( d->globalConfig ? d->sDict() : d->mDict );
00438 if ( !dict->isEmpty() && (*dict)[word] == Unknown ) {
00439 dict->replace( word, Okay );
00440 }
00441 ++d->checksDone;
00442 if (d->checksDone == d->checksRequested) {
00443 d->spellTimeout->stop();
00444 slotRehighlight();
00445 } else {
00446 d->spellTimeout->start( tenSeconds, true );
00447 }
00448 }
00449
00450 void KDictSpellingHighlighter::dictionaryChanged()
00451 {
00452 QObject *oldMonitor = KDictSpellingHighlighterPrivate::sDictionaryMonitor;
00453 KDictSpellingHighlighterPrivate::sDictionaryMonitor = new QObject();
00454 KDictSpellingHighlighterPrivate::sDict()->clear();
00455 delete oldMonitor;
00456 }
00457
00458 void KDictSpellingHighlighter::restartBackgroundSpellCheck()
00459 {
00460 kdDebug(0) << "KDictSpellingHighlighter::restartBackgroundSpellCheck()" << endl;
00461 slotDictionaryChanged();
00462 }
00463
00464 void KDictSpellingHighlighter::setActive( bool active )
00465 {
00466 if ( active == d->active )
00467 return;
00468
00469 d->active = active;
00470 rehighlight();
00471 if ( d->active )
00472 emit activeChanged( i18n("As-you-type spell checking enabled.") );
00473 else
00474 emit activeChanged( i18n("As-you-type spell checking disabled.") );
00475 }
00476
00477 bool KDictSpellingHighlighter::isActive() const
00478 {
00479 return d->active;
00480 }
00481
00482 void KDictSpellingHighlighter::setAutomatic( bool automatic )
00483 {
00484 if ( automatic == d->automatic )
00485 return;
00486
00487 d->automatic = automatic;
00488 if ( d->automatic )
00489 slotAutoDetection();
00490 }
00491
00492 bool KDictSpellingHighlighter::automatic() const
00493 {
00494 return d->automatic;
00495 }
00496
00497 void KDictSpellingHighlighter::slotRehighlight()
00498 {
00499 kdDebug(0) << "KDictSpellingHighlighter::slotRehighlight()" << endl;
00500 if (d->completeRehighlightRequired) {
00501 rehighlight();
00502 } else {
00503 int para, index;
00504 textEdit()->getCursorPosition( ¶, &index );
00505
00506 textEdit()->insertAt( "", para, index );
00507 }
00508 if (d->checksDone == d->checksRequested)
00509 d->completeRehighlightRequired = false;
00510 QTimer::singleShot( 0, this, SLOT( slotAutoDetection() ));
00511 }
00512
00513 void KDictSpellingHighlighter::slotDictionaryChanged()
00514 {
00515 delete d->spell;
00516 d->spellReady = false;
00517 d->wordCount = 0;
00518 d->errorCount = 0;
00519 d->autoDict.clear();
00520
00521 d->spell = new KSpell( 0, i18n( "Incremental Spellcheck" ), this,
00522 SLOT( slotSpellReady( KSpell * ) ), d->mSpellConfig );
00523 }
00524
00525 void KDictSpellingHighlighter::slotLocalSpellConfigChanged()
00526 {
00527 kdDebug(0) << "KDictSpellingHighlighter::slotSpellConfigChanged()" << endl;
00528
00529 d->mDict->clear();
00530 slotDictionaryChanged();
00531 }
00532
00533 QString KDictSpellingHighlighter::spellKey()
00534 {
00535 KConfig *config = KGlobal::config();
00536 KConfigGroupSaver cs( config, "KSpell" );
00537 config->reparseConfiguration();
00538 QString key;
00539 key += QString::number( config->readNumEntry( "KSpell_NoRootAffix", 0 ));
00540 key += '/';
00541 key += QString::number( config->readNumEntry( "KSpell_RunTogether", 0 ));
00542 key += '/';
00543 key += config->readEntry( "KSpell_Dictionary", "" );
00544 key += '/';
00545 key += QString::number( config->readNumEntry( "KSpell_DictFromList", false ));
00546 key += '/';
00547 key += QString::number( config->readNumEntry( "KSpell_Encoding", KS_E_ASCII ));
00548 key += '/';
00549 key += QString::number( config->readNumEntry( "KSpell_Client", KS_CLIENT_ISPELL ));
00550 return key;
00551 }
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561 void KDictSpellingHighlighter::slotAutoDetection()
00562 {
00563 if ( !d->autoReady )
00564 return;
00565
00566 bool savedActive = d->active;
00567
00568 if ( d->automatic ) {
00569
00570 bool tme = d->wordCount >= d->disableWordCount && d->errorCount * 100 >= d->disablePercentage * d->wordCount;
00571 if ( d->active && tme )
00572 d->active = false;
00573 else if ( !d->active && !tme )
00574 d->active = true;
00575 }
00576 if ( d->active != savedActive ) {
00577 if ( d->wordCount > 1 )
00578 if ( d->active )
00579 emit activeChanged( i18n("As-you-type spell checking enabled.") );
00580 else
00581 emit activeChanged( i18n( "Too many misspelled words. "
00582 "As-you-type spell checking disabled." ) );
00583 d->completeRehighlightRequired = true;
00584 d->rehighlightRequest->start( 100, true );
00585 }
00586 }
00587
00588 void KDictSpellingHighlighter::slotKSpellNotResponding()
00589 {
00590 static int retries = 0;
00591 if (retries < 10) {
00592 if ( d->globalConfig )
00593 KDictSpellingHighlighter::dictionaryChanged();
00594 else
00595 slotLocalSpellConfigChanged();
00596 } else {
00597 setAutomatic( false );
00598 setActive( false );
00599 }
00600 ++retries;
00601 }
00602
00603 bool KDictSpellingHighlighter::eventFilter( QObject *o, QEvent *e)
00604 {
00605 if (o == textEdit() && (e->type() == QEvent::FocusIn)) {
00606 if ( d->globalConfig ) {
00607 QString skey = spellKey();
00608 if ( d->spell && d->spellKey != skey ) {
00609 d->spellKey = skey;
00610 KDictSpellingHighlighter::dictionaryChanged();
00611 }
00612 }
00613 }
00614
00615 if (o == textEdit() && (e->type() == QEvent::KeyPress)) {
00616 QKeyEvent *k = static_cast<QKeyEvent *>(e);
00617 d->autoReady = true;
00618 if (d->rehighlightRequest->isActive())
00619 d->rehighlightRequest->changeInterval( 500 );
00620 if ( k->key() == Key_Enter ||
00621 k->key() == Key_Return ||
00622 k->key() == Key_Up ||
00623 k->key() == Key_Down ||
00624 k->key() == Key_Left ||
00625 k->key() == Key_Right ||
00626 k->key() == Key_PageUp ||
00627 k->key() == Key_PageDown ||
00628 k->key() == Key_Home ||
00629 k->key() == Key_End ||
00630 (( k->state() & ControlButton ) &&
00631 ((k->key() == Key_A) ||
00632 (k->key() == Key_B) ||
00633 (k->key() == Key_E) ||
00634 (k->key() == Key_N) ||
00635 (k->key() == Key_P))) ) {
00636 if ( intraWordEditing() ) {
00637 setIntraWordEditing( false );
00638 d->completeRehighlightRequired = true;
00639 d->rehighlightRequest->start( 500, true );
00640 }
00641 if (d->checksDone != d->checksRequested) {
00642
00643
00644 d->completeRehighlightRequired = true;
00645 d->rehighlightRequest->start( 500, true );
00646 }
00647 } else {
00648 setIntraWordEditing( true );
00649 }
00650 if ( k->key() == Key_Space ||
00651 k->key() == Key_Enter ||
00652 k->key() == Key_Return ) {
00653 QTimer::singleShot( 0, this, SLOT( slotAutoDetection() ));
00654 }
00655 }
00656
00657 else if ( o == textEdit()->viewport() &&
00658 ( e->type() == QEvent::MouseButtonPress )) {
00659 d->autoReady = true;
00660 if ( intraWordEditing() ) {
00661 setIntraWordEditing( false );
00662 d->completeRehighlightRequired = true;
00663 d->rehighlightRequest->start( 0, true );
00664 }
00665 }
00666
00667 return false;
00668 }
00669
00670 #include "ksyntaxhighlighter.moc"