%% File `memhangul-patch.sty`
%%
%% Copyright (c) 2005-2013 Kangsoo Kim <karnes at ktug org>
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% 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.3c or later is part of all distributions of LaTeX
%% version 2006/05/20 or later.
%%
%% 2005/10/15
%% some day, this patch has to be weaved into memhangul-use, 1.4, I hope.
%% 2005/12/28
%% appendix-related minor fix
%% 2006/04/27
%% remove redundant prerenderunicodes according to dhucs 3.0.0
%%
\typeout{memhangul-ucs patch for memoir 1.618, 2006/04/27}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% PART
\long\def\@part[#1]#2{%
%  \ifx#1\@empty\PrerenderUnicode{#2}\else\PrerenderUnicode{#1}\fi
  \M@gettitle{#1}%
  \ifnum \c@secnumdepth >-2\relax
    \refstepcounter{part}%
    \addcontentsline{toc}{part}%
%      {\protect\partnumberline{\thepart}#1}%
      {\protect\partnumberline{\hparttitlehead}#1}%
    \mempartinfo{\thepart}{#1}{#2}%
  \else
    \addcontentsline{toc}{part}{#1}%
    \mempartinfo{}{#1}{#2}%
  \fi
  \partmark{#1}%
  {\centering
   \interlinepenalty \@M
   \normalfont
   \ifnum \c@secnumdepth >-2\relax
%     \printpartname \partnamenum \printpartnum
%     \midpartskip
     \prepartnum \partnamenum \printpartnum \partnamenum \postpartnum
     \midpartskip
   \fi
   \printparttitle{#2}\par}%
  \@endpart}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CHAPTER
%% memhangul : opt args : different from memoir 1.618
%% \chapter[toc][hdr]{title}
%% \chapter[toc]{hdr/title}
%% \chapter{toc/hdr/title}
\def\@chapter[#1]#2{%
%  \ifx\ch@pt@c\@empty % no optional args
%    \def\f@rtoc{#2}%
%    \def\f@rhdr{#2}%
%    \PrerenderUnicode{#2}%
%  \else                  % at least one opt arg
%    \let\f@rtoc\ch@pt@c
%    \ifx\@empty#1\@empty
%      \let\f@rhdr\ch@pt@c
%      \PrerenderUnicode{#1#2}%
%    \else
%      \def\f@rhdr{#1}%
%      \PrerenderUnicode{#1}%
%    \fi
%  \fi
  \ifx\ch@pt@c\m@m@empty % no optional args
    \def\f@rtoc{#1}%
    \def\f@rhdr{#1}%
%    \PrerenderUnicode{#1}%
  \else                  % at least one opt arg
    \def\f@rtoc{\ch@pt@c}%
    \nametest{#1}{#2}%
    \ifsamename          % one opt arg
      \def\f@rhdr{\ch@pt@c}%
%      \PrerenderUnicode{#1#2}%
    \else                % two opt args
      \def\f@rhdr{#1}%
%      \PrerenderUnicode{#1}%
    \fi
  \fi 
  \m@m@Andfalse
  \ifnum \c@secnumdepth >\m@ne
    \if@mainmatter
      \m@m@Andtrue
    \fi
  \fi
  \ifm@m@And
   \ifanappendix
    \refstepcounter{APPchapter}%
   \else
    \refstepcounter{chapter}%
   \fi
  \fi
  \chaptermark{\f@rhdr}
  \ifartopt
    \@makechapterhead{#2}%
    \@afterheading
  \else
    \insertchapterspace
    \if@twocolumn
      \@topnewpage[\@makechapterhead{#2}]%
    \else
      \@makechapterhead{#2}%
    \fi
    \@afterheading
  \fi
  \ifm@m@And
    \ifanappendix
      \addcontentsline{toc}{appendix}{%
%       \protect\chapternumberline{\thechapter}\f@rtoc}%
       \protect\chapternumberline{\hchaptertitlehead}\f@rtoc}%
%       \memappchapinfo{\thechapter}{\f@rtoc}{\f@rhdr}{#2}%
       \memappchapinfo{\thechapter}{\f@rtoc}{\f@rhdr}{#2}%
    \else
      \addcontentsline{toc}{chapter}{%
%        \protect\chapternumberline{\thechapter}\f@rtoc}%
        \protect\chapternumberline{\hchaptertitlehead}\f@rtoc}%
%      \memchapinfo{\thechapter}{\f@rtoc}{\f@rhdr}{#2}%
       \memchapinfo{\hchaptertitlehead}{\f@rtoc}{\f@rhdr}{#2}%
    \fi
  \else
    \addcontentsline{toc}{chapter}{\f@rtoc}%
    \ifanappendix
      \memappchapinfo{}{\f@rtoc}{\f@rhdr}{#2}%
    \else
      \memchapinfo{\hchaptertitlehead}{\f@rtoc}{\f@rhdr}{#2}%
    \fi
  \fi
  \ifheadnameref\M@gettitle{\f@rhdr}\else\M@gettitle{\f@rtoc}\fi
%%% for chapter
  \ifanappendix
     \protected@edef\@currentlabel{\thechapter}%
  \fi
}

\def\@makechapterhead#1{%
  \chapterheadstart%  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
   \ifm@m@And
   	 \memucsinterwordchapterskiphook
%     \printchaptername \chapternamenum \printchapternum
%     \afterchapternum % \par\nobreak \vskip 20\p@
      \prechapternum \chapternamenum \printchapternum \chapternamenum \postchapternum
      \afterchapternum % \par\nobreak \vskip 20\p@
   \else
     \printchapternonum
   \fi
   \interlinepenalty\@M
   \printchaptertitle{#1} % \Huge \bfseries #1
   \afterchaptertitle % \par\nobreak \vskip 40\p@
  }}


\renewcommand{\@m@mschapter}[2][\@empty]{%
  \@schapter{#2}%
  \ifx \@empty#1
    \def\f@rhdr{#2}%
%    \PrerenderUnicode{#2}%
  \else   % opt arg
    \def\f@rhdr{#1}%
%    \PrerenderUnicode{#1}%
    \setcounter{secnumdepth}{-10}%
    \chaptermark{#1}%
    \setcounter{secnumdepth}{\value{maxsecnumdepth}}%
  \fi
  \ifanappendix
    \memappchapstarinfo{\f@rhdr}{#2}%
  \else
    \memchapstarinfo{\f@rhdr}{#2}%
  \fi}

\def\@makeschapterhead#1{%
  \chapterheadstart
%  {\parindent \z@ \raggedright \normalfont
  {\parindent \z@ \raggedright \normalfont \memucsinterwordchapterskiphook
   \printchapternonum
   \interlinepenalty\@M
   \printchaptertitle{#1}
   \afterchaptertitle
  }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput
