xref: /netbsd/external/bsd/nvi/dist/ex/ex_z.c (revision cc73507a)
1*cc73507aSchristos /*	$NetBSD: ex_z.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
23a571abcSchristos /*-
33a571abcSchristos  * Copyright (c) 1993, 1994
43a571abcSchristos  *	The Regents of the University of California.  All rights reserved.
53a571abcSchristos  * Copyright (c) 1993, 1994, 1995, 1996
63a571abcSchristos  *	Keith Bostic.  All rights reserved.
73a571abcSchristos  *
83a571abcSchristos  * See the LICENSE file for redistribution information.
93a571abcSchristos  */
103a571abcSchristos 
113a571abcSchristos #include "config.h"
123a571abcSchristos 
13*cc73507aSchristos #include <sys/cdefs.h>
14*cc73507aSchristos #if 0
153a571abcSchristos #ifndef lint
163a571abcSchristos static const char sccsid[] = "Id: ex_z.c,v 10.12 2001/06/25 15:19:22 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:22 ";
173a571abcSchristos #endif /* not lint */
18*cc73507aSchristos #else
19*cc73507aSchristos __RCSID("$NetBSD: ex_z.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*cc73507aSchristos #endif
213a571abcSchristos 
223a571abcSchristos #include <sys/types.h>
233a571abcSchristos #include <sys/queue.h>
243a571abcSchristos 
253a571abcSchristos #include <bitstring.h>
263a571abcSchristos #include <limits.h>
273a571abcSchristos #include <stdio.h>
283a571abcSchristos #include <stdlib.h>
293a571abcSchristos #include <string.h>
303a571abcSchristos 
313a571abcSchristos #include "../common/common.h"
323a571abcSchristos 
333a571abcSchristos /*
343a571abcSchristos  * ex_z -- :[line] z [^-.+=] [count] [flags]
353a571abcSchristos  *	Adjust window.
363a571abcSchristos  *
373a571abcSchristos  * PUBLIC: int ex_z __P((SCR *, EXCMD *));
383a571abcSchristos  */
393a571abcSchristos int
ex_z(SCR * sp,EXCMD * cmdp)403a571abcSchristos ex_z(SCR *sp, EXCMD *cmdp)
413a571abcSchristos {
4208d478e3Schristos 	MARK abm;
433a571abcSchristos 	db_recno_t cnt, equals, lno;
443a571abcSchristos 	int eofcheck;
453a571abcSchristos 
463a571abcSchristos 	NEEDFILE(sp, cmdp);
473a571abcSchristos 
483a571abcSchristos 	/*
493a571abcSchristos 	 * !!!
503a571abcSchristos 	 * If no count specified, use either two times the size of the
513a571abcSchristos 	 * scrolling region, or the size of the window option.  POSIX
523a571abcSchristos 	 * 1003.2 claims that the latter is correct, but historic ex/vi
533a571abcSchristos 	 * documentation and practice appear to use the scrolling region.
543a571abcSchristos 	 * I'm using the window size as it means that the entire screen
553a571abcSchristos 	 * is used instead of losing a line to roundoff.  Note, we drop
563a571abcSchristos 	 * a line from the cnt if using the window size to leave room for
573a571abcSchristos 	 * the next ex prompt.
583a571abcSchristos 	 */
593a571abcSchristos 	if (FL_ISSET(cmdp->iflags, E_C_COUNT))
603a571abcSchristos 		cnt = cmdp->count;
613a571abcSchristos 	else
623a571abcSchristos #ifdef HISTORIC_PRACTICE
633a571abcSchristos 		cnt = O_VAL(sp, O_SCROLL) * 2;
643a571abcSchristos #else
653a571abcSchristos 		cnt = O_VAL(sp, O_WINDOW) - 1;
663a571abcSchristos #endif
673a571abcSchristos 
683a571abcSchristos 	equals = 0;
693a571abcSchristos 	eofcheck = 0;
703a571abcSchristos 	lno = cmdp->addr1.lno;
713a571abcSchristos 
723a571abcSchristos 	switch (FL_ISSET(cmdp->iflags,
733a571abcSchristos 	    E_C_CARAT | E_C_DASH | E_C_DOT | E_C_EQUAL | E_C_PLUS)) {
743a571abcSchristos 	case E_C_CARAT:		/* Display cnt * 2 before the line. */
753a571abcSchristos 		eofcheck = 1;
763a571abcSchristos 		if (lno > cnt * 2)
773a571abcSchristos 			cmdp->addr1.lno = (lno - cnt * 2) + 1;
783a571abcSchristos 		else
793a571abcSchristos 			cmdp->addr1.lno = 1;
803a571abcSchristos 		cmdp->addr2.lno = (cmdp->addr1.lno + cnt) - 1;
813a571abcSchristos 		break;
823a571abcSchristos 	case E_C_DASH:		/* Line goes at the bottom of the screen. */
833a571abcSchristos 		cmdp->addr1.lno = lno > cnt ? (lno - cnt) + 1 : 1;
843a571abcSchristos 		cmdp->addr2.lno = lno;
853a571abcSchristos 		break;
863a571abcSchristos 	case E_C_DOT:		/* Line goes in the middle of the screen. */
873a571abcSchristos 		/*
883a571abcSchristos 		 * !!!
893a571abcSchristos 		 * Historically, the "middleness" of the line overrode the
903a571abcSchristos 		 * count, so that "3z.19" or "3z.20" would display the first
913a571abcSchristos 		 * 12 lines of the file, i.e. (N - 1) / 2 lines before and
923a571abcSchristos 		 * after the specified line.
933a571abcSchristos 		 */
943a571abcSchristos 		eofcheck = 1;
953a571abcSchristos 		cnt = (cnt - 1) / 2;
963a571abcSchristos 		cmdp->addr1.lno = lno > cnt ? lno - cnt : 1;
973a571abcSchristos 		cmdp->addr2.lno = lno + cnt;
983a571abcSchristos 
993a571abcSchristos 		/*
1003a571abcSchristos 		 * !!!
1013a571abcSchristos 		 * Historically, z. set the absolute cursor mark.
1023a571abcSchristos 		 */
10308d478e3Schristos 		abm.lno = sp->lno;
10408d478e3Schristos 		abm.cno = sp->cno;
10508d478e3Schristos 		(void)mark_set(sp, ABSMARK1, &abm, 1);
1063a571abcSchristos 		break;
1073a571abcSchristos 	case E_C_EQUAL:		/* Center with hyphens. */
1083a571abcSchristos 		/*
1093a571abcSchristos 		 * !!!
1103a571abcSchristos 		 * Strangeness.  The '=' flag is like the '.' flag (see the
1113a571abcSchristos 		 * above comment, it applies here as well) but with a special
1123a571abcSchristos 		 * little hack.  Print out lines of hyphens before and after
1133a571abcSchristos 		 * the specified line.  Additionally, the cursor remains set
1143a571abcSchristos 		 * on that line.
1153a571abcSchristos 		 */
1163a571abcSchristos 		eofcheck = 1;
1173a571abcSchristos 		cnt = (cnt - 1) / 2;
1183a571abcSchristos 		cmdp->addr1.lno = lno > cnt ? lno - cnt : 1;
1193a571abcSchristos 		cmdp->addr2.lno = lno - 1;
1203a571abcSchristos 		if (ex_pr(sp, cmdp))
1213a571abcSchristos 			return (1);
1223a571abcSchristos 		(void)ex_puts(sp, "----------------------------------------\n");
1233a571abcSchristos 		cmdp->addr2.lno = cmdp->addr1.lno = equals = lno;
1243a571abcSchristos 		if (ex_pr(sp, cmdp))
1253a571abcSchristos 			return (1);
1263a571abcSchristos 		(void)ex_puts(sp, "----------------------------------------\n");
1273a571abcSchristos 		cmdp->addr1.lno = lno + 1;
1283a571abcSchristos 		cmdp->addr2.lno = (lno + cnt) - 1;
1293a571abcSchristos 		break;
1303a571abcSchristos 	default:
1313a571abcSchristos 		/* If no line specified, move to the next one. */
1323a571abcSchristos 		if (F_ISSET(cmdp, E_ADDR_DEF))
1333a571abcSchristos 			++lno;
1343a571abcSchristos 		/* FALLTHROUGH */
1353a571abcSchristos 	case E_C_PLUS:		/* Line goes at the top of the screen. */
1363a571abcSchristos 		eofcheck = 1;
1373a571abcSchristos 		cmdp->addr1.lno = lno;
1383a571abcSchristos 		cmdp->addr2.lno = (lno + cnt) - 1;
1393a571abcSchristos 		break;
1403a571abcSchristos 	}
1413a571abcSchristos 
1423a571abcSchristos 	if (eofcheck) {
1433a571abcSchristos 		if (db_last(sp, &lno))
1443a571abcSchristos 			return (1);
1453a571abcSchristos 		if (cmdp->addr2.lno > lno)
1463a571abcSchristos 			cmdp->addr2.lno = lno;
1473a571abcSchristos 	}
1483a571abcSchristos 
1493a571abcSchristos 	if (ex_pr(sp, cmdp))
1503a571abcSchristos 		return (1);
1513a571abcSchristos 	if (equals)
1523a571abcSchristos 		sp->lno = equals;
1533a571abcSchristos 	return (0);
1543a571abcSchristos }
155