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.66 2001/05/14 15:50:22 skimo Exp $ (Berkeley) $Date: 2001/05/14 15:50:22 $";
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
)) {
62 INT2DISP(sp
, str
, len
, dstr
, dlen
);
63 if (waddnwstr(win
, dstr
, dlen
) == ERR
)
67 if (waddnstr(win
, str
, len
) == ERR
)
77 * Add len bytes from the string at the cursor, advancing the cursor.
79 * PUBLIC: int cl_waddstr __P((SCR *, const CHAR_T *, size_t));
82 cl_waddstr(sp
, str
, len
)
87 addstr4(sp
, (void *)str
, len
, 1);
92 * Add len bytes from the string at the cursor, advancing the cursor.
94 * PUBLIC: int cl_addstr __P((SCR *, const char *, size_t));
97 cl_addstr(sp
, str
, len
)
102 addstr4(sp
, (void *)str
, len
, 0);
107 * Toggle a screen attribute on/off.
109 * PUBLIC: int cl_attr __P((SCR *, scr_attr_t, int));
112 cl_attr(sp
, attribute
, on
)
114 scr_attr_t attribute
;
121 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
127 * There's a major layering violation here. The problem is that the
128 * X11 xterm screen has what's known as an "alternate" screen. Some
129 * xterm termcap/terminfo entries include sequences to switch to/from
130 * that alternate screen as part of the ti/te (smcup/rmcup) strings.
131 * Vi runs in the alternate screen, so that you are returned to the
132 * same screen contents on exit from vi that you had when you entered
133 * vi. Further, when you run :shell, or :!date or similar ex commands,
134 * you also see the original screen contents. This wasn't deliberate
135 * on vi's part, it's just that it historically sent terminal init/end
136 * sequences at those times, and the addition of the alternate screen
137 * sequences to the strings changed the behavior of vi. The problem
138 * caused by this is that we don't want to switch back to the alternate
139 * screen while getting a new command from the user, when the user is
140 * continuing to enter ex commands, e.g.:
142 * :!date <<< switch to original screen
143 * [Hit return to continue] <<< prompt user to continue
144 * :command <<< get command from user
146 * Note that the :command input is a true vi input mode, e.g., input
147 * maps and abbreviations are being done. So, we need to be able to
148 * switch back into the vi screen mode, without flashing the screen.
150 * To make matters worse, the curses initscr() and endwin() calls will
151 * do this automatically -- so, this attribute isn't as controlled by
152 * the higher level screen as closely as one might like.
155 if (clp
->ti_te
!= TI_SENT
) {
156 clp
->ti_te
= TI_SENT
;
157 if (clp
->smcup
== NULL
)
158 (void)cl_getcap(sp
, "smcup", &clp
->smcup
);
159 if (clp
->smcup
!= NULL
)
160 (void)tputs(clp
->smcup
, 1, cl_putchar
);
163 if (clp
->ti_te
!= TE_SENT
) {
164 clp
->ti_te
= TE_SENT
;
165 if (clp
->rmcup
== NULL
)
166 (void)cl_getcap(sp
, "rmcup", &clp
->rmcup
);
167 if (clp
->rmcup
!= NULL
)
168 (void)tputs(clp
->rmcup
, 1, cl_putchar
);
169 (void)fflush(stdout
);
171 (void)fflush(stdout
);
174 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
)) {
175 if (clp
->smso
== NULL
)
178 (void)tputs(clp
->smso
, 1, cl_putchar
);
180 (void)tputs(clp
->rmso
, 1, cl_putchar
);
181 (void)fflush(stdout
);
184 (void)wstandout(win
);
186 (void)wstandend(win
);
197 * Return the baud rate.
199 * PUBLIC: int cl_baud __P((SCR *, u_long *));
210 * There's no portable way to get a "baud rate" -- cfgetospeed(3)
211 * returns the value associated with some #define, which we may
212 * never have heard of, or which may be a purely local speed. Vi
213 * only cares if it's SLOW (w300), slow (w1200) or fast (w9600).
214 * Try and detect the slow ones, and default to fast.
217 switch (cfgetospeed(&clp
->orig
)) {
240 * Ring the bell/flash the screen.
242 * PUBLIC: int cl_bell __P((SCR *));
248 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
| SC_SCR_EX
))
249 (void)write(STDOUT_FILENO
, "\07", 1); /* \a */
252 * Vi has an edit option which determines if the terminal
253 * should be beeped or the screen flashed.
255 if (O_ISSET(sp
, O_FLASH
))
265 * Clear from the current cursor to the end of the line.
267 * PUBLIC: int cl_clrtoeol __P((SCR *));
276 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
280 /* The cursor must be returned to its original position. */
282 for (spcnt
= (sp
->coff
+ sp
->cols
) - x
; spcnt
> 0; --spcnt
)
283 (void)waddch(win
, ' ');
284 (void)wmove(win
, y
, x
);
288 return (wclrtoeol(win
) == ERR
);
293 * Return the current cursor position.
295 * PUBLIC: int cl_cursor __P((SCR *, size_t *, size_t *));
298 cl_cursor(sp
, yp
, xp
)
303 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
305 * The curses screen support splits a single underlying curses screen
306 * into multiple screens to support split screen semantics. For this
307 * reason the returned value must be adjusted to be relative to the
308 * current screen, and not absolute. Screens that implement the split
309 * using physically distinct screens won't need this hack.
311 getyx(win
, *yp
, *xp
);
321 * Delete the current line, scrolling all lines below it.
323 * PUBLIC: int cl_deleteln __P((SCR *));
332 size_t col
, lno
, spcnt
, y
, x
;
335 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
338 * This clause is required because the curses screen uses reverse
339 * video to delimit split screens. If the screen does not do this,
340 * this code won't be necessary.
342 * If the bottom line was in reverse video, rewrite it in normal
343 * video before it's scrolled.
345 * Check for the existence of a chgat function; XSI requires it, but
346 * historic implementations of System V curses don't. If it's not
347 * a #define, we'll fall back to doing it by hand, which is slow but
350 * By hand means walking through the line, retrieving and rewriting
351 * each character. Curses has no EOL marker, so track strings of
352 * spaces, and copy the trailing spaces only if there's a non-space
353 * character following.
355 if (!F_ISSET(sp
, SC_SCR_EXWROTE
) && IS_SPLIT(sp
)) {
358 mvwchgat(win
, RLNO(sp
, LASTLINE(sp
)), 0, -1, A_NORMAL
, 0, NULL
);
360 for (lno
= RLNO(sp
, LASTLINE(sp
)), col
= spcnt
= 0;;) {
361 (void)wmove(win
, lno
, col
);
366 (void)wmove(win
, lno
, col
- spcnt
);
367 for (; spcnt
> 0; --spcnt
)
368 (void)waddch(win
, ' ');
369 (void)waddch(win
, ch
);
371 if (++col
>= sp
->cols
)
375 (void)wmove(win
, y
, x
);
379 * The bottom line is expected to be blank after this operation,
380 * and other screens must support that semantic.
382 return (wdeleteln(win
) == ERR
);
389 * PUBLIC: int cl_discard __P((SCR *, SCR **));
392 cl_discard(discardp
, acquirep
)
393 SCR
*discardp
, **acquirep
;
400 F_SET(clp
, CL_LAYOUT
);
402 if (CLSP(discardp
)) {
403 delwin(CLSP(discardp
));
404 CLSP(discardp
) = NULL
;
408 /* no screens got a piece; we're done */
412 for (; (tsp
= *acquirep
) != NULL
; ++acquirep
) {
414 F_SET(clp
, CL_LAYOUT
);
418 CLSP(tsp
) = subwin(stdscr
, tsp
->rows
, tsp
->cols
,
419 tsp
->roff
, tsp
->coff
);
422 /* discardp is going away, acquirep is taking up its space. */
428 * Adjust the screen for ex. This routine is purely for standalone
429 * ex programs. All special purpose, all special case.
431 * PUBLIC: int cl_ex_adjust __P((SCR *, exadj_t));
434 cl_ex_adjust(sp
, action
)
444 /* Move the cursor up one line if that's possible. */
445 if (clp
->cuu1
!= NULL
)
446 (void)tputs(clp
->cuu1
, 1, cl_putchar
);
447 else if (clp
->cup
!= NULL
)
448 (void)tputs(tgoto(clp
->cup
,
449 0, LINES
- 2), 1, cl_putchar
);
454 /* Clear the line. */
455 if (clp
->el
!= NULL
) {
457 (void)tputs(clp
->el
, 1, cl_putchar
);
460 * Historically, ex didn't erase the line, so, if the
461 * displayed line was only a single glyph, and <eof>
462 * was more than one glyph, the output would not fully
463 * overwrite the user's input. To fix this, output
464 * the maxiumum character number of spaces. Note,
465 * this won't help if the user entered extra prompt
466 * or <blank> characters before the command character.
467 * We'd have to do a lot of work to make that work, and
468 * it's almost certainly not worth the effort.
470 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
472 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
475 (void)fflush(stdout
);
486 * Push down the current line, discarding the bottom line.
488 * PUBLIC: int cl_insertln __P((SCR *));
495 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
497 * The current line is expected to be blank after this operation,
498 * and the screen must support that semantic.
500 return (winsertln(win
) == ERR
);
505 * Return the value for a special key.
507 * PUBLIC: int cl_keyval __P((SCR *, scr_keyval_t, CHAR_T *, int *));
510 cl_keyval(sp
, val
, chp
, dnep
)
519 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
520 * VWERASE is a 4BSD extension.
525 *dnep
= (*chp
= clp
->orig
.c_cc
[VEOF
]) == _POSIX_VDISABLE
;
528 *dnep
= (*chp
= clp
->orig
.c_cc
[VERASE
]) == _POSIX_VDISABLE
;
531 *dnep
= (*chp
= clp
->orig
.c_cc
[VKILL
]) == _POSIX_VDISABLE
;
535 *dnep
= (*chp
= clp
->orig
.c_cc
[VWERASE
]) == _POSIX_VDISABLE
;
549 * PUBLIC: int cl_move __P((SCR *, size_t, size_t));
552 cl_move(sp
, lno
, cno
)
557 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
558 /* See the comment in cl_cursor. */
559 if (wmove(win
, RLNO(sp
, lno
), RCNO(sp
, cno
)) == ERR
) {
560 msgq(sp
, M_ERR
, "Error: move: l(%u + %u) c(%u + %u)",
561 lno
, sp
->roff
, cno
, sp
->coff
);
569 * Refresh the screen.
571 * PUBLIC: int cl_refresh __P((SCR *, int));
574 cl_refresh(sp
, repaint
)
586 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
589 * If we received a killer signal, we're done, there's no point
590 * in refreshing the screen.
596 * If repaint is set, the editor is telling us that we don't know
597 * what's on the screen, so we have to repaint from scratch.
599 * If repaint set or the screen layout changed, we need to redraw
600 * any lines separating vertically split screens. If the horizontal
601 * offsets are the same, then the split was vertical, and need to
602 * draw a dividing line.
604 if (repaint
|| F_ISSET(clp
, CL_LAYOUT
)) {
607 psp
!= (void *)&sp
->wp
->scrq
; psp
= psp
->q
.cqe_next
)
608 for (tsp
= psp
->q
.cqe_next
;
609 tsp
!= (void *)&sp
->wp
->scrq
;
610 tsp
= tsp
->q
.cqe_next
)
611 if (psp
->roff
== tsp
->roff
) {
612 if (psp
->coff
+ psp
->cols
+ 1 == tsp
->coff
)
615 if (tsp
->coff
+ tsp
->cols
+ 1 == psp
->coff
)
618 (void)wmove(stdscr
, y
, x
);
619 F_CLR(clp
, CL_LAYOUT
);
623 * In the curses library, doing wrefresh(curscr) is okay, but the
624 * screen flashes when we then apply the refresh() to bring it up
625 * to date. So, use clearok().
629 return (wnoutrefresh(stdscr
) == ERR
||
630 wnoutrefresh(win
) == ERR
|| doupdate() == ERR
);
635 * Draw a dividing line between two vertically split screens.
643 for (cnt
= 0; cnt
< sp
->rows
- 1; ++cnt
) {
644 wmove(stdscr
, sp
->roff
+ cnt
, sp
->cols
+ sp
->coff
);
653 * PUBLIC: int cl_rename __P((SCR *, char *, int));
656 cl_rename(sp
, name
, on
)
670 if (!F_ISSET(clp
, CL_RENAME_OK
))
675 * We can only rename windows for xterm.
677 if (strncmp(OG_STR(gp
, GO_TERM
), "xterm", sizeof("xterm") - 1))
682 * Try and figure out the current name of this window. There
683 * are two forms of the xwininfo output I've seen:
685 * Window id: 0x400000d "name"
686 * Window id: 0x140000d (name)
689 "expr \"`xwininfo -id $WINDOWID | grep id:`\" : '.* [\"(]\\(.*\\)[\")]'"
691 if (clp
->oname
== NULL
&&
692 (pfp
= popen(COMMAND
, "r")) != NULL
) {
693 if (fgets(buf
, sizeof(buf
), pfp
) != NULL
&&
694 (p
= strchr(buf
, '\n')) != NULL
) {
696 clp
->oname
= strdup(buf
);
701 cl_setname(gp
, name
);
703 F_SET(clp
, CL_RENAME
);
705 if (F_ISSET(clp
, CL_RENAME
)) {
706 cl_setname(gp
, clp
->oname
);
708 F_CLR(clp
, CL_RENAME
);
715 * Set a X11 icon/window name.
717 * PUBLIC: void cl_setname __P((GS *, char *));
724 /* X11 xterm escape sequence to rename the icon/window. */
725 #define XTERM_RENAME "\033]0;%s\007"
727 (void)printf(XTERM_RENAME
, name
== NULL
? OG_STR(gp
, GO_TERM
) : name
);
728 (void)fflush(stdout
);
735 * PUBLIC: int cl_split __P((SCR *, SCR *));
738 cl_split(origp
, newp
)
744 F_SET(clp
, CL_LAYOUT
);
749 CLSP(origp
) = subwin(stdscr
, origp
->rows
, origp
->cols
,
750 origp
->roff
, origp
->coff
);
751 CLSP(newp
) = subwin(stdscr
, newp
->rows
, newp
->cols
,
752 newp
->roff
, newp
->coff
);
754 /* origp is the original screen, giving up space to newp. */
762 * PUBLIC: int cl_suspend __P((SCR *, int *));
765 cl_suspend(sp
, allowedp
)
778 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
782 * The ex implementation of this function isn't needed by screens not
783 * supporting ex commands that require full terminal canonical mode
786 * The vi implementation of this function isn't needed by screens not
787 * supporting vi process suspension, i.e. any screen that isn't backed
790 * Setting allowedp to 0 will cause the editor to reject the command.
792 if (F_ISSET(sp
, SC_EX
)) {
793 /* Save the terminal settings, and restore the original ones. */
794 if (F_ISSET(clp
, CL_STDIN_TTY
)) {
795 (void)tcgetattr(STDIN_FILENO
, &t
);
796 (void)tcsetattr(STDIN_FILENO
,
797 TCSASOFT
| TCSADRAIN
, &clp
->orig
);
800 /* Stop the process group. */
801 (void)kill(0, SIGTSTP
);
803 /* Time passes ... */
805 /* Restore terminal settings. */
806 if (F_ISSET(clp
, CL_STDIN_TTY
))
807 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
812 * Move to the lower left-hand corner of the screen.
815 * Not sure this is necessary in System V implementations, but it
819 (void)wmove(win
, LINES
- 1, 0);
823 * Temporarily end the screen. System V introduced a semantic where
824 * endwin() could be restarted. We use it because restarting curses
825 * from scratch often fails in System V. 4BSD curses didn't support
826 * restarting after endwin(), so we have to do what clean up we can
827 * without calling it.
829 /* Save the terminal settings. */
830 (void)tcgetattr(STDIN_FILENO
, &t
);
832 /* Restore the cursor keys to normal mode. */
833 (void)keypad(stdscr
, FALSE
);
835 /* Restore the window name. */
836 (void)cl_rename(sp
, NULL
, 0);
838 #ifdef HAVE_BSD_CURSES
839 (void)cl_attr(sp
, SA_ALTERNATE
, 0);
845 * Restore the original terminal settings. This is bad -- the
846 * reset can cause character loss from the tty queue. However,
847 * we can't call endwin() in BSD curses implementations, and too
848 * many System V curses implementations don't get it right.
850 (void)tcsetattr(STDIN_FILENO
, TCSADRAIN
| TCSASOFT
, &clp
->orig
);
852 /* Stop the process group. */
853 (void)kill(0, SIGTSTP
);
855 /* Time passes ... */
858 * If we received a killer signal, we're done. Leave everything
859 * unchanged. In addition, the terminal has already been reset
860 * correctly, so leave it alone.
862 if (clp
->killersig
) {
863 F_CLR(clp
, CL_SCR_EX_INIT
| CL_SCR_VI_INIT
);
867 /* Restore terminal settings. */
868 wrefresh(win
); /* Needed on SunOs/Solaris ? */
869 if (F_ISSET(clp
, CL_STDIN_TTY
))
870 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
872 #ifdef HAVE_BSD_CURSES
873 (void)cl_attr(sp
, SA_ALTERNATE
, 1);
876 /* Set the window name. */
877 (void)cl_rename(sp
, sp
->frp
->name
, 1);
879 /* Put the cursor keys into application mode. */
880 (void)keypad(stdscr
, TRUE
);
882 /* Refresh and repaint the screen. */
883 (void)wmove(win
, y
, x
);
884 (void)cl_refresh(sp
, 1);
886 /* If the screen changed size, set the SIGWINCH bit. */
887 if (cl_ssize(sp
, 1, NULL
, NULL
, &changed
))
890 F_SET(CLP(sp
), CL_SIGWINCH
);
897 * Print out the curses usage messages.
899 * PUBLIC: void cl_usage __P((void));
905 usage: ex [-eFRrSsv] [-c command] [-t tag] [-w size] [file ...]\n\
906 usage: vi [-eFlRrSv] [-c command] [-t tag] [-w size] [file ...]\n"
907 (void)fprintf(stderr
, "%s", USAGE
);
914 * Stub routine so can flush out curses screen changes using gdb.
920 return (0); /* XXX Convince gdb to run it. */