-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
bbcolors
executable file
·386 lines (268 loc) · 9.52 KB
/
bbcolors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/env perl
#
# bbcolors -- A text color scheme manager for BBEdit and TextWrangler.
#
# Copyright (c) 2006 John Gruber
# <http://daringfireball.net/projects/bbcolors/>
#
# This program is free software; you may redistribute it and/or
# modify it under the same terms as Perl itself.
#
use warnings;
use strict;
use Getopt::Long;
use Pod::Usage;
use encoding 'utf8';
use utf8;
use vars qw($VERSION);
$VERSION = '1.0.1';
# Sun 15 Oct 2006
my $delete_setname;
my $load_setname;
my $save_setname;
my $language;
my $bbopen = 0; # If set, open a disk browser window showing the current saved color sets.
my $reckless = 0; # If set, apply defaults changes even if app is running.
my $zap = 0; # If set, clear all existing text color prefs, resetting to BBEdit defaults.
my %app = (
name => "BBEdit",
bundle => "com.barebones.bbedit",
creator => "R*ch",
tool => "bbedit",
);
# This should work even on systems not using English; the true names aren't localized:
my $bbcolors_support_folder = "$ENV{HOME}/Library/Application Support/BBColors";
unless (-d $bbcolors_support_folder) {
mkdir $bbcolors_support_folder
or die "Can't create application support folder: $!";
}
GetOptions (
"delete=s" => \$delete_setname,
"list" => \&list_color_sets,
"load=s" => \$load_setname,
"save=s" => \$save_setname,
"reckless" => \$reckless,
"language=s" => \$language,
"bbedit" => , # Do nothing; uses BBEdit by default
"textwrangler|tw" => sub {%app = (
name => "TextWrangler",
bundle => "com.barebones.textwrangler",
creator => "!Rch",
tool => "edit",
)},
"open" => \&open_sets_folder_in_finder,
"bbopen" => \$bbopen,
"zap" => \$zap,
'help|?' => sub { pod2usage(1); },
'man' => sub { pod2usage(-verbose => 2); },
"version" => sub { print "Version $VERSION\n"; exit 0; },
) or pod2usage(2);
# Save first:
if ($save_setname) {
open (SETFILE, '>:utf8', "$bbcolors_support_folder/$save_setname.bbcolors")
or die "Can't open file for writing: $!";
my @lines = qx(defaults read $app{bundle} | egrep \\"Color:);
if ($language) {
# Only save color prefs for the language "$language"
$language = quotemeta $language;
@lines = grep(/"Color:.+:$language/, @lines);
}
my $len = 0; # Longest key string
foreach (@lines) {
if (m{^\s*(.+?) = (.+);\s*$}ms) {
my $key = $1; # Should already have quotes around it
my $val = $2;
$key = unescape_plist_string($key);
$len = length $key if (length $key > $len);
$_ = "$key = $val;";
}
}
# Loop through again to print; I can't figure out a way to do this
# in one loop and still get the pretty-printed alignment.
foreach (@lines) {
if (m{^\s*(.+?) = (.+);\s*$}ms) {
my $key = $1; # Should already have quotes around it
my $val = $2;
$val = " " . $val unless $val =~ m/^"/; # Looks better with a space before non-string values.
printf SETFILE ("%-${len}s = %s;\n", $key, $val);
}
}
close SETFILE;
}
# Then Zap:
if ($zap) {
if ( is_editor_running($app{name}) and ! $reckless ) {
print "Won't zap prefs while $app{name} is running.\n";
exit 0;
}
else {
my @lines = qx(defaults read $app{bundle} | egrep \\"Color:);
if ($language) {
# Only zap color prefs for the language "$language"
$language = quotemeta $language;
@lines = grep(/"Color:[^"]+:$language/, @lines);
}
foreach (@lines) {
if (m{^\s*(.+?) = (.+);\s*$}ms) {
my $key = $1; # Should already have quotes around it
$key = unescape_plist_string($key);
my $res = qx(defaults delete $app{bundle} $key );
}
else {
print STDERR "Can't parse line: «$_»\n";
}
}
}
}
# Then load:
if ($load_setname) {
if ( is_editor_running($app{name}) and ! $reckless ) {
print "Won't load prefs while $app{name} is running.\n";
exit 0;
}
else {
open (SETFILE, '<:utf8', "$bbcolors_support_folder/$load_setname.bbcolors")
or do {print "No such scheme: '$load_setname'\nTry 'bbcolors -list' to see available schemes.\n"; exit;};
while (<SETFILE>) {
#
# These lines should look like:
# "Color:CTagsIdentifier" = "rgb(39321,0,26214)";
# "Color:ColorAttributesSeparately" = 1;
#
next if m{^[ \t]*//}ms; # Treat // lines as comments.
if (m{^\s*(.+?)[ \t]*=[ \t]*(.+);\s*$}ms) {
my $key = $1; # These two captures keep the quotes around the key...
my $val = $2; # ...and value.
my $type = "-string";
if ($val =~ m{^\d+$}) {
# If the value is nothing but digits, treat it as an integer.
# This seems to work even if it's actually a boolean. (I.e.
# there's no need to guess whether 1 and 0 are bools or ints.)
$type = "-integer";
}
if ($language) {
my $quoted = quotemeta $language;
next if ($key !~ m/$quoted"$/);
}
my $res = qx(defaults write $app{bundle} $key $type $val );
}
else {
# Do nothing with lines that don't match.
}
}
}
}
# Then delete:
if ($delete_setname) {
if (-f "$bbcolors_support_folder/$delete_setname.bbcolors") {
unlink "$bbcolors_support_folder/$delete_setname.bbcolors"
or die "Unable to delete $bbcolors_support_folder/$delete_setname.bbcolors: $!";
}
else {
print "Color scheme '$delete_setname' does not exist.\n";
}
}
# bbopen last; that way it won't fire up your editor before you diddle with the prefs
if ($bbopen) {
system($app{tool}, $bbcolors_support_folder);
}
#### End of main script. ####
sub unescape_plist_string {
my $str = shift;
# Un-double backslashes
$str =~ s{\\\\}{\\}g;
# Unescape Unicode chars:
$str =~ s{ \\U([[:xdigit:]]{4}) }{ chr(hex($1)) }egx;
return $str;
}
sub escape_plist_string {
my $str = shift;
# Encode Unicode chars into plist \U hex escapes;
# e.g. "Langüage" -> "Lang\U00fcage"
$str =~ s{ (\P{IsASCII}) }{ sprintf("\\U%04x", ord($1)) }egx;
# Double all backslashes:
$str =~ s{\\}{\\\\}g;
return $str;
}
sub is_editor_running {
my $app_name = shift;
my @processes = qx( ps -xc -o command );
chomp @processes;
my $count = grep { $_ eq $app_name } @processes;
return $count;
}
sub list_color_sets {
my $count = 0;
opendir(FOLDER, $bbcolors_support_folder)
or die "Can't open $bbcolors_support_folder: $!";
while( my $file = readdir FOLDER ) {
if (-T "$bbcolors_support_folder/$file" and $file =~ m/[.]bbcolors$/) {
$count++;
$file =~ s/[.]bbcolors$//; # trim the extension
print "$file\n";
}
}
print "No color sets available in $bbcolors_support_folder\n" unless $count;
closedir(FOLDER);
}
sub open_sets_folder_in_finder {
system("open", $bbcolors_support_folder);
}
__END__
=head1 NAME
bbcolors - Save and load text color sets for BBEdit.
=head1 SYNOPSIS
bbcolors -save "scheme name"
bbcolors -load "scheme name"
bbcolors -man
=head1 DESCRIPTION
B<bbcolors> saves and loads "sets" of text color preferences for BBEdit.
=head1 OPTIONS
=over 8
=item B<-delete> F<setname>
Delete the saved bbcolors scheme named 'setname'. Does not effect current BBEdit preferences.
=item B<-list>
List all color sets.
=item B<-load> F<setname>
Replace current BBEdit text colors with scheme named "setname".
=item B<-save> F<setname>
Save current BBEdit text colors to scheme named "setname', overwriting any existing scheme with that name.
=item B<-zap>
Deletes all current BBEdit (or TextWrangler) color settings, including those keyed to specific languages. For example, if you set custom colors for the Perl language in BBEdit, and then use bbcolors to load a new colorset that does not contain Perl-specific colors, your previously-set custom colors for Perl will remain in place. If you use the B<-zap> option when loading the new scheme, however, your custom color settings will be deleted, and your Perl files will be displayed using the default colors of the just-loaded color scheme.
=item B<-lang> F<"language name">
Target a specific language when saving, loading, or zapping.
=over 4
=item *
If saving, only save those colors set for the specified language. (The <-save> command normally saves all colors.)
=item *
If loading, only load those colors from the named scheme which are keyed to the specified language.
=item *
If zapping, only zap the preferences keyed to the specified language.
=back
=item B<-open>
Open the ~/Library/Application Support/BBColors/ folder in the Finder. If the folder doesn't exist, it will be created.
=item B<-bbopen>
Open the ~/Library/Application Support/BBColors/ folder in a new disk browser window using BBEdit (or TextWrangler, if used in conjunction with the B<-tw> option). If the folder doesn't exist, it will be created.
=item B<-bbedit>
Explicitly use with BBEdit (as opposed to TextWrangler). This option is on by default.
=item B<-textwrangler>
=item B<-tw>
Use TextWrangler instead of BBEdit.
=item B<-help>
Print a brief help message and exit.
=item B<-man>
Print the full bbcolors manual page.
=item B<-version>
Print the current bbcolors version number.
=back
=head1 A NOTE ON TEXT ENCODING
B<bbcolors> should work just fine with languages modules and saved color sets whose names contain non-ASCII characters. The only provision is that all input and output is assumed to be UTF-8.
=head1 VERSION HISTORY
=over 8
=item 1.0.1 - Sun 15 Oct 2006
Switched to a more efficient method of testing whether BBEdit and TextWrangler are currently running. Cleaned up the documentation slightly, replacing several instances of "color set" with "color scheme".
=back
=head1 COPYRIGHT AND LICENSE
Copyright (c) 2006 John Gruber. <http://daringfireball.net/>
This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
=cut