-
Notifications
You must be signed in to change notification settings - Fork 1
/
window.c
631 lines (535 loc) · 12.8 KB
/
window.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
* Window handling.
*/
#include "def.h"
INT prevwind(INT f, INT n);
INT shrinkwind(INT f, INT n);
/*
* Mg3a: Whether to refresh the screen when recentering.
*/
INT recenter_redisplay = 0;
/*
* Recenter like in GNU Emacs. If GOSREC is defined, with no argument
* it defaults to 1 and works like in Gosling.
*/
INT
recenter(INT f, INT n)
{
#ifndef GOSREC
if (f & FFARG) {
if (f & FFNUM) {
curwp->w_force = (n >= 0) ? n + 1 : n;
} else {
curwp->w_force = 0;
}
} else {
curwp->w_force = 0;
if (recenter_redisplay) sgarbf = TRUE;
}
#else
curwp->w_force = n;
sgarbf = TRUE;
#endif
curwp->w_flag |= WFFORCE;
return TRUE;
}
/*
* Mg3a: recenter-top-bottom like in GNU Emacs.
*/
INT
recentertopbottom(INT f, INT n)
{
if (f & FFARG) {
if (f & FFNUM) {
curwp->w_force = (n >= 0) ? n + 1 : n;
thisflag |= CFCL3; /* Try to avoid clearing screen */
} else {
curwp->w_force = 0;
thisflag |= CFCL1; /* Try to avoid clearing screen */
}
} else if (lastflag & CFCL1) {
curwp->w_force = 1;
thisflag |= CFCL2;
} else if (lastflag & CFCL2) {
curwp->w_force = -1;
thisflag |= CFCL3;
} else {
curwp->w_force = 0;
if (recenter_redisplay && !(lastflag & CFCL3)) sgarbf = TRUE;
thisflag |= CFCL1;
}
curwp->w_flag |= WFFORCE;
return TRUE;
}
/*
* Mg3a: move-to-window-line logic.
*/
static INT
internal_movetoline(INT line)
{
LINE *lp, *blp = curbp->b_linep;
reframe_window(curwp); // We depend on the current framing. Makes it work in a script.
lp = curwp->w_linep;
if (line < 0) line = curwp->w_ntrows + line;
if (line < 0) {
for (; line < 0 && lback(lp) != blp; line++) lp = lback(lp);
} else {
for (; line > 0 && lforw(lp) != blp; line--) lp = lforw(lp);
}
adjustpos(lp, 0);
return TRUE;
}
/*
* Mg3a: "move-to-window-line" a la Emacs.
*/
INT
movetoline(INT f, INT n)
{
if (!(f & FFARG)) n = curwp->w_ntrows / 2;
return internal_movetoline(n);
}
/*
* Mg3a: "move-to-window-line-top-bottom" a la Emacs.
*/
INT
movetoline_topbottom(INT f, INT n)
{
if (f & FFARG) {
return internal_movetoline(n);
} else if (lastflag & CFMR1) {
thisflag |= CFMR2;
return internal_movetoline(0);
} else if (lastflag & CFMR2) {
return internal_movetoline(-1);
} else {
thisflag |= CFMR1;
return internal_movetoline(curwp->w_ntrows / 2);
}
}
/*
* Refresh the display. A call is made to the "ttresize" entry in the
* terminal handler, which tries to reset "nrow" and "ncol". In the
* normal case the call to "update" in "main.c" refreshes the screen,
* and all of the windows need not be recomputed. Note that when you
* get to the "display unusable" message, the screen will be messed
* up. If you make the window bigger again, and send another command,
* everything will get fixed!
*/
INT
refresh(INT f, INT n)
{
WINDOW *wp;
INT oldnrow;
INT oldncol;
if (f & FFARG) {
ewprintf("Display size is %d by %d", nrow, ncol);
return TRUE;
}
oldnrow = nrow;
oldncol = ncol;
ttresize();
if (nrow!=oldnrow || ncol!=oldncol) {
wp = wheadp; /* Find last. */
while (wp->w_wndp != NULL)
wp = wp->w_wndp;
if (nrow < wp->w_toprow+3) { /* Check if too small. */
ewprintf("Display unusable");
return (FALSE);
}
wp->w_ntrows = nrow-wp->w_toprow-2;
}
sgarbf = TRUE;
return TRUE;
}
/*
* The command to make the next window (next => down the screen) the
* current window. There are no real errors, although the command does
* nothing if there is only 1 window on the screen.
*/
INT
nextwind(INT f, INT n)
{
WINDOW *wp;
if (n < 0) return prevwind(f, -n);
if ((wp=curwp->w_wndp) == NULL)
wp = wheadp;
curwp = wp;
curbp = wp->w_bufp;
return TRUE;
}
/*
* This command makes the previous window (previous => up the screen)
* the current window. There arn't any errors, although the command
* does not do a lot if there is 1 window.
*/
INT
prevwind(INT f, INT n)
{
WINDOW *wp1;
WINDOW *wp2;
if (n < 0) return nextwind(f, -n);
wp1 = wheadp;
wp2 = curwp;
if (wp1 == wp2)
wp2 = NULL;
while (wp1->w_wndp != wp2)
wp1 = wp1->w_wndp;
curwp = wp1;
curbp = wp1->w_bufp;
return TRUE;
}
/*
* This command makes the current window the only window on the
* screen. Try to set the framing so that "." does not have to move on
* the display. Some care has to be taken to keep the values of dot
* and mark in the buffer structures right if the distruction of a
* window makes a buffer become undisplayed.
*/
INT
onlywind(INT f, INT n)
{
WINDOW *wp;
LINE *lp;
INT i;
while (wheadp != curwp) {
wp = wheadp;
wheadp = wp->w_wndp;
if (--wp->w_bufp->b_nwnd == 0) {
wp->w_bufp->b_dotp = wp->w_dotp;
wp->w_bufp->b_doto = wp->w_doto;
wp->w_bufp->b_markp = wp->w_markp;
wp->w_bufp->b_marko = wp->w_marko;
wp->w_bufp->b_dotpos = wp->w_dotpos;
}
free(wp);
}
while (curwp->w_wndp != NULL) {
wp = curwp->w_wndp;
curwp->w_wndp = wp->w_wndp;
if (--wp->w_bufp->b_nwnd == 0) {
wp->w_bufp->b_dotp = wp->w_dotp;
wp->w_bufp->b_doto = wp->w_doto;
wp->w_bufp->b_markp = wp->w_markp;
wp->w_bufp->b_marko = wp->w_marko;
wp->w_bufp->b_dotpos = wp->w_dotpos;
}
free(wp);
}
lp = curwp->w_linep;
i = curwp->w_toprow;
while (i!=0 && lback(lp)!=curbp->b_linep) {
--i;
lp = lback(lp);
}
curwp->w_toprow = 0;
curwp->w_ntrows = nrow-2; /* 2 = mode, echo. */
curwp->w_linep = lp;
curwp->w_flag |= WFMODE|WFHARD;
return TRUE;
}
/*
* Split the current window.
*
* Mg3a: Use arguments like in Emacs.
*/
INT
splitwind(INT f, INT n)
{
WINDOW *wp;
LINE *lp;
INT ntru;
INT ntrd;
INT ntrl;
WINDOW *wp1, *wp2;
INT rows;
rows = curwp->w_ntrows;
if (rows < 3) {
ewprintf("Cannot split a %d line window", rows);
return FALSE;
}
if (f & FFARG) {
if (n >= 3 && n <= rows - 2) {
ntru = n - 1;
ntrl = rows - ntru - 1;
} else if (-n >= 3 && -n <= rows - 2) {
ntrl = -n - 1;
ntru = rows - ntrl - 1;
} else {
ewprintf("Cannot split window as specified");
return FALSE;
}
} else {
ntru = (rows - 1) / 2; /* Upper size */
ntrl = rows - ntru - 1; /* Lower size */
}
if ((wp = (WINDOW *)malloc_msg(sizeof(WINDOW))) == NULL) return FALSE;
++curbp->b_nwnd; /* Displayed twice. */
wp->w_bufp = curbp;
wp->w_dotp = curwp->w_dotp;
wp->w_doto = curwp->w_doto;
wp->w_markp = curwp->w_markp;
wp->w_marko = curwp->w_marko;
wp->w_dotpos = curwp->w_dotpos;
wp->w_flag = 0;
wp->w_force = 0;
lp = curwp->w_linep;
ntrd = 0;
while (lp != curwp->w_dotp) {
++ntrd;
lp = lforw(lp);
}
lp = curwp->w_linep;
if (ntrd <= ntru || (backward_compat & BACKW_DYNWIND) == 0) { /* Old is upper window. */
if (ntrd == ntru) /* Hit mode line. */
lp = lforw(lp);
curwp->w_ntrows = ntru;
wp->w_wndp = curwp->w_wndp;
curwp->w_wndp = wp;
wp->w_toprow = curwp->w_toprow+ntru+1;
wp->w_ntrows = ntrl;
} else { /* Old is lower window */
wp1 = NULL;
wp2 = wheadp;
while (wp2 != curwp) {
wp1 = wp2;
wp2 = wp2->w_wndp;
}
if (wp1 == NULL)
wheadp = wp;
else
wp1->w_wndp = wp;
wp->w_wndp = curwp;
wp->w_toprow = curwp->w_toprow;
wp->w_ntrows = ntru;
++ntru; /* Mode line. */
curwp->w_toprow += ntru;
curwp->w_ntrows = ntrl;
while (ntru--)
lp = lforw(lp);
}
curwp->w_linep = lp; /* Adjust the top lines */
wp->w_linep = lp; /* if necessary. */
curwp->w_flag |= WFMODE|WFHARD;
wp->w_flag |= WFMODE|WFHARD;
return TRUE;
}
/*
* Enlarge the current window. Find the window that loses space. Make
* sure it is big enough. If so, hack the window descriptions, and ask
* redisplay to do all the hard work. You don't just set "force
* reframe" because dot would move.
*/
INT
enlargewind(INT f, INT n)
{
WINDOW *adjwp;
LINE *lp;
INT i;
if (n < 0)
return shrinkwind(f, -n);
if (wheadp->w_wndp == NULL) {
ewprintf("Only one window");
return FALSE;
}
if ((adjwp=curwp->w_wndp) == NULL) {
adjwp = wheadp;
while (adjwp->w_wndp != curwp)
adjwp = adjwp->w_wndp;
}
if (adjwp->w_ntrows <= n) {
ewprintf("Impossible change");
return FALSE;
}
if (curwp->w_wndp == adjwp) { /* Shrink below. */
lp = adjwp->w_linep;
for (i=0; i<n && lp!=adjwp->w_bufp->b_linep; ++i)
lp = lforw(lp);
adjwp->w_linep = lp;
adjwp->w_toprow += n;
} else { /* Shrink above. */
lp = curwp->w_linep;
for (i=0; i<n && lback(lp)!=curbp->b_linep; ++i)
lp = lback(lp);
curwp->w_linep = lp;
curwp->w_toprow -= n;
}
curwp->w_ntrows += n;
adjwp->w_ntrows -= n;
curwp->w_flag |= WFMODE|WFHARD;
adjwp->w_flag |= WFMODE|WFHARD;
return TRUE;
}
/*
* Shrink the current window. Find the window that gains space. Hack
* at the window descriptions. Ask the redisplay to do all the hard
* work.
*/
INT
shrinkwind(INT f, INT n)
{
WINDOW *adjwp;
LINE *lp;
INT i;
if (n < 0)
return enlargewind(f, -n);
if (wheadp->w_wndp == NULL) {
ewprintf("Only one window");
return FALSE;
}
/*
* Bit of flakiness - FFRAND means it was an internal call, and
* to be trusted implicitly about sizes.
*/
if (!(f & FFRAND) && curwp->w_ntrows <= n) {
ewprintf("Impossible change");
return (FALSE);
}
if ((adjwp=curwp->w_wndp) == NULL) {
adjwp = wheadp;
while (adjwp->w_wndp != curwp)
adjwp = adjwp->w_wndp;
}
if (curwp->w_wndp == adjwp) { /* Grow below. */
lp = adjwp->w_linep;
for (i=0; i<n && lback(lp)!=adjwp->w_bufp->b_linep; ++i)
lp = lback(lp);
adjwp->w_linep = lp;
adjwp->w_toprow -= n;
} else { /* Grow above. */
lp = curwp->w_linep;
for (i=0; i<n && lp!=curbp->b_linep; ++i)
lp = lforw(lp);
curwp->w_linep = lp;
curwp->w_toprow += n;
}
curwp->w_ntrows -= n;
adjwp->w_ntrows += n;
curwp->w_flag |= WFMODE|WFHARD;
adjwp->w_flag |= WFMODE|WFHARD;
return (TRUE);
}
/*
* Delete current window. Call shrink-window to do the screen
* updating, then throw away the window.
*/
INT
delwind(INT f, INT n)
{
WINDOW *wp, *nwp;
wp = curwp; /* Cheap... */
/* shrinkwind returning false means only one window... */
if (shrinkwind(FFRAND, wp->w_ntrows + 1) == FALSE)
return FALSE;
if (--wp->w_bufp->b_nwnd == 0) {
wp->w_bufp->b_dotp = wp->w_dotp;
wp->w_bufp->b_doto = wp->w_doto;
wp->w_bufp->b_markp = wp->w_markp;
wp->w_bufp->b_marko = wp->w_marko;
wp->w_bufp->b_dotpos = wp->w_dotpos;
}
/* since shrinkwind did't crap out, we know we have a second window */
if (wp == wheadp) wheadp = curwp = wp->w_wndp;
else if ((curwp = wp->w_wndp) == NULL) curwp = wheadp;
curbp = curwp->w_bufp;
for (nwp = wheadp; nwp != NULL; nwp = nwp->w_wndp)
if (nwp->w_wndp == wp) {
nwp->w_wndp = wp->w_wndp;
break ;
}
free(wp);
return TRUE;
}
/*
* Pick a window for a pop-up. Split the screen if there is only one
* window. Return a pointer, or NULL on error.
*
* Mg3a: Pick the next-below window or the top one, like in GNU Emacs.
*/
WINDOW *
wpopup()
{
if (wheadp->w_wndp == NULL) {
if (splitwind(0, 1) == FALSE) return NULL;
}
return curwp->w_wndp ? curwp->w_wndp : wheadp;
}
/*
* Mg3a: Quit a window. From GNU Emacs. Prefer non-system, non-dired,
* buffers. Resort to scratch if there is nothing else.
*/
INT
quitwind(INT f, INT n)
{
BUFFER *bp = NULL;
if (wheadp->w_wndp) return delwind(f, n);
if (curbp->b_altb && !(curbp->b_altb->b_flag & (BFSYS|BFDIRED))) {
bp = curbp->b_altb;
} else {
for (bp = bheadp; bp; bp = bp->b_bufp) {
if (bp != curbp && !(bp->b_flag & (BFSYS|BFDIRED))) break;
}
}
if (bp == NULL && (bp = bfind("*scratch*", TRUE)) == NULL) return FALSE;
curbp = bp;
return showbuffer(bp, curwp);
}
/*
* Mg3a: balance window areas. Put larger windows at bottom.
*/
INT
balancewind(INT f, INT n)
{
INT wcount, wlen, total, longlen, line, len;
WINDOW *wp;
wcount = 0;
for (wp = wheadp; wp; wp = wp->w_wndp) wcount++;
total = nrow - 1;
wlen = total/wcount;
longlen = total - wlen*wcount; // Excess window length, to be distributed
line = 0;
for (wp = wheadp; wp; wp = wp->w_wndp) {
wp->w_toprow = line;
len = wlen;
if (longlen++ >= wcount) len++; // Balanced bottom windows get it
wp->w_ntrows = len - 1;
line += len;
}
refreshbuf(NULL);
return TRUE;
}
/*
* Mg3a: Shrink window if it's larger than the buffer and the buffer's
* first line is displayed. From GNU Emacs.
*/
INT
shrinkwind_iflarge(INT f, INT n)
{
INT count, rows;
LINE *lp, *blp, *dotp;
reframe_window(curwp);
blp = curwp->w_bufp->b_linep;
/* If first line is not shown, or only one window */
if (curwp->w_linep != lforw(blp) || wheadp->w_wndp == NULL) return TRUE;
rows = curwp->w_ntrows;
lp = lback(blp);
/* Ignore last zero-length line */
if (lp != blp && llength(lp) == 0) lp = lback(lp);
count = 0;
while (lp != blp) {
count++;
lp = lback(lp);
if (count > rows) return TRUE;
}
dotp = curwp->w_dotp;
if (count < 2) {
count = 2;
} else if (dotp == lback(blp) && llength(dotp) == 0) {
/* Move cursor if window otherwise would scroll */
endofpreviousline();
}
/* We always set it to minimum 2, so no FFRAND */
shrinkwind(0, rows - count);
/* Must reset for bottom window */
curwp->w_linep = lforw(curbp->b_linep);
return TRUE;
}