-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
194 lines (172 loc) · 3.92 KB
/
main.c
File metadata and controls
194 lines (172 loc) · 3.92 KB
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
#include "util.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include "emsys.h"
#include "fileio.h"
#include "find.h"
#include "pipe.h"
#include "region.h"
#include "register.h"
#include "history.h"
#include "buffer.h"
#include "completion.h"
#include "transform.h"
#include "undo.h"
#include "unicode.h"
#include "unused.h"
#include "terminal.h"
#include "display.h"
#include "keymap.h"
#include "util.h"
const int page_overlap = 2;
struct editorConfig E;
void setupHandlers(void);
/*** output ***/
void editorSuspend(int UNUSED(sig)) {
signal(SIGTSTP, SIG_DFL);
disableRawMode();
raise(SIGTSTP);
}
void editorResume(int UNUSED(sig)) {
setupHandlers();
enableRawMode();
editorResizeScreen(0);
}
#ifdef SIGWINCH
void sigwinchHandler(int UNUSED(sig)) {
editorResizeScreen(0);
}
#endif
/*** init ***/
void setupHandlers(void) {
#ifdef SIGWINCH
signal(SIGWINCH, sigwinchHandler);
#endif
signal(SIGCONT, editorResume);
signal(SIGTSTP, editorSuspend);
}
void initEditor(void) {
E.statusmsg[0] = 0;
E.kill = NULL;
E.rectKill = NULL;
E.windows = xmalloc(sizeof(struct editorWindow *) * 1);
E.windows[0] = xcalloc(1, sizeof(struct editorWindow));
E.windows[0]->focused = 1;
E.nwindows = 1;
E.recording = 0;
E.macro.nkeys = 0;
E.macro.keys = NULL;
E.micro = 0;
E.playback = 0;
E.headbuf = NULL;
memset(E.registers, 0, sizeof(E.registers));
setupCommands(&E);
E.lastVisitedBuffer = NULL;
E.macro_depth = 0;
initHistory(&E.file_history);
initHistory(&E.command_history);
initHistory(&E.shell_history);
initHistory(&E.search_history);
initHistory(&E.kill_history);
E.kill_ring_pos = -1;
if (getWindowSize(&E.screenrows, &E.screencols) == -1)
die("getWindowSize");
}
int main(int argc, char *argv[]) {
// Check for --version flag before entering raw mode
if (argc >= 2 && strcmp(argv[1], "--version") == 0) {
printf("emsys %s\n", EMSYS_VERSION);
return 0;
}
enableRawMode();
initEditor();
E.headbuf = newBuffer();
E.buf = E.headbuf;
if (argc >= 2) {
int i = 1;
int linum = -1;
if (argv[1][0] == '+' && argc > 2) {
linum = atoi(argv[1] + 1);
i++;
}
for (; i < argc; i++) {
struct editorBuffer *newBuf = newBuffer();
editorOpen(newBuf, argv[i]);
newBuf->next = E.headbuf;
if (linum > 0) {
if (newBuf->numrows == 0) {
newBuf->cy = 0;
} else if (linum - 1 >= newBuf->numrows) {
newBuf->cy = newBuf->numrows - 1;
} else {
newBuf->cy = linum - 1;
}
linum = -1;
}
E.headbuf = newBuf;
E.buf = newBuf;
}
}
E.windows[0]->buf = E.buf;
/* Initialize minibuffer */
E.minibuf = newBuffer();
E.minibuf->single_line = 1;
E.minibuf->truncate_lines = 1;
E.minibuf->filename = xstrdup("*minibuffer*");
E.edbuf = E.buf;
editorSetStatusMessage("emsys " EMSYS_VERSION " - C-x C-c to quit");
setupHandlers();
for (;;) {
refreshScreen();
int c = editorReadKey();
if (c == MACRO_RECORD) {
if (E.recording) {
editorSetStatusMessage(
"Already defining keyboard macro");
} else {
editorSetStatusMessage(
"Defining keyboard macro...");
E.recording = 1;
E.macro.nkeys = 0;
E.macro.skeys = 0x10;
free(E.macro.keys);
E.macro.keys =
xmalloc(E.macro.skeys * sizeof(int));
}
} else if (c == MACRO_END) {
if (E.recording) {
editorSetStatusMessage(
"Keyboard macro defined");
E.recording = 0;
} else {
editorSetStatusMessage(
"Not defining keyboard macro");
}
} else if (c == MACRO_EXEC ||
(E.micro == MACRO_EXEC && (c == 'e' || c == 'E'))) {
if (E.recording) {
editorSetStatusMessage(
"Keyboard macro defined");
E.recording = 0;
}
editorExecMacro(&E.macro);
E.micro = MACRO_EXEC;
} else {
editorRecordKey(c);
executeCommand(c);
}
}
/* cleanup */
free(E.kill);
return 0;
}