-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisual_mode.c
127 lines (112 loc) · 3.31 KB
/
visual_mode.c
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
#include "mode.h"
#include <ctype.h>
#include <stdlib.h>
#include <termbox.h>
#include "buf.h"
#include "buffer.h"
#include "gap.h"
#include "editor.h"
#include "motion.h"
#include "util.h"
#include "window.h"
void visual_mode_entered(struct editor *editor) {
struct visual_mode *mode = editor_get_visual_mode(editor);
mode->kind = (enum visual_mode_kind) mode->mode.arg;
// If we're entering the mode by popping (e.g. because we did a search and
// finished), then keep the current anchor.
if (!mode->cursor) {
mode->cursor = window_cursor(editor->window);
editor->window->visual_mode_selection = &mode->selection;
visual_mode_selection_update(editor);
}
if (editor->opt.showmode) {
editor_status_msg(editor, "-- VISUAL --");
}
}
void visual_mode_exited(struct editor *editor) {
struct visual_mode *mode = editor_get_visual_mode(editor);
mode->cursor = 0;
editor->window->visual_mode_selection = NULL;
buf_clear(editor->status);
}
void visual_mode_key_pressed(struct editor* editor, struct tb_event* ev) {
if (ev->ch != '0' && isdigit((int) ev->ch)) {
editor->count = 0;
while (isdigit((int) ev->ch)) {
editor->count *= 10;
editor->count += ev->ch - '0';
editor_waitkey(editor, ev);
}
}
if (ev->ch == '"') {
editor_waitkey(editor, ev);
char name = (char) tolower((int) ev->ch);
if (editor_get_register(editor, name)) {
editor->register_ = name;
}
return;
}
switch (ev->key) {
case TB_KEY_ESC: case TB_KEY_CTRL_C:
editor_pop_mode(editor);
return;
case TB_KEY_CTRL_B:
window_page_up(editor->window);
break;
case TB_KEY_CTRL_F:
window_page_down(editor->window);
break;
default: {
struct motion *motion = motion_get(editor, ev);
if (motion) {
window_set_cursor(editor->window, motion_apply(motion, editor));
return;
}
op_func *op = op_find((char) ev->ch);
if (op) {
struct region *selection = editor->window->visual_mode_selection;
editor_pop_mode(editor);
op(editor, selection);
}
}
}
}
// FIXME(ibadawi): Duplicated from motion.c
static bool is_line_start(struct gapbuf *gb, size_t pos) {
return pos == 0 || gb_getchar(gb, pos - 1) == '\n';
}
static bool is_line_end(struct gapbuf *gb, size_t pos) {
return pos == gb_size(gb) - 1 || gb_getchar(gb, pos) == '\n';
}
static size_t line_start(struct gapbuf *gb, size_t pos) {
if (is_line_start(gb, pos)) {
return pos;
}
return (size_t) (gb_lastindexof(gb, '\n', pos - 1) + 1);
}
static size_t line_end(struct gapbuf *gb, size_t pos) {
if (is_line_end(gb, pos)) {
return pos;
}
return gb_indexof(gb, '\n', pos);
}
void visual_mode_selection_update(struct editor *editor) {
struct visual_mode *mode = (struct visual_mode*) editor->mode;
while (mode && mode != &editor->modes.visual) {
mode = (struct visual_mode*) mode->mode.parent;
}
if (!mode) {
return;
}
struct region *selection = &mode->selection;
region_set(selection, window_cursor(editor->window), mode->cursor);
struct gapbuf *gb = editor->window->buffer->text;
if (mode->kind == VISUAL_MODE_LINEWISE) {
selection->start = line_start(gb, selection->start);
selection->end = line_end(gb, selection->end);
}
if (mode->kind == VISUAL_MODE_LINEWISE ||
!is_line_end(gb, selection->end)) {
++selection->end;
}
}