KTextEditor

macrorecorder.cpp
1/*
2 SPDX-FileCopyrightText: KDE Developers
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "macrorecorder.h"
8#include "completionrecorder.h"
9#include "completionreplayer.h"
10#include "globalstate.h"
11#include "kateview.h"
12#include "lastchangerecorder.h"
13#include "macros.h"
14#include <vimode/inputmodemanager.h>
15#include <vimode/keymapper.h>
16
17namespace
18{
19const QChar LastPlayedRegister = QLatin1Char('@');
20}
21
22using namespace KateVi;
23
24MacroRecorder::MacroRecorder(InputModeManager *viInputModeManager)
25 : m_viInputModeManager(viInputModeManager)
26 , m_isRecording(false)
27 , m_macrosBeingReplayedCount(0)
28 , m_lastPlayedMacroRegister(QChar::Null)
29{
30}
31
32void MacroRecorder::start(const QChar &macroRegister)
33{
34 Q_ASSERT(!m_isRecording);
35 m_isRecording = true;
36 m_register = macroRegister;
37 m_viInputModeManager->globalState()->macros()->remove(macroRegister);
38 m_eventsLog.clear();
39 m_viInputModeManager->completionRecorder()->start();
40}
41
42void MacroRecorder::stop()
43{
44 Q_ASSERT(m_isRecording);
45 m_isRecording = false;
46 CompletionList completions = m_viInputModeManager->completionRecorder()->stop();
47 m_viInputModeManager->globalState()->macros()->store(m_register, m_eventsLog, completions);
48}
49
50bool MacroRecorder::isRecording() const
51{
52 return m_isRecording;
53}
54
55void MacroRecorder::record(const QKeyEvent &event)
56{
57 if (isRepeatOfLastShortcutOverrideAsKeyPress(event, m_eventsLog)) {
58 return;
59 }
60 m_eventsLog.append(KeyEvent::fromQKeyEvent(event));
61}
62
63void MacroRecorder::dropLast()
64{
65 if (m_isRecording) {
66 Q_ASSERT(!m_eventsLog.isEmpty());
67 m_eventsLog.pop_back();
68 }
69}
70
71void MacroRecorder::replay(const QChar &macroRegister)
72{
73 const QChar reg = (macroRegister == LastPlayedRegister) ? m_lastPlayedMacroRegister : macroRegister;
74
75 m_lastPlayedMacroRegister = reg;
76 const QString macroAsFeedableKeypresses = m_viInputModeManager->globalState()->macros()->get(reg);
77
78 std::shared_ptr<KeyMapper> mapper(new KeyMapper(m_viInputModeManager, m_viInputModeManager->view()->doc()));
79 CompletionList completions = m_viInputModeManager->globalState()->macros()->getCompletions(reg);
80
81 m_macrosBeingReplayedCount++;
82 m_viInputModeManager->completionReplayer()->start(completions);
83 m_viInputModeManager->pushKeyMapper(mapper);
84 m_viInputModeManager->feedKeyPresses(macroAsFeedableKeypresses);
85 m_viInputModeManager->popKeyMapper();
86 m_viInputModeManager->completionReplayer()->stop();
87 m_macrosBeingReplayedCount--;
88}
89
90bool MacroRecorder::isReplaying() const
91{
92 return m_macrosBeingReplayedCount > 0;
93}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Feb 24 2024 20:00:58 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.