forked from bloovis/microemacs.mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.c
More file actions
506 lines (479 loc) · 11.8 KB
/
window.c
File metadata and controls
506 lines (479 loc) · 11.8 KB
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
/*
Copyright (C) 2008 Mark Alexander
This file is part of MicroEMACS, a small text editor.
MicroEMACS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Name: MicroEMACS
* Window handling.
* Version: 29
* Last edit: 27-Oct-87
* By: rex::conroy
* decvax!decwrl!dec-rhea!dec-rex!conroy
* Modified by: Mark Alexander
* drivax!alexande
*/
#include "def.h"
/*
* Reposition dot in the current
* window to line "n". If the argument is
* positive, it is that line. If it is negative it
* is that line from the bottom. If it is 0 the window
* is centered (this is what the standard redisplay code
* does). With no argument it defaults to 1.
* Because of the default, it works like in
* Gosling.
*/
int
reposition (int f, int n, int k)
{
curwp->w_force = n;
curwp->w_flag |= WFFORCE;
return (TRUE);
}
/*
* Refresh the display. A call is made to the
* "ttresize" entry in the terminal handler, which tries
* to reset "nrow" and "ncol". They will, however, never
* be set outside of the NROW or NCOL range. If the display
* changed size, arrange that everything is redone, then
* call "update" to fix the display. We do this so the
* new size can be displayed. 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
erefresh (int f, int n, int k)
{
register EWINDOW *wp;
register int oldnrow;
register int oldncol;
oldnrow = nrow;
oldncol = ncol;
if (f)
npages = n;
ttresize ();
sgarbf = TRUE; /* screen is garbage */
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. */
eprintf ("Display unusable");
return (FALSE);
}
wp->w_ntrows = nrow - wp->w_toprow - 2;
ALLWIND (wp) /* Redraw all. */
wp->w_flag |= WFMODE | WFHARD;
update ();
eprintf ("[New size %d by %d]", nrow, ncol);
}
return (TRUE);
}
/*
* The command 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, int k)
{
register EWINDOW *wp;
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, int k)
{
register EWINDOW *wp1;
register EWINDOW *wp2;
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 moves the current
* window down by "arg" lines. Recompute
* the top line in the window. The move up and
* move down code is almost completely the same;
* most of the work has to do with reframing the
* window, and picking a new dot. We share the
* code by having "move down" just be an interface
* to "move up".
*/
int
mvdnwind (int f, int n, int k)
{
return (mvupwind (f, -n, KRANDOM));
}
/*
* Move the current window up by "arg"
* lines. Recompute the new top line of the window.
* Look to see if "." is still on the screen. If it is,
* you win. If it isn't, then move "." to center it
* in the new framing of the window (this command does
* not really move "."; it moves the frame).
*/
int
mvupwind (int f, int n, int k)
{
register LINE *lp;
register int i;
lp = curwp->w_linep;
if (n < 0)
{
while (n++ && lp != lastline (curbp))
lp = lforw (lp);
}
else
{
while (n-- && lp != firstline (curbp))
lp = lback (lp);
}
curwp->w_linep = lp;
curwp->w_flag |= WFHARD; /* Mode line is OK. */
for (i = 0; i < curwp->w_ntrows; ++i)
{
if (lp == curwp->w_dot.p)
return (TRUE);
if (lp == lastline (curbp))
break;
lp = lforw (lp);
}
lp = curwp->w_linep;
i = curwp->w_ntrows / 2;
while (i-- && lp != lastline (curbp))
lp = lforw (lp);
curwp->w_dot.p = lp;
curwp->w_dot.o = 0;
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, int k)
{
register EWINDOW *wp;
register LINE *lp;
register int i;
while (wheadp != curwp)
{
wp = wheadp;
wheadp = wp->w_wndp;
addwind (wp, -1);
free ((char *) wp);
}
while (curwp->w_wndp != NULL)
{
wp = curwp->w_wndp;
curwp->w_wndp = wp->w_wndp;
addwind (wp, -1);
free ((char *) wp);
}
lp = curwp->w_linep;
i = curwp->w_toprow;
while (i != 0 && lp != firstline (curbp))
{
--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. A window
* smaller than 3 lines cannot be split.
* The only other error that is possible is
* a "malloc" failure allocating the structure
* for the new window.
*/
int
splitwind (int f, int n, int k)
{
register EWINDOW *wp;
register LINE *lp;
register int ntru;
register int ntrl;
register int ntrd;
register EWINDOW *wp1;
register EWINDOW *wp2;
if (curwp->w_ntrows < 3)
{
eprintf ("Cannot split a %d line window", curwp->w_ntrows);
return (FALSE);
}
if ((wp = (EWINDOW *) malloc (sizeof (EWINDOW))) == NULL)
{
eprintf ("Cannot allocate EWINDOW block");
return (FALSE);
}
++curbp->b_nwnd; /* Displayed twice. */
wp->w_bufp = curbp;
wp->w_dot = curwp->w_dot;
wp->w_mark = curwp->w_mark;
wp->w_ring = curwp->w_ring;
wp->w_flag = 0;
wp->w_force = 0;
wp->w_leftcol = curwp->w_leftcol;
ntru = (curwp->w_ntrows - 1) / 2; /* Upper size */
ntrl = (curwp->w_ntrows - 1) - ntru; /* Lower size */
lp = curwp->w_linep;
ntrd = 0;
while (lp != curwp->w_dot.p)
{
++ntrd;
lp = lforw (lp);
}
lp = curwp->w_linep;
if (ntrd <= ntru)
{ /* 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;
wp->w_savep = NULL;
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, int k)
{
register EWINDOW *adjwp;
register LINE *lp;
register int i;
if (n < 0)
return (shrinkwind (f, -n, KRANDOM));
if (wheadp->w_wndp == NULL)
{
eprintf ("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)
{
eprintf ("Impossible change");
return (FALSE);
}
if (curwp->w_wndp == adjwp)
{ /* Shrink below. */
lp = adjwp->w_linep;
for (i = 0; i < n && lp != lastline (adjwp->w_bufp); ++i)
lp = lforw (lp);
adjwp->w_linep = lp;
adjwp->w_toprow += n;
}
else
{ /* Shrink above. */
lp = curwp->w_linep;
for (i = 0; i < n && lp != firstline (curbp); ++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, int k)
{
register EWINDOW *adjwp;
register LINE *lp;
register int i;
if (n < 0)
return (enlargewind (f, -n, KRANDOM));
if (wheadp->w_wndp == NULL)
{
eprintf ("Only one window");
return (FALSE);
}
if ((adjwp = curwp->w_wndp) == NULL)
{
adjwp = wheadp;
while (adjwp->w_wndp != curwp)
adjwp = adjwp->w_wndp;
}
if (curwp->w_ntrows <= n)
{
eprintf ("Impossible change");
return (FALSE);
}
if (curwp->w_wndp == adjwp)
{ /* Grow below. */
lp = adjwp->w_linep;
for (i = 0; i < n && lp != firstline (adjwp->w_bufp); ++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 != lastline (curbp); ++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);
}
/*
* Adjust windows so that they all have approximately
* the same height.
*/
int
balancewindows (int f, int n, int k)
{
register EWINDOW *wp;
register LINE *lp;
register int toprow, size, nwind, i;
if (wheadp->w_wndp == NULL)
{
eprintf ("Only one window");
return (FALSE);
}
nwind = 0;
ALLWIND(wp)
{
nwind++;
}
toprow = 0;
size = ((nrow - 1) / nwind) - 1;
ALLWIND(wp)
{
if (wp->w_wndp == NULL)
size = nrow - toprow - 2;
if (size < wp->w_ntrows)
{
/* Shrink this window. */
n = wp->w_ntrows - size;
lp = wp->w_linep;
for (i = 0; i < n && lp != lastline (wp->w_bufp); ++i)
lp = lforw (lp);
wp->w_linep = lp;
}
wp->w_toprow = toprow;
wp->w_ntrows = size;
wp->w_flag |= WFMODE | WFHARD;
toprow += size + 1;
}
return (TRUE);
}
/*
* Pick a window for a pop-up.
* Split the screen if there is only
* one window. Pick the uppermost window that
* isn't the current window. An LRU algorithm
* might be better. Return a pointer, or
* NULL on error.
*/
EWINDOW *
wpopup (void)
{
register EWINDOW *wp;
if (wheadp->w_wndp == NULL && splitwind (FALSE, 0, KRANDOM) == FALSE)
return (NULL);
wp = wheadp; /* Find window to use */
while (wp != NULL && wp == curwp)
wp = wp->w_wndp;
return (wp);
}