xref: /original-bsd/usr.bin/window/wwinschar.c (revision 65ba69af)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
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
19 static char sccsid[] = "@(#)wwinschar.c	3.16 (Berkeley) 06/29/88";
20 #endif /* not lint */
21 
22 #include "ww.h"
23 #include "tt.h"
24 
25 wwinschar(w, row, col, c)
26 register struct ww *w;
27 short c;
28 {
29 	register i;
30 	int nvis;
31 
32 	/*
33 	 * First, shift the line.
34 	 */
35 	{
36 		register union ww_char *p, *q;
37 
38 		p = &w->ww_buf[row][w->ww_b.r];
39 		q = p - 1;
40 		for (i = w->ww_b.r - col; --i > 0;)
41 			*--p = *--q;
42 		q->c_w = c;
43 	}
44 
45 	/*
46 	 * If can't see it, just return.
47 	 */
48 	if (row < w->ww_i.t || row >= w->ww_i.b
49 	    || w->ww_i.r <= 0 || w->ww_i.r <= col)
50 		return;
51 
52 	if (col < w->ww_i.l)
53 		col = w->ww_i.l;
54 
55 	/*
56 	 * Now find out how much is actually changed, and fix wwns.
57 	 */
58 	{
59 		register union ww_char *buf;
60 		register char *win;
61 		register union ww_char *ns;
62 		register char *smap;
63 		char touched;
64 
65 		nvis = 0;
66 		smap = &wwsmap[row][col];
67 		for (i = col; i < w->ww_i.r && *smap++ != w->ww_index; i++)
68 			;
69 		if (i >= w->ww_i.r)
70 			return;
71 		col = i;
72 		buf = w->ww_buf[row];
73 		win = w->ww_win[row];
74 		ns = wwns[row];
75 		smap = &wwsmap[row][i];
76 		touched = wwtouched[row];
77 		for (; i < w->ww_i.r; i++) {
78 			if (*smap++ != w->ww_index)
79 				continue;
80 			touched |= WWU_TOUCHED;
81 			if (win[i])
82 				ns[i].c_w =
83 					buf[i].c_w ^ win[i] << WWC_MSHIFT;
84 			else {
85 				nvis++;
86 				ns[i] = buf[i];
87 			}
88 		}
89 		wwtouched[row] = touched;
90 	}
91 
92 	/*
93 	 * Can/Should we use delete character?
94 	 */
95 	if (tt.tt_hasinsert && nvis > (wwncol - col) / 2) {
96 		register union ww_char *p, *q;
97 
98 		tt.tt_ninsert = 1;
99 		tt.tt_nmodes = c >> WWC_MSHIFT & tt.tt_availmodes;
100 		(*tt.tt_move)(row, col);
101 		(*tt.tt_putc)(c & WWC_CMASK);
102 		tt.tt_ninsert = 0;
103 
104 		p = &wwos[row][wwncol];
105 		q = p - 1;
106 		for (i = wwncol - col; --i > 0;)
107 			*--p = *--q;
108 		q->c_w = c;
109 	}
110 }
111