~ecs/sd

ref: f654cc6705b2e65181f3e1d915388bb592bceb5a sd/input.c -rw-r--r-- 3.5 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
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
#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(char c, struct state *state)
{
	char c_str[] = {'\0', '\0'};
	int ret = 0;
	switch (c) {
	case CTRL_C:
		if (strlen(state->insert_buffer.contents) == 0) {
			return -1;
		}
		/* fallthrough */
	case ESCAPE:
		if (strlen(state->insert_buffer.contents) > 0) {
			set_row(&state->insert_buffer, "");
		}
		break;
	case DEL:
		if (strlen(state->insert_buffer.contents) > 1) {
			char *sibc = state->insert_buffer.contents;
			sibc[strlen(sibc) - 1] = '\0';
			return 0;
		}
		c = 'h';
		/* fallthrough */
	default:
		c_str[0] = c;
		append_row(&state->insert_buffer, c_str);
		ret = run_command(state, state->insert_buffer.contents);
		switch (ret) {
		case -2:
			break;
		default:
			set_row(&state->insert_buffer, "");
		}
	}

	{
		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 ret;
}

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);
			set_row(&state->status, "");
			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;
}