1/*	$NetBSD: wwinschar.c,v 1.6 2002/06/14 01:06:59 wiz Exp $	*/
2
3/*
4 * Copyright (c) 1983, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)wwinschar.c	8.1 (Berkeley) 6/6/93";
39#else
40__RCSID("$NetBSD: wwinschar.c,v 1.6 2002/06/14 01:06:59 wiz Exp $");
41#endif
42#endif /* not lint */
43
44#include "ww.h"
45#include "tt.h"
46#include "xx.h"
47
48void
49wwinschar(struct ww *w, int row, int col, char c, char m)
50{
51	int i;
52	int nvis;
53	short x = c | m << WWC_MSHIFT;
54
55	/*
56	 * First, shift the line.
57	 */
58	{
59		union ww_char *p, *q;
60
61		p = &w->ww_buf[row][w->ww_b.r];
62		q = p - 1;
63		for (i = w->ww_b.r - col; --i > 0;)
64			*--p = *--q;
65		q->c_w = x;
66	}
67
68	/*
69	 * If can't see it, just return.
70	 */
71	if (row < w->ww_i.t || row >= w->ww_i.b
72	    || w->ww_i.r <= 0 || w->ww_i.r <= col)
73		return;
74
75	if (col < w->ww_i.l)
76		col = w->ww_i.l;
77
78	/*
79	 * Now find out how much is actually changed, and fix wwns.
80	 */
81	{
82		union ww_char *buf;
83		char *win;
84		union ww_char *ns;
85		unsigned char *smap;
86		char touched;
87
88		nvis = 0;
89		smap = &wwsmap[row][col];
90		for (i = col; i < w->ww_i.r && *smap++ != w->ww_index; i++)
91			;
92		if (i >= w->ww_i.r)
93			return;
94		col = i;
95		buf = w->ww_buf[row];
96		win = w->ww_win[row];
97		ns = wwns[row];
98		smap = &wwsmap[row][i];
99		touched = wwtouched[row];
100		for (; i < w->ww_i.r; i++) {
101			if (*smap++ != w->ww_index)
102				continue;
103			touched |= WWU_TOUCHED;
104			if (win[i])
105				ns[i].c_w =
106					buf[i].c_w ^ win[i] << WWC_MSHIFT;
107			else {
108				nvis++;
109				ns[i] = buf[i];
110			}
111		}
112		wwtouched[row] = touched;
113	}
114
115	/*
116	 * Can/Should we use delete character?
117	 */
118	if ((tt.tt_inschar || tt.tt_insspace) && nvis > (wwncol - col) / 2) {
119		union ww_char *p, *q;
120
121		if (tt.tt_inschar)
122			xxinschar(row, col, c, m);
123		else {
124			xxinsspace(row, col);
125			x = ' ';
126		}
127		p = &wwos[row][wwncol];
128		q = p - 1;
129		for (i = wwncol - col; --i > 0;)
130			*--p = *--q;
131		q->c_w = x;
132	}
133}
134