Commit 35a21894 authored by Kåre Särs's avatar Kåre Särs

Search plugin: Add the missing command interface

parent 87a45c67
......@@ -22,6 +22,8 @@
#include "plugin_search.moc"
#include <kate/application.h>
#include <ktexteditor/editor.h>
#include <ktexteditor/view.h>
#include <ktexteditor/document.h>
#include <kate/documentmanager.h>
......@@ -33,23 +35,43 @@
#include <kpluginloader.h>
#include <kaboutdata.h>
#include <kurlcompletion.h>
#include <QLineEdit>
K_PLUGIN_FACTORY(KatePluginSearchFactory, registerPlugin<KatePluginSearch>();)
K_EXPORT_PLUGIN(KatePluginSearchFactory(KAboutData("katesearch","katesearch",ki18n("Search in files"), "0.1", ki18n("Find in open files plugin"))))
KatePluginSearch::KatePluginSearch(QObject* parent, const QList<QVariant>&)
: Kate::Plugin((Kate::Application*)parent, "kate-search-plugin")
: Kate::Plugin((Kate::Application*)parent, "kate-search-plugin"),
m_searchCommand(0)
{
KGlobal::locale()->insertCatalog("katesearch");
KTextEditor::CommandInterface* iface =
qobject_cast<KTextEditor::CommandInterface*>(Kate::application()->editor());
if (iface) {
m_searchCommand = new KateSearchCommand(this);
iface->registerCommand(m_searchCommand);
}
}
KatePluginSearch::~KatePluginSearch()
{
KTextEditor::CommandInterface* iface =
qobject_cast<KTextEditor::CommandInterface*>(Kate::application()->editor());
if (iface && m_searchCommand) {
iface->unregisterCommand(m_searchCommand);
}
}
Kate::PluginView *KatePluginSearch::createView(Kate::MainWindow *mainWindow)
{
return new KatePluginSearchView(mainWindow, application());
KatePluginSearchView *view = new KatePluginSearchView(mainWindow, application());
connect(m_searchCommand, SIGNAL(setSearchPlace(int)), view, SLOT(setSearchPlace(int)));
connect(m_searchCommand, SIGNAL(setCurrentFolder()), view, SLOT(setCurrentFolder()));
connect(m_searchCommand, SIGNAL(setSearchString(QString)), view, SLOT(setSearchString(QString)));
connect(m_searchCommand, SIGNAL(startSearch()), view, SLOT(startSearch()));
return view;
}
......@@ -167,8 +189,15 @@ void KatePluginSearchView::toggleSearchView()
}
}
void KatePluginSearchView::setSearchString(const QString &pattern)
{
m_ui.searchCombo->lineEdit()->setText(pattern);
}
void KatePluginSearchView::startSearch()
{
mainWindow()->showToolView(m_toolView); // in case we are invoked from the command interface
if (m_ui.searchCombo->currentText().isEmpty()) {
// return pressed in the folder combo or filter combo
return;
......@@ -203,6 +232,11 @@ void KatePluginSearchView::toggleOptions(bool show)
m_ui.stackedWidget->setCurrentIndex((show) ? 1:0);
}
void KatePluginSearchView::setSearchPlace(int place)
{
m_ui.searchPlaceCombo->setCurrentIndex(place);
}
void KatePluginSearchView::searchPlaceChanged()
{
bool disable = (m_ui.searchPlaceCombo->currentIndex() == 0);
......@@ -309,5 +343,48 @@ void KatePluginSearchView::writeSessionConfig(KConfigBase* config, const QString
cg.writeEntry("CurrentFilter", m_ui.filterCombo->currentIndex());
}
// kate: space-indent on; indent-width 4; replace-tabs on;
KateSearchCommand::KateSearchCommand(QObject *parent)
: QObject(parent), KTextEditor::Command()
{
}
const QStringList& KateSearchCommand::cmds()
{
static QStringList sl = QStringList() << "grep" << "search";
return sl;
}
bool KateSearchCommand::exec (KTextEditor::View* /*view*/, const QString& cmd, QString& /*msg*/)
{
//create a list of args
QStringList args(cmd.split(' ', QString::KeepEmptyParts));
QString command = args.takeFirst();
QString searchText = args.join(QString(' '));
if (command == "grep") {
emit setSearchPlace(1);
emit setCurrentFolder();
}
else if (command == "search") {
emit setSearchPlace(0);
}
emit setSearchString(searchText);
emit startSearch();
return true;
}
bool KateSearchCommand::help (KTextEditor::View */*view*/, const QString &cmd, QString & msg)
{
if (cmd.startsWith("grep")) {
msg = i18n("Usage: grep [pattern to search for in folder]");
}
else if (cmd.startsWith("search")) {
msg = i18n("Usage: search [pattern to search for in open files]");
}
return true;
}
// kate: space-indent on; indent-width 4; replace-tabs on;
......@@ -23,6 +23,7 @@
#include <kate/mainwindow.h>
#include <kate/plugin.h>
#include <ktexteditor/commandinterface.h>
#include <kxmlguiclient.h>
#include <kaction.h>
......@@ -31,6 +32,7 @@
#include "search_open_files.h"
#include "search_folder.h"
class KateSearchCommand;
class KatePluginSearch : public Kate::Plugin
{
......@@ -41,6 +43,9 @@ public:
virtual ~KatePluginSearch();
Kate::PluginView *createView(Kate::MainWindow *mainWindow);
private:
KateSearchCommand* m_searchCommand;
};
......@@ -56,20 +61,24 @@ public:
virtual void readSessionConfig(KConfigBase* config, const QString& groupPrefix);
virtual void writeSessionConfig(KConfigBase* config, const QString& groupPrefix);
public Q_SLOTS:
void startSearch();
void setSearchString(const QString &pattern);
void setCurrentFolder();
void setSearchPlace(int place);
private Q_SLOTS:
void toggleSearchView();
void startSearch();
void toggleOptions(bool show);
void searchPlaceChanged();
void searchPatternChanged();
void setCurrentFolder();
void matchFound(const QString &fileName, int line, int column, const QString &lineContent);
void searchDone();
void itemSelected(QTreeWidgetItem *item);
private:
Ui::SearchDialog m_ui;
QWidget *m_toolView;
......@@ -80,6 +89,28 @@ private:
KAction *m_useRegExp;
};
class KateSearchCommand : public QObject, public KTextEditor::Command
{
Q_OBJECT
public:
KateSearchCommand(QObject *parent);
Q_SIGNALS:
void setSearchPlace(int place);
void setCurrentFolder();
void setSearchString(const QString &pattern);
void startSearch();
//
// KTextEditor::Command
//
public:
const QStringList &cmds ();
bool exec (KTextEditor::View *view, const QString &cmd, QString &msg);
bool help (KTextEditor::View *view, const QString &cmd, QString &msg);
};
#endif
// kate: space-indent on; indent-width 4; replace-tabs on;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment