• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • vimode
katevikeymapper.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries and the Kate part.
2  *
3  * Copyright (C) 2008-2009 Erlend Hamberg <ehamberg@gmail.com>
4  * Copyright (C) 2013 Simon St James <kdedevel@etotheipiplusone.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 #include "katevikeymapper.h"
22 #include "kateglobal.h"
23 #include "kateviglobal.h"
24 
25 #include <QtCore/QTimer>
26 
27 KateViKeyMapper::KateViKeyMapper(KateViInputModeManager* kateViInputModeManager, KateDocument* doc, KateView *view )
28  : m_viInputModeManager(kateViInputModeManager),
29  m_doc(doc),
30  m_view(view)
31 {
32  m_mappingTimer = new QTimer( this );
33  m_doNotExpandFurtherMappings = false;
34  m_timeoutlen = 1000; // FIXME: make configurable
35  m_doNotMapNextKeypress = false;
36  m_numMappingsBeingExecuted = 0;
37  m_isPlayingBackRejectedKeys = false;
38  connect(m_mappingTimer, SIGNAL(timeout()), this, SLOT(mappingTimerTimeOut()));
39 }
40 
41 void KateViKeyMapper::executeMapping()
42 {
43  m_mappingKeys.clear();
44  m_mappingTimer->stop();
45  m_numMappingsBeingExecuted++;
46  const QString mappedKeypresses = KateGlobal::self()->viInputModeGlobal()->getMapping(KateViGlobal::mappingModeForCurrentViMode(m_view), m_fullMappingMatch);
47  if (!KateGlobal::self()->viInputModeGlobal()->isMappingRecursive(KateViGlobal::mappingModeForCurrentViMode(m_view), m_fullMappingMatch))
48  {
49  kDebug(13070) << "Non-recursive: " << mappedKeypresses;
50  m_doNotExpandFurtherMappings = true;
51  }
52  m_doc->editBegin();
53  m_viInputModeManager->feedKeyPresses(mappedKeypresses);
54  m_doNotExpandFurtherMappings = false;
55  m_doc->editEnd();
56  m_numMappingsBeingExecuted--;
57 }
58 
59 void KateViKeyMapper::playBackRejectedKeys()
60 {
61  m_isPlayingBackRejectedKeys = true;
62  const QString mappingKeys = m_mappingKeys;
63  m_mappingKeys.clear();
64  m_viInputModeManager->feedKeyPresses(mappingKeys);
65  m_isPlayingBackRejectedKeys = false;
66 }
67 
68 void KateViKeyMapper::setMappingTimeout(int timeoutMS)
69 {
70  m_timeoutlen = timeoutMS;
71 }
72 
73 void KateViKeyMapper::mappingTimerTimeOut()
74 {
75  kDebug( 13070 ) << "timeout! key presses: " << m_mappingKeys;
76  if (!m_fullMappingMatch.isNull())
77  {
78  executeMapping();
79  }
80  else
81  {
82  playBackRejectedKeys();
83  }
84  m_mappingKeys.clear();
85 }
86 
87 bool KateViKeyMapper::handleKeypress(QChar key)
88 {
89  if ( !m_doNotExpandFurtherMappings && !m_doNotMapNextKeypress && !m_isPlayingBackRejectedKeys) {
90  m_mappingKeys.append( key );
91 
92  bool isPartialMapping = false;
93  bool isFullMapping = false;
94  m_fullMappingMatch.clear();
95  foreach ( const QString &mapping, KateGlobal::self()->viInputModeGlobal()->getMappings(KateViGlobal::mappingModeForCurrentViMode(m_view)) ) {
96  if ( mapping.startsWith( m_mappingKeys ) ) {
97  if ( mapping == m_mappingKeys ) {
98  isFullMapping = true;
99  m_fullMappingMatch = mapping;
100  } else {
101  isPartialMapping = true;
102  }
103  }
104  }
105  if (isFullMapping && !isPartialMapping)
106  {
107  // Great - m_mappingKeys is a mapping, and one that can't be extended to
108  // a longer one - execute it immediately.
109  executeMapping();
110  return true;
111  }
112  if (isPartialMapping)
113  {
114  // Need to wait for more characters (or a timeout) before we decide what to
115  // do with this.
116  m_mappingTimer->start( m_timeoutlen );
117  m_mappingTimer->setSingleShot( true );
118  return true;
119  }
120  // We've been swallowing all the keypresses meant for m_keys for our mapping keys; now that we know
121  // this cannot be a mapping, restore them.
122  Q_ASSERT(!isPartialMapping && !isFullMapping);
123  playBackRejectedKeys();
124  return true;
125  }
126  m_doNotMapNextKeypress = false;
127  return false;
128 }
129 
130 void KateViKeyMapper::setDoNotMapNextKeypress()
131 {
132  m_doNotMapNextKeypress = true;
133 }
134 
135 bool KateViKeyMapper::isExecutingMapping()
136 {
137  return m_numMappingsBeingExecuted > 0;
138 }
139 
140 bool KateViKeyMapper::isPlayingBackRejectedKeys()
141 {
142  return m_isPlayingBackRejectedKeys;
143 }
QString::append
QString & append(QChar ch)
KateViGlobal::getMapping
const QString getMapping(MappingMode mode, const QString &from, bool decode=false) const
Definition: kateviglobal.cpp:186
KateDocument::editBegin
void editBegin()
Alias for editStart()
Definition: katedocument.h:213
QChar
KateViInputModeManager
Definition: kateviinputmodemanager.h:68
KateViKeyMapper::setDoNotMapNextKeypress
void setDoNotMapNextKeypress()
Definition: katevikeymapper.cpp:130
KateGlobal::self
static KateGlobal * self()
Kate Part Internal stuff ;)
Definition: kateglobal.cpp:465
KateViKeyMapper::setMappingTimeout
void setMappingTimeout(int timeoutMS)
Definition: katevikeymapper.cpp:68
QString::isNull
bool isNull() const
KateViKeyMapper::isExecutingMapping
bool isExecutingMapping()
Definition: katevikeymapper.cpp:135
QString::clear
void clear()
katevikeymapper.h
QTimer
kateglobal.h
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QString
KateView
Definition: kateview.h:77
KateDocument
Definition: katedocument.h:74
QTimer::stop
void stop()
KateGlobal::viInputModeGlobal
KateViGlobal * viInputModeGlobal()
vi input mode global
Definition: kateglobal.h:339
KateDocument::editEnd
void editEnd()
End a editor operation.
Definition: katedocument.cpp:796
KateViGlobal::mappingModeForCurrentViMode
static MappingMode mappingModeForCurrentViMode(KateView *view)
Returns CommandModeMapping if the emulated command bar is active, else the mapping mode corresponding...
Definition: kateviglobal.cpp:218
KateViKeyMapper::handleKeypress
bool handleKeypress(QChar key)
Definition: katevikeymapper.cpp:87
QTimer::start
void start(int msec)
KateViKeyMapper::isPlayingBackRejectedKeys
bool isPlayingBackRejectedKeys()
Definition: katevikeymapper.cpp:140
KateViInputModeManager::feedKeyPresses
void feedKeyPresses(const QString &keyPresses) const
feed key the given list of key presses to the key handling code, one by one
Definition: kateviinputmodemanager.cpp:172
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
kateviglobal.h
KateViKeyMapper::KateViKeyMapper
KateViKeyMapper(KateViInputModeManager *kateViInputModeManager, KateDocument *doc, KateView *view)
Definition: katevikeymapper.cpp:27
KateViKeyMapper::mappingTimerTimeOut
void mappingTimerTimeOut()
Definition: katevikeymapper.cpp:73
QTimer::setSingleShot
void setSingleShot(bool singleShot)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:59 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal