24 #include "katespell.h"
25 #include "katespell.moc"
29 #include <tdeaction.h>
30 #include <kstdaction.h>
34 #include <tdemessagebox.h>
36 KateSpell::KateSpell( KateView* view )
43 KateSpell::~KateSpell()
48 m_tdespell->setAutoDelete(
true);
49 m_tdespell->cleanUp();
57 TDEAction *a =
new TDEAction( i18n(
"Spelling (from cursor)..."),
"tools-check-spelling", 0,
this, TQ_SLOT(spellcheckFromCursor()), ac,
"tools_spelling_from_cursor" );
58 a->
setWhatsThis(i18n(
"Check the document's spelling from the cursor and forward"));
60 m_spellcheckSelection =
new TDEAction( i18n(
"Spellcheck Selection..."),
"tools-check-spelling", 0,
this, TQ_SLOT(spellcheckSelection()), ac,
"tools_spelling_selection" );
61 m_spellcheckSelection->setWhatsThis(i18n(
"Check spelling of the selected text"));
64 void KateSpell::updateActions ()
66 m_spellcheckSelection->setEnabled (m_view->hasSelection ());
69 void KateSpell::spellcheckFromCursor()
71 spellcheck(
KateTextCursor(m_view->cursorLine(), m_view->cursorColumnReal()) );
74 void KateSpell::spellcheckSelection()
76 KateTextCursor from( m_view->selStartLine(), m_view->selStartCol() );
78 spellcheck( from, to );
81 void KateSpell::spellcheck()
91 if ( to.line() == 0 && to.col() == 0 )
93 int lln = m_view->doc()->lastLine();
94 m_spellEnd.setLine( lln );
95 m_spellEnd.setCol( m_view->doc()->lineLength( lln ) );
98 m_spellPosCursor = from;
101 TQString mt = m_view->doc()->mimeType();
104 if ( mt ==
"text/x-tex" || mt ==
"text/x-latex" )
106 else if ( mt ==
"text/html" || mt ==
"text/xml" || mt ==
"text/docbook" || mt ==
"application/x-php")
110 TQStringList ksEncodings;
111 ksEncodings <<
"US-ASCII" <<
"ISO 8859-1" <<
"ISO 8859-2" <<
"ISO 8859-3"
112 <<
"ISO 8859-4" <<
"ISO 8859-5" <<
"ISO 8859-7" <<
"ISO 8859-8"
113 <<
"ISO 8859-9" <<
"ISO 8859-13" <<
"ISO 8859-15" <<
"UTF-8"
114 <<
"KOI8-R" <<
"KOI8-U" <<
"CP1251" <<
"CP1255";
116 int enc = ksEncodings.findIndex( m_view->doc()->encoding() );
119 ksc->setEncoding( enc );
120 kdDebug(13020)<<
"KateSpell::spellCheck(): using encoding: "<<enc<<
" ("<<ksEncodings[enc]<<
") and KSpell::Type "<<type<<
" (for '"<<mt<<
"')"<<
endl;
123 kdDebug(13020)<<
"KateSpell::spellCheck(): using encoding: "<<enc<<
" and KSpell::Type "<<type<<
" (for '"<<mt<<
"')"<<
endl;
125 m_tdespell =
new KSpell( m_view, i18n(
"Spellcheck"),
126 this, TQ_SLOT(ready(
KSpell *)), ksc,
true,
true, type );
128 connect( m_tdespell, TQ_SIGNAL(death()),
129 this, TQ_SLOT(spellCleanDone()) );
131 connect( m_tdespell, TQ_SIGNAL(misspelling(
const TQString&,
const TQStringList&,
unsigned int)),
132 this, TQ_SLOT(misspelling(
const TQString&,
const TQStringList&,
unsigned int)) );
133 connect( m_tdespell, TQ_SIGNAL(corrected(
const TQString&,
const TQString&,
unsigned int)),
134 this, TQ_SLOT(corrected(
const TQString&,
const TQString&,
unsigned int)) );
135 connect( m_tdespell, TQ_SIGNAL(done(
const TQString&)),
136 this, TQ_SLOT(spellResult(
const TQString&)) );
139 void KateSpell::ready(
KSpell *)
141 m_tdespell->setProgressResolution( 1 );
143 m_tdespell->check( m_view->doc()->text( m_spellStart.line(), m_spellStart.col(), m_spellEnd.line(), m_spellEnd.col() ) );
145 kdDebug (13020) <<
"SPELLING READY STATUS: " << m_tdespell->status () <<
endl;
148 void KateSpell::locatePosition( uint pos, uint& line, uint& col )
152 while ( m_spellLastPos < pos )
154 remains = pos - m_spellLastPos;
155 uint l = m_view->doc()->lineLength( m_spellPosCursor.line() ) - m_spellPosCursor.col();
158 m_spellPosCursor.setCol( m_spellPosCursor.col() + remains );
159 m_spellLastPos = pos;
163 m_spellPosCursor.setLine( m_spellPosCursor.line() + 1 );
164 m_spellPosCursor.setCol(0);
165 m_spellLastPos += l + 1;
169 line = m_spellPosCursor.line();
170 col = m_spellPosCursor.col();
173 void KateSpell::misspelling(
const TQString& origword,
const TQStringList&,
unsigned int pos )
177 locatePosition( pos, line, col );
179 m_view->setCursorPositionInternal (line, col, 1);
180 m_view->setSelection( line, col, line, col + origword.length() );
183 void KateSpell::corrected(
const TQString& originalword,
const TQString& newword,
unsigned int pos )
187 locatePosition( pos, line, col );
189 m_view->doc()->removeText( line, col, line, col + originalword.length() );
190 m_view->doc()->insertText( line, col, newword );
193 void KateSpell::spellResult(
const TQString& )
195 m_view->clearSelection();
196 m_tdespell->cleanUp();
199 void KateSpell::spellCleanDone()
203 if( status == KSpell::Error ) {
205 i18n(
"The spelling program could not be started. "
206 "Please make sure you have set the correct spelling program "
207 "and that it is properly configured and in your PATH."));
208 }
else if( status == KSpell::Crashed ) {
210 i18n(
"The spelling program seems to have crashed."));
static void sorry(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, int options=Notify)
Simple cursor class with no document pointer.
virtual void setWhatsThis(const TQString &text)
kndbgstream & endl(kndbgstream &s)
kdbgstream kdDebug(int area=0)
TDEAction * spelling(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)