*** empty log message ***
[dana/urxvt.git] / src / screen.C
index db93054..5836898 100644 (file)
@@ -25,7 +25,6 @@
  */
 
 #include "../config.h"          /* NECESSARY */
-#define INTERN_SCREEN
 #include "rxvt.h"               /* NECESSARY */
 
 #include <X11/Xmd.h>            /* get the typedef for CARD32 */
@@ -42,7 +41,8 @@ inline void fill_text (text_t *start, text_t value, int len)
 }
 
 /* ------------------------------------------------------------------------- */
-#define PROP_SIZE               16384
+#define PROP_SIZE               256*1024
+#define PASTE_SIZE             32768
 #define TABSIZE                 8       /* default tab size */
 
 /* ------------------------------------------------------------------------- *
@@ -114,12 +114,13 @@ inline void fill_text (text_t *start, text_t value, int len)
 /* ------------------------------------------------------------------------- *
  *                        SCREEN `COMMON' ROUTINES                           *
  * ------------------------------------------------------------------------- */
+
 /* Fill part/all of a line with blanks. */
 void
 rxvt_term::scr_blank_line (text_t *et, rend_t *er, unsigned int width, rend_t efs)
 {
   efs &= ~RS_baseattrMask;
-  efs = SET_FONT (efs, TermWin.ascii_map [' ' - 0x20]);
+  efs = SET_FONT (efs, FONTSET (efs)->find_font (' '));
 
   while (width--)
     {
@@ -419,7 +420,7 @@ rxvt_term::scr_reset ()
 void
 rxvt_term::scr_reset_realloc ()
 {
-  uint16_t total_rows, nrow;
+  unsigned int total_rows, nrow;
 
   nrow = TermWin.nrow;
   total_rows = nrow + TermWin.saveLines;
@@ -446,7 +447,7 @@ rxvt_term::scr_reset_realloc ()
 void
 rxvt_term::scr_release ()
 {
-  uint16_t total_rows;
+  unsigned int total_rows;
   int i;
 
   total_rows = TermWin.nrow + TermWin.saveLines;
@@ -513,21 +514,21 @@ rxvt_term::scr_cursor (int mode)
   switch (mode)
     {
       case SAVE:
-        s->s_cur.row = s->cur.row;
-        s->s_cur.col = s->cur.col;
+        s->s_cur.row = screen.cur.row;
+        s->s_cur.col = screen.cur.col;
         s->s_rstyle = rstyle;
-        s->s_charset = s->charset;
-        s->s_charset_char = charsets[s->charset];
+        s->s_charset = screen.charset;
+        s->s_charset_char = charsets[screen.charset];
         break;
 
       case RESTORE:
         want_refresh = 1;
-        s->cur.row = s->s_cur.row;
-        s->cur.col = s->s_cur.col;
-        s->flags &= ~Screen_WrapNext;
+        screen.cur.row = s->s_cur.row;
+        screen.cur.col = s->s_cur.col;
+        screen.flags &= ~Screen_WrapNext;
         rstyle = s->s_rstyle;
-        s->charset = s->s_charset;
-        charsets[s->charset] = s->s_charset_char;
+        screen.charset = s->s_charset;
+        charsets[screen.charset] = s->s_charset_char;
         set_font_style ();
         break;
     }
@@ -568,28 +569,32 @@ rxvt_term::scr_change_screen (int scrn)
   selection_check (2);        /* check for boundary cross */
 
   SWAP_IT (current_screen, scrn, int);
+
+  SWAP_IT (screen.cur.row, swap.cur.row, int16_t);
+  SWAP_IT (screen.cur.col, swap.cur.col, int16_t);
+# ifdef DEBUG_STRICT
+  assert (screen.cur.row >= 0 && screen.cur.row < prev_nrow);
+  assert (screen.cur.col >= 0 && screen.cur.col < prev_ncol);
+# else                          /* drive with your eyes closed */
+  MAX_IT (screen.cur.row, 0);
+  MIN_IT (screen.cur.row, (int32_t)prev_nrow - 1);
+  MAX_IT (screen.cur.col, 0);
+  MIN_IT (screen.cur.col, (int32_t)prev_ncol - 1);
+# endif
+
 #if NSCREENS
   if (options & Opt_secondaryScreen)
     {
       num_scr = 0;
       offset = TermWin.saveLines;
+
       for (i = prev_nrow; i--;)
         {
           SWAP_IT (screen.text[i + offset], swap.text[i], text_t *);
           SWAP_IT (screen.tlen[i + offset], swap.tlen[i], int16_t);
           SWAP_IT (screen.rend[i + offset], swap.rend[i], rend_t *);
         }
-      SWAP_IT (screen.cur.row, swap.cur.row, int16_t);
-      SWAP_IT (screen.cur.col, swap.cur.col, int16_t);
-# ifdef DEBUG_STRICT
-      assert ((screen.cur.row >= 0) && (screen.cur.row < prev_nrow));
-      assert ((screen.cur.col >= 0) && (screen.cur.col < prev_ncol));
-# else                          /* drive with your eyes closed */
-      MAX_IT (screen.cur.row, 0);
-      MIN_IT (screen.cur.row, (int32_t)prev_nrow - 1);
-      MAX_IT (screen.cur.col, 0);
-      MIN_IT (screen.cur.col, (int32_t)prev_ncol - 1);
-# endif
+
       SWAP_IT (screen.charset, swap.charset, int16_t);
       SWAP_IT (screen.flags, swap.flags, int);
       screen.flags |= Screen_VisibleCursor;
@@ -598,8 +603,8 @@ rxvt_term::scr_change_screen (int scrn)
   else
 #endif
     if (options & Opt_secondaryScroll)
-      //if (current_screen == PRIMARY)
-        scr_scroll_text (0, (prev_nrow - 1), prev_nrow, 0);
+      scr_scroll_text (0, prev_nrow - 1, prev_nrow, 0);
+
   return scrn;
 }
 
@@ -627,7 +632,9 @@ rxvt_term::scr_do_wrap ()
 void
 rxvt_term::scr_color (unsigned int color, int fgbg)
 {
-  color &= RS_fgMask;
+  if (color > maxTermCOLOR)
+    color = fgbg;
+
   if (fgbg == Color_fg)
     rstyle = SET_FGCOLOR (rstyle, color);
   else
@@ -675,7 +682,7 @@ rxvt_term::scr_scroll_text (int row1, int row2, int count, int spec)
       if (nscrolled > (long)TermWin.saveLines)
         TermWin.nscrolled = TermWin.saveLines;
       else
-        TermWin.nscrolled = (uint16_t)nscrolled;
+        TermWin.nscrolled = (unsigned int)nscrolled;
 
       if ((options & Opt_scrollWithBuffer)
           && TermWin.view_start != 0
@@ -828,7 +835,7 @@ rxvt_term::scr_add_lines (const unicode_t *str, int nlines, int len)
         switch (c)
           {
             case C0_HT:
-              scr_tab (1);
+              scr_tab (1, true);
               continue;
 
             case C0_LF:
@@ -878,9 +885,6 @@ rxvt_term::scr_add_lines (const unicode_t *str, int nlines, int len)
           srp = screen.rend[row];  /* _must_ refresh */
         }
 
-      if (screen.flags & Screen_Insert)
-        scr_insdel_chars (1, INSERT);
-
       // rely on wcwidth to tell us the character width, at least for non-latin1
       // do wcwidth before further replacements, as wcwidth says that line-drawing
       // characters have width -1 (DOH!) on GNU/Linux sometimes.
@@ -889,22 +893,35 @@ rxvt_term::scr_add_lines (const unicode_t *str, int nlines, int len)
       if (charsets[screen.charset] == '0') // DEC SPECIAL
         {
           // vt100 special graphics and line drawing
-          static uint16_t vt100_0[32] = { // 5f .. 7e
-            0x0020, 0x25c6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0,
-            0x00b1, 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c,
-            0x23ba, 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534,
-            0x252c, 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7,
+          // 5f-7e standard vt100
+          // 40-5e rxvt extension for extra curses acs chars
+          static uint16_t vt100_0[63] = { // 40 .. 7e
+            0x0000, 0x2191, 0x2193, 0x2192, 0x2190, 0x2588, 0x259a, 0x2603, // 40-47 hi mr. snowman!
+            0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 48-4f
+            0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 50-57
+            0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 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 >= 0x5f && c <= 0x7e)
+          if (c >= 0x40 && c <= 0x7e && vt100_0[c - 0x40])
             {
-              c = vt100_0[c - 0x5f];
+              c = vt100_0[c - 0x40];
               width = 1;
             }
         }
 
+      if (screen.flags & Screen_Insert)
+        scr_insdel_chars (width, INSERT);
+
       if (width != 0)
         {
+          // some utf-8 decoders decode surrogate characters.
+          if (0xd800 <= c && c <= 0xdfff)
+            c = 0xfffd;
+
 #if !UNICODE_3
           // trim characters we can't store directly :(
           if (c >= 0x10000)
@@ -914,11 +931,29 @@ rxvt_term::scr_add_lines (const unicode_t *str, int nlines, int len)
             c = 0xfffd;
 # endif
 #endif
-          bool bold = (options & Opt_realBold) && ((rstyle & RS_Bold) != 0);
-          rend_t rend = SET_FONT (rstyle,
-                                  c > 0x7f || bold || c < 0x20
-                                     ? TermWin.fontset->find_font (c, bold)
-                                     : TermWin.ascii_map [c - 0x20]);
+
+          // nuke the character at this position, if required
+          if (stp[screen.cur.col] == NOCHAR
+              || (screen.cur.col < TermWin.ncol - 1
+                  && stp[screen.cur.col + 1] == NOCHAR))
+            {
+              int col = screen.cur.col;
+
+              // find begin
+              while (col > 0 && stp[col] == NOCHAR)
+                col--;
+
+              rend_t rend = SET_FONT (srp[col], FONTSET (srp[col])->find_font (' '));
+
+              // found begin, nuke
+              do {
+                stp[col] = ' ';
+                srp[col] = rend;
+                col++;
+              } while (col < TermWin.ncol && stp[col] == NOCHAR);
+            }
+
+          rend_t rend = SET_FONT (rstyle, FONTSET (rstyle)->find_font (c));
 
           do
             {
@@ -940,11 +975,12 @@ rxvt_term::scr_add_lines (const unicode_t *str, int nlines, int len)
           while (--width > 0);
 
           // pad with spaces when overwriting wide character with smaller one
-          for (int c = screen.cur.col; c < last_col && stp[c] == NOCHAR; c++)
-            {
-              stp[c] = ' ';
-              srp[c] = rend;
-            }
+          if (!width)
+            for (int c = screen.cur.col; c < last_col && stp[c] == NOCHAR; c++)
+              {
+                stp[c] = ' ';
+                srp[c] = rend;
+              }
         }
       else if (width == 0)
         {
@@ -961,27 +997,31 @@ rxvt_term::scr_add_lines (const unicode_t *str, int nlines, int len)
             {
               tp = stp + screen.cur.col - 1;
               rp = srp + screen.cur.col - 1;
+
+              while (*tp == NOCHAR && tp > stp)
+                tp--, rp--;
             }
           else if (screen.cur.row > 0
                    && screen.tlen [screen.cur.row - 1 + TermWin.saveLines] == -1)
             {
-              tp = screen.text[screen.cur.row - 1 + TermWin.saveLines] + last_col - 1;
-              rp = screen.rend[screen.cur.row - 1 + TermWin.saveLines] + last_col - 1;
+              int line = screen.cur.row - 1 + TermWin.saveLines;
+
+              tp = screen.text[line] + last_col - 1;
+              rp = screen.rend[line] + last_col - 1;
+
+              while (*tp == NOCHAR && tp > screen.text[line])
+                tp--, rp--;
             }
           else
             continue;
 
-          // handle double-width-chars by making them look extremely ugly
-          if (*tp == NOCHAR)
-            *tp = ' '; // hack //D //TODO //--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, TermWin.fontset->find_font (*tp));
+          *rp = SET_FONT (*rp, FONTSET (*rp)->find_font (*tp));
 #endif
         }
     }
@@ -1030,7 +1070,7 @@ rxvt_term::scr_backspace ()
  * XTERM_SEQ: CTRL-I
  */
 void
-rxvt_term::scr_tab (int count)
+rxvt_term::scr_tab (int count, bool ht)
 {
   int i, x;
 
@@ -1041,6 +1081,12 @@ rxvt_term::scr_tab (int count)
     return;
   else if (count > 0)
     {
+      int row = TermWin.saveLines + screen.cur.row;
+      text_t *tp = screen.text[row];
+      rend_t *rp = screen.rend[row];
+      rend_t base_rend = rp[i];
+      ht &= tp[i] == ' ';
+
       for (; ++i < TermWin.ncol; )
         if (tabs[i])
           {
@@ -1048,9 +1094,33 @@ rxvt_term::scr_tab (int count)
             if (!--count)
               break;
           }
+        else 
+          ht &= tp[i] == ' '
+                && RS_SAME (rp[i], base_rend);
 
       if (count)
         x = TermWin.ncol - 1;
+
+      // store horizontal tab commands as characters inside the text
+      // buffer so they can be selected and pasted.
+      if (ht && options & Opt_pastableTabs)
+        {
+          base_rend = SET_FONT (base_rend, 0);
+
+          if (screen.tlen[row] != -1)      /* XXX: think about this */
+            MAX_IT (screen.tlen[row], x);
+
+          i = screen.cur.col;
+
+          tp[i] = '\t';
+          rp[i] = base_rend;
+
+          while (++i < x)
+            {
+              tp[i] = NOCHAR;
+              rp[i] = base_rend;
+            }
+        }
     }
   else /* if (count < 0) */
     {
@@ -1316,7 +1386,7 @@ rxvt_term::scr_erase_screen (int mode)
     }
   else
     {
-      ren = (rstyle & (RS_fgMask | RS_bgMask));
+      ren = rstyle & (RS_fgMask | RS_bgMask);
       gcvalue.foreground = pix_colors[GET_BGCOLOR (rstyle)];
       XChangeGC (display->display, TermWin.gc, GCForeground, &gcvalue);
       ERASE_ROWS (row, num);
@@ -1326,11 +1396,9 @@ rxvt_term::scr_erase_screen (int mode)
 
   for (; num--; row++)
     {
-      scr_blank_screen_mem (screen.text, screen.rend,
-                            (unsigned int) (row + row_offset), rstyle);
+      scr_blank_screen_mem (screen.text, screen.rend, (unsigned int) (row + row_offset), rstyle);
       screen.tlen[row + row_offset] = 0;
-      scr_blank_line (drawn_text[row], drawn_rend[row],
-                      (unsigned int)TermWin.ncol, ren);
+      scr_blank_line (drawn_text[row], drawn_rend[row], (unsigned int)TermWin.ncol, ren);
     }
 }
 
@@ -1351,7 +1419,7 @@ rxvt_term::scr_E ()
   num_scr_allow = 0;
   selection_check (3);
 
-  fs = SET_FONT (rstyle, TermWin.fontset->find_font ('E'));
+  fs = SET_FONT (rstyle, FONTSET (rstyle)->find_font ('E'));
   for (k = TermWin.saveLines, i = TermWin.nrow; i--; k++)
     {
       screen.tlen[k] = TermWin.ncol;    /* make the `E's selectable */
@@ -1424,8 +1492,7 @@ rxvt_term::scr_insdel_chars (int count, int insdel)
   switch (insdel)
     {
       case INSERT:
-        for (col = TermWin.ncol - 1; (col - count) >= screen.cur.col;
-             col--)
+        for (col = TermWin.ncol - 1; (col - count) >= screen.cur.col; col--)
           {
             stp[col] = stp[col - count];
             srp[col] = srp[col - count];
@@ -1445,13 +1512,13 @@ rxvt_term::scr_insdel_chars (int count, int insdel)
               CLEAR_SELECTION ();
             else
               {              /* shift selection */
-                selection.beg.col += count;
+                selection.beg.col  += count;
                 selection.mark.col += count; /* XXX: yes? */
-                selection.end.col += count;
+                selection.end.col  += count;
               }
           }
 
-        scr_blank_line (& (stp[screen.cur.col]), & (srp[screen.cur.col]),
+        scr_blank_line (&stp[screen.cur.col], &srp[screen.cur.col],
                         (unsigned int)count, rstyle);
         break;
 
@@ -1459,7 +1526,7 @@ rxvt_term::scr_insdel_chars (int count, int insdel)
         screen.cur.col += count;     /* don't worry if > TermWin.ncol */
         selection_check (1);
         screen.cur.col -= count;
-        scr_blank_line (& (stp[screen.cur.col]), & (srp[screen.cur.col]),
+        scr_blank_line (&stp[screen.cur.col], &srp[screen.cur.col],
                         (unsigned int)count, rstyle);
         break;
 
@@ -1472,8 +1539,7 @@ rxvt_term::scr_insdel_chars (int count, int insdel)
             srp[col] = srp[col + count];
           }
 
-        scr_blank_line (& (stp[TermWin.ncol - count]),
-                        & (srp[TermWin.ncol - count]),
+        scr_blank_line (&stp[TermWin.ncol - count], &srp[TermWin.ncol - count],
                         (unsigned int)count, tr);
 
         if (*slp == -1) /* break line continuation */
@@ -1492,9 +1558,9 @@ rxvt_term::scr_insdel_chars (int count, int insdel)
             else
               {
                 /* shift selection */
-                selection.beg.col -= count;
+                selection.beg.col  -= count;
                 selection.mark.col -= count; /* XXX: yes? */
-                selection.end.col -= count;
+                selection.end.col  -= count;
               }
           }
 
@@ -1570,6 +1636,7 @@ rxvt_term::scr_relative_origin (int mode)
     screen.flags |= Screen_Relative;
   else
     screen.flags &= ~Screen_Relative;
+
   scr_gotorc (0, 0, 0);
 }
 
@@ -1619,10 +1686,10 @@ rxvt_term::scr_rvideo_mode (int mode)
     {
       rvideo = mode;
       SWAP_IT (pix_colors[Color_fg], pix_colors[Color_bg], rxvt_color);
-#if defined(XPM_BACKGROUND)
+#if XPM_BACKGROUND
       if (bgPixmap.pixmap == None)
 #endif
-#if defined(TRANSPARENT)
+#if TRANSPARENT
         if (! (options & Opt_transparent) || am_transparent == 0)
 #endif
           XSetWindowBackground (display->display, TermWin.vt,
@@ -1687,10 +1754,10 @@ rxvt_term::set_font_style ()
  */
 void
 rxvt_term::scr_charset_choose (int set)
-  {
-    screen.charset = set;
-    set_font_style ();
-  }
+{
+  screen.charset = set;
+  set_font_style ();
+}
 
 /* ------------------------------------------------------------------------- */
 /*
@@ -1703,10 +1770,10 @@ rxvt_term::scr_charset_choose (int set)
  */
 void
 rxvt_term::scr_charset_set (int set, unsigned int ch)
-  {
-    charsets[set] = (unsigned char)ch;
-    set_font_style ();
-  }
+{
+  charsets[set] = (unsigned char)ch;
+  set_font_style ();
+}
 
 \f
 /* ------------------------------------------------------------------------- *
@@ -1810,7 +1877,7 @@ int
 rxvt_term::scr_move_to (int y, int len)
 {
   long p = 0;
-  uint16_t oldviewstart;
+  unsigned int oldviewstart;
 
   oldviewstart = TermWin.view_start;
 
@@ -1821,7 +1888,7 @@ rxvt_term::scr_move_to (int y, int len)
       p = max (p, 0);
     }
 
-  TermWin.view_start = (uint16_t)min (p, TermWin.nscrolled);
+  TermWin.view_start = (unsigned int)min (p, TermWin.nscrolled);
 
   return scr_changeview (oldviewstart);
 }
@@ -1835,7 +1902,7 @@ int
 rxvt_term::scr_page (enum page_dirn direction, int nlines)
 {
   int n;
-  uint16_t oldviewstart;
+  unsigned int oldviewstart;
 
 #ifdef DEBUG_STRICT
   assert ((nlines >= 0) && (nlines <= TermWin.nrow));
@@ -1855,7 +1922,7 @@ rxvt_term::scr_page (enum page_dirn direction, int nlines)
 }
 
 int
-rxvt_term::scr_changeview (uint16_t oldviewstart)
+rxvt_term::scr_changeview (unsigned int oldviewstart)
 {
   if (TermWin.view_start != oldviewstart)
     {
@@ -1962,7 +2029,6 @@ void
 rxvt_term::scr_refresh (unsigned char refresh_type)
 {
   unsigned char must_clear, /* use draw_string not draw_image_string     */
-                rvid,       /* reverse video this position               */
                 showcursor; /* show the cursor                           */
   int16_t col, row,   /* column/row we're processing               */
           ocrow;      /* old cursor row                            */
@@ -1973,6 +2039,8 @@ rxvt_term::scr_refresh (unsigned char refresh_type)
 #endif
   rend_t *crp;        // cursor rendition pointer
 
+  want_refresh = 0;        /* screen is current */
+
   if (refresh_type == NO_REFRESH || !TermWin.mapped)
     return;
 
@@ -1984,10 +2052,10 @@ rxvt_term::scr_refresh (unsigned char refresh_type)
 
   row_offset = TermWin.saveLines - TermWin.view_start;
 
-#ifdef XPM_BACKGROUND
+#if XPM_BACKGROUND
   must_clear |= (bgPixmap.pixmap != None);
 #endif
-#ifdef TRANSPARENT
+#if TRANSPARENT
   must_clear |= ((options & Opt_transparent) && am_transparent);
 #endif
   ocrow = oldcursor.row; /* is there an old outline cursor on screen? */
@@ -1996,9 +2064,6 @@ rxvt_term::scr_refresh (unsigned char refresh_type)
    * B: reverse any characters which are selected
    */
   scr_reverse_selection ();
-#if ENABLE_OVERLAY
-  scr_swap_overlay ();
-#endif
 
   /*
    * C: set the cursor character (s)
@@ -2082,8 +2147,13 @@ rxvt_term::scr_refresh (unsigned char refresh_type)
       }
   }
 
+#if ENABLE_OVERLAY
+  scr_swap_overlay ();
+#endif
+
   rend_t *drp, *srp;  /* drawn-rend-pointer, screen-rend-pointer   */
   text_t *dtp, *stp;  /* drawn-text-pointer, screen-text-pointer   */
+
 #ifndef NO_SLOW_LINK_SUPPORT
   /*
    * D: CopyArea pass - very useful for slower links
@@ -2170,14 +2240,14 @@ rxvt_term::scr_refresh (unsigned char refresh_type)
         {
           /* compare new text with old - if exactly the same then continue */
           if (stp[col] == dtp[col]    /* Must match characters to skip. */
-              && (srp[col] == drp[col]    /* Either rendition the same or   */
+              && (RS_SAME (srp[col], drp[col])    /* Either rendition the same or   */
                   || (stp[col] == ' ' /* space w/ no background change  */
                       && GET_BGATTR (srp[col]) == GET_BGATTR (drp[col]))))
             continue;
 
           // redraw one or more characters
 
-          // seek to the beginning if wide characters
+          // seek to the beginning of wide characters
           while (stp[col] == NOCHAR && col > 0)
             --col;
 
@@ -2190,8 +2260,6 @@ rxvt_term::scr_refresh (unsigned char refresh_type)
 
           int xpixel = Col2Pixel (col);
 
-          // this loop looks very messy, it can probably be optimized
-          // and cleaned a bit by you?
           for (i = 0; ++col < TermWin.ncol; )
             {
               if (stp[col] == NOCHAR)
@@ -2204,13 +2272,13 @@ rxvt_term::scr_refresh (unsigned char refresh_type)
                   continue;
                 }
 
-              if (rend != srp[col])
+              if (!RS_SAME (rend, srp[col]))
                 break;
 
               count++;
 
               if (stp[col] != dtp[col]
-                  || srp[col] != drp[col])
+                  || !RS_SAME (srp[col], drp[col]))
                 {
                   if (must_clear && (i++ > count / 2))
                     break;
@@ -2230,62 +2298,84 @@ rxvt_term::scr_refresh (unsigned char refresh_type)
           while (i && text[count] == NOCHAR)
             count++, i--;
 
+#if ENABLE_STYLES
+          // force redraw after "careful" characters to avoid pixel droppings
+          if (srp[col] & RS_Careful && col < TermWin.ncol - 1 && 0)
+            drp[col + 1] = ~srp[col + 1];
+
+          // 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 -= TermWin.fwidth;
+#endif
+
           /*
            * Determine the attributes for the string
            */
-          int fid = GET_FONT (rend);
           int fore = GET_FGCOLOR (rend); // desired foreground
           int back = GET_BGCOLOR (rend); // desired background
 
-          rend = GET_ATTR (rend);
-
-          rvid = !!(rend & RS_RVid);
+          // only do special processing if ana attributes are set, which is rare
+          if (rend & (RS_Bold | RS_Italic | RS_Uline | RS_RVid | RS_Blink))
+            {
+              bool invert = rend & RS_RVid;
 
 #ifndef NO_BOLD_UNDERLINE_REVERSE
-          if (rend & RS_Bold && fore == Color_fg && !(options & Opt_realBold))
-            {
-              if (ISSET_PIXCOLOR (Color_BD))
-                fore = Color_BD;
-              else
-                rvid = !rvid;
-            }
+              if (rend & RS_Bold
+                  && fore == Color_fg)
+                {
+                  if (ISSET_PIXCOLOR (Color_BD))
+                    fore = Color_BD;
+# if !ENABLE_STYLES
+                  else
+                    invert = !invert;
+# endif
+                }
+
+              if (rend & RS_Italic
+                  && fore == Color_fg)
+                {
+                  if (ISSET_PIXCOLOR (Color_IT))
+                    fore = Color_IT;
+# if !ENABLE_STYLES
+                  else
+                    invert = !invert;
+# endif
+                }
 
-          if (rend & RS_Uline)
-            if (ISSET_PIXCOLOR (Color_UL))
-              fore = Color_UL;
+              if (rend & RS_Uline && ISSET_PIXCOLOR (Color_UL))
+                fore = Color_UL;
 #endif
 
-          if (rvid)
-            {
-              SWAP_IT (fore, back, int);
+              if (invert)
+                {
+                  SWAP_IT (fore, back, int);
 
 #ifndef NO_BOLD_UNDERLINE_REVERSE
-              if (ISSET_PIXCOLOR (Color_RV)
-# ifndef NO_CURSORCOLOR
-                  && !ISSET_PIXCOLOR (Color_cursor)
-# endif
-                 )
-                back = Color_RV;
+                  if (ISSET_PIXCOLOR (Color_RV))
+                    back = Color_RV;
 #endif
-            }
+                }
 
 #ifdef TEXT_BLINK
-          if (rend & RS_Blink && (back == Color_bg || fore == Color_bg))
-            {
-              if (!text_blink_ev.active)
+              if (rend & RS_Blink && (back == Color_bg || fore == Color_bg))
                 {
-                  text_blink_ev.start (NOW + TEXT_BLINK_INTERVAL);
-                  hidden_text = 0;
+                  if (!text_blink_ev.active)
+                    {
+                      text_blink_ev.start (NOW + TEXT_BLINK_INTERVAL);
+                      hidden_text = 0;
+                    }
+                  else if (hidden_text)
+                    fore = back;
                 }
-              else if (hidden_text)
-                fore = back;
-            }
 #endif
+            }
 
           /*
            * Actually do the drawing of the string here
            */
-          rxvt_font *font = (*TermWin.fontset)[fid];
+          rxvt_font *font = (*TermWin.fontset[GET_STYLE (rend)])[GET_FONT (rend)];
 
           if (back == fore)
             font->clear_rect (*TermWin.drawable, xpixel, ypixel,
@@ -2311,12 +2401,19 @@ rxvt_term::scr_refresh (unsigned char refresh_type)
             font->draw (*TermWin.drawable, xpixel, ypixel, text, count, fore, back);
 
           if (rend & RS_Uline && font->descent > 1 && fore != back)
-            XDrawLine (display->display, drawBuffer, TermWin.gc,
-                       xpixel, ypixel + font->ascent + 1,
-                       xpixel + Width2Pixel (count) - 1, ypixel + font->ascent + 1);
+            {
+              XSetForeground (display->display, TermWin.gc, pix_colors[fore]);
+              XDrawLine (display->display, drawBuffer, TermWin.gc,
+                         xpixel, ypixel + font->ascent + 1,
+                         xpixel + Width2Pixel (count) - 1, ypixel + font->ascent + 1);
+            }
         }                     /* for (col....) */
     }                         /* for (row....) */
 
+#if ENABLE_OVERLAY
+  scr_swap_overlay ();
+#endif
+
   /*
    * G: cleanup cursor and display outline cursor if necessary
    */
@@ -2351,9 +2448,6 @@ rxvt_term::scr_refresh (unsigned char refresh_type)
   /*
    * H: cleanup selection
    */
-#if ENABLE_OVERLAY
-  scr_swap_overlay ();
-#endif
   scr_reverse_selection ();
 
   if (refresh_type & SMOOTH_REFRESH)
@@ -2361,7 +2455,6 @@ rxvt_term::scr_refresh (unsigned char refresh_type)
 
   num_scr = 0;
   num_scr_allow = 1;
-  want_refresh = 0;        /* screen is current */
 }
 
 void
@@ -2371,7 +2464,7 @@ rxvt_term::scr_remap_chars (text_t *tp, rend_t *rp)
     return;
 
   for (int i = TermWin.ncol; i; i--, rp++, tp++)
-    *rp = SET_FONT (*rp, TermWin.fontset->find_font (*tp));
+    *rp = SET_FONT (*rp, FONTSET (*rp)->find_font (*tp));
 }
 
 void
@@ -2387,6 +2480,36 @@ rxvt_term::scr_remap_chars ()
     }
 }
 
+void
+rxvt_term::scr_recolour ()
+{
+  if (1
+#if TRANSPARENT
+      && !am_transparent
+#endif
+#if XPM_BACKGROUND
+      && !bgPixmap.pixmap
+#endif
+      )
+    {
+      XSetWindowBackground (display->display, TermWin.parent[0], pix_colors[Color_border]);
+      XClearWindow (display->display, TermWin.parent[0]);
+      XSetWindowBackground (display->display, TermWin.vt, pix_colors[Color_bg]);
+#if HAVE_SCROLLBARS
+      if (scrollBar.win)
+        {
+          XSetWindowBackground (display->display, scrollBar.win, pix_colors[Color_border]);
+          scrollBar.setIdle ();
+          scrollbar_show (0);
+        }
+#endif
+    }
+
+  scr_clear ();
+  scr_touch (true);
+  want_refresh = 1;
+}
+
 /* ------------------------------------------------------------------------- */
 void
 rxvt_term::scr_clear (bool really)
@@ -2397,12 +2520,12 @@ rxvt_term::scr_clear (bool really)
   num_scr_allow = 0;
   want_refresh = 1;
 
-#ifdef TRANSPARENT
+#if TRANSPARENT
   if ((options & Opt_transparent) && (am_pixmap_trans == 0))
     {
       int i;
 
-      if (! (options & Opt_transparent_all))
+      if (!(options & Opt_transparent_all))
         i = 0;
       else
         i = (int) (sizeof (TermWin.parent) / sizeof (Window));
@@ -2428,22 +2551,36 @@ rxvt_term::scr_reverse_selection ()
       int col, row = selection.end.row + TermWin.saveLines;
       rend_t *srp;
 
-      if (i >= end_row)
-        col = selection.beg.col;
-      else
+#if ENABLE_FRILLS
+      if (selection.rect)
         {
-          col = 0;
-          i = end_row;
+          end_row += TermWin.nrow;
+
+          for (; i <= row && i <= end_row; i++)
+            for (srp = screen.rend[i], col = selection.beg.col; col < selection.end.col; col++)
+              srp[col] ^= RS_RVid;
         }
+      else
+#endif
+        {
+          if (i >= end_row)
+            col = selection.beg.col;
+          else
+            {
+              col = 0;
+              i = end_row;
+            }
+
+          end_row += TermWin.nrow;
 
-      end_row += TermWin.nrow;
-      for (; i < row && i < end_row; i++, col = 0)
-        for (srp = screen.rend[i]; col < TermWin.ncol; col++)
-          srp[col] ^= RS_RVid;
+          for (; i < row && i < end_row; i++, col = 0)
+            for (srp = screen.rend[i]; col < TermWin.ncol; col++)
+              srp[col] ^= RS_RVid;
 
-      if (i == row && i < end_row)
-        for (srp = screen.rend[i]; col < selection.end.col; col++)
-          srp[col] ^= RS_RVid;
+          if (i == row && i < end_row)
+            for (srp = screen.rend[i]; col < selection.end.col; col++)
+              srp[col] ^= RS_RVid;
+        }
     }
 }
 
@@ -2524,12 +2661,12 @@ void
 rxvt_term::paste (const unsigned char *data, unsigned int len)
 {
   unsigned int i, j, n;
-  unsigned char *ds = (unsigned char *)rxvt_malloc (PROP_SIZE);
+  unsigned char *ds = (unsigned char *)rxvt_malloc (PASTE_SIZE);
 
   /* convert normal newline chars into common keyboard Return key sequence */
-  for (i = 0; i < len; i += PROP_SIZE)
+  for (i = 0; i < len; i += PASTE_SIZE)
     {
-      n = min (len - i, PROP_SIZE);
+      n = min (len - i, PASTE_SIZE);
       memcpy (ds, data + i, n);
 
       for (j = 0; j < n; j++)
@@ -2547,13 +2684,9 @@ rxvt_term::paste (const unsigned char *data, unsigned int len)
  * Respond to a notification that a primary selection has been sent
  * EXT: SelectionNotify
  */
-int
+void
 rxvt_term::selection_paste (Window win, Atom prop, bool delete_prop)
 {
-  long nread = 0;
-  unsigned long bytes_after;
-  XTextProperty ct;
-
   if (prop == None)         /* check for failed XConvertSelection */
     {
       if ((selection_type & Sel_CompoundText))
@@ -2576,36 +2709,80 @@ rxvt_term::selection_paste (Window win, Atom prop, bool delete_prop)
             selection_type = 0;
         }
 
-      return 0;
+      return;
     }
 
-  for (;;)
+  unsigned long bytes_after;
+  XTextProperty ct;
+
+  if (XGetWindowProperty (display->display, win, prop,
+                          0, PROP_SIZE / 4,
+                          delete_prop, AnyPropertyType,
+                          &ct.encoding, &ct.format,
+                          &ct.nitems, &bytes_after,
+                          &ct.value) != Success)
     {
-      if (XGetWindowProperty (display->display, win, prop, (long) (nread / 4),
-                              (long) (PROP_SIZE / 4), delete_prop,
-                              AnyPropertyType, &ct.encoding, &ct.format,
-                              &ct.nitems, &bytes_after,
-                              &ct.value) != Success)
-        break;
+      ct.value = 0;
+      goto bailout;
+    }
 
-      if (ct.encoding == 0)
-        break;
+  if (ct.encoding == None)
+    goto bailout;
 
-      if (ct.encoding == xa[XA_INCR])
-        {
-          // INCR selection, start handshake
-          XDeleteProperty (display->display, win, prop);
-          selection_wait = Sel_incr;
-          incr_ev.start (NOW + 10);
-          break;
-        }
+  if (bytes_after)
+    {
+      // fetch and append remaining data
+      XTextProperty ct2;
+
+      if (XGetWindowProperty (display->display, win, prop,
+                              ct.nitems / 4, (bytes_after + 3) / 4,
+                              delete_prop, AnyPropertyType,
+                              &ct2.encoding, &ct2.format,
+                              &ct2.nitems, &bytes_after,
+                              &ct2.value) != Success)
+        goto bailout;
+
+      // realloc should be compatible to XFree, here, and elsewhere, too
+      ct.value = (unsigned char *)realloc (ct.value, ct.nitems + ct2.nitems + 1);
+      memcpy (ct.value + ct.nitems, ct2.value, ct2.nitems + 1);
+      ct.nitems += ct2.nitems;
+
+      XFree (ct2.value);
+    }
+
+  if (ct.value == 0)
+    goto bailout;
+
+  if (ct.encoding == xa[XA_INCR])
+    {
+      // INCR selection, start handshake
+      if (!delete_prop)
+        XDeleteProperty (display->display, win, prop);
+
+      selection_wait = Sel_incr;
+      incr_buf_fill = 0;
+      incr_ev.start (NOW + 10);
 
-      if (ct.value == NULL)
-        continue;
+      goto bailout;
+    }
 
-      if (ct.nitems == 0)
+  if (ct.nitems == 0)
+    {
+      if (selection_wait == Sel_incr)
         {
-          if (selection_wait == Sel_normal && nread == 0
+          XFree (ct.value);
+
+          // finally complete, now paste the whole thing
+          selection_wait = Sel_normal;
+          ct.value = (unsigned char *)incr_buf;
+          ct.nitems = incr_buf_fill;
+          incr_buf = 0;
+          incr_buf_size = 0;
+          incr_ev.stop ();
+        }
+      else 
+        {
+          if (selection_wait == Sel_normal
               && (win != display->root || prop != XA_CUT_BUFFER0)) // avoid recursion
             {
               /*
@@ -2615,37 +2792,58 @@ rxvt_term::selection_paste (Window win, Atom prop, bool delete_prop)
               selection_paste (display->root, XA_CUT_BUFFER0, False);
             }
 
-          nread = -1;         /* discount any previous stuff */
-          break;
+          goto bailout;
         }
+    }
+  else if (selection_wait == Sel_incr)
+    {
+      incr_ev.start (NOW + 10);
 
-      nread += ct.nitems;
-
-      char **cl;
-      int cr;
-      if (XmbTextPropertyToTextList (display->display, &ct, &cl, &cr) >= 0 && cl)
+      while (incr_buf_fill + ct.nitems > incr_buf_size)
         {
-          for (int i = 0; i < cr; i++)
-            paste ((unsigned char *)cl[i], strlen (cl[i]));
-
-          XFreeStringList (cl);
+          incr_buf_size = incr_buf_size ? incr_buf_size * 2 : 128*1024;
+          incr_buf = (char *)realloc (incr_buf, incr_buf_size);
         }
-      else
-        paste (ct.value, ct.nitems);
 
-      if (bytes_after == 0)
-        break;
+      memcpy (incr_buf + incr_buf_fill, ct.value, ct.nitems);
+      incr_buf_fill += ct.nitems;
 
-      XFree (ct.value);
+      goto bailout;
+    }
+
+  char **cl;
+  int cr;
+
+#if ENABLE_FRILLS
+  // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it
+  // so recode it manually
+  if (ct.encoding == xa[XA_UTF8_STRING])
+    {
+      wchar_t *w = rxvt_utf8towcs ((const char *)ct.value, ct.nitems);
+      char *s = rxvt_wcstombs (w);
+      free (w);
+      // TODO: strlen == only the first element will be converted. well...
+      paste ((unsigned char *)s, strlen (s));
+      free (s);
+    }
+  else
+#endif
+  if (XmbTextPropertyToTextList (display->display, &ct, &cl, &cr) >= 0
+      && cl)
+    {
+      for (int i = 0; i < cr; i++)
+        paste ((unsigned char *)cl[i], strlen (cl[i]));
+
+      XFreeStringList (cl);
     }
+  else
+    paste (ct.value, ct.nitems); // paste raw
 
-  if (ct.value)
-    XFree (ct.value);
+bailout:
+  XFree (ct.value);
 
   if (selection_wait == Sel_normal)
     selection_wait = Sel_none;
-
-  return (int)nread;
 }
 
 void
@@ -2653,25 +2851,19 @@ rxvt_term::incr_cb (time_watcher &w)
 {
   selection_wait = Sel_none;
 
+  incr_buf_size = 0;
+  free (incr_buf);
+
   rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n");
 }
 
-/*
- * INCR support originally provided by Paul Sheer <psheer@obsidian.co.za>
- */
 void
 rxvt_term::selection_property (Window win, Atom prop)
 {
   if (prop == None || selection_wait != Sel_incr)
     return;
 
-  if (selection_paste (win, prop, 1) > 0)
-    incr_ev.start (NOW + 10);
-  else
-    {
-      selection_wait = Sel_none;
-      incr_ev.stop ();
-    }
+  selection_paste (win, prop, true);
 }
 
 /* ------------------------------------------------------------------------- */
@@ -2715,12 +2907,11 @@ rxvt_term::selection_request (Time tm, int x, int y)
           if (selection_request_other (xa[XA_COMPOUND_TEXT], i))
             return;
 #endif
-
         }
     }
 
-  selection_wait = Sel_none;       /* don't loop in rxvt_selection_paste () */
-  selection_paste (display->root, XA_CUT_BUFFER0, False);
+  selection_wait = Sel_none;       /* don't loop in selection_paste () */
+  selection_paste (display->root, XA_CUT_BUFFER0, false);
 }
 
 int
@@ -2803,7 +2994,6 @@ rxvt_term::selection_make (Time tm)
   new_selection_text = (wchar_t *)rxvt_malloc ((i + 4) * sizeof (wchar_t));
 
   col = selection.beg.col;
-  MAX_IT (col, 0);
   row = selection.beg.row + TermWin.saveLines;
   end_row = selection.end.row + TermWin.saveLines;
   int ofs = 0;
@@ -2811,16 +3001,25 @@ rxvt_term::selection_make (Time tm)
 
   for (; row <= end_row; row++, col = 0)
     {
-      t = &screen.text[row][col];
-
       end_col = screen.tlen[row];
 
+#if ENABLE_FRILLS
+      if (selection.rect)
+        {
+          col = selection.beg.col;
+          end_col = TermWin.ncol + 1;
+        }
+#endif
+
+      MAX_IT (col, 0);
+
       if (end_col == -1)
         end_col = TermWin.ncol;
 
-      if (row == end_row)
+      if (row == end_row || selection.rect)
         MIN_IT (end_col, selection.end.col);
 
+      t = &screen.text[row][col];
       for (; col < end_col; col++)
         {
           if (*t == NOCHAR)
@@ -2878,8 +3077,7 @@ rxvt_term::selection_make (Time tm)
 
   if (XwcTextListToTextProperty (display->display, &selection.text, 1, XStringStyle, &ct) >= 0)
     {
-      XChangeProperty (display->display, display->root, XA_CUT_BUFFER0, XA_STRING, 8,
-                       PropModeReplace, ct.value, ct.nitems);
+      set_string_property (XA_CUT_BUFFER0, ct.value, ct.nitems);
       XFree (ct.value);
     }
 #endif
@@ -3090,6 +3288,7 @@ rxvt_term::selection_extend_colrow (int32_t col, int32_t row, int button3, int b
   } closeto = RIGHT;
 
   want_refresh = 1;
+
   switch (selection.op)
     {
       case SELECTION_INIT:
@@ -3112,6 +3311,7 @@ rxvt_term::selection_extend_colrow (int32_t col, int32_t row, int button3, int b
       default:
         return;
     }
+
   if (selection.beg.col == selection.end.col
       && selection.beg.col != selection.mark.col
       && selection.beg.row == selection.end.row
@@ -3171,9 +3371,10 @@ rxvt_term::selection_extend_colrow (int32_t col, int32_t row, int button3, int b
     { /* 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)
+          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;
@@ -3194,21 +3395,23 @@ rxvt_term::selection_extend_colrow (int32_t col, int32_t row, int button3, int b
     {
       end_col = screen.tlen[selection.beg.row + TermWin.saveLines];
 
-      if (end_col != -1 && selection.beg.col > end_col)
-        {
-#if 1
-          selection.beg.col = ncol;
-#else
-          if (selection.beg.row != selection.end.row)
-            selection.beg.col = ncol;
-          else
-            selection.beg.col = selection.mark.col;
+      if (selection.beg.col > end_col
+          && end_col != -1
+#if ENABLE_FRILLS
+          && !selection.rect
 #endif
-        }
+         )
+        selection.beg.col = ncol;
 
       end_col = screen.tlen[selection.end.row + TermWin.saveLines];
 
-      if (end_col != -1 && selection.end.col > end_col)
+      if (
+          selection.end.col > end_col
+          && end_col != -1
+#if ENABLE_FRILLS
+          && !selection.rect
+#endif
+         )
         selection.end.col = ncol;
     }
   else if (selection.clicks == 2)
@@ -3216,8 +3419,8 @@ rxvt_term::selection_extend_colrow (int32_t col, int32_t row, int button3, int b
       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));
+      selection_delimit_word (UP, &selection.beg, &selection.beg);
+      selection_delimit_word (DN, &selection.end, &selection.end);
     }
   else if (selection.clicks == 3)
     {
@@ -3226,7 +3429,7 @@ rxvt_term::selection_extend_colrow (int32_t col, int32_t row, int button3, int b
         {
           int end_row;
 
-          selection_delimit_word (UP, & (selection.beg), & (selection.beg));
+          selection_delimit_word (UP, &selection.beg, &selection.beg);
           end_row = screen.tlen[selection.mark.row + TermWin.saveLines];
 
           for (end_row = selection.mark.row; end_row < TermWin.nrow; end_row++)
@@ -3247,6 +3450,7 @@ rxvt_term::selection_extend_colrow (int32_t col, int32_t row, int button3, int b
         {
           if (ROWCOL_IS_AFTER (selection.mark, selection.beg))
             selection.mark.col++;
+
           selection.beg.col = 0;
           selection.end.col = ncol;
         }
@@ -3265,25 +3469,35 @@ rxvt_term::selection_extend_colrow (int32_t col, int32_t row, int button3, int b
           selection.mark.col = selection.beg.col;
         }
     }
+
+#if ENABLE_FRILLS
+  if (selection.rect && selection.beg.col > selection.end.col)
+    SWAP_IT (selection.beg.col, selection.end.col, int);
+#endif
 }
 
 #if ENABLE_FRILLS
 void
 rxvt_term::selection_remove_trailing_spaces ()
 {
-  int32_t         end_col, end_row;
-  text_t         *stp;
+  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 = screen.text[end_row + TermWin.saveLines];
+
       while (--end_col >= 0)
         {
-          if (stp[end_col] != ' ' && stp[end_col] != '\t')
+          if (stp[end_col] != ' '
+              && stp[end_col] != '\t'
+              && stp[end_col] != NOCHAR)
             break;
         }
+
       if (end_col >= 0
           || screen.tlen[end_row - 1 + TermWin.saveLines] != -1)
         {
@@ -3291,9 +3505,11 @@ rxvt_term::selection_remove_trailing_spaces ()
           selection.end.row = end_row;
           break;
         }
+
       end_row--;
       end_col = TermWin.ncol;
     }
+
   if (selection.mark.row > selection.end.row)
     {
       selection.mark.row = selection.end.row;
@@ -3319,14 +3535,6 @@ rxvt_term::selection_rotate (int x, int y)
 
 /* ------------------------------------------------------------------------- */
 /*
- * 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
  */
@@ -3334,9 +3542,6 @@ void
 rxvt_term::selection_send (const XSelectionRequestEvent &rq)
 {
   XSelectionEvent ev;
-  XTextProperty ct;
-  XICCEncodingStyle style;
-  Atom target;
 
   ev.type = SelectionNotify;
   ev.property = None;
@@ -3348,22 +3553,21 @@ rxvt_term::selection_send (const XSelectionRequestEvent &rq)
 
   if (rq.target == xa[XA_TARGETS])
     {
-      Atom32 target_list[6];
-      Atom32 *target = target_list;
+      Atom target_list[6];
+      Atom *target = target_list;
 
-      *target++ = (Atom32) xa[XA_TARGETS];
-      *target++ = (Atom32) xa[XA_TIMESTAMP];
-      *target++ = (Atom32) XA_STRING;
-      *target++ = (Atom32) xa[XA_TEXT];
-      *target++ = (Atom32) xa[XA_COMPOUND_TEXT];
+      *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++ = (Atom32) xa[XA_UTF8_STRING];
+      *target++ = xa[XA_UTF8_STRING];
 #endif
 
       XChangeProperty (display->display, rq.requestor, rq.property, XA_ATOM,
-                       (8 * sizeof (target_list[0])), PropModeReplace,
-                       (unsigned char *)target_list,
-                       target - target_list);
+                       32, PropModeReplace,
+                       (unsigned char *)target_list, target - target_list);
       ev.property = rq.property;
     }
 #if TODO // TODO
@@ -3374,9 +3578,8 @@ rxvt_term::selection_send (const XSelectionRequestEvent &rq)
 #endif
   else if (rq.target == xa[XA_TIMESTAMP] && selection.text)
     {
-      XChangeProperty (display->display, rq.requestor, rq.property, XA_INTEGER,
-                       (8 * sizeof (Time)), PropModeReplace,
-                       (unsigned char *)&selection_time, 1);
+      XChangeProperty (display->display, rq.requestor, rq.property, rq.target,
+                       32, PropModeReplace, (unsigned char *)&selection_time, 1);
       ev.property = rq.property;
     }
   else if (rq.target == XA_STRING
@@ -3385,28 +3588,38 @@ rxvt_term::selection_send (const XSelectionRequestEvent &rq)
            || rq.target == xa[XA_UTF8_STRING]
           )
     {
+      XTextProperty ct;
+      Atom target = rq.target;
       short freect = 0;
       int selectlen;
       wchar_t *cl;
-
-      target = rq.target;
+      enum {
+        enc_string        = XStringStyle,
+        enc_text          = XStdICCTextStyle,
+        enc_compound_text = XCompoundTextStyle,
+#ifdef X_HAVE_UTF8_STRING
+        enc_utf8          = XUTF8StringStyle,
+#else
+        enc_utf8          = -1,
+#endif
+      } 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 = XStringStyle;
+        style = enc_string;
       else if (target == xa[XA_TEXT])
-        style = XStdICCTextStyle;
+        style = enc_text;
       else if (target == xa[XA_COMPOUND_TEXT])
-        style = XCompoundTextStyle;
-#if X_HAVE_UTF8_STRING
+        style = enc_compound_text;
+#if ENABLE_FRILLS
       else if (target == xa[XA_UTF8_STRING])
-        style = XUTF8StringStyle;
+        style = enc_utf8;
 #endif
       else
         {
           target = xa[XA_COMPOUND_TEXT];
-          style = XCompoundTextStyle;
+          style = enc_compound_text;
         }
 
       if (selection.text)
@@ -3420,19 +3633,31 @@ rxvt_term::selection_send (const XSelectionRequestEvent &rq)
           selectlen = 0;
         }
 
-      // Xwc doesn't handle iso-10646 in wchar_t gracefully, so maybe recode it
-      // manually for XUTF8StringStyle.
-      if (XwcTextListToTextProperty (display->display, &cl, 1, style, &ct) >= 0)
+#if ENABLE_FRILLS
+      // 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 (display->display, &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 (display->display, rq.requestor, rq.property,
-                       target, 8, PropModeReplace,
+                       ct.encoding, 8, PropModeReplace,
                        ct.value, (int)ct.nitems);
       ev.property = rq.property;
 
@@ -3509,7 +3734,7 @@ rxvt_term::scr_overlay_new (int x, int y, int w, int h)
       rend_t *rp = ov_rend[y] = new rend_t[w];
 
       text_t t0, t1, t2;
-      rend_t r = SET_BGCOLOR (SET_FGCOLOR (RS_None, Color_bg), Color_fg);
+      rend_t r = OVERLAY_RSTYLE;
 
       if (y == 0)
         t0 = 0x2554, t1 = 0x2550, t2 = 0x2557;
@@ -3553,7 +3778,7 @@ rxvt_term::scr_overlay_off ()
 void
 rxvt_term::scr_overlay_set (int x, int y, text_t text, rend_t rend)
 {
-  if (!ov_text || x >= ov_w - 2 || y >= ov_h)
+  if (!ov_text || x >= ov_w - 2 || y >= ov_h - 2)
     return;
 
   x++, y++;
@@ -3589,7 +3814,7 @@ rxvt_term::scr_swap_overlay ()
       for (int x = ov_w; x--; )
         {
           text_t t = *t1; *t1++ = *t2; *t2++ = t;
-          rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, TermWin.fontset->find_font (t, false));
+          rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, FONTSET (r)->find_font (t));
         }
     }
 }