#!@PERL@
# $Id: w3mhelp.cgi.in,v 1.1.4.15 2002/11/27 14:35:49 suto Exp $

if ( $^O =~ /^(ms)?(dos|win(32|nt)?)/i ) {
  $CYGPATH = 1;
}

$helpdir = "@HELP_DIR@";
if ($CYGPATH) {
  $helpdir = &cygwin_pathconv($helpdir);
}
unshift(@INC, $helpdir);
@docdirs = split(/\s+/, '@DOCDIRS@');

local (%edit_keyfunc) =
  (
   'C-f', 'E:MVR',
   'C-b', 'E:MVL',
   'C-h', 'E:BS',
   'C-d', 'E:DELC',
   'C-k', 'E:KILLN',
   'C-u', 'E:KILLB',
   'C-a', 'E:MVB',
   'C-e', 'E:MVE',
   'C-p', 'E:PREV',
   'C-n', 'E:NEXT',
   'C-q', 'E:QUO',
   'C-v', 'E:QUO',
   'C-x', 'E:TCOMPL',
   'TAB', 'E:COMPL',
   'SPC', 'E:COMPL',
   'ESC', 'E:ESC',
   'RETURN', 'E:ENTER',
   );

local (%menu_keyfunc) =
  (
   'C-a', 'M:TOP',
   'C-b', 'M:PREV',
   'C-c', 'M:CLOSE',
   'C-e', 'M:LAST',
   'C-f', 'M:NEXT',
   'C-h', 'M:CANCEL',
   'RETURN', 'M:OK',
   'C-n', 'M:DOWN',
   'C-p', 'M:UP',
   'C-z', 'M:SUSP',
   'ESC', 'M:ESC',
   'SPC', 'M:OK',
   '/', 'M:SRCHF',
   '?', 'M:SRCHB',
   'N', 'M:SRCHP',
   'j', 'M:DOWN',
   'k', 'M:UP',
   'l', 'M:OK',
   'n', 'M:SRCHN',
   'DEL', 'M:CANCEL',
   'ESC-v', 'M:PREV',
   'UP', 'M:UP',
   'DOWN', 'M:DOWN',
   'RIGHT', 'M:OK',
   'LEFT', 'M:CANCEL',
   );

require "w3mhelp-funcname.pl";

%htmlesc = ('<', '&lt;', '>', '&gt;', '&', '&amp;');

$RC_DIR = '@RC_DIR@';
$RC_DIR =~ s/^~/$ENV{'HOME'}/;
if ($CYGPATH) {
  $RC_DIR = &cygwin_pathconv("$RC_DIR");
}
$keymap = "$RC_DIR/keymap";
$version = '*unknown*';
$lang = 'en';
$me = 'w3m';
if (defined($ENV{'QUERY_STRING'})) {
    if ($ENV{'QUERY_STRING'} =~ /(^|&)version=([^&]*)/) {
	$version = $2;
	$version =~ s/\+|%([0-9A-Fa-f][0-9A-Fa-f])/$& eq '+' ? ' ' : pack('C', hex($1))/ge;
	$version =~ s/w3m\///;
    }
    if ($ENV{'QUERY_STRING'} =~ /(^|&)myname=([^&]*)/) {
	local ($t_me) = $2;

	$t_me =~ s/\+|%([0-9A-Fa-f][0-9A-Fa-f])/$& eq '+' ? ' ' : pack('C', hex($1))/ge;
	if (-r "$ENV{'HOME'}/.$t_me/keymap") {
	    $me = $t_me;
	    $keymap = "$ENV{'HOME'}/.$me/keymap";
	}
    }
    if ($ENV{'QUERY_STRING'} =~ /(^|&)lang=([^&]*)/) {
	local ($tlang) = $2;

	$tlang =~ s/\+|%([0-9A-Fa-f][0-9A-Fa-f])/$& eq '+' ? ' ' : pack('C', hex($1))/ge;
	$tlang =~ tr/A-Z/a-z/;
	if ($tlang !~ /^[a-z][a-z]$/) {
	    $tlang = 'en';
	}
	eval {require "w3mhelp-funcdesc.$tlang.pl"};
	if (defined(%funcdesc)) {
	    $lang = $tlang;
	}
    }
}

require "w3mhelp-funcdesc.en.pl" unless (defined(%funcdesc));

%f = %keyfunc;
%keyfunc = ();
while (($key, $fname) = each %f) {
	$key = &norm_key($key);
	$keyfunc{$key} = $fname;
}

if (-f $keymap) {
    open(KEYMAP, "< $keymap") || die "cannot open keymap: $keymap, $!";
    &load_keymap(*KEYMAP, $func);
    close(KEYMAP);
}

local (%funckeydesc, $key, $fname, $desc);

while (($fname, $desc) = each %funcdesc) {
    $fname =~ /^[EM]:/ && $desc =~ s/^[^:]*://;
    $funckeydesc{$funcname{$fname}} = "$desc\n";
}

while (($key, $fname) = each %keyfunc) {
    $funckeydesc{$funcname{$fname}} .= "$key,";
}
while (($key, $fname) = each %menu_keyfunc) {
    $funckeydesc{$funcname{$fname}} .= "$key,";
}
while (($key, $fname) = each %edit_keyfunc) {
    $funckeydesc{$funcname{$fname}} .= "$key,";
}

$w3m_version = $version;
$w3m_version =~ s/\+mee-.*//;
$mee_version = $version;
$mee_version =~ s/.*\+(mee-)/$1/;
$mee_version =~ s/[<>&]/$& eq '<' ? '&lt;' : $& eq '>' ? '&gt;' : '&amp;'/ge;

print <<HEADING;
Content-Type: text/html

<HTML>
<HEAD>
<TITLE>w3m help page</TITLE>
</HEAD>
<BODY>
<CENTER>
******* 
<A HREF="http://w3m.sourceforge.net/">w3m</A>
 (WWW-wo-Miru)  Version $w3m_version by 
<A HREF="mailto:aito\@fw.ipsj.or.jp">A.ITO</A> ********<BR>
HEADING

if ($mee_version ne '') {
  print <<MEE;
##### with <a href="http://pub.ks-and-ks.ne.jp/prog/w3mmee/">$mee_version</a>
by <a href="mailto:suto\@ks-and-ks.ne.jp">Kiyokazu SUTO</a> #####<br>
MEE
}

print <<HEADING;
             ***** Key assign table *****
</CENTER>

HEADING

$q_version = $version;
$q_version =~ s/[^A-Za-z0-9_\$\.\-]/sprintf('%%%02X', ord($&))/ge;
$q_me = $me;
$q_me =~ s/[^A-Za-z0-9_\$\.\-]/sprintf('%%%02X', ord($&))/ge;
$script = "<A HREF=\"$ENV{'SCRIPT_NAME'}?version=$q_version&amp;myname=$q_me&amp;lang=";

# doc:en_English doc-jp:ja_Japanese
for $otherlang (@docdirs) {
    local (@d) = split(/[:_]/, $otherlang);

    if ($d[1] ne $lang) {
	$d[1] =~ s/[^A-Za-z0-9_\$\.\-]/sprintf('%%%02X', ord($&))/ge;
	print $script, $d[1], "\">$d[2] version</A><BR>\n";
    }
}

&show_keymap("Page/Cursor motion", 
	     'pgFore', 'pgBack', 'movR', 'movL', 'movD', 'movU', 'lup1', 'ldown1', 'scrollXY', 
	     'goLineF', 'goLineL', 'movRW', 'movLW', 'shiftl', 'shiftr',
	     'col1R', 'col1L', 'goLine', 'ctrCsrH', 'ctrCsrV',
	     'topA', 'lastA', 'nextA', 'prevA', 'physNextA', 'physPrevA', 'gotoXY',
	     'nextL', 'nextLU', 'nextR', 'nextRD', 'nextD', 'nextU',
	     'next_frame', 'prev_frame');

&show_keymap("Hyperlink operation",
	     'followA', 'svA', 'peekURL', 'peekIMG', 'followI', 'svI',
	     'chkURL', 'chkWORD', 'chkNMID', 'curURL', 'pginfo', 'curlno', 'ldHist',
	     'rFrame', 'extbrz', 'linkbrz');

&show_keymap("File/Stream operation",
	     'goURL', 'gorURL', 'ldfile', 'readsh', 'pipesh');

&show_keymap("Buffer operation",
	     'backBf', 'vwSrc', 'nextBf', 'prevBf', 'selMn', 'editBf', 'rdrwSc', 'reload', 'reload_view', 'rerender',
	     'svBuf', 'svSrc', 'editScr', 'dispI', 'stopI');

&show_keymap("Menu mode",
	     'mEsc', 'mSelect', 'mDown', 'mUp', 'mLast', 'mTop', 'mNext', 'mPrev', 'mFore', 'mBack',
	     'mOk', 'mCancel', 'mClose', 'mSusp', 'mSrchF', 'mSrchB', 'mSrchN', 'mSrchP', 'smDelBuf',
	     'mUpMenu', 'mDownMenu', 'mLeftMenu', 'mRightMenu', 'mMoveMenu');

&show_keymap("Bookmark operation",
	     'ldBmark', 'adBmark');

&show_keymap("Search",
	     'srchfor', 'srchbak', 'srchnxt', 'srchprv', 'isrchfor', 'isrchbak');

&show_keymap("Dictionary look-up",
	     'dictword', 'dictwordat', 'dictregion');

&show_keymap("Mark operation",
	     '_mark', 'prevMk', 'nextMk', 'reMark');

&show_keymap("Miscellany",
	     'setAlarm', 'execsh', 'ldhelp', 'ldOpt', 'cooLst',
	     'susp', 'qquitfm', 'quitfm', 'procList', 'resetAE', 'reinit', 'defkey', 'chgDir');

&show_keymap("Line-edit mode",
	     'linein_bs', 'linein_bsw', 'linein_compl', 'linein_delC',
	     'linein_delRw', 'linein_enter', 'linein_esc', 'linein_inbrk',
	     'linein_killb', 'linein_killn', 'linein_mvB', 'linein_mvE',
	     'linein_mvL', 'linein_mvLw', 'linein_mvR', 'linein_mvRw',
	     'linein_next', 'linein_copyregion', 'linein_nextchar', 'linein_prev', 'linein_quo',
	     'linein_tcompl');

print <<PAGE;
<HR>
</BODY>
</HTML>
PAGE

exit 0;

sub load_keymap {
    local (*FH) = shift;
    local ($func) = @_;

    while (<FH>) {
	next if /^#/;
	next if /^\s*$/;
	($keymap, $key, $fname, $data) = split;
	next if $data ne '';
	next unless $keymap =~ /keymap/;
	$fname =~ tr/a-z/A-Z/;
	next unless (defined($funcdesc{$fname}));
	$key = &norm_key($key);
	if ($fname =~ /^E:/) {
	  $edit_keyfunc{$key} = $fname;
	}
	elsif ($fname =~ /^M:/) {
	  $menu_keyfunc{$key} = $fname;
	}
	else {
	  $keyfunc{$key} = $fname;
	}
    }
}

sub norm_key {
    local($_) = @_;

    s/^\^/C-/;
    s/^(C-\[|M-)/ESC-/;
    if (/^ESC-\[/) {
	/^ESC-\[A$/ && return "UP";
	/^ESC-\[B$/ && return "DOWN";
	/^ESC-\[C$/ && return "RIGHT";
	/^ESC-\[D$/ && return "LEFT";
	s/^ESC-/\^\[/;
	return $_;
    }
    s/^(ESC-)\^/ESC-C-/;
    s/^(ESC-)?C-[iI]/${1}TAB/;
    s/^(ESC-)?C-\?/${1}DEL/;
    s/^\\(.)/$1/;
    return $_;
}

sub show_keymap {
    local ($head, @list) = @_;
    local ($fid);
    local ($attr) = " WIDTH=140";

    if (defined($title{$head})) {
	$head = $title{$head};
	$head =~ s/[<>&]/$htmlesc{$&}/ge;
    }

    print <<PAGE;
<H2>$head</H2>
<table cellpadding=0>
PAGE
    foreach $fid (@list) {
	local ($desc, $keys) = split("\n", $funckeydesc{$fid}, 2);
	$keys =~ s/,$//;
	$keys = "<NONE for $fid>" if ($keys eq '');
	$keys =~ s/[<>&]/$htmlesc{$&}/ge;
	$desc =~ s/[<>&]/$htmlesc{$&}/ge;
	print <<PAGE;
<TR><TD$attr>$keys</TD><TD>$desc</TD></TR>
PAGE
	$attr = "";
    }
    print "</table>\n";
}


sub cygwin_pathconv {
  local($_) = @_;
  local(*CYGPATH);

  open(CYGPATH, '-|') || exec('cygpath', '-w', $_);
  $_ = <CYGPATH>;
  close(CYGPATH);
  s/\r?\n$//;
  s!\\!/!g;
  s!/$!!;
  return $_;
}
