From 2e0e84d56a934bcc180d5638969fb78a7016b9f3 Mon Sep 17 00:00:00 2001 From: Jordan Callicoat Date: Wed, 28 Feb 2024 07:34:06 -0600 Subject: [PATCH] Add selectioncolors patch (#110) https://st.suckless.org/patches/selectioncolors/ Closes #91 --- config.def.h | 7 +++++++ patches.def.h | 7 +++++++ st.h | 3 +++ x.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/config.def.h b/config.def.h index 2dcaef7..f79e508 100644 --- a/config.def.h +++ b/config.def.h @@ -206,6 +206,13 @@ unsigned int defaultbg = 258; unsigned int defaultfg = 259; unsigned int defaultcs = 256; unsigned int defaultrcs = 257; +#if SELECTION_COLORS_PATCH +unsigned int selectionfg = 258; +unsigned int selectionbg = 259; +/* If 0 use selectionfg as foreground in order to have a uniform foreground-color */ +/* Else if 1 keep original foreground-color of each cell => more colors :) */ +static int ignoreselfg = 1; +#endif // SELECTION_COLORS_PATCH #if VIM_BROWSE_PATCH unsigned int const currentBg = 6, buffSize = 2048; diff --git a/patches.def.h b/patches.def.h index 013cc10..2150443 100644 --- a/patches.def.h +++ b/patches.def.h @@ -293,6 +293,13 @@ */ #define SCROLLBACK_MOUSE_ALTSCREEN_PATCH 0 +/* This patch adds the two color-settings selectionfg and selectionbg to config.def.h. + * Those define the fore- and background colors which are used when text on the screen is selected + * with the mouse. This removes the default behaviour which would simply reverse the colors. + * https://st.suckless.org/patches/selectioncolors/ + */ +#define SELECTION_COLORS_PATCH 0 + /* This is the single drawable buffer patch as outlined in the FAQ to get images * in w3m to display. While this patch does not break the alpha patch it images * are not shown in w3m if the alpha patch is applied. diff --git a/st.h b/st.h index 41af307..cea15f2 100644 --- a/st.h +++ b/st.h @@ -62,6 +62,9 @@ enum glyph_attribute { ATTR_SIXEL = 1 << 13, #endif // SIXEL_PATCH ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT, + #if SELECTION_COLORS_PATCH + ATTR_SELECTED = 1 << 14, + #endif // SELECTION_COLORS_PATCH #if UNDERCURL_PATCH ATTR_DIRTYUNDERLINE = 1 << 15, #endif // UNDERCURL_PATCH diff --git a/x.c b/x.c index a5cc7ef..e1104ae 100644 --- a/x.c +++ b/x.c @@ -2078,6 +2078,14 @@ xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, i #endif // SPOILER_PATCH } + #if SELECTION_COLORS_PATCH + if (base.mode & ATTR_SELECTED) { + bg = &dc.col[selectionbg]; + if (!ignoreselfg) + fg = &dc.col[selectionfg]; + } + #endif // SELECTION_COLORS_PATCH + if (base.mode & ATTR_BLINK && win.mode & MODE_BLINK) fg = bg; @@ -2605,7 +2613,11 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og) /* remove the old cursor */ if (selected(ox, oy)) + #if SELECTION_COLORS_PATCH + og.mode |= ATTR_SELECTED; + #else og.mode ^= ATTR_REVERSE; + #endif // SELECTION_COLORS_PATCH #if LIGATURES_PATCH /* Redraw the line where cursor was previously. * It will restore the ligatures broken by the cursor. */ @@ -2634,6 +2646,10 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og) if (IS_SET(MODE_REVERSE)) { g.mode |= ATTR_REVERSE; g.bg = defaultfg; + #if SELECTION_COLORS_PATCH + g.fg = defaultcs; + drawcol = dc.col[defaultrcs]; + #else if (selected(cx, cy)) { drawcol = dc.col[defaultcs]; g.fg = defaultrcs; @@ -2641,7 +2657,13 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og) drawcol = dc.col[defaultrcs]; g.fg = defaultcs; } + #endif // SELECTION_COLORS_PATCH } else { + #if SELECTION_COLORS_PATCH + g.fg = defaultbg; + g.bg = defaultcs; + drawcol = dc.col[defaultcs]; + #else if (selected(cx, cy)) { g.fg = defaultfg; g.bg = defaultrcs; @@ -2669,6 +2691,7 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og) } else drawcol = dc.col[g.bg]; #endif // DYNAMIC_CURSOR_COLOR_PATCH + #endif // SELECTION_COLORS_PATCH } /* draw the new one */ @@ -2898,7 +2921,11 @@ xdrawline(Line line, int x1, int y1, int x2) if (new.mode == ATTR_WDUMMY) continue; if (selected(x, y1)) + #if SELECTION_COLORS_PATCH + new.mode |= ATTR_SELECTED; + #else new.mode ^= ATTR_REVERSE; + #endif // SELECTION_COLORS_PATCH if (i > 0 && ATTRCMP(base, new)) { xdrawglyphfontspecs(specs, base, i, ox, y1, dmode); specs += i; @@ -2927,7 +2954,11 @@ xdrawline(Line line, int x1, int y1, int x2) if (new.mode == ATTR_WDUMMY) continue; if (selected(x, y1)) + #if SELECTION_COLORS_PATCH + new.mode |= ATTR_SELECTED; + #else new.mode ^= ATTR_REVERSE; + #endif // SELECTION_COLORS_PATCH if (i > 0 && ATTRCMP(base, new)) { xdrawglyphfontspecs(specs, base, i, ox, y1); specs += i;