-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathvicmd.cpp
238 lines (205 loc) · 5.39 KB
/
vicmd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#if defined(CB_PRECOMP)
#include "sdk.h"
#else
#include "sdk_common.h"
#include "sdk_events.h"
#include "logmanager.h"
#endif
#include "vicmd.h"
#include "cbvike.h"
#include "vifunc.h"
/***************** VikeCommand start *********************/
VikeCommand::VikeCommand(wxChar prefix, VikeHighlight &hl)
: m_highlight(hl),
m_cPrefix(prefix)
{
Clear();
}
void VikeCommand::AppendCommand(wxChar cmdChar)
{
m_sCommand.Append(cmdChar);
}
void VikeCommand::BackCommand()
{
m_sCommand.RemoveLast();
}
bool VikeCommand::IsEmpty()
{
return m_sCommand.IsEmpty();
}
wxChar VikeCommand::GetPrefix()
{
return m_cPrefix;
}
wxString VikeCommand::GetCommand()
{
this->SetPrefix();
return m_sCommand.Mid(1);
}
wxString &VikeCommand::GetCommandWithPrefix()
{
this->SetPrefix();
return m_sCommand;
}
void VikeCommand::Clear()
{
m_sCommand.Clear();
m_sCommand.Prepend(m_cPrefix);
}
void VikeCommand::Store()
{
}
void VikeCommand::SetPrefix()
{
if(m_sCommand.Length() == 0 ||
(m_sCommand.Length() > 0 && m_sCommand[0] != m_cPrefix))
{
m_sCommand.Prepend(m_cPrefix);
}
}
/***************** VikeCommand end *********************/
/***************** VikeSearchCommand start *****************/
VikeSearchCmd::VikeSearchCmd(wxChar prefix, VikeHighlight &hl)
: VikeCommand(prefix, hl),
m_iLast(0),
m_iMaxIndicator(VIKE_INDIC_START)
{}
void VikeSearchCmd::doSearch(wxScintilla *editor)
{
int curPos = editor->GetCurrentPos();
bool found = StoreFind(0, editor->GetLength(), editor);
if(found){
editor->GotoPos(NextPos(curPos));
}
}
int VikeSearchCmd::NextPos(int curPos)
{
return NeighborPos(curPos, true);
}
int VikeSearchCmd::PrevPos(int curPos)
{
return NeighborPos(curPos, false);
}
bool VikeSearchCmd::StoreFind(int start, int end, wxScintilla *editor)
{
// Clear
m_highlight.Hide(editor);
bool found = false;
int len;
ClearState();
int pos = start;
do{
pos = editor->FindText(pos + 1, end, this->GetCommand(), wxSCI_FIND_REGEXP|wxSCI_FIND_MATCHCASE, &len);
if(pos >= 0){
found = true;
m_arrFind.push_back(pos);
// highlight
m_highlight.SetPos(pos, len, editor);
}
//LOGIT(_T("pos is %d"), pos);
}while(pos >= 0);
m_highlight.Show(editor);
LOGIT(_T("find string length: %d"), len);
return found;
}
int VikeSearchCmd::NeighborPos(int curPos, bool lookForward)
{
if(m_arrFind.GetCount() == 0){
return curPos;
}
// Get next or previous position directly through m_iLast
int step = lookForward ? 1 : -1;
if(m_iLast == curPos){
m_iLast = (m_iLast + step) % m_arrFind.GetCount();
return m_arrFind[m_iLast];
}
// Find next or previous position by searching in m_arrFind
int pos = curPos;
if(lookForward){
for(int i = 0; i < m_arrFind.GetCount(); i++){
if(m_arrFind[i] > curPos){
pos = m_arrFind[i];
m_iLast = i;
break;
}
}
}else{
for(int i = m_arrFind.GetCount() - 1; i >= 0; i--){
if(m_arrFind[i] < curPos){
pos = m_arrFind[i];
m_iLast = i;
break;
}
}
}
// nothing find after curPos
if(pos == curPos){
pos = lookForward ? m_arrFind[0] : m_arrFind.Last();
}
return pos;
}
void VikeSearchCmd::ClearState()
{
m_iFindLen = 0;
m_iLast = 0;
m_iMaxIndicator = VIKE_INDIC_START;
m_arrFind.Clear();
}
/***************** VikeSearchCommand end *****************/
/***************** VikeGeneralCommand start *****************/
VikeGeneralCmd::VikeGeneralCmd(wxChar prefix, VikeHighlight &hl)
:VikeCommand(prefix, hl)
{
m_pCmdFunc = VikeCmdFunc::Instance();
}
void VikeGeneralCmd::ParseCommand(VikeWin *vike, wxScintilla *editor)
{
wxString cmd = GetCommand();
int len = 10;
wxString **argv = new wxString*[len];
VikeCmdHandler handler = m_pCmdFunc->GetHandler(cmd);
if(handler){
handler(1, argv, vike, editor);
}
}
/***************** VikeGeneralCommand end *****************/
/***************** VikeCmdFunc start *****************/
/* the instance */
VikeCmdFunc VikeCmdFunc::m_pInstance;
VikeCmdFunc::VikeCmdFunc()
{
m_cmdMap[_T("nohl")] = &nohl;
m_cmdMap[_T("w")] = &cmd_write;
m_cmdMap[_T("q")] = &cmd_quit;
m_cmdMap[_T("wq")] = [](int argc, wxString** argv, VikeWin* vike, wxScintilla* editor) ->int {
EditorManager * editorMgr = Manager::Get()->GetEditorManager();
editorMgr->SaveActive();
editorMgr->CloseActive();
};
}
VikeCmdFunc::VikeCmdFunc(const VikeCmdFunc&)
{
}
VikeCmdHandler VikeCmdFunc::GetHandler(const wxString &cmd)
{
return m_cmdMap[cmd];
}
/* All the commands */
int VikeCmdFunc::nohl(int argc, wxString *argv[], VikeWin *vike, wxScintilla *editor)
{
vike->GetHighlight().Hide(editor);
}
int VikeCmdFunc::cmd_write(int argc, wxString *argv[], VikeWin *vike, wxScintilla *editor)
{
Manager::Get()->GetEditorManager()->SaveActive();
}
int VikeCmdFunc::cmd_quit(int argc, wxString *argv[], VikeWin *vike, wxScintilla *editor)
{
Manager::Get()->GetEditorManager()->CloseActive();
}
/***************** VikeCmdFunc end *****************/