#!/usr/bin/perl -w
#
# Copyright (c) 2008 petschge <mail@petschge.de>
# based on romaji.pl by Victor Ivanov <v0rbiz@yahoo.com>
#
# 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 = '0.09';
%IRSSI = (
    authors	=> 'petschge with help from pxkl2, moritz and others',
    contact	=> 'mail@petschge.de',
    name	=> 'texify',
    description => 'translates the latex names of characters to utf-8 letters',
    license	=> 'BSD 2-clause',
);

my(%letters) = (
    "Alpha" => "Α", "Beta" => "Β", "Gamma" => "Γ", "Delta" => "Δ", "Epsilon" => "Ε", "Zeta" => "Ζ", "Eta" => "Η", "Theta" => "Θ",
    "Iota" => "Ι", "Kappa" => "Κ", "Lambda" => "Λ", "Mu" => "Μ", "Mü" => "Μ", "Nu" => "Ν", "Nü" => "Ν", "Xi" => "Ξ",
    "Omicron" => "Ο", "Pi" => "Π", "Rho" => "Ρ", "Sigma" => "Σ", "Tau" => "Τ", "Upsilon" => "Υ", "Phi" => "Φ", "Chi" => "Χ",
    "Psi" => "Ψ", "Omega" => "Ω",
    "alpha" => "α", "beta" => "β", "gamma" => "γ", "delta" => "δ", "epsilon" => "ε", "zeta" => "ζ", "eta" => "η", "theta" => "θ",
    "iota" => "ι", "kappa" => "κ", "lambda" => "λ", "mu" => "µ", "mü" => "µ", "nu" => "ν", "nü" => "ν", "xi" => "ξ",
    "omicron" => "ο", "pi" => "π", "rho" => "ρ", "sigma" => "σ", "tau" => "τ", "upsilon" => "υ", "phi" => "φ", "chi" => "χ",
    "psi" => "ψ", "omega" => "ω",
    "downarrow" => "↓", "gets" =>  "←", "leftarrow" => "←", "to" => "→", "rightarrow" => "→", "uparrow" => "↑",
    "Downarrow" => "⇓", "Leftarrow" => "⇐", "Rightarrow" => "⇒", "Uparrow" => "⇑",
    "leftrightarrow" => "↔", "updownarrow" => "↕", "Leftrightarrow" => "⇔", "Updownarrow" => "⇕",
    "nearrow" => "↗", "nwarrow" => "↖", "searrow" => "↘", "swarrow" => "↙",
    "approx" => "≈", "asymp" => "≍", "bowtie" => "⋈", "join" => "⋈", "cong" => "≅", "ncong" => "≇", "doteq" => "≐",
    "equiv" => "≡", "neq" => "≠", "propto" => "∝", "sim" => "∼", "nsim" => "≁", "simeq" => "≃",
    "centerdot" => "·", "cdot" => "·", "div" => "÷", "star" => "⋆",
    "oplus" => "⊕", "ominus" => "⊖", "otimes" => "⊗", "oslash" => "⊘", "odot" => "⊙",
    "uplus" => "⊎",
    "le" => "≤", "nless" => "≮", "nleq" => "≰", "lneqq" => "≨", "ge" => "≥", "ngtr" => "≯", "ngeq" => "≱", "gneqq" => "≩",
    "ll" => "≪", "gg" => "≫",
    "subset" => "⊂", "supset" => "⊃", "subseteq" => "⊆", "supseteq" => "⊇", "sqsubset" => "⊏", "sqsupset" => "⊐",
    "sqsubseteq" => "⊑", "sqsupseteq" => "⊒", "sqcap" => "⊓", "sqcup" => "⊔",
    "prec" => "≺", "succ" => "≻", "preceq" => "≼", "succeq" => "≽",
    "vdash" => "⊢", "dashv" => "⊣", "models" => "⊧", "perp" => "⊥", "parallel" => "∥", "nparrallel" => "∦", "mid" => "∣",
    "nmid" => "∤", "hbar" => "ℏ", "infinity" => "∞", "infty" => "∞", "exists" => "∃", "forall" => "∀", "partial" => "∂",
    "neg" => "¬", "nabla" => "∇", "triangle" => "△",
    "sum" => "∑", "int" => "∫", "oint" => "∮", "prod" => "∏", "coprod" => "∐",
    "dagger" => "†", "ddagger" => "‡",
    "dots" => "…", "ldots" => "…", "angle" => "∠", "aleph" => "א", "imath" => "ⅈ", "jmath" => "ⅉ", "Re" => "ℜ", "Im" => "ℑ",
    "mho" => "℧", "emptyset" => "∅",
    "flat" => "♭", "natural" => "♮", "sharp" => "♯",
    "percent" => "%", "bullet" => "•", "diamond" => "⋄", "in" => "∈", "ni" => "∋",
    "clubsuit" => "♣", "diamondsuit" => "♢", "heartsuit" => "♥", "spadesuit" => "♤",
    "S" => "§", "P" => "¶", "copyright" => "©", "pounds" => "£",
    "AA" => "Å", "ae" => "æ", "AE" => "Æ", "L" => "Ł", "oe" => "œ", "o" => "ø", "O" => "Ø", "OE" => "Œ", "ss" => "ß",
    );

my $regex = join '|', map quotemeta, keys %letters;

my($lock_ev) = 0;

sub event1 {
    my ($line, $server, $witem) = @_;

    return unless ref $witem;
    if ($lock_ev) { return };

    $line =~ s/\\($regex)\b/$letters{$1}/g;

    $lock_ev = 1;
    Irssi::signal_emit('send command', $line, $server, $witem);
    Irssi::signal_stop();
    $lock_ev = 0;
}

Irssi::signal_add('send command', "event1");

Irssi::print($IRSSI{name}.' '.$VERSION.'%n loaded');
