%%
%% This is file 'pxcjkcat.sty'.
%%
%% Copyright (c) 2010-2025 Takayuki YATO (aka. "ZR")
%%   GitHub:   https://github.com/zr-tex8r
%%   Twitter:  @zr_tex8r
%%
%% This package is distributed under the MIT License.
%%

%% package declaration
\NeedsTeXFormat{pLaTeX2e}
\ProvidesPackage{pxcjkcat}[2025/02/20 v1.6 PX cjk-category mode]
\def\pxkc@pkgname{pxcjkcat}

%% code guards
\edef\pxkc@restore@codes{%
\catcode33=\the\catcode33%
\catcode34=\the\catcode34%
\catcode39=\the\catcode39%
\catcode43=\the\catcode43%
\catcode45=\the\catcode45%
\catcode46=\the\catcode46%
\catcode47=\the\catcode47%
\catcode58=\the\catcode58%
\catcode60=\the\catcode60%
\catcode62=\the\catcode62%
\catcode63=\the\catcode63%
\catcode94=\the\catcode94%
\catcode96=\the\catcode96%
\catcode239=\the\catcode239%
\endlinechar=\the\endlinechar%
\relax}%
\catcode33=12 % <!>
\catcode34=12 % <">
\catcode39=12 % <'>
\catcode43=12 % <+>
\catcode45=12 % <->
\catcode46=12 % <.>
\catcode47=12 % </>
\catcode58=12 % <:>
\catcode60=12 % <<>
\catcode62=12 % <>>
\catcode63=12 % <?>
\catcode94=7 % <^>
\catcode96=12 % <`>
\catcode239=13 %
\AtEndOfPackage{%
\pxkc@restore@codes
\let\pxkc@restore@codes\relax}

%--------------------------------------- general

%% error messages
\def\pxkc@error{\PackageError\pxkc@pkgname}
\def\pxkc@err@ivcsy#1{%
  \PackageError\pxkc@pkgname
   {Invalid name (#1) for cjk-category}%
   {It was replaced by 18(cjk).\MessageBreak
    \@ehc}}
\def\pxkc@err@ivbsy#1{%
  \PackageError\pxkc@pkgname
   {Invalid name (#1) for Unicode block}%
   {\@eha}}
\def\pxkc@err@ivmsy#1{%
  \PackageError\pxkc@pkgname
   {Invalid name (#1) for cjk-category mode}%
   {\@eha}}
\def\pxkc@err@naltn{%
  \PackageError\pxkc@pkgname
   {Change to cjk-category of 'latn' is forbidden}%
   {\@eha}}
\def\pxkc@warn@dfblk#1{%
  \PackageWarning\pxkc@pkgname
   {Unsupported Unicode block '#1' is\MessageBreak
    specified}}
\def\pxkc@err@ivfmt#1{%
  \PackageError\pxkc@pkgname
   {Block specs are given in bad format%
    \ifx\detokenize\@undefined\else
      :\MessageBreak \space'\detokenize{#1}'\fi}%
   {\@eha}}

%% \pxkc@debug{<message>}
\ifx\bxUseDebug\@undefined
\let\pxkc@debug\@gobble
\else
\def\pxkc@debug#1{\typeout{**[\pxkc@pkgname] #1}}
\fi

%% packages
\RequirePackage{keyval}\relax

%% unique tokens
\def\pxkc@end{\pxkc@end@}
\def\pxkc@mt{\pxkc@mt@}
\let\pxkc@mk\indent

%% variables
\newif\ifpxkc@ok
\newif\ifpxkc@warn@bad \pxkc@warn@badtrue
\let\pxkc@good@mccv\relax
\let\pxkc@mccv\relax
\let\pxkc@ccmode@opt\relax
\let\pxkc@arg\relax
\let\pxkc@blk\relax
\let\pxkc@cp\relax

%% \pxkc@letcs\CSa{<csname-b>}
\def\pxkc@letcs#1#2{%
  \expandafter\let\expandafter#1\csname#2\endcsname}

%% \pxkc@expanded{<text>}
\def\pxkc@expanded#1{%
  \edef\pxkc@next{#1}\pxkc@next}

%% \pxkc@setup{<key>=<value>,...}
\def\pxkc@setup#1{%
  \let\pxkc@KV@errx\KV@errx \def\KV@errx##1{\@unknownoptionerror}%
  \setkeys{\pxkc@pkgname}{#1}
  \let\KV@errx\pxkc@KV@errx}

%% \pxkc@handle@key{<key>}{<value>}{<name>}
\def\pxkc@handle@key#1#2#3{%
  \expandafter\let\expandafter\pxkc@tmpa\csname pxkc@#3\endcsname
  \ifx\pxkc@tmpa\relax
    \PackageError\pxkc@pkgname
     {Invalid value for key '#1':\MessageBreak\@spaces#2}%
     {\@eha}%
  \else \pxkc@tmpa
  \fi}

%--------------------------------------- package options

%% mode CCV options
\DeclareOption{ccv1}{\chardef\pxkc@mccv=1 }
\DeclareOption{ccv2}{\chardef\pxkc@mccv=2 }
\DeclareOption{ccv3}{\chardef\pxkc@mccv=3 }
\DeclareOption{ccv4}{\chardef\pxkc@mccv=4 }
\DeclareOption{ccv5}{\chardef\pxkc@mccv=5 }
\DeclareOption{ccv+}{\chardef\pxkc@mccv=0 }
\DeclareOption{real}{\chardef\pxkc@mccv=0 }
%% mode options
% NB. \[pxkc@M@@...] is later redefined.
\def\pxkc@M@@forcecjk     {1}
\def\pxkc@M@@prefercjk    {1}
\def\pxkc@M@@prefercjkvar {1}
\def\pxkc@M@@japanese     {3}
\def\pxkc@M@@japanesevar  {3}
\def\pxkc@M@@prefernoncjk {1}
\def\pxkc@M@@default      {1}
\def\pxkc@M@@nomode       {0}% special
\chardef\pxkc@good@mccv=0
%% key-value options
\define@key{\pxkc@pkgname}{ccv}{%
  \pxkc@handle@key{ccv}{#1}{kv@ccv@@#1}}
\@namedef{pxkc@kv@ccv@@1}{\chardef\pxkc@mccv=1 }
\@namedef{pxkc@kv@ccv@@2}{\chardef\pxkc@mccv=2 }
\@namedef{pxkc@kv@ccv@@3}{\chardef\pxkc@mccv=3 }
\@namedef{pxkc@kv@ccv@@4}{\chardef\pxkc@mccv=4 }
\@namedef{pxkc@kv@ccv@@real}{\chardef\pxkc@mccv=0 }
%% dispatch
\DeclareOption*{%
  \@ifundefined{pxkc@M@@\CurrentOption}{% key option
    \expandafter\pxkc@setup\expandafter{\CurrentOption}%
  }{%else; mode option
    \chardef\pxkc@good@mccv=\@nameuse{pxkc@M@@\CurrentOption}\relax
    \edef\pxkc@ccmode@opt{\CurrentOption}}}
\ProcessOptions\relax

%% clear up
\ifnum\pxkc@good@mccv=0
  \let\pxkc@ccmode@opt\relax
\fi
\let\pxkc@M@@nomode\@undefined

%% set default mode CCV (ccv1)
\ifx\pxkc@mccv\relax
  \chardef\pxkc@mccv=1
\else
  \chardef\pxkc@good@mccv=0 % disable check
\fi

%--------------------------------------- environment check

%% check engine
\def\pxkc@tmpa{Native upTeX is required}
\ifx\ucs\@undefined\else \ifnum\ucs"3000="3000
  \ifx\forcecjktoken\@undefined
    \def\pxkc@tmpa{This upTeX is too old}
  \else \let\pxkc@tmpa\relax
  \fi
\fi\fi
\ifx\pxkc@tmpa\relax\else
  \PackageError\pxkc@pkgname
   {\pxkc@tmpa}%
   {Package loading is aborted.\MessageBreak
    \@ehc}
\expandafter\endinput\fi\relax

%% engine version
\chardef\pxkc@uptex@version=0
\def\pxkc@next#1.#2#3#4\pxkc@end{%
  \ifx\pxkc@mk#1\pxkc@mk \ifnum99<1#2#3
    \chardef\pxkc@uptex@version=\the\uptexversion#2#3\relax
  \fi\fi}
\ifx\uptexversion\@undefined\else \ifx\uptexrevision\@undefined\else
  \expandafter\pxkc@next\uptexrevision...\pxkc@end
\fi\fi

%%-------- classification version check

%% \pxkc@is@same@block
\def\pxkc@is@same@block#1#2{%
  \pxkc@okfalse
  \begingroup
    \kcatcode#1=16\kcatcode#2=17\relax
    \ifnum\kcatcode#1=17 \aftergroup\pxkc@oktrue\fi
  \endgroup}

%% \pxkc@ccv : real CCV
\pxkc@is@same@block{"FF01}{"FF11}\ifpxkc@ok
  \chardef\pxkc@ccv=1     % v0.11+
\else
  \pxkc@is@same@block{"A0}{"C0}\ifpxkc@ok
    \chardef\pxkc@ccv=2   % v0.29+
  \else\ifnum\pxkc@uptex@version<125
    \chardef\pxkc@ccv=3   % v1.23+
  \else\ifnum\pxkc@uptex@version<135
    \chardef\pxkc@ccv=4   % v1.25+
  \else
    \chardef\pxkc@ccv=5   % v1.35+
  \fi\fi\fi
\fi
\ifx\bxDebugCcv\@undefined\else % override
  \chardef\pxkc@ccv=\bxDebugCcv\relax
\fi
\pxkc@debug{upTeX version \number\pxkc@uptex@version}
\pxkc@debug{CJK category version \number\pxkc@ccv}

%% Here show warning about the buggy engine version (v1.24)
\ifnum\pxkc@ccv=1 \ifnum\pxkc@uptex@version=124
  \PackageWarningNoLine\pxkc@pkgname
   {The upTeX engine of version 1.24 is used!\MessageBreak
    This version has a bug on CJK category,\MessageBreak
    and here real CCV is regard as 1}
\fi\fi

%% Here show warning for mode CCV recommendation.
%% (No warning if the good mode CCV is unavailable.)
\ifnum\pxkc@good@mccv>\pxkc@mccv \ifnum\pxkc@good@mccv>\pxkc@ccv\else
  \PackageWarningNoLine\pxkc@pkgname
   {Mode CCV \number\pxkc@good@mccv\space
      (option 'ccv\number\pxkc@good@mccv') is recommended\MessageBreak
    for the use of category mode '\pxkc@ccmode@opt'!\MessageBreak
    Or you can simply suppress this warning by giving\MessageBreak
    option 'ccv\number\pxkc@mccv' explicitly}
\fi\fi

%% set mode CCV if 'ccv+' is given
\ifnum\pxkc@mccv=\z@
  \let\pxkc@mccv\pxkc@ccv
\fi

%% mode CCV cannot be newer than real CCV
\ifnum\pxkc@mccv>\pxkc@ccv
  \PackageError\pxkc@pkgname
   {Bad mode-CCV option (ccv\number\pxkc@mccv) is given!\MessageBreak
    The mode-CCV (\number\pxkc@mccv) is newer than
      the real CCV (\number\pxkc@ccv)}
   {I assume the mode-CCV equals the real CCV.\MessageBreak
    \@ehc}
  \let\pxkc@mccv\pxkc@ccv
\fi

%--------------------------------------- helpers

%% constants
\chardef\pxkc@ascii@max="7F

%% \pxkc@cond\ifXXX...\fi{<true>}{<false>}
\def\pxkc@cond#1\fi{%
  #1\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}

%% \pxkc@get@jchar{<charcode>}
\def\pxkc@get@jchar#1{%
  \begingroup
    \forcecjktoken
    \kansujichar\@ne=#1\relax
    \xdef\pxkc@jchar{\kansuji\@ne}%
  \endgroup}

%% \pxkc@kcatcode{<charcode>}{<kcatcode>}
\ifx\bxUseDebug\@undefined
\def\pxkc@kcatcode#1#2{\kcatcode#1=#2\relax}
\else
\def\pxkc@kcatcode#1#2{%
  \kcatcode#1=#2\relax
  \chardef\pxkc@tmpa=#1\relax
  \expandafter\pxkc@kcatcode@a\meaning\pxkc@tmpa\pxkc@end}
\def\pxkc@kcatcode@a#1"#2\pxkc@end{%
  \pxkc@debug{\string\kcatcode"#2 set to \number\kcatcode\pxkc@tmpa}}
\fi

%--------------------------------------- data tables

%% variables
%\[pxkc@B@@<block>] : start charcode of the block
%\[pxkc@G@@<block>] : members of the group block
%\[pxkc@C@@<category>] : kcatcode value of the category

%% \pxkc@bslst
\def\pxkc@bslst{\@gobble}
\def\pxkc@bbslst{}
%% \pxkc@bval{<min_ccv>}{<block>}{<charcode>}{<full_name>}
\def\pxkc@bval#1#2#3#4{% #4 is unused for now
  \ifnum#1<\z@\else
    % check if min_ccv >= 2 (now min_ccv has no other role)
    \ifnum#1<\tw@ \pxkc@okfalse
    \else
      \@tempcnta=#3\relax
      \@tempcntb\@tempcnta \advance\@tempcntb\m@ne
      \pxkc@is@same@block\@tempcnta\@tempcntb
    \fi
    \ifpxkc@ok % bad
      \@namedef{pxkc@B@@#2}{-1}%
      \edef\pxkc@bbslst{\pxkc@bbslst\space#2}%
    \else\ifnum#3=\z@ % 'latn'
      \@namedef{pxkc@B@@#2}{0}%
    \else
      \@namedef{pxkc@B@@#2}{#3}%
      \edef\pxkc@bslst{\pxkc@bslst,#2}%
    \fi\fi
  \fi}
%% \pxkc@gval{<gblock>}{<block>,...}{<full_name>}
\def\pxkc@gval#1#2#3{\@namedef{pxkc@G@@#1}{#2}}

%% \pxkc@cval{<category>}{<kcatcode>}
\def\pxkc@cval#1#2{\@namedef{pxkc@C@@#1}{#2}}
\pxkc@cval{noncjk}{15}
\pxkc@cval{kanji}{16}
\pxkc@cval{han}{16}
\pxkc@cval{kana}{17}
\pxkc@cval{cjk}{18}
\pxkc@cval{hangul}{19}
\ifnum\pxkc@ccv<5 \else
\pxkc@cval{modifier}{20}
\fi

%% Basic blocks
%B======================================
\pxkc@bval{1}{latn}{"0}         {Basic Latin}
\pxkc@bval{1}{latn1}{"80}       {Latin-1 Supplement}
\pxkc@bval{1}{latnA}{"100}      {Latin Extended-A}
\pxkc@bval{1}{latnB}{"180}      {Latin Extended-B}
\pxkc@bval{1}{latn2}{"250}      {IPA Extensions}
\pxkc@bval{1}{sym01}{"2B0}      {Spacing Modifier Letters}
\pxkc@bval{1}{sym02}{"300}      {Combining Diacritical Marks}
\pxkc@bval{1}{grek}{"370}       {Greek and Coptic}
\pxkc@bval{1}{cyrl}{"400}       {Cyrillic}
\pxkc@bval{1}{cyrl1}{"500}      {Cyrillic Supplement}
\pxkc@bval{1}{armn}{"530}       {Armenian}
\pxkc@bval{1}{hebr}{"590}       {Hebrew}
\pxkc@bval{1}{arab}{"600}       {Arabic}
\pxkc@bval{1}{syrc}{"700}       {Syriac}
\pxkc@bval{1}{arab1}{"750}      {Arabic Supplement}
\pxkc@bval{1}{thaa}{"780}       {Thaana}
\pxkc@bval{1}{nkoo}{"7C0}       {NKo}
\pxkc@bval{2}{samr}{"800}       {Samaritan}
\pxkc@bval{2}{mand}{"840}       {Mandaic}
\pxkc@bval{3}{syrc1}{"860}      {Syriac Supplement}
\pxkc@bval{4}{arabB}{"870}      {Arabic Extended-B}
\pxkc@bval{2}{arabA}{"8A0}      {Arabic Extended-A}
\pxkc@bval{1}{deva}{"900}       {Devanagari}
\pxkc@bval{1}{beng}{"980}       {Bengali}
\pxkc@bval{1}{guru}{"A00}       {Gurmukhi}
\pxkc@bval{1}{gujr}{"A80}       {Gujarati}
\pxkc@bval{1}{orya}{"B00}       {Oriya}
\pxkc@bval{1}{taml}{"B80}       {Tamil}
\pxkc@bval{1}{telu}{"C00}       {Telugu}
\pxkc@bval{1}{knda}{"C80}       {Kannada}
\pxkc@bval{1}{mlym}{"D00}       {Malayalam}
\pxkc@bval{1}{sinh}{"D80}       {Sinhala}
\pxkc@bval{1}{thai}{"E00}       {Thai}
\pxkc@bval{1}{laoo}{"E80}       {Lao}
\pxkc@bval{1}{tibt}{"F00}       {Tibetan}
\pxkc@bval{1}{mymr}{"1000}      {Myanmar}
\pxkc@bval{1}{geor}{"10A0}      {Georgian}
\pxkc@bval{1}{hang1}{"1100}     {Hangul Jamo}
\pxkc@bval{1}{ethi}{"1200}      {Ethiopic}
\pxkc@bval{1}{ethi1}{"1380}     {Ethiopic Supplement}
\pxkc@bval{1}{cher}{"13A0}      {Cherokee}
\pxkc@bval{1}{cans}{"1400}      {Unified Canadian Aboriginal Syllabics}
\pxkc@bval{1}{ogam}{"1680}      {Ogham}
\pxkc@bval{1}{runr}{"16A0}      {Runic}
\pxkc@bval{1}{tglg}{"1700}      {Tagalog}
\pxkc@bval{1}{hano}{"1720}      {Hanunoo}
\pxkc@bval{1}{buhd}{"1740}      {Buhid}
\pxkc@bval{1}{tagb}{"1760}      {Tagbanwa}
\pxkc@bval{1}{khmr}{"1780}      {Khmer}
\pxkc@bval{1}{mong}{"1800}      {Mongolian}
\pxkc@bval{2}{cans1}{"18B0}     {Unified Canadian Aboriginal Syllabics Extended}
\pxkc@bval{1}{limb}{"1900}      {Limbu}
\pxkc@bval{1}{tale}{"1950}      {Tai Le}
\pxkc@bval{1}{talu}{"1980}      {New Tai Lue}
\pxkc@bval{1}{khmr1}{"19E0}     {Khmer Symbols}
\pxkc@bval{1}{bugi}{"1A00}      {Buginese}
\pxkc@bval{2}{lana}{"1A20}      {Tai Tham}
\pxkc@bval{2}{sym52}{"1AB0}     {Combining Diacritical Marks Extended}
\pxkc@bval{1}{bali}{"1B00}      {Balinese}
\pxkc@bval{2}{sund}{"1B80}      {Sundanese}
\pxkc@bval{2}{batk}{"1BC0}      {Batak}
\pxkc@bval{2}{lepc}{"1C00}      {Lepcha}
\pxkc@bval{2}{olck}{"1C50}      {Ol Chiki}
\pxkc@bval{2}{cyrlC}{"1C80}     {Cyrillic Extended-C}
\pxkc@bval{4}{geor2}{"1C90}     {Georgian Extended}
\pxkc@bval{2}{sund1}{"1CC0}     {Sundanese Supplement}
\pxkc@bval{2}{sym38}{"1CD0}     {Vedic Extensions}
\pxkc@bval{1}{latn4}{"1D00}     {Phonetic Extensions}
\pxkc@bval{1}{latn5}{"1D80}     {Phonetic Extensions Supplement}
\pxkc@bval{1}{sym03}{"1DC0}     {Combining Diacritical Marks Supplement}
\pxkc@bval{1}{latn3}{"1E00}     {Latin Extended Additional}
\pxkc@bval{1}{grek1}{"1F00}     {Greek Extended}
\pxkc@bval{1}{sym04}{"2000}     {General Punctuation}
\pxkc@bval{1}{sym05}{"2070}     {Superscripts and Subscripts}
\pxkc@bval{1}{sym06}{"20A0}     {Currency Symbols}
\pxkc@bval{1}{sym07}{"20D0}     {Combining Diacritical Marks for Symbols}
\pxkc@bval{1}{sym08}{"2100}     {Letterlike Symbols}
\pxkc@bval{1}{sym09}{"2150}     {Number Forms}
\pxkc@bval{1}{sym10}{"2190}     {Arrows}
\pxkc@bval{1}{sym11}{"2200}     {Mathematical Operators}
\pxkc@bval{1}{sym12}{"2300}     {Miscellaneous Technical}
\pxkc@bval{1}{sym13}{"2400}     {Control Pictures}
\pxkc@bval{1}{sym14}{"2440}     {Optical Character Recognition}
\pxkc@bval{1}{sym15}{"2460}     {Enclosed Alphanumerics}
\pxkc@bval{1}{sym16}{"2500}     {Box Drawing}
\pxkc@bval{1}{sym17}{"2580}     {Block Elements}
\pxkc@bval{1}{sym18}{"25A0}     {Geometric Shapes}
\pxkc@bval{1}{sym19}{"2600}     {Miscellaneous Symbols}
\pxkc@bval{1}{sym20}{"2700}     {Dingbats}
\pxkc@bval{1}{sym21}{"27C0}     {Miscellaneous Mathematical Symbols-A}
\pxkc@bval{1}{sym22}{"27F0}     {Supplemental Arrows-A}
\pxkc@bval{1}{brai}{"2800}      {Braille Patterns}
\pxkc@bval{1}{sym23}{"2900}     {Supplemental Arrows-B}
\pxkc@bval{1}{sym24}{"2980}     {Miscellaneous Mathematical Symbols-B}
\pxkc@bval{1}{sym25}{"2A00}     {Supplemental Mathematical Operators}
\pxkc@bval{1}{sym26}{"2B00}     {Miscellaneous Symbols and Arrows}
\pxkc@bval{1}{glag}{"2C00}      {Glagolitic}
\pxkc@bval{1}{latnC}{"2C60}     {Latin Extended-C}
\pxkc@bval{1}{copt}{"2C80}      {Coptic}
\pxkc@bval{1}{geor1}{"2D00}     {Georgian Supplement}
\pxkc@bval{1}{tfng}{"2D30}      {Tifinagh}
\pxkc@bval{1}{ethi2}{"2D80}     {Ethiopic Extended}
\pxkc@bval{2}{cyrlA}{"2DE0}     {Cyrillic Extended-A}
\pxkc@bval{1}{sym27}{"2E00}     {Supplemental Punctuation}
\pxkc@bval{1}{cjk01}{"2E80}     {CJK Radicals Supplement}
\pxkc@bval{1}{cjk02}{"2F00}     {Kangxi Radicals}
\pxkc@bval{1}{cjk03}{"2FF0}     {Ideographic Description Characters}
\pxkc@bval{1}{cjk04}{"3000}     {CJK Symbols and Punctuation}
\pxkc@bval{1}{hira}{"3040}      {Hiragana}
\pxkc@bval{1}{kana}{"30A0}      {Katakana}
\pxkc@bval{1}{bopo}{"3100}      {Bopomofo}
\pxkc@bval{1}{hang2}{"3130}     {Hangul Compatibility Jamo}
\pxkc@bval{1}{cjk05}{"3190}     {Kanbun}
\pxkc@bval{1}{bopo1}{"31A0}     {Bopomofo Extended}
\pxkc@bval{1}{cjk06}{"31C0}     {CJK Strokes}
\pxkc@bval{1}{kana1}{"31F0}     {Katakana Phonetic Extensions}
\pxkc@bval{1}{cjk07}{"3200}     {Enclosed CJK Letters and Months}
\pxkc@bval{1}{cjk08}{"3300}     {CJK Compatibility}
\pxkc@bval{1}{haniA}{"3400}     {CJK Unified Ideographs Extension A}
\pxkc@bval{1}{sym28}{"4DC0}     {Yijing Hexagram Symbols}
\pxkc@bval{1}{hani}{"4E00}      {CJK Unified Ideographs}
\pxkc@bval{1}{yiii}{"A000}      {Yi Syllables}
\pxkc@bval{1}{yiii1}{"A490}     {Yi Radicals}
\pxkc@bval{2}{lisu}{"A4D0}      {Lisu}
\pxkc@bval{2}{vaii}{"A500}      {Vai}
\pxkc@bval{2}{cyrlB}{"A640}     {Cyrillic Extended-B}
\pxkc@bval{2}{bamu}{"A6A0}      {Bamum}
\pxkc@bval{1}{sym29}{"A700}     {Modifier Tone Letters}
\pxkc@bval{1}{latnD}{"A720}     {Latin Extended-D}
\pxkc@bval{1}{sylo}{"A800}      {Syloti Nagri}
\pxkc@bval{2}{sym39}{"A830}     {Common Indic Number Forms}
\pxkc@bval{1}{phag}{"A840}      {Phags-pa}
\pxkc@bval{2}{saur}{"A880}      {Saurashtra}
\pxkc@bval{2}{deva1}{"A8E0}     {Devanagari Extended}
\pxkc@bval{2}{kali}{"A900}      {Kayah Li}
\pxkc@bval{2}{rjng}{"A930}      {Rejang}
\pxkc@bval{2}{hangA}{"A960}     {Hangul Jamo Extended-A}
\pxkc@bval{2}{java}{"A980}      {Javanese}
\pxkc@bval{2}{mymrB}{"A9E0}     {Myanmar Extended-B}
\pxkc@bval{2}{cham}{"AA00}      {Cham}
\pxkc@bval{2}{mymrA}{"AA60}     {Myanmar Extended-A}
\pxkc@bval{2}{tavt}{"AA80}      {Tai Viet}
\pxkc@bval{2}{mtei1}{"AAE0}     {Meetei Mayek Extensions}
\pxkc@bval{2}{ethiA}{"AB00}     {Ethiopic Extended-A}
\pxkc@bval{2}{latnE}{"AB30}     {Latin Extended-E}
\pxkc@bval{2}{cher1}{"AB70}     {Cherokee Supplement}
\pxkc@bval{2}{mtei}{"ABC0}      {Meetei Mayek}
\pxkc@bval{1}{hang}{"AC00}      {Hangul Syllables}
\pxkc@bval{2}{hangB}{"D7B0}     {Hangul Jamo Extended-B}
\pxkc@bval{1}{spc01}{"D800}     {High Surrogates}
\pxkc@bval{1}{spc02}{"DB80}     {High Private Use Surrogates}
\pxkc@bval{1}{spc03}{"DC00}     {Low Surrogates}
\pxkc@bval{1}{spc04}{"E000}     {Private Use Area}
\pxkc@bval{1}{hani1}{"F900}     {CJK Compatibility Ideographs}
\pxkc@bval{1}{latn6}{"FB00}     {Alphabetic Presentation Forms}
\pxkc@bval{1}{arab2}{"FB50}     {Arabic Presentation Forms-A}
\pxkc@bval{1}{spc05}{"FE00}     {Variation Selectors}
\pxkc@bval{1}{cjk09}{"FE10}     {Vertical Forms}
\pxkc@bval{1}{sym30}{"FE20}     {Combining Half Marks}
\pxkc@bval{1}{cjk10}{"FE30}     {CJK Compatibility Forms}
\pxkc@bval{1}{cjk11}{"FE50}     {Small Form Variants}
\pxkc@bval{1}{arab3}{"FE70}     {Arabic Presentation Forms-B}
\pxkc@bval{1}{cjk12}{"FF00}     {Halfwidth and Fullwidth Forms}
\pxkc@bval{1}{spc06}{"FFF0}     {Specials}
\pxkc@bval{1}{linb}{"10000}     {Linear B Syllabary}
\pxkc@bval{1}{linb1}{"10080}    {Linear B Ideograms}
\pxkc@bval{1}{sym31}{"10100}    {Aegean Numbers}
\pxkc@bval{1}{grek2}{"10140}    {Ancient Greek Numbers}
\pxkc@bval{2}{sym40}{"10190}    {Ancient Symbols}
\pxkc@bval{2}{sym41}{"101D0}    {Phaistos Disc}
\pxkc@bval{2}{lyci}{"10280}     {Lycian}
\pxkc@bval{2}{cari}{"102A0}     {Carian}
\pxkc@bval{2}{copt1}{"102E0}    {Coptic Epact Numbers}
\pxkc@bval{1}{ital}{"10300}     {Old Italic}
\pxkc@bval{1}{goth}{"10330}     {Gothic}
\pxkc@bval{2}{perm}{"10350}     {Old Permic}
\pxkc@bval{1}{ugar}{"10380}     {Ugaritic}
\pxkc@bval{1}{xpeo}{"103A0}     {Old Persian}
\pxkc@bval{1}{dsrt}{"10400}     {Deseret}
\pxkc@bval{1}{shaw}{"10450}     {Shavian}
\pxkc@bval{1}{osma}{"10480}     {Osmanya}
\pxkc@bval{2}{osge}{"104B0}     {Osage}
\pxkc@bval{2}{elba}{"10500}     {Elbasan}
\pxkc@bval{2}{aghb}{"10530}     {Caucasian Albanian}
\pxkc@bval{4}{vith}{"10570}     {Vithkuqi}
\pxkc@bval{4}{todr}{"105C0}     {Todhri}
\pxkc@bval{2}{lina}{"10600}     {Linear A}
\pxkc@bval{4}{latnF}{"10780}    {Latin Extended-F}
\pxkc@bval{1}{cprt}{"10800}     {Cypriot Syllabary}
\pxkc@bval{2}{armi}{"10840}     {Imperial Aramaic}
\pxkc@bval{2}{palm}{"10860}     {Palmyrene}
\pxkc@bval{2}{nbat}{"10880}     {Nabataean}
\pxkc@bval{2}{hatr}{"108E0}     {Hatran}
\pxkc@bval{1}{phnx}{"10900}     {Phoenician}
\pxkc@bval{2}{lydi}{"10920}     {Lydian}
\pxkc@bval{2}{mero}{"10980}     {Meroitic Hieroglyphs}
\pxkc@bval{2}{merc}{"109A0}     {Meroitic Cursive}
\pxkc@bval{1}{khar}{"10A00}     {Kharoshthi}
\pxkc@bval{2}{sarb}{"10A60}     {Old South Arabian}
\pxkc@bval{2}{narb}{"10A80}     {Old North Arabian}
\pxkc@bval{2}{mani}{"10AC0}     {Manichaean}
\pxkc@bval{2}{avst}{"10B00}     {Avestan}
\pxkc@bval{2}{prti}{"10B40}     {Inscriptional Parthian}
\pxkc@bval{2}{phli}{"10B60}     {Inscriptional Pahlavi}
\pxkc@bval{2}{phlp}{"10B80}     {Psalter Pahlavi}
\pxkc@bval{2}{orkh}{"10C00}     {Old Turkic}
\pxkc@bval{2}{hung}{"10C80}     {Old Hungarian}
\pxkc@bval{4}{rohg}{"10D00}     {Hanifi Rohingya}
\pxkc@bval{4}{gara}{"10D40}     {Garay}
\pxkc@bval{2}{sym42}{"10E60}    {Rumi Numeral Symbols}
\pxkc@bval{4}{yezi}{"10E80}     {Yezidi}
\pxkc@bval{4}{arabC}{"10EC0}    {Arabic Extended-C}
\pxkc@bval{4}{sogo}{"10F00}     {Old Sogdian}
\pxkc@bval{4}{sogd}{"10F30}     {Sogdian}
\pxkc@bval{4}{ougr}{"10F70}     {Old Uyghur}
\pxkc@bval{4}{chrs}{"10FB0}     {Chorasmian}
\pxkc@bval{4}{elym}{"10FE0}     {Elymaic}
\pxkc@bval{2}{brah}{"11000}     {Brahmi}
\pxkc@bval{2}{kthi}{"11080}     {Kaithi}
\pxkc@bval{2}{sora}{"110D0}     {Sora Sompeng}
\pxkc@bval{2}{cakm}{"11100}     {Chakma}
\pxkc@bval{2}{mahj}{"11150}     {Mahajani}
\pxkc@bval{2}{shrd}{"11180}     {Sharada}
\pxkc@bval{2}{sinh1}{"111E0}    {Sinhala Archaic Numbers}
\pxkc@bval{2}{khoj}{"11200}     {Khojki}
\pxkc@bval{2}{mult}{"11280}     {Multani}
\pxkc@bval{2}{sind}{"112B0}     {Khudawadi}
\pxkc@bval{2}{gran}{"11300}     {Grantha}
\pxkc@bval{4}{tutg}{"11380}     {Tulu-Tigalari}
\pxkc@bval{2}{newa}{"11400}     {Newa}
\pxkc@bval{2}{tirh}{"11480}     {Tirhuta}
\pxkc@bval{2}{sidd}{"11580}     {Siddham}
\pxkc@bval{2}{modi}{"11600}     {Modi}
\pxkc@bval{2}{mong1}{"11660}    {Mongolian Supplement}
\pxkc@bval{2}{takr}{"11680}     {Takri}
\pxkc@bval{4}{mymrC}{"116D0}    {Myanmar Extended-C}
\pxkc@bval{2}{ahom}{"11700}     {Ahom}
\pxkc@bval{4}{dogr}{"11800}     {Dogra}
\pxkc@bval{2}{wara}{"118A0}     {Warang Citi}
\pxkc@bval{4}{diak}{"11900}     {Dives Akuru}
\pxkc@bval{4}{nand}{"119A0}     {Nandinagari}
\pxkc@bval{3}{zanb}{"11A00}     {Zanabazar Square}
\pxkc@bval{3}{soyo}{"11A50}     {Soyombo}
\pxkc@bval{4}{cansA}{"11AB0}    {Unified Canadian Aboriginal Syllabics Extended-A}
\pxkc@bval{2}{pauc}{"11AC0}     {Pau Cin Hau}
\pxkc@bval{4}{devaA}{"11B00}    {Devanagari Extended-A}
\pxkc@bval{4}{sunu}{"11BC0}     {Sunuwar}
\pxkc@bval{2}{bhks}{"11C00}     {Bhaiksuki}
\pxkc@bval{2}{marc}{"11C70}     {Marchen}
\pxkc@bval{3}{gonm}{"11D00}     {Masaram Gondi}
\pxkc@bval{4}{gong}{"11D60}     {Gunjala Gondi}
\pxkc@bval{4}{maka}{"11EE0}     {Makasar}
\pxkc@bval{4}{kawi}{"11F00}     {Kawi}
\pxkc@bval{4}{lisu1}{"11FB0}    {Lisu Supplement}
\pxkc@bval{4}{taml1}{"11FC0}    {Tamil Supplement}
\pxkc@bval{1}{xsux}{"12000}     {Cuneiform}
\pxkc@bval{1}{xsux1}{"12400}    {Cuneiform Numbers and Punctuation}
\pxkc@bval{2}{xsux2}{"12480}    {Early Dynastic Cuneiform}
\pxkc@bval{4}{cpmn}{"12F90}     {Cypro-Minoan}
\pxkc@bval{2}{egyp}{"13000}     {Egyptian Hieroglyphs}
\pxkc@bval{4}{egyp1}{"13430}    {Egyptian Hieroglyph Format Controls}
\pxkc@bval{4}{egypA}{"13460}    {Egyptian Hieroglyphs Extended-A}
\pxkc@bval{2}{hluw}{"14400}     {Anatolian Hieroglyphs}
\pxkc@bval{4}{gukh}{"16100}     {Gurung Khema}
\pxkc@bval{2}{bamu1}{"16800}    {Bamum Supplement}
\pxkc@bval{2}{mroo}{"16A40}     {Mro}
\pxkc@bval{4}{tnsa}{"16A70}     {Tangsa}
\pxkc@bval{2}{bass}{"16AD0}     {Bassa Vah}
\pxkc@bval{2}{hmng}{"16B00}     {Pahawh Hmong}
\pxkc@bval{4}{krai}{"16D40}     {Kirat Rai}
\pxkc@bval{4}{medf}{"16E40}     {Medefaidrin}
\pxkc@bval{2}{plrd}{"16F00}     {Miao}
\pxkc@bval{2}{cjk14}{"16FE0}    {Ideographic Symbols and Punctuation}
\pxkc@bval{2}{tang}{"17000}     {Tangut}
\pxkc@bval{2}{tang1}{"18800}    {Tangut Components}
\pxkc@bval{4}{kits}{"18B00}     {Khitan Small Script}
\pxkc@bval{4}{tang2}{"18D00}    {Tangut Supplement}
\pxkc@bval{4}{kanaB}{"1AFF0}    {Kana Extended-B}
\pxkc@bval{2}{kana2}{"1B000}    {Kana Supplement}
\pxkc@bval{3}{kanaA}{"1B100}    {Kana Extended-A}
\pxkc@bval{4}{kana3}{"1B130}    {Small Kana Extension}
\pxkc@bval{3}{nshu}{"1B170}     {Nushu}
\pxkc@bval{2}{dupl}{"1BC00}     {Duployan}
\pxkc@bval{2}{sym53}{"1BCA0}    {Shorthand Format Controls}
\pxkc@bval{4}{sym66}{"1CC00}    {Symbols for Legacy Computing Supplement}
\pxkc@bval{4}{sym64}{"1CF00}    {Znamenny Musical Notation}
\pxkc@bval{1}{sym32}{"1D000}    {Byzantine Musical Symbols}
\pxkc@bval{1}{sym33}{"1D100}    {Musical Symbols}
\pxkc@bval{1}{sym34}{"1D200}    {Ancient Greek Musical Notation}
\pxkc@bval{4}{sym65}{"1D2C0}    {Kaktovik Numerals}
\pxkc@bval{4}{sym58}{"1D2E0}    {Mayan Numerals}
\pxkc@bval{1}{sym35}{"1D300}    {Tai Xuan Jing Symbols}
\pxkc@bval{1}{sym36}{"1D360}    {Counting Rod Numerals}
\pxkc@bval{1}{sym37}{"1D400}    {Mathematical Alphanumeric Symbols}
\pxkc@bval{2}{sgnw}{"1D800}     {Sutton SignWriting}
\pxkc@bval{4}{latnG}{"1DF00}    {Latin Extended-G}
\pxkc@bval{2}{glag1}{"1E000}    {Glagolitic Supplement}
\pxkc@bval{4}{cyrlD}{"1E030}    {Cyrillic Extended-D}
\pxkc@bval{4}{hmnp}{"1E100}     {Nyiakeng Puachue Hmong}
\pxkc@bval{4}{toto}{"1E290}     {Toto}
\pxkc@bval{4}{wcho}{"1E2C0}     {Wancho}
\pxkc@bval{4}{nagm}{"1E4D0}     {Nag Mundari}
\pxkc@bval{4}{onao}{"1E5D0}     {Ol Onal}
\pxkc@bval{4}{ethiB}{"1E7E0}    {Ethiopic Extended-B}
\pxkc@bval{2}{mend}{"1E800}     {Mende Kikakui}
\pxkc@bval{2}{adlm}{"1E900}     {Adlam}
\pxkc@bval{4}{sym59}{"1EC70}    {Indic Siyaq Numbers}
\pxkc@bval{4}{sym61}{"1ED00}    {Ottoman Siyaq Numbers}
\pxkc@bval{2}{sym51}{"1EE00}    {Arabic Mathematical Alphabetic Symbols}
\pxkc@bval{2}{sym43}{"1F000}    {Mahjong Tiles}
\pxkc@bval{2}{sym44}{"1F030}    {Domino Tiles}
\pxkc@bval{2}{sym46}{"1F0A0}    {Playing Cards}
\pxkc@bval{2}{sym45}{"1F100}    {Enclosed Alphanumeric Supplement}
\pxkc@bval{2}{cjk13}{"1F200}    {Enclosed Ideographic Supplement}
\pxkc@bval{2}{sym47}{"1F300}    {Miscellaneous Symbols and Pictographs}
\pxkc@bval{2}{sym48}{"1F600}    {Emoticons}
\pxkc@bval{2}{sym54}{"1F650}    {Ornamental Dingbats}
\pxkc@bval{2}{sym49}{"1F680}    {Transport and Map Symbols}
\pxkc@bval{2}{sym50}{"1F700}    {Alchemical Symbols}
\pxkc@bval{2}{sym55}{"1F780}    {Geometric Shapes Extended}
\pxkc@bval{2}{sym56}{"1F800}    {Supplemental Arrows-C}
\pxkc@bval{2}{sym57}{"1F900}    {Supplemental Symbols and Pictographs}
\pxkc@bval{4}{sym60}{"1FA00}    {Chess Symbols}
\pxkc@bval{4}{sym62}{"1FA70}    {Symbols and Pictographs Extended-A}
\pxkc@bval{4}{sym63}{"1FB00}    {Symbols for Legacy Computing}
\pxkc@bval{1}{haniB}{"20000}    {CJK Unified Ideographs Extension B}
\pxkc@bval{2}{haniC}{"2A700}    {CJK Unified Ideographs Extension C}
\pxkc@bval{2}{haniD}{"2B740}    {CJK Unified Ideographs Extension D}
\pxkc@bval{2}{haniE}{"2B820}    {CJK Unified Ideographs Extension E}
\pxkc@bval{3}{haniF}{"2CEB0}    {CJK Unified Ideographs Extension F}
\pxkc@bval{4}{haniI}{"2EBF0}    {CJK Unified Ideographs Extension I}
\pxkc@bval{1}{hani2}{"2F800}    {CJK Compatibility Ideographs Supplement}
\pxkc@bval{4}{haniG}{"30000}    {CJK Unified Ideographs Extension G}
\pxkc@bval{4}{haniH}{"31350}    {CJK Unified Ideographs Extension H}
\pxkc@bval{4}{haniJ}{"323B0}    {CJK Unified Ideographs Extension J}
\pxkc@bval{1}{spc07}{"E0000}    {Tags}
\pxkc@bval{1}{spc08}{"E0100}    {Variation Selectors Supplement}
\pxkc@bval{1}{spc09}{"F0000}    {Supplementary Private Use Area-A}
\pxkc@bval{1}{spc10}{"100000}   {Supplementary Private Use Area-B}
%=======================================
%% Block subdivisions
\ifnum\pxkc@ccv<2 \else
\pxkc@gval{cjk12}{cjk1a,cjk1b,cjk1c}{Halfwidth and Fullwidth Forms}
\pxkc@bval{0}{cjk1a}{"FF01}{Halfwidth and Fullwidth Forms/Other}
\pxkc@bval{0}{cjk1b}{"FF10}{Halfwidth and Fullwidth Forms/Latin}
\pxkc@bval{0}{cjk1c}{"FF66}{Halfwidth and Fullwidth Forms/Kana}
\fi
\ifnum\pxkc@ccv<3 \else
\pxkc@gval{latn1}{latnx,latny}{Latin-1 Supplement}
\pxkc@bval{0}{latnx}{"00A0}{Latin-1 Supplement/Other}
\pxkc@bval{0}{latny}{"00C0}{Latin-1 Supplement/Latin}
\fi
\ifnum\pxkc@ccv<5 \else
\pxkc@gval{hira}{hirax,hiram}{Hiragana}
\pxkc@bval{0}{hirax}{"3040}{Hiragana/Other}
\pxkc@bval{0}{hiram}{"3099}{Hiragana/Modifier}
\pxkc@gval{sym07}{sym07x,sym07m}{Combining Diacritical Marks for Symbols}
\pxkc@bval{0}{sym07x}{"20D0}{Combining Diacritical Marks for Symbols/Other}
\pxkc@bval{0}{sym07m}{"20E3}{Combining Diacritical Marks for Symbols/Modifier}
\pxkc@gval{sym45}{sym45x,sym45m}{Enclosed Alphanumeric Supplement}
\pxkc@bval{0}{sym45x}{"1F100}{Enclosed Alphanumeric Supplement/Other}
\pxkc@bval{0}{sym45m}{"1F1E6}{Enclosed Alphanumeric Supplement/Modifier}
\pxkc@gval{sym47}{sym47x,sym47m}{Miscellaneous Symbols and Pictographs}
\pxkc@bval{0}{sym47x}{"1F300}{Miscellaneous Symbols and Pictographs/Other}
\pxkc@bval{0}{sym47m}{"1F3FB}{Miscellaneous Symbols and Pictographs/Modifier}
\fi
\ifx\pxkc@bbslst\@empty\else
  \pxkc@debug{Unsupported blocks:\pxkc@bbslst}
\fi

%% \pxkc@foreach@blocks
\edef\pxkc@foreach@blocks{%
  \noexpand\@for\noexpand\pxkc@blk:={\pxkc@bslst}}

%% clean up
\let\pxkc@bval\relax
\let\pxkc@cval\relax
\let\pxkc@gval\relax
\let\pxkc@bslst\relax
\let\pxkc@bbslst\relax

%--------------------------------------- public interfaces

%%<*> \cjkcategory{<block>}{<category>}
% Sets the kcatcode of block <block> to <category>.
% Here <block> is given as a block name or a single character in the
% block (groups are not considered) to be specified;
% <category> is given as a category name or a kcatcode value (15--19).
\newcommand*\cjkcategory{%
  \begingroup
    \forcecjktoken
  \expandafter\endgroup\pxkc@cjkcategory@}% read args
\def\pxkc@cjkcategory@#1#2{%
  \edef\pxkc@arg{#2}%
  \ifnum2<1\pxkc@arg\relax \else % <category> is a number
    \@ifundefined{pxkc@C@@#2}{%
      \pxkc@err@ivcsy{#2}\def\pxkc@arg{18}%
    }{%else
      \edef\pxkc@arg{\@nameuse{pxkc@C@@#2}}%
    }%
  \fi
  \pxkc@cjkcategory@a{#1}}
\def\pxkc@cjkcategory@a#1{%{<block>}
  \pxkc@expand@text\pxkc@tmpa{#1}%
  \ifx\pxkc@tmpa\relax
    \pxkc@err@ivfmt{#1}%
  \else
    \@for\pxkc@blk:=\pxkc@tmpa\do{%
      \expandafter\pxkc@cjkcategory@b\expandafter{\pxkc@blk}}%
  \fi}
\def\pxkc@cjkcategory@b#1{%
  \pxkc@get@cp{#1}\ifpxkc@ok % charcode
    \pxkc@kcatcode{\pxkc@cp}{\pxkc@arg}%
  \else
    \ifcase\pxkc@cp % no-op
    \or % name
      \edef\pxkc@blk{#1}%
      \pxkc@set@cjkcategory
    \else \pxkc@err@ivfmt{#1}% error
    \fi
  \fi}

%% \pxkc@set@cjkcategory
% Sees \pxkc@blk (block name) and \pxkc@arg (kcatcode).
\def\pxkc@set@cjkcategory{%
  \pxkc@letcs\pxkc@tmpa{pxkc@G@@\pxkc@blk}%
  \ifx\pxkc@tmpa\relax
    \pxkc@letcs\pxkc@cp{pxkc@B@@\pxkc@blk}%
    \ifx\pxkc@cp\relax
      \pxkc@err@ivbsy{\pxkc@blk}%
    \else\ifnum\pxkc@cp<\z@ % bad
      \ifpxkc@warn@bad \pxkc@warn@dfblk{\pxkc@blk}\fi
    \else\ifnum\pxkc@cp=\z@
      \ifnum\pxkc@arg=15 \else
        \pxkc@err@naltn
      \fi
    \else
      \pxkc@kcatcode{\pxkc@cp}{\pxkc@arg}%
    \fi\fi\fi
  \else
    \pxkc@expanded{\noexpand\pxkc@cjkcategory@a{\pxkc@tmpa}}%
  \fi}

%% \pxkc@cjkcategory{<block>}{<kcatcode>}
%% \pxkc@cjkcategory@mccv{<minmccv>}{<maxmccv>}{<block>}{<kcatcode>}
% Other entry points for internal use.
\def\pxkc@cjkcategory#1#2{%
  \def\pxkc@blk{#1}\edef\pxkc@arg{#2}%
  \pxkc@set@cjkcategory}
\def\pxkc@cjkcategory@mccv#1#2#3#4{%
  \ifnum#1>\pxkc@mccv\else \ifnum#2<\pxkc@mccv\else
    \pxkc@cjkcategory{#3}{#4}%
  \fi\fi}

%% \pxkc@get@cp{<arg>}
% ok / 0=nop / 1=name / 2=err
\def\pxkc@get@cp#1{%
  \pxkc@okfalse \chardef\pxkc@cp\z@
  \pxkc@get@cp@a#1\pxkc@mk\pxkc@end}
\def\pxkc@get@cp@a#1{%
  \pxkc@cond\ifx#1\pxkc@mk\fi{%
    \pxkc@get@cp@fin\pxkc@mk
  }{\pxkc@cond\ifnum\pxkc@get@cp@b#1>\z@\fi{%
    \pxkc@get@cp@c#1%
  }{\pxkc@cond\ifnum2<"1#1 \fi{%
    \pxkc@get@cp@c"#1%
  }{%else
    \chardef\pxkc@cp`#1 %
    \pxkc@cond\ifnum\pxkc@cp>\pxkc@ascii@max\fi{%
      \pxkc@oktrue \pxkc@get@cp@fin
    }{%
      \pxkc@okfalse \chardef\pxkc@cp\@ne \pxkc@get@cp@e
    }%
  }}}}
\def\pxkc@get@cp@b#1{0\if#1"1\fi\if#1'1\fi\if#1+1\fi}
\def\pxkc@get@cp@c{%
  \afterassignment\pxkc@get@cp@d\count@=}
\def\pxkc@get@cp@d{%
  \chardef\pxkc@cp\count@
  \ifnum\pxkc@cp=\count@ \pxkc@oktrue
  \else \chardef\pxkc@cp\z@
  \fi
  \pxkc@get@cp@fin}
\def\pxkc@get@cp@e#1\pxkc@mk{%
  \pxkc@get@cp@fin\pxkc@mk}
\def\pxkc@get@cp@fin#1\pxkc@mk\pxkc@end{%
  \ifx\pxkc@mt#1\pxkc@mt\else
    \pxkc@okfalse \chardef\pxkc@cp\tw@
  \fi}

%%<*> \cjkcategorymode{<mode>}
% Invokes the cjk-category mode <mode>.
\newcommand*\cjkcategorymode[1]{%
  \pxkc@letcs\pxkc@next{pxkc@M@@#1}%
  \ifx\pxkc@next\relax
    \pxkc@err@ivmsy{#1}%
  \else
    \pxkc@warn@badfalse
    \pxkc@next
    \pxkc@warn@badtrue
    \kcatcode0=15 % 'latn' is fixed
  \fi}

%--------------------------------------- CJK category modes

%% variables
\let\pxkc@cat\relax
\let\pxkc@base@cat\relax

%% \[pxkc@K@@<block>] : the kcatcode of CJK core blocks
\def\do#1#2{\expandafter\chardef\csname pxkc@K@@#1\endcsname=#2 }
%C======================================
\do{hang1} {19} % Hangul Jamo
\do{sym15} {18} % Enclosed Alphanumerics
\do{cjk01} {16} % CJK Radicals Supplement
\do{cjk02} {16} % Kangxi Radicals
\do{cjk03} {16} % Ideographic Description Characters
\do{cjk04} {18} % CJK Symbols and Punctuation
\do{hira}  {17} % Hiragana
\do{hirax} {17} % Hiragana/Other
\do{hiram} {20} % Hiragana/Modifier
\do{kana}  {17} % Katakana
\do{bopo}  {16} % Bopomofo
\do{hang2} {19} % Hangul Compatibility Jamo
\do{cjk05} {16} % Kanbun
\do{bopo1} {16} % Bopomofo Extended
\do{cjk06} {16} % CJK Strokes
\do{kana1} {17} % Katakana Phonetic Extensions
\do{cjk07} {18} % Enclosed CJK Letters and Months
\do{cjk08} {18} % CJK Compatibility
\do{haniA} {16} % CJK Unified Ideographs Extension A
\do{hani}  {16} % CJK Unified Ideographs
\do{yiii}  {18} % Yi Syllables
\do{yiii1} {18} % Yi Radicals
\do{hangA} {19} % Hangul Jamo Extended-A
\do{hang}  {19} % Hangul Syllables
\do{hangB} {19} % Hangul Jamo Extended-B
\do{hani1} {16} % CJK Compatibility Ideographs
\do{cjk09} {18} % Vertical Forms
\do{cjk10} {18} % CJK Compatibility Forms
\do{cjk11} {18} % Small Form Variants
\do{cjk12} {18} % Halfwidth and Fullwidth Forms
\do{cjk1a} {18} % Halfwidth and Fullwidth Forms/Other
\do{cjk1b} {17} % Halfwidth and Fullwidth Forms/Latin
\do{cjk1c} {17} % Halfwidth and Fullwidth Forms/Kana
\do{cjk14} {18} % Ideographic Symbols and Punctuation
\do{tang}  {18} % Tangut
\do{tang1} {18} % Tangut Components
\do{kits}  {18} % Khitan Small Script
\do{tang2} {18} % Tangut Supplement
\do{kanaB} {17} % Kana Extended-B
\do{kana2} {17} % Kana Supplement
\do{kanaA} {17} % Kana Extended-A
\do{kana3} {17} % Small Kana Extension
\do{nshu}  {18} % Nushu
\do{sym45} {18} % Enclosed Alphanumeric Supplement
\do{cjk13} {18} % Enclosed Ideographic Supplement
\do{haniB} {16} % CJK Unified Ideographs Extension B
\do{haniC} {16} % CJK Unified Ideographs Extension C
\do{haniD} {16} % CJK Unified Ideographs Extension D
\do{haniE} {16} % CJK Unified Ideographs Extension E
\do{haniF} {16} % CJK Unified Ideographs Extension F
\do{haniI} {16} % CJK Unified Ideographs Extension I
\do{hani2} {16} % CJK Compatibility Ideographs Supplement
\do{haniG} {16} % CJK Unified Ideographs Extension G
\do{haniH} {16} % CJK Unified Ideographs Extension H
\do{haniJ} {16} % CJK Unified Ideographs Extension J
%=======================================
\ifnum\pxkc@mccv<2
  \do{cjk1b} {18}
  \do{cjk1c} {18}
\fi
\ifnum\pxkc@mccv<5 \else
  \do{cjk03} {18}
  \do{spc05} {20}
  \do{spc08} {20}
\fi

%% \pxkc@reset@all{<kcatcode>}
\def\pxkc@reset@all#1{%
  \chardef\pxkc@base@cat=#1\relax
  \pxkc@foreach@blocks\do{%
    \pxkc@letcs\pxkc@cat{pxkc@K@@\pxkc@blk}%
    \ifx\pxkc@cat\relax \let\pxkc@cat\pxkc@base@cat \fi
    \pxkc@kcatcode{\@nameuse{pxkc@B@@\pxkc@blk}}{\pxkc@cat}}}

%% \[pxkc@M@@<mode>] : the implementation of a mode
%P======================================
%% 'forcecjk'
\def\pxkc@M@@forcecjk{%
  \pxkc@reset@all{18}%
  \pxkc@cjkcategory@mccv{5}{5}{sym07m}{20}%
  \pxkc@cjkcategory@mccv{5}{5}{sym45m}{20}%
  \pxkc@cjkcategory@mccv{5}{5}{sym47m}{20}%
}
%% 'prefercjk'
\def\pxkc@M@@prefercjk{%
  \pxkc@M@@prefercjkvar
  \pxkc@cjkcategory{grek}{18}%
  \pxkc@cjkcategory{cyrl}{18}%
  \pxkc@cjkcategory{grek1}{18}%
}
%% 'prefercjkvar'
\def\pxkc@M@@prefercjkvar{%
  \pxkc@M@@japanesevar
  \pxkc@cjkcategory@mccv{3}{5}{latny}{18}%
  \pxkc@cjkcategory{latnA}{18}%
  \pxkc@cjkcategory{latnB}{18}%
  \pxkc@cjkcategory{latn2}{18}%
  \pxkc@cjkcategory{sym01}{18}%
  \pxkc@cjkcategory{sym02}{18}%
  \pxkc@cjkcategory{latn3}{18}%
  \pxkc@cjkcategory{sym05}{18}%
  \pxkc@cjkcategory{sym06}{18}%
  \pxkc@cjkcategory{latn6}{18}%
}
%% 'japanese'
\def\pxkc@M@@japanese{%
  \pxkc@M@@japanesevar
  \pxkc@cjkcategory{grek}{18}%
  \pxkc@cjkcategory{cyrl}{18}%
}
%% 'japanesevar'
\def\pxkc@M@@japanesevar{%
  \pxkc@M@@prefernoncjk
  \pxkc@cjkcategory@mccv{1}{2}{latn1}{18}%
  \pxkc@cjkcategory@mccv{3}{5}{latnx}{18}%
  \pxkc@cjkcategory{sym04}{18}%
  \pxkc@cjkcategory@mccv{1}{4}{sym07}{18}%
  \pxkc@cjkcategory@mccv{5}{5}{sym07x}{18}%
  \pxkc@cjkcategory@mccv{5}{5}{sym07m}{20}%
  \pxkc@cjkcategory{sym08}{18}%
  \pxkc@cjkcategory{sym09}{18}%
  \pxkc@cjkcategory{sym10}{18}%
  \pxkc@cjkcategory{sym11}{18}%
  \pxkc@cjkcategory{sym12}{18}%
  \pxkc@cjkcategory{sym13}{18}%
  \pxkc@cjkcategory{sym16}{18}%
  \pxkc@cjkcategory{sym17}{18}%
  \pxkc@cjkcategory{sym18}{18}%
  \pxkc@cjkcategory{sym19}{18}%
  \pxkc@cjkcategory{sym20}{18}%
  \pxkc@cjkcategory{sym23}{18}%
  \pxkc@cjkcategory{sym24}{18}%
  \pxkc@cjkcategory{sym26}{18}%
  \pxkc@cjkcategory{spc04}{18}%
}
%% 'prefernoncjk'
\def\pxkc@M@@prefernoncjk{%
  \pxkc@reset@all{15}%
  \pxkc@cjkcategory@mccv{5}{5}{sym45x}{18}%
  \pxkc@cjkcategory@mccv{5}{5}{sym45m}{20}%
}
%=======================================
%% 'default'
\def\pxkc@M@@default{%
  \pxkc@M@@forcecjk
  \pxkc@cjkcategory@mccv{3}{5}{latny}{15}%
  \pxkc@cjkcategory@mccv{3}{5}{latnA}{15}%
  \pxkc@cjkcategory@mccv{4}{5}{latnB}{15}%
  \pxkc@cjkcategory@mccv{5}{5}{latn3}{15}%
}

%--------------------------------------- safe text expansion

%% variables
\let\pxkc@expd\relax

%% \pxkc@expand@text\CS{<text>}
\def\pxkc@expand@text#1#2{%
  \begingroup
    \let\UTFviii@invalid@err \noexpand
    \let\UTFviii@two@octets  \pxkc@lead@byte@NB
    \let\UTFviii@three@octets\pxkc@lead@byte@NC
    \let\UTFviii@four@octets \pxkc@lead@byte@ND
    \edef\pxkc@tmpa{#2}%
    \let\UTFviii@invalid@err \pxkc@expand@text@err
    \let\UTFviii@two@octets  \pxkc@lead@byte@PB
    \let\UTFviii@three@octets\pxkc@lead@byte@PC
    \let\UTFviii@four@octets \pxkc@lead@byte@PD
    \let\pxkc@expd\@empty
    \expandafter\pxkc@expand@text@a\pxkc@tmpa\pxkc@mk\pxkc@end
    \global\let\pxkc@g@tmpa\pxkc@expd
  \endgroup
  \let#1\pxkc@g@tmpa}
\def\pxkc@expand@text@a{%
  \futurelet\pxkc@tok\pxkc@expand@text@b}
\def\pxkc@expand@text@b{%
  \ifcat\noexpand\pxkc@tok0% other
    \let\pxkc@next\pxkc@expand@text@d
  \else\ifcat\noexpand\pxkc@tok\@sptoken
    \let\pxkc@next\pxkc@expand@text@f
  \else\ifx\pxkc@tok\pxkc@mk % end
    \let\pxkc@next\pxkc@expand@text@fin
  \else\ifx\pxkc@tok\bgroup
    \let\pxkc@next\pxkc@expand@text@err
  \else\expandafter\ifx\noexpand\pxkc@tok\pxkc@tok % unexpandable
    \let\pxkc@next\pxkc@expand@text@c
  \else \let\pxkc@next\pxkc@expand@text@e
  \fi\fi\fi\fi\fi
  \pxkc@next}
\def\pxkc@expand@text@c#1{%
  \edef\pxkc@expd{\pxkc@expd#1}%
  \pxkc@expand@text@a}
\def\pxkc@expand@text@d#1{%
  \ifnum`#1>\pxkc@ascii@max % high byte
    \let\pxkc@next\pxkc@expand@text@ret
    \lccode"EF=`#1\relax\lowercase{\expandafter^^ef}% reactivate
  \else \expandafter\pxkc@expand@text@c\expandafter#1%
  \fi}
\def\pxkc@expand@text@e#1{%
  \ifcat\noexpand#1\noexpand^^ef% active-char
    \let\pxkc@next\pxkc@expand@text@ret
    \expandafter#1%
  \else \expandafter\pxkc@expand@text@err
  \fi}
\expandafter\def\expandafter\pxkc@expand@text@f\space{%
  \pxkc@expand@text@c\space}
\def\pxkc@expand@text@fin#1#2\pxkc@end{%
  \ifx\pxkc@mt#2\pxkc@mt \else \let\pxkc@expd\relax \fi}
\def\pxkc@expand@text@err{%
  \pxkc@expand@text@fin\pxkc@mk}
\def\pxkc@expand@text@ret{% UTF-8 bytes are read
  \pxkc@get@jchar\count@
  \pxkc@expand@text@c\pxkc@jchar}

%% processors
\def\pxkc@lead@byte@NB#1{\noexpand#1\noexpand}
\def\pxkc@lead@byte@NC#1#2{\noexpand#1\noexpand#2\noexpand}
\def\pxkc@lead@byte@ND#1#2#3{\noexpand#1\noexpand#2\noexpand#3\noexpand}
\def\pxkc@lead@byte@PB#1#2{%
  \count@`#1 \advance\count@-"C0
  \pxkc@trail@byte{#2}\pxkc@next}
\def\pxkc@lead@byte@PC#1#2#3{%
  \count@`#1 \advance\count@-"E0
  \pxkc@trail@byte{#2}\pxkc@trail@byte{#3}\pxkc@next}
\def\pxkc@lead@byte@PD#1#2#3#4{%
  \count@`#1 \advance\count@-"F0
  \pxkc@trail@byte{#2}\pxkc@trail@byte{#3}\pxkc@trail@byte{#4}\pxkc@next}
\def\pxkc@trail@byte#1{%
  \multiply\count@"40 \advance\count@`#1 \advance\count@-"80 }

%--------------------------------------- miscellaneous accessories

%%<*> \showcjkcategory
\newcommand*\showcjkcategory{%
  \pxkc@foreach@blocks\do{%
    \@ifundefined{pxkc@B@@\pxkc@blk}{}{%else
      \typeout{\pxkc@blk\space= \the\kcatcode\@nameuse{pxkc@B@@\pxkc@blk}}%
    }}}

%--------------------------------------- get current cjktoken state

%% variables
\let\pxkc@ctmode\relax

\ifx\currentcjktoken\@undefined         %<*!e-pTeX>

%% \pxkc@make@probe
\def\pxkc@make@probe{%
  \edef\pxkc@tmpa{\expandafter\string\csname
   pxkc@@\string ^^e3\string ^^81\string ^^82%
   \string ^^e3\string ^^82\string ^^a2\endcsname}}
\begingroup \kcatcode"3040=15 \kcatcode"30A0=18
  \disablecjktoken\pxkc@make@probe \global\let\pxkc@prb@d\pxkc@tmpa
  \enablecjktoken\pxkc@make@probe  \global\let\pxkc@prb@e\pxkc@tmpa
  \forcecjktoken\pxkc@make@probe   \global\let\pxkc@prb@f\pxkc@tmpa
\endgroup

%% \pxkc@get@cjktoken@mode\CS
\def\pxkc@get@cjktoken@mode#1{%
  \begingroup
    \kcatcode"3040=15 \kcatcode"30A0=18 \pxkc@make@probe
    \ifx\pxkc@tmpa\pxkc@prb@d \global\chardef\pxkc@gtmpa\z@
    \else\ifx\pxkc@tmpa\pxkc@prb@e \global\chardef\pxkc@gtmpa\@ne
    \else\ifx\pxkc@tmpa\pxkc@prb@f \global\chardef\pxkc@gtmpa\tw@
    \else \global\chardef\pxkc@gtmpa\@cclv
    \fi\fi\fi
  \endgroup
  \let#1\pxkc@gtmpa}

% The method does not work on new upTeX, but running LaTeX
% on new upTeX without e-TeX is highly improbable....
\ifx\pxkc@prb@d\pxkc@prb@e
  \def\pxkc@get@cjktoken@mode#1{%
    \chardef#1\@cclv} % always fail
\fi

\else                                   %<*e-pTeX>

\def\pxkc@get@cjktoken@mode#1{%
  \chardef#1\ifcase\currentcjktoken
    \@ne \or \z@ \or \tw@ \or \@cclv
  \fi}

\fi                                     %</e-pTeX>

%%<*> \getcjktokenmode
% Returns the value showing current mode to \thecjktokenmode
%  0: \disablecjktoken
%  1: \enablecjktoken
%  2: \forcecjktoken
%  255: failure
\newcommand*\getcjktokenmode{%
  \pxkc@get@cjktoken@mode\thecjktokenmode}

%%<*> \setcjktokenmode{<value>}
\newcommand*\setcjktokenmode[1]{%
  \ifcase#1\disablecjktoken
  \or \enablecjktoken
  \or \forcecjktoken
  \fi}

%%<*> \withcjktokendisabled, etc.
\newcommand*\withcjktokendisabled{%
  \pxkc@with@cjktoken@mode\disablecjktoken\@iden}
\newcommand*\withcjktokenenabled{%
  \pxkc@with@cjktoken@mode\enablecjktoken\@iden}
\newcommand*\withcjktokenforced{%
  \pxkc@with@cjktoken@mode\forcecjktoken\@iden}
\newcommand*\withcjktokendisabledex{%
  \pxkc@with@cjktoken@mode\disablecjktoken}
\newcommand*\withcjktokenenabledex{%
  \pxkc@with@cjktoken@mode\enablecjktoken}
\newcommand*\withcjktokenforcedex{%
  \pxkc@with@cjktoken@mode\forcecjktoken}
\def\pxkc@with@cjktoken@mode#1{%
  \pxkc@get@cjktoken@mode\pxkc@ctmode
  \ifnum\pxkc@ctmode=\@cclv \pxkc@with@cjktoken@mode@b \fi
  #1%
  \pxkc@with@cjktoken@mode@a}
\def\pxkc@with@cjktoken@mode@a#1#2{%
  #1{#2}%
  \setcjktokenmode\pxkc@ctmode}
\def\pxkc@with@cjktoken@mode@b{%
  \global\let\pxkc@with@cjktoken@mode@b\relax
  \PackageWarning\pxkc@pkgname
   {Failed in getting current cjktoken mode,\MessageBreak
    reported}}

%--------------------------------------- initial setup

\ifx\pxkc@ccmode@opt\relax\else
  \cjkcategorymode{\pxkc@ccmode@opt}
\fi

%--------------------------------------- all done
\endinput
%% EOF
