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.62 2000/07/23 11:14:44 skimo Exp $ (Berkeley) $Date: 2000/07/23 11:14:44 $";
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
*));
37 * Add len bytes from the string at the cursor, advancing the cursor.
39 * PUBLIC: int cl_waddstr __P((SCR *, const CHAR_T *, size_t));
42 cl_waddstr(sp
, str
, len
)
50 INT2DISP(sp
, str
, len
, np
, nlen
);
51 cl_addstr(sp
, np
, nlen
);
56 * Add len bytes from the string at the cursor, advancing the cursor.
58 * PUBLIC: int cl_addstr __P((SCR *, const char *, size_t));
61 cl_addstr(sp
, str
, len
)
72 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
75 * If ex isn't in control, it's the last line of the screen and
76 * it's a split screen, use inverse video.
80 if (!F_ISSET(sp
, SC_SCR_EXWROTE
) &&
81 y
== RLNO(sp
, LASTLINE(sp
)) && IS_SPLIT(sp
)) {
86 if (waddnstr(win
, str
, len
) == ERR
)
96 * Toggle a screen attribute on/off.
98 * PUBLIC: int cl_attr __P((SCR *, scr_attr_t, int));
101 cl_attr(sp
, attribute
, on
)
103 scr_attr_t attribute
;
110 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
116 * There's a major layering violation here. The problem is that the
117 * X11 xterm screen has what's known as an "alternate" screen. Some
118 * xterm termcap/terminfo entries include sequences to switch to/from
119 * that alternate screen as part of the ti/te (smcup/rmcup) strings.
120 * Vi runs in the alternate screen, so that you are returned to the
121 * same screen contents on exit from vi that you had when you entered
122 * vi. Further, when you run :shell, or :!date or similar ex commands,
123 * you also see the original screen contents. This wasn't deliberate
124 * on vi's part, it's just that it historically sent terminal init/end
125 * sequences at those times, and the addition of the alternate screen
126 * sequences to the strings changed the behavior of vi. The problem
127 * caused by this is that we don't want to switch back to the alternate
128 * screen while getting a new command from the user, when the user is
129 * continuing to enter ex commands, e.g.:
131 * :!date <<< switch to original screen
132 * [Hit return to continue] <<< prompt user to continue
133 * :command <<< get command from user
135 * Note that the :command input is a true vi input mode, e.g., input
136 * maps and abbreviations are being done. So, we need to be able to
137 * switch back into the vi screen mode, without flashing the screen.
139 * To make matters worse, the curses initscr() and endwin() calls will
140 * do this automatically -- so, this attribute isn't as controlled by
141 * the higher level screen as closely as one might like.
144 if (clp
->ti_te
!= TI_SENT
) {
145 clp
->ti_te
= TI_SENT
;
146 if (clp
->smcup
== NULL
)
147 (void)cl_getcap(sp
, "smcup", &clp
->smcup
);
148 if (clp
->smcup
!= NULL
)
149 (void)tputs(clp
->smcup
, 1, cl_putchar
);
152 if (clp
->ti_te
!= TE_SENT
) {
153 clp
->ti_te
= TE_SENT
;
154 if (clp
->rmcup
== NULL
)
155 (void)cl_getcap(sp
, "rmcup", &clp
->rmcup
);
156 if (clp
->rmcup
!= NULL
)
157 (void)tputs(clp
->rmcup
, 1, cl_putchar
);
158 (void)fflush(stdout
);
160 (void)fflush(stdout
);
163 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
)) {
164 if (clp
->smso
== NULL
)
167 (void)tputs(clp
->smso
, 1, cl_putchar
);
169 (void)tputs(clp
->rmso
, 1, cl_putchar
);
170 (void)fflush(stdout
);
173 (void)wstandout(win
);
175 (void)wstandend(win
);
186 * Return the baud rate.
188 * PUBLIC: int cl_baud __P((SCR *, u_long *));
199 * There's no portable way to get a "baud rate" -- cfgetospeed(3)
200 * returns the value associated with some #define, which we may
201 * never have heard of, or which may be a purely local speed. Vi
202 * only cares if it's SLOW (w300), slow (w1200) or fast (w9600).
203 * Try and detect the slow ones, and default to fast.
206 switch (cfgetospeed(&clp
->orig
)) {
229 * Ring the bell/flash the screen.
231 * PUBLIC: int cl_bell __P((SCR *));
237 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
| SC_SCR_EX
))
238 (void)write(STDOUT_FILENO
, "\07", 1); /* \a */
241 * Vi has an edit option which determines if the terminal
242 * should be beeped or the screen flashed.
244 if (O_ISSET(sp
, O_FLASH
))
254 * Clear from the current cursor to the end of the line.
256 * PUBLIC: int cl_clrtoeol __P((SCR *));
265 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
269 /* The cursor must be returned to its original position. */
271 for (spcnt
= (sp
->coff
+ sp
->cols
) - x
; spcnt
> 0; --spcnt
)
272 (void)waddch(win
, ' ');
273 (void)wmove(win
, y
, x
);
277 return (wclrtoeol(win
) == ERR
);
282 * Return the current cursor position.
284 * PUBLIC: int cl_cursor __P((SCR *, size_t *, size_t *));
287 cl_cursor(sp
, yp
, xp
)
292 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
294 * The curses screen support splits a single underlying curses screen
295 * into multiple screens to support split screen semantics. For this
296 * reason the returned value must be adjusted to be relative to the
297 * current screen, and not absolute. Screens that implement the split
298 * using physically distinct screens won't need this hack.
300 getyx(win
, *yp
, *xp
);
310 * Delete the current line, scrolling all lines below it.
312 * PUBLIC: int cl_deleteln __P((SCR *));
321 size_t col
, lno
, spcnt
, y
, x
;
324 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
327 * This clause is required because the curses screen uses reverse
328 * video to delimit split screens. If the screen does not do this,
329 * this code won't be necessary.
331 * If the bottom line was in reverse video, rewrite it in normal
332 * video before it's scrolled.
334 * Check for the existence of a chgat function; XSI requires it, but
335 * historic implementations of System V curses don't. If it's not
336 * a #define, we'll fall back to doing it by hand, which is slow but
339 * By hand means walking through the line, retrieving and rewriting
340 * each character. Curses has no EOL marker, so track strings of
341 * spaces, and copy the trailing spaces only if there's a non-space
342 * character following.
344 if (!F_ISSET(sp
, SC_SCR_EXWROTE
) && IS_SPLIT(sp
)) {
347 mvwchgat(win
, RLNO(sp
, LASTLINE(sp
)), 0, -1, A_NORMAL
, 0, NULL
);
349 for (lno
= RLNO(sp
, LASTLINE(sp
)), col
= spcnt
= 0;;) {
350 (void)wmove(win
, lno
, col
);
355 (void)wmove(win
, lno
, col
- spcnt
);
356 for (; spcnt
> 0; --spcnt
)
357 (void)waddch(win
, ' ');
358 (void)waddch(win
, ch
);
360 if (++col
>= sp
->cols
)
364 (void)wmove(win
, y
, x
);
368 * The bottom line is expected to be blank after this operation,
369 * and other screens must support that semantic.
371 return (wdeleteln(win
) == ERR
);
378 * PUBLIC: int cl_discard __P((SCR *, SCR **));
381 cl_discard(discardp
, acquirep
)
382 SCR
*discardp
, **acquirep
;
389 F_SET(clp
, CL_LAYOUT
);
391 if (CLSP(discardp
)) {
392 delwin(CLSP(discardp
));
393 CLSP(discardp
) = NULL
;
397 /* no screens got a piece; we're done */
401 for (; (tsp
= *acquirep
) != NULL
; ++acquirep
) {
403 F_SET(clp
, CL_LAYOUT
);
407 CLSP(tsp
) = subwin(stdscr
, tsp
->rows
, tsp
->cols
,
408 tsp
->roff
, tsp
->coff
);
411 /* discardp is going away, acquirep is taking up its space. */
417 * Adjust the screen for ex. This routine is purely for standalone
418 * ex programs. All special purpose, all special case.
420 * PUBLIC: int cl_ex_adjust __P((SCR *, exadj_t));
423 cl_ex_adjust(sp
, action
)
433 /* Move the cursor up one line if that's possible. */
434 if (clp
->cuu1
!= NULL
)
435 (void)tputs(clp
->cuu1
, 1, cl_putchar
);
436 else if (clp
->cup
!= NULL
)
437 (void)tputs(tgoto(clp
->cup
,
438 0, LINES
- 2), 1, cl_putchar
);
443 /* Clear the line. */
444 if (clp
->el
!= NULL
) {
446 (void)tputs(clp
->el
, 1, cl_putchar
);
449 * Historically, ex didn't erase the line, so, if the
450 * displayed line was only a single glyph, and <eof>
451 * was more than one glyph, the output would not fully
452 * overwrite the user's input. To fix this, output
453 * the maxiumum character number of spaces. Note,
454 * this won't help if the user entered extra prompt
455 * or <blank> characters before the command character.
456 * We'd have to do a lot of work to make that work, and
457 * it's almost certainly not worth the effort.
459 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
461 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
464 (void)fflush(stdout
);
475 * Push down the current line, discarding the bottom line.
477 * PUBLIC: int cl_insertln __P((SCR *));
484 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
486 * The current line is expected to be blank after this operation,
487 * and the screen must support that semantic.
489 return (winsertln(win
) == ERR
);
494 * Return the value for a special key.
496 * PUBLIC: int cl_keyval __P((SCR *, scr_keyval_t, CHAR_T *, int *));
499 cl_keyval(sp
, val
, chp
, dnep
)
508 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
509 * VWERASE is a 4BSD extension.
514 *dnep
= (*chp
= clp
->orig
.c_cc
[VEOF
]) == _POSIX_VDISABLE
;
517 *dnep
= (*chp
= clp
->orig
.c_cc
[VERASE
]) == _POSIX_VDISABLE
;
520 *dnep
= (*chp
= clp
->orig
.c_cc
[VKILL
]) == _POSIX_VDISABLE
;
524 *dnep
= (*chp
= clp
->orig
.c_cc
[VWERASE
]) == _POSIX_VDISABLE
;
538 * PUBLIC: int cl_move __P((SCR *, size_t, size_t));
541 cl_move(sp
, lno
, cno
)
546 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
547 /* See the comment in cl_cursor. */
548 if (wmove(win
, RLNO(sp
, lno
), RCNO(sp
, cno
)) == ERR
) {
549 msgq(sp
, M_ERR
, "Error: move: l(%u + %u) c(%u + %u)",
550 lno
, sp
->roff
, cno
, sp
->coff
);
558 * Refresh the screen.
560 * PUBLIC: int cl_refresh __P((SCR *, int));
563 cl_refresh(sp
, repaint
)
575 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
578 * If we received a killer signal, we're done, there's no point
579 * in refreshing the screen.
585 * If repaint is set, the editor is telling us that we don't know
586 * what's on the screen, so we have to repaint from scratch.
588 * If repaint set or the screen layout changed, we need to redraw
589 * any lines separating vertically split screens. If the horizontal
590 * offsets are the same, then the split was vertical, and need to
591 * draw a dividing line.
593 if (repaint
|| F_ISSET(clp
, CL_LAYOUT
)) {
596 psp
!= (void *)&sp
->wp
->scrq
; psp
= psp
->q
.cqe_next
)
597 for (tsp
= psp
->q
.cqe_next
;
598 tsp
!= (void *)&sp
->wp
->scrq
;
599 tsp
= tsp
->q
.cqe_next
)
600 if (psp
->roff
== tsp
->roff
) {
601 if (psp
->coff
+ psp
->cols
+ 1 == tsp
->coff
)
604 if (tsp
->coff
+ tsp
->cols
+ 1 == psp
->coff
)
607 (void)wmove(win
, y
, x
);
608 F_CLR(clp
, CL_LAYOUT
);
612 * In the curses library, doing wrefresh(curscr) is okay, but the
613 * screen flashes when we then apply the refresh() to bring it up
614 * to date. So, use clearok().
618 return (wrefresh(stdscr
) == ERR
|| wrefresh(win
) == ERR
);
623 * Draw a dividing line between two vertically split screens.
632 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
634 for (cnt
= 0; cnt
< sp
->rows
- 1; ++cnt
) {
635 wmove(stdscr
, sp
->roff
+ cnt
, sp
->cols
+ sp
->coff
);
644 * PUBLIC: int cl_rename __P((SCR *, char *, int));
647 cl_rename(sp
, name
, on
)
661 if (!F_ISSET(clp
, CL_RENAME_OK
))
666 * We can only rename windows for xterm.
668 if (strncmp(OG_STR(gp
, GO_TERM
), "xterm", sizeof("xterm") - 1))
673 * Try and figure out the current name of this window. There
674 * are two forms of the xwininfo output I've seen:
676 * Window id: 0x400000d "name"
677 * Window id: 0x140000d (name)
680 "expr \"`xwininfo -id $WINDOWID | grep id:`\" : '.* [\"(]\\(.*\\)[\")]'"
682 if (clp
->oname
== NULL
&&
683 (pfp
= popen(COMMAND
, "r")) != NULL
) {
684 if (fgets(buf
, sizeof(buf
), pfp
) != NULL
&&
685 (p
= strchr(buf
, '\n')) != NULL
) {
687 clp
->oname
= strdup(buf
);
692 cl_setname(gp
, name
);
694 F_SET(clp
, CL_RENAME
);
696 if (F_ISSET(clp
, CL_RENAME
)) {
697 cl_setname(gp
, clp
->oname
);
699 F_CLR(clp
, CL_RENAME
);
706 * Set a X11 icon/window name.
708 * PUBLIC: void cl_setname __P((GS *, char *));
715 /* X11 xterm escape sequence to rename the icon/window. */
716 #define XTERM_RENAME "\033]0;%s\007"
718 (void)printf(XTERM_RENAME
, name
== NULL
? OG_STR(gp
, GO_TERM
) : name
);
719 (void)fflush(stdout
);
726 * PUBLIC: int cl_split __P((SCR *, SCR *));
729 cl_split(origp
, newp
)
735 F_SET(clp
, CL_LAYOUT
);
740 CLSP(origp
) = subwin(stdscr
, origp
->rows
, origp
->cols
,
741 origp
->roff
, origp
->coff
);
742 CLSP(newp
) = subwin(stdscr
, newp
->rows
, newp
->cols
,
743 newp
->roff
, newp
->coff
);
745 /* origp is the original screen, giving up space to newp. */
753 * PUBLIC: int cl_suspend __P((SCR *, int *));
756 cl_suspend(sp
, allowedp
)
769 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
773 * The ex implementation of this function isn't needed by screens not
774 * supporting ex commands that require full terminal canonical mode
777 * The vi implementation of this function isn't needed by screens not
778 * supporting vi process suspension, i.e. any screen that isn't backed
781 * Setting allowedp to 0 will cause the editor to reject the command.
783 if (F_ISSET(sp
, SC_EX
)) {
784 /* Save the terminal settings, and restore the original ones. */
785 if (F_ISSET(clp
, CL_STDIN_TTY
)) {
786 (void)tcgetattr(STDIN_FILENO
, &t
);
787 (void)tcsetattr(STDIN_FILENO
,
788 TCSASOFT
| TCSADRAIN
, &clp
->orig
);
791 /* Stop the process group. */
792 (void)kill(0, SIGTSTP
);
794 /* Time passes ... */
796 /* Restore terminal settings. */
797 if (F_ISSET(clp
, CL_STDIN_TTY
))
798 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
803 * Move to the lower left-hand corner of the screen.
806 * Not sure this is necessary in System V implementations, but it
810 (void)wmove(win
, LINES
- 1, 0);
814 * Temporarily end the screen. System V introduced a semantic where
815 * endwin() could be restarted. We use it because restarting curses
816 * from scratch often fails in System V. 4BSD curses didn't support
817 * restarting after endwin(), so we have to do what clean up we can
818 * without calling it.
820 /* Save the terminal settings. */
821 (void)tcgetattr(STDIN_FILENO
, &t
);
823 /* Restore the cursor keys to normal mode. */
824 (void)keypad(stdscr
, FALSE
);
826 /* Restore the window name. */
827 (void)cl_rename(sp
, NULL
, 0);
829 #ifdef HAVE_BSD_CURSES
830 (void)cl_attr(sp
, SA_ALTERNATE
, 0);
836 * Restore the original terminal settings. This is bad -- the
837 * reset can cause character loss from the tty queue. However,
838 * we can't call endwin() in BSD curses implementations, and too
839 * many System V curses implementations don't get it right.
841 (void)tcsetattr(STDIN_FILENO
, TCSADRAIN
| TCSASOFT
, &clp
->orig
);
843 /* Stop the process group. */
844 (void)kill(0, SIGTSTP
);
846 /* Time passes ... */
849 * If we received a killer signal, we're done. Leave everything
850 * unchanged. In addition, the terminal has already been reset
851 * correctly, so leave it alone.
853 if (clp
->killersig
) {
854 F_CLR(clp
, CL_SCR_EX_INIT
| CL_SCR_VI_INIT
);
858 /* Restore terminal settings. */
859 wrefresh(win
); /* Needed on SunOs/Solaris ? */
860 if (F_ISSET(clp
, CL_STDIN_TTY
))
861 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
863 #ifdef HAVE_BSD_CURSES
864 (void)cl_attr(sp
, SA_ALTERNATE
, 1);
867 /* Set the window name. */
868 (void)cl_rename(sp
, sp
->frp
->name
, 1);
870 /* Put the cursor keys into application mode. */
871 (void)keypad(stdscr
, TRUE
);
873 /* Refresh and repaint the screen. */
874 (void)wmove(win
, y
, x
);
875 (void)cl_refresh(sp
, 1);
877 /* If the screen changed size, set the SIGWINCH bit. */
878 if (cl_ssize(sp
, 1, NULL
, NULL
, &changed
))
881 F_SET(CLP(sp
), CL_SIGWINCH
);
888 * Print out the curses usage messages.
890 * PUBLIC: void cl_usage __P((void));
896 usage: ex [-eFRrSsv] [-c command] [-t tag] [-w size] [file ...]\n\
897 usage: vi [-eFlRrSv] [-c command] [-t tag] [-w size] [file ...]\n"
898 (void)fprintf(stderr
, "%s", USAGE
);
905 * Stub routine so can flush out curses screen changes using gdb.
911 return (0); /* XXX Convince gdb to run it. */