summaryrefslogtreecommitdiff
path: root/shed_insert.c
blob: 384cbead826b81725becdac5311586f78ee1d8e1 (plain)
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
#define _POSIX_C_SOURCE 200809L
#include <termios.h>
#include "shed.h"

static int shed_insert_special(struct shed *e, struct termios *t, char c)
{
	if (c == '\n') {
		return shed_execute(e);
	}

	if (c == t->c_cc[VERASE]) {
		return shed_backspace(e);
	}

	if (c == t->c_cc[VINTR]) {
		return shed_cancel(e);
	}
	
	if (c == t->c_cc[VKILL]) {
		return shed_erase(e);
	}

	if (c == CTRL_V) {
		/* quote the next character */
		return 1;
	}

	if (c == CTRL_W) {
		return shed_worderase(e);
	}

	if (c == t->c_cc[VEOF]) {
		return shed_eof(e);
	}

	if (c == ESCAPE) {
		e->handle = shed_handle_edit;
		return 1;
	}

	return -1;
}

int shed_handle_insert(struct shed *e, struct termios *t, char c)
{
	int r = shed_insert_special(e, t, c);
	if (r == -1) {
		shed_insert_char(e->cur, c);
		r = 1;
	}
	return r;
}

int shed_handle_replace(struct shed *e, struct termios *t, char c)
{
	int r = shed_insert_special(e, t, c);
	if (r == -1) {
		shed_replace_char(e->cur, c);
		r = 1;
	}
	return r;
}