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 xprint(1, "$ln, $i\n");
267 while ($ln <= $i) { # the global $i
268 if ($input[$ln] =~ m!^\s*#A[0-9]*#!) {
269 xprint(1, "Ending abbreviation handling in line $ln.\n");
275 if ($input[$ln] =~ s/\s+\#d\s*([^#]*)$//) {
279 if ($input[$ln] =~ m!^\s*['"]([^"']*)['"]\s+\$?['"]([^"']*)['"]!) {
280 $abbrev = $1; $value = &escape_string($2);
281 xprint(2, "ab: $abbrev -> $value ($doc);\n");
282 push(@{ $data{"abbrev-$secmap[$sec]"} }, "\\command\{$abbrev\}\{\\kbd\{$value" . ($doc ne '' ? "\}\\quad $doc" : "\}") . "\}");
284 xprint(0, "Line didn't look like abbreviation in abbreviations section: " . $input[$ln] . "\n");
292 my ($sec, $desc) = @_;
295 if ($input[$ln] =~ m!\s*alias (-[haocC] +)*([^=]*)=["'](.*)["']!) {
296 $alias=$2; $value=&escape_string($3);
297 $desc =~ s!\@a\@!$value!;
298 push(@{ $data{"aliases-$secmap[$sec]"} }, "\\command\{$alias\}\{$desc\}");
306 sub __function { #{{{
307 my ($sec, $desc) = @_;
309 if ($input[$ln] =~ m!\s*(function)?\s*([^(\s]*)!) {
310 xprint(2, " - $2()\n");
311 push(@{ $data{"functions-$secmap[$sec]"} }, "\\command\{$2()\}\{$desc\}");
320 my ($sec, $desc) = @_;
325 if ($input[$ln] =~ m/^\s*\#d[0-9]*\#/) {
326 xprint(1, "Ending hashed dir handling in line $ln.\n");
331 if ($input[$ln] =~ m!\s*hash\s+-d\s+([^=]+)=(.*)!) {
332 $dir=$1; $value=&escape_string($2);
333 push(@{ $data{"hasheddirs-$secmap[$sec]"} }, "\\command\{$dir\}\{$value\}");
343 sub __keybinding { #{{{
344 my ($sec, $desc) = @_;
347 if ($input[$ln] =~ m!^.*bindkey\s+[^'"]*(.*)['"]\s+([\w-]*)\#?.*!) {
348 ($kbd, $value) = ($1, $2);
349 $value=&escape_string($value);
352 $kbd=&demystify_keys($kbd);
353 $desc =~ s!\@k\@!$value!;
355 #xprint(0, "!-> DEBUG: kbd: $kbd - value: $value - desc: $desc\n");
357 push(@{ $data{"keybindings-$secmap[$sec]"} }, "\\command\{$kbd\}\{$desc\}");
365 sub __variable { #{{{
366 my ($sec, $desc) = @_;
369 if ($input[$ln] =~ m/\s*(\S+)=(.+)$/) {
370 $var = $1 ; $value = $2;
371 $value =~ s!^\$\{\w*:-(.*)\}!$1!;
374 $value = &escape_string($value);
375 push(@{ $data{"variables-$secmap[$sec]"} }, "\\command\{$var\}\{\\kbd\{$value" . ($desc ne '' ? "\}\\quad $desc" : "\}") . "\}");
388 ### read our input from stdin {{{
390 $input[0]='index==linenumber :-)';
393 if ($i > $MAX_INPUT) {
394 die "Sorry dude, input lines exeeded maximum ($MAX_INPUT)}\n";
400 # work on the lines in memory (jumping back and forth is simple this way)
403 if ($input[$ln] =~ m/^\#\@\#\s*(.*)$/) {
408 if ($input[$ln] =~ m/^\s*\#([a-zA-Z])([0-9]*)\#\s*(.*)$/) {
410 &handle("alias", \&__alias, $2, $3);
411 } elsif ($1 eq 'A') {
412 &handle("abbreviation", \&__abbrev, $2, $3);
413 } elsif ($1 eq 'd') {
414 &handle("hashed dir", \&__hashdir, $2, $3);
415 } elsif ($1 eq 'f') {
416 &handle("function", \&__function, $2, $3);
417 } elsif ($1 eq 'k') {
418 &handle("keybinding", \&__keybinding, $2, $3);
419 } elsif ($1 eq 'v') {
420 &handle("variable", \&__variable, $2, $3);
421 } elsif ($1 eq 'o') {
422 &handle_other($2, $3);
423 } elsif ($1 eq 'm') {
425 $arg =~ m!^\s*([a-zA-Z][0-9]*)\s+(\S+)\s+(.*)!;
426 &handle_manual($1, $2, $3);
428 warn("Unknown doc-definition character in line $ln ($1). IGNORING.\n");
439 # read the .in file and put in stuff we gathered from stdin earlier
440 open(IN, "<$refin") or die "could not open $refin: $!\n";
446 while (m!\@\@INSERT-other-[^@]+\@\@!) {
447 s!\@\@INSERT-other-([^@]+)\@\@!$other{$1}!;
448 xprint(2, "Inserting \@\@INSERT-other-$1\@\@ -> $other{$1}\n");
451 if (m!^\@\@INSERT-([^-]*)-?(.*)\@\@!) {
453 die "malformed insertion tag in line $i ($_). ABORT\n";