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