code to install color themes for linux + OS X builds
[ardour.git] / tools / fmt-bindings
1 #!/usr/bin/perl
2
3 # import module
4 use Getopt::Long; 
5 use File::Basename;
6 use File::Spec;
7     
8 $semicolon = ";"; # help out stupid emacs
9 $title = "Ardour Shortcuts";
10 $in_group_def = 0;
11 $group_name;
12 $group_text;
13 $group_key;
14 $group_number = 0;
15 %group_names;
16 %group_text;
17 %owner_bindings;
18 %group_owners;
19 %group_bindings;
20 %modifier_map;
21 %group_numbering;
22 %merge_bindings;
23
24 $platform = linux;
25 $winkey = 'Win';
26 $make_cheatsheet = 1;
27 $make_accelmap = 0;
28 $merge_from = "";
29 $html = 0;
30
31 GetOptions ("platform=s" => \$platform,
32             "winkey=s" => \$winkey,
33             "cheatsheet" => \$make_cheatsheet,
34             "accelmap" => \$make_accelmap,
35             "merge=s" => \$merge_from,
36             "html" => \$html);
37
38 if ($platform eq "darwin") {
39
40     $gtk_modifier_map{'PRIMARY'} = 'Primary'; # GTK supports Primary to allow platform-independent binding to the "primary" modifier, which on OS X is Command
41     $gtk_modifier_map{'SECONDARY'} = 'Control';
42     $gtk_modifier_map{'TERTIARY'} = 'Shift';
43     $gtk_modifier_map{'LEVEL4'} = 'Mod1'; 
44
45     # cs_modifier_map == "Cheat Sheet Modifier Map"
46     # Used to control what gets shown in the
47     # cheat sheet for a given (meta)-modifier
48
49     $cs_modifier_map{'PRIMARY'} = 'Cmd';
50     $cs_modifier_map{'SECONDARY'} = 'Control';
51     $cs_modifier_map{'TERTIARY'} = 'Shift';
52     $cs_modifier_map{'LEVEL4'} = 'Opt';
53
54     # used to display what gets shown in the
55     # cheat sheet for mouse bindings. Differs
56     # from cs_modifier map in using shorter
57     # abbreviations.
58     
59     $mouse_modifier_map{'PRIMARY'} = 'Cmd';
60     $mouse_modifier_map{'SECONDARY'} = 'Ctrl';
61     $mouse_modifier_map{'TERTIARY'} = 'Shift';
62     $mouse_modifier_map{'LEVEL4'} = 'Opt';
63
64 } else {
65
66     $gtk_modifier_map{'PRIMARY'} = 'Control';
67     $gtk_modifier_map{'SECONDARY'} = 'Alt';
68     $gtk_modifier_map{'TERTIARY'} = 'Shift';
69     $gtk_modifier_map{'LEVEL4'} = $winkey;  # something like "Mod4><Super" 
70
71     # cs_modifier_map == "Cheat Sheet Modifier Map"
72     # Used to control what gets shown in the
73     # cheat sheet for a given (meta)-modifier
74
75     $cs_modifier_map{'PRIMARY'} = 'Control';
76     $cs_modifier_map{'SECONDARY'} = 'Alt';
77     $cs_modifier_map{'TERTIARY'} = 'Shift';
78     $cs_modifier_map{'LEVEL4'} = 'Win';
79
80     # used to display what gets shown in the
81     # cheat sheet for mouse bindings. Differs
82     # from cs_modifier map in using shorter
83     # abbreviations.
84
85     $mouse_modifier_map{'PRIMARY'} = 'Ctl';
86     $mouse_modifier_map{'SECONDARY'} = 'Alt';
87     $mouse_modifier_map{'TERTIARY'} = 'Shift';
88     $mouse_modifier_map{'LEVEL4'} = 'Win';
89 }
90
91 %keycodes = ();
92
93 if ($html) {
94     %keycodes = (
95         'asciicircum' => '^',
96         'apostrophe' => '\'',
97         'bracketleft' => '[',
98         'bracketright' => ']',
99         'braceleft' => '{',
100         'braceright' => '}',
101         'backslash' => '\\',
102         'slash' => '/',
103         'rightanglebracket' => '&gt;',
104         'leftanglebracket' => '&lt;',
105         'ampersand' => '&',
106         'comma' => ',',
107         'period' => '.',
108         'semicolon' => ';',
109         'colon' => ':',
110         'equal' => '=',
111         'minus' => '-',
112         'plus' => '+',
113         'grave' => '`',
114         'rightarrow' => '&rarr;',
115         'leftarrow' => '&larr;',
116         'uparrow' => '&uarr;',
117         'downarrow' => '&darr;',
118         'Page_Down' => 'PageDown',
119         'Page_Up' => 'PageUp',
120         'space' => 'space',
121         'KP_Right' => 'KP-&rarr;',
122         'KP_Left' => 'KP-&larr;',
123         'KP_Up' => 'KP-&uarr;',
124         'KP_Down' => 'KP-&darr;',
125         'KP_0' => 'KP-0;',
126         'greater' => '&gt;',
127         'less' => '&lt;',
128         );
129 } else {
130
131     %keycodes = (
132         'asciicircum' => '\\verb=^=',
133         'apostrophe' => '\'',
134         'bracketleft' => '[',
135         'bracketright' => ']',
136         'braceleft' => '\\{',
137         'braceright' => '\\}',
138         'backslash' => '$\\backslash$',
139         'slash' => '/',
140         'rightanglebracket' => '>',
141         'leftanglebracket' => '<',
142         'ampersand' => '\\&',
143         'comma' => ',',
144         'period' => '.',
145         'semicolon' => ';',
146         'colon' => ':',
147         'equal' => '=',
148         'minus' => '-',
149         'plus' => '+',
150         'grave' => '`',
151         'rightarrow' => '$\rightarrow$',
152         'leftarrow' => '$\\leftarrow$',
153         'uparrow' => '$\\uparrow$',
154         'downarrow' => '$\\downarrow$',
155         'Page_Down' => 'Page Down',
156         'Page_Up' => 'Page Up',
157         'space' => 'space',
158         'KP_' => 'KP$\_$',
159         'greater' => '>',
160         'less' => '<',
161     );
162 }
163
164 if ($merge_from) {
165     open (BINDINGS, $merge_from) || die ("merge from bindings: file not readable");
166     while (<BINDINGS>) {
167         next if (/^$semicolon/);
168         if (/^\(gtk_accel/) {
169             chop; # newline
170             chop; # closing parenthesis
171             s/"//g;
172             ($junk, $action, $binding) = split;
173             $merge_bindings{$action} = $binding;
174         }
175     }
176     close (BINDINGS);
177 }
178
179 if ($make_accelmap && !$merge_from) {
180     print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
181 }
182
183 $bindings_name = basename ($ARGV[0]);
184 $bindings_name =~ s/.bindings\.in$//;
185
186 open SOURCE, "<", $ARGV[0] or die $!;
187
188 while (<SOURCE>) {
189     next if /^$semicolon/;
190
191     if (/^\$/) {
192         s/^\$//;
193         $title = $_;
194         next;
195     }
196
197     if (/^%/) {
198         
199         if ($in_group_def) {
200             chop $group_text;
201             $group_names{$group_key} = $group_name;
202             $group_text{$group_key} = $group_text;
203             $group_numbering{$group_key} = $group_number;
204             # each binding entry is 2 element array. bindings
205             # are all collected into a container array. create
206             # the first dummy entry so that perl knows what we
207             # are doing.
208             $group_bindings{$group_key} = [ [] ];
209         }
210
211         s/^%//;
212         chop;
213         ($group_key,$owner,$group_name) = split (/\s+/, $_, 3);
214         if ($make_accelmap) {
215             if (!exists ($owner_bindings{$owner})) {
216                 $owner_bindings{$owner} = [ [] ];
217             }
218             $group_owners{$group_key} = $owner;
219         }
220         $group_number++;
221         $group_text = "";
222         $in_group_def = 1;
223         next;
224     }
225
226     if ($in_group_def) {
227         if (/^@/) {
228             chop $group_text;
229             $group_names{$group_key} = $group_name;
230             $group_text{$group_key} = $group_text;
231             $in_group_def = 0;
232         } else {
233             next if (/^[ \t]+$/);
234             $group_text .= $_;
235             $group_text;
236             next;
237         }
238     }
239
240     if (/^@/) {
241         s/^@//;
242         chop;
243         ($key,$action,$binding,$text) = split (/\|/, $_, 4);
244
245         $gkey = $key;
246         $gkey =~ s/^-//;
247         $owner = $group_owners{$gkey};
248
249         # substitute bindings
250
251         $gtk_binding = $binding;
252
253         if ($merge_from) {
254             $lookup = "<Actions>/" . $action;
255             if ($merge_bindings{$lookup}) {
256                 $binding = $merge_bindings{$lookup};
257             } else {
258                 if ($key =~ /^\+/) {
259                     # forced inclusion of bindings from template
260                 } else {
261                     # this action is not defined in the merge from set, so forget it 
262                     next;
263                 }
264             }
265         } 
266
267         # print the accelmap output
268
269         if ($key =~ /^\+/) {
270             # remove + and don't print it in the accelmap
271             $key =~ s/^\+//;
272         } else {
273             # include this in the accelmap if it is part of a group that has an "owner"
274             if (!$merge_from && $make_accelmap && exists ($owner_bindings{$owner})) {
275
276                 $b = $binding;
277                 $b =~ s/<@//g;
278                 $b =~ s/@>//g;
279                 $b =~ s/PRIMARY/Primary-/;
280                 $b =~ s/SECONDARY/Secondary-/;
281                 $b =~ s/TERTIARY/Tertiary-/;
282                 $b =~ s/LEVEL4/Level4-/;
283                 
284                 $bref = $owner_bindings{$owner};
285                 push (@$bref, [ $action, $b ]);
286             }
287         }
288
289         if ($key =~ /^-/) {
290             # do not include this binding in the cheat sheet
291             next;
292         }
293
294         $bref = $group_bindings{$key};
295         push (@$bref, [$binding, $text]);
296
297         next;
298     }
299
300     next;
301 }
302
303 if ($make_accelmap) {
304     print "<BindingSet name=\"" . $bindings_name . "\">\n";
305     
306     foreach $owner (keys %owner_bindings) {
307         print " <Bindings name=\"$owner\">\n  <Press>\n";
308         $bindings = $owner_bindings{$owner};
309         shift (@$bindings); # remove initial empty element
310         for my $binding (@$bindings) {
311             print '   <Binding key="' . @$binding[1] . '" action="' . @$binding[0] . "\"/>\n";
312         }
313         print "  </Press>\n </Bindings>\n";
314     }
315
316     # merge in the "fixed" bindings that are not defined by the argument given to this program
317     # namely, the step editor and the mixer windows
318
319     foreach $hardcoded_bindings ("mixer.bindings", "step_editing.bindings", "monitor.bindings", "processor_box.bindings") {
320         $path = File::Spec->catfile (dirname ($ARGV[0]), $hardcoded_bindings);
321         open HARDCODED, "<", $path or die $!;
322         while (<HARDCODED>) {
323             print $_;
324         }
325         close HARDCODED;
326     }
327     
328     print "</BindingSet>\n";
329 }
330
331 if ($make_accelmap || !$make_cheatsheet) {
332     exit 0;
333 }
334
335 if ($html) {
336
337     @groups_sorted_by_number = sort { $group_numbering{$a} <=> $group_numbering{$b} } keys %group_numbering; 
338     
339     foreach $gk (@groups_sorted_by_number) {
340
341         if ($gk =~ /^m/) {
342             # mouse stuff - ignore
343             next;
344         }
345
346         # $bref is a reference to the array of arrays for this group
347         $bref = $group_bindings{$gk};
348         
349         if (scalar @$bref > 1) {
350             
351             $name = $group_names{$gk};
352             $name =~ s/\\linebreak.*//;
353             $name =~ s/\\&/&/;
354             $name =~ s/\$\\_\$/-/g;
355             $name =~ s/\\[a-z]+ //g;
356             $name =~ s/[{}]//g;
357             $name =~ s/\\par//g;
358
359             print "<h3>$name</h3>\n";
360
361             $gtext = $group_text{$gk};
362             $gtext =~ s/\\linebreak.*//;
363             $gtext =~ s/\\&/&/;
364             $gtext =~ s/\$\\_\$/-/g;
365             $gtext =~ s/\\[a-z]+ //g;
366             $gtext =~ s/[{}]//g;
367             $gtext =~ s/\\par//g;
368             
369             if (!($gtext eq  "")) {
370                 print "$gtext\n\n";
371             }
372             
373             # ignore the first entry, which was empty
374             
375             shift (@$bref);
376             
377             # set up the list
378             
379             print "<dl class=\"bindings\">\n";
380             
381             # sort the array of arrays by the descriptive text for nicer appearance,
382             # and print them
383             
384             for $bbref (sort { @$a[1] cmp @$b[1] } @$bref) {
385                 # $bbref is a reference to an array
386                 
387                 $binding = @$bbref[0];
388                 $text = @$bbref[1];
389
390                 if ($binding =~ /:/) { # mouse binding with "where" clause
391                     ($binding,$where) = split (/:/, $binding, 2);
392                 }
393                 
394                 foreach $k (keys %cs_modifier_map) {
395                     $binding =~ s/\@$k\@/$cs_modifier_map{$k}/;
396                 }
397
398                 # remove braces for HTML
399
400                 $binding =~ s/></\+/g;
401                 $binding =~ s/^<//;
402                 $binding =~ s/>/\+/;
403                 
404                 # substitute keycode names for something printable
405                 
406                 $re = qr/${ \(join'|', map quotemeta, keys %keycodes)}/;
407                 $binding =~ s/($re)/$keycodes{$1}/g;
408
409                 # tidy up description
410
411                 $descr = @$bbref[1];
412                 $descr =~ s/\\linebreak.*//;
413                 $descr =~ s/\\&/&/;
414                 $descr =~ s/\$\\_\$/-/g;
415                 $descr =~ s/\\[a-z]+ //g;
416                 $descr =~ s/[{}]//g;
417                 $descr =~ s/\\par//g;
418
419                 print "<dt>$descr</dt><dd>$binding</dd>\n";
420             }
421             
422             print "</dl>\n";
423         
424         }
425     }
426     print "&nbsp; <!-- remove this if more text is added below -->\n";
427     exit 0;
428 }
429
430
431 # Now print the cheatsheet
432
433 $boilerplate_header = <<END_HEADER;
434 \\documentclass[10pt,landscape]{article}
435 %\\documentclass[10pt,landscape,a4paper]{article}
436 %\\documentclass[10pt,landscape,letterpaper]{article}
437 \\usepackage{multicol}
438 \\usepackage{calc}
439 \\usepackage{ifthen}
440 \\usepackage{palatino}
441 \\usepackage{geometry}
442
443 \\setlength{\\parskip}{0pt}
444 \\setlength{\\parsep}{0pt}
445 \\setlength{\\headsep}{0pt}
446 \\setlength{\\topskip}{0pt}
447 \\setlength{\\topmargin}{0pt}
448 \\setlength{\\topsep}{0pt}
449 \\setlength{\\partopsep}{0pt}
450
451 % This sets page margins to .5 inch if using letter paper, and to 1cm
452 % if using A4 paper. (This probably isnott strictly necessary.)
453 % If using another size paper, use default 1cm margins.
454 \\ifthenelse{\\lengthtest { \\paperwidth = 11in}}
455         { \\geometry{top=.5in,left=.5in,right=.5in,bottom=.5in} }
456         {\\ifthenelse{ \\lengthtest{ \\paperwidth = 297mm}}
457                 {\\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
458                 {\\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
459         }
460
461 % Turn off header and footer
462 \\pagestyle{empty}
463  
464 % Redefine section commands to use less space
465 \\makeatletter
466 \\renewcommand{\\section}{\\\@startsection{section}{1}{0mm}%
467                                 {-1ex plus -.5ex minus -.2ex}%
468                                 {0.5ex plus .2ex}%
469                                 {\\normalfont\\large\\bfseries}}
470 \\renewcommand{\\subsection}{\\\@startsection{subsection}{2}{0mm}%
471                                 {-1explus -.5ex minus -.2ex}%
472                                 {0.5ex plus .2ex}%
473                                 {\\normalfont\\normalsize\\bfseries}}
474 \\renewcommand{\\subsubsection}{\\\@startsection{subsubsection}{3}{0mm}%
475                                 {-1ex plus -.5ex minus -.2ex}%
476                                 {1ex plus .2ex}%
477                                 {\\normalfont\\small\\bfseries}}
478 \\makeatother
479
480 % Do not print section numbers% Do not print section numbers
481 \\setcounter{secnumdepth}{0}
482
483 \\setlength{\\parindent}{0pt}
484 \\setlength{\\parskip}{0pt plus 0.5ex}
485
486 %-------------------------------------------
487
488 \\begin{document}
489 \\newlength{\\MyLen}
490 \\raggedright
491 \\footnotesize
492 \\begin{multicols}{3}
493 END_HEADER
494
495 $boilerplate_footer = <<END_FOOTER;
496 \\rule{0.3\\linewidth}{0.25pt}
497 \\scriptsize
498
499 Copyright \\copyright\\ 2013 ardour.org
500
501 % Should change this to be date of file, not current date.
502
503 http://manual.ardour.org
504
505 \\end{multicols}
506 \\end{document}
507 END_FOOTER
508
509 if ($make_cheatsheet) {
510     print $boilerplate_header;
511     print "\\begin{center}\\Large\\bf $title \\end{center}\n";
512 }
513
514 @groups_sorted_by_number = sort { $group_numbering{$a} <=> $group_numbering{$b} } keys %group_numbering; 
515
516 foreach $gk (@groups_sorted_by_number) {
517     # $bref is a reference to the array of arrays for this group
518     $bref = $group_bindings{$gk};
519
520     if (scalar @$bref > 1) {
521         print "\\section{$group_names{$gk}}\n";
522
523         if (!($group_text{$gk} eq  "")) {
524             print "$group_text{$gk}\n\\par\n";
525         }
526         
527         # ignore the first entry, which was empty
528
529         shift (@$bref);
530
531         # find the longest descriptive text (this is not 100% accuracy due to typography)
532
533         $maxtextlen = 0;
534         $maxtext = "";
535
536         for $bbref (@$bref) {
537             # $bbref is a reference to an array
538             $text = @$bbref[1];
539             
540             #
541             # if there is a linebreak, just use everything up the linebreak
542             # to determine the width
543             #
544
545             if ($text =~ /\\linebreak/) {
546                 $matchtext = s/\\linebreak.*//;
547             } else {
548                 $matchtext = $text;
549             }
550             if (length ($matchtext) > $maxtextlen) {
551                 $maxtextlen = length ($matchtext);
552                 $maxtext = $matchtext;
553             }
554         }
555
556         if ($gk =~ /^m/) {
557             # mouse mode: don't extend max text at all - space it tight
558             $maxtext .= ".";
559         } else {
560             $maxtext .= "....";
561         }
562
563         # set up the table
564
565         print "\\settowidth{\\MyLen}{\\texttt{$maxtext}}\n";
566         print "\\begin{tabular}{\@{}p{\\the\\MyLen}% 
567                                 \@{}p{\\linewidth-\\the\\MyLen}%
568                                 \@{}}\n";
569
570         # sort the array of arrays by the descriptive text for nicer appearance,
571         # and print them
572
573         for $bbref (sort { @$a[1] cmp @$b[1] } @$bref) {
574             # $bbref is a reference to an array
575
576             $binding = @$bbref[0];
577             $text = @$bbref[1];
578
579             if ($binding =~ /:/) { # mouse binding with "where" clause
580                 ($binding,$where) = split (/:/, $binding, 2);
581             }
582
583             if ($gk =~ /^m/) {
584                 # mouse mode - use shorter abbrevs
585                 foreach $k (keys %mouse_modifier_map) {
586                     $binding =~ s/\@$k\@/$mouse_modifier_map{$k}/;
587                 }
588             } else {
589                 foreach $k (keys %cs_modifier_map) {
590                     $binding =~ s/\@$k\@/$cs_modifier_map{$k}/;
591                 }
592             }
593
594             $binding =~ s/></\+/g;
595             $binding =~ s/^<//;
596             $binding =~ s/>/\+/;
597
598             # substitute keycode names for something printable
599
600             $re = qr/${ \(join'|', map quotemeta, keys %keycodes)}/;
601             $binding =~ s/($re)/$keycodes{$1}/g;
602
603             # split up mouse bindings to "click" and "where" parts
604
605             if ($gk eq "mobject") {
606                 print "{\\tt @$bbref[1] } & {\\tt $binding} {\\it $where}\\\\\n";
607             } else {
608                 print "{\\tt @$bbref[1] } & {\\tt $binding} \\\\\n";
609             }
610         }
611
612         print "\\end{tabular}\n";
613
614     }
615 }
616
617 print $boilerplate_footer;
618
619 exit 0;