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