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
[] = "@(#)cl_funcs.c 10.50 (Berkeley) 9/24/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
31 #include "../common/common.h"
37 * Add len bytes from the string at the cursor, advancing the cursor.
39 * PUBLIC: int cl_addstr __P((SCR *, const char *, size_t));
42 cl_addstr(sp
, str
, len
)
54 * If ex isn't in control, it's the last line of the screen and
55 * it's a split screen, use inverse video.
58 getyx(stdscr
, oldy
, oldx
);
59 if (!F_ISSET(sp
, SC_SCR_EXWROTE
) &&
60 oldy
== RLNO(sp
, LASTLINE(sp
)) && IS_SPLIT(sp
)) {
65 if (addnstr(str
, len
) == ERR
)
75 * Toggle a screen attribute on/off.
77 * PUBLIC: int cl_attr __P((SCR *, scr_attr_t, int));
80 cl_attr(sp
, attribute
, on
)
93 * There's a major layering violation here. The problem is that the
94 * X11 xterm screen has what's known as an "alternate" screen. Some
95 * xterm termcap/terminfo entries include sequences to switch to/from
96 * that alternate screen as part of the ti/te (smcup/rmcup) strings.
97 * Vi runs in the alternate screen, so that you are returned to the
98 * same screen contents on exit from vi that you had when you entered
99 * vi. Further, when you run :shell, or :!date or similar ex commands,
100 * you also see the original screen contents. This wasn't deliberate
101 * on vi's part, it's just that it historically sent terminal init/end
102 * sequences at those times, and the addition of the alternate screen
103 * sequences to the strings changed the behavior of vi. The problem
104 * caused by this is that we don't want to switch back to the alternate
105 * screen while getting a new command from the user, when the user is
106 * continuing to enter ex commands, e.g.:
108 * :!date <<< switch to original screen
109 * [Hit return to continue] <<< prompt user to continue
110 * :command <<< get command from user
112 * Note that the :command input is a true vi input mode, e.g., input
113 * maps and abbreviations are being done. So, we need to be able to
114 * switch back into the vi screen mode, without flashing the screen.
116 * To make matters worse, the curses initscr() and endwin() calls will
117 * do this automatically -- so, this attribute isn't as controlled by
118 * the higher level screen as closely as one might like.
121 if (clp
->ti_te
!= TI_SENT
) {
122 clp
->ti_te
= TI_SENT
;
123 if (clp
->smcup
== NULL
)
124 (void)cl_getcap(sp
, "smcup", &clp
->smcup
);
125 if (clp
->smcup
!= NULL
)
126 (void)tputs(clp
->smcup
, 1, cl_putchar
);
129 if (clp
->ti_te
!= TE_SENT
) {
130 clp
->ti_te
= TE_SENT
;
131 if (clp
->rmcup
== NULL
)
132 (void)cl_getcap(sp
, "rmcup", &clp
->rmcup
);
133 if (clp
->rmcup
!= NULL
)
134 (void)tputs(clp
->rmcup
, 1, cl_putchar
);
135 (void)fflush(stdout
);
137 (void)fflush(stdout
);
140 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
)) {
141 if (clp
->smso
== NULL
)
144 (void)tputs(clp
->smso
, 1, cl_putchar
);
146 (void)tputs(clp
->rmso
, 1, cl_putchar
);
147 (void)fflush(stdout
);
163 * Return the baud rate.
165 * PUBLIC: int cl_baud __P((SCR *, u_long *));
176 * There's no portable way to get a "baud rate" -- cfgetospeed(3)
177 * returns the value associated with some #define, which we may
178 * never have heard of, or which may be a purely local speed. Vi
179 * only cares if it's SLOW (w300), slow (w1200) or fast (w9600).
180 * Try and detect the slow ones, and default to fast.
183 switch (cfgetospeed(&clp
->orig
)) {
206 * Ring the bell/flash the screen.
208 * PUBLIC: int cl_bell __P((SCR *));
214 if (F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
))
215 (void)write(STDOUT_FILENO
, "\07", 1); /* \a */
218 * Vi has an edit option which determines if the terminal
219 * should be beeped or the screen flashed.
221 if (O_ISSET(sp
, O_FLASH
))
231 * Clear from the current cursor to the end of the line.
233 * PUBLIC: int cl_clrtoeol __P((SCR *));
239 return (clrtoeol() == ERR
);
244 * Return the current cursor position.
246 * PUBLIC: int cl_cursor __P((SCR *, size_t *, size_t *));
249 cl_cursor(sp
, yp
, xp
)
254 * The curses screen support splits a single underlying curses screen
255 * into multiple screens to support split screen semantics. For this
256 * reason the returned value must be adjusted to be relative to the
257 * current screen, and not absolute. Screens that implement the split
258 * using physically distinct screens won't need this hack.
260 getyx(stdscr
, *yp
, *xp
);
267 * Delete the current line, scrolling all lines below it.
269 * PUBLIC: int cl_deleteln __P((SCR *));
277 size_t col
, lno
, spcnt
, oldy
, oldx
;
282 * This clause is required because the curses screen uses reverse
283 * video to delimit split screens. If the screen does not do this,
284 * this code won't be necessary.
286 * If the bottom line was in reverse video, rewrite it in normal
287 * video before it's scrolled.
289 * Check for the existence of a chgat function; XSI requires it, but
290 * historic implementations of System V curses don't. If it's not
291 * a #define, we'll fall back to doing it by hand, which is slow but
294 * By hand means walking through the line, retrieving and rewriting
295 * each character. Curses has no EOL marker, so track strings of
296 * spaces, and copy the trailing spaces only if there's a non-space
297 * character following.
299 if (!F_ISSET(sp
, SC_SCR_EXWROTE
) && IS_SPLIT(sp
)) {
300 getyx(stdscr
, oldy
, oldx
);
302 mvchgat(RLNO(sp
, LASTLINE(sp
)), 0, -1, A_NORMAL
, 0, NULL
);
304 for (lno
= RLNO(sp
, LASTLINE(sp
)), col
= spcnt
= 0;;) {
305 (void)move(lno
, col
);
310 (void)move(lno
, col
- spcnt
);
311 for (; spcnt
> 0; --spcnt
)
315 if (++col
>= sp
->cols
)
319 (void)move(oldy
, oldx
);
323 * The bottom line is expected to be blank after this operation,
324 * and other screens must support that semantic.
326 return (deleteln() == ERR
);
331 * Adjust the screen for ex. This routine is purely for standalone
332 * ex programs. All special purpose, all special case.
334 * PUBLIC: int cl_ex_adjust __P((SCR *, exadj_t));
337 cl_ex_adjust(sp
, action
)
347 /* Move the cursor up one line if that's possible. */
348 if (clp
->cuu1
!= NULL
)
349 (void)tputs(clp
->cuu1
, 1, cl_putchar
);
350 else if (clp
->cup
!= NULL
)
351 (void)tputs(tgoto(clp
->cup
,
352 0, LINES
- 2), 1, cl_putchar
);
357 /* Clear the line. */
358 if (clp
->el
!= NULL
) {
360 (void)tputs(clp
->el
, 1, cl_putchar
);
363 * Historically, ex didn't erase the line, so, if the
364 * displayed line was only a single glyph, and <eof>
365 * was more than one glyph, the output would not fully
366 * overwrite the user's input. To fix this, output
367 * the maxiumum character number of spaces. Note,
368 * this won't help if the user entered extra prompt
369 * or <blank> characters before the command character.
370 * We'd have to do a lot of work to make that work, and
371 * it's almost certainly not worth the effort.
373 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
375 for (cnt
= 0; cnt
< MAX_CHARACTER_COLUMNS
; ++cnt
)
378 (void)fflush(stdout
);
389 * Push down the current line, discarding the bottom line.
391 * PUBLIC: int cl_insertln __P((SCR *));
398 * The current line is expected to be blank after this operation,
399 * and the screen must support that semantic.
401 return (insertln() == ERR
);
406 * Return the value for a special key.
408 * PUBLIC: int cl_keyval __P((SCR *, scr_keyval_t, CHAR_T *, int *));
411 cl_keyval(sp
, val
, chp
, dnep
)
420 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
421 * VWERASE is a 4BSD extension.
426 *dnep
= (*chp
= clp
->orig
.c_cc
[VEOF
]) == _POSIX_VDISABLE
;
429 *dnep
= (*chp
= clp
->orig
.c_cc
[VERASE
]) == _POSIX_VDISABLE
;
432 *dnep
= (*chp
= clp
->orig
.c_cc
[VKILL
]) == _POSIX_VDISABLE
;
436 *dnep
= (*chp
= clp
->orig
.c_cc
[VWERASE
]) == _POSIX_VDISABLE
;
450 * PUBLIC: int cl_move __P((SCR *, size_t, size_t));
453 cl_move(sp
, lno
, cno
)
457 /* See the comment in cl_cursor. */
458 if (move(RLNO(sp
, lno
), cno
) == ERR
) {
460 "Error: move: l(%u) c(%u) o(%u)", lno
, cno
, sp
->woff
);
468 * Refresh the screen.
470 * PUBLIC: int cl_refresh __P((SCR *, int));
473 cl_refresh(sp
, repaint
)
482 * If we received a killer signal, we're done, there's no point
483 * in refreshing the screen.
489 * If repaint is set, the editor is telling us that we don't know
490 * what's on the screen, so we have to repaint from scratch.
492 * In the curses library, doing wrefresh(curscr) is okay, but the
493 * screen flashes when we then apply the refresh() to bring it up
494 * to date. So, use clearok().
498 return (refresh() == ERR
);
505 * PUBLIC: int cl_rename __P((SCR *, char *, int));
508 cl_rename(sp
, name
, on
)
520 ttype
= OG_STR(gp
, GO_TERM
);
524 * We can only rename windows for xterm.
527 if (F_ISSET(clp
, CL_RENAME_OK
) &&
528 !strncmp(ttype
, "xterm", sizeof("xterm") - 1)) {
529 F_SET(clp
, CL_RENAME
);
530 (void)printf(XTERM_RENAME
, name
);
531 (void)fflush(stdout
);
534 if (F_ISSET(clp
, CL_RENAME
)) {
535 F_CLR(clp
, CL_RENAME
);
536 (void)printf(XTERM_RENAME
, ttype
);
537 (void)fflush(stdout
);
546 * PUBLIC: int cl_suspend __P((SCR *, int *));
549 cl_suspend(sp
, allowedp
)
564 * The ex implementation of this function isn't needed by screens not
565 * supporting ex commands that require full terminal canonical mode
568 * The vi implementation of this function isn't needed by screens not
569 * supporting vi process suspension, i.e. any screen that isn't backed
572 * Setting allowedp to 0 will cause the editor to reject the command.
574 if (F_ISSET(sp
, SC_EX
)) {
575 /* Save the terminal settings, and restore the original ones. */
576 if (F_ISSET(clp
, CL_STDIN_TTY
)) {
577 (void)tcgetattr(STDIN_FILENO
, &t
);
578 (void)tcsetattr(STDIN_FILENO
,
579 TCSASOFT
| TCSADRAIN
, &clp
->orig
);
582 /* Stop the process group. */
583 (void)kill(0, SIGTSTP
);
585 /* Time passes ... */
587 /* Restore terminal settings. */
588 if (F_ISSET(clp
, CL_STDIN_TTY
))
589 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
594 * Move to the lower left-hand corner of the screen.
597 * Not sure this is necessary in System V implementations, but it
600 getyx(stdscr
, oldy
, oldx
);
601 (void)move(LINES
- 1, 0);
605 * Temporarily end the screen. System V introduced a semantic where
606 * endwin() could be restarted. We use it because restarting curses
607 * from scratch often fails in System V. 4BSD curses didn't support
608 * restarting after endwin(), so we have to do what clean up we can
609 * without calling it.
611 #ifdef HAVE_BSD_CURSES
612 /* Save the terminal settings. */
613 (void)tcgetattr(STDIN_FILENO
, &t
);
616 /* Restore the cursor keys to normal mode. */
617 (void)keypad(stdscr
, FALSE
);
619 /* Restore the window name. */
620 (void)cl_rename(sp
, NULL
, 0);
622 #ifdef HAVE_BSD_CURSES
623 (void)cl_attr(sp
, SA_ALTERNATE
, 0);
629 * Restore the original terminal settings. This is bad -- the
630 * reset can cause character loss from the tty queue. However,
631 * we can't call endwin() in BSD curses implementations, and too
632 * many System V curses implementations don't get it right.
634 (void)tcsetattr(STDIN_FILENO
, TCSADRAIN
| TCSASOFT
, &clp
->orig
);
636 /* Stop the process group. */
637 (void)kill(0, SIGTSTP
);
639 /* Time passes ... */
642 * If we received a killer signal, we're done. Leave everything
643 * unchanged. In addition, the terminal has already been reset
644 * correctly, so leave it alone.
646 if (clp
->killersig
) {
647 F_CLR(clp
, CL_SCR_EX_INIT
| CL_SCR_VI_INIT
);
651 #ifdef HAVE_BSD_CURSES
652 /* Restore terminal settings. */
653 if (F_ISSET(clp
, CL_STDIN_TTY
))
654 (void)tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &t
);
656 (void)cl_attr(sp
, SA_ALTERNATE
, 1);
659 /* Set the window name. */
660 (void)cl_rename(sp
, sp
->frp
->name
, 1);
662 /* Put the cursor keys into application mode. */
663 (void)keypad(stdscr
, TRUE
);
665 /* Refresh and repaint the screen. */
666 (void)move(oldy
, oldx
);
667 (void)cl_refresh(sp
, 1);
669 /* If the screen changed size, set the SIGWINCH bit. */
670 if (cl_ssize(sp
, 1, NULL
, NULL
, &changed
))
673 F_SET(CLP(sp
), CL_SIGWINCH
);
680 * Print out the curses usage messages.
682 * PUBLIC: void cl_usage __P((void));
688 usage: ex [-eFRrSsv] [-c command] [-t tag] [-w size] [file ...]\n\
689 usage: vi [-eFlRrSv] [-c command] [-t tag] [-w size] [file ...]\n"
690 (void)fprintf(stderr
, "%s", USAGE
);
697 * Stub routine so can flush out curses screen changes using gdb.
703 return (0); /* XXX Convince gdb to run it. */