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.52 1996/10/31 09:28:13 bostic Exp $ (Berkeley) $Date: 1996/10/31 09:28:13 $";
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
)
55 * If ex isn't in control, it's the last line of the screen and
56 * it's a split screen, use inverse video.
60 if (!F_ISSET(sp
, SC_SCR_EXWROTE
) &&
61 y
== RLNO(sp
, LASTLINE(sp
)) && IS_SPLIT(sp
)) {
66 if (addnstr(str
, len
) == ERR
)
76 * Toggle a screen attribute on/off.
78 * PUBLIC: int cl_attr __P((SCR *, scr_attr_t, int));
81 cl_attr(sp
, attribute
, on
)
94 * There's a major layering violation here. The problem is that the
95 * X11 xterm screen has what's known as an "alternate" screen. Some
96 * xterm termcap/terminfo entries include sequences to switch to/from
97 * that alternate screen as part of the ti/te (smcup/rmcup) strings.
98 * Vi runs in the alternate screen, so that you are returned to the
99 * same screen contents on exit from vi that you had when you entered
100 * vi. Further, when you run :shell, or :!date or similar ex commands,
101 * you also see the original screen contents. This wasn't deliberate
102 * on vi's part, it's just that it historically sent terminal init/end
103 * sequences at those times, and the addition of the alternate screen
104 * sequences to the strings changed the behavior of vi. The problem
105 * caused by this is that we don't want to switch back to the alternate
106 * screen while getting a new command from the user, when the user is
107 * continuing to enter ex commands, e.g.:
109 * :!date <<< switch to original screen
110 * [Hit return to continue] <<< prompt user to continue
111 * :command <<< get command from user
113 * Note that the :command input is a true vi input mode, e.g., input
114 * maps and abbreviations are being done. So, we need to be able to
115 * switch back into the vi screen mode, without flashing the screen.
117 * To make matters worse, the curses initscr() and endwin() calls will
118 * do this automatically -- so, this attribute isn't as controlled by
119 * the higher level screen as closely as one might like.
122 if (clp
->ti_te
!= TI_SENT
) {
123 clp
->ti_te
= TI_SENT
;
124 if (clp
->smcup
== NULL
)
125 (void)cl_getcap(sp
, "smcup", &clp
->smcup
);
126 if (clp
->smcup
!= NULL
)
127 (void)tputs(clp
->smcup
, 1, cl_putchar
);
130 if (clp
->ti_te
!= TE_SENT
) {
131 clp
->ti_te
= TE_SENT
;
132 if (clp
->rmcup
== NULL
)
133 (void)cl_getcap(sp
, "rmcup", &clp
->rmcup
);
134 if (clp
->rmcup
!= NULL
)
135 (void)tputs(clp
->rmcup
, 1, cl_putchar
);
136 (void)fflush(stdout
);
138 (void)fflush(stdout
);
141 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
)) {
142 if (clp
->smso
== NULL
)
145 (void)tputs(clp
->smso
, 1, cl_putchar
);
147 (void)tputs(clp
->rmso
, 1, cl_putchar
);
148 (void)fflush(stdout
);
164 * Return the baud rate.
166 * PUBLIC: int cl_baud __P((SCR *, u_long *));
177 * There's no portable way to get a "baud rate" -- cfgetospeed(3)
178 * returns the value associated with some #define, which we may
179 * never have heard of, or which may be a purely local speed. Vi
180 * only cares if it's SLOW (w300), slow (w1200) or fast (w9600).
181 * Try and detect the slow ones, and default to fast.
184 switch (cfgetospeed(&clp
->orig
)) {
207 * Ring the bell/flash the screen.
209 * PUBLIC: int cl_bell __P((SCR *));
215 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
))
216 (void)write(STDOUT_FILENO
, "\07", 1); /* \a */
219 * Vi has an edit option which determines if the terminal
220 * should be beeped or the screen flashed.
222 if (O_ISSET(sp
, O_FLASH
))
232 * Clear from the current cursor to the end of the line.
234 * PUBLIC: int cl_clrtoeol __P((SCR *));
243 /* The cursor must be returned to its original position. */
246 ((sp
->coff
+ sp
->cols
) - x
) - 1; spcnt
> 0; --spcnt
)
251 return (clrtoeol() == ERR
);
256 * Return the current cursor position.
258 * PUBLIC: int cl_cursor __P((SCR *, size_t *, size_t *));
261 cl_cursor(sp
, yp
, xp
)
266 * The curses screen support splits a single underlying curses screen
267 * into multiple screens to support split screen semantics. For this
268 * reason the returned value must be adjusted to be relative to the
269 * current screen, and not absolute. Screens that implement the split
270 * using physically distinct screens won't need this hack.
272 getyx(stdscr
, *yp
, *xp
);
280 * Delete the current line, scrolling all lines below it.
282 * PUBLIC: int cl_deleteln __P((SCR *));
290 size_t col
, lno
, spcnt
, y
, x
;
295 * This clause is required because the curses screen uses reverse
296 * video to delimit split screens. If the screen does not do this,
297 * this code won't be necessary.
299 * If the bottom line was in reverse video, rewrite it in normal
300 * video before it's scrolled.
302 * Check for the existence of a chgat function; XSI requires it, but
303 * historic implementations of System V curses don't. If it's not
304 * a #define, we'll fall back to doing it by hand, which is slow but
307 * By hand means walking through the line, retrieving and rewriting
308 * each character. Curses has no EOL marker, so track strings of
309 * spaces, and copy the trailing spaces only if there's a non-space
310 * character following.
312 if (!F_ISSET(sp
, SC_SCR_EXWROTE
) && IS_SPLIT(sp
)) {
315 mvchgat(RLNO(sp
, LASTLINE(sp
)), 0, -1, A_NORMAL
, 0, NULL
);
317 for (lno
= RLNO(sp
, LASTLINE(sp
)), col
= spcnt
= 0;;) {
318 (void)move(lno
, col
);
323 (void)move(lno
, col
- spcnt
);
324 for (; spcnt
> 0; --spcnt
)
328 if (++col
>= sp
->cols
)
336 * The bottom line is expected to be blank after this operation,
337 * and other screens must support that semantic.
339 return (deleteln() == ERR
);
346 * PUBLIC: int cl_discard __P((SCR *, SCR *));
349 cl_discard(discardp
, acquirep
)
350 SCR
*discardp
, *acquirep
;
355 F_SET(clp
, CL_LAYOUT
);
357 /* discardp is going away, acquirep is taking up its space. */
363 * Adjust the screen for ex. This routine is purely for standalone
364 * ex programs. All special purpose, all special case.
366 * PUBLIC: int cl_ex_adjust __P((SCR *, exadj_t));
369 cl_ex_adjust(sp
, action
)
379 /* Move the cursor up one line if that's possible. */
380 if (clp
->cuu1
!= NULL
)
381 (void)tputs(clp
->cuu1
, 1, cl_putchar
);
382 else if (clp
->cup
!= NULL
)
383 (void)tputs(tgoto(clp
->cup
,
384 0, LINES
- 2), 1, cl_putchar
);
389 /* Clear the line. */
390 if (clp
->el
!= NULL
) {
392 (void)tputs(clp
->el
, 1, cl_putchar
);
395 * Historically, ex didn't erase the line, so, if the
396 * displayed line was only a single glyph, and <eof>
397 * was more than one glyph, the output would not fully
398 * overwrite the user's input. To fix this, output
399 * the maxiumum character number of spaces. Note,
400 * this won't help if the user entered extra prompt
401 * or <blank> characters before the command character.
402 * We'd have to do a lot of work to make that work, and
403 * it's almost certainly not worth the effort.
405 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
407 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
410 (void)fflush(stdout
);
421 * Push down the current line, discarding the bottom line.
423 * PUBLIC: int cl_insertln __P((SCR *));
430 * The current line is expected to be blank after this operation,
431 * and the screen must support that semantic.
433 return (insertln() == ERR
);
438 * Return the value for a special key.
440 * PUBLIC: int cl_keyval __P((SCR *, scr_keyval_t, CHAR_T *, int *));
443 cl_keyval(sp
, val
, chp
, dnep
)
452 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
453 * VWERASE is a 4BSD extension.
458 *dnep
= (*chp
= clp
->orig
.c_cc
[VEOF
]) == _POSIX_VDISABLE
;
461 *dnep
= (*chp
= clp
->orig
.c_cc
[VERASE
]) == _POSIX_VDISABLE
;
464 *dnep
= (*chp
= clp
->orig
.c_cc
[VKILL
]) == _POSIX_VDISABLE
;
468 *dnep
= (*chp
= clp
->orig
.c_cc
[VWERASE
]) == _POSIX_VDISABLE
;
482 * PUBLIC: int cl_move __P((SCR *, size_t, size_t));
485 cl_move(sp
, lno
, cno
)
489 /* See the comment in cl_cursor. */
490 if (move(RLNO(sp
, lno
), RCNO(sp
, cno
)) == ERR
) {
491 msgq(sp
, M_ERR
, "Error: move: l(%u + %u) c(%u + %u)",
492 lno
, sp
->roff
, cno
, sp
->coff
);
500 * Refresh the screen.
502 * PUBLIC: int cl_refresh __P((SCR *, int));
505 cl_refresh(sp
, repaint
)
518 * If we received a killer signal, we're done, there's no point
519 * in refreshing the screen.
525 * If repaint is set, the editor is telling us that we don't know
526 * what's on the screen, so we have to repaint from scratch.
528 * If repaint set or the screen layout changed, we need to redraw
529 * any lines separating vertically split screens. If the horizontal
530 * offsets are the same, then the split was vertical, and need to
531 * draw a dividing line.
533 if (repaint
|| F_ISSET(clp
, CL_LAYOUT
)) {
535 for (psp
= sp
; psp
!= (void *)&gp
->dq
; psp
= psp
->q
.cqe_next
)
536 for (tsp
= psp
->q
.cqe_next
;
537 tsp
!= (void *)&gp
->dq
; tsp
= tsp
->q
.cqe_next
)
538 if (psp
->roff
== tsp
->roff
)
541 F_CLR(clp
, CL_LAYOUT
);
545 * In the curses library, doing wrefresh(curscr) is okay, but the
546 * screen flashes when we then apply the refresh() to bring it up
547 * to date. So, use clearok().
551 return (refresh() == ERR
);
556 * Draw a dividing line between two vertically split screens.
564 for (cnt
= 0; cnt
< sp
->rows
- 1; ++cnt
) {
565 move(sp
->roff
+ cnt
, sp
->cols
+ 1);
574 * PUBLIC: int cl_rename __P((SCR *, char *, int));
577 cl_rename(sp
, name
, on
)
589 ttype
= OG_STR(gp
, GO_TERM
);
593 * We can only rename windows for xterm.
596 if (F_ISSET(clp
, CL_RENAME_OK
) &&
597 !strncmp(ttype
, "xterm", sizeof("xterm") - 1)) {
598 F_SET(clp
, CL_RENAME
);
599 (void)printf(XTERM_RENAME
, name
);
600 (void)fflush(stdout
);
603 if (F_ISSET(clp
, CL_RENAME
)) {
604 F_CLR(clp
, CL_RENAME
);
605 (void)printf(XTERM_RENAME
, ttype
);
606 (void)fflush(stdout
);
615 * PUBLIC: int cl_split __P((SCR *, SCR *));
618 cl_split(origp
, newp
)
624 F_SET(clp
, CL_LAYOUT
);
626 /* origp is the original screen, giving up space to newp. */
634 * PUBLIC: int cl_suspend __P((SCR *, int *));
637 cl_suspend(sp
, allowedp
)
652 * The ex implementation of this function isn't needed by screens not
653 * supporting ex commands that require full terminal canonical mode
656 * The vi implementation of this function isn't needed by screens not
657 * supporting vi process suspension, i.e. any screen that isn't backed
660 * Setting allowedp to 0 will cause the editor to reject the command.
662 if (F_ISSET(sp
, SC_EX
)) {
663 /* Save the terminal settings, and restore the original ones. */
664 if (F_ISSET(clp
, CL_STDIN_TTY
)) {
665 (void)tcgetattr(STDIN_FILENO
, &t
);
666 (void)tcsetattr(STDIN_FILENO
,
667 TCSASOFT
| TCSADRAIN
, &clp
->orig
);
670 /* Stop the process group. */
671 (void)kill(0, SIGTSTP
);
673 /* Time passes ... */
675 /* Restore terminal settings. */
676 if (F_ISSET(clp
, CL_STDIN_TTY
))
677 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
682 * Move to the lower left-hand corner of the screen.
685 * Not sure this is necessary in System V implementations, but it
689 (void)move(LINES
- 1, 0);
693 * Temporarily end the screen. System V introduced a semantic where
694 * endwin() could be restarted. We use it because restarting curses
695 * from scratch often fails in System V. 4BSD curses didn't support
696 * restarting after endwin(), so we have to do what clean up we can
697 * without calling it.
699 #ifdef HAVE_BSD_CURSES
700 /* Save the terminal settings. */
701 (void)tcgetattr(STDIN_FILENO
, &t
);
704 /* Restore the cursor keys to normal mode. */
705 (void)keypad(stdscr
, FALSE
);
707 /* Restore the window name. */
708 (void)cl_rename(sp
, NULL
, 0);
710 #ifdef HAVE_BSD_CURSES
711 (void)cl_attr(sp
, SA_ALTERNATE
, 0);
717 * Restore the original terminal settings. This is bad -- the
718 * reset can cause character loss from the tty queue. However,
719 * we can't call endwin() in BSD curses implementations, and too
720 * many System V curses implementations don't get it right.
722 (void)tcsetattr(STDIN_FILENO
, TCSADRAIN
| TCSASOFT
, &clp
->orig
);
724 /* Stop the process group. */
725 (void)kill(0, SIGTSTP
);
727 /* Time passes ... */
730 * If we received a killer signal, we're done. Leave everything
731 * unchanged. In addition, the terminal has already been reset
732 * correctly, so leave it alone.
734 if (clp
->killersig
) {
735 F_CLR(clp
, CL_SCR_EX_INIT
| CL_SCR_VI_INIT
);
739 #ifdef HAVE_BSD_CURSES
740 /* Restore terminal settings. */
741 if (F_ISSET(clp
, CL_STDIN_TTY
))
742 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
744 (void)cl_attr(sp
, SA_ALTERNATE
, 1);
747 /* Set the window name. */
748 (void)cl_rename(sp
, sp
->frp
->name
, 1);
750 /* Put the cursor keys into application mode. */
751 (void)keypad(stdscr
, TRUE
);
753 /* Refresh and repaint the screen. */
755 (void)cl_refresh(sp
, 1);
757 /* If the screen changed size, set the SIGWINCH bit. */
758 if (cl_ssize(sp
, 1, NULL
, NULL
, &changed
))
761 F_SET(CLP(sp
), CL_SIGWINCH
);
768 * Print out the curses usage messages.
770 * PUBLIC: void cl_usage __P((void));
776 usage: ex [-eFRrSsv] [-c command] [-t tag] [-w size] [file ...]\n\
777 usage: vi [-eFlRrSv] [-c command] [-t tag] [-w size] [file ...]\n"
778 (void)fprintf(stderr
, "%s", USAGE
);
785 * Stub routine so can flush out curses screen changes using gdb.
791 return (0); /* XXX Convince gdb to run it. */