digraphs: run vim parser with safety options
[sheet.git] / tools / mkdigraphs-vim
1 #!/bin/sh
2
3 VERSION=1.02
4
5 if [ "$#" -ne 0 ]
6 then
7         echo "Usage: $0 >digraphs-vim.inc.pl"
8         echo 'Collect digraph data from vim, standard output as Perl hash.'
9         exit 64 # EX_USAGE
10 fi
11
12 VIM='vim -RXZ -i NONE -u NONE -N -n'
13
14 $VIM -e -c'echo version' -cdigraphs -cq | perl -CSD -nE '
15         if ($. == 1) { # head
16                 say "# digraphs list from vim version ", m/(\d+)\s*\z/;
17                 say "+{";
18         } else { # body
19                 while (/\G(\S\S) (.[^ ]*) +([0-9]+)\s+/g) {
20                         my ($mnem, $chr, $cp) = ($1, $2, $3);
21                         $mnem =~ s/(?=[\\}])/\\/g;  # escape string delimiter
22                         $chrstr = sprintf q("\x{%x}"), $cp;
23                         say "q{$mnem} => $chrstr,";
24                 }
25         } END { # foot
26                 say "}";
27                 exit warn "only $. lines of :digraphs output\n" if $. < 6;
28         }
29 '