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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
28 #include "../common/common.h"
32 static void cl_rdiv(SCR *);
35 addstr4(SCR *sp, void *str, size_t len, int wide)
43 win = CLSP(sp) ? CLSP(sp) : stdscr;
46 * If ex isn't in control, it's the last line of the screen and
47 * it's a split screen, use inverse video.
51 if (!F_ISSET(sp, SC_SCR_EXWROTE) &&
52 y == RLNO(sp, LASTLINE(sp)) && IS_SPLIT(sp)) {
59 if (waddnwstr(win, str, len) == ERR)
63 if (waddnstr(win, str, len) == ERR)
73 * Add len bytes from the string at the cursor, advancing the cursor.
75 * PUBLIC: int cl_waddstr(SCR *, const CHAR_T *, size_t);
78 cl_waddstr(SCR *sp, const CHAR_T *str, size_t len)
80 return addstr4(sp, (void *)str, len, 1);
85 * Add len bytes from the string at the cursor, advancing the cursor.
87 * PUBLIC: int cl_addstr(SCR *, const char *, size_t);
90 cl_addstr(SCR *sp, const char *str, size_t len)
92 return addstr4(sp, (void *)str, len, 0);
97 * Toggle a screen attribute on/off.
99 * PUBLIC: int cl_attr(SCR *, scr_attr_t, int);
102 cl_attr(SCR *sp, scr_attr_t attribute, int on)
108 win = CLSP(sp) ? CLSP(sp) : stdscr;
114 * There's a major layering violation here. The problem is that the
115 * X11 xterm screen has what's known as an "alternate" screen. Some
116 * xterm termcap/terminfo entries include sequences to switch to/from
117 * that alternate screen as part of the ti/te (smcup/rmcup) strings.
118 * Vi runs in the alternate screen, so that you are returned to the
119 * same screen contents on exit from vi that you had when you entered
120 * vi. Further, when you run :shell, or :!date or similar ex commands,
121 * you also see the original screen contents. This wasn't deliberate
122 * on vi's part, it's just that it historically sent terminal init/end
123 * sequences at those times, and the addition of the alternate screen
124 * sequences to the strings changed the behavior of vi. The problem
125 * caused by this is that we don't want to switch back to the alternate
126 * screen while getting a new command from the user, when the user is
127 * continuing to enter ex commands, e.g.:
129 * :!date <<< switch to original screen
130 * [Hit return to continue] <<< prompt user to continue
131 * :command <<< get command from user
133 * Note that the :command input is a true vi input mode, e.g., input
134 * maps and abbreviations are being done. So, we need to be able to
135 * switch back into the vi screen mode, without flashing the screen.
137 * To make matters worse, the curses initscr() and endwin() calls will
138 * do this automatically -- so, this attribute isn't as controlled by
139 * the higher level screen as closely as one might like.
142 if (clp->ti_te != TI_SENT) {
143 clp->ti_te = TI_SENT;
144 if (clp->smcup == NULL)
145 (void)cl_getcap(sp, "smcup", &clp->smcup);
146 if (clp->smcup != NULL)
147 (void)tputs(clp->smcup, 1, cl_putchar);
150 if (clp->ti_te != TE_SENT) {
151 clp->ti_te = TE_SENT;
152 if (clp->rmcup == NULL)
153 (void)cl_getcap(sp, "rmcup", &clp->rmcup);
154 if (clp->rmcup != NULL)
155 (void)tputs(clp->rmcup, 1, cl_putchar);
156 (void)fflush(stdout);
158 (void)fflush(stdout);
161 if (F_ISSET(sp, SC_EX | SC_SCR_EXWROTE)) {
162 if (clp->smso == NULL)
165 (void)tputs(clp->smso, 1, cl_putchar);
167 (void)tputs(clp->rmso, 1, cl_putchar);
168 (void)fflush(stdout);
171 (void)wstandout(win);
173 (void)wstandend(win);
184 * Return the baud rate.
186 * PUBLIC: int cl_baud(SCR *, u_long *);
189 cl_baud(SCR *sp, u_long *ratep)
195 * There's no portable way to get a "baud rate" -- cfgetospeed(3)
196 * returns the value associated with some #define, which we may
197 * never have heard of, or which may be a purely local speed. Vi
198 * only cares if it's SLOW (w300), slow (w1200) or fast (w9600).
199 * Try and detect the slow ones, and default to fast.
202 switch (cfgetospeed(&clp->orig)) {
225 * Ring the bell/flash the screen.
227 * PUBLIC: int cl_bell(SCR *);
232 if (F_ISSET(sp, SC_EX | SC_SCR_EXWROTE | SC_SCR_EX))
233 (void)write(STDOUT_FILENO, "\07", 1); /* \a */
236 * Vi has an edit option which determines if the terminal
237 * should be beeped or the screen flashed.
239 if (O_ISSET(sp, O_FLASH))
249 * Clear from the current cursor to the end of the line.
251 * PUBLIC: int cl_clrtoeol(SCR *);
261 win = CLSP(sp) ? CLSP(sp) : stdscr;
265 /* The cursor must be returned to its original position. */
267 for (spcnt = (sp->coff + sp->cols) - x; spcnt > 0; --spcnt)
268 (void)waddch(win, ' ');
269 (void)wmove(win, y, x);
273 return (wclrtoeol(win) == ERR);
278 * Return the current cursor position.
280 * PUBLIC: int cl_cursor(SCR *, size_t *, size_t *);
283 cl_cursor(SCR *sp, size_t *yp, size_t *xp)
286 win = CLSP(sp) ? CLSP(sp) : stdscr;
288 * The curses screen support splits a single underlying curses screen
289 * into multiple screens to support split screen semantics. For this
290 * reason the returned value must be adjusted to be relative to the
291 * current screen, and not absolute. Screens that implement the split
292 * using physically distinct screens won't need this hack.
294 getyx(win, *yp, *xp);
304 * Delete the current line, scrolling all lines below it.
306 * PUBLIC: int cl_deleteln(SCR *);
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 if (!F_ISSET(sp, SC_SCR_EXWROTE) && IS_SPLIT(sp)) {
328 mvwchgat(win, RLNO(sp, LASTLINE(sp)), 0, -1, A_NORMAL, 0, NULL);
329 (void)wmove(win, y, x);
333 * The bottom line is expected to be blank after this operation,
334 * and other screens must support that semantic.
336 return (wdeleteln(win) == ERR);
343 * PUBLIC: int cl_discard(SCR *, SCR **);
346 cl_discard(SCR *discardp, SCR **acquirep)
353 F_SET(clp, CL_LAYOUT);
355 if (CLSP(discardp)) {
356 delwin(CLSP(discardp));
357 discardp->cl_private = NULL;
361 /* no screens got a piece; we're done */
365 for (; (tsp = *acquirep) != NULL; ++acquirep) {
367 F_SET(clp, CL_LAYOUT);
371 tsp->cl_private = subwin(stdscr, tsp->rows, tsp->cols,
372 tsp->roff, tsp->coff);
375 /* discardp is going away, acquirep is taking up its space. */
381 * Adjust the screen for ex. This routine is purely for standalone
382 * ex programs. All special purpose, all special case.
384 * PUBLIC: int cl_ex_adjust(SCR *, exadj_t);
387 cl_ex_adjust(SCR *sp, exadj_t action)
395 /* Move the cursor up one line if that's possible. */
396 if (clp->cuu1 != NULL)
397 (void)tputs(clp->cuu1, 1, cl_putchar);
398 else if (clp->cup != NULL)
399 (void)tputs(tgoto(clp->cup,
400 0, LINES - 2), 1, cl_putchar);
405 /* Clear the line. */
406 if (clp->el != NULL) {
408 (void)tputs(clp->el, 1, cl_putchar);
411 * Historically, ex didn't erase the line, so, if the
412 * displayed line was only a single glyph, and <eof>
413 * was more than one glyph, the output would not fully
414 * overwrite the user's input. To fix this, output
415 * the maxiumum character number of spaces. Note,
416 * this won't help if the user entered extra prompt
417 * or <blank> characters before the command character.
418 * We'd have to do a lot of work to make that work, and
419 * it's almost certainly not worth the effort.
421 for (cnt = 0; cnt < MAX_CHARACTER_COLUMNS; ++cnt)
423 for (cnt = 0; cnt < MAX_CHARACTER_COLUMNS; ++cnt)
426 (void)fflush(stdout);
437 * Push down the current line, discarding the bottom line.
439 * PUBLIC: int cl_insertln(SCR *);
445 win = CLSP(sp) ? CLSP(sp) : stdscr;
447 * The current line is expected to be blank after this operation,
448 * and the screen must support that semantic.
450 return (winsertln(win) == ERR);
455 * Return the value for a special key.
457 * PUBLIC: int cl_keyval(SCR *, scr_keyval_t, CHAR_T *, int *);
460 cl_keyval(SCR *sp, scr_keyval_t val, CHAR_T *chp, int *dnep)
465 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
466 * VWERASE is a 4BSD extension.
471 *dnep = (*chp = clp->orig.c_cc[VEOF]) == _POSIX_VDISABLE;
474 *dnep = (*chp = clp->orig.c_cc[VERASE]) == _POSIX_VDISABLE;
477 *dnep = (*chp = clp->orig.c_cc[VKILL]) == _POSIX_VDISABLE;
481 *dnep = (*chp = clp->orig.c_cc[VWERASE]) == _POSIX_VDISABLE;
495 * PUBLIC: int cl_move(SCR *, size_t, size_t);
498 cl_move(SCR *sp, size_t lno, size_t cno)
501 win = CLSP(sp) ? CLSP(sp) : stdscr;
502 /* See the comment in cl_cursor. */
503 if (wmove(win, RLNO(sp, lno), RCNO(sp, cno)) == ERR) {
504 msgq(sp, M_ERR, "Error: move: l(%zu + %zu) c(%zu + %zu)",
505 lno, sp->roff, cno, sp->coff);
513 * Refresh the screen.
515 * PUBLIC: int cl_refresh(SCR *, int);
518 cl_refresh(SCR *sp, int repaint)
528 win = CLSP(sp) ? CLSP(sp) : stdscr;
531 * If we received a killer signal, we're done, there's no point
532 * in refreshing the screen.
538 * If repaint is set, the editor is telling us that we don't know
539 * what's on the screen, so we have to repaint from scratch.
541 * If repaint set or the screen layout changed, we need to redraw
542 * any lines separating vertically split screens. If the horizontal
543 * offsets are the same, then the split was vertical, and need to
544 * draw a dividing line.
546 if (repaint || F_ISSET(clp, CL_LAYOUT)) {
548 for (psp = sp; psp != NULL; psp = TAILQ_NEXT(psp, q))
549 for (tsp = TAILQ_NEXT(psp, q); tsp != NULL;
550 tsp = TAILQ_NEXT(tsp, q))
551 if (psp->roff == tsp->roff) {
552 if (psp->coff + psp->cols + 1 == tsp->coff)
555 if (tsp->coff + tsp->cols + 1 == psp->coff)
558 (void)wmove(stdscr, y, x);
559 F_CLR(clp, CL_LAYOUT);
563 * In the curses library, doing wrefresh(curscr) is okay, but the
564 * screen flashes when we then apply the refresh() to bring it up
565 * to date. So, use clearok().
570 * Only do an actual refresh, when this is the focus window,
571 * i.e. the one holding the cursor. This assumes that refresh
572 * is called for that window after refreshing the others.
573 * This prevents the cursor being drawn in the other windows.
575 return (wnoutrefresh(stdscr) == ERR ||
576 wnoutrefresh(win) == ERR ||
577 (sp == clp->focus && doupdate() == ERR));
582 * Draw a dividing line between two vertically split screens.
588 mvvline(sp->roff, sp->cols + sp->coff, '|', sp->rows);
590 mvvline(sp->roff, sp->cols + sp->coff, ACS_VLINE, sp->rows);
598 * PUBLIC: int cl_rename(SCR *, char *, int);
601 cl_rename(SCR *sp, char *name, int on)
606 char buf[256], *s, *e;
615 * We can only rename windows for xterm.
619 if (!F_ISSET(clp, CL_RENAME_OK) ||
620 strncmp(OG_STR(gp, GO_TERM), "xterm", 5))
623 if (clp->oname == NULL && (wid = getenv("WINDOWID"))) {
624 snprintf(cmd, sizeof(cmd), "xprop -id %s WM_NAME", wid);
625 if ((pfp = popen(cmd, "r")) == NULL)
627 if (fgets(buf, sizeof(buf), pfp) == NULL) {
632 if ((s = strchr(buf, '"')) != NULL &&
633 (e = strrchr(buf, '"')) != NULL)
634 clp->oname = strndup(s + 1, e - s - 1);
637 rename: cl_setname(gp, name);
639 F_SET(clp, CL_RENAME);
641 if (F_ISSET(clp, CL_RENAME)) {
642 cl_setname(gp, clp->oname);
644 F_CLR(clp, CL_RENAME);
651 * Set a X11 icon/window name.
653 * PUBLIC: void cl_setname(GS *, char *);
656 cl_setname(GS *gp, char *name)
658 /* X11 xterm escape sequence to rename the icon/window. */
659 #define XTERM_RENAME "\033]0;%s\007"
661 (void)printf(XTERM_RENAME, name == NULL ? OG_STR(gp, GO_TERM) : name);
662 (void)fflush(stdout);
670 * PUBLIC: int cl_split(SCR *, SCR *);
673 cl_split(SCR *origp, SCR *newp)
678 F_SET(clp, CL_LAYOUT);
683 origp->cl_private = subwin(stdscr, origp->rows, origp->cols,
684 origp->roff, origp->coff);
685 newp->cl_private = subwin(stdscr, newp->rows, newp->cols,
686 newp->roff, newp->coff);
688 /* origp is the original screen, giving up space to newp. */
696 * PUBLIC: int cl_suspend(SCR *, int *);
699 cl_suspend(SCR *sp, int *allowedp)
710 win = CLSP(sp) ? CLSP(sp) : stdscr;
714 * The ex implementation of this function isn't needed by screens not
715 * supporting ex commands that require full terminal canonical mode
718 * The vi implementation of this function isn't needed by screens not
719 * supporting vi process suspension, i.e. any screen that isn't backed
722 * Setting allowedp to 0 will cause the editor to reject the command.
724 if (F_ISSET(sp, SC_EX)) {
725 /* Save the terminal settings, and restore the original ones. */
726 if (F_ISSET(clp, CL_STDIN_TTY)) {
727 (void)tcgetattr(STDIN_FILENO, &t);
728 (void)tcsetattr(STDIN_FILENO,
729 TCSASOFT | TCSADRAIN, &clp->orig);
732 /* Stop the process group. */
733 (void)kill(0, SIGTSTP);
735 /* Time passes ... */
737 /* Restore terminal settings. */
738 if (F_ISSET(clp, CL_STDIN_TTY))
739 (void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &t);
744 * Move to the lower left-hand corner of the screen.
747 * Not sure this is necessary in System V implementations, but it
751 (void)wmove(win, LINES - 1, 0);
755 * Temporarily end the screen. System V introduced a semantic where
756 * endwin() could be restarted. We use it because restarting curses
757 * from scratch often fails in System V. 4BSD curses didn't support
758 * restarting after endwin(), so we have to do what clean up we can
759 * without calling it.
761 /* Save the terminal settings. */
762 (void)tcgetattr(STDIN_FILENO, &t);
764 /* Restore the cursor keys to normal mode. */
765 (void)keypad(stdscr, FALSE);
767 /* Restore the window name. */
768 (void)cl_rename(sp, NULL, 0);
774 * Restore the original terminal settings. This is bad -- the
775 * reset can cause character loss from the tty queue. However,
776 * we can't call endwin() in BSD curses implementations, and too
777 * many System V curses implementations don't get it right.
779 (void)tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->orig);
781 /* Stop the process group. */
782 (void)kill(0, SIGTSTP);
784 /* Time passes ... */
787 * If we received a killer signal, we're done. Leave everything
788 * unchanged. In addition, the terminal has already been reset
789 * correctly, so leave it alone.
791 if (clp->killersig) {
792 F_CLR(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT);
796 /* Restore terminal settings. */
797 wrefresh(win); /* Needed on SunOs/Solaris ? */
798 if (F_ISSET(clp, CL_STDIN_TTY))
799 (void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &t);
801 /* Set the window name. */
802 (void)cl_rename(sp, sp->frp->name, 1);
804 /* Put the cursor keys into application mode. */
805 (void)keypad(stdscr, TRUE);
807 /* Refresh and repaint the screen. */
808 (void)wmove(win, y, x);
809 (void)cl_refresh(sp, 1);
811 /* If the screen changed size, set the SIGWINCH bit. */
812 if (cl_ssize(sp, 1, NULL, NULL, &changed))
815 F_SET(CLP(sp), CL_SIGWINCH);
822 * Print out the curses usage messages.
824 * PUBLIC: void cl_usage(void);
830 usage: ex [-eFRrSsv] [-c command] [-t tag] [-w size] [file ...]\n\
831 usage: vi [-eFlRrSv] [-c command] [-t tag] [-w size] [file ...]\n"
832 (void)fprintf(stderr, "%s", USAGE);
839 * Stub routine so can flush out curses screen changes using gdb.
842 __attribute__((unused))
846 return (0); /* XXX Convince gdb to run it. */