File: wind.c

package info (click to toggle)
jove 4.16-5
  • links: PTS
  • area: main
  • in suites: potato, slink
  • size: 1,804 kB
  • ctags: 2,866
  • sloc: ansic: 27,140; makefile: 401
file content (641 lines) | stat: -rw-r--r-- 13,560 bytes parent folder | download | duplicates (2)
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
632
633
634
635
636
637
638
639
640
641
/************************************************************************
 * This program is Copyright (C) 1986-1996 by Jonathan Payne.  JOVE is  *
 * provided to you without charge, and with no warranty.  You may give  *
 * away copies of JOVE, including sources, provided that this notice is *
 * included in all the files.                                           *
 ************************************************************************/

/* This creates/deletes/divides/grows/shrinks windows.  */

#include "jove.h"
#include "chars.h"
#include "disp.h"
#include "ask.h"
#include "extend.h"
#include "commands.h"	/* for FindCmd and ExecCmd */
#include "mac.h"
#include "reapp.h"
#include "wind.h"
#include "screen.h"

private const char
	onlyone[] = "You only have one window!",
	toosmall[] = "Resulting window would be too small.";

#ifdef HIGHLIGHTING
bool	ScrollBar = NO;	/* VAR: whether the scrollbar is used */
#endif

Window
	*curwind,
	*fwind = NULL;

/* First line in a Window */

int
FLine(w)
register Window	*w;
{
	register Window	*wp = fwind;
	register int	lineno = -1;

	while (wp != w) {
		lineno += wp->w_height;
		wp = wp->w_next;
		if (wp == fwind)
			complain("window?");
	}
	return lineno + 1;
}

/* Delete `wp' from the screen.  If it is the only window left
   on the screen, then complain.  It gives its body
   to the next window if there is one, otherwise the previous
   window gets the body.  */

void
del_wind(wp)
register Window	*wp;
{
	register Window
		*prev = wp->w_prev,
		*heir = prev;	/* default: previous window inherits space */

	if (one_windp())
		complain(onlyone);

	prev->w_next = wp->w_next;
	wp->w_next->w_prev = prev;

	if (fwind == wp) {
		heir = fwind = wp->w_next;	/* no window above: next inherits */
		/* Here try to do something intelligent for redisplay() */
		SetTop(fwind, prev_line(fwind->w_top, wp->w_height));
	}
	heir->w_height += wp->w_height;
	if (curwind == wp)
		SetWind(heir);
#ifdef MAC
	RemoveScrollBar(wp);
	Windchange = YES;
#endif
	free((UnivPtr) wp);
}

/* Divide the window WP N times, or at least once.  Complains if WP is too
   small to be split into that many pieces.  It returns the new window. */

Window *
div_wind(wp, n)
register Window	*wp;
int	n;
{
	Window	*latest = wp;
	int	amt;

	if (n < 1)
		n = 1;
	amt = wp->w_height / (n + 1);
	if (amt < 2)
		complain(toosmall);

	do {
		register Window	*new = (Window *) emalloc(sizeof (Window));

		new->w_flags = 0;
		new->w_LRscroll = 0;

		new->w_height = amt;
		wp->w_height -= amt;

		/* set the lines such that w_line is the center in
		   each Window */
		new->w_line = wp->w_line;
		new->w_char = wp->w_char;
		new->w_bufp = wp->w_bufp;
		new->w_top = prev_line(new->w_line, WSIZE(new)/2);

		/* Link the new window into the list */
		new->w_prev = latest;
		new->w_next = latest->w_next;
		new->w_next->w_prev = latest->w_next = new;
		latest = new;
#ifdef MAC
		new->w_control = NULL;
#endif
	} while (--n > 0);
#ifdef MAC
	Windchange = YES;
#endif
	return latest;
}

/* Initialze the first window setting the bounds to the size of the
   screen.  There is no buffer with this window.  See parse for the
   setting of this window. */

void
winit()
{
	register Window	*w;

	w = curwind = fwind = (Window *) emalloc(sizeof (Window));
	w->w_line = w->w_top = NULL;
	w->w_LRscroll = 0;
	w->w_flags = 0;
	w->w_char = 0;
	w->w_next = w->w_prev = fwind;
	w->w_height = ILI;
	w->w_bufp = NULL;
#ifdef MAC
	w->w_control = NULL;
	Windchange = YES;
#endif
}

void
tiewind(w, bp)
register Window	*w;
register Buffer	*bp;
{
	bool	not_tied = (w->w_bufp != bp);

	UpdModLine = YES;	/* kludge ... but speeds things up considerably */
	w->w_line = bp->b_dot;
	w->w_char = bp->b_char;
	w->w_bufp = bp;
	if (not_tied)
		CalcWind(w);	/* ah, this has been missing since the
				   beginning of time! */
}

/* Change to previous window. */

void
PrevWindow()
{
	register Window	*new = curwind->w_prev;

	if (Asking)
		complain((char *)NULL);
	if (one_windp())
		complain(onlyone);
	SetWind(new);
}

/* Make NEW the current Window */

void
SetWind(new)
register Window	*new;
{
	if (!Asking && curbuf!=NULL) {		/* can you say kludge? */
		curwind->w_line = curline;
		curwind->w_char = curchar;
		curwind->w_bufp = curbuf;
	}
	if (new == curwind)
		return;
	SetBuf(new->w_bufp);
	if (!inlist(new->w_bufp->b_first, new->w_line)) {
		new->w_line = curline;
		new->w_char = curchar;
	}
	DotTo(new->w_line, new->w_char);
	if (curchar > (int)strlen(linebuf))
		new->w_char = curchar = strlen(linebuf);
	curwind = new;
}

/* delete the current window if it isn't the only one left */

void
DelCurWindow()
{
	SetABuf(curwind->w_bufp);
	del_wind(curwind);
}

/* put the current line of `w' in the middle of the window */

void
CentWind(w)
register Window	*w;
{
	SetTop(w, prev_line(w->w_line, WSIZE(w)/2));
}

int	ScrollStep = 0;	/* VAR: how should we scroll (full scrolling) */

/* Calculate the new topline of the window.  If ScrollStep == 0
   it means we should center the current line in the window. */

void
CalcWind(w)
register Window	*w;
{
	register int	up;
	int	scr_step;
	LinePtr	newtop;

	if (ScrollStep == 0) {	/* Means just center it */
		CentWind(w);
	} else {
		up = inorder(w->w_line, 0, w->w_top, 0);
		if (up == -1) {
			CentWind(w);
			return;
		}
		scr_step = (ScrollStep < 0) ? WSIZE(w) + ScrollStep :
			   ScrollStep - 1;
		/* up: point is above the screen */
		newtop = prev_line(w->w_line, up?
			scr_step : (WSIZE(w) - 1 - scr_step));
		if (LineDist(newtop, w->w_top) >= WSIZE(w) - 1)
			CentWind(w);
		else
			SetTop(w, newtop);
	}
}

/* This is bound to ^X 4 [BTF].  To make the screen stay the
   same we have to remember various things, like the current
   top line in the current window.  It's sorta gross, but it's
   necessary because of the way this is implemented (i.e., in
   terms of do_find(), do_select() which manipulate the windows. */

void
WindFind()
{
	register Buffer
		*obuf = curbuf,
		*nbuf;
	LinePtr	ltop = curwind->w_top;
	Bufpos
		odot,
		ndot;
	void	(*cmd) ptrproto((void));

	DOTsave(&odot);

	switch (waitchar()) {
	case 't':
	case 'T':
		cmd = FindTag;
		break;

	case CTL('T'):
		cmd = FDotTag;
		break;

	case 'b':
	case 'B':
		cmd = BufSelect;
		break;

	case 'f':
	case 'F':
		cmd = FindFile;
		break;

	default:
		cmd = NULL;	/* avoid uninitialized complaint from gcc -W */
		complain("T: find-tag, ^T: find-tag-at-point, F: find-file, B: select-buffer.");
		/*NOTREACHED*/
	}
	ExecCmd((data_obj *) FindCmd(cmd));

	nbuf = curbuf;
	DOTsave(&ndot);
	SetBuf(obuf);
	SetDot(&odot);
	SetTop(curwind, ltop);	/* there! it's as if we did nothing */

	if (one_windp())
		(void) div_wind(curwind, 1);

	tiewind(curwind->w_next, nbuf);
	SetWind(curwind->w_next);
	SetDot(&ndot);
}

/* Go into one window mode by deleting all the other windows */

void
OneWindow()
{
	while (curwind->w_next != curwind)
		del_wind(curwind->w_next);
}

Window *
windbp(bp)
register Buffer	*bp;
{

	register Window	*wp = fwind;

	if (bp == NULL)
		return NULL;
	do {
		if (wp->w_bufp == bp)
			return wp;
		wp = wp->w_next;
	} while (wp != fwind);
	return NULL;
}

/* Change window into the next window.  Curwind becomes the new window. */

void
NextWindow()
{
	register Window	*new = curwind->w_next;

	if (Asking)
		complain((char *)NULL);
	if (one_windp())
		complain(onlyone);
	SetWind(new);
}

/* Scroll the next Window */

void
PageNWind()
{
	if (one_windp())
		complain(onlyone);
	NextWindow();
	NextPage();
	PrevWindow();
}

private Window *
w_nam_typ(name, type)
register char	*name;
int	type;
{
	register Window *w;
	register Buffer	*b;

	b = buf_exists(name);
	w = fwind;
	if (b != NULL) {
		do {
			if (w->w_bufp == b)
				return w;
		} while ((w = w->w_next) != fwind);
	}

	w = fwind;
	do {
		if (w->w_bufp->b_type == type)
			return w;
	} while ((w = w->w_next) != fwind);

	return NULL;
}

/* Put a window with the buffer `name' in it.  Erase the buffer if
   `clobber' is YES. */

void
pop_wind(name, clobber, btype)
register char	*name;
bool	clobber;
int	btype;
{
	register Window	*wp;
	register Buffer	*newb;

	if ((newb = buf_exists(name)) != NULL)
		btype = -1;	/* if the buffer exists, don't change
				   it's type */
	if ((wp = w_nam_typ(name, btype)) == NULL) {
		if (one_windp())
			SetWind(div_wind(curwind, 1));
		else
			PrevWindow();
	} else
		SetWind(wp);

	newb = do_select((Window *)NULL, name);
	if (clobber)
		buf_clear(newb);
	tiewind(curwind, newb);
	if (btype != -1)
		newb->b_type = btype;
	SetBuf(newb);
}

void
GrowWindowCmd()
{
	WindSize(curwind, abs(arg_value()));
}

void
ShrWindow()
{
	WindSize(curwind, -abs(arg_value()));
}

/* Change the size of the window by inc.  First arg is the window,
   second is the increment. */

void
WindSize(w, inc)
register Window	*w;
register int	inc;
{
	if (one_windp())
		complain(onlyone);

	if (inc == 0)
		return;
	else if (inc < 0) {	/* Shrinking this Window. */
		if (w->w_height + inc < 2)
			complain(toosmall);
		w->w_height += inc;
		w->w_prev->w_height -= inc;
	} else {		/* Growing the window. */
		/* Change made from original code so that growing a window
		   exactly offsets effect of shrinking a window, i.e.
		   doing either followed by the other restores original
		   sizes of all affected windows. */
		if (w->w_prev->w_height - inc < 2)
			complain(toosmall);
		w->w_height += inc;
		w->w_prev->w_height -= inc;
	}
#ifdef MAC
	Windchange = YES;
#endif
}

/* Set the topline of the window, calculating its number in the buffer.
   This is for numbering the lines only. */

void
SetTop(w, line)
Window	*w;
register LinePtr	line;
{
#ifdef HIGHLIGHTING
	if (ScrollBar)
		UpdModLine = YES;
#endif
	w->w_top = line;
	if (w->w_flags & W_NUMLINES)
		w->w_topnum = LinesTo(w->w_bufp->b_first, line) + 1;
}

void
WNumLines()
{
	curwind->w_flags ^= W_NUMLINES;
	SetTop(curwind, curwind->w_top);
}

void
WVisSpace()
{
	curwind->w_flags ^= W_VISSPACE;
	ClAndRedraw();
}

/* If `line' is in `windes', return its screen line number;
   otherwise return -1. */

int
in_window(windes, line)
register Window	*windes;
register LinePtr	line;
{
	register int	i;
	register LinePtr	lp = windes->w_top;

	for (i = 0; lp != NULL && i < windes->w_height - 1; i++, lp = lp->l_next)
		if (lp == line)
			return FLine(windes) + i;
	return -1;
}

void
SplitWind()
{
	SetWind(div_wind(curwind, arg_or_default(2) - 1));
}

/* Goto the window with the named buffer.  If no such window
   exists, pop one and attach the buffer to it. */
void
GotoWind()
{
	char	*bname = ask_buf(lastbuf, ALLOW_OLD | ALLOW_INDEX | ALLOW_NEW);
	Window	*w;

	w = curwind->w_next;
	do {
		if (w->w_bufp->b_name == bname) {
			SetABuf(curbuf);
			SetWind(w);
			return;
		}
		w = w->w_next;
	} while (w != curwind);
	SetABuf(curbuf);
	pop_wind(bname, NO, -1);
}

void
ScrollRight()
{
	int	amt = arg_or_default(ScrollWidth);

	if (curwind->w_LRscroll - amt < 0)
		curwind->w_LRscroll = 0;
	else
		curwind->w_LRscroll -= amt;
	UpdModLine = YES;
}

void
ScrollLeft()
{
	int	amt = arg_or_default(ScrollWidth);

	curwind->w_LRscroll += amt;
	UpdModLine = YES;
}

LineEffects
WindowRange(w)
Window *w;
{
#ifdef HIGHLIGHTING
	static struct LErange range = {0-0, 0-0, SO_effect, US_effect};

	range.start = range.width = 0;	/* default: no highlighting */
	if (ScrollBar) {
		register int	/* line counts of various portions -- slow! */
			above = LinesTo(w->w_bufp->b_first, w->w_top),
			below = LinesTo(w->w_top, (LinePtr)NULL),
			total = above + below,
			in = min(below, WSIZE(w));

		if (above == -1 || below == -1)
			return &range;	/* something fishy */
		below -= in;	/* correction */
		if (in != total) {
			/* Window shows only part of the buffer: highlight "thumb".
			 *
			 * Required properties:
			 * - proportionality of "below", "in", and "above" segments
			 * - monotonicity and smoothness of representation
			 * - a segment vanishes iff it is empty (but "in" is never empty)
			 * - extreme L & R ends of modeline must indicate presence/absence
			 *   of first/last line of buffer; hence some non-linearity
			 *   thereabouts.
			 *
			 * Implementation:
			 * - Use unsigned long to prevent overflow.
			 * - Allocate space to "above" and "below", rounding to nearest
			 *   for best proportionality.
			 * - Allocate the rest to "in".
			 * - Ensure "in" not empty by ensuring total space allocated
			 *   to above and below must leave at least one col.  This
			 *   is done fiddling the rounding term when "in" is small.
			 *
			 * - The first (last) char in the modeline represents the
			 *   first (last) line in the buffer.
			 *
			 * - That leaves the rest of the modeline to represent
			 *   (linearly) the remaining (total-2) lines of the buffer,
			 *   of which (above-1) should be represented by highlighting
			 *   from char position 2 onwards and (below-1) should be
			 *   represented by highlighting from (totalcols-1) backwards.
			 *
			 * - Rounding is applied in this region, and is fiddled to
			 *   ensure that the white bit in the middle never shrinks to
			 *   zero.
			 */
			int
				totalcols_2 = CO - 1 - (4 * SG) - 2,
				total_2 = total - 2,
				rounding = (in < total_2/totalcols_2) ? (totalcols_2*in)/2 : total_2/2,
				abovecols = (above == 0) ? 0 :
					1 + ((long)(above-1)*totalcols_2 + rounding) / total_2,
				belowcols = (below == 0) ? 0 :
					1 + ((long)(below-1)*totalcols_2 + rounding) / total_2;

			range.start = abovecols;
			range.width = totalcols_2 + 2 - abovecols - belowcols;
		}
	}
	return &range;
#else /* !HIGHLIGHTING */
	return YES;	/*  modeline always stands out */
#endif /* !HIGHLIGHTING */
}