File indexing completed on 2024-04-28 03:58:07

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