place generated bindings files in gtk2_ardour/
[ardour.git] / tools / fmt-bindings
1 #!/usr/bin/perl
2
3 # import module
4 use Getopt::Long; 
5
6 $semicolon = ";"; # help out stupid emacs
7 $title = "Ardour Shortcuts";
8 $in_group_def = 0;
9 $group_name;
10 $group_text;
11 $group_key;
12 $group_number = 0;
13 %group_names;
14 %group_text;
15 %group_files;
16 %group_handles;
17 %group_bindings;
18 %modifier_map;
19 %group_numbering;
20 %merge_bindings;
21
22 $platform = linux;
23 $winkey = 'Win';
24 $make_cheatsheet = 1;
25 $make_accelmap = 0;
26 $ardour_bindings = 0;
27 $merge_from = "";
28 $html = 0;
29
30 GetOptions ("platform=s" => \$platform,
31             "winkey=s" => \$winkey,
32             "cheatsheet" => \$make_cheatsheet,
33             "accelmap" => \$make_accelmap,
34             "ardourbindings" => \$ardour_bindings,
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 && !$ardour_bindings) {
180     print ";; this accelmap was produced by tools/fmt-bindings\n";
181 }
182
183 while (<>) {
184     next if /^$semicolon/;
185
186     if (/^\$/) {
187         s/^\$//;
188         $title = $_;
189         next;
190     }
191
192     if (/^%/) {
193         
194         if ($in_group_def) {
195             chop $group_text;
196             $group_names{$group_key} = $group_name;
197             $group_text{$group_key} = $group_text;
198             $group_numbering{$group_key} = $group_number;
199             # each binding entry is 2 element array. bindings
200             # are all collected into a container array. create
201             # the first dummy entry so that perl knows what we
202             # are doing.
203             $group_bindings{$group_key} = [ [] ];
204         }
205
206         s/^%//;
207         chop;
208         ($group_key,$group_file,$group_name) = split (/\s+/, $_, 3);
209         if ($make_accelmap && $ardour_bindings) {
210             if (!exists ($group_handles{$group_file})) {
211                 open $group_handles{$group_file}, ">", "gtk2_ardour/" . $group_file . ".bindings" or die "Cannot open bindings file " . $group_file . ".bindings: $!"
212             }
213             $group_files{$group_key} = $group_handles{$group_file}
214         }
215         $group_number++;
216         $group_text = "";
217         $in_group_def = 1;
218         next;
219     }
220
221     if ($in_group_def) {
222         if (/^@/) {
223             chop $group_text;
224             $group_names{$group_key} = $group_name;
225             $group_text{$group_key} = $group_text;
226             $in_group_def = 0;
227         } else {
228             next if (/^[ \t]+$/);
229             $group_text .= $_;
230             $group_text;
231             next;
232         }
233     }
234
235     if (/^@/) {
236         s/^@//;
237         chop;
238         ($key,$action,$binding,$text) = split (/\|/, $_, 4);
239
240         $gkey = $key;
241         $gkey =~ s/^-//;
242             
243         # substitute bindings
244
245         $gtk_binding = $binding;
246
247         if ($merge_from) {
248             $lookup = "<Actions>/" . $action;
249             if ($merge_bindings{$lookup}) {
250                 $binding = $merge_bindings{$lookup};
251             } else {
252                 if ($key =~ /^\+/) {
253                     # forced inclusion of bindings from template
254                 } else {
255                     # this action is not defined in the merge from set, so forget it 
256                     next;
257                 }
258             }
259         } 
260
261         # print the accelmap output
262
263         if ($key =~ /^\+/) {
264             # remove + and don't print it in the accelmap
265             $key =~ s/^\+//;
266         } else {
267             # include this in the accelmap
268             if (!$merge_from && $make_accelmap) {
269                 if (!$ardour_bindings) {
270                     foreach $k (keys %gtk_modifier_map) {
271                         $gtk_binding =~ s/\@$k\@/$gtk_modifier_map{$k}/;
272                     }
273                     print "(gtk_accel_path \"<Actions>/$action\" \"$gtk_binding\")\n";
274                 } else {
275                     $b = $binding;
276                     $b =~ s/<@//g;
277                     $b =~ s/@>//g;
278                     $b =~ s/PRIMARY/Primary-/;
279                     $b =~ s/SECONDARY/Secondary-/;
280                     $b =~ s/TERTIARY/Tertiary-/;
281                     $b =~ s/LEVEL4/Level4-/;
282                     
283                     if (exists ($group_files{$gkey})) {
284                         print { $group_files{$gkey}  } "<Binding key=\"" . $b . "\" action=\"" . $action . "\"/>\n";
285                     }
286                 }
287             }
288         }
289
290         if ($key =~ /^-/) {
291             # do not include this binding in the cheat sheet
292             next;
293         }
294
295         $bref = $group_bindings{$key};
296         push (@$bref, [$binding, $text]);
297
298         next;
299     }
300
301     next;
302 }
303
304 foreach my $key (keys %group_handles) {
305     close $group_handles{$key} or die "Group file $group_files{$key} not closed!"
306 }
307
308 if ($make_accelmap || !$make_cheatsheet) {
309     exit 0;
310 }
311
312 if ($html) {
313
314     @groups_sorted_by_number = sort { $group_numbering{$a} <=> $group_numbering{$b} } keys %group_numbering; 
315     
316     foreach $gk (@groups_sorted_by_number) {
317
318         if ($gk =~ /^m/) {
319             # mouse stuff - ignore
320             next;
321         }
322
323         # $bref is a reference to the array of arrays for this group
324         $bref = $group_bindings{$gk};
325         
326         if (scalar @$bref > 1) {
327             
328             $name = $group_names{$gk};
329             $name =~ s/\\linebreak.*//;
330             $name =~ s/\\&/&/;
331             $name =~ s/\$\\_\$/-/g;
332             $name =~ s/\\[a-z]+ //g;
333             $name =~ s/[{}]//g;
334             $name =~ s/\\par//g;
335
336             print "<h3>$name</h3>\n";
337
338             $gtext = $group_text{$gk};
339             $gtext =~ s/\\linebreak.*//;
340             $gtext =~ s/\\&/&/;
341             $gtext =~ s/\$\\_\$/-/g;
342             $gtext =~ s/\\[a-z]+ //g;
343             $gtext =~ s/[{}]//g;
344             $gtext =~ s/\\par//g;
345             
346             if (!($gtext eq  "")) {
347                 print "$gtext\n\n";
348             }
349             
350             # ignore the first entry, which was empty
351             
352             shift (@$bref);
353             
354             # set up the list
355             
356             print "<dl class=\"bindings\">\n";
357             
358             # sort the array of arrays by the descriptive text for nicer appearance,
359             # and print them
360             
361             for $bbref (sort { @$a[1] cmp @$b[1] } @$bref) {
362                 # $bbref is a reference to an array
363                 
364                 $binding = @$bbref[0];
365                 $text = @$bbref[1];
366
367                 if ($binding =~ /:/) { # mouse binding with "where" clause
368                     ($binding,$where) = split (/:/, $binding, 2);
369                 }
370                 
371                 foreach $k (keys %cs_modifier_map) {
372                     $binding =~ s/\@$k\@/$cs_modifier_map{$k}/;
373                 }
374
375                 # remove braces for HTML
376
377                 $binding =~ s/></\+/g;
378                 $binding =~ s/^<//;
379                 $binding =~ s/>/\+/;
380                 
381                 # substitute keycode names for something printable
382                 
383                 $re = qr/${ \(join'|', map quotemeta, keys %keycodes)}/;
384                 $binding =~ s/($re)/$keycodes{$1}/g;
385
386                 # tidy up description
387
388                 $descr = @$bbref[1];
389                 $descr =~ s/\\linebreak.*//;
390                 $descr =~ s/\\&/&/;
391                 $descr =~ s/\$\\_\$/-/g;
392                 $descr =~ s/\\[a-z]+ //g;
393                 $descr =~ s/[{}]//g;
394                 $descr =~ s/\\par//g;
395
396                 print "<dt>$descr</dt><dd>$binding</dd>\n";
397             }
398             
399             print "</dl>\n";
400         
401         }
402     }
403     print "&nbsp; <!-- remove this if more text is added below -->\n";
404     exit 0;
405 }
406
407
408 # Now print the cheatsheet
409
410 $boilerplate_header = <<END_HEADER;
411 \\documentclass[10pt,landscape]{article}
412 %\\documentclass[10pt,landscape,a4paper]{article}
413 %\\documentclass[10pt,landscape,letterpaper]{article}
414 \\usepackage{multicol}
415 \\usepackage{calc}
416 \\usepackage{ifthen}
417 \\usepackage{palatino}
418 \\usepackage{geometry}
419
420 \\setlength{\\parskip}{0pt}
421 \\setlength{\\parsep}{0pt}
422 \\setlength{\\headsep}{0pt}
423 \\setlength{\\topskip}{0pt}
424 \\setlength{\\topmargin}{0pt}
425 \\setlength{\\topsep}{0pt}
426 \\setlength{\\partopsep}{0pt}
427
428 % This sets page margins to .5 inch if using letter paper, and to 1cm
429 % if using A4 paper. (This probably isnott strictly necessary.)
430 % If using another size paper, use default 1cm margins.
431 \\ifthenelse{\\lengthtest { \\paperwidth = 11in}}
432         { \\geometry{top=.5in,left=.5in,right=.5in,bottom=.5in} }
433         {\\ifthenelse{ \\lengthtest{ \\paperwidth = 297mm}}
434                 {\\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
435                 {\\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
436         }
437
438 % Turn off header and footer
439 \\pagestyle{empty}
440  
441 % Redefine section commands to use less space
442 \\makeatletter
443 \\renewcommand{\\section}{\\\@startsection{section}{1}{0mm}%
444                                 {-1ex plus -.5ex minus -.2ex}%
445                                 {0.5ex plus .2ex}%
446                                 {\\normalfont\\large\\bfseries}}
447 \\renewcommand{\\subsection}{\\\@startsection{subsection}{2}{0mm}%
448                                 {-1explus -.5ex minus -.2ex}%
449                                 {0.5ex plus .2ex}%
450                                 {\\normalfont\\normalsize\\bfseries}}
451 \\renewcommand{\\subsubsection}{\\\@startsection{subsubsection}{3}{0mm}%
452                                 {-1ex plus -.5ex minus -.2ex}%
453                                 {1ex plus .2ex}%
454                                 {\\normalfont\\small\\bfseries}}
455 \\makeatother
456
457 % Do not print section numbers% Do not print section numbers
458 \\setcounter{secnumdepth}{0}
459
460 \\setlength{\\parindent}{0pt}
461 \\setlength{\\parskip}{0pt plus 0.5ex}
462
463 %-------------------------------------------
464
465 \\begin{document}
466 \\newlength{\\MyLen}
467 \\raggedright
468 \\footnotesize
469 \\begin{multicols}{3}
470 END_HEADER
471
472 $boilerplate_footer = <<END_FOOTER;
473 \\rule{0.3\\linewidth}{0.25pt}
474 \\scriptsize
475
476 Copyright \\copyright\\ 2013 ardour.org
477
478 % Should change this to be date of file, not current date.
479
480 http://manual.ardour.org
481
482 \\end{multicols}
483 \\end{document}
484 END_FOOTER
485
486 if ($make_cheatsheet) {
487     print $boilerplate_header;
488     print "\\begin{center}\\Large\\bf $title \\end{center}\n";
489 }
490
491 @groups_sorted_by_number = sort { $group_numbering{$a} <=> $group_numbering{$b} } keys %group_numbering; 
492
493 foreach $gk (@groups_sorted_by_number) {
494     # $bref is a reference to the array of arrays for this group
495     $bref = $group_bindings{$gk};
496
497     if (scalar @$bref > 1) {
498         print "\\section{$group_names{$gk}}\n";
499
500         if (!($group_text{$gk} eq  "")) {
501             print "$group_text{$gk}\n\\par\n";
502         }
503         
504         # ignore the first entry, which was empty
505
506         shift (@$bref);
507
508         # find the longest descriptive text (this is not 100% accuracy due to typography)
509
510         $maxtextlen = 0;
511         $maxtext = "";
512
513         for $bbref (@$bref) {
514             # $bbref is a reference to an array
515             $text = @$bbref[1];
516             
517             #
518             # if there is a linebreak, just use everything up the linebreak
519             # to determine the width
520             #
521
522             if ($text =~ /\\linebreak/) {
523                 $matchtext = s/\\linebreak.*//;
524             } else {
525                 $matchtext = $text;
526             }
527             if (length ($matchtext) > $maxtextlen) {
528                 $maxtextlen = length ($matchtext);
529                 $maxtext = $matchtext;
530             }
531         }
532
533         if ($gk =~ /^m/) {
534             # mouse mode: don't extend max text at all - space it tight
535             $maxtext .= ".";
536         } else {
537             $maxtext .= "....";
538         }
539
540         # set up the table
541
542         print "\\settowidth{\\MyLen}{\\texttt{$maxtext}}\n";
543         print "\\begin{tabular}{\@{}p{\\the\\MyLen}% 
544                                 \@{}p{\\linewidth-\\the\\MyLen}%
545                                 \@{}}\n";
546
547         # sort the array of arrays by the descriptive text for nicer appearance,
548         # and print them
549
550         for $bbref (sort { @$a[1] cmp @$b[1] } @$bref) {
551             # $bbref is a reference to an array
552
553             $binding = @$bbref[0];
554             $text = @$bbref[1];
555
556             if ($binding =~ /:/) { # mouse binding with "where" clause
557                 ($binding,$where) = split (/:/, $binding, 2);
558             }
559
560             if ($gk =~ /^m/) {
561                 # mouse mode - use shorter abbrevs
562                 foreach $k (keys %mouse_modifier_map) {
563                     $binding =~ s/\@$k\@/$mouse_modifier_map{$k}/;
564                 }
565             } else {
566                 foreach $k (keys %cs_modifier_map) {
567                     $binding =~ s/\@$k\@/$cs_modifier_map{$k}/;
568                 }
569             }
570
571             $binding =~ s/></\+/g;
572             $binding =~ s/^<//;
573             $binding =~ s/>/\+/;
574
575             # substitute keycode names for something printable
576
577             $re = qr/${ \(join'|', map quotemeta, keys %keycodes)}/;
578             $binding =~ s/($re)/$keycodes{$1}/g;
579
580             # split up mouse bindings to "click" and "where" parts
581
582             if ($gk eq "mobject") {
583                 print "{\\tt @$bbref[1] } & {\\tt $binding} {\\it $where}\\\\\n";
584             } else {
585                 print "{\\tt @$bbref[1] } & {\\tt $binding} \\\\\n";
586             }
587         }
588
589         print "\\end{tabular}\n";
590
591     }
592 }
593
594 print $boilerplate_footer;
595
596 exit 0;