%%% kswdash.sty
%%%
\ProvidesExplPackage{kswdash}
	{2025/05/18}
	{version 0.9.1}
	{wavedash and cdot(s) in Korean context}

%%% check engine and Load fontspec
\msg_new:nnn { kswdash } { engine } { xelatex~or~lualatex~recommended }
\RequirePackage{iftex}
\ifx\ifxetex\ifluatex
	\msg_warning:nn { kswdash } { engine }
	\input{kswdash-pdftex.code.tex}
	\expandafter \endinput
\else
	\RequirePackage{fontspec}
\fi

\RequirePackage{l3keys2e}

\keys_define:nn { kswdash }
{
	wdash 	.choice:,
		wdash/noto 		.code:n = { \tl_set:Nn \opt_wdashopt_tl { noto } },
		wdash/hcr 		.code:n = { \tl_set:Nn \opt_wdashopt_tl { hcr } },
		wdash/baekmuk	.code:n = { \tl_set:Nn \opt_wdashopt_tl { baekmuk } },
		wdash/un 		.code:n = { \tl_set:Nn \opt_wdashopt_tl { un } },
		wdash/currentfont	.code:n = { \tl_set:Nn \opt_wdashopt_tl { none } },
	ddashtowave	.bool_set:N = \opt_ddashtowave_bool,
}

\keys_set:nn { kswdash }
{
	wdash = un,
	ddashtowave = false,
}

\ProcessKeysOptions { kswdash }

%%% \fontspec or \hangulfontspec
\cs_if_exist:NTF \hangulfontspec
{
	\cs_set_eq:NN \select_kswdash_font:n \hangulfontspec
}
{
	\cs_set_eq:NN \select_kswdash_font:n \fontspec
}

\str_case_e:nnF { \opt_wdashopt_tl }
{
	{ noto }	{ \tl_set:Nn \KSWdashFont  { \select_kswdash_font:n { Noto~Serif~CJK~KR } } }
	{ hcr  }	{ \tl_set:Nn \KSWdashFont  { \select_kswdash_font:n { HANBatang-LVT.ttf } } }
	{ baekmuk }	{ \tl_set:Nn \KSWdashFont  { \select_kswdash_font:n { batang.ttf } } }
	{ un }		{ \tl_set:Nn \KSWdashFont  { \select_kswdash_font:n { UnBatang.ttf } } }
	{ currentfont }	{ \tl_set:Nn \KSWdashFont  { \kswdashcurrentfont } }
}
{
	\tl_set:Nn \KSWdashFont  { \select_kswdash_font:n { batang.ttf } }
}

\tl_set:Nn \KSWdashCdotFont     { \select_kswdash_font:n { HANBatang-LVT.ttf } }
\tl_set:Nn \KSWdashEllipsisFont { \select_kswdash_font:n { HANBatang-LVT.ttf } }
\cs_set_eq:NN \kswdashcurrentfont \scan_stop:

\bool_new:N \cdotbox_natural_bool

\cs_set_eq:NN  \original_tilde_macro \~
\cs_set_eq:NN  \tildeorgcmd \original_tilde_macro
\cs_undefine:N \~

\str_if_eq:eeTF { \opt_wdashopt_tl } { noto }
{
	\tl_set:Nn \KSWdashCode { FF5E }
}
{
	\tl_set:Nn \KSWdashCode { 223C }
}

\tl_set:Nn \kor_tilde_wavedash { 
	\mode_if_vertical:T { \mode_leave_vertical: }
	\nobreak
    \mbox { \KSWdashFont \exp_args:Nx \symbol{ "\KSWdashCode } } 
    \nobreak
}

\cs_new_protected:Nn \new_tilde_macro:
{
    \new_tilde_trial:n
}

\cs_new:Npn \new_tilde_trial:n #1
{
    \tl_set:Nn \l_tmpa_tl { #1 }
    \tl_if_empty:NTF \l_tmpa_tl
    {
        \kor_tilde_wavedash
    }
    {
        \regex_match:nVTF { [가-힣\d] } \l_tmpa_tl
        {
            \kor_tilde_wavedash
            #1
        }
        {
            \original_tilde_macro {#1}
        }
    }
}

\cs_set_eq:NN \~ \new_tilde_macro:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% cdot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\cs_set_eq:NN \asthyphenorgcmd \*
\cs_undefine:N \*

\dim_new:N \kor_cdot_box_width_dim 
\dim_set:Nn \kor_cdot_box_width_dim { .67em }

\tl_set:Nn \kor_cdot_macro {
    \KSWdashCdotFont \symbol{"00B7}
}

\cs_new_protected:Nn \new_cdot_macro:
{
	\mode_if_vertical:T { \mode_leave_vertical: }
	\nobreak
	\bool_if:NTF \cdotbox_natural_bool
	{
		\mbox { \kor_cdot_macro }
	}
	{
		\hbox_to_wd:nn { \kor_cdot_box_width_dim } {\hss \kor_cdot_macro \hss}
	}
}

\cs_new:Npn \new_cdot_macro_dot:n #1
{
	\tl_if_empty:nTF { #1 }
	{
		\new_cdot_macro: 
		\relax
	}
	{
		\regex_match:nnTF { [가-힣\d] } { #1 }
		{
			\new_cdot_macro:
			#1
		}
		{
			\org_backslash_dot:
			{#1}
		}
	}
}

\cs_set_eq:NN \original_cdot_macro:  \cdot
\cs_set_eq:NN \original_cdots_macro: \cdots

\NewDocumentCommand \kswdash_cdot {}
{
	\mode_if_math:TF {
		\original_cdot_macro:
	}
	{
		\new_cdot_macro:
	}
}

% \NewDocumentCommand \* {}
% {
% 	\peek_charcode_remove:NTF *
% 	{
% 		\cdot_or_Cdot:
% 	}
% 	{
% 		\kswdash_cdot
% 	}
% }

\cs_set_eq:NN \org_backslash_dot: \.
\cs_set_eq:NN \cdotbydotorgcmd \org_backslash_dot:
\cs_undefine:N \.

% \bool_if:NTF \opt_cdotdot_bool
% {
\NewDocumentCommand \. {}
{
	\peek_charcode_remove:NTF .
	{
		\cdot_or_Cdot_dot:
	}
	{
		\new_cdot_macro_dot:n
	}
}
% }
% {
% 	\NewDocumentCommand \. {}
% 	{
% 		\peek_charcode_remove:NTF .
% 		{
% 			\cdot_or_Cdot_dot:
% 		}
% 		{
% 			\org_backslash_dot:
% 		}
% 	}
% }

% \cs_new:Nn \cdot_or_Cdot:
% {
% 	\peek_charcode_remove:NTF *
% 	{
% 		\kswdash_Cdots
% 	}
% 	{
% 		\kswdash_cdots
% 	}
% }

\cs_new:Nn \cdot_or_Cdot_dot:
{
	\peek_charcode_remove:NTF .
	{
		\kswdash_Cdots
	}
	{
		\kswdash_cdots
	}
}

\NewDocumentCommand \setcdotboxwidth { m }
{
	\str_if_eq:eeTF { #1 } { natural }
	{
		\bool_gset_true:N \cdotbox_natural_bool
	}
	{
		\bool_gset_false:N \cdotbox_natural_bool
		\dim_set:Nn \kor_cdot_box_width_dim { #1 }
	}
}

%%% hellipsis

%\cs_undefine:N \cdots

\NewDocumentCommand \kswdash_cdots {}
{
	\mode_if_vertical:T { \mode_leave_vertical: }
	\mode_if_math:TF
	{ \original_cdots_macro: }
	{ { \KSWdashEllipsisFont \symbol{"2026} } }
}

\NewDocumentCommand \kswdash_Cdots {}
{
	\mode_if_vertical:T { \mode_leave_vertical: }
	\mbox { \KSWdashEllipsisFont \symbol{"2026}\symbol{"2026} }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% expldash %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%

\bool_if:NTF \opt_ddashtowave_bool
{
	\cs_set_eq:NN \_kswdash_dbldash_cmd: \kor_tilde_wavedash
}
{
	\cs_set_eq:NN \_kswdash_dbldash_cmd: \textemdash
}

%\@ifpackageloaded{oblivoir}{}{\endinput}

\cs_set_eq:NN \orig_hyphen_cmd: \-
\cs_undefine:N \-

%%%\protected\def\edsh#1~{
%%%	\_na_cmd_dash_fn:n { #1 }
%%%}
%%%
%%%\let\-\edsh
%%%
%%%\cs_new:Npn \_na_cmd_dash_fn:n #1
%%%{
%%%	\regex_match:nnTF { ^[\-] } { #1 }
%%%	{
%%%		\_ma_cmd_dash_fn:n { #1 }
%%%	}
%%%	{
%%%		\orig_hyphen_cmd:
%%%	}
%%%}
%%%
%%%\cs_new:Npn \_ma_cmd_dash_fn:n #1
%%%{
%%%	\tl_set:Nn \_madash_tl { #1 }
%%%	\regex_replace_once:nnN { ^\-\- } { \c{expldash} } \_madash_tl
%%%	\regex_replace_once:nnN { ^\- } { \c{_kswdash_dbldash_cmd:} } \_madash_tl
%%%	\_madash_tl \space
%%%}

\NewDocumentCommand \edsh_function {}
{
	\peek_charcode_remove:NTF -
	{
		\_next_dash_trial:
	}
	{
		\orig_hyphen_cmd:
	}
}

\cs_set_eq:NN \- \edsh_function
\cs_set_eq:NN \edsh \edsh_function

\cs_new:Nn \_next_dash_trial:
{
	\peek_charcode_remove:NTF -
	{
		\protect\expldash
	}
	{
		\_kswdash_dbldash_cmd:
	}
}

\cs_if_exist:NF \expldash
{
	\def\expldash{\;\textemdash\;}
}

%%% alias of \let\-\edsh
\NewDocumentCommand \dashmeaningedsh {}
{
	\let\-\edsh_function
}

%%% patch \footnote
\AtEndPreamble{
	\pretocmd{\@makefntext}{\let\-\edsh}{}{}
}

%%% end of file
