insert mode bug fix and cleanup
[unix-history] / usr / src / usr.bin / window / ttzapple.c
CommitLineData
1dbd69e7 1/*
2d152f42 2 * Copyright (c) 1989 Regents of the University of California.
1dbd69e7
EW
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#ifndef lint
d9375810 19static char sccsid[] = "@(#)ttzapple.c 3.5 (Berkeley) %G%";
1dbd69e7
EW
20#endif /* not lint */
21
22#include "ww.h"
23#include "tt.h"
24#include "char.h"
25
26/*
27zz|zapple|unorthodox apple:\
28 :am:pt:co#80:li#24:le=^H:nd=^F:up=^K:do=^J:\
29 :ho=^[0:ll=^[1:cm=^]%+ %+ =:ch=^\%+ <:cv=^\%+ >:\
30 :cl=^[4:ce=^[2:cd=^[3:rp=^]%.%+ @:\
31 :so=^[+:se=^[-:\
32 :dc=^[c:DC=^\%+ C:ic=^[i:IC=^\%+ I:\
33 :al=^[a:AL=^\%+ A:dl=^[d:DL=^\%+ D:\
34 :sf=^[f:SF=^\%+ F:sr=^[r:SR=^\%+ R:cs=^]%+ %+ ?:
35*/
36
ab8b3b31
EW
37#define NCOL 80
38#define NROW 24
39#define TOKEN_MAX 32
1dbd69e7
EW
40
41#define pc(c) ttputc(c)
42#define esc() pc(ctrl('['))
43#define esc1() pc(ctrl('\\'))
44#define esc2() pc(ctrl(']'))
45
1dbd69e7
EW
46extern short gen_frame[];
47
1dbd69e7
EW
48zz_setmodes(new)
49{
50 if (new & WWM_REV) {
51 if ((tt.tt_modes & WWM_REV) == 0) {
52 esc();
53 pc('+');
54 }
55 } else
56 if (tt.tt_modes & WWM_REV) {
57 esc();
58 pc('-');
59 }
60 tt.tt_modes = new;
61}
62
2d152f42 63zz_insline(n)
1dbd69e7 64{
2d152f42
EW
65 if (n == 0) {
66 esc();
67 pc('a');
68 } else {
69 esc1();
70 pc(n + ' ');
71 pc('A');
72 }
1dbd69e7
EW
73}
74
2d152f42 75zz_delline(n)
1dbd69e7 76{
2d152f42
EW
77 if (n == 0) {
78 esc();
79 pc('d');
80 } else {
81 esc1();
82 pc(n + ' ');
83 pc('D');
84 }
1dbd69e7
EW
85}
86
87zz_putc(c)
88 char c;
89{
1dbd69e7
EW
90 if (tt.tt_nmodes != tt.tt_modes)
91 zz_setmodes(tt.tt_nmodes);
2d152f42 92 ttputc(c);
1dbd69e7
EW
93 if (++tt.tt_col == NCOL)
94 tt.tt_col = 0, tt.tt_row++;
95}
96
1dbd69e7
EW
97zz_write(p, n)
98 register char *p;
99 register n;
100{
1dbd69e7
EW
101 if (tt.tt_nmodes != tt.tt_modes)
102 zz_setmodes(tt.tt_nmodes);
ab8b3b31 103 ttwrite(p, n);
1dbd69e7
EW
104 tt.tt_col += n;
105 if (tt.tt_col == NCOL)
106 tt.tt_col = 0, tt.tt_row++;
107}
108
109zz_move(row, col)
110 register row, col;
111{
ab8b3b31
EW
112 register x;
113
1dbd69e7 114 if (tt.tt_row == row) {
ab8b3b31 115 if ((x = col - tt.tt_col) == 0)
1dbd69e7
EW
116 return;
117 if (col == 0) {
118 pc('\r');
119 goto out;
120 }
ab8b3b31
EW
121 switch (x) {
122 case 2:
123 pc(ctrl('f'));
124 case 1:
1dbd69e7
EW
125 pc(ctrl('f'));
126 goto out;
ab8b3b31 127 case -2:
1dbd69e7 128 pc('\b');
ab8b3b31
EW
129 case -1:
130 pc('\b');
131 goto out;
132 }
133 if (col & 7 == 0 && x > 0 && x <= 16) {
134 pc('\t');
135 if (x > 8)
136 pc('\t');
1dbd69e7
EW
137 goto out;
138 }
139 esc1();
140 pc(col + ' ');
141 pc('<');
142 goto out;
143 }
144 if (tt.tt_col == col) {
ab8b3b31
EW
145 switch (row - tt.tt_row) {
146 case 2:
147 pc('\n');
148 case 1:
1dbd69e7
EW
149 pc('\n');
150 goto out;
ab8b3b31
EW
151 case -2:
152 pc(ctrl('k'));
153 case -1:
154 pc(ctrl('k'));
155 goto out;
1dbd69e7
EW
156 }
157 if (col == 0) {
158 if (row == 0)
159 goto home;
160 if (row == NROW - 1)
161 goto ll;
162 }
163 esc1();
164 pc(row + ' ');
165 pc('>');
166 goto out;
167 }
168 if (col == 0) {
169 if (row == 0) {
170home:
171 esc();
172 pc('0');
173 goto out;
174 }
ab8b3b31
EW
175 if (row == tt.tt_row + 1) {
176 pc('\r');
177 pc('\n');
178 goto out;
179 }
1dbd69e7
EW
180 if (row == NROW - 1) {
181ll:
182 esc();
183 pc('1');
184 goto out;
185 }
186 }
187 esc2();
188 pc(' ' + row);
189 pc(' ' + col);
190 pc('=');
191out:
192 tt.tt_col = col;
193 tt.tt_row = row;
194}
195
ab8b3b31 196zz_start()
1dbd69e7 197{
1dbd69e7
EW
198 zz_setmodes(0);
199 zz_setscroll(0, NROW - 1);
200 zz_clear();
2d152f42 201 esc1();
ab8b3b31 202 pc(TOKEN_MAX + ' ');
2d152f42 203 pc('T');
1dbd69e7
EW
204}
205
206zz_end()
207{
2d152f42
EW
208 esc1();
209 pc(' ');
210 pc('T');
1dbd69e7
EW
211}
212
213zz_clreol()
214{
215 esc();
216 pc('2');
217}
218
219zz_clreos()
220{
221 esc();
222 pc('3');
223}
224
225zz_clear()
226{
227 esc();
228 pc('4');
229 tt.tt_col = tt.tt_row = 0;
230}
231
d9375810 232zz_insspace(n)
1dbd69e7 233{
2d152f42
EW
234 if (n != 1) {
235 esc1();
236 pc(n + ' ');
237 pc('I');
238 } else {
239 esc();
240 pc('i');
241 }
242}
243
244zz_delchar(n)
245{
246 if (n != 1) {
247 esc1();
248 pc(n + ' ');
249 pc('C');
250 } else {
251 esc();
252 pc('c');
253 }
1dbd69e7
EW
254}
255
2d152f42 256zz_scroll_down(n)
1dbd69e7 257{
2d152f42
EW
258 if (n != 1) {
259 esc1();
260 pc(n + ' ');
261 pc('F');
ab8b3b31 262 } else if (tt.tt_row == NROW - 1)
1dbd69e7
EW
263 pc('\n');
264 else {
265 esc();
266 pc('f');
267 }
268}
269
2d152f42 270zz_scroll_up(n)
1dbd69e7 271{
2d152f42
EW
272 if (n == 1) {
273 esc();
274 pc('r');
275 } else {
276 esc1();
277 pc(n + ' ');
278 pc('R');
279 }
1dbd69e7
EW
280}
281
282zz_setscroll(top, bot)
283{
284 esc2();
285 pc(top + ' ');
286 pc(bot + ' ');
287 pc('?');
288 tt.tt_scroll_top = top;
289 tt.tt_scroll_bot = bot;
290}
291
ab8b3b31
EW
292int zz_debug = 0;
293
294zz_set_token(t, s, n)
2d152f42 295 char *s;
1dbd69e7 296{
ab8b3b31
EW
297 if (tt.tt_nmodes != tt.tt_modes)
298 zz_setmodes(tt.tt_nmodes);
299 if (zz_debug) {
300 char buf[100];
301 zz_setmodes(WWM_REV);
302 (void) sprintf(buf, "%02x=", t);
303 ttputs(buf);
304 tt.tt_col += 3;
2d152f42 305 }
c3f42fe5
EW
306 pc(0x80);
307 pc(t + 1);
ab8b3b31
EW
308 s[n - 1] |= 0x80;
309 ttwrite(s, n);
310 s[n - 1] &= ~0x80;
311 tt.tt_col += n;
312 if (tt.tt_col == NCOL)
313 tt.tt_col = 0, tt.tt_row++;
2d152f42
EW
314}
315
ab8b3b31
EW
316/*ARGSUSED*/
317zz_put_token(t, s, n)
318 char *s;
2d152f42 319{
ab8b3b31
EW
320 if (tt.tt_nmodes != tt.tt_modes)
321 zz_setmodes(tt.tt_nmodes);
322 if (zz_debug) {
323 char buf[100];
324 zz_setmodes(WWM_REV);
325 (void) sprintf(buf, "%02x>", t);
326 ttputs(buf);
327 tt.tt_col += 3;
1dbd69e7 328 }
c3f42fe5 329 pc(t + 0x81);
ab8b3b31
EW
330 tt.tt_col += n;
331 if (tt.tt_col == NCOL)
332 tt.tt_col = 0, tt.tt_row++;
1dbd69e7 333}
1dbd69e7
EW
334
335tt_zapple()
336{
d9375810 337 tt.tt_insspace = zz_insspace;
1dbd69e7
EW
338 tt.tt_delchar = zz_delchar;
339 tt.tt_insline = zz_insline;
340 tt.tt_delline = zz_delline;
341 tt.tt_clreol = zz_clreol;
342 tt.tt_clreos = zz_clreos;
343 tt.tt_scroll_down = zz_scroll_down;
344 tt.tt_scroll_up = zz_scroll_up;
345 tt.tt_setscroll = zz_setscroll;
346 tt.tt_availmodes = WWM_REV;
1dbd69e7
EW
347 tt.tt_wrap = 1;
348 tt.tt_retain = 0;
ab8b3b31
EW
349 tt.tt_ncol = NCOL;
350 tt.tt_nrow = NROW;
351 tt.tt_start = zz_start;
1dbd69e7
EW
352 tt.tt_end = zz_end;
353 tt.tt_write = zz_write;
354 tt.tt_putc = zz_putc;
355 tt.tt_move = zz_move;
356 tt.tt_clear = zz_clear;
1dbd69e7
EW
357 tt.tt_setmodes = zz_setmodes;
358 tt.tt_frame = gen_frame;
c3f42fe5 359 tt.tt_ntoken = 127;
ab8b3b31
EW
360 tt.tt_set_token = zz_set_token;
361 tt.tt_put_token = zz_put_token;
362 tt.tt_token_min = 1;
363 tt.tt_token_max = TOKEN_MAX;
364 tt.tt_set_token_cost = 2;
365 tt.tt_put_token_cost = 1;
1dbd69e7
EW
366 return 0;
367}