summaryrefslogtreecommitdiffstats
path: root/kate/part/katespell.cpp
blob: 84b4337d9dde3fa91d980b2bef241086887b4ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* This file is part of the KDE libraries
   Copyright (C) 2004-2005 Anders Lund <[email protected]>
   Copyright (C) 2003 Clarence Dang <[email protected]>
   Copyright (C) 2002 John Firebaugh <[email protected]>
   Copyright (C) 2001-2004 Christoph Cullmann <[email protected]>
   Copyright (C) 2001 Joseph Wenninger <[email protected]>
   Copyright (C) 1999 Jochen Wilhelmy <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "katespell.h"
#include "katespell.moc"

#include "kateview.h"

#include <tdeaction.h>
#include <kstdaction.h>
#include <tdespell.h>
#include <ksconfig.h>
#include <kdebug.h>
#include <tdemessagebox.h>

KateSpell::KateSpell( KateView* view )
  : TQObject( view )
  , m_view (view)
  , m_tdespell (0)
{
}

KateSpell::~KateSpell()
{
  // tdespell stuff
  if( m_tdespell )
  {
    m_tdespell->setAutoDelete(true);
    m_tdespell->cleanUp(); // need a way to wait for this to complete
    delete m_tdespell;
  }
}

void KateSpell::createActions( TDEActionCollection* ac )
{
   KStdAction::spelling( this, TQT_SLOT(spellcheck()), ac );
   TDEAction *a = new TDEAction( i18n("Spelling (from cursor)..."), "tools-check-spelling", 0, this, TQT_SLOT(spellcheckFromCursor()), ac, "tools_spelling_from_cursor" );
   a->setWhatsThis(i18n("Check the document's spelling from the cursor and forward"));

   m_spellcheckSelection = new TDEAction( i18n("Spellcheck Selection..."), "tools-check-spelling", 0, this, TQT_SLOT(spellcheckSelection()), ac, "tools_spelling_selection" );
   m_spellcheckSelection->setWhatsThis(i18n("Check spelling of the selected text"));
}

void KateSpell::updateActions ()
{
  m_spellcheckSelection->setEnabled (m_view->hasSelection ());
}

void KateSpell::spellcheckFromCursor()
{
  spellcheck( KateTextCursor(m_view->cursorLine(), m_view->cursorColumnReal()) );
}

void KateSpell::spellcheckSelection()
{
  KateTextCursor from( m_view->selStartLine(), m_view->selStartCol() );
  KateTextCursor to( m_view->selEndLine(), m_view->selEndCol() );
  spellcheck( from, to );
}

void KateSpell::spellcheck()
{
  spellcheck( KateTextCursor( 0, 0 ) );
}

void KateSpell::spellcheck( const KateTextCursor &from, const KateTextCursor &to )
{
  m_spellStart = from;
  m_spellEnd = to;

  if ( to.line() == 0 && to.col() == 0 )
  {
    int lln = m_view->doc()->lastLine();
    m_spellEnd.setLine( lln );
    m_spellEnd.setCol( m_view->doc()->lineLength( lln ) );
  }

  m_spellPosCursor = from;
  m_spellLastPos = 0;

  TQString mt = m_view->doc()->mimeType()/*->name()*/;

  KSpell::SpellerType type = KSpell::Text;
  if ( mt == "text/x-tex" || mt == "text/x-latex" )
    type = KSpell::TeX;
  else if ( mt == "text/html" || mt == "text/xml" || mt == "text/docbook" || mt == "application/x-php")
    type = KSpell::HTML;

  KSpellConfig *ksc = new KSpellConfig;
  TQStringList ksEncodings;
  ksEncodings << "US-ASCII" << "ISO 8859-1" << "ISO 8859-2" << "ISO 8859-3"
      << "ISO 8859-4" << "ISO 8859-5" << "ISO 8859-7" << "ISO 8859-8"
      << "ISO 8859-9" << "ISO 8859-13" << "ISO 8859-15" << "UTF-8"
      << "KOI8-R" << "KOI8-U" << "CP1251" << "CP1255";

  int enc = ksEncodings.findIndex( m_view->doc()->encoding() );
  if ( enc > -1 )
  {
    ksc->setEncoding( enc );
    kdDebug(13020)<<"KateSpell::spellCheck(): using encoding: "<<enc<<" ("<<ksEncodings[enc]<<") and KSpell::Type "<<type<<" (for '"<<mt<<"')"<<endl;
  }
  else
    kdDebug(13020)<<"KateSpell::spellCheck(): using encoding: "<<enc<<" and KSpell::Type "<<type<<" (for '"<<mt<<"')"<<endl;

  m_tdespell = new KSpell( m_view, i18n("Spellcheck"),
                         this, TQT_SLOT(ready(KSpell *)), ksc, true, true, type );

  connect( m_tdespell, TQT_SIGNAL(death()),
           this, TQT_SLOT(spellCleanDone()) );

  connect( m_tdespell, TQT_SIGNAL(misspelling(const TQString&, const TQStringList&, unsigned int)),
           this, TQT_SLOT(misspelling(const TQString&, const TQStringList&, unsigned int)) );
  connect( m_tdespell, TQT_SIGNAL(corrected(const TQString&, const TQString&, unsigned int)),
           this, TQT_SLOT(corrected(const TQString&, const TQString&, unsigned int)) );
  connect( m_tdespell, TQT_SIGNAL(done(const TQString&)),
           this, TQT_SLOT(spellResult(const TQString&)) );
}

void KateSpell::ready(KSpell *)
{
  m_tdespell->setProgressResolution( 1 );

  m_tdespell->check( m_view->doc()->text( m_spellStart.line(), m_spellStart.col(), m_spellEnd.line(), m_spellEnd.col() ) );

  kdDebug (13020) << "SPELLING READY STATUS: " << m_tdespell->status () << endl;
}

void KateSpell::locatePosition( uint pos, uint& line, uint& col )
{
  uint remains;

  while ( m_spellLastPos < pos )
  {
    remains = pos - m_spellLastPos;
    uint l = m_view->doc()->lineLength( m_spellPosCursor.line() ) - m_spellPosCursor.col();
    if ( l > remains )
    {
      m_spellPosCursor.setCol( m_spellPosCursor.col() + remains );
      m_spellLastPos = pos;
    }
    else
    {
      m_spellPosCursor.setLine( m_spellPosCursor.line() + 1 );
      m_spellPosCursor.setCol(0);
      m_spellLastPos += l + 1;
    }
  }

  line = m_spellPosCursor.line();
  col = m_spellPosCursor.col();
}

void KateSpell::misspelling( const TQString& origword, const TQStringList&, unsigned int pos )
{
  uint line, col;

  locatePosition( pos, line, col );

  m_view->setCursorPositionInternal (line, col, 1);
  m_view->setSelection( line, col, line, col + origword.length() );
}

void KateSpell::corrected( const TQString& originalword, const TQString& newword, unsigned int pos )
{
  uint line, col;

  locatePosition( pos, line, col );

  m_view->doc()->removeText( line, col, line, col + originalword.length() );
  m_view->doc()->insertText( line, col, newword );
}

void KateSpell::spellResult( const TQString& )
{
  m_view->clearSelection();
  m_tdespell->cleanUp();
}

void KateSpell::spellCleanDone()
{
  KSpell::spellStatus status = m_tdespell->status();

  if( status == KSpell::Error ) {
    KMessageBox::sorry( 0,
      i18n("The spelling program could not be started. "
           "Please make sure you have set the correct spelling program "
           "and that it is properly configured and in your PATH."));
  } else if( status == KSpell::Crashed ) {
    KMessageBox::sorry( 0,
      i18n("The spelling program seems to have crashed."));
  }

  delete m_tdespell;
  m_tdespell = 0;

  kdDebug (13020) << "SPELLING END" << endl;
}
//END