Skip to content

Instantly share code, notes, and snippets.

@choppsv1
Last active May 27, 2016 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save choppsv1/73d51cedd3e8ec72e1c1 to your computer and use it in GitHub Desktop.
Save choppsv1/73d51cedd3e8ec72e1c1 to your computer and use it in GitHub Desktop.
True color (24-bit) terminal support for emacs-24.3
This diff is a modified version of a diff written by Rüdiger Sonderfeld.
The original diff can be found here:
http://emacs.1067599.n5.nabble.com/RFC-Add-tty-True-Color-support-tt299962.html
To enable the feature one must set one of 2 environment variables either
ITERM_24BIT or KONSOLE_DBUS_SESSION. The former will enable ITU T.416 mode, the
latter will use semi-colon seperators used by other terminals (and also
supported by iterm development branch).
I've added ITERM_24BIT as an environment variable to set that will cause emacs
to use the : seperated values as specified by ITU T.416. These are apparently
better to use and supported by iterm development branch.
Other chagnes:
- modified map_tty_color to set the color into the pixel field.
- modified the colors defined to be the standard X11 list.
- modified tty-color-desc to add missing colors on demand.
*** ./lisp/term/tty-colors.el.orig 2013-01-01 15:37:17.000000000 -0500
--- ./lisp/term/tty-colors.el 2014-09-02 19:29:50.000000000 -0400
***************
*** 1007,1017 ****
Value is a list of the form \(NAME INDEX R G B\). The returned NAME or
RGB value may not be the same as the argument COLOR, because the latter
might need to be approximated if it is not supported directly."
(and (stringp color)
(let ((color (tty-color-canonicalize color)))
! (or (assoc color (tty-color-alist frame))
! (let ((rgb (tty-color-standard-values color)))
! (and rgb (tty-color-approximate rgb frame)))))))
(defun tty-color-gray-shades (&optional display)
"Return the number of gray colors supported by DISPLAY's terminal.
--- 1007,1027 ----
Value is a list of the form \(NAME INDEX R G B\). The returned NAME or
RGB value may not be the same as the argument COLOR, because the latter
might need to be approximated if it is not supported directly."
+ ;; Here we are expeting to return (name idx (values)), be nice if we
+ ;; could just return the rgb, there is one use in
+ ;; read-face-attribute that uses the name unforutnately. To
+ ;; accomidate we just create entries with RGB values.
(and (stringp color)
(let ((color (tty-color-canonicalize color)))
! (or (assoc color (tty-color-alist frame))
! (let ((rgb (tty-color-standard-values color)) elt len)
! (if rgb
! (if (< (display-color-cells frame) 16777216)
! (tty-color-approximate rgb frame)
! ;; Add this value to tty-color-alist
! (setq elt (append (list color (length tty-defined-color-alist)) rgb))
! (tty-modify-color-alist elt frame)
! elt)))))))
(defun tty-color-gray-shades (&optional display)
"Return the number of gray colors supported by DISPLAY's terminal.
*** ./lisp/term/xterm.el.orig 2013-02-12 23:17:58.000000000 -0500
--- ./lisp/term/xterm.el 2014-09-02 18:41:42.000000000 -0400
***************
*** 641,646 ****
--- 641,655 ----
;; are more colors to support, compute them now.
(when (> ncolors 0)
(cond
+ ((= (display-color-cells (selected-frame)) 16777216) ; 24-bit xterm
+ (let ((idx (length xterm-standard-colors)))
+ ;; Insert standard X colors after the standard xterm ones
+ (mapc (lambda (color)
+ (if (not (assoc (car color) xterm-standard-colors))
+ (progn
+ (tty-color-define (car color) idx (cdr color))
+ (setq idx (1+ idx)))))
+ color-name-rgb-alist)))
((= ncolors 240) ; 256-color xterm
;; 216 non-gray colors first
(let ((r 0) (g 0) (b 0))
*** ./README-24BIT.orig 2014-09-03 11:52:55.000000000 -0400
--- ./README-24BIT 2014-09-03 11:54:21.000000000 -0400
***************
*** 0 ****
--- 1,19 ----
+ This diff is a modified version of a diff written by Rüdiger Sonderfeld.
+
+ The original diff can be found here:
+ http://emacs.1067599.n5.nabble.com/RFC-Add-tty-True-Color-support-tt299962.html
+
+ To enable the feature one must set one of 2 environment variables either
+ ITERM_24BIT or KONSOLE_DBUS_SESSION. The former will enable ITU T.416 mode, the
+ latter will use semi-colon seperators used by other terminals (and also
+ supported by iterm development branch). If you also use tmux with the 24bit
+ patch you may need to use the latter environment variable.
+
+ I've added ITERM_24BIT as an environment variable to set that will cause emacs
+ to use the : seperated values as specified by ITU T.416. These are apparently
+ better to use and supported by iterm development branch.
+
+ Other chagnes:
+ - modified map_tty_color to set the color into the pixel field.
+ - modified the colors defined to be the standard X11 list.
+ - modified tty-color-desc to add missing colors on demand.
*** ./src/dispextern.h.orig 2013-01-01 15:37:17.000000000 -0500
--- ./src/dispextern.h 2014-09-03 11:52:15.000000000 -0400
***************
*** 1677,1682 ****
--- 1677,1689 ----
#define FACE_TTY_DEFAULT_BG_COLOR ((unsigned long) -3)
+ DISPEXTERN_INLINE bool
+ face_tty_specified_24_bit_color (unsigned long color)
+ {
+ /* 24 bit colors have 24th but not 25th bit set */
+ return ((color & (0x03 << 24)) == (0x01 << 24));
+ }
+
/* Non-zero if FACE was realized for unibyte use. */
#define FACE_UNIBYTE_P(FACE) ((FACE)->charset < 0)
*** ./src/term.c.orig 2013-01-01 15:37:17.000000000 -0500
--- ./src/term.c 2014-09-02 18:59:47.000000000 -0400
***************
*** 1976,1993 ****
const char *ts;
char *p;
! ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
if (fg >= 0 && ts)
{
! p = tparam (ts, NULL, 0, (int) fg, 0, 0, 0);
OUTPUT (tty, p);
xfree (p);
}
! ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
if (bg >= 0 && ts)
{
! p = tparam (ts, NULL, 0, (int) bg, 0, 0, 0);
OUTPUT (tty, p);
xfree (p);
}
--- 1976,2015 ----
const char *ts;
char *p;
! if (face_tty_specified_24_bit_color(fg))
! ts = tty->standout_mode ? tty->TS_set_rgb_background : tty->TS_set_rgb_foreground;
! else
! ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground;
if (fg >= 0 && ts)
{
! if (!face_tty_specified_24_bit_color(fg))
! p = tparam (ts, NULL, 0, (int) fg, 0, 0, 0);
! else
! {
! const unsigned char r = (fg >> 16) & 0xFF,
! g = (fg >> 8) & 0xFF,
! b = fg & 0xFF;
! p = tparam (ts, NULL, 0, (int)r, (int)g, (int)b, 0);
! }
OUTPUT (tty, p);
xfree (p);
}
! if (face_tty_specified_24_bit_color(bg))
! ts = tty->standout_mode ? tty->TS_set_rgb_foreground : tty->TS_set_rgb_background;
! else
! ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background;
if (bg >= 0 && ts)
{
! if (!face_tty_specified_24_bit_color(bg))
! p = tparam (ts, NULL, 0, (int) bg, 0, 0, 0);
! else
! {
! const unsigned char r = (bg >> 16) & 0xFF,
! g = (bg >> 8) & 0xFF,
! b = bg & 0xFF;
! p = tparam (ts, NULL, 0, (int)r, (int)g, (int)b, 0);
! }
OUTPUT (tty, p);
xfree (p);
}
***************
*** 2091,2096 ****
--- 2113,2120 ----
struct terminal *t = get_tty_terminal (terminal, 0);
if (!t)
return make_number (0);
+ else if (t->display_info.tty->TS_set_rgb_foreground)
+ return make_number (16777216); /* 24 bit True Color */
else
return make_number (t->display_info.tty->TN_max_colors);
}
***************
*** 2106,2111 ****
--- 2130,2137 ----
static char *default_orig_pair;
static char *default_set_foreground;
static char *default_set_background;
+ static char *default_set_rgb_foreground;
+ static char *default_set_rgb_background;
/* Save or restore the default color-related capabilities of this
terminal. */
***************
*** 2126,2131 ****
--- 2152,2165 ----
default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
: NULL;
+ xfree (default_set_rgb_foreground);
+ default_set_rgb_foreground = tty->TS_set_rgb_foreground ? xstrdup (tty->TS_set_rgb_foreground)
+ : NULL;
+
+ xfree (default_set_rgb_background);
+ default_set_rgb_background = tty->TS_set_rgb_background ? xstrdup (tty->TS_set_rgb_background)
+ : NULL;
+
default_max_colors = tty->TN_max_colors;
default_max_pairs = tty->TN_max_pairs;
default_no_color_video = tty->TN_no_color_video;
***************
*** 2135,2140 ****
--- 2169,2176 ----
tty->TS_orig_pair = default_orig_pair;
tty->TS_set_foreground = default_set_foreground;
tty->TS_set_background = default_set_background;
+ tty->TS_set_rgb_foreground = default_set_rgb_foreground;
+ tty->TS_set_rgb_background = default_set_rgb_background;
tty->TN_max_colors = default_max_colors;
tty->TN_max_pairs = default_max_pairs;
tty->TN_no_color_video = default_no_color_video;
***************
*** 2159,2164 ****
--- 2195,2201 ----
tty->TN_max_pairs = 0;
tty->TN_no_color_video = 0;
tty->TS_set_foreground = tty->TS_set_background = tty->TS_orig_pair = NULL;
+ tty->TS_set_rgb_foreground = tty->TS_set_rgb_background = NULL;
break;
case 0: /* default colors, if any */
default:
***************
*** 2173,2182 ****
--- 2210,2238 ----
tty->TS_set_foreground = "\033[3%dm";
tty->TS_set_background = "\033[4%dm";
#endif
+ tty->TS_set_rgb_foreground = NULL;
+ tty->TS_set_rgb_background = NULL;
tty->TN_max_colors = 8;
tty->TN_max_pairs = 64;
tty->TN_no_color_video = 0;
break;
+ case 16777216: /* RGB colors */
+ tty->TS_orig_pair = "\033[0m";
+ #ifdef TERMINFO
+ tty->TS_set_foreground = "\033[3%p1%dm";
+ tty->TS_set_background = "\033[4%p1%dm";
+ tty->TS_set_rgb_foreground = "\033[38;2;%p1%d;%p2%d;%p3%dm";
+ tty->TS_set_rgb_background = "\033[48;2;%p1%d;%p2%d;%p3%dm";
+ #else
+ tty->TS_set_foreground = "\033[3%dm";
+ tty->TS_set_background = "\033[4%dm";
+ tty->TS_set_rgb_foreground = "\033[38;2;%d;%d;%dm";
+ tty->TS_set_rgb_background = "\033[48;2;%d;%d;%dm";
+ #endif
+ tty->TN_max_colors = 16777216;
+ /*tty->TN_max_pairs = 64; TODO */
+ tty->TN_no_color_video = 0;
+ break;
}
}
***************
*** 3207,3212 ****
--- 3263,3298 ----
tty->TN_no_color_video = tgetnum ("NC");
if (tty->TN_no_color_video == -1)
tty->TN_no_color_video = 0;
+
+ /* TODO Reliable way to detect: Konsole, iTerm2, st */
+ if (getenv ("KONSOLE_DBUS_SESSION"))
+ {
+ /* TODO This should be extracted from terminfo/termcap. */
+ #ifdef TERMINFO
+ tty->TS_set_rgb_foreground = "\033[38;2;%p1%d;%p2%d;%p3%dm";
+ tty->TS_set_rgb_background = "\033[48;2;%p1%d;%p2%d;%p3%dm";
+ #else
+ tty->TS_set_rgb_foreground = "\033[38;2;%d;%d;%dm";
+ tty->TS_set_rgb_background = "\033[48;2;%d;%d;%dm";
+ #endif
+ }
+ else if (getenv ("ITERM_24BIT"))
+ {
+ /* XXX chopps use ITU T.421 ':' separator */
+ /* TODO This should be extracted from terminfo/termcap. */
+ #ifdef TERMINFO
+ tty->TS_set_rgb_foreground = "\033[38:2:%p1%d:%p2%d:%p3%dm";
+ tty->TS_set_rgb_background = "\033[48:2:%p1%d:%p2%d:%p3%dm";
+ #else
+ tty->TS_set_rgb_foreground = "\033[38:2:%d:%d:%dm";
+ tty->TS_set_rgb_background = "\033[48:2:%d:%d:%dm";
+ #endif
+ }
+ else
+ {
+ tty->TS_set_rgb_foreground = NULL;
+ tty->TS_set_rgb_background = NULL;
+ }
}
tty_default_color_capabilities (tty, 1);
*** ./src/termchar.h.orig 2013-01-01 15:37:17.000000000 -0500
--- ./src/termchar.h 2014-09-02 18:41:42.000000000 -0400
***************
*** 154,159 ****
--- 154,163 ----
const char *TS_set_foreground;
const char *TS_set_background;
+ /* Support for 24bit RGB color terminals. */
+ const char *TS_set_rgb_foreground;
+ const char *TS_set_rgb_background;
+
int TF_hazeltine; /* termcap hz flag. */
int TF_insmode_motion; /* termcap mi flag: can move while in insert mode. */
int TF_standout_motion; /* termcap mi flag: can move while in standout mode. */
*** ./src/xfaces.c.orig 2013-01-01 15:37:17.000000000 -0500
--- ./src/xfaces.c 2014-09-02 19:26:32.000000000 -0400
***************
*** 400,406 ****
/* TTY color-related functions (defined in tty-colors.el). */
! static Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
/* The name of the function used to compute colors on TTYs. */
--- 400,406 ----
/* TTY color-related functions (defined in tty-colors.el). */
! static Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values, Qtty_color_canonicalize;
/* The name of the function used to compute colors on TTYs. */
***************
*** 998,1051 ****
if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
return 0;
! XSETFRAME (frame, f);
! color_desc = call2 (Qtty_color_desc, color, frame);
! if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
{
! Lisp_Object rgb;
! if (! INTEGERP (XCAR (XCDR (color_desc))))
! return 0;
! tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
! rgb = XCDR (XCDR (color_desc));
! if (! parse_rgb_list (rgb, tty_color))
! return 0;
! /* Should we fill in STD_COLOR too? */
! if (std_color)
! {
! /* Default STD_COLOR to the same as TTY_COLOR. */
! *std_color = *tty_color;
! /* Do a quick check to see if the returned descriptor is
! actually _exactly_ equal to COLOR, otherwise we have to
! lookup STD_COLOR separately. If it's impossible to lookup
! a standard color, we just give up and use TTY_COLOR. */
! if ((!STRINGP (XCAR (color_desc))
! || NILP (Fstring_equal (color, XCAR (color_desc))))
! && !NILP (Ffboundp (Qtty_color_standard_values)))
! {
! /* Look up STD_COLOR separately. */
! rgb = call1 (Qtty_color_standard_values, color);
! if (! parse_rgb_list (rgb, std_color))
! return 0;
! }
! }
! return 1;
}
- else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
- /* We were called early during startup, and the colors are not
- yet set up in tty-defined-color-alist. Don't return a failure
- indication, since this produces the annoying "Unable to
- load color" messages in the *Messages* buffer. */
- return 1;
- else
- /* tty-color-desc seems to have returned a bad value. */
- return 0;
}
/* A version of defined_color for non-X frames. */
--- 998,1077 ----
if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
return 0;
! if (f->output_method == output_termcap
! && f->output_data.tty->display_info->TS_set_rgb_foreground
! && !NILP (Ffboundp (Qtty_color_standard_values)))
! {
! /* Terminal supports 3 byte RGB colors. */
! if (!NILP (Ffboundp (Qtty_color_canonicalize)))
! color = call1(Qtty_color_canonicalize, color);
!
! color_desc = call1 (Qtty_color_standard_values, color);
! if (! parse_rgb_list (color_desc, tty_color))
! return 0;
!
! /* Map XColor to 3 byte values. */
! tty_color->pixel = 1 << 24 /* Set bit 24 to mark RGB values. */
! | (tty_color->red / 256) << 16
! | (tty_color->green / 256) << 8
! | (tty_color->blue / 256);
!
! if (std_color)
! *std_color = *tty_color;
! return 1;
! }
! else
{
! XSETFRAME (frame, f);
! color_desc = call2 (Qtty_color_desc, color, frame);
! if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
! {
! Lisp_Object rgb;
! if (! INTEGERP (XCAR (XCDR (color_desc))))
! return 0;
! tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
! rgb = XCDR (XCDR (color_desc));
! if (! parse_rgb_list (rgb, tty_color))
! return 0;
! /* Should we fill in STD_COLOR too? */
! if (std_color)
! {
! /* Default STD_COLOR to the same as TTY_COLOR. */
! *std_color = *tty_color;
! /* Do a quick check to see if the returned descriptor is
! actually _exactly_ equal to COLOR, otherwise we have to
! lookup STD_COLOR separately. If it's impossible to lookup
! a standard color, we just give up and use TTY_COLOR. */
! if ((!STRINGP (XCAR (color_desc))
! || NILP (Fstring_equal (color, XCAR (color_desc))))
! && !NILP (Ffboundp (Qtty_color_standard_values)))
! {
! /* Look up STD_COLOR separately. */
! rgb = call1 (Qtty_color_standard_values, color);
! if (! parse_rgb_list (rgb, std_color))
! return 0;
! }
! }
!
! return 1;
! }
! else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
! /* We were called early during startup, and the colors are not
! yet set up in tty-defined-color-alist. Don't return a failure
! indication, since this produces the annoying "Unable to
! load color" messages in the *Messages* buffer. */
! return 1;
! else
! /* tty-color-desc seems to have returned a bad value. */
! return 0;
}
}
/* A version of defined_color for non-X frames. */
***************
*** 5824,5829 ****
--- 5850,5856 ----
unsigned long default_pixel =
foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
unsigned long pixel = default_pixel;
+ XColor true_color;
#ifdef MSDOS
unsigned long default_other_pixel =
foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
***************
*** 5842,5848 ****
{
/* Associations in tty-defined-color-alist are of the form
(NAME INDEX R G B). We need the INDEX part. */
! pixel = XINT (XCAR (XCDR (def)));
}
if (pixel == default_pixel && STRINGP (color))
--- 5869,5886 ----
{
/* Associations in tty-defined-color-alist are of the form
(NAME INDEX R G B). We need the INDEX part. */
! if (f->output_method == output_termcap
! && f->output_data.tty->display_info->TS_set_rgb_foreground
! && parse_rgb_list (XCDR (XCDR(def)), &true_color))
! {
! /* Map XColor to 3 byte values. */
! pixel = 1 << 24 /* Set bit 24 to mark RGB values. */
! | (true_color.red / 256) << 16
! | (true_color.green / 256) << 8
! | (true_color.blue / 256);
! }
! else
! pixel = XINT (XCAR (XCDR (def)));
}
if (pixel == default_pixel && STRINGP (color))
***************
*** 6555,6560 ****
--- 6593,6599 ----
DEFSYM (Qmouse, "mouse");
DEFSYM (Qmode_line_inactive, "mode-line-inactive");
DEFSYM (Qvertical_border, "vertical-border");
+ DEFSYM (Qtty_color_canonicalize, "tty-color-canonicalize");
DEFSYM (Qtty_color_desc, "tty-color-desc");
DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
DEFSYM (Qtty_color_by_index, "tty-color-by-index");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment