29 #include "docwordcompletion.h"
31 #include <tdetexteditor/document.h>
32 #include <tdetexteditor/viewcursorinterface.h>
33 #include <tdetexteditor/editinterface.h>
34 #include <tdetexteditor/variableinterface.h>
36 #include <tdeapplication.h>
37 #include <tdeconfig.h>
39 #include <kgenericfactory.h>
41 #include <tdeaction.h>
42 #include <knotifyclient.h>
43 #include <tdeparts/part.h>
44 #include <kiconloader.h>
49 #include <tqspinbox.h>
53 #include <tqwhatsthis.h>
54 #include <tqcheckbox.h>
61 DocWordCompletionPlugin::DocWordCompletionPlugin( TQObject *parent,
64 : KTextEditor::Plugin ( (KTextEditor::Document*) parent, name )
69 void DocWordCompletionPlugin::readConfig()
72 config->
setGroup(
"DocWordCompletion Plugin" );
77 void DocWordCompletionPlugin::writeConfig()
80 config->
setGroup(
"DocWordCompletion Plugin");
85 void DocWordCompletionPlugin::addView(KTextEditor::View *view)
87 DocWordCompletionPluginView *nview =
new DocWordCompletionPluginView (m_treshold, m_autopopup, view,
"Document word completion");
88 m_views.append (nview);
91 void DocWordCompletionPlugin::removeView(KTextEditor::View *view)
93 for (uint z=0; z < m_views.count(); z++)
94 if (m_views.at(z)->parentClient() == view)
96 DocWordCompletionPluginView *nview = m_views.at(z);
97 m_views.remove (nview);
102 KTextEditor::ConfigPage* DocWordCompletionPlugin::configPage( uint, TQWidget *parent,
const char *name )
104 return new DocWordCompletionConfigPage(
this, parent, name );
107 TQString DocWordCompletionPlugin::configPageName( uint )
const
109 return i18n(
"Word Completion Plugin");
112 TQString DocWordCompletionPlugin::configPageFullName( uint )
const
114 return i18n(
"Configure the Word Completion Plugin");
118 TQPixmap DocWordCompletionPlugin::configPagePixmap( uint,
int size )
const
120 return UserIcon(
"kte_wordcompletion", size );
125 struct DocWordCompletionPluginViewPrivate
138 DocWordCompletionPluginView::DocWordCompletionPluginView( uint treshold,
bool autopopup, KTextEditor::View *view,
const char *name )
139 : TQObject( view,
name ),
142 d( new DocWordCompletionPluginViewPrivate )
144 d->treshold = treshold;
145 view->insertChildClient(
this );
148 (void)
new TDEAction( i18n(
"Reuse Word Above"), CTRL+Key_8,
this,
149 TQ_SLOT(completeBackwards()), actionCollection(),
"doccomplete_bw" );
150 (void)
new TDEAction( i18n(
"Reuse Word Below"), CTRL+Key_9,
this,
151 TQ_SLOT(completeForwards()), actionCollection(),
"doccomplete_fw" );
152 (void)
new TDEAction( i18n(
"Pop Up Completion List"), 0,
this,
153 TQ_SLOT(popupCompletionList()), actionCollection(),
"doccomplete_pu" );
154 (void)
new TDEAction( i18n(
"Shell Completion"), 0,
this,
155 TQ_SLOT(shellComplete()), actionCollection(),
"doccomplete_sh" );
156 d->autopopup =
new TDEToggleAction( i18n(
"Automatic Completion Popup"), 0,
this,
157 TQ_SLOT(toggleAutoPopup()), actionCollection(),
"enable_autopopup" );
159 d->autopopup->setChecked( autopopup );
162 setXMLFile(
"docwordcompletionui.rc");
164 KTextEditor::VariableInterface *vi = KTextEditor::variableInterface( view->document() );
167 TQString e = vi->variable(
"wordcompletion-autopopup");
169 d->autopopup->setEnabled( e ==
"true" );
171 connect( view->document(), TQ_SIGNAL(variableChanged(
const TQString &,
const TQString &)),
172 this, TQ_SLOT(slotVariableChanged(
const TQString &,
const TQString &)) );
176 void DocWordCompletionPluginView::settreshold( uint t )
181 void DocWordCompletionPluginView::completeBackwards()
186 void DocWordCompletionPluginView::completeForwards()
192 void DocWordCompletionPluginView::popupCompletionList( TQString w )
199 KTextEditor::CodeCompletionInterface *cci = codeCompletionInterface( m_view );
200 cci->showCompletionBox( allMatches( w ), w.length() );
203 void DocWordCompletionPluginView::toggleAutoPopup()
205 if ( d->autopopup->isChecked() ) {
206 if ( ! connect( m_view->document(), TQ_SIGNAL(charactersInteractivelyInserted(
int ,
int ,
const TQString&)),
207 this, TQ_SLOT(autoPopupCompletionList()) ))
209 connect( m_view->document(), TQ_SIGNAL(textChanged()),
this, TQ_SLOT(autoPopupCompletionList()) );
212 disconnect( m_view->document(), TQ_SIGNAL(textChanged()),
this, TQ_SLOT(autoPopupCompletionList()) );
213 disconnect( m_view->document(), TQ_SIGNAL(charactersInteractivelyInserted(
int ,
int ,
const TQString&)),
214 this, TQ_SLOT(autoPopupCompletionList()) );
220 void DocWordCompletionPluginView::autoPopupCompletionList()
222 if ( ! m_view->hasFocus() )
return;
224 if ( w.length() >= d->treshold )
226 popupCompletionList( w );
231 void DocWordCompletionPluginView::shellComplete()
234 KTextEditor::EditInterface * ei = KTextEditor::editInterface(m_view->document());
237 viewCursorInterface(m_view)->cursorPositionReal(&cline, &ccol);
238 TQString wrd = word();
242 TQValueList < KTextEditor::CompletionEntry > matches = allMatches(wrd);
243 if (matches.size() == 0)
245 TQString partial = findLongestUnique(matches);
246 if (partial.length() == wrd.length())
248 KTextEditor::CodeCompletionInterface * cci = codeCompletionInterface(m_view);
249 cci->showCompletionBox(matches, wrd.length());
253 partial.remove(0, wrd.length());
254 ei->insertText(cline, ccol, partial);
260 void DocWordCompletionPluginView::complete(
bool fw )
263 KTextEditor::EditInterface *ei = KTextEditor::editInterface( m_view->document() );
266 viewCursorInterface( m_view )->cursorPositionReal( &cline, &ccol );
267 TQString wrd = word();
271 int inc = fw ? 1 : -1;
279 if ( cline == d-> cline &&
280 ccol - d->lilen == d->ccol &&
281 wrd.endsWith( d->lastIns ) )
286 if ( ( fw && d->directionalPos == -1 ) ||
287 ( !fw && d->directionalPos == 1 ) )
290 ei->removeText( d->cline, d->ccol, d->cline, d->ccol + d->lilen );
296 d->directionalPos = 0;
307 d->directionalPos += inc;
316 d->col = ccol - wrd.length();
318 d->directionalPos = inc;
321 d->re.setPattern(
"\\b" + wrd +
"(\\w+)" );
323 TQString ln = ei->textLine( d->line );
328 d->re.search( ln, d->col ) :
329 d->re.searchRev( ln, d->col );
333 TQString m = d->re.cap( 1 );
334 if ( m != d->lastIns )
338 ei->removeText( d->cline, d->ccol, d->cline, d->ccol + d->lilen );
339 ei->insertText( d->cline, d->ccol, m );
342 d->lilen = m.length();
354 d->col += d->re.matchedLength();
363 ln = ei->textLine( d->line );
364 d->col = ln.length();
381 if ( (! fw && d->line == 0 ) || ( fw && d->line >= (uint)ei->numLines() ) )
389 ln = ei->textLine( d->line );
390 d->col = fw ? 0 : ln.length();
396 TQString DocWordCompletionPluginView::findLongestUnique(
const TQValueList < KTextEditor::CompletionEntry > &matches)
398 TQString partial = matches.front().text;
399 TQValueList < KTextEditor::CompletionEntry >::const_iterator i = matches.begin();
400 for (++i; i != matches.end(); ++i)
402 if (!(*i).text.startsWith(partial))
404 while(partial.length() > 0)
406 partial.remove(partial.length() - 1, 1);
407 if ((*i).text.startsWith(partial))
412 if (partial.length() == 0)
421 TQString DocWordCompletionPluginView::word()
424 viewCursorInterface( m_view )->cursorPositionReal( &cline, &ccol );
425 if ( ! ccol )
return TQString::null;
426 KTextEditor::EditInterface *ei = KTextEditor::editInterface( m_view->document() );
427 d->re.setPattern(
"\\b(\\w+)$" );
428 if ( d->re.searchRev(
429 ei->text( cline, 0, cline, ccol )
431 return TQString::null;
432 return d->re.cap( 1 );
437 TQValueList<KTextEditor::CompletionEntry> DocWordCompletionPluginView::allMatches(
const TQString &word )
439 TQValueList<KTextEditor::CompletionEntry> l;
442 d->re.setPattern(
"\\b("+word+
"\\w+)" );
444 KTextEditor::EditInterface *ei = KTextEditor::editInterface( m_view->document() );
448 viewCursorInterface( m_view )->cursorPositionReal( &cline, &ccol );
450 while( i < ei->numLines() )
452 s = ei->textLine( i );
456 pos = d->re.search( s, pos );
460 if ( i == cline && pos + word.length() == ccol )
462 pos += word.length();
468 seen.insert( m, &sawit );
469 KTextEditor::CompletionEntry e;
473 pos += d->re.matchedLength();
481 void DocWordCompletionPluginView::slotVariableChanged(
const TQString &var,
const TQString &val )
483 if ( var ==
"wordcompletion-autopopup" )
484 d->autopopup->setEnabled( val ==
"true" );
485 else if ( var ==
"wordcompletion-treshold" )
486 d->treshold = val.toInt();
491 DocWordCompletionConfigPage::DocWordCompletionConfigPage( DocWordCompletionPlugin *completion, TQWidget *parent,
const char *name )
492 : KTextEditor::ConfigPage( parent,
name )
495 TQVBoxLayout *lo =
new TQVBoxLayout(
this );
498 cbAutoPopup =
new TQCheckBox( i18n(
"Automatically &show completion list"),
this );
499 lo->addWidget( cbAutoPopup );
501 TQHBox *hb =
new TQHBox(
this );
504 TQLabel *l =
new TQLabel( i18n(
505 "Translators: This is the first part of two strings wich will comprise the "
506 "sentence 'Show completions when a word is at least N characters'. The first "
507 "part is on the right side of the N, which is represented by a spinbox "
508 "widget, followed by the second part: 'characters long'. Characters is a "
509 "ingeger number between and including 1 and 30. Feel free to leave the "
510 "second part of the sentence blank if it suits your language better. ",
511 "Show completions &when a word is at least"), hb );
512 sbAutoPopup =
new TQSpinBox( 1, 30, 1, hb );
513 l->setBuddy( sbAutoPopup );
514 lSbRight =
new TQLabel( i18n(
515 "This is the second part of two strings that will comprise teh sentence "
516 "'Show completions when a word is at least N characters'",
517 "characters long."), hb );
519 TQWhatsThis::add( cbAutoPopup, i18n(
520 "Enable the automatic completion list popup as default. The popup can "
521 "be disabled on a view basis from the 'Tools' menu.") );
522 TQWhatsThis::add( sbAutoPopup, i18n(
523 "Define the length a word should have before the completion list "
526 cbAutoPopup->setChecked( m_completion->autoPopupEnabled() );
527 sbAutoPopup->setValue( m_completion->treshold() );
532 void DocWordCompletionConfigPage::apply()
534 m_completion->setAutoPopupEnabled( cbAutoPopup->isChecked() );
535 m_completion->setTreshold( sbAutoPopup->value() );
536 m_completion->writeConfig();
539 void DocWordCompletionConfigPage::reset()
541 cbAutoPopup->setChecked( m_completion->autoPopupEnabled() );
542 sbAutoPopup->setValue( m_completion->treshold() );
545 void DocWordCompletionConfigPage::defaults()
547 cbAutoPopup->setChecked(
true );
548 sbAutoPopup->setValue( 3 );
553 #include "docwordcompletion.moc"
int readNumEntry(const TQString &pKey, int nDefault=0) const
bool readBoolEntry(const TQString &pKey, bool bDefault=false) const
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
void setGroup(const TQString &group)
void beep(const TQString &reason=TQString::null)
TQString name(StdAccel id)
const TDEShortcut & completion()