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 "katespell.h"
00025 #include "katespell.moc"
00026
00027 #include "kateview.h"
00028
00029 #include <kaction.h>
00030 #include <kstdaction.h>
00031 #include <kspell.h>
00032 #include <ksconfig.h>
00033 #include <kdebug.h>
00034 #include <kmessagebox.h>
00035
00036 KateSpell::KateSpell( KateView* view )
00037 : QObject( view )
00038 , m_view (view)
00039 , m_kspell (0)
00040 {
00041 }
00042
00043 KateSpell::~KateSpell()
00044 {
00045
00046 if( m_kspell )
00047 {
00048 m_kspell->setAutoDelete(true);
00049 m_kspell->cleanUp();
00050 delete m_kspell;
00051 }
00052 }
00053
00054 void KateSpell::createActions( KActionCollection* ac )
00055 {
00056 KStdAction::spelling( this, SLOT(spellcheck()), ac );
00057 KAction *a = new KAction( i18n("Spelling (from cursor)..."), "spellcheck", 0, this, SLOT(spellcheckFromCursor()), ac, "tools_spelling_from_cursor" );
00058 a->setWhatsThis(i18n("Check the document's spelling from the cursor and forward"));
00059
00060 m_spellcheckSelection = new KAction( i18n("Spellcheck Selection..."), "spellcheck", 0, this, SLOT(spellcheckSelection()), ac, "tools_spelling_selection" );
00061 m_spellcheckSelection->setWhatsThis(i18n("Check spelling of the selected text"));
00062 }
00063
00064 void KateSpell::updateActions ()
00065 {
00066 m_spellcheckSelection->setEnabled (m_view->hasSelection ());
00067 }
00068
00069 void KateSpell::spellcheckFromCursor()
00070 {
00071 spellcheck( KateTextCursor(m_view->cursorLine(), m_view->cursorColumnReal()) );
00072 }
00073
00074 void KateSpell::spellcheckSelection()
00075 {
00076 KateTextCursor from( m_view->selStartLine(), m_view->selStartCol() );
00077 KateTextCursor to( m_view->selEndLine(), m_view->selEndCol() );
00078 spellcheck( from, to );
00079 }
00080
00081 void KateSpell::spellcheck()
00082 {
00083 spellcheck( KateTextCursor( 0, 0 ) );
00084 }
00085
00086 void KateSpell::spellcheck( const KateTextCursor &from, const KateTextCursor &to )
00087 {
00088 m_spellStart = from;
00089 m_spellEnd = to;
00090
00091 if ( to.line() == 0 && to.col() == 0 )
00092 {
00093 int lln = m_view->doc()->lastLine();
00094 m_spellEnd.setLine( lln );
00095 m_spellEnd.setCol( m_view->doc()->lineLength( lln ) );
00096 }
00097
00098 m_spellPosCursor = from;
00099 m_spellLastPos = 0;
00100
00101 QString mt = m_view->doc()->mimeType();
00102
00103 KSpell::SpellerType type = KSpell::Text;
00104 if ( mt == "text/x-tex" || mt == "text/x-latex" )
00105 type = KSpell::TeX;
00106 else if ( mt == "text/html" || mt == "text/xml" || mt == "text/docbook" || mt == "application/x-php")
00107 type = KSpell::HTML;
00108
00109 KSpellConfig *ksc = new KSpellConfig;
00110 QStringList ksEncodings;
00111 ksEncodings << "US-ASCII" << "ISO 8859-1" << "ISO 8859-2" << "ISO 8859-3"
00112 << "ISO 8859-4" << "ISO 8859-5" << "ISO 8859-7" << "ISO 8859-8"
00113 << "ISO 8859-9" << "ISO 8859-13" << "ISO 8859-15" << "UTF-8"
00114 << "KOI8-R" << "KOI8-U" << "CP1251" << "CP1255";
00115
00116 int enc = ksEncodings.findIndex( m_view->doc()->encoding() );
00117 if ( enc > -1 )
00118 ksc->setEncoding( enc );
00119
00120 kdDebug(13020)<<"KateSpell::spellCheck(): using encoding: "<<enc<<" ("<<ksEncodings[enc]<<") and KSpell::Type "<<type<<" (for '"<<mt<<"')"<<endl;
00121
00122 m_kspell = new KSpell( m_view, i18n("Spellcheck"),
00123 this, SLOT(ready(KSpell *)), ksc, true, true, type );
00124
00125 connect( m_kspell, SIGNAL(death()),
00126 this, SLOT(spellCleanDone()) );
00127
00128 connect( m_kspell, SIGNAL(misspelling(const QString&, const QStringList&, unsigned int)),
00129 this, SLOT(misspelling(const QString&, const QStringList&, unsigned int)) );
00130 connect( m_kspell, SIGNAL(corrected(const QString&, const QString&, unsigned int)),
00131 this, SLOT(corrected(const QString&, const QString&, unsigned int)) );
00132 connect( m_kspell, SIGNAL(done(const QString&)),
00133 this, SLOT(spellResult(const QString&)) );
00134 }
00135
00136 void KateSpell::ready(KSpell *)
00137 {
00138 m_kspell->setProgressResolution( 1 );
00139
00140 m_kspell->check( m_view->doc()->text( m_spellStart.line(), m_spellStart.col(), m_spellEnd.line(), m_spellEnd.col() ) );
00141
00142 kdDebug (13020) << "SPELLING READY STATUS: " << m_kspell->status () << endl;
00143 }
00144
00145 void KateSpell::locatePosition( uint pos, uint& line, uint& col )
00146 {
00147 uint remains;
00148
00149 while ( m_spellLastPos < pos )
00150 {
00151 remains = pos - m_spellLastPos;
00152 uint l = m_view->doc()->lineLength( m_spellPosCursor.line() ) - m_spellPosCursor.col();
00153 if ( l > remains )
00154 {
00155 m_spellPosCursor.setCol( m_spellPosCursor.col() + remains );
00156 m_spellLastPos = pos;
00157 }
00158 else
00159 {
00160 m_spellPosCursor.setLine( m_spellPosCursor.line() + 1 );
00161 m_spellPosCursor.setCol(0);
00162 m_spellLastPos += l + 1;
00163 }
00164 }
00165
00166 line = m_spellPosCursor.line();
00167 col = m_spellPosCursor.col();
00168 }
00169
00170 void KateSpell::misspelling( const QString& origword, const QStringList&, unsigned int pos )
00171 {
00172 uint line, col;
00173
00174 locatePosition( pos, line, col );
00175
00176 m_view->setCursorPositionInternal (line, col, 1);
00177 m_view->setSelection( line, col, line, col + origword.length() );
00178 }
00179
00180 void KateSpell::corrected( const QString& originalword, const QString& newword, unsigned int pos )
00181 {
00182 uint line, col;
00183
00184 locatePosition( pos, line, col );
00185
00186 m_view->doc()->removeText( line, col, line, col + originalword.length() );
00187 m_view->doc()->insertText( line, col, newword );
00188 }
00189
00190 void KateSpell::spellResult( const QString& )
00191 {
00192 m_view->clearSelection();
00193 m_kspell->cleanUp();
00194 }
00195
00196 void KateSpell::spellCleanDone()
00197 {
00198 KSpell::spellStatus status = m_kspell->status();
00199
00200 if( status == KSpell::Error ) {
00201 KMessageBox::sorry( 0,
00202 i18n("The spelling program could not be started. "
00203 "Please make sure you have set the correct spelling program "
00204 "and that it is properly configured and in your PATH."));
00205 } else if( status == KSpell::Crashed ) {
00206 KMessageBox::sorry( 0,
00207 i18n("The spelling program seems to have crashed."));
00208 }
00209
00210 delete m_kspell;
00211 m_kspell = 0;
00212
00213 kdDebug (13020) << "SPELLING END" << endl;
00214 }
00215
00216
00217
00218