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