| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* |
| 4 |
* Copyright (C) 2008 Alf. |
| 5 |
* |
| 6 |
* Contact: Alf <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License |
| 10 |
* as published by the Free Software Foundation; either version 2.0 of |
| 11 |
* the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public |
| 19 |
* License along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
* |
| 22 |
*/ |
| 23 |
/* @CPPFILE ApvlvCmds.h |
| 24 |
* |
| 25 |
* Author: Alf <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef _APVLV_CMDS_H_ |
| 29 |
#define _APVLV_CMDS_H_ |
| 30 |
|
| 31 |
#include <QKeyEvent> |
| 32 |
#include <QTimer> |
| 33 |
#include <map> |
| 34 |
#include <vector> |
| 35 |
|
| 36 |
namespace apvlv |
| 37 |
{ |
| 38 |
enum class CmdType |
| 39 |
{ |
| 40 |
CT_CMD, |
| 41 |
CT_STRING, |
| 42 |
CT_STRING_RETURN |
| 43 |
}; |
| 44 |
|
| 45 |
enum class CmdState |
| 46 |
{ |
| 47 |
GETTING_COUNT, |
| 48 |
GETTING_CMD, |
| 49 |
CMD_OK, |
| 50 |
}; |
| 51 |
|
| 52 |
// command type |
| 53 |
enum class CmdStatusType |
| 54 |
{ |
| 55 |
CMD_NONE, |
| 56 |
CMD_MESSAGE, |
| 57 |
CMD_CMD |
| 58 |
}; |
| 59 |
|
| 60 |
// function return type |
| 61 |
enum class CmdReturn |
| 62 |
{ |
| 63 |
MATCH, |
| 64 |
NEED_MORE, |
| 65 |
NO_MATCH, |
| 66 |
}; |
| 67 |
|
| 68 |
// because every unsigned char is < 256, so use this marco to stand for |
| 69 |
// Ctrl+char, Shift+char |
| 70 |
constexpr int |
| 71 |
ctrlValue (int c) |
| 72 |
{ |
| 73 |
return c + 256; |
| 74 |
} |
| 75 |
|
| 76 |
using StringKeyMap = std::map<std::string, int>; |
| 77 |
|
| 78 |
class Command; |
| 79 |
using CommandKeyList = std::vector<int>; |
| 80 |
using CommandMap = std::map<CommandKeyList, Command *>; |
| 81 |
|
| 82 |
class ApvlvView; |
| 83 |
class Command final |
| 84 |
{ |
| 85 |
public: |
| 86 |
Command (); |
| 87 |
|
| 88 |
~Command () = default; |
| 89 |
|
| 90 |
void process (ApvlvView *view); |
| 91 |
|
| 92 |
void push (std::string_view s, CmdType type = CmdType::CT_CMD); |
| 93 |
|
| 94 |
bool append (QKeyEvent *key); |
| 95 |
|
| 96 |
const char *append (const char *s); |
| 97 |
|
| 98 |
void type (CmdType type); |
| 99 |
|
| 100 |
CmdType type (); |
| 101 |
|
| 102 |
CommandKeyList *keyVals (); |
| 103 |
|
| 104 |
CommandKeyList keyvalv (); |
| 105 |
|
| 106 |
void setPreCount (int precount); |
| 107 |
|
| 108 |
[[nodiscard]] int preCount () const; |
| 109 |
|
| 110 |
int keyval (uint id); |
| 111 |
|
| 112 |
Command *next (); |
| 113 |
|
| 114 |
void origin (Command *cmd); |
| 115 |
|
| 116 |
Command *origin (); |
| 117 |
|
| 118 |
private: |
| 119 |
static StringKeyMap mKeyMap; |
| 120 |
|
| 121 |
// command type |
| 122 |
CmdType mType; |
| 123 |
|
| 124 |
// if it has count |
| 125 |
bool mHasPreCount; |
| 126 |
|
| 127 |
// how to describe this command in .apvlvrc |
| 128 |
// like <C-d><C-w>, <S-b>s, or :run, :vsp, ... |
| 129 |
std::string mStrCommand; |
| 130 |
|
| 131 |
// key's value list |
| 132 |
CommandKeyList mKeyVals; |
| 133 |
|
| 134 |
// cmd's pre count |
| 135 |
int mPreCount; |
| 136 |
|
| 137 |
// next command |
| 138 |
std::unique_ptr<Command> mNext; |
| 139 |
|
| 140 |
// when a key is map to other, this is the origin cmd. |
| 141 |
// after a mapped key was processed, return to this cmds |
| 142 |
Command *mOrigin; |
| 143 |
}; |
| 144 |
|
| 145 |
class ApvlvCmds : public QObject |
| 146 |
{ |
| 147 |
Q_OBJECT |
| 148 |
public: |
| 149 |
explicit ApvlvCmds (ApvlvView *view); |
| 150 |
|
| 151 |
~ApvlvCmds () override; |
| 152 |
|
| 153 |
void append (QKeyEvent *gev); |
| 154 |
|
| 155 |
static void buildCommandMap (std::string_view os, std::string_view ms); |
| 156 |
|
| 157 |
private: |
| 158 |
Command *process (Command *cmd); |
| 159 |
|
| 160 |
static CmdReturn isMapCommand (CommandKeyList *ack); |
| 161 |
|
| 162 |
static Command *getMapCommand (Command *cmd); |
| 163 |
|
| 164 |
static CommandMap mMaps; |
| 165 |
|
| 166 |
std::unique_ptr<Command> mCmdHead; |
| 167 |
|
| 168 |
// command view |
| 169 |
ApvlvView *mView; |
| 170 |
|
| 171 |
CmdState mState; |
| 172 |
|
| 173 |
std::unique_ptr<QTimer> mTimeoutTimer; |
| 174 |
|
| 175 |
std::string mCountString; |
| 176 |
|
| 177 |
private slots: |
| 178 |
void timeoutCallback (); |
| 179 |
}; |
| 180 |
} |
| 181 |
|
| 182 |
#endif |
| 183 |
|
| 184 |
// Local Variables: |
| 185 |
// mode: c++ |
| 186 |
// End: |