vim: custom key rows specifiable via ?status
[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.1';
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 q{,}, $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 terse);
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 my %keytrans = qw(
129         ^@ NUL ^a SOH ^b STX ^c ETX  ^d EOT ^e ENQ ^f ACK ^g BEL
130         ^h BS  ^i tab ^j LF  ^k VT   ^l FF  ^m CR  ^n SO  ^o SI
131         ^p DLE ^q DC1 ^r DC2 ^s DC3  ^t DC4 ^u NAK ^v SYN ^w ETB
132         ^x CAN ^y EM  ^z SUB ^[ ESC  ^\ FS  ^] GS  ^^ RS  ^_ US
133         ^? DEL
134 );
135
136 sub print_key {
137         my ($mode, $key, $keyinfo) = @_;
138
139         $keyinfo = [ $sign{alias}.$keyinfo, keyunalias($keyinfo)->[1] . ' alias' ]
140                 if defined $keyinfo and not ref $keyinfo;  # alias
141         my ($desc, $flags, $mnem) = @$keyinfo if defined $keyinfo;
142         defined $desc or $flags = $key eq '^0' ? 'ni' : 'no';
143
144 #       $key = $keytrans{$key} if defined $keytrans{$key};
145         my $keytxt = $mode . Entity($key) if $key ne '^0';
146            $keytxt .= $sign{arg} while $flags =~ s/ ?\barg\b//;  # argument
147            $keytxt .= "<small>$sign{motion}</small>" if $flags =~ s/ ?\bargm\b//;  # motion argument
148            $keytxt =~ s{\^(?=.)}{<small>^</small>};  # element around ctrl-identifier
149         my $onclick = $flags =~ s/ ?\bmode(\S*)// && defined $keys{$1} && sprintf(
150                 ' onclick="setmode(%s)"',
151                 $1 eq '' ? '' : sprintf(q{'mode%s'}, escapeclass($1))
152         );
153         $onclick .= sprintf(q{ onclick="document.location='%s'"}, $1)
154                 if $flags =~ s/ ?\blink(\S*)//;
155         my $keyhint = defined($mnem) && qq{ title="$mnem"};
156
157         print qq{\t\t<li class="$flags"$onclick><b$keyhint>$keytxt</b>};
158         print ' ', $desc if defined $desc;
159         print "\n";
160 }
161
162 our $map = defined $keyrows{$get{map}} ? $get{map} : 'qwerty';
163 my $keyrows = $keyrows{$map};
164 my @moderows = $get{static} ? split(/\s+/, $get{static}) : sort keys %keys;
165
166 for (my $row = 0; $row <= $#$keyrows; $row++) {
167         my $keyrow = $keyrows->[$row];
168         my @caserows = 0 .. $#$keyrow;
169
170         print qq{<li class="row row$row"><ul>\n};
171         for my $modefull (@moderows) {
172                 my $mode = $modefull;
173                 my @showcase = $mode =~ s/(\d+)(?:-(\d+))?$//
174                         ? (map {3 - $_} split //, $row == 0 && $2 || $1) : @caserows;
175                 my $modekeys = $keys{$mode};
176
177                 for my $case (@showcase) {
178                         my $keycase = $keyrow->[$case] or next;
179                           @$keycase or next;
180
181                         printf "\t<li%s>", $mode ne '' && sprintf(
182                                 ' class="%s"', ($get{static} ? '' : 'mode ') . 'mode' . escapeclass($mode)
183                         );
184                         printf("<h3>%s<small>: %s</small></h3>\n", # XXX insert &nbsp; here to fix msie<=6
185                                         $modekeys->{desc} || "mode $mode",
186                                         "$rowdesc[$row] row $casedesc[$case]"
187                         );
188                         my $caseclass = 'keys';
189                            $caseclass .= ' lead' if defined $modekeys->{lead};  # leading command key shown
190                            $caseclass .= " $casedesc[$case]" if defined $casedesc[$case];
191                         print qq{\t\t<ul class="$caseclass">\n};
192                         print_key($modekeys->{lead}, $_, $modekeys->{$_}) for @$keycase;
193                         print qq{\t\t</ul>\n};
194                 } # case
195
196         } # mode
197         print qq{\t</ul>\n};
198 } # row
199
200 :>
201 </ul>
202
203 <hr>
204
205 <div class="help">
206         <div class="left">
207                 <dl class="legend legend-types">
208                 <dt class="ci">info
209                         <dd>Info command: shows/does something without altering anything.
210                 <dt class="pm">motion
211                         <dd>Moves the cursor, or defines the range for an operator (<:= $sign{motion} :>).
212                 <dt class="po">positioning
213                         <dd>Other movement (jumps, window (re)positioning).
214                 <dt class="co">command
215                         <dd>Direct action command.
216                 <dt class="mi">ins mode
217                         <dd>Enters Insert or Replace mode.
218                 <dt class="mo">mode
219                         <dd>Enters a different mode.
220                 <dt class="mv">vis mode
221                         <dd>Enters Visual or Select mode.
222                 <dt class="me">key cmd
223                         <dd>Additional key commands (click for overview).
224                 </dl>
225         </div>
226
227         <div class="right">
228                 <dl class="legend legend-options">
229                 <dt>key<:= $sign{arg} :>
230                         <dd>Commands with a dot need a char argument afterwards.
231                 <dt>key<:= $sign{motion} :>
232                         <dd>Requires a motion afterwards, operates between cursor and destination.
233                 <dt class="vim">vim
234                         <dd>Not in original Vi (assessment incomplete).
235                 <dt class="vim7">vim7
236                         <dd>New in vim version 7.x.
237                 </dl>
238
239                 <ul class="legend legend-set">
240                 <li>keyboard <strong>map</strong> is
241                         <:= $get{map} ? 'set to ' : '' :><em><:= $map :></em>
242                 <li><strong>ascii</strong> mode is
243                         <:= exists $get{ascii} && 'forced ' :><em><:=
244                                 $ascii ? 'on' : 'off' :></em>
245                 <li><strong>keys</strong> are
246                         <em><:= $showkeys ? 'always shown' : 'hidden if unassigned' :></em><:=
247                                 !exists $get{keys} && ' by default' :>
248                 <li>default <strong>style</strong> is
249                         <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
250                 </ul>
251         </div>
252 </div>
253
254 <p class="footer">
255         <a href="http://vi.shiar.net/">vi.<strong>shiar.net</strong></a>
256         <a href="git://dev.shiar.net/vi-cheat"><:= "v$VERSION" :></a>
257         created by Shiar <:= $sign{sep} :>
258         <a title="Licensed under the GNU Affero General Public License, version 3"
259            href="http://www.fsf.org/licensing/licenses/agpl-3.0.html">AGPLv3</a> <:= $sign{sep} :>
260         last update <:
261                 use Time::Format qw(time_format);
262                 print time_format('yyyy-mm-dd', (stat 'vim-cmds.inc.pl')[9]);
263         :>
264 </p>
265
266 </html>