-
Notifications
You must be signed in to change notification settings - Fork 25
/
display.c
285 lines (252 loc) · 6.99 KB
/
display.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* display.c, Atto Emacs, Public Domain, Hugh Barney, 2016, Derived from: Anthony's Editor January 93 */
#include "header.h"
/* Reverse scan for start of logical line containing offset */
point_t lnstart(buffer_t *bp, register point_t off)
{
register char_t *p;
do
p = ptr(bp, --off);
while (bp->b_buf < p && *p != '\n');
return (bp->b_buf < p ? ++off : 0);
}
/* Forward scan for start of logical line segment (corresponds to screen line) containing 'finish' */
point_t segstart(buffer_t *bp, point_t start, point_t finish)
{
char_t *p;
int c = 0;
point_t scan = start;
while (scan < finish) {
p = ptr(bp, scan);
if (*p == '\n') {
c = 0;
start = scan + 1;
} else if (COLS <= c) {
c = 0;
start = scan;
}
scan += utf8_size(*ptr(bp,scan));
c += *p == '\t' ? 8 - (c & 7) : 1;
}
return (c < COLS ? start : finish);
}
/* Forward scan for start of logical line segment following 'finish' */
point_t segnext(buffer_t *bp, point_t start, point_t finish)
{
char_t *p;
int c = 0;
point_t scan = segstart(bp, start, finish);
for (;;) {
p = ptr(bp, scan);
if (bp->b_ebuf <= p || COLS <= c)
break;
scan += utf8_size(*ptr(bp,scan));
if (*p == '\n')
break;
c += *p == '\t' ? 8 - (c & 7) : 1;
}
return (p < bp->b_ebuf ? scan : pos(bp, bp->b_ebuf));
}
/* Move up one screen line */
point_t upup(buffer_t *bp, point_t off)
{
point_t curr = lnstart(bp, off);
point_t seg = segstart(bp, curr, off);
if (curr < seg)
off = segstart(bp, curr, seg-1);
else
off = segstart(bp, lnstart(bp,curr-1), curr-1);
return (off);
}
/* Move down one screen line */
point_t dndn(buffer_t *bp, point_t off)
{
return (segnext(bp, lnstart(bp,off), off));
}
/* Return the offset of a column on the specified line */
point_t lncolumn(buffer_t *bp, point_t offset, int column)
{
int c = 0;
char_t *p;
while ((p = ptr(bp, offset)) < bp->b_ebuf && *p != '\n' && c < column) {
c += *p == '\t' ? 8 - (c & 7) : 1;
offset += utf8_size(*ptr(bp,offset));
}
return (offset);
}
void display(window_t *wp, int flag)
{
char_t *p;
int i, j, k, nch;
buffer_t *bp = wp->w_bufp;
int token_type = ID_DEFAULT;
/* find start of screen, handle scroll up off page or top of file */
/* point is always within b_page and b_epage */
if (bp->b_point < bp->b_page)
bp->b_page = segstart(bp, lnstart(bp,bp->b_point), bp->b_point);
/* reframe when scrolled off bottom */
if (bp->b_reframe == 1 || (bp->b_epage <= bp->b_point && curbp->b_point != pos(curbp, curbp->b_ebuf))) {
bp->b_reframe = 0;
/* Find end of screen plus one. */
bp->b_page = dndn(bp, bp->b_point);
/* if we scroll to EOF we show 1 blank line at bottom of screen */
if (pos(bp, bp->b_ebuf) <= bp->b_page) {
bp->b_page = pos(bp, bp->b_ebuf);
i = wp->w_rows - 1;
} else {
i = wp->w_rows - 0;
}
/* Scan backwards the required number of lines. */
while (0 < i--)
bp->b_page = upup(bp, bp->b_page);
}
move(wp->w_top, 0); /* start from top of window */
i = wp->w_top;
j = 0;
bp->b_epage = bp->b_page;
set_parse_state(bp, bp->b_epage); /* are we in a multline comment ? */
/* paint screen from top of page until we hit maxline */
while (1) {
/* reached point - store the cursor position */
if (bp->b_point == bp->b_epage) {
bp->b_row = i;
bp->b_col = j;
}
p = ptr(bp, bp->b_epage);
nch = 1;
if (wp->w_top + wp->w_rows <= i || bp->b_ebuf <= p) /* maxline */
break;
if (*p != '\r') {
nch = utf8_size(*p);
if ( nch > 1) {
wchar_t c;
/* reset if invalid multi-byte character */
if (mbtowc(&c, (char*)p, 6) < 0) mbtowc(NULL, NULL, 0);
j += wcwidth(c) < 0 ? 1 : wcwidth(c);
display_utf8(bp, *p, nch);
} else if (isprint(*p) || *p == '\t' || *p == '\n') {
j += *p == '\t' ? 8-(j&7) : 1;
token_type = parse_text(bp, bp->b_epage);
attron(COLOR_PAIR(token_type));
addch(*p);
} else {
const char *ctrl = unctrl(*p);
j += (int) strlen(ctrl);
addstr(ctrl);
}
}
if (*p == '\n' || COLS <= j) {
j -= COLS;
if (j < 0)
j = 0;
++i;
}
bp->b_epage = bp->b_epage + nch;
}
/* replacement for clrtobot() to bottom of window */
for (k=i; k < wp->w_top + wp->w_rows; k++) {
move(k, j); /* clear from very last char not start of line */
clrtoeol();
j = 0; /* thereafter start of line */
}
b2w(wp); /* save buffer stuff on window */
modeline(wp);
if (wp == curwp && flag) {
dispmsg();
move(bp->b_row, bp->b_col); /* set cursor */
refresh();
}
wp->w_update = FALSE;
attron(COLOR_PAIR(ID_SYMBOL));
}
void display_utf8(buffer_t *bp, char_t c, int n)
{
char sbuf[6];
int i = 0;
for (i=0; i<n; i++)
sbuf[i] = *ptr(bp, bp->b_epage + i);
sbuf[n] = '\0';
addstr(sbuf);
}
void modeline(window_t *wp)
{
int i;
char lch, mch, och;
attron(COLOR_PAIR(ID_MODELINE));
move(wp->w_top + wp->w_rows, 0);
lch = (wp == curwp ? '=' : '-');
mch = ((wp->w_bufp->b_flags & B_MODIFIED) ? '*' : lch);
och = ((wp->w_bufp->b_flags & B_OVERWRITE) ? 'O' : lch);
sprintf(temp, "%c%c%c Atto: %c%c %s", lch,och,mch,lch,lch, get_buffer_name(wp->w_bufp));
addstr(temp);
for (i = strlen(temp) + 1; i <= COLS; i++)
addch(lch);
}
void dispmsg()
{
move(MSGLINE, 0);
if (msgflag) {
attron(COLOR_PAIR(ID_SYMBOL));
addstr(msgline);
msgflag = FALSE;
}
clrtoeol();
}
void display_prompt_and_response(char *prompt, char *response)
{
mvaddstr(MSGLINE, 0, prompt);
/* if we have a value print it and go to end of it */
if (response[0] != '\0')
addstr(response);
clrtoeol();
}
void update_display()
{
window_t *wp;
buffer_t *bp;
bp = curwp->w_bufp;
bp->b_cpoint = bp->b_point; /* cpoint only ever set here */
/* only one window */
if (wheadp->w_next == NULL) {
display(curwp, TRUE);
refresh();
bp->b_psize = bp->b_size;
return;
}
display(curwp, FALSE); /* this is key, we must call our win first to get accurate page and epage etc */
/* never curwp, but same buffer in different window or update flag set*/
for (wp=wheadp; wp != NULL; wp = wp->w_next) {
if (wp != curwp && (wp->w_bufp == bp || wp->w_update)) {
w2b(wp);
display(wp, FALSE);
}
}
/* now display our window and buffer */
w2b(curwp);
dispmsg();
move(curwp->w_row, curwp->w_col); /* set cursor for curwp */
refresh();
bp->b_psize = bp->b_size; /* now safe to save previous size for next time */
}
void w2b(window_t *w)
{
w->w_bufp->b_point = w->w_point;
w->w_bufp->b_page = w->w_page;
w->w_bufp->b_epage = w->w_epage;
w->w_bufp->b_row = w->w_row;
w->w_bufp->b_col = w->w_col;
/* fixup pointers in other windows of the same buffer, if size of edit text changed */
if (w->w_bufp->b_point > w->w_bufp->b_cpoint) {
w->w_bufp->b_point += (w->w_bufp->b_size - w->w_bufp->b_psize);
w->w_bufp->b_page += (w->w_bufp->b_size - w->w_bufp->b_psize);
w->w_bufp->b_epage += (w->w_bufp->b_size - w->w_bufp->b_psize);
}
}
void b2w(window_t *w)
{
w->w_point = w->w_bufp->b_point;
w->w_page = w->w_bufp->b_page;
w->w_epage = w->w_bufp->b_epage;
w->w_row = w->w_bufp->b_row;
w->w_col = w->w_bufp->b_col;
w->w_bufp->b_size = (w->w_bufp->b_ebuf - w->w_bufp->b_buf) - (w->w_bufp->b_egap - w->w_bufp->b_gap);
}