forked from liyanage/xcode-text-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-cheat-sheet.pl
More file actions
executable file
·194 lines (151 loc) · 5.13 KB
/
Copy pathmake-cheat-sheet.pl
File metadata and controls
executable file
·194 lines (151 loc) · 5.13 KB
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
#!/usr/bin/perl
#
# Format Xcode text macro list
#
use strict;
use warnings;
use XML::LibXML;
exit Tool->new(@ARGV)->run();
package Tool;
sub new {
my $self = shift;
my %args = @_;
my $class = ref($self) || $self;
$self = bless {args => \%args}, $class;
$self->init();
return $self;
}
sub init {
my $self = shift;
$self->{keyboard_shortcuts} = {};
$self->{macro_elements} = {};
}
sub run {
my $self = shift;
$self->read_keyboard_shortcuts();
$self->read_macro_definitions();
$self->generate_output();
return 0;
}
sub read_keyboard_shortcuts {
my $self = shift;
my $xcode_prefs = $self->read_plist("$ENV{HOME}/Library/Preferences/com.apple.Xcode.plist");
my $active_key_binding = $xcode_prefs->findvalue('//string[preceding-sibling::key[1][. = "PBXActiveKeyBindings"]]');
my $path = "$ENV{HOME}/Library/Application Support/Xcode/Key Bindings/$active_key_binding.pbxkeys";
return unless (-e $path); # no personalized key bindings available
my $bindings = $self->read_plist($path);
my @text_macro_nodes = $bindings->findnodes('//string[starts-with(., "textMacro_")]');
my @pairs = map {$_->findvalue('.') => $_->findvalue('preceding-sibling::key[1]')} @text_macro_nodes;
my %modifier_map = (
'^' => "\x{2303}",
'@' => "\x{2318}",
'~' => "\x{2325}",
'$' => "\x{21e7}",
);
while (my $macro_identifier = shift @pairs) {
my $shortcut = shift @pairs;
$macro_identifier = join('.', $macro_identifier =~ /_([^_]+)/g);
$shortcut =~ s/(.)/$modifier_map{$1} || $1/eg;
$shortcut =~ s/(\w)/uc($1)/eg;
$shortcut =~ s/\\\\/\\/g;
$self->{keyboard_shortcuts}->{$macro_identifier} = $shortcut;
}
}
sub read_macro_definitions {
my $self = shift;
my @macrofiles = (
'/Developer/Applications/Xcode.app/Contents/PlugIns/TextMacros.xctxtmacro/Contents/Resources/C.xctxtmacro',
'/Developer/Applications/Xcode.app/Contents/PlugIns/TextMacros.xctxtmacro/Contents/Resources/ObjectiveC.xctxtmacro',
glob("'$ENV{HOME}/Library/Application\ Support/Developer/Shared/Xcode/Specifications/'*.xctxtmacro"),
);
foreach my $macrofile (@macrofiles) {
$self->read_macrofile($macrofile);
}
my $doc = XML::LibXML::Document->new();
my $root = $doc->createElement('plist');
$doc->setDocumentElement($root);
my $array = $doc->createElement('array');
$root->appendChild($array);
foreach my $identifier (sort keys %{$self->{macro_elements}}) {
my $element = $self->{macro_elements}->{$identifier};
$array->appendChild($element);
}
$self->{doc} = $doc;
}
sub read_macrofile {
my $self = shift;
my ($path) = @_;
my $doc = $self->read_plist($path);
my %macrofile;
if($path =~ m|^/Developer/Applications/Xcode.app/.*/([^/]+)\.xctxtmacro$|) {
%macrofile = ("basename" => $1, "defined" => "xcode");
}
elsif($path =~ m|/([^/]+)\.xctxtmacro$|) {
%macrofile = ("basename" => $1, "defined" => "user");
}
foreach my $element ($doc->findnodes('//dict[key = "Identifier"]')) {
$element->setAttribute(macrofile => $macrofile{"basename"});
$element->setAttribute(defined_by => $macrofile{"defined"});
$self->add_macro($element);
}
}
sub add_macro {
my $self = shift;
my ($element) = @_;
# my %formatting_options = (
# PreExpressionsSpacing => " ",
# PostCommaSpacing => " ",
# PreMethodTypeSpacing => " ",
# PreMethodDeclSpacing => " ",
# BlockSeparator => " ",
# CaseStatementSpacing => "\t",
# PostBlockSeparator => "\t",
# FunctionBlockSeparator => "\t",
# );
#
# my ($textnode) = $element->findnodes('string[preceding-sibling::key[1][. = "TextString"]]');
# if ($textnode) {
# my $text = $textnode->textContent();
# $text =~ s/\$\((\w+)\)/$formatting_options{$1} || ''/eg;
# $textnode->removeChildNodes();
# $textnode->appendText($text);
# }
my $identifier = $element->findvalue('string[preceding-sibling::key[1][. = "Identifier"]]');
my $name_lc = lc($element->findvalue('string[preceding-sibling::key[1][. = "Name"]]'));
$element->setAttribute(identifier => $identifier);
$element->setAttribute(name_lc => $name_lc);
$self->assign_shortcut_to_macro($element);
$self->{macro_elements}->{$identifier} = $element;
}
sub assign_shortcut_to_macro {
my $self = shift;
my ($element) = @_;
my $identifier = $element->getAttribute('identifier');
my $shortcut = $self->{keyboard_shortcuts}->{$identifier};
return unless $shortcut;
$element->setAttribute(shortcut => $shortcut);
# print $element->toString();
}
sub read_plist {
my $self = shift;
my ($path) = @_;
my $is_valid = !(system('plutil', '-lint', '-s', $path) >> 8);
die "Invalid plist '$path'" unless ($is_valid);
my $plist_xml_string = qx(plutil -convert xml1 -o - "$path");
die "Unable to read plist at '$path'" unless $plist_xml_string;
my $parser = XML::LibXML->new();
$parser->no_network(1);
$parser->recover_silently(1);
my $doc = $parser->parse_string($plist_xml_string);
die "Unable to parse XML converted from '$path'" unless $doc;
return $doc;
}
sub generate_output {
my $self = shift;
my $path = '/tmp/xcode-text-macros.plist';
my $path_out = 'xcode-macro-cheat-sheet.html';
$self->{doc}->toFile($path);
system('xsltproc', '-o', $path_out, 'xctxtmacro2html.xslt', $path);
# system('qlmanage', '-p', $path_out);
system('open', $path_out);
}