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.58 2000/06/25 17:34:36 skimo Exp $ (Berkeley) $Date: 2000/06/25 17:34:36 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
30 #include "../common/common.h"
34 static void cl_rdiv
__P((SCR
*));
38 * Add len bytes from the string at the cursor, advancing the cursor.
40 * PUBLIC: int cl_addstr __P((SCR *, const char *, size_t));
43 cl_addstr(sp
, str
, len
)
54 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
57 * If ex isn't in control, it's the last line of the screen and
58 * it's a split screen, use inverse video.
62 if (!F_ISSET(sp
, SC_SCR_EXWROTE
) &&
63 y
== RLNO(sp
, LASTLINE(sp
)) && IS_SPLIT(sp
)) {
68 if (waddnstr(win
, str
, len
) == ERR
)
78 * Toggle a screen attribute on/off.
80 * PUBLIC: int cl_attr __P((SCR *, scr_attr_t, int));
83 cl_attr(sp
, attribute
, on
)
92 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
98 * There's a major layering violation here. The problem is that the
99 * X11 xterm screen has what's known as an "alternate" screen. Some
100 * xterm termcap/terminfo entries include sequences to switch to/from
101 * that alternate screen as part of the ti/te (smcup/rmcup) strings.
102 * Vi runs in the alternate screen, so that you are returned to the
103 * same screen contents on exit from vi that you had when you entered
104 * vi. Further, when you run :shell, or :!date or similar ex commands,
105 * you also see the original screen contents. This wasn't deliberate
106 * on vi's part, it's just that it historically sent terminal init/end
107 * sequences at those times, and the addition of the alternate screen
108 * sequences to the strings changed the behavior of vi. The problem
109 * caused by this is that we don't want to switch back to the alternate
110 * screen while getting a new command from the user, when the user is
111 * continuing to enter ex commands, e.g.:
113 * :!date <<< switch to original screen
114 * [Hit return to continue] <<< prompt user to continue
115 * :command <<< get command from user
117 * Note that the :command input is a true vi input mode, e.g., input
118 * maps and abbreviations are being done. So, we need to be able to
119 * switch back into the vi screen mode, without flashing the screen.
121 * To make matters worse, the curses initscr() and endwin() calls will
122 * do this automatically -- so, this attribute isn't as controlled by
123 * the higher level screen as closely as one might like.
126 if (clp
->ti_te
!= TI_SENT
) {
127 clp
->ti_te
= TI_SENT
;
128 if (clp
->smcup
== NULL
)
129 (void)cl_getcap(sp
, "smcup", &clp
->smcup
);
130 if (clp
->smcup
!= NULL
)
131 (void)tputs(clp
->smcup
, 1, cl_putchar
);
134 if (clp
->ti_te
!= TE_SENT
) {
135 clp
->ti_te
= TE_SENT
;
136 if (clp
->rmcup
== NULL
)
137 (void)cl_getcap(sp
, "rmcup", &clp
->rmcup
);
138 if (clp
->rmcup
!= NULL
)
139 (void)tputs(clp
->rmcup
, 1, cl_putchar
);
140 (void)fflush(stdout
);
142 (void)fflush(stdout
);
145 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
)) {
146 if (clp
->smso
== NULL
)
149 (void)tputs(clp
->smso
, 1, cl_putchar
);
151 (void)tputs(clp
->rmso
, 1, cl_putchar
);
152 (void)fflush(stdout
);
155 (void)wstandout(win
);
157 (void)wstandend(win
);
168 * Return the baud rate.
170 * PUBLIC: int cl_baud __P((SCR *, u_long *));
181 * There's no portable way to get a "baud rate" -- cfgetospeed(3)
182 * returns the value associated with some #define, which we may
183 * never have heard of, or which may be a purely local speed. Vi
184 * only cares if it's SLOW (w300), slow (w1200) or fast (w9600).
185 * Try and detect the slow ones, and default to fast.
188 switch (cfgetospeed(&clp
->orig
)) {
211 * Ring the bell/flash the screen.
213 * PUBLIC: int cl_bell __P((SCR *));
219 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
| SC_SCR_EX
))
220 (void)write(STDOUT_FILENO
, "\07", 1); /* \a */
223 * Vi has an edit option which determines if the terminal
224 * should be beeped or the screen flashed.
226 if (O_ISSET(sp
, O_FLASH
))
236 * Clear from the current cursor to the end of the line.
238 * PUBLIC: int cl_clrtoeol __P((SCR *));
247 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
251 /* The cursor must be returned to its original position. */
253 for (spcnt
= (sp
->coff
+ sp
->cols
) - x
; spcnt
> 0; --spcnt
)
254 (void)waddch(win
, ' ');
255 (void)wmove(win
, y
, x
);
259 return (wclrtoeol(win
) == ERR
);
264 * Return the current cursor position.
266 * PUBLIC: int cl_cursor __P((SCR *, size_t *, size_t *));
269 cl_cursor(sp
, yp
, xp
)
274 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
276 * The curses screen support splits a single underlying curses screen
277 * into multiple screens to support split screen semantics. For this
278 * reason the returned value must be adjusted to be relative to the
279 * current screen, and not absolute. Screens that implement the split
280 * using physically distinct screens won't need this hack.
282 getyx(win
, *yp
, *xp
);
292 * Delete the current line, scrolling all lines below it.
294 * PUBLIC: int cl_deleteln __P((SCR *));
303 size_t col
, lno
, spcnt
, y
, x
;
306 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
309 * This clause is required because the curses screen uses reverse
310 * video to delimit split screens. If the screen does not do this,
311 * this code won't be necessary.
313 * If the bottom line was in reverse video, rewrite it in normal
314 * video before it's scrolled.
316 * Check for the existence of a chgat function; XSI requires it, but
317 * historic implementations of System V curses don't. If it's not
318 * a #define, we'll fall back to doing it by hand, which is slow but
321 * By hand means walking through the line, retrieving and rewriting
322 * each character. Curses has no EOL marker, so track strings of
323 * spaces, and copy the trailing spaces only if there's a non-space
324 * character following.
326 if (!F_ISSET(sp
, SC_SCR_EXWROTE
) && IS_SPLIT(sp
)) {
329 mvwchgat(win
, RLNO(sp
, LASTLINE(sp
)), 0, -1, A_NORMAL
, 0, NULL
);
331 for (lno
= RLNO(sp
, LASTLINE(sp
)), col
= spcnt
= 0;;) {
332 (void)wmove(win
, lno
, col
);
337 (void)wmove(win
, lno
, col
- spcnt
);
338 for (; spcnt
> 0; --spcnt
)
339 (void)waddch(win
, ' ');
340 (void)waddch(win
, ch
);
342 if (++col
>= sp
->cols
)
346 (void)wmove(win
, y
, x
);
350 * The bottom line is expected to be blank after this operation,
351 * and other screens must support that semantic.
353 return (wdeleteln(win
) == ERR
);
360 * PUBLIC: int cl_discard __P((SCR *, SCR **));
363 cl_discard(discardp
, acquirep
)
364 SCR
*discardp
, **acquirep
;
371 F_SET(clp
, CL_LAYOUT
);
373 if (CLSP(discardp
)) {
374 delwin(CLSP(discardp
));
375 CLSP(discardp
) = NULL
;
379 /* no screens got a piece; we're done */
383 for (; (tsp
= *acquirep
) != NULL
; ++acquirep
) {
385 F_SET(clp
, CL_LAYOUT
);
389 CLSP(tsp
) = subwin(stdscr
, tsp
->rows
, tsp
->cols
,
390 tsp
->roff
, tsp
->coff
);
393 /* discardp is going away, acquirep is taking up its space. */
399 * Adjust the screen for ex. This routine is purely for standalone
400 * ex programs. All special purpose, all special case.
402 * PUBLIC: int cl_ex_adjust __P((SCR *, exadj_t));
405 cl_ex_adjust(sp
, action
)
415 /* Move the cursor up one line if that's possible. */
416 if (clp
->cuu1
!= NULL
)
417 (void)tputs(clp
->cuu1
, 1, cl_putchar
);
418 else if (clp
->cup
!= NULL
)
419 (void)tputs(tgoto(clp
->cup
,
420 0, LINES
- 2), 1, cl_putchar
);
425 /* Clear the line. */
426 if (clp
->el
!= NULL
) {
428 (void)tputs(clp
->el
, 1, cl_putchar
);
431 * Historically, ex didn't erase the line, so, if the
432 * displayed line was only a single glyph, and <eof>
433 * was more than one glyph, the output would not fully
434 * overwrite the user's input. To fix this, output
435 * the maxiumum character number of spaces. Note,
436 * this won't help if the user entered extra prompt
437 * or <blank> characters before the command character.
438 * We'd have to do a lot of work to make that work, and
439 * it's almost certainly not worth the effort.
441 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
443 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
446 (void)fflush(stdout
);
457 * Push down the current line, discarding the bottom line.
459 * PUBLIC: int cl_insertln __P((SCR *));
466 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
468 * The current line is expected to be blank after this operation,
469 * and the screen must support that semantic.
471 return (winsertln(win
) == ERR
);
476 * Return the value for a special key.
478 * PUBLIC: int cl_keyval __P((SCR *, scr_keyval_t, CHAR_T *, int *));
481 cl_keyval(sp
, val
, chp
, dnep
)
490 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
491 * VWERASE is a 4BSD extension.
496 *dnep
= (*chp
= clp
->orig
.c_cc
[VEOF
]) == _POSIX_VDISABLE
;
499 *dnep
= (*chp
= clp
->orig
.c_cc
[VERASE
]) == _POSIX_VDISABLE
;
502 *dnep
= (*chp
= clp
->orig
.c_cc
[VKILL
]) == _POSIX_VDISABLE
;
506 *dnep
= (*chp
= clp
->orig
.c_cc
[VWERASE
]) == _POSIX_VDISABLE
;
520 * PUBLIC: int cl_move __P((SCR *, size_t, size_t));
523 cl_move(sp
, lno
, cno
)
528 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
529 /* See the comment in cl_cursor. */
530 if (wmove(win
, RLNO(sp
, lno
), RCNO(sp
, cno
)) == ERR
) {
531 msgq(sp
, M_ERR
, "Error: move: l(%u + %u) c(%u + %u)",
532 lno
, sp
->roff
, cno
, sp
->coff
);
540 * Refresh the screen.
542 * PUBLIC: int cl_refresh __P((SCR *, int));
545 cl_refresh(sp
, repaint
)
557 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
560 * If we received a killer signal, we're done, there's no point
561 * in refreshing the screen.
567 * If repaint is set, the editor is telling us that we don't know
568 * what's on the screen, so we have to repaint from scratch.
570 * If repaint set or the screen layout changed, we need to redraw
571 * any lines separating vertically split screens. If the horizontal
572 * offsets are the same, then the split was vertical, and need to
573 * draw a dividing line.
575 if (repaint
|| F_ISSET(clp
, CL_LAYOUT
)) {
578 psp
!= (void *)&sp
->wp
->scrq
; psp
= psp
->q
.cqe_next
)
579 for (tsp
= psp
->q
.cqe_next
;
580 tsp
!= (void *)&sp
->wp
->scrq
;
581 tsp
= tsp
->q
.cqe_next
)
582 if (psp
->roff
== tsp
->roff
) {
583 if (psp
->coff
+ psp
->cols
+ 1 == tsp
->coff
)
586 if (tsp
->coff
+ tsp
->cols
+ 1 == psp
->coff
)
589 (void)wmove(win
, y
, x
);
590 F_CLR(clp
, CL_LAYOUT
);
594 * In the curses library, doing wrefresh(curscr) is okay, but the
595 * screen flashes when we then apply the refresh() to bring it up
596 * to date. So, use clearok().
600 return (wrefresh(stdscr
) == ERR
|| wrefresh(win
) == ERR
);
605 * Draw a dividing line between two vertically split screens.
614 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
616 for (cnt
= 0; cnt
< sp
->rows
- 1; ++cnt
) {
617 wmove(stdscr
, sp
->roff
+ cnt
, sp
->cols
+ sp
->coff
);
626 * PUBLIC: int cl_rename __P((SCR *, char *, int));
629 cl_rename(sp
, name
, on
)
643 if (!F_ISSET(clp
, CL_RENAME_OK
))
648 * We can only rename windows for xterm.
650 if (strncmp(OG_STR(gp
, GO_TERM
), "xterm", sizeof("xterm") - 1))
655 * Try and figure out the current name of this window. There
656 * are two forms of the xwininfo output I've seen:
658 * Window id: 0x400000d "name"
659 * Window id: 0x140000d (name)
662 "expr \"`xwininfo -id $WINDOWID | grep id:`\" : '.* [\"(]\\(.*\\)[\")]'"
664 if (clp
->oname
== NULL
&&
665 (pfp
= popen(COMMAND
, "r")) != NULL
) {
666 if (fgets(buf
, sizeof(buf
), pfp
) != NULL
&&
667 (p
= strchr(buf
, '\n')) != NULL
) {
669 clp
->oname
= strdup(buf
);
674 cl_setname(gp
, name
);
676 F_SET(clp
, CL_RENAME
);
678 if (F_ISSET(clp
, CL_RENAME
)) {
679 cl_setname(gp
, clp
->oname
);
681 F_CLR(clp
, CL_RENAME
);
688 * Set a X11 icon/window name.
690 * PUBLIC: void cl_setname __P((GS *, char *));
697 /* X11 xterm escape sequence to rename the icon/window. */
698 #define XTERM_RENAME "\033]0;%s\007"
700 (void)printf(XTERM_RENAME
, name
== NULL
? OG_STR(gp
, GO_TERM
) : name
);
701 (void)fflush(stdout
);
708 * PUBLIC: int cl_split __P((SCR *, SCR *));
711 cl_split(origp
, newp
)
717 F_SET(clp
, CL_LAYOUT
);
722 CLSP(origp
) = subwin(stdscr
, origp
->rows
, origp
->cols
,
723 origp
->roff
, origp
->coff
);
724 CLSP(newp
) = subwin(stdscr
, newp
->rows
, newp
->cols
,
725 newp
->roff
, newp
->coff
);
727 /* origp is the original screen, giving up space to newp. */
735 * PUBLIC: int cl_suspend __P((SCR *, int *));
738 cl_suspend(sp
, allowedp
)
751 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
755 * The ex implementation of this function isn't needed by screens not
756 * supporting ex commands that require full terminal canonical mode
759 * The vi implementation of this function isn't needed by screens not
760 * supporting vi process suspension, i.e. any screen that isn't backed
763 * Setting allowedp to 0 will cause the editor to reject the command.
765 if (F_ISSET(sp
, SC_EX
)) {
766 /* Save the terminal settings, and restore the original ones. */
767 if (F_ISSET(clp
, CL_STDIN_TTY
)) {
768 (void)tcgetattr(STDIN_FILENO
, &t
);
769 (void)tcsetattr(STDIN_FILENO
,
770 TCSASOFT
| TCSADRAIN
, &clp
->orig
);
773 /* Stop the process group. */
774 (void)kill(0, SIGTSTP
);
776 /* Time passes ... */
778 /* Restore terminal settings. */
779 if (F_ISSET(clp
, CL_STDIN_TTY
))
780 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
785 * Move to the lower left-hand corner of the screen.
788 * Not sure this is necessary in System V implementations, but it
792 (void)wmove(win
, LINES
- 1, 0);
796 * Temporarily end the screen. System V introduced a semantic where
797 * endwin() could be restarted. We use it because restarting curses
798 * from scratch often fails in System V. 4BSD curses didn't support
799 * restarting after endwin(), so we have to do what clean up we can
800 * without calling it.
802 /* Save the terminal settings. */
803 (void)tcgetattr(STDIN_FILENO
, &t
);
805 /* Restore the cursor keys to normal mode. */
806 (void)keypad(stdscr
, FALSE
);
808 /* Restore the window name. */
809 (void)cl_rename(sp
, NULL
, 0);
811 #ifdef HAVE_BSD_CURSES
812 (void)cl_attr(sp
, SA_ALTERNATE
, 0);
818 * Restore the original terminal settings. This is bad -- the
819 * reset can cause character loss from the tty queue. However,
820 * we can't call endwin() in BSD curses implementations, and too
821 * many System V curses implementations don't get it right.
823 (void)tcsetattr(STDIN_FILENO
, TCSADRAIN
| TCSASOFT
, &clp
->orig
);
825 /* Stop the process group. */
826 (void)kill(0, SIGTSTP
);
828 /* Time passes ... */
831 * If we received a killer signal, we're done. Leave everything
832 * unchanged. In addition, the terminal has already been reset
833 * correctly, so leave it alone.
835 if (clp
->killersig
) {
836 F_CLR(clp
, CL_SCR_EX_INIT
| CL_SCR_VI_INIT
);
840 /* Restore terminal settings. */
841 wrefresh(win
); /* Needed on SunOs/Solaris ? */
842 if (F_ISSET(clp
, CL_STDIN_TTY
))
843 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
845 #ifdef HAVE_BSD_CURSES
846 (void)cl_attr(sp
, SA_ALTERNATE
, 1);
849 /* Set the window name. */
850 (void)cl_rename(sp
, sp
->frp
->name
, 1);
852 /* Put the cursor keys into application mode. */
853 (void)keypad(stdscr
, TRUE
);
855 /* Refresh and repaint the screen. */
856 (void)wmove(win
, y
, x
);
857 (void)cl_refresh(sp
, 1);
859 /* If the screen changed size, set the SIGWINCH bit. */
860 if (cl_ssize(sp
, 1, NULL
, NULL
, &changed
))
863 F_SET(CLP(sp
), CL_SIGWINCH
);
870 * Print out the curses usage messages.
872 * PUBLIC: void cl_usage __P((void));
878 usage: ex [-eFRrSsv] [-c command] [-t tag] [-w size] [file ...]\n\
879 usage: vi [-eFlRrSv] [-c command] [-t tag] [-w size] [file ...]\n"
880 (void)fprintf(stderr
, "%s", USAGE
);
887 * Stub routine so can flush out curses screen changes using gdb.
893 return (0); /* XXX Convince gdb to run it. */