1 rizwank 1.1 /*
2 * Definitions for the ANSI color tty outputs.
3 * Author: Bill Petheram <petheram@acm.org>
4 * Markku Rossi <mtr@iki.fi>
5 */
6
7 state lang_ansi
8 {
9 BEGIN {
10 /* The current face as an ANSI str. */
11 ansi_face_str = 0;
12
13 ansi_face_stack = list();
14 ansi_face_stack_pos = 0;
15
16 ansi_color_table = list();
17 ansi_color_table_size = 0;
18
19 sub ansi_define_color(ansi, r, g, b)
20 {
21 ansi_color_table[ansi_color_table_size] = list(ansi, r, g, b);
22 rizwank 1.1 ansi_color_table_size = ansi_color_table_size + 1;
23 }
24
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 */
33
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 */
42
43 rizwank 1.1 sub ansi_count_delta(a, b)
44 {
45 local delta = a - b;
46
47 if (delta < 0)
48 delta = 0 - delta;
49
50 return delta * delta;
51 }
52
53 sub ansi_count_color_delta(ansi_color, r, g, b)
54 {
55 local delta = 0;
56
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);
60
61 return delta;
62 }
63
64 rizwank 1.1 sub map_color(r, g, b)
65 {
66 local i, min = 65536, min_color = 0, delta;
67
68 for (i = 0; i < ansi_color_table_size; i++)
69 {
70 delta = ansi_count_color_delta(ansi_color_table[i], r, g, b);
71 if (delta < min)
72 {
73 min_color = ansi_color_table[i][0];
74 min = delta;
75 }
76 }
77 if (min > 16384)
78 /* Don't accept too bad matches. */
79 min_color = 0;
80
81 return min_color;
82 }
83
84 LANGUAGE_SPECIALS = /\n/;
85 rizwank 1.1
86 sub language_print(str)
87 {
88 if (ansi_face_str)
89 str = regsuball(str, /\n/, concat("\033[0m\n", ansi_face_str));
90
91 print(str);
92 }
93
94 sub header()
95 {
96 }
97
98 sub trailer()
99 {
100 print("\033[0m");
101 }
102
103 sub ansi_set_face(face)
104 {
105 local b = "", i = "", fg ="", bg = "";
106 rizwank 1.1
107 if (face[boldp])
108 b = ";1";
109 if (face[italicp])
110 i = ";4";
111 if (face[fg_color])
112 fg = concat(";", string(face[fg_color]));
113 if (face[bg_color])
114 bg = concat(";", string(face[bg_color] + 10));
115
116 ansi_face_str = concat("\033[0", b, i, fg, bg, "m");
117
118 print(ansi_face_str);
119 }
120
121 sub face_on(face)
122 {
123 ansi_face_stack[ansi_face_stack_pos++] = face;
124 ansi_set_face(face);
125 }
126
127 rizwank 1.1 sub face_off(face)
128 {
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--;
133
134 if (ansi_face_stack_pos == 0)
135 {
136 print("\033[0m");
137 ansi_face_str = 0;
138 }
139 else
140 ansi_set_face(ansi_face_stack[ansi_face_stack_pos - 1]);
141 }
142
143 return;
144 }
145 }
146
147
148 rizwank 1.1 /*
149 Local variables:
150 mode: c
151 End:
152 */
|