2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: cl_funcs.c,v 10.70 2001/06/13 20:00:18 skimo Exp $ (Berkeley) $Date: 2001/06/13 20:00:18 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
29 #include "../common/common.h"
33 static void cl_rdiv
__P((SCR
*));
36 addstr4(SCR
*sp
, void *str
, size_t len
, int wide
)
44 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
47 * If ex isn't in control, it's the last line of the screen and
48 * it's a split screen, use inverse video.
52 if (!F_ISSET(sp
, SC_SCR_EXWROTE
) &&
53 y
== RLNO(sp
, LASTLINE(sp
)) && IS_SPLIT(sp
)) {
60 if (waddnwstr(win
, str
, len
) == ERR
)
64 if (waddnstr(win
, str
, len
) == ERR
)
74 * Add len bytes from the string at the cursor, advancing the cursor.
76 * PUBLIC: int cl_waddstr __P((SCR *, const CHAR_T *, size_t));
79 cl_waddstr(sp
, str
, len
)
84 addstr4(sp
, (void *)str
, len
, 1);
89 * Add len bytes from the string at the cursor, advancing the cursor.
91 * PUBLIC: int cl_addstr __P((SCR *, const char *, size_t));
94 cl_addstr(sp
, str
, len
)
99 addstr4(sp
, (void *)str
, len
, 0);
104 * Toggle a screen attribute on/off.
106 * PUBLIC: int cl_attr __P((SCR *, scr_attr_t, int));
109 cl_attr(sp
, attribute
, on
)
111 scr_attr_t attribute
;
118 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
124 * There's a major layering violation here. The problem is that the
125 * X11 xterm screen has what's known as an "alternate" screen. Some
126 * xterm termcap/terminfo entries include sequences to switch to/from
127 * that alternate screen as part of the ti/te (smcup/rmcup) strings.
128 * Vi runs in the alternate screen, so that you are returned to the
129 * same screen contents on exit from vi that you had when you entered
130 * vi. Further, when you run :shell, or :!date or similar ex commands,
131 * you also see the original screen contents. This wasn't deliberate
132 * on vi's part, it's just that it historically sent terminal init/end
133 * sequences at those times, and the addition of the alternate screen
134 * sequences to the strings changed the behavior of vi. The problem
135 * caused by this is that we don't want to switch back to the alternate
136 * screen while getting a new command from the user, when the user is
137 * continuing to enter ex commands, e.g.:
139 * :!date <<< switch to original screen
140 * [Hit return to continue] <<< prompt user to continue
141 * :command <<< get command from user
143 * Note that the :command input is a true vi input mode, e.g., input
144 * maps and abbreviations are being done. So, we need to be able to
145 * switch back into the vi screen mode, without flashing the screen.
147 * To make matters worse, the curses initscr() and endwin() calls will
148 * do this automatically -- so, this attribute isn't as controlled by
149 * the higher level screen as closely as one might like.
152 if (clp
->ti_te
!= TI_SENT
) {
153 clp
->ti_te
= TI_SENT
;
154 if (clp
->smcup
== NULL
)
155 (void)cl_getcap(sp
, "smcup", &clp
->smcup
);
156 if (clp
->smcup
!= NULL
)
157 (void)tputs(clp
->smcup
, 1, cl_putchar
);
160 if (clp
->ti_te
!= TE_SENT
) {
161 clp
->ti_te
= TE_SENT
;
162 if (clp
->rmcup
== NULL
)
163 (void)cl_getcap(sp
, "rmcup", &clp
->rmcup
);
164 if (clp
->rmcup
!= NULL
)
165 (void)tputs(clp
->rmcup
, 1, cl_putchar
);
166 (void)fflush(stdout
);
168 (void)fflush(stdout
);
171 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
)) {
172 if (clp
->smso
== NULL
)
175 (void)tputs(clp
->smso
, 1, cl_putchar
);
177 (void)tputs(clp
->rmso
, 1, cl_putchar
);
178 (void)fflush(stdout
);
181 (void)wstandout(win
);
183 (void)wstandend(win
);
194 * Return the baud rate.
196 * PUBLIC: int cl_baud __P((SCR *, u_long *));
207 * There's no portable way to get a "baud rate" -- cfgetospeed(3)
208 * returns the value associated with some #define, which we may
209 * never have heard of, or which may be a purely local speed. Vi
210 * only cares if it's SLOW (w300), slow (w1200) or fast (w9600).
211 * Try and detect the slow ones, and default to fast.
214 switch (cfgetospeed(&clp
->orig
)) {
237 * Ring the bell/flash the screen.
239 * PUBLIC: int cl_bell __P((SCR *));
245 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
| SC_SCR_EX
))
246 (void)write(STDOUT_FILENO
, "\07", 1); /* \a */
249 * Vi has an edit option which determines if the terminal
250 * should be beeped or the screen flashed.
252 if (O_ISSET(sp
, O_FLASH
))
262 * Clear from the current cursor to the end of the line.
264 * PUBLIC: int cl_clrtoeol __P((SCR *));
273 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
277 /* The cursor must be returned to its original position. */
279 for (spcnt
= (sp
->coff
+ sp
->cols
) - x
; spcnt
> 0; --spcnt
)
280 (void)waddch(win
, ' ');
281 (void)wmove(win
, y
, x
);
285 return (wclrtoeol(win
) == ERR
);
290 * Return the current cursor position.
292 * PUBLIC: int cl_cursor __P((SCR *, size_t *, size_t *));
295 cl_cursor(sp
, yp
, xp
)
300 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
302 * The curses screen support splits a single underlying curses screen
303 * into multiple screens to support split screen semantics. For this
304 * reason the returned value must be adjusted to be relative to the
305 * current screen, and not absolute. Screens that implement the split
306 * using physically distinct screens won't need this hack.
308 getyx(win
, *yp
, *xp
);
318 * Delete the current line, scrolling all lines below it.
320 * PUBLIC: int cl_deleteln __P((SCR *));
329 size_t col
, lno
, spcnt
, y
, x
;
332 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
335 * This clause is required because the curses screen uses reverse
336 * video to delimit split screens. If the screen does not do this,
337 * this code won't be necessary.
339 * If the bottom line was in reverse video, rewrite it in normal
340 * video before it's scrolled.
342 * Check for the existence of a chgat function; XSI requires it, but
343 * historic implementations of System V curses don't. If it's not
344 * a #define, we'll fall back to doing it by hand, which is slow but
347 * By hand means walking through the line, retrieving and rewriting
348 * each character. Curses has no EOL marker, so track strings of
349 * spaces, and copy the trailing spaces only if there's a non-space
350 * character following.
352 if (!F_ISSET(sp
, SC_SCR_EXWROTE
) && IS_SPLIT(sp
)) {
355 mvwchgat(win
, RLNO(sp
, LASTLINE(sp
)), 0, -1, A_NORMAL
, 0, NULL
);
357 for (lno
= RLNO(sp
, LASTLINE(sp
)), col
= spcnt
= 0;;) {
358 (void)wmove(win
, lno
, col
);
363 (void)wmove(win
, lno
, col
- spcnt
);
364 for (; spcnt
> 0; --spcnt
)
365 (void)waddch(win
, ' ');
366 (void)waddch(win
, ch
);
368 if (++col
>= sp
->cols
)
372 (void)wmove(win
, y
, x
);
376 * The bottom line is expected to be blank after this operation,
377 * and other screens must support that semantic.
379 return (wdeleteln(win
) == ERR
);
386 * PUBLIC: int cl_discard __P((SCR *, SCR **));
389 cl_discard(discardp
, acquirep
)
390 SCR
*discardp
, **acquirep
;
397 F_SET(clp
, CL_LAYOUT
);
399 if (CLSP(discardp
)) {
400 delwin(CLSP(discardp
));
401 CLSP(discardp
) = NULL
;
405 /* no screens got a piece; we're done */
409 for (; (tsp
= *acquirep
) != NULL
; ++acquirep
) {
411 F_SET(clp
, CL_LAYOUT
);
415 CLSP(tsp
) = subwin(stdscr
, tsp
->rows
, tsp
->cols
,
416 tsp
->roff
, tsp
->coff
);
419 /* discardp is going away, acquirep is taking up its space. */
425 * Adjust the screen for ex. This routine is purely for standalone
426 * ex programs. All special purpose, all special case.
428 * PUBLIC: int cl_ex_adjust __P((SCR *, exadj_t));
431 cl_ex_adjust(sp
, action
)
441 /* Move the cursor up one line if that's possible. */
442 if (clp
->cuu1
!= NULL
)
443 (void)tputs(clp
->cuu1
, 1, cl_putchar
);
444 else if (clp
->cup
!= NULL
)
445 (void)tputs(tgoto(clp
->cup
,
446 0, LINES
- 2), 1, cl_putchar
);
451 /* Clear the line. */
452 if (clp
->el
!= NULL
) {
454 (void)tputs(clp
->el
, 1, cl_putchar
);
457 * Historically, ex didn't erase the line, so, if the
458 * displayed line was only a single glyph, and <eof>
459 * was more than one glyph, the output would not fully
460 * overwrite the user's input. To fix this, output
461 * the maxiumum character number of spaces. Note,
462 * this won't help if the user entered extra prompt
463 * or <blank> characters before the command character.
464 * We'd have to do a lot of work to make that work, and
465 * it's almost certainly not worth the effort.
467 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
469 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
472 (void)fflush(stdout
);
483 * Push down the current line, discarding the bottom line.
485 * PUBLIC: int cl_insertln __P((SCR *));
492 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
494 * The current line is expected to be blank after this operation,
495 * and the screen must support that semantic.
497 return (winsertln(win
) == ERR
);
502 * Return the value for a special key.
504 * PUBLIC: int cl_keyval __P((SCR *, scr_keyval_t, CHAR_T *, int *));
507 cl_keyval(sp
, val
, chp
, dnep
)
516 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
517 * VWERASE is a 4BSD extension.
522 *dnep
= (*chp
= clp
->orig
.c_cc
[VEOF
]) == _POSIX_VDISABLE
;
525 *dnep
= (*chp
= clp
->orig
.c_cc
[VERASE
]) == _POSIX_VDISABLE
;
528 *dnep
= (*chp
= clp
->orig
.c_cc
[VKILL
]) == _POSIX_VDISABLE
;
532 *dnep
= (*chp
= clp
->orig
.c_cc
[VWERASE
]) == _POSIX_VDISABLE
;
546 * PUBLIC: int cl_move __P((SCR *, size_t, size_t));
549 cl_move(sp
, lno
, cno
)
554 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
555 /* See the comment in cl_cursor. */
556 if (wmove(win
, RLNO(sp
, lno
), RCNO(sp
, cno
)) == ERR
) {
557 msgq(sp
, M_ERR
, "Error: move: l(%u + %u) c(%u + %u)",
558 lno
, sp
->roff
, cno
, sp
->coff
);
566 * Refresh the screen.
568 * PUBLIC: int cl_refresh __P((SCR *, int));
571 cl_refresh(sp
, repaint
)
583 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
586 * If we received a killer signal, we're done, there's no point
587 * in refreshing the screen.
593 * If repaint is set, the editor is telling us that we don't know
594 * what's on the screen, so we have to repaint from scratch.
596 * If repaint set or the screen layout changed, we need to redraw
597 * any lines separating vertically split screens. If the horizontal
598 * offsets are the same, then the split was vertical, and need to
599 * draw a dividing line.
601 if (repaint
|| F_ISSET(clp
, CL_LAYOUT
)) {
604 psp
!= (void *)&sp
->wp
->scrq
; psp
= psp
->q
.cqe_next
)
605 for (tsp
= psp
->q
.cqe_next
;
606 tsp
!= (void *)&sp
->wp
->scrq
;
607 tsp
= tsp
->q
.cqe_next
)
608 if (psp
->roff
== tsp
->roff
) {
609 if (psp
->coff
+ psp
->cols
+ 1 == tsp
->coff
)
612 if (tsp
->coff
+ tsp
->cols
+ 1 == psp
->coff
)
615 (void)wmove(stdscr
, y
, x
);
616 F_CLR(clp
, CL_LAYOUT
);
620 * In the curses library, doing wrefresh(curscr) is okay, but the
621 * screen flashes when we then apply the refresh() to bring it up
622 * to date. So, use clearok().
627 * Only do an actual refresh, when this is the focus window,
628 * i.e. the one holding the cursor. This assumes that refresh
629 * is called for that window after refreshing the others.
630 * This prevents the cursor being drawn in the other windows.
632 return (wnoutrefresh(stdscr
) == ERR
||
633 wnoutrefresh(win
) == ERR
||
634 (sp
== clp
->focus
&& doupdate() == ERR
));
639 * Draw a dividing line between two vertically split screens.
647 for (cnt
= 0; cnt
< sp
->rows
- 1; ++cnt
) {
648 wmove(stdscr
, sp
->roff
+ cnt
, sp
->cols
+ sp
->coff
);
657 * PUBLIC: int cl_rename __P((SCR *, char *, int));
660 cl_rename(sp
, name
, on
)
675 if (!F_ISSET(clp
, CL_RENAME_OK
))
680 * We can only rename windows for xterm.
682 if (strncmp(OG_STR(gp
, GO_TERM
), "xterm", sizeof("xterm") - 1))
687 * Try and figure out the current name of this window. There
688 * are two forms of the xwininfo output I've seen:
690 * Window id: 0x400000d "name"
691 * Window id: 0x140000d (name)
694 "expr \"`xwininfo -id $WINDOWID | grep id:`\" : '.* [\"(]\\(.*\\)[\")]'"
696 if (clp
->oname
== NULL
&&
697 (pfp
= popen(COMMAND
, "r")) != NULL
) {
698 if (fgets(buf
, sizeof(buf
), pfp
) != NULL
&&
699 (p
= strchr(buf
, '\n')) != NULL
) {
701 clp
->oname
= strdup(buf
);
706 cl_setname(gp
, name
);
708 F_SET(clp
, CL_RENAME
);
710 if (F_ISSET(clp
, CL_RENAME
)) {
711 cl_setname(gp
, clp
->oname
);
713 F_CLR(clp
, CL_RENAME
);
720 * Set a X11 icon/window name.
722 * PUBLIC: void cl_setname __P((GS *, char *));
729 /* X11 xterm escape sequence to rename the icon/window. */
730 #define XTERM_RENAME "\033]0;%s\007"
732 (void)printf(XTERM_RENAME
, name
== NULL
? OG_STR(gp
, GO_TERM
) : name
);
733 (void)fflush(stdout
);
740 * PUBLIC: int cl_split __P((SCR *, SCR *));
743 cl_split(origp
, newp
)
749 F_SET(clp
, CL_LAYOUT
);
754 CLSP(origp
) = subwin(stdscr
, origp
->rows
, origp
->cols
,
755 origp
->roff
, origp
->coff
);
756 CLSP(newp
) = subwin(stdscr
, newp
->rows
, newp
->cols
,
757 newp
->roff
, newp
->coff
);
759 /* origp is the original screen, giving up space to newp. */
767 * PUBLIC: int cl_suspend __P((SCR *, int *));
770 cl_suspend(sp
, allowedp
)
783 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
787 * The ex implementation of this function isn't needed by screens not
788 * supporting ex commands that require full terminal canonical mode
791 * The vi implementation of this function isn't needed by screens not
792 * supporting vi process suspension, i.e. any screen that isn't backed
795 * Setting allowedp to 0 will cause the editor to reject the command.
797 if (F_ISSET(sp
, SC_EX
)) {
798 /* Save the terminal settings, and restore the original ones. */
799 if (F_ISSET(clp
, CL_STDIN_TTY
)) {
800 (void)tcgetattr(STDIN_FILENO
, &t
);
801 (void)tcsetattr(STDIN_FILENO
,
802 TCSASOFT
| TCSADRAIN
, &clp
->orig
);
805 /* Stop the process group. */
806 (void)kill(0, SIGTSTP
);
808 /* Time passes ... */
810 /* Restore terminal settings. */
811 if (F_ISSET(clp
, CL_STDIN_TTY
))
812 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
817 * Move to the lower left-hand corner of the screen.
820 * Not sure this is necessary in System V implementations, but it
824 (void)wmove(win
, LINES
- 1, 0);
828 * Temporarily end the screen. System V introduced a semantic where
829 * endwin() could be restarted. We use it because restarting curses
830 * from scratch often fails in System V. 4BSD curses didn't support
831 * restarting after endwin(), so we have to do what clean up we can
832 * without calling it.
834 /* Save the terminal settings. */
835 (void)tcgetattr(STDIN_FILENO
, &t
);
837 /* Restore the cursor keys to normal mode. */
838 (void)keypad(stdscr
, FALSE
);
840 /* Restore the window name. */
841 (void)cl_rename(sp
, NULL
, 0);
843 #ifdef HAVE_BSD_CURSES
844 (void)cl_attr(sp
, SA_ALTERNATE
, 0);
850 * Restore the original terminal settings. This is bad -- the
851 * reset can cause character loss from the tty queue. However,
852 * we can't call endwin() in BSD curses implementations, and too
853 * many System V curses implementations don't get it right.
855 (void)tcsetattr(STDIN_FILENO
, TCSADRAIN
| TCSASOFT
, &clp
->orig
);
857 /* Stop the process group. */
858 (void)kill(0, SIGTSTP
);
860 /* Time passes ... */
863 * If we received a killer signal, we're done. Leave everything
864 * unchanged. In addition, the terminal has already been reset
865 * correctly, so leave it alone.
867 if (clp
->killersig
) {
868 F_CLR(clp
, CL_SCR_EX_INIT
| CL_SCR_VI_INIT
);
872 /* Restore terminal settings. */
873 wrefresh(win
); /* Needed on SunOs/Solaris ? */
874 if (F_ISSET(clp
, CL_STDIN_TTY
))
875 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
877 #ifdef HAVE_BSD_CURSES
878 (void)cl_attr(sp
, SA_ALTERNATE
, 1);
881 /* Set the window name. */
882 (void)cl_rename(sp
, sp
->frp
->name
, 1);
884 /* Put the cursor keys into application mode. */
885 (void)keypad(stdscr
, TRUE
);
887 /* Refresh and repaint the screen. */
888 (void)wmove(win
, y
, x
);
889 (void)cl_refresh(sp
, 1);
891 /* If the screen changed size, set the SIGWINCH bit. */
892 if (cl_ssize(sp
, 1, NULL
, NULL
, &changed
))
895 F_SET(CLP(sp
), CL_SIGWINCH
);
902 * Print out the curses usage messages.
904 * PUBLIC: void cl_usage __P((void));
910 usage: ex [-eFRrSsv] [-c command] [-t tag] [-w size] [file ...]\n\
911 usage: vi [-eFlRrSv] [-c command] [-t tag] [-w size] [file ...]\n"
912 (void)fprintf(stderr
, "%s", USAGE
);
919 * Stub routine so can flush out curses screen changes using gdb.
925 return (0); /* XXX Convince gdb to run it. */