add vi_send(), vi_translate() to list of exported functions
[nvi.git] / cl / cl_funcs.c
blob41c3453d2d5f01b5c51b076345523aeb40f06eea
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
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 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <curses.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <termios.h>
28 #include <unistd.h>
30 #include "../common/common.h"
31 #include "../vi/vi.h"
32 #include "cl.h"
34 static void cl_rdiv __P((SCR *));
37 * cl_addstr --
38 * Add len bytes from the string at the cursor, advancing the cursor.
40 * PUBLIC: int cl_addstr __P((SCR *, const char *, size_t));
42 int
43 cl_addstr(sp, str, len)
44 SCR *sp;
45 const char *str;
46 size_t len;
48 CL_PRIVATE *clp;
49 size_t y, x;
50 int iv;
52 clp = CLP(sp);
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.
58 iv = 0;
59 getyx(stdscr, y, x);
60 if (!F_ISSET(sp, SC_SCR_EXWROTE) &&
61 y == RLNO(sp, LASTLINE(sp)) && IS_SPLIT(sp)) {
62 iv = 1;
63 (void)standout();
66 if (addnstr(str, len) == ERR)
67 return (1);
69 if (iv)
70 (void)standend();
71 return (0);
75 * cl_attr --
76 * Toggle a screen attribute on/off.
78 * PUBLIC: int cl_attr __P((SCR *, scr_attr_t, int));
80 int
81 cl_attr(sp, attribute, on)
82 SCR *sp;
83 scr_attr_t attribute;
84 int on;
86 CL_PRIVATE *clp;
88 clp = CLP(sp);
90 switch (attribute) {
91 case SA_ALTERNATE:
93 * !!!
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.
121 if (on) {
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);
129 } else
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);
139 break;
140 case SA_INVERSE:
141 if (F_ISSET(sp, SC_EX | SC_SCR_EXWROTE)) {
142 if (clp->smso == NULL)
143 return (1);
144 if (on)
145 (void)tputs(clp->smso, 1, cl_putchar);
146 else
147 (void)tputs(clp->rmso, 1, cl_putchar);
148 (void)fflush(stdout);
149 } else {
150 if (on)
151 (void)standout();
152 else
153 (void)standend();
155 break;
156 default:
157 abort();
159 return (0);
163 * cl_baud --
164 * Return the baud rate.
166 * PUBLIC: int cl_baud __P((SCR *, u_long *));
169 cl_baud(sp, ratep)
170 SCR *sp;
171 u_long *ratep;
173 CL_PRIVATE *clp;
176 * XXX
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.
183 clp = CLP(sp);
184 switch (cfgetospeed(&clp->orig)) {
185 case B50:
186 case B75:
187 case B110:
188 case B134:
189 case B150:
190 case B200:
191 case B300:
192 case B600:
193 *ratep = 600;
194 break;
195 case B1200:
196 *ratep = 1200;
197 break;
198 default:
199 *ratep = 9600;
200 break;
202 return (0);
206 * cl_bell --
207 * Ring the bell/flash the screen.
209 * PUBLIC: int cl_bell __P((SCR *));
212 cl_bell(sp)
213 SCR *sp;
215 if (F_ISSET(sp, SC_EX | SC_SCR_EXWROTE))
216 (void)write(STDOUT_FILENO, "\07", 1); /* \a */
217 else {
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))
223 (void)flash();
224 else
225 (void)beep();
227 return (0);
231 * cl_clrtoeol --
232 * Clear from the current cursor to the end of the line.
234 * PUBLIC: int cl_clrtoeol __P((SCR *));
237 cl_clrtoeol(sp)
238 SCR *sp;
240 size_t spcnt, y, x;
242 if (IS_VSPLIT(sp)) {
243 /* The cursor must be returned to its original position. */
244 getyx(stdscr, y, x);
245 for (spcnt =
246 ((sp->coff + sp->cols) - x) - 1; spcnt > 0; --spcnt)
247 (void)addch(' ');
248 (void)move(y, x);
249 return (0);
250 } else
251 return (clrtoeol() == ERR);
255 * cl_cursor --
256 * Return the current cursor position.
258 * PUBLIC: int cl_cursor __P((SCR *, size_t *, size_t *));
261 cl_cursor(sp, yp, xp)
262 SCR *sp;
263 size_t *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);
273 *yp -= sp->roff;
274 *xp -= sp->coff;
275 return (0);
279 * cl_deleteln --
280 * Delete the current line, scrolling all lines below it.
282 * PUBLIC: int cl_deleteln __P((SCR *));
285 cl_deleteln(sp)
286 SCR *sp;
288 CHAR_T ch;
289 CL_PRIVATE *clp;
290 size_t col, lno, spcnt, y, x;
292 clp = CLP(sp);
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
305 * acceptable.
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)) {
313 getyx(stdscr, y, x);
314 #ifdef mvchgat
315 mvchgat(RLNO(sp, LASTLINE(sp)), 0, -1, A_NORMAL, 0, NULL);
316 #else
317 for (lno = RLNO(sp, LASTLINE(sp)), col = spcnt = 0;;) {
318 (void)move(lno, col);
319 ch = winch(stdscr);
320 if (isblank(ch))
321 ++spcnt;
322 else {
323 (void)move(lno, col - spcnt);
324 for (; spcnt > 0; --spcnt)
325 (void)addch(' ');
326 (void)addch(ch);
328 if (++col >= sp->cols)
329 break;
331 #endif
332 (void)move(y, x);
336 * The bottom line is expected to be blank after this operation,
337 * and other screens must support that semantic.
339 return (deleteln() == ERR);
343 * cl_discard --
344 * Discard a screen.
346 * PUBLIC: int cl_discard __P((SCR *, SCR *));
349 cl_discard(discardp, acquirep)
350 SCR *discardp, *acquirep;
352 CL_PRIVATE *clp;
354 clp = CLP(discardp);
355 F_SET(clp, CL_LAYOUT);
357 /* discardp is going away, acquirep is taking up its space. */
358 return (0);
362 * cl_ex_adjust --
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)
370 SCR *sp;
371 exadj_t action;
373 CL_PRIVATE *clp;
374 int cnt;
376 clp = CLP(sp);
377 switch (action) {
378 case EX_TERM_SCROLL:
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);
385 else
386 return (0);
387 /* FALLTHROUGH */
388 case EX_TERM_CE:
389 /* Clear the line. */
390 if (clp->el != NULL) {
391 (void)putchar('\r');
392 (void)tputs(clp->el, 1, cl_putchar);
393 } else {
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)
406 (void)putchar('\b');
407 for (cnt = 0; cnt < MAX_CHARACTER_COLUMNS; ++cnt)
408 (void)putchar(' ');
409 (void)putchar('\r');
410 (void)fflush(stdout);
412 break;
413 default:
414 abort();
416 return (0);
420 * cl_insertln --
421 * Push down the current line, discarding the bottom line.
423 * PUBLIC: int cl_insertln __P((SCR *));
426 cl_insertln(sp)
427 SCR *sp;
430 * The current line is expected to be blank after this operation,
431 * and the screen must support that semantic.
433 return (insertln() == ERR);
437 * cl_keyval --
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)
444 SCR *sp;
445 scr_keyval_t val;
446 CHAR_T *chp;
447 int *dnep;
449 CL_PRIVATE *clp;
452 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
453 * VWERASE is a 4BSD extension.
455 clp = CLP(sp);
456 switch (val) {
457 case KEY_VEOF:
458 *dnep = (*chp = clp->orig.c_cc[VEOF]) == _POSIX_VDISABLE;
459 break;
460 case KEY_VERASE:
461 *dnep = (*chp = clp->orig.c_cc[VERASE]) == _POSIX_VDISABLE;
462 break;
463 case KEY_VKILL:
464 *dnep = (*chp = clp->orig.c_cc[VKILL]) == _POSIX_VDISABLE;
465 break;
466 #ifdef VWERASE
467 case KEY_VWERASE:
468 *dnep = (*chp = clp->orig.c_cc[VWERASE]) == _POSIX_VDISABLE;
469 break;
470 #endif
471 default:
472 *dnep = 1;
473 break;
475 return (0);
479 * cl_move --
480 * Move the cursor.
482 * PUBLIC: int cl_move __P((SCR *, size_t, size_t));
485 cl_move(sp, lno, cno)
486 SCR *sp;
487 size_t 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);
493 return (1);
495 return (0);
499 * cl_refresh --
500 * Refresh the screen.
502 * PUBLIC: int cl_refresh __P((SCR *, int));
505 cl_refresh(sp, repaint)
506 SCR *sp;
507 int repaint;
509 GS *gp;
510 CL_PRIVATE *clp;
511 SCR *psp, *tsp;
512 size_t y, x;
514 gp = sp->gp;
515 clp = CLP(sp);
518 * If we received a killer signal, we're done, there's no point
519 * in refreshing the screen.
521 if (clp->killersig)
522 return (0);
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)) {
534 getyx(stdscr, y, x);
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)
539 cl_rdiv(tsp);
540 (void)move(y, x);
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().
549 if (repaint)
550 clearok(curscr, 1);
551 return (refresh() == ERR);
555 * cl_rdiv --
556 * Draw a dividing line between two vertically split screens.
558 static void
559 cl_rdiv(sp)
560 SCR *sp;
562 size_t cnt;
564 for (cnt = 0; cnt < sp->rows - 1; ++cnt) {
565 move(sp->roff + cnt, sp->cols + 1);
566 addch('|');
571 * cl_rename --
572 * Rename the file.
574 * PUBLIC: int cl_rename __P((SCR *, char *, int));
577 cl_rename(sp, name, on)
578 SCR *sp;
579 char *name;
580 int on;
582 GS *gp;
583 CL_PRIVATE *clp;
584 char *ttype;
586 gp = sp->gp;
587 clp = CLP(sp);
589 ttype = OG_STR(gp, GO_TERM);
592 * XXX
593 * We can only rename windows for xterm.
595 if (on) {
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);
602 } else
603 if (F_ISSET(clp, CL_RENAME)) {
604 F_CLR(clp, CL_RENAME);
605 (void)printf(XTERM_RENAME, ttype);
606 (void)fflush(stdout);
608 return (0);
612 * cl_split --
613 * Split a screen.
615 * PUBLIC: int cl_split __P((SCR *, SCR *));
618 cl_split(origp, newp)
619 SCR *origp, *newp;
621 CL_PRIVATE *clp;
623 clp = CLP(origp);
624 F_SET(clp, CL_LAYOUT);
626 /* origp is the original screen, giving up space to newp. */
627 return (0);
631 * cl_suspend --
632 * Suspend a screen.
634 * PUBLIC: int cl_suspend __P((SCR *, int *));
637 cl_suspend(sp, allowedp)
638 SCR *sp;
639 int *allowedp;
641 struct termios t;
642 CL_PRIVATE *clp;
643 GS *gp;
644 size_t y, x;
645 int changed;
647 gp = sp->gp;
648 clp = CLP(sp);
649 *allowedp = 1;
652 * The ex implementation of this function isn't needed by screens not
653 * supporting ex commands that require full terminal canonical mode
654 * (e.g. :suspend).
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
658 * by a UNIX shell.
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);
678 return (0);
682 * Move to the lower left-hand corner of the screen.
684 * XXX
685 * Not sure this is necessary in System V implementations, but it
686 * shouldn't hurt.
688 getyx(stdscr, y, x);
689 (void)move(LINES - 1, 0);
690 (void)refresh();
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);
702 #endif
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);
712 #else
713 (void)endwin();
714 #endif
716 * XXX
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);
736 return (0);
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);
745 #endif
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. */
754 (void)move(y, x);
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))
759 return (1);
760 if (changed)
761 F_SET(CLP(sp), CL_SIGWINCH);
763 return (0);
767 * cl_usage --
768 * Print out the curses usage messages.
770 * PUBLIC: void cl_usage __P((void));
772 void
773 cl_usage()
775 #define USAGE "\
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);
779 #undef USAGE
782 #ifdef DEBUG
784 * gdbrefresh --
785 * Stub routine so can flush out curses screen changes using gdb.
788 gdbrefresh()
790 refresh();
791 return (0); /* XXX Convince gdb to run it. */
793 #endif