00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "katebookmarks.h"
00021 #include "katebookmarks.moc"
00022
00023 #include "katedocument.h"
00024 #include "kateview.h"
00025
00026 #include <klocale.h>
00027 #include <kaction.h>
00028 #include <kpopupmenu.h>
00029 #include <kstringhandler.h>
00030 #include <kdebug.h>
00031
00032 #include <qregexp.h>
00033 #include <qmemarray.h>
00034
00042 static void ssort( QMemArray<uint> &a, int max )
00043 {
00044 uint tmp, j, maxpos;
00045 for ( uint h = max; h >= 1; h-- )
00046 {
00047 maxpos = 0;
00048 for ( j = 0; j <= h; j++ )
00049 maxpos = a[j] > a[maxpos] ? j : maxpos;
00050 tmp = a[maxpos];
00051 a[maxpos] = a[h];
00052 a[h] = tmp;
00053 }
00054 }
00055
00056
00057
00058 KateBookmarks::KateBookmarks( KateView* view, Sorting sort )
00059 : QObject( view, "kate bookmarks" )
00060 , m_view( view )
00061 , m_sorting( sort )
00062 {
00063 connect (view->getDoc(), SIGNAL(marksChanged()), this, SLOT(marksChanged()));
00064 }
00065
00066 KateBookmarks::~KateBookmarks()
00067 {
00068 }
00069
00070 void KateBookmarks::createActions( KActionCollection* ac )
00071 {
00072 m_bookmarkMenu = new KActionMenu(
00073 i18n("&Bookmarks"), ac, "bookmarks" );
00074 m_bookmarkMenu->setWhatsThis(i18n("Bookmark manipulation"));
00075
00076
00077 m_bookmarkToggle = new KAction(
00078 i18n("Toggle &Bookmark"), "bookmark", CTRL+Key_B,
00079 this, SLOT(toggleBookmark()),
00080 ac, "bookmarks_toggle" );
00081 m_bookmarkToggle->setWhatsThis(i18n("If a line has no bookmark then add one, otherwise remove it."));
00082
00083 m_bookmarkClear = new KAction(
00084 i18n("Clear Bookmarks"), 0,
00085 this, SLOT(clearBookmarks()),
00086 ac, "bookmarks_clear");
00087 m_bookmarkClear->setWhatsThis(i18n("Remove all bookmarks of the current document."));
00088
00089 m_goNext = new KAction(
00090 i18n("Next Bookmark"), "next", ALT + Key_PageDown,
00091 this, SLOT(goNext()),
00092 ac, "bookmarks_next");
00093 m_goNext->setWhatsThis(i18n("Go to the next bookmark."));
00094
00095 m_goPrevious = new KAction(
00096 i18n("Previous Bookmark"), "previous", ALT + Key_PageUp,
00097 this, SLOT(goPrevious()),
00098 ac, "bookmarks_previous");
00099 m_goPrevious->setWhatsThis(i18n("Go to the previous bookmark."));
00100
00101 QPopupMenu *m = (QPopupMenu*)m_bookmarkMenu->popupMenu ();
00102
00103
00104 connect( m, SIGNAL(aboutToShow()),
00105 this, SLOT(bookmarkMenuAboutToShow()));
00106
00107
00108
00109
00110 connect( m, SIGNAL(aboutToHide()),
00111 this, SLOT(bookmarkMenuAboutToHide()) );
00112
00113 bookmarkMenuAboutToHide();
00114 marksChanged ();
00115 }
00116
00117 void KateBookmarks::toggleBookmark ()
00118 {
00119 uint mark = m_view->getDoc()->mark( m_view->cursorLine() );
00120 if( mark & KTextEditor::MarkInterface::markType01 )
00121 m_view->getDoc()->removeMark( m_view->cursorLine(),
00122 KTextEditor::MarkInterface::markType01 );
00123 else
00124 m_view->getDoc()->addMark( m_view->cursorLine(),
00125 KTextEditor::MarkInterface::markType01 );
00126 }
00127
00128 void KateBookmarks::clearBookmarks ()
00129 {
00130 QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00131 for (uint i=0; i < m.count(); i++)
00132 m_view->getDoc()->removeMark( m.at(i)->line, KTextEditor::MarkInterface::markType01 );
00133
00134
00135 marksChanged ();
00136 }
00137
00138 void KateBookmarks::bookmarkMenuAboutToShow()
00139 {
00140 QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00141
00142 m_bookmarkMenu->popupMenu()->clear();
00143 m_bookmarkMenu->insert ( m_bookmarkToggle );
00144 m_bookmarkMenu->insert ( m_bookmarkClear );
00145
00146 KTextEditor::Mark *next = 0;
00147 KTextEditor::Mark *prev = 0;
00148 uint line = m_view->cursorLine();
00149
00150 const QRegExp re("&(?!&)");
00151
00152 int idx( -1 );
00153 QMemArray<uint> sortArray( m.count() );
00154 QPtrListIterator<KTextEditor::Mark> it( m );
00155 kdDebug()<<"Redoing bookmarks menu, "<<it.count()<<" bookmarks in file"<<endl;
00156 if ( it.count() > 0 )
00157 m_bookmarkMenu->popupMenu()->insertSeparator();
00158 for( int i = 0; *it; ++it, ++i ) {
00159 if( (*it)->type & KTextEditor::MarkInterface::markType01 ) {
00160 QString bText = KStringHandler::rEmSqueeze
00161 ( m_view->getDoc()->textLine( (*it)->line ),
00162 m_bookmarkMenu->popupMenu()->fontMetrics(), 32 );
00163 bText.replace(re, "&&");
00164 if ( m_sorting == Position )
00165 {
00166 sortArray[i] = (*it)->line;
00167 ssort( sortArray, i );
00168 idx = sortArray.find( (*it)->line ) + 3;
00169 }
00170 m_bookmarkMenu->popupMenu()->insertItem(
00171 QString("%1 - \"%2\"").arg( (*it)->line+1 ).arg( bText ),
00172 m_view, SLOT (gotoLineNumber(int)), 0, (*it)->line, idx );
00173 if ( (*it)->line < line )
00174 {
00175 if ( ! prev || prev->line < (*it)->line )
00176 prev = (*it);
00177 }
00178 else if ( (*it)->line > line )
00179 {
00180 if ( ! next || next->line > (*it)->line )
00181 next = (*it);
00182 }
00183 }
00184 }
00185
00186 idx = 3;
00187 if ( next )
00188 {
00189 m_goNext->setText( i18n("&Next: %1 - \"%2\"").arg( next->line + 1 )
00190 .arg( KStringHandler::rsqueeze( m_view->getDoc()->textLine( next->line ), 24 ) ) );
00191 m_bookmarkMenu->insert ( m_goNext, idx );
00192 idx++;
00193 }
00194 if ( prev )
00195 {
00196 m_goPrevious->setText( i18n("&Previous: %1 - \"%2\"").arg(prev->line + 1 )
00197 .arg( KStringHandler::rsqueeze( m_view->getDoc()->textLine( prev->line ), 24 ) ) );
00198 m_bookmarkMenu->insert ( m_goPrevious, idx );
00199 idx++;
00200 }
00201 if ( next || prev )
00202 m_bookmarkMenu->popupMenu()->insertSeparator( idx );
00203 }
00204
00205
00206
00207
00208 void KateBookmarks::bookmarkMenuAboutToHide()
00209 {
00210
00211 m_bookmarkMenu->insert ( m_bookmarkToggle );
00212 m_bookmarkMenu->insert ( m_bookmarkClear );
00213 m_goNext->setText( i18n("Next Bookmark") );
00214 m_bookmarkMenu->insert ( m_goNext );
00215 m_goPrevious->setText( i18n("Previous Bookmark") );
00216 m_bookmarkMenu->insert (m_goPrevious );
00217 }
00218
00219 void KateBookmarks::goNext()
00220 {
00221 QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00222 if (m.isEmpty())
00223 return;
00224
00225 uint line = m_view->cursorLine();
00226 int found = -1;
00227
00228 for (uint z=0; z < m.count(); z++)
00229 {
00230 if ((m.at(z)->line > line) && ((found == -1) || (uint(found) > m.at(z)->line)))
00231 found = m.at(z)->line;
00232 }
00233
00234 if (found != -1)
00235 m_view->gotoLineNumber ( found );
00236 }
00237
00238 void KateBookmarks::goPrevious()
00239 {
00240 QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00241 if (m.isEmpty())
00242 return;
00243
00244 uint line = m_view->cursorLine();
00245 int found = -1;
00246
00247 for (uint z=0; z < m.count(); z++)
00248 {
00249 if ((m.at(z)->line < line) && ((found == -1) || (uint(found) < m.at(z)->line)))
00250 found = m.at(z)->line;
00251 }
00252
00253 if (found != -1)
00254 m_view->gotoLineNumber ( found );
00255 }
00256
00257 void KateBookmarks::marksChanged ()
00258 {
00259 m_bookmarkClear->setEnabled( !m_view->getDoc()->marks().isEmpty() );
00260 }
00261
00262