xref: /original-bsd/usr.bin/window/ttzapple.c (revision 3b6250d9)
1 /*
2  * Copyright (c) 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)ttzapple.c	3.11 (Berkeley) 06/06/90";
13 #endif /* not lint */
14 
15 #include "ww.h"
16 #include "tt.h"
17 #include "char.h"
18 
19 /*
20 zz|zapple|perfect apple:\
21 	:am:pt:co#80:li#24:le=^H:nd=^F:up=^K:do=^J:\
22 	:ho=\E0:ll=\E1:cm=\E=%+ %+ :ch=\E<%+ :cv=\E>%+ :\
23 	:cl=\E4:ce=\E2:cd=\E3:rp=\E@%.%+ :\
24 	:so=\E+:se=\E-:\
25 	:dc=\Ec:DC=\EC%+ :ic=\Ei:IC=\EI%+ :\
26 	:al=\Ea:AL=\EA%+ :dl=\Ed:DL=\ED%+ :\
27 	:sf=\Ef:SF=\EF%+ :sr=\Er:SR=\ER%+ :cs=\E?%+ %+ :\
28 	:is=\E-\ET :
29 */
30 
31 #define NCOL		80
32 #define NROW		24
33 #define TOKEN_MAX	32
34 
35 extern short gen_frame[];
36 
37 zz_setmodes(new)
38 {
39 	if (new & WWM_REV) {
40 		if ((tt.tt_modes & WWM_REV) == 0)
41 			ttesc('+');
42 	} else
43 		if (tt.tt_modes & WWM_REV)
44 			ttesc('-');
45 	tt.tt_modes = new;
46 }
47 
48 zz_insline(n)
49 {
50 	if (n == 1)
51 		ttesc('a');
52 	else {
53 		ttesc('A');
54 		ttputc(n + ' ');
55 	}
56 }
57 
58 zz_delline(n)
59 {
60 	if (n == 1)
61 		ttesc('d');
62 	else {
63 		ttesc('D');
64 		ttputc(n + ' ');
65 	}
66 }
67 
68 zz_putc(c)
69 	char c;
70 {
71 	if (tt.tt_nmodes != tt.tt_modes)
72 		zz_setmodes(tt.tt_nmodes);
73 	ttputc(c);
74 	if (++tt.tt_col == NCOL)
75 		tt.tt_col = 0, tt.tt_row++;
76 }
77 
78 zz_write(p, n)
79 	register char *p;
80 	register n;
81 {
82 	if (tt.tt_nmodes != tt.tt_modes)
83 		zz_setmodes(tt.tt_nmodes);
84 	ttwrite(p, n);
85 	tt.tt_col += n;
86 	if (tt.tt_col == NCOL)
87 		tt.tt_col = 0, tt.tt_row++;
88 }
89 
90 zz_move(row, col)
91 	register row, col;
92 {
93 	register x;
94 
95 	if (tt.tt_row == row) {
96 same_row:
97 		if ((x = col - tt.tt_col) == 0)
98 			return;
99 		if (col == 0) {
100 			ttctrl('m');
101 			goto out;
102 		}
103 		switch (x) {
104 		case 2:
105 			ttctrl('f');
106 		case 1:
107 			ttctrl('f');
108 			goto out;
109 		case -2:
110 			ttctrl('h');
111 		case -1:
112 			ttctrl('h');
113 			goto out;
114 		}
115 		if ((col & 7) == 0 && x > 0 && x <= 16) {
116 			ttctrl('i');
117 			if (x > 8)
118 				ttctrl('i');
119 			goto out;
120 		}
121 		ttesc('<');
122 		ttputc(col + ' ');
123 		goto out;
124 	}
125 	if (tt.tt_col == col) {
126 		switch (row - tt.tt_row) {
127 		case 2:
128 			ttctrl('j');
129 		case 1:
130 			ttctrl('j');
131 			goto out;
132 		case -2:
133 			ttctrl('k');
134 		case -1:
135 			ttctrl('k');
136 			goto out;
137 		}
138 		if (col == 0) {
139 			if (row == 0)
140 				goto home;
141 			if (row == NROW - 1)
142 				goto ll;
143 		}
144 		ttesc('>');
145 		ttputc(row + ' ');
146 		goto out;
147 	}
148 	if (col == 0) {
149 		if (row == 0) {
150 home:
151 			ttesc('0');
152 			goto out;
153 		}
154 		if (row == tt.tt_row + 1) {
155 			/*
156 			 * Do newline first to match the sequence
157 			 * for scroll down and return
158 			 */
159 			ttctrl('j');
160 			ttctrl('m');
161 			goto out;
162 		}
163 		if (row == NROW - 1) {
164 ll:
165 			ttesc('1');
166 			goto out;
167 		}
168 	}
169 	/* favor local motion for better compression */
170 	if (row == tt.tt_row + 1) {
171 		ttctrl('j');
172 		goto same_row;
173 	}
174 	if (row == tt.tt_row - 1) {
175 		ttctrl('k');
176 		goto same_row;
177 	}
178 	ttesc('=');
179 	ttputc(' ' + row);
180 	ttputc(' ' + col);
181 out:
182 	tt.tt_col = col;
183 	tt.tt_row = row;
184 }
185 
186 zz_start()
187 {
188 	zz_setmodes(0);
189 	zz_setscroll(0, NROW - 1);
190 	zz_clear();
191 	ttesc('T');
192 	ttputc(TOKEN_MAX + ' ');
193 }
194 
195 zz_end()
196 {
197 	ttesc('T');
198 	ttputc(' ');
199 }
200 
201 zz_clreol()
202 {
203 	ttesc('2');
204 }
205 
206 zz_clreos()
207 {
208 	ttesc('3');
209 }
210 
211 zz_clear()
212 {
213 	ttesc('4');
214 	tt.tt_col = tt.tt_row = 0;
215 }
216 
217 zz_insspace(n)
218 {
219 	if (n == 1)
220 		ttesc('i');
221 	else {
222 		ttesc('I');
223 		ttputc(n + ' ');
224 	}
225 }
226 
227 zz_delchar(n)
228 {
229 	if (n == 1)
230 		ttesc('c');
231 	else {
232 		ttesc('C');
233 		ttputc(n + ' ');
234 	}
235 }
236 
237 zz_scroll_down(n)
238 {
239 	if (n == 1)
240 		if (tt.tt_row == NROW - 1)
241 			ttctrl('j');
242 		else
243 			ttesc('f');
244 	else {
245 		ttesc('F');
246 		ttputc(n + ' ');
247 	}
248 }
249 
250 zz_scroll_up(n)
251 {
252 	if (n == 1)
253 		ttesc('r');
254 	else {
255 		ttesc('R');
256 		ttputc(n + ' ');
257 	}
258 }
259 
260 zz_setscroll(top, bot)
261 {
262 	ttesc('?');
263 	ttputc(top + ' ');
264 	ttputc(bot + ' ');
265 	tt.tt_scroll_top = top;
266 	tt.tt_scroll_bot = bot;
267 }
268 
269 int zz_debug = 0;
270 
271 zz_set_token(t, s, n)
272 	char *s;
273 {
274 	if (tt.tt_nmodes != tt.tt_modes)
275 		zz_setmodes(tt.tt_nmodes);
276 	if (zz_debug) {
277 		char buf[100];
278 		zz_setmodes(WWM_REV);
279 		(void) sprintf(buf, "%02x=", t);
280 		ttputs(buf);
281 		tt.tt_col += 3;
282 	}
283 	ttputc(0x80);
284 	ttputc(t + 1);
285 	s[n - 1] |= 0x80;
286 	ttwrite(s, n);
287 	s[n - 1] &= ~0x80;
288 }
289 
290 /*ARGSUSED*/
291 zz_put_token(t, s, n)
292 	char *s;
293 {
294 	if (tt.tt_nmodes != tt.tt_modes)
295 		zz_setmodes(tt.tt_nmodes);
296 	if (zz_debug) {
297 		char buf[100];
298 		zz_setmodes(WWM_REV);
299 		(void) sprintf(buf, "%02x>", t);
300 		ttputs(buf);
301 		tt.tt_col += 3;
302 	}
303 	ttputc(t + 0x81);
304 }
305 
306 tt_zapple()
307 {
308 	tt.tt_insspace = zz_insspace;
309 	tt.tt_delchar = zz_delchar;
310 	tt.tt_insline = zz_insline;
311 	tt.tt_delline = zz_delline;
312 	tt.tt_clreol = zz_clreol;
313 	tt.tt_clreos = zz_clreos;
314 	tt.tt_scroll_down = zz_scroll_down;
315 	tt.tt_scroll_up = zz_scroll_up;
316 	tt.tt_setscroll = zz_setscroll;
317 	tt.tt_availmodes = WWM_REV;
318 	tt.tt_wrap = 1;
319 	tt.tt_retain = 0;
320 	tt.tt_ncol = NCOL;
321 	tt.tt_nrow = NROW;
322 	tt.tt_start = zz_start;
323 	tt.tt_end = zz_end;
324 	tt.tt_write = zz_write;
325 	tt.tt_putc = zz_putc;
326 	tt.tt_move = zz_move;
327 	tt.tt_clear = zz_clear;
328 	tt.tt_setmodes = zz_setmodes;
329 	tt.tt_frame = gen_frame;
330 	tt.tt_padc = TT_PADC_NONE;
331 	tt.tt_ntoken = 127;
332 	tt.tt_set_token = zz_set_token;
333 	tt.tt_put_token = zz_put_token;
334 	tt.tt_token_min = 1;
335 	tt.tt_token_max = TOKEN_MAX;
336 	tt.tt_set_token_cost = 2;
337 	tt.tt_put_token_cost = 1;
338 	return 0;
339 }
340