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.
13 static const char sccsid
[] = "$Id: ex_print.c,v 10.24 2001/07/29 19:07:29 skimo Exp $ (Berkeley) $Date: 2001/07/29 19:07:29 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
31 #include "../common/common.h"
33 static int ex_prchars
__P((SCR
*, const CHAR_T
*, size_t *, size_t,
37 * ex_list -- :[line [,line]] l[ist] [count] [flags]
39 * Display the addressed lines such that the output is unambiguous.
41 * PUBLIC: int ex_list __P((SCR *, EXCMD *));
44 ex_list(SCR
*sp
, EXCMD
*cmdp
)
46 if (ex_print(sp
, cmdp
,
47 &cmdp
->addr1
, &cmdp
->addr2
, cmdp
->iflags
| E_C_LIST
))
49 sp
->lno
= cmdp
->addr2
.lno
;
50 sp
->cno
= cmdp
->addr2
.cno
;
55 * ex_number -- :[line [,line]] nu[mber] [count] [flags]
57 * Display the addressed lines with a leading line number.
59 * PUBLIC: int ex_number __P((SCR *, EXCMD *));
62 ex_number(SCR
*sp
, EXCMD
*cmdp
)
64 if (ex_print(sp
, cmdp
,
65 &cmdp
->addr1
, &cmdp
->addr2
, cmdp
->iflags
| E_C_HASH
))
67 sp
->lno
= cmdp
->addr2
.lno
;
68 sp
->cno
= cmdp
->addr2
.cno
;
73 * ex_pr -- :[line [,line]] p[rint] [count] [flags]
75 * Display the addressed lines.
77 * PUBLIC: int ex_pr __P((SCR *, EXCMD *));
80 ex_pr(SCR
*sp
, EXCMD
*cmdp
)
82 if (ex_print(sp
, cmdp
, &cmdp
->addr1
, &cmdp
->addr2
, cmdp
->iflags
))
84 sp
->lno
= cmdp
->addr2
.lno
;
85 sp
->cno
= cmdp
->addr2
.cno
;
91 * Print the selected lines.
93 * PUBLIC: int ex_print __P((SCR *, EXCMD *, MARK *, MARK *, u_int32_t));
96 ex_print(SCR
*sp
, EXCMD
*cmdp
, MARK
*fp
, MARK
*tp
, u_int32_t flags
)
109 for (from
= fp
->lno
, to
= tp
->lno
; from
<= to
; ++from
) {
113 * Display the line number. The %6 format is specified
114 * by POSIX 1003.2, and is almost certainly large enough.
115 * Check, though, just in case.
117 if (LF_ISSET(E_C_HASH
)) {
118 if (from
<= 999999) {
119 SPRINTF(buf
, SIZE(buf
), L("%6ld "), from
);
123 if (ex_prchars(sp
, p
, &col
, 8, 0, 0))
128 * Display the line. The format for E_C_PRINT isn't very good,
129 * especially in handling end-of-line tabs, but they're almost
130 * backward compatible.
132 if (db_get(sp
, from
, DBG_FATAL
, &p
, &len
))
135 if (len
== 0 && !LF_ISSET(E_C_LIST
))
136 (void)ex_puts(sp
, "\n");
137 else if (ex_ldisplay(sp
, p
, len
, col
, flags
))
148 * Display a line without any preceding number.
150 * PUBLIC: int ex_ldisplay __P((SCR *, const CHAR_T *, size_t, size_t, u_int));
153 ex_ldisplay(SCR
*sp
, const CHAR_T
*p
, size_t len
, size_t col
, u_int flags
)
155 if (len
> 0 && ex_prchars(sp
, p
, &col
, len
, LF_ISSET(E_C_LIST
), 0))
157 if (!INTERRUPTED(sp
) && LF_ISSET(E_C_LIST
)) {
159 if (ex_prchars(sp
, p
, &col
, 1, LF_ISSET(E_C_LIST
), 0))
162 if (!INTERRUPTED(sp
))
163 (void)ex_puts(sp
, "\n");
169 * Display a line for the substitute with confirmation routine.
171 * PUBLIC: int ex_scprint __P((SCR *, MARK *, MARK *));
174 ex_scprint(SCR
*sp
, MARK
*fp
, MARK
*tp
)
180 if (O_ISSET(sp
, O_NUMBER
)) {
182 if (ex_prchars(sp
, p
, &col
, 8, 0, 0))
186 if (db_get(sp
, fp
->lno
, DBG_FATAL
, &p
, &len
))
189 if (ex_prchars(sp
, p
, &col
, fp
->cno
, 0, ' '))
193 p
, &col
, tp
->cno
== fp
->cno
? 1 : tp
->cno
- fp
->cno
, 0, '^'))
197 p
= L("[ynq]"); /* XXX: should be msg_cat. */
198 if (ex_prchars(sp
, p
, &col
, 5, 0, 0))
206 * Local routine to dump characters to the screen.
209 ex_prchars(SCR
*sp
, const CHAR_T
*p
, size_t *colp
, size_t len
,
210 u_int flags
, int repeatc
)
215 size_t col
, tlen
, ts
;
217 if (O_ISSET(sp
, O_LIST
))
220 ts
= O_VAL(sp
, O_TABSTOP
);
221 for (col
= *colp
; len
--;)
222 if ((ch
= *p
++) == L('\t') && !LF_ISSET(E_C_LIST
))
223 for (tlen
= ts
- col
% ts
;
224 col
< sp
->cols
&& tlen
--; ++col
) {
226 "%c", repeatc
? repeatc
: ' ');
233 CHAR_T str
[2] = {0, 0};
235 INT2CHAR(sp
, str
, 2, kp
, tlen
);
237 kp
= KEY_NAME(sp
, ch
);
238 tlen
= KEY_LEN(sp
, ch
);
240 if (!repeatc
&& col
+ tlen
< sp
->cols
) {
241 (void)ex_puts(sp
, kp
);
244 for (; tlen
--; ++kp
, ++col
) {
245 if (col
== sp
->cols
) {
247 (void)ex_puts(sp
, "\n");
250 "%c", repeatc
? repeatc
: *kp
);
261 * Ex's version of printf.
263 * PUBLIC: int ex_printf __P((SCR *, const char *, ...));
267 ex_printf(SCR
*sp
, const char *fmt
, ...)
269 ex_printf(sp
, fmt
, va_alist
)
287 exp
->obp_len
+= n
= vsnprintf(exp
->obp
+ exp
->obp_len
,
288 sizeof(exp
->obp
) - exp
->obp_len
, fmt
, ap
);
291 /* Flush when reach a <newline> or half the buffer. */
292 if (exp
->obp
[exp
->obp_len
- 1] == '\n' ||
293 exp
->obp_len
> sizeof(exp
->obp
) / 2)
300 * Ex's version of puts.
302 * PUBLIC: int ex_puts __P((SCR *, const char *));
305 ex_puts(SCR
*sp
, const char *str
)
312 /* Flush when reach a <newline> or the end of the buffer. */
313 for (doflush
= n
= 0; *str
!= '\0'; ++n
) {
314 if (exp
->obp_len
> sizeof(exp
->obp
))
316 if ((exp
->obp
[exp
->obp_len
++] = *str
++) == '\n')
326 * Ex's version of fflush.
328 * PUBLIC: int ex_fflush __P((SCR *sp));
337 if (exp
->obp_len
!= 0) {
338 sp
->wp
->scr_msg(sp
, M_NONE
, exp
->obp
, exp
->obp_len
);