File indexing completed on 2024-05-05 03:58:01

0001 /*
0002     SPDX-FileCopyrightText: KDE Developers
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "katenormalinputmode.h"
0008 #include "katecompletionwidget.h"
0009 #include "kateconfig.h"
0010 #include "katedocument.h"
0011 #include "katerenderer.h"
0012 #include "katesearchbar.h"
0013 #include "kateview.h"
0014 #include "kateviewinternal.h"
0015 
0016 #include <KLocalizedString>
0017 
0018 KateNormalInputMode::KateNormalInputMode(KateViewInternal *viewInternal)
0019     : KateAbstractInputMode(viewInternal)
0020 {
0021 }
0022 
0023 void KateNormalInputMode::activate()
0024 {
0025     view()->activateEditActions();
0026 }
0027 
0028 void KateNormalInputMode::deactivate()
0029 {
0030     view()->deactivateEditActions();
0031 }
0032 
0033 void KateNormalInputMode::reset()
0034 {
0035     // nothing todo
0036 }
0037 
0038 bool KateNormalInputMode::overwrite() const
0039 {
0040     return view()->doc()->config()->ovr();
0041 }
0042 
0043 void KateNormalInputMode::overwrittenChar(const QChar &)
0044 {
0045     // nothing todo
0046 }
0047 
0048 void KateNormalInputMode::clearSelection()
0049 {
0050     view()->clearSelection();
0051 }
0052 
0053 bool KateNormalInputMode::stealKey(QKeyEvent *)
0054 {
0055     return false;
0056 }
0057 
0058 KTextEditor::View::InputMode KateNormalInputMode::viewInputMode() const
0059 {
0060     return KTextEditor::View::NormalInputMode;
0061 }
0062 
0063 QString KateNormalInputMode::viewInputModeHuman() const
0064 {
0065     return i18n("Normal");
0066 }
0067 
0068 KTextEditor::View::ViewMode KateNormalInputMode::viewMode() const
0069 {
0070     return view()->isOverwriteMode() ? KTextEditor::View::NormalModeOverwrite : KTextEditor::View::NormalModeInsert;
0071 }
0072 
0073 QString KateNormalInputMode::viewModeHuman() const
0074 {
0075     return view()->isOverwriteMode() ? i18n("OVERWRITE") : i18n("INSERT");
0076 }
0077 
0078 void KateNormalInputMode::gotFocus()
0079 {
0080     view()->activateEditActions();
0081 }
0082 
0083 void KateNormalInputMode::lostFocus()
0084 {
0085     view()->deactivateEditActions();
0086 }
0087 
0088 void KateNormalInputMode::readSessionConfig(const KConfigGroup &)
0089 {
0090     // do nothing
0091 }
0092 
0093 void KateNormalInputMode::writeSessionConfig(KConfigGroup &)
0094 {
0095     // do nothing
0096 }
0097 
0098 void KateNormalInputMode::updateConfig()
0099 {
0100     // do nothing
0101 }
0102 
0103 void KateNormalInputMode::readWriteChanged(bool)
0104 {
0105     // inform search bar
0106     if (m_searchBar) {
0107         m_searchBar->slotReadWriteChanged();
0108     }
0109 }
0110 
0111 void KateNormalInputMode::find()
0112 {
0113     KateSearchBar *const bar = searchBar(IncrementalSearchBar);
0114     view()->bottomViewBar()->addBarWidget(bar);
0115     view()->bottomViewBar()->showBarWidget(bar);
0116     bar->setFocus();
0117 }
0118 
0119 void KateNormalInputMode::findSelectedForwards()
0120 {
0121     searchBar(IncrementalSearchBarOrKeepMode)->nextMatchForSelection(view(), KateSearchBar::SearchForward);
0122 }
0123 
0124 void KateNormalInputMode::findSelectedBackwards()
0125 {
0126     searchBar(IncrementalSearchBarOrKeepMode)->nextMatchForSelection(view(), KateSearchBar::SearchBackward);
0127 }
0128 
0129 void KateNormalInputMode::findReplace()
0130 {
0131     KateSearchBar *const bar = searchBar(PowerSearchBar);
0132     view()->bottomViewBar()->addBarWidget(bar);
0133     view()->bottomViewBar()->showBarWidget(bar);
0134     bar->setFocus();
0135 }
0136 
0137 void KateNormalInputMode::findNext()
0138 {
0139     searchBar(IncrementalSearchBarOrKeepMode)->findNext();
0140 }
0141 
0142 void KateNormalInputMode::findPrevious()
0143 {
0144     searchBar(IncrementalSearchBarOrKeepMode)->findPrevious();
0145 }
0146 
0147 void KateNormalInputMode::activateCommandLine()
0148 {
0149     const KTextEditor::Range selection = view()->selectionRange();
0150 
0151     // if the user has selected text, insert the selection's range (start line to end line) in the
0152     // command line when opened
0153     if (selection.start().line() != -1 && selection.end().line() != -1) {
0154         cmdLineBar()->setText(QString::number(selection.start().line() + 1) + QLatin1Char(',') + QString::number(selection.end().line() + 1));
0155     }
0156     view()->bottomViewBar()->showBarWidget(cmdLineBar());
0157     cmdLineBar()->setFocus();
0158 }
0159 
0160 KateSearchBar *KateNormalInputMode::searchBar(const SearchBarMode mode)
0161 {
0162     // power mode wanted?
0163     const bool wantPowerMode = (mode == PowerSearchBar);
0164 
0165     // create search bar is not there? use right mode
0166     if (!m_searchBar) {
0167         m_searchBar.reset(new KateSearchBar(wantPowerMode, view(), KateViewConfig::global()));
0168     }
0169 
0170     // else: switch mode if needed!
0171     else if (mode != IncrementalSearchBarOrKeepMode) {
0172         if (wantPowerMode) {
0173             m_searchBar->enterPowerMode();
0174         } else {
0175             m_searchBar->enterIncrementalMode();
0176         }
0177     }
0178 
0179     return m_searchBar.get();
0180 }
0181 
0182 KateCommandLineBar *KateNormalInputMode::cmdLineBar()
0183 {
0184     if (!m_cmdLine) {
0185         m_cmdLine.reset(new KateCommandLineBar(view(), view()->bottomViewBar()));
0186         view()->bottomViewBar()->addBarWidget(m_cmdLine.get());
0187     }
0188 
0189     return m_cmdLine.get();
0190 }
0191 
0192 void KateNormalInputMode::updateRendererConfig()
0193 {
0194     if (m_searchBar) {
0195         m_searchBar->updateHighlightColors();
0196     }
0197 }
0198 
0199 bool KateNormalInputMode::keyPress(QKeyEvent *e)
0200 {
0201     // Note: AND'ing with <Shift> is a quick hack to fix Key_Enter
0202     const int key = e->key() | (e->modifiers() & Qt::ShiftModifier);
0203 
0204     if (view()->isCompletionActive()) {
0205         if (key == Qt::Key_Tab || key == (Qt::SHIFT | Qt::Key_Backtab).toCombined() || key == Qt::Key_Backtab) {
0206             if (KateViewConfig::global()->tabCompletion()) {
0207                 e->accept();
0208                 using W = KateCompletionWidget;
0209                 const auto direction = key == Qt::Key_Tab ? W::Down : W::Up;
0210                 view()->completionWidget()->tabCompletion(direction);
0211                 return true;
0212             }
0213         }
0214 
0215         const bool isEnter = key == Qt::Key_Enter || key == Qt::Key_Return;
0216         if (isEnter || key == Qt::Key_Tab) {
0217             // Are we allowed to execute a completion on enter press?
0218             if (isEnter && !view()->config()->value(KateViewConfig::EnterToInsertCompletion).toBool()) {
0219                 return false;
0220             }
0221 
0222             if (view()->completionWidget()->execute()) {
0223                 e->accept();
0224                 return true;
0225             }
0226         }
0227     }
0228 
0229     return false;
0230 }
0231 
0232 bool KateNormalInputMode::blinkCaret() const
0233 {
0234     return true;
0235 }
0236 
0237 KTextEditor::caretStyles KateNormalInputMode::caretStyle() const
0238 {
0239     return view()->isOverwriteMode() ? KTextEditor::caretStyles::Block : KTextEditor::caretStyles::Line;
0240 }
0241 
0242 void KateNormalInputMode::toggleInsert()
0243 {
0244     view()->toggleInsert();
0245 }
0246 
0247 void KateNormalInputMode::launchInteractiveCommand(const QString &command)
0248 {
0249     KateCommandLineBar *cmdLine = cmdLineBar();
0250     view()->bottomViewBar()->showBarWidget(cmdLine);
0251     cmdLine->setText(command, false);
0252 }
0253 
0254 QString KateNormalInputMode::bookmarkLabel(int) const
0255 {
0256     return QString();
0257 }