#!/usr/bin/perl -w
#
# Copyright (c) 2002 Victor Ivanov
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
use strict;
use vars qw($VERSION %IRSSI);
$VERSION = "1.0b";
%IRSSI = (
authors => 'Victor Ivanov',
contact => '[email protected]',
name => 'romajibind',
description => 'Dynamic romaji binds',
sbitems => 'ro1_sb',
license => 'BSD 2-clause',
url => 'http://irssi.org/scripts/'
);
# Some help...
# First, this is UTF-8 script.
# Press ctrl-R to switch between Hiragana, Katakana and English input
#
# When the script is loading, it will install the huge amount of
# second-level binds. This takes some time.
#
# When you press ctrl-R it will install some binds, but not the whole
# bunch. Still, it takes noticeable amount of time. If you want
# something faster, try the simple romaji.pl :)
#
# The system is mostly Hepburn, but it could have some kunrei mappings also.
#
# Because of the irssi bind limits, the small tsu is not automatic as in
# the romaji.pl. You need to type it explicitly, using 'tt'.
# Same goes for ã and ã³, which are typed with nn or mm.
#
# There is a statusbar item which shows a glyph for the current mapping.
# [è±]èª -> [å¹³]ä»®å -> [ç]ä»®å
#
# If you want it, type
# /statusbar window add ro1_sb
# (just once, it will remember it)
use Irssi;
use Irssi::TextUI;
# Meow
# These are almost the same as in romaji.pl
my(%hira) = (
"a" => "ã", "i" => "ã", "u" => "ã", "e" => "ã", "o" => "ã",
"ka" => "ã", "ki" => "ã", "ku" => "ã", "ke" => "ã", "ko" => "ã",
"sa" => "ã", "shi" => "ã", "su" => "ã", "se" => "ã", "so" => "ã",
"ta" => "ã", "chi" => "ã¡", "tsu" => "ã¤", "te" => "ã¦", "to" => "ã¨",
"na" => "ãª", "ni" => "ã«", "nu" => "ã¬", "ne" => "ã", "no" => "ã®",
"ha" => "ã¯", "hi" => "ã²", "hu" => "ãµ", "he" => "ã¸", "ho" => "ã»", "fu" => "ãµ",
"ma" => "ã¾", "mi" => "ã¿", "mu" => "ã", "me" => "ã", "mo" => "ã",
"ya" => "ã", "yu" => "ã", "yo" => "ã",
"ra" => "ã", "ri" => "ã", "ru" => "ã", "re" => "ã", "ro" => "ã",
"wa" => "ã", "wi" => "ã", "we" => "ã", "wo" => "ã",
"nn" => "ã",
"mm" => "ã",
"ga" => "ã", "gi" => "ã", "gu" => "ã", "ge" => "ã", "go" => "ã",
"za" => "ã", "ji" => "ã", "zu" => "ã", "ze" => "ã", "zo" => "ã",
"da" => "ã ", "dzi" => "ã¢", "dzu" => "ã¥", "de" => "ã§", "do" => "ã©",
"ba" => "ã°", "bi" => "ã³", "bu" => "ã¶", "be" => "ã¹", "bo" => "ã¼",
"pa" => "ã±", "pi" => "ã´", "pu" => "ã·", "pe" => "ãº", "po" => "ã½",
"fa" => "ãµã", "fi" => "ãµã", "fe" => "ãµã", "fo" => "ãµã",
"di" => "ã§ã",
"kya" => "ãã", "kyu" => "ãã
", "kyo" => "ãã",
"sha" => "ãã", "shu" => "ãã
", "sho" => "ãã",
"cha" => "ã¡ã", "chu" => "ã¡ã
", "cho" => "ã¡ã",
"nya" => "ã«ã", "nyu" => "ã«ã
", "nyo" => "ã«ã",
"hya" => "ã²ã", "hyu" => "ã²ã
", "hyo" => "ã²ã",
"mya" => "ã¿ã", "myu" => "ã¿ã
", "myo" => "ã¿ã",
"rya" => "ãã", "ryu" => "ãã
", "ryo" => "ãã",
"gya" => "ãã", "gyu" => "ãã
", "gyo" => "ãã",
"ja" => "ãã", "ju" => "ãã
", "jo" => "ãã",
"jya" => "ãã", "jyu" => "ãã
", "jyo" => "ãã",
"dza" => "ã¢ã", "dju" => "ã¢ã
", "dzo" => "ã¢ã",
"dja" => "ã¢ã", "djo" => "ã¢ã",
"bya" => "ã³ã", "byu" => "ã³ã
", "byo" => "ã³ã",
"pya" => "ã´ã", "pyu" => "ã´ã
", "pyo" => "ã´ã",
"tt" => "ã£"
);
my(%kata) = (
"a" => "ã¢", "i" => "ã¤", "u" => "ã¦", "e" => "ã¨", "o" => "ãª",
"ka" => "ã«", "ki" => "ã", "ku" => "ã¯", "ke" => "ã±", "ko" => "ã³",
"sa" => "ãµ", "shi" => "ã·", "su" => "ã¹", "se" => "ã»", "so" => "ã½",
"ta" => "ã¿", "chi" => "ã", "tsu" => "ã", "te" => "ã", "to" => "ã",
"na" => "ã", "ni" => "ã", "nu" => "ã", "ne" => "ã", "no" => "ã",
"ha" => "ã", "hi" => "ã", "hu" => "ã", "he" => "ã", "ho" => "ã", "fu" => "ã",
"ma" => "ã", "mi" => "ã", "mu" => "ã ", "me" => "ã¡", "mo" => "ã¢",
"ya" => "ã¤", "yu" => "ã¦", "yo" => "ã¨", "ye" => "ã¨",
"ra" => "ã©", "ri" => "ãª", "ru" => "ã«", "re" => "ã¬", "ro" => "ã",
"wa" => "ã¯", "wi" => "ã°", "we" => "ã±", "wo" => "ã²",
"nn" => "ã³",
"mm" => "ã³",
"ga" => "ã¬", "gi" => "ã®", "gu" => "ã°", "ge" => "ã²", "go" => "ã´",
"za" => "ã¶", "ji" => "ã¸", "zu" => "ãº", "ze" => "ã¼", "zo" => "ã¾",
"da" => "ã", "dzi" => "ã", "dzu" => "ã
", "de" => "ã", "do" => "ã",
"ba" => "ã", "bi" => "ã", "bu" => "ã", "be" => "ã", "bo" => "ã",
"pa" => "ã", "pi" => "ã", "pu" => "ã", "pe" => "ã", "po" => "ã",
"va" => "ã´ã¡", "vi" => "ã´ã£", "vu" => "ã´", "ve" => "ã´ã§", "vo" => "ã´ã©",
"fa" => "ãã¡", "fi" => "ãã£", "fe" => "ãã§", "fo" => "ãã©",
"di" => "ãã£",
"dje" => "ãã§", "dze" => "ãã§",
"kya" => "ãã£", "kyu" => "ãã¥", "kyo" => "ãã§",
"sha" => "ã·ã£", "shu" => "ã·ã¥", "sho" => "ã·ã§",
"cha" => "ãã£", "chu" => "ãã¥", "cho" => "ãã§",
"nya" => "ãã£", "nyu" => "ãã¥", "nyo" => "ãã§",
"hya" => "ãã£", "hyu" => "ãã¥", "hyo" => "ãã§",
"mya" => "ãã£", "myu" => "ãã¥", "myo" => "ãã§",
"rya" => "ãªã£", "ryu" => "ãªã¥", "ryo" => "ãªã§",
"gya" => "ã®ã£", "gyu" => "ã®ã¥", "gyo" => "ã®ã§",
"ja" => "ã¸ã£", "ju" => "ã¸ã¥", "jo" => "ã¸ã§",
"jya" => "ã¸ã£", "jyu" => "ã¸ã¥", "jyo" => "ã¸ã§",
"dza" => "ãã£", "dju" => "ãã¥", "dzo" => "ãã§",
"dja" => "ãã£", "djo" => "ãã§",
"bya" => "ãã£", "byu" => "ãã¥", "byo" => "ãã§",
"pya" => "ãã£", "pyu" => "ãã¥", "pyo" => "ãã§",
"tt" => "ã"
);
my(%comm) = (
"-" => "ã¼",
"." => "ã",
"," => "ã",
"!" => "ï¼",
"?" => "ï¼",
"~" => "ã",
"[" => "ã", "]" => "ã",
"{" => "ã", "}" => "ã",
"(" => "ï¼", ")" => "ï¼",
"0" => "ï¼", "1" => "ï¼", "2" => "ï¼", "3" => "ï¼", "4" => "ï¼",
"5" => "ï¼", "6" => "ï¼", "7" => "ï¼", "8" => "ï¼", "9" => "ï¼",
"*" => "â
", # â is uglier :P
# where to put ⪠?
);
my(@squot) = ( "ã", "ã" );
my($squoti) = 0;
my(@dquot) = ( "ã", "ã" );
my($dquoti) = 0;
my(%hirab); # Contains DIRECT insert_texts and first-level metas for Hiragana
my(%katab); # Contains DIRECT insert_texts and first-level metas for Katakana
my(%commb); # Common binds
my(%persb); # Persistent binds (don't collide and are all second-level or more)
my($currs) = "è±"; # Current state eigo -> hiragana -> katakana
# Builds irssi binds from a hash containing romaji -> utf-8 pairs
# Arguments: sh, dh, pr
# sh: Source Hash (%hira, %kata, %comm)
# dh: Destination Hash (%hirab or %katab)
# pr: Prefix for meta keys (hira or kata)
# The function uses %persb for all non-direct binds
sub build_binds ($$$) {
my($sh) = $_[0]; # Source hash, %hira or %kata
my($dh) = $_[1]; # Destination hash, %hirab or %katab
my($pr) = $_[2]; # The prefix
my($k, $v); # for each from the source hash
while (($k, $v) = each %{$sh}) {
my($ll) = length($k); # get the length of the KEY
my($tk, $tv); # used to take apart the KEY into chars
if ($ll == 1) { # one-char KEYs are easy
${$dh}{$k} = "insert_text $v";
} elsif ($ll >= 2) {
# take the first and the second chars
$tk = substr($k, 0, 1);
$tv = substr($k, 1, 1);
# if the meta-key is not defined yet, define it now
if (!${$dh}{$tk}) {
${$dh}{$tk} = "key $pr$tk";
}
# if the KEY is 2-char, define it now
if ($ll == 2) {
$persb{"$pr$tk-$tv"} = "insert_text $v";
} else {
# otherwise register a new meta key, if not yet registered
if (!$persb{"$pr$tk-$tv"}) {
$persb{"$pr$tk-$tv"} = "key $pr$tk$tv";
}
# and now register the key...
$tk .= $tv;
$tv = substr($k, 2, 1);
$persb{"$pr$tk-$tv"} = "insert_text $v";
}
}
}
}
# Applies all binds in a given hash
sub do_binds ($) {
my($h) = $_[0];
my($k, $v);
while (($k, $v) = each %{$h}) {
Irssi::command("^bind $k $v");
}
}
# Deletes all binds existing in the given hash
sub del_binds ($) {
my($h) = $_[0];
my($k, $v);
while (($k, $v) = each %{$h}) {
Irssi::command("^bind -delete $k");
}
}
# Bindings for hiragana, next Ctrl-R will bind Katakana
sub cmd_rohira {
Irssi::command("^bind ^R /rokata");
do_binds \%hirab;
do_binds \%commb;
$currs = "å¹³";
Irssi::statusbar_items_redraw('ro1_sb');
}
# Bindings for Katakana, next Ctrl-R will restore
sub cmd_rokata {
Irssi::command("^bind ^R /rorest");
del_binds \%hirab;
do_binds \%katab;
# no need to rebind commons from %commb
$currs = "ç";
Irssi::statusbar_items_redraw('ro1_sb');
}
# Delete bindings (first-level), next Ctrl-R will bind Hiragana
sub cmd_rorest {
Irssi::command("^bind ^R /rohira");
del_binds \%katab;
del_binds \%commb;
$currs = "è±";
Irssi::statusbar_items_redraw('ro1_sb');
}
# Display the statusbar item
sub ro1_sb_show ($$) {
my ($item, $get_size_only) = @_;
$item->{min_size} = $item->{max_size} = 2;
$item->default_handler($get_size_only, "{sb " . $currs . "}", 0, 1);
}
# Register the /commands
Irssi::command_bind('rohira', 'cmd_rohira');
Irssi::command_bind('rokata', 'cmd_rokata');
Irssi::command_bind('rorest', 'cmd_rorest');
# Register the statusbar item
Irssi::statusbar_item_register('ro1_sb', 0, "ro1_sb_show");
Irssi::statusbar_items_redraw('ro1_sb');
# Bind Ctrl-R to Hiragana (initial position)
Irssi::command("^bind ^R /rohira");
# Build the bind hashes
build_binds \%hira, \%hirab, "hira";
build_binds \%kata, \%katab, "kata";
build_binds \%comm, \%commb, "comm";
# Register persistent binds... SLOWwwwwww :(((
do_binds \%persb;