%***************************************************************
%
%% betababel.sty
%% Copyright 2005 Felix G. Berkemeier
% 
%
% This work may be distributed and/or modified under the 
% conditions of the LaTeX Project Public License, either 
% version 1.3 of this license or (at your option) any later 
% version.
% 
% The latest version of this license is in
% 
%   http://www.latex-project.org/lppl.txt
%   
% and version 1.3 or later is part of all distributions of LaTeX
% version 2003/12/01 or later.
% 
% Please report errors to: felix.berkemeier@gmx.de
% 
% This work consists of the file betababel.sty
% 
%***************************************************************

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{betababel}%
  [2005/07/10 v0.5 betababel.sty Beta Code for Babel package]

\IfFileExists{teubner.sty}%
{%
	\def\TeubnerLo@ded{}
	\PassOptionsToPackage{greek}{babel}
	\DeclareOption{polutonikogreek}{}
	\DeclareOption{greek}{}
	\DeclareOption*{%
	  \PassOptionsToPackage{\CurrentOption}{babel}%
	}
	\ProcessOptions\relax
	\RequirePackage{babel}
	\RequirePackage{teubner}%
}%
{%
	\PassOptionsToPackage{polutonikogreek}{babel}
	\DeclareOption{polutonikogreek}{}
	\DeclareOption{greek}{}
	\DeclareOption*{%
	  \PassOptionsToPackage{\CurrentOption}{babel}%
	}
	\ProcessOptions\relax
	\RequirePackage{babel}%
}

%***************************************************************

\newlength\betaskip
\setlength\betaskip{\medskipamount}

{%
	\catcode`$=0
	\catcode`$\=12
	$catcode`$&=6
	$catcode`$#=12
	$catcode`$==12
	$gdef$Terminus{Terminus}
	$gdef$empty@{}
	$newcount$suffix@
	$suffix@=-48
	$newcount$Bet@Mode
	$newcount$FirstP@r
	$newcount$FirstWord@
	$newcount$Ch@rCode
	
	$gdef$SigmaW@rning&1{$gdef$GreekCh@r{}$PackageWarning{betababel}{The BetaCode s&1 does not exist for greek characters}}
	$gdef$LunateW@rning{$PackageWarning{betababel}{The Lunate Sigma (BetaCode: s3) is not implemented (yet?)!}}
	$gdef$SubDotW@rning{$PackageWarning{betababel}{The Subscript Dot (BetaCode: ?) is not implemented (yet?)!}}
	$gdef$JW@rning{$PackageError{betababel}{'J' and 'j' aren't valid characters in BetaCode! You shouldn't use them, because they can make betababel interrupt. Please remove them from your source or replace them by 'S' or 's'}{You can continue typesetting by pressing <return>, but maybe betababel has ignored some of the BetaCode text you wanted to be typeset. You should correct your document and eliminate every 'J' or 'j' within the BetaCode.}}
	$gdef$HashW@rning{$PackageWarning{betababel}{There is no BetaCode '*#'. Therefore those two characters have been ignored}}
	$gdef$H@shWarning&1{$PackageWarning{betababel}{The extended functionality of betababel (e.g. all the codes containing the hash character '#') only works when teubner.sty is installed. Unless your TeX finds teubner.sty, all the unusable characters will be ignored --- as e.g. the &1 }}
	
	$gdef$SetCodes@{%
		$lccode`(=0$lccode`)=0
		$lccode`[=0$lccode`]=0
		$lccode`+=0$lccode`/=0
		$lccode`$==0$lccode`|=0
		$uccode`(=0$uccode`)=0
		$uccode`[=0$uccode`]=0
		$uccode`+=0$uccode`/=0
		$uccode`$==0$uccode`|=0
		$lccode`-=0$lccode`_=0
		$lccode`.=0$lccode`,=0
		$lccode`:=0$lccode`;=0
		$lccode`'=0$lccode`?=0
	}
	
	$long$gdef$s@veText&1{$xdef$TextToTypeset@{&1}}
	$gdef$@ddToText{$let$tempString@$TextToTypeset@$addToText@}
	$long$gdef$addToText@&1{$s@veText{$tempString@&1}}
	$gdef$@ddCharToText{$let$tempString@$TextToTypeset@$addCh@rToText}
	$gdef$addCh@rToText&1{$s@veText{$tempString@$char&1}}
	$gdef$@ddCsToText{$let$tempString@$TextToTypeset@$addCsToText@}
	$gdef$addCsToText@&1{$s@veText{$tempString@$myProtect@$csname&1$endcsname}}
		
	$gdef$bcode{%
		$expandafter$ifx$csname TeubnerLo@ded$endcsname$relax$fi
		$def$TextToTypeset@{}%
		$Bet@Mode=1
		$FirstP@r=1
		$noindent
		$begingroup
		$catcode`$=0
		$catcode`$\=12
		$catcode`$&=6
		$catcode`$#=12
		$catcode`$==12
		$Bet@Code
	}
	
	$long$gdef$betacode{%
		$expandafter$ifx$csname TeubnerLo@ded$endcsname$relax$fi
		$def$TextToTypeset@{}%
		$Bet@Mode=0
		$vspace{$betaskip}
		$begingroup
		$catcode`$=0
		$catcode`$\=12
		$catcode`$&=6
		$catcode`$#=12
		$catcode`$==12
		$Bet@Environment
	}
	
	$long$gdef$Bet@Environment&1\end{%
		$SetCodes@
		$gdef$myProtect@{$noexpand$myProtect@$noexpand}%
		$ReadP@r&1$par Terminus$par
		$endgroup
%		$message{Final:$TextToTypeset@}% Just for debug purposes
		$gdef$myProtect@{}%
		$foreignlanguage{polutonikogreek}{$obeyspaces$TextToTypeset@}
		$end
	}
	
	$long$gdef$endbetacode{%
		$par$vspace{$betaskip}
	}
	
	$long$gdef$Bet@Code&1{%
		$SetCodes@
		$gdef$myProtect@{$noexpand$myProtect@$noexpand}%
		$ReadP@r&1$par Terminus$par
		$endgroup
%		$message{Final:$TextToTypeset@}% Just for debug purposes
		$gdef$myProtect@{}%
		$foreignlanguage{polutonikogreek}{$obeyspaces$TextToTypeset@}%
	}
	
	$gdef$ReadP@r&1$par{%
	  $gdef$p@rTemp{&1}%
	  $ifx$p@rTemp$Terminus%
	  $else
	    $ifcase$Bet@Mode$@ddToText{$par}$or%
	      $ifnum$FirstP@r=1$FirstWord@=1$FirstP@r=0$else$@ddToText{$par$noindent}$fi%
	    $fi%
	    $Re@dWord&1 J 
	    $expandafter$ReadP@r
	  $fi
	}
	
	$gdef$Re@dWord&1 {%
	  $gdef$wordTemp@{&1}%
	  $if&1J%
	  $else$ifx$wordTemp@$empty@
	    $else
	      $ifnum$FirstWord@=1$FirstWord@=0$else$@ddToText{{ }}$fi%
	      $ReadCh@r&1 JJJJJJ$Terminus
	    $fi
	    $expandafter$Re@dWord
	  $fi
	}

	$gdef$ReadCh@r&1&2&3&4&5&6$Terminus{%
%	  $message{&1,&2,&3,&4,&5,&6; }% Just for debug purposes
	  $lowercase{$if&1}j%
	    $lowercase{$if&2}j%
	    $else$JW@rning$ReadCh@r&2&3&4&5&6$Terminus
	    $fi
	  $else$if&1*%
	      $if&2#%
	        $ifx$TeubnerLo@ded$relax
	          $H@shWarning{&2}%
	          $ReadCh@r&3&4&5&6$Terminus
	        $else
	          $ifcase1&3$or$HashW@rning$ReadCh@r&3&4&5&6$Terminus
	          $else$ifcase1&4$or$H@ndleHash&3jj$ReadCh@r&4&5&6$Terminus
	            $else$ifcase1&5$or$H@ndleHash&3&4j$ReadCh@r&5&6$Terminus
	              $else$H@ndleHash&3&4&5$ReadCh@r&6$Terminus
	              $fi
	            $fi
	          $fi
	        $fi
	      $else
	        $ifnum$lccode`&2=0
	          $ifnum$lccode`&3=0
	            $ifnum$lccode`&4=0
	              $uppercase{$Bet@Spiritus&2&3&4&5}%
	              $ReadCh@r&6$Terminus
	            $else$uppercase{$Bet@Spiritus&2&3j&4}%
	              $ReadCh@r&5&6$Terminus
	            $fi
	          $else$uppercase{$Bet@Spiritus&2jj&3}%
	            $ReadCh@r&4&5&6$Terminus
	          $fi
	        $else$lowercase{$if&2}v%
	            $@ddCsToText{Digamma}%
	          $else
	            $uppercase{$Bet@Spiritus&3&4&5&2}%
	          $fi
	          $ReadCh@r&3&4&5&6$Terminus
	        $fi
	      $fi
	    $else$if&1#%
	        $ifx$TeubnerLo@ded$relax
	          $H@shWarning{&1}
	          $ReadCh@r&2&3&4&5&6$Terminus
	        $else
	          $ifcase1&2$or$@ddToText{$char254{}}$ReadCh@r&2&3&4&5&6$Terminus
	          $else$ifcase1&3$or$HandleH@sh&2jj$ReadCh@r&3&4&5&6$Terminus
	            $else$ifcase1&4$or$HandleH@sh&2&3j$ReadCh@r&4&5&6$Terminus
	              $else$HandleH@sh&2&3&4$ReadCh@r&5&6$Terminus
	              $fi
	            $fi
	          $fi
	        $fi
	      $else$ifnum$lccode`&1=0
	          $ifnum`&1=45$@ddToText{-}$fi%               -
	          $ifnum`&1=95$@ddToText{--}$fi%              _ -> --
	          $ifnum`&1=46$@ddToText{.}$fi%               .
	          $ifnum`&1=44$@ddToText{,}$fi%               ,
	          $ifnum`&1=58$@ddToText{;}$fi%               : -> ;
	          $ifnum`&1=59$@ddToText{?}$fi%               ; -> ?
	          $ifnum`&1=39$@ddToText{''}$fi%              ' -> ''
	          $ifnum`&1=91$@ddToText{[}$fi%				[
	          $ifnum`&1=93$@ddToText{]}$fi%				]
	          $ifnum`&1=63$SubDotW@rning$fi%              ?
	        $else$lowercase{$if&1}v%
	            $@ddCsToText{digamma}%
	          $else
	            $lowercase{$Bet@Spiritus&2&3&4&1}%
	          $fi
	        $fi
	        $ReadCh@r&2&3&4&5&6$Terminus
	      $fi
	    $fi
	  $fi
	}
	
	$gdef$HandleH@sh&1&2&3{% for unprefixed hashes: #...
%		$message{hash: &1,&2,&3}% Just for debug purposes
		$ifcase1&1$or$else
			$ifcase&1$HandleH@sh&2&3j%
				$or$ifcase1&2$or$@ddCsToText{koppa}%		#1
					$else$ifcase&2%
						$or
						$or$ifcase1&3$or$@ddToText{---}%	#12
							$fi
						$fi
					$fi
				$or$ifcase1&2$or$@ddCsToText{stigma}%		#2
					$else$ifcase&2%
						$or
						$or$ifcase1&3$or$@ddToText{$char255{}}%	#22
							$fi
						$fi
					$fi
				$or$ifcase1&2$or$@ddCsToText{coppa}%		#3
					$fi
				$or
				$or$ifcase1&2$or$@ddCsToText{sampi}%		#5
					$fi
				$or$ifcase1&2$or$@ddCsToText{paragr}%		#6
					$fi
				$or
				$or
				$or
			$else
			$fi
		$fi
	}
	
	$gdef$H@ndleHash&1&2&3{% for hashes with asterisk prefix: *#...
%		$message{Hash: &1,&2,&3}% Just for debug purposes
		$ifcase1&1$or$else
			$ifcase&1$H@ndleHash&2&3j%
				$or
				$or$ifcase1&2$or$@ddCsToText{Stigma}%		*#2
					$fi
				$or$ifcase1&2$or$@ddCsToText{Coppa}%		*#3
					$fi
				$or
				$or$ifcase1&2$or$@ddCsToText{Sampi}%		*#5
					$fi
				$or
				$or
				$or
				$or
			$else
			$fi
		$fi
	}

	$gdef$Fin@lSigma&1&2{%
	  $lowercase{$if&1}s%
	    $ifnum`&2=45$suffix@=58
	    $else$ifnum`&2=46$suffix@=58
	      $else$ifnum`&2=44$suffix@=58
	        $else$ifnum`&2=58$suffix@=58
	          $else$ifnum`&2=59$suffix@=58
	            $else$ifnum`&2=63$suffix@=58
	              $else$advance$suffix@`&2
	              $fi
	            $fi
	          $fi
	        $fi
	      $fi
	    $fi
	  $fi
	}

	$gdef$Bet@Spiritus&1&2&3&4{%
	  $Fin@lSigma&4&1%
	  $if&1($@ddToText{<}$Bet@Accent&2&3&4%
	  $else$if&1)$@ddToText{>}$Bet@Accent&2&3&4%
	    $else$if&1+$@ddToText{"}$Bet@Accent&2&3&4%
	      $else$Bet@Accent&1&2&4%
	      $fi
	    $fi
	  $fi
	}

	{%
		$catcode`$~=12
		$gdef$Bet@Accent&1&2&3{%
	      $if&1/$@ddToText{'}$Bet@IotaSigma&2&3%
	      $else$if&1\$@ddToText{`}$Bet@IotaSigma&2&3%
	        $else$if&1=$@ddToText{~}$Bet@IotaSigma&2&3%
	          $else$if&1!$@ddToText{`}$Bet@IotaSigma&2&3% workaround, see below
	          $else$Bet@IotaSigma&1&3%
	          $fi% this is a workaround for the footnote bug (when using betacode within another command)
	          $fi
	        $fi
	      $fi
	    }
    }

	$gdef$Bet@IotaSigma&1&2{%
	  $if&2s%
	    $ifcase$suffix@$expandafter$SigmaW@rning$number$suffix@
	      $or$@ddToText{sv}%
	      $or$@ddToText{c}%
	      $or$LunateW@rning
	      $or$expandafter$SigmaW@rning$number$suffix@
	      $or$expandafter$SigmaW@rning$number$suffix@
	      $or$expandafter$SigmaW@rning$number$suffix@
	      $or$expandafter$SigmaW@rning$number$suffix@
	      $or$expandafter$SigmaW@rning$number$suffix@
	      $or$expandafter$SigmaW@rning$number$suffix@
	    $else
	      $ifnum$suffix@=58$@ddToText{s}%
	      $else$@ddToText{sv}%
	      $fi
	    $fi
	  $else$Ch@rCode=`&2
	    $lowercase{$if&2}q$advance$Ch@rCode-7$fi
	    $lowercase{$if&2}c$advance$Ch@rCode21$fi
	    $lowercase{$if&2}x$advance$Ch@rCode-7$fi
	    $@ddCharToText{$number$Ch@rCode}%
	  $fi
	  $if&1|$@ddToText{|}%
	  $fi
	  $suffix@=-48
	}
}
