1*0a6a1f1dSLionel Sambuc /* $NetBSD: ex_z.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc * Copyright (c) 1993, 1994
484d9c625SLionel Sambuc * The Regents of the University of California. All rights reserved.
584d9c625SLionel Sambuc * Copyright (c) 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc * Keith Bostic. All rights reserved.
784d9c625SLionel Sambuc *
884d9c625SLionel Sambuc * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc */
1084d9c625SLionel Sambuc
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc 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 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_z.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc
2584d9c625SLionel Sambuc #include <bitstring.h>
2684d9c625SLionel Sambuc #include <limits.h>
2784d9c625SLionel Sambuc #include <stdio.h>
2884d9c625SLionel Sambuc #include <stdlib.h>
2984d9c625SLionel Sambuc #include <string.h>
3084d9c625SLionel Sambuc
3184d9c625SLionel Sambuc #include "../common/common.h"
3284d9c625SLionel Sambuc
3384d9c625SLionel Sambuc /*
3484d9c625SLionel Sambuc * ex_z -- :[line] z [^-.+=] [count] [flags]
3584d9c625SLionel Sambuc * Adjust window.
3684d9c625SLionel Sambuc *
3784d9c625SLionel Sambuc * PUBLIC: int ex_z __P((SCR *, EXCMD *));
3884d9c625SLionel Sambuc */
3984d9c625SLionel Sambuc int
ex_z(SCR * sp,EXCMD * cmdp)4084d9c625SLionel Sambuc ex_z(SCR *sp, EXCMD *cmdp)
4184d9c625SLionel Sambuc {
4284d9c625SLionel Sambuc MARK abm;
4384d9c625SLionel Sambuc db_recno_t cnt, equals, lno;
4484d9c625SLionel Sambuc int eofcheck;
4584d9c625SLionel Sambuc
4684d9c625SLionel Sambuc NEEDFILE(sp, cmdp);
4784d9c625SLionel Sambuc
4884d9c625SLionel Sambuc /*
4984d9c625SLionel Sambuc * !!!
5084d9c625SLionel Sambuc * If no count specified, use either two times the size of the
5184d9c625SLionel Sambuc * scrolling region, or the size of the window option. POSIX
5284d9c625SLionel Sambuc * 1003.2 claims that the latter is correct, but historic ex/vi
5384d9c625SLionel Sambuc * documentation and practice appear to use the scrolling region.
5484d9c625SLionel Sambuc * I'm using the window size as it means that the entire screen
5584d9c625SLionel Sambuc * is used instead of losing a line to roundoff. Note, we drop
5684d9c625SLionel Sambuc * a line from the cnt if using the window size to leave room for
5784d9c625SLionel Sambuc * the next ex prompt.
5884d9c625SLionel Sambuc */
5984d9c625SLionel Sambuc if (FL_ISSET(cmdp->iflags, E_C_COUNT))
6084d9c625SLionel Sambuc cnt = cmdp->count;
6184d9c625SLionel Sambuc else
6284d9c625SLionel Sambuc #ifdef HISTORIC_PRACTICE
6384d9c625SLionel Sambuc cnt = O_VAL(sp, O_SCROLL) * 2;
6484d9c625SLionel Sambuc #else
6584d9c625SLionel Sambuc cnt = O_VAL(sp, O_WINDOW) - 1;
6684d9c625SLionel Sambuc #endif
6784d9c625SLionel Sambuc
6884d9c625SLionel Sambuc equals = 0;
6984d9c625SLionel Sambuc eofcheck = 0;
7084d9c625SLionel Sambuc lno = cmdp->addr1.lno;
7184d9c625SLionel Sambuc
7284d9c625SLionel Sambuc switch (FL_ISSET(cmdp->iflags,
7384d9c625SLionel Sambuc E_C_CARAT | E_C_DASH | E_C_DOT | E_C_EQUAL | E_C_PLUS)) {
7484d9c625SLionel Sambuc case E_C_CARAT: /* Display cnt * 2 before the line. */
7584d9c625SLionel Sambuc eofcheck = 1;
7684d9c625SLionel Sambuc if (lno > cnt * 2)
7784d9c625SLionel Sambuc cmdp->addr1.lno = (lno - cnt * 2) + 1;
7884d9c625SLionel Sambuc else
7984d9c625SLionel Sambuc cmdp->addr1.lno = 1;
8084d9c625SLionel Sambuc cmdp->addr2.lno = (cmdp->addr1.lno + cnt) - 1;
8184d9c625SLionel Sambuc break;
8284d9c625SLionel Sambuc case E_C_DASH: /* Line goes at the bottom of the screen. */
8384d9c625SLionel Sambuc cmdp->addr1.lno = lno > cnt ? (lno - cnt) + 1 : 1;
8484d9c625SLionel Sambuc cmdp->addr2.lno = lno;
8584d9c625SLionel Sambuc break;
8684d9c625SLionel Sambuc case E_C_DOT: /* Line goes in the middle of the screen. */
8784d9c625SLionel Sambuc /*
8884d9c625SLionel Sambuc * !!!
8984d9c625SLionel Sambuc * Historically, the "middleness" of the line overrode the
9084d9c625SLionel Sambuc * count, so that "3z.19" or "3z.20" would display the first
9184d9c625SLionel Sambuc * 12 lines of the file, i.e. (N - 1) / 2 lines before and
9284d9c625SLionel Sambuc * after the specified line.
9384d9c625SLionel Sambuc */
9484d9c625SLionel Sambuc eofcheck = 1;
9584d9c625SLionel Sambuc cnt = (cnt - 1) / 2;
9684d9c625SLionel Sambuc cmdp->addr1.lno = lno > cnt ? lno - cnt : 1;
9784d9c625SLionel Sambuc cmdp->addr2.lno = lno + cnt;
9884d9c625SLionel Sambuc
9984d9c625SLionel Sambuc /*
10084d9c625SLionel Sambuc * !!!
10184d9c625SLionel Sambuc * Historically, z. set the absolute cursor mark.
10284d9c625SLionel Sambuc */
10384d9c625SLionel Sambuc abm.lno = sp->lno;
10484d9c625SLionel Sambuc abm.cno = sp->cno;
10584d9c625SLionel Sambuc (void)mark_set(sp, ABSMARK1, &abm, 1);
10684d9c625SLionel Sambuc break;
10784d9c625SLionel Sambuc case E_C_EQUAL: /* Center with hyphens. */
10884d9c625SLionel Sambuc /*
10984d9c625SLionel Sambuc * !!!
11084d9c625SLionel Sambuc * Strangeness. The '=' flag is like the '.' flag (see the
11184d9c625SLionel Sambuc * above comment, it applies here as well) but with a special
11284d9c625SLionel Sambuc * little hack. Print out lines of hyphens before and after
11384d9c625SLionel Sambuc * the specified line. Additionally, the cursor remains set
11484d9c625SLionel Sambuc * on that line.
11584d9c625SLionel Sambuc */
11684d9c625SLionel Sambuc eofcheck = 1;
11784d9c625SLionel Sambuc cnt = (cnt - 1) / 2;
11884d9c625SLionel Sambuc cmdp->addr1.lno = lno > cnt ? lno - cnt : 1;
11984d9c625SLionel Sambuc cmdp->addr2.lno = lno - 1;
12084d9c625SLionel Sambuc if (ex_pr(sp, cmdp))
12184d9c625SLionel Sambuc return (1);
12284d9c625SLionel Sambuc (void)ex_puts(sp, "----------------------------------------\n");
12384d9c625SLionel Sambuc cmdp->addr2.lno = cmdp->addr1.lno = equals = lno;
12484d9c625SLionel Sambuc if (ex_pr(sp, cmdp))
12584d9c625SLionel Sambuc return (1);
12684d9c625SLionel Sambuc (void)ex_puts(sp, "----------------------------------------\n");
12784d9c625SLionel Sambuc cmdp->addr1.lno = lno + 1;
12884d9c625SLionel Sambuc cmdp->addr2.lno = (lno + cnt) - 1;
12984d9c625SLionel Sambuc break;
13084d9c625SLionel Sambuc default:
13184d9c625SLionel Sambuc /* If no line specified, move to the next one. */
13284d9c625SLionel Sambuc if (F_ISSET(cmdp, E_ADDR_DEF))
13384d9c625SLionel Sambuc ++lno;
13484d9c625SLionel Sambuc /* FALLTHROUGH */
13584d9c625SLionel Sambuc case E_C_PLUS: /* Line goes at the top of the screen. */
13684d9c625SLionel Sambuc eofcheck = 1;
13784d9c625SLionel Sambuc cmdp->addr1.lno = lno;
13884d9c625SLionel Sambuc cmdp->addr2.lno = (lno + cnt) - 1;
13984d9c625SLionel Sambuc break;
14084d9c625SLionel Sambuc }
14184d9c625SLionel Sambuc
14284d9c625SLionel Sambuc if (eofcheck) {
14384d9c625SLionel Sambuc if (db_last(sp, &lno))
14484d9c625SLionel Sambuc return (1);
14584d9c625SLionel Sambuc if (cmdp->addr2.lno > lno)
14684d9c625SLionel Sambuc cmdp->addr2.lno = lno;
14784d9c625SLionel Sambuc }
14884d9c625SLionel Sambuc
14984d9c625SLionel Sambuc if (ex_pr(sp, cmdp))
15084d9c625SLionel Sambuc return (1);
15184d9c625SLionel Sambuc if (equals)
15284d9c625SLionel Sambuc sp->lno = equals;
15384d9c625SLionel Sambuc return (0);
15484d9c625SLionel Sambuc }
155