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