4 ### Author: Frank Terbeck <ft@bewatermyfriend.org>
7 ### generate grml zsh refcard.
12 ### #A#: abbreviations
16 ### consider these lines in zshrc:
17 ### #a3# execute \kbd{apt-cache policy}
18 ### alias acp='apt-cache policy'
20 ### Now this script will add a new description for 'acp' into
21 ### the replacement list created by the @@INSERT-aliases-debian@@
24 ### @@INSERT-aliases-default@@ == @@INSERT-aliases@@
25 ### @@INSERT-aliases-all@@ will create a sorted list of _all_
26 ### aliases from all subsections.
28 ### @@INSERT-other-foobar@@ is special, just does text replaces
29 ### without any special formatting, useful for:
30 ### \command{umask @@INSERT-other-umask@@}{...
31 ### 'other' does not have -default nor -all.
33 ### you may specify certain subsections like this:
34 ### #a3#, which will put the input in the into the
35 ### @@INSERT-aliases-debian@@ tag.
37 ### See the @secmap array below for section numbers.
38 ### Yes, this could be done with real names instead of numbers.
39 ### But names tend to be rather long. I don't want that.
41 ### This scripts works on quite some global variables, which is *not*
42 ### a good idea most of the time. Thank god it's just a ~500 line script. :)
47 my $refin = "./grml-zsh-refcard.tex.in";
48 if (defined($ARGV[0]) && $ARGV[0] =~ m!^[^+]!) {
55 if (defined($ARGV[0])) {
56 $verbose = length($ARGV[0]);
70 $inc, # global counter for input lines
73 %other, # @@INSERT-other-*@@
74 %splits # if lists get long, we might need to split them. HoA
77 my $splitstring="\\commandlistend\n\\pagebreak\n\\commandlistbegin";
84 if ($verbose < 5) { return; }
85 xprint(5, " --- Data ---\n");
87 foreach $key (sort keys(%other)) {
88 xprint(5, " \@\@INSERT-other-$key\@\@ -> $other{$key}\n");
91 foreach $key (sort keys(%data)) {
92 xprint(5, " \@\@INSERT-$key\@\@ =>\n");
93 foreach $entry (sort @{ $data{$key} }) {
94 xprint(5, "$entry\n");
98 foreach $key (sort keys(%splits)) {
99 xprint(5, " List-Splitting Offset for $key:\n");
100 foreach $entry (@{ $splits{$key} }) {
101 xprint(5, "$entry\n");
104 xprint(5, " --- Dump ---\n");
110 if ($verbose >= $level) {
115 sub escape_string { #{{{
118 $in =~ s!([\\\{\}\*\&~\$_])!\\$1!g;
122 sub demystify_keys { #{{{
123 # what an ugly hack :-)
127 @tok = split(/(\\e[^\^]|\^.)/, $keys);
134 if ($k =~ m!^[^\\\^]!) {
139 $k =~ s!\^[jJmM]!return!g;
149 my ($linenum, $cat, $sec) = @_;
156 if (!defined($data{"$cat-$sec"})) {
157 warn("Unknown insertion tag in line $linenum (\@\@INSERT-$cat-$sec\@\@). IGNORING.\n");
161 xprint(1, "inserting: category($cat) section($sec), line: $linenum\n");
164 foreach $entry (sort @{ $data{"$cat-$sec"} }) {
167 foreach $is (@{ $splits{"$cat-$sec"} } ) {
169 print("$splitstring\n");
178 sub set_option { #{{{
179 my ($optstring) = @_;
183 if ($optstring =~ m!([a-zA-Z0-9_-]+)\s+(.*)!) {
186 if ($opt eq 'split') {
187 if ($val =~ m!([a-zA-Z0-9_-]+)\s+(.*)!) {
190 xprint(2, " splitting values (for $what): " . join(' ', split(/,/, $when)) . "\n");
191 @{ $splits{"$what"} } = split(/,/, $when);
193 warn("Parsing split option failed in line $ln. IGNORING.\n");
196 warn("Unknown option ($opt) in line $ln. IGNORING.\n");
199 warn("Parsing option in line $ln failed. IGNORING.\n");
205 # name: hashdir, abbrev etc.
206 # wfuncref: reference to the function, that does the work
207 # sec: section number
208 # desc: description string
209 my ($name, $wfuncref, $sec, $desc) = @_;
215 xprint(1, "Handling $name (section: secmap[$sec]) in line $ln ($desc)\n");
218 if (!$wfuncref->($sec, $desc)) {
219 warn("Broken $name in line $ln. IGNORING.\n");
223 sub handle_manual { #{{{
224 # this is different than the other handle_*() subs.
225 my ($code, $key, $value) = @_;
228 xprint(1, "Handling manual entry (code: $code) in line $ln ($key -> $value)\n");
230 $sec = ( (length($code) > 1) ? substr($code, 1) : 0);
231 if (substr($code, 0, 1) eq 'a') {
232 push(@{ $data{"aliases-$secmap[$sec]"} }, "\\command\{$key\}\{$value\}");
233 } elsif (substr($code, 0, 1) eq 'A') {
234 push(@{ $data{"abbrev-$secmap[$sec]"} }, "\\command\{$key\}\{$value\}");
235 } elsif (substr($code, 0, 1) eq 'd') {
236 push(@{ $data{"hasheddirs-$secmap[$sec]"} }, "\\command\{$key\}\{$value\}");
237 } elsif (substr($code, 0, 1) eq 'f') {
238 push(@{ $data{"functions-$secmap[$sec]"} }, "\\command\{$key\}\{$value\}");
239 } elsif (substr($code, 0, 1) eq 'k') {
240 push(@{ $data{"keybindings-$secmap[$sec]"} }, "\\command\{$key\}\{$value\}");
241 } elsif (substr($code, 0, 1) eq 'o') {
242 push(@{ $data{"other-$secmap[$sec]"} }, "\\command\{$key\}\{$value\}");
243 } elsif (substr($code, 0, 1) eq 'v') {
244 push(@{ $data{"variables-$secmap[$sec]"} }, "\\command\{$key\}\{$value\}");
246 warn("Unknown doc-definition character in manual-line $ln ($1). IGNORING.\n");
252 sub handle_other { #{{{
253 # a very simple handler
254 my ($sec, $desc) = @_;
256 $desc =~ m!([^\s]+)\s+(.*)!;
257 xprint(1, "Handling 'other' tag in line $ln ($1 -> $2))\n");
263 my ($sec, $desc) = @_;
264 my ($abbrev, $value, $doc);
266 while ($ln <= $i) { # the global $i
267 if ($input[$ln] =~ m!^\s*\#A[0-9]*\#!) {
268 xprint(1, "Ending abbreviation handling in line $ln.\n");
274 if ($input[$ln] =~ s/\s+\#d\s*([^#]*)$//) {
278 if ($input[$ln] =~ m!\s*['"]([^"']*)['"]\s\$?['"]([^"']*)['"]!) {
279 $abbrev = $1; $value = &escape_string($2);
280 xprint(2, "ab: $abbrev -> $value ($doc);\n");
281 push(@{ $data{"abbrev-$secmap[$sec]"} }, "\\command\{$abbrev\}\{\\kbd\{$value" . ($doc ne '' ? "\}\\quad $doc" : "\}") . "\}");
291 my ($sec, $desc) = @_;
294 if ($input[$ln] =~ m!\s*alias (-[haocC] +)*([^=]*)=["'](.*)["']!) {
295 $alias=$2; $value=&escape_string($3);
296 $desc =~ s!\@a\@!$value!;
297 push(@{ $data{"aliases-$secmap[$sec]"} }, "\\command\{$alias\}\{$desc\}");
305 sub __function { #{{{
306 my ($sec, $desc) = @_;
308 if ($input[$ln] =~ m!\s*(function)?\s*([^(\s]*)!) {
309 xprint(2, " - $2()\n");
310 push(@{ $data{"functions-$secmap[$sec]"} }, "\\command\{$2()\}\{$desc\}");
319 my ($sec, $desc) = @_;
324 if ($input[$ln] =~ m/^\s*\#d[0-9]*\#/) {
325 xprint(1, "Ending hashed dir handling in line $ln.\n");
330 if ($input[$ln] =~ m!\s*hash\s+-d\s+([^=]+)=(.*)!) {
331 $dir=$1; $value=&escape_string($2);
332 push(@{ $data{"hasheddirs-$secmap[$sec]"} }, "\\command\{$dir\}\{$value\}");
342 sub __keybinding { #{{{
343 my ($sec, $desc) = @_;
346 if ($input[$ln] =~ m!^.*bindkey\s+[^'"]*(.*)['"]\s+([\w-]*)\#?.*!) {
347 ($kbd, $value) = ($1, $2);
348 $value=&escape_string($value);
351 $kbd=&demystify_keys($kbd);
352 $desc =~ s!\@k\@!$value!;
354 #xprint(0, "!-> DEBUG: kbd: $kbd - value: $value - desc: $desc\n");
356 push(@{ $data{"keybindings-$secmap[$sec]"} }, "\\command\{$kbd\}\{$desc\}");
364 sub __variable { #{{{
365 my ($sec, $desc) = @_;
368 if ($input[$ln] =~ m/\s*(\S+)=(.+)$/) {
369 $var = $1 ; $value = $2;
370 $value =~ s!^\$\{\w*:-(.*)\}!$1!;
373 $value = &escape_string($value);
374 push(@{ $data{"variables-$secmap[$sec]"} }, "\\command\{$var\}\{\\kbd\{$value" . ($desc ne '' ? "\}\\quad $desc" : "\}") . "\}");
387 ### read our input from stdin {{{
389 $input[0]='index==linenumber :-)';
392 if ($i > $MAX_INPUT) {
393 die "Sorry dude, input lines exeeded maximum ($MAX_INPUT)}\n";
399 # work on the lines in memory (jumping back and forth is simple this way)
402 if ($input[$ln] =~ m/^\#\@\#\s*(.*)$/) {
407 if ($input[$ln] =~ m/^\s*\#([a-zA-Z])([0-9]*)\#\s*(.*)$/) {
409 &handle("alias", \&__alias, $2, $3);
410 } elsif ($1 eq 'A') {
411 &handle("abbreviation", \&__abbrev, $2, $3);
412 } elsif ($1 eq 'd') {
413 &handle("hashed dir", \&__hashdir, $2, $3);
414 } elsif ($1 eq 'f') {
415 &handle("function", \&__function, $2, $3);
416 } elsif ($1 eq 'k') {
417 &handle("keybinding", \&__keybinding, $2, $3);
418 } elsif ($1 eq 'v') {
419 &handle("variable", \&__variable, $2, $3);
420 } elsif ($1 eq 'o') {
421 &handle_other($2, $3);
422 } elsif ($1 eq 'm') {
424 $arg =~ m!^\s*([a-zA-Z][0-9]*)\s+(\S+)\s+(.*)!;
425 &handle_manual($1, $2, $3);
427 warn("Unknown doc-definition character in line $ln ($1). IGNORING.\n");
438 # read the .in file and put in stuff we gathered from stdin earlier
439 open(IN, "<$refin") or die "could not open $refin: $!\n";
445 while (m!\@\@INSERT-other-[^@]+\@\@!) {
446 s!\@\@INSERT-other-([^@]+)\@\@!$other{$1}!;
447 xprint(2, "Inserting \@\@INSERT-other-$1\@\@ -> $other{$1}\n");
450 if (m!^\@\@INSERT-([^-]*)-?(.*)\@\@!) {
452 die "malformed insertion tag in line $i ($_). ABORT\n";