26 #include <kstaticdeleter.h>
35 static KStaticDeleter<Filter> sd;
36 static Filter* defFilter = 0;
47 Filter* Filter::defaultFilter()
50 sd.setObject( defFilter,
new Filter() );
60 : m_currentPosition( 0 )
71 void Filter::setSettings( Settings *conf )
76 Settings *Filter::settings()
const
81 void Filter::restart()
83 m_currentPosition = 0;
86 void Filter::setBuffer(
const TQString& buffer )
89 m_currentPosition = 0;
92 TQString Filter::buffer()
const
97 bool Filter::atEnd()
const
99 if ( m_currentPosition >= m_buffer.length() ) {
105 Word Filter::nextWord()
const
107 TQChar currentChar = skipToLetter( m_currentPosition );
109 if ( m_currentPosition >= m_buffer.length() ) {
110 return Filter::end();
113 bool allUppercase = currentChar.category() & TQChar::Letter_Uppercase;
114 bool runTogether =
false;
117 int start = m_currentPosition;
118 while ( currentChar.isLetter() ) {
119 if ( currentChar.category() & TQChar::Letter_Lowercase )
120 allUppercase =
false;
129 foundWord += currentChar;
131 currentChar = m_buffer[ m_currentPosition ];
134 if ( shouldBeSkipped( allUppercase, runTogether, foundWord ) )
137 return Word( foundWord, start );
140 Word Filter::previousWord()
const
142 while ( !m_buffer[ m_currentPosition ].isLetter() &&
143 m_currentPosition != 0) {
147 if ( m_currentPosition == 0 ) {
148 return Filter::end();
152 int start = m_currentPosition;
153 while ( m_buffer[ start ].isLetter() ) {
154 foundWord.prepend( m_buffer[ m_currentPosition ] );
158 return Word( foundWord, start );
161 Word Filter::wordAtPosition(
unsigned int pos )
const
163 if ( pos > m_buffer.length() )
164 return Filter::end();
166 int currentPosition = pos - 1;
168 while ( currentPosition >= 0 &&
169 m_buffer[ currentPosition ].isLetter() ) {
170 foundWord.prepend( m_buffer[ currentPosition ] );
176 int start = (currentPosition < 0) ? 0 : ++currentPosition;
177 currentPosition = pos ;
178 if ( m_buffer[ currentPosition ].isLetter() ) {
179 while ( m_buffer[ currentPosition ].isLetter() ) {
180 foundWord.append( m_buffer[ currentPosition ] );
185 return Word( foundWord, start );
189 void Filter::setCurrentPosition(
int i )
191 m_currentPosition = i;
195 while ( m_buffer[m_currentPosition].isLetter() && m_currentPosition > 0 )
199 int Filter::currentPosition()
const
201 return m_currentPosition;
204 void Filter::replace(
const Word& w,
const TQString& newWord)
206 int oldLen = w.word.length();
207 int newLen = newWord.length();
209 if ( oldLen != newLen && m_currentPosition > w.start ) {
210 if ( m_currentPosition > w.start ) {
211 int len = newLen - oldLen;
212 m_currentPosition += len;
215 m_buffer = m_buffer.replace( w.start, oldLen, newWord );
218 TQString Filter::context()
const
223 int signedPosition = m_currentPosition;
224 bool begin = ( (signedPosition - len/2)<=0 ) ? true :
false;
227 TQString buffer = m_buffer;
228 Word word = wordAtPosition( m_currentPosition );
229 buffer = buffer.replace( word.start, word.word.length(),
230 TQString(
"<b>%1</b>" ).arg( word.word ) );
234 context = TQString(
"%1...")
235 .arg( buffer.mid( 0, len ) );
237 context = TQString(
"...%1..." )
238 .arg( buffer.mid( m_currentPosition - 20, len ) );
240 context = context.replace(
'\n',
' ' );
245 bool Filter::trySkipLinks()
const
247 TQChar currentChar = m_buffer[ m_currentPosition ];
249 uint length = m_buffer.length();
251 if ( currentChar ==
':' &&
252 ( m_buffer[ ++m_currentPosition] ==
'/' || ( m_currentPosition + 1 ) >= length ) ) {
254 while ( !m_buffer[ m_currentPosition++ ].isSpace() && m_currentPosition < length )
260 if ( currentChar ==
'@' ) {
261 while ( !m_buffer[ ++m_currentPosition ].isSpace() && m_currentPosition < length )
269 bool Filter::ignore(
const TQString& word )
const
272 return d->settings->ignore( word );
277 TQChar Filter::skipToLetter( uint &fromPosition )
const
280 TQChar currentChar = m_buffer[ fromPosition ];
281 while ( !currentChar.isLetter() &&
282 ++fromPosition < m_buffer.length() ) {
283 currentChar = m_buffer[ fromPosition ];
288 bool Filter::shouldBeSkipped(
bool wordWasUppercase,
bool wordWasRunTogether,
289 const TQString& foundWord )
const
291 bool checkUpper = ( d->settings ) ?
292 d->settings->checkUppercase () :
true;
293 bool skipRunTogether = ( d->settings ) ?
294 d->settings->skipRunTogether() :
true;
296 if ( trySkipLinks() )
299 if ( wordWasUppercase && !checkUpper )
302 if ( wordWasRunTogether && skipRunTogether )
305 return ignore( foundWord );
Settings * settings() const
Returns currently used Settings object.
Structure abstracts the word and its position in the parent text.