File indexing completed on 2024-04-28 05:49:11

0001 /*
0002     SPDX-FileCopyrightText: 2020 Kåre Särs <kare.sars@iki.fi>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "KateSearchCommand.h"
0008 #include "MatchModel.h"
0009 
0010 #include <KLocalizedString>
0011 
0012 KateSearchCommand::KateSearchCommand(QObject *parent)
0013     : KTextEditor::Command(QStringList() << QStringLiteral("grep") << QStringLiteral("newGrep") << QStringLiteral("search") << QStringLiteral("newSearch")
0014                                          << QStringLiteral("pgrep") << QStringLiteral("newPGrep") << QStringLiteral("preg"),
0015                            parent)
0016 {
0017 }
0018 
0019 void KateSearchCommand::setBusy(bool busy)
0020 {
0021     m_busy = busy;
0022 }
0023 
0024 bool KateSearchCommand::exec(KTextEditor::View * /*view*/, const QString &cmd, QString & /*msg*/, const KTextEditor::Range &)
0025 {
0026     if (m_busy) {
0027         return false;
0028     }
0029     // create a list of args
0030     QStringList args(cmd.split(QLatin1Char(' '), Qt::KeepEmptyParts));
0031 
0032     QString command = args.takeFirst();
0033     QString searchText = args.join(QLatin1Char(' '));
0034 
0035     if (command == QLatin1String("grep") || command == QLatin1String("newGrep")) {
0036         Q_EMIT setSearchPlace(MatchModel::Folder);
0037         Q_EMIT setCurrentFolder();
0038         if (command == QLatin1String("newGrep")) {
0039             Q_EMIT newTab();
0040         }
0041     }
0042 
0043     else if (command == QLatin1String("search") || command == QLatin1String("newSearch")) {
0044         Q_EMIT setSearchPlace(MatchModel::OpenFiles);
0045         if (command == QLatin1String("newSearch")) {
0046             Q_EMIT newTab();
0047         }
0048     }
0049 
0050     else if (command == QLatin1String("pgrep") || command == QLatin1String("newPGrep")) {
0051         Q_EMIT setSearchPlace(MatchModel::Project);
0052         if (command == QLatin1String("newPGrep")) {
0053             Q_EMIT newTab();
0054         }
0055     }
0056 
0057     /**
0058      * preg command
0059      * - Uses regex always
0060      * - Is case insensitive
0061      * - Will expand the tree on search completion if -e is used
0062      */
0063     else if (command == QLatin1String("preg")) {
0064         Q_EMIT setSearchPlace(MatchModel::Project);
0065         Q_EMIT setRegexMode(true);
0066         Q_EMIT setCaseInsensitive(true);
0067         Q_EMIT setExpandResults(true);
0068         Q_EMIT newTab();
0069     }
0070 
0071     Q_EMIT setSearchString(searchText);
0072     Q_EMIT startSearch();
0073 
0074     return true;
0075 }
0076 
0077 bool KateSearchCommand::help(KTextEditor::View * /*view*/, const QString &cmd, QString &msg)
0078 {
0079     if (cmd.startsWith(QLatin1String("grep"))) {
0080         msg = i18n("Usage: grep [pattern to search for in folder]");
0081     } else if (cmd.startsWith(QLatin1String("newGrep"))) {
0082         msg = i18n("Usage: newGrep [pattern to search for in folder]");
0083     }
0084 
0085     else if (cmd.startsWith(QLatin1String("search"))) {
0086         msg = i18n("Usage: search [pattern to search for in open files]");
0087     } else if (cmd.startsWith(QLatin1String("newSearch"))) {
0088         msg = i18n("Usage: search [pattern to search for in open files]");
0089     }
0090 
0091     else if (cmd.startsWith(QLatin1String("pgrep"))) {
0092         msg = i18n("Usage: pgrep [pattern to search for in current project]");
0093     } else if (cmd.startsWith(QLatin1String("newPGrep"))) {
0094         msg = i18n("Usage: newPGrep [pattern to search for in current project]");
0095     }
0096 
0097     else if (cmd.startsWith(QLatin1String("preg"))) {
0098         msg = i18n("Usage: preg [regex pattern to search for in current project]");
0099     }
0100 
0101     return true;
0102 }
0103 
0104 #include "moc_KateSearchCommand.cpp"
0105 
0106 // kate: space-indent on; indent-width 4; replace-tabs on;