-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.c
157 lines (131 loc) · 3.77 KB
/
main.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
/*
* main.c, Femto Emacs, Hugh Barney, Public Domain, 2016
* Derived from: Anthony's Editor January 93, (Public Domain 1991, 1993 by Anthony Howe)
*/
#include "header.h"
int main(int argc, char **argv)
{
int i;
/*m0*/ MEVENT event;
/* Find basename. */
prog_name = *argv;
i = strlen(prog_name);
while (0 <= i && prog_name[i] != '\\' && prog_name[i] != '/')
--i;
prog_name += i+1;
setlocale(LC_ALL, "") ; /* required for 3,4 byte UTF8 chars */
/* we need to load up lisp early as it has an impact on mode settings for buffers */
init_lisp(512); /* with 512k heap */
if (initscr() == NULL)
fatal(f_initscr);
raw();
noecho();
idlok(stdscr, TRUE);
start_color();
init_pair(ID_COLOR_SYMBOL, COLOR_WHITE, COLOR_BLACK); /* non alpha, non digit */
init_pair(ID_COLOR_MODELINE, COLOR_BLACK, COLOR_WHITE); /* modeline */
init_pair(ID_COLOR_BRACE, COLOR_BLACK, COLOR_CYAN); /* brace highlight */
init_pair(ID_COLOR_KEYWORD, COLOR_MAGENTA, COLOR_BLACK); /* keyword */
init_pair(ID_COLOR_ALPHA, COLOR_CYAN, COLOR_BLACK); /* alpha */
init_pair(ID_COLOR_DIGITS, COLOR_YELLOW, COLOR_BLACK); /* digits */
init_pair(ID_COLOR_COMMENTS, COLOR_GREEN, COLOR_BLACK); /* line comments */
init_pair(ID_COLOR_BLOCK, COLOR_GREEN, COLOR_BLACK); /* block comments */
init_pair(ID_COLOR_STRING, COLOR_YELLOW, COLOR_BLACK); /* strings */
if ( (argc == 3) && (strcmp(argv[2], "+") == 0) )
{ mousemask( ALL_MOUSE_EVENTS |
REPORT_MOUSE_POSITION, NULL);
}
if ( (argc == 2) && (strcmp(argv[1], "+") == 0))
{ mousemask( ALL_MOUSE_EVENTS |
REPORT_MOUSE_POSITION, NULL);
argc = 1;
}
bkgd((chtype) (' ' | COLOR_PAIR(ID_COLOR_ALPHA)));
if (argc > 1) {
char bname[NBUFN];
char fname[NAME_MAX + 1];
/* Save filename irregardless of load() success. */
safe_strncpy(fname, argv[1], NAME_MAX);
make_buffer_name(bname, fname);
curbp = find_buffer(bname, TRUE);
(void)insert_file(fname, FALSE);
strcpy(curbp->b_fname, fname);
} else {
curbp = find_buffer(str_scratch, TRUE);
}
wheadp = curwp = new_window();
one_window(curwp);
associate_b2w(curbp, curwp);
beginning_of_buffer();
undoset();
call_lisp("(show-startup-message)");
key_map = keymap;
while (!done) {
update_display();
input = get_key(key_map, &key_return);
if (key_return != NULL) {
whatKey= key_return->key_name;
(key_return->func)();
} else {
/*
* if first char of input is a control char then
* key is not bound, except TAB and NEWLINE
*/
if (*input > 31 || *input == 0x0A || *input == 0x09)
insert();
else
msg(str_not_bound);
}
/* debug_stats("main loop:"); */
match_parens();
}
if (scrap != NULL)
free(scrap);
move(LINES-1, 0);
refresh();
noraw();
endwin();
return (EXIT_OK);
}
void fatal(char *m)
{
if (curscr != NULL) {
move(LINES-1, 0);
refresh();
endwin();
putchar('\n');
}
fprintf(stderr, m, prog_name);
if (m == f_ok)
exit(EXIT_OK);
if (m == f_error)
exit(EXIT_ERROR);
if (m == f_usage)
exit(EXIT_USAGE);
exit(EXIT_FAIL);
}
void msg(char *m, ...)
{
va_list args;
va_start(args, m);
(void) vsprintf(msgline, m, args);
va_end(args);
msgflag = TRUE;
}
void debug(char *format, ...)
{
char buffer[256];
va_list args;
va_start (args, format);
static FILE *debug_fp = NULL;
if (debug_fp == NULL) {
debug_fp = fopen("debug.out","w");
}
vsprintf (buffer, format, args);
va_end(args);
fprintf(debug_fp,"%s", buffer);
fflush(debug_fp);
}
void debug_stats(char *s) {
debug("%s bsz=%d p=%d m=%d gap=%d egap=%d\n", s, curbp->b_ebuf - curbp->b_buf, curbp->b_point, curbp->b_mark, curbp->b_gap - curbp->b_buf, curbp->b_egap - curbp->b_buf);
}