Debian Codegraph @

wind.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 /* This creates/deletes/divides/grows/shrinks windows. */
9
10 #include "jove.h"
11 #include "chars.h"
12 #include "disp.h"
13 #include "ask.h"
14 #include "extend.h"
15 #include "commands.h" /* for FindCmd and ExecCmd */
16 #include "mac.h"
17 #include "reapp.h"
18 #include "wind.h"
19 #include "screen.h"
20
21 private const char
22 onlyone[] = "You only have one window!",
23 toosmall[] = "Resulting window would be too small.";
24
25 #ifdef HIGHLIGHTING
26 jbool ScrollBar = NO; /* VAR: whether the scrollbar is used */
27 #endif
28
29 Window
30 *curwind,
31 *fwind = NULL;
32
33 /* First line in a Window */
34
35 private int
36 FLine(Window *w)
37 {
38 Window *wp = fwind;
39 int lineno = -1;
40
41 while (wp != w) {
42 lineno += wp->w_height;
43 wp = wp->w_next;
44 if (wp == fwind) {
45 complain("window?");
46 /* NOTREACHED */
47 }
48 }
49 return lineno + 1;
50 }
51
52 /* Delete `wp' from the screen. If it is the only window left
53 * on the screen, then complain. It gives its body
54 * to the next window if there is one, otherwise the previous
55 * window gets the body.
56 */
57 void
58 del_wind(Window *wp)
59 {
60 Window *prev = wp->w_prev,
61 *heir = prev; /* default: previous window inherits space */
62
63 if (one_windp()) {
64 complain(onlyone);
65 /* NOTREACHED */
66 }
67 prev->w_next = wp->w_next;
68 wp->w_next->w_prev = prev;
69
70 if (fwind == wp) {
71 heir = fwind = wp->w_next; /* no window above: next inherits */
72 /* Here try to do something intelligent for redisplay() */
73 SetTop(fwind, prev_line(fwind->w_top, wp->w_height));
74 }
75 heir->w_height += wp->w_height;
76 if (curwind == wp)
77 SetWind(heir);
78 #ifdef MAC
79 RemoveScrollBar(wp);
80 Windchange = YES;
81 #endif
82 free(wp);
83 }
84
85 /* Divide the window WP N times, or at least once. Complains if WP is too
86 * small to be split into that many pieces. It returns the new window.
87 */
88 Window *
89 div_wind(Window *wp, int n)
90 {
91 Window *latest = wp;
92 int amt;
93
94 if (n < 1)
95 n = 1;
96 amt = wp->w_height / (n + 1);
97 if (amt < 2) {
98 complain(toosmall);
99 /* NOTREACHED */
100 }
101 do {
102 Window *new = (Window *) emalloc(sizeof (Window));
103
104 new->w_flags = 0;
105 new->w_LRscroll = 0;
106
107 new->w_height = amt;
108 wp->w_height -= amt;
109
110 /* set the lines such that w_line is the center in
111 * each Window
112 */
113 new->w_line = wp->w_line;
114 new->w_char = wp->w_char;
115 new->w_bufp = wp->w_bufp;
116 new->w_top = prev_line(new->w_line, WSIZE(new)/2);
117
118 /* Link the new window into the list */
119 new->w_prev = latest;
120 new->w_next = latest->w_next;
121 new->w_next->w_prev = latest->w_next = new;
122 latest = new;
123 #ifdef MAC
124 new->w_control = NULL;
125 #endif
126 } while (--n > 0);
127 #ifdef MAC
128 Windchange = YES;
129 #endif
130 return latest;
131 }
132
133 /* Initialze the first window setting the bounds to the size of the
134 * screen. There is no buffer with this window. See parse for the
135 * setting of this window.
136 */
137 void
138 winit(void)
139 {
140 Window *w;
141
142 w = curwind = fwind = (Window *) emalloc(sizeof (Window));
143 w->w_line = w->w_top = NULL;
144 w->w_LRscroll = 0;
145 w->w_flags = 0;
146 w->w_char = 0;
147 w->w_next = w->w_prev = fwind;
148 w->w_height = ILI;
149 w->w_bufp = NULL;
150 #ifdef MAC
151 w->w_control = NULL;
152 Windchange = YES;
153 #endif
154 }
155
156 void
157 tiewind(Window *w, Buffer *bp)
158 {
159 jbool not_tied = (w->w_bufp != bp);
160
161 UpdModLine = YES; /* kludge ... but speeds things up considerably */
162 w->w_line = bp->b_dot;
163 w->w_char = bp->b_char;
164 w->w_bufp = bp;
165 if (not_tied)
166 CalcWind(w); /* ah, this has been missing since the
167 beginning of time! */
168 }
169
170 /* Change to previous window. */
171
172 void
173 PrevWindow(void)
174 {
175 Window *new = curwind->w_prev;
176
177 if (Asking) {
178 complain((char *)NULL);
179 /* NOTREACHED */
180 }
181 if (one_windp()) {
182 complain(onlyone);
183 /* NOTREACHED */
184 }
185 SetWind(new);
186 }
187
188 /* Make NEW the current Window */
189
190 void
191 SetWind(Window *new)
192 {
193 if (!Asking && curbuf!=NULL) { /* can you say kludge? */
194 curwind->w_line = curline;
195 curwind->w_char = curchar;
196 curwind->w_bufp = curbuf;
197 }
198 if (new == curwind)
199 return;
200
201 SetBuf(new->w_bufp);
202 if (!inlist(new->w_bufp->b_first, new->w_line)) {
203 new->w_line = curline;
204 new->w_char = curchar;
205 }
206 DotTo(new->w_line, new->w_char);
207 if (curchar > (int)strlen(linebuf))
208 new->w_char = curchar = strlen(linebuf);
209 curwind = new;
210 }
211
212 /* delete the current window if it isn't the only one left */
213
214 void
215 DelCurWindow(void)
216 {
217 SetABuf(curwind->w_bufp);
218 del_wind(curwind);
219 }
220
221 /* put the current line of `w' in the middle of the window */
222
223 void
224 CentWind(Window *w)
225 {
226 SetTop(w, prev_line(w->w_line, WSIZE(w)/2));
227 }
228
229 int ScrollStep = 0; /* VAR: how should we scroll (full scrolling) */
230
231 /* Calculate the new topline of the window. If ScrollStep == 0
232 * it means we should center the current line in the window.
233 */
234 void
235 CalcWind(Window *w)
236 {
237 int up,
238 scr_step;
239 LinePtr newtop;
240
241 if (ScrollStep == 0) { /* Means just center it */
242 CentWind(w);
243 } else {
244 up = inorder(w->w_line, 0, w->w_top, 0);
245 if (up == -1) {
246 CentWind(w);
247 return;
248 }
249 scr_step = (ScrollStep < 0) ? WSIZE(w) + ScrollStep : ScrollStep - 1;
250 /* up: point is above the screen */
251 newtop = prev_line(w->w_line, up?
252 scr_step : (WSIZE(w) - 1 - scr_step));
253 if (LineDist(newtop, w->w_top) >= WSIZE(w) - 1)
254 CentWind(w);
255 else
256 SetTop(w, newtop);
257 }
258 }
259
260 /* This is bound to ^X 4 [BTF]. To make the screen stay the
261 * same we have to remember various things, like the current
262 * top line in the current window. It's sorta gross, but it's
263 * necessary because of the way this is implemented (i.e., in
264 * terms of do_find(), do_select() which manipulate the windows.
265 */
266 void
267 WindFind(void)
268 {
269 Buffer *obuf = curbuf,
270 *nbuf;
271 LinePtr ltop = curwind->w_top;
272 Bufpos
273 odot,
274 ndot;
275 cmdproc_t cmd;
276
277 DOTsave(&odot);
278
279 switch (waitchar()) {
280 case 't':
281 case 'T':
282 cmd = FindTag;
283 break;
284
285 case CTL('T'):
286 cmd = FDotTag;
287 break;
288
289 case 'b':
290 case 'B':
291 cmd = BufSelect;
292 break;
293
294 case 'f':
295 case 'F':
296 cmd = FindFile;
297 break;
298
299 default:
300 cmd = (cmdproc_t)0; /* avoid uninitialized complaint from gcc -W */
301 complain("T: find-tag, ^T: find-tag-at-point, F: find-file, B: select-buffer.");
302 /*NOTREACHED*/
303 }
304 ExecCmd((data_obj *) FindCmd(cmd));
305
306 nbuf = curbuf;
307 DOTsave(&ndot);
308 SetBuf(obuf);
309 SetDot(&odot);
310 SetTop(curwind, ltop); /* there! it's as if we did nothing */
311
312 if (one_windp())
313 (void) div_wind(curwind, 1);
314
315 tiewind(curwind->w_next, nbuf);
316 SetWind(curwind->w_next);
317 SetDot(&ndot);
318 }
319
320 /* Go into one window mode by deleting all the other windows */
321
322 void
323 OneWindow(void)
324 {
325 while (curwind->w_next != curwind)
326 del_wind(curwind->w_next);
327 }
328
329 Window *
330 windbp(Buffer *bp)
331 {
332
333 Window *wp = fwind;
334
335 if (bp == NULL)
336 return NULL;
337 do {
338 if (wp->w_bufp == bp)
339 return wp;
340
341 wp = wp->w_next;
342 } while (wp != fwind);
343 return NULL;
344 }
345
346 /* Change window into the next window. Curwind becomes the new window. */
347
348 void
349 NextWindow(void)
350 {
351 Window *new = curwind->w_next;
352
353 if (Asking) {
354 complain((char *)NULL);
355 /* NOTREACHED */
356 }
357 if (one_windp()) {
358 complain(onlyone);
359 /* NOTREACHED */
360 }
361 SetWind(new);
362 }
363
364 /* Scroll the next Window */
365
366 void
367 PageNWind(void)
368 {
369 if (one_windp()) {
370 complain(onlyone);
371 /* NOTREACHED */
372 }
373 NextWindow();
374 NextPage();
375 PrevWindow();
376 }
377
378 private Window *
379 w_nam_typ(const char *name, int type)
380 {
381 Window *w;
382 Buffer *b;
383
384 b = buf_exists(name);
385 w = fwind;
386 if (b != NULL) {
387 do {
388 if (w->w_bufp == b)
389 return w;
390 } while ((w = w->w_next) != fwind);
391 }
392
393 w = fwind;
394 do {
395 if (w->w_bufp->b_type == type)
396 return w;
397 } while ((w = w->w_next) != fwind);
398
399 return NULL;
400 }
401
402 /* Put a window with the buffer `name' in it. Erase the buffer if
403 * `clobber' is YES.
404 */
405 void
406 pop_wind(const char *name, jbool clobber, int btype)
407 {
408 Window *wp;
409 Buffer *newb;
410
411 if ((newb = buf_exists(name)) != NULL)
412 btype = -1; /* if the buffer exists, don't change
413 it's type */
414 if ((wp = w_nam_typ(name, btype)) == NULL) {
415 if (one_windp())
416 SetWind(div_wind(curwind, 1));
417 else
418 PrevWindow();
419 } else
420 SetWind(wp);
421
422 newb = do_select((Window *)NULL, name);
423 if (clobber)
424 buf_clear(newb);
425 tiewind(curwind, newb);
426 if (btype != -1)
427 newb->b_type = btype;
428 SetBuf(newb);
429 }
430
431 void
432 GrowWindowCmd(void)
433 {
434 WindSize(curwind, abs((int)arg_value()));
435 }
436
437 void
438 ShrWindow(void)
439 {
440 WindSize(curwind, -abs((int)arg_value()));
441 }
442
443 /* Change the size of the window by inc. First arg is the window,
444 * second is the increment.
445 */
446 void
447 WindSize(Window *w, int inc)
448 {
449 if (one_windp()) {
450 complain(onlyone);
451 /* NOTREACHED */
452 }
453 if (inc == 0)
454 return;
455 else if (inc < 0) { /* Shrinking this Window. */
456 if (w->w_height + inc < 2) {
457 complain(toosmall);
458 /* NOTREACHED */
459 }
460 w->w_height += inc;
461 w->w_prev->w_height -= inc;
462 } else { /* Growing the window. */
463 /* Change made from original code so that growing a window
464 * exactly offsets effect of shrinking a window, i.e.
465 * doing either followed by the other restores original
466 * sizes of all affected windows.
467 */
468 if (w->w_prev->w_height - inc < 2) {
469 complain(toosmall);
470 /* NOTREACHED */
471 }
472 w->w_height += inc;
473 w->w_prev->w_height -= inc;
474 }
475 #ifdef MAC
476 Windchange = YES;
477 #endif
478 }
479
480 /* Set the topline of the window, calculating its number in the buffer.
481 * This is for numbering the lines only.
482 */
483
484 void
485 SetTop(Window *w, LinePtr line)
486 {
487 #ifdef HIGHLIGHTING
488 if (ScrollBar)
489 UpdModLine = YES;
490 #endif
491 w->w_top = line;
492 if (w->w_flags & W_NUMLINES)
493 w->w_topnum = LinesTo(w->w_bufp->b_first, line) + 1;
494 }
495
496 void
497 WNumLines(void)
498 {
499 curwind->w_flags ^= W_NUMLINES;
500 SetTop(curwind, curwind->w_top);
501 }
502
503 void
504 WVisSpace(void)
505 {
506 curwind->w_flags ^= W_VISSPACE;
507 ClAndRedraw();
508 }
509
510 /* If `line' is in `windes', return its screen line number;
511 * otherwise return -1.
512 */
513 int
514 in_window(Window *windes, LinePtr line)
515 {
516 int i;
517 LinePtr lp = windes->w_top;
518
519 for (i = 0; lp != NULL && i < windes->w_height - 1; i++, lp = lp->l_next)
520 if (lp == line)
521 return FLine(windes) + i;
522 return -1;
523 }
524
525 void
526 SplitWind(void)
527 {
528 SetWind(div_wind(curwind, arg_or_default(2) - 1));
529 }
530
531 /* Goto the window with the named buffer. If no such window
532 * exists, pop one and attach the buffer to it.
533 */
534 void
535 GotoWind(void)
536 {
537 const char *bname = ask_buf(lastbuf, ALLOW_OLD | ALLOW_INDEX | ALLOW_NEW);
538 Window *w;
539
540 w = curwind->w_next;
541 do {
542 if (w->w_bufp->b_name == bname) {
543 SetABuf(curbuf);
544 SetWind(w);
545 return;
546 }
547 w = w->w_next;
548 } while (w != curwind);
549 SetABuf(curbuf);
550 pop_wind(bname, NO, -1);
551 }
552
553 void
554 ScrollRight(void)
555 {
556 int amt = arg_or_default(ScrollWidth);
557
558 if (curwind->w_LRscroll - amt < 0)
559 curwind->w_LRscroll = 0;
560 else
561 curwind->w_LRscroll -= amt;
562 UpdModLine = YES;
563 }
564
565 void
566 ScrollLeft(void)
567 {
568 int amt = arg_or_default(ScrollWidth);
569
570 curwind->w_LRscroll += amt;
571 UpdModLine = YES;
572 }
573
574 LineEffects
575 WindowRange(Window *w)
576 {
577 #ifdef HIGHLIGHTING
578 static struct LErange range = {0-0, 0-0, SO_effect, US_effect};
579
580 range.start = range.width = 0; /* default: no highlighting */
581 if (ScrollBar) {
582 int /* line counts of various portions -- slow! */
583 above = LinesTo(w->w_bufp->b_first, w->w_top),
584 below = LinesTo(w->w_top, (LinePtr)NULL),
585 total = above + below,
586 in = jmin(below, WSIZE(w));
587
588 if (above == -1 || below == -1)
589 return &range; /* something fishy */
590
591 below -= in; /* correction */
592 if (in != total) {
593 /* Window shows only part of the buffer: highlight "thumb".
594 *
595 * Required properties:
596 * - proportionality of "below", "in", and "above" segments
597 * - monotonicity and smoothness of representation
598 * - a segment vanishes iff it is empty (but "in" is never empty)
599 * - extreme L & R ends of modeline must indicate presence/absence
600 * of first/last line of buffer; hence some non-linearity
601 * thereabouts.
602 *
603 * Implementation:
604 * - Use unsigned long to prevent overflow.
605 * - Allocate space to "above" and "below", rounding to nearest
606 * for best proportionality.
607 * - Allocate the rest to "in".
608 * - Ensure "in" not empty by ensuring total space allocated
609 * to above and below must leave at least one col. This
610 * is done fiddling the rounding term when "in" is small.
611 *
612 * - The first (last) char in the modeline represents the
613 * first (last) line in the buffer.
614 *
615 * - That leaves the rest of the modeline to represent
616 * (linearly) the remaining (total-2) lines of the buffer,
617 * of which (above-1) should be represented by highlighting
618 * from char position 2 onwards and (below-1) should be
619 * represented by highlighting from (totalcols-1) backwards.
620 *
621 * - Rounding is applied in this region, and is fiddled to
622 * ensure that the white bit in the middle never shrinks to
623 * zero.
624 */
625 int
626 totalcols_2 = CO - 1 - (4 * SG) - 2,
627 total_2 = total - 2,
628 rounding = (in < total_2/totalcols_2) ? (totalcols_2*in)/2 : total_2/2,
629 abovecols = (above == 0) ? 0 :
630 1 + ((long)(above-1)*totalcols_2 + rounding) / total_2,
631 belowcols = (below == 0) ? 0 :
632 1 + ((long)(below-1)*totalcols_2 + rounding) / total_2;
633
634 range.start = abovecols;
635 range.width = totalcols_2 + 2 - abovecols - belowcols;
636 }
637 }
638 return &range;
639 #else /* !HIGHLIGHTING */
640 return YES; /* modeline always stands out */
641 #endif /* !HIGHLIGHTING */
642 }