File indexing completed on 2024-04-14 03:55:29

0001 /*
0002     SPDX-FileCopyrightText: 2003-2005 Anders Lund <anders@alweb.dk>
0003     SPDX-FileCopyrightText: 2001-2010 Christoph Cullmann <cullmann@kde.org>
0004     SPDX-FileCopyrightText: 2001 Charles Samuels <charles@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KATE_CMDS_H
0010 #define KATE_CMDS_H
0011 
0012 #include <KTextEditor/Command>
0013 
0014 #include <QStringList>
0015 
0016 class KCompletion;
0017 
0018 /**
0019  * The KateCommands namespace collects subclasses of KTextEditor::Command
0020  * for specific use in kate.
0021  */
0022 namespace KateCommands
0023 {
0024 /**
0025  * This KTextEditor::Command provides access to a lot of the core functionality
0026  * of kate part, settings, utilities, navigation etc.
0027  * it needs to get a kateview pointer, it will cast the kate::view pointer
0028  * hard to kateview
0029  */
0030 class CoreCommands : public KTextEditor::Command
0031 {
0032     CoreCommands()
0033         : KTextEditor::Command({QStringLiteral("indent"),
0034                                 QStringLiteral("unindent"),
0035                                 QStringLiteral("cleanindent"),
0036                                 QStringLiteral("fold"),
0037                                 QStringLiteral("tfold"),
0038                                 QStringLiteral("unfold"),
0039                                 QStringLiteral("comment"),
0040                                 QStringLiteral("uncomment"),
0041                                 QStringLiteral("goto"),
0042                                 QStringLiteral("kill-line"),
0043                                 QStringLiteral("set-tab-width"),
0044                                 QStringLiteral("set-replace-tabs"),
0045                                 QStringLiteral("set-show-tabs"),
0046                                 QStringLiteral("set-indent-width"),
0047                                 QStringLiteral("set-indent-mode"),
0048                                 QStringLiteral("set-auto-indent"),
0049                                 QStringLiteral("set-line-numbers"),
0050                                 QStringLiteral("set-folding-markers"),
0051                                 QStringLiteral("set-icon-border"),
0052                                 QStringLiteral("set-indent-pasted-text"),
0053                                 QStringLiteral("set-word-wrap"),
0054                                 QStringLiteral("set-word-wrap-column"),
0055                                 QStringLiteral("set-replace-tabs-save"),
0056                                 QStringLiteral("set-remove-trailing-spaces"),
0057                                 QStringLiteral("set-highlight"),
0058                                 QStringLiteral("set-mode"),
0059                                 QStringLiteral("set-show-indent"),
0060                                 QStringLiteral("print")})
0061     {
0062     }
0063 
0064     static CoreCommands *m_instance;
0065 
0066 public:
0067     ~CoreCommands() override
0068     {
0069         m_instance = nullptr;
0070     }
0071 
0072     /**
0073      * execute command
0074      * @param view view to use for execution
0075      * @param cmd cmd string
0076      * @param errorMsg error to return if no success
0077      * @return success
0078      */
0079     bool exec(class KTextEditor::View *view, const QString &cmd, QString &errorMsg);
0080 
0081     /**
0082      * execute command on given range
0083      * @param view view to use for execution
0084      * @param cmd cmd string
0085      * @param errorMsg error to return if no success
0086      * @param range range to execute command on
0087      * @return success
0088      */
0089     bool
0090     exec(class KTextEditor::View *view, const QString &cmd, QString &errorMsg, const KTextEditor::Range &range = KTextEditor::Range(-1, -0, -1, 0)) override;
0091 
0092     bool supportsRange(const QString &range) override;
0093 
0094     /** This command does not have help. @see KTextEditor::Command::help */
0095     bool help(class KTextEditor::View *, const QString &, QString &) override;
0096 
0097     /** override from KTextEditor::Command */
0098     KCompletion *completionObject(KTextEditor::View *, const QString &) override;
0099 
0100     static CoreCommands *self()
0101     {
0102         if (m_instance == nullptr) {
0103             m_instance = new CoreCommands();
0104         }
0105         return m_instance;
0106     }
0107 };
0108 
0109 /**
0110  * insert a unicode or ascii character
0111  * base 9+1: 1234
0112  * hex: 0x1234 or x1234
0113  * octal: 01231
0114  *
0115  * prefixed with "char:"
0116  **/
0117 class Character : public KTextEditor::Command
0118 {
0119     Character()
0120         : KTextEditor::Command({QStringLiteral("char")})
0121     {
0122     }
0123 
0124     static Character *m_instance;
0125 
0126 public:
0127     ~Character() override
0128     {
0129         m_instance = nullptr;
0130     }
0131 
0132     /**
0133      * execute command
0134      * @param view view to use for execution
0135      * @param cmd cmd string
0136      * @param errorMsg error to return if no success
0137      * @return success
0138      */
0139     bool
0140     exec(class KTextEditor::View *view, const QString &cmd, QString &errorMsg, const KTextEditor::Range &range = KTextEditor::Range(-1, -0, -1, 0)) override;
0141 
0142     /** This command does not have help. @see KTextEditor::Command::help */
0143     bool help(class KTextEditor::View *, const QString &, QString &) override;
0144 
0145     static Character *self()
0146     {
0147         if (m_instance == nullptr) {
0148             m_instance = new Character();
0149         }
0150         return m_instance;
0151     }
0152 };
0153 
0154 /**
0155  * insert the current date/time in the given format
0156  */
0157 class Date : public KTextEditor::Command
0158 {
0159     Date()
0160         : KTextEditor::Command({QStringLiteral("date")})
0161     {
0162     }
0163 
0164     static Date *m_instance;
0165 
0166 public:
0167     ~Date() override
0168     {
0169         m_instance = nullptr;
0170     }
0171 
0172     /**
0173      * execute command
0174      * @param view view to use for execution
0175      * @param cmd cmd string
0176      * @param errorMsg error to return if no success
0177      * @return success
0178      */
0179     bool
0180     exec(class KTextEditor::View *view, const QString &cmd, QString &errorMsg, const KTextEditor::Range &range = KTextEditor::Range(-1, -0, -1, 0)) override;
0181 
0182     /** This command does not have help. @see KTextEditor::Command::help */
0183     bool help(class KTextEditor::View *, const QString &, QString &) override;
0184 
0185     static Date *self()
0186     {
0187         if (m_instance == nullptr) {
0188             m_instance = new Date();
0189         }
0190         return m_instance;
0191     }
0192 };
0193 
0194 } // namespace KateCommands
0195 #endif