1 /*---------------------------------------------------------------------------*
3 *---------------------------------------------------------------------------*
5 * Copyright (c) 1997-2001 Geoff Wing <gcw@pobox.com>
6 * Copyright (c) 2003-2007 Marc Lehmann <pcg@goof.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *--------------------------------------------------------------------------*/
24 * This file handles _all_ screen updates and selections
27 #include "../config.h" /* NECESSARY */
28 #include "rxvt.h" /* NECESSARY */
29 #include "rxvtperl.h" /* NECESSARY */
33 #include "salloc.C" // HACK, should be a seperate compile!
35 static inline void fill_text (text_t *start, text_t value, int len)
41 /* ------------------------------------------------------------------------- */
42 #define PROP_SIZE 256*1024
43 #define PASTE_SIZE 32768
44 #define TABSIZE 8 /* default tab size */
46 /* ------------------------------------------------------------------------- *
47 * GENERAL SCREEN AND SELECTION UPDATE ROUTINES *
48 * ------------------------------------------------------------------------- */
49 #define ZERO_SCROLLBACK() \
50 if (option (Opt_scrollTtyOutput)) \
52 #define CLEAR_SELECTION() \
53 selection.beg.row = selection.beg.col \
54 = selection.end.row = selection.end.col = 0
55 #define CLEAR_ALL_SELECTION() \
56 selection.beg.row = selection.beg.col \
57 = selection.mark.row = selection.mark.col \
58 = selection.end.row = selection.end.col = 0
60 #define ROW_AND_COL_IS_AFTER(A, B, C, D) \
61 (((A) > (C)) || (((A) == (C)) && ((B) > (D))))
62 #define ROW_AND_COL_IS_BEFORE(A, B, C, D) \
63 (((A) < (C)) || (((A) == (C)) && ((B) < (D))))
64 #define ROW_AND_COL_IN_ROW_AFTER(A, B, C, D) \
65 (((A) == (C)) && ((B) > (D)))
66 #define ROW_AND_COL_IN_ROW_AT_OR_AFTER(A, B, C, D) \
67 (((A) == (C)) && ((B) >= (D)))
68 #define ROW_AND_COL_IN_ROW_BEFORE(A, B, C, D) \
69 (((A) == (C)) && ((B) < (D)))
70 #define ROW_AND_COL_IN_ROW_AT_OR_BEFORE(A, B, C, D) \
71 (((A) == (C)) && ((B) <= (D)))
73 /* these must be row_col_t */
74 #define ROWCOL_IS_AFTER(X, Y) \
75 ROW_AND_COL_IS_AFTER ((X).row, (X).col, (Y).row, (Y).col)
76 #define ROWCOL_IS_BEFORE(X, Y) \
77 ROW_AND_COL_IS_BEFORE ((X).row, (X).col, (Y).row, (Y).col)
78 #define ROWCOL_IN_ROW_AFTER(X, Y) \
79 ROW_AND_COL_IN_ROW_AFTER ((X).row, (X).col, (Y).row, (Y).col)
80 #define ROWCOL_IN_ROW_BEFORE(X, Y) \
81 ROW_AND_COL_IN_ROW_BEFORE ((X).row, (X).col, (Y).row, (Y).col)
82 #define ROWCOL_IN_ROW_AT_OR_AFTER(X, Y) \
83 ROW_AND_COL_IN_ROW_AT_OR_AFTER ((X).row, (X).col, (Y).row, (Y).col)
84 #define ROWCOL_IN_ROW_AT_OR_BEFORE(X, Y) \
85 ROW_AND_COL_IN_ROW_AT_OR_BEFORE ((X).row, (X).col, (Y).row, (Y).col)
88 * CLEAR_CHARS: clear <num> chars starting from pixel position <x,y>
90 #define CLEAR_CHARS(x, y, num) \
92 XClearArea (dpy, vt, x, y, \
93 (unsigned int)Width2Pixel (num), \
94 (unsigned int)Height2Pixel (1), False)
96 /* ------------------------------------------------------------------------- *
97 * SCREEN `COMMON' ROUTINES *
98 * ------------------------------------------------------------------------- */
100 /* Fill part/all of a line with blanks. */
102 rxvt_term::scr_blank_line (line_t &l, unsigned int col, unsigned int width, rend_t efs) const NOTHROW
113 efs &= ~RS_baseattrMask; // remove italic etc. fontstyles
114 efs = SET_FONT (efs, FONTSET (efs)->find_font (' '));
116 text_t *et = l.t + col;
117 rend_t *er = l.r + col;
126 /* ------------------------------------------------------------------------- */
127 /* Fill a full line with blanks - make sure it is allocated first */
129 rxvt_term::scr_blank_screen_mem (line_t &l, rend_t efs) const NOTHROW
131 scr_blank_line (l, 0, ncol, efs);
137 // nuke a single wide character at the given column
139 rxvt_term::scr_kill_char (line_t &l, int col) const NOTHROW
142 while (col > 0 && l.t[col] == NOCHAR)
145 rend_t rend = l.r[col] & ~RS_baseattrMask;
146 rend = SET_FONT (rend, FONTSET (rend)->find_font (' '));
153 } while (col < ncol && l.t[col] == NOCHAR);
156 /* ------------------------------------------------------------------------- *
157 * SCREEN INITIALISATION *
158 * ------------------------------------------------------------------------- */
161 rxvt_term::scr_reset ()
172 if (ncol == prev_ncol && nrow == prev_nrow)
175 // we need at least two lines for wrapping to work correctly
176 while (nrow + saveLines < 2)
186 int prev_total_rows = prev_nrow + saveLines;
187 total_rows = nrow + saveLines;
190 screen.bscroll = nrow - 1;
195 * first time called so just malloc everything: don't rely on realloc
200 talloc = new rxvt_salloc (ncol * sizeof (text_t));
201 ralloc = new rxvt_salloc (ncol * sizeof (rend_t));
203 row_buf = (line_t *)rxvt_calloc (total_rows + nrow, sizeof (line_t));
204 drawn_buf = (line_t *)rxvt_calloc (nrow , sizeof (line_t));
205 swap_buf = (line_t *)rxvt_calloc (nrow , sizeof (line_t));
207 for (int row = nrow; row--; )
209 scr_blank_screen_mem (ROW (row), DEFAULT_RSTYLE);
210 scr_blank_screen_mem (swap_buf [row], DEFAULT_RSTYLE);
211 scr_blank_screen_mem (drawn_buf[row], DEFAULT_RSTYLE);
214 memset (charsets, 'B', sizeof (charsets));
215 rstyle = DEFAULT_RSTYLE;
216 screen.flags = Screen_DefaultFlags;
217 screen.cur.row = screen.cur.col = 0;
219 current_screen = PRIMARY;
223 swap.flags = Screen_DefaultFlags;
224 swap.cur.row = swap.cur.col = 0;
226 current_screen = SECONDARY;
228 current_screen = PRIMARY;
231 selection.text = NULL;
233 selection.op = SELECTION_CLEAR;
234 selection.screen = PRIMARY;
235 selection.clicks = 0;
236 selection.clip_text = NULL;
237 selection.clip_len = 0;
242 * add or delete rows as appropriate
245 rxvt_salloc *old_ta = talloc; talloc = new rxvt_salloc (ncol * sizeof (text_t));
246 rxvt_salloc *old_ra = ralloc; ralloc = new rxvt_salloc (ncol * sizeof (rend_t));
249 if (nrow < prev_nrow)
251 for (int row = nrow; row < prev_nrow; row++)
253 lfree (swap_buf [row]);
254 lfree (drawn_buf[row]);
259 drawn_buf = (line_t *)rxvt_realloc (drawn_buf, nrow * sizeof (line_t));
260 swap_buf = (line_t *)rxvt_realloc (swap_buf , nrow * sizeof (line_t));
262 for (int row = min (nrow, prev_nrow); row--; )
264 lresize (drawn_buf[row]);
265 lresize (swap_buf [row]);
268 for (int row = prev_nrow; row < nrow; row++)
270 swap_buf [row].clear (); scr_blank_screen_mem (swap_buf [row], DEFAULT_RSTYLE);
271 drawn_buf[row].clear (); scr_blank_screen_mem (drawn_buf[row], DEFAULT_RSTYLE);
274 line_t *old_buf = row_buf;
275 row_buf = (line_t *)rxvt_calloc (total_rows + nrow, sizeof (line_t));
277 int p = MOD (term_start + prev_nrow, prev_total_rows); // previous row
278 int pend = MOD (term_start + top_row , prev_total_rows);
279 int q = total_rows; // rewrapped row
283 // Re-wrap lines. This is rather ugly, possibly because I am too dumb
284 // to come up with a lean and mean algorithm.
285 // TODO: maybe optimise when width didn't change
287 row_col_t ocur = screen.cur;
288 ocur.row = MOD (term_start + ocur.row, prev_total_rows);
292 p = MOD (p - 1, prev_total_rows);
293 assert (old_buf [MOD (p, prev_total_rows)].t);
295 int llen = old_buf [MOD (p, prev_total_rows)].l;
297 while (p != pend && old_buf [MOD (p - 1, prev_total_rows)].is_longer ())
299 p = MOD (p - 1, prev_total_rows);
305 int qlines = max (0, (llen - 1) / ncol) + 1;
307 // drop partial lines completely
316 // re-assemble the full line by destination lines
317 for (int qrow = q; qlines--; qrow++)
319 qline = row_buf + qrow;
322 qline->is_longer (1);
326 // see below for cursor adjustment rationale
328 screen.cur.row = q - (total_rows - nrow);
330 // fill a single destination line
331 while (lofs < llen && qcol < ncol)
333 int prow = lofs / prev_ncol;
334 int pcol = lofs % prev_ncol;
336 prow = MOD (p + prow, prev_total_rows);
338 // we only adjust the cursor _row_ and put it into
339 // the topmost line of "long line" it was in, as
340 // this seems to upset applications/shells/readline
342 if (prow == ocur.row)
343 screen.cur.row = q - (total_rows - nrow);
345 line_t &pline = old_buf [prow];
347 int len = min (min (prev_ncol - pcol, ncol - qcol), llen - lofs);
349 memcpy (qline->t + qcol, pline.t + pcol, len * sizeof (text_t));
350 memcpy (qline->r + qcol, pline.r + pcol, len * sizeof (rend_t));
357 qline->l = llen ? MOD (llen - 1, ncol) + 1 : 0;
358 qline->is_longer (0);
359 scr_blank_line (*qline, qline->l, ncol - qline->l, DEFAULT_RSTYLE);
361 while (p != pend && q > 0);
363 term_start = total_rows - nrow;
364 top_row = q - term_start;
366 // make sure all terminal lines exist
368 scr_blank_screen_mem (ROW (--top_row), DEFAULT_RSTYLE);
372 // if no scrollback exists (yet), wing, instead of wrap
374 for (int row = min (nrow, prev_nrow); row--; )
376 line_t &pline = old_buf [MOD (term_start + row, prev_total_rows)];
377 line_t &qline = row_buf [row];
383 for (int row = prev_nrow; row < nrow; row++)
385 row_buf [row].clear (); scr_blank_screen_mem (row_buf [row], DEFAULT_RSTYLE);
395 clamp_it (screen.cur.row, 0, nrow - 1);
396 clamp_it (screen.cur.col, 0, ncol - 1);
400 tabs = (char *)rxvt_malloc (ncol);
402 for (int col = ncol; --col; )
403 tabs [col] = col % TABSIZE == 0;
405 CLEAR_ALL_SELECTION ();
412 HOOK_INVOKE ((this, HOOK_RESET, DT_END));
415 /* ------------------------------------------------------------------------- */
417 * Free everything. That way malloc debugging can find leakage.
420 rxvt_term::scr_release () NOTHROW
424 delete talloc; talloc = 0;
425 delete ralloc; ralloc = 0;
430 row_buf = 0; // signal that we freed all the arrays above
437 /* ------------------------------------------------------------------------- */
442 rxvt_term::scr_poweron ()
445 prev_nrow = prev_ncol = 0;
455 rxvt_term::scr_soft_reset ()
457 /* only affects modes, nothing drastic such as clearing the screen */
462 if (current_screen != PRIMARY)
465 scr_scroll_region (0, MAX_ROWS - 1);
466 scr_rendition (0, ~RS_None);
470 /* ------------------------------------------------------------------------- *
471 * PROCESS SCREEN COMMANDS *
472 * ------------------------------------------------------------------------- */
474 * Save and Restore cursor
475 * XTERM_SEQ: Save cursor : ESC 7
476 * XTERM_SEQ: Restore cursor: ESC 8
479 rxvt_term::scr_cursor (cursor_mode mode) NOTHROW
483 #if NSCREENS && !defined(NO_SECONDARY_SCREEN_CURSOR)
484 if (current_screen == SECONDARY)
493 s->s_cur.row = screen.cur.row;
494 s->s_cur.col = screen.cur.col;
495 s->s_rstyle = rstyle;
496 s->s_charset = screen.charset;
497 s->s_charset_char = charsets[screen.charset];
502 screen.cur.row = s->s_cur.row;
503 screen.cur.col = s->s_cur.col;
504 screen.flags &= ~Screen_WrapNext;
505 rstyle = s->s_rstyle;
506 screen.charset = s->s_charset;
507 charsets[screen.charset] = s->s_charset_char;
512 /* boundary check in case screen size changed between SAVE and RESTORE */
513 min_it (s->cur.row, nrow - 1);
514 min_it (s->cur.col, ncol - 1);
515 assert (s->cur.row >= 0);
516 assert (s->cur.col >= 0);
520 rxvt_term::scr_swap_screen ()
522 if (!option (Opt_secondaryScreen))
525 for (int i = prev_nrow; i--; )
526 ::swap (ROW(i), swap_buf [i]);
528 ::swap (screen.cur, swap.cur);
530 screen.cur.row = clamp (screen.cur.row, 0, prev_nrow - 1);
531 screen.cur.col = clamp (screen.cur.col, 0, prev_ncol - 1);
534 /* ------------------------------------------------------------------------- */
536 * Swap between primary and secondary screens
537 * XTERM_SEQ: Primary screen : ESC [ ? 4 7 h
538 * XTERM_SEQ: Secondary screen: ESC [ ? 4 7 l
541 rxvt_term::scr_change_screen (int scrn)
543 if (scrn == current_screen)
549 selection_check (2); /* check for boundary cross */
551 current_screen = scrn;
554 if (option (Opt_secondaryScreen))
560 ::swap (screen.charset, swap.charset);
561 ::swap (screen.flags, swap.flags);
562 screen.flags |= Screen_VisibleCursor;
563 swap.flags |= Screen_VisibleCursor;
567 if (option (Opt_secondaryScroll))
568 scr_scroll_text (0, prev_nrow - 1, prev_nrow);
571 // clear WrapNext indicator, solidifying position on next line
573 rxvt_term::scr_do_wrap () NOTHROW
575 if (!(screen.flags & Screen_WrapNext))
578 screen.flags &= ~Screen_WrapNext;
582 if (screen.cur.row == screen.bscroll)
583 scr_scroll_text (screen.tscroll, screen.bscroll, 1);
584 else if (screen.cur.row < nrow - 1)
588 /* ------------------------------------------------------------------------- */
590 * Change the colour for following text
593 rxvt_term::scr_color (unsigned int color, int fgbg) NOTHROW
595 if (!IN_RANGE_INC (color, minCOLOR, maxTermCOLOR))
598 if (fgbg == Color_fg)
599 rstyle = SET_FGCOLOR (rstyle, color);
601 rstyle = SET_BGCOLOR (rstyle, color);
604 /* ------------------------------------------------------------------------- */
606 * Change the rendition style for following text
609 rxvt_term::scr_rendition (int set, int style) NOTHROW
613 else if (style == ~RS_None)
614 rstyle = DEFAULT_RSTYLE;
619 /* ------------------------------------------------------------------------- */
621 * Scroll text between <row1> and <row2> inclusive, by <count> lines
622 * count positive ==> scroll up
623 * count negative ==> scroll down
626 rxvt_term::scr_scroll_text (int row1, int row2, int count) NOTHROW
628 if (count == 0 || (row1 > row2))
636 && (current_screen == PRIMARY || option (Opt_secondaryScroll)))
638 top_row = max (top_row - count, -saveLines);
640 // scroll everything up 'count' lines
641 term_start = (term_start + count) % total_rows;
643 // sever bottommost line
645 line_t &l = ROW(row2 - count);
650 // erase newly scrolled-in lines
651 for (int i = count; i--; )
653 line_t &l = ROW(nrow - 1 - i);
655 // optimize if already cleared, can be significant on slow machines
656 // could be rolled into scr_blank_screen_mem
657 if (l.r && l.l < ncol - 1 && !((l.r[l.l + 1] ^ rstyle) & (RS_fgMask | RS_bgMask)))
659 scr_blank_line (l, 0, l.l, rstyle);
664 scr_blank_screen_mem (l, rstyle);
667 // now copy lines below the scroll region bottom to the
668 // bottom of the screen again, so they look as if they
670 for (int i = nrow; --i > row2; )
672 line_t &l1 = ROW(i - count);
679 // move and/or clear selection, if any
680 if (selection.op && current_screen == selection.screen)
682 selection.beg.row -= count;
683 selection.end.row -= count;
684 selection.mark.row -= count;
686 if (selection.beg.row < top_row
687 || selection.end.row < top_row
688 || selection.mark.row < top_row)
690 CLEAR_ALL_SELECTION ();
691 selection.op = SELECTION_CLEAR;
695 // finally move the view window, if desired
696 if (option (Opt_scrollWithBuffer)
698 && view_start != -saveLines)
699 scr_page (UP, count);
701 if (SHOULD_INVOKE (HOOK_SCROLL_BACK))
702 HOOK_INVOKE ((this, HOOK_SCROLL_BACK, DT_INT, count, DT_INT, top_row, DT_END));
706 if (selection.op && current_screen == selection.screen)
708 if ((selection.beg.row < row1 && selection.end.row > row1)
709 || (selection.beg.row < row2 && selection.end.row > row2)
710 || (selection.beg.row - count < row1 && selection.beg.row >= row1)
711 || (selection.beg.row - count > row2 && selection.beg.row <= row2)
712 || (selection.end.row - count < row1 && selection.end.row >= row1)
713 || (selection.end.row - count > row2 && selection.end.row <= row2))
715 CLEAR_ALL_SELECTION ();
716 selection.op = SELECTION_CLEAR;
718 else if (selection.end.row >= row1 && selection.end.row <= row2)
720 /* move selected region too */
721 selection.beg.row -= count;
722 selection.end.row -= count;
723 selection.mark.row -= count;
729 // use a simple and robust scrolling algorithm, this
730 // part of scr_scroll_text is not time-critical.
732 int rows = row2 - row1 + 1;
734 min_it (count, rows);
736 line_t *temp_buf = row_buf + total_rows;
738 for (int row = 0; row < rows; row++)
740 temp_buf [row] = ROW(row1 + (row + count + rows) % rows);
742 if (!IN_RANGE_EXC (row + count, 0, rows))
743 scr_blank_screen_mem (temp_buf [row], rstyle);
746 for (int row = 0; row < rows; row++)
747 ROW(row1 + row) = temp_buf [row];
753 /* ------------------------------------------------------------------------- */
755 * Add text given in <str> of length <len> to screen struct
758 rxvt_term::scr_add_lines (const wchar_t *str, int len, int minlines) NOTHROW
760 if (len <= 0) /* sanity */
763 unsigned char checksel;
765 int ncol = this->ncol;
766 const wchar_t *strend = str + len;
773 minlines += screen.cur.row - screen.bscroll;
774 min_it (minlines, screen.cur.row - top_row);
777 && screen.tscroll == 0
778 && screen.bscroll == nrow - 1)
780 /* _at least_ this many lines need to be scrolled */
781 scr_scroll_text (screen.tscroll, screen.bscroll, minlines);
782 screen.cur.row -= minlines;
786 assert (screen.cur.col < ncol);
787 assert (screen.cur.row < nrow
788 && screen.cur.row >= top_row);
789 int row = screen.cur.row;
791 checksel = selection.op && current_screen == selection.screen ? 1 : 0;
793 line_t *line = &ROW(row);
797 c = (unicode_t)*str++; // convert to rxvt-unicodes representation
799 if (expect_false (c < 0x20))
802 max_it (line->l, screen.cur.col);
804 screen.flags &= ~Screen_WrapNext;
806 if (screen.cur.row == screen.bscroll)
807 scr_scroll_text (screen.tscroll, screen.bscroll, 1);
808 else if (screen.cur.row < (nrow - 1))
809 row = ++screen.cur.row;
811 line = &ROW(row); /* _must_ refresh */
816 max_it (line->l, screen.cur.col);
818 screen.flags &= ~Screen_WrapNext;
829 checksel /* see if we're writing within selection */
830 && !ROWCOL_IS_BEFORE (screen.cur, selection.beg)
831 && ROWCOL_IS_BEFORE (screen.cur, selection.end)
836 * If we wrote anywhere in the selected area, kill the selection
837 * XXX: should we kill the mark too? Possibly, but maybe that
838 * should be a similar check.
843 if (expect_false (screen.flags & Screen_WrapNext))
850 row = screen.cur.row;
851 line = &ROW(row); /* _must_ refresh */
854 // some utf-8 decoders "decode" surrogate characters: let's fix this.
855 if (expect_false (IN_RANGE_INC (c, 0xd800, 0xdfff)))
858 // rely on wcwidth to tell us the character width, do wcwidth before
859 // further replacements, as wcwidth might return -1 for the line
860 // drawing characters below as they might be invalid in the current
862 int width = WCWIDTH (c);
864 if (expect_false (charsets [screen.charset] == '0')) // DEC SPECIAL
866 // vt100 special graphics and line drawing
867 // 5f-7e standard vt100
868 // 40-5e rxvt extension for extra curses acs chars
869 static uint16_t vt100_0[62] = { // 41 .. 7e
870 0x2191, 0x2193, 0x2192, 0x2190, 0x2588, 0x259a, 0x2603, // 41-47 hi mr. snowman!
871 0, 0, 0, 0, 0, 0, 0, 0, // 48-4f
872 0, 0, 0, 0, 0, 0, 0, 0, // 50-57
873 0, 0, 0, 0, 0, 0, 0, 0x0020, // 58-5f
874 0x25c6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1, // 60-67
875 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba, // 68-6f
876 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c, // 70-77
877 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, // 78-7e
880 if (c >= 0x41 && c <= 0x7e && vt100_0[c - 0x41])
882 c = vt100_0[c - 0x41];
883 width = 1; // vt100 line drawing characters are always single-width
887 if (expect_false (screen.flags & Screen_Insert))
888 scr_insdel_chars (width, INSERT);
893 // trim characters we can't store directly :(
895 # if ENABLE_COMBINING
896 c = rxvt_composite.compose (c); // map to lower 16 bits
902 // nuke the character at this position, if required
904 line->t[screen.cur.col] == NOCHAR
905 || (screen.cur.col < ncol - 1
906 && line->t[screen.cur.col + 1] == NOCHAR)
908 scr_kill_char (*line, screen.cur.col);
910 rend_t rend = SET_FONT (rstyle, FONTSET (rstyle)->find_font (c));
912 // if the character doesn't fit into the remaining columns...
913 if (expect_false (screen.cur.col > ncol - width && ncol >= width))
915 // ... artificially enlargen the previous one
917 // and try the same character next loop iteration
925 line->t[screen.cur.col] = c;
926 line->r[screen.cur.col] = rend;
928 if (expect_true (screen.cur.col < ncol - 1))
933 if (screen.flags & Screen_Autowrap)
934 screen.flags |= Screen_WrapNext;
940 while (expect_false (--width > 0));
942 // pad with spaces when overwriting wide character with smaller one
943 if (expect_false (!width))
947 for (int c = screen.cur.col; c < ncol && line->t[c] == NOCHAR; c++)
957 if (c != 0xfeff) // ignore BOM
959 // handle combining characters
960 // we just tag the accent on the previous on-screen character.
961 // this is arguably not correct, but also arguably not wrong.
962 // we don't handle double-width characters nicely yet.
967 if (screen.cur.col > 0)
970 tp = line->t + screen.cur.col - 1;
971 rp = line->r + screen.cur.col - 1;
973 else if (screen.cur.row > 0
974 && ROW(screen.cur.row - 1).is_longer ())
976 linep = &ROW(screen.cur.row - 1);
977 tp = line->t + ncol - 1;
978 rp = line->r + ncol - 1;
985 while (*tp == NOCHAR && tp > linep->t)
988 // first try to find a precomposed character
989 unicode_t n = rxvt_compose (*tp, c);
991 n = rxvt_composite.compose (*tp, c);
994 *rp = SET_FONT (*rp, FONTSET (*rp)->find_font (*tp));
1000 max_it (line->l, screen.cur.col);
1002 assert (screen.cur.row >= 0);
1005 /* ------------------------------------------------------------------------- */
1007 * Process Backspace. Move back the cursor back a position, wrap if have to
1011 rxvt_term::scr_backspace () NOTHROW
1013 if (screen.cur.col == 0)
1015 if (screen.cur.row > 0)
1017 #ifdef TERMCAP_HAS_BW
1018 screen.cur.col = ncol - 1;
1026 scr_gotorc (0, -1, RELATIVE);
1029 /* ------------------------------------------------------------------------- */
1031 * Process Horizontal Tab
1032 * count: +ve = forward; -ve = backwards
1036 rxvt_term::scr_tab (int count, bool ht) NOTHROW
1041 i = x = screen.cur.col;
1047 line_t &l = ROW(screen.cur.row);
1048 rend_t base_rend = l.r[i];
1049 ht &= l.t[i] == ' ';
1051 for (; ++i < ncol; )
1061 && RS_SAME (l.r[i], base_rend);
1066 // store horizontal tab commands as characters inside the text
1067 // buffer so they can be selected and pasted.
1068 if (ht && option (Opt_pastableTabs))
1070 base_rend = SET_FONT (base_rend, 0);
1086 else /* if (count < 0) */
1100 if (x != screen.cur.col)
1101 scr_gotorc (0, x, R_RELATIVE);
1104 /* ------------------------------------------------------------------------- */
1106 * Process DEC Back Index
1108 * Move cursor left in row. If we're at the left boundary, shift everything
1109 * in that row right. Clear left column.
1113 rxvt_term::scr_backindex () NOTHROW
1115 if (screen.cur.col > 0)
1116 scr_gotorc (0, -1, R_RELATIVE | C_RELATIVE);
1118 scr_insdel_chars (1, INSERT);
1121 /* ------------------------------------------------------------------------- */
1123 * Process DEC Forward Index
1125 * Move cursor right in row. If we're at the right boundary, shift everything
1126 * in that row left. Clear right column.
1130 rxvt_term::scr_forwardindex () NOTHROW
1132 if (screen.cur.col < ncol - 1)
1133 scr_gotorc (0, 1, R_RELATIVE | C_RELATIVE);
1136 line_t &l = ROW(screen.cur.row);
1141 scr_gotorc (0, 0, R_RELATIVE);
1142 scr_insdel_chars (1, DELETE);
1143 scr_gotorc (0, ncol - 1, R_RELATIVE);
1148 /* ------------------------------------------------------------------------- */
1153 rxvt_term::scr_gotorc (int row, int col, int relative) NOTHROW
1158 screen.cur.col = relative & C_RELATIVE ? screen.cur.col + col : col;
1159 clamp_it (screen.cur.col, 0, ncol - 1);
1161 screen.flags &= ~Screen_WrapNext;
1163 if (relative & R_RELATIVE)
1167 if (screen.cur.row <= screen.bscroll
1168 && (screen.cur.row + row) > screen.bscroll)
1169 screen.cur.row = screen.bscroll;
1171 screen.cur.row += row;
1175 if (screen.cur.row >= screen.tscroll
1176 && (screen.cur.row + row) < screen.tscroll)
1177 screen.cur.row = screen.tscroll;
1179 screen.cur.row += row;
1184 if (screen.flags & Screen_Relative)
1186 /* relative origin mode */
1187 screen.cur.row = row + screen.tscroll;
1188 min_it (screen.cur.row, screen.bscroll);
1191 screen.cur.row = row;
1194 clamp_it (screen.cur.row, 0, nrow - 1);
1197 /* ------------------------------------------------------------------------- */
1199 * direction should be UP or DN
1202 rxvt_term::scr_index (enum page_dirn direction) NOTHROW
1209 dirn = ((direction == UP) ? 1 : -1);
1211 screen.flags &= ~Screen_WrapNext;
1213 if ((screen.cur.row == screen.bscroll && direction == UP)
1214 || (screen.cur.row == screen.tscroll && direction == DN))
1215 scr_scroll_text (screen.tscroll, screen.bscroll, dirn);
1217 screen.cur.row += dirn;
1219 clamp_it (screen.cur.row, 0, nrow - 1);
1220 selection_check (0);
1223 /* ------------------------------------------------------------------------- */
1225 * Erase part or whole of a line
1226 * XTERM_SEQ: Clear line to right: ESC [ 0 K
1227 * XTERM_SEQ: Clear line to left : ESC [ 1 K
1228 * XTERM_SEQ: Clear whole line : ESC [ 2 K
1229 * extension: clear to right unless wrapped: ESC [ 3 K
1232 rxvt_term::scr_erase_line (int mode) NOTHROW
1234 unsigned int col, num;
1239 selection_check (1);
1241 line_t &line = ROW(screen.cur.row);
1249 if (screen.flags & Screen_WrapNext)
1254 case 0: /* erase to end of line */
1255 col = screen.cur.col;
1257 min_it (line.l, col);
1259 if (ROWCOL_IN_ROW_AT_OR_AFTER (selection.beg, screen.cur)
1260 || ROWCOL_IN_ROW_AT_OR_AFTER (selection.end, screen.cur))
1264 case 1: /* erase to beginning of line */
1266 num = screen.cur.col + 1;
1268 if (ROWCOL_IN_ROW_AT_OR_BEFORE (selection.beg, screen.cur)
1269 || ROWCOL_IN_ROW_AT_OR_BEFORE (selection.end, screen.cur))
1273 case 2: /* erase whole line */
1277 if (selection.beg.row <= screen.cur.row
1278 && selection.end.row >= screen.cur.row)
1285 scr_blank_line (line, col, num, rstyle);
1288 /* ------------------------------------------------------------------------- */
1290 * Erase part of whole of the screen
1291 * XTERM_SEQ: Clear screen after cursor : ESC [ 0 J
1292 * XTERM_SEQ: Clear screen before cursor: ESC [ 1 J
1293 * XTERM_SEQ: Clear whole screen : ESC [ 2 J
1296 rxvt_term::scr_erase_screen (int mode) NOTHROW
1308 case 0: /* erase to end of screen */
1309 selection_check (1);
1311 row = screen.cur.row + 1; /* possible OOB */
1314 case 1: /* erase to beginning of screen */
1315 selection_check (3);
1318 num = screen.cur.row;
1320 case 2: /* erase whole screen */
1321 selection_check (3);
1329 if (selection.op && current_screen == selection.screen
1330 && ((selection.beg.row >= row && selection.beg.row <= row + num)
1331 || (selection.end.row >= row
1332 && selection.end.row <= row + num)))
1335 if (row >= nrow) /* Out Of Bounds */
1338 min_it (num, nrow - row);
1340 // TODO: the code below does not work when view_start != 0
1341 // the workaround is to disable the clear and use a normal refresh
1342 // when view_start != 0. mysterious.
1343 if (rstyle & (RS_RVid | RS_Uline))
1344 ren = (rend_t) ~RS_None;
1345 else if (GET_BASEBG (rstyle) == Color_bg)
1347 ren = DEFAULT_RSTYLE;
1349 if (mapped && !view_start)
1350 XClearArea (dpy, vt, 0,
1351 Row2Pixel (row - view_start), (unsigned int)width,
1352 (unsigned int)Height2Pixel (num), False);
1356 ren = rstyle & (RS_fgMask | RS_bgMask);
1358 if (mapped && !view_start)
1360 gcvalue.foreground = pix_colors[bgcolor_of (rstyle)];
1361 XChangeGC (dpy, gc, GCForeground, &gcvalue);
1362 XFillRectangle (dpy, vt, gc,
1363 0, Row2Pixel (row - view_start),
1364 (unsigned int)width,
1365 (unsigned int)Height2Pixel (num));
1366 gcvalue.foreground = pix_colors[Color_fg];
1367 XChangeGC (dpy, gc, GCForeground, &gcvalue);
1371 for (; num--; row++)
1373 scr_blank_screen_mem (ROW(row), rstyle);
1376 scr_blank_line (drawn_buf [row], 0, ncol, ren);
1382 rxvt_term::scr_erase_savelines () NOTHROW
1391 /* ------------------------------------------------------------------------- */
1393 * Fill the screen with `E's
1394 * XTERM_SEQ: Screen Alignment Test: ESC # 8
1397 rxvt_term::scr_E () NOTHROW
1405 selection_check (3);
1407 fs = SET_FONT (rstyle, FONTSET (rstyle)->find_font ('E'));
1408 for (int row = nrow; row--; )
1410 line_t &line = ROW(row);
1412 fill_text (line.t, 'E', ncol);
1413 rend_t *r1 = line.r;
1415 for (int j = ncol; j--; )
1423 /* ------------------------------------------------------------------------- */
1425 * Insert/Delete <count> lines
1428 rxvt_term::scr_insdel_lines (int count, int insdel) NOTHROW
1434 selection_check (1);
1436 if (screen.cur.row > screen.bscroll)
1439 end = screen.bscroll - screen.cur.row + 1;
1442 if (insdel == DELETE)
1444 else if (insdel == INSERT)
1450 scr_scroll_text (screen.cur.row, screen.bscroll, insdel * count);
1453 /* ------------------------------------------------------------------------- */
1455 * Insert/Delete <count> characters from the current position
1458 rxvt_term::scr_insdel_chars (int count, int insdel) NOTHROW
1468 selection_check (1);
1469 min_it (count, ncol - screen.cur.col);
1471 int row = screen.cur.row;
1473 line_t *line = &ROW(row);
1476 line->is_longer (0);
1478 // nuke wide spanning the start
1479 if (line->t[screen.cur.col] == NOCHAR)
1480 scr_kill_char (*line, screen.cur.col);
1485 line->l = min (line->l + count, ncol);
1487 if (line->t[screen.cur.col] == NOCHAR)
1488 scr_kill_char (*line, screen.cur.col);
1490 for (int col = ncol - 1; (col - count) >= screen.cur.col; col--)
1492 line->t[col] = line->t[col - count];
1493 line->r[col] = line->r[col - count];
1496 if (selection.op && current_screen == selection.screen
1497 && ROWCOL_IN_ROW_AT_OR_AFTER (selection.beg, screen.cur))
1499 if (selection.end.row != screen.cur.row
1500 || (selection.end.col + count >= ncol))
1504 /* shift selection */
1505 selection.beg.col += count;
1506 selection.mark.col += count; /* XXX: yes? */
1507 selection.end.col += count;
1511 scr_blank_line (*line, screen.cur.col, count, rstyle);
1515 screen.cur.col += count; /* don't worry if > ncol */
1516 selection_check (1);
1517 screen.cur.col -= count;
1519 // nuke wide char after the end
1520 if (screen.cur.col + count < ncol && line->t[screen.cur.col + count] == NOCHAR)
1521 scr_kill_char (*line, screen.cur.col + count);
1523 scr_blank_line (*line, screen.cur.col, count, rstyle);
1527 line->l = max (line->l - count, 0);
1529 // nuke wide char spanning the end
1530 if (screen.cur.col + count < ncol && line->t[screen.cur.col + count] == NOCHAR)
1531 scr_kill_char (*line, screen.cur.col + count);
1533 for (int col = screen.cur.col; (col + count) < ncol; col++)
1535 line->t[col] = line->t[col + count];
1536 line->r[col] = line->r[col + count];
1539 scr_blank_line (*line, ncol - count, count, rstyle);
1541 if (selection.op && current_screen == selection.screen
1542 && ROWCOL_IN_ROW_AT_OR_AFTER (selection.beg, screen.cur))
1544 if (selection.end.row != screen.cur.row
1545 || (screen.cur.col >= selection.beg.col - count)
1546 || selection.end.col >= ncol)
1550 /* shift selection */
1551 selection.beg.col -= count;
1552 selection.mark.col -= count; /* XXX: yes? */
1553 selection.end.col -= count;
1561 /* ------------------------------------------------------------------------- */
1563 * Set the scrolling region
1564 * XTERM_SEQ: Set region <top> - <bot> inclusive: ESC [ <top> ; <bot> r
1567 rxvt_term::scr_scroll_region (int top, int bot) NOTHROW
1570 min_it (bot, nrow - 1);
1575 screen.tscroll = top;
1576 screen.bscroll = bot;
1577 scr_gotorc (0, 0, 0);
1580 /* ------------------------------------------------------------------------- */
1582 * Make the cursor visible/invisible
1583 * XTERM_SEQ: Make cursor visible : ESC [ ? 25 h
1584 * XTERM_SEQ: Make cursor invisible: ESC [ ? 25 l
1587 rxvt_term::scr_cursor_visible (int mode) NOTHROW
1592 screen.flags |= Screen_VisibleCursor;
1594 screen.flags &= ~Screen_VisibleCursor;
1597 /* ------------------------------------------------------------------------- */
1599 * Set/unset automatic wrapping
1600 * XTERM_SEQ: Set Wraparound : ESC [ ? 7 h
1601 * XTERM_SEQ: Unset Wraparound: ESC [ ? 7 l
1604 rxvt_term::scr_autowrap (int mode) NOTHROW
1607 screen.flags |= Screen_Autowrap;
1609 screen.flags &= ~(Screen_Autowrap | Screen_WrapNext);
1612 /* ------------------------------------------------------------------------- */
1614 * Set/unset margin origin mode
1615 * Absolute mode: line numbers are counted relative to top margin of screen
1616 * and the cursor can be moved outside the scrolling region.
1617 * Relative mode: line numbers are relative to top margin of scrolling region
1618 * and the cursor cannot be moved outside.
1619 * XTERM_SEQ: Set Absolute: ESC [ ? 6 h
1620 * XTERM_SEQ: Set Relative: ESC [ ? 6 l
1623 rxvt_term::scr_relative_origin (int mode) NOTHROW
1626 screen.flags |= Screen_Relative;
1628 screen.flags &= ~Screen_Relative;
1630 scr_gotorc (0, 0, 0);
1633 /* ------------------------------------------------------------------------- */
1635 * Set insert/replace mode
1636 * XTERM_SEQ: Set Insert mode : ESC [ ? 4 h
1637 * XTERM_SEQ: Set Replace mode: ESC [ ? 4 l
1640 rxvt_term::scr_insert_mode (int mode) NOTHROW
1643 screen.flags |= Screen_Insert;
1645 screen.flags &= ~Screen_Insert;
1648 /* ------------------------------------------------------------------------- */
1651 * XTERM_SEQ: Set tab at current column : ESC H
1652 * XTERM_SEQ: Clear tab at current column: ESC [ 0 g
1653 * XTERM_SEQ: Clear all tabs : ESC [ 3 g
1656 rxvt_term::scr_set_tab (int mode) NOTHROW
1659 memset (tabs, 0, ncol);
1660 else if (screen.cur.col < ncol)
1661 tabs [screen.cur.col] = !!mode;
1664 /* ------------------------------------------------------------------------- */
1666 * Set reverse/normal video
1667 * XTERM_SEQ: Reverse video: ESC [ ? 5 h
1668 * XTERM_SEQ: Normal video : ESC [ ? 5 l
1671 rxvt_term::scr_rvideo_mode (bool on) NOTHROW
1679 if (rvideo_state != on)
1683 ::swap (pix_colors[Color_fg], pix_colors[Color_bg]);
1684 #ifdef HAVE_BG_PIXMAP
1685 if (bgPixmap.pixmap == None)
1687 XSetWindowBackground (dpy, vt, pix_colors[Color_bg]);
1690 gcvalue.foreground = pix_colors[Color_fg];
1691 gcvalue.background = pix_colors[Color_bg];
1692 XChangeGC (dpy, gc, GCBackground | GCForeground, &gcvalue);
1699 /* ------------------------------------------------------------------------- */
1701 * Report current cursor position
1702 * XTERM_SEQ: Report position: ESC [ 6 n
1705 rxvt_term::scr_report_position () NOTHROW
1707 tt_printf ("\033[%d;%dR", screen.cur.row + 1, screen.cur.col + 1);
1710 /* ------------------------------------------------------------------------- *
1712 * ------------------------------------------------------------------------- */
1718 rxvt_term::set_font_style () NOTHROW
1721 switch (charsets [screen.charset])
1723 case '0': /* DEC Special Character & Line Drawing Set */
1725 case 'A': /* United Kingdom (UK) */
1727 case 'B': /* United States (USASCII) */
1729 case '<': /* Multinational character set */
1731 case '5': /* Finnish character set */
1733 case 'C': /* Finnish character set */
1735 case 'K': /* German character set */
1741 /* ------------------------------------------------------------------------- */
1744 * XTERM_SEQ: Invoke G0 character set: CTRL-O
1745 * XTERM_SEQ: Invoke G1 character set: CTRL-N
1746 * XTERM_SEQ: Invoke G2 character set: ESC N
1747 * XTERM_SEQ: Invoke G3 character set: ESC O
1750 rxvt_term::scr_charset_choose (int set) NOTHROW
1752 screen.charset = set;
1756 /* ------------------------------------------------------------------------- */
1759 * XTERM_SEQ: Set G0 character set: ESC ( <C>
1760 * XTERM_SEQ: Set G1 character set: ESC ) <C>
1761 * XTERM_SEQ: Set G2 character set: ESC * <C>
1762 * XTERM_SEQ: Set G3 character set: ESC + <C>
1763 * See set_font_style for possible values for <C>
1766 rxvt_term::scr_charset_set (int set, unsigned int ch) NOTHROW
1768 charsets[set] = (unsigned char)ch;
1773 /* ------------------------------------------------------------------------- *
1774 * MAJOR SCREEN MANIPULATION *
1775 * ------------------------------------------------------------------------- */
1778 * refresh matching text.
1781 rxvt_term::scr_refresh_rend (rend_t mask, rend_t value) NOTHROW
1785 for (int i = 0; i < nrow; i++)
1787 rend_t *drp = drawn_buf[i].r;
1789 for (int col = 0; col < ncol; col++, drp++)
1790 if ((*drp & mask) == value)
1810 rxvt_term::scr_expose (int x, int y, int ewidth, int eheight, bool refresh) NOTHROW
1813 row_col_t rc[RC_COUNT];
1815 if (!drawn_buf) /* sanity check */
1818 #ifndef NO_SLOW_LINK_SUPPORT
1819 if (refresh_type == FAST_REFRESH && !display->is_local)
1827 rc[PART_BEG].col = Pixel2Col (x);
1828 rc[PART_BEG].row = Pixel2Row (y);
1830 rc[PART_END].col = Pixel2Width (x + ewidth + fwidth - 1);
1831 rc[PART_END].row = Pixel2Row (y + eheight + fheight - 1);
1834 for (i = PART_BEG; i < RC_COUNT; i++)
1836 min_it (rc[i].col, ncol - 1);
1837 min_it (rc[i].row, nrow - 1);
1840 for (i = rc[PART_BEG].row; i <= rc[PART_END].row; i++)
1841 fill_text (&drawn_buf[i].t[rc[PART_BEG].col], 0, rc[PART_END].col - rc[PART_BEG].col + 1);
1849 /* ------------------------------------------------------------------------- */
1851 * Refresh the entire screen
1854 rxvt_term::scr_touch (bool refresh) NOTHROW
1856 scr_expose (0, 0, width, height, refresh);
1859 /* ------------------------------------------------------------------------- */
1861 * Move the display so that the line represented by scrollbar value Y is at
1862 * the top of the screen
1865 rxvt_term::scr_move_to (int y, int len) NOTHROW
1867 scr_changeview ((top_row - nrow) * (len - y) / len + (nrow - 1));
1870 /* ------------------------------------------------------------------------- */
1872 * Page the screen up/down nlines
1873 * direction should be UP or DN
1876 rxvt_term::scr_page (enum page_dirn direction, int nlines) NOTHROW
1878 int new_view_start =
1879 direction == UP ? view_start - nlines
1880 : view_start + nlines;
1882 return scr_changeview (new_view_start);
1886 rxvt_term::scr_changeview (int new_view_start) NOTHROW
1888 clamp_it (new_view_start, top_row, 0);
1890 if (new_view_start == view_start)
1893 num_scr += new_view_start - view_start;
1894 view_start = new_view_start;
1897 HOOK_INVOKE ((this, HOOK_VIEW_CHANGE, DT_INT, view_start, DT_END));
1904 rxvt_term::bell_cb (ev::timer &w, int revents)
1906 rvideo_bell = false;
1907 scr_rvideo_mode (rvideo_mode);
1912 /* ------------------------------------------------------------------------- */
1914 rxvt_term::scr_bell () NOTHROW
1918 # ifndef NO_MAPALERT
1919 # ifdef MAPALERT_OPTION
1920 if (option (Opt_mapAlert))
1922 XMapWindow (dpy, parent[0]);
1926 if (option (Opt_urgentOnBell))
1930 if (option (Opt_visualBell))
1933 scr_rvideo_mode (rvideo_mode);
1936 bell_ev.start (VISUAL_BELL_DURATION);
1940 HOOK_INVOKE ((this, HOOK_BELL, DT_END));
1944 /* ------------------------------------------------------------------------- */
1947 rxvt_term::scr_printscreen (int fullhist) NOTHROW
1950 int nrows, row_start;
1951 FILE *fd = popen_printer ();
1958 nrows = nrow - top_row;
1959 row_start = top_row;
1964 row_start = view_start;
1969 for (int r1 = row_start; r1 < row_start + nrows; r1++)
1971 text_t *tp = ROW(r1).t;
1972 int len = ROW(r1).l;
1974 for (int i = len >= 0 ? len : ncol - 1; i--; ) //TODO//FIXME//LEN
1976 char mb[MB_LEN_MAX];
1981 len = wctomb (mb, t);
1989 fwrite (mb, 1, len, fd);
1995 pclose_printer (fd);
1999 /* ------------------------------------------------------------------------- */
2001 * Refresh the screen
2002 * drawn_text/drawn_rend contain the screen information before the update.
2003 * screen.text/screen.rend contain what the screen will change to.
2006 rxvt_term::scr_refresh () NOTHROW
2008 int16_t col, row, /* column/row we're processing */
2009 ocrow; /* old cursor row */
2011 #ifndef NO_CURSORCOLOR
2012 rend_t cc1; /* store colours at cursor position (s) */
2014 rend_t *crp; // cursor rendition pointer
2015 rend_t ccol1, /* Cursor colour */
2016 ccol2; /* Cursor colour2 */
2018 want_refresh = 0; /* screen is current */
2020 if (refresh_type == NO_REFRESH || !mapped)
2028 unsigned int old_screen_flags = screen.flags;
2030 #ifdef HAVE_BG_PIXMAP
2031 have_bg = bgPixmap.pixmap != None;
2033 ocrow = oldcursor.row; /* is there an old outline cursor on screen? */
2036 * B: reverse any characters which are selected
2038 scr_reverse_selection ();
2040 HOOK_INVOKE ((this, HOOK_REFRESH_BEGIN, DT_END));
2042 scr_swap_overlay ();
2045 char showcursor = screen.flags & Screen_VisibleCursor;
2048 * C: set the cursor character (s)
2051 unsigned char setoldcursor;
2060 int col = screen.cur.col;
2062 while (col && ROW(screen.cur.row).t[col] == NOCHAR)
2065 crp = &ROW(screen.cur.row).r[col];
2067 #ifndef NO_CURSORCOLOR
2068 cc1 = *crp & (RS_fgMask | RS_bgMask);
2069 if (ISSET_PIXCOLOR (Color_cursor))
2070 ccol1 = Color_cursor;
2073 #ifdef CURSOR_COLOR_IS_RENDITION_COLOR
2074 ccol1 = fgcolor_of (rstyle);
2079 #ifndef NO_CURSORCOLOR
2080 if (ISSET_PIXCOLOR (Color_cursor2))
2081 ccol2 = Color_cursor2;
2084 #ifdef CURSOR_COLOR_IS_RENDITION_COLOR
2085 ccol2 = bgcolor_of (rstyle);
2090 if (showcursor && focus)
2092 if (option (Opt_cursorUnderline))
2097 *crp = SET_FGCOLOR (*crp, ccol1);
2098 *crp = SET_BGCOLOR (*crp, ccol2);
2103 /* make sure no outline cursor is left around */
2107 if (screen.cur.row - view_start != ocrow
2108 || screen.cur.col != oldcursor.col)
2111 && oldcursor.col < ncol)
2112 drawn_buf[ocrow].r[oldcursor.col] ^= (RS_RVid | RS_Uline);
2114 if (focus || !showcursor)
2125 if (screen.cur.row - view_start >= nrow)
2129 oldcursor.row = screen.cur.row - view_start;
2130 oldcursor.col = screen.cur.col;
2135 #ifndef NO_SLOW_LINK_SUPPORT
2137 * D: CopyArea pass - very useful for slower links
2138 * This has been deliberately kept simple.
2140 if (!display->is_local
2141 && refresh_type == FAST_REFRESH && num_scr_allow && num_scr
2142 && abs (num_scr) < nrow && !have_bg)
2148 dLocal (int, num_scr);
2152 row = i > 0 ? 0 : j - 1;
2154 for (; j-- >= 0; row += (i > 0 ? 1 : -1))
2156 if (row + i >= 0 && row + i < nrow && row + i != ocrow)
2158 line_t s = ROW(view_start + row);
2159 line_t d = drawn_buf[row];
2160 line_t d2 = drawn_buf[row + i];
2162 for (nits = 0, col = ncol; col--; )
2163 if (s.t[col] != d2.t[col] || s.r[col] != d2.r[col])
2165 else if (s.t[col] != d.t[col] || s.r[col] != d.r[col])
2168 if (nits > 8) /* XXX: arbitrary choice */
2170 for (col = ncol; col--; )
2186 /* also comes here at end if needed because of >= above */
2192 gcv.graphics_exposures = 1; XChangeGC (dpy, gc, GCGraphicsExposures, &gcv);
2193 XCopyArea (dpy, vt, vt,
2194 gc, 0, Row2Pixel (len + i),
2195 (unsigned int)this->width,
2196 (unsigned int)Height2Pixel (wlen - len + 1),
2197 0, Row2Pixel (len));
2198 gcv.graphics_exposures = 0; XChangeGC (dpy, gc, GCGraphicsExposures, &gcv);
2207 * E: main pass across every character
2209 for (row = 0; row < nrow; row++)
2211 text_t *stp = ROW(view_start + row).t;
2212 rend_t *srp = ROW(view_start + row).r;
2213 text_t *dtp = drawn_buf[row].t;
2214 rend_t *drp = drawn_buf[row].r;
2217 * E2: OK, now the real pass
2219 int ypixel = (int)Row2Pixel (row);
2221 for (col = 0; col < ncol; col++)
2223 /* compare new text with old - if exactly the same then continue */
2224 if (stp[col] == dtp[col] /* Must match characters to skip. */
2225 && (RS_SAME (srp[col], drp[col]) /* Either rendition the same or */
2226 || (stp[col] == ' ' /* space w/ no background change */
2227 && GET_BGATTR (srp[col]) == GET_BGATTR (drp[col]))))
2230 // redraw one or more characters
2232 // seek to the beginning of wide characters
2233 while (expect_false (stp[col] == NOCHAR && col > 0))
2236 rend_t rend = srp[col]; /* screen rendition (target rendtion) */
2237 text_t *text = stp + col;
2240 dtp[col] = stp[col];
2243 int xpixel = Col2Pixel (col);
2245 for (i = 0; ++col < ncol; )
2247 if (stp[col] == NOCHAR)
2249 dtp[col] = stp[col];
2250 drp[col] = srp[col];
2258 if (!RS_SAME (rend, srp[col]))
2263 if (stp[col] != dtp[col]
2264 || !RS_SAME (srp[col], drp[col]))
2266 if (have_bg && (i++ > count / 2))
2269 dtp[col] = stp[col];
2273 else if (have_bg || (stp[col] != ' ' && ++i >= 16))
2277 col--; /* went one too far. move back */
2278 count -= i; /* dump any matching trailing chars */
2280 // sometimes we optimize away the trailing NOCHAR's, add them back
2281 while (expect_false (i && text[count] == NOCHAR))
2285 * Determine the attributes for the string
2287 int fore = fgcolor_of (rend); // desired foreground
2288 int back = bgcolor_of (rend); // desired background
2290 // only do special processing if any attributes are set, which is unlikely
2291 if (expect_false (rend & (RS_Bold | RS_Italic | RS_Uline | RS_RVid | RS_Blink | RS_Careful)))
2293 bool invert = rend & RS_RVid;
2295 #ifndef NO_BOLD_UNDERLINE_REVERSE
2296 if (rend & RS_Bold && fore == Color_fg)
2298 if (ISSET_PIXCOLOR (Color_BD))
2306 if (rend & RS_Italic && fore == Color_fg)
2308 if (ISSET_PIXCOLOR (Color_IT))
2316 if (rend & RS_Uline && fore == Color_fg && ISSET_PIXCOLOR (Color_UL))
2323 if ((showcursor && row == screen.cur.row && text - stp == screen.cur.col)
2324 || !ISSET_PIXCOLOR (Color_HC))
2326 /* invert the column if no highlightColor is set or it is the
2327 * current cursor column */
2328 ::swap (fore, back);
2330 else if (ISSET_PIXCOLOR (Color_HC))
2334 #ifndef NO_BOLD_UNDERLINE_REVERSE
2336 if (ISSET_PIXCOLOR (Color_RV))
2348 if (rend & RS_Blink && (back == Color_bg || fore == Color_bg))
2350 if (!text_blink_ev.is_active ())
2352 text_blink_ev.again ();
2355 else if (hidden_text)
2361 // "careful" (too wide) character handling
2363 // include previous careful character(s) if possible, looks nicer (best effort...)
2365 && srp[text - stp - 1] & RS_Careful
2366 && RS_SAME (rend, srp[text - stp - 1]))
2367 text--, count++, xpixel -= fwidth;
2369 // force redraw after "careful" characters to avoid pixel droppings
2370 for (int i = 0; srp[col + i] & RS_Careful && col + i < ncol - 1; i++)
2371 drp[col + i + 1] = srp[col + i + 1] ^ RS_redraw;
2373 // force redraw before "careful" characters to avoid pixel droppings
2374 for (int i = 0; srp[text - stp - i] & RS_Careful && text - i > stp; i++)
2375 drp[text - stp - i - 1] = srp[text - stp - i - 1] ^ RS_redraw;
2380 * Actually do the drawing of the string here
2382 rxvt_font *font = (*fontset[GET_STYLE (rend)])[GET_FONT (rend)];
2384 if (expect_true (have_bg && back == Color_bg))
2386 // this is very ugly, maybe push it into ->draw?
2388 for (i = 0; i < count; i++) /* don't draw empty strings */
2391 font->draw (*drawable, xpixel, ypixel, text, count, fore, Color_transparent);
2395 CLEAR_CHARS (xpixel, ypixel, count);
2399 font->draw (*drawable, xpixel, ypixel, text, count, fore, back);
2401 if (expect_false (rend & RS_Uline && font->descent > 1 && fore != back))
2404 if (ISSET_PIXCOLOR (Color_underline))
2405 XSetForeground (dpy, gc, pix_colors[Color_underline]);
2408 XSetForeground (dpy, gc, pix_colors[fore]);
2410 XDrawLine (dpy, vt, gc,
2411 xpixel, ypixel + font->ascent + 1,
2412 xpixel + Width2Pixel (count) - 1, ypixel + font->ascent + 1);
2414 } /* for (col....) */
2415 } /* for (row....) */
2418 * G: cleanup cursor and display outline cursor if necessary
2424 if (option (Opt_cursorUnderline))
2429 #ifndef NO_CURSORCOLOR
2430 *crp = (*crp & ~ (RS_fgMask | RS_bgMask)) | cc1;
2434 else if (oldcursor.row >= 0)
2436 int cursorwidth = 1;
2437 int col = oldcursor.col;
2439 while (col && ROW(screen.cur.row).t[col] == NOCHAR)
2442 while (col + cursorwidth < ncol
2443 && drawn_buf[oldcursor.row].t[col + cursorwidth] == NOCHAR)
2446 #ifndef NO_CURSORCOLOR
2447 if (ISSET_PIXCOLOR (Color_cursor))
2448 XSetForeground (dpy, gc, pix_colors[Color_cursor]);
2451 XSetForeground (dpy, gc, pix_colors[ccol1]);
2453 XDrawRectangle (dpy, vt, gc,
2455 Row2Pixel (oldcursor.row),
2456 (unsigned int) (Width2Pixel (cursorwidth) - 1),
2457 (unsigned int) (Height2Pixel (1) - lineSpace - 1));
2462 * H: cleanup selection
2465 scr_swap_overlay ();
2467 HOOK_INVOKE ((this, HOOK_REFRESH_END, DT_END));
2469 scr_reverse_selection ();
2471 screen.flags = old_screen_flags;
2477 rxvt_term::scr_remap_chars (line_t &l) NOTHROW
2482 l.touch (); // maybe a bit of an overkill, but it's not performance-relevant
2484 for (int i = ncol; i--; )
2485 l.r[i] = SET_FONT (l.r[i], FONTSET (l.r[i])->find_font (l.t[i]));
2489 rxvt_term::scr_remap_chars () NOTHROW
2491 for (int i = total_rows; i--; )
2492 scr_remap_chars (row_buf [i]);
2494 for (int i = nrow; i--; )
2496 scr_remap_chars (drawn_buf [i]);
2497 scr_remap_chars (swap_buf [i]);
2502 rxvt_term::scr_recolour () NOTHROW
2504 #ifdef HAVE_BG_PIXMAP
2508 XSetWindowBackground (dpy, parent[0], pix_colors[Color_border]);
2509 XClearWindow (dpy, parent[0]);
2510 XSetWindowBackground (dpy, vt, pix_colors[Color_bg]);
2514 XSetWindowBackground (dpy, scrollBar.win, pix_colors[Color_border]);
2515 scrollBar.state = STATE_IDLE;
2521 /* bgPixmap.apply () does not do the following : */
2527 /* ------------------------------------------------------------------------- */
2529 rxvt_term::scr_clear (bool really) NOTHROW
2538 XClearWindow (dpy, vt);
2542 rxvt_term::scr_xor_rect (int beg_row, int beg_col, int end_row, int end_col, rend_t rstyle1, rend_t rstyle2) NOTHROW
2544 int view_end = view_start + nrow;
2547 for (row = max (beg_row, view_start); row <= min (end_row, view_end); row++)
2549 text_t *stp = ROW(row).t;
2550 rend_t *srp = ROW(row).r;
2552 for (col = beg_col; col < end_col; col++)
2553 srp[col] ^= rstyle1;
2555 while (col-- > beg_col && (stp[col] == NOCHAR || unicode::is_space (stp[col])))
2556 srp[col] ^= rstyle2;
2558 if (++col < end_col)
2559 srp[col] ^= rstyle2;
2564 rxvt_term::scr_xor_span (int beg_row, int beg_col, int end_row, int end_col, rend_t rstyle) NOTHROW
2566 int view_end = view_start + nrow;
2569 if (beg_row >= view_start)
2580 for (; row < min (end_row, view_end); row++, col = 0)
2581 for (rend_t *srp = ROW(row).r; col < ncol; col++)
2585 for (rend_t *srp = ROW(row).r; col < end_col; col++)
2589 /* ------------------------------------------------------------------------- */
2591 rxvt_term::scr_reverse_selection () NOTHROW
2594 && current_screen == selection.screen
2595 && selection.end.row >= view_start)
2599 scr_xor_rect (selection.beg.row, selection.beg.col,
2600 selection.end.row, selection.end.col,
2601 RS_RVid, RS_RVid | RS_Uline);
2604 scr_xor_span (selection.beg.row, selection.beg.col,
2605 selection.end.row, selection.end.col,
2610 /* ------------------------------------------------------------------------- */
2612 * Dump the whole scrollback and screen to the passed filedescriptor. The
2613 * invoking routine must close the fd.
2617 rxvt_term::scr_dump (int fd) NOTHROW
2620 unsigned int width, towrite;
2623 for (row = saveLines + top_row;
2624 row < saveLines + nrow - 1; row++)
2626 width = row_buf[row].l >= 0 ? row_buf[row].l
2628 for (towrite = width; towrite; towrite -= wrote)
2630 wrote = write (fd, & (row_buf[row].t[width - towrite]),
2633 return; /* XXX: death, no report */
2635 if (row_buf[row].l >= 0)
2636 if (write (fd, r1, 1) <= 0)
2637 return; /* XXX: death, no report */
2642 /* ------------------------------------------------------------------------- *
2643 * CHARACTER SELECTION *
2644 * ------------------------------------------------------------------------- */
2646 rxvt_term::selection_check (int check_more) NOTHROW
2653 pos.row = pos.col = 0;
2654 if (!IN_RANGE_EXC (selection.beg.row, top_row, nrow)
2655 || !IN_RANGE_EXC (selection.mark.row, top_row, nrow)
2656 || !IN_RANGE_EXC (selection.end.row, top_row, nrow)
2658 && current_screen == selection.screen
2659 && !ROWCOL_IS_BEFORE (screen.cur, selection.beg)
2660 && ROWCOL_IS_BEFORE (screen.cur, selection.end))
2662 && ROWCOL_IS_BEFORE (selection.beg, pos)
2663 && ROWCOL_IS_AFTER (selection.end, pos))
2665 && ROWCOL_IS_AFTER (selection.end, pos))
2666 || (check_more == 4 /* screen width change */
2667 && (selection.beg.row != selection.end.row
2668 || selection.end.col > ncol)))
2672 /* ------------------------------------------------------------------------- */
2674 * Paste a selection direct to the command fd
2677 rxvt_term::paste (char *data, unsigned int len) NOTHROW
2679 /* convert normal newline chars into common keyboard Return key sequence */
2680 for (unsigned int i = 0; i < len; i++)
2681 if (data[i] == C0_LF)
2684 if (priv_modes & PrivMode_BracketPaste)
2685 tt_printf ("\e[200~");
2687 tt_write (data, len);
2689 if (priv_modes & PrivMode_BracketPaste)
2690 tt_printf ("\e[201~");
2693 /* ------------------------------------------------------------------------- */
2695 * Respond to a notification that a primary selection has been sent
2696 * EXT: SelectionNotify
2699 rxvt_term::selection_paste (Window win, Atom prop, bool delete_prop) NOTHROW
2701 if (prop == None) /* check for failed XConvertSelection */
2703 if ((selection_type & Sel_CompoundText))
2705 int selnum = selection_type & Sel_whereMask;
2708 if (selnum != Sel_direct)
2709 selection_request_other (XA_STRING, selnum);
2712 if ((selection_type & Sel_UTF8String))
2714 int selnum = selection_type & Sel_whereMask;
2716 selection_type = Sel_CompoundText;
2717 if (selnum != Sel_direct)
2718 selection_request_other (xa[XA_COMPOUND_TEXT], selnum);
2726 unsigned long bytes_after;
2729 if (XGetWindowProperty (dpy, win, prop,
2731 delete_prop, AnyPropertyType,
2732 &ct.encoding, &ct.format,
2733 &ct.nitems, &bytes_after,
2734 &ct.value) != Success)
2740 if (ct.encoding == None)
2745 // fetch and append remaining data
2748 if (XGetWindowProperty (dpy, win, prop,
2749 ct.nitems / 4, (bytes_after + 3) / 4,
2750 delete_prop, AnyPropertyType,
2751 &ct2.encoding, &ct2.format,
2752 &ct2.nitems, &bytes_after,
2753 &ct2.value) != Success)
2756 // realloc should be compatible to XFree, here, and elsewhere, too
2757 ct.value = (unsigned char *)realloc (ct.value, ct.nitems + ct2.nitems + 1);
2758 memcpy (ct.value + ct.nitems, ct2.value, ct2.nitems + 1);
2759 ct.nitems += ct2.nitems;
2767 if (ct.encoding == xa[XA_INCR])
2769 // INCR selection, start handshake
2771 XDeleteProperty (dpy, win, prop);
2773 selection_wait = Sel_incr;
2782 if (selection_wait == Sel_incr)
2786 // finally complete, now paste the whole thing
2787 selection_wait = Sel_normal;
2788 ct.value = (unsigned char *)incr_buf;
2789 ct.nitems = incr_buf_fill;
2796 if (selection_wait == Sel_normal
2797 && (win != display->root || prop != XA_CUT_BUFFER0)) // avoid recursion
2800 * pass through again trying CUT_BUFFER0 if we've come from
2801 * XConvertSelection () but nothing was presented
2803 selection_paste (display->root, XA_CUT_BUFFER0, False);
2809 else if (selection_wait == Sel_incr)
2813 while (incr_buf_fill + ct.nitems > incr_buf_size)
2815 incr_buf_size = incr_buf_size ? incr_buf_size * 2 : 128*1024;
2816 incr_buf = (char *)realloc (incr_buf, incr_buf_size);
2819 memcpy (incr_buf + incr_buf_fill, ct.value, ct.nitems);
2820 incr_buf_fill += ct.nitems;
2829 // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it
2830 // so recode it manually
2831 if (ct.encoding == xa[XA_UTF8_STRING])
2833 wchar_t *w = rxvt_utf8towcs ((const char *)ct.value, ct.nitems);
2834 char *s = rxvt_wcstombs (w);
2836 // TODO: strlen == only the first element will be converted. well...
2837 paste (s, strlen (s));
2842 if (XmbTextPropertyToTextList (dpy, &ct, &cl, &cr) >= 0
2845 for (int i = 0; i < cr; i++)
2846 paste (cl[i], strlen (cl[i]));
2848 XFreeStringList (cl);
2851 paste ((char *)ct.value, ct.nitems); // paste raw
2856 if (selection_wait == Sel_normal)
2857 selection_wait = Sel_none;
2861 rxvt_term::incr_cb (ev::timer &w, int revents) NOTHROW
2863 selection_wait = Sel_none;
2868 rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n");
2872 rxvt_term::selection_property (Window win, Atom prop) NOTHROW
2874 if (prop == None || selection_wait != Sel_incr)
2877 selection_paste (win, prop, true);
2880 /* ------------------------------------------------------------------------- */
2882 * Request the current selection:
2883 * Order: > internal selection if available
2884 * > PRIMARY, SECONDARY, CLIPBOARD if ownership is claimed (+)
2886 * (+) if ownership is claimed but property is empty, rxvt_selection_paste ()
2887 * will auto fallback to CUT_BUFFER0
2888 * EXT: button 2 release
2891 rxvt_term::selection_request (Time tm, int selnum) NOTHROW
2893 if (selection.text && selnum == Sel_Primary)
2895 /* internal selection */
2896 char *str = rxvt_wcstombs (selection.text, selection.len);
2897 paste (str, strlen (str));
2903 selection_request_time = tm;
2904 selection_wait = Sel_normal;
2906 #if X_HAVE_UTF8_STRING
2907 selection_type = Sel_UTF8String;
2908 if (selection_request_other (xa[XA_UTF8_STRING], selnum))
2911 selection_type = Sel_CompoundText;
2912 if (selection_request_other (xa[XA_COMPOUND_TEXT], selnum))
2917 selection_wait = Sel_none; /* don't loop in selection_paste () */
2918 selection_paste (display->root, XA_CUT_BUFFER0, false);
2922 rxvt_term::selection_request_other (Atom target, int selnum) NOTHROW
2926 selection_type |= selnum;
2928 if (selnum == Sel_Primary)
2930 else if (selnum == Sel_Secondary)
2933 sel = xa[XA_CLIPBOARD];
2935 if (XGetSelectionOwner (dpy, sel) != None)
2937 XConvertSelection (dpy, sel, target, xa[XA_VT_SELECTION],
2938 vt, selection_request_time);
2945 /* ------------------------------------------------------------------------- */
2947 * Clear all selected text
2948 * EXT: SelectionClear
2951 rxvt_term::selection_clear (bool clipboard) NOTHROW
2956 free (selection.text);
2957 selection.text = NULL;
2961 if (display->selection_owner == this)
2962 display->selection_owner = 0;
2966 free (selection.clip_text);
2967 selection.clip_text = NULL;
2968 selection.clip_len = 0;
2970 if (display->clipboard_owner == this)
2971 display->clipboard_owner = 0;
2976 rxvt_term::clipboard_copy (Time tm)
2978 if (selection.len > 0)
2980 free (selection.clip_text);
2981 selection.clip_len = selection.len;
2982 selection.clip_text = (wchar_t *) malloc (sizeof (wchar_t) * selection.clip_len);
2983 memcpy (selection.clip_text, selection.text,
2984 sizeof (wchar_t) * selection.clip_len);
2985 selection_grab (tm, true);
2989 /* ------------------------------------------------------------------------- */
2991 * Copy a selection into the cut buffer
2992 * EXT: button 1 or 3 release
2995 rxvt_term::selection_make (Time tm)
2998 wchar_t *new_selection_text;
3001 switch (selection.op)
3003 case SELECTION_CONT:
3005 case SELECTION_INIT:
3008 case SELECTION_BEGIN:
3009 selection.op = SELECTION_DONE;
3015 selection.op = SELECTION_DONE;
3017 if (selection.clicks == 4)
3018 return; /* nothing selected, go away */
3020 if (HOOK_INVOKE ((this, HOOK_SEL_MAKE, DT_LONG, (long)tm, DT_END)))
3023 i = (selection.end.row - selection.beg.row + 1) * (ncol + 1);
3024 new_selection_text = (wchar_t *)rxvt_malloc ((i + 4) * sizeof (wchar_t));
3029 int col = selection.beg.col;
3030 int row = selection.beg.row;
3034 for (; row <= selection.end.row; row++, col = 0)
3039 col = selection.beg.col;
3044 end_col = ROW(row).l;
3048 if (row == selection.end.row
3053 min_it (end_col, selection.end.col);
3055 t = ROW(row).t + col;
3057 for (; col < end_col; col++)
3061 #if ENABLE_COMBINING
3062 else if (IS_COMPOSE (*t))
3064 int len = rxvt_composite.expand (*t, 0);
3072 new_selection_text = (wchar_t *)rxvt_realloc (new_selection_text, (i + 4) * sizeof (wchar_t));
3075 ofs += rxvt_composite.expand (*t++, new_selection_text + ofs);
3079 new_selection_text[ofs++] = *t++;
3086 && new_selection_text[ofs - 1] != C0_LF
3087 && unicode::is_space (new_selection_text[ofs - 1]))
3090 new_selection_text[ofs++] = C0_LF;
3094 if (!ROW(row).is_longer () && row != selection.end.row)
3095 new_selection_text[ofs++] = C0_LF;
3098 if (end_col != selection.end.col)
3099 new_selection_text[ofs++] = C0_LF;
3101 new_selection_text[ofs] = 0;
3105 free (new_selection_text);
3109 free (selection.text);
3111 // we usually allocate much more than necessary, so realloc it smaller again
3112 selection.len = ofs;
3113 selection.text = (wchar_t *)rxvt_realloc (new_selection_text, (ofs + 1) * sizeof (wchar_t));
3115 if (HOOK_INVOKE ((this, HOOK_SEL_GRAB, DT_LONG, (long)tm, DT_END)))
3118 selection_grab (tm);
3122 rxvt_term::selection_grab (Time tm, bool clipboard) NOTHROW
3128 selection_time = tm;
3133 clipboard_time = tm;
3134 sel = xa[XA_CLIPBOARD];
3137 XSetSelectionOwner (dpy, sel, vt, tm);
3138 if (XGetSelectionOwner (dpy, sel) == vt)
3140 display->set_selection_owner (this, clipboard);
3145 selection_clear (clipboard);
3152 if (XwcTextListToTextProperty (dpy, &selection.text, 1, XStringStyle, &ct) >= 0)
3154 set_string_property (XA_CUT_BUFFER0, ct.value, ct.nitems);
3160 /* ------------------------------------------------------------------------- */
3162 * Mark or select text based upon number of clicks: 1, 2, or 3
3163 * EXT: button 1 press
3166 rxvt_term::selection_click (int clicks, int x, int y) NOTHROW
3168 clicks = ((clicks - 1) % 3) + 1;
3169 selection.clicks = clicks; /* save clicks so extend will work */
3171 if (clicks == 2 && !selection.rect
3172 && HOOK_INVOKE ((this, HOOK_SEL_EXTEND, DT_END)))
3174 MEvent.clicks = 1; // what a mess
3175 selection.screen = current_screen;
3176 selection.op = SELECTION_CONT;
3180 selection_start_colrow (Pixel2Col (x), Pixel2Row (y));
3182 if (clicks == 2 || clicks == 3)
3183 selection_extend_colrow (selection.mark.col,
3184 selection.mark.row - view_start,
3186 1, /* button press */
3187 0); /* click change */
3190 /* ------------------------------------------------------------------------- */
3192 * Mark a selection at the specified col/row
3195 rxvt_term::selection_start_colrow (int col, int row) NOTHROW
3199 selection.mark.row = row + view_start;
3200 selection.mark.col = col;
3202 selection.mark.row = clamp (selection.mark.row, top_row, nrow - 1);
3203 selection.mark.col = clamp (selection.mark.col, 0, ncol - 1);
3205 while (selection.mark.col > 0
3206 && ROW(selection.mark.row).t[selection.mark.col] == NOCHAR)
3207 --selection.mark.col;
3211 /* clear the old selection */
3212 selection.beg.row = selection.end.row = selection.mark.row;
3213 selection.beg.col = selection.end.col = selection.mark.col;
3216 selection.op = SELECTION_INIT;
3217 selection.screen = current_screen;
3220 /* ------------------------------------------------------------------------- */
3222 * Word select: select text for 2 clicks
3223 * We now only find out the boundary in one direction
3226 /* what do we want: spaces/tabs are delimiters or cutchars or non-cutchars */
3227 #define DELIMIT_TEXT(x) \
3228 (unicode::is_space (x) ? 2 : (x) <= 0xff && !!strchr (rs[Rs_cutchars], (x)))
3229 #define DELIMIT_REND(x) 1
3232 rxvt_term::selection_delimit_word (enum page_dirn dirn, const row_col_t *mark, row_col_t *ret) NOTHROW
3234 int col, row, dirnadd, tcol, trow, w1, w2;
3241 bound.row = top_row - 1;
3248 bound.col = ncol - 1;
3253 col = max (mark->col, 0);
3255 /* find the edge of a word */
3256 stp = ROW(row).t + col; w1 = DELIMIT_TEXT (*stp);
3257 srp = ROW(row).r + col; w2 = DELIMIT_REND (*srp);
3261 for (; col != bound.col; col += dirnadd)
3269 if (DELIMIT_TEXT (*stp) != w1)
3271 if (DELIMIT_REND (*srp) != w2)
3275 if ((col == bound.col) && (row != bound.row))
3277 if (ROW(row - (dirn == UP ? 1 : 0)).is_longer ())
3279 trow = row + dirnadd;
3280 tcol = dirn == UP ? ncol - 1 : 0;
3285 stp = ROW(trow).t + tcol;
3286 srp = ROW(trow).r + tcol;
3288 if (DELIMIT_TEXT (*stp) != w1 || DELIMIT_REND (*srp) != w2)
3300 col++; /* put us on one past the end */
3302 /* Poke the values back in */
3307 /* ------------------------------------------------------------------------- */
3309 * Extend the selection to the specified x/y pixel location
3310 * EXT: button 3 press; button 1 or 3 drag
3311 * flag == 0 ==> button 1
3312 * flag == 1 ==> button 3 press
3313 * flag == 2 ==> button 3 motion
3316 rxvt_term::selection_extend (int x, int y, int flag) NOTHROW
3318 int col = clamp (Pixel2Col (x), 0, ncol);
3319 int row = clamp (Pixel2Row (y), 0, nrow - 1);
3322 * If we're selecting characters (single click) then we must check first
3323 * if we are at the same place as the original mark. If we are then
3324 * select nothing. Otherwise, if we're to the right of the mark, you have to
3325 * be _past_ a character for it to be selected.
3327 if (((selection.clicks % 3) == 1) && !flag
3328 && (col == selection.mark.col
3329 && (row == selection.mark.row - view_start)))
3331 /* select nothing */
3332 selection.beg.row = selection.end.row = 0;
3333 selection.beg.col = selection.end.col = 0;
3334 selection.clicks = 4;
3339 if (selection.clicks == 4)
3340 selection.clicks = 1;
3342 selection_extend_colrow (col, row, !!flag, /* ? button 3 */
3343 flag == 1 ? 1 : 0, /* ? button press */
3344 0); /* no click change */
3347 /* ------------------------------------------------------------------------- */
3349 * Extend the selection to the specified col/row
3352 rxvt_term::selection_extend_colrow (int32_t col, int32_t row, int button3, int buttonpress, int clickchange) NOTHROW
3361 switch (selection.op)
3363 case SELECTION_INIT:
3365 selection.op = SELECTION_BEGIN;
3367 case SELECTION_BEGIN:
3368 if (row != selection.mark.row || col != selection.mark.col
3369 || (!button3 && buttonpress))
3370 selection.op = SELECTION_CONT;
3372 case SELECTION_DONE:
3373 selection.op = SELECTION_CONT;
3375 case SELECTION_CONT:
3377 case SELECTION_CLEAR:
3378 selection_start_colrow (col, row);
3384 if (selection.beg.col == selection.end.col
3385 && selection.beg.col != selection.mark.col
3386 && selection.beg.row == selection.end.row
3387 && selection.beg.row != selection.mark.row)
3389 selection.beg.col = selection.end.col = selection.mark.col;
3390 selection.beg.row = selection.end.row = selection.mark.row;
3394 pos.row = view_start + row;
3397 * This is mainly xterm style selection with a couple of differences, mainly
3398 * in the way button3 drag extension works.
3399 * We're either doing: button1 drag; button3 press; or button3 drag
3400 * a) button1 drag : select around a midpoint/word/line - that point/word/line
3401 * is always at the left/right edge of the selection.
3402 * b) button3 press: extend/contract character/word/line at whichever edge of
3403 * the selection we are closest to.
3404 * c) button3 drag : extend/contract character/word/line - we select around
3405 * a point/word/line which is either the start or end of the selection
3406 * and it was decided by whichever point/word/line was `fixed' at the
3407 * time of the most recent button3 press
3409 if (button3 && buttonpress)
3413 * first determine which edge of the selection we are closest to
3415 if (ROWCOL_IS_BEFORE (pos, selection.beg)
3416 || (!ROWCOL_IS_AFTER (pos, selection.end)
3417 && (((pos.col - selection.beg.col)
3418 + ((pos.row - selection.beg.row) * ncol))
3419 < ((selection.end.col - pos.col)
3420 + ((selection.end.row - pos.row) * ncol)))))
3423 if (closeto == LEFT)
3425 selection.beg.row = pos.row;
3426 selection.beg.col = pos.col;
3427 selection.mark.row = selection.end.row;
3428 selection.mark.col = selection.end.col - (selection.clicks == 2);
3432 selection.end.row = pos.row;
3433 selection.end.col = pos.col;
3434 selection.mark.row = selection.beg.row;
3435 selection.mark.col = selection.beg.col;
3440 /* button1 drag or button3 drag */
3441 if (ROWCOL_IS_AFTER (selection.mark, pos))
3443 if (selection.mark.row == selection.end.row
3444 && selection.mark.col == selection.end.col
3446 && selection.clicks == 2)
3447 selection.mark.col--;
3449 selection.beg.row = pos.row;
3450 selection.beg.col = pos.col;
3451 selection.end.row = selection.mark.row;
3452 selection.end.col = selection.mark.col + (selection.clicks == 2);
3456 selection.beg.row = selection.mark.row;
3457 selection.beg.col = selection.mark.col;
3458 selection.end.row = pos.row;
3459 selection.end.col = pos.col;
3463 if (selection.clicks == 1)
3465 if (selection.beg.col > ROW(selection.beg.row).l //TODO//FIXME//LEN
3466 && !ROW(selection.beg.row).is_longer ()
3471 selection.beg.col = ncol;
3474 selection.end.col > ROW(selection.end.row).l //TODO//FIXME//LEN
3475 && !ROW(selection.end.row).is_longer ()
3480 selection.end.col = ncol;
3482 else if (selection.clicks == 2)
3484 if (ROWCOL_IS_AFTER (selection.end, selection.beg))
3485 selection.end.col--;
3487 selection_delimit_word (UP, &selection.beg, &selection.beg);
3488 selection_delimit_word (DN, &selection.end, &selection.end);
3490 else if (selection.clicks == 3)
3493 if (option (Opt_tripleclickwords))
3495 selection_delimit_word (UP, &selection.beg, &selection.beg);
3497 for (int end_row = selection.mark.row; end_row < nrow; end_row++)
3499 if (!ROW(end_row).is_longer ())
3501 selection.end.row = end_row;
3502 selection.end.col = ROW(end_row).l;
3503 # if !ENABLE_MINIMAL
3504 selection_remove_trailing_spaces ();
3513 if (ROWCOL_IS_AFTER (selection.mark, selection.beg))
3514 selection.mark.col++;
3516 selection.beg.col = 0;
3517 selection.end.col = ncol;
3519 // select a complete logical line
3520 while (selection.beg.row > -saveLines
3521 && ROW(selection.beg.row - 1).is_longer ())
3522 selection.beg.row--;
3524 while (selection.end.row < nrow
3525 && ROW(selection.end.row).is_longer ())
3526 selection.end.row++;
3530 if (button3 && buttonpress)
3532 /* mark may need to be changed */
3533 if (closeto == LEFT)
3535 selection.mark.row = selection.end.row;
3536 selection.mark.col = selection.end.col - (selection.clicks == 2);
3540 selection.mark.row = selection.beg.row;
3541 selection.mark.col = selection.beg.col;
3546 if (selection.rect && selection.beg.col > selection.end.col)
3547 ::swap (selection.beg.col, selection.end.col);
3553 rxvt_term::selection_remove_trailing_spaces () NOTHROW
3555 int32_t end_col, end_row;
3558 end_col = selection.end.col;
3559 end_row = selection.end.row;
3561 for (; end_row >= selection.beg.row; )
3563 stp = ROW(end_row).t;
3565 while (--end_col >= 0)
3567 if (stp[end_col] != NOCHAR
3568 && !unicode::is_space (stp[end_col]))
3573 || !ROW(end_row - 1).is_longer ())
3575 selection.end.col = end_col + 1;
3576 selection.end.row = end_row;
3584 if (selection.mark.row > selection.end.row)
3586 selection.mark.row = selection.end.row;
3587 selection.mark.col = selection.end.col;
3589 else if (selection.mark.row == selection.end.row
3590 && selection.mark.col > selection.end.col)
3591 selection.mark.col = selection.end.col;
3595 /* ------------------------------------------------------------------------- */
3597 * Double click on button 3 when already selected
3598 * EXT: button 3 double click
3601 rxvt_term::selection_rotate (int x, int y) NOTHROW
3603 selection.clicks = selection.clicks % 3 + 1;
3604 selection_extend_colrow (Pixel2Col (x), Pixel2Row (y), 1, 0, 1);
3607 /* ------------------------------------------------------------------------- */
3609 * Respond to a request for our current selection
3610 * EXT: SelectionRequest
3613 rxvt_term::selection_send (const XSelectionRequestEvent &rq) NOTHROW
3617 ev.type = SelectionNotify;
3619 ev.display = rq.display;
3620 ev.requestor = rq.requestor;
3621 ev.selection = rq.selection;
3622 ev.target = rq.target;
3625 if (rq.target == xa[XA_TARGETS])
3627 Atom target_list[6];
3628 Atom *target = target_list;
3630 *target++ = xa[XA_TARGETS];
3631 *target++ = xa[XA_TIMESTAMP];
3632 *target++ = XA_STRING;
3633 *target++ = xa[XA_TEXT];
3634 *target++ = xa[XA_COMPOUND_TEXT];
3635 #if X_HAVE_UTF8_STRING
3636 *target++ = xa[XA_UTF8_STRING];
3639 XChangeProperty (dpy, rq.requestor, rq.property, XA_ATOM,
3640 32, PropModeReplace,
3641 (unsigned char *)target_list, target - target_list);
3642 ev.property = rq.property;
3645 else if (rq.target == xa[XA_MULTIPLE])
3647 /* TODO: Handle MULTIPLE */
3650 else if (rq.target == xa[XA_TIMESTAMP] && rq.selection == XA_PRIMARY && selection.text)
3652 XChangeProperty (dpy, rq.requestor, rq.property, rq.target,
3653 32, PropModeReplace, (unsigned char *)&selection_time, 1);
3654 ev.property = rq.property;
3656 else if (rq.target == xa[XA_TIMESTAMP] && rq.selection == xa[XA_CLIPBOARD] && selection.clip_text)
3658 XChangeProperty (dpy, rq.requestor, rq.property, rq.target,
3659 32, PropModeReplace, (unsigned char *)&clipboard_time, 1);
3660 ev.property = rq.property;
3662 else if (rq.target == XA_STRING
3663 || rq.target == xa[XA_TEXT]
3664 || rq.target == xa[XA_COMPOUND_TEXT]
3665 || rq.target == xa[XA_UTF8_STRING]
3669 Atom target = rq.target;
3674 enc_string = XStringStyle,
3675 enc_text = XStdICCTextStyle,
3676 enc_compound_text = XCompoundTextStyle,
3677 #ifdef X_HAVE_UTF8_STRING
3678 enc_utf8 = XUTF8StringStyle,
3684 if (target == XA_STRING)
3685 // we actually don't do XA_STRING, but who cares, as i18n clients
3686 // will ask for another format anyways.
3688 else if (target == xa[XA_TEXT])
3690 else if (target == xa[XA_COMPOUND_TEXT])
3691 style = enc_compound_text;
3693 else if (target == xa[XA_UTF8_STRING])
3698 target = xa[XA_COMPOUND_TEXT];
3699 style = enc_compound_text;
3702 if (rq.selection == XA_PRIMARY && selection.text)
3704 cl = selection.text;
3705 selectlen = selection.len;
3707 else if (rq.selection == xa[XA_CLIPBOARD] && selection.clip_text)
3709 cl = selection.clip_text;
3710 selectlen = selection.clip_len;
3719 // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it
3720 // so recode it manually
3721 if (style == enc_utf8)
3724 ct.encoding = target;
3726 ct.value = (unsigned char *)rxvt_wcstoutf8 (cl, selectlen);
3727 ct.nitems = strlen ((char *)ct.value);
3731 if (XwcTextListToTextProperty (dpy, &cl, 1, (XICCEncodingStyle) style, &ct) >= 0)
3735 /* if we failed to convert then send it raw */
3736 ct.value = (unsigned char *)cl;
3737 ct.nitems = selectlen;
3738 ct.encoding = target;
3741 XChangeProperty (dpy, rq.requestor, rq.property,
3742 ct.encoding, 8, PropModeReplace,
3743 ct.value, (int)ct.nitems);
3744 ev.property = rq.property;
3750 XSendEvent (dpy, rq.requestor, False, 0L, (XEvent *)&ev);
3753 /* ------------------------------------------------------------------------- */
3756 rxvt_term::im_set_position (XPoint &pos) NOTHROW
3758 XWindowAttributes xwa;
3760 XGetWindowAttributes (dpy, vt, &xwa);
3762 pos.x = xwa.x + Col2Pixel (screen.cur.col);
3763 pos.y = xwa.y + Height2Pixel (screen.cur.row) + fbase;
3769 rxvt_term::scr_overlay_new (int x, int y, int w, int h) NOTHROW
3771 if (nrow < 1 || ncol < 1)
3778 if (x < 0) x = ncol - w;
3779 if (y < 0) y = nrow - h;
3781 // make space for border
3782 w += 2; min_it (w, ncol);
3783 h += 2; min_it (h, nrow);
3785 x -= 1; clamp_it (x, 0, ncol - w);
3786 y -= 1; clamp_it (y, 0, nrow - h);
3791 ov.text = new text_t *[h];
3792 ov.rend = new rend_t *[h];
3794 for (y = 0; y < h; y++)
3796 text_t *tp = ov.text[y] = new text_t[w];
3797 rend_t *rp = ov.rend[y] = new rend_t[w];
3800 rend_t r = OVERLAY_RSTYLE;
3803 t0 = 0x2554, t1 = 0x2550, t2 = 0x2557;
3805 t0 = 0x2551, t1 = 0x0020, t2 = 0x2551;
3807 t0 = 0x255a, t1 = 0x2550, t2 = 0x255d;
3812 for (x = w - 2; x > 0; --x)
3824 rxvt_term::scr_overlay_off () NOTHROW
3831 for (int y = 0; y < ov.h; y++)
3833 delete [] ov.text[y];
3834 delete [] ov.rend[y];
3837 delete [] ov.text; ov.text = 0;
3838 delete [] ov.rend; ov.rend = 0;
3842 rxvt_term::scr_overlay_set (int x, int y, text_t text, rend_t rend) NOTHROW
3844 if (!ov.text || x >= ov.w - 2 || y >= ov.h - 2)
3849 ov.text[y][x] = text;
3850 ov.rend[y][x] = rend;
3854 rxvt_term::scr_overlay_set (int x, int y, const char *s) NOTHROW
3857 scr_overlay_set (x++, y, *s++);
3861 rxvt_term::scr_overlay_set (int x, int y, const wchar_t *s) NOTHROW
3866 int width = WCWIDTH (t);
3870 scr_overlay_set (x++, y, t);
3877 rxvt_term::scr_swap_overlay () NOTHROW
3882 // hide cursor if it is within the overlay area
3883 if (IN_RANGE_EXC (screen.cur.col - ov.x, 0, ov.w)
3884 && IN_RANGE_EXC (screen.cur.row - ov.y, 0, ov.h))
3885 screen.flags &= ~Screen_VisibleCursor;
3887 // swap screen mem with overlay
3888 for (int y = ov.h; y--; )
3890 text_t *t1 = ov.text[y];
3891 rend_t *r1 = ov.rend[y];
3893 text_t *t2 = ROW(y + ov.y + view_start).t + ov.x;
3894 rend_t *r2 = ROW(y + ov.y + view_start).r + ov.x;
3896 for (int x = ov.w; x--; )
3898 text_t t = *t1; *t1++ = *t2; *t2++ = t;
3899 rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, FONTSET (r)->find_font (t));
3905 /* ------------------------------------------------------------------------- */