% Language definition file (part of polyglossia v2.10 -- 2026/04/18)
%
\ProvidesFile{gloss-latin.ldf}[polyglossia: module for Latin]

\ExplSyntaxOn

\PolyglossiaSetup {latin}
  {
    bcp47 = la,
    bcp47-language = la,
    bcp47-script = Latn,
    hyphenmins = {2,2},
    frenchspacing = true,
    langtag = LAT
  }

% BCP-47 compliant aliases
\setlanguagealias*{latin}{la}
\setlanguagealias*[variant=classic]{latin}{la-x-classic}
\setlanguagealias*[variant=ecclesiastic]{latin}{la-x-ecclesia}
\setlanguagealias*[variant=medieval]{latin}{la-x-medieval}

% Babel aliases
\setlanguagealias[variant=classic]{latin}{classicallatin}
\setlanguagealias[variant=classic]{latin}{classiclatin}
\setlanguagealias[variant=ecclesiastic]{latin}{ecclesiasticallatin}
\setlanguagealias[variant=ecclesiastic]{latin}{ecclesiasticlatin}
\setlanguagealias[variant=medieval]{latin}{medievallatin}


%%%%% Variables and commands concerning spelling

\bool_new:N \l_polyglossia_latin_use_j_bool
\bool_new:N \l_polyglossia_latin_use_v_bool
\bool_new:N \l_polyglossia_latin_use_ligatures_bool
\bool_new:N \l_polyglossia_latin_capitalize_month_bool
\bool_new:N \l_polyglossia_latin_babelshorthands_bool
\bool_new:N \l_polyglossia_latin_prosodic_shorthands_bool
\bool_new:N \l_polyglossia_latin_ecclesiastic_footnotes_bool

\DeclareUppercaseMapping[la-x-classic]{`u}{V}
\DeclareLowercaseMapping[la-x-classic]{`V}{u}
% The mapping of u and V for medieval Latin is part of the l3text module,
% which is part of the LaTeX format (see source3.pdf).
\DeclareUppercaseMapping[la-x-classic]{`ú}{\a'{V}}
\DeclareUppercaseMapping[la-x-classic]{`ū}{\a={V}}
\DeclareUppercaseMapping[la-x-classic]{`ŭ}{\u{V}}
\DeclareUppercaseMapping[la-x-medieval]{`ú}{\a'{V}}
\DeclareUppercaseMapping[la-x-medieval]{`ū}{\a={V}}
\DeclareUppercaseMapping[la-x-medieval]{`ŭ}{\u{V}}

%%%%% Messages and commands concerning hyphenation

\msg_new:nnn {polyglossia} {latin / missing modern patterns}
  {
    The~hyphenation~patterns~for~modern~Latin~were~not~found~\msg_line_context:.
  }

\msg_new:nnn {polyglossia} {latin / missing patterns}
  {
    The~"#1"~hyphenation~patterns~were~not~found~\msg_line_context:.~
    I~will~use~the~patterns~for~modern~Latin~instead.
  }

\cs_new:Npn \polyglossia_latin_use_modern_patterns:
  {
    \IfLanguageDefinedTF {latin}
      {
        \def \latin@language
          {
            \SetupPolyglossiaLangPatterns {latin}
            \str_case:Vn \l_polyglossia_latin_variant_str
              {
                {classic}      { \adddialect \l@classiclatin \l@latin }
                {medieval}     { \adddialect \l@medievallatin \l@latin }
                {ecclesiastic} { \adddialect \l@ecclesiasticlatin \l@latin }
              }
          }
      }
      {
        \msg_warning:nn {polyglossia} {latin / missing modern patterns}
        \str_case:Vn \l_polyglossia_latin_variant_str
          {
            {classic}      { \adddialect \l@classiclatin \l@nohyphenation }
            {medieval}     { \adddialect \l@medievallatin \l@nohyphenation }
            {modern}       { \adddialect \l@latin \l@nohyphenation }
            {ecclesiastic} { \adddialect \l@ecclesiasticlatin \l@nohyphenation }
          }
      }
  }

\cs_new:Npn \polyglossia_latin_set_patterns:n #1
% #1 may be "classiclatin" or "liturgicallatin"
  {
    \IfLanguageDefinedTF {#1}
      {
        \def \latin@language
          {
            \SetupPolyglossiaLangPatterns {#1}
            \str_case:Vn \l_polyglossia_latin_variant_str
              {
                {classic}      {
                                 \str_if_eq:nnF {#1} {classiclatin}
                                   {
                                     \adddialect \l@classiclatin  { \use:c {l@#1} }
                                   }
                               }
                {medieval}     { \adddialect \l@medievallatin { \use:c {l@#1} } }
                {modern}       { \adddialect \l@latin { \use:c {l@#1} } }
                {ecclesiastic} { \adddialect \l@ecclesiasticlatin { \use:c {l@#1} } }
              }
          }
      }
      {
        \msg_warning:nnn {polyglossia} {latin / missing patterns} {#1}
        \polyglossia_latin_use_modern_patterns:
      }
  }


%%%%% Settings for the spacing of the punctuation for ecclesiastical Latin

\bool_new:N \g_polyglossia_latin_punctuation_spacing_bool

\sys_if_engine_luatex:TF
  {
    \directlua { require('polyglossia-latin') }
  }
  {
    \newXeTeXintercharclass \g_polyglossia_latin_question_exclamation_class
    \newXeTeXintercharclass \g_polyglossia_latin_colon_semicolon_class
    \newXeTeXintercharclass \g_polyglossia_latin_opening_guillemet_class
    \newXeTeXintercharclass \g_polyglossia_latin_closing_guillemet_class
    \newXeTeXintercharclass \g_polyglossia_latin_opening_bracket_class
    \newXeTeXintercharclass \g_polyglossia_latin_closing_bracket_class

    \cs_new:Npn \polyglossia_latin_insert_punctuation_space:
      {
        \nobreak
        \skip_horizontal:n { 0.08333 \fontdimen6 \font } % 1/12 quad
      }
    \cs_new:Npn \polyglossia_latin_replace_preceding_space:
      {
        \dim_compare:nNnT {\lastskip} > {\c_zero_dim} {\unskip}
        \polyglossia_latin_insert_punctuation_space:
      }
    \cs_new:Npn \polyglossia_latin_replace_following_space:
      {
        \polyglossia_latin_insert_punctuation_space:
        \ignorespaces
      }
  }

\cs_new:Npn \polyglossia_latin_punctuation_spacing:
  {
    \sys_if_engine_luatex:TF
      {
        \directlua { polyglossia.activate_latin_punct() }
      }
      {
        \XeTeXinterchartokenstate = 1
        \XeTeXcharclass `\! \g_polyglossia_latin_question_exclamation_class
        \XeTeXcharclass `\? \g_polyglossia_latin_question_exclamation_class
        \XeTeXcharclass `\‼ \g_polyglossia_latin_question_exclamation_class
        \XeTeXcharclass `\⁇ \g_polyglossia_latin_question_exclamation_class
        \XeTeXcharclass `\⁈ \g_polyglossia_latin_question_exclamation_class
        \XeTeXcharclass `\⁉ \g_polyglossia_latin_question_exclamation_class
        \XeTeXcharclass `\‽ \g_polyglossia_latin_question_exclamation_class
        \XeTeXcharclass `\; \g_polyglossia_latin_colon_semicolon_class
        \XeTeXcharclass `\: \g_polyglossia_latin_colon_semicolon_class
        \XeTeXcharclass `\« \g_polyglossia_latin_opening_guillemet_class
        \XeTeXcharclass `\» \g_polyglossia_latin_closing_guillemet_class
        \XeTeXcharclass `\‹ \g_polyglossia_latin_opening_guillemet_class
        \XeTeXcharclass `\› \g_polyglossia_latin_closing_guillemet_class
        \XeTeXcharclass `\( \g_polyglossia_latin_opening_bracket_class
        \XeTeXcharclass `\) \g_polyglossia_latin_closing_bracket_class
        \XeTeXcharclass `\[ \g_polyglossia_latin_opening_bracket_class
        \XeTeXcharclass `\] \g_polyglossia_latin_closing_bracket_class
        \XeTeXcharclass `\{ \g_polyglossia_latin_opening_bracket_class
        \XeTeXcharclass `\} \g_polyglossia_latin_closing_bracket_class
        \XeTeXcharclass `\⟨ \g_polyglossia_latin_opening_bracket_class
        \XeTeXcharclass `\⟩ \g_polyglossia_latin_closing_bracket_class

        % question or exclamation mark followed by a closing guillemet
        \XeTeXinterchartoks \g_polyglossia_latin_question_exclamation_class \g_polyglossia_latin_closing_guillemet_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % question or exclamation mark followed by a colon or semicolon
        \XeTeXinterchartoks \g_polyglossia_latin_question_exclamation_class \g_polyglossia_latin_colon_semicolon_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % colon or semicolon followed by a closing guillemet
        \XeTeXinterchartoks \g_polyglossia_latin_colon_semicolon_class \g_polyglossia_latin_closing_guillemet_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % closing bracket followed by a question or exclamation mark
        \XeTeXinterchartoks \g_polyglossia_latin_closing_bracket_class \g_polyglossia_latin_question_exclamation_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % closing bracket followed by a colon or semicolon
        \XeTeXinterchartoks \g_polyglossia_latin_closing_bracket_class \g_polyglossia_latin_colon_semicolon_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % closing bracket followed by a closing guillemet
        \XeTeXinterchartoks \g_polyglossia_latin_closing_bracket_class \g_polyglossia_latin_closing_guillemet_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % opening guillemet followed by a space
        \XeTeXinterchartoks \g_polyglossia_latin_opening_guillemet_class \e@alloc@intercharclass@top =
          {
            \polyglossia_latin_replace_following_space:
          }
        % opening guillemet followed by an opening guillemet
        \XeTeXinterchartoks \g_polyglossia_latin_opening_guillemet_class \g_polyglossia_latin_opening_guillemet_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % opening guillemet followed by an ordinary character
        \XeTeXinterchartoks \g_polyglossia_latin_opening_guillemet_class \z@ =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % closing guillemet followed by a closing guillemet
        \XeTeXinterchartoks \g_polyglossia_latin_closing_guillemet_class \g_polyglossia_latin_closing_guillemet_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % closing guillemet followed by a question or exclamation mark
        \XeTeXinterchartoks \g_polyglossia_latin_closing_guillemet_class \g_polyglossia_latin_question_exclamation_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % closing guillemet followed by a colon or semicolon
        \XeTeXinterchartoks \g_polyglossia_latin_closing_guillemet_class \g_polyglossia_latin_colon_semicolon_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % space followed by a question or exclamation mark
        \XeTeXinterchartoks \e@alloc@intercharclass@top \g_polyglossia_latin_question_exclamation_class =
          {
            \polyglossia_latin_replace_preceding_space:
          }
        % space followed by a colon or semicolon
        \XeTeXinterchartoks \e@alloc@intercharclass@top \g_polyglossia_latin_colon_semicolon_class =
          {
            \polyglossia_latin_replace_preceding_space:
          }
        % space followed by closing guillemet
        \XeTeXinterchartoks \e@alloc@intercharclass@top \g_polyglossia_latin_closing_guillemet_class =
          {
            \polyglossia_latin_replace_preceding_space:
          }
        % ordinary character followed by a question or exclamation mark
        \XeTeXinterchartoks \z@ \g_polyglossia_latin_question_exclamation_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % ordinary character followed by a colon or semicolon
        \XeTeXinterchartoks \z@ \g_polyglossia_latin_colon_semicolon_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
        % ordinary character followed by closing guillemet
        \XeTeXinterchartoks \z@ \g_polyglossia_latin_closing_guillemet_class =
          {
            \polyglossia_latin_insert_punctuation_space:
          }
      }
  }

\cs_new:Npn \polyglossia_latin_no_punctuation_spacing:
  {
    \sys_if_engine_luatex:TF
      {
        \directlua { polyglossia.deactivate_latin_punct() }
      }
      {
        \XeTeXcharclass `\! \z@
        \XeTeXcharclass `\? \z@
        \XeTeXcharclass `\‼ \z@
        \XeTeXcharclass `\⁇ \z@
        \XeTeXcharclass `\⁈ \z@
        \XeTeXcharclass `\⁉ \z@
        \XeTeXcharclass `\‽ \z@
        \XeTeXcharclass `\; \z@
        \XeTeXcharclass `\: \z@
        \XeTeXcharclass `\« \z@
        \XeTeXcharclass `\» \z@
        \XeTeXcharclass `\‹ \z@
        \XeTeXcharclass `\› \z@
        \XeTeXcharclass `\( \z@
        \XeTeXcharclass `\) \z@
        \XeTeXcharclass `\[ \z@
        \XeTeXcharclass `\] \z@
        \XeTeXcharclass `\{ \z@
        \XeTeXcharclass `\} \z@
        \XeTeXcharclass `\⟨ \z@
        \XeTeXcharclass `\⟩ \z@
        \XeTeXinterchartokenstate = 0
      }
  }


%%%%% Messages and commands concerning footnotes

% Save original footnote definition
% Do this at the end of the preamble to catch other
% packages' footnote changes (#391)
\hook_gput_code:nnn { begindocument / before } {.}
{
  \cs_if_exist:NT \@makefntext
    {
      \cs_set_eq:NN \polyglossia_latin_original_footnote:n \@makefntext
    }
}

% This is the footnote style as defined by the "ecclesiastic" package.
\cs_new:Npn \polyglossia_latin_variant_footnote:n #1
  {
    \parindent 1em
    \noindent
    \hbox { \normalfont \@thefnmark . }
    \enspace #1
  }

\msg_new:nnn {polyglossia} {latin / ineffective footnote option}
  {
    The~option~"ecclesiasticfootnotes"~is~ineffective~\msg_line_context:
    \c_space_tl as~Latin~is~not~the~main~language.
  }

\cs_new:Npn \polyglossia_latin_apply_footnote_option:
  {
    \IfMainLanguageTF {latin}
      {
        \cs_if_exist:NT \@makefntext
          {
            \bool_if:NTF \l_polyglossia_latin_ecclesiastic_footnotes_bool
             {
              \let \@makefntext \polyglossia_latin_variant_footnote:n
             }
             {
              \let \@makefntext \polyglossia_latin_original_footnote:n
             }
          }
      }
      {
        \bool_if:NT \l_polyglossia_latin_ecclesiastic_footnotes_bool
          {
            \msg_warning:nn {polyglossia} {latin / ineffective footnote option}
          }
      }
  }

%%%%% Language variants: classic, medieval, modern, and ecclesiastic

\str_new:N \l_polyglossia_latin_variant_str

\msg_new:nnn {polyglossia} {latin / language variant}
  {
    Activating~Latin~language~variant~"#1"~\msg_line_context:.
  }

\msg_new:nnn {polyglossia} {latin / illegal language variant}
  {
    The~Latin~language~variant~"#1"~is~undefined~\msg_line_context:.
  }

\cs_new:Npn \polyglossia_latin_classic_settings:
  {
    \bool_set_false:N \l_polyglossia_latin_use_j_bool
    \bool_set_false:N \l_polyglossia_latin_use_v_bool
    \bool_set_false:N \l_polyglossia_latin_use_ligatures_bool
    \bool_set_true:N \l_polyglossia_latin_capitalize_month_bool
    \bool_set_false:N \l_polyglossia_latin_punctuation_spacing_bool
    \str_set:Nn \l_polyglossia_latin_variant_str {classic}
    \SetLanguageKeys {latin} { babelname = classicallatin, bcp47 = la-x-classic, bcp47-extension-x = classic, bcp47-casing = la-x-classic }
    \polyglossia_latin_set_patterns:n {classiclatin}
  }

\cs_new:Npn \polyglossia_latin_medieval_settings:
  {
    \bool_set_false:N \l_polyglossia_latin_use_j_bool
    \bool_set_false:N \l_polyglossia_latin_use_v_bool
    \bool_set_true:N \l_polyglossia_latin_use_ligatures_bool
    \bool_set_true:N \l_polyglossia_latin_capitalize_month_bool
    \bool_set_false:N \l_polyglossia_latin_punctuation_spacing_bool
    \str_set:Nn \l_polyglossia_latin_variant_str {medieval}
    \SetLanguageKeys {latin} { babelname = medievallatin, bcp47 = la-x-medieval, bcp47-extension-x = medieval, bcp47-casing = la-x-medieval }
    \polyglossia_latin_use_modern_patterns:
  }

\cs_new:Npn \polyglossia_latin_modern_settings:
  {
    \bool_set_false:N \l_polyglossia_latin_use_j_bool
    \bool_set_true:N \l_polyglossia_latin_use_v_bool
    \bool_set_false:N \l_polyglossia_latin_use_ligatures_bool
    \bool_set_true:N \l_polyglossia_latin_capitalize_month_bool
    \bool_set_false:N \l_polyglossia_latin_punctuation_spacing_bool
    \str_set:Nn \l_polyglossia_latin_variant_str {modern}
    \SetLanguageKeys {latin} { babelname = latin, bcp47 = la }
    \polyglossia_latin_use_modern_patterns:
  }

\cs_new:Npn \polyglossia_latin_ecclesiastic_settings:
  {
    \bool_set_false:N \l_polyglossia_latin_use_j_bool
    \bool_set_true:N \l_polyglossia_latin_use_v_bool
    \bool_set_true:N \l_polyglossia_latin_use_ligatures_bool
    \bool_set_false:N \l_polyglossia_latin_capitalize_month_bool
    \bool_set_true:N \l_polyglossia_latin_punctuation_spacing_bool
    \str_set:Nn \l_polyglossia_latin_variant_str {ecclesiastic}
    \SetLanguageKeys {latin} { babelname = ecclesiasticallatin, bcp47 = la-x-ecclesia, bcp47-extension-x = ecclesia }
    \polyglossia_latin_use_modern_patterns:
  }

%%%%% Hyphenation variants: classic, liturgical, and modern

\msg_new:nnn {polyglossia} {latin / hyphenation variant}
  {
    Activating~hyphenation~patterns~for~#1~Latin~\msg_line_context:.
  }

\msg_new:nnn {polyglossia} {latin / illegal hyphenation variant}
  {
    The~Latin~hyphenation~variant~"#1"~is~undefined~\msg_line_context:.
  }

%%%%% Latin captions and date

\def \captionslatin
  {
    \def \prefacename
      {
        \bool_if:NTF \l_polyglossia_latin_use_ligatures_bool {Præfatio} {Praefatio}
      }
    \def \refname {Conspectus~librorum}
    \def \abstractname {Summarium}
    \def \bibname {Conspectus~librorum}
    \def \chaptername {Caput}
    \def \appendixname {Additamentum}
    \def \contentsname {Index}
    \def \listfigurename {Conspectus~descriptionum}
    \def \listtablename {Conspectus~tabularum}
    \def \indexname {Index~rerum~notabilium}
    \def \figurename {Descriptio}
    \def \tablename {Tabula}
    \def \partname {Pars}
    \def \enclname {Additur}
    \def \ccname {Exemplar}
    \def \headtoname {\ignorespaces}
    \def \pagename {charta}
    \def \seename {cfr.}
    \def \alsoname {cfr.}
    \def \proofname {Demonstratio}
    \def \glossaryname {Glossarium}
  }

\cs_new:Npn \polyglossia_latin_month_name:
  {
    \tl_set:Nx \l_tmpa_tl
      {
        \int_case:nn { \month }
          {
            {1} { \bool_if:NTF \l_polyglossia_latin_use_j_bool {januarii} {ianuarii} }
            {2} {februarii}
            {3} {martii}
            {4} {aprilis}
            {5} { \bool_if:NTF \l_polyglossia_latin_use_j_bool {maji} {maii} }
            {6} { \bool_if:NTF \l_polyglossia_latin_use_j_bool {junii} {iunii} }
            {7} { \bool_if:NTF \l_polyglossia_latin_use_j_bool {julii} {iulii} }
            {8} {augusti}
            {9} {septembris}
            {10} {octobris}
            {11} { \bool_if:NTF \l_polyglossia_latin_use_v_bool {novembris} {nouembris} }
            {12} {decembris}
          }
      }
    \bool_if:NTF \l_polyglossia_latin_capitalize_month_bool
      {
        \text_titlecase:n { \l_tmpa_tl }
      }
      {
        \l_tmpa_tl
      }
  }

\def \datelatin
  {
    \def \today
      {
        \int_to_Roman:n { \day }
        \c_space_tl
        \polyglossia_latin_month_name:
        \c_space_tl
        \int_to_Roman:n { \year }
      }
  }

\DeclareKeys[polyglossia/gloss/latin]
  {
    babelshorthands.bool_set:N = \l_polyglossia_latin_babelshorthands_bool,
    babelshorthands.default:n = true,
    prosodicshorthands.bool_set:N = \l_polyglossia_latin_prosodic_shorthands_bool,
    prosodicshorthands.default:n = true,
    usej.bool_set:N = \l_polyglossia_latin_use_j_bool,
    capitalizemonth.bool_set:N = \l_polyglossia_latin_capitalize_month_bool,
    hyphenation.code =
      \str_case:nnTF {#1}
      {
        {classic}    { \polyglossia_latin_set_patterns:n {classiclatin} }
        {liturgical} { \polyglossia_latin_set_patterns:n {liturgicallatin} }
        {modern}     { \polyglossia_latin_use_modern_patterns: }
      }
      {
        \msg_info:nnn {polyglossia} {latin / hyphenation variant} {#1}
      }
      {
        \msg_warning:nnn {polyglossia} {latin / illegal hyphenation variant} {#1}
      },
    variant.code =
      \str_case:nnF {#1}
      {
        {classic}
        {
          \msg_info:nnn {polyglossia} {latin / language variant} {classic}
          \polyglossia_latin_classic_settings:
        }
        {medieval}
        {
          \msg_info:nnn {polyglossia} {latin / language variant} {medieval}
          \polyglossia_latin_medieval_settings:
        }
        {modern}
        {
          \msg_info:nnn {polyglossia} {latin / language variant} {modern}
          \polyglossia_latin_modern_settings:
        }
        {ecclesiastic}
        {
          \msg_info:nnn {polyglossia} {latin / language variant} {ecclesiastic}
          \polyglossia_latin_ecclesiastic_settings:
        }
      }
      {
        \msg_warning:nnn {polyglossia} {latin / illegal language variant} {#1}
      },
    ecclesiasticfootnotes.choices:nn = { true, false }
      {
        \ifcase\UseName{l_keys_choice_int}\or
           \bool_set_true:N \l_polyglossia_latin_ecclesiastic_footnotes_bool
        \else
          \bool_set_false:N \l_polyglossia_latin_ecclesiastic_footnotes_bool
        \fi
        \xpg_if_in_preamble:TF
          {
            % within the preamble
            % The application of the option has to be postponed as the main
            % language may be undefined when the option is called.
            \AtBeginDocument { \polyglossia_latin_apply_footnote_option: }
          }
          {
            % within the document
            \polyglossia_latin_apply_footnote_option:
          }
      },
    ecclesiasticfootnotes.default:n = true
  }

\ifsystem@babelshorthands
  \SetGlossOptions{latin}{babelshorthands=true}
  % Register default options
  \InitializeGlossOptions{latin}{variant=modern,hyphenation=modern,babelshorthands=true,
                                 prosodicshorthands=false,ecclesiasticfootnotes=false,
                                 usej=false,capitalizemonth=true}
\else
  \SetGlossOptions{latin}{babelshorthands=false}
  % Register default options
  \InitializeGlossOptions{latin}{variant=modern,hyphenation=modern,babelshorthands=false,
                                 prosodicshorthands=false,ecclesiasticfootnotes=false,
                                 usej=false,capitalizemonth=true}
\fi

\ExplSyntaxOff % babelsh.def does not support expl3 syntax
\@ifundefined{initiate@active@char}{\input{babelsh.def}}{}
\ExplSyntaxOn

\initiate@active@char {"}
\initiate@active@char {'}
\initiate@active@char {^}
\initiate@active@char {=}

\shorthandoff {"}
\shorthandoff {'}
\shorthandoff* {^}
\shorthandoff {=}

% The active = character may cause problems with key=value interfaces.
% We have to make sure here that no problems can occur outside a Latin
% prosodic shorthand environment.
% The active ^ character may cause problems with TeX's ^^xx convention
% for hexadecimal input. We have to make sure here that no problems can
% occur outside a Latin prosodic shorthand environment.
% The active ' character may cause problems with the unicode-math package
% (in case Latin is used as a secondary language, see #394). We have to
% turn it off if Latin is not the main language.

% For the aux file:
\protected@write \@auxout { }
  {
    \ProvideDocumentCommand \shorthandoff{sm}{} % #643
    \shorthandoff {=}
    \shorthandoff* {^} % #582
  }

\AtBeginDocument
  {
    \IfMainLanguageTF {latin}
      {
        \bool_if:NF \l_polyglossia_latin_prosodic_shorthands_bool
         {
          \shorthandoff {=}
          \shorthandoff* {^}
         }
      }
      {
        % The following command should not be called if the main language
        % defines a = shorthand. However, there are no languages besides
        % Latin defining such a shorthand in polyglossia.
        \shorthandoff {=}
        % The following command should not be called if the main language
        % defines a ^ shorthand. However, there are no languages besides
        % Latin defining such a shorthand in polyglossia.
        \shorthandoff* {^}
        % The following command should not be called if the main language
        % defines a ' shorthand. However, there are no languages besides
        % Latin defining such a shorthand in polyglossia.
        \shorthandoff {'}
      }
  }

\cs_new:Npn \polyglossia_latin_shorthands:
  {
    \def \language@group {latin}
    \xpg@activate@shorthands ["]
    \declare@shorthand {latin} {"}
      {
        \mode_if_math:TF
          {
            \token_to_str:N "
          }
          {
            \polyglossia_latin_apply_quotemark:N
          }
      }
    % The ' shorthand is normally turned off to avoid problems with the unicode-math
    % package. We have to turn it on here.
    \shorthandon {'}
    \xpg@activate@shorthands [']
    \declare@shorthand {latin} {'}
      {
        \mode_if_math:TF
          {
            \active@math@prime % defined in "latex.ltx"
            % This definition is differing from the primes of the unicode-math package.
            % TO DO: Make sure that the appearance of primes is the same as with the
            % unicode-math package if this package is loaded.
          }
          {
            \polyglossia_latin_put_acute:N
          }
      }
  }

\cs_new:Npn \polyglossia_latin_prosodic_shorthands:
  {
    \def \language@group {latin}
    % The '=' and '^' shorthands are normally turned off to avoid problems with
    % key=value interfaces and TeX's ^^xx convention for hexadecimal input. We
    % turn them on here to enable prosodic shorthands.
    \shorthandon {=}
    \bbl@activate {=}
    \declare@shorthand {latin} {=}
      {
        \mode_if_math:TF
          {
            \token_to_str:N =
          }
          {
            \polyglossia_latin_put_macron:N
          }
      }
    \shorthandon {^}
    \bbl@activate {^}
    \declare@shorthand {latin} {^}
      {
        \mode_if_math:TF
          {
            \token_to_str:N ^
          }
          {
            \polyglossia_latin_put_breve:N
          }
      }
  }

\cs_new:Npn \polyglossia_latin_apply_quotemark:N #1
  {
    \str_case:nnF {#1}
      {
        {A} { \polyglossia_latin_ligature_shorthand:Nnn E { Æ }
                {
                  \polyglossia_latin_ligature_shorthand:Nnn e { Æ }
                    {
                      \polyglossia_latin_allow_hyphens: A
                    }
                }
            }
        {a} { \polyglossia_latin_ligature_shorthand:Nnn e { æ }
                {
                  \polyglossia_latin_allow_hyphens: a
                }
            }
        {O} { \polyglossia_latin_ligature_shorthand:Nnn E { Œ }
                {
                  \polyglossia_latin_ligature_shorthand:Nnn e { Œ }
                    {
                      \polyglossia_latin_allow_hyphens: O
                    }
                }
            }
        {o} { \polyglossia_latin_ligature_shorthand:Nnn e { œ }
                {
                  \polyglossia_latin_allow_hyphens: o
                }
            }
        {|} { \polyglossia_latin_allow_hyphens: }
        {<} { « }
        {>} { » }
      }
      {
        \tl_if_single_token:nTF {#1}
          {
            \token_if_letter:NTF #1 { \polyglossia_latin_allow_hyphens: }
              {
                \polyglossia_latin_if_ligature_command:NTF #1 { \polyglossia_latin_allow_hyphens: }
                  {
                    \token_to_str:N "
                  }
              }
          }
          {
            \token_to_str:N "
          }
          #1
      }
  }

\cs_new:Npn \polyglossia_latin_put_acute:N #1
  {
    \str_case:nnF {#1}
      {
        {A} { \polyglossia_latin_ligature_shorthand:Nnn E { Ǽ }
                {
                  \polyglossia_latin_ligature_shorthand:Nnn e { Ǽ } { Á }
                }
            }
        {a} { \polyglossia_latin_ligature_shorthand:Nnn e { ǽ } { á } }
        {E} { É }
        {e} { é }
        {I} { Í }
        {i} { í }
        {O} { \polyglossia_latin_ligature_shorthand:Nnn E { \'Œ }
                {
                  \polyglossia_latin_ligature_shorthand:Nnn e { \'Œ } { Ó }
                }
            }
        {o} { \polyglossia_latin_ligature_shorthand:Nnn e { \'œ } { ó } }
        {U} { Ú }
        {u} { ú }
        {V} { \' V } % V may be a vowel, but lowercase v is never used as a vowel.
        {Y} { Ý }
        {y} { ý }
        {Æ} { Ǽ }
        {æ} { ǽ }
        {Œ} { \'Œ }
        {œ} { \'œ }
      }
      {
        \tl_if_single_token:nTF {#1}
          {
            \polyglossia_latin_if_ligature_command:NTF #1 { \' }
              {
                \token_to_str:N '
              }
          }
          {
            \token_to_str:N '
          }
          #1
      }
  }

\cs_new:Npn \polyglossia_latin_put_macron:N #1
  {
    \str_case:nnF {#1}
      {
        {A} { \polyglossia_latin_ligature_macron:NNnn AE { Ǣ }
                {
                  \polyglossia_latin_ligature_macron:NNnn Ae { Ǣ }
                    {
                      \polyglossia_latin_diphthong_macron:NNn AU
                        {
                          \polyglossia_latin_diphthong_macron:NNn Au { Ā }
                        }
                    }
                }
            }
        {a} { \polyglossia_latin_ligature_macron:NNnn ae { ǣ }
                {
                  \polyglossia_latin_diphthong_macron:NNn au { ā }
                }
            }
        {E} { \polyglossia_latin_diphthong_macron:NNn EU
                {
                  \polyglossia_latin_diphthong_macron:NNn Eu { Ē }
                }
            }
        {e} { \polyglossia_latin_diphthong_macron:NNn eu { ē } }
        {I} { Ī }
        {i} { ī }
        {O} { \polyglossia_latin_ligature_macron:NNnn OE { \=Œ }
                {
                  \polyglossia_latin_ligature_macron:NNnn Oe { \=Œ } { Ō }
                }
            }
        {o} { \polyglossia_latin_ligature_macron:NNnn oe { \=œ } { ō } }
        {U} { Ū }
        {u} { ū }
        {V} { \= V } % V may be a vowel, but lowercase v is never used as a vowel.
        {Y} { Ȳ }
        {y} { ȳ }
      }
      {
        \tl_if_single_token:nTF {#1}
          {
            \polyglossia_latin_if_ligature_command:NTF #1 { \= }
              {
                \token_to_str:N =
              }
          }
          {
            \token_to_str:N =
          }
        #1
      }
  }

\cs_new:Npn \polyglossia_latin_put_breve:N #1
  {
    \str_case:nnF {#1}
      {
        {A} { Ă }
        {a} { ă }
        {E} { Ĕ }
        {e} { ĕ }
        {I} { Ĭ }
        {i} { ĭ }
        {O} { Ŏ }
        {o} { ŏ }
        {U} { Ŭ }
        {u} { ŭ }
        {V} { \u{V} } % V may be a vowel, but lowercase v is never used as a vowel.
        {Y} { \u{Y} }
        {y} { \u{y} }
      }
      {
        \token_to_str:N ^
        #1
      }
  }


\cs_new:Npn \polyglossia_latin_allow_hyphens:
  {
    \bbl@allowhyphens
    \discretionary {-} {} {}
    \bbl@allowhyphens
  }

\cs_new:Npn \polyglossia_latin_ligature_shorthand:Nnn #1#2#3
% #1: second letter of ligature (E or e)
% #2: ligature character
% #3: non-ligature code
  {
    \bool_if:NTF \l_polyglossia_latin_use_ligatures_bool
      {
        \peek_meaning_remove:NTF #1 {#2} {#3}
      }
      {
        #3
      }
  }

\msg_new:nnn {polyglossia} {latin / no-double-macron-font}
  {
    The~combining~double~macron~(U+035E)~is~not~available~in~the~current~
    font.~The~diphthong~is~typeset~without~macron~ \msg_line_context: .
  }

\cs_new:Npn \polyglossia_latin_diphthong_macron:NNn #1#2#3
% #1: first character of diphthong
% #2: second character of diphthong
% #3: non-diphthong code
  {
    \peek_meaning:NTF #2
      {
        #1
        \iffontchar \font "35E \relax % U+035E: combining double macron
          \char "35E \relax
        \else
          \msg_warning:nn {polyglossia} {latin / no-double-macron-font}
        \fi
      }
      {
        #3
      }
  }

\cs_new:Npn \polyglossia_latin_ligature_macron:NNnn #1#2#3#4
% #1: first character of diphthong
% #2: second character of diphthong
% #3: ligature code
% #4: false code
  {
    \polyglossia_latin_ligature_shorthand:Nnn #2 {#3}
      {
        \polyglossia_latin_diphthong_macron:NNn #1 #2 {#4}
      }
  }

% The following conditional tests if the argument is a ligature command (\AE,
% \ae, \OE, or \oe).
\prg_new_conditional:Npnn \polyglossia_latin_if_ligature_command:N #1 {TF}
  {
    \token_if_eq_meaning:NNTF #1 \AE { \prg_return_true: }
      {
        \token_if_eq_meaning:NNTF #1 \ae { \prg_return_true: }
          {
            \token_if_eq_meaning:NNTF #1 \OE { \prg_return_true: }
              {
                \token_if_eq_meaning:NNTF #1 \oe { \prg_return_true: }
                  {
                    \prg_return_false:
                  }
              }
          }
      }
  }

\cs_new:Npn \polyglossia_latin_no_shorthands:
  {
    \xpg@deactivate@shorthands ["]
    \xpg@deactivate@shorthands [']
  }

\cs_new:Npn \polyglossia_latin_no_prosodic_shorthands:
  {
    \bbl@deactivate {=}
    \bbl@deactivate {^}
    % The active '=' character may cause problems with key=value interfaces.
    % We have to make sure here that no problems can occur outside a Latin
    % prosodic shorthand environment.
    \shorthandoff {=}
    % The active '^' character may cause problems with TeX's ^^xx convention
    % for hexadecimal input. We have to make sure here that no problems can
    % occur outside a Latin prosodic shorthand environment.
    \shorthandoff* {^}
  }


%%%%% Further settings

\let \xpgla@savedvalues \empty

\hook_gput_code:nnn { begindocument / before } {.}
  {
    \edef \xpgla@savedvalues
      {
        \clubpenalty = \the \clubpenalty \space
        \@clubpenalty = \the \@clubpenalty \space
        \widowpenalty = \the \widowpenalty \space
        \finalhyphendemerits = \the \finalhyphendemerits
      }
  }

\def \noextras@latin
  {
    \bool_if:NT \l_polyglossia_latin_babelshorthands_bool
     {
      \polyglossia_latin_no_shorthands:
     }
    \bool_if:NT \l_polyglossia_latin_prosodic_shorthands_bool
     {
      \polyglossia_latin_no_prosodic_shorthands:
     }
    \xpgla@savedvalues
    \polyglossia_latin_no_punctuation_spacing:
    \bool_if:NF \l_polyglossia_latin_use_v_bool
      {
        \char_set_lccode:nn {`\V} {`\v}
      }
  }

\cs_new:Npn \polyglossia_latin_inline_extras:
  {
    \bool_if:NF \l_polyglossia_latin_use_v_bool
      {
        \char_set_lccode:nn {`\V} {`\u}
      }
    \bool_if:NT \l_polyglossia_latin_punctuation_spacing_bool
      {
        \polyglossia_latin_punctuation_spacing:
      }
    \bool_if:NTF \l_polyglossia_latin_babelshorthands_bool
      {
        \polyglossia_latin_shorthands:
      }
      {
        \polyglossia_latin_no_shorthands:
      }
    \bool_if:NTF \l_polyglossia_latin_prosodic_shorthands_bool
      {
        \polyglossia_latin_prosodic_shorthands:
      }
      {
        \polyglossia_latin_no_prosodic_shorthands:
      }
  }

\def \blockextras@latin
  {
    % The following four values were overtaken from the Italian language module.
    % It is unclear why they were chosen.
    \clubpenalty = 3000
    \@clubpenalty = 3000
    \widowpenalty = 3000
    \finalhyphendemerits = 50000000
    \polyglossia_latin_inline_extras:
  }

\def \inlineextras@latin
  {
    \polyglossia_latin_inline_extras:
  }


%% Default settings

\polyglossia_latin_modern_settings:

\ExplSyntaxOff

%%   Copyright (C) Claudio Beccari 2013-2016
%%   Copyright (C) Élie Roux 2016-2019
%%   Copyright (C) Keno Wehr 2019-2025
%%
%%   Permission is hereby granted, free of charge, to any person obtaining
%%   a copy of this software and associated documentation files
%%   (the "Software"), to deal in the Software without restriction, including
%%   without limitation the rights to use, copy, modify, merge, publish,
%%   distribute, sublicense, and/or sell copies of the Software, and to permit
%%   persons to whom the Software is furnished to do so, subject to the following
%%   conditions:
%%
%%   The above copyright notice and this permission notice shall be included in
%%   all copies or substantial portions of the Software.
%%
%%   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
%%   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
%%   IN THE SOFTWARE.
%%
%% End of file `gloss-latin.ldf'.
