2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: vs_split.c,v 10.34 1996/12/18 10:24:31 bostic Exp $ (Berkeley) $Date: 1996/12/18 10:24:31 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
27 #include "../common/common.h"
30 typedef enum { HORIZ_FOLLOW
, HORIZ_PRECEDE
, VERT_FOLLOW
, VERT_PRECEDE
} jdir_t
;
32 static SCR
*vs_getbg
__P((SCR
*, char *));
33 static void vs_insert
__P((SCR
*));
34 static int vs_join
__P((SCR
*, SCR
**, jdir_t
*));
38 * Create a new screen, horizontally.
40 * PUBLIC: int vs_split __P((SCR *, SCR *, int));
43 vs_split(sp
, new, ccl
)
45 int ccl
; /* Colon-command line split. */
50 int issmallscreen
, splitup
;
54 /* Check to see if it's possible. */
55 /* XXX: The IS_ONELINE fix will change this, too. */
58 "222|Screen must be larger than %d lines to split", 4 - 1);
62 /* Wait for any messages in the screen. */
63 vs_resolve(sp
, NULL
, 1);
65 /* Get a new screen map. */
66 CALLOC(sp
, _HMAP(new), SMAP
*, SIZE_HMAP(sp
), sizeof(SMAP
));
67 if (_HMAP(new) == NULL
)
69 _HMAP(new)->lno
= sp
->lno
;
73 /* Split the screen in half. */
79 * Small screens: see vs_refresh.c section 6a. Set a flag so
80 * we know to fix the screen up later.
82 issmallscreen
= IS_SMALL(sp
);
84 /* The columns in the screen don't change. */
89 * Split the screen, and link the screens together. If creating a
90 * screen to edit the colon command line or the cursor is in the top
91 * half of the current screen, the new screen goes under the current
92 * screen. Else, it goes above the current screen.
94 * Recalculate current cursor position based on sp->lno, we're called
95 * with the cursor on the colon command line. Then split the screen
96 * in half and update the shared information.
99 !ccl
&& (vs_sm_cursor(sp
, &smp
) ? 0 : (smp
- HMAP
) + 1) >= half
;
100 if (splitup
) { /* Old is bottom half. */
101 new->rows
= sp
->rows
- half
; /* New. */
102 new->roff
= sp
->roff
;
103 sp
->rows
= half
; /* Old. */
104 sp
->roff
+= new->rows
;
107 * If the parent is the bottom half of the screen, shift
108 * the map down to match on-screen text.
110 memcpy(_HMAP(sp
), _HMAP(sp
) + new->rows
,
111 (sp
->t_maxrows
- new->rows
) * sizeof(SMAP
));
112 } else { /* Old is top half. */
113 new->rows
= half
; /* New. */
114 sp
->rows
-= half
; /* Old. */
115 new->roff
= sp
->roff
+ sp
->rows
;
118 /* Adjust maximum text count. */
119 sp
->t_maxrows
= IS_ONELINE(sp
) ? 1 : sp
->rows
- 1;
120 new->t_maxrows
= IS_ONELINE(new) ? 1 : new->rows
- 1;
123 * Small screens: see vs_refresh.c, section 6a.
125 * The child may have different screen options sizes than the parent,
126 * so use them. Guarantee that text counts aren't larger than the
130 /* Fix the text line count for the parent. */
132 sp
->t_rows
-= new->rows
;
134 /* Fix the parent screen. */
135 if (sp
->t_rows
> sp
->t_maxrows
)
136 sp
->t_rows
= sp
->t_maxrows
;
137 if (sp
->t_minrows
> sp
->t_maxrows
)
138 sp
->t_minrows
= sp
->t_maxrows
;
140 /* Fix the child screen. */
141 new->t_minrows
= new->t_rows
= O_VAL(sp
, O_WINDOW
);
142 if (new->t_rows
> new->t_maxrows
)
143 new->t_rows
= new->t_maxrows
;
144 if (new->t_minrows
> new->t_maxrows
)
145 new->t_minrows
= new->t_maxrows
;
147 sp
->t_minrows
= sp
->t_rows
= IS_ONELINE(sp
) ? 1 : sp
->rows
- 1;
150 * The new screen may be a small screen, even if the parent
151 * was not. Don't complain if O_WINDOW is too large, we're
152 * splitting the screen so the screen is much smaller than
155 new->t_minrows
= new->t_rows
= O_VAL(sp
, O_WINDOW
);
156 if (new->t_rows
> new->rows
- 1)
157 new->t_minrows
= new->t_rows
=
158 IS_ONELINE(new) ? 1 : new->rows
- 1;
161 /* Adjust the ends of the new and old maps. */
162 _TMAP(sp
) = IS_ONELINE(sp
) ?
163 _HMAP(sp
) : _HMAP(sp
) + (sp
->t_rows
- 1);
164 _TMAP(new) = IS_ONELINE(new) ?
165 _HMAP(new) : _HMAP(new) + (new->t_rows
- 1);
167 /* Reset the length of the default scroll. */
168 if ((sp
->defscroll
= sp
->t_maxrows
/ 2) == 0)
170 if ((new->defscroll
= new->t_maxrows
/ 2) == 0)
173 /* Fit the screen into the logical chain. */
176 /* Tell the display that we're splitting. */
177 (void)gp
->scr_split(sp
, new);
180 * Initialize the screen flags:
182 * If we're in vi mode in one screen, we don't have to reinitialize.
183 * This isn't just a cosmetic fix. The path goes like this:
185 * return into vi(), SC_SSWITCH set
186 * call vs_refresh() with SC_STATUS set
187 * call vs_resolve to display the status message
188 * call vs_refresh() because the SC_SCR_VI bit isn't set
190 * Things go downhill at this point.
192 * Draw the new screen from scratch, and add a status line.
195 SC_SCR_REFORMAT
| SC_STATUS
|
196 F_ISSET(sp
, SC_EX
| SC_VI
| SC_SCR_VI
| SC_SCR_EX
));
202 * Create a new screen, vertically.
204 * PUBLIC: int vs_vsplit __P((SCR *, SCR *));
215 /* Check to see if it's possible. */
216 if (sp
->cols
/ 2 <= MINIMUM_SCREEN_COLS
) {
218 "288|Screen must be larger than %d columns to split",
219 MINIMUM_SCREEN_COLS
* 2);
223 /* Wait for any messages in the screen. */
224 vs_resolve(sp
, NULL
, 1);
226 /* Get a new screen map. */
227 CALLOC(sp
, _HMAP(new), SMAP
*, SIZE_HMAP(sp
), sizeof(SMAP
));
228 if (_HMAP(new) == NULL
)
230 _HMAP(new)->lno
= sp
->lno
;
231 _HMAP(new)->coff
= 0;
232 _HMAP(new)->soff
= 1;
235 * Split the screen in half; we have to sacrifice a column to delimit
239 * We always split to the right... that makes more sense to me, and
240 * I don't want to play the stupid games that I play when splitting
244 * We reserve a column for the screen, "knowing" that curses needs
245 * one. This should be worked out with the display interface.
248 new->cols
= sp
->cols
- cols
- 1;
250 new->coff
= cols
+ 1;
253 /* Nothing else changes. */
254 new->rows
= sp
->rows
;
255 new->t_rows
= sp
->t_rows
;
256 new->t_maxrows
= sp
->t_maxrows
;
257 new->t_minrows
= sp
->t_minrows
;
258 new->roff
= sp
->roff
;
259 new->defscroll
= sp
->defscroll
;
260 _TMAP(new) = _HMAP(new) + (new->t_rows
- 1);
262 /* Fit the screen into the logical chain. */
265 /* Tell the display that we're splitting. */
266 (void)gp
->scr_split(sp
, new);
268 /* Redraw the old screen from scratch. */
269 F_SET(sp
, SC_SCR_REFORMAT
| SC_STATUS
);
272 * Initialize the screen flags:
274 * If we're in vi mode in one screen, we don't have to reinitialize.
275 * This isn't just a cosmetic fix. The path goes like this:
277 * return into vi(), SC_SSWITCH set
278 * call vs_refresh() with SC_STATUS set
279 * call vs_resolve to display the status message
280 * call vs_refresh() because the SC_SCR_VI bit isn't set
282 * Things go downhill at this point.
284 * Draw the new screen from scratch, and add a status line.
287 SC_SCR_REFORMAT
| SC_STATUS
|
288 F_ISSET(sp
, SC_EX
| SC_VI
| SC_SCR_VI
| SC_SCR_EX
));
294 * Insert the new screen into the correct place in the logical
306 /* Move past all screens with lower row numbers. */
307 for (tsp
= gp
->dq
.cqh_first
;
308 tsp
!= (void *)&gp
->dq
; tsp
= tsp
->q
.cqe_next
)
309 if (tsp
->roff
>= sp
->roff
)
312 * Move past all screens with the same row number and lower
315 for (; tsp
!= (void *)&gp
->dq
; tsp
= tsp
->q
.cqe_next
)
316 if (tsp
->roff
!= sp
->roff
|| tsp
->coff
> sp
->coff
)
320 * If we reached the end, this screen goes there. Otherwise,
321 * put it before or after the screen where we stopped.
323 if (tsp
== (void *)&gp
->dq
) {
324 CIRCLEQ_INSERT_TAIL(&gp
->dq
, sp
, q
);
325 } else if (tsp
->roff
< sp
->roff
||
326 tsp
->roff
== sp
->roff
&& tsp
->coff
< sp
->coff
) {
327 CIRCLEQ_INSERT_BEFORE(&gp
->dq
, tsp
, sp
, q
);
329 CIRCLEQ_INSERT_AFTER(&gp
->dq
, tsp
, sp
, q
);
334 * Discard the screen, folding the real-estate into a related screen,
335 * if one exists, and return that screen.
337 * PUBLIC: int vs_discard __P((SCR *, SCR **));
344 SCR
*tsp
, **lp
, *list
[100];
350 * Save the old screen's cursor information.
353 * If called after file_end(), and the underlying file was a tmp
354 * file, it may have gone away.
356 if (sp
->frp
!= NULL
) {
357 sp
->frp
->lno
= sp
->lno
;
358 sp
->frp
->cno
= sp
->cno
;
359 F_SET(sp
->frp
, FR_CURSORSET
);
362 /* If no other screens to join, we're done. */
364 (void)gp
->scr_discard(sp
, NULL
);
372 * Find a set of screens that cover one of the screen's borders.
373 * Check the vertical axis first, for no particular reason.
376 * It's possible (I think?), to create a screen that shares no full
377 * border with any other set of screens, so we can't discard it. We
378 * just complain at the user until they clean it up.
380 if (vs_join(sp
, list
, &jdir
))
384 * Modify the affected screens. Redraw the modified screen(s) from
385 * scratch, setting a status line. If this is ever a performance
386 * problem we could play games with the map, but I wrote that code
387 * before and it was never clean or easy.
389 * Don't clean up the discarded screen's information. If the screen
390 * isn't exiting, we'll do the work when the user redisplays it.
395 for (lp
= &list
[0]; (tsp
= *lp
) != NULL
; ++lp
) {
397 * Small screens: see vs_refresh.c section 6a. Adjust
398 * text line info, unless it's a small screen.
400 * Reset the length of the default scroll.
402 * Reset the map references.
404 tsp
->rows
+= sp
->rows
;
406 tsp
->t_rows
= tsp
->t_minrows
= tsp
->rows
- 1;
407 tsp
->t_maxrows
= tsp
->rows
- 1;
409 tsp
->defscroll
= tsp
->t_maxrows
/ 2;
411 *(_HMAP(tsp
) + (tsp
->t_rows
- 1)) = *_TMAP(tsp
);
412 _TMAP(tsp
) = _HMAP(tsp
) + (tsp
->t_rows
- 1);
416 tsp
->roff
= sp
->roff
;
417 vs_sm_fill(tsp
, OOBLNO
, P_TOP
);
420 vs_sm_fill(tsp
, OOBLNO
, P_BOTTOM
);
425 F_SET(tsp
, SC_STATUS
);
430 for (lp
= &list
[0]; (tsp
= *lp
) != NULL
; ++lp
) {
431 if (jdir
== VERT_FOLLOW
)
432 tsp
->coff
= sp
->coff
;
433 tsp
->cols
+= sp
->cols
+ 1; /* XXX: DIVIDER */
434 vs_sm_fill(tsp
, OOBLNO
, P_TOP
);
435 F_SET(tsp
, SC_STATUS
);
442 /* Find the closest screen that changed and move to it. */
447 /* Tell the display that we're discarding a screen. */
448 (void)gp
->scr_discard(sp
, tsp
);
455 * Find a set of screens that covers a screen's border.
458 vs_join(sp
, listp
, jdirp
)
469 /* Check preceding vertical. */
470 for (lp
= listp
, tlen
= sp
->rows
,
471 tsp
= gp
->dq
.cqh_first
;
472 tsp
!= (void *)&gp
->dq
; tsp
= tsp
->q
.cqe_next
) {
475 /* Test if precedes the screen vertically. */
476 if (tsp
->coff
+ tsp
->cols
+ 1 != sp
->coff
)
479 * Test if a subset on the vertical axis. If overlaps the
480 * beginning or end, we can't join on this axis at all.
482 if (tsp
->roff
> sp
->roff
+ sp
->rows
)
484 if (tsp
->roff
< sp
->roff
) {
485 if (tsp
->roff
+ tsp
->rows
>= sp
->roff
)
489 if (tsp
->roff
+ tsp
->rows
> sp
->roff
+ sp
->rows
)
492 if (tlen
< tsp
->rows
)
500 *jdirp
= VERT_PRECEDE
;
504 /* Check following vertical. */
505 for (lp
= listp
, tlen
= sp
->rows
,
506 tsp
= gp
->dq
.cqh_first
;
507 tsp
!= (void *)&gp
->dq
; tsp
= tsp
->q
.cqe_next
) {
510 /* Test if follows the screen vertically. */
511 if (tsp
->coff
!= sp
->coff
+ sp
->cols
+ 1)
514 * Test if a subset on the vertical axis. If overlaps the
515 * beginning or end, we can't join on this axis at all.
517 if (tsp
->roff
> sp
->roff
+ sp
->rows
)
519 if (tsp
->roff
< sp
->roff
) {
520 if (tsp
->roff
+ tsp
->rows
>= sp
->roff
)
524 if (tsp
->roff
+ tsp
->rows
> sp
->roff
+ sp
->rows
)
527 if (tlen
< tsp
->rows
)
535 *jdirp
= VERT_FOLLOW
;
539 /* Check preceding horizontal. */
540 for (first
= 0, lp
= listp
, tlen
= sp
->cols
,
541 tsp
= gp
->dq
.cqh_first
;
542 tsp
!= (void *)&gp
->dq
; tsp
= tsp
->q
.cqe_next
) {
545 /* Test if precedes the screen horizontally. */
546 if (tsp
->roff
+ tsp
->rows
!= sp
->roff
)
549 * Test if a subset on the horizontal axis. If overlaps the
550 * beginning or end, we can't join on this axis at all.
552 if (tsp
->coff
> sp
->coff
+ sp
->cols
)
554 if (tsp
->coff
< sp
->coff
) {
555 if (tsp
->coff
+ tsp
->cols
>= sp
->coff
)
559 if (tsp
->coff
+ tsp
->cols
> sp
->coff
+ sp
->cols
)
562 if (tlen
< tsp
->cols
)
565 tlen
-= tsp
->cols
+ first
;
571 *jdirp
= HORIZ_PRECEDE
;
575 /* Check following horizontal. */
576 for (first
= 0, lp
= listp
, tlen
= sp
->cols
,
577 tsp
= gp
->dq
.cqh_first
;
578 tsp
!= (void *)&gp
->dq
; tsp
= tsp
->q
.cqe_next
) {
581 /* Test if precedes the screen horizontally. */
582 if (tsp
->roff
!= sp
->roff
+ sp
->rows
)
585 * Test if a subset on the horizontal axis. If overlaps the
586 * beginning or end, we can't join on this axis at all.
588 if (tsp
->coff
> sp
->coff
+ sp
->cols
)
590 if (tsp
->coff
< sp
->coff
) {
591 if (tsp
->coff
+ tsp
->cols
>= sp
->coff
)
595 if (tsp
->coff
+ tsp
->cols
> sp
->coff
+ sp
->cols
)
598 if (tlen
< tsp
->cols
)
601 tlen
-= tsp
->cols
+ first
;
607 *jdirp
= HORIZ_FOLLOW
;
615 * Background the current screen, and foreground a new one.
617 * PUBLIC: int vs_fg __P((SCR *, SCR **, CHAR_T *, int));
620 vs_fg(sp
, nspp
, name
, newscreen
)
631 /* Get the specified background screen. */
632 nsp
= vs_getbg(sp
, name
);
635 if (vs_swap(sp
, &nsp
, name
))
638 if ((*nspp
= nsp
) == NULL
) {
639 msgq_str(sp
, M_ERR
, name
,
641 "223|There are no background screens" :
642 "224|There's no background screen editing a file named %s");
647 /* Remove the new screen from the background queue. */
648 CIRCLEQ_REMOVE(&gp
->hq
, nsp
, q
);
650 /* Split the screen; if we fail, hook the screen back in. */
651 if (vs_split(sp
, nsp
, 0)) {
652 CIRCLEQ_INSERT_TAIL(&gp
->hq
, nsp
, q
);
656 /* Move the old screen to the background queue. */
657 CIRCLEQ_REMOVE(&gp
->dq
, sp
, q
);
658 CIRCLEQ_INSERT_TAIL(&gp
->hq
, sp
, q
);
665 * Background the screen, and switch to the next one.
667 * PUBLIC: int vs_bg __P((SCR *));
678 /* Try and join with another screen. */
679 if (vs_discard(sp
, &nsp
))
683 "225|You may not background your only displayed screen");
687 /* Move the old screen to the background queue. */
688 CIRCLEQ_REMOVE(&gp
->dq
, sp
, q
);
689 CIRCLEQ_INSERT_TAIL(&gp
->hq
, sp
, q
);
691 /* Toss the screen map. */
695 /* Switch screens. */
697 F_SET(sp
, SC_SSWITCH
);
704 * Swap the current screen with a backgrounded one.
706 * PUBLIC: int vs_swap __P((SCR *, SCR **, char *));
709 vs_swap(sp
, nspp
, name
)
718 /* Get the specified background screen. */
719 if ((*nspp
= nsp
= vs_getbg(sp
, name
)) == NULL
)
723 * Save the old screen's cursor information.
726 * If called after file_end(), and the underlying file was a tmp
727 * file, it may have gone away.
729 if (sp
->frp
!= NULL
) {
730 sp
->frp
->lno
= sp
->lno
;
731 sp
->frp
->cno
= sp
->cno
;
732 F_SET(sp
->frp
, FR_CURSORSET
);
735 /* Switch screens. */
737 F_SET(sp
, SC_SSWITCH
);
739 /* Initialize terminal information. */
740 VIP(nsp
)->srows
= VIP(sp
)->srows
;
742 /* Initialize screen information. */
743 nsp
->cols
= sp
->cols
;
744 nsp
->rows
= sp
->rows
; /* XXX: Only place in vi that sets rows. */
745 nsp
->roff
= sp
->roff
;
748 * Small screens: see vs_refresh.c, section 6a.
750 * The new screens may have different screen options sizes than the
751 * old one, so use them. Make sure that text counts aren't larger
752 * than the new screen sizes.
755 nsp
->t_minrows
= nsp
->t_rows
= O_VAL(nsp
, O_WINDOW
);
756 if (nsp
->t_rows
> sp
->t_maxrows
)
757 nsp
->t_rows
= nsp
->t_maxrows
;
758 if (nsp
->t_minrows
> sp
->t_maxrows
)
759 nsp
->t_minrows
= nsp
->t_maxrows
;
761 nsp
->t_rows
= nsp
->t_maxrows
= nsp
->t_minrows
= nsp
->rows
- 1;
763 /* Reset the length of the default scroll. */
764 nsp
->defscroll
= nsp
->t_maxrows
/ 2;
766 /* Allocate a new screen map. */
767 CALLOC_RET(nsp
, _HMAP(nsp
), SMAP
*, SIZE_HMAP(nsp
), sizeof(SMAP
));
768 _TMAP(nsp
) = _HMAP(nsp
) + (nsp
->t_rows
- 1);
771 if (vs_sm_fill(nsp
, nsp
->lno
, P_FILL
))
775 * The new screen replaces the old screen in the parent/child list.
776 * We insert the new screen after the old one. If we're exiting,
777 * the exit will delete the old one, if we're foregrounding, the fg
778 * code will move the old one to the background queue.
780 CIRCLEQ_REMOVE(&gp
->hq
, nsp
, q
);
781 CIRCLEQ_INSERT_AFTER(&gp
->dq
, sp
, nsp
, q
);
784 * Don't change the screen's cursor information other than to
785 * note that the cursor is wrong.
787 F_SET(VIP(nsp
), VIP_CUR_INVALID
);
789 /* Draw the new screen from scratch, and add a status line. */
790 F_SET(nsp
, SC_SCR_REDRAW
| SC_STATUS
);
796 * Change the absolute size of the current screen.
798 * PUBLIC: int vs_resize __P((SCR *, long, adj_t));
801 vs_resize(sp
, count
, adj
)
813 * Figure out which screens will grow, which will shrink, and
814 * make sure it's possible.
819 if (sp
->t_maxrows
== count
)
821 if (sp
->t_maxrows
> count
) {
823 count
= sp
->t_maxrows
- count
;
826 count
= count
- sp
->t_maxrows
;
831 if (adj
== A_DECREASE
) {
835 if (s
->t_maxrows
< MINIMUM_SCREEN_ROWS
+ count
)
837 if ((g
= sp
->q
.cqe_prev
) == (void *)&gp
->dq
) {
838 if ((g
= sp
->q
.cqe_next
) == (void *)&gp
->dq
)
845 if ((s
= sp
->q
.cqe_next
) != (void *)&gp
->dq
)
846 if (s
->t_maxrows
< MINIMUM_SCREEN_ROWS
+ count
)
853 if ((s
= sp
->q
.cqe_prev
) == (void *)&gp
->dq
) {
854 toobig
: msgq(sp
, M_BERR
, adj
== A_DECREASE
?
855 "227|The screen cannot shrink" :
856 "228|The screen cannot grow");
859 if (s
->t_maxrows
< MINIMUM_SCREEN_ROWS
+ count
) {
860 toosmall
: msgq(sp
, M_BERR
,
861 "226|The screen can only shrink to %d rows",
862 MINIMUM_SCREEN_ROWS
);
870 * Fix up the screens; we could optimize the reformatting of the
871 * screen, but this isn't likely to be a common enough operation
872 * to make it worthwhile.
880 if (g
->t_minrows
== g
->t_maxrows
)
881 g
->t_minrows
+= count
;
882 g
->t_maxrows
+= count
;
884 F_SET(g
, SC_SCR_REFORMAT
| SC_STATUS
);
887 s
->t_maxrows
-= count
;
888 if (s
->t_minrows
> s
->t_maxrows
)
889 s
->t_minrows
= s
->t_maxrows
;
891 F_SET(s
, SC_SCR_REFORMAT
| SC_STATUS
);
898 * Get the specified background screen, or, if name is NULL, the first
912 /* If name is NULL, return the first background screen on the list. */
914 nsp
= gp
->hq
.cqh_first
;
915 return (nsp
== (void *)&gp
->hq
? NULL
: nsp
);
918 /* Search for a full match. */
919 for (nsp
= gp
->hq
.cqh_first
;
920 nsp
!= (void *)&gp
->hq
; nsp
= nsp
->q
.cqe_next
)
921 if (!strcmp(nsp
->frp
->name
, name
))
923 if (nsp
!= (void *)&gp
->hq
)
926 /* Search for a last-component match. */
927 for (nsp
= gp
->hq
.cqh_first
;
928 nsp
!= (void *)&gp
->hq
; nsp
= nsp
->q
.cqe_next
) {
929 if ((p
= strrchr(nsp
->frp
->name
, '/')) == NULL
)
933 if (!strcmp(p
, name
))
936 if (nsp
!= (void *)&gp
->hq
)