minor command key improvements; i^g mode
[sheet.git] / index.plp
1 <:
2 use utf8;
3 use strict;
4 use warnings;
5 no  warnings "qw";  # you know what you doing
6 no  warnings "uninitialized";  # save some useless checks for more legible code
7
8 our $VERSION = "1.0";
9
10 our $ascii = 0;
11 if (exists $get{ascii}) {
12         $ascii = $get{ascii} ne "0";  # manual override
13 } elsif (defined $ENV{HTTP_ACCEPT_CHARSET}) {
14         $ascii = 1;
15         for (split ",", $ENV{HTTP_ACCEPT_CHARSET}) {
16                 $ascii = 0, last if $_ eq "*" or m/utf-?8/i;
17         }
18 }
19
20 my $charset = $ascii ? "us-ascii" : "utf-8";
21 my $ctype = "text/html; charset=$charset";
22 $header{content_type} = $ctype;
23
24 :><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
25  "http://www.w3.org/TR/html4/loose.dtd">
26 <html>
27
28 <head>
29 <title>vi cheat sheet</title>
30 <meta http-equiv="content-type" content="<:= $ctype :>">
31 <link rel="stylesheet" type="text/css" media="all" href="base.css">
32 <!--[if lte IE 6]><style> .help dl.legend dt {margin:0 0 1px} </style><![endif]-->
33 <!--[if lte IE 7]><style> .help dl.legend dd {float:none} </style><![endif]--><:
34         my %styles = map {$_ => $_} qw(dark circus mono);
35         our $style = exists $get{style} && $styles{$get{style}} || "light";
36         printf(qq{\n<link rel="%s" type="text/css" media="all" href="%s" title="%s">},
37                 $_ eq $style ? "stylesheet" : "alternate stylesheet", "$_.css", $_
38         ) for keys %styles;
39
40         our $showkeys = exists $get{keys} && $get{keys} ne "0";
41         print "\n<style> .no {visibility:hidden} </style>" unless $showkeys;
42         print "\n<style> .no, .alias {opacity:.5} </style>"
43                 if $showkeys and $get{keys} eq "ghost";
44 :>
45
46 <script><!--
47 function setmode(classname) {
48         // set style for each #rows>li>ul>li to display:none unless it matches classname
49         var showclass = classname ? ' '+classname+'(?!\\w)' : '^$';
50         var parentskip = /^keys/;
51         var row = document.getElementById('rows').firstChild;
52         do {
53                 if (row.tagName == 'LI' && row.firstChild.tagName == 'UL'
54                 && !row.firstChild.className.match(parentskip)) {
55                         var el = row.firstChild.firstChild;
56                         if (el) do {
57                                 if (el.tagName == 'LI') {
58                                         el.style.display = el.className.match(showclass) ? 'block' : 'none';
59                                 }
60                         } while (el = el.nextSibling);
61                 }
62         } while (row = row.nextSibling);
63
64         // update H2 to reflect the first part of a currently active (but hidden) H3
65         var h3s = document.getElementsByTagName('H3');
66         for (var i = 0; i < h3s.length; i++) {
67                 if (h3s[i].parentNode.style.display != 'block') continue;
68                 document.getElementsByTagName('H2')[0].innerHTML = h3s[i].firstChild.data;
69         }
70 } // setmode
71 //--></script>
72
73 <body>
74
75 <h1>vi/vim cheat sheet</h1>
76
77 <h2>normal mode (default)</h2>
78
79 <ul id="rows">
80
81 <li class="row">
82         <ul class="keys omni">
83         <li class="mo" onclick="setmode()"><b>Esc</b> normal mode
84                 <!-- not as static anymore, but never bothered; just see ^[ -->
85         </ul>
86 </li>
87
88 <:
89 our %sign = (
90         arg    => $ascii ? "." : "·",  # described as 'dot'
91         motion => $ascii ? "|" : "↕",
92         alias  => $ascii ? "see: " : "»",
93         up     => $ascii ? "up"    : "▲",
94         right  => $ascii ? "right" : "▶",
95         down   => $ascii ? "down"  : "▼",
96         left   => $ascii ? "left"  : "◀",
97         sep    => $ascii ? "*"     : "•",
98         _      => exists $get{ascii} && !$ascii ? "\x{200b}" : "<wbr>",
99                 # use the correct ZWNJ only when unicode is forced on
100                 # default to use unofficial html for best support
101 );
102
103 my %keys = do "vim-cmds.inc.pl";
104
105 my @casedesc = qw(ctrl shift);
106 my @rowdesc = qw(numeric top home bottom);
107 my %keyrows = do "vim-keys.inc.pl";
108
109 sub keyunalias {
110         my ($key, %tree) = @_;
111         $key =~ s/(\S*?)(\^?\S)($|\s.*)/$2/;
112         my $mode = $1;
113         return [] unless defined $keys{$mode}{$key};
114         return $keys{$mode}{$key} if ref $keys{$mode}{$key};
115         return if $tree{$key}++;  # endless loop failsafe
116         return keyunalias($keys{$mode}{$key}, %tree);
117 }
118
119 sub escapeclass {
120         local $_ = shift;
121         s/\^/_c/g;
122         s/\[/_sbo/g;
123         s/\]/_sbc/g;
124         s/^$/_/;
125         return $_;
126 }
127
128 our $map = defined $keyrows{$get{map}} ? $get{map} : "qwerty";
129 my $keyrows = $keyrows{$map};
130
131 for (my $row = 0; $row <= $#$keyrows; $row++) {
132         my $keyrow = $keyrows->[$row];
133         print qq{<li class="row row$row"><ul>\n};
134         while (my ($mode, $modekeys) = each %keys) {
135                 for (my $case = 0; $case <= $#$keyrow; $case++) {
136                         my $keycase = $keyrow->[$case];
137                           @$keycase or next;
138
139                         printf(qq{\t<li%s><h3>%s</h3>\n}, # XXX insert &nbsp; here to fix msie<=6
140                                 $mode eq '' ? '' : sprintf(
141                                         ' class="%s"', "mode mode" . escapeclass($mode)
142                                 ),
143                                 sprintf('%s<small>: %s</small>',
144                                         $modekeys->{desc} || "mode $mode",
145                                         "$rowdesc[$row] row $casedesc[$case]"
146                                 )
147                         );
148                         my $modeclass = "keys";
149                            $modeclass .= " lead" if defined $modekeys->{lead};  # leading command key shown
150                            $modeclass .= " $casedesc[$case]" if defined $casedesc[$case];
151                         print qq{\t\t<ul class="$modeclass">\n};
152
153                         for my $key (@$keycase) {
154                                 my $keyinfo = $modekeys->{$key};
155                                 $keyinfo = [ $sign{alias}.$keyinfo, keyunalias($keyinfo)->[1] . " alias" ]
156                                         if defined $keyinfo and not ref $keyinfo;  # alias
157                                 my ($desc, $flags, $mnem) = @$keyinfo if defined $keyinfo;
158                                 defined $desc or $flags = $key eq '^0' ? "ni" : "no";
159
160                                 my $keytxt = $modekeys->{lead} . Entity($key) if $key ne '^0';
161                                    $keytxt .= $sign{arg} while $flags =~ s/ ?\barg\b//;  # argument
162                                    $keytxt .= "<small>$sign{motion}</small>" if $flags =~ s/ ?\bargm\b//;  # motion argument
163                                    $keytxt =~ s{\^(?=.)}{<small>^</small>};  # element around ctrl-identifier
164                                 my $onclick = $flags =~ s/ ?\bmode(\S*)// && defined $keys{$1} && sprintf(
165                                         ' onclick="setmode(%s)"',
166                                         $1 eq '' ? '' : sprintf("'mode%s'", escapeclass($1))
167                                 );
168                                 my $keyhint = defined($mnem) && qq{ title="$mnem"};
169
170                                 print qq{\t\t<li class="$flags"$onclick><b$keyhint>$keytxt</b>};
171                                 print ' ', $desc if defined $desc;
172                                 print "\n";
173                         } # key
174
175                         print qq{\t\t</ul>\n};
176                 } # case
177
178         } # mode
179         print qq{\t</ul>\n};
180 } # row
181
182 :>
183 </ul>
184
185 <hr>
186
187 <div class="help">
188         <div class="left">
189                 <dl class="legend legend-types">
190                 <dt class="ci">info
191                         <dd>Info command: shows/does something without altering anything.
192                 <dt class="pm">motion
193                         <dd>Moves the cursor, or defines the range for an operator (<:= $sign{motion} :>).
194                 <dt class="po">positioning
195                         <dd>Other movement (jumps, window (re)positioning).
196                 <dt class="co">command
197                         <dd>Direct action command.
198                 <dt class="mi">ins mode
199                         <dd>Enters Insert or Replace mode.
200                 <dt class="mo">mode
201                         <dd>Enters a different mode.
202                 <dt class="mv">vis mode
203                         <dd>Enters Visual or Select mode.
204                 <dt class="me">key cmd
205                         <dd>Additional key commands (click for overview).
206                 </dl>
207         </div>
208
209         <div class="right">
210                 <dl class="legend legend-options">
211                 <dt>key<:= $sign{arg} :>
212                         <dd>Commands with a dot need a char argument afterwards.
213                 <dt>key<:= $sign{motion} :>
214                         <dd>Requires a motion afterwards, operates between cursor and destination.
215                 <dt class="vim">vim
216                         <dd>Not in original Vi (assessment incomplete).
217                 <dt class="vim7">vim7
218                         <dd>New in vim version 7.x.
219                 </dl>
220
221                 <ul class="legend legend-set">
222                 <li>keyboard <strong>map</strong> is
223                         <:= $get{map} ? "set to " : "" :><em><:= $map :></em>
224                 <li><strong>ascii</strong> mode is
225                         <:= exists $get{ascii} ? "forced " : "" :><em><:=
226                                 $ascii ? "on" : "off" :></em>
227                 <li><strong>keys</strong> are
228                         <em><:= $showkeys ? "always shown" : "hidden if unassigned" :></em><:=
229                                 exists $get{keys} ? "" : " by default" :>
230                 <li>default <strong>style</strong> is
231                         <:= defined $get{style} ? "set to " : "" :><em><:= $style :></em>
232                 </ul>
233         </div>
234 </div>
235
236 <p class="footer">
237         <a href="http://<:= $ENV{SERVER_NAME} :>/vim-cheat">shiar.demon.nl/<b>vim-cheat</b></a>
238         <a href="vim-cheat.tar.gz"><:= "v$VERSION" :></a>
239         created by Shiar <:= $sign{sep} :> last update <:
240                 use Time::Format qw(time_format);
241                 print time_format("yyyy-mm-dd", (stat "vim-cmds.inc.pl")[9]);
242         :>
243 </p>
244
245 </html>