~ecs/sd

ref: f654cc6705b2e65181f3e1d915388bb592bceb5a sd/commands.c -rw-r--r-- 1.9 KiB
f654cc67 — Eyal Sawady Use registry-based commands for normal mode 10 months ago
                                                                                
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
#include "common.h"
#include "rendering.h"

int
quit(struct state *state)
{
	(void)state;
	return -1;
}

int
force_quit(struct state *state)
{
	state->dirty = 0;
	return -1;
}

int
nop(struct state *state)
{
	(void)state;
	return 0;
}

int
init_creg(struct registry *reg)
{
	init_registry(reg);
	if (register_command(reg, "q", quit)) {
		return -1;
	}
	if (register_command(reg, "q!", force_quit)) {
		return -1;
	}
	if (register_command(reg, "nop", nop)) {
		return -1;
	}
	/* TODO: more commands */
	return 0;
}

int
h(struct state *state)
{
	/* TODO: horizontal scrolling */
	if (state->cur_x > 0) {
		state->cur_x--;
	}
	return 0;
}

int
j(struct state *state)
{
	if (state->cur_y + state->cur_line >= state->lines->sz - 1) {
		return 0;
	}
	if (state->cur_y < state->rows - 2) {
		state->cur_y++;
	} else {
		state->cur_line++;
	}
	return 0;
}

int
k(struct state *state)
{
	if (state->cur_y > 0) {
		state->cur_y--;
	} else if (state->cur_line > 0) {
		state->cur_line--;
	}
	return 0;
}

int
l(struct state *state)
{
	/* TODO: horizontal scrolling */
	if (state->cur_x < state->cols - 1) {
		state->cur_x++;
	}
	return 0;
}

int
colon(struct state *state)
{
	state->mode = CMD;
	set_row(&state->status, ":");
	return 0;
}

int
form_feed(struct state *state)
{
	set_row(&state->status, "");
	refresh_screen(state);
	return 0;
}

int
i(struct state *state)
{
	state->mode = INPUT;
	set_row(&state->status, "INSERT");
	return 0;
}

int
init_nreg(struct registry *reg)
{
	init_registry(reg);
	if (register_command(reg, "j", j)) {
		return -1;
	}
	if (register_command(reg, "k", k)) {
		return -1;
	}
	if (register_command(reg, "h", h)) {
		return -1;
	}
	if (register_command(reg, "l", l)) {
		return -1;
	}
	if (register_command(reg, "i", i)) {
		return -1;
	}
	if (register_command(reg, ":", colon)) {
		return -1;
	}
	if (register_command(reg, "\f", form_feed)) {
		return -1;
	}
	return 0;
}