forked from theabraham/chex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.c
184 lines (163 loc) · 4.83 KB
/
route.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
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
#define _POSIX_SOURCE
#include <signal.h>
#include "common.h"
/* Value of KEY when CTRL key is subsequently being held down (e.g. ^d and ^u.) */
#define CTRL(key) ((key) & 0x1F)
/* Move up or down N lines in the buffer. */
static void move_line(int n)
{
int index = buf.index + (n * view.bpline);
buf_setindex(index, buf.nybble);
}
/* Move left or right N lines in the buffer. */
static void move_col(int n)
{
if (buf.mode == HEX) {
/* In HEX mode, 2-columns represent one INDEX value, and odd columns
represent the lower nybble of the current byte. */
int index = buf.index + (n / 2);
if (n % 2 != 0) {
if (buf.nybble) {
buf_setindex(index + (n > 0 ? 1 : 0), false);
} else {
buf_setindex(index - (n > 0 ? 0 : 1), true);
}
} else {
buf_setindex(index, false);
}
} else {
/* ASCII mode is one-to-one in regards of INDEX to COLUMNS. */
buf_setindex(buf.index + n, false);
}
}
/* Suspend the program. */
static void suspend()
{
kill(getpid(), SIGTSTP);
}
/* Toggle the buffer's editing mode between HEX and ASCII. */
static void toggle_mode()
{
if (buf.mode == HEX) {
buf.mode = ASCII;
buf.nybble = false;
} else {
buf.mode = HEX;
}
}
/* Set the buffer's editing state. */
static void set_state(states_t state)
{
buf.state = state;
}
/* Replace the current INDEX in the buffer with the given CH value; however, if
CH is a backspace character, revert the current character at INDEX. */
static void replace_char(char ch)
{
bool is_backspace = (ch == 127);
if (is_backspace) {
move_col(-1);
buf_revertchar();
} else {
buf_putchar(ch);
move_col(+1);
}
}
/* Go to the beginning of the current line. */
static void goto_line_beg()
{
int index = buf.index - (buf.index % view.bpline);
buf_setindex(index, false);
}
/* Go to the end of the current line. */
static void goto_line_end()
{
int index = (buf.index - (buf.index % view.bpline)) + (view.bpline - 1);
if (index >= buf.size)
buf_setindex(buf.size - 1, true);
else
buf_setindex(index, true);
}
/* Skip forwards in the buffer by a byte group. */
static void goto_grp_next()
{
int index = buf.index + (view.bpgrp - (buf.index % view.bpgrp));
buf_setindex(index, false);
}
/* Skip backwards in the buffer by a byte group. */
static void goto_grp_prev()
{
if (buf.index % view.bpgrp) {
/* Go to beginning of current byte group. */
buf_setindex(buf.index - (buf.index % view.bpgrp), false);
} else {
/* Go to beginning of previous byte group. */
buf_setindex(buf.index - view.bpgrp, false);
}
}
/* Go to the beginning of the buffer. */
static void goto_buffer_beg()
{
buf_setindex(0, false);
}
/* Go to the end of the buffer. */
static void goto_buffer_end()
{
int index = buf.size - (buf.size % view.bpline);
buf_setindex(index, false);
}
/* Go down half a screenful in the buffer. */
static void goto_half_next()
{
int index = buf.index + ((LINES / 2) * view.bpline);
buf_setindex(index, buf.nybble);
}
/* Go up half a screenful in the buffer. */
static void goto_half_prev()
{
int index = buf.index - ((LINES / 2) * view.bpline);
buf_setindex(index, buf.nybble);
}
bool route(int ch)
{
/* Keys available in both ESCAPE and REPLACE modes. */
switch (ch) {
case CTRL('c'):
case CTRL('q'): return false; break;
case KEY_SUSPEND:
case CTRL('z'): suspend(); break;
case CTRL('w'): buf_write(); break;
case CTRL('r'): buf_revert(); break;
case CTRL('['): set_state(ESCAPE); break;
case KEY_LEFT: move_col(-1); break;
case KEY_UP: move_line(-1); break;
case KEY_RIGHT: move_col(+1); break;
case KEY_DOWN: move_line(+1); break;
case '\t': toggle_mode(); break;
default:
if (buf.state == REPLACE) {
replace_char(ch);
return true;
}
break;
}
/* Keys available in only ESCAPE mode. */
switch (ch) {
case 'R': set_state(REPLACE); break;
case 'h': move_col(-1); break;
case 'k': move_line(-1); break;
case 'l': move_col(+1); break;
case 'j': move_line(+1); break;
case 'w': goto_grp_next(); break;
case 'b': goto_grp_prev(); break;
case 'g': goto_buffer_beg(); break;
case 'G': goto_buffer_end(); break;
case '^': goto_line_beg(); break;
case '$': goto_line_end(); break;
case 'd': goto_half_next(); break;
case 'u': goto_half_prev(); break;
case '?': view.help = !view.help; break;
default: break;
}
return true;
}