forked from Rezonality/zep
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mode_standard.cpp
360 lines (324 loc) · 11.3 KB
/
mode_standard.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "zep/mode_standard.h"
#include "zep/mcommon/string/stringutils.h"
// Note:
// This is a version of the buffer that behaves like notepad.
// It is basic, but can easily be extended
// STANDARD:
//
// DONE:
// -----
// CTRLZ/Y Undo Redo
// Insert
// Delete/Backspace
// TAB
// Arrows - up,down, left, right
// Home (+Ctrl) move top/startline
// End (+Ctrol) move bottom/endline
// Shift == Select
// control+Shift == select word
// CTRL - CVX (copy paste, cut) + Delete Selection
namespace Zep
{
ZepMode_Standard::ZepMode_Standard(ZepEditor& editor)
: ZepMode(editor)
{
}
ZepMode_Standard::~ZepMode_Standard()
{
}
void ZepMode_Standard::Init()
{
// In standard mode, we always show the insert cursor type
m_visualCursorType = CursorType::Insert;
m_modeFlags |= ModeFlags::InsertModeGroupUndo | ModeFlags::StayInInsertMode;
for (int i = 0; i <= 9; i++)
{
GetEditor().SetRegister('0' + (const char)i, "");
}
GetEditor().SetRegister('"', "");
// Insert Mode
keymap_add({ &m_insertMap }, { "<Backspace>" }, id_Backspace);
keymap_add({ &m_insertMap }, { "<Return>" }, id_InsertCarriageReturn);
keymap_add({ &m_insertMap }, { "<Tab>" }, id_InsertTab);
keymap_add({ &m_insertMap, &m_visualMap }, { "<Del>" }, id_Delete);
keymap_add({ &m_insertMap, &m_visualMap }, { "<C-y>" }, id_Redo);
keymap_add({ &m_insertMap, &m_visualMap }, { "<C-z>" }, id_Undo);
keymap_add({ &m_insertMap, &m_visualMap }, { "<Left>" }, id_MotionStandardLeft);
keymap_add({ &m_insertMap, &m_visualMap }, { "<Right>" }, id_MotionStandardRight);
keymap_add({ &m_insertMap, &m_visualMap }, { "<Up>" }, id_MotionStandardUp);
keymap_add({ &m_insertMap, &m_visualMap }, { "<Down>" }, id_MotionStandardDown);
keymap_add({ &m_insertMap }, { "<C-Left>" }, id_MotionStandardLeftWord);
keymap_add({ &m_insertMap }, { "<C-Right>" }, id_MotionStandardRightWord);
keymap_add({ &m_insertMap, &m_visualMap }, { "<C-S-Left>" }, id_MotionStandardLeftWordSelect);
keymap_add({ &m_insertMap, &m_visualMap }, { "<C-S-Right>" }, id_MotionStandardRightWordSelect);
keymap_add({ &m_insertMap, &m_visualMap }, { "<S-Left>" }, id_MotionStandardLeftSelect);
keymap_add({ &m_insertMap, &m_visualMap }, { "<S-Right>" }, id_MotionStandardRightSelect);
keymap_add({ &m_insertMap, &m_visualMap }, { "<S-Up>" }, id_MotionStandardUpSelect);
keymap_add({ &m_insertMap, &m_visualMap }, { "<S-Down>" }, id_MotionStandardDownSelect);
keymap_add({ &m_visualMap }, { "<C-x>" }, id_Delete);
keymap_add({ &m_visualMap }, { "<Backspace>" }, id_Delete);
keymap_add({ &m_insertMap, &m_visualMap }, { "<C-v>" }, id_StandardPaste);
keymap_add({ &m_visualMap }, { "<C-c>" }, id_StandardCopy);
keymap_add({ &m_insertMap, &m_visualMap }, { "<C-a>" }, id_StandardSelectAll);
keymap_add({ &m_normalMap, &m_visualMap, &m_insertMap }, { "<Escape>" }, id_InsertMode);
keymap_add({ &m_normalMap }, { "<Backspace>" }, id_MotionStandardLeft);
}
void ZepMode_Standard::Begin(ZepWindow* pWindow)
{
ZepMode::Begin(pWindow);
// This will also set the cursor type
SwitchMode(EditorMode::Insert);
}
/*void ZepMode_Standard::AddKeyPress(uint32_t key, uint32_t modifierKeys)
{
ZepMode::AddKeyPress(key, modifierKeys);
std::string ch(1, (char)key);
bool copyRegion = false;
bool lineWise = false;
auto& buffer = GetCurrentWindow()->GetBuffer();
BufferLocation bufferCursor = GetCurrentWindow()->GetBufferCursor();
BufferLocation startOffset = bufferCursor;
BufferLocation endOffset = buffer.LocationFromOffsetByChars(bufferCursor, long(ch.length()));
CommandOperation op = CommandOperation::None;
auto normalizeOffsets = [&]() {
// Clamp and orient the correct way around
startOffset = buffer.Clamp(startOffset);
endOffset = buffer.Clamp(endOffset);
if (startOffset > endOffset)
{
std::swap(startOffset, endOffset);
}
};
if (key == ExtKeys::ESCAPE)
{
SwitchMode(EditorMode::Insert);
return spContext;
}
bool begin_shift = false;
bool return_to_insert = false;
switch (key)
{
case ExtKeys::DOWN:
case ExtKeys::UP:
case ExtKeys::LEFT:
case ExtKeys::RIGHT:
case ExtKeys::END:
case ExtKeys::HOME:
case ExtKeys::PAGEDOWN:
case ExtKeys::PAGEUP:
if (modifierKeys & ModifierKey::Shift)
{
begin_shift = (m_currentMode != EditorMode::Visual) ? true : false;
SwitchMode(EditorMode::Visual);
}
else
{
// Immediate switch back to insert
SwitchMode(EditorMode::Insert);
}
break;
default:
return_to_insert = true;
break;
}
// CTRL + ...
if (modifierKeys & ModifierKey::Ctrl)
{
// Motions fall through to selection code
else if (key == ExtKeys::HOME)
{
// CTRL HOME = top of file
GetCurrentWindow()->SetBufferCursor(BufferLocation{0});
}
else if (key == ExtKeys::END)
{
// CTRL END = end of file
GetCurrentWindow()->SetBufferCursor(buffer.EndLocation());
}
// Cut/Copy
else if (key == 'x')
{
// A delete, but also a copy!
op = CommandOperation::Delete;
copyRegion = true;
if (m_currentMode == EditorMode::Visual)
{
startOffset = m_visualBegin;
endOffset = m_visualEnd;
normalizeOffsets();
}
else
{
lineWise = true;
startOffset = buffer.GetLinePos(bufferCursor, LineLocation::LineBegin);
endOffset = buffer.GetLinePos(bufferCursor, LineLocation::BeyondLineEnd);
}
}
// Copy
else if (key == 'c')
{
op = CommandOperation::Copy;
if (m_currentMode == EditorMode::Visual)
{
startOffset = m_visualBegin;
endOffset = m_visualEnd;
normalizeOffsets();
return_to_insert = false;
}
else
{
lineWise = true;
startOffset = buffer.GetLinePos(bufferCursor, LineLocation::LineBegin);
endOffset = buffer.GetLinePos(bufferCursor, LineLocation::BeyondLineEnd);
}
}
// Paste
else if (key == 'v')
{
auto pRegister = &GetEditor().GetRegister('"');
if (!pRegister->text.empty())
{
ch = pRegister->text;
}
else
{
ch.clear();
}
op = CommandOperation::Insert;
startOffset = bufferCursor;
endOffset = startOffset + BufferLocation(ch.length());
}
}
else if (key == ExtKeys::HOME)
{
// Beginning of line
auto pos = buffer.GetLinePos(bufferCursor, LineLocation::LineFirstGraphChar);
GetCurrentWindow()->SetBufferCursor(pos);
GetEditor().ResetCursorTimer();
}
else if (key == ExtKeys::END)
{
auto pos = buffer.GetLinePos(bufferCursor, LineLocation::LineCRBegin);
GetCurrentWindow()->SetBufferCursor(pos);
GetEditor().ResetCursorTimer();
}
else if (key == ExtKeys::DEL)
{
op = CommandOperation::Delete;
if (m_currentMode == EditorMode::Visual)
{
startOffset = m_visualBegin;
endOffset = m_visualEnd;
normalizeOffsets();
}
else
{
endOffset = buffer.LocationFromOffsetByChars(startOffset, 1);
}
}
else if (key == ExtKeys::BACKSPACE)
{
op = CommandOperation::Delete;
if (m_currentMode == EditorMode::Visual)
{
startOffset = m_visualBegin;
endOffset = m_visualEnd;
normalizeOffsets();
}
else
{
endOffset = startOffset;
startOffset = buffer.LocationFromOffsetByChars(startOffset, -1);
}
}
else
{
op = CommandOperation::Insert;
}
startOffset = buffer.Clamp(startOffset);
endOffset = buffer.Clamp(endOffset);
// Here we handle the logic of wether the visual range starts or ends before or
// after the cursor! It depends on which direction the user moved when they started the selection
if (m_currentMode == EditorMode::Visual)
{
if (begin_shift)
{
if (GetCurrentWindow()->GetBufferCursor() <= startOffset)
{
m_visualBegin = startOffset; // buffer.LocationFromOffsetByChars(startOffset, -1);
}
else
{
m_visualBegin = startOffset;
}
}
if (GetCurrentWindow()->GetBufferCursor() > m_visualBegin)
{
m_visualEnd = GetCurrentWindow()->GetBufferCursor();
}
else
{
m_visualEnd = GetCurrentWindow()->GetBufferCursor(); // buffer.LocationFromOffsetByChars(GetCurrentWindow()->GetBufferCursor(), -1);
}
}
// Op is a copy or also requires the region to be copied
if (copyRegion || op == CommandOperation::Copy)
{
// Grab it
std::string str = std::string(buffer.GetWorkingBuffer().begin() + startOffset, buffer.GetWorkingBuffer().begin() + endOffset);
GetEditor().GetRegister('"').text = str;
GetEditor().GetRegister('"').lineWise = lineWise;
GetEditor().GetRegister('0').text = str;
GetEditor().GetRegister('0').lineWise = lineWise;
// Copy doesn't clear the visual seletion
return_to_insert = false;
}
// check other cases (might copy && delete!)
if (op == CommandOperation::Insert)
{
bool boundary = false;
if (m_currentMode == EditorMode::Visual)
{
startOffset = m_visualBegin;
endOffset = m_visualEnd;
normalizeOffsets();
// Delete existing selection
auto cmd = std::make_shared<ZepCommand_DeleteRange>(buffer, startOffset, endOffset, GetCurrentWindow()->GetBufferCursor());
if (!ch.empty())
{
cmd->SetFlags(CommandFlags::GroupBoundary);
}
AddCommand(std::static_pointer_cast<ZepCommand>(cmd));
boundary = true;
}
// Simple insert
if (!ch.empty())
{
auto cmd = std::make_shared<ZepCommand_Insert>(buffer, startOffset, ch, GetCurrentWindow()->GetBufferCursor());
if (boundary)
{
cmd->SetFlags(CommandFlags::GroupBoundary);
}
AddCommand(std::static_pointer_cast<ZepCommand>(cmd));
}
return_to_insert = true;
}
// Delete from buffer
else if (op == CommandOperation::Delete)
{
// Delete
auto cmd = std::make_shared<ZepCommand_DeleteRange>(buffer, startOffset, endOffset, GetCurrentWindow()->GetBufferCursor());
AddCommand(std::static_pointer_cast<ZepCommand>(cmd));
return_to_insert = true;
}
if (return_to_insert)
{
SwitchMode(EditorMode::Insert);
}
if (m_currentMode == EditorMode::Visual)
{
buffer.SetSelection(GlyphRange{m_visualBegin, m_visualEnd});
}
}
*/
} // namespace Zep