]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - vim/term2gui.pl
vim/vimrc: move helper functions to the top
[config/dotfiles.git] / vim / term2gui.pl
1 #!/usr/bin/perl
2
3 # Convert a terminal color scheme for 256 color terminals to a GUI color
4 # scheme by adding the appropriate gui* options.
5
6 # Copyright (C) 2012-2014  Simon Ruderich
7 #
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 3 of the License, or
11 # (at your option) any later version.
12 #
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.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21
22 use strict;
23 use warnings;
24
25
26 sub xterm2rgb {
27     my ($color) = @_;
28
29     # Taken from the urxvt-8.2-256color.patch in Debian's urxvt-unicode
30     # package 9.07-2.
31     my @colors = (
32         'rgb:00/00/00',
33         'rgb:00/00/5f',
34         'rgb:00/00/87',
35         'rgb:00/00/af',
36         'rgb:00/00/d7',
37         'rgb:00/00/ff',
38         'rgb:00/5f/00',
39         'rgb:00/5f/5f',
40         'rgb:00/5f/87',
41         'rgb:00/5f/af',
42         'rgb:00/5f/d7',
43         'rgb:00/5f/ff',
44         'rgb:00/87/00',
45         'rgb:00/87/5f',
46         'rgb:00/87/87',
47         'rgb:00/87/af',
48         'rgb:00/87/d7',
49         'rgb:00/87/ff',
50         'rgb:00/af/00',
51         'rgb:00/af/5f',
52         'rgb:00/af/87',
53         'rgb:00/af/af',
54         'rgb:00/af/d7',
55         'rgb:00/af/ff',
56         'rgb:00/d7/00',
57         'rgb:00/d7/5f',
58         'rgb:00/d7/87',
59         'rgb:00/d7/af',
60         'rgb:00/d7/d7',
61         'rgb:00/d7/ff',
62         'rgb:00/ff/00',
63         'rgb:00/ff/5f',
64         'rgb:00/ff/87',
65         'rgb:00/ff/af',
66         'rgb:00/ff/d7',
67         'rgb:00/ff/ff',
68         'rgb:5f/00/00',
69         'rgb:5f/00/5f',
70         'rgb:5f/00/87',
71         'rgb:5f/00/af',
72         'rgb:5f/00/d7',
73         'rgb:5f/00/ff',
74         'rgb:5f/5f/00',
75         'rgb:5f/5f/5f',
76         'rgb:5f/5f/87',
77         'rgb:5f/5f/af',
78         'rgb:5f/5f/d7',
79         'rgb:5f/5f/ff',
80         'rgb:5f/87/00',
81         'rgb:5f/87/5f',
82         'rgb:5f/87/87',
83         'rgb:5f/87/af',
84         'rgb:5f/87/d7',
85         'rgb:5f/87/ff',
86         'rgb:5f/af/00',
87         'rgb:5f/af/5f',
88         'rgb:5f/af/87',
89         'rgb:5f/af/af',
90         'rgb:5f/af/d7',
91         'rgb:5f/af/ff',
92         'rgb:5f/d7/00',
93         'rgb:5f/d7/5f',
94         'rgb:5f/d7/87',
95         'rgb:5f/d7/af',
96         'rgb:5f/d7/d7',
97         'rgb:5f/d7/ff',
98         'rgb:5f/ff/00',
99         'rgb:5f/ff/5f',
100         'rgb:5f/ff/87',
101         'rgb:5f/ff/af',
102         'rgb:5f/ff/d7',
103         'rgb:5f/ff/ff',
104         'rgb:87/00/00',
105         'rgb:87/00/5f',
106         'rgb:87/00/87',
107         'rgb:87/00/af',
108         'rgb:87/00/d7',
109         'rgb:87/00/ff',
110         'rgb:87/5f/00',
111         'rgb:87/5f/5f',
112         'rgb:87/5f/87',
113         'rgb:87/5f/af',
114         'rgb:87/5f/d7',
115         'rgb:87/5f/ff',
116         'rgb:87/87/00',
117         'rgb:87/87/5f',
118         'rgb:87/87/87',
119         'rgb:87/87/af',
120         'rgb:87/87/d7',
121         'rgb:87/87/ff',
122         'rgb:87/af/00',
123         'rgb:87/af/5f',
124         'rgb:87/af/87',
125         'rgb:87/af/af',
126         'rgb:87/af/d7',
127         'rgb:87/af/ff',
128         'rgb:87/d7/00',
129         'rgb:87/d7/5f',
130         'rgb:87/d7/87',
131         'rgb:87/d7/af',
132         'rgb:87/d7/d7',
133         'rgb:87/d7/ff',
134         'rgb:87/ff/00',
135         'rgb:87/ff/5f',
136         'rgb:87/ff/87',
137         'rgb:87/ff/af',
138         'rgb:87/ff/d7',
139         'rgb:87/ff/ff',
140         'rgb:af/00/00',
141         'rgb:af/00/5f',
142         'rgb:af/00/87',
143         'rgb:af/00/af',
144         'rgb:af/00/d7',
145         'rgb:af/00/ff',
146         'rgb:af/5f/00',
147         'rgb:af/5f/5f',
148         'rgb:af/5f/87',
149         'rgb:af/5f/af',
150         'rgb:af/5f/d7',
151         'rgb:af/5f/ff',
152         'rgb:af/87/00',
153         'rgb:af/87/5f',
154         'rgb:af/87/87',
155         'rgb:af/87/af',
156         'rgb:af/87/d7',
157         'rgb:af/87/ff',
158         'rgb:af/af/00',
159         'rgb:af/af/5f',
160         'rgb:af/af/87',
161         'rgb:af/af/af',
162         'rgb:af/af/d7',
163         'rgb:af/af/ff',
164         'rgb:af/d7/00',
165         'rgb:af/d7/5f',
166         'rgb:af/d7/87',
167         'rgb:af/d7/af',
168         'rgb:af/d7/d7',
169         'rgb:af/d7/ff',
170         'rgb:af/ff/00',
171         'rgb:af/ff/5f',
172         'rgb:af/ff/87',
173         'rgb:af/ff/af',
174         'rgb:af/ff/d7',
175         'rgb:af/ff/ff',
176         'rgb:d7/00/00',
177         'rgb:d7/00/5f',
178         'rgb:d7/00/87',
179         'rgb:d7/00/af',
180         'rgb:d7/00/d7',
181         'rgb:d7/00/ff',
182         'rgb:d7/5f/00',
183         'rgb:d7/5f/5f',
184         'rgb:d7/5f/87',
185         'rgb:d7/5f/af',
186         'rgb:d7/5f/d7',
187         'rgb:d7/5f/ff',
188         'rgb:d7/87/00',
189         'rgb:d7/87/5f',
190         'rgb:d7/87/87',
191         'rgb:d7/87/af',
192         'rgb:d7/87/d7',
193         'rgb:d7/87/ff',
194         'rgb:d7/af/00',
195         'rgb:d7/af/5f',
196         'rgb:d7/af/87',
197         'rgb:d7/af/af',
198         'rgb:d7/af/d7',
199         'rgb:d7/af/ff',
200         'rgb:d7/d7/00',
201         'rgb:d7/d7/5f',
202         'rgb:d7/d7/87',
203         'rgb:d7/d7/af',
204         'rgb:d7/d7/d7',
205         'rgb:d7/d7/ff',
206         'rgb:d7/ff/00',
207         'rgb:d7/ff/5f',
208         'rgb:d7/ff/87',
209         'rgb:d7/ff/af',
210         'rgb:d7/ff/d7',
211         'rgb:d7/ff/ff',
212         'rgb:ff/00/00',
213         'rgb:ff/00/5f',
214         'rgb:ff/00/87',
215         'rgb:ff/00/af',
216         'rgb:ff/00/d7',
217         'rgb:ff/00/ff',
218         'rgb:ff/5f/00',
219         'rgb:ff/5f/5f',
220         'rgb:ff/5f/87',
221         'rgb:ff/5f/af',
222         'rgb:ff/5f/d7',
223         'rgb:ff/5f/ff',
224         'rgb:ff/87/00',
225         'rgb:ff/87/5f',
226         'rgb:ff/87/87',
227         'rgb:ff/87/af',
228         'rgb:ff/87/d7',
229         'rgb:ff/87/ff',
230         'rgb:ff/af/00',
231         'rgb:ff/af/5f',
232         'rgb:ff/af/87',
233         'rgb:ff/af/af',
234         'rgb:ff/af/d7',
235         'rgb:ff/af/ff',
236         'rgb:ff/d7/00',
237         'rgb:ff/d7/5f',
238         'rgb:ff/d7/87',
239         'rgb:ff/d7/af',
240         'rgb:ff/d7/d7',
241         'rgb:ff/d7/ff',
242         'rgb:ff/ff/00',
243         'rgb:ff/ff/5f',
244         'rgb:ff/ff/87',
245         'rgb:ff/ff/af',
246         'rgb:ff/ff/d7',
247         'rgb:ff/ff/ff',
248         'rgb:08/08/08',
249         'rgb:12/12/12',
250         'rgb:1c/1c/1c',
251         'rgb:26/26/26',
252         'rgb:30/30/30',
253         'rgb:3a/3a/3a',
254         'rgb:44/44/44',
255         'rgb:4e/4e/4e',
256         'rgb:58/58/58',
257         'rgb:62/62/62',
258         'rgb:6c/6c/6c',
259         'rgb:76/76/76',
260         'rgb:80/80/80',
261         'rgb:8a/8a/8a',
262         'rgb:94/94/94',
263         'rgb:9e/9e/9e',
264         'rgb:a8/a8/a8',
265         'rgb:b2/b2/b2',
266         'rgb:bc/bc/bc',
267         'rgb:c6/c6/c6',
268         'rgb:d0/d0/d0',
269         'rgb:da/da/da',
270         'rgb:e4/e4/e4',
271         'rgb:ee/ee/ee',
272     );
273
274     return if $color < 16 or $color > 255; # undef
275
276     my $value = $colors[$color - 16];
277     $value =~ s{^rgb:}{};
278     $value =~ s{/}{}g;
279
280     return '#' . $value;
281 }
282
283
284 while (<STDIN>) {
285     # Only handle lines with highlight commands.
286     if (/^\s*\bhi(?:ghlight)?\b/) {
287         foreach my $setting (m/\bcterm(?:fg|bg)?=\S+/g) {
288             my ($name, $value) = split /=/, $setting;
289
290             my $gui_name = $name;
291             $gui_name =~ s/cterm/gui/;
292
293             my $new_value;
294             if ($name eq 'cterm') {
295                 $new_value = $value;
296             } elsif ($value eq 'NONE') {
297                 $new_value = 'NONE';
298             } else {
299                 $new_value = xterm2rgb($value);
300             }
301
302             # Update existing value.
303             if (/\b\Q$gui_name\E=/) {
304                 s/\Q$gui_name\E=\S+/$gui_name=$new_value/;
305             # Append at the end of the line.
306             } else {
307                 s/\n/ $gui_name=$new_value\n/;
308             }
309         }
310     }
311
312     print;
313 }