2 * Copyright (c) 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: vs_msg.c,v 10.83 2001/06/09 18:26:32 skimo Exp $ (Berkeley) $Date: 2001/06/09 18:26:32 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
27 #include "../common/common.h"
31 SCROLL_W
, /* User wait. */
32 SCROLL_W_EX
, /* User wait, or enter : to continue. */
33 SCROLL_W_QUIT
/* User wait, or enter q to quit. */
35 * SCROLL_W_QUIT has another semantic
36 * -- only wait if the screen is full
40 static void vs_divider
__P((SCR
*));
41 static void vs_msgsave
__P((SCR
*, mtype_t
, char *, size_t));
42 static void vs_output
__P((SCR
*, mtype_t
, const char *, int));
43 static void vs_scroll
__P((SCR
*, int *, sw_t
));
44 static void vs_wait
__P((SCR
*, int *, sw_t
));
48 * Display, update or clear a busy message.
50 * This routine is the default editor interface for vi busy messages. It
51 * implements a standard strategy of stealing lines from the bottom of the
52 * vi text screen. Screens using an alternate method of displaying busy
53 * messages, e.g. X11 clock icons, should set their scr_busy function to the
54 * correct function before calling the main editor routine.
56 * PUBLIC: void vs_busy __P((SCR *, const char *, busy_t));
59 vs_busy(sp
, msg
, btype
)
66 static const char flagc
[] = "|/-\\";
71 /* Ex doesn't display busy messages. */
72 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
))
79 * Most of this routine is to deal with the screen sharing real estate
80 * between the normal edit messages and the busy messages. Logically,
81 * all that's needed is something that puts up a message, periodically
82 * updates it, and then goes away.
87 if (vip
->totalcount
!= 0 || vip
->busy_ref
!= 1)
90 /* Initialize state for updates. */
92 (void)gettimeofday(&vip
->busy_tv
, NULL
);
94 /* Save the current cursor. */
95 (void)gp
->scr_cursor(sp
, &vip
->busy_oldy
, &vip
->busy_oldx
);
97 /* Display the busy message. */
98 p
= msg_cat(sp
, msg
, &len
);
99 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
100 (void)gp
->scr_addstr(sp
, p
, len
);
101 (void)gp
->scr_cursor(sp
, ¬used
, &vip
->busy_fx
);
102 (void)gp
->scr_clrtoeol(sp
);
103 (void)gp
->scr_move(sp
, LASTLINE(sp
), vip
->busy_fx
);
106 if (vip
->busy_ref
== 0)
111 * If the line isn't in use for another purpose, clear it.
112 * Always return to the original position.
114 if (vip
->totalcount
== 0 && vip
->busy_ref
== 0) {
115 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
116 (void)gp
->scr_clrtoeol(sp
);
118 (void)gp
->scr_move(sp
, vip
->busy_oldy
, vip
->busy_oldx
);
121 if (vip
->totalcount
!= 0 || vip
->busy_ref
== 0)
124 /* Update no more than every 1/8 of a second. */
125 (void)gettimeofday(&tv
, NULL
);
126 if (((tv
.tv_sec
- vip
->busy_tv
.tv_sec
) * 1000000 +
127 (tv
.tv_usec
- vip
->busy_tv
.tv_usec
)) < 125000)
131 /* Display the update. */
132 if (vip
->busy_ch
== sizeof(flagc
) - 1)
134 (void)gp
->scr_move(sp
, LASTLINE(sp
), vip
->busy_fx
);
135 (void)gp
->scr_addstr(sp
, flagc
+ vip
->busy_ch
++, 1);
136 (void)gp
->scr_move(sp
, LASTLINE(sp
), vip
->busy_fx
);
139 (void)gp
->scr_refresh(sp
, 0);
144 * Home the cursor to the bottom row, left-most column.
146 * PUBLIC: void vs_home __P((SCR *));
152 (void)sp
->gp
->scr_move(sp
, LASTLINE(sp
), 0);
153 (void)sp
->gp
->scr_refresh(sp
, 0);
160 * PUBLIC: void vs_update __P((SCR *, const char *, const CHAR_T *));
163 vs_update(SCR
*sp
, const char *m1
, const CHAR_T
*m2
)
166 size_t len
, mlen
, oldx
, oldy
;
173 * This routine displays a message on the bottom line of the screen,
174 * without updating any of the command structures that would keep it
175 * there for any period of time, i.e. it is overwritten immediately.
177 * It's used by the ex read and ! commands when the user's command is
178 * expanded, and by the ex substitution confirmation prompt.
180 if (F_ISSET(sp
, SC_SCR_EXWROTE
)) {
182 INT2CHAR(sp
, m2
, STRLEN(m2
) + 1, np
, nlen
);
184 "%s\n", m1
== NULL
? "" : m1
, m2
== NULL
? "" : np
);
189 * Save the cursor position, the substitute-with-confirmation code
190 * will have already set it correctly.
192 (void)gp
->scr_cursor(sp
, &oldy
, &oldx
);
194 /* Clear the bottom line. */
195 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
196 (void)gp
->scr_clrtoeol(sp
);
200 * Don't let long file names screw up the screen.
203 mlen
= len
= strlen(m1
);
204 if (len
> sp
->cols
- 2)
205 mlen
= len
= sp
->cols
- 2;
206 (void)gp
->scr_addstr(sp
, m1
, mlen
);
211 if (len
+ mlen
> sp
->cols
- 2)
212 mlen
= (sp
->cols
- 2) - len
;
213 (void)gp
->scr_waddstr(sp
, m2
, mlen
);
216 (void)gp
->scr_move(sp
, oldy
, oldx
);
217 (void)gp
->scr_refresh(sp
, 0);
222 * Display ex output or error messages for the screen.
224 * This routine is the default editor interface for all ex output, and all ex
225 * and vi error/informational messages. It implements the standard strategy
226 * of stealing lines from the bottom of the vi text screen. Screens using an
227 * alternate method of displaying messages, e.g. dialog boxes, should set their
228 * scr_msg function to the correct function before calling the editor.
230 * PUBLIC: void vs_msg __P((SCR *, mtype_t, char *, size_t));
233 vs_msg(sp
, mtype
, line
, len
)
241 size_t maxcols
, oldx
, oldy
, padding
;
242 const char *e
, *s
, *t
;
248 * Ring the bell if it's scheduled.
251 * Shouldn't we save this, too?
253 if (F_ISSET(sp
, SC_TINPUT_INFO
) || F_ISSET(gp
, G_BELLSCHED
))
254 if (F_ISSET(sp
, SC_SCR_VI
)) {
255 F_CLR(gp
, G_BELLSCHED
);
256 (void)gp
->scr_bell(sp
);
258 F_SET(gp
, G_BELLSCHED
);
261 * If vi is using the error line for text input, there's no screen
262 * real-estate for the error message. Nothing to do without some
263 * information as to how important the error message is.
265 if (F_ISSET(sp
, SC_TINPUT_INFO
))
269 * Ex or ex controlled screen output.
271 * If output happens during startup, e.g., a .exrc file, we may be
272 * in ex mode but haven't initialized the screen. Initialize here,
273 * and in this case, stay in ex mode.
275 * If the SC_SCR_EXWROTE bit is set, then we're switching back and
276 * forth between ex and vi, but the screen is trashed and we have
277 * to respect that. Switch to ex mode long enough to put out the
280 * If the SC_EX_WAIT_NO bit is set, turn it off -- we're writing to
281 * the screen, so previous opinions are ignored.
283 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
)) {
284 if (!F_ISSET(sp
, SC_SCR_EX
))
285 if (F_ISSET(sp
, SC_SCR_EXWROTE
)) {
286 if (sp
->gp
->scr_screen(sp
, SC_EX
))
293 (void)gp
->scr_attr(sp
, SA_INVERSE
, 1);
294 (void)printf("%.*s", (int)len
, line
);
296 (void)gp
->scr_attr(sp
, SA_INVERSE
, 0);
297 (void)fflush(stdout
);
299 F_CLR(sp
, SC_EX_WAIT_NO
);
301 if (!F_ISSET(sp
, SC_SCR_EX
))
302 (void)sp
->gp
->scr_screen(sp
, SC_VI
);
306 /* If the vi screen isn't ready, save the message. */
307 if (!F_ISSET(sp
, SC_SCR_VI
)) {
308 (void)vs_msgsave(sp
, mtype
, line
, len
);
312 /* Save the cursor position. */
313 (void)gp
->scr_cursor(sp
, &oldy
, &oldx
);
315 /* If it's an ex output message, just write it out. */
316 if (mtype
== M_NONE
) {
317 vs_output(sp
, mtype
, line
, len
);
322 * If it's a vi message, strip the trailing <newline> so we can
323 * try and paste messages together.
325 if (line
[len
- 1] == '\n')
329 * If a message won't fit on a single line, try to split on a <blank>.
330 * If a subsequent message fits on the same line, write a separator
331 * and output it. Otherwise, put out a newline.
333 * Need up to two padding characters normally; a semi-colon and a
334 * separating space. If only a single line on the screen, add some
335 * more for the trailing continuation message.
338 * Assume that periods and semi-colons take up a single column on the
342 * There are almost certainly pathological cases that will break this
346 (void)msg_cmsg(sp
, CMSG_CONT_S
, &padding
);
351 maxcols
= sp
->cols
- 1;
352 if (vip
->lcontinue
!= 0)
353 if (len
+ vip
->lcontinue
+ padding
> maxcols
)
354 vs_output(sp
, vip
->mtype
, ".\n", 2);
356 vs_output(sp
, vip
->mtype
, ";", 1);
357 vs_output(sp
, M_NONE
, " ", 1);
360 for (s
= line
;; s
= t
) {
361 for (; len
> 0 && isblank(*s
); --len
, ++s
);
364 if (len
+ vip
->lcontinue
> maxcols
) {
365 for (e
= s
+ (maxcols
- vip
->lcontinue
);
366 e
> s
&& !isblank(*e
); --e
);
368 e
= t
= s
+ (maxcols
- vip
->lcontinue
);
370 for (t
= e
; isblank(e
[-1]); --e
);
375 * If the message ends in a period, discard it, we want to
376 * gang messages where possible.
379 if (len
== 0 && (e
- s
) > 1 && s
[(e
- s
) - 1] == '.')
381 vs_output(sp
, mtype
, s
, e
- s
);
384 vs_output(sp
, M_NONE
, "\n", 1);
390 ret
: (void)gp
->scr_move(sp
, oldy
, oldx
);
391 (void)gp
->scr_refresh(sp
, 0);
396 * Output the text to the screen.
399 vs_output(sp
, mtype
, line
, llen
)
408 size_t chlen
, notused
;
409 int ch
, len
, rlen
, tlen
;
411 char *cbp
, *ecbp
, cbuf
[128];
415 for (p
= line
, rlen
= llen
; llen
> 0;) {
416 /* Get the next physical line. */
417 if ((p
= memchr(line
, '\n', llen
)) == NULL
)
423 * The max is sp->cols characters, and we may have already
424 * written part of the line.
426 if (len
+ vip
->lcontinue
> sp
->cols
)
427 len
= sp
->cols
- vip
->lcontinue
;
430 * If the first line output, do nothing. If the second line
431 * output, draw the divider line. If drew a full screen, we
432 * remove the divider line. If it's a continuation line, move
433 * to the continuation point, else, move the screen up.
435 if (vip
->lcontinue
== 0) {
436 if (!IS_ONELINE(sp
)) {
437 if (vip
->totalcount
== 1) {
438 (void)gp
->scr_move(sp
,
439 LASTLINE(sp
) - 1, 0);
440 (void)gp
->scr_clrtoeol(sp
);
441 (void)vs_divider(sp
);
442 F_SET(vip
, VIP_DIVIDER
);
446 if (vip
->totalcount
== sp
->t_maxrows
&&
447 F_ISSET(vip
, VIP_DIVIDER
)) {
450 F_CLR(vip
, VIP_DIVIDER
);
453 if (vip
->totalcount
!= 0)
454 vs_scroll(sp
, NULL
, SCROLL_W_QUIT
);
456 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
463 (void)gp
->scr_move(sp
, LASTLINE(sp
), vip
->lcontinue
);
465 /* Error messages are in inverse video. */
467 (void)gp
->scr_attr(sp
, SA_INVERSE
, 1);
469 /* Display the line, doing character translation. */
472 (void)gp->scr_addstr(sp, cbuf, cbp - cbuf); \
475 ecbp
= (cbp
= cbuf
) + sizeof(cbuf
) - 1;
476 for (t
= line
, tlen
= len
; tlen
--; ++t
) {
479 * Replace tabs with spaces, there are places in
480 * ex that do column calculations without looking
481 * at <tabs> -- and all routines that care about
482 * <tabs> do their own expansions. This catches
483 * <tabs> in things like tag search strings.
487 chlen
= KEY_LEN(sp
, ch
);
488 if (cbp
+ chlen
>= ecbp
)
490 for (kp
= KEY_NAME(sp
, ch
); chlen
--;)
496 (void)gp
->scr_attr(sp
, SA_INVERSE
, 0);
498 /* Clear the rest of the line. */
499 (void)gp
->scr_clrtoeol(sp
);
501 /* If we loop, it's a new line. */
504 /* Reset for the next line. */
513 /* Set up next continuation line. */
515 gp
->scr_cursor(sp
, ¬used
, &vip
->lcontinue
);
520 * Deal with ex message output.
522 * This routine is called when exiting a colon command to resolve any ex
523 * output that may have occurred.
525 * PUBLIC: int vs_ex_resolve __P((SCR *, int *));
528 vs_ex_resolve(sp
, continuep
)
541 /* If we ran any ex command, we can't trust the cursor position. */
542 F_SET(vip
, VIP_CUR_INVALID
);
544 /* Terminate any partially written message. */
545 if (vip
->lcontinue
!= 0) {
546 vs_output(sp
, vip
->mtype
, ".", 1);
553 * If we switched out of the vi screen into ex, switch back while we
554 * figure out what to do with the screen and potentially get another
555 * command to execute.
557 * If we didn't switch into ex, we're not required to wait, and less
558 * than 2 lines of output, we can continue without waiting for the
561 * Note, all other code paths require waiting, so we leave the report
562 * of modified lines until later, so that we won't wait for no other
563 * reason than a threshold number of lines were modified. This means
564 * we display cumulative line modification reports for groups of ex
565 * commands. That seems right to me (well, at least not wrong).
567 if (F_ISSET(sp
, SC_SCR_EXWROTE
)) {
568 if (sp
->gp
->scr_screen(sp
, SC_VI
))
571 if (!F_ISSET(sp
, SC_EX_WAIT_YES
) && vip
->totalcount
< 2) {
572 F_CLR(sp
, SC_EX_WAIT_NO
);
576 /* Clear the required wait flag, it's no longer needed. */
577 F_CLR(sp
, SC_EX_WAIT_YES
);
580 * Wait, unless explicitly told not to wait or the user interrupted
581 * the command. If the user is leaving the screen, for any reason,
582 * they can't continue with further ex commands.
584 if (!F_ISSET(sp
, SC_EX_WAIT_NO
) && !INTERRUPTED(sp
)) {
585 wtype
= F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
|
586 SC_FSWITCH
| SC_SSWITCH
) ? SCROLL_W
: SCROLL_W_EX
;
587 if (F_ISSET(sp
, SC_SCR_EXWROTE
))
588 vs_wait(sp
, continuep
, wtype
);
590 vs_scroll(sp
, continuep
, wtype
);
595 /* If ex wrote on the screen, refresh the screen image. */
596 if (F_ISSET(sp
, SC_SCR_EXWROTE
))
597 F_SET(vip
, VIP_N_EX_PAINT
);
600 * If we're not the bottom of the split screen stack, the screen
601 * image itself is wrong, so redraw everything.
603 if (sp
->q
.cqe_next
!= (void *)&sp
->wp
->scrq
)
604 F_SET(sp
, SC_SCR_REDRAW
);
606 /* If ex changed the underlying file, the map itself is wrong. */
607 if (F_ISSET(vip
, VIP_N_EX_REDRAW
))
608 F_SET(sp
, SC_SCR_REFORMAT
);
610 /* Ex may have switched out of the alternate screen, return. */
611 (void)gp
->scr_attr(sp
, SA_ALTERNATE
, 1);
614 * Whew. We're finally back home, after what feels like years.
617 F_CLR(sp
, SC_SCR_EXWROTE
| SC_EX_WAIT_NO
);
620 * We may need to repaint some of the screen, e.g.:
625 * gives us a combination of some lines that are "wrong", and a need
626 * for a full refresh.
628 if (vip
->totalcount
> 1) {
629 /* Set up the redraw of the overwritten lines. */
630 ev
.e_event
= E_REPAINT
;
631 ev
.e_flno
= vip
->totalcount
>=
632 sp
->rows
? 1 : sp
->rows
- vip
->totalcount
;
633 ev
.e_tlno
= sp
->rows
;
635 /* Reset the count of overwriting lines. */
636 vip
->linecount
= vip
->lcontinue
= vip
->totalcount
= 0;
639 (void)v_erepaint(sp
, &ev
);
641 /* Reset the count of overwriting lines. */
642 vip
->linecount
= vip
->lcontinue
= vip
->totalcount
= 0;
649 * Deal with message output.
651 * PUBLIC: int vs_resolve __P((SCR *, SCR *, int));
654 vs_resolve(sp
, csp
, forcewait
)
666 * Vs_resolve is called from the main vi loop and the refresh function
667 * to periodically ensure that the user has seen any messages that have
668 * been displayed and that any status lines are correct. The sp screen
669 * is the screen we're checking, usually the current screen. When it's
670 * not, csp is the current screen, used for final cursor positioning.
677 /* Save the cursor position. */
678 (void)gp
->scr_cursor(csp
, &oldy
, &oldx
);
680 /* Ring the bell if it's scheduled. */
681 if (F_ISSET(gp
, G_BELLSCHED
)) {
682 F_CLR(gp
, G_BELLSCHED
);
683 (void)gp
->scr_bell(sp
);
686 /* Display new file status line. */
687 if (F_ISSET(sp
, SC_STATUS
)) {
688 F_CLR(sp
, SC_STATUS
);
689 msgq_status(sp
, sp
->lno
, MSTAT_TRUNCATE
);
692 /* Report on line modifications. */
696 * Flush any saved messages. If the screen isn't ready, refresh
697 * it. (A side-effect of screen refresh is that we can display
698 * messages.) Once this is done, don't trust the cursor. That
699 * extra refresh screwed the pooch.
701 if (gp
->msgq
.lh_first
!= NULL
) {
702 if (!F_ISSET(sp
, SC_SCR_VI
) && vs_refresh(sp
, 1))
704 while ((mp
= gp
->msgq
.lh_first
) != NULL
) {
705 gp
->scr_msg(sp
, mp
->mtype
, mp
->buf
, mp
->len
);
710 F_SET(vip
, VIP_CUR_INVALID
);
713 switch (vip
->totalcount
) {
719 * If we're switching screens, we have to wait for messages,
720 * regardless. If we don't wait, skip updating the modeline.
723 vs_scroll(sp
, NULL
, SCROLL_W
);
725 F_SET(vip
, VIP_S_MODELINE
);
731 * If >1 message line in use, prompt the user to continue and
732 * repaint overwritten lines.
734 vs_scroll(sp
, NULL
, SCROLL_W
);
736 ev
.e_event
= E_REPAINT
;
737 ev
.e_flno
= vip
->totalcount
>=
738 sp
->rows
? 1 : sp
->rows
- vip
->totalcount
;
739 ev
.e_tlno
= sp
->rows
;
745 /* Reset the count of overwriting lines. */
746 vip
->linecount
= vip
->lcontinue
= vip
->totalcount
= 0;
750 (void)v_erepaint(sp
, &ev
);
752 /* Restore the cursor position. */
753 (void)gp
->scr_move(csp
, oldy
, oldx
);
760 * Scroll the screen for output.
763 vs_scroll(sp
, continuep
, wtype
)
773 if (!IS_ONELINE(sp
)) {
775 * Scroll the screen. Instead of scrolling the entire screen,
776 * delete the line above the first line output so preserve the
777 * maximum amount of the screen.
779 (void)gp
->scr_move(sp
, vip
->totalcount
<
780 sp
->rows
? LASTLINE(sp
) - vip
->totalcount
: 0, 0);
781 (void)gp
->scr_deleteln(sp
);
783 /* If there are screens below us, push them back into place. */
784 if (sp
->q
.cqe_next
!= (void *)&sp
->wp
->scrq
) {
785 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
786 (void)gp
->scr_insertln(sp
);
789 if (wtype
== SCROLL_W_QUIT
&& vip
->linecount
< sp
->t_maxrows
)
791 vs_wait(sp
, continuep
, wtype
);
796 * Prompt the user to continue.
799 vs_wait(sp
, continuep
, wtype
)
813 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
815 p
= msg_cmsg(sp
, CMSG_CONT_S
, &len
);
819 p
= msg_cmsg(sp
, CMSG_CONT_Q
, &len
);
822 p
= msg_cmsg(sp
, CMSG_CONT_EX
, &len
);
825 p
= msg_cmsg(sp
, CMSG_CONT
, &len
);
831 (void)gp
->scr_addstr(sp
, p
, len
);
836 (void)gp
->scr_clrtoeol(sp
);
837 (void)gp
->scr_refresh(sp
, 0);
839 /* Get a single character from the terminal. */
840 if (continuep
!= NULL
)
843 if (v_event_get(sp
, &ev
, 0, 0))
845 if (ev
.e_event
== E_CHARACTER
)
847 if (ev
.e_event
== E_INTERRUPT
) {
849 F_SET(gp
, G_INTERRUPTED
);
852 (void)gp
->scr_bell(sp
);
856 if (ev
.e_c
== CH_QUIT
)
857 F_SET(gp
, G_INTERRUPTED
);
860 if (ev
.e_c
== ':' && continuep
!= NULL
)
870 * Draw a dividing line between the screen and the output.
879 #define DIVIDESTR "+=+=+=+=+=+=+=+"
881 sizeof(DIVIDESTR
) - 1 > sp
->cols
? sp
->cols
: sizeof(DIVIDESTR
) - 1;
883 (void)gp
->scr_attr(sp
, SA_INVERSE
, 1);
884 (void)gp
->scr_addstr(sp
, DIVIDESTR
, len
);
885 (void)gp
->scr_attr(sp
, SA_INVERSE
, 0);
890 * Save a message for later display.
893 vs_msgsave(sp
, mt
, p
, len
)
903 * We have to handle messages before we have any place to put them.
904 * If there's no screen support yet, allocate a msg structure, copy
905 * in the message, and queue it on the global structure. If we can't
906 * allocate memory here, we're genuinely screwed, dump the message
907 * to stderr in the (probably) vain hope that someone will see it.
909 CALLOC_GOTO(sp
, mp_n
, MSGS
*, 1, sizeof(MSGS
));
910 MALLOC_GOTO(sp
, mp_n
->buf
, char *, len
);
912 memmove(mp_n
->buf
, p
, len
);
917 if ((mp_c
= gp
->msgq
.lh_first
) == NULL
) {
918 LIST_INSERT_HEAD(&gp
->msgq
, mp_n
, q
);
920 for (; mp_c
->q
.le_next
!= NULL
; mp_c
= mp_c
->q
.le_next
);
921 LIST_INSERT_AFTER(mp_c
, mp_n
, q
);
928 (void)fprintf(stderr
, "%.*s\n", (int)len
, p
);