File indexing completed on 2024-05-12 11:58:32

0001 /*
0002     SPDX-FileCopyrightText: 2008-2009 Erlend Hamberg <ehamberg@gmail.com>
0003     SPDX-FileCopyrightText: 2009 Paul Gideon Dann <pdgiddie@gmail.com>
0004     SPDX-FileCopyrightText: 2011 Svyatoslav Kuzmich <svatoslav1@gmail.com>
0005     SPDX-FileCopyrightText: 2012-2013 Simon St James <kdedevel@etotheipiplusone.com>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef KATEVI_MODE_BASE_H
0011 #define KATEVI_MODE_BASE_H
0012 
0013 #include <ktexteditor/range.h>
0014 #include <ktexteditor_export.h>
0015 
0016 #include "kateview.h"
0017 #include <vimode/definitions.h>
0018 #include <vimode/range.h>
0019 
0020 class QKeyEvent;
0021 class QString;
0022 class QRegularExpression;
0023 class KateViewInternal;
0024 namespace KTextEditor
0025 {
0026 class DocumentPrivate;
0027 class ViewPrivate;
0028 class Message;
0029 }
0030 
0031 namespace KateVi
0032 {
0033 class InputModeManager;
0034 
0035 enum Direction { Up, Down, Left, Right, Next, Prev };
0036 
0037 class KTEXTEDITOR_EXPORT ModeBase : public QObject
0038 {
0039     Q_OBJECT
0040 
0041 public:
0042     ModeBase() = default;
0043     ~ModeBase() override = default;
0044 
0045     /**
0046      * @return normal mode command accumulated so far
0047      */
0048     QString getVerbatimKeys() const;
0049     virtual bool handleKeypress(const QKeyEvent *e) = 0;
0050 
0051     void setCount(unsigned int count)
0052     {
0053         m_count = count;
0054     }
0055     void setRegister(QChar reg)
0056     {
0057         m_register = reg;
0058     }
0059 
0060     void error(const QString &errorMsg);
0061     void message(const QString &msg);
0062 
0063     Range motionFindNext();
0064     Range motionFindPrev();
0065 
0066     virtual void goToPos(const Range &r);
0067     int getCount() const;
0068 
0069 protected:
0070     // helper methods
0071     void yankToClipBoard(QChar chosen_register, const QString &text);
0072     bool deleteRange(Range &r, OperationMode mode = LineWise, bool addToRegister = true);
0073     const QString getRange(Range &r, OperationMode mode = LineWise) const;
0074     const QString getLine(int line = -1) const;
0075     const QChar getCharUnderCursor() const;
0076     const QString getWordUnderCursor() const;
0077     const KTextEditor::Range getWordRangeUnderCursor() const;
0078     KTextEditor::Cursor findNextWordStart(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
0079     KTextEditor::Cursor findNextWORDStart(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
0080     KTextEditor::Cursor findPrevWordStart(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
0081     KTextEditor::Cursor findPrevWORDStart(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
0082     KTextEditor::Cursor findPrevWordEnd(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
0083     KTextEditor::Cursor findPrevWORDEnd(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
0084     KTextEditor::Cursor findWordEnd(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
0085     KTextEditor::Cursor findWORDEnd(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
0086 
0087     Range findSurroundingBrackets(const QChar &c1, const QChar &c2, bool inner, const QChar &nested1, const QChar &nested2) const;
0088 
0089     Range findSurrounding(const QRegularExpression &c1, const QRegularExpression &c2, bool inner = false) const;
0090     Range findSurroundingQuotes(const QChar &c, bool inner = false) const;
0091 
0092     int findLineStartingWitchChar(const QChar &c, int count, bool forward = true) const;
0093     void updateCursor(const KTextEditor::Cursor c) const;
0094     static const QChar getCharAtVirtualColumn(const QString &line, int virtualColumn, int tabWidht);
0095 
0096     void addToNumberUnderCursor(int count);
0097 
0098     Range goLineUp();
0099     Range goLineDown();
0100     Range goLineUpDown(int lines);
0101     Range goVisualLineUpDown(int lines);
0102 
0103     unsigned int linesDisplayed() const;
0104     void scrollViewLines(int l);
0105 
0106     bool isCounted() const
0107     {
0108         return m_iscounted;
0109     }
0110 
0111     bool startNormalMode();
0112     bool startInsertMode();
0113     bool startVisualMode();
0114     bool startVisualLineMode();
0115     bool startVisualBlockMode();
0116     bool startReplaceMode();
0117 
0118     QChar getChosenRegister(const QChar &defaultReg) const;
0119     QString getRegisterContent(const QChar &reg);
0120     OperationMode getRegisterFlag(const QChar &reg) const;
0121     void fillRegister(const QChar &reg, const QString &text, OperationMode flag = CharWise);
0122 
0123     void switchView(Direction direction = Next);
0124 
0125     KTextEditor::Cursor getNextJump(KTextEditor::Cursor) const;
0126     KTextEditor::Cursor getPrevJump(KTextEditor::Cursor) const;
0127 
0128     inline KTextEditor::DocumentPrivate *doc() const
0129     {
0130         return m_view->doc();
0131     }
0132 
0133 protected:
0134     QChar m_register;
0135 
0136     Range m_commandRange;
0137     unsigned int m_count = 0;
0138     int m_oneTimeCountOverride = -1;
0139     bool m_iscounted = false;
0140 
0141     QString m_extraWordCharacters;
0142     QString m_keysVerbatim;
0143 
0144     int m_stickyColumn = -1;
0145     bool m_lastMotionWasVisualLineUpOrDown;
0146     bool m_currentMotionWasVisualLineUpOrDown;
0147 
0148     KTextEditor::ViewPrivate *m_view;
0149     KateViewInternal *m_viewInternal;
0150     InputModeManager *m_viInputModeManager;
0151 
0152     // info message of vi mode
0153     QPointer<KTextEditor::Message> m_infoMessage;
0154 };
0155 
0156 }
0157 
0158 #endif