Annotation of src/usr.bin/mg/window.c, Revision 1.15
1.15 ! vincent 1: /* $OpenBSD: window.c,v 1.14 2003/11/29 17:28:39 vincent Exp $ */
1.5 niklas 2:
1.1 deraadt 3: /*
4: * Window handling.
5: */
1.4 millert 6:
7: #include "def.h"
1.1 deraadt 8:
1.12 vincent 9: MGWIN *
10: new_window(BUFFER *bp)
11: {
12: MGWIN *wp;
13:
1.13 vincent 14: wp = calloc(1, sizeof(MGWIN));
1.12 vincent 15: if (wp == NULL)
16: return (NULL);
17:
18: wp->w_bufp = bp;
19: wp->w_dotp = NULL;
20: wp->w_doto = 0;
21: wp->w_markp = NULL;
22: wp->w_marko = 0;
23: wp->w_flag = 0;
24: wp->w_force = 0;
1.15 ! vincent 25: wp->w_wrapline = NULL;
1.13 vincent 26: if (bp)
27: bp->b_nwnd++;
1.12 vincent 28: LIST_INIT(&wp->w_undo);
29: wp->w_undoptr = NULL;
1.14 vincent 30: wp->w_undopos = 0;
1.12 vincent 31:
32: return (wp);
33: }
34:
35: void
36: free_window(MGWIN *wp)
37: {
38: struct undo_rec *rec, *next;
39:
40: rec = LIST_FIRST(&wp->w_undo);
41: while (rec != NULL) {
42: next = LIST_NEXT(rec, next);
43: free_undo_record(rec);
44: rec = next;
45: }
46: free(wp);
47: }
48:
1.1 deraadt 49: /*
1.7 mickey 50: * Reposition dot in the current window to line "n". If the argument is
51: * positive, it is that line. If it is negative it is that line from the
52: * bottom. If it is 0 the window is centered (this is what the standard
53: * redisplay code does). If GOSREC is undefined, default is 0, so it acts
54: * like GNU. If GOSREC is defined, with no argument it defaults to 1 and
1.4 millert 55: * works like in Gosling.
1.1 deraadt 56: */
1.3 millert 57: /* ARGSUSED */
1.4 millert 58: int
1.11 cloder 59: reposition(int f, int n)
1.1 deraadt 60: {
61: #ifndef GOSREC
1.3 millert 62: curwp->w_force = (f & FFARG) ? (n >= 0 ? n + 1 : n) : 0;
1.4 millert 63: #else /* !GOSREC */
1.1 deraadt 64: curwp->w_force = n;
1.4 millert 65: #endif /* !GOSREC */
1.1 deraadt 66: curwp->w_flag |= WFFORCE;
67: sgarbf = TRUE;
68: return TRUE;
69: }
70:
71: /*
1.7 mickey 72: * Refresh the display. A call is made to the "ttresize" entry in the
73: * terminal handler, which tries to reset "nrow" and "ncol". They will,
1.4 millert 74: * however, never be set outside of the NROW or NCOL range. If the display
1.7 mickey 75: * changed size, arrange that everything is redone, then call "update" to
76: * fix the display. We do this so the new size can be displayed. In the
77: * normal case the call to "update" in "main.c" refreshes the screen, and
78: * all of the windows need not be recomputed. Note that when you get to the
79: * "display unusable" message, the screen will be messed up. If you make the
1.4 millert 80: * window bigger again, and send another command, everything will get fixed!
1.1 deraadt 81: */
1.3 millert 82: /* ARGSUSED */
1.4 millert 83: int
1.11 cloder 84: refresh(int f, int n)
1.1 deraadt 85: {
1.4 millert 86: MGWIN *wp;
87: int oldnrow;
88: int oldncol;
1.1 deraadt 89:
90: oldnrow = nrow;
91: oldncol = ncol;
92: ttresize();
1.3 millert 93: if (nrow != oldnrow || ncol != oldncol) {
1.4 millert 94:
95: /* find last */
96: wp = wheadp;
1.1 deraadt 97: while (wp->w_wndp != NULL)
98: wp = wp->w_wndp;
1.4 millert 99:
100: /* check if too small */
101: if (nrow < wp->w_toprow + 3) {
1.1 deraadt 102: ewprintf("Display unusable");
103: return (FALSE);
104: }
1.3 millert 105: wp->w_ntrows = nrow - wp->w_toprow - 2;
1.1 deraadt 106: sgarbf = TRUE;
107: update();
108: } else
109: sgarbf = TRUE;
110: return TRUE;
111: }
112:
113: /*
1.7 mickey 114: * The command to make the next window (next => down the screen) the current
115: * window. There are no real errors, although the command does nothing if
1.4 millert 116: * there is only 1 window on the screen.
1.1 deraadt 117: */
1.3 millert 118: /* ARGSUSED */
1.4 millert 119: int
1.11 cloder 120: nextwind(int f, int n)
1.1 deraadt 121: {
1.4 millert 122: MGWIN *wp;
1.1 deraadt 123:
1.3 millert 124: if ((wp = curwp->w_wndp) == NULL)
1.1 deraadt 125: wp = wheadp;
126: curwp = wp;
127: curbp = wp->w_bufp;
128: return TRUE;
129: }
130:
131: /* not in Gnu Emacs */
132: /*
1.4 millert 133: * This command makes the previous window (previous => up the screen) the
1.7 mickey 134: * current window. There are no errors, although the command does not do
1.4 millert 135: * a lot if there is only 1 window.
1.1 deraadt 136: */
1.3 millert 137: /* ARGSUSED */
1.4 millert 138: int
1.11 cloder 139: prevwind(int f, int n)
1.1 deraadt 140: {
1.4 millert 141: MGWIN *wp1, *wp2;
1.1 deraadt 142:
143: wp1 = wheadp;
144: wp2 = curwp;
145: if (wp1 == wp2)
146: wp2 = NULL;
147: while (wp1->w_wndp != wp2)
148: wp1 = wp1->w_wndp;
149: curwp = wp1;
150: curbp = wp1->w_bufp;
151: return TRUE;
152: }
153:
154: /*
1.7 mickey 155: * This command makes the current window the only window on the screen. Try
156: * to set the framing so that "." does not have to move on the display. Some
157: * care has to be taken to keep the values of dot and mark in the buffer
158: * structures right if the distruction of a window makes a buffer become
1.4 millert 159: * undisplayed.
1.1 deraadt 160: */
1.3 millert 161: /* ARGSUSED */
1.4 millert 162: int
1.11 cloder 163: onlywind(int f, int n)
1.1 deraadt 164: {
1.4 millert 165: MGWIN *wp;
166: LINE *lp;
167: int i;
1.1 deraadt 168:
169: while (wheadp != curwp) {
170: wp = wheadp;
171: wheadp = wp->w_wndp;
172: if (--wp->w_bufp->b_nwnd == 0) {
1.3 millert 173: wp->w_bufp->b_dotp = wp->w_dotp;
174: wp->w_bufp->b_doto = wp->w_doto;
1.1 deraadt 175: wp->w_bufp->b_markp = wp->w_markp;
176: wp->w_bufp->b_marko = wp->w_marko;
177: }
1.12 vincent 178: free_window(wp);
1.1 deraadt 179: }
180: while (curwp->w_wndp != NULL) {
181: wp = curwp->w_wndp;
182: curwp->w_wndp = wp->w_wndp;
183: if (--wp->w_bufp->b_nwnd == 0) {
1.3 millert 184: wp->w_bufp->b_dotp = wp->w_dotp;
185: wp->w_bufp->b_doto = wp->w_doto;
1.1 deraadt 186: wp->w_bufp->b_markp = wp->w_markp;
187: wp->w_bufp->b_marko = wp->w_marko;
188: }
1.12 vincent 189: free_window(wp);
1.1 deraadt 190: }
191: lp = curwp->w_linep;
1.3 millert 192: i = curwp->w_toprow;
193: while (i != 0 && lback(lp) != curbp->b_linep) {
1.1 deraadt 194: --i;
195: lp = lback(lp);
196: }
197: curwp->w_toprow = 0;
1.4 millert 198:
199: /* 2 = mode, echo */
200: curwp->w_ntrows = nrow - 2;
1.3 millert 201: curwp->w_linep = lp;
202: curwp->w_flag |= WFMODE | WFHARD;
1.1 deraadt 203: return TRUE;
204: }
205:
206: /*
1.4 millert 207: * Split the current window. A window smaller than 3 lines cannot be split.
1.7 mickey 208: * The only other error that is possible is a "malloc" failure allocating the
1.4 millert 209: * structure for the new window.
1.1 deraadt 210: */
1.3 millert 211: /* ARGSUSED */
1.4 millert 212: int
1.11 cloder 213: splitwind(int f, int n)
1.1 deraadt 214: {
1.4 millert 215: MGWIN *wp, *wp1, *wp2;
216: LINE *lp;
217: int ntru, ntrd, ntrl;
1.1 deraadt 218:
219: if (curwp->w_ntrows < 3) {
220: ewprintf("Cannot split a %d line window", curwp->w_ntrows);
221: return (FALSE);
222: }
1.12 vincent 223: wp = new_window(curbp);
224: if (wp == NULL) {
225: ewprintf("Unable to create a window");
1.1 deraadt 226: return (FALSE);
227: }
1.4 millert 228:
1.12 vincent 229: /* use the current dot and mark */
1.3 millert 230: wp->w_dotp = curwp->w_dotp;
231: wp->w_doto = curwp->w_doto;
1.1 deraadt 232: wp->w_markp = curwp->w_markp;
233: wp->w_marko = curwp->w_marko;
1.12 vincent 234:
235: /* figure out which half of the screen we're in */
236: ntru = (curwp->w_ntrows - 1) / 2; /* Upper size */
237: ntrl = (curwp->w_ntrows - 1) - ntru; /* Lower size */
238:
239: for (lp = curwp->w_linep, ntrd = 0; lp != curwp->w_dotp;
240: lp = lforw(lp))
241: ntrd++;
242:
1.1 deraadt 243: lp = curwp->w_linep;
1.4 millert 244:
245: /* old is upper window */
246: if (ntrd <= ntru) {
247: /* hit mode line */
248: if (ntrd == ntru)
1.1 deraadt 249: lp = lforw(lp);
250: curwp->w_ntrows = ntru;
251: wp->w_wndp = curwp->w_wndp;
252: curwp->w_wndp = wp;
1.3 millert 253: wp->w_toprow = curwp->w_toprow + ntru + 1;
1.1 deraadt 254: wp->w_ntrows = ntrl;
1.4 millert 255: /* old is lower window */
256: } else {
1.1 deraadt 257: wp1 = NULL;
258: wp2 = wheadp;
259: while (wp2 != curwp) {
260: wp1 = wp2;
261: wp2 = wp2->w_wndp;
262: }
263: if (wp1 == NULL)
264: wheadp = wp;
265: else
266: wp1->w_wndp = wp;
1.3 millert 267: wp->w_wndp = curwp;
1.1 deraadt 268: wp->w_toprow = curwp->w_toprow;
269: wp->w_ntrows = ntru;
1.4 millert 270:
271: /* mode line */
272: ++ntru;
1.1 deraadt 273: curwp->w_toprow += ntru;
1.3 millert 274: curwp->w_ntrows = ntrl;
1.1 deraadt 275: while (ntru--)
276: lp = lforw(lp);
277: }
1.4 millert 278:
279: /* adjust the top lines if necessary */
280: curwp->w_linep = lp;
281: wp->w_linep = lp;
282:
1.3 millert 283: curwp->w_flag |= WFMODE | WFHARD;
284: wp->w_flag |= WFMODE | WFHARD;
1.1 deraadt 285: return TRUE;
286: }
287:
288: /*
1.7 mickey 289: * Enlarge the current window. Find the window that loses space. Make sure
290: * it is big enough. If so, hack the window descriptions, and ask redisplay
291: * to do all the hard work. You don't just set "force reframe" because dot
1.4 millert 292: * would move.
1.1 deraadt 293: */
1.3 millert 294: /* ARGSUSED */
1.4 millert 295: int
1.11 cloder 296: enlargewind(int f, int n)
1.1 deraadt 297: {
1.4 millert 298: MGWIN *adjwp;
299: LINE *lp;
300: int i;
1.1 deraadt 301:
302: if (n < 0)
303: return shrinkwind(f, -n);
304: if (wheadp->w_wndp == NULL) {
305: ewprintf("Only one window");
306: return FALSE;
307: }
1.3 millert 308: if ((adjwp = curwp->w_wndp) == NULL) {
1.1 deraadt 309: adjwp = wheadp;
310: while (adjwp->w_wndp != curwp)
311: adjwp = adjwp->w_wndp;
312: }
313: if (adjwp->w_ntrows <= n) {
314: ewprintf("Impossible change");
315: return FALSE;
316: }
1.4 millert 317:
318: /* shrink below */
319: if (curwp->w_wndp == adjwp) {
1.1 deraadt 320: lp = adjwp->w_linep;
1.3 millert 321: for (i = 0; i < n && lp != adjwp->w_bufp->b_linep; ++i)
1.1 deraadt 322: lp = lforw(lp);
1.3 millert 323: adjwp->w_linep = lp;
1.1 deraadt 324: adjwp->w_toprow += n;
1.4 millert 325: /* shrink above */
326: } else {
1.1 deraadt 327: lp = curwp->w_linep;
1.3 millert 328: for (i = 0; i < n && lback(lp) != curbp->b_linep; ++i)
1.1 deraadt 329: lp = lback(lp);
1.3 millert 330: curwp->w_linep = lp;
1.1 deraadt 331: curwp->w_toprow -= n;
332: }
333: curwp->w_ntrows += n;
334: adjwp->w_ntrows -= n;
1.3 millert 335: curwp->w_flag |= WFMODE | WFHARD;
336: adjwp->w_flag |= WFMODE | WFHARD;
1.1 deraadt 337: return TRUE;
338: }
339:
340: /*
1.7 mickey 341: * Shrink the current window. Find the window that gains space. Hack at the
1.4 millert 342: * window descriptions. Ask the redisplay to do all the hard work.
1.1 deraadt 343: */
1.4 millert 344: int
1.11 cloder 345: shrinkwind(int f, int n)
1.1 deraadt 346: {
1.4 millert 347: MGWIN *adjwp;
348: LINE *lp;
349: int i;
1.1 deraadt 350:
351: if (n < 0)
352: return enlargewind(f, -n);
353: if (wheadp->w_wndp == NULL) {
354: ewprintf("Only one window");
355: return FALSE;
356: }
357: /*
358: * Bit of flakiness - KRANDOM means it was an internal call, and
359: * to be trusted implicitly about sizes.
360: */
1.3 millert 361: if (!(f & FFRAND) && curwp->w_ntrows <= n) {
1.1 deraadt 362: ewprintf("Impossible change");
363: return (FALSE);
364: }
1.3 millert 365: if ((adjwp = curwp->w_wndp) == NULL) {
1.1 deraadt 366: adjwp = wheadp;
367: while (adjwp->w_wndp != curwp)
368: adjwp = adjwp->w_wndp;
369: }
1.4 millert 370:
371: /* grow below */
372: if (curwp->w_wndp == adjwp) {
1.1 deraadt 373: lp = adjwp->w_linep;
1.3 millert 374: for (i = 0; i < n && lback(lp) != adjwp->w_bufp->b_linep; ++i)
1.1 deraadt 375: lp = lback(lp);
1.3 millert 376: adjwp->w_linep = lp;
1.1 deraadt 377: adjwp->w_toprow -= n;
1.4 millert 378: /* grow above */
379: } else {
1.1 deraadt 380: lp = curwp->w_linep;
1.3 millert 381: for (i = 0; i < n && lp != curbp->b_linep; ++i)
1.1 deraadt 382: lp = lforw(lp);
1.3 millert 383: curwp->w_linep = lp;
1.1 deraadt 384: curwp->w_toprow += n;
385: }
386: curwp->w_ntrows -= n;
387: adjwp->w_ntrows += n;
1.3 millert 388: curwp->w_flag |= WFMODE | WFHARD;
389: adjwp->w_flag |= WFMODE | WFHARD;
1.1 deraadt 390: return (TRUE);
391: }
392:
393: /*
1.7 mickey 394: * Delete current window. Call shrink-window to do the screen updating, then
1.4 millert 395: * throw away the window.
1.1 deraadt 396: */
1.3 millert 397: /* ARGSUSED */
1.4 millert 398: int
1.11 cloder 399: delwind(int f, int n)
1.1 deraadt 400: {
1.4 millert 401: MGWIN *wp, *nwp;
1.1 deraadt 402:
1.3 millert 403: wp = curwp; /* Cheap... */
1.4 millert 404:
1.1 deraadt 405: /* shrinkwind returning false means only one window... */
406: if (shrinkwind(FFRAND, wp->w_ntrows + 1) == FALSE)
407: return FALSE;
408: if (--wp->w_bufp->b_nwnd == 0) {
1.3 millert 409: wp->w_bufp->b_dotp = wp->w_dotp;
410: wp->w_bufp->b_doto = wp->w_doto;
1.1 deraadt 411: wp->w_bufp->b_markp = wp->w_markp;
412: wp->w_bufp->b_marko = wp->w_marko;
413: }
1.4 millert 414:
1.1 deraadt 415: /* since shrinkwind did't crap out, we know we have a second window */
1.3 millert 416: if (wp == wheadp)
417: wheadp = curwp = wp->w_wndp;
418: else if ((curwp = wp->w_wndp) == NULL)
419: curwp = wheadp;
1.1 deraadt 420: curbp = curwp->w_bufp;
421: for (nwp = wheadp; nwp != NULL; nwp = nwp->w_wndp)
422: if (nwp->w_wndp == wp) {
423: nwp->w_wndp = wp->w_wndp;
1.3 millert 424: break;
1.1 deraadt 425: }
1.12 vincent 426: free_window(wp);
1.1 deraadt 427: return TRUE;
428: }
1.4 millert 429:
1.1 deraadt 430: /*
1.7 mickey 431: * Pick a window for a pop-up. Split the screen if there is only one window.
1.4 millert 432: * Pick the uppermost window that isn't the current window. An LRU algorithm
433: * might be better. Return a pointer, or NULL on error.
1.1 deraadt 434: */
1.3 millert 435: MGWIN *
1.11 cloder 436: wpopup(void)
1.3 millert 437: {
1.4 millert 438: MGWIN *wp;
1.1 deraadt 439:
1.9 deraadt 440: if (wheadp->w_wndp == NULL &&
441: splitwind(FFRAND, 0) == FALSE)
1.1 deraadt 442: return NULL;
1.4 millert 443:
444: /* find a window to use */
445: wp = wheadp;
446:
1.3 millert 447: while (wp != NULL && wp == curwp)
1.1 deraadt 448: wp = wp->w_wndp;
449: return wp;
450: }