1 /* $NetBSD: vs_msg.c,v 1.5 2013/12/01 02:34:54 christos Exp $ */
3 * Copyright (c) 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
14 static const char sccsid
[] = "Id: vs_msg.c,v 10.85 2001/07/29 19:07:31 skimo Exp (Berkeley) Date: 2001/07/29 19:07:31 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
28 #include "../common/common.h"
32 SCROLL_W
, /* User wait. */
33 SCROLL_W_EX
, /* User wait, or enter : to continue. */
34 SCROLL_W_QUIT
/* User wait, or enter q to quit. */
36 * SCROLL_W_QUIT has another semantic
37 * -- only wait if the screen is full
41 static void vs_divider
__P((SCR
*));
42 static void vs_msgsave
__P((SCR
*, mtype_t
, char *, size_t));
43 static void vs_output
__P((SCR
*, mtype_t
, const char *, int));
44 static void vs_scroll
__P((SCR
*, int *, sw_t
));
45 static void vs_wait
__P((SCR
*, int *, sw_t
));
49 * Display, update or clear a busy message.
51 * This routine is the default editor interface for vi busy messages. It
52 * implements a standard strategy of stealing lines from the bottom of the
53 * vi text screen. Screens using an alternate method of displaying busy
54 * messages, e.g. X11 clock icons, should set their scr_busy function to the
55 * correct function before calling the main editor routine.
57 * PUBLIC: void vs_busy __P((SCR *, const char *, busy_t));
60 vs_busy(SCR
*sp
, const char *msg
, busy_t btype
)
64 static const char flagc
[] = "|/-\\";
69 /* Ex doesn't display busy messages. */
70 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
))
77 * Most of this routine is to deal with the screen sharing real estate
78 * between the normal edit messages and the busy messages. Logically,
79 * all that's needed is something that puts up a message, periodically
80 * updates it, and then goes away.
85 if (vip
->totalcount
!= 0 || vip
->busy_ref
!= 1)
88 /* Initialize state for updates. */
90 (void)gettimeofday(&vip
->busy_tv
, NULL
);
92 /* Save the current cursor. */
93 (void)gp
->scr_cursor(sp
, &vip
->busy_oldy
, &vip
->busy_oldx
);
95 /* Display the busy message. */
96 p
= msg_cat(sp
, msg
, &len
);
97 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
98 (void)gp
->scr_addstr(sp
, p
, len
);
99 (void)gp
->scr_cursor(sp
, ¬used
, &vip
->busy_fx
);
100 (void)gp
->scr_clrtoeol(sp
);
101 (void)gp
->scr_move(sp
, LASTLINE(sp
), vip
->busy_fx
);
104 if (vip
->busy_ref
== 0)
109 * If the line isn't in use for another purpose, clear it.
110 * Always return to the original position.
112 if (vip
->totalcount
== 0 && vip
->busy_ref
== 0) {
113 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
114 (void)gp
->scr_clrtoeol(sp
);
116 (void)gp
->scr_move(sp
, vip
->busy_oldy
, vip
->busy_oldx
);
119 if (vip
->totalcount
!= 0 || vip
->busy_ref
== 0)
122 /* Update no more than every 1/8 of a second. */
123 (void)gettimeofday(&tv
, NULL
);
124 if (((tv
.tv_sec
- vip
->busy_tv
.tv_sec
) * 1000000 +
125 (tv
.tv_usec
- vip
->busy_tv
.tv_usec
)) < 125000)
129 /* Display the update. */
130 if (vip
->busy_ch
== sizeof(flagc
) - 1)
132 (void)gp
->scr_move(sp
, LASTLINE(sp
), vip
->busy_fx
);
133 (void)gp
->scr_addstr(sp
, flagc
+ vip
->busy_ch
++, 1);
134 (void)gp
->scr_move(sp
, LASTLINE(sp
), vip
->busy_fx
);
137 (void)gp
->scr_refresh(sp
, 0);
142 * Home the cursor to the bottom row, left-most column.
144 * PUBLIC: void vs_home __P((SCR *));
149 (void)sp
->gp
->scr_move(sp
, LASTLINE(sp
), 0);
150 (void)sp
->gp
->scr_refresh(sp
, 0);
157 * PUBLIC: void vs_update __P((SCR *, const char *, const CHAR_T *));
160 vs_update(SCR
*sp
, const char *m1
, const CHAR_T
*m2
)
163 size_t len
, mlen
, oldx
, oldy
;
170 * This routine displays a message on the bottom line of the screen,
171 * without updating any of the command structures that would keep it
172 * there for any period of time, i.e. it is overwritten immediately.
174 * It's used by the ex read and ! commands when the user's command is
175 * expanded, and by the ex substitution confirmation prompt.
177 if (F_ISSET(sp
, SC_SCR_EXWROTE
)) {
179 INT2CHAR(sp
, m2
, STRLEN(m2
) + 1, np
, nlen
);
181 "%s%s\n", m1
== NULL
? "" : m1
, m2
== NULL
? "" : np
);
186 * Save the cursor position, the substitute-with-confirmation code
187 * will have already set it correctly.
189 (void)gp
->scr_cursor(sp
, &oldy
, &oldx
);
191 /* Clear the bottom line. */
192 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
193 (void)gp
->scr_clrtoeol(sp
);
197 * Don't let long file names screw up the screen.
200 mlen
= len
= strlen(m1
);
201 if (len
> sp
->cols
- 2)
202 mlen
= len
= sp
->cols
- 2;
203 (void)gp
->scr_addstr(sp
, m1
, mlen
);
208 if (len
+ mlen
> sp
->cols
- 2)
209 mlen
= (sp
->cols
- 2) - len
;
210 (void)gp
->scr_waddstr(sp
, m2
, mlen
);
213 (void)gp
->scr_move(sp
, oldy
, oldx
);
214 (void)gp
->scr_refresh(sp
, 0);
219 * Display ex output or error messages for the screen.
221 * This routine is the default editor interface for all ex output, and all ex
222 * and vi error/informational messages. It implements the standard strategy
223 * of stealing lines from the bottom of the vi text screen. Screens using an
224 * alternate method of displaying messages, e.g. dialog boxes, should set their
225 * scr_msg function to the correct function before calling the editor.
227 * PUBLIC: void vs_msg __P((SCR *, mtype_t, char *, size_t));
230 vs_msg(SCR
*sp
, mtype_t mtype
, char *line
, size_t len
)
234 size_t maxcols
, oldx
, oldy
, padding
;
235 const char *e
, *s
, *t
;
241 * Ring the bell if it's scheduled.
244 * Shouldn't we save this, too?
246 if (F_ISSET(sp
, SC_TINPUT_INFO
) || F_ISSET(gp
, G_BELLSCHED
)) {
247 if (F_ISSET(sp
, SC_SCR_VI
)) {
248 F_CLR(gp
, G_BELLSCHED
);
249 (void)gp
->scr_bell(sp
);
251 F_SET(gp
, G_BELLSCHED
);
255 * If vi is using the error line for text input, there's no screen
256 * real-estate for the error message. Nothing to do without some
257 * information as to how important the error message is.
259 if (F_ISSET(sp
, SC_TINPUT_INFO
))
263 * Ex or ex controlled screen output.
265 * If output happens during startup, e.g., a .exrc file, we may be
266 * in ex mode but haven't initialized the screen. Initialize here,
267 * and in this case, stay in ex mode.
269 * If the SC_SCR_EXWROTE bit is set, then we're switching back and
270 * forth between ex and vi, but the screen is trashed and we have
271 * to respect that. Switch to ex mode long enough to put out the
274 * If the SC_EX_WAIT_NO bit is set, turn it off -- we're writing to
275 * the screen, so previous opinions are ignored.
277 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
)) {
278 if (!F_ISSET(sp
, SC_SCR_EX
)) {
279 if (F_ISSET(sp
, SC_SCR_EXWROTE
)) {
280 if (sp
->gp
->scr_screen(sp
, SC_EX
))
288 (void)gp
->scr_attr(sp
, SA_INVERSE
, 1);
289 (void)printf("%.*s", (int)len
, line
);
291 (void)gp
->scr_attr(sp
, SA_INVERSE
, 0);
292 (void)fflush(stdout
);
294 F_CLR(sp
, SC_EX_WAIT_NO
);
296 if (!F_ISSET(sp
, SC_SCR_EX
))
297 (void)sp
->gp
->scr_screen(sp
, SC_VI
);
301 /* If the vi screen isn't ready, save the message. */
302 if (!F_ISSET(sp
, SC_SCR_VI
)) {
303 (void)vs_msgsave(sp
, mtype
, line
, len
);
307 /* Save the cursor position. */
308 (void)gp
->scr_cursor(sp
, &oldy
, &oldx
);
310 /* If it's an ex output message, just write it out. */
311 if (mtype
== M_NONE
) {
312 vs_output(sp
, mtype
, line
, len
);
317 * If it's a vi message, strip the trailing <newline> so we can
318 * try and paste messages together.
320 if (line
[len
- 1] == '\n')
324 * If a message won't fit on a single line, try to split on a <blank>.
325 * If a subsequent message fits on the same line, write a separator
326 * and output it. Otherwise, put out a newline.
328 * Need up to two padding characters normally; a semi-colon and a
329 * separating space. If only a single line on the screen, add some
330 * more for the trailing continuation message.
333 * Assume that periods and semi-colons take up a single column on the
337 * There are almost certainly pathological cases that will break this
341 (void)msg_cmsg(sp
, CMSG_CONT_S
, &padding
);
346 maxcols
= sp
->cols
- 1;
347 if (vip
->lcontinue
!= 0) {
348 if (len
+ vip
->lcontinue
+ padding
> maxcols
)
349 vs_output(sp
, vip
->mtype
, ".\n", 2);
351 vs_output(sp
, vip
->mtype
, ";", 1);
352 vs_output(sp
, M_NONE
, " ", 1);
356 for (s
= line
;; s
= t
) {
357 for (; len
> 0 && isblank((unsigned char)*s
); --len
, ++s
);
360 if (len
+ vip
->lcontinue
> maxcols
) {
361 for (e
= s
+ (maxcols
- vip
->lcontinue
);
362 e
> s
&& !isblank((unsigned char)*e
); --e
);
364 e
= t
= s
+ (maxcols
- vip
->lcontinue
);
366 for (t
= e
; isblank((unsigned char)e
[-1]); --e
);
371 * If the message ends in a period, discard it, we want to
372 * gang messages where possible.
375 if (len
== 0 && (e
- s
) > 1 && s
[(e
- s
) - 1] == '.')
377 vs_output(sp
, mtype
, s
, e
- s
);
380 vs_output(sp
, M_NONE
, "\n", 1);
386 ret
: (void)gp
->scr_move(sp
, oldy
, oldx
);
387 (void)gp
->scr_refresh(sp
, 0);
392 * Output the text to the screen.
395 vs_output(SCR
*sp
, mtype_t mtype
, const char *line
, int llen
)
400 size_t chlen
, notused
;
403 char *cbp
, *ecbp
, cbuf
[128];
407 for (p
= line
; llen
> 0;) {
408 /* Get the next physical line. */
409 if ((p
= memchr(line
, '\n', llen
)) == NULL
)
415 * The max is sp->cols characters, and we may have already
416 * written part of the line.
418 if (len
+ vip
->lcontinue
> sp
->cols
)
419 len
= sp
->cols
- vip
->lcontinue
;
422 * If the first line output, do nothing. If the second line
423 * output, draw the divider line. If drew a full screen, we
424 * remove the divider line. If it's a continuation line, move
425 * to the continuation point, else, move the screen up.
427 if (vip
->lcontinue
== 0) {
428 if (!IS_ONELINE(sp
)) {
429 if (vip
->totalcount
== 1) {
430 (void)gp
->scr_move(sp
,
431 LASTLINE(sp
) - 1, 0);
432 (void)gp
->scr_clrtoeol(sp
);
433 (void)vs_divider(sp
);
434 F_SET(vip
, VIP_DIVIDER
);
438 if (vip
->totalcount
== sp
->t_maxrows
&&
439 F_ISSET(vip
, VIP_DIVIDER
)) {
442 F_CLR(vip
, VIP_DIVIDER
);
445 if (vip
->totalcount
!= 0)
446 vs_scroll(sp
, NULL
, SCROLL_W_QUIT
);
448 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
455 (void)gp
->scr_move(sp
, LASTLINE(sp
), vip
->lcontinue
);
457 /* Error messages are in inverse video. */
459 (void)gp
->scr_attr(sp
, SA_INVERSE
, 1);
461 /* Display the line, doing character translation. */
464 (void)gp->scr_addstr(sp, cbuf, cbp - cbuf); \
467 ecbp
= (cbp
= cbuf
) + sizeof(cbuf
) - 1;
468 for (t
= line
, tlen
= len
; tlen
--; ++t
) {
471 * Replace tabs with spaces, there are places in
472 * ex that do column calculations without looking
473 * at <tabs> -- and all routines that care about
474 * <tabs> do their own expansions. This catches
475 * <tabs> in things like tag search strings.
479 chlen
= KEY_LEN(sp
, ch
);
480 if (cbp
+ chlen
>= ecbp
)
482 for (kp
= KEY_NAME(sp
, ch
); chlen
--;)
488 (void)gp
->scr_attr(sp
, SA_INVERSE
, 0);
490 /* Clear the rest of the line. */
491 (void)gp
->scr_clrtoeol(sp
);
493 /* If we loop, it's a new line. */
496 /* Reset for the next line. */
505 /* Set up next continuation line. */
507 gp
->scr_cursor(sp
, ¬used
, &vip
->lcontinue
);
512 * Deal with ex message output.
514 * This routine is called when exiting a colon command to resolve any ex
515 * output that may have occurred.
517 * PUBLIC: int vs_ex_resolve __P((SCR *, int *));
520 vs_ex_resolve(SCR
*sp
, int *continuep
)
531 /* If we ran any ex command, we can't trust the cursor position. */
532 F_SET(vip
, VIP_CUR_INVALID
);
534 /* Terminate any partially written message. */
535 if (vip
->lcontinue
!= 0) {
536 vs_output(sp
, vip
->mtype
, ".", 1);
543 * If we switched out of the vi screen into ex, switch back while we
544 * figure out what to do with the screen and potentially get another
545 * command to execute.
547 * If we didn't switch into ex, we're not required to wait, and less
548 * than 2 lines of output, we can continue without waiting for the
551 * Note, all other code paths require waiting, so we leave the report
552 * of modified lines until later, so that we won't wait for no other
553 * reason than a threshold number of lines were modified. This means
554 * we display cumulative line modification reports for groups of ex
555 * commands. That seems right to me (well, at least not wrong).
557 if (F_ISSET(sp
, SC_SCR_EXWROTE
)) {
558 if (sp
->gp
->scr_screen(sp
, SC_VI
))
561 if (!F_ISSET(sp
, SC_EX_WAIT_YES
) && vip
->totalcount
< 2) {
562 F_CLR(sp
, SC_EX_WAIT_NO
);
566 /* Clear the required wait flag, it's no longer needed. */
567 F_CLR(sp
, SC_EX_WAIT_YES
);
570 * Wait, unless explicitly told not to wait or the user interrupted
571 * the command. If the user is leaving the screen, for any reason,
572 * they can't continue with further ex commands.
574 if (!F_ISSET(sp
, SC_EX_WAIT_NO
) && !INTERRUPTED(sp
)) {
575 wtype
= F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
|
576 SC_FSWITCH
| SC_SSWITCH
) ? SCROLL_W
: SCROLL_W_EX
;
577 if (F_ISSET(sp
, SC_SCR_EXWROTE
))
578 vs_wait(sp
, continuep
, wtype
);
580 vs_scroll(sp
, continuep
, wtype
);
585 /* If ex wrote on the screen, refresh the screen image. */
586 if (F_ISSET(sp
, SC_SCR_EXWROTE
))
587 F_SET(vip
, VIP_N_EX_PAINT
);
590 * If we're not the bottom of the split screen stack, the screen
591 * image itself is wrong, so redraw everything.
593 if (TAILQ_NEXT(sp
, q
) != NULL
)
594 F_SET(sp
, SC_SCR_REDRAW
);
596 /* If ex changed the underlying file, the map itself is wrong. */
597 if (F_ISSET(vip
, VIP_N_EX_REDRAW
))
598 F_SET(sp
, SC_SCR_REFORMAT
);
600 /* Ex may have switched out of the alternate screen, return. */
601 (void)gp
->scr_attr(sp
, SA_ALTERNATE
, 1);
604 * Whew. We're finally back home, after what feels like years.
607 F_CLR(sp
, SC_SCR_EXWROTE
| SC_EX_WAIT_NO
);
610 * We may need to repaint some of the screen, e.g.:
615 * gives us a combination of some lines that are "wrong", and a need
616 * for a full refresh.
618 if (vip
->totalcount
> 1) {
619 /* Set up the redraw of the overwritten lines. */
620 ev
.e_event
= E_REPAINT
;
621 ev
.e_flno
= vip
->totalcount
>=
622 sp
->rows
? 1 : sp
->rows
- vip
->totalcount
;
623 ev
.e_tlno
= sp
->rows
;
625 /* Reset the count of overwriting lines. */
626 vip
->linecount
= vip
->lcontinue
= vip
->totalcount
= 0;
629 (void)v_erepaint(sp
, &ev
);
631 /* Reset the count of overwriting lines. */
632 vip
->linecount
= vip
->lcontinue
= vip
->totalcount
= 0;
639 * Deal with message output.
641 * PUBLIC: int vs_resolve __P((SCR *, SCR *, int));
644 vs_resolve(SCR
*sp
, SCR
*csp
, int forcewait
)
655 * Vs_resolve is called from the main vi loop and the refresh function
656 * to periodically ensure that the user has seen any messages that have
657 * been displayed and that any status lines are correct. The sp screen
658 * is the screen we're checking, usually the current screen. When it's
659 * not, csp is the current screen, used for final cursor positioning.
667 /* Save the cursor position. */
668 (void)gp
->scr_cursor(csp
, &oldy
, &oldx
);
670 /* Ring the bell if it's scheduled. */
671 if (F_ISSET(gp
, G_BELLSCHED
)) {
672 F_CLR(gp
, G_BELLSCHED
);
673 (void)gp
->scr_bell(sp
);
676 /* Display new file status line. */
677 if (F_ISSET(sp
, SC_STATUS
)) {
678 F_CLR(sp
, SC_STATUS
);
679 msgq_status(sp
, sp
->lno
, MSTAT_TRUNCATE
);
682 /* Report on line modifications. */
686 * Flush any saved messages. If the screen isn't ready, refresh
687 * it. (A side-effect of screen refresh is that we can display
688 * messages.) Once this is done, don't trust the cursor. That
689 * extra refresh screwed the pooch.
691 if (!LIST_EMPTY(&gp
->msgq
)) {
692 if (!F_ISSET(sp
, SC_SCR_VI
) && vs_refresh(sp
, 1))
694 while ((mp
= LIST_FIRST(&gp
->msgq
)) != NULL
) {
695 wp
->scr_msg(sp
, mp
->mtype
, mp
->buf
, mp
->len
);
700 F_SET(vip
, VIP_CUR_INVALID
);
703 switch (vip
->totalcount
) {
709 * If we're switching screens, we have to wait for messages,
710 * regardless. If we don't wait, skip updating the modeline.
713 vs_scroll(sp
, NULL
, SCROLL_W
);
715 F_SET(vip
, VIP_S_MODELINE
);
721 * If >1 message line in use, prompt the user to continue and
722 * repaint overwritten lines.
724 vs_scroll(sp
, NULL
, SCROLL_W
);
726 ev
.e_event
= E_REPAINT
;
727 ev
.e_flno
= vip
->totalcount
>=
728 sp
->rows
? 1 : sp
->rows
- vip
->totalcount
;
729 ev
.e_tlno
= sp
->rows
;
735 /* Reset the count of overwriting lines. */
736 vip
->linecount
= vip
->lcontinue
= vip
->totalcount
= 0;
740 (void)v_erepaint(sp
, &ev
);
742 /* Restore the cursor position. */
743 (void)gp
->scr_move(csp
, oldy
, oldx
);
750 * Scroll the screen for output.
753 vs_scroll(SCR
*sp
, int *continuep
, sw_t wtype
)
760 if (!IS_ONELINE(sp
)) {
762 * Scroll the screen. Instead of scrolling the entire screen,
763 * delete the line above the first line output so preserve the
764 * maximum amount of the screen.
766 (void)gp
->scr_move(sp
, vip
->totalcount
<
767 sp
->rows
? LASTLINE(sp
) - vip
->totalcount
: 0, 0);
768 (void)gp
->scr_deleteln(sp
);
770 /* If there are screens below us, push them back into place. */
771 if (TAILQ_NEXT(sp
, q
) != NULL
) {
772 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
773 (void)gp
->scr_insertln(sp
);
776 if (wtype
== SCROLL_W_QUIT
&& vip
->linecount
< sp
->t_maxrows
)
778 vs_wait(sp
, continuep
, wtype
);
783 * Prompt the user to continue.
786 vs_wait(SCR
*sp
, int *continuep
, sw_t wtype
)
797 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
799 p
= msg_cmsg(sp
, CMSG_CONT_S
, &len
);
803 p
= msg_cmsg(sp
, CMSG_CONT_Q
, &len
);
806 p
= msg_cmsg(sp
, CMSG_CONT_EX
, &len
);
809 p
= msg_cmsg(sp
, CMSG_CONT
, &len
);
815 (void)gp
->scr_addstr(sp
, p
, len
);
820 (void)gp
->scr_clrtoeol(sp
);
821 (void)gp
->scr_refresh(sp
, 0);
823 /* Get a single character from the terminal. */
824 if (continuep
!= NULL
)
827 if (v_event_get(sp
, &ev
, 0, 0))
829 if (ev
.e_event
== E_CHARACTER
)
831 if (ev
.e_event
== E_INTERRUPT
) {
833 F_SET(gp
, G_INTERRUPTED
);
836 (void)gp
->scr_bell(sp
);
840 if (ev
.e_c
== CH_QUIT
)
841 F_SET(gp
, G_INTERRUPTED
);
844 if (ev
.e_c
== ':' && continuep
!= NULL
)
854 * Draw a dividing line between the screen and the output.
862 #define DIVIDESTR "+=+=+=+=+=+=+=+"
864 sizeof(DIVIDESTR
) - 1 > sp
->cols
? sp
->cols
: sizeof(DIVIDESTR
) - 1;
866 (void)gp
->scr_attr(sp
, SA_INVERSE
, 1);
867 (void)gp
->scr_addstr(sp
, DIVIDESTR
, len
);
868 (void)gp
->scr_attr(sp
, SA_INVERSE
, 0);
873 * Save a message for later display.
876 vs_msgsave(SCR
*sp
, mtype_t mt
, char *p
, size_t len
)
882 * We have to handle messages before we have any place to put them.
883 * If there's no screen support yet, allocate a msg structure, copy
884 * in the message, and queue it on the global structure. If we can't
885 * allocate memory here, we're genuinely screwed, dump the message
886 * to stderr in the (probably) vain hope that someone will see it.
888 CALLOC_GOTO(sp
, mp_n
, MSGS
*, 1, sizeof(MSGS
));
889 MALLOC_GOTO(sp
, mp_n
->buf
, char *, len
);
891 memmove(mp_n
->buf
, p
, len
);
896 if ((mp_c
= LIST_FIRST(&gp
->msgq
)) == NULL
) {
897 LIST_INSERT_HEAD(&gp
->msgq
, mp_n
, q
);
899 while (LIST_NEXT(mp_c
, q
) != NULL
)
900 mp_c
= LIST_NEXT(mp_c
, q
);
901 LIST_INSERT_AFTER(mp_c
, mp_n
, q
);
908 (void)fprintf(stderr
, "%.*s\n", (int)len
, p
);