1 | /* |
---|---|
2 | SPDX-FileCopyrightText: KDE Developers |
3 | |
4 | SPDX-License-Identifier: LGPL-2.0-or-later |
5 | */ |
6 | |
7 | #include "katenormalinputmode.h" |
8 | #include "katecompletionwidget.h" |
9 | #include "kateconfig.h" |
10 | #include "katedocument.h" |
11 | #include "katerenderer.h" |
12 | #include "katesearchbar.h" |
13 | #include "kateview.h" |
14 | #include "kateviewinternal.h" |
15 | |
16 | #include <KLocalizedString> |
17 | |
18 | KateNormalInputMode::KateNormalInputMode(KateViewInternal *viewInternal) |
19 | : KateAbstractInputMode(viewInternal) |
20 | { |
21 | } |
22 | |
23 | void KateNormalInputMode::activate() |
24 | { |
25 | view()->activateEditActions(); |
26 | } |
27 | |
28 | void KateNormalInputMode::deactivate() |
29 | { |
30 | view()->deactivateEditActions(); |
31 | } |
32 | |
33 | void KateNormalInputMode::reset() |
34 | { |
35 | // nothing todo |
36 | } |
37 | |
38 | bool KateNormalInputMode::overwrite() const |
39 | { |
40 | return view()->doc()->config()->ovr(); |
41 | } |
42 | |
43 | void KateNormalInputMode::overwrittenChar(const QChar &) |
44 | { |
45 | // nothing todo |
46 | } |
47 | |
48 | void KateNormalInputMode::clearSelection() |
49 | { |
50 | view()->clearSelection(); |
51 | } |
52 | |
53 | bool KateNormalInputMode::stealKey(QKeyEvent *) |
54 | { |
55 | return false; |
56 | } |
57 | |
58 | KTextEditor::View::InputMode KateNormalInputMode::viewInputMode() const |
59 | { |
60 | return KTextEditor::View::NormalInputMode; |
61 | } |
62 | |
63 | QString KateNormalInputMode::viewInputModeHuman() const |
64 | { |
65 | return i18n("Normal"); |
66 | } |
67 | |
68 | KTextEditor::View::ViewMode KateNormalInputMode::viewMode() const |
69 | { |
70 | return view()->isOverwriteMode() ? KTextEditor::View::NormalModeOverwrite : KTextEditor::View::NormalModeInsert; |
71 | } |
72 | |
73 | QString KateNormalInputMode::viewModeHuman() const |
74 | { |
75 | return view()->isOverwriteMode() ? i18n("OVERWRITE") : i18n( "INSERT"); |
76 | } |
77 | |
78 | void KateNormalInputMode::gotFocus() |
79 | { |
80 | view()->activateEditActions(); |
81 | } |
82 | |
83 | void KateNormalInputMode::lostFocus() |
84 | { |
85 | view()->deactivateEditActions(); |
86 | } |
87 | |
88 | void KateNormalInputMode::readSessionConfig(const KConfigGroup &) |
89 | { |
90 | // do nothing |
91 | } |
92 | |
93 | void KateNormalInputMode::writeSessionConfig(KConfigGroup &) |
94 | { |
95 | // do nothing |
96 | } |
97 | |
98 | void KateNormalInputMode::updateConfig() |
99 | { |
100 | // do nothing |
101 | } |
102 | |
103 | void KateNormalInputMode::readWriteChanged(bool) |
104 | { |
105 | // inform search bar |
106 | if (m_searchBar) { |
107 | m_searchBar->slotReadWriteChanged(); |
108 | } |
109 | } |
110 | |
111 | void KateNormalInputMode::find() |
112 | { |
113 | KateSearchBar *const bar = searchBar(mode: IncrementalSearchBar); |
114 | view()->bottomViewBar()->addBarWidget(newBarWidget: bar); |
115 | view()->bottomViewBar()->showBarWidget(barWidget: bar); |
116 | bar->setFocus(); |
117 | } |
118 | |
119 | void KateNormalInputMode::findSelectedForwards() |
120 | { |
121 | searchBar(mode: IncrementalSearchBarOrKeepMode)->nextMatchForSelection(view: view(), searchDirection: KateSearchBar::SearchForward); |
122 | } |
123 | |
124 | void KateNormalInputMode::findSelectedBackwards() |
125 | { |
126 | searchBar(mode: IncrementalSearchBarOrKeepMode)->nextMatchForSelection(view: view(), searchDirection: KateSearchBar::SearchBackward); |
127 | } |
128 | |
129 | void KateNormalInputMode::findReplace() |
130 | { |
131 | KateSearchBar *const bar = searchBar(mode: PowerSearchBar); |
132 | view()->bottomViewBar()->addBarWidget(newBarWidget: bar); |
133 | view()->bottomViewBar()->showBarWidget(barWidget: bar); |
134 | bar->setFocus(); |
135 | } |
136 | |
137 | void KateNormalInputMode::findNext() |
138 | { |
139 | searchBar(mode: IncrementalSearchBarOrKeepMode)->findNext(); |
140 | } |
141 | |
142 | void KateNormalInputMode::findPrevious() |
143 | { |
144 | searchBar(mode: IncrementalSearchBarOrKeepMode)->findPrevious(); |
145 | } |
146 | |
147 | void KateNormalInputMode::activateCommandLine() |
148 | { |
149 | const KTextEditor::Range selection = view()->selectionRange(); |
150 | |
151 | // if the user has selected text, insert the selection's range (start line to end line) in the |
152 | // command line when opened |
153 | if (selection.start().line() != -1 && selection.end().line() != -1) { |
154 | cmdLineBar()->setText(text: QString::number(selection.start().line() + 1) + QLatin1Char(',') + QString::number(selection.end().line() + 1)); |
155 | } |
156 | view()->bottomViewBar()->showBarWidget(barWidget: cmdLineBar()); |
157 | cmdLineBar()->setFocus(); |
158 | } |
159 | |
160 | KateSearchBar *KateNormalInputMode::searchBar(const SearchBarMode mode) |
161 | { |
162 | // power mode wanted? |
163 | const bool wantPowerMode = (mode == PowerSearchBar); |
164 | |
165 | // create search bar is not there? use right mode |
166 | if (!m_searchBar) { |
167 | m_searchBar.reset(p: new KateSearchBar(wantPowerMode, view(), KateViewConfig::global())); |
168 | } |
169 | |
170 | // else: switch mode if needed! |
171 | else if (mode != IncrementalSearchBarOrKeepMode) { |
172 | if (wantPowerMode) { |
173 | m_searchBar->enterPowerMode(); |
174 | } else { |
175 | m_searchBar->enterIncrementalMode(); |
176 | } |
177 | } |
178 | |
179 | return m_searchBar.get(); |
180 | } |
181 | |
182 | KateCommandLineBar *KateNormalInputMode::cmdLineBar() |
183 | { |
184 | if (!m_cmdLine) { |
185 | m_cmdLine.reset(p: new KateCommandLineBar(view(), view()->bottomViewBar())); |
186 | view()->bottomViewBar()->addBarWidget(newBarWidget: m_cmdLine.get()); |
187 | } |
188 | |
189 | return m_cmdLine.get(); |
190 | } |
191 | |
192 | void KateNormalInputMode::updateRendererConfig() |
193 | { |
194 | if (m_searchBar) { |
195 | m_searchBar->updateHighlightColors(); |
196 | } |
197 | } |
198 | |
199 | bool KateNormalInputMode::keyPress(QKeyEvent *e) |
200 | { |
201 | // Note: AND'ing with <Shift> is a quick hack to fix Key_Enter |
202 | const int key = e->key() | (e->modifiers() & Qt::ShiftModifier); |
203 | |
204 | if (view()->isCompletionActive()) { |
205 | if (key == Qt::Key_Tab || key == (Qt::SHIFT | Qt::Key_Backtab).toCombined() || key == Qt::Key_Backtab) { |
206 | if (KateViewConfig::global()->tabCompletion()) { |
207 | e->accept(); |
208 | using W = KateCompletionWidget; |
209 | const auto direction = key == Qt::Key_Tab ? W::Down : W::Up; |
210 | view()->completionWidget()->tabCompletion(direction); |
211 | return true; |
212 | } |
213 | } |
214 | |
215 | const bool isEnter = key == Qt::Key_Enter || key == Qt::Key_Return; |
216 | if (isEnter || key == Qt::Key_Tab) { |
217 | // Are we allowed to execute a completion on enter press? |
218 | if (isEnter && !view()->config()->value(key: KateViewConfig::EnterToInsertCompletion).toBool()) { |
219 | return false; |
220 | } |
221 | |
222 | if (view()->completionWidget()->execute()) { |
223 | e->accept(); |
224 | return true; |
225 | } |
226 | } |
227 | } |
228 | |
229 | return false; |
230 | } |
231 | |
232 | bool KateNormalInputMode::blinkCaret() const |
233 | { |
234 | return true; |
235 | } |
236 | |
237 | KTextEditor::caretStyles KateNormalInputMode::caretStyle() const |
238 | { |
239 | return view()->isOverwriteMode() ? KTextEditor::caretStyles::Block : KTextEditor::caretStyles::Line; |
240 | } |
241 | |
242 | void KateNormalInputMode::toggleInsert() |
243 | { |
244 | view()->toggleInsert(); |
245 | } |
246 | |
247 | void KateNormalInputMode::launchInteractiveCommand(const QString &command) |
248 | { |
249 | KateCommandLineBar *cmdLine = cmdLineBar(); |
250 | view()->bottomViewBar()->showBarWidget(barWidget: cmdLine); |
251 | cmdLine->setText(text: command, selected: false); |
252 | } |
253 | |
254 | QString KateNormalInputMode::bookmarkLabel(int) const |
255 | { |
256 | return QString(); |
257 | } |
258 |