TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

katespell.cpp 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* This file is part of the KDE libraries
  2. Copyright (C) 2004-2005 Anders Lund <anders@alweb.dk>
  3. Copyright (C) 2003 Clarence Dang <dang@kde.org>
  4. Copyright (C) 2002 John Firebaugh <jfirebaugh@kde.org>
  5. Copyright (C) 2001-2004 Christoph Cullmann <cullmann@kde.org>
  6. Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
  7. Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Library General Public
  10. License version 2 as published by the Free Software Foundation.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public License
  16. along with this library; see the file COPYING.LIB. If not, write to
  17. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. Boston, MA 02110-1301, USA.
  19. */
  20. #include "katespell.h"
  21. #include "katespell.moc"
  22. #include "kateview.h"
  23. #include <tdeaction.h>
  24. #include <kstdaction.h>
  25. #include <tdespell.h>
  26. #include <ksconfig.h>
  27. #include <kdebug.h>
  28. #include <tdemessagebox.h>
  29. KateSpell::KateSpell( KateView* view )
  30. : TQObject( view )
  31. , m_view (view)
  32. , m_tdespell (0)
  33. {
  34. }
  35. KateSpell::~KateSpell()
  36. {
  37. // tdespell stuff
  38. if( m_tdespell )
  39. {
  40. m_tdespell->setAutoDelete(true);
  41. m_tdespell->cleanUp(); // need a way to wait for this to complete
  42. delete m_tdespell;
  43. }
  44. }
  45. void KateSpell::createActions( TDEActionCollection* ac )
  46. {
  47. KStdAction::spelling( this, TQT_SLOT(spellcheck()), ac );
  48. TDEAction *a = new TDEAction( i18n("Spelling (from cursor)..."), "spellcheck", 0, this, TQT_SLOT(spellcheckFromCursor()), ac, "tools_spelling_from_cursor" );
  49. a->setWhatsThis(i18n("Check the document's spelling from the cursor and forward"));
  50. m_spellcheckSelection = new TDEAction( i18n("Spellcheck Selection..."), "spellcheck", 0, this, TQT_SLOT(spellcheckSelection()), ac, "tools_spelling_selection" );
  51. m_spellcheckSelection->setWhatsThis(i18n("Check spelling of the selected text"));
  52. }
  53. void KateSpell::updateActions ()
  54. {
  55. m_spellcheckSelection->setEnabled (m_view->hasSelection ());
  56. }
  57. void KateSpell::spellcheckFromCursor()
  58. {
  59. spellcheck( KateTextCursor(m_view->cursorLine(), m_view->cursorColumnReal()) );
  60. }
  61. void KateSpell::spellcheckSelection()
  62. {
  63. KateTextCursor from( m_view->selStartLine(), m_view->selStartCol() );
  64. KateTextCursor to( m_view->selEndLine(), m_view->selEndCol() );
  65. spellcheck( from, to );
  66. }
  67. void KateSpell::spellcheck()
  68. {
  69. spellcheck( KateTextCursor( 0, 0 ) );
  70. }
  71. void KateSpell::spellcheck( const KateTextCursor &from, const KateTextCursor &to )
  72. {
  73. m_spellStart = from;
  74. m_spellEnd = to;
  75. if ( to.line() == 0 && to.col() == 0 )
  76. {
  77. int lln = m_view->doc()->lastLine();
  78. m_spellEnd.setLine( lln );
  79. m_spellEnd.setCol( m_view->doc()->lineLength( lln ) );
  80. }
  81. m_spellPosCursor = from;
  82. m_spellLastPos = 0;
  83. TQString mt = m_view->doc()->mimeType()/*->name()*/;
  84. KSpell::SpellerType type = KSpell::Text;
  85. if ( mt == "text/x-tex" || mt == "text/x-latex" )
  86. type = KSpell::TeX;
  87. else if ( mt == "text/html" || mt == "text/xml" || mt == "text/docbook" || mt == "application/x-php")
  88. type = KSpell::HTML;
  89. KSpellConfig *ksc = new KSpellConfig;
  90. TQStringList ksEncodings;
  91. ksEncodings << "US-ASCII" << "ISO 8859-1" << "ISO 8859-2" << "ISO 8859-3"
  92. << "ISO 8859-4" << "ISO 8859-5" << "ISO 8859-7" << "ISO 8859-8"
  93. << "ISO 8859-9" << "ISO 8859-13" << "ISO 8859-15" << "UTF-8"
  94. << "KOI8-R" << "KOI8-U" << "CP1251" << "CP1255";
  95. int enc = ksEncodings.findIndex( m_view->doc()->encoding() );
  96. if ( enc > -1 )
  97. {
  98. ksc->setEncoding( enc );
  99. kdDebug(13020)<<"KateSpell::spellCheck(): using encoding: "<<enc<<" ("<<ksEncodings[enc]<<") and KSpell::Type "<<type<<" (for '"<<mt<<"')"<<endl;
  100. }
  101. else
  102. kdDebug(13020)<<"KateSpell::spellCheck(): using encoding: "<<enc<<" and KSpell::Type "<<type<<" (for '"<<mt<<"')"<<endl;
  103. m_tdespell = new KSpell( m_view, i18n("Spellcheck"),
  104. this, TQT_SLOT(ready(KSpell *)), ksc, true, true, type );
  105. connect( m_tdespell, TQT_SIGNAL(death()),
  106. this, TQT_SLOT(spellCleanDone()) );
  107. connect( m_tdespell, TQT_SIGNAL(misspelling(const TQString&, const TQStringList&, unsigned int)),
  108. this, TQT_SLOT(misspelling(const TQString&, const TQStringList&, unsigned int)) );
  109. connect( m_tdespell, TQT_SIGNAL(corrected(const TQString&, const TQString&, unsigned int)),
  110. this, TQT_SLOT(corrected(const TQString&, const TQString&, unsigned int)) );
  111. connect( m_tdespell, TQT_SIGNAL(done(const TQString&)),
  112. this, TQT_SLOT(spellResult(const TQString&)) );
  113. }
  114. void KateSpell::ready(KSpell *)
  115. {
  116. m_tdespell->setProgressResolution( 1 );
  117. m_tdespell->check( m_view->doc()->text( m_spellStart.line(), m_spellStart.col(), m_spellEnd.line(), m_spellEnd.col() ) );
  118. kdDebug (13020) << "SPELLING READY STATUS: " << m_tdespell->status () << endl;
  119. }
  120. void KateSpell::locatePosition( uint pos, uint& line, uint& col )
  121. {
  122. uint remains;
  123. while ( m_spellLastPos < pos )
  124. {
  125. remains = pos - m_spellLastPos;
  126. uint l = m_view->doc()->lineLength( m_spellPosCursor.line() ) - m_spellPosCursor.col();
  127. if ( l > remains )
  128. {
  129. m_spellPosCursor.setCol( m_spellPosCursor.col() + remains );
  130. m_spellLastPos = pos;
  131. }
  132. else
  133. {
  134. m_spellPosCursor.setLine( m_spellPosCursor.line() + 1 );
  135. m_spellPosCursor.setCol(0);
  136. m_spellLastPos += l + 1;
  137. }
  138. }
  139. line = m_spellPosCursor.line();
  140. col = m_spellPosCursor.col();
  141. }
  142. void KateSpell::misspelling( const TQString& origword, const TQStringList&, unsigned int pos )
  143. {
  144. uint line, col;
  145. locatePosition( pos, line, col );
  146. m_view->setCursorPositionInternal (line, col, 1);
  147. m_view->setSelection( line, col, line, col + origword.length() );
  148. }
  149. void KateSpell::corrected( const TQString& originalword, const TQString& newword, unsigned int pos )
  150. {
  151. uint line, col;
  152. locatePosition( pos, line, col );
  153. m_view->doc()->removeText( line, col, line, col + originalword.length() );
  154. m_view->doc()->insertText( line, col, newword );
  155. }
  156. void KateSpell::spellResult( const TQString& )
  157. {
  158. m_view->clearSelection();
  159. m_tdespell->cleanUp();
  160. }
  161. void KateSpell::spellCleanDone()
  162. {
  163. KSpell::spellStatus status = m_tdespell->status();
  164. if( status == KSpell::Error ) {
  165. KMessageBox::sorry( 0,
  166. i18n("The spelling program could not be started. "
  167. "Please make sure you have set the correct spelling program "
  168. "and that it is properly configured and in your PATH."));
  169. } else if( status == KSpell::Crashed ) {
  170. KMessageBox::sorry( 0,
  171. i18n("The spelling program seems to have crashed."));
  172. }
  173. delete m_tdespell;
  174. m_tdespell = 0;
  175. kdDebug (13020) << "SPELLING END" << endl;
  176. }
  177. //END
  178. // kate: space-indent on; indent-width 2; replace-tabs on;