% This file:
%   http://angg.twu.net/LATEX/mynewunicodechar.sty.html
%   http://angg.twu.net/LATEX/mynewunicodechar.sty
%           (find-angg "LATEX/mynewunicodechar.sty")
% Author: Eduardo Ochs <eduardoochs@gmail.com>
%
% Based on: (find-es "tex" "newunicodechar")
% (find-fline "/usr/local/texlive/2019/texmf-dist/tex/latex/newunicodechar/newunicodechar.sty")
%
% This is a modified version of Enrico Gregorio's newunicodechar.sty
% that allows calling the main macro from anywhere - even outside the
% preamble.
%
% Usage: put this in the preamble:
%   \usepackage{mynewunicodechar}   % (find-LATEX "mynewunicodechar.sty")
%
\def\defunicodechar#1#2{\newunicodechar{#1}{#2}}
%
%\NeedsTeXFormat{LaTeX2e}[2018/04/01]
%\ProvidesPackage{mynewunicodechar}
%    [2018/04/08 v1.2 Defining Unicode characters]
%\@ifundefined{eTeXversion}
%  {\PackageError{mynewunicodechar}{LaTeX engine too old, aborting}
%   {Please upgrade your TeX system}\@@end}{}
\DeclareOption{verbose}{\let\nuc@verbose=T}
\ProcessOptions\relax
\def\nuc@onebyteerr{\PackageError{mynewunicodechar}
  {ASCII character requested}
  {Only characters above U+007F may be defined; you asked
   for\MessageBreak a plain ASCII character and your definition
   has been ignored.}}
\def\nuc@emptyargerr{\PackageError{mynewunicodechar}
  {Empty argument}
  {You shouldn't write \protect\newunicodechar{}{...}}}
\def\nuc@invalidargerr{\PackageError{mynewunicodechar}
  {Invalid argument}
  {The first argument to \protect\newunicodechar\space is
   either\MessageBreak too long or an invalid sequence of bytes}}
\begingroup
\catcode`\^=7 \catcode30=12 \catcode`\!=12 % for safety
\edef\next{\@gobble^^^^0021}
\expandafter\endgroup
\ifx\next\@empty % Start of code for Unicode engines
\chardef\nuc@atcode=\catcode`\~
\catcode`\~=\active
\def\newunicodechar#1#2{%
  \if\relax\detokenize{#1}\relax
    \nuc@emptyargerr
  \else
    \if\relax\detokenize\expandafter{\@cdr#1\@nil}\relax
      \ifnum`#1>\string"7F
        \catcode`#1=\active
        \begingroup\lccode`\~=`#1
        \lowercase{\endgroup\protected\def~}{#2}%
      \else
        \nuc@onebyteerr
      \fi
    \else
      \nuc@invalidargerr
    \fi
  \fi}
\catcode`\~=\nuc@atcode
% \@onlypreamble\newunicodechar
\expandafter\endinput
\fi % End of code for Unicode engines
\def\nuc@stop{\PackageWarningNoLine{mynewunicodechar}
  {This package only works if the document\MessageBreak
   encoding is `utf8'}%
  \let\newunicodechar\@gobbletwo\endinput}
\edef\@tempa{\detokenize{utf8}}
\edef\@tempb{\detokenize\expandafter{\inputencodingname}}
\ifx\@tempb\@tempa\else
  \nuc@stop
\fi
\def\newunicodechar#1#2{%
  \@tempswafalse
  \edef\nuc@tempa{\detokenize{#1}}%
  \if\relax\nuc@tempa\relax
    \nuc@emptyargerr
  \else
    \edef\@tempb{\expandafter\@car\nuc@tempa\@nil}%
    \nuc@check
    \if@tempswa
      \@ifundefined{u8:\nuc@tempa}{}
        {\PackageWarning{mynewunicodechar}
          {Redefining Unicode character\ifdefined\nuc@verbose;
           it meant\MessageBreak
           ***\space\space\nuc@meaning\space\space***\MessageBreak
           before your redefinition\fi}}%
      \@namedef{u8:\nuc@tempa}{#2}%
    \fi
  \fi
}
\def\nuc@getlength#1{%
  \ifx#1\@nil
    \expandafter\relax
  \else
    +1\expandafter\nuc@getlength
  \fi}
\ifdefined\nuc@verbose
  \def\nuc@meaning{\expandafter\expandafter\expandafter
    \strip@prefix\expandafter\meaning\csname u8:\nuc@tempa\endcsname}
\fi
\def\nuc@check{%
  \ifcase\numexpr0\expandafter\nuc@getlength\nuc@tempa\@nil
    \or %0
    \nuc@onebyteerr\or %1
    \nuc@ch@ck{192}\or %2
    \nuc@ch@ck{224}\or %3
    \nuc@ch@ck{240}\else %4
    \nuc@invalidargerr
  \fi}
\def\nuc@ch@ck#1{%
  \expandafter\ifnum\expandafter`\@tempb<#1\relax
    \nuc@invalidargerr
  \else
    \@tempswatrue
  \fi
}
% \@onlypreamble\newunicodechar
\endinput
