104cf7d27Sedward /*
2*e85b5252Sbostic * Copyright (c) 1983, 1993
3*e85b5252Sbostic * The Regents of the University of California. All rights reserved.
4bac0d407Sbostic *
58e751acfSbostic * This code is derived from software contributed to Berkeley by
68e751acfSbostic * Edward Wang at The University of California, Berkeley.
78e751acfSbostic *
8122a1d9eSbostic * %sccs.include.redist.c%
904cf7d27Sedward */
1004cf7d27Sedward
11bac0d407Sbostic #ifndef lint
12*e85b5252Sbostic static char sccsid[] = "@(#)cmd7.c 8.1 (Berkeley) 06/06/93";
13bac0d407Sbostic #endif /* not lint */
14bac0d407Sbostic
159ba19766Sedward #include "defs.h"
16ece0d8dcSedward #include "string.h"
179ba19766Sedward
189ba19766Sedward /*
199ba19766Sedward * Window size.
209ba19766Sedward */
219ba19766Sedward
c_size(w)229ba19766Sedward c_size(w)
239ba19766Sedward register struct ww *w;
249ba19766Sedward {
259ba19766Sedward int col, row;
269ba19766Sedward
279ba19766Sedward if (!terse)
289ba19766Sedward wwputs("New window size (lower right corner): ", cmdwin);
299ba19766Sedward col = MIN(w->ww_w.r, wwncol) - 1;
309ba19766Sedward row = MIN(w->ww_w.b, wwnrow) - 1;
319ba19766Sedward wwadd(boxwin, framewin->ww_back);
329ba19766Sedward for (;;) {
339ba19766Sedward wwbox(boxwin, w->ww_w.t - 1, w->ww_w.l - 1,
349ba19766Sedward row - w->ww_w.t + 3, col - w->ww_w.l + 3);
359ba19766Sedward wwsetcursor(row, col);
369ba19766Sedward while (wwpeekc() < 0)
379ba19766Sedward wwiomux();
389ba19766Sedward switch (getpos(&row, &col, w->ww_w.t, w->ww_w.l,
399ba19766Sedward wwnrow - 1, wwncol - 1)) {
409ba19766Sedward case 3:
419ba19766Sedward wwunbox(boxwin);
429ba19766Sedward wwdelete(boxwin);
439ba19766Sedward return;
449ba19766Sedward case 2:
459ba19766Sedward wwunbox(boxwin);
469ba19766Sedward break;
479ba19766Sedward case 1:
489ba19766Sedward wwunbox(boxwin);
499ba19766Sedward case 0:
509ba19766Sedward continue;
519ba19766Sedward }
529ba19766Sedward break;
539ba19766Sedward }
549ba19766Sedward wwdelete(boxwin);
559ba19766Sedward if (!terse)
569ba19766Sedward wwputc('\n', cmdwin);
579ba19766Sedward wwcurtowin(cmdwin);
589ba19766Sedward sizewin(w, row - w->ww_w.t + 1, col - w->ww_w.l + 1);
599ba19766Sedward }
60ece0d8dcSedward
61ece0d8dcSedward /*
62ece0d8dcSedward * Yank and put
63ece0d8dcSedward */
64ece0d8dcSedward
65ece0d8dcSedward struct yb {
66ece0d8dcSedward char *line;
67ece0d8dcSedward int length;
68ece0d8dcSedward struct yb *link;
69ece0d8dcSedward };
70ece0d8dcSedward struct yb *yb_head, *yb_tail;
71ece0d8dcSedward
c_yank()72ece0d8dcSedward c_yank()
73ece0d8dcSedward {
74ece0d8dcSedward struct ww *w = selwin;
75ece0d8dcSedward int col1, row1;
76ece0d8dcSedward int col2, row2;
77ece0d8dcSedward int r, c;
78ece0d8dcSedward
79ece0d8dcSedward if (!terse)
80ece0d8dcSedward wwputs("Yank starting position: ", cmdwin);
81ece0d8dcSedward wwcursor(w, 0);
82ece0d8dcSedward row1 = w->ww_cur.r;
83ece0d8dcSedward col1 = w->ww_cur.c;
84ece0d8dcSedward for (;;) {
85ece0d8dcSedward wwsetcursor(row1, col1);
86ece0d8dcSedward while (wwpeekc() < 0)
87ece0d8dcSedward wwiomux();
88ece0d8dcSedward switch (getpos(&row1, &col1, w->ww_i.t, w->ww_i.l,
89ece0d8dcSedward w->ww_i.b - 1, w->ww_i.r - 1)) {
90ece0d8dcSedward case 3:
91ece0d8dcSedward goto out;
92ece0d8dcSedward case 2:
93ece0d8dcSedward break;
94ece0d8dcSedward case 1:
95ece0d8dcSedward case 0:
96ece0d8dcSedward continue;
97ece0d8dcSedward }
98ece0d8dcSedward break;
99ece0d8dcSedward }
100ece0d8dcSedward if (!terse)
101ece0d8dcSedward wwputs("\nYank ending position: ", cmdwin);
102ece0d8dcSedward row2 = row1;
103ece0d8dcSedward col2 = col1;
104ece0d8dcSedward for (;;) {
105ece0d8dcSedward wwsetcursor(row2, col2);
106ece0d8dcSedward while (wwpeekc() < 0)
107ece0d8dcSedward wwiomux();
108ece0d8dcSedward r = row2;
109ece0d8dcSedward c = col2;
110ece0d8dcSedward switch (getpos(&row2, &col2, w->ww_i.t, w->ww_i.l,
111ece0d8dcSedward w->ww_i.b - 1, w->ww_i.r - 1)) {
112ece0d8dcSedward case 3:
113ece0d8dcSedward yank_highlight(row1, col1, r, c);
114ece0d8dcSedward goto out;
115ece0d8dcSedward case 2:
116ece0d8dcSedward break;
117ece0d8dcSedward case 1:
118ece0d8dcSedward yank_highlight(row1, col1, r, c);
119ece0d8dcSedward yank_highlight(row1, col1, row2, col2);
120ece0d8dcSedward case 0:
121ece0d8dcSedward continue;
122ece0d8dcSedward }
123ece0d8dcSedward break;
124ece0d8dcSedward }
125ece0d8dcSedward if (row2 < row1 || row2 == row1 && col2 < col1) {
126ece0d8dcSedward r = row1;
127ece0d8dcSedward c = col1;
128ece0d8dcSedward row1 = row2;
129ece0d8dcSedward col1 = col2;
130ece0d8dcSedward row2 = r;
131ece0d8dcSedward col2 = c;
132ece0d8dcSedward }
133ece0d8dcSedward unyank();
134ece0d8dcSedward c = col1;
135ece0d8dcSedward for (r = row1; r < row2; r++) {
136ece0d8dcSedward yank_line(r, c, w->ww_b.r);
137ece0d8dcSedward c = w->ww_b.l;
138ece0d8dcSedward }
139ece0d8dcSedward yank_line(r, c, col2);
140ece0d8dcSedward yank_highlight(row1, col1, row2, col2);
141ece0d8dcSedward if (!terse)
142ece0d8dcSedward wwputc('\n', cmdwin);
143ece0d8dcSedward out:
144ece0d8dcSedward wwcursor(w, 1);
145ece0d8dcSedward }
146ece0d8dcSedward
yank_highlight(row1,col1,row2,col2)147ece0d8dcSedward yank_highlight(row1, col1, row2, col2)
148ece0d8dcSedward {
149ece0d8dcSedward struct ww *w = selwin;
150ece0d8dcSedward int r, c;
151ece0d8dcSedward
152ece0d8dcSedward if ((wwavailmodes & WWM_REV) == 0)
153ece0d8dcSedward return;
154ece0d8dcSedward if (row2 < row1 || row2 == row1 && col2 < col1) {
155ece0d8dcSedward r = row1;
156ece0d8dcSedward c = col1;
157ece0d8dcSedward row1 = row2;
158ece0d8dcSedward col1 = col2;
159ece0d8dcSedward row2 = r;
160ece0d8dcSedward col2 = c;
161ece0d8dcSedward }
162ece0d8dcSedward c = col1;
163ece0d8dcSedward for (r = row1; r < row2; r++) {
164ece0d8dcSedward yank_highlight_line(r, c, w->ww_b.r);
165ece0d8dcSedward c = w->ww_b.l;
166ece0d8dcSedward }
167ece0d8dcSedward yank_highlight_line(r, c, col2);
168ece0d8dcSedward }
169ece0d8dcSedward
yank_highlight_line(r,c,cend)170ece0d8dcSedward yank_highlight_line(r, c, cend)
171ece0d8dcSedward {
172ece0d8dcSedward struct ww *w = selwin;
173ece0d8dcSedward char *win;
174ece0d8dcSedward
175ece0d8dcSedward if (r < w->ww_i.t || r >= w->ww_i.b)
176ece0d8dcSedward return;
177ece0d8dcSedward if (c < w->ww_i.l)
178ece0d8dcSedward c = w->ww_i.l;
179ece0d8dcSedward if (cend >= w->ww_i.r)
180ece0d8dcSedward cend = w->ww_i.r;
181ece0d8dcSedward for (win = w->ww_win[r] + c; c < cend; c++, win++) {
182ece0d8dcSedward *win ^= WWM_REV;
183ece0d8dcSedward if (wwsmap[r][c] == w->ww_index) {
184ece0d8dcSedward if (*win == 0)
185ece0d8dcSedward w->ww_nvis[r]++;
186ece0d8dcSedward else if (*win == WWM_REV)
187ece0d8dcSedward w->ww_nvis[r]--;
188ece0d8dcSedward wwns[r][c].c_m ^= WWM_REV;
189ece0d8dcSedward wwtouched[r] |= WWU_TOUCHED;
190ece0d8dcSedward }
191ece0d8dcSedward }
192ece0d8dcSedward }
193ece0d8dcSedward
unyank()194ece0d8dcSedward unyank()
195ece0d8dcSedward {
196ece0d8dcSedward struct yb *yp, *yq;
197ece0d8dcSedward
198ece0d8dcSedward for (yp = yb_head; yp; yp = yq) {
199ece0d8dcSedward yq = yp->link;
200ece0d8dcSedward str_free(yp->line);
201ece0d8dcSedward free((char *) yp);
202ece0d8dcSedward }
203ece0d8dcSedward yb_head = yb_tail = 0;
204ece0d8dcSedward }
205ece0d8dcSedward
yank_line(r,c,cend)206ece0d8dcSedward yank_line(r, c, cend)
207ece0d8dcSedward {
208ece0d8dcSedward struct yb *yp;
209ece0d8dcSedward int nl = 0;
210ece0d8dcSedward int n;
211ece0d8dcSedward union ww_char *bp;
212ece0d8dcSedward char *cp;
213ece0d8dcSedward
214ece0d8dcSedward if (c == cend)
215ece0d8dcSedward return;
216ece0d8dcSedward if ((yp = (struct yb *) malloc(sizeof *yp)) == 0)
217ece0d8dcSedward return;
218ece0d8dcSedward yp->link = 0;
219ece0d8dcSedward nl = cend == selwin->ww_b.r;
220ece0d8dcSedward bp = selwin->ww_buf[r];
221ece0d8dcSedward for (cend--; cend >= c; cend--)
222ece0d8dcSedward if (bp[cend].c_c != ' ')
223ece0d8dcSedward break;
224ece0d8dcSedward yp->length = n = cend - c + 1;
225ece0d8dcSedward if (nl)
226ece0d8dcSedward yp->length++;
227ece0d8dcSedward yp->line = str_alloc(yp->length + 1);
228ece0d8dcSedward for (bp += c, cp = yp->line; --n >= 0;)
229ece0d8dcSedward *cp++ = bp++->c_c;
230ece0d8dcSedward if (nl)
231ece0d8dcSedward *cp++ = '\n';
232ece0d8dcSedward *cp = 0;
233ece0d8dcSedward if (yb_head)
234ece0d8dcSedward yb_tail = yb_tail->link = yp;
235ece0d8dcSedward else
236ece0d8dcSedward yb_head = yb_tail = yp;
237ece0d8dcSedward }
238ece0d8dcSedward
c_put()239ece0d8dcSedward c_put()
240ece0d8dcSedward {
241ece0d8dcSedward struct yb *yp;
242ece0d8dcSedward
243ece0d8dcSedward for (yp = yb_head; yp; yp = yp->link)
244ece0d8dcSedward (void) write(selwin->ww_pty, yp->line, yp->length);
245ece0d8dcSedward }
246