2 * Definitions for the ANSI color tty outputs.
3 * Author: Bill Petheram <petheram@acm.org>
4 * Markku Rossi <mtr@iki.fi>
10 /* The current face as an ANSI str. */
13 ansi_face_stack = list();
14 ansi_face_stack_pos = 0;
16 ansi_color_table = list();
17 ansi_color_table_size = 0;
19 sub ansi_define_color(ansi, r, g, b)
21 ansi_color_table[ansi_color_table_size] = list(ansi, r, g, b);
22 ansi_color_table_size = ansi_color_table_size + 1;
25 ansi_define_color(30, 0, 0, 0); /* black */
26 ansi_define_color(31, 205, 0, 0); /* red3 */
27 ansi_define_color(32, 0, 205, 0); /* green3 */
28 ansi_define_color(33, 205, 205, 0); /* yellow3 */
29 ansi_define_color(34, 0, 0, 205); /* blue3 */
30 ansi_define_color(35, 205, 0, 205); /* magenta3 */
31 ansi_define_color(36, 0, 205, 205); /* cyan3 */
32 ansi_define_color(37, 229, 229, 229); /* gray90 */
34 ansi_define_color(90, 77, 77, 77); /* gray30 */
35 ansi_define_color(91, 255, 0, 0); /* red */
36 ansi_define_color(92, 0, 255, 0); /* green */
37 ansi_define_color(93, 255, 255, 0); /* yellow */
38 ansi_define_color(94, 0, 0, 255); /* blue */
39 ansi_define_color(95, 255, 0, 255); /* magenta */
40 ansi_define_color(96, 0, 255, 255); /* cyan */
41 ansi_define_color(97, 255, 255, 255); /* white */
43 sub ansi_count_delta(a, b)
53 sub ansi_count_color_delta(ansi_color, r, g, b)
57 delta += ansi_count_delta(ansi_color[1], r);
58 delta += ansi_count_delta(ansi_color[2], g);
59 delta += ansi_count_delta(ansi_color[3], b);
64 sub map_color(r, g, b)
66 local i, min = 65536, min_color = 0, delta;
68 for (i = 0; i < ansi_color_table_size; i++)
70 delta = ansi_count_color_delta(ansi_color_table[i], r, g, b);
73 min_color = ansi_color_table[i][0];
78 /* Don't accept too bad matches. */
84 LANGUAGE_SPECIALS = /\n/;
86 sub language_print(str)
89 str = regsuball(str, /\n/, concat("\033[0m\n", ansi_face_str));
103 sub ansi_set_face(face)
105 local b = "", i = "", fg ="", bg = "";
112 fg = concat(";", string(face[fg_color]));
114 bg = concat(";", string(face[bg_color] + 10));
116 ansi_face_str = concat("\033[0", b, i, fg, bg, "m");
118 print(ansi_face_str);
123 ansi_face_stack[ansi_face_stack_pos++] = face;
129 if (ansi_face_stack_pos > 0)
130 /* Just to make sure that we don't fail on some broken
131 highlighting rules. */
132 ansi_face_stack_pos--;
134 if (ansi_face_stack_pos == 0)
140 ansi_set_face(ansi_face_stack[ansi_face_stack_pos - 1]);