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