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