--- rxvt-unicode/src/screen.C 2003/11/24 17:31:27 1.2 +++ rxvt-unicode/src/screen.C 2021/06/17 11:01:57 1.462 @@ -1,13 +1,14 @@ -/*--------------------------------*-C-*--------------------------------------* - * File: screen.c +/*---------------------------------------------------------------------------* + * File: screen.C *---------------------------------------------------------------------------* - * $Id: screen.C,v 1.2 2003/11/24 17:31:27 pcg Exp $ * * Copyright (c) 1997-2001 Geoff Wing + * Copyright (c) 2003-2007 Marc Lehmann + * Copyright (c) 2015 Emanuele Giaquinta * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, @@ -19,446 +20,471 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *--------------------------------------------------------------------------*/ + /* - * We handle _all_ screen updates and selections + * This file handles _all_ screen updates and selections */ -#include "../config.h" /* NECESSARY */ -#define INTERN_SCREEN -#include "rxvt.h" /* NECESSARY */ -#include "screen.intpro" /* PROTOS for internal routines */ - -#include /* get the typedef for CARD32 */ +#include "../config.h" /* NECESSARY */ +#include "rxvt.h" /* NECESSARY */ +#include "rxvtperl.h" /* NECESSARY */ -#include -#include +#include -inline void fill_text (text_t *start, text_t value, int len) +static inline void +fill_text (text_t *start, text_t value, int len) { while (len--) *start++ = value; } -#define RESET_CHSTAT(H) \ - if ((H)->chstat == WBYTE) \ - (H)->chstat = SBYTE, (H)->lost_multi = 1 - /* ------------------------------------------------------------------------- */ -#define PROP_SIZE 16384 -#define TABSIZE 8 /* default tab size */ +#define TABSIZE 8 /* default tab size */ /* ------------------------------------------------------------------------- * * GENERAL SCREEN AND SELECTION UPDATE ROUTINES * * ------------------------------------------------------------------------- */ -#define ZERO_SCROLLBACK(R) \ - if (((R)->Options & Opt_scrollTtyOutput) == Opt_scrollTtyOutput) \ - (R)->TermWin.view_start = 0 -#define CLEAR_SELECTION(R) \ - (R)->selection.beg.row = (R)->selection.beg.col \ - = (R)->selection.end.row = (R)->selection.end.col = 0 -#define CLEAR_ALL_SELECTION(R) \ - (R)->selection.beg.row = (R)->selection.beg.col \ - = (R)->selection.mark.row = (R)->selection.mark.col \ - = (R)->selection.end.row = (R)->selection.end.col = 0 +#define ZERO_SCROLLBACK() \ + if (option (Opt_scrollTtyOutput)) \ + view_start = 0 +#define CLEAR_SELECTION() \ + selection.beg.row = selection.beg.col \ + = selection.end.row = selection.end.col = 0 +#define CLEAR_ALL_SELECTION() \ + selection.beg.row = selection.beg.col \ + = selection.mark.row = selection.mark.col \ + = selection.end.row = selection.end.col = 0 -#define ROW_AND_COL_IS_AFTER(A, B, C, D) \ +#define ROW_AND_COL_IS_AFTER(A, B, C, D) \ (((A) > (C)) || (((A) == (C)) && ((B) > (D)))) -#define ROW_AND_COL_IS_BEFORE(A, B, C, D) \ +#define ROW_AND_COL_IS_BEFORE(A, B, C, D) \ (((A) < (C)) || (((A) == (C)) && ((B) < (D)))) -#define ROW_AND_COL_IN_ROW_AFTER(A, B, C, D) \ +#define ROW_AND_COL_IN_ROW_AFTER(A, B, C, D) \ (((A) == (C)) && ((B) > (D))) -#define ROW_AND_COL_IN_ROW_AT_OR_AFTER(A, B, C, D) \ +#define ROW_AND_COL_IN_ROW_AT_OR_AFTER(A, B, C, D) \ (((A) == (C)) && ((B) >= (D))) -#define ROW_AND_COL_IN_ROW_BEFORE(A, B, C, D) \ +#define ROW_AND_COL_IN_ROW_BEFORE(A, B, C, D) \ (((A) == (C)) && ((B) < (D))) -#define ROW_AND_COL_IN_ROW_AT_OR_BEFORE(A, B, C, D) \ +#define ROW_AND_COL_IN_ROW_AT_OR_BEFORE(A, B, C, D) \ (((A) == (C)) && ((B) <= (D))) /* these must be row_col_t */ -#define ROWCOL_IS_AFTER(X, Y) \ - ROW_AND_COL_IS_AFTER((X).row, (X).col, (Y).row, (Y).col) -#define ROWCOL_IS_BEFORE(X, Y) \ - ROW_AND_COL_IS_BEFORE((X).row, (X).col, (Y).row, (Y).col) -#define ROWCOL_IN_ROW_AFTER(X, Y) \ - ROW_AND_COL_IN_ROW_AFTER((X).row, (X).col, (Y).row, (Y).col) -#define ROWCOL_IN_ROW_BEFORE(X, Y) \ - ROW_AND_COL_IN_ROW_BEFORE((X).row, (X).col, (Y).row, (Y).col) -#define ROWCOL_IN_ROW_AT_OR_AFTER(X, Y) \ - ROW_AND_COL_IN_ROW_AT_OR_AFTER((X).row, (X).col, (Y).row, (Y).col) -#define ROWCOL_IN_ROW_AT_OR_BEFORE(X, Y) \ - ROW_AND_COL_IN_ROW_AT_OR_BEFORE((X).row, (X).col, (Y).row, (Y).col) +#define ROWCOL_IS_AFTER(X, Y) \ + ROW_AND_COL_IS_AFTER ((X).row, (X).col, (Y).row, (Y).col) +#define ROWCOL_IS_BEFORE(X, Y) \ + ROW_AND_COL_IS_BEFORE ((X).row, (X).col, (Y).row, (Y).col) +#define ROWCOL_IN_ROW_AFTER(X, Y) \ + ROW_AND_COL_IN_ROW_AFTER ((X).row, (X).col, (Y).row, (Y).col) +#define ROWCOL_IN_ROW_BEFORE(X, Y) \ + ROW_AND_COL_IN_ROW_BEFORE ((X).row, (X).col, (Y).row, (Y).col) +#define ROWCOL_IN_ROW_AT_OR_AFTER(X, Y) \ + ROW_AND_COL_IN_ROW_AT_OR_AFTER ((X).row, (X).col, (Y).row, (Y).col) +#define ROWCOL_IN_ROW_AT_OR_BEFORE(X, Y) \ + ROW_AND_COL_IN_ROW_AT_OR_BEFORE ((X).row, (X).col, (Y).row, (Y).col) /* - * CLEAR_ROWS : clear rows starting from row * CLEAR_CHARS: clear chars starting from pixel position - * ERASE_ROWS : set rows starting from row to the foreground colour */ -#define drawBuffer (R->TermWin.vt) - -#define CLEAR_ROWS(row, num) \ - if (R->TermWin.mapped) \ - XClearArea(R->Xdisplay, drawBuffer, R->TermWin.int_bwidth, \ - Row2Pixel(row), (unsigned int)R->TermWin.width, \ - (unsigned int)Height2Pixel(num), False) - -#define CLEAR_CHARS(x, y, num) \ - if (R->TermWin.mapped) \ - XClearArea(R->Xdisplay, drawBuffer, x, y, \ - (unsigned int)Width2Pixel(num), \ - (unsigned int)Height2Pixel(1), False) - -#define ERASE_ROWS(row, num) \ - XFillRectangle(R->Xdisplay, drawBuffer, R->TermWin.gc, \ - R->TermWin.int_bwidth, Row2Pixel(row), \ - (unsigned int)R->TermWin.width, \ - (unsigned int)Height2Pixel(num)) +#define CLEAR_CHARS(x, y, num) \ + if (mapped) \ + XClearArea (dpy, vt, x, y, \ + (unsigned int)Width2Pixel (num), \ + (unsigned int)Height2Pixel (1), False) /* ------------------------------------------------------------------------- * * SCREEN `COMMON' ROUTINES * * ------------------------------------------------------------------------- */ + /* Fill part/all of a line with blanks. */ -/* INTPROTO */ void -rxvt_blank_line(pR_ text_t *et, rend_t *er, unsigned int width, rend_t efs) +rxvt_term::scr_blank_line (line_t &l, unsigned int col, unsigned int width, rend_t efs) const noexcept { - efs &= ~RS_baseattrMask; - efs = SET_FONT (efs, R->TermWin.fontset->find_font (' ')); + if (!l.valid ()) + { + l.alloc (); + col = 0; + width = ncol; + } - while (width--) - { - *et++ = ' '; - *er++ = efs; - } + l.touch (); + + efs &= ~RS_baseattrMask; // remove italic etc. fontstyles + efs = SET_FONT (efs, FONTSET (efs)->find_space_font ()); + + text_t *et = l.t + col; + rend_t *er = l.r + col; + + while (width--) + { + *et++ = ' '; + *er++ = efs; + } } /* ------------------------------------------------------------------------- */ /* Fill a full line with blanks - make sure it is allocated first */ -/* INTPROTO */ void -rxvt_blank_screen_mem(pR_ text_t **tp, rend_t **rp, unsigned int row, rend_t efs) +rxvt_term::scr_blank_screen_mem (line_t &l, rend_t efs) const noexcept { - int width = R->TermWin.ncol; - rend_t *er; + scr_blank_line (l, 0, ncol, efs); -#ifdef DEBUG_STRICT - assert((tp[row] && rp[row]) || (tp[row] == NULL && rp[row] == NULL)); -#endif - if (tp[row] == NULL) { - tp[row] = (text_t *)rxvt_malloc(sizeof(text_t) * width); - rp[row] = (rend_t *)rxvt_malloc(sizeof(rend_t) * width); - } - rxvt_blank_line (aR_ tp[row], rp[row], width, efs); + l.l = 0; + l.f = 0; +} + +// nuke a single wide character at the given column +void +rxvt_term::scr_kill_char (line_t &l, int col) const noexcept +{ + // find begin + while (col > 0 && l.t[col] == NOCHAR) + col--; + + rend_t rend = l.r[col] & ~RS_baseattrMask; + rend = SET_FONT (rend, FONTSET (rend)->find_space_font ()); + + l.touch (); + + // found start, nuke + do { + l.t[col] = ' '; + l.r[col] = rend; + col++; + } while (col < ncol && l.t[col] == NOCHAR); +} + +// set the rendition of a single wide character beginning at the given column +void +rxvt_term::scr_set_char_rend (line_t &l, int col, rend_t rend) +{ + do { + l.r[col] = rend; + col++; + } while (col < ncol && l.t[col] == NOCHAR); } /* ------------------------------------------------------------------------- * * SCREEN INITIALISATION * * ------------------------------------------------------------------------- */ -/* EXTPROTO */ + +void +rxvt_term::scr_alloc () noexcept +{ + int tsize = sizeof (text_t) * ncol; + int rsize = sizeof (rend_t) * ncol; + + // we assume that rend_t size is a sufficient alignment + // factor for text_t and line_t values, and we only + // need to adjust tsize. + tsize = (tsize + sizeof (rend_t) - 1); + tsize -= tsize % sizeof (rend_t); + + int all_rows = total_rows + nrow + nrow; + + chunk_size = (sizeof (line_t) + rsize + tsize) * all_rows; + chunk = chunk_alloc (chunk_size, 0); + + char *base = (char *)chunk + sizeof (line_t) * all_rows; + + for (int row = 0; row < all_rows; ++row) + { + line_t &l = ((line_t *)chunk) [row]; + + l.t = (text_t *)base; base += tsize; + l.r = (rend_t *)base; base += rsize; + l.l = -1; + l.f = 0; + } + + drawn_buf = (line_t *)chunk; + swap_buf = drawn_buf + nrow; + row_buf = swap_buf + nrow; +} + void -rxvt_scr_reset(pR) +rxvt_term::copy_line (line_t &dst, line_t &src) { - unsigned int ncol, nrow, prev_ncol, prev_nrow, - total_rows, prev_total_rows; - unsigned int p, q; - int k; - rend_t setrstyle; - - D_SCREEN((stderr, "rxvt_scr_reset()")); - - R->TermWin.view_start = 0; - RESET_CHSTAT(R->h); - R->h->num_scr = 0; - - prev_ncol = R->h->prev_ncol; - prev_nrow = R->h->prev_nrow; - if (R->TermWin.ncol == 0) - R->TermWin.ncol = 80; - if (R->TermWin.nrow == 0) - R->TermWin.nrow = 24; - ncol = R->TermWin.ncol; - nrow = R->TermWin.nrow; - if (ncol == prev_ncol && nrow == prev_nrow) - return; - - R->h->want_refresh = 1; - - total_rows = nrow + R->TermWin.saveLines; - prev_total_rows = prev_nrow + R->TermWin.saveLines; - - R->screen.tscroll = 0; - R->screen.bscroll = nrow - 1; - - if (prev_nrow == 0) { -/* - * A: first time called so just malloc everything : don't rely on realloc - * Note: this is still needed so that all the scrollback lines are NULL - */ - R->screen.text = (text_t **)rxvt_calloc(total_rows, sizeof(text_t *)); - R->buf_text = (text_t **)rxvt_calloc(total_rows, sizeof(text_t *)); - R->drawn_text = (text_t **)rxvt_calloc(nrow, sizeof(text_t *)); - R->swap.text = (text_t **)rxvt_calloc(nrow, sizeof(text_t *)); - - R->screen.tlen = (int16_t *)rxvt_calloc(total_rows, sizeof(int16_t)); - R->swap.tlen = (int16_t *)rxvt_calloc(nrow, sizeof(int16_t)); - - R->screen.rend = (rend_t **)rxvt_calloc(total_rows, sizeof(rend_t *)); - R->buf_rend = (rend_t **)rxvt_calloc(total_rows, sizeof(rend_t *)); - R->drawn_rend = (rend_t **)rxvt_calloc(nrow, sizeof(rend_t *)); - R->swap.rend = (rend_t **)rxvt_calloc(nrow, sizeof(rend_t *)); - - for (p = 0; p < nrow; p++) { - q = p + R->TermWin.saveLines; - rxvt_blank_screen_mem(aR_ R->screen.text, R->screen.rend, - q, DEFAULT_RSTYLE); - rxvt_blank_screen_mem(aR_ R->swap.text, R->swap.rend, - p, DEFAULT_RSTYLE); - R->screen.tlen[q] = R->swap.tlen[p] = 0; - rxvt_blank_screen_mem(aR_ R->drawn_text, R->drawn_rend, - p, DEFAULT_RSTYLE); - } - MEMSET(R->h->charsets, 'B', sizeof(R->h->charsets)); - R->TermWin.nscrolled = 0; /* no saved lines */ - R->h->rstyle = DEFAULT_RSTYLE; - R->screen.flags = Screen_DefaultFlags; - R->screen.cur.row = R->screen.cur.col = 0; - R->screen.charset = 0; - R->h->current_screen = PRIMARY; - rxvt_scr_cursor(aR_ SAVE); + scr_blank_screen_mem (dst, DEFAULT_RSTYLE); + dst.l = min (src.l, ncol); + memcpy (dst.t, src.t, sizeof (text_t) * dst.l); + memcpy (dst.r, src.r, sizeof (rend_t) * dst.l); + dst.f = src.f; +} + +void ecb_cold +rxvt_term::scr_reset () +{ +#if ENABLE_OVERLAY + scr_overlay_off (); +#endif + + view_start = 0; + num_scr = 0; + + if (ncol == 0) + ncol = 80; + + if (nrow == 0) + nrow = 24; + + if (ncol == prev_ncol && nrow == prev_nrow) + return; + + // we need at least two lines for wrapping to work correctly + while (nrow + saveLines < 2) + { + //TODO//FIXME + saveLines++; + prev_nrow--; + top_row--; + } + + want_refresh = 1; + + int prev_total_rows = prev_nrow + saveLines; + total_rows = nrow + saveLines; + + screen.tscroll = 0; + screen.bscroll = nrow - 1; + + void *prev_chunk = chunk; + size_t prev_chunk_size = chunk_size; + line_t *prev_drawn_buf = drawn_buf; + line_t *prev_swap_buf = swap_buf; + line_t *prev_row_buf = row_buf; + + scr_alloc (); + + if (!prev_row_buf) + { + top_row = 0; + term_start = 0; + + memset (charsets, 'B', sizeof (charsets)); + rstyle = DEFAULT_RSTYLE; + screen.flags = Screen_DefaultFlags; + screen.cur.row = screen.cur.col = 0; + screen.charset = 0; + current_screen = PRIMARY; + scr_cursor (SAVE); + #if NSCREENS - R->swap.flags = Screen_DefaultFlags; - R->swap.cur.row = R->swap.cur.col = 0; - R->swap.charset = 0; - R->h->current_screen = SECONDARY; - rxvt_scr_cursor(aR_ SAVE); - R->h->current_screen = PRIMARY; -#endif - R->selection.text = NULL; - R->selection.len = 0; - R->selection.op = SELECTION_CLEAR; - R->selection.screen = PRIMARY; - R->selection.clicks = 0; - CLEAR_ALL_SELECTION(R); - R->h->rvideo = 0; - - } else { -/* - * B1: add or delete rows as appropriate - */ - setrstyle = DEFAULT_RSTYLE; - - if (nrow < prev_nrow) { - /* delete rows */ - k = min(R->TermWin.nscrolled, prev_nrow - nrow); - rxvt_scroll_text(aR_ 0, (int)prev_nrow - 1, k, 1); - for (p = nrow; p < prev_nrow; p++) { - q = p + R->TermWin.saveLines; - if (R->screen.text[q]) { -#ifdef DEBUG_STRICT - assert(R->screen.rend[q]); -#endif - free(R->screen.text[q]); - free(R->screen.rend[q]); - } - if (R->swap.text[p]) { -#ifdef DEBUG_STRICT - assert(R->swap.rend[p]); -#endif - free(R->swap.text[p]); - free(R->swap.rend[p]); - } -#ifdef DEBUG_STRICT - assert(R->drawn_text[p] && R->drawn_rend[p]); -#endif - free(R->drawn_text[p]); - free(R->drawn_rend[p]); - } - /* we have fewer rows so fix up cursor position */ - MIN_IT(R->screen.cur.row, (int32_t)nrow - 1); - MIN_IT(R->swap.cur.row, (int32_t)nrow - 1); - - rxvt_scr_reset_realloc(aR); /* realloc _last_ */ - - } else if (nrow > prev_nrow) { - /* add rows */ - rxvt_scr_reset_realloc(aR); /* realloc _first_ */ - - k = min(R->TermWin.nscrolled, nrow - prev_nrow); - for (p = prev_total_rows; p < total_rows; p++) { - R->screen.tlen[p] = 0; - R->screen.text[p] = NULL; - R->screen.rend[p] = NULL; - } - for (p = prev_total_rows; p < total_rows - k; p++) - rxvt_blank_screen_mem(aR_ R->screen.text, R->screen.rend, - p, setrstyle); - for (p = prev_nrow; p < nrow; p++) { - R->swap.tlen[p] = 0; - R->swap.text[p] = NULL; - R->swap.rend[p] = NULL; - R->drawn_text[p] = NULL; - R->drawn_rend[p] = NULL; - rxvt_blank_screen_mem(aR_ R->swap.text, R->swap.rend, - p, setrstyle); - rxvt_blank_screen_mem(aR_ R->drawn_text, R->drawn_rend, - p, setrstyle); - } - if (k > 0) { - rxvt_scroll_text(aR_ 0, (int)nrow - 1, -k, 1); - R->screen.cur.row += k; - R->screen.s_cur.row += k; - R->TermWin.nscrolled -= k; - } -#ifdef DEBUG_STRICT - assert(R->screen.cur.row < R->TermWin.nrow); - assert(R->swap.cur.row < R->TermWin.nrow); -#else /* drive with your eyes closed */ - MIN_IT(R->screen.cur.row, nrow - 1); - MIN_IT(R->swap.cur.row, nrow - 1); -#endif - } -/* B2: resize columns */ - if (ncol != prev_ncol) { - for (p = 0; p < total_rows; p++) { - if (R->screen.text[p]) { - R->screen.text[p] = (text_t *)rxvt_realloc(R->screen.text[p], ncol * sizeof(text_t)); - R->screen.rend[p] = (rend_t *)rxvt_realloc(R->screen.rend[p], ncol * sizeof(rend_t)); - MIN_IT(R->screen.tlen[p], (int16_t)ncol); - if (ncol > prev_ncol) - rxvt_blank_line(aR_ - &(R->screen.text[p][prev_ncol]), - &(R->screen.rend[p][prev_ncol]), - ncol - prev_ncol, - setrstyle); - } - } - for (p = 0; p < nrow; p++) { - R->drawn_text[p] = (text_t *)rxvt_realloc(R->drawn_text[p], ncol * sizeof(text_t)); - R->drawn_rend[p] = (rend_t *)rxvt_realloc(R->drawn_rend[p], ncol * sizeof(rend_t)); - if (R->swap.text[p]) { - R->swap.text[p] = (text_t *)rxvt_realloc(R->swap.text[p], ncol * sizeof(text_t)); - R->swap.rend[p] = (rend_t *)rxvt_realloc(R->swap.rend[p], ncol * sizeof(rend_t)); - MIN_IT(R->swap.tlen[p], (int16_t)ncol); - if (ncol > prev_ncol) - rxvt_blank_line(aR_ - &(R->swap.text[p][prev_ncol]), - &(R->swap.rend[p][prev_ncol]), - ncol - prev_ncol, setrstyle); - } - if (ncol > prev_ncol) - rxvt_blank_line(aR_ - &(R->drawn_text[p][prev_ncol]), - &(R->drawn_rend[p][prev_ncol]), - ncol - prev_ncol, setrstyle); - } - MIN_IT(R->screen.cur.col, (int16_t)ncol - 1); - MIN_IT(R->swap.cur.col, (int16_t)ncol - 1); - } - if (R->tabs) - free(R->tabs); - } - - R->tabs = (char *)rxvt_malloc(ncol * sizeof(char)); - - for (p = 0; p < ncol; p++) - R->tabs[p] = (p % TABSIZE == 0) ? 1 : 0; - - R->h->prev_nrow = nrow; - R->h->prev_ncol = ncol; - - rxvt_tt_winsize(R->cmd_fd, R->TermWin.ncol, R->TermWin.nrow, R->h->cmd_pid); -} - -/* INTPROTO */ -void -rxvt_scr_reset_realloc(pR) -{ - uint16_t total_rows, nrow; - - nrow = R->TermWin.nrow; - total_rows = nrow + R->TermWin.saveLines; -/* *INDENT-OFF* */ - R->screen.text = (text_t **)rxvt_realloc(R->screen.text, total_rows * sizeof(text_t *)); - R->buf_text = (text_t **)rxvt_realloc(R->buf_text , total_rows * sizeof(text_t *)); - R->drawn_text = (text_t **)rxvt_realloc(R->drawn_text , nrow * sizeof(text_t *)); - R->swap.text = (text_t **)rxvt_realloc(R->swap.text , nrow * sizeof(text_t *)); - - R->screen.tlen = (int16_t *)rxvt_realloc(R->screen.tlen, total_rows * sizeof(int16_t)); - R->swap.tlen = (int16_t *)rxvt_realloc(R->swap.tlen , total_rows * sizeof(int16_t)); - - R->screen.rend = (rend_t **)rxvt_realloc(R->screen.rend, total_rows * sizeof(rend_t *)); - R->buf_rend = (rend_t **)rxvt_realloc(R->buf_rend , total_rows * sizeof(rend_t *)); - R->drawn_rend = (rend_t **)rxvt_realloc(R->drawn_rend , nrow * sizeof(rend_t *)); - R->swap.rend = (rend_t **)rxvt_realloc(R->swap.rend , nrow * sizeof(rend_t *)); -/* *INDENT-ON* */ -} - -/* ------------------------------------------------------------------------- */ -/* - * Free everything. That way malloc debugging can find leakage. - */ -/* EXTPROTO */ -void -rxvt_scr_release(pR) -{ - uint16_t total_rows; - int i; - - total_rows = R->TermWin.nrow + R->TermWin.saveLines; - for (i = 0; i < total_rows; i++) { - if (R->screen.text[i]) { /* then so is R->screen.rend[i] */ - free(R->screen.text[i]); -#ifdef DEBUG_STRICT - assert(R->screen.rend[i]); -#endif - free(R->screen.rend[i]); - } - } - for (i = 0; i < R->TermWin.nrow; i++) { - free(R->drawn_text[i]); - free(R->drawn_rend[i]); - free(R->swap.text[i]); - free(R->swap.rend[i]); - } - free(R->screen.text); - free(R->screen.tlen); - free(R->screen.rend); - free(R->drawn_text); - free(R->drawn_rend); - free(R->swap.text); - free(R->swap.tlen); - free(R->swap.rend); - free(R->buf_text); - free(R->buf_rend); - free(R->tabs); - -/* NULL these so if anything tries to use them, we'll know about it */ - R->screen.text = R->drawn_text = R->swap.text = NULL; - R->screen.rend = R->drawn_rend = R->swap.rend = NULL; - R->screen.tlen = R->swap.tlen = NULL; - R->buf_text = NULL; - R->buf_rend = NULL; - R->tabs = NULL; -} - -/* ------------------------------------------------------------------------- */ -/* - * Hard reset - */ -/* EXTPROTO */ -void -rxvt_scr_poweron(pR) -{ - D_SCREEN((stderr, "rxvt_scr_poweron()")); - - rxvt_scr_release(aR); - R->h->prev_nrow = R->h->prev_ncol = 0; - rxvt_scr_reset(aR); - - rxvt_scr_clear(aR); - rxvt_scr_refresh(aR_ SLOW_REFRESH); -#ifdef RXVT_GRAPHICS - rxvt_Gr_reset(aR); + swap.flags = Screen_DefaultFlags; + swap.cur.row = swap.cur.col = 0; + swap.charset = 0; + current_screen = SECONDARY; + scr_cursor (SAVE); + current_screen = PRIMARY; +#endif + + selection.text = NULL; + selection.len = 0; + selection.op = SELECTION_CLEAR; + selection.screen = PRIMARY; + selection.clicks = 0; + selection.clip_text = NULL; + selection.clip_len = 0; + } + else + { + /* + * add or delete rows as appropriate + */ + + int common_col = min (prev_ncol, ncol); + + for (int row = min (nrow, prev_nrow); row--; ) + { + scr_blank_screen_mem (drawn_buf [row], DEFAULT_RSTYLE); + memcpy (drawn_buf [row].t, prev_drawn_buf [row].t, sizeof (text_t) * common_col); + memcpy (drawn_buf [row].r, prev_drawn_buf [row].r, sizeof (rend_t) * common_col); + + copy_line (swap_buf [row], prev_swap_buf [row]); + } + + int p = MOD (term_start + prev_nrow, prev_total_rows); // previous row + int pend = MOD (term_start + top_row , prev_total_rows); + int q = total_rows; // rewrapped row + + if (top_row) + { + // Re-wrap lines. This is rather ugly, possibly because I am too dumb + // to come up with a lean and mean algorithm. + // TODO: maybe optimise when width didn't change + + row_col_t ocur = screen.cur; + ocur.row = MOD (term_start + ocur.row, prev_total_rows); + + do + { + p = MOD (p - 1, prev_total_rows); + assert (prev_row_buf [MOD (p, prev_total_rows)].t); + int plines = 1; + int llen = prev_row_buf [MOD (p, prev_total_rows)].l; + + while (p != pend && prev_row_buf [MOD (p - 1, prev_total_rows)].is_longer ()) + { + p = MOD (p - 1, prev_total_rows); + + plines++; + llen += prev_ncol; + } + + int qlines = max (0, (llen - 1) / ncol) + 1; + + // drop partial lines completely + if (q < qlines) + break; + + q -= qlines; + + int lofs = 0; + line_t *qline; + + // re-assemble the full line by destination lines + for (int qrow = q; qlines--; qrow++) + { + qline = row_buf + qrow; + qline->alloc (); // redundant with next line + qline->l = ncol; + qline->is_longer (1); + + int qcol = 0; + + // see below for cursor adjustment rationale + if (p == ocur.row) + screen.cur.row = q - (total_rows - nrow); + + // fill a single destination line + while (lofs < llen && qcol < ncol) + { + int prow = lofs / prev_ncol; + int pcol = lofs % prev_ncol; + + prow = MOD (p + prow, prev_total_rows); + + // we only adjust the cursor _row_ and put it into + // the topmost line of "long line" it was in, as + // this seems to upset applications/shells/readline + // least. + if (prow == ocur.row) + screen.cur.row = q - (total_rows - nrow); + + line_t &pline = prev_row_buf [prow]; + + int len = min (min (prev_ncol - pcol, ncol - qcol), llen - lofs); + + memcpy (qline->t + qcol, pline.t + pcol, len * sizeof (text_t)); + memcpy (qline->r + qcol, pline.r + pcol, len * sizeof (rend_t)); + + lofs += len; + qcol += len; + } + } + + qline->l = llen ? MOD (llen - 1, ncol) + 1 : 0; + qline->is_longer (0); + scr_blank_line (*qline, qline->l, ncol - qline->l, DEFAULT_RSTYLE); + } + while (p != pend && q > 0); + + term_start = total_rows - nrow; + top_row = q - term_start; + + // make sure all terminal lines exist + while (top_row > 0) + scr_blank_screen_mem (ROW (--top_row), DEFAULT_RSTYLE); + } + else + { + // if no scrollback exists (yet), wing, instead of wrap + + for (int row = min (nrow, prev_nrow); row--; ) + { + line_t &src = prev_row_buf [MOD (term_start + row, prev_total_rows)]; + line_t &dst = row_buf [row]; + + copy_line (dst, src); + } + + for (int row = prev_nrow; row < nrow; row++) + scr_blank_screen_mem (row_buf [row], DEFAULT_RSTYLE); + + term_start = 0; + } + + clamp_it (screen.cur.row, 0, nrow - 1); + clamp_it (screen.cur.col, 0, ncol - 1); + } + + for (int row = nrow; row--; ) + { + if (!ROW (row).valid ()) scr_blank_screen_mem (ROW (row), DEFAULT_RSTYLE); + if (!swap_buf [row].valid ()) scr_blank_screen_mem (swap_buf [row], DEFAULT_RSTYLE); + if (!drawn_buf [row].valid ()) scr_blank_screen_mem (drawn_buf [row], DEFAULT_RSTYLE); + } + + chunk_free (prev_chunk, prev_chunk_size); + + free (tabs); + tabs = (char *)rxvt_malloc (ncol); + + for (int col = ncol; col--; ) + tabs [col] = col % TABSIZE == 0; + + CLEAR_ALL_SELECTION (); + + prev_nrow = nrow; + prev_ncol = ncol; + + tt_winch (); + + HOOK_INVOKE ((this, HOOK_RESET, DT_END)); +} + +void ecb_cold +rxvt_term::scr_release () noexcept +{ + chunk_free (chunk, chunk_size); + chunk = 0; + row_buf = 0; + + free (tabs); + tabs = 0; +} + +/* ------------------------------------------------------------------------- */ +/* + * Hard/Soft reset + */ +void ecb_cold +rxvt_term::scr_poweron () +{ + scr_release (); + + prev_nrow = prev_ncol = 0; + rvideo_mode = false; + scr_soft_reset (); + scr_reset (); + + scr_clear (true); + scr_refresh (); +} + +void ecb_cold +rxvt_term::scr_soft_reset () noexcept +{ + /* only affects modes, nothing drastic such as clearing the screen */ +#if ENABLE_OVERLAY + scr_overlay_off (); #endif + + if (current_screen != PRIMARY) + scr_swap_screen (); + + scr_scroll_region (0, MAX_ROWS - 1); + scr_rendition (0, ~RS_None); + scr_insert_mode (0); } /* ------------------------------------------------------------------------- * @@ -469,49 +495,60 @@ * XTERM_SEQ: Save cursor : ESC 7 * XTERM_SEQ: Restore cursor: ESC 8 */ -/* EXTPROTO */ void -rxvt_scr_cursor(pR_ int mode) +rxvt_term::scr_cursor (cursor_mode mode) noexcept { - screen_t *s; - - D_SCREEN((stderr, "rxvt_scr_cursor(%c)", mode)); + screen_t *s; #if NSCREENS && !defined(NO_SECONDARY_SCREEN_CURSOR) - if (R->h->current_screen == SECONDARY) - s = &(R->swap); - else -#endif - s = &(R->screen); - switch (mode) { - case SAVE: - s->s_cur.row = s->cur.row; - s->s_cur.col = s->cur.col; - s->s_rstyle = R->h->rstyle; - s->s_charset = s->charset; - s->s_charset_char = R->h->charsets[s->charset]; - break; - case RESTORE: - R->h->want_refresh = 1; - s->cur.row = s->s_cur.row; - s->cur.col = s->s_cur.col; - s->flags &= ~Screen_WrapNext; - R->h->rstyle = s->s_rstyle; - s->charset = s->s_charset; - R->h->charsets[s->charset] = s->s_charset_char; - rxvt_set_font_style(aR); - break; - } -/* boundary check in case screen size changed between SAVE and RESTORE */ - MIN_IT(s->cur.row, R->TermWin.nrow - 1); - MIN_IT(s->cur.col, R->TermWin.ncol - 1); -#ifdef DEBUG_STRICT - assert(s->cur.row >= 0); - assert(s->cur.col >= 0); -#else /* drive with your eyes closed */ - MAX_IT(s->cur.row, 0); - MAX_IT(s->cur.col, 0); + if (current_screen == SECONDARY) + s = &swap; + else #endif + s = &screen; + + switch (mode) + { + case SAVE: + s->s_cur.row = screen.cur.row; + s->s_cur.col = screen.cur.col; + s->s_rstyle = rstyle; + s->s_charset = screen.charset; + s->s_charset_char = charsets[screen.charset]; + break; + + case RESTORE: + want_refresh = 1; + screen.cur.row = s->s_cur.row; + screen.cur.col = s->s_cur.col; + screen.flags &= ~Screen_WrapNext; + rstyle = s->s_rstyle; + screen.charset = s->s_charset; + charsets[screen.charset] = s->s_charset_char; + set_font_style (); + break; + } + + /* boundary check in case screen size changed between SAVE and RESTORE */ + min_it (s->cur.row, nrow - 1); + min_it (s->cur.col, ncol - 1); + assert (s->cur.row >= 0); + assert (s->cur.col >= 0); +} + +void +rxvt_term::scr_swap_screen () noexcept +{ + if (!option (Opt_secondaryScreen)) + return; + + for (int i = prev_nrow; i--; ) + ::swap (ROW(i), swap_buf [i]); + + ::swap (screen.cur, swap.cur); + + screen.cur.row = clamp (screen.cur.row, 0, prev_nrow - 1); + screen.cur.col = clamp (screen.cur.col, 0, prev_ncol - 1); } /* ------------------------------------------------------------------------- */ @@ -520,105 +557,88 @@ * XTERM_SEQ: Primary screen : ESC [ ? 4 7 h * XTERM_SEQ: Secondary screen: ESC [ ? 4 7 l */ -/* EXTPROTO */ -int -rxvt_scr_change_screen(pR_ int scrn) +void +rxvt_term::scr_change_screen (int scrn) { - int i; -#if NSCREENS - int offset; -#endif + if (scrn == current_screen) + return; - R->h->want_refresh = 1; + want_refresh = 1; + view_start = 0; - D_SCREEN((stderr, "rxvt_scr_change_screen(%d)", scrn)); + /* check for boundary cross */ + row_col_t pos; + pos.row = pos.col = 0; + if (ROWCOL_IS_BEFORE (selection.beg, pos) + && ROWCOL_IS_AFTER (selection.end, pos)) + CLEAR_SELECTION (); - R->TermWin.view_start = 0; - RESET_CHSTAT(R->h); + current_screen = scrn; - if (R->h->current_screen == scrn) - return R->h->current_screen; +#if NSCREENS + if (option (Opt_secondaryScreen)) + { + num_scr = 0; - rxvt_selection_check(aR_ 2); /* check for boundary cross */ + scr_swap_screen (); - SWAP_IT(R->h->current_screen, scrn, int); -#if NSCREENS - R->h->num_scr = 0; - offset = R->TermWin.saveLines; - for (i = R->h->prev_nrow; i--;) { - SWAP_IT(R->screen.text[i + offset], R->swap.text[i], text_t *); - SWAP_IT(R->screen.tlen[i + offset], R->swap.tlen[i], int16_t); - SWAP_IT(R->screen.rend[i + offset], R->swap.rend[i], rend_t *); - } - SWAP_IT(R->screen.cur.row, R->swap.cur.row, int16_t); - SWAP_IT(R->screen.cur.col, R->swap.cur.col, int16_t); -# ifdef DEBUG_STRICT - assert((R->screen.cur.row >= 0) && (R->screen.cur.row < R->h->prev_nrow)); - assert((R->screen.cur.col >= 0) && (R->screen.cur.col < R->h->prev_ncol)); -# else /* drive with your eyes closed */ - MAX_IT(R->screen.cur.row, 0); - MIN_IT(R->screen.cur.row, (int32_t)R->h->prev_nrow - 1); - MAX_IT(R->screen.cur.col, 0); - MIN_IT(R->screen.cur.col, (int32_t)R->h->prev_ncol - 1); -# endif - SWAP_IT(R->screen.charset, R->swap.charset, int16_t); - SWAP_IT(R->screen.flags, R->swap.flags, int); - R->screen.flags |= Screen_VisibleCursor; - R->swap.flags |= Screen_VisibleCursor; - -# ifdef RXVT_GRAPHICS - - if (rxvt_Gr_Displayed(aR)) { - rxvt_Gr_scroll(aR_ 0); - rxvt_Gr_ChangeScreen(aR); + ::swap (screen.charset, swap.charset); + ::swap (screen.flags, swap.flags); + screen.flags |= Screen_VisibleCursor; + swap.flags |= Screen_VisibleCursor; } -# endif -#else -# ifdef SCROLL_ON_NO_SECONDARY -# ifdef RXVT_GRAPHICS - if (rxvt_Gr_Displayed(aR)) - rxvt_Gr_ClearScreen(aR); -# endif - if (R->h->current_screen == PRIMARY -# ifdef RXVT_GRAPHICS - && !rxvt_Gr_Displayed(aR) -# endif - ) - rxvt_scroll_text(aR_ 0, (R->h->prev_nrow - 1), R->h->prev_nrow, 0); -# endif + else #endif - return scrn; + if (option (Opt_secondaryScroll)) + scr_scroll_text (0, prev_nrow - 1, prev_nrow); +} + +// clear WrapNext indicator, solidifying position on next line +void +rxvt_term::scr_do_wrap () noexcept +{ + if (!(screen.flags & Screen_WrapNext)) + return; + + screen.flags &= ~Screen_WrapNext; + + screen.cur.col = 0; + + if (screen.cur.row == screen.bscroll) + scr_scroll_text (screen.tscroll, screen.bscroll, 1); + else if (screen.cur.row < nrow - 1) + screen.cur.row++; } /* ------------------------------------------------------------------------- */ /* * Change the colour for following text */ -/* EXTPROTO */ void -rxvt_scr_color(pR_ unsigned int color, int fgbg) +rxvt_term::scr_color (unsigned int color, int fgbg) noexcept { - color &= RS_fgMask; - if (fgbg == Color_fg) - R->h->rstyle = SET_FGCOLOR(R->h->rstyle, color); - else - R->h->rstyle = SET_BGCOLOR(R->h->rstyle, color); + if (!IN_RANGE_INC (color, minCOLOR, maxTermCOLOR24)) + color = fgbg; + + if (fgbg == Color_fg) + rstyle = SET_FGCOLOR (rstyle, color); + else + rstyle = SET_BGCOLOR (rstyle, color); } /* ------------------------------------------------------------------------- */ /* * Change the rendition style for following text */ -/* EXTPROTO */ void -rxvt_scr_rendition(pR_ int set, int style) +rxvt_term::scr_rendition (int set, int style) noexcept { - if (set) - R->h->rstyle |= style; - else if (style == ~RS_None) - R->h->rstyle = DEFAULT_RSTYLE; - else - R->h->rstyle &= ~style; + if (set) + rstyle |= style; + else if (style == ~RS_None) + rstyle = DEFAULT_RSTYLE; + else + rstyle &= ~style; } /* ------------------------------------------------------------------------- */ @@ -626,279 +646,400 @@ * Scroll text between and inclusive, by lines * count positive ==> scroll up * count negative ==> scroll down - * spec == 0 for normal routines */ -/* EXTPROTO */ -int -rxvt_scroll_text(pR_ int row1, int row2, int count, int spec) -{ - int i, j; - long nscrolled; - - if (count == 0 || (row1 > row2)) - return 0; - - R->h->want_refresh = 1; - D_SCREEN((stderr, "rxvt_scroll_text(%d,%d,%d,%d): %s", row1, row2, count, spec, (R->h->current_screen == PRIMARY) ? "Primary" : "Secondary")); - - if ((count > 0) && (row1 == 0) && (R->h->current_screen == PRIMARY)) { - nscrolled = (long)R->TermWin.nscrolled + (long)count;; - if (nscrolled > (long)R->TermWin.saveLines) - R->TermWin.nscrolled = R->TermWin.saveLines; - else - R->TermWin.nscrolled = (uint16_t)nscrolled; - if ((R->Options & Opt_scrollWithBuffer) - && R->TermWin.view_start != 0 - && R->TermWin.view_start != R->TermWin.saveLines) - rxvt_scr_page(aR_ UP, count); - } else if (!spec) - row1 += R->TermWin.saveLines; - row2 += R->TermWin.saveLines; - - if (R->selection.op && R->h->current_screen == R->selection.screen) { - i = R->selection.beg.row + R->TermWin.saveLines; - j = R->selection.end.row + R->TermWin.saveLines; - if ((i < row1 && j > row1) - || (i < row2 && j > row2) - || (i - count < row1 && i >= row1) - || (i - count > row2 && i <= row2) - || (j - count < row1 && j >= row1) - || (j - count > row2 && j <= row2)) { - CLEAR_ALL_SELECTION(R); - R->selection.op = SELECTION_CLEAR; /* XXX: too aggressive? */ - } else if (j >= row1 && j <= row2) { - /* move selected region too */ - R->selection.beg.row -= count; - R->selection.end.row -= count; - R->selection.mark.row -= count; - } - } - rxvt_selection_check(aR_ 0); /* _after_ R->TermWin.nscrolled update */ - - R->h->num_scr += count; - j = count; - if (count < 0) - count = -count; - i = row2 - row1 + 1; - MIN_IT(count, i); - - if (j > 0) { -/* A: scroll up */ - -/* A1: Copy lines that will get clobbered by the rotation */ - for (i = 0, j = row1; i < count; i++, j++) { - R->buf_text[i] = R->screen.text[j]; - R->buf_rend[i] = R->screen.rend[j]; - } -/* A2: Rotate lines */ - for (j = row1, i = j + count; i <= row2; i++, j++) { - R->screen.tlen[j] = R->screen.tlen[i]; - R->screen.text[j] = R->screen.text[i]; - R->screen.rend[j] = R->screen.rend[i]; - } - j = row2 - count + 1, i = count; - } else /* if (j < 0) */ { -/* B: scroll down */ - -/* B1: Copy lines that will get clobbered by the rotation */ - for (i = 0, j = row2; i < count; i++, j--) { - R->buf_text[i] = R->screen.text[j]; - R->buf_rend[i] = R->screen.rend[j]; - } -/* B2: Rotate lines */ - for (j = row2, i = j - count; i >= row1; i--, j--) { - R->screen.tlen[j] = R->screen.tlen[i]; - R->screen.text[j] = R->screen.text[i]; - R->screen.rend[j] = R->screen.rend[i]; - } - j = row1, i = count; - count = -count; - } - -/* C: Resurrect lines */ - for (; i--; j++) { - R->screen.tlen[j] = 0; - R->screen.text[j] = R->buf_text[i]; - R->screen.rend[j] = R->buf_rend[i]; - if (!spec) /* line length may not equal TermWin.ncol */ - rxvt_blank_screen_mem(aR_ R->screen.text, R->screen.rend, - (unsigned int)j, R->h->rstyle); - } - -#ifdef RXVT_GRAPHICS - if (rxvt_Gr_Displayed(aR)) - rxvt_Gr_scroll(aR_ count); -#endif - return count; +int ecb_hot +rxvt_term::scr_scroll_text (int row1, int row2, int count) noexcept +{ + if (count == 0 || (row1 > row2)) + return 0; + + want_refresh = 1; + num_scr += count; + + if (count > 0 + && row1 == 0 + && (current_screen == PRIMARY || option (Opt_secondaryScroll))) + { + min_it (count, total_rows - (nrow - (row2 + 1))); + + top_row = max (top_row - count, -saveLines); + + // sever bottommost line + { + line_t &l = ROW(row2); + l.is_longer (0); + l.touch (); + } + + // scroll everything up 'count' lines + term_start = (term_start + count) % total_rows; + + // now copy lines below the scroll region bottom to the + // bottom of the screen again, so they look as if they + // hadn't moved. + for (int i = nrow; --i > row2; ) + { + line_t &l1 = ROW(i - count); + line_t &l2 = ROW(i); + + ::swap (l1, l2); + l2.touch (); + } + + // erase newly scrolled-in lines + for (int i = count; i--; ) + { + line_t &l = ROW(row2 - i); + + // optimise if already cleared, can be significant on slow machines + // could be rolled into scr_blank_screen_mem + if (l.r && l.l < ncol - 1 && !((l.r[l.l + 1] ^ rstyle) & (RS_fgMask | RS_bgMask))) + { + scr_blank_line (l, 0, l.l, rstyle); + l.l = 0; + l.f = 0; + } + else + scr_blank_screen_mem (l, rstyle); + } + + // move and/or clear selection, if any + if (selection.op && current_screen == selection.screen + && selection.beg.row <= row2) + { + selection.beg.row -= count; + selection.end.row -= count; + selection.mark.row -= count; + + selection_check (0); + } + + // finally move the view window, if desired + if (option (Opt_scrollWithBuffer) + && view_start != 0 + && view_start != -saveLines) + scr_page (count); + + if (SHOULD_INVOKE (HOOK_SCROLL_BACK)) + HOOK_INVOKE ((this, HOOK_SCROLL_BACK, DT_INT, count, DT_INT, top_row, DT_END)); + } + else + { + if (selection.op && current_screen == selection.screen) + { + if ((selection.beg.row < row1 && selection.end.row > row1) + || (selection.beg.row < row2 && selection.end.row > row2) + || (selection.beg.row - count < row1 && selection.beg.row >= row1) + || (selection.beg.row - count > row2 && selection.beg.row <= row2) + || (selection.end.row - count < row1 && selection.end.row >= row1) + || (selection.end.row - count > row2 && selection.end.row <= row2)) + { + CLEAR_ALL_SELECTION (); + selection.op = SELECTION_CLEAR; + } + else if (selection.end.row >= row1 && selection.end.row <= row2) + { + /* move selected region too */ + selection.beg.row -= count; + selection.end.row -= count; + selection.mark.row -= count; + + selection_check (0); + } + } + + // use a simple and robust scrolling algorithm, this + // part of scr_scroll_text is not time-critical. + + // sever line above scroll region + if (row1) + { + line_t &l = ROW(row1 - 1); + l.is_longer (0); + l.touch (); + } + + int rows = row2 - row1 + 1; + + min_it (count, rows); + + line_t *temp_buf = rxvt_temp_buf (rows); + + for (int row = 0; row < rows; row++) + { + temp_buf [row] = ROW(row1 + (row + count + rows) % rows); + + if (!IN_RANGE_EXC (row + count, 0, rows)) + scr_blank_screen_mem (temp_buf [row], rstyle); + } + + for (int row = 0; row < rows; row++) + ROW(row1 + row) = temp_buf [row]; + + // sever bottommost line + { + line_t &l = ROW(row2); + l.is_longer (0); + l.touch (); + } + } + + return count; } /* ------------------------------------------------------------------------- */ /* * Add text given in of length to screen struct */ -/* EXTPROTO */ -void -rxvt_scr_add_lines(pR_ const uint32_t *str, int nlines, int len) +void ecb_hot +rxvt_term::scr_add_lines (const wchar_t *str, int len, int minlines) noexcept { - unsigned char checksel, clearsel; - uint32_t c; - int i, row, last_col; - text_t *stp; - rend_t *srp; - struct rxvt_hidden *h = R->h; - - if (len <= 0) /* sanity */ - return; - - h->want_refresh = 1; - last_col = R->TermWin.ncol; - - D_SCREEN((stderr, "rxvt_scr_add_lines(%d,%d)", nlines, len)); - ZERO_SCROLLBACK(R); - if (nlines > 0) { - nlines += (R->screen.cur.row - R->screen.bscroll); - if ((nlines > 0) - && (R->screen.tscroll == 0) - && (R->screen.bscroll == (R->TermWin.nrow - 1))) { - /* _at least_ this many lines need to be scrolled */ - rxvt_scroll_text(aR_ R->screen.tscroll, R->screen.bscroll, nlines, - 0); - R->screen.cur.row -= nlines; - } - } -#ifdef DEBUG_STRICT - assert(R->screen.cur.col < last_col); - assert((R->screen.cur.row < R->TermWin.nrow) - && (R->screen.cur.row >= -(int32_t)R->TermWin.nscrolled)); -#else /* drive with your eyes closed */ - MIN_IT(R->screen.cur.col, last_col - 1); - MIN_IT(R->screen.cur.row, (int32_t)R->TermWin.nrow - 1); - MAX_IT(R->screen.cur.row, -(int32_t)R->TermWin.nscrolled); -#endif - row = R->screen.cur.row + R->TermWin.saveLines; - - checksel = (R->selection.op - && h->current_screen == R->selection.screen) ? 1 : 0; - clearsel = 0; - - stp = R->screen.text[row]; - srp = R->screen.rend[row]; - - for (i = 0; i < len;) { - c = str[i++]; - switch (c) { - case '\t': - rxvt_scr_tab(aR_ 1); - continue; - case '\n': - if (R->screen.tlen[row] != -1) /* XXX: think about this */ - MAX_IT(R->screen.tlen[row], R->screen.cur.col); - R->screen.flags &= ~Screen_WrapNext; - if (R->screen.cur.row == R->screen.bscroll) - rxvt_scroll_text(aR_ R->screen.tscroll, R->screen.bscroll, 1, 0); - else if (R->screen.cur.row < (R->TermWin.nrow - 1)) - row = (++R->screen.cur.row) + R->TermWin.saveLines; - stp = R->screen.text[row]; /* _must_ refresh */ - srp = R->screen.rend[row]; /* _must_ refresh */ - RESET_CHSTAT(h); - continue; - case '\r': - if (R->screen.tlen[row] != -1) /* XXX: think about this */ - MAX_IT(R->screen.tlen[row], R->screen.cur.col); - R->screen.flags &= ~Screen_WrapNext; - R->screen.cur.col = 0; - RESET_CHSTAT(h); - continue; - default: - if (c == 127) - continue; /* yummmm..... */ - break; - } - - if (checksel /* see if we're writing within selection */ - && !ROWCOL_IS_BEFORE(R->screen.cur, R->selection.beg) - && ROWCOL_IS_BEFORE(R->screen.cur, R->selection.end)) { - checksel = 0; - clearsel = 1; - } - if (R->screen.flags & Screen_WrapNext) { - R->screen.tlen[row] = -1; - if (R->screen.cur.row == R->screen.bscroll) - rxvt_scroll_text(aR_ R->screen.tscroll, R->screen.bscroll, 1, 0); - else if (R->screen.cur.row < (R->TermWin.nrow - 1)) - row = (++R->screen.cur.row) + R->TermWin.saveLines; - stp = R->screen.text[row]; /* _must_ refresh */ - srp = R->screen.rend[row]; /* _must_ refresh */ - R->screen.cur.col = 0; - R->screen.flags &= ~Screen_WrapNext; - } - if (R->screen.flags & Screen_Insert) - rxvt_scr_insdel_chars(aR_ 1, INSERT); - - if (R->h->charsets[R->screen.charset] == '0') // DEC SPECIAL - switch (c) - { - case '+': c = 0x2192; break; case ',': c = 0x2190; break; case '-': c = 0x2191; break; - case '.': c = 0x2193; break; case '0': c = 0x25ae; break; case '`': c = 0x25c6; break; - case 'a': c = 0x2592; break; case 'f': c = 0x00b0; break; case 'g': c = 0x00b1; break; - case 'h': c = 0x2592; break; case 'i': c = 0x2603; break; case 'j': c = 0x2518; break; - case 'k': c = 0x2510; break; case 'l': c = 0x250c; break; case 'm': c = 0x2514; break; - case 'n': c = 0x253c; break; case 'o': c = 0x23ba; break; case 'p': c = 0x23bb; break; - case 'q': c = 0x2500; break; case 'r': c = 0x23bc; break; case 's': c = 0x23bd; break; - case 't': c = 0x251c; break; case 'u': c = 0x2524; break; case 'v': c = 0x2534; break; - case 'w': c = 0x252c; break; case 'x': c = 0x2502; break; case 'y': c = 0x2264; break; - case 'z': c = 0x2265; break; case '{': c = 0x03c0; break; case '|': c = 0x2260; break; - case '}': c = 0x00a3; break; case '~': c = 0x00b7; break; - } + if (len <= 0) /* sanity */ + return; - rend_t rend = SET_FONT (h->rstyle, R->TermWin.fontset->find_font (c)); + bool checksel; + unicode_t c; + int ncol = this->ncol; + const wchar_t *strend = str + len; - stp[R->screen.cur.col] = c; - srp[R->screen.cur.col] = rend; + want_refresh = 1; + ZERO_SCROLLBACK (); - if (c > 255) + if (minlines > 0) + { + minlines += screen.cur.row - screen.bscroll; + min_it (minlines, screen.cur.row - top_row); + + if (minlines > 0 + && screen.tscroll == 0 + && screen.bscroll == nrow - 1) + { + /* _at least_ this many lines need to be scrolled */ + scr_scroll_text (screen.tscroll, screen.bscroll, minlines); + screen.cur.row -= minlines; + } + } + + assert (screen.cur.col < ncol); + assert (screen.cur.row < nrow + && screen.cur.row >= top_row); + int row = screen.cur.row; + + checksel = selection.op && current_screen == selection.screen ? 1 : 0; + + line_t *line = &ROW(row); + + while (str < strend) + { + c = (unicode_t)*str++; // convert to rxvt-unicodes representation + + if (ecb_unlikely (c < 0x20)) + if (c == C0_LF) { - // rely on wcwidth to tell us the character width, at least for non iso-8859-1 - int width = wcwidth (c); - - if (width > 1) - { - while (--width > 0 && R->screen.cur.col < last_col - 1) - { + max_it (line->l, screen.cur.col); - srp[R->screen.cur.col] |= RS_wide; + screen.flags &= ~Screen_WrapNext; - R->screen.cur.col++; - stp[R->screen.cur.col] = NOCHAR; - srp[R->screen.cur.col] = rend; - } - } + if (screen.cur.row == screen.bscroll) + scr_scroll_text (screen.tscroll, screen.bscroll, 1); + else if (screen.cur.row < (nrow - 1)) + row = ++screen.cur.row; + + line = &ROW(row); /* _must_ refresh */ + continue; + } + else if (c == C0_CR) + { + max_it (line->l, screen.cur.col); + + screen.flags &= ~Screen_WrapNext; + screen.cur.col = 0; + continue; } + else if (c == C0_HT) + { + scr_tab (1, true); + continue; + } + + if (ecb_unlikely ( + checksel /* see if we're writing within selection */ + && !ROWCOL_IS_BEFORE (screen.cur, selection.beg) + && ROWCOL_IS_BEFORE (screen.cur, selection.end) + )) + { + checksel = 0; + /* + * If we wrote anywhere in the selected area, kill the selection + * XXX: should we kill the mark too? Possibly, but maybe that + * should be a similar check. + */ + CLEAR_SELECTION (); + } + + if (ecb_unlikely (screen.flags & Screen_WrapNext)) + { + scr_do_wrap (); + + line->l = ncol; + line->is_longer (1); + + row = screen.cur.row; + line = &ROW(row); /* _must_ refresh */ + } + + // some utf-8 decoders "decode" surrogate characters: let's fix this. + if (ecb_unlikely (IN_RANGE_INC (c, 0xd800, 0xdfff))) + c = 0xfffd; + + // rely on wcwidth to tell us the character width, do wcwidth before + // further replacements, as wcwidth might return -1 for the line + // drawing characters below as they might be invalid in the current + // locale. + int width = WCWIDTH (c); + + if (ecb_unlikely (charsets [screen.charset] == '0')) // DEC SPECIAL + { + // vt100 special graphics and line drawing + // 5f-7e standard vt100 + // 40-5e rxvt extension for extra curses acs chars + static uint16_t vt100_0[62] = { // 41 .. 7e + 0x2191, 0x2193, 0x2192, 0x2190, 0x2588, 0x259a, 0x2603, // 41-47 hi mr. snowman! + 0, 0, 0, 0, 0, 0, 0, 0, // 48-4f + 0, 0, 0, 0, 0, 0, 0, 0, // 50-57 + 0, 0, 0, 0, 0, 0, 0, 0x0020, // 58-5f + 0x25c6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1, // 60-67 + 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba, // 68-6f + 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c, // 70-77 + 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, // 78-7e + }; + + if (c >= 0x41 && c <= 0x7e && vt100_0[c - 0x41]) + { + c = vt100_0[c - 0x41]; + width = 1; // vt100 line drawing characters are always single-width + } + } + + if (ecb_unlikely (screen.flags & Screen_Insert)) + scr_insdel_chars (width, INSERT); + + if (width != 0) + { +#if !UNICODE_3 + // trim characters we can't store directly :( + if (c >= 0x10000) +# if ENABLE_COMBINING + c = rxvt_composite.compose (c); // map to lower 16 bits +# else + c = 0xfffd; +# endif +#endif + + rend_t rend = SET_FONT (rstyle, FONTSET (rstyle)->find_font (c)); + + // if the character doesn't fit into the remaining columns... + if (ecb_unlikely (screen.cur.col > ncol - width && ncol >= width)) + { + if (screen.flags & Screen_Autowrap) + { + // ... artificially enlargen the previous one + c = NOCHAR; + // and try the same character next loop iteration + --str; + } + else + screen.cur.col = ncol - width; + } + + // nuke the character at this position, if required + // due to wonderful coincidences everywhere else in this loop + // we never have to check for overwriting a wide char itself, + // only its tail. + if (ecb_unlikely (line->t[screen.cur.col] == NOCHAR)) + scr_kill_char (*line, screen.cur.col); + + line->touch (); + + do + { + line->t[screen.cur.col] = c; + line->r[screen.cur.col] = rend; + + if (ecb_likely (screen.cur.col < ncol - 1)) + screen.cur.col++; + else + { + line->l = ncol; + if (screen.flags & Screen_Autowrap) + screen.flags |= Screen_WrapNext; + + goto end_of_line; + } + + c = NOCHAR; + } + while (ecb_unlikely (--width > 0)); - if (R->screen.cur.col < last_col - 1) - R->screen.cur.col++; - else { - R->screen.tlen[row] = last_col; - if (R->screen.flags & Screen_Autowrap) - R->screen.flags |= Screen_WrapNext; - } - } - if (R->screen.tlen[row] != -1) /* XXX: think about this */ - MAX_IT(R->screen.tlen[row], R->screen.cur.col); - -/* - * If we wrote anywhere in the selected area, kill the selection - * XXX: should we kill the mark too? Possibly, but maybe that - * should be a similar check. - */ - if (clearsel) - CLEAR_SELECTION(R); - -#ifdef DEBUG_STRICT - assert(R->screen.cur.row >= 0); -#else /* drive with your eyes closed */ - MAX_IT(R->screen.cur.row, 0); + // pad with spaces when overwriting wide character with smaller one + for (int c = screen.cur.col; ecb_unlikely (c < ncol && line->t[c] == NOCHAR); c++) + { + line->t[c] = ' '; + line->r[c] = rend; + } + +end_of_line: + ; + } +#if ENABLE_COMBINING + else // width == 0 + { + if (c != 0xfeff) // ignore BOM + { + // handle combining characters + // we just tag the accent on the previous on-screen character. + // this is arguably not correct, but also arguably not wrong. + // we don't handle double-width characters nicely yet. + line_t *linep; + text_t *tp; + rend_t *rp; + + if (screen.cur.col > 0) + { + linep = line; + tp = line->t + screen.cur.col - 1; + rp = line->r + screen.cur.col - 1; + } + else if (screen.cur.row > 0 + && ROW(screen.cur.row - 1).is_longer ()) + { + linep = &ROW(screen.cur.row - 1); + tp = linep->t + ncol - 1; + rp = linep->r + ncol - 1; + } + else + continue; + + linep->touch (); + + while (*tp == NOCHAR && tp > linep->t) + tp--, rp--; + + // first try to find a precomposed character + unicode_t n = rxvt_compose (*tp, c); + if (n == NOCHAR) + n = rxvt_composite.compose (*tp, c); + + *tp = n; + *rp = SET_FONT (*rp, FONTSET (*rp)->find_font (*tp)); + } + } #endif + } + + max_it (line->l, screen.cur.col); + + assert (screen.cur.row >= 0); } /* ------------------------------------------------------------------------- */ @@ -906,23 +1047,25 @@ * Process Backspace. Move back the cursor back a position, wrap if have to * XTERM_SEQ: CTRL-H */ -/* EXTPROTO */ void -rxvt_scr_backspace(pR) +rxvt_term::scr_backspace () noexcept { - RESET_CHSTAT(R->h); - R->h->want_refresh = 1; - if (R->screen.cur.col == 0) { - if (R->screen.cur.row > 0) { + if (screen.cur.col == 0) + { + if (screen.cur.row > 0) + { #ifdef TERMCAP_HAS_BW - R->screen.cur.col = R->TermWin.ncol - 1; - R->screen.cur.row--; - return; -#endif - } - } else if ((R->screen.flags & Screen_WrapNext) == 0) - rxvt_scr_gotorc(aR_ 0, -1, RELATIVE); - R->screen.flags &= ~Screen_WrapNext; + screen.cur.col = ncol - 1; + --screen.cur.row; + + want_refresh = 1; +#endif + } + } + else if (screen.flags & Screen_WrapNext) + screen.flags &= ~Screen_WrapNext; + else + scr_gotorc (0, -1, RELATIVE); } /* ------------------------------------------------------------------------- */ @@ -931,39 +1074,73 @@ * count: +ve = forward; -ve = backwards * XTERM_SEQ: CTRL-I */ -/* EXTPROTO */ void -rxvt_scr_tab(pR_ int count) +rxvt_term::scr_tab (int count, bool ht) noexcept { - int i, x; + int i, x; + + want_refresh = 1; + i = x = screen.cur.col; + + if (count == 0) + return; + else if (count > 0) + { + line_t &l = ROW(screen.cur.row); + rend_t base_rend = l.r[i]; + ht &= l.t[i] == ' '; + + for (; ++i < ncol; ) + if (tabs[i]) + { + x = i; + + if (!--count) + break; + } + else + ht &= l.t[i] == ' ' + && RS_SAME (l.r[i], base_rend); + + if (count) + x = ncol - 1; + + // store horizontal tab commands as characters inside the text + // buffer so they can be selected and pasted. + if (ht && option (Opt_pastableTabs)) + { + base_rend = SET_FONT (base_rend, 0); + + l.touch (x); + + i = screen.cur.col; + + l.t[i] = '\t'; + l.r[i] = base_rend; + + while (++i < x) + { + l.t[i] = NOCHAR; + l.r[i] = base_rend; + } + } + } + else /* if (count < 0) */ + { + for (; --i >= 0; ) + if (tabs[i]) + { + x = i; + if (!++count) + break; + } - D_SCREEN((stderr, "rxvt_scr_tab(%d)", count)); - R->h->want_refresh = 1; - RESET_CHSTAT(R->h); - i = x = R->screen.cur.col; - if (count == 0) - return; - else if (count > 0) { - for (; ++i < R->TermWin.ncol; ) - if (R->tabs[i]) { - x = i; - if (!--count) - break; - } - if (count) - x = R->TermWin.ncol - 1; - } else /* if (count < 0) */ { - for (; --i >= 0; ) - if (R->tabs[i]) { - x = i; - if (!++count) - break; - } - if (count) - x = 0; + if (count) + x = 0; } - if (x != R->screen.cur.col) - rxvt_scr_gotorc(aR_ 0, x, R_RELATIVE); + + if (x != screen.cur.col) + scr_gotorc (0, x, R_RELATIVE); } /* ------------------------------------------------------------------------- */ @@ -973,18 +1150,14 @@ * Move cursor left in row. If we're at the left boundary, shift everything * in that row right. Clear left column. */ -#ifndef NO_FRILLS -/* EXTPROTO */ -void -rxvt_scr_backindex(pR) -{ - if (R->screen.cur.col > 0) - rxvt_scr_gotorc(aR_ 0, -1, R_RELATIVE | C_RELATIVE); - else { - if (R->screen.tlen[R->screen.cur.row + R->TermWin.saveLines] == 0) - return; /* um, yeah? */ - rxvt_scr_insdel_chars(aR_ 1, INSERT); - } +#if !ENABLE_MINIMAL +void ecb_cold +rxvt_term::scr_backindex () noexcept +{ + if (screen.cur.col > 0) + scr_gotorc (0, -1, R_RELATIVE | C_RELATIVE); + else + scr_insdel_chars (1, INSERT); } #endif /* ------------------------------------------------------------------------- */ @@ -994,24 +1167,22 @@ * Move cursor right in row. If we're at the right boundary, shift everything * in that row left. Clear right column. */ -#ifndef NO_FRILLS -/* EXTPROTO */ -void -rxvt_scr_forwardindex(pR) -{ - int row; +#if !ENABLE_MINIMAL +void ecb_cold +rxvt_term::scr_forwardindex () noexcept +{ + if (screen.cur.col < ncol - 1) + scr_gotorc (0, 1, R_RELATIVE | C_RELATIVE); + else + { + line_t &l = ROW(screen.cur.row); + + l.touch (); + l.is_longer (0); - if (R->screen.cur.col < R->TermWin.ncol - 1) - rxvt_scr_gotorc(aR_ 0, 1, R_RELATIVE | C_RELATIVE); - else { - row = R->screen.cur.row + R->TermWin.saveLines; - if (R->screen.tlen[row] == 0) - return; /* um, yeah? */ - else if (R->screen.tlen[row] == -1) - R->screen.tlen[row] = R->TermWin.ncol; - rxvt_scr_gotorc(aR_ 0, 0, R_RELATIVE); - rxvt_scr_insdel_chars(aR_ 1, DELETE); - rxvt_scr_gotorc(aR_ 0, R->TermWin.ncol - 1, R_RELATIVE); + scr_gotorc (0, 0, R_RELATIVE); + scr_insdel_chars (1, DELETE); + scr_gotorc (0, ncol - 1, R_RELATIVE); } } #endif @@ -1020,82 +1191,71 @@ /* * Goto Row/Column */ -/* EXTPROTO */ void -rxvt_scr_gotorc(pR_ int row, int col, int relative) +rxvt_term::scr_gotorc (int row, int col, int relative) noexcept { - R->h->want_refresh = 1; - ZERO_SCROLLBACK(R); - RESET_CHSTAT(R->h); -#ifdef RXVT_GRAPHICS - if (rxvt_Gr_Displayed(aR)) - rxvt_Gr_scroll(aR_ 0); -#endif + want_refresh = 1; + ZERO_SCROLLBACK (); - D_SCREEN((stderr, "rxvt_scr_gotorc(r:%s%d,c:%s%d): from (r:%d,c:%d)", (relative & R_RELATIVE ? "+" : ""), row, (relative & C_RELATIVE ? "+" : ""), col, R->screen.cur.row, R->screen.cur.col)); + screen.cur.col = relative & C_RELATIVE ? screen.cur.col + col : col; + clamp_it (screen.cur.col, 0, ncol - 1); - R->screen.cur.col = ((relative & C_RELATIVE) ? (R->screen.cur.col + col) - : col); - MAX_IT(R->screen.cur.col, 0); - MIN_IT(R->screen.cur.col, (int32_t)R->TermWin.ncol - 1); + screen.flags &= ~Screen_WrapNext; - R->screen.flags &= ~Screen_WrapNext; - if (relative & R_RELATIVE) { - if (row > 0) { - if (R->screen.cur.row <= R->screen.bscroll - && (R->screen.cur.row + row) > R->screen.bscroll) - R->screen.cur.row = R->screen.bscroll; - else - R->screen.cur.row += row; - } else if (row < 0) { - if (R->screen.cur.row >= R->screen.tscroll - && (R->screen.cur.row + row) < R->screen.tscroll) - R->screen.cur.row = R->screen.tscroll; - else - R->screen.cur.row += row; - } - } else { - if (R->screen.flags & Screen_Relative) { /* relative origin mode */ - R->screen.cur.row = row + R->screen.tscroll; - MIN_IT(R->screen.cur.row, R->screen.bscroll); - } else - R->screen.cur.row = row; + if (relative & R_RELATIVE) + { + if (row > 0) + { + if (screen.cur.row <= screen.bscroll + && (screen.cur.row + row) > screen.bscroll) + screen.cur.row = screen.bscroll; + else + screen.cur.row += row; + } + else if (row < 0) + { + if (screen.cur.row >= screen.tscroll + && (screen.cur.row + row) < screen.tscroll) + screen.cur.row = screen.tscroll; + else + screen.cur.row += row; + } } - MAX_IT(R->screen.cur.row, 0); - MIN_IT(R->screen.cur.row, (int32_t)R->TermWin.nrow - 1); + else + { + if (screen.flags & Screen_Relative) + { + /* relative origin mode */ + screen.cur.row = row + screen.tscroll; + min_it (screen.cur.row, screen.bscroll); + } + else + screen.cur.row = row; + } + + clamp_it (screen.cur.row, 0, nrow - 1); } /* ------------------------------------------------------------------------- */ /* - * direction should be UP or DN + * direction should be UP or DN */ -/* EXTPROTO */ void -rxvt_scr_index(pR_ enum page_dirn direction) +rxvt_term::scr_index (enum page_dirn direction) noexcept { - int dirn; + want_refresh = 1; + ZERO_SCROLLBACK (); - R->h->want_refresh = 1; - dirn = ((direction == UP) ? 1 : -1); - D_SCREEN((stderr, "rxvt_scr_index(%d)", dirn)); + screen.flags &= ~Screen_WrapNext; - ZERO_SCROLLBACK(R); - RESET_CHSTAT(R->h); + if ((screen.cur.row == screen.bscroll && direction == UP) + || (screen.cur.row == screen.tscroll && direction == DN)) + scr_scroll_text (screen.tscroll, screen.bscroll, direction); + else + screen.cur.row += direction; -#ifdef RXVT_GRAPHICS - if (rxvt_Gr_Displayed(aR)) - rxvt_Gr_scroll(aR_ 0); -#endif - - R->screen.flags &= ~Screen_WrapNext; - if ((R->screen.cur.row == R->screen.bscroll && direction == UP) - || (R->screen.cur.row == R->screen.tscroll && direction == DN)) - rxvt_scroll_text(aR_ R->screen.tscroll, R->screen.bscroll, dirn, 0); - else - R->screen.cur.row += dirn; - MAX_IT(R->screen.cur.row, 0); - MIN_IT(R->screen.cur.row, (int32_t)R->TermWin.nrow - 1); - rxvt_selection_check(aR_ 0); + clamp_it (screen.cur.row, 0, nrow - 1); + selection_check (0); } /* ------------------------------------------------------------------------- */ @@ -1104,166 +1264,195 @@ * XTERM_SEQ: Clear line to right: ESC [ 0 K * XTERM_SEQ: Clear line to left : ESC [ 1 K * XTERM_SEQ: Clear whole line : ESC [ 2 K + * extension: clear to right unless wrapped: ESC [ 3 K */ -/* EXTPROTO */ void -rxvt_scr_erase_line(pR_ int mode) +rxvt_term::scr_erase_line (int mode) noexcept { - unsigned int row, col, num; + unsigned int col, num; - R->h->want_refresh = 1; - D_SCREEN((stderr, "rxvt_scr_erase_line(%d) at screen row: %d", mode, R->screen.cur.row)); - ZERO_SCROLLBACK(R); - RESET_CHSTAT(R->h); - -#ifdef RXVT_GRAPHICS - if (rxvt_Gr_Displayed(aR)) - rxvt_Gr_scroll(aR_ 0); -#endif - - rxvt_selection_check(aR_ 1); - - R->screen.flags &= ~Screen_WrapNext; - - row = R->TermWin.saveLines + R->screen.cur.row; - switch (mode) { - case 0: /* erase to end of line */ - col = R->screen.cur.col; - num = R->TermWin.ncol - col; - MIN_IT(R->screen.tlen[row], (int16_t)col); - if (ROWCOL_IN_ROW_AT_OR_AFTER(R->selection.beg, R->screen.cur) - || ROWCOL_IN_ROW_AT_OR_AFTER(R->selection.end, R->screen.cur)) - CLEAR_SELECTION(R); - break; - case 1: /* erase to beginning of line */ - col = 0; - num = R->screen.cur.col + 1; - if (ROWCOL_IN_ROW_AT_OR_BEFORE(R->selection.beg, R->screen.cur) - || ROWCOL_IN_ROW_AT_OR_BEFORE(R->selection.end, R->screen.cur)) - CLEAR_SELECTION(R); - break; - case 2: /* erase whole line */ - col = 0; - num = R->TermWin.ncol; - R->screen.tlen[row] = 0; - if (R->selection.beg.row <= R->screen.cur.row - && R->selection.end.row >= R->screen.cur.row) - CLEAR_SELECTION(R); - break; - default: - return; - } - if (R->screen.text[row]) - rxvt_blank_line(aR_ - &(R->screen.text[row][col]), - &(R->screen.rend[row][col]), num, R->h->rstyle); - else - rxvt_blank_screen_mem(aR_ R->screen.text, R->screen.rend, row, - R->h->rstyle); + want_refresh = 1; + ZERO_SCROLLBACK (); + + selection_check (1); + + line_t &line = ROW(screen.cur.row); + + line.touch (); + line.is_longer (0); + + switch (mode) + { + case 3: + if (screen.flags & Screen_WrapNext) + return; + + /* fall through */ + + case 0: /* erase to end of line */ + col = screen.cur.col; + num = ncol - col; + min_it (line.l, col); + + if (ROWCOL_IN_ROW_AT_OR_AFTER (selection.beg, screen.cur) + || ROWCOL_IN_ROW_AT_OR_AFTER (selection.end, screen.cur)) + CLEAR_SELECTION (); + break; + + case 1: /* erase to beginning of line */ + col = 0; + num = screen.cur.col + 1; + + if (ROWCOL_IN_ROW_AT_OR_BEFORE (selection.beg, screen.cur) + || ROWCOL_IN_ROW_AT_OR_BEFORE (selection.end, screen.cur)) + CLEAR_SELECTION (); + break; + + case 2: /* erase whole line */ + col = 0; + num = ncol; + line.l = 0; + if (selection.beg.row <= screen.cur.row + && selection.end.row >= screen.cur.row) + CLEAR_SELECTION (); + break; + default: + return; + } + + scr_blank_line (line, col, num, rstyle); } /* ------------------------------------------------------------------------- */ /* - * Erase part of whole of the screen + * Erase part or whole of the screen * XTERM_SEQ: Clear screen after cursor : ESC [ 0 J * XTERM_SEQ: Clear screen before cursor: ESC [ 1 J * XTERM_SEQ: Clear whole screen : ESC [ 2 J */ -/* EXTPROTO */ void -rxvt_scr_erase_screen(pR_ int mode) +rxvt_term::scr_erase_screen (int mode) noexcept { - int num; - int32_t row, row_offset; - rend_t ren; - XGCValues gcvalue; - - R->h->want_refresh = 1; - D_SCREEN((stderr, "rxvt_scr_erase_screen(%d) at screen row: %d", mode, R->screen.cur.row)); - ZERO_SCROLLBACK(R); - RESET_CHSTAT(R->h); - row_offset = (int32_t)R->TermWin.saveLines; - - switch (mode) { - case 0: /* erase to end of screen */ - rxvt_selection_check(aR_ 1); - rxvt_scr_erase_line(aR_ 0); - row = R->screen.cur.row + 1; /* possible OOB */ - num = R->TermWin.nrow - row; - break; - case 1: /* erase to beginning of screen */ - rxvt_selection_check(aR_ 3); - rxvt_scr_erase_line(aR_ 1); - row = 0; - num = R->screen.cur.row; - break; - case 2: /* erase whole screen */ - rxvt_selection_check(aR_ 3); -#ifdef RXVT_GRAPHICS - rxvt_Gr_ClearScreen(aR); -#endif - row = 0; - num = R->TermWin.nrow; - break; - default: - return; - } - R->h->refresh_type |= REFRESH_BOUNDS; - if (R->selection.op && R->h->current_screen == R->selection.screen - && ((R->selection.beg.row >= row && R->selection.beg.row <= row + num) - || (R->selection.end.row >= row - && R->selection.end.row <= row + num))) - CLEAR_SELECTION(R); - if (row >= R->TermWin.nrow) /* Out Of Bounds */ - return; - MIN_IT(num, (R->TermWin.nrow - row)); - if (R->h->rstyle & (RS_RVid | RS_Uline)) - ren = (rend_t) ~RS_None; - else if (GET_BASEBG(R->h->rstyle) == Color_bg) { - ren = DEFAULT_RSTYLE; - CLEAR_ROWS(row, num); - } else { - ren = (R->h->rstyle & (RS_fgMask | RS_bgMask)); - gcvalue.foreground = R->PixColors[GET_BGCOLOR(R->h->rstyle)]; - XChangeGC(R->Xdisplay, R->TermWin.gc, GCForeground, &gcvalue); - ERASE_ROWS(row, num); - gcvalue.foreground = R->PixColors[Color_fg]; - XChangeGC(R->Xdisplay, R->TermWin.gc, GCForeground, &gcvalue); - } - for (; num--; row++) { - rxvt_blank_screen_mem(aR_ R->screen.text, R->screen.rend, - (unsigned int)(row + row_offset), R->h->rstyle); - R->screen.tlen[row + row_offset] = 0; - rxvt_blank_line(aR_ - R->drawn_text[row], R->drawn_rend[row], - (unsigned int)R->TermWin.ncol, ren); + int num; + int32_t row; + rend_t ren; + XGCValues gcvalue; + + want_refresh = 1; + ZERO_SCROLLBACK (); + + switch (mode) + { + case 0: /* erase to end of screen */ + scr_erase_line (0); + row = screen.cur.row + 1; /* possible OOB */ + num = nrow - row; + break; + case 1: /* erase to beginning of screen */ + scr_erase_line (1); + row = 0; + num = screen.cur.row; + break; + case 2: /* erase whole screen */ + row = 0; + num = nrow; + break; + default: + return; + } + + if (selection.op && current_screen == selection.screen + && ((selection.beg.row >= row && selection.beg.row <= row + num) + || (selection.end.row >= row + && selection.end.row <= row + num))) + CLEAR_SELECTION (); + + if (row >= nrow) /* Out Of Bounds */ + return; + + min_it (num, nrow - row); + + if (rstyle & (RS_Blink | RS_RVid | RS_Uline)) + ren = (rend_t) ~RS_None; + else if (GET_BASEBG (rstyle) == Color_bg) + { + ren = DEFAULT_RSTYLE; + + if (mapped) + XClearArea (dpy, vt, 0, + Row2Pixel (row - view_start), (unsigned int)vt_width, + (unsigned int)Height2Pixel (num), False); + } + else + { + ren = rstyle & (RS_fgMask | RS_bgMask); + + if (mapped) + { + gcvalue.foreground = pix_colors[bgcolor_of (rstyle)]; + XChangeGC (dpy, gc, GCForeground, &gcvalue); + XFillRectangle (dpy, vt, gc, + 0, Row2Pixel (row - view_start), + (unsigned int)vt_width, + (unsigned int)Height2Pixel (num)); + gcvalue.foreground = pix_colors[Color_fg]; + XChangeGC (dpy, gc, GCForeground, &gcvalue); + } + } + + for (; num--; row++) + { + scr_blank_screen_mem (ROW(row), rstyle); + + if (row - view_start < nrow) + scr_blank_line (drawn_buf [row - view_start], 0, ncol, ren); } } +#if !ENABLE_MINIMAL +void +rxvt_term::scr_erase_savelines () noexcept +{ + want_refresh = 1; + ZERO_SCROLLBACK (); + + top_row = 0; +} +#endif + /* ------------------------------------------------------------------------- */ /* * Fill the screen with `E's * XTERM_SEQ: Screen Alignment Test: ESC # 8 */ -/* EXTPROTO */ -void -rxvt_scr_E(pR) +void ecb_cold +rxvt_term::scr_E () noexcept { - int i, j, k; - rend_t *r1, fs; + rend_t fs; + + want_refresh = 1; + ZERO_SCROLLBACK (); - R->h->want_refresh = 1; - R->h->num_scr_allow = 0; - ZERO_SCROLLBACK(R); - RESET_CHSTAT(R->h); - rxvt_selection_check(aR_ 3); - - fs = SET_FONT (R->h->rstyle, R->TermWin.fontset->find_font ('E')); - for (k = R->TermWin.saveLines, i = R->TermWin.nrow; i--; k++) { - R->screen.tlen[k] = R->TermWin.ncol; /* make the `E's selectable */ - fill_text (R->screen.text[k], 'E', R->TermWin.ncol); - for (r1 = R->screen.rend[k], j = R->TermWin.ncol; j--; ) - *r1++ = fs; + num_scr_allow = 0; + + row_col_t pos; + pos.row = pos.col = 0; + if (ROWCOL_IS_AFTER (selection.end, pos)) + CLEAR_SELECTION (); + + fs = SET_FONT (rstyle, FONTSET (rstyle)->find_font ('E')); + for (int row = nrow; row--; ) + { + line_t &line = ROW(row); + + fill_text (line.t, 'E', ncol); + rend_t *r1 = line.r; + + for (int j = ncol; j--; ) + *r1++ = fs; + + line.is_longer (0); + line.touch (ncol); } } @@ -1271,136 +1460,137 @@ /* * Insert/Delete lines */ -/* EXTPROTO */ void -rxvt_scr_insdel_lines(pR_ int count, int insdel) +rxvt_term::scr_insdel_lines (int count, int insdel) noexcept { - int end; + int end; - ZERO_SCROLLBACK(R); - RESET_CHSTAT(R->h); + ZERO_SCROLLBACK (); -#ifdef RXVT_GRAPHICS - if (rxvt_Gr_Displayed(aR)) - rxvt_Gr_scroll(aR_ 0); -#endif - - rxvt_selection_check(aR_ 1); + selection_check (1); - if (R->screen.cur.row > R->screen.bscroll) - return; + if (screen.cur.row > screen.bscroll) + return; - end = R->screen.bscroll - R->screen.cur.row + 1; - if (count > end) { - if (insdel == DELETE) - return; - else if (insdel == INSERT) - count = end; + end = screen.bscroll - screen.cur.row + 1; + if (count > end) + { + if (insdel == DELETE) + return; + else if (insdel == INSERT) + count = end; } - R->screen.flags &= ~Screen_WrapNext; - rxvt_scroll_text(aR_ R->screen.cur.row, R->screen.bscroll, insdel * count, - 0); + scr_do_wrap (); + + scr_scroll_text (screen.cur.row, screen.bscroll, insdel * count); } /* ------------------------------------------------------------------------- */ /* * Insert/Delete characters from the current position */ -/* EXTPROTO */ void -rxvt_scr_insdel_chars(pR_ int count, int insdel) +rxvt_term::scr_insdel_chars (int count, int insdel) noexcept { - int col, row; - rend_t tr; - text_t *stp; - rend_t *srp; - int16_t *slp; + want_refresh = 1; + ZERO_SCROLLBACK (); - R->h->want_refresh = 1; - ZERO_SCROLLBACK(R); -#if 0 - RESET_CHSTAT(R->h); -#endif + if (count <= 0) + return; + + scr_do_wrap (); + + selection_check (1); + min_it (count, ncol - screen.cur.col); + + int row = screen.cur.row; + + line_t *line = &ROW(row); + + line->touch (); + line->is_longer (0); + + // nuke wide spanning the start + if (line->t[screen.cur.col] == NOCHAR) + scr_kill_char (*line, screen.cur.col); + + switch (insdel) + { + case INSERT: + line->l = min (line->l + count, ncol); + + if (line->t[screen.cur.col] == NOCHAR) + scr_kill_char (*line, screen.cur.col); + + for (int col = ncol - 1; (col - count) >= screen.cur.col; col--) + { + line->t[col] = line->t[col - count]; + line->r[col] = line->r[col - count]; + } + + if (selection.op && current_screen == selection.screen + && ROWCOL_IN_ROW_AT_OR_AFTER (selection.beg, screen.cur)) + { + if (selection.end.row != screen.cur.row + || (selection.end.col + count >= ncol)) + CLEAR_SELECTION (); + else + { + /* shift selection */ + selection.beg.col += count; + selection.mark.col += count; /* XXX: yes? */ + selection.end.col += count; + } + } -#ifdef RXVT_GRAPHICS - if (rxvt_Gr_Displayed(aR)) - rxvt_Gr_scroll(aR_ 0); -#endif - - if (count <= 0) - return; - - rxvt_selection_check(aR_ 1); - MIN_IT(count, (R->TermWin.ncol - R->screen.cur.col)); - - row = R->screen.cur.row + R->TermWin.saveLines; - R->screen.flags &= ~Screen_WrapNext; - - stp = R->screen.text[row]; - srp = R->screen.rend[row]; - slp = &(R->screen.tlen[row]); - switch (insdel) { - case INSERT: - for (col = R->TermWin.ncol - 1; (col - count) >= R->screen.cur.col; - col--) { - stp[col] = stp[col - count]; - srp[col] = srp[col - count]; - } - if (*slp != -1) { - *slp += count; - MIN_IT(*slp, R->TermWin.ncol); - } - if (R->selection.op && R->h->current_screen == R->selection.screen - && ROWCOL_IN_ROW_AT_OR_AFTER(R->selection.beg, R->screen.cur)) { - if (R->selection.end.row != R->screen.cur.row - || (R->selection.end.col + count >= R->TermWin.ncol)) - CLEAR_SELECTION(R); - else { /* shift selection */ - R->selection.beg.col += count; - R->selection.mark.col += count; /* XXX: yes? */ - R->selection.end.col += count; - } - } - rxvt_blank_line(aR_ &(stp[R->screen.cur.col]), &(srp[R->screen.cur.col]), - (unsigned int)count, R->h->rstyle); - break; - case ERASE: - R->screen.cur.col += count; /* don't worry if > R->TermWin.ncol */ - rxvt_selection_check(aR_ 1); - R->screen.cur.col -= count; - rxvt_blank_line(aR_ &(stp[R->screen.cur.col]), &(srp[R->screen.cur.col]), - (unsigned int)count, R->h->rstyle); - break; - case DELETE: - tr = srp[R->TermWin.ncol - 1] - & (RS_fgMask | RS_bgMask | RS_baseattrMask); - for (col = R->screen.cur.col; (col + count) < R->TermWin.ncol; col++) { - stp[col] = stp[col + count]; - srp[col] = srp[col + count]; - } - rxvt_blank_line(aR_ - &(stp[R->TermWin.ncol - count]), - &(srp[R->TermWin.ncol - count]), - (unsigned int)count, tr); - if (*slp == -1) /* break line continuation */ - *slp = R->TermWin.ncol; - *slp -= count; - MAX_IT(*slp, 0); - if (R->selection.op && R->h->current_screen == R->selection.screen - && ROWCOL_IN_ROW_AT_OR_AFTER(R->selection.beg, R->screen.cur)) { - if (R->selection.end.row != R->screen.cur.row - || (R->screen.cur.col >= R->selection.beg.col - count) - || R->selection.end.col >= R->TermWin.ncol) - CLEAR_SELECTION(R); - else { - /* shift selection */ - R->selection.beg.col -= count; - R->selection.mark.col -= count; /* XXX: yes? */ - R->selection.end.col -= count; - } - } - break; + scr_blank_line (*line, screen.cur.col, count, rstyle); + break; + + case ERASE: + screen.cur.col += count; /* don't worry if > ncol */ + selection_check (1); + screen.cur.col -= count; + + // nuke wide char after the end + if (screen.cur.col + count < ncol && line->t[screen.cur.col + count] == NOCHAR) + scr_kill_char (*line, screen.cur.col + count); + + scr_blank_line (*line, screen.cur.col, count, rstyle); + break; + + case DELETE: + line->l = max (line->l - count, 0); + + // nuke wide char spanning the end + if (screen.cur.col + count < ncol && line->t[screen.cur.col + count] == NOCHAR) + scr_kill_char (*line, screen.cur.col + count); + + for (int col = screen.cur.col; (col + count) < ncol; col++) + { + line->t[col] = line->t[col + count]; + line->r[col] = line->r[col + count]; + } + + scr_blank_line (*line, ncol - count, count, rstyle); + + if (selection.op && current_screen == selection.screen + && ROWCOL_IN_ROW_AT_OR_AFTER (selection.beg, screen.cur)) + { + if (selection.end.row != screen.cur.row + || (screen.cur.col >= selection.beg.col - count) + || selection.end.col >= ncol) + CLEAR_SELECTION (); + else + { + /* shift selection */ + selection.beg.col -= count; + selection.mark.col -= count; /* XXX: yes? */ + selection.end.col -= count; + } + } + + break; } } @@ -1409,17 +1599,18 @@ * Set the scrolling region * XTERM_SEQ: Set region - inclusive: ESC [ ; r */ -/* EXTPROTO */ -void -rxvt_scr_scroll_region(pR_ int top, int bot) +void ecb_cold +rxvt_term::scr_scroll_region (int top, int bot) noexcept { - MAX_IT(top, 0); - MIN_IT(bot, (int)R->TermWin.nrow - 1); - if (top > bot) - return; - R->screen.tscroll = top; - R->screen.bscroll = bot; - rxvt_scr_gotorc(aR_ 0, 0, 0); + max_it (top, 0); + min_it (bot, nrow - 1); + + if (top > bot) + return; + + screen.tscroll = top; + screen.bscroll = bot; + scr_gotorc (0, 0, 0); } /* ------------------------------------------------------------------------- */ @@ -1428,15 +1619,15 @@ * XTERM_SEQ: Make cursor visible : ESC [ ? 25 h * XTERM_SEQ: Make cursor invisible: ESC [ ? 25 l */ -/* EXTPROTO */ -void -rxvt_scr_cursor_visible(pR_ int mode) +void ecb_cold +rxvt_term::scr_cursor_visible (int mode) noexcept { - R->h->want_refresh = 1; - if (mode) - R->screen.flags |= Screen_VisibleCursor; - else - R->screen.flags &= ~Screen_VisibleCursor; + want_refresh = 1; + + if (mode) + screen.flags |= Screen_VisibleCursor; + else + screen.flags &= ~Screen_VisibleCursor; } /* ------------------------------------------------------------------------- */ @@ -1445,14 +1636,13 @@ * XTERM_SEQ: Set Wraparound : ESC [ ? 7 h * XTERM_SEQ: Unset Wraparound: ESC [ ? 7 l */ -/* EXTPROTO */ -void -rxvt_scr_autowrap(pR_ int mode) +void ecb_cold +rxvt_term::scr_autowrap (int mode) noexcept { - if (mode) - R->screen.flags |= Screen_Autowrap; - else - R->screen.flags &= ~(Screen_Autowrap | Screen_WrapNext); + if (mode) + screen.flags |= Screen_Autowrap; + else + screen.flags &= ~(Screen_Autowrap | Screen_WrapNext); } /* ------------------------------------------------------------------------- */ @@ -1465,15 +1655,15 @@ * XTERM_SEQ: Set Absolute: ESC [ ? 6 h * XTERM_SEQ: Set Relative: ESC [ ? 6 l */ -/* EXTPROTO */ -void -rxvt_scr_relative_origin(pR_ int mode) +void ecb_cold +rxvt_term::scr_relative_origin (int mode) noexcept { - if (mode) - R->screen.flags |= Screen_Relative; - else - R->screen.flags &= ~Screen_Relative; - rxvt_scr_gotorc(aR_ 0, 0, 0); + if (mode) + screen.flags |= Screen_Relative; + else + screen.flags &= ~Screen_Relative; + + scr_gotorc (0, 0, 0); } /* ------------------------------------------------------------------------- */ @@ -1482,14 +1672,13 @@ * XTERM_SEQ: Set Insert mode : ESC [ ? 4 h * XTERM_SEQ: Set Replace mode: ESC [ ? 4 l */ -/* EXTPROTO */ -void -rxvt_scr_insert_mode(pR_ int mode) +void ecb_cold +rxvt_term::scr_insert_mode (int mode) noexcept { - if (mode) - R->screen.flags |= Screen_Insert; - else - R->screen.flags &= ~Screen_Insert; + if (mode) + screen.flags |= Screen_Insert; + else + screen.flags &= ~Screen_Insert; } /* ------------------------------------------------------------------------- */ @@ -1499,14 +1688,13 @@ * XTERM_SEQ: Clear tab at current column: ESC [ 0 g * XTERM_SEQ: Clear all tabs : ESC [ 3 g */ -/* EXTPROTO */ -void -rxvt_scr_set_tab(pR_ int mode) +void ecb_cold +rxvt_term::scr_set_tab (int mode) noexcept { - if (mode < 0) - MEMSET(R->tabs, 0, R->TermWin.ncol * sizeof(char)); - else if (R->screen.cur.col < R->TermWin.ncol) - R->tabs[R->screen.cur.col] = (mode ? 1 : 0); + if (mode < 0) + memset (tabs, 0, ncol); + else if (screen.cur.col < ncol) + tabs [screen.cur.col] = !!mode; } /* ------------------------------------------------------------------------- */ @@ -1515,30 +1703,40 @@ * XTERM_SEQ: Reverse video: ESC [ ? 5 h * XTERM_SEQ: Normal video : ESC [ ? 5 l */ -/* EXTPROTO */ void -rxvt_scr_rvideo_mode(pR_ int mode) +rxvt_term::scr_rvideo_mode (bool on) noexcept { - XGCValues gcvalue; + rvideo_mode = on; + +#ifndef NO_BELL + on ^= rvideo_bell; +#endif + + if (rvideo_state != on) + { + rvideo_state = on; - if (R->h->rvideo != mode) { - R->h->rvideo = mode; - SWAP_IT(R->PixColors[Color_fg], R->PixColors[Color_bg], rxvt_color); -#if defined(XPM_BACKGROUND) - if (R->h->bgPixmap.pixmap == None) -#endif -#if defined(TRANSPARENT) - if (!(R->Options & Opt_transparent) || R->h->am_transparent == 0) -#endif - XSetWindowBackground(R->Xdisplay, R->TermWin.vt, - R->PixColors[Color_bg]); - - gcvalue.foreground = R->PixColors[Color_fg]; - gcvalue.background = R->PixColors[Color_bg]; - XChangeGC(R->Xdisplay, R->TermWin.gc, GCBackground | GCForeground, - &gcvalue); - rxvt_scr_clear(aR); - rxvt_scr_touch(aR_ True); +#if OFF_FOCUS_FADING + if (rs[Rs_fade]) + { + ::swap (pix_colors_focused[Color_fg], pix_colors_focused[Color_bg]); + ::swap (pix_colors_unfocused[Color_fg], pix_colors_unfocused[Color_bg]); + } + else +#endif + ::swap (pix_colors[Color_fg], pix_colors[Color_bg]); +#ifdef HAVE_IMG + if (bg_img == 0) +#endif + XSetWindowBackground (dpy, vt, pix_colors[Color_bg]); + + XGCValues gcvalue; + gcvalue.foreground = pix_colors[Color_fg]; + gcvalue.background = pix_colors[Color_bg]; + XChangeGC (dpy, gc, GCBackground | GCForeground, &gcvalue); + + scr_clear (); + scr_touch (true); } } @@ -1547,12 +1745,10 @@ * Report current cursor position * XTERM_SEQ: Report position: ESC [ 6 n */ -/* EXTPROTO */ -void -rxvt_scr_report_position(pR) +void ecb_cold +rxvt_term::scr_report_position () noexcept { - rxvt_tt_printf(aR_ "\033[%d;%dR", R->screen.cur.row + 1, - R->screen.cur.col + 1); + tt_printf ("\033[%d;%dR", screen.cur.row + 1, screen.cur.col + 1); } /* ------------------------------------------------------------------------- * @@ -1562,26 +1758,28 @@ /* * Set font style */ -/* INTPROTO */ -void -rxvt_set_font_style(pR) +void ecb_cold +rxvt_term::set_font_style () noexcept { - switch (R->h->charsets[R->screen.charset]) { - case '0': /* DEC Special Character & Line Drawing Set */ - break; - case 'A': /* United Kingdom (UK) */ - break; - case 'B': /* United States (USASCII) */ - break; - case '<': /* Multinational character set */ - break; - case '5': /* Finnish character set */ - break; - case 'C': /* Finnish character set */ - break; - case 'K': /* German character set */ - break; +#if 0 + switch (charsets [screen.charset]) + { + case '0': /* DEC Special Character & Line Drawing Set */ + break; + case 'A': /* United Kingdom (UK) */ + break; + case 'B': /* United States (USASCII) */ + break; + case '<': /* Multinational character set */ + break; + case '5': /* Finnish character set */ + break; + case 'C': /* Finnish character set */ + break; + case 'K': /* German character set */ + break; } +#endif } /* ------------------------------------------------------------------------- */ @@ -1592,12 +1790,11 @@ * XTERM_SEQ: Invoke G2 character set: ESC N * XTERM_SEQ: Invoke G3 character set: ESC O */ -/* EXTPROTO */ -void -rxvt_scr_charset_choose(pR_ int set) +void ecb_cold +rxvt_term::scr_charset_choose (int set) noexcept { - R->screen.charset = set; - rxvt_set_font_style(aR); + screen.charset = set; + set_font_style (); } /* ------------------------------------------------------------------------- */ @@ -1609,98 +1806,98 @@ * XTERM_SEQ: Set G3 character set: ESC + * See set_font_style for possible values for */ -/* EXTPROTO */ void -rxvt_scr_charset_set(pR_ int set, unsigned int ch) +rxvt_term::scr_charset_set (int set, unsigned int ch) noexcept { - R->h->charsets[set] = (unsigned char)ch; - rxvt_set_font_style(aR); + charsets[set] = (unsigned char)ch; + set_font_style (); } /* ------------------------------------------------------------------------- * - * GRAPHICS COLOURS * + * MAJOR SCREEN MANIPULATION * * ------------------------------------------------------------------------- */ -#ifdef RXVT_GRAPHICS -/* EXTPROTO */ -int -rxvt_scr_get_fgcolor(pR) +/* + * refresh matching text. + */ +bool ecb_cold +rxvt_term::scr_refresh_rend (rend_t mask, rend_t value) noexcept { - return GET_FGCOLOR(R->h->rstyle); -} + bool found = false; -/* ------------------------------------------------------------------------- */ -/* EXTPROTO */ -int -rxvt_scr_get_bgcolor(pR) -{ - return GET_BGCOLOR(R->h->rstyle); + for (int i = 0; i < nrow; i++) + { + rend_t *drp = drawn_buf[i].r; + + for (int col = 0; col < ncol; col++, drp++) + if ((*drp & mask) == value) + { + found = true; + *drp = ~value; + } + } + + return found; } -#endif - -/* ------------------------------------------------------------------------- * - * MAJOR SCREEN MANIPULATION * - * ------------------------------------------------------------------------- */ /* * Refresh an area */ enum { - PART_BEG = 0, - PART_END, - RC_COUNT + PART_BEG = 0, + PART_END, + RC_COUNT }; -/* EXTPROTO */ -void -rxvt_scr_expose(pR_ int x, int y, int width, int height, Bool refresh) +void ecb_hot +rxvt_term::scr_expose (int x, int y, int ewidth, int eheight, bool refresh) noexcept { - int i; - row_col_t rc[RC_COUNT]; + int i; + row_col_t rc[RC_COUNT]; - if (R->drawn_text == NULL) /* sanity check */ - return; + if (!drawn_buf) /* sanity check */ + return; -#ifdef DEBUG_STRICT - x = max(x, (int)R->TermWin.int_bwidth); - x = min(x, (int)R->TermWin.width); - y = max(y, (int)R->TermWin.int_bwidth); - y = min(y, (int)R->TermWin.height); +#ifndef NO_SLOW_LINK_SUPPORT + if (refresh_type == FAST_REFRESH && !display->is_local) + { + y = 0; + eheight = height; + } #endif -/* round down */ - rc[PART_BEG].col = Pixel2Col(x); - rc[PART_BEG].row = Pixel2Row(y); -/* round up */ - rc[PART_END].col = Pixel2Width(x + width + R->TermWin.fwidth - 1); - rc[PART_END].row = Pixel2Row(y + height + R->TermWin.fheight - 1); + /* round down */ + rc[PART_BEG].col = Pixel2Col (x); + rc[PART_BEG].row = Pixel2Row (y); + /* round up */ + rc[PART_END].col = Pixel2Width (x + ewidth + fwidth - 1); + rc[PART_END].row = Pixel2Row (y + eheight + fheight - 1); -/* sanity checks */ - for (i = PART_BEG; i < RC_COUNT; i++) { - MIN_IT(rc[i].col, R->TermWin.ncol - 1); - MIN_IT(rc[i].row, R->TermWin.nrow - 1); + /* sanity checks */ + for (i = PART_BEG; i < RC_COUNT; i++) + { + min_it (rc[i].col, ncol - 1); + min_it (rc[i].row, nrow - 1); } - D_SCREEN((stderr, "rxvt_scr_expose(x:%d, y:%d, w:%d, h:%d) area (c:%d,r:%d)-(c:%d,r:%d)", x, y, width, height, rc[PART_BEG].col, rc[PART_BEG].row, rc[PART_END].col, rc[PART_END].row)); + for (i = rc[PART_BEG].row; i <= rc[PART_END].row; i++) + fill_text (&drawn_buf[i].t[rc[PART_BEG].col], 0, rc[PART_END].col - rc[PART_BEG].col + 1); - for (i = rc[PART_BEG].row; i <= rc[PART_END].row; i++) - fill_text (&(R->drawn_text[i][rc[PART_BEG].col]), 0, - (rc[PART_END].col - rc[PART_BEG].col + 1)); + num_scr_allow = 0; - if (refresh) - rxvt_scr_refresh(aR_ SLOW_REFRESH | REFRESH_BOUNDS); + if (refresh) + scr_refresh (); } /* ------------------------------------------------------------------------- */ /* * Refresh the entire screen */ -/* EXTPROTO */ void -rxvt_scr_touch(pR_ Bool refresh) +rxvt_term::scr_touch (bool refresh) noexcept { - rxvt_scr_expose(aR_ 0, 0, R->TermWin.width, R->TermWin.height, refresh); + scr_expose (0, 0, vt_width, vt_height, refresh); } /* ------------------------------------------------------------------------- */ @@ -1708,23 +1905,11 @@ * Move the display so that the line represented by scrollbar value Y is at * the top of the screen */ -/* EXTPROTO */ -int -rxvt_scr_move_to(pR_ int y, int len) -{ - long p = 0; - uint16_t oldviewstart; - - oldviewstart = R->TermWin.view_start; - if (y < len) { - p = (R->TermWin.nrow + R->TermWin.nscrolled) * (len - y) / len; - p -= (long)(R->TermWin.nrow - 1); - p = max(p, 0); - } - R->TermWin.view_start = (uint16_t)min(p, R->TermWin.nscrolled); - D_SCREEN((stderr, "rxvt_scr_move_to(%d, %d) view_start:%d", y, len, R->TermWin.view_start)); - - return rxvt_scr_changeview(aR_ oldviewstart); +void +rxvt_term::scr_move_to (int y, int len) noexcept +{ + // lerp (y, 0, len, top_row, nrow - 1) + scr_changeview (top_row + (nrow - 1 - top_row) * y / len); } /* ------------------------------------------------------------------------- */ @@ -1732,598 +1917,743 @@ * Page the screen up/down nlines * direction should be UP or DN */ -/* EXTPROTO */ -int -rxvt_scr_page(pR_ enum page_dirn direction, int nlines) +bool +rxvt_term::scr_page (int nlines) noexcept { - int n; - uint16_t oldviewstart; + return scr_changeview (view_start - nlines); +} - D_SCREEN((stderr, "rxvt_scr_page(%s, %d) view_start:%d", ((direction == UP) ? "UP" : "DN"), nlines, R->TermWin.view_start)); -#ifdef DEBUG_STRICT - assert((nlines >= 0) && (nlines <= R->TermWin.nrow)); -#endif - oldviewstart = R->TermWin.view_start; - if (direction == UP) { - n = R->TermWin.view_start + nlines; - R->TermWin.view_start = min(n, R->TermWin.nscrolled); - } else { - n = R->TermWin.view_start - nlines; - R->TermWin.view_start = max(n, 0); - } - return rxvt_scr_changeview(aR_ oldviewstart); +bool +rxvt_term::scr_changeview (int new_view_start) noexcept +{ + clamp_it (new_view_start, top_row, 0); + + if (new_view_start == view_start) + return false; + + num_scr += new_view_start - view_start; + view_start = new_view_start; + want_refresh = 1; + + HOOK_INVOKE ((this, HOOK_VIEW_CHANGE, DT_INT, view_start, DT_END)); + + return true; } -/* INTPROTO */ -int -rxvt_scr_changeview(pR_ uint16_t oldviewstart) +#ifndef NO_BELL +void +rxvt_term::bell_cb (ev::timer &w, int revents) { - if (R->TermWin.view_start != oldviewstart) { - R->h->want_refresh = 1; -#ifdef RXVT_GRAPHICS - if (rxvt_Gr_Displayed(aR)) - rxvt_Gr_scroll(aR_ 0); -#endif - R->h->num_scr -= (R->TermWin.view_start - oldviewstart); - } - return (int)(R->TermWin.view_start - oldviewstart); + rvideo_bell = false; + scr_rvideo_mode (rvideo_mode); + refresh_check (); } +#endif /* ------------------------------------------------------------------------- */ -/* EXTPROTO */ void -rxvt_scr_bell(pR) +rxvt_term::scr_bell () noexcept { #ifndef NO_BELL + # ifndef NO_MAPALERT # ifdef MAPALERT_OPTION - if (R->Options & Opt_mapAlert) + if (option (Opt_mapAlert)) # endif - XMapWindow(R->Xdisplay, R->TermWin.parent[0]); + XMapWindow (dpy, parent); # endif - if (R->Options & Opt_visualBell) { - rxvt_scr_rvideo_mode(aR_ !R->h->rvideo); /* refresh also done */ - rxvt_scr_rvideo_mode(aR_ !R->h->rvideo); /* refresh also done */ - } else - XBell(R->Xdisplay, 0); + +# if ENABLE_FRILLS + if (option (Opt_urgentOnBell)) + set_urgency (1); +# endif + + if (option (Opt_visualBell)) + { + rvideo_bell = true; + scr_rvideo_mode (rvideo_mode); + flush (); + + bell_ev.start (VISUAL_BELL_DURATION); + } + else + XBell (dpy, 0); + HOOK_INVOKE ((this, HOOK_BELL, DT_END)); #endif } /* ------------------------------------------------------------------------- */ -/* ARGSUSED */ -/* EXTPROTO */ -void -rxvt_scr_printscreen(pR_ int fullhist) +void ecb_cold +rxvt_term::scr_printscreen (int fullhist) noexcept { #ifdef PRINTPIPE - int i, r1, nrows, row_offset; - text_t *t; - FILE *fd; - - if ((fd = rxvt_popen_printer(aR)) == NULL) - return; - nrows = R->TermWin.nrow; - row_offset = R->TermWin.saveLines; - if (!fullhist) - row_offset -= R->TermWin.view_start; - else { - nrows += R->TermWin.nscrolled; - row_offset -= R->TermWin.nscrolled; - } - - for (r1 = 0; r1 < nrows; r1++) { - t = R->screen.text[r1 + row_offset]; - for (i = R->TermWin.ncol - 1; i >= 0; i--) - if (!isspace(t[i])) - break; - fprintf(fd, "%.*s\n", (i + 1), t); + int nrows, row_start; + FILE *fd = popen_printer (); + + if (!fd) + return; + + if (fullhist) + { + nrows = nrow - top_row; + row_start = top_row; + } + else + { + nrows = nrow; + row_start = view_start; } - rxvt_pclose_printer(fd); -#endif -} -/* ------------------------------------------------------------------------- */ -/* - * Refresh the screen - * R->drawn_text/R->drawn_rend contain the screen information before the update. - * R->screen.text/R->screen.rend contain what the screen will change to. - */ + wctomb (0, 0); -#if defined (NO_BRIGHTCOLOR) || defined (VERYBOLD) -# define MONO_BOLD(x) ((x) & (RS_Bold|RS_Blink)) -# define MONO_BOLD_FG(x, fg) MONO_BOLD(x) -#else -# define MONO_BOLD(x) \ - (((x) & (RS_Bold | RS_fgMask)) == (RS_Bold | Color_fg)) -# define MONO_BOLD_FG(x, fg) (((x) & RS_Bold) && (fg) == Color_fg) -#endif - -#define FONT_WIDTH(X, Y) \ - (X)->per_char[(Y) - (X)->min_char_or_byte2].width -#define FONT_RBEAR(X, Y) \ - (X)->per_char[(Y) - (X)->min_char_or_byte2].rbearing -#define FONT_LBEAR(X, Y) \ - (X)->per_char[(Y) - (X)->min_char_or_byte2].lbearing -#define IS_FONT_CHAR(X, Y) \ - ((Y) >= (X)->min_char_or_byte2 && (Y) <= (X)->max_char_or_byte2) - -/* EXTPROTO */ -void -rxvt_scr_refresh(pR_ unsigned char refresh_type) -{ - unsigned char clearfirst, /* first character writes before cell */ - clearlast, /* last character writes beyond cell */ - must_clear, /* use draw_string not draw_image_string */ -#ifndef NO_BOLDFONT - bfont, /* we've changed font to bold font */ -#endif - rvid, /* reverse video this position */ - wbyte, /* we're in multibyte */ - showcursor; /* show the cursor */ - int fore, back; /* desired foreground/background */ - int16_t col, row, /* column/row we're processing */ - ocrow; /* old cursor row */ - int cursorwidth; - int i, /* tmp */ - row_offset; /* basic offset in screen structure */ -#ifndef NO_CURSORCOLOR - rend_t cc1; /* store colours at cursor position(s) */ - rend_t cc2; /* store colours at cursor position(s) */ -#endif - rend_t *drp, *srp; /* drawn-rend-pointer, screen-rend-pointer */ - text_t *dtp, *stp; /* drawn-text-pointer, screen-text-pointer */ - char *buffer; /* local copy of R->h->buffer */ - struct rxvt_hidden *h = R->h; + for (int r1 = row_start; r1 < row_start + nrows; r1++) + { + text_t *tp = ROW(r1).t; + int len = ROW(r1).l; - if (refresh_type == NO_REFRESH || !R->TermWin.mapped) - return; + for (int i = len >= 0 ? len : ncol - 1; i--; ) //TODO//FIXME//LEN + { + char mb[MB_LEN_MAX]; + text_t t = *tp++; + if (t == NOCHAR) + continue; -/* - * A: set up vars - */ - clearfirst = clearlast = must_clear = wbyte = 0; -#ifndef NO_BOLDFONT - bfont = 0; -#endif + len = wctomb (mb, t); - if (h->currmaxcol < R->TermWin.ncol) { - h->currmaxcol = R->TermWin.ncol; - h->buffer = (char *)rxvt_realloc (h->buffer, - sizeof(char) * (h->currmaxcol + 1) * MB_CUR_MAX); - } - buffer = h->buffer; - h->refresh_count = 0; + if (len <= 0) + { + mb[0] = ' '; + len = 1; + } - row_offset = R->TermWin.saveLines - R->TermWin.view_start; + fwrite (mb, 1, len, fd); + } - if ((refresh_type & REFRESH_BOUNDS)) { - clearfirst = clearlast = 1; - h->refresh_type &= ~REFRESH_BOUNDS; + fputc ('\n', fd); } -#if defined(XPM_BACKGROUND) - must_clear |= (h->bgPixmap.pixmap != None); -#endif -#if defined(TRANSPARENT) - must_clear |= ((R->Options & Opt_transparent) && h->am_transparent); -#endif - ocrow = h->oldcursor.row; /* is there an old outline cursor on screen? */ -/* - * B: reverse any characters which are selected - */ - rxvt_scr_reverse_selection(aR); + pclose_printer (fd); +#endif +} +/* ------------------------------------------------------------------------- */ /* - * C: set the cursor character(s) + * Refresh the screen + * drawn_text/drawn_rend contain the screen information before the update. + * screen.text/screen.rend contain what the screen will change to. */ - { - unsigned char setoldcursor; - rend_t ccol1, /* Cursor colour */ - ccol2; /* Cursor colour2 */ - - showcursor = (R->screen.flags & Screen_VisibleCursor); - cursorwidth = 0; +void ecb_hot +rxvt_term::scr_refresh () noexcept +{ + int16_t col, row, /* column/row we're processing */ + ocrow; /* old cursor row */ + int i; /* tmp */ + rend_t ccol1, /* Cursor colour */ + ccol2; /* Cursor colour2 */ + rend_t cur_rend; + int cur_col; + int cursorwidth; + + want_refresh = 0; /* screen is current */ + + if (refresh_type == NO_REFRESH || !mapped) + return; + + /* + * A: set up vars + */ + refresh_count = 0; + + unsigned int old_screen_flags = screen.flags; + bool have_bg = 0; +#ifdef HAVE_IMG + have_bg = bg_img != 0; +#endif + ocrow = oldcursor.row; /* is there an old outline cursor on screen? */ + + /* + * B: reverse any characters which are selected + */ + scr_reverse_selection (); + + HOOK_INVOKE ((this, HOOK_REFRESH_BEGIN, DT_END)); +#if ENABLE_OVERLAY + scr_swap_overlay (); +#endif + + bool showcursor = screen.flags & Screen_VisibleCursor; + + /* + * C: set the cursor character (s) + */ + { #ifdef CURSOR_BLINK - if (R->h->hidden_cursor) - showcursor = 0; + if (hidden_cursor) + showcursor = 0; #endif - cursorwidth = 0; + if (showcursor) + { + int col = screen.cur.col; - if (showcursor) - { - cursorwidth++; + while (col && ROW(screen.cur.row).t[col] == NOCHAR) + col--; - srp = &(R->screen.rend[R->screen.cur.row + R->TermWin.saveLines] - [R->screen.cur.col]); + cursorwidth = 1; + while (col + cursorwidth < ncol + && ROW(screen.cur.row).t[col + cursorwidth] == NOCHAR) + cursorwidth++; + + cur_rend = ROW(screen.cur.row).r[col]; + cur_col = col; - if (showcursor && R->TermWin.focus) - { - *srp ^= RS_RVid; #ifndef NO_CURSORCOLOR - cc1 = *srp & (RS_fgMask | RS_bgMask); - if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_cursor)) - ccol1 = Color_cursor; - else + if (ISSET_PIXCOLOR (Color_cursor)) + ccol1 = Color_cursor; + else +#endif #ifdef CURSOR_COLOR_IS_RENDITION_COLOR - ccol1 = GET_FGCOLOR(h->rstyle); + ccol1 = fgcolor_of (rstyle); #else - ccol1 = Color_fg; + ccol1 = Color_fg; +#endif + +#ifndef NO_CURSORCOLOR + if (ISSET_PIXCOLOR (Color_cursor2)) + ccol2 = Color_cursor2; + else #endif - if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_cursor2)) - ccol2 = Color_cursor2; - else #ifdef CURSOR_COLOR_IS_RENDITION_COLOR - ccol2 = GET_BGCOLOR(h->rstyle); + ccol2 = bgcolor_of (rstyle); #else - ccol2 = Color_bg; -#endif - *srp = SET_FGCOLOR(*srp, ccol1); - *srp = SET_BGCOLOR(*srp, ccol2); + ccol2 = Color_bg; #endif - } - while (IS_WIDE (*srp)) - cursorwidth++, srp++; + if (focus && cursor_type == 0) + { + rend_t rend = cur_rend; + + rend ^= RS_RVid; + rend = SET_FGCOLOR (rend, ccol1); + rend = SET_BGCOLOR (rend, ccol2); + + scr_set_char_rend (ROW(screen.cur.row), cur_col, rend); } + } - /* make sure no outline cursor is left around */ - setoldcursor = 0; - if (ocrow != -1) { - if (R->screen.cur.row + R->TermWin.view_start != ocrow - || R->screen.cur.col != h->oldcursor.col) { - if (ocrow < R->TermWin.nrow - && h->oldcursor.col < R->TermWin.ncol) { - R->drawn_rend[ocrow][h->oldcursor.col] ^= (RS_RVid | RS_Uline); - } - if (R->TermWin.focus || !showcursor) - h->oldcursor.row = -1; - else - setoldcursor = 1; - } - } else if (!R->TermWin.focus) - setoldcursor = 1; - if (setoldcursor) { - if (R->screen.cur.row + R->TermWin.view_start >= R->TermWin.nrow) - h->oldcursor.row = -1; - else { - h->oldcursor.row = R->screen.cur.row + R->TermWin.view_start; - h->oldcursor.col = R->screen.cur.col; - } - } - } + /* make sure no outline cursor is left around */ + if (ocrow != -1 && ocrow < nrow && oldcursor.col < ncol) + drawn_buf[ocrow].r[oldcursor.col] ^= (RS_RVid | RS_Uline); + + // save the current cursor coordinates if the cursor is visible + // and either the window is unfocused or the cursor style is + // underline or vertical bar, so as to clear the outline cursor in + // the next refresh if the cursor moves or becomes invisible + if (showcursor && (!focus || cursor_type != 0) && screen.cur.row - view_start < nrow) + { + oldcursor.row = screen.cur.row - view_start; + oldcursor.col = screen.cur.col; + } + else + oldcursor.row = -1; + } #ifndef NO_SLOW_LINK_SUPPORT -/* - * D: CopyArea pass - very useful for slower links - * This has been deliberately kept simple. - */ - i = h->num_scr; - if (refresh_type == FAST_REFRESH && h->num_scr_allow && i - && abs(i) < R->TermWin.nrow && !must_clear) { - int16_t nits; - int j; - rend_t *drp2; - text_t *dtp2; - int len, wlen; - - j = R->TermWin.nrow; - wlen = len = -1; - row = i > 0 ? 0 : j - 1; - for (; j-- >= 0; row += (i > 0 ? 1 : -1)) { - if (row + i >= 0 && row + i < R->TermWin.nrow && row + i != ocrow) { - stp = R->screen.text[row + row_offset]; - srp = R->screen.rend[row + row_offset]; - dtp = R->drawn_text[row]; - dtp2 = R->drawn_text[row + i]; - drp = R->drawn_rend[row]; - drp2 = R->drawn_rend[row + i]; - for (nits = 0, col = R->TermWin.ncol; col--; ) - if (stp[col] != dtp2[col] || srp[col] != drp2[col]) - nits--; - else if (stp[col] != dtp[col] || srp[col] != drp[col]) - nits++; - if (nits > 8) { /* XXX: arbitrary choice */ - for (col = R->TermWin.ncol; col--; ) { - *dtp++ = *dtp2++; - *drp++ = *drp2++; - } - if (len == -1) - len = row; - wlen = row; - continue; - } - } - if (len != -1) { - /* also comes here at end if needed because of >= above */ - if (wlen < len) - SWAP_IT(wlen, len, int); - D_SCREEN((stderr, "rxvt_scr_refresh(): XCopyArea: %d -> %d (height: %d)", len + i, len, wlen - len + 1)); - XCopyArea(R->Xdisplay, R->TermWin.vt, R->TermWin.vt, - R->TermWin.gc, 0, Row2Pixel(len + i), - (unsigned int)TermWin_TotalWidth(), - (unsigned int)Height2Pixel(wlen - len + 1), - 0, Row2Pixel(len)); - len = -1; - } - } + /* + * D: CopyArea pass - very useful for slower links + * This has been deliberately kept simple. + */ + if (!display->is_local + && refresh_type == FAST_REFRESH && num_scr_allow && num_scr + && abs (num_scr) < nrow && !have_bg) + { + int16_t nits; + int i = num_scr; + int j; + int len, wlen; + + j = nrow; + wlen = len = -1; + row = i > 0 ? 0 : j - 1; + + for (; j-- >= 0; row += (i > 0 ? 1 : -1)) + { + if (row + i >= 0 && row + i < nrow && row + i != ocrow) + { + line_t s = ROW(view_start + row); + line_t d = drawn_buf[row]; + line_t d2 = drawn_buf[row + i]; + + for (nits = 0, col = ncol; col--; ) + if (s.t[col] != d2.t[col] || s.r[col] != d2.r[col]) + nits--; + else if (s.t[col] != d.t[col] || s.r[col] != d.r[col]) + nits++; + + if (nits > 8) /* XXX: arbitrary choice */ + { + for (col = ncol; col--; ) + { + *d.t++ = *d2.t++; + *d.r++ = *d2.r++; + } + + if (len == -1) + len = row; + + wlen = row; + continue; + } + } + + if (len >= 0) + { + /* also comes here at end if needed because of >= above */ + if (wlen < len) + ::swap (wlen, len); + + XGCValues gcv; + + gcv.graphics_exposures = 1; XChangeGC (dpy, gc, GCGraphicsExposures, &gcv); + XCopyArea (dpy, vt, vt, + gc, 0, Row2Pixel (len + i), + (unsigned int)this->width, + (unsigned int)Height2Pixel (wlen - len + 1), + 0, Row2Pixel (len)); + gcv.graphics_exposures = 0; XChangeGC (dpy, gc, GCGraphicsExposures, &gcv); + + len = -1; + } + } } #endif -/* - * E: main pass across every character - */ - for (row = 0; row < R->TermWin.nrow; row++) - { - stp = R->screen.text[row + row_offset]; - srp = R->screen.rend[row + row_offset]; - dtp = R->drawn_text[row]; - drp = R->drawn_rend[row]; + /* + * E: main pass across every character + */ + for (row = 0; row < nrow; row++) + { + text_t *stp = ROW(view_start + row).t; + rend_t *srp = ROW(view_start + row).r; + text_t *dtp = drawn_buf[row].t; + rend_t *drp = drawn_buf[row].r; + + /* + * E2: OK, now the real pass + */ + int ypixel = (int)Row2Pixel (row); + + for (col = 0; col < ncol; col++) + { + /* compare new text with old - if exactly the same then continue */ + if (stp[col] == dtp[col] && RS_SAME (srp[col], drp[col])) + continue; + + // redraw one or more characters + + // seek to the beginning of wide characters + while (ecb_unlikely (stp[col] == NOCHAR && col > 0)) + --col; + + rend_t rend = srp[col]; /* screen rendition (target rendition) */ + text_t *text = stp + col; + int count = 1; -/* - * E2: OK, now the real pass - */ - int ypixel = (int)Row2Pixel(row); + dtp[col] = stp[col]; + drp[col] = rend; - for (col = 0; col < R->TermWin.ncol; col++) { - /* compare new text with old - if exactly the same then continue */ - rend_t rend = srp[col]; /* screen rendition (target rendtion) */ + int xpixel = Col2Pixel (col); - if (stp[col] == dtp[col] /* Must match characters to skip. */ - && (rend == drp[col] /* Either rendition the same or */ - || (stp[col] == ' ' /* space w/ no background change */ - && GET_BGATTR(rend) == GET_BGATTR(drp[col])))) - continue; + for (i = 0; ++col < ncol; ) + { + if (stp[col] == NOCHAR) + { + dtp[col] = stp[col]; + drp[col] = srp[col]; + + count++; + i++; + + continue; + } + + if (!RS_SAME (rend, srp[col])) + break; + + count++; + + if (stp[col] != dtp[col] + || !RS_SAME (srp[col], drp[col])) + { + if (have_bg && (i++ > count / 2)) + break; + + dtp[col] = stp[col]; + drp[col] = rend; + i = 0; + } + else if (have_bg || (stp[col] != ' ' && ++i >= 16)) + break; + } - text_t *text = stp + col; - int count = 1; + col--; /* went one too far. move back */ + count -= i; /* dump any matching trailing chars */ - /* redraw one or more characters */ + // sometimes we optimize away the trailing NOCHAR's, add them back + while (ecb_unlikely (i && text[count] == NOCHAR)) + count++, i--; + + /* + * Determine the attributes for the string + */ + int fore = fgcolor_of (rend); // desired foreground + int back = bgcolor_of (rend); // desired background - dtp[col] = stp[col]; - drp[col] = rend; + // only do special processing if any attributes are set, which is unlikely + if (ecb_unlikely (rend & (RS_baseattrMask | RS_Careful | RS_Sel))) + { + bool invert = rend & RS_RVid; - if (*text == NOCHAR) // never start redrawing at invisible characters. */ - continue; +#ifndef NO_BOLD_UNDERLINE_REVERSE + if (rend & RS_Bold && fore == Color_fg) + { + if (ISSET_PIXCOLOR (Color_BD)) + fore = Color_BD; +# if !ENABLE_STYLES + else + invert = !invert; +# endif + } - int xpixel = Col2Pixel(col); + if (rend & RS_Italic && fore == Color_fg) + { + if (ISSET_PIXCOLOR (Color_IT)) + fore = Color_IT; +# if !ENABLE_STYLES + else + invert = !invert; +# endif + } - // this loop looks very messy, it can probably be optimized - // and cleaned a bit by you? - for (i = 0; ++col < R->TermWin.ncol; ) - { - if (stp[col] == NOCHAR) - { - dtp[col] = stp[col]; - drp[col] = rend; - count++; + if (rend & RS_Uline && fore == Color_fg && ISSET_PIXCOLOR (Color_UL)) + fore = Color_UL; +#endif - if (i) // only possible skip if char unchanged - i++; +#ifdef OPTION_HC + if (rend & RS_Sel) + { + /* invert the column if no highlightColor is set or it is the + * current cursor column */ + if (!(showcursor && row == screen.cur.row && text - stp == screen.cur.col) + && ISSET_PIXCOLOR (Color_HC)) + { + if (ISSET_PIXCOLOR (Color_HTC)) + fore = Color_HTC; + // if invert is 0 reverse video is set so we use bg color as fg color + else if (!invert) + fore = back; + + back = Color_HC; + invert = 0; + } + } +#endif + + if (invert) + { + ::swap (fore, back); - continue; - } +#ifndef NO_BOLD_UNDERLINE_REVERSE + if (fore == back) + { + fore = Color_bg; + back = Color_fg; + } +#endif + } + +#ifdef TEXT_BLINK + if (rend & RS_Blink && (back == Color_bg || fore == Color_bg)) + { + if (!text_blink_ev.is_active ()) + { + text_blink_ev.again (); + hidden_text = 0; + } + else if (hidden_text) + fore = back; + } +#endif + +#if ENABLE_STYLES + // "careful" (too wide) character handling + + // include previous careful character(s) if possible, looks nicer (best effort...) + while (text > stp + && srp[text - stp - 1] & RS_Careful + && RS_SAME (rend, srp[text - stp - 1])) + text--, count++, xpixel -= fwidth; + + // force redraw after "careful" characters to avoid pixel droppings + for (int i = 0; srp[col + i] & RS_Careful && col + i < ncol - 1; i++) + drp[col + i + 1] = srp[col + i + 1] ^ RS_redraw; + + // force redraw before "careful" characters to avoid pixel droppings + for (int i = 0; srp[text - stp - i] & RS_Careful && text - i > stp; i++) + drp[text - stp - i - 1] = srp[text - stp - i - 1] ^ RS_redraw; +#endif + } - if (((rend ^ srp[col]) & ~RS_wide) != 0) - break; + /* + * Actually do the drawing of the string here + */ + rxvt_font *font = (*fontset[GET_STYLE (rend)])[GET_FONT (rend)]; - count++; + if (ecb_likely (have_bg && back == Color_bg)) + { + // this is very ugly, maybe push it into ->draw? - if (stp[col] != dtp[col] - || srp[col] != drp[col]) + for (i = 0; i < count; i++) /* don't draw empty strings */ + if (text[i] != ' ') { - if (must_clear && (i++ > (count / 2))) - break; - - dtp[col] = stp[col]; - drp[col] = rend; - i = 0; + font->draw (*drawable, xpixel, ypixel, text, count, fore, Color_transparent); + goto did_clear; } - else if (must_clear || (stp[col] != ' ' && ++i >= 32)) - break; - } - col--; /* went one too far. move back */ - count -= i; /* dump any matching trailing chars */ + CLEAR_CHARS (xpixel, ypixel, count); + did_clear: ; + } + else + font->draw (*drawable, xpixel, ypixel, text, count, fore, back); -/* - * Determine the attributes for the string - */ - int fid = GET_FONT(rend); - fore = GET_FGCOLOR(rend); - back = GET_BGCOLOR(rend); - rend = GET_ATTR(rend); + if (ecb_unlikely (rend & RS_Uline && font->descent > 1 && fore != back)) + { + if (showcursor && focus && row == screen.cur.row + && IN_RANGE_EXC (col, cur_col, cur_col + cursorwidth)) + XSetForeground (dpy, gc, pix_colors[ccol1]); + else +#if ENABLE_FRILLS + if (ISSET_PIXCOLOR (Color_underline)) + XSetForeground (dpy, gc, pix_colors[Color_underline]); + else +#endif + XSetForeground (dpy, gc, pix_colors[fore]); + + XDrawLine (dpy, vt, gc, + xpixel, ypixel + font->ascent + 1, + xpixel + Width2Pixel (count) - 1, ypixel + font->ascent + 1); + } + } /* for (col....) */ + } /* for (row....) */ - rvid = (rend & RS_RVid) ? 1 : 0; -#ifdef OPTION_HC - if (!rvid && (rend & RS_Blink)) - { - if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_HC)) - back = Color_HC; - else - rvid = !rvid; /* fall back */ - } -#endif - if (rvid) - { - SWAP_IT(fore, back, int); + /* + * G: cleanup cursor and display outline cursor if necessary + */ + if (showcursor) + { + if (focus) + { + if (cursor_type == 0) + scr_set_char_rend (ROW(screen.cur.row), cur_col, cur_rend); + else if (oldcursor.row >= 0) + { + XSetForeground (dpy, gc, pix_colors[ccol1]); + if (cursor_type == 1) + XFillRectangle (dpy, vt, gc, + Col2Pixel (cur_col), + Row2Pixel (oldcursor.row + 1) - 2, + Width2Pixel (1), + 2); + else + XFillRectangle (dpy, vt, gc, + Col2Pixel (cur_col), + Row2Pixel (oldcursor.row), + 2, + Height2Pixel (1)); + } + } + else if (oldcursor.row >= 0) + { + XSetForeground (dpy, gc, pix_colors[ccol1]); -#ifndef NO_BOLD_UNDERLINE_REVERSE - if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_RV) -# ifndef NO_CURSORCOLOR - && !ISSET_PIXCOLOR(h, Color_cursor) -# endif - ) - back = Color_RV; + XDrawRectangle (dpy, vt, gc, + Col2Pixel (cur_col), + Row2Pixel (oldcursor.row), + (unsigned int) (Width2Pixel (cursorwidth) - 1), + (unsigned int) (Height2Pixel (1) - 1)); + } + } + + /* + * H: cleanup selection + */ +#if ENABLE_OVERLAY + scr_swap_overlay (); #endif - } -#ifndef NO_BOLD_UNDERLINE_REVERSE - else if (rend & RS_Bold) - { - if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_BD)) - { - fore = Color_BD; -# ifndef VERYBOLD - rend &= ~RS_Bold; /* we've taken care of it */ + HOOK_INVOKE ((this, HOOK_REFRESH_END, DT_END)); + + scr_reverse_selection (); + + screen.flags = old_screen_flags; + num_scr = 0; + num_scr_allow = 1; +} + +void ecb_cold +rxvt_term::scr_remap_chars (line_t &l) noexcept +{ + if (!l.valid ()) + return; + + l.touch (); // maybe a bit of an overkill, but it's not performance-relevant + + for (int i = ncol; i--; ) + l.r[i] = SET_FONT (l.r[i], FONTSET (l.r[i])->find_font (l.t[i])); +} + +void ecb_cold +rxvt_term::scr_remap_chars () noexcept +{ + for (int i = total_rows; i--; ) + scr_remap_chars (row_buf [i]); + + for (int i = nrow; i--; ) + { + scr_remap_chars (drawn_buf [i]); + scr_remap_chars (swap_buf [i]); + } +} + +void ecb_cold +rxvt_term::scr_recolor (bool refresh) noexcept +{ + bool transparent = false; + +#ifdef HAVE_IMG + if (bg_img != 0) + { +# if ENABLE_TRANSPARENCY + if (bg_flags & BG_IS_TRANSPARENT) + { + XSetWindowBackgroundPixmap (dpy, parent, bg_img->pm); + XSetWindowBackgroundPixmap (dpy, vt, ParentRelative); + + transparent = true; + } + else # endif - } - } - else if (rend & RS_Uline) - { - if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_UL)) - { - fore = Color_UL; - rend &= ~RS_Uline; /* we've taken care of it */ - } - } + { + XSetWindowBackground (dpy, parent, pix_colors[Color_border]); + XSetWindowBackgroundPixmap (dpy, vt, bg_img->pm); + } + } + else #endif + { + XSetWindowBackground (dpy, parent, pix_colors[Color_border]); + XSetWindowBackground (dpy, vt, pix_colors[Color_bg]); + } -/* - * Actually do the drawing of the string here - */ - rxvt_font *font = (*R->TermWin.fontset)[fid]; + XClearWindow (dpy, parent); - if (back == Color_bg) - { - if (must_clear) - { - for (i = 0; i < count; i++) /* don't draw empty strings */ - if (text[i] != ' ') { - font->draw (xpixel, ypixel, text, count, fore, -1); - goto nodraw; - } - CLEAR_CHARS(xpixel, ypixel, count); -nodraw: ; - } - else - font->draw (xpixel, ypixel, text, count, fore, Color_bg); - } else - font->draw (xpixel, ypixel, text, count, fore, back); - - if ((rend & RS_Uline) && (font->descent > 1)) - XDrawLine(R->Xdisplay, drawBuffer, R->TermWin.gc, - xpixel, ypixel + font->ascent + 1, - xpixel + Width2Pixel(count) - 1, ypixel + font->ascent + 1); - } /* for (col....) */ - } /* for (row....) */ - -/* - * G: cleanup cursor and display outline cursor if necessary - */ - if (showcursor) { - if (R->TermWin.focus) { - srp = &(R->screen.rend[R->screen.cur.row + R->TermWin.saveLines] - [R->screen.cur.col]); - *srp ^= RS_RVid; -#ifndef NO_CURSORCOLOR - *srp = (*srp & ~(RS_fgMask | RS_bgMask)) | cc1; -#endif - } else if (h->oldcursor.row >= 0) { -#ifndef NO_CURSORCOLOR - unsigned long gcmask; /* Graphics Context mask */ + if (scrollBar.state && scrollBar.win) + { + if (transparent) + XSetWindowBackgroundPixmap (dpy, scrollBar.win, ParentRelative); + else + XSetWindowBackground (dpy, scrollBar.win, pix_colors[scrollBar.color ()]); + scrollBar.state = SB_STATE_IDLE; + scrollBar.show (0); + } - if (XDEPTH > 2 && ISSET_PIXCOLOR(h, Color_cursor)) { - XSetForeground (R->Xdisplay, R->TermWin.gc, R->PixColors[Color_cursor]); - } -#endif - XDrawRectangle(R->Xdisplay, drawBuffer, R->TermWin.gc, - Col2Pixel(h->oldcursor.col), - Row2Pixel(h->oldcursor.row), - (unsigned int)(Width2Pixel(cursorwidth) - 1), - (unsigned int)(Height2Pixel(1) - R->TermWin.lineSpace - 1)); - } - } -/* - * H: cleanup selection - */ - rxvt_scr_reverse_selection(aR); - -/* - * I: other general cleanup - */ - if (clearfirst && R->TermWin.int_bwidth) - /* - * clear the whole screen height, note that width == 0 is treated - * specially by XClearArea - */ - XClearArea(R->Xdisplay, R->TermWin.vt, 0, 0, - (unsigned int)R->TermWin.int_bwidth, - (unsigned int)TermWin_TotalHeight(), False); - if (clearlast && R->TermWin.int_bwidth) - /* - * clear the whole screen height, note that width == 0 is treated - * specially by XClearArea - */ - XClearArea(R->Xdisplay, R->TermWin.vt, - R->TermWin.width + R->TermWin.int_bwidth, 0, - (unsigned int)R->TermWin.int_bwidth, - (unsigned int)TermWin_TotalHeight(), False); - if (refresh_type & SMOOTH_REFRESH) - XSync(R->Xdisplay, False); - - h->num_scr = 0; - h->num_scr_allow = 1; - h->want_refresh = 0; /* screen is current */ -} -/* ------------------------------------------------------------------------- */ - -/* EXTPROTO */ -void -rxvt_scr_clear(pR) -{ - if (!R->TermWin.mapped) - return; - R->h->num_scr_allow = 0; - R->h->want_refresh = 1; -#ifdef TRANSPARENT - if ((R->Options & Opt_transparent) && (R->h->am_pixmap_trans == 0)) { - int i; - - if (!(R->Options & Opt_transparent_all)) - i = 0; - else - i = (int)(sizeof(R->TermWin.parent) / sizeof(Window)); - for (; i--;) - if (R->TermWin.parent[i] != None) - XClearWindow(R->Xdisplay, R->TermWin.parent[i]); - } -#endif - XClearWindow(R->Xdisplay, R->TermWin.vt); -} - -/* ------------------------------------------------------------------------- */ -/* INTPROTO */ -void -rxvt_scr_reverse_selection(pR) -{ - int i, col, row, end_row; - rend_t *srp; - - if (R->selection.op && R->h->current_screen == R->selection.screen) { - end_row = R->TermWin.saveLines - R->TermWin.view_start; - i = R->selection.beg.row + R->TermWin.saveLines; - row = R->selection.end.row + R->TermWin.saveLines; - if (i >= end_row) - col = R->selection.beg.col; - else { - col = 0; - i = end_row; - } - end_row += R->TermWin.nrow; - for (; i < row && i < end_row; i++, col = 0) - for (srp = R->screen.rend[i]; col < R->TermWin.ncol; col++) -#ifndef OPTION_HC - srp[col] ^= RS_RVid; -#else - srp[col] ^= RS_Blink; -#endif - if (i == row && i < end_row) - for (srp = R->screen.rend[i]; col < R->selection.end.col; col++) -#ifndef OPTION_HC - srp[col] ^= RS_RVid; -#else - srp[col] ^= RS_Blink; -#endif + if (refresh) + { + scr_clear (); + scr_touch (true); + } + want_refresh = 1; +} + +/* ------------------------------------------------------------------------- */ +void +rxvt_term::scr_clear (bool really) noexcept +{ + if (!mapped) + return; + + num_scr_allow = 0; + want_refresh = 1; + + if (really) + XClearWindow (dpy, vt); +} + +void +rxvt_term::scr_xor_rect (int beg_row, int beg_col, int end_row, int end_col, rend_t rstyle1, rend_t rstyle2) noexcept +{ + int view_end = view_start + nrow; + int row, col; + + for (row = max (beg_row, view_start); row <= min (end_row, view_end); row++) + { + text_t *stp = ROW(row).t; + rend_t *srp = ROW(row).r; + + for (col = beg_col; col < end_col; col++) + srp[col] ^= rstyle1; + + while (col-- > beg_col && (stp[col] == NOCHAR || unicode::is_space (stp[col]))) + srp[col] ^= rstyle2; + + if (++col < end_col) + srp[col] ^= rstyle2; + } +} + +void +rxvt_term::scr_xor_span (int beg_row, int beg_col, int end_row, int end_col, rend_t rstyle) noexcept +{ + int view_end = view_start + nrow; + int row, col; + + if (beg_row >= view_start) + { + col = beg_col; + row = beg_row; + } + else + { + col = 0; + row = view_start; + } + + for (; row < min (end_row, view_end); row++, col = 0) + for (rend_t *srp = ROW(row).r; col < ncol; col++) + srp[col] ^= rstyle; + + if (row == end_row) + for (rend_t *srp = ROW(row).r; col < end_col; col++) + srp[col] ^= rstyle; +} + +/* ------------------------------------------------------------------------- */ +void ecb_hot +rxvt_term::scr_reverse_selection () noexcept +{ + if (selection.op + && current_screen == selection.screen + && selection.end.row >= view_start) + { +#if !ENABLE_MINIMAL + if (selection.rect) + scr_xor_rect (selection.beg.row, selection.beg.col, + selection.end.row, selection.end.col, + RS_Sel | RS_RVid, RS_Sel | RS_RVid | RS_Uline); + else +#endif + scr_xor_span (selection.beg.row, selection.beg.col, + selection.end.row, selection.end.col, + RS_Sel | RS_RVid); } } @@ -2333,290 +2663,92 @@ * invoking routine must close the fd. */ #if 0 -/* EXTPROTO */ void -rxvt_scr_dump(pR_ int fd) +rxvt_term::scr_dump (int fd) noexcept { - int row, wrote; - unsigned int width, towrite; - char r1[] = "\n"; - - for (row = R->TermWin.saveLines - R->TermWin.nscrolled; - row < R->TermWin.saveLines + R->TermWin.nrow - 1; row++) { - width = R->screen.tlen[row] >= 0 ? R->screen.tlen[row] - : R->TermWin.ncol; - for (towrite = width; towrite; towrite -= wrote) { - wrote = write(fd, &(R->screen.text[row][width - towrite]), - towrite); - if (wrote < 0) - return; /* XXX: death, no report */ - } - if (R->screen.tlen[row] >= 0) - if (write(fd, r1, 1) <= 0) - return; /* XXX: death, no report */ - } + // if this method is needed, it can be implemented by factoring the + // relevant code in scr_printscreen } #endif /* ------------------------------------------------------------------------- * * CHARACTER SELECTION * * ------------------------------------------------------------------------- */ +void +rxvt_term::selection_check (int check_more) noexcept +{ + if (!selection.op) + return; + + if (!IN_RANGE_EXC (selection.beg.row, top_row, nrow) + || !IN_RANGE_EXC (selection.mark.row, top_row, nrow) + || !IN_RANGE_EXC (selection.end.row, top_row, nrow) + || (check_more == 1 + && current_screen == selection.screen + && !ROWCOL_IS_BEFORE (screen.cur, selection.beg) + && ROWCOL_IS_BEFORE (screen.cur, selection.end))) + CLEAR_ALL_SELECTION (); +} -/* - * -R->TermWin.nscrolled <= (selection row) <= R->TermWin.nrow - 1 - */ -/* EXTPROTO */ void -rxvt_selection_check(pR_ int check_more) +rxvt_term::selection_changed () noexcept { - row_col_t pos; + line_t &r1 = ROW (selection.beg.row); + while (selection.beg.col < r1.l && r1.t [selection.beg.col] == NOCHAR) + ++selection.beg.col; - if (!R->selection.op) - return; - - pos.row = pos.col = 0; - if ((R->selection.beg.row < -(int32_t)R->TermWin.nscrolled) - || (R->selection.beg.row >= R->TermWin.nrow) - || (R->selection.mark.row < -(int32_t)R->TermWin.nscrolled) - || (R->selection.mark.row >= R->TermWin.nrow) - || (R->selection.end.row < -(int32_t)R->TermWin.nscrolled) - || (R->selection.end.row >= R->TermWin.nrow) - || (check_more == 1 - && R->h->current_screen == R->selection.screen - && !ROWCOL_IS_BEFORE(R->screen.cur, R->selection.beg) - && ROWCOL_IS_BEFORE(R->screen.cur, R->selection.end)) - || (check_more == 2 - && ROWCOL_IS_BEFORE(R->selection.beg, pos) - && ROWCOL_IS_AFTER(R->selection.end, pos)) - || (check_more == 3 - && ROWCOL_IS_AFTER(R->selection.end, pos)) - || (check_more == 4 /* screen width change */ - && (R->selection.beg.row != R->selection.end.row - || R->selection.end.col > R->TermWin.ncol))) - CLEAR_SELECTION(R); + line_t &r2 = ROW (selection.end.row); + while (selection.end.col < r2.l && r2.t [selection.end.col] == NOCHAR) + ++selection.end.col; + + want_refresh = 1; } /* ------------------------------------------------------------------------- */ /* * Paste a selection direct to the command fd */ -/* INTPROTO */ void -rxvt_PasteIt(pR_ const unsigned char *data, unsigned int nitems) +rxvt_term::tt_paste (char *data, unsigned int len) noexcept { - unsigned int i, j, n; - unsigned char *ds = (unsigned char *)rxvt_malloc(PROP_SIZE); - -/* convert normal newline chars into common keyboard Return key sequence */ - for (i = 0; i < nitems; i += PROP_SIZE) { - n = min(nitems - i, PROP_SIZE); - MEMCPY(ds, data + i, n); - for (j = 0; j < n; j++) - if (ds[j] == '\n') - ds[j] = '\r'; - rxvt_tt_write(aR_ ds, (int)n); - } - free(ds); -} - -/* ------------------------------------------------------------------------- */ -/* - * Respond to a notification that a primary selection has been sent - * EXT: SelectionNotify - */ -/* EXTPROTO */ -int -rxvt_selection_paste(pR_ Window win, Atom prop, Bool delete_prop) -{ - long nread = 0; - unsigned long bytes_after; - XTextProperty ct; -#ifdef MULTICHAR_SET - int dummy_count; - char **cl; -#endif - - D_SELECT((stderr, "rxvt_selection_paste(%08lx, %lu, %d), wait=%2x", win, (unsigned long)prop, (int)delete_prop, R->h->selection_wait)); - - if (prop == None) { /* check for failed XConvertSelection */ -#ifdef MULTICHAR_SET - if ((R->h->selection_type & Sel_CompoundText)) { - int selnum = R->h->selection_type & Sel_whereMask; - - R->h->selection_type = 0; - if (selnum != Sel_direct) - rxvt_selection_request_other(aR_ XA_STRING, selnum); - } -#endif - return 0; - } - for (;;) { - if (XGetWindowProperty(R->Xdisplay, win, prop, (long)(nread / 4), - (long)(PROP_SIZE / 4), delete_prop, - AnyPropertyType, &ct.encoding, &ct.format, - &ct.nitems, &bytes_after, - &ct.value) != Success) - break; - if (ct.encoding == 0) { - D_SELECT((stderr, "rxvt_selection_paste: property didn't exist!")); - break; - } - if (ct.value == NULL) { - D_SELECT((stderr, "rxvt_selection_paste: property shooting blanks!")); - continue; - } - if (ct.nitems == 0) { - D_SELECT((stderr, "rxvt_selection_paste: property empty - also INCR end")); - if (R->h->selection_wait == Sel_normal && nread == 0) { - /* - * pass through again trying CUT_BUFFER0 if we've come from - * XConvertSelection() but nothing was presented - */ - D_SELECT((stderr, "rxvt_selection_request: pasting CUT_BUFFER0")); - rxvt_selection_paste(aR_ Xroot, XA_CUT_BUFFER0, False); - } - nread = -1; /* discount any previous stuff */ - break; - } - nread += ct.nitems; -#ifdef MULTICHAR_SET - if (XmbTextPropertyToTextList(R->Xdisplay, &ct, &cl, - &dummy_count) == Success && cl) { - rxvt_PasteIt(aR_ cl[0], STRLEN(cl[0])); - XFreeStringList(cl); - } else -#endif - rxvt_PasteIt(aR_ ct.value, (unsigned int)ct.nitems); - if (bytes_after == 0) - break; - XFree(ct.value); - } - if (ct.value) - XFree(ct.value); - if (R->h->selection_wait == Sel_normal) - R->h->selection_wait = Sel_none; - D_SELECT((stderr, "rxvt_selection_paste: bytes written: %ld", nread)); - return (int)nread; -} - -/* - * INCR support originally provided by Paul Sheer - */ -/* EXTPROTO */ -void -rxvt_selection_property(pR_ Window win, Atom prop) -{ - int reget_time = 0; - - if (prop == None) - return; - D_SELECT((stderr, "rxvt_selection_property(%08lx, %lu)", win, (unsigned long)prop)); - if (R->h->selection_wait == Sel_normal) { - int a, afmt; - Atom atype; - unsigned long bytes_after, nitems; - unsigned char *s = NULL; - - a = XGetWindowProperty(R->Xdisplay, win, prop, 0L, 1L, False, - R->h->xa[XA_INCR], &atype, &afmt, &nitems, - &bytes_after, &s); - if (s) - XFree(s); - if (a != Success) - return; -#ifndef __CYGWIN32__ - if (atype == R->h->xa[XA_INCR]) { /* start an INCR transfer */ - D_SELECT((stderr, "rxvt_selection_property: INCR: starting transfer")); - XDeleteProperty(R->Xdisplay, win, prop); - XFlush(R->Xdisplay); - reget_time = 1; - R->h->selection_wait = Sel_incr; - } -#endif - } else if (R->h->selection_wait == Sel_incr) { - reget_time = 1; - if (rxvt_selection_paste(aR_ win, prop, True) == -1) { - D_SELECT((stderr, "rxvt_selection_property: INCR: clean end")); - R->h->selection_wait = Sel_none; - R->h->timeout[TIMEOUT_INCR].tv_sec = 0; /* turn off timer */ - } - } - if (reget_time) { /* received more data so reget time */ - (void)gettimeofday(&(R->h->timeout[TIMEOUT_INCR]), NULL); - R->h->timeout[TIMEOUT_INCR].tv_sec += 10; /* ten seconds wait */ - } -} -/* ------------------------------------------------------------------------- */ -/* - * Request the current selection: - * Order: > internal selection if available - * > PRIMARY, SECONDARY, CLIPBOARD if ownership is claimed (+) - * > CUT_BUFFER0 - * (+) if ownership is claimed but property is empty, rxvt_selection_paste() - * will auto fallback to CUT_BUFFER0 - * EXT: button 2 release - */ -/* EXTPROTO */ + /* convert normal newline chars into common keyboard Return key sequence */ + for (unsigned int i = 0; i < len; i++) + if (data[i] == C0_LF) + data[i] = C0_CR; + + if (priv_modes & PrivMode_BracketPaste) + tt_printf ("\x1b[200~"); + + tt_write (data, len); + + if (priv_modes & PrivMode_BracketPaste) + tt_printf ("\x1b[201~"); +} + void -rxvt_selection_request(pR_ Time tm, int x, int y) +rxvt_term::paste (char *data, unsigned int len) noexcept { - D_SELECT((stderr, "rxvt_selection_request(%lu, %d, %d)", tm, x, y)); - if (x < 0 || x >= R->TermWin.width || y < 0 || y >= R->TermWin.height) - return; /* outside window */ - - if (R->selection.text != NULL) { /* internal selection */ - D_SELECT((stderr, "rxvt_selection_request: pasting internal")); - rxvt_PasteIt(aR_ R->selection.text, R->selection.len); - return; - } else { - int i; - - R->h->selection_request_time = tm; - R->h->selection_wait = Sel_normal; - for (i = Sel_Primary; i <= Sel_Clipboard; i++) { -#ifdef MULTICHAR_SET - R->h->selection_type = Sel_CompoundText; -#else - R->h->selection_type = 0; -#endif - if (rxvt_selection_request_other(aR_ -#ifdef MULTICHAR_SET - R->h->xa[XA_COMPOUND_TEXT], -#else - XA_STRING, -#endif - i)) - return; - } - } - R->h->selection_wait = Sel_none; /* don't loop in rxvt_selection_paste() */ - D_SELECT((stderr, "rxvt_selection_request: pasting CUT_BUFFER0")); - rxvt_selection_paste(aR_ Xroot, XA_CUT_BUFFER0, False); + if (HOOK_INVOKE ((this, HOOK_TT_PASTE, DT_STR_LEN, data, len, DT_END))) + return; + + tt_paste (data, len); } -/* INTPROTO */ -int -rxvt_selection_request_other(pR_ Atom target, int selnum) +/* ------------------------------------------------------------------------- */ +/* + * Request PRIMARY, SECONDARY or CLIPBOARD selection. + * if the requested selection has no owner or is empty CUT_BUFFER0 is used + * as fallback + * EXT: button 2 release + */ +void +rxvt_term::selection_request (Time tm, int selnum) noexcept { - Atom sel; -#ifdef DEBUG_SELECT - char *debug_xa_names[] = { "PRIMARY", "SECONDARY", "CLIPBOARD" }; -#endif - - R->h->selection_type |= selnum; - if (selnum == Sel_Primary) - sel = XA_PRIMARY; - else if (selnum == Sel_Secondary) - sel = XA_SECONDARY; - else - sel = R->h->xa[XA_CLIPBOARD]; - if (XGetSelectionOwner(R->Xdisplay, sel) != None) { - D_SELECT((stderr, "rxvt_selection_request_other: pasting %s", debug_xa_names[selnum])); - XConvertSelection(R->Xdisplay, sel, target, R->h->xa[XA_VT_SELECTION], - R->TermWin.vt, R->h->selection_request_time); - return 1; + if (!selection_req) + { + selection_req = new rxvt_selection (display, selnum, tm, vt, xa[XA_VT_SELECTION], this); + selection_req->run (); } - return 0; } /* ------------------------------------------------------------------------- */ @@ -2624,18 +2756,29 @@ * Clear all selected text * EXT: SelectionClear */ -/* EXTPROTO */ void -rxvt_selection_clear(pR) +rxvt_term::selection_clear (bool clipboard) noexcept { - D_SELECT((stderr, "rxvt_selection_clear()")); + if (!clipboard) + { + want_refresh = 1; + free (selection.text); + selection.text = NULL; + selection.len = 0; + CLEAR_SELECTION (); + + if (display->selection_owner == this) + display->selection_owner = 0; + } + else + { + free (selection.clip_text); + selection.clip_text = NULL; + selection.clip_len = 0; - R->h->want_refresh = 1; - if (R->selection.text) - free(R->selection.text); - R->selection.text = NULL; - R->selection.len = 0; - CLEAR_SELECTION(R); + if (display->clipboard_owner == this) + display->clipboard_owner = 0; + } } /* ------------------------------------------------------------------------- */ @@ -2643,108 +2786,165 @@ * Copy a selection into the cut buffer * EXT: button 1 or 3 release */ -/* EXTPROTO */ void -rxvt_selection_make(pR_ Time tm) +rxvt_term::selection_make (Time tm) { - int i, col, end_col, row, end_row; - unsigned char *new_selection_text; - char *str; - text_t *t; -#ifdef ACS_ASCII - rend_t *re; -#endif - - D_SELECT((stderr, "rxvt_selection_make(): R->selection.op=%d, R->selection.clicks=%d", R->selection.op, R->selection.clicks)); - switch (R->selection.op) { - case SELECTION_CONT: - break; - case SELECTION_INIT: - CLEAR_SELECTION(R); - /* FALLTHROUGH */ - case SELECTION_BEGIN: - R->selection.op = SELECTION_DONE; - /* FALLTHROUGH */ - default: - return; - } - R->selection.op = SELECTION_DONE; - - if (R->selection.clicks == 4) - return; /* nothing selected, go away */ - - i = (R->selection.end.row - R->selection.beg.row + 1) * (R->TermWin.ncol + 1) + 1; - str = (char *)rxvt_malloc(i * MB_CUR_MAX + 1); - - new_selection_text = (unsigned char *)str; - - col = R->selection.beg.col; - MAX_IT(col, 0); - row = R->selection.beg.row + R->TermWin.saveLines; - end_row = R->selection.end.row + R->TermWin.saveLines; + int size; + wchar_t *new_selection_text; + text_t *t; - for (; row <= end_row; row++, col = 0) - { - t = &(R->screen.text[row][col]); + switch (selection.op) + { + case SELECTION_CONT: + break; + case SELECTION_INIT: + CLEAR_SELECTION (); + /* FALLTHROUGH */ + case SELECTION_BEGIN: + selection.op = SELECTION_DONE; + /* FALLTHROUGH */ + default: + return; + } + + selection.op = SELECTION_DONE; - end_col = R->screen.tlen[row]; + if (selection.clicks == 4) + return; /* nothing selected, go away */ - if (end_col == -1) - end_col = R->TermWin.ncol; + if (HOOK_INVOKE ((this, HOOK_SEL_MAKE, DT_LONG, (long)tm, DT_END))) + return; - if (row == end_row) - MIN_IT (end_col, R->selection.end.col); + size = (selection.end.row - selection.beg.row + 1) * (ncol + 1); + new_selection_text = (wchar_t *)rxvt_malloc ((size + 4) * sizeof (wchar_t)); + + int ofs = 0; + int extra = 0; + + int col = selection.beg.col; + int row = selection.beg.row; + + int end_col; + + for (; row <= selection.end.row; row++, col = 0) + { +#if !ENABLE_MINIMAL + if (selection.rect) + { + col = selection.beg.col; + end_col = selection.end.col; + } + else +#endif + end_col = ROW(row).l; - for (; col < end_col; col++) + col = max (col, 0); + + if (row == selection.end.row) + min_it (end_col, selection.end.col); + + t = ROW(row).t + col; + + for (; col < end_col; col++) + { if (*t == NOCHAR) t++; - else +#if ENABLE_COMBINING + else if (IS_COMPOSE (*t)) { - int len = wctomb (str, *t++); - if (len > 0) - str += len; - } + int len = rxvt_composite.expand (*t); - if (R->screen.tlen[row] != -1 && row != end_row) - *str++ = '\n'; - } + extra -= (len - 1); + + if (extra < 0) + { + extra += size; + size += size; + new_selection_text = (wchar_t *)rxvt_realloc (new_selection_text, (size + 4) * sizeof (wchar_t)); + } -#ifndef NO_OLD_SELECTION - if (R->selection_style == OLD_SELECT) - if (end_col == R->TermWin.ncol) - *str++ = '\n'; -#endif -#ifndef NO_NEW_SELECTION - if (R->selection_style != OLD_SELECT) - if (end_col != R->selection.end.col) - *str++ = '\n'; + ofs += rxvt_composite.expand (*t++, new_selection_text + ofs); + } #endif - *str = '\0'; + else + new_selection_text[ofs++] = *t++; + } - i = str - (char *)new_selection_text; - if (i == 0) - { - free (new_selection_text); - return; - } +#if !ENABLE_MINIMAL + if (selection.rect) + { + while (ofs + && new_selection_text[ofs - 1] != C0_LF + && unicode::is_space (new_selection_text[ofs - 1])) + --ofs; + + new_selection_text[ofs++] = C0_LF; + } + else +#endif + if (!ROW(row).is_longer () + && (row != selection.end.row || end_col != selection.end.col) + && (row != selection.beg.row || selection.beg.col < ncol)) + new_selection_text[ofs++] = C0_LF; + } + + new_selection_text[ofs] = 0; + + if (ofs == 0) + { + free (new_selection_text); + return; + } + + free (selection.text); + + // we usually allocate much more than necessary, so realloc it smaller again + selection.len = ofs; + selection.text = (wchar_t *)rxvt_realloc (new_selection_text, (ofs + 1) * sizeof (wchar_t)); + + if (HOOK_INVOKE ((this, HOOK_SEL_GRAB, DT_LONG, (long)tm, DT_END))) + return; + + selection_grab (tm); +} + +bool +rxvt_term::selection_grab (Time tm, bool clipboard) noexcept +{ + Atom sel; + + if (!clipboard) + { + selection_time = tm; + sel = XA_PRIMARY; + } + else + { + clipboard_time = tm; + sel = xa[XA_CLIPBOARD]; + } + + XSetSelectionOwner (dpy, sel, vt, tm); + if (XGetSelectionOwner (dpy, sel) == vt) + { + display->set_selection_owner (this, clipboard); + return true; + } + else + { + selection_clear (clipboard); + return false; + } + +#if 0 + XTextProperty ct; - // due to MB_MAX_CUR, selection wastage is usually high - if (str - (char *)new_selection_text > 1024) - new_selection_text = (unsigned char *)rxvt_realloc (new_selection_text, i + 1); - - R->selection.len = i; - if (R->selection.text) - free (R->selection.text); - - R->selection.text = new_selection_text; - - XSetSelectionOwner(R->Xdisplay, XA_PRIMARY, R->TermWin.vt, tm); - if (XGetSelectionOwner(R->Xdisplay, XA_PRIMARY) != R->TermWin.vt) - rxvt_print_error("can't get primary selection"); - XChangeProperty(R->Xdisplay, Xroot, XA_CUT_BUFFER0, XA_STRING, 8, - PropModeReplace, R->selection.text, (int)R->selection.len); - R->h->selection_time = tm; - D_SELECT((stderr, "rxvt_selection_make(): R->selection.len=%d", R->selection.len)); + if (XwcTextListToTextProperty (dpy, &selection.text, 1, XStringStyle, &ct) >= 0) + { + set_string_property (XA_CUT_BUFFER0, ct.value, ct.nitems); + XFree (ct.value); + } +#endif } /* ------------------------------------------------------------------------- */ @@ -2752,47 +2952,59 @@ * Mark or select text based upon number of clicks: 1, 2, or 3 * EXT: button 1 press */ -/* EXTPROTO */ -void -rxvt_selection_click(pR_ int clicks, int x, int y) +void ecb_cold +rxvt_term::selection_click (int clicks, int x, int y) noexcept { - D_SELECT((stderr, "rxvt_selection_click(%d, %d, %d)", clicks, x, y)); + clicks = ((clicks - 1) % 3) + 1; + selection.clicks = clicks; /* save clicks so extend will work */ - clicks = ((clicks - 1) % 3) + 1; - R->selection.clicks = clicks; /* save clicks so extend will work */ + if (clicks == 2 && !selection.rect + && HOOK_INVOKE ((this, HOOK_SEL_EXTEND, DT_END))) + { + MEvent.clicks = 1; // what a mess + selection.screen = current_screen; + selection.op = SELECTION_CONT; + return; + } - rxvt_selection_start_colrow(aR_ Pixel2Col(x), Pixel2Row(y)); - if (clicks == 2 || clicks == 3) - rxvt_selection_extend_colrow(aR_ R->selection.mark.col, - R->selection.mark.row - + R->TermWin.view_start, - 0, /* button 3 */ - 1, /* button press */ - 0); /* click change */ + selection_start_colrow (Pixel2Col (x), Pixel2Row (y)); + + if (clicks == 2 || clicks == 3) + selection_extend_colrow (selection.mark.col, + selection.mark.row - view_start, + 0, /* button 3 */ + 1, /* button press */ + 0); /* click change */ } /* ------------------------------------------------------------------------- */ /* * Mark a selection at the specified col/row */ -/* INTPROTO */ -void -rxvt_selection_start_colrow(pR_ int col, int row) +void ecb_cold +rxvt_term::selection_start_colrow (int col, int row) noexcept { - R->h->want_refresh = 1; - R->selection.mark.col = col; - R->selection.mark.row = row - R->TermWin.view_start; - MAX_IT(R->selection.mark.row, -(int32_t)R->TermWin.nscrolled); - MIN_IT(R->selection.mark.row, (int32_t)R->TermWin.nrow - 1); - MAX_IT(R->selection.mark.col, 0); - MIN_IT(R->selection.mark.col, (int32_t)R->TermWin.ncol - 1); - - if (R->selection.op) { /* clear the old selection */ - R->selection.beg.row = R->selection.end.row = R->selection.mark.row; - R->selection.beg.col = R->selection.end.col = R->selection.mark.col; + want_refresh = 1; + + selection.mark.row = row + view_start; + selection.mark.col = col; + + selection.mark.row = clamp (selection.mark.row, top_row, nrow - 1); + selection.mark.col = clamp (selection.mark.col, 0, ncol - 1); + + while (selection.mark.col > 0 + && ROW(selection.mark.row).t[selection.mark.col] == NOCHAR) + --selection.mark.col; + + if (selection.op) + { + /* clear the old selection */ + selection.beg.row = selection.end.row = selection.mark.row; + selection.beg.col = selection.end.col = selection.mark.col; } - R->selection.op = SELECTION_INIT; - R->selection.screen = R->h->current_screen; + + selection.op = SELECTION_INIT; + selection.screen = current_screen; } /* ------------------------------------------------------------------------- */ @@ -2802,86 +3014,84 @@ */ /* what do we want: spaces/tabs are delimiters or cutchars or non-cutchars */ -#define DELIMIT_TEXT(x) \ - (((x) == ' ' || (x) == '\t') ? 2 : (STRCHR(R->h->rs[Rs_cutchars], (x)) != NULL)) -#ifdef MULTICHAR_SET -# define DELIMIT_REND(x) (((x) & RS_multiMask) ? 1 : 0) -#else -# define DELIMIT_REND(x) 1 -#endif +#define DELIMIT_TEXT(x) \ + (unicode::is_space (x) ? 2 : (x) <= 0xff && !!strchr (rs[Rs_cutchars], (x))) +#define DELIMIT_REND(x) 1 + +void ecb_cold +rxvt_term::selection_delimit_word (enum page_dirn dirn, const row_col_t *mark, row_col_t *ret) noexcept +{ + int col, row, dirnadd, tcol, trow, w1, w2; + row_col_t bound; + text_t *stp; + rend_t *srp; -/* INTPROTO */ -void -rxvt_selection_delimit_word(pR_ enum page_dirn dirn, const row_col_t *mark, row_col_t *ret) -{ - int col, row, dirnadd, tcol, trow, w1, w2; - row_col_t bound; - text_t *stp; - rend_t *srp; - - if (dirn == UP) { - bound.row = R->TermWin.saveLines - R->TermWin.nscrolled - 1; - bound.col = 0; - dirnadd = -1; - } else { - bound.row = R->TermWin.saveLines + R->TermWin.nrow; - bound.col = R->TermWin.ncol - 1; - dirnadd = 1; - } - row = mark->row + R->TermWin.saveLines; - col = mark->col; - MAX_IT(col, 0); -/* find the edge of a word */ - stp = &(R->screen.text[row][col]); - w1 = DELIMIT_TEXT(*stp); - - if (R->selection_style != NEW_SELECT) { - if (w1 == 1) { - stp += dirnadd; - if (DELIMIT_TEXT(*stp) == 1) - goto Old_Word_Selection_You_Die; - col += dirnadd; - } - w1 = 0; - } - srp = (&R->screen.rend[row][col]); - w2 = DELIMIT_REND(*srp); - - for (;;) { - for (; col != bound.col; col += dirnadd) { - stp += dirnadd; - if (DELIMIT_TEXT(*stp) != w1) - break; - srp += dirnadd; - if (DELIMIT_REND(*srp) != w2) - break; - } - if ((col == bound.col) && (row != bound.row)) { - if (R->screen.tlen[(row - (dirn == UP ? 1 : 0))] == -1) { - trow = row + dirnadd; - tcol = dirn == UP ? R->TermWin.ncol - 1 : 0; - if (R->screen.text[trow] == NULL) - break; - stp = &(R->screen.text[trow][tcol]); - srp = &(R->screen.rend[trow][tcol]); - if (DELIMIT_TEXT(*stp) != w1 || DELIMIT_REND(*srp) != w2) - break; - row = trow; - col = tcol; - continue; - } - } - break; - } - Old_Word_Selection_You_Die: - D_SELECT((stderr, "rxvt_selection_delimit_word(%s,...) @ (r:%3d, c:%3d) has boundary (r:%3d, c:%3d)", (dirn == UP ? "up " : "down"), mark->row, mark->col, row - R->TermWin.saveLines, col)); - - if (dirn == DN) - col++; /* put us on one past the end */ - -/* Poke the values back in */ - ret->row = row - R->TermWin.saveLines; - ret->col = col; + if (dirn == UP) + { + bound.row = top_row - 1; + bound.col = 0; + dirnadd = -1; + } + else + { + bound.row = nrow; + bound.col = ncol - 1; + dirnadd = 1; + } + + row = mark->row; + col = max (mark->col, 0); + + /* find the edge of a word */ + stp = ROW(row).t + col; w1 = DELIMIT_TEXT (*stp); + srp = ROW(row).r + col; w2 = DELIMIT_REND (*srp); + + for (;;) + { + for (; col != bound.col; col += dirnadd) + { + stp += dirnadd; + srp += dirnadd; + + if (*stp == NOCHAR) + continue; + + if (DELIMIT_TEXT (*stp) != w1) + break; + if (DELIMIT_REND (*srp) != w2) + break; + } + + if ((col == bound.col) && (row != bound.row)) + { + if (ROW(row - (dirn == UP ? 1 : 0)).is_longer ()) + { + trow = row + dirnadd; + tcol = dirn == UP ? ncol - 1 : 0; + + if (!ROW(trow).t) + break; + + stp = ROW(trow).t + tcol; + srp = ROW(trow).r + tcol; + + if (DELIMIT_TEXT (*stp) != w1 || DELIMIT_REND (*srp) != w2) + break; + + row = trow; + col = tcol; + continue; + } + } + break; + } + + if (dirn == DN) + col++; /* put us on one past the end */ + + /* Poke the values back in */ + ret->row = row; + ret->col = col; } /* ------------------------------------------------------------------------- */ @@ -2892,299 +3102,283 @@ * flag == 1 ==> button 3 press * flag == 2 ==> button 3 motion */ -/* EXTPROTO */ -void -rxvt_selection_extend(pR_ int x, int y, int flag) +void ecb_cold +rxvt_term::selection_extend (int x, int y, int flag) noexcept { - int col, row; + int col = clamp (Pixel2Col (x), 0, ncol); + int row = clamp (Pixel2Row (y), 0, nrow - 1); - col = Pixel2Col(x); - row = Pixel2Row(y); - MAX_IT(row, 0); - MIN_IT(row, (int)R->TermWin.nrow - 1); - MAX_IT(col, 0); - MIN_IT(col, (int)R->TermWin.ncol); -#ifndef NO_NEW_SELECTION -/* - * If we're selecting characters (single click) then we must check first - * if we are at the same place as the original mark. If we are then - * select nothing. Otherwise, if we're to the right of the mark, you have to - * be _past_ a character for it to be selected. - */ - if (R->selection_style != OLD_SELECT) { - if (((R->selection.clicks % 3) == 1) && !flag - && (col == R->selection.mark.col - && (row == R->selection.mark.row + R->TermWin.view_start))) { - /* select nothing */ - R->selection.beg.row = R->selection.end.row = 0; - R->selection.beg.col = R->selection.end.col = 0; - R->selection.clicks = 4; - R->h->want_refresh = 1; - D_SELECT((stderr, "rxvt_selection_extend() R->selection.clicks = 4")); - return; - } - } -#endif - if (R->selection.clicks == 4) - R->selection.clicks = 1; - rxvt_selection_extend_colrow(aR_ col, row, !!flag, /* ? button 3 */ - flag == 1 ? 1 : 0, /* ? button press */ - 0); /* no click change */ + /* + * If we're selecting characters (single click) then we must check first + * if we are at the same place as the original mark. If we are then + * select nothing. Otherwise, if we're to the right of the mark, you have to + * be _past_ a character for it to be selected. + */ + if (((selection.clicks % 3) == 1) && !flag + && (col == selection.mark.col + && (row == selection.mark.row - view_start))) + { + /* select nothing */ + selection.beg.row = selection.end.row = 0; + selection.beg.col = selection.end.col = 0; + selection.clicks = 4; + want_refresh = 1; + return; + } + + if (selection.clicks == 4) + selection.clicks = 1; + + selection_extend_colrow (col, row, !!flag, /* ? button 3 */ + flag == 1 ? 1 : 0, /* ? button press */ + 0); /* no click change */ } /* ------------------------------------------------------------------------- */ /* * Extend the selection to the specified col/row */ -/* INTPROTO */ -void -rxvt_selection_extend_colrow(pR_ int32_t col, int32_t row, int button3, int buttonpress, int clickchange) +void ecb_cold +rxvt_term::selection_extend_colrow (int32_t col, int32_t row, int button3, int buttonpress, int clickchange) noexcept { - int16_t ncol = R->TermWin.ncol; - int end_col; - row_col_t pos; - enum { - LEFT, RIGHT - } closeto = RIGHT; - - D_SELECT((stderr, "rxvt_selection_extend_colrow(c:%d, r:%d, %d, %d) clicks:%d, op:%d", col, row, button3, buttonpress, R->selection.clicks, R->selection.op)); - D_SELECT((stderr, "rxvt_selection_extend_colrow() ENT b:(r:%d,c:%d) m:(r:%d,c:%d), e:(r:%d,c:%d)", R->selection.beg.row, R->selection.beg.col, R->selection.mark.row, R->selection.mark.col, R->selection.end.row, R->selection.end.col)); - - R->h->want_refresh = 1; - switch (R->selection.op) { - case SELECTION_INIT: - CLEAR_SELECTION(R); - R->selection.op = SELECTION_BEGIN; - /* FALLTHROUGH */ - case SELECTION_BEGIN: - if (row != R->selection.mark.row || col != R->selection.mark.col - || (!button3 && buttonpress)) - R->selection.op = SELECTION_CONT; - break; - case SELECTION_DONE: - R->selection.op = SELECTION_CONT; - /* FALLTHROUGH */ - case SELECTION_CONT: - break; - case SELECTION_CLEAR: - rxvt_selection_start_colrow(aR_ col, row); - /* FALLTHROUGH */ - default: - return; - } - if (R->selection.beg.col == R->selection.end.col - && R->selection.beg.col != R->selection.mark.col - && R->selection.beg.row == R->selection.end.row - && R->selection.beg.row != R->selection.mark.row) { - R->selection.beg.col = R->selection.end.col = R->selection.mark.col; - R->selection.beg.row = R->selection.end.row = R->selection.mark.row; - D_SELECT((stderr, "rxvt_selection_extend_colrow() ENT2 b:(r:%d,c:%d) m:(r:%d,c:%d), e:(r:%d,c:%d)", R->selection.beg.row, R->selection.beg.col, R->selection.mark.row, R->selection.mark.col, R->selection.end.row, R->selection.end.col)); - } - - pos.col = col; - pos.row = row; - - pos.row -= R->TermWin.view_start; /* adjust for scroll */ - -#ifndef NO_OLD_SELECTION -/* - * This mimics some of the selection behaviour of version 2.20 and before. - * There are no ``selection modes'', button3 is always character extension. - * Note: button3 drag is always available, c.f. v2.20 - * Selection always terminates (left or right as appropriate) at the mark. - */ - if (R->selection_style == OLD_SELECT) { - if (R->selection.clicks == 1 || button3) { - if (R->h->hate_those_clicks) { - R->h->hate_those_clicks = 0; - if (R->selection.clicks == 1) { - R->selection.beg.row = R->selection.mark.row; - R->selection.beg.col = R->selection.mark.col; - } else { - R->selection.mark.row = R->selection.beg.row; - R->selection.mark.col = R->selection.beg.col; - } - } - if (ROWCOL_IS_BEFORE(pos, R->selection.mark)) { - R->selection.end.row = R->selection.mark.row; - R->selection.end.col = R->selection.mark.col + 1; - R->selection.beg.row = pos.row; - R->selection.beg.col = pos.col; - } else { - R->selection.beg.row = R->selection.mark.row; - R->selection.beg.col = R->selection.mark.col; - R->selection.end.row = pos.row; - R->selection.end.col = pos.col + 1; - } - } else if (R->selection.clicks == 2) { - rxvt_selection_delimit_word(aR_ UP, &(R->selection.mark), - &(R->selection.beg)); - rxvt_selection_delimit_word(aR_ DN, &(R->selection.mark), - &(R->selection.end)); - R->h->hate_those_clicks = 1; - } else if (R->selection.clicks == 3) { - R->selection.beg.row = R->selection.end.row = R->selection.mark.row; - R->selection.beg.col = 0; - R->selection.end.col = ncol; - R->h->hate_those_clicks = 1; - } - D_SELECT((stderr, "rxvt_selection_extend_colrow() EXIT b:(r:%d,c:%d) m:(r:%d,c:%d), e:(r:%d,c:%d)", R->selection.beg.row, R->selection.beg.col, R->selection.mark.row, R->selection.mark.col, R->selection.end.row, R->selection.end.col)); - return; - } -#endif /* ! NO_OLD_SELECTION */ -#ifndef NO_NEW_SELECTION -/* selection_style must not be OLD_SELECT to get here */ -/* - * This is mainly xterm style selection with a couple of differences, mainly - * in the way button3 drag extension works. - * We're either doing: button1 drag; button3 press; or button3 drag - * a) button1 drag : select around a midpoint/word/line - that point/word/line - * is always at the left/right edge of the R->selection. - * b) button3 press: extend/contract character/word/line at whichever edge of - * the selection we are closest to. - * c) button3 drag : extend/contract character/word/line - we select around - * a point/word/line which is either the start or end of the selection - * and it was decided by whichever point/word/line was `fixed' at the - * time of the most recent button3 press - */ - if (button3 && buttonpress) { /* button3 press */ - /* - * first determine which edge of the selection we are closest to - */ - if (ROWCOL_IS_BEFORE(pos, R->selection.beg) - || (!ROWCOL_IS_AFTER(pos, R->selection.end) - && (((pos.col - R->selection.beg.col) - + ((pos.row - R->selection.beg.row) * ncol)) - < ((R->selection.end.col - pos.col) - + ((R->selection.end.row - pos.row) * ncol))))) - closeto = LEFT; - if (closeto == LEFT) { - R->selection.beg.row = pos.row; - R->selection.beg.col = pos.col; - R->selection.mark.row = R->selection.end.row; - R->selection.mark.col = R->selection.end.col - - (R->selection.clicks == 2); - } else { - R->selection.end.row = pos.row; - R->selection.end.col = pos.col; - R->selection.mark.row = R->selection.beg.row; - R->selection.mark.col = R->selection.beg.col; - } - } else { /* button1 drag or button3 drag */ - if (ROWCOL_IS_AFTER(R->selection.mark, pos)) { - if ((R->selection.mark.row == R->selection.end.row) - && (R->selection.mark.col == R->selection.end.col) - && clickchange && R->selection.clicks == 2) - R->selection.mark.col--; - R->selection.beg.row = pos.row; - R->selection.beg.col = pos.col; - R->selection.end.row = R->selection.mark.row; - R->selection.end.col = R->selection.mark.col - + (R->selection.clicks == 2); - } else { - R->selection.beg.row = R->selection.mark.row; - R->selection.beg.col = R->selection.mark.col; - R->selection.end.row = pos.row; - R->selection.end.col = pos.col; - } - } - - if (R->selection.clicks == 1) { - end_col = R->screen.tlen[R->selection.beg.row + R->TermWin.saveLines]; - if (end_col != -1 && R->selection.beg.col > end_col) { -#if 1 - R->selection.beg.col = ncol; -#else - if (R->selection.beg.row != R->selection.end.row) - R->selection.beg.col = ncol; - else - R->selection.beg.col = R->selection.mark.col; -#endif - } - end_col = R->screen.tlen[R->selection.end.row + R->TermWin.saveLines]; - if (end_col != -1 && R->selection.end.col > end_col) - R->selection.end.col = ncol; - - } else if (R->selection.clicks == 2) { - if (ROWCOL_IS_AFTER(R->selection.end, R->selection.beg)) - R->selection.end.col--; - rxvt_selection_delimit_word(aR_ UP, &(R->selection.beg), - &(R->selection.beg)); - rxvt_selection_delimit_word(aR_ DN, &(R->selection.end), - &(R->selection.end)); - } else if (R->selection.clicks == 3) { -#ifndef NO_FRILLS - if ((R->Options & Opt_tripleclickwords)) { - int end_row; - - rxvt_selection_delimit_word(aR_ UP, &(R->selection.beg), - &(R->selection.beg)); - end_row = R->screen.tlen[R->selection.mark.row - + R->TermWin.saveLines]; - for (end_row = R->selection.mark.row; end_row < R->TermWin.nrow; - end_row++) { - end_col = R->screen.tlen[end_row + R->TermWin.saveLines]; - if (end_col != -1) { - R->selection.end.row = end_row; - R->selection.end.col = end_col; - rxvt_selection_remove_trailing_spaces(aR); - break; - } - } - } else -#endif - { - if (ROWCOL_IS_AFTER(R->selection.mark, R->selection.beg)) - R->selection.mark.col++; - R->selection.beg.col = 0; - R->selection.end.col = ncol; - } - } - if (button3 && buttonpress) { /* mark may need to be changed */ - if (closeto == LEFT) { - R->selection.mark.row = R->selection.end.row; - R->selection.mark.col = R->selection.end.col - - (R->selection.clicks == 2); - } else { - R->selection.mark.row = R->selection.beg.row; - R->selection.mark.col = R->selection.beg.col; - } - } - D_SELECT((stderr, "rxvt_selection_extend_colrow() EXIT b:(r:%d,c:%d) m:(r:%d,c:%d), e:(r:%d,c:%d)", R->selection.beg.row, R->selection.beg.col, R->selection.mark.row, R->selection.mark.col, R->selection.end.row, R->selection.end.col)); -#endif /* ! NO_NEW_SELECTION */ -} - -#ifndef NO_FRILLS -/* INTPROTO */ -void -rxvt_selection_remove_trailing_spaces(pR) -{ - int32_t end_col, end_row; - text_t *stp; - - end_col = R->selection.end.col; - end_row = R->selection.end.row; - for ( ; end_row >= R->selection.beg.row; ) { - stp = R->screen.text[end_row + R->TermWin.saveLines]; - while (--end_col >= 0) { - if (stp[end_col] != ' ' && stp[end_col] != '\t') - break; - } - if (end_col >= 0 - || R->screen.tlen[end_row - 1 + R->TermWin.saveLines] != -1) { - R->selection.end.col = end_col + 1; - R->selection.end.row = end_row; - break; - } - end_row--; - end_col = R->TermWin.ncol; - } - if (R->selection.mark.row > R->selection.end.row) { - R->selection.mark.row = R->selection.end.row; - R->selection.mark.col = R->selection.end.col; - } else if (R->selection.mark.row == R->selection.end.row - && R->selection.mark.col > R->selection.end.col) - R->selection.mark.col = R->selection.end.col; + row_col_t pos; + enum { + LEFT, RIGHT + } closeto = RIGHT; + + switch (selection.op) + { + case SELECTION_INIT: + CLEAR_SELECTION (); + selection.op = SELECTION_BEGIN; + /* FALLTHROUGH */ + case SELECTION_BEGIN: + if (row != selection.mark.row || col != selection.mark.col + || (!button3 && buttonpress)) + selection.op = SELECTION_CONT; + break; + case SELECTION_DONE: + selection.op = SELECTION_CONT; + /* FALLTHROUGH */ + case SELECTION_CONT: + break; + case SELECTION_CLEAR: + selection_start_colrow (col, row); + /* FALLTHROUGH */ + default: + return; + } + + if (selection.beg.col == selection.end.col + && selection.beg.col != selection.mark.col + && selection.beg.row == selection.end.row + && selection.beg.row != selection.mark.row) + { + selection.beg.col = selection.end.col = selection.mark.col; + selection.beg.row = selection.end.row = selection.mark.row; + } + + pos.col = col; + pos.row = view_start + row; + + /* + * This is mainly xterm style selection with a couple of differences, mainly + * in the way button3 drag extension works. + * We're either doing: button1 drag; button3 press; or button3 drag + * a) button1 drag : select around a midpoint/word/line - that point/word/line + * is always at the left/right edge of the selection. + * b) button3 press: extend/contract character/word/line at whichever edge of + * the selection we are closest to. + * c) button3 drag : extend/contract character/word/line - we select around + * a point/word/line which is either the start or end of the selection + * and it was decided by whichever point/word/line was `fixed' at the + * time of the most recent button3 press + */ + if (button3 && buttonpress) + { + /* button3 press */ + /* + * first determine which edge of the selection we are closest to + */ + if (ROWCOL_IS_BEFORE (pos, selection.beg) + || (!ROWCOL_IS_AFTER (pos, selection.end) + && (((pos.col - selection.beg.col) + + ((pos.row - selection.beg.row) * ncol)) + < ((selection.end.col - pos.col) + + ((selection.end.row - pos.row) * ncol))))) + closeto = LEFT; + + if (closeto == LEFT) + { + selection.beg.row = pos.row; + selection.beg.col = pos.col; + selection.mark.row = selection.end.row; + selection.mark.col = selection.end.col - (selection.clicks == 2); + } + else + { + selection.end.row = pos.row; + selection.end.col = pos.col; + selection.mark.row = selection.beg.row; + selection.mark.col = selection.beg.col; + } + } + else + { + /* button1 drag or button3 drag */ + if (ROWCOL_IS_AFTER (selection.mark, pos)) + { + if (selection.mark.row == selection.end.row + && selection.mark.col == selection.end.col + && clickchange + && selection.clicks == 2) + selection.mark.col--; + + selection.beg.row = pos.row; + selection.beg.col = pos.col; + selection.end.row = selection.mark.row; + selection.end.col = selection.mark.col + (selection.clicks == 2); + } + else + { + selection.beg.row = selection.mark.row; + selection.beg.col = selection.mark.col; + selection.end.row = pos.row; + selection.end.col = pos.col; + } + } + + if (selection.clicks == 1) + { + if (selection.beg.col > ROW(selection.beg.row).l //TODO//FIXME//LEN + && !ROW(selection.beg.row).is_longer () +#if !ENABLE_MINIMAL + && !selection.rect +#endif + ) + selection.beg.col = ncol; + + if ( + selection.end.col > ROW(selection.end.row).l //TODO//FIXME//LEN + && !ROW(selection.end.row).is_longer () +#if !ENABLE_MINIMAL + && !selection.rect +#endif + ) + selection.end.col = ncol; + } + else if (selection.clicks == 2) + { + if (ROWCOL_IS_AFTER (selection.end, selection.beg)) + selection.end.col--; + + selection_delimit_word (UP, &selection.beg, &selection.beg); + selection_delimit_word (DN, &selection.end, &selection.end); + } + else if (selection.clicks == 3) + { +#if ENABLE_FRILLS + if (option (Opt_tripleclickwords)) + { + selection_delimit_word (UP, &selection.beg, &selection.beg); + + for (int end_row = selection.mark.row; end_row < nrow; end_row++) + { + if (!ROW(end_row).is_longer ()) + { + selection.end.row = end_row; + selection.end.col = ROW(end_row).l; +# if !ENABLE_MINIMAL + selection_remove_trailing_spaces (); +# endif + break; + } + } + } + else +#endif + { + if (ROWCOL_IS_AFTER (selection.mark, selection.beg)) + selection.mark.col++; + + selection.beg.col = 0; + selection.end.col = ncol; + + // select a complete logical line + while (selection.beg.row > -saveLines + && ROW(selection.beg.row - 1).is_longer ()) + selection.beg.row--; + + while (selection.end.row < nrow + && ROW(selection.end.row).is_longer ()) + selection.end.row++; + } + } + + if (button3 && buttonpress) + { + /* mark may need to be changed */ + if (closeto == LEFT) + { + selection.mark.row = selection.end.row; + selection.mark.col = selection.end.col - (selection.clicks == 2); + } + else + { + selection.mark.row = selection.beg.row; + selection.mark.col = selection.beg.col; + } + } + +#if !ENABLE_MINIMAL + if (selection.rect && selection.beg.col > selection.end.col) + ::swap (selection.beg.col, selection.end.col); +#endif + + selection_changed (); +} + +#if !ENABLE_MINIMAL +void ecb_cold +rxvt_term::selection_remove_trailing_spaces () noexcept +{ + int32_t end_col, end_row; + text_t *stp; + + end_col = selection.end.col; + end_row = selection.end.row; + + for (; end_row >= selection.beg.row; ) + { + stp = ROW(end_row).t; + + while (--end_col >= 0) + { + if (stp[end_col] != NOCHAR + && !unicode::is_space (stp[end_col])) + break; + } + + if (end_col >= 0 + || !ROW(end_row - 1).is_longer ()) + { + selection.end.col = end_col + 1; + selection.end.row = end_row; + break; + } + + end_row--; + end_col = ncol; + } + + if (selection.mark.row > selection.end.row) + { + selection.mark.row = selection.end.row; + selection.mark.col = selection.end.col; + } + else if (selection.mark.row == selection.end.row + && selection.mark.col > selection.end.col) + selection.mark.col = selection.end.col; } #endif @@ -3193,191 +3387,311 @@ * Double click on button 3 when already selected * EXT: button 3 double click */ -/* EXTPROTO */ -void -rxvt_selection_rotate(pR_ int x, int y) +void ecb_cold +rxvt_term::selection_rotate (int x, int y) noexcept { - R->selection.clicks = R->selection.clicks % 3 + 1; - rxvt_selection_extend_colrow(aR_ Pixel2Col(x), Pixel2Row(y), 1, 0, 1); + selection.clicks = selection.clicks % 3 + 1; + selection_extend_colrow (Pixel2Col (x), Pixel2Row (y), 1, 0, 1); } /* ------------------------------------------------------------------------- */ /* - * On some systems, the Atom typedef is 64 bits wide. We need to have a type - * that is exactly 32 bits wide, because a format of 64 is not allowed by - * the X11 protocol. - */ -typedef CARD32 Atom32; - -/* ------------------------------------------------------------------------- */ -/* * Respond to a request for our current selection * EXT: SelectionRequest */ -/* EXTPROTO */ -void -rxvt_selection_send(pR_ const XSelectionRequestEvent *rq) +void ecb_cold +rxvt_term::selection_send (const XSelectionRequestEvent &rq) noexcept { - XSelectionEvent ev; -#ifdef USE_XIM - Atom32 target_list[4]; + Atom property = rq.property == None ? rq.target : rq.property; + XSelectionEvent ev; + + ev.type = SelectionNotify; + ev.property = None; + ev.display = rq.display; + ev.requestor = rq.requestor; + ev.selection = rq.selection; + ev.target = rq.target; + ev.time = rq.time; + + if (rq.target == xa[XA_TARGETS]) + { + Atom target_list[6]; + Atom *target = target_list; + + *target++ = xa[XA_TARGETS]; + *target++ = xa[XA_TIMESTAMP]; + *target++ = XA_STRING; + *target++ = xa[XA_TEXT]; + *target++ = xa[XA_COMPOUND_TEXT]; +#if X_HAVE_UTF8_STRING + *target++ = xa[XA_UTF8_STRING]; +#endif + + XChangeProperty (dpy, rq.requestor, property, XA_ATOM, + 32, PropModeReplace, + (unsigned char *)target_list, target - target_list); + ev.property = property; + } +#if TODO // TODO + else if (rq.target == xa[XA_MULTIPLE]) + { + /* TODO: Handle MULTIPLE */ + } +#endif + else if (rq.target == xa[XA_TIMESTAMP] && rq.selection == XA_PRIMARY && selection.text) + { + XChangeProperty (dpy, rq.requestor, property, rq.target, + 32, PropModeReplace, (unsigned char *)&selection_time, 1); + ev.property = property; + } + else if (rq.target == xa[XA_TIMESTAMP] && rq.selection == xa[XA_CLIPBOARD] && selection.clip_text) + { + XChangeProperty (dpy, rq.requestor, property, rq.target, + 32, PropModeReplace, (unsigned char *)&clipboard_time, 1); + ev.property = property; + } + else if (rq.target == XA_STRING + || rq.target == xa[XA_TEXT] + || rq.target == xa[XA_COMPOUND_TEXT] + || rq.target == xa[XA_UTF8_STRING] + ) + { + XTextProperty ct; + Atom target = rq.target; + short freect = 0; + int selectlen; + wchar_t *cl; + enum { + enc_string = XStringStyle, + enc_text = XStdICCTextStyle, + enc_compound_text = XCompoundTextStyle, +#ifdef X_HAVE_UTF8_STRING + enc_utf8 = XUTF8StringStyle, #else - Atom32 target_list[3]; + enc_utf8 = -1, #endif - Atom target; - XTextProperty ct; - XICCEncodingStyle style; - char *cl[2], dummy[1]; - - ev.type = SelectionNotify; - ev.property = None; - ev.display = rq->display; - ev.requestor = rq->requestor; - ev.selection = rq->selection; - ev.target = rq->target; - ev.time = rq->time; - - if (rq->target == R->h->xa[XA_TARGETS]) { - target_list[0] = (Atom32) R->h->xa[XA_TARGETS]; - target_list[1] = (Atom32) XA_STRING; - target_list[2] = (Atom32) R->h->xa[XA_TEXT]; -#ifdef USE_XIM - target_list[3] = (Atom32) R->h->xa[XA_COMPOUND_TEXT]; -#endif - XChangeProperty(R->Xdisplay, rq->requestor, rq->property, XA_ATOM, - (8 * sizeof(target_list[0])), PropModeReplace, - (unsigned char *)target_list, - (sizeof(target_list) / sizeof(target_list[0]))); - ev.property = rq->property; - } else if (rq->target == R->h->xa[XA_MULTIPLE]) { - /* TODO: Handle MULTIPLE */ - } else if (rq->target == R->h->xa[XA_TIMESTAMP] && R->selection.text) { - XChangeProperty(R->Xdisplay, rq->requestor, rq->property, XA_INTEGER, - (8 * sizeof(Time)), PropModeReplace, - (unsigned char *)&R->h->selection_time, 1); - ev.property = rq->property; - } else if (rq->target == XA_STRING - || rq->target == R->h->xa[XA_COMPOUND_TEXT] - || rq->target == R->h->xa[XA_TEXT]) { -#ifdef USE_XIM - short freect = 0; -#endif - int selectlen; - -#ifdef USE_XIM - if (rq->target != XA_STRING) { - target = R->h->xa[XA_COMPOUND_TEXT]; - style = (rq->target == R->h->xa[XA_COMPOUND_TEXT]) - ? XCompoundTextStyle : XStdICCTextStyle; - } else -#endif - { - target = XA_STRING; - style = XStringStyle; - } - if (R->selection.text) { - cl[0] = (char *)R->selection.text; - selectlen = R->selection.len; - } else { - cl[0] = dummy; - *dummy = '\0'; - selectlen = 0; - } -#ifdef USE_XIM - if (XmbTextListToTextProperty(R->Xdisplay, cl, 1, style, &ct) - == Success) /* if we failed to convert then send it raw */ - freect = 1; - else -#endif - { - ct.value = (unsigned char *)cl[0]; - ct.nitems = selectlen; - } - XChangeProperty(R->Xdisplay, rq->requestor, rq->property, - target, 8, PropModeReplace, - ct.value, (int)ct.nitems); - ev.property = rq->property; -#ifdef USE_XIM - if (freect) - XFree(ct.value); + } style; + + if (target == XA_STRING) + // we actually don't do XA_STRING, but who cares, as i18n clients + // will ask for another format anyways. + style = enc_string; + else if (target == xa[XA_TEXT]) + style = enc_text; + else if (target == xa[XA_COMPOUND_TEXT]) + style = enc_compound_text; +#if !ENABLE_MINIMAL + else if (target == xa[XA_UTF8_STRING]) + style = enc_utf8; #endif + else + { + target = xa[XA_COMPOUND_TEXT]; + style = enc_compound_text; + } + + if (rq.selection == XA_PRIMARY && selection.text) + { + cl = selection.text; + selectlen = selection.len; + } + else if (rq.selection == xa[XA_CLIPBOARD] && selection.clip_text) + { + cl = selection.clip_text; + selectlen = selection.clip_len; + } + else + { + cl = (wchar_t *)L""; + selectlen = 0; + } + +#if !ENABLE_MINIMAL + // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it + // so recode it manually + if (style == enc_utf8) + { + freect = 1; + ct.encoding = target; + ct.format = 8; + ct.value = (unsigned char *)rxvt_wcstoutf8 (cl, selectlen); + ct.nitems = strlen ((char *)ct.value); + } + else +#endif + if (XwcTextListToTextProperty (dpy, &cl, 1, (XICCEncodingStyle) style, &ct) >= 0) + freect = 1; + else + { + /* if we failed to convert then send it raw */ + ct.value = (unsigned char *)cl; + ct.nitems = selectlen; + ct.encoding = target; + } + + XChangeProperty (dpy, rq.requestor, property, + ct.encoding, 8, PropModeReplace, + ct.value, (int)ct.nitems); + ev.property = property; + + if (freect) + XFree (ct.value); + } + + XSendEvent (dpy, rq.requestor, False, 0L, (XEvent *)&ev); +} + +/* ------------------------------------------------------------------------- */ +#if USE_XIM +void ecb_cold +rxvt_term::im_set_position (XPoint &pos) noexcept +{ + XWindowAttributes xwa; + + XGetWindowAttributes (dpy, vt, &xwa); + + pos.x = xwa.x + Col2Pixel (screen.cur.col); + pos.y = xwa.y + Height2Pixel (screen.cur.row) + fbase; +} +#endif + +#if ENABLE_OVERLAY +void +rxvt_term::scr_overlay_new (int x, int y, int w, int h) noexcept +{ + if (nrow < 1 || ncol < 1) + return; + + want_refresh = 1; + + scr_overlay_off (); + + if (x < 0) x = ncol - w; + if (y < 0) y = nrow - h; + + // make space for border + w += 2; min_it (w, ncol); + h += 2; min_it (h, nrow); + + x -= 1; clamp_it (x, 0, ncol - w); + y -= 1; clamp_it (y, 0, nrow - h); + + ov.x = x; ov.y = y; + ov.w = w; ov.h = h; + + ov.text = new text_t *[h]; + ov.rend = new rend_t *[h]; + + for (y = 0; y < h; y++) + { + text_t *tp = ov.text[y] = new text_t[w]; + rend_t *rp = ov.rend[y] = new rend_t[w]; + + text_t t0, t1, t2; + rend_t r = OVERLAY_RSTYLE; + + if (y == 0) + t0 = 0x2554, t1 = 0x2550, t2 = 0x2557; + else if (y < h - 1) + t0 = 0x2551, t1 = 0x0020, t2 = 0x2551; + else + t0 = 0x255a, t1 = 0x2550, t2 = 0x255d; + + *tp++ = t0; + *rp++ = r; + + for (x = w - 2; x > 0; --x) + { + *tp++ = t1; + *rp++ = r; + } + + *tp = t2; + *rp = r; } - XSendEvent(R->Xdisplay, rq->requestor, False, 0L, (XEvent *)&ev); } - -/* ------------------------------------------------------------------------- * - * MOUSE ROUTINES * - * ------------------------------------------------------------------------- */ -/* - * return col/row values corresponding to x/y pixel values - */ -/* EXTPROTO */ void -rxvt_pixel_position(pR_ int *x, int *y) +rxvt_term::scr_overlay_off () noexcept { - *x = Pixel2Col(*x); -/* MAX_IT(*x, 0); MIN_IT(*x, (int)R->TermWin.ncol - 1); */ - *y = Pixel2Row(*y); -/* MAX_IT(*y, 0); MIN_IT(*y, (int)R->TermWin.nrow - 1); */ + if (!ov.text) + return; + + want_refresh = 1; + + for (int y = 0; y < ov.h; y++) + { + delete [] ov.text[y]; + delete [] ov.rend[y]; + } + + delete [] ov.text; ov.text = 0; + delete [] ov.rend; ov.rend = 0; } -/* ------------------------------------------------------------------------- */ -#ifdef USE_XIM -/* EXTPROTO */ + void -rxvt_setPosition(pR_ XPoint *pos) +rxvt_term::scr_overlay_set (int x, int y, text_t text, rend_t rend) noexcept { - XWindowAttributes xwa; + if (!ov.text || x >= ov.w - 2 || y >= ov.h - 2) + return; + + x++, y++; - XGetWindowAttributes(R->Xdisplay, R->TermWin.vt, &xwa); - pos->x = Col2Pixel(R->screen.cur.col) + xwa.x; - pos->y = Height2Pixel((R->screen.cur.row + 1)) + xwa.y - - R->TermWin.lineSpace; + ov.text[y][x] = text; + ov.rend[y][x] = rend; } -#endif -/* ------------------------------------------------------------------------- */ - -/* ------------------------------------------------------------------------- * - * DEBUG ROUTINES * - * ------------------------------------------------------------------------- */ -#if 0 -/* INTPROTO */ + void -rxvt_debug_colors(void) +rxvt_term::scr_overlay_set (int x, int y, const char *s) noexcept { - int color; - const char *name[] = { - "fg", "bg", - "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white" - }; - - fprintf(stderr, "Color ( "); - if (R->h->rstyle & RS_RVid) - fprintf(stderr, "rvid "); - if (R->h->rstyle & RS_Bold) - fprintf(stderr, "bold "); - if (R->h->rstyle & RS_Blink) - fprintf(stderr, "blink "); - if (R->h->rstyle & RS_Uline) - fprintf(stderr, "uline "); - fprintf(stderr, "): "); - - color = GET_FGCOLOR(R->h->rstyle); -#ifndef NO_BRIGHTCOLOR - if (color >= minBrightCOLOR && color <= maxBrightCOLOR) { - color -= (minBrightCOLOR - minCOLOR); - fprintf(stderr, "bright "); - } -#endif - fprintf(stderr, "%s on ", name[color]); - - color = GET_BGCOLOR(R->h->rstyle); -#ifndef NO_BRIGHTCOLOR - if (color >= minBrightCOLOR && color <= maxBrightCOLOR) { - color -= (minBrightCOLOR - minCOLOR); - fprintf(stderr, "bright "); + while (*s) + scr_overlay_set (x++, y, *s++); +} + +void +rxvt_term::scr_overlay_set (int x, int y, const wchar_t *s) noexcept +{ + while (*s) + { + text_t t = *s++; + int width = WCWIDTH (t); + + while (width--) + { + scr_overlay_set (x++, y, t); + t = NOCHAR; + } } -#endif - fprintf(stderr, "%s\n", name[color]); } + +void +rxvt_term::scr_swap_overlay () noexcept +{ + if (!ov.text) + return; + + // hide cursor if it is within the overlay area + if (IN_RANGE_EXC (screen.cur.col - ov.x, 0, ov.w) + && IN_RANGE_EXC (screen.cur.row - ov.y, 0, ov.h)) + screen.flags &= ~Screen_VisibleCursor; + + // swap screen mem with overlay + for (int y = ov.h; y--; ) + { + text_t *t1 = ov.text[y]; + rend_t *r1 = ov.rend[y]; + + text_t *t2 = ROW(y + ov.y + view_start).t + ov.x; + rend_t *r2 = ROW(y + ov.y + view_start).r + ov.x; + + for (int x = ov.w; x--; ) + { + text_t t = *t1; *t1++ = *t2; *t2++ = t; + rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, FONTSET (r)->find_font (t)); + } + } +} + #endif +/* ------------------------------------------------------------------------- */ +