]> git.saurik.com Git - apple/shell_cmds.git/blame - window/cmd7.c
shell_cmds-34.tar.gz
[apple/shell_cmds.git] / window / cmd7.c
CommitLineData
44bd5ea7
A
1/* $NetBSD: cmd7.c,v 1.4 1997/11/21 08:35:54 lukem 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#include <sys/cdefs.h>
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)cmd7.c 8.1 (Berkeley) 6/6/93";
43#else
44__RCSID("$NetBSD: cmd7.c,v 1.4 1997/11/21 08:35:54 lukem Exp $");
45#endif
46#endif /* not lint */
47
48#include <stdlib.h>
49#include <unistd.h>
50#include "defs.h"
51#include "window_string.h"
52
53void unyank __P((void));
54void yank_highlight __P((int, int, int, int));
55void yank_highlight_line __P((int, int, int));
56void yank_line __P((int, int, int));
57
58/*
59 * Window size.
60 */
61void
62c_size(w)
63 struct ww *w;
64{
65 int col, row;
66
67 if (!terse)
68 wwputs("New window size (lower right corner): ", cmdwin);
69 col = MIN(w->ww_w.r, wwncol) - 1;
70 row = MIN(w->ww_w.b, wwnrow) - 1;
71 wwadd(boxwin, framewin->ww_back);
72 for (;;) {
73 wwbox(boxwin, w->ww_w.t - 1, w->ww_w.l - 1,
74 row - w->ww_w.t + 3, col - w->ww_w.l + 3);
75 wwsetcursor(row, col);
76 while (wwpeekc() < 0)
77 wwiomux();
78 switch (getpos(&row, &col, w->ww_w.t, w->ww_w.l,
79 wwnrow - 1, wwncol - 1)) {
80 case 3:
81 wwunbox(boxwin);
82 wwdelete(boxwin);
83 return;
84 case 2:
85 wwunbox(boxwin);
86 break;
87 case 1:
88 wwunbox(boxwin);
89 case 0:
90 continue;
91 }
92 break;
93 }
94 wwdelete(boxwin);
95 if (!terse)
96 wwputc('\n', cmdwin);
97 wwcurtowin(cmdwin);
98 sizewin(w, row - w->ww_w.t + 1, col - w->ww_w.l + 1);
99}
100
101/*
102 * Yank and put
103 */
104
105struct yb {
106 char *line;
107 int length;
108 struct yb *link;
109};
110struct yb *yb_head, *yb_tail;
111
112void
113c_yank()
114{
115 struct ww *w = selwin;
116 int col1, row1;
117 int col2, row2;
118 int r, c;
119
120 if (!terse)
121 wwputs("Yank starting position: ", cmdwin);
122 wwcursor(w, 0);
123 row1 = w->ww_cur.r;
124 col1 = w->ww_cur.c;
125 for (;;) {
126 wwsetcursor(row1, col1);
127 while (wwpeekc() < 0)
128 wwiomux();
129 switch (getpos(&row1, &col1, w->ww_i.t, w->ww_i.l,
130 w->ww_i.b - 1, w->ww_i.r - 1)) {
131 case 3:
132 goto out;
133 case 2:
134 break;
135 case 1:
136 case 0:
137 continue;
138 }
139 break;
140 }
141 if (!terse)
142 wwputs("\nYank ending position: ", cmdwin);
143 row2 = row1;
144 col2 = col1;
145 for (;;) {
146 wwsetcursor(row2, col2);
147 while (wwpeekc() < 0)
148 wwiomux();
149 r = row2;
150 c = col2;
151 switch (getpos(&row2, &col2, w->ww_i.t, w->ww_i.l,
152 w->ww_i.b - 1, w->ww_i.r - 1)) {
153 case 3:
154 yank_highlight(row1, col1, r, c);
155 goto out;
156 case 2:
157 break;
158 case 1:
159 yank_highlight(row1, col1, r, c);
160 yank_highlight(row1, col1, row2, col2);
161 case 0:
162 continue;
163 }
164 break;
165 }
166 if (row2 < row1 || (row2 == row1 && col2 < col1)) {
167 r = row1;
168 c = col1;
169 row1 = row2;
170 col1 = col2;
171 row2 = r;
172 col2 = c;
173 }
174 unyank();
175 c = col1;
176 for (r = row1; r < row2; r++) {
177 yank_line(r, c, w->ww_b.r);
178 c = w->ww_b.l;
179 }
180 yank_line(r, c, col2);
181 yank_highlight(row1, col1, row2, col2);
182 if (!terse)
183 wwputc('\n', cmdwin);
184out:
185 wwcursor(w, 1);
186}
187
188void
189yank_highlight(row1, col1, row2, col2)
190 int row1, col1, row2, col2;
191{
192 struct ww *w = selwin;
193 int r, c;
194
195 if ((wwavailmodes & WWM_REV) == 0)
196 return;
197 if (row2 < row1 || (row2 == row1 && col2 < col1)) {
198 r = row1;
199 c = col1;
200 row1 = row2;
201 col1 = col2;
202 row2 = r;
203 col2 = c;
204 }
205 c = col1;
206 for (r = row1; r < row2; r++) {
207 yank_highlight_line(r, c, w->ww_b.r);
208 c = w->ww_b.l;
209 }
210 yank_highlight_line(r, c, col2);
211}
212
213void
214yank_highlight_line(r, c, cend)
215 int r, c, cend;
216{
217 struct ww *w = selwin;
218 char *win;
219
220 if (r < w->ww_i.t || r >= w->ww_i.b)
221 return;
222 if (c < w->ww_i.l)
223 c = w->ww_i.l;
224 if (cend >= w->ww_i.r)
225 cend = w->ww_i.r;
226 for (win = w->ww_win[r] + c; c < cend; c++, win++) {
227 *win ^= WWM_REV;
228 if (wwsmap[r][c] == w->ww_index) {
229 if (*win == 0)
230 w->ww_nvis[r]++;
231 else if (*win == WWM_REV)
232 w->ww_nvis[r]--;
233 wwns[r][c].c_m ^= WWM_REV;
234 wwtouched[r] |= WWU_TOUCHED;
235 }
236 }
237}
238
239void
240unyank()
241{
242 struct yb *yp, *yq;
243
244 for (yp = yb_head; yp; yp = yq) {
245 yq = yp->link;
246 str_free(yp->line);
247 free((char *) yp);
248 }
249 yb_head = yb_tail = 0;
250}
251
252void
253yank_line(r, c, cend)
254 int r, c, cend;
255{
256 struct yb *yp;
257 int nl = 0;
258 int n;
259 union ww_char *bp;
260 char *cp;
261
262 if (c == cend)
263 return;
264 if ((yp = (struct yb *) malloc(sizeof *yp)) == 0)
265 return;
266 yp->link = 0;
267 nl = cend == selwin->ww_b.r;
268 bp = selwin->ww_buf[r];
269 for (cend--; cend >= c; cend--)
270 if (bp[cend].c_c != ' ')
271 break;
272 yp->length = n = cend - c + 1;
273 if (nl)
274 yp->length++;
275 yp->line = str_alloc(yp->length + 1);
276 for (bp += c, cp = yp->line; --n >= 0;)
277 *cp++ = bp++->c_c;
278 if (nl)
279 *cp++ = '\n';
280 *cp = 0;
281 if (yb_head)
282 yb_tail = yb_tail->link = yp;
283 else
284 yb_head = yb_tail = yp;
285}
286
287void
288c_put()
289{
290 struct yb *yp;
291
292 for (yp = yb_head; yp; yp = yp->link)
293 (void) write(selwin->ww_pty, yp->line, yp->length);
294}