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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGUI/Event.h>
#include <LibGUI/TextEditor.h>
#include <LibGUI/VimEditingEngine.h>
namespace GUI {
VimEditingEngine::VimEditingEngine()
{
m_editing_engine_type = EditingEngineType::Vim;
}
CursorWidth VimEditingEngine::cursor_width() const
{
return m_vim_mode == VimMode::Insert ? CursorWidth::NARROW : CursorWidth::WIDE;
}
bool VimEditingEngine::on_key(const KeyEvent& event)
{
if (EditingEngine::on_key(event))
return true;
switch (m_vim_mode) {
case (VimMode::Insert):
return on_key_in_insert_mode(event);
case (VimMode::Visual):
return on_key_in_visual_mode(event);
case (VimMode::Normal):
return on_key_in_normal_mode(event);
default:
ASSERT_NOT_REACHED();
}
return false;
}
bool VimEditingEngine::on_key_in_insert_mode(const KeyEvent& event)
{
if (event.key() == KeyCode::Key_Escape || (event.ctrl() && event.key() == KeyCode::Key_LeftBracket) || (event.ctrl() && event.key() == KeyCode::Key_C)) {
switch_to_normal_mode();
return true;
}
return false;
}
bool VimEditingEngine::on_key_in_normal_mode(const KeyEvent& event)
{
// FIXME: changing or deleting word methods don't correctly support 1 letter words.
// For example, in the line 'return 0;' with the cursor on the '0',
// keys 'cw' will move to the delete '0;' rather than just the '0'.
// FIXME: Changing or deleting the last word on a line will bring the next line
// up to the cursor.
if (m_previous_key == KeyCode::Key_D) {
if (event.key() == KeyCode::Key_D) {
yank(Line);
delete_line();
} else if (event.key() == KeyCode::Key_W) {
// If the current char is an alnum or punct, delete from said char, to the
// beginning of the next word including any whitespace in between the two words.
// If the current char is whitespace, delete from the cursor to the beginning of the next world.
u32 current_char = m_editor->current_line().view().code_points()[m_editor->cursor().column()];
TextPosition delete_to;
if (isspace(current_char)) {
delete_to = find_beginning_of_next_word();
} else {
delete_to = find_end_of_next_word();
delete_to.set_column(delete_to.column() + 1);
}
m_editor->delete_text_range(TextRange(m_editor->cursor(), delete_to).normalized());
} else if (event.key() == KeyCode::Key_B) {
// Will delete from the current char to the beginning of the previous word regardless of whitespace.
// Does not delete the starting char, see note below.
TextPosition delete_to = find_beginning_of_previous_word();
// NOTE: Intentionally don't adjust m_editor->cursor() for the wide cursor's column
// because in the original vim... they don't.
m_editor->delete_text_range(TextRange(delete_to, m_editor->cursor()).normalized());
} else if (event.key() == KeyCode::Key_E) {
// Delete from the current char to the end of the next word regardless of whitespace.
TextPosition delete_to = find_end_of_next_word();
delete_to.set_column(delete_to.column() + 1);
m_editor->delete_text_range(TextRange(m_editor->cursor(), delete_to).normalized());
}
clear_previous_key();
} else if (m_previous_key == KeyCode::Key_G) {
if (event.key() == KeyCode::Key_G) {
move_to_first_line();
} else if (event.key() == KeyCode::Key_E) {
move_to_end_of_previous_word();
}
clear_previous_key();
} else if (m_previous_key == KeyCode::Key_Y) {
if (event.key() == KeyCode::Key_Y) {
yank(Line);
}
clear_previous_key();
} else if (m_previous_key == KeyCode::Key_C) {
if (event.key() == KeyCode::Key_C) {
// Needed because the code to replace the deleted line is called after delete_line() so
// what was the second last line before the delete, is now the last line.
bool was_second_last_line = m_editor->cursor().line() == m_editor->line_count() - 2;
yank(Line);
delete_line();
if (was_second_last_line || (m_editor->cursor().line() != 0 && m_editor->cursor().line() != m_editor->line_count() - 1)) {
move_one_up(event);
move_to_line_end(event);
m_editor->add_code_point(0x0A);
} else if (m_editor->cursor().line() == 0) {
move_to_line_beginning(event);
m_editor->add_code_point(0x0A);
move_one_up(event);
} else if (m_editor->cursor().line() == m_editor->line_count() - 1) {
m_editor->add_code_point(0x0A);
}
switch_to_insert_mode();
} else if (event.key() == KeyCode::Key_W) {
// Delete to the end of the next word, if in the middle of a word, this will delete
// from the cursor to the said of said word. If the cursor is on whitespace,
// any whitespace between the cursor and the beginning of the next word will be deleted.
u32 current_char = m_editor->current_line().view().code_points()[m_editor->cursor().column()];
TextPosition delete_to;
if (isspace(current_char)) {
delete_to = find_beginning_of_next_word();
} else {
delete_to = find_end_of_next_word();
delete_to.set_column(delete_to.column() + 1);
}
m_editor->delete_text_range(TextRange(m_editor->cursor(), delete_to).normalized());
switch_to_insert_mode();
} else if (event.key() == KeyCode::Key_B) {
// Delete to the beginning of the previous word, if in the middle of a word, this will delete
// from the cursor to the beginning of said word. If the cursor is on whitespace
// it, and the previous word will be deleted.
u32 current_char = m_editor->current_line().view().code_points()[m_editor->cursor().column()];
TextPosition delete_to = find_beginning_of_previous_word();
TextPosition adjusted_cursor = m_editor->cursor();
// Adjust cursor for the column the wide cursor is covering
if (isalnum(current_char) || ispunct(current_char))
adjusted_cursor.set_column(adjusted_cursor.column() + 1);
m_editor->delete_text_range(TextRange(delete_to, adjusted_cursor).normalized());
switch_to_insert_mode();
} else if (event.key() == KeyCode::Key_E) {
// Delete to the end of the next word, if in the middle of a word, this will delete
// from the cursor to the end of said word. If the cursor is on whitespace
// it, and the next word will be deleted.
TextPosition delete_to = find_end_of_next_word();
TextPosition adjusted_cursor = m_editor->cursor();
delete_to.set_column(delete_to.column() + 1);
m_editor->delete_text_range(TextRange(adjusted_cursor, delete_to).normalized());
switch_to_insert_mode();
}
clear_previous_key();
} else {
// Handle first any key codes that are to be applied regardless of modifiers.
switch (event.key()) {
case (KeyCode::Key_Dollar):
move_to_line_end(event);
break;
case (KeyCode::Key_Escape):
if (m_editor->on_escape_pressed)
m_editor->on_escape_pressed();
break;
default:
break;
}
// SHIFT is pressed.
if (event.shift() && !event.ctrl() && !event.alt()) {
switch (event.key()) {
case (KeyCode::Key_A):
move_to_line_end(event);
switch_to_insert_mode();
break;
case (KeyCode::Key_G):
move_to_last_line();
break;
case (KeyCode::Key_I):
move_to_line_beginning(event);
switch_to_insert_mode();
break;
case (KeyCode::Key_O):
move_to_line_beginning(event);
m_editor->add_code_point(0x0A);
move_one_up(event);
switch_to_insert_mode();
break;
default:
break;
}
}
// CTRL is pressed.
if (event.ctrl() && !event.shift() && !event.alt()) {
switch (event.key()) {
case (KeyCode::Key_D):
move_half_page_down(event);
break;
case (KeyCode::Key_R):
m_editor->redo();
break;
case (KeyCode::Key_U):
move_half_page_up(event);
break;
default:
break;
}
}
// FIXME: H and L movement keys will move to the previous or next line when reaching the beginning or end
// of the line and pressed again.
// No modifier is pressed.
if (!event.ctrl() && !event.shift() && !event.alt()) {
switch (event.key()) {
case (KeyCode::Key_A):
move_one_right(event);
switch_to_insert_mode();
break;
case (KeyCode::Key_B):
move_to_beginning_of_previous_word();
break;
case (KeyCode::Key_C):
set_previous_key(event);
break;
case (KeyCode::Key_Backspace):
case (KeyCode::Key_H):
case (KeyCode::Key_Left):
move_one_left(event);
break;
case (KeyCode::Key_D):
set_previous_key(event);
break;
case (KeyCode::Key_E):
move_to_end_of_next_word();
break;
case (KeyCode::Key_G):
set_previous_key(event);
break;
case (KeyCode::Key_Down):
case (KeyCode::Key_J):
move_one_down(event);
break;
case (KeyCode::Key_I):
switch_to_insert_mode();
break;
case (KeyCode::Key_K):
case (KeyCode::Key_Up):
move_one_up(event);
break;
case (KeyCode::Key_L):
case (KeyCode::Key_Right):
move_one_right(event);
break;
case (KeyCode::Key_O):
move_to_line_end(event);
m_editor->add_code_point(0x0A);
switch_to_insert_mode();
break;
case (KeyCode::Key_U):
m_editor->undo();
break;
case (KeyCode::Key_W):
move_to_beginning_of_next_word();
break;
case (KeyCode::Key_X):
yank({ m_editor->cursor(), { m_editor->cursor().line(), m_editor->cursor().column() + 1 } });
delete_char();
break;
case (KeyCode::Key_0):
move_to_line_beginning(event);
break;
case (KeyCode::Key_V):
switch_to_visual_mode();
break;
case (KeyCode::Key_Y):
set_previous_key(event);
break;
case (KeyCode::Key_P):
put(event);
break;
default:
break;
}
}
}
return true;
}
bool VimEditingEngine::on_key_in_visual_mode(const KeyEvent& event)
{
if (m_previous_key == KeyCode::Key_G) {
if (event.key() == KeyCode::Key_G) {
move_to_first_line();
update_selection_on_cursor_move();
} else if (event.key() == KeyCode::Key_E) {
move_to_end_of_previous_word();
update_selection_on_cursor_move();
}
clear_previous_key();
} else {
// Handle first any key codes that are to be applied regardless of modifiers.
switch (event.key()) {
case (KeyCode::Key_Dollar):
move_to_line_end(event);
update_selection_on_cursor_move();
break;
case (KeyCode::Key_Escape):
switch_to_normal_mode();
if (m_editor->on_escape_pressed)
m_editor->on_escape_pressed();
break;
default:
break;
}
// SHIFT is pressed.
if (event.shift() && !event.ctrl() && !event.alt()) {
switch (event.key()) {
case (KeyCode::Key_A):
move_to_line_end(event);
switch_to_insert_mode();
break;
case (KeyCode::Key_G):
move_to_last_line();
break;
case (KeyCode::Key_I):
move_to_line_beginning(event);
switch_to_insert_mode();
break;
default:
break;
}
}
// CTRL is pressed.
if (event.ctrl() && !event.shift() && !event.alt()) {
switch (event.key()) {
case (KeyCode::Key_D):
move_half_page_down(event);
update_selection_on_cursor_move();
break;
case (KeyCode::Key_U):
move_half_page_up(event);
update_selection_on_cursor_move();
break;
default:
break;
}
}
// No modifier is pressed.
if (!event.ctrl() && !event.shift() && !event.alt()) {
switch (event.key()) {
case (KeyCode::Key_B):
move_to_beginning_of_previous_word();
update_selection_on_cursor_move();
break;
case (KeyCode::Key_Backspace):
case (KeyCode::Key_H):
case (KeyCode::Key_Left):
move_one_left(event);
update_selection_on_cursor_move();
break;
case (KeyCode::Key_D):
yank(Selection);
m_editor->do_delete();
switch_to_normal_mode();
break;
case (KeyCode::Key_E):
move_to_end_of_next_word();
update_selection_on_cursor_move();
break;
case (KeyCode::Key_G):
set_previous_key(event);
break;
case (KeyCode::Key_Down):
case (KeyCode::Key_J):
move_one_down(event);
update_selection_on_cursor_move();
break;
case (KeyCode::Key_K):
case (KeyCode::Key_Up):
move_one_up(event);
update_selection_on_cursor_move();
break;
case (KeyCode::Key_L):
case (KeyCode::Key_Right):
move_one_right(event);
update_selection_on_cursor_move();
break;
case (KeyCode::Key_U):
// FIXME: Set selection to uppercase.
break;
case (KeyCode::Key_W):
move_to_beginning_of_next_word();
update_selection_on_cursor_move();
break;
case (KeyCode::Key_X):
yank(Selection);
m_editor->do_delete();
switch_to_normal_mode();
break;
case (KeyCode::Key_0):
move_to_line_beginning(event);
update_selection_on_cursor_move();
break;
case (KeyCode::Key_V):
switch_to_normal_mode();
break;
case (KeyCode::Key_C):
yank(Selection);
m_editor->do_delete();
switch_to_insert_mode();
break;
case (KeyCode::Key_Y):
yank(Selection);
switch_to_normal_mode();
break;
default:
break;
}
}
}
return true;
}
void VimEditingEngine::switch_to_normal_mode()
{
m_vim_mode = VimMode::Normal;
m_editor->reset_cursor_blink();
clear_previous_key();
clear_visual_mode_data();
if (on_mode_change)
on_mode_change(m_vim_mode);
};
void VimEditingEngine::switch_to_insert_mode()
{
m_vim_mode = VimMode::Insert;
m_editor->reset_cursor_blink();
clear_previous_key();
clear_visual_mode_data();
if (on_mode_change)
on_mode_change(m_vim_mode);
};
void VimEditingEngine::switch_to_visual_mode()
{
m_vim_mode = VimMode::Visual;
m_editor->reset_cursor_blink();
clear_previous_key();
m_selection_start_position = m_editor->cursor();
m_editor->selection()->set(m_editor->cursor(), { m_editor->cursor().line(), m_editor->cursor().column() + 1 });
m_editor->did_update_selection();
if (on_mode_change)
on_mode_change(m_vim_mode);
}
void VimEditingEngine::update_selection_on_cursor_move()
{
auto cursor = m_editor->cursor();
auto start = m_selection_start_position < cursor ? m_selection_start_position : cursor;
auto end = m_selection_start_position < cursor ? cursor : m_selection_start_position;
end.set_column(end.column() + 1);
m_editor->selection()->set(start, end);
m_editor->did_update_selection();
}
void VimEditingEngine::clear_visual_mode_data()
{
if (m_editor->has_selection()) {
m_editor->selection()->clear();
m_editor->did_update_selection();
}
m_selection_start_position = {};
}
void VimEditingEngine::move_half_page_up(const KeyEvent& event)
{
move_up(event, 0.5);
};
void VimEditingEngine::move_half_page_down(const KeyEvent& event)
{
move_down(event, 0.5);
};
void VimEditingEngine::yank(YankType type)
{
m_yank_type = type;
if (type == YankType::Line) {
m_yank_buffer = m_editor->current_line().to_utf8();
} else {
m_yank_buffer = m_editor->selected_text();
}
// When putting this, auto indentation (if enabled) will indent as far as
// is necessary, then any whitespace captured before the yanked text will be placed
// after the indentation, doubling the indentation.
if (m_editor->is_automatic_indentation_enabled())
m_yank_buffer = m_yank_buffer.trim_whitespace(TrimMode::Left);
}
void VimEditingEngine::yank(TextRange range)
{
m_yank_type = YankType::Selection;
m_yank_buffer = m_editor->document().text_in_range(range);
}
void VimEditingEngine::put(const GUI::KeyEvent& event)
{
if (m_yank_type == YankType::Line) {
move_to_line_end(event);
StringBuilder sb = StringBuilder(m_yank_buffer.length() + 1);
sb.append_code_point(0x0A);
sb.append(m_yank_buffer);
m_editor->insert_at_cursor_or_replace_selection(sb.to_string());
m_editor->set_cursor({ m_editor->cursor().line(), m_editor->current_line().first_non_whitespace_column() });
} else {
// FIXME: If attempting to put on the last column a line,
// the buffer will bne placed on the next line due to the move_one_left/right behaviour.
move_one_right(event);
m_editor->insert_at_cursor_or_replace_selection(m_yank_buffer);
move_one_left(event);
}
}
}
|