~ecs/sd

ref: d988cda562c3800b153ba8407b2c9f3330c80ba2 sd/input.c -rw-r--r-- 3.9 KiB
d988cda5 — Eyal Sawady run_command: keep searching on -2 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
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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"
#include "rendering.h"
#include "term.h"

static int
process_normal(const char c, struct state *state)
{
	switch (c) {
	case ':':
		state->mode = CMD;
		set_row(&state->status, ":");
		break;
	case 'i':
		state->mode = INPUT;
		set_row(&state->status, "INSERT");
		break;
	/* TODO: long lines */
	case DEL:
	case 'h':
		if (state->cur_x > 0) {
			state->cur_x--;
		}
		break;
	case 'j':
		if (state->cur_y + state->cur_line >= state->lines->sz - 1) {
			break;
		}
		if (state->cur_y < state->rows - 2) {
			state->cur_y++;
		} else {
			state->cur_line++;
		}
		break;
	case 'k':
		if (state->cur_y > 0) {
			state->cur_y--;
		} else if (state->cur_line > 0) {
			state->cur_line--;
		}
		break;
	case 'l':
		if (state->cur_x < state->cols - 1) {
			state->cur_x++;
		}
		break;
	case CTRL_C:
		return -1;
	case '\f':
		set_row(&state->status, "");
		refresh_screen(state);
		break;
	default:
		set_row(&state->status, "Error: unknown command '");

		if (iscntrl(c)) {
			/* '\' + 2 chars + '\0' */
			char buf[4];
			sprintf(buf, "\\%02x", c);
			append_row(&state->status, buf);
		} else {
			char buf[2] = {0, 0};
			buf[0] = c;
			append_row(&state->status, buf);
		}
		append_row(&state->status, "'");
	}
	{
		size_t lineno = state->cur_line + state->cur_y;
		size_t lineln = strlen(state->lines->contents[lineno].contents);

		if (lineln == 0) {
			state->cur_x = 0;
		} else if ((size_t)state->cur_x >= lineln - 1) {
			state->cur_x = lineln - 1;
		}
	}

	return 0;
}

static int
process_insert(const char c, struct state *state)
{
	switch (c) {
	case CTRL_C:
	case ESCAPE:
		insert_row(
			&state->lines->contents[state->cur_line + state->cur_y],
			state->insert_buffer.contents, state->cur_x);
		state->cur_x += strlen(state->insert_buffer.contents) - 1;
		if (state->cur_x < 0) {
			state->cur_x = 0;
		}
		state->mode = NORMAL;
		state->dirty++;
		set_row(&state->status, "");
		set_row(&state->insert_buffer, "");
		break;
	case DEL:
		if (strlen(state->insert_buffer.contents) <= 1) {
			set_row(&state->insert_buffer, "");
		} else {
			char *sibc = state->insert_buffer.contents;
			sibc[strlen(sibc) - 1] = '\0';
		}
		break;
	case '\r':
	case '\n':
		{
			size_t pos = state->cur_line + state->cur_y;
			struct line *cur = &state->lines->contents[pos];
			struct line new = split_row(cur, state->cur_x);
			append_row(cur, state->insert_buffer.contents);
			set_row(&state->insert_buffer, "");
			if (state->cur_y < state->rows - 2) {
				state->cur_y++;
			} else {
				state->cur_line++;
			}
			state->cur_x = 0;
			insert_line(state->lines, new,
				state->cur_line + state->cur_y);
			break;
		}
	default:
		{
			char c_str[] = {'\0', '\0'};
			c_str[0] = c;
			append_row(&state->insert_buffer, c_str);
		}
	}

	return 0;
}

static int
process_cmd(char c, struct state *state) {
	switch (c) {
	case CTRL_C:
	case ESCAPE:
		state->mode = NORMAL;
		set_row(&state->status, "");
		break;
	case DEL:
		if (strlen(state->status.contents) <= 1) {
			state->mode = NORMAL;
			set_row(&state->status, "");
		} else {
			char *ssc = state->status.contents;
			ssc[strlen(ssc) - 1] = '\0';
		}
		break;
	case '\r':
	case '\n':
		{
			char *ssc = state->status.contents;
			int ret =
				run_command(state, ssc + 1);
			state->mode = NORMAL;
			if (ret == -2) {
				/* +1 for NULL, -1 for ':' */
				char *buf = malloc(strlen(ssc));
				strcpy(buf, ssc + 1);
				set_row(&state->status, "Invalid command: '");
				append_row(&state->status, buf);
				append_row(&state->status, "'");
				free(buf);
				return 0;
			}
			return ret;
		}
	default:
		{
			char buf[2] = {0, 0};
			buf[0] = c;
			append_row(&state->status, buf);
		}
	}

	return 0;
}

int
process_char(const char c, struct state *state)
{
	switch (state->mode) {
	case NORMAL:
		return process_normal(c, state);
	case INPUT:
		return process_insert(c, state);
	case CMD:
		return process_cmd(c, state);
	}

	return 0;
}