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