Fix for a crash which happened when a document couldn't be opened.
[AROS-Contrib.git] / fish / microemacs / window.c
blob0a43f6911fbfdb8f75e7a7188631d14baeb073f1
2 /*
3 * Window management. Some of the functions are internal, and some are
4 * attached to keys that the user actually types.
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "ed.h"
13 * Reposition dot in the current window to line "n". If the argument is
14 * positive, it is that line. If it is negative it is that line from the
15 * bottom. If it is 0 the window is centered (this is what the standard
16 * redisplay code does). With no argument it defaults to 1. Bound to M-!.
17 * Because of the default, it works like in Gosling.
19 int reposition(f, n)
20 int f;
21 int n;
23 curwp->w_force = n;
24 curwp->w_flag |= WFFORCE;
25 return (TRUE);
29 * Refresh the screen. With no argument, it just does the refresh. With an
30 * argument it recenters "." in the current window. Bound to "C-L".
32 int refresh(f, n)
33 int f;
34 int n;
36 if (f == FALSE)
37 sgarbf = TRUE;
38 else
40 curwp->w_force = 0; /* Center dot. */
41 curwp->w_flag |= WFFORCE;
44 return (TRUE);
48 * The command make the next window (next => down the screen) the current
49 * window. There are no real errors, although the command does nothing if
50 * there is only 1 window on the screen. Bound to "C-X C-N".
52 int nextwind(f, n)
53 int f;
54 int n;
56 register WINDOW *wp;
58 if ((wp = curwp->w_wndp) == NULL)
59 wp = wheadp;
61 curwp = wp;
62 curbp = wp->w_bufp;
63 return (TRUE);
67 * This command makes the previous window (previous => up the screen) the
68 * current window. There arn't any errors, although the command does not do a
69 * lot if there is 1 window.
71 int prevwind(f, n)
72 int f;
73 int n;
75 register WINDOW *wp1;
76 register WINDOW *wp2;
78 wp1 = wheadp;
79 wp2 = curwp;
81 if (wp1 == wp2)
82 wp2 = NULL;
84 while (wp1->w_wndp != wp2)
85 wp1 = wp1->w_wndp;
87 curwp = wp1;
88 curbp = wp1->w_bufp;
89 return (TRUE);
93 * This command moves the current window down by "arg" lines. Recompute the
94 * top line in the window. The move up and move down code is almost completely
95 * the same; most of the work has to do with reframing the window, and picking
96 * a new dot. We share the code by having "move down" just be an interface to
97 * "move up". Magic. Bound to "C-X C-N".
99 int mvdnwind(f, n)
100 int f;
101 int n;
103 return (mvupwind(f, -n));
107 * Move the current window up by "arg" lines. Recompute the new top line of
108 * the window. Look to see if "." is still on the screen. If it is, you win.
109 * If it isn't, then move "." to center it in the new framing of the window
110 * (this command does not really move "."; it moves the frame). Bound to
111 * "C-X C-P".
113 int mvupwind(f, n)
114 int f;
115 int n;
117 register LINE *lp;
118 register int i;
120 lp = curwp->w_linep;
122 if (n < 0)
124 while (n++ && lp!=curbp->b_linep)
125 lp = lforw(lp);
127 else
129 while (n-- && lback(lp)!=curbp->b_linep)
130 lp = lback(lp);
133 curwp->w_linep = lp;
134 curwp->w_flag |= WFHARD; /* Mode line is OK. */
136 for (i = 0; i < curwp->w_ntrows; ++i)
138 if (lp == curwp->w_dotp)
139 return (TRUE);
140 if (lp == curbp->b_linep)
141 break;
142 lp = lforw(lp);
145 lp = curwp->w_linep;
146 i = curwp->w_ntrows/2;
148 while (i-- && lp != curbp->b_linep)
149 lp = lforw(lp);
151 curwp->w_dotp = lp;
152 curwp->w_doto = 0;
153 return (TRUE);
157 * This command makes the current window the only window on the screen. Bound
158 * to "C-X 1". Try to set the framing so that "." does not have to move on the
159 * display. Some care has to be taken to keep the values of dot and mark in
160 * the buffer structures right if the distruction of a window makes a buffer
161 * become undisplayed.
163 int onlywind(f, n)
164 int f;
165 int n;
167 register WINDOW *wp;
168 register LINE *lp;
169 register int i;
171 while (wheadp != curwp) {
172 wp = wheadp;
173 wheadp = wp->w_wndp;
174 if (--wp->w_bufp->b_nwnd == 0) {
175 wp->w_bufp->b_dotp = wp->w_dotp;
176 wp->w_bufp->b_doto = wp->w_doto;
177 wp->w_bufp->b_markp = wp->w_markp;
178 wp->w_bufp->b_marko = wp->w_marko;
180 free((char *) wp);
182 while (curwp->w_wndp != NULL) {
183 wp = curwp->w_wndp;
184 curwp->w_wndp = wp->w_wndp;
185 if (--wp->w_bufp->b_nwnd == 0) {
186 wp->w_bufp->b_dotp = wp->w_dotp;
187 wp->w_bufp->b_doto = wp->w_doto;
188 wp->w_bufp->b_markp = wp->w_markp;
189 wp->w_bufp->b_marko = wp->w_marko;
191 free((char *) wp);
193 lp = curwp->w_linep;
194 i = curwp->w_toprow;
195 while (i!=0 && lback(lp)!=curbp->b_linep) {
196 --i;
197 lp = lback(lp);
199 curwp->w_toprow = 0;
200 curwp->w_ntrows = term.t_nrow-1;
201 curwp->w_linep = lp;
202 curwp->w_flag |= WFMODE|WFHARD;
203 return (TRUE);
207 * Split the current window. A window smaller than 3 lines cannot be split.
208 * The only other error that is possible is a "malloc" failure allocating the
209 * structure for the new window. Bound to "C-X 2".
211 int splitwind(f, n)
212 int f;
213 int n;
215 register WINDOW *wp;
216 register LINE *lp;
217 register int ntru;
218 register int ntrl;
219 register int ntrd;
220 register WINDOW *wp1;
221 register WINDOW *wp2;
223 if (curwp->w_ntrows < 3) {
224 mlwrite("Cannot split a %d line window", curwp->w_ntrows);
225 return (FALSE);
227 if ((wp = (WINDOW *) malloc(sizeof(WINDOW))) == NULL) {
228 mlwrite("Cannot allocate WINDOW block");
229 return (FALSE);
231 ++curbp->b_nwnd; /* Displayed twice. */
232 wp->w_bufp = curbp;
233 wp->w_dotp = curwp->w_dotp;
234 wp->w_doto = curwp->w_doto;
235 wp->w_markp = curwp->w_markp;
236 wp->w_marko = curwp->w_marko;
237 wp->w_flag = 0;
238 wp->w_force = 0;
239 ntru = (curwp->w_ntrows-1) / 2; /* Upper size */
240 ntrl = (curwp->w_ntrows-1) - ntru; /* Lower size */
241 lp = curwp->w_linep;
242 ntrd = 0;
243 while (lp != curwp->w_dotp) {
244 ++ntrd;
245 lp = lforw(lp);
247 lp = curwp->w_linep;
248 if (ntrd <= ntru) { /* Old is upper window. */
249 if (ntrd == ntru) /* Hit mode line. */
250 lp = lforw(lp);
251 curwp->w_ntrows = ntru;
252 wp->w_wndp = curwp->w_wndp;
253 curwp->w_wndp = wp;
254 wp->w_toprow = curwp->w_toprow+ntru+1;
255 wp->w_ntrows = ntrl;
256 } else { /* Old is lower window */
257 wp1 = NULL;
258 wp2 = wheadp;
259 while (wp2 != curwp) {
260 wp1 = wp2;
261 wp2 = wp2->w_wndp;
263 if (wp1 == NULL)
264 wheadp = wp;
265 else
266 wp1->w_wndp = wp;
267 wp->w_wndp = curwp;
268 wp->w_toprow = curwp->w_toprow;
269 wp->w_ntrows = ntru;
270 ++ntru; /* Mode line. */
271 curwp->w_toprow += ntru;
272 curwp->w_ntrows = ntrl;
273 while (ntru--)
274 lp = lforw(lp);
276 curwp->w_linep = lp; /* Adjust the top lines */
277 wp->w_linep = lp; /* if necessary. */
278 curwp->w_flag |= WFMODE|WFHARD;
279 wp->w_flag |= WFMODE|WFHARD;
280 return (TRUE);
284 * Enlarge the current window. Find the window that loses space. Make sure it
285 * is big enough. If so, hack the window descriptions, and ask redisplay to do
286 * all the hard work. You don't just set "force reframe" because dot would
287 * move. Bound to "C-X Z".
289 int enlargewind(f, n)
290 int f;
291 int n;
293 register WINDOW *adjwp;
294 register LINE *lp;
295 register int i;
297 if (n < 0)
298 return (shrinkwind(f, -n));
299 if (wheadp->w_wndp == NULL) {
300 mlwrite("Only one window");
301 return (FALSE);
303 if ((adjwp=curwp->w_wndp) == NULL) {
304 adjwp = wheadp;
305 while (adjwp->w_wndp != curwp)
306 adjwp = adjwp->w_wndp;
308 if (adjwp->w_ntrows <= n) {
309 mlwrite("Impossible change");
310 return (FALSE);
312 if (curwp->w_wndp == adjwp) { /* Shrink below. */
313 lp = adjwp->w_linep;
314 for (i=0; i<n && lp!=adjwp->w_bufp->b_linep; ++i)
315 lp = lforw(lp);
316 adjwp->w_linep = lp;
317 adjwp->w_toprow += n;
318 } else { /* Shrink above. */
319 lp = curwp->w_linep;
320 for (i=0; i<n && lback(lp)!=curbp->b_linep; ++i)
321 lp = lback(lp);
322 curwp->w_linep = lp;
323 curwp->w_toprow -= n;
325 curwp->w_ntrows += n;
326 adjwp->w_ntrows -= n;
327 curwp->w_flag |= WFMODE|WFHARD;
328 adjwp->w_flag |= WFMODE|WFHARD;
329 return (TRUE);
333 * Shrink the current window. Find the window that gains space. Hack at the
334 * window descriptions. Ask the redisplay to do all the hard work. Bound to
335 * "C-X C-Z".
337 int shrinkwind(f, n)
338 int f;
339 int n;
341 register WINDOW *adjwp;
342 register LINE *lp;
343 register int i;
345 if (n < 0)
346 return (enlargewind(f, -n));
347 if (wheadp->w_wndp == NULL) {
348 mlwrite("Only one window");
349 return (FALSE);
351 if ((adjwp=curwp->w_wndp) == NULL) {
352 adjwp = wheadp;
353 while (adjwp->w_wndp != curwp)
354 adjwp = adjwp->w_wndp;
356 if (curwp->w_ntrows <= n) {
357 mlwrite("Impossible change");
358 return (FALSE);
360 if (curwp->w_wndp == adjwp) { /* Grow below. */
361 lp = adjwp->w_linep;
362 for (i=0; i<n && lback(lp)!=adjwp->w_bufp->b_linep; ++i)
363 lp = lback(lp);
364 adjwp->w_linep = lp;
365 adjwp->w_toprow -= n;
366 } else { /* Grow above. */
367 lp = curwp->w_linep;
368 for (i=0; i<n && lp!=curbp->b_linep; ++i)
369 lp = lforw(lp);
370 curwp->w_linep = lp;
371 curwp->w_toprow += n;
373 curwp->w_ntrows -= n;
374 adjwp->w_ntrows += n;
375 curwp->w_flag |= WFMODE|WFHARD;
376 adjwp->w_flag |= WFMODE|WFHARD;
377 return (TRUE);
381 * Pick a window for a pop-up. Split the screen if there is only one window.
382 * Pick the uppermost window that isn't the current window. An LRU algorithm
383 * might be better. Return a pointer, or NULL on error.
385 WINDOW *
386 wpopup()
388 register WINDOW *wp;
390 if (wheadp->w_wndp == NULL /* Only 1 window */
391 && splitwind(FALSE, 0) == FALSE) /* and it won't split */
392 return (NULL);
393 wp = wheadp; /* Find window to use */
394 while (wp!=NULL && wp==curwp)
395 wp = wp->w_wndp;
396 return (wp);