1 /*
2     SPDX-FileCopyrightText: 2008-2009 Erlend Hamberg <ehamberg@gmail.com>
3     SPDX-FileCopyrightText: 2009 Paul Gideon Dann <pdgiddie@gmail.com>
4     SPDX-FileCopyrightText: 2011 Svyatoslav Kuzmich <svatoslav1@gmail.com>
5     SPDX-FileCopyrightText: 2012-2013 Simon St James <kdedevel@etotheipiplusone.com>
6 
7     SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #ifndef KATEVI_MODE_BASE_H
11 #define KATEVI_MODE_BASE_H
12 
13 #include <ktexteditor/range.h>
14 #include <ktexteditor_export.h>
15 
16 #include "kateview.h"
17 #include <vimode/definitions.h>
18 #include <vimode/range.h>
19 
20 class QKeyEvent;
21 class QString;
22 class QRegularExpression;
23 class KateViewInternal;
24 namespace KTextEditor
25 {
26 class DocumentPrivate;
27 class ViewPrivate;
28 class Message;
29 }
30 
31 namespace KateVi
32 {
33 class InputModeManager;
34 
35 enum Direction { Up, Down, Left, Right, Next, Prev };
36 
37 class KTEXTEDITOR_EXPORT ModeBase : public QObject
38 {
39     Q_OBJECT
40 
41 public:
42     ModeBase() = default;
43     ~ModeBase() override = default;
44 
45     /**
46      * @return normal mode command accumulated so far
47      */
48     QString getVerbatimKeys() const;
49     virtual bool handleKeypress(const QKeyEvent *e) = 0;
50 
setCount(unsigned int count)51     void setCount(unsigned int count)
52     {
53         m_count = count;
54     }
setRegister(QChar reg)55     void setRegister(QChar reg)
56     {
57         m_register = reg;
58     }
59 
60     void error(const QString &errorMsg);
61     void message(const QString &msg);
62 
63     Range motionFindNext();
64     Range motionFindPrev();
65 
66     virtual void goToPos(const Range &r);
67     int getCount() const;
68 
69 protected:
70     // helper methods
71     void yankToClipBoard(QChar chosen_register, const QString &text);
72     bool deleteRange(Range &r, OperationMode mode = LineWise, bool addToRegister = true);
73     const QString getRange(Range &r, OperationMode mode = LineWise) const;
74     const QString getLine(int line = -1) const;
75     const QChar getCharUnderCursor() const;
76     const QString getWordUnderCursor() const;
77     const KTextEditor::Range getWordRangeUnderCursor() const;
78     KTextEditor::Cursor findNextWordStart(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
79     KTextEditor::Cursor findNextWORDStart(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
80     KTextEditor::Cursor findPrevWordStart(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
81     KTextEditor::Cursor findPrevWORDStart(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
82     KTextEditor::Cursor findPrevWordEnd(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
83     KTextEditor::Cursor findPrevWORDEnd(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
84     KTextEditor::Cursor findWordEnd(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
85     KTextEditor::Cursor findWORDEnd(int fromLine, int fromColumn, bool onlyCurrentLine = false) const;
86 
87     Range findSurroundingBrackets(const QChar &c1, const QChar &c2, bool inner, const QChar &nested1, const QChar &nested2) const;
88 
89     Range findSurrounding(const QRegularExpression &c1, const QRegularExpression &c2, bool inner = false) const;
90     Range findSurroundingQuotes(const QChar &c, bool inner = false) const;
91 
92     int findLineStartingWitchChar(const QChar &c, int count, bool forward = true) const;
93     void updateCursor(const KTextEditor::Cursor c) const;
94     const QChar getCharAtVirtualColumn(const QString &line, int virtualColumn, int tabWidht) const;
95 
96     void addToNumberUnderCursor(int count);
97 
98     Range goLineUp();
99     Range goLineDown();
100     Range goLineUpDown(int lines);
101     Range goVisualLineUpDown(int lines);
102 
103     unsigned int linesDisplayed() const;
104     void scrollViewLines(int l);
105 
isCounted()106     bool isCounted() const
107     {
108         return m_iscounted;
109     }
110 
111     bool startNormalMode();
112     bool startInsertMode();
113     bool startVisualMode();
114     bool startVisualLineMode();
115     bool startVisualBlockMode();
116     bool startReplaceMode();
117 
118     QChar getChosenRegister(const QChar &defaultReg) const;
119     QString getRegisterContent(const QChar &reg);
120     OperationMode getRegisterFlag(const QChar &reg) const;
121     void fillRegister(const QChar &reg, const QString &text, OperationMode flag = CharWise);
122 
123     void switchView(Direction direction = Next);
124 
125     KTextEditor::Cursor getNextJump(KTextEditor::Cursor) const;
126     KTextEditor::Cursor getPrevJump(KTextEditor::Cursor) const;
127 
doc()128     inline KTextEditor::DocumentPrivate *doc() const
129     {
130         return m_view->doc();
131     }
132 
133 protected:
134     QChar m_register;
135 
136     Range m_commandRange;
137     unsigned int m_count = 0;
138     int m_oneTimeCountOverride = -1;
139     bool m_iscounted = false;
140 
141     QString m_extraWordCharacters;
142     QString m_keysVerbatim;
143 
144     int m_stickyColumn = -1;
145     bool m_lastMotionWasVisualLineUpOrDown;
146     bool m_currentMotionWasVisualLineUpOrDown;
147 
148     KTextEditor::ViewPrivate *m_view;
149     KateViewInternal *m_viewInternal;
150     InputModeManager *m_viInputModeManager;
151 
152     // info message of vi mode
153     QPointer<KTextEditor::Message> m_infoMessage;
154 };
155 
156 }
157 
158 #endif
159