%% This is part of the OpTeX project, see http://petr.olsak.net/optex

\_codedecl \begitems {Lists: begitems, enditems <2023-10-20>} % preloaded in format

   \_doc -----------------------------
   \`\_aboveliskip` is used above the list of items,\nl
   \`\_belowliskip` is used below the list of items,\nl
   \`\_setlistskip` sets the skip dependent on the current level of items,\nl
   \`\_listskipab` is \^`\ilistskipamount` or \^`\olistskipamount`.
   \_cod -----------------------------

\_def\_aboveliskip {\_removelastskip \_penalty-100 \_vskip\_listskipab}
\_def\_belowliskip {\_penalty-200 \_vskip\_listskipab}
\_newskip\_listskipab

\_def\_setlistskip {%
   \_ifnum \_ilevel = 1 \_listskipab = \_olistskipamount \_relax
   \_else  \_listskipab = \_ilistskipamount \_relax
   \_fi}

   \_doc -----------------------------
   The \`\itemnum` is locally reset to zero in each group declared by
   `\begitems`. So nested lists are numbered independently. Users can set
   initial value of `\itemnum` to another value after `\beitems` if they want.
   \nl
   Each level of nested lists is indented by the new `\iindent` from left.
   The default item mark is `\_printitem`.
   \nl
   The \`\begitems` runs `\_aboveliskip` only if we are not near below a title,
   where a vertical skip is placed already and where the `\penalty` 11333 is.
   It activates `*` and defines it as \`\_startitem`.
   \nl
   The \`\enditems` runs `\_isnextchar\_par{}{\_noindent}` thus the next
   paragraph is without indentation if there is no empty line between
   the list and this paragraph (it is similar behavior as after display math).
   \_cod -----------------------------

\_newcount\_itemnum  \_itemnum=0
\_newtoks\_printitem

\_def\_begitems{\_par
  \_bgroup
  \_advance \_ilevel by1
  \_setlistskip
  \_ifnum\_lastpenalty<10000 \_aboveliskip \_fi
  \_itemnum=0 \_adef*{\_relax\_ifmmode*\_else\_ea\_startitem\_fi}
  \_advance\_leftskip by\_iindent
  \_printitem=\_defaultitem
  \_the\_everylist \_relax
}
\_def\_enditems{\_par\_belowliskip\_egroup \_isnextchar\_par{}{\_noindent}}

\_def\_startitem{\_par \_ifnum\_itemnum>0 \_vskip\_itemskipamount \_fi
   \_advance\_itemnum by1
   \_the\_everyitem \_noindent\_llap{\_the\_printitem}\_ignorespaces
}
\_public \begitems \enditems \itemnum ;

   \_doc ----------------------------
   \`\novspaces` sets \^`\_listskipab` and \^`\itemskipamount` to 0pt.
   Moreover, it deactivates \^`\_setlistskip` (for inner lists).
   \_cod ----------------------------

\_def\_novspaces {\_removelastskip
   \_listskipab=\_zoskip \_itemskipamount=\_zoskip \_let\_setlistskip=\_relax}
\_public \novspaces ;

   \_doc -----------------------------
   Various item marks are saved in `\_item:<letter>` macros.
   You can re-define then or define more such macros.
   The \`\style` `<letter>` does \`\_printitem``={\_item:<letter>}`.
   More exactly: \^`\begitems` does \^`\_printitem=`\^`\defaultitem` first,
   then \^`\style` `<letter>` does \^`\_printitem``={\_item:<letter>}`
   when it is used and finally, \^`\_startitem` alias `*` uses \^`\_printitem`.
   \_cod -----------------------------

\_def\_style#1{%
   \_ifcsname _item:#1\_endcsname \_printitem=\_ea{\_csname _item:#1\_endcsname}%
   \_else \_printitem=\_defaultitem \_fi
}
\_sdef{_item:o}{\_raise.4ex\_hbox{$\_scriptscriptstyle\_bullet$} }
\_sdef{_item:-}{- }
\_sdef{_item:n}{\_the\_itemnum. }
\_sdef{_item:N}{\_the\_itemnum) }
\_sdef{_item:i}{(\_romannumeral\_itemnum) }
\_sdef{_item:I}{\_uppercase\_ea{\_romannumeral\_itemnum}\_kern.5em}
\_sdef{_item:a}{\_athe\_itemnum) }
\_sdef{_item:A}{\_uppercase\_ea{\_athe\_itemnum}) }
\_sdef{_item:x}{\_raise.3ex\_fullrectangle{.6ex}\_kern.4em}
\_sdef{_item:X}{\_raise.2ex\_fullrectangle{1ex}\_kern.5em}
\_sdef{_item:d}{\_aftergroup\_dword}
\_def\_dword#1#2{{\_bf #2 }\_ignorespaces} % #1 is \_ignorespaces from \_startitem

   \_doc -----------------------------
   \`\_athe``{<num>}` returns the `<num>`s lowercase letter from the alphabet.\nl
   \`\_fullrectangle``{<dimen>}` prints full rectangle with given `<dimen>`.
   \_cod -----------------------------

\_def\_fullrectangle#1{\_hbox{\_vrule height#1 width#1}}

\_def\_athe#1{\_ifcase#1?\_or a\_or b\_or c\_or d\_or e\_or f\_or g\_or h\_or
   i\_or j\_or k\_or l\_or m\_or n\_or o\_or p\_or q\_or r\_or s\_or t\_or
   u\_or v\_or w\_or x\_or y\_or z\_else ?\_fi
}
\_public \style ;

   \_doc -----------------------------
   The \`\begblock` macro selects fonts from footnotes \^`\_fnset`
   and opens new indentation in a group.
   \`\endblock` closes the group.
   This is implemented as an counterpart
   of Markdown's Blockquotes. Redefine these macros if you want to declare
   different design.
   The \ulink[http://petr.olsak.net/optex/optex-tricks.html\#greyblock]{\OpTeX/ trick 0031}
   shows how to create blocks with grey background splittable to more pages.
   \_cod -----------------------------

\_def\_begblock{\_bgroup\_fnset \_medskip \_advance\_leftskip by\_iindent \_firstnoindent}
\_def\_endblock{\_par\_medskip\_egroup\_isnextchar\_par{}{\_noindent}}

\_public \begblock \endblock ;


\_endcode % -------------------------------------

2023-10-20 \style d moved from OpTeX trick 0108 to the format
2022-02-25 \olistskipamount, \ilistskipamount, \itemskipamount introduced
2021-03-10 \_ifmmode*\_else\_ea\_startitem\_fi added to * definition
2020-04-21 \isnextchar\par added to \enditems
2020-03-18 introduced
