#
# accent.pl	--- translate dead-key accents to troff, tex etc. (library)
#
# Copyright (c) 1995 Oscar Nierstrasz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (as the file COPYING in the main directory of
# the distribution); if not, write to the Free Software Foundation,
# Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# This package and friends can be found at:
# http://iamwww.unibe.ch/~scg/Src/
#
# Filter "dead-key" conventions to produce the right
# sequences for TeX, Framemaker mml files or troff.
#
# Examples:
# acute accent:	\'a
# grave accent:	\`e
# circumflex:	\^o (or \<o)
# dieresis:	\"u (or \:u)
# tilde:	\~n
# cedilla:	\,c
# slash:	\/o
#
# ring:		\oa		*** FRAME ONLY
# ae:		\ae		*** FRAME ONLY
#
# tau:		\tau		*** PRESENTLY ONLY FOR TEX ***
# pi:		\pi
#

# NB: if you copy this file by ftp BE SURE to use binary mode.
#
# THIS FILE CONTAINS 8-BIT CHARS!!!

package accent;

#v = 'accent.pl 1.0'; # Author: Oscar Nierstrasz (Revised: Jan 1990)
#v = 'accent.pl 1.1'; # Added 7 & 8 bit option (Sept 1991)
$v = 'accent.pl 1.2'; # Rewritten as perl script 27/6/93

# strip dead-key accents
sub strip {
    study;
    s/\\[:"]oe/oe/g;
    s/\\[:"]([AOUaou])/$1e/g;
    s/\\\/([Oo])/$1e/g;
    s/\\o([Aa])/$1a/g;
    s/\\ae/ae/g;
    s/\\ss/ss/g;
    s/\\alpha/alpha/g;
    s/\\beta/beta/g;
    s/\\pi/pi/g;
    s/\\tau/tau/g;
    s/\\.//g;
}

# convert dead-key accents to troff -ms .AM macros
sub ms {
    study;
    s/\\'([aeiou])/$1\\*'/g;
    s/\\`([aeiou])/$1\\*`/g;
    s/\\[\^<]([aeiou])/$1\\*^/g;
    s/\\:([aeiou])/$1\\*:/g;
    s/\\"([aeiou])/$1\\*:/g;
    s/\\:([AOU])/$1e/g;
    s/\\"([AOU])/$1e/g;
    s/\\~([aon])/$1\\*~/g;
    s/\\,([c])/$1\\*,/g;
    s/\\\/([Oo])/$1\\*\//g;
    s/\\ss/ss/g;
    s/\\alpha/alpha/g;
    s/\\beta/beta/g;
    s/\\tau/tau/g;
    s/\\pi/pi/g;
}

# convert dead-key accents to LaTeX
sub tex {
    study;
    s/\$/\\\$/g;
    s/[&#_]/\\$&/g;
    s/\\\/=/\$\\neq\$/g;
    s/\\(['`^"])([aeou])/\\$1{$2}/g;
    s/\\<([aeou])/\\^{$1}/g;
    s/\\(['`^"])i/\\$1{\\i}/g;
    s/\\<i/\\^{\\i}/g;
    s/\\:([aeou])/\\"{$1}/g;
    s/\\:i/\\"{\\i}/g;
    s/\\~([aon])/\\~{$1}/g;
    s/\\,([c])/\\c{$1}/g;
    s/\\\/([Oo])/{\\o}/g;
    s/\\oA/\\AA/g;
    s/\\oa/\\aa/g;
    # s/\\AE/&/g
    # s/\\ae/&/g
    # s/\\ss/&/g
    s/\\alpha/\$$&\$/g;
    s/\\beta/\$$&\$/g;
    s/\\mu/\$$&\$/g;
    s/\\tau/\$$&\$/g;
    s/\\pi/\$$&\$/g;
    s/[<>=|]+/\$$&\$/g;
}

sub mml_init {
	print <<'eof' ;
<MML 1.0 Accent macros>
<Comment "copied from Oscar Nierstrasz, corrected and completed by E. Casais">
<Comment "accent.mml --- Framemaker accents">
<!DefineMacro UAgrave       "<Character \\xcb >" >
<!DefineMacro UAacute       "<Character \\xe7 >" >
<!DefineMacro UAcircumflex  "<Character \\xe5 >" >
<!DefineMacro UAtilde       "<Character \\xcc >" >
<!DefineMacro UAdieresis    "<Character \\x80 >" >
<!DefineMacro UAring        "<Character \\x81 >" >
<!DefineMacro UAE           "<Character \\xae >" >
<!DefineMacro agrave        "<Character \\x88 >" >
<!DefineMacro aacute        "<Character \\x87 >" >
<!DefineMacro acircumflex   "<Character \\x89 >" >
<!DefineMacro atilde        "<Character \\x8b >" >
<!DefineMacro adieresis     "<Character \\x8a >" >
<!DefineMacro aring         "<Character \\x8c >" >
<!DefineMacro ae            "<Character \\xbe >" >
<!DefineMacro UEgrave       "<Character \\xe9 >" >
<!DefineMacro UEacute       "<Character \\x83 >" >
<!DefineMacro UEcircumflex  "<Character \\xe6 >" >
<!DefineMacro UEdieresis    "<Character \\xe8 >" >
<!DefineMacro egrave        "<Character \\x8f >" >
<!DefineMacro eacute        "<Character \\x8e >" >
<!DefineMacro ecircumflex   "<Character \\x90 >" >
<!DefineMacro edieresis     "<Character \\x91 >" >
<!DefineMacro UIgrave       "<Character \\xed >" >
<!DefineMacro UIacute       "<Character \\xea >" >
<!DefineMacro UIcircumflex  "<Character \\xeb >" >
<!DefineMacro UIdieresis    "<Character \\xec >" >
<!DefineMacro igrave        "<Character \\x93 >" >
<!DefineMacro iacute        "<Character \\x92 >" >
<!DefineMacro icircumflex   "<Character \\x94 >" >
<!DefineMacro idieresis     "<Character \\x95 >" >
<!DefineMacro dotlessi      "<Character \\xf5 >" >
<!DefineMacro UOgrave       "<Character \\xf1 >" >
<!DefineMacro UOacute       "<Character \\xee >" >
<!DefineMacro UOcircumflex  "<Character \\xef >" >
<!DefineMacro UOtilde       "<Character \\xcd >" >
<!DefineMacro UOdieresis    "<Character \\x85 >" >
<!DefineMacro UOslash       "<Character \\xaf >" >
<!DefineMacro UOE           "<Character \\xce >" >
<!DefineMacro ograve        "<Character \\x98 >" >
<!DefineMacro oacute        "<Character \\x97 >" >
<!DefineMacro ocircumflex   "<Character \\x99 >" >
<!DefineMacro otilde        "<Character \\x9b >" >
<!DefineMacro odieresis     "<Character \\x9a >" >
<!DefineMacro oslash        "<Character \\xbf >" >
<!DefineMacro oe            "<Character \\xcf >" >
<!DefineMacro UUgrave       "<Character \\xf4 >" >
<!DefineMacro UUacute       "<Character \\xf2 >" >
<!DefineMacro UUcircumflex  "<Character \\xf3 >" >
<!DefineMacro UUdieresis    "<Character \\x86 >" >
<!DefineMacro ugrave        "<Character \\x9d >" >
<!DefineMacro uacute        "<Character \\x9c >" >
<!DefineMacro ucircumflex   "<Character \\x9e >" >
<!DefineMacro udieresis     "<Character \\x9f >" >
<!DefineMacro UYdieresis    "<Character \\xd9 >" >
<!DefineMacro ydieresis     "<Character \\xd8 >" >
<!DefineMacro UCcedilla     "<Character \\x82 >" >
<!DefineMacro ccedilla      "<Character \\x8d >" >
<!DefineMacro UNtilde       "<Character \\x84 >" >
<!DefineMacro ntilde        "<Character \\x96 >" >
<!DefineMacro germandbls    "<Character \\xa7 >" >
<!DefineMacro alpha      "<family symbol> <Character \\x61 > <family times>" >
<!DefineMacro beta       "<family symbol> <Character \\x62 > <family times>" >
<!DefineMacro pi         "<family symbol> <Character \\x70 > <family times>" >
<!DefineMacro tau        "<family symbol> <Character \\x74 > <family times>" >
<!DefineMacro endash        "<Character \\xd0 >" >
<!DefineMacro emdash        "<Character \\xd1 >" >
<!DefineMacro quotedblleft  "<Character \\xd2 >" >
<!DefineMacro quoteblright  "<Character \\xd3 >" >
<!DefineMacro quoteleft     "<Character \\xd4 >" >
<!DefineMacro quoteright    "<Character \\xd5 >" >
<!DefineMacro guillemotleft "<Character \\xc7 >" >
<!DefineMacro guillemotright "<Character \\xc8 >" >
<!DefineMacro guilsinglleft "<Character \\xdc >" >
<!DefineMacro guilsinglright "<Character \\xdd >" >
<!DefineMacro langle "<Character \\< >" >
<!DefineMacro rangle "<Character \\> >" >
eof
}

# convert dead-key accents to Framemaker MML
sub mml {
    study;
    s/\\'([aeiou])/<$1acute>/g;
    s/\\`([aeiou])/<$1grave>/g;
    s/\\[\^<]([aeiou])/<$1circumflex>/g;
    s/\\:([aeiou])/<$1dieresis>/g;
    s/\\"([aeiou])/<$1dieresis>/g;
    s/\\~([aon])/<$1tilde>/g;
    s/\\'([AEIOU])/<U$1acute>/g;
    s/\\`([AEIOU])/<U$1grave>/g;
    s/\\[\^<]([AEIOU])/<U$1circumflex>/g;
    s/\\:([AEIOU])/<U$1dieresis>/g;
    s/\\"([AEIOU])/<U$1dieresis>/g;
    s/\\~([AON])/<U$1tilde>/g;
    s/\\,([c])/<$1cedilla>/g;
    s/\\oA/<UAring>/g;
    s/\\oa/<aring>/g;
    s/\\AE/<UAE>/g;
    s/\\ae/<ae>/g;
    s/\\ss/<germandbls>/g;
    # s/\\alpha/<alpha>/g
    # s/\\beta/<beta>/g
    # s/\\tau/<tau>/g
    # s/\\pi/<pi>/g
    s/\\\/([Oo])/<$1slash>/g;
    s/``/<quotedblleft>/g;
    s/''/<quoteblright>/g;
    s/--/<emdash>/g;
}

# convert dead-key accents to HTML
sub html {
    study;
    s/\\AE/\&AElig;/g;
    s/\\'([AEIOUYaeiouy])/\&$1acute;/g;
    s/\\[<^]([AEIOUaeiou])/\&$1circ;/g;
    s/\\`([AEIOUaeiou])/\&$1grave;/g;
    s/\\o([Aa])/\&$1ring;/g;
    s/\\~([ANOano])/\&$1tilde;/g;
    s/\\[:"]([AEIOUYaeiouy])/\&$1uml;/g;
    s/\\,([Cc])/\&$1cedil;/g;
    s/\\\/([Oo])/\&$1slash;/g;
    s/\\ss/\&szlig;/g;
}

# convert dead-key accents to overstruck characters
sub os {
    study;
    s/\\'([aeiou])/\\o'\\'$1'/g;
    s/\\`([aeiou])/\\o'\\(ga$1'/g;
    s/\\[\^<]([aeiou])/\\o'^$1'/g;
    s/\\[:"]([aeiou])/\\o'\\(um$1'/g;
    s/\\~([an])/\\o'~$1'/g;
    s/\\,([c])/\\o'\\(cdc'/g;
    s/\\\/([Oo])/\\o'\/$1'/g;
    s/\\tau/tau/g;
    s/\\pi/pi/g;
}

# convert nixdorf accents to dead-key accents
sub nix {
    study;
    s/\\\*([aeiouAEIOU])/\\"$1/g;
    s/\\tau/tau/g;
    s/\\pi/pi/g;
}

# convert accents from PC (DOS) files
# used for SI files
sub filemaker {
    study;
    s/\210/\\`a/g;
    s/\212/\\:a/g;
    s/\215/\\,c/g;
    s/\216/\\'e/g;
    s/\217/\\`e/g;
    s/\220/\\<e/g;
    s/\230/\\`o/g;
    s/\231/\\<o/g;
    s/\232/\\:o/g;
    s/\237/\\:u/g;
}

# convert accents from PC (DOS) files
# used for SI files
# pipe through cat -v first
sub si {
    study;
    s/M-^H/\\`a/g; s/M-^J/\\:a/g; s/M-^M/\\,c/g;
    s/M-^N/\\'e/g; s/M-^O/\\`e/g; s/M-^P/\\<e/g;
    s/M-^Y/\\<a/g; s/M-^Z/\\:o/g; s/M-^_/\\:u/g;
}

# convert accents from PC (DOS) files
# pipe through cat -v first
# Used for Gert Florijn's address files
sub pc {
    study;
    s/M-^A/\\:u/g; s/M-^B/\\'e/g; s/M-^C/\\<a/g;
    s/M-^D/\\:a/g; s/M-^E/\\`a/g; s/M-^G/\\,c/g;
    s/M-^H/\\<e/g; s/M-^I/\\:e/g; s/M-^J/\\`e/g;
    s/M-^K/\\:i/g; s/M-^L/\\<i/g; s/M-^S/\\<o/g;
    s/M-^T/\\:o/g;
}

# convert from Mac (Word?) files
# INCOMPLETE
sub mac {
	s/=46/F/g;
	s/=C9/\\'E/g;
	s/=DC/\\:U/g;
	s/=E0/\\`a/g;
	s/=E2/\\<a/g;
	s/=E7/\\,c/g;
	s/=E8/\\`e/g;
	s/=E9/\\'e/g;
	s/=EA/\\<e/g;
	s/=EB/\\:e/g;
	s/=FC/\\:u/g;
        s/=E4/\\:a/g;
        s/=F4/\\<o/g;
        s/=F6/\\:o/g;
        s/=FC/\\:u/g;
        s/=93/``/g;
        s/=\n//g;
}

# convert 8bit ascii to 7bit escapes
#
# not handled:
# ¤ ¦ ¨ ª ¬ ­ ¯ ° ´ ¶ ¸ º Þ ð þ
sub seven {
    study;
    s/’/'/g; s/“/``/g; s/”/''/g;
    s/·/\\bu/g; s/¿/\\??/g; s/¡/\\!!/g; s/×/\\xx/g;
    s/±/\\+-/g; s/÷/\\-:/g; s/«/\\<</g; s/»/\\>>/g;
    s/Ð/\\-D/g; s/£/\\-L/g; s/¥/\\-Y/g; s/¢/\\\/c/g;
    s/½/\\12/g; s/¼/\\14/g; s/¾/\\34/g; s/¹/\\^1/g;
    s/²/\\^2/g; s/³/\\^3/g; s/ß/\\ss/g; s/µ/\\\/u/g;
    s/§/\\so/g; s/ß/\\beta/g; s/µ/\\mu/g; s/©/\\co/g;
    s/®/\\ro/g; s/Æ/\\AE/g; s/æ/\\ae/g; s/Å/\\oA/g;
    s/å/\\oa/g; s/Ø/\\\/O/g; s/ø/\\\/o/g; s/Ç/\\,C/g;
    s/ç/\\,c/g; s/À/\\`A/g; s/È/\\`E/g; s/Ì/\\`I/g;
    s/Ò/\\`O/g; s/Ù/\\`U/g; s/à/\\`a/g; s/è/\\`e/g;
    s/ì/\\`i/g; s/ò/\\`o/g; s/ù/\\`u/g; s/Á/\\'A/g;
    s/É/\\'E/g; s/Í/\\'I/g; s/Ó/\\'O/g; s/Ú/\\'U/g;
    s/Ý/\\'Y/g; s/á/\\'a/g; s/é/\\'e/g; s/í/\\'i/g;
    s/ó/\\'o/g; s/ú/\\'u/g; s/ý/\\'y/g; s/Ä/\\:A/g;
    s/Ë/\\:E/g; s/Ï/\\:I/g; s/Ö/\\:O/g; s/Ü/\\:U/g;
    s/ä/\\:a/g; s/ë/\\:e/g; s/ï/\\:i/g; s/ö/\\:o/g;
    s/ü/\\:u/g; s/ÿ/\\:y/g; s/Ä/\\"A/g; s/Ë/\\"E/g;
    s/Ï/\\"I/g; s/Ö/\\"O/g; s/Ü/\\"U/g; s/ä/\\"a/g;
    s/ë/\\"e/g; s/ï/\\"i/g; s/ö/\\"o/g; s/ü/\\"u/g;
    s/ÿ/\\"y/g; s/Â/\\<A/g; s/Ê/\\<E/g; s/Î/\\<I/g;
    s/Ô/\\<O/g; s/Û/\\<U/g; s/â/\\<a/g; s/ê/\\<e/g;
    s/î/\\<i/g; s/ô/\\<o/g; s/û/\\<u/g; s/Â/\\^A/g;
    s/Ê/\\^E/g; s/Î/\\^I/g; s/Ô/\\^O/g; s/Û/\\^U/g;
    s/â/\\^a/g; s/ê/\\^e/g; s/î/\\^i/g; s/ô/\\^o/g;
    s/û/\\^u/g; s/Ã/\\~A/g; s/Ñ/\\~N/g; s/Õ/\\~O/g;
    s/ã/\\~a/g; s/ñ/\\~n/g; s/õ/\\~o/g;
}

# convert dead-key accents to 8bit meta-chars
# not handled:
# ¤ ¦ ¨ ª ¬ ­ ¯ ° ´ ¶ · ¸ º Þ ð
sub eight {
    study;
    s/\\bu/·/g; s/\\\?\?/¿/g; s/\\!!/¡/g; s/\\xx/×/g;
    s/\\\+-/±/g; s/\\-:/÷/g; s/\\<</«/g; s/\\>>/»/g;
    s/\\-D/Ð/g; s/\\-L/£/g; s/\\-Y/¥/g; s/\\\/c/¢/g;
    s/\\12/½/g; s/\\14/¼/g; s/\\34/¾/g; s/\\\^1/¹/g;
    s/\\\^2/²/g; s/\\\^3/³/g; s/\\ss/ß/g; s/\\\/u/µ/g;
    s/\\so/§/g; s/\\beta/ß/g; s/\\mu/µ/g; s/\\co/©/g;
    s/\\ro/®/g; s/\\AE/Æ/g; s/\\ae/æ/g; s/\\oA/Å/g;
    s/\\oa/å/g; s/\\\/O/Ø/g; s/\\\/o/ø/g; s/\\,C/Ç/g;
    s/\\,c/ç/g; s/\\`A/À/g; s/\\`E/È/g; s/\\`I/Ì/g;
    s/\\`O/Ò/g; s/\\`U/Ù/g; s/\\`a/à/g; s/\\`e/è/g;
    s/\\`i/ì/g; s/\\`o/ò/g; s/\\`u/ù/g; s/\\'A/Á/g;
    s/\\'E/É/g; s/\\'I/Í/g; s/\\'O/Ó/g; s/\\'U/Ú/g;
    s/\\'Y/Ý/g; s/\\'a/á/g; s/\\'e/é/g; s/\\'i/í/g;
    s/\\'o/ó/g; s/\\'u/ú/g; s/\\'y/ý/g; s/\\:A/Ä/g;
    s/\\:E/Ë/g; s/\\:I/Ï/g; s/\\:O/Ö/g; s/\\:U/Ü/g;
    s/\\:a/ä/g; s/\\:e/ë/g; s/\\:i/ï/g; s/\\:o/ö/g;
    s/\\:u/ü/g; s/\\:y/ÿ/g; s/\\"A/Ä/g; s/\\"E/Ë/g;
    s/\\"I/Ï/g; s/\\"O/Ö/g; s/\\"U/Ü/g; s/\\"a/ä/g;
    s/\\"e/ë/g; s/\\"i/ï/g; s/\\"o/ö/g; s/\\"u/ü/g;
    s/\\"y/ÿ/g; s/\\<A/Â/g; s/\\<E/Ê/g; s/\\<I/Î/g;
    s/\\<O/Ô/g; s/\\<U/Û/g; s/\\<a/â/g; s/\\<e/ê/g;
    s/\\<i/î/g; s/\\<o/ô/g; s/\\<u/û/g; s/\\^A/Â/g;
    s/\\^E/Ê/g; s/\\^I/Î/g; s/\\^O/Ô/g; s/\\^U/Û/g;
    s/\\^a/â/g; s/\\^e/ê/g; s/\\^i/î/g; s/\\^o/ô/g;
    s/\\^u/û/g; s/\\~A/Ã/g; s/\\~N/Ñ/g; s/\\~O/Õ/g;
    s/\\~a/ã/g; s/\\~n/ñ/g; s/\\~o/õ/g;
}

1;

