2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
23 #include "../common/common.h"
25 static int ex_prchars(SCR *,
26 const CHAR_T *, size_t *, size_t, u_int, int);
29 * ex_list -- :[line [,line]] l[ist] [count] [flags]
31 * Display the addressed lines such that the output is unambiguous.
33 * PUBLIC: int ex_list(SCR *, EXCMD *);
36 ex_list(SCR *sp, EXCMD *cmdp)
38 if (ex_print(sp, cmdp,
39 &cmdp->addr1, &cmdp->addr2, cmdp->iflags | E_C_LIST))
41 sp->lno = cmdp->addr2.lno;
42 sp->cno = cmdp->addr2.cno;
47 * ex_number -- :[line [,line]] nu[mber] [count] [flags]
49 * Display the addressed lines with a leading line number.
51 * PUBLIC: int ex_number(SCR *, EXCMD *);
54 ex_number(SCR *sp, EXCMD *cmdp)
56 if (ex_print(sp, cmdp,
57 &cmdp->addr1, &cmdp->addr2, cmdp->iflags | E_C_HASH))
59 sp->lno = cmdp->addr2.lno;
60 sp->cno = cmdp->addr2.cno;
65 * ex_pr -- :[line [,line]] p[rint] [count] [flags]
67 * Display the addressed lines.
69 * PUBLIC: int ex_pr(SCR *, EXCMD *);
72 ex_pr(SCR *sp, EXCMD *cmdp)
74 if (ex_print(sp, cmdp, &cmdp->addr1, &cmdp->addr2, cmdp->iflags))
76 sp->lno = cmdp->addr2.lno;
77 sp->cno = cmdp->addr2.cno;
83 * Print the selected lines.
85 * PUBLIC: int ex_print(SCR *, EXCMD *, MARK *, MARK *, u_int32_t);
88 ex_print(SCR *sp, EXCMD *cmdp, MARK *fp, MARK *tp, u_int32_t flags)
99 for (from = fp->lno, to = tp->lno; from <= to; ++from) {
103 * Display the line number. The %6 format is specified
104 * by POSIX 1003.2, and is almost certainly large enough.
105 * Check, though, just in case.
107 if (LF_ISSET(E_C_HASH)) {
108 if (from <= 999999) {
109 SPRINTF(buf, SIZE(buf), L("%6u "), from);
113 if (ex_prchars(sp, p, &col, 8, 0, 0))
118 * Display the line. The format for E_C_PRINT isn't very good,
119 * especially in handling end-of-line tabs, but they're almost
120 * backward compatible.
122 if (db_get(sp, from, DBG_FATAL, &p, &len))
125 if (len == 0 && !LF_ISSET(E_C_LIST))
126 (void)ex_puts(sp, "\n");
127 else if (ex_ldisplay(sp, p, len, col, flags))
138 * Display a line without any preceding number.
140 * PUBLIC: int ex_ldisplay(SCR *, const CHAR_T *, size_t, size_t, u_int);
143 ex_ldisplay(SCR *sp, const CHAR_T *p, size_t len, size_t col, u_int flags)
145 if (len > 0 && ex_prchars(sp, p, &col, len, LF_ISSET(E_C_LIST), 0))
147 if (!INTERRUPTED(sp) && LF_ISSET(E_C_LIST)) {
149 if (ex_prchars(sp, p, &col, 1, LF_ISSET(E_C_LIST), 0))
152 if (!INTERRUPTED(sp))
153 (void)ex_puts(sp, "\n");
159 * Display a line for the substitute with confirmation routine.
161 * PUBLIC: int ex_scprint(SCR *, MARK *, MARK *);
164 ex_scprint(SCR *sp, MARK *fp, MARK *tp)
170 if (O_ISSET(sp, O_NUMBER)) {
172 if (ex_prchars(sp, p, &col, 8, 0, 0))
176 if (db_get(sp, fp->lno, DBG_FATAL, &p, &len))
179 if (ex_prchars(sp, p, &col, fp->cno, 0, ' '))
183 p, &col, tp->cno == fp->cno ? 1 : tp->cno - fp->cno, 0, '^'))
187 p = L("[ynq]"); /* XXX: should be msg_cat. */
188 if (ex_prchars(sp, p, &col, 5, 0, 0))
196 * Local routine to dump characters to the screen.
199 ex_prchars(SCR *sp, const CHAR_T *p, size_t *colp, size_t len,
200 u_int flags, int repeatc)
205 size_t col, tlen, ts;
207 if (O_ISSET(sp, O_LIST))
210 ts = O_VAL(sp, O_TABSTOP);
211 for (col = *colp; len--;)
212 if ((ch = *p++) == '\t' && !LF_ISSET(E_C_LIST))
213 for (tlen = ts - col % ts;
214 col < sp->cols && tlen--; ++col) {
216 "%c", repeatc ? repeatc : ' ');
221 kp = KEY_NAME(sp, ch);
222 tlen = KEY_COL(sp, ch);
225 * Start a new line if the last character does not fit
226 * into the current line. The implicit new lines are
229 if (col + tlen > sp->cols) {
231 (void)ex_puts(sp, "\n");
236 (void)ex_puts(sp, kp);
239 } else while (tlen--) {
240 (void)ex_printf(sp, "%c", repeatc);
244 if (col == sp->cols) {
246 (void)ex_puts(sp, "\n");
255 * Ex's version of printf.
257 * PUBLIC: int ex_printf(SCR *, const char *, ...);
272 exp->obp_len += n = vsnprintf(exp->obp + exp->obp_len,
273 sizeof(exp->obp) - exp->obp_len, fmt, ap);
276 /* Flush when reach a <newline> or half the buffer. */
277 if (exp->obp[exp->obp_len - 1] == '\n' ||
278 exp->obp_len > sizeof(exp->obp) / 2)
285 * Ex's version of puts.
287 * PUBLIC: int ex_puts(SCR *, const char *);
290 ex_puts(SCR *sp, const char *str)
297 /* Flush when reach a <newline> or the end of the buffer. */
298 for (doflush = n = 0; *str != '\0'; ++n) {
299 if (exp->obp_len > sizeof(exp->obp))
301 if ((exp->obp[exp->obp_len++] = *str++) == '\n')
311 * Ex's version of fflush.
313 * PUBLIC: int ex_fflush(SCR *sp);
322 if (exp->obp_len != 0) {
323 sp->gp->scr_msg(sp, M_NONE, exp->obp, exp->obp_len);