#include "talk.h"
#include <ctype.h>
static int readwin(WINDOW *, int, int);
static void xscroll(register xwin_t *, int);
xwin_t my_win;
xwin_t rem_win;
WINDOW *line_win;
int curses_initialized = 0;
static int
max(a, b)
int a, b;
{
if (a > b) {
return (a);
} else {
return (b);
}
}
int
display(win, text, size)
register xwin_t *win;
register char *text;
int size;
{
register int i;
int mb_cur_max = MB_CUR_MAX;
for (i = 0; i < size; i++) {
int itext;
if (*text == '\n'|| *text == '\r') {
xscroll(win, 0);
text++;
continue;
}
if (*text == win->cerase) {
wmove(win->x_win, win->x_line, max(--win->x_col, 0));
getyx(win->x_win, win->x_line, win->x_col);
waddch(win->x_win, ' ');
wmove(win->x_win, win->x_line, win->x_col);
getyx(win->x_win, win->x_line, win->x_col);
text++;
continue;
}
if (*text == win->werase) {
int endcol, xcol, i, c;
endcol = win->x_col;
xcol = endcol - 1;
while (xcol >= 0) {
c = readwin(win->x_win, win->x_line, xcol);
if (c != ' ')
break;
xcol--;
}
while (xcol >= 0) {
c = readwin(win->x_win, win->x_line, xcol);
if (c == ' ')
break;
xcol--;
}
wmove(win->x_win, win->x_line, xcol + 1);
for (i = xcol + 1; i < endcol; i++)
waddch(win->x_win, ' ');
wmove(win->x_win, win->x_line, xcol + 1);
getyx(win->x_win, win->x_line, win->x_col);
continue;
}
if (*text == win->kill) {
wmove(win->x_win, win->x_line, 0);
wclrtoeol(win->x_win);
getyx(win->x_win, win->x_line, win->x_col);
text++;
continue;
}
if (*text == '\f') {
if (win == &my_win)
wrefresh(curscr);
text++;
continue;
}
if (*text == '\004') {
quit();
}
if (*text == '\007') {
beep();
continue;
}
if (win->x_col == COLS-1) {
xscroll(win, 0);
}
if (mb_cur_max > 1 && mblen(text, mb_cur_max) > 1) {
wchar_t wc;
int len;
len = mbtowc(&wc, text, mb_cur_max);
if (iswprint(wc) || iswspace(wc)) {
do {
if (win->x_col == COLS-1)
xscroll(win, 0);
waddch(win->x_win, *text++);
getyx(win->x_win, win->x_line, win->x_col);
} while (--len > 0);
continue;
}
text += len;
waddch(win->x_win, '?');
getyx(win->x_win, win->x_line, win->x_col);
continue;
}
itext = (unsigned int) *text;
if (isprint(itext) || *text == ' ' || *text == '\t' ||
*text == '\013' || *text == '\007' ) {
waddch(win->x_win, *text);
} else {
if (!isascii(*text)) {
if (win->x_col == COLS-3) {
xscroll(win, 0);
}
waddch(win->x_win, 'M');
waddch(win->x_win, '-');
*text = toascii(*text);
}
if (iscntrl(*text)) {
getyx(win->x_win, win->x_line, win->x_col);
if (win->x_col == COLS-2) {
xscroll(win, 0);
}
waddch(win->x_win, '^');
waddch(win->x_win, *text + 0100);
}
else
waddch(win->x_win, *text);
}
getyx(win->x_win, win->x_line, win->x_col);
text++;
}
wrefresh(win->x_win);
return (0);
}
static int
readwin(win, line, col)
WINDOW *win;
int line, col;
{
int oldline, oldcol;
register int c;
getyx(win, oldline, oldcol);
wmove(win, line, col);
c = winch(win);
wmove(win, oldline, oldcol);
return (c);
}
static void
xscroll(win, flag)
register xwin_t *win;
int flag;
{
if (flag == -1) {
wmove(win->x_win, 0, 0);
win->x_line = 0;
win->x_col = 0;
return;
}
win->x_line = (win->x_line + 1) % win->x_nlines;
win->x_col = 0;
wmove(win->x_win, win->x_line, win->x_col);
wclrtoeol(win->x_win);
wmove(win->x_win, (win->x_line + 1) % win->x_nlines, win->x_col);
wclrtoeol(win->x_win);
wmove(win->x_win, win->x_line, win->x_col);
}