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.71 2001/06/25 15:19:05 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:05 $";
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(SCR
*sp
, const CHAR_T
*str
, size_t len
)
81 addstr4(sp
, (void *)str
, len
, 1);
86 * Add len bytes from the string at the cursor, advancing the cursor.
88 * PUBLIC: int cl_addstr __P((SCR *, const char *, size_t));
91 cl_addstr(SCR
*sp
, const char *str
, size_t len
)
93 addstr4(sp
, (void *)str
, len
, 0);
98 * Toggle a screen attribute on/off.
100 * PUBLIC: int cl_attr __P((SCR *, scr_attr_t, int));
103 cl_attr(SCR
*sp
, scr_attr_t attribute
, int on
)
109 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
115 * There's a major layering violation here. The problem is that the
116 * X11 xterm screen has what's known as an "alternate" screen. Some
117 * xterm termcap/terminfo entries include sequences to switch to/from
118 * that alternate screen as part of the ti/te (smcup/rmcup) strings.
119 * Vi runs in the alternate screen, so that you are returned to the
120 * same screen contents on exit from vi that you had when you entered
121 * vi. Further, when you run :shell, or :!date or similar ex commands,
122 * you also see the original screen contents. This wasn't deliberate
123 * on vi's part, it's just that it historically sent terminal init/end
124 * sequences at those times, and the addition of the alternate screen
125 * sequences to the strings changed the behavior of vi. The problem
126 * caused by this is that we don't want to switch back to the alternate
127 * screen while getting a new command from the user, when the user is
128 * continuing to enter ex commands, e.g.:
130 * :!date <<< switch to original screen
131 * [Hit return to continue] <<< prompt user to continue
132 * :command <<< get command from user
134 * Note that the :command input is a true vi input mode, e.g., input
135 * maps and abbreviations are being done. So, we need to be able to
136 * switch back into the vi screen mode, without flashing the screen.
138 * To make matters worse, the curses initscr() and endwin() calls will
139 * do this automatically -- so, this attribute isn't as controlled by
140 * the higher level screen as closely as one might like.
143 if (clp
->ti_te
!= TI_SENT
) {
144 clp
->ti_te
= TI_SENT
;
145 if (clp
->smcup
== NULL
)
146 (void)cl_getcap(sp
, "smcup", &clp
->smcup
);
147 if (clp
->smcup
!= NULL
)
148 (void)tputs(clp
->smcup
, 1, cl_putchar
);
151 if (clp
->ti_te
!= TE_SENT
) {
152 clp
->ti_te
= TE_SENT
;
153 if (clp
->rmcup
== NULL
)
154 (void)cl_getcap(sp
, "rmcup", &clp
->rmcup
);
155 if (clp
->rmcup
!= NULL
)
156 (void)tputs(clp
->rmcup
, 1, cl_putchar
);
157 (void)fflush(stdout
);
159 (void)fflush(stdout
);
162 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
)) {
163 if (clp
->smso
== NULL
)
166 (void)tputs(clp
->smso
, 1, cl_putchar
);
168 (void)tputs(clp
->rmso
, 1, cl_putchar
);
169 (void)fflush(stdout
);
172 (void)wstandout(win
);
174 (void)wstandend(win
);
185 * Return the baud rate.
187 * PUBLIC: int cl_baud __P((SCR *, u_long *));
190 cl_baud(SCR
*sp
, u_long
*ratep
)
196 * There's no portable way to get a "baud rate" -- cfgetospeed(3)
197 * returns the value associated with some #define, which we may
198 * never have heard of, or which may be a purely local speed. Vi
199 * only cares if it's SLOW (w300), slow (w1200) or fast (w9600).
200 * Try and detect the slow ones, and default to fast.
203 switch (cfgetospeed(&clp
->orig
)) {
226 * Ring the bell/flash the screen.
228 * PUBLIC: int cl_bell __P((SCR *));
233 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
| SC_SCR_EX
))
234 (void)write(STDOUT_FILENO
, "\07", 1); /* \a */
237 * Vi has an edit option which determines if the terminal
238 * should be beeped or the screen flashed.
240 if (O_ISSET(sp
, O_FLASH
))
250 * Clear from the current cursor to the end of the line.
252 * PUBLIC: int cl_clrtoeol __P((SCR *));
260 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
264 /* The cursor must be returned to its original position. */
266 for (spcnt
= (sp
->coff
+ sp
->cols
) - x
; spcnt
> 0; --spcnt
)
267 (void)waddch(win
, ' ');
268 (void)wmove(win
, y
, x
);
272 return (wclrtoeol(win
) == ERR
);
277 * Return the current cursor position.
279 * PUBLIC: int cl_cursor __P((SCR *, size_t *, size_t *));
282 cl_cursor(SCR
*sp
, size_t *yp
, size_t *xp
)
285 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
287 * The curses screen support splits a single underlying curses screen
288 * into multiple screens to support split screen semantics. For this
289 * reason the returned value must be adjusted to be relative to the
290 * current screen, and not absolute. Screens that implement the split
291 * using physically distinct screens won't need this hack.
293 getyx(win
, *yp
, *xp
);
303 * Delete the current line, scrolling all lines below it.
305 * PUBLIC: int cl_deleteln __P((SCR *));
313 size_t col
, lno
, spcnt
, y
, x
;
316 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
319 * This clause is required because the curses screen uses reverse
320 * video to delimit split screens. If the screen does not do this,
321 * this code won't be necessary.
323 * If the bottom line was in reverse video, rewrite it in normal
324 * video before it's scrolled.
326 * Check for the existence of a chgat function; XSI requires it, but
327 * historic implementations of System V curses don't. If it's not
328 * a #define, we'll fall back to doing it by hand, which is slow but
331 * By hand means walking through the line, retrieving and rewriting
332 * each character. Curses has no EOL marker, so track strings of
333 * spaces, and copy the trailing spaces only if there's a non-space
334 * character following.
336 if (!F_ISSET(sp
, SC_SCR_EXWROTE
) && IS_SPLIT(sp
)) {
339 mvwchgat(win
, RLNO(sp
, LASTLINE(sp
)), 0, -1, A_NORMAL
, 0, NULL
);
341 for (lno
= RLNO(sp
, LASTLINE(sp
)), col
= spcnt
= 0;;) {
342 (void)wmove(win
, lno
, col
);
347 (void)wmove(win
, lno
, col
- spcnt
);
348 for (; spcnt
> 0; --spcnt
)
349 (void)waddch(win
, ' ');
350 (void)waddch(win
, ch
);
352 if (++col
>= sp
->cols
)
356 (void)wmove(win
, y
, x
);
360 * The bottom line is expected to be blank after this operation,
361 * and other screens must support that semantic.
363 return (wdeleteln(win
) == ERR
);
370 * PUBLIC: int cl_discard __P((SCR *, SCR **));
373 cl_discard(SCR
*discardp
, SCR
**acquirep
)
380 F_SET(clp
, CL_LAYOUT
);
382 if (CLSP(discardp
)) {
383 delwin(CLSP(discardp
));
384 CLSP(discardp
) = NULL
;
388 /* no screens got a piece; we're done */
392 for (; (tsp
= *acquirep
) != NULL
; ++acquirep
) {
394 F_SET(clp
, CL_LAYOUT
);
398 CLSP(tsp
) = subwin(stdscr
, tsp
->rows
, tsp
->cols
,
399 tsp
->roff
, tsp
->coff
);
402 /* discardp is going away, acquirep is taking up its space. */
408 * Adjust the screen for ex. This routine is purely for standalone
409 * ex programs. All special purpose, all special case.
411 * PUBLIC: int cl_ex_adjust __P((SCR *, exadj_t));
414 cl_ex_adjust(SCR
*sp
, exadj_t action
)
422 /* Move the cursor up one line if that's possible. */
423 if (clp
->cuu1
!= NULL
)
424 (void)tputs(clp
->cuu1
, 1, cl_putchar
);
425 else if (clp
->cup
!= NULL
)
426 (void)tputs(tgoto(clp
->cup
,
427 0, LINES
- 2), 1, cl_putchar
);
432 /* Clear the line. */
433 if (clp
->el
!= NULL
) {
435 (void)tputs(clp
->el
, 1, cl_putchar
);
438 * Historically, ex didn't erase the line, so, if the
439 * displayed line was only a single glyph, and <eof>
440 * was more than one glyph, the output would not fully
441 * overwrite the user's input. To fix this, output
442 * the maxiumum character number of spaces. Note,
443 * this won't help if the user entered extra prompt
444 * or <blank> characters before the command character.
445 * We'd have to do a lot of work to make that work, and
446 * it's almost certainly not worth the effort.
448 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
450 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
453 (void)fflush(stdout
);
464 * Push down the current line, discarding the bottom line.
466 * PUBLIC: int cl_insertln __P((SCR *));
472 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
474 * The current line is expected to be blank after this operation,
475 * and the screen must support that semantic.
477 return (winsertln(win
) == ERR
);
482 * Return the value for a special key.
484 * PUBLIC: int cl_keyval __P((SCR *, scr_keyval_t, CHAR_T *, int *));
487 cl_keyval(SCR
*sp
, scr_keyval_t val
, CHAR_T
*chp
, int *dnep
)
492 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
493 * VWERASE is a 4BSD extension.
498 *dnep
= (*chp
= clp
->orig
.c_cc
[VEOF
]) == _POSIX_VDISABLE
;
501 *dnep
= (*chp
= clp
->orig
.c_cc
[VERASE
]) == _POSIX_VDISABLE
;
504 *dnep
= (*chp
= clp
->orig
.c_cc
[VKILL
]) == _POSIX_VDISABLE
;
508 *dnep
= (*chp
= clp
->orig
.c_cc
[VWERASE
]) == _POSIX_VDISABLE
;
522 * PUBLIC: int cl_move __P((SCR *, size_t, size_t));
525 cl_move(SCR
*sp
, size_t lno
, size_t 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(SCR
*sp
, int repaint
)
555 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
558 * If we received a killer signal, we're done, there's no point
559 * in refreshing the screen.
565 * If repaint is set, the editor is telling us that we don't know
566 * what's on the screen, so we have to repaint from scratch.
568 * If repaint set or the screen layout changed, we need to redraw
569 * any lines separating vertically split screens. If the horizontal
570 * offsets are the same, then the split was vertical, and need to
571 * draw a dividing line.
573 if (repaint
|| F_ISSET(clp
, CL_LAYOUT
)) {
576 psp
!= (void *)&sp
->wp
->scrq
; psp
= psp
->q
.cqe_next
)
577 for (tsp
= psp
->q
.cqe_next
;
578 tsp
!= (void *)&sp
->wp
->scrq
;
579 tsp
= tsp
->q
.cqe_next
)
580 if (psp
->roff
== tsp
->roff
) {
581 if (psp
->coff
+ psp
->cols
+ 1 == tsp
->coff
)
584 if (tsp
->coff
+ tsp
->cols
+ 1 == psp
->coff
)
587 (void)wmove(stdscr
, y
, x
);
588 F_CLR(clp
, CL_LAYOUT
);
592 * In the curses library, doing wrefresh(curscr) is okay, but the
593 * screen flashes when we then apply the refresh() to bring it up
594 * to date. So, use clearok().
599 * Only do an actual refresh, when this is the focus window,
600 * i.e. the one holding the cursor. This assumes that refresh
601 * is called for that window after refreshing the others.
602 * This prevents the cursor being drawn in the other windows.
604 return (wnoutrefresh(stdscr
) == ERR
||
605 wnoutrefresh(win
) == ERR
||
606 (sp
== clp
->focus
&& doupdate() == ERR
));
611 * Draw a dividing line between two vertically split screens.
618 for (cnt
= 0; cnt
< sp
->rows
- 1; ++cnt
) {
619 wmove(stdscr
, sp
->roff
+ cnt
, sp
->cols
+ sp
->coff
);
628 * PUBLIC: int cl_rename __P((SCR *, char *, int));
631 cl_rename(SCR
*sp
, char *name
, int 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 *));
693 cl_setname(GS
*gp
, char *name
)
695 /* X11 xterm escape sequence to rename the icon/window. */
696 #define XTERM_RENAME "\033]0;%s\007"
698 (void)printf(XTERM_RENAME
, name
== NULL
? OG_STR(gp
, GO_TERM
) : name
);
699 (void)fflush(stdout
);
706 * PUBLIC: int cl_split __P((SCR *, SCR *));
709 cl_split(SCR
*origp
, SCR
*newp
)
714 F_SET(clp
, CL_LAYOUT
);
719 CLSP(origp
) = subwin(stdscr
, origp
->rows
, origp
->cols
,
720 origp
->roff
, origp
->coff
);
721 CLSP(newp
) = subwin(stdscr
, newp
->rows
, newp
->cols
,
722 newp
->roff
, newp
->coff
);
724 /* origp is the original screen, giving up space to newp. */
732 * PUBLIC: int cl_suspend __P((SCR *, int *));
735 cl_suspend(SCR
*sp
, int *allowedp
)
746 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
750 * The ex implementation of this function isn't needed by screens not
751 * supporting ex commands that require full terminal canonical mode
754 * The vi implementation of this function isn't needed by screens not
755 * supporting vi process suspension, i.e. any screen that isn't backed
758 * Setting allowedp to 0 will cause the editor to reject the command.
760 if (F_ISSET(sp
, SC_EX
)) {
761 /* Save the terminal settings, and restore the original ones. */
762 if (F_ISSET(clp
, CL_STDIN_TTY
)) {
763 (void)tcgetattr(STDIN_FILENO
, &t
);
764 (void)tcsetattr(STDIN_FILENO
,
765 TCSASOFT
| TCSADRAIN
, &clp
->orig
);
768 /* Stop the process group. */
769 (void)kill(0, SIGTSTP
);
771 /* Time passes ... */
773 /* Restore terminal settings. */
774 if (F_ISSET(clp
, CL_STDIN_TTY
))
775 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
780 * Move to the lower left-hand corner of the screen.
783 * Not sure this is necessary in System V implementations, but it
787 (void)wmove(win
, LINES
- 1, 0);
791 * Temporarily end the screen. System V introduced a semantic where
792 * endwin() could be restarted. We use it because restarting curses
793 * from scratch often fails in System V. 4BSD curses didn't support
794 * restarting after endwin(), so we have to do what clean up we can
795 * without calling it.
797 /* Save the terminal settings. */
798 (void)tcgetattr(STDIN_FILENO
, &t
);
800 /* Restore the cursor keys to normal mode. */
801 (void)keypad(stdscr
, FALSE
);
803 /* Restore the window name. */
804 (void)cl_rename(sp
, NULL
, 0);
806 #ifdef HAVE_BSD_CURSES
807 (void)cl_attr(sp
, SA_ALTERNATE
, 0);
813 * Restore the original terminal settings. This is bad -- the
814 * reset can cause character loss from the tty queue. However,
815 * we can't call endwin() in BSD curses implementations, and too
816 * many System V curses implementations don't get it right.
818 (void)tcsetattr(STDIN_FILENO
, TCSADRAIN
| TCSASOFT
, &clp
->orig
);
820 /* Stop the process group. */
821 (void)kill(0, SIGTSTP
);
823 /* Time passes ... */
826 * If we received a killer signal, we're done. Leave everything
827 * unchanged. In addition, the terminal has already been reset
828 * correctly, so leave it alone.
830 if (clp
->killersig
) {
831 F_CLR(clp
, CL_SCR_EX_INIT
| CL_SCR_VI_INIT
);
835 /* Restore terminal settings. */
836 wrefresh(win
); /* Needed on SunOs/Solaris ? */
837 if (F_ISSET(clp
, CL_STDIN_TTY
))
838 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
840 #ifdef HAVE_BSD_CURSES
841 (void)cl_attr(sp
, SA_ALTERNATE
, 1);
844 /* Set the window name. */
845 (void)cl_rename(sp
, sp
->frp
->name
, 1);
847 /* Put the cursor keys into application mode. */
848 (void)keypad(stdscr
, TRUE
);
850 /* Refresh and repaint the screen. */
851 (void)wmove(win
, y
, x
);
852 (void)cl_refresh(sp
, 1);
854 /* If the screen changed size, set the SIGWINCH bit. */
855 if (cl_ssize(sp
, 1, NULL
, NULL
, &changed
))
858 F_SET(CLP(sp
), CL_SIGWINCH
);
865 * Print out the curses usage messages.
867 * PUBLIC: void cl_usage __P((void));
873 usage: ex [-eFRrSsv] [-c command] [-t tag] [-w size] [file ...]\n\
874 usage: vi [-eFlRrSv] [-c command] [-t tag] [-w size] [file ...]\n"
875 (void)fprintf(stderr
, "%s", USAGE
);
882 * Stub routine so can flush out curses screen changes using gdb.
888 return (0); /* XXX Convince gdb to run it. */