%D \module
%D   [       file=tabl-mis,
%D        version=2012.06.28,
%D          title=\CONTEXT\ Table Macros,
%D       subtitle=Miscellaneous,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Table Macros / Miscellaneous}

\unprotect

%D \macros
%D   {somekindoftab,kindoftabposition}
%D
%D This macro can be used to create tabs:
%D
%D \starttyping
%D \setupheadertexts[{\somekindoftab[alternative=horizontal]{\framed{\kindoftabposition}}}]
%D \setuptexttexts  [{\somekindoftab[alternative=vertical]  {\framed{\kindoftabposition}}}]
%D
%D \starttext
%D     \showframe \dorecurse{10}{test\page}
%D \stoptext
%D \stoptyping
%D
%D (This rather old but updated code used to be in \type {core-mis.mkiv}.)

\mutable\let\kindoftabposition\!!zerocount

\permanent\tolerant\protected\def\somekindoftab[#S#1]%
  {\bgroup
   \getdummyparameters
     [\c!alternative=\v!vertical,
      \c!width=\textwidth,\c!height=\textheight,
      \c!n=\lastpage,\c!m=\realpageno,
      #1]%
   \ifcstok{\directdummyparameter\c!alternative}\v!vertical
     \expandafter\typo_kindoftab_indeed_v
   \else
     \expandafter\typo_kindoftab_indeed_h
   \fi}

\def\typo_kindoftab_indeed_v{\typo_kindoftab_indeed\vbox\vskip\c!height}
\def\typo_kindoftab_indeed_h{\typo_kindoftab_indeed\hbox\hskip\c!width }

\def\typo_kindoftab_indeed#1#2#3#4%
  {#1 to \directdummyparameter#3 \bgroup
     \forgetall
     \scratchnx\directdummyparameter\c!n\relax
     \scratchmx\directdummyparameter\c!m\relax
     \edef\kindoftabposition{\the\scratchmx}%
     \ifnum\scratchmx>\plusone
       #2\zeropoint \s!plus \the\numexpr\scratchmx-\plusone  \relax\s!fill\relax
     \fi
     #4% can use \kindoftabposition
     \ifnum\scratchmx<\scratchnx\relax
       #2\zeropoint \s!plus \the\numexpr\scratchnx-\scratchmx\relax\s!fill\relax
     \fi
   \egroup
   \egroup}

%D The following paragraphs mechanism is probably one of the oldest of \CONTEXT\ and
%D mostly served as a table mechanism capable of dealing with paragraphs. Nowadays
%D one can also use tabulate or natural tables.
%D
%D \startbuffer
%D \defineparagraphs[sample][n=2,rule=on]
%D
%D \startsample
%D     first  \nextsample
%D     second \nextsample
%D     third
%D \stopsample
%D
%D \startsample
%D     \input tufte \nextsample
%D     \input ward  \nextsample
%D     \input davis \nextsample
%D     \input zapf
%D \stopsample
%D
%D \startparagraphs[sample]
%D     first  \nextsample
%D     second \nextsample
%D     third
%D \stopparagraphs
%D
%D \startparagraphs[sample]
%D     \startparagraphscell
%D         first
%D     \stopparagraphscell
%D     \startparagraphscell
%D         second
%D     \stopparagraphscell
%D     \startparagraphscell
%D         third
%D     \stopparagraphscell
%D \stopparagraphs
%D \stopbuffer
%D
%D \typebuffer \getbuffer

\installcorenamespace{paragraphs}

\installframedcommandhandler \??paragraphs {paragraphs} \??paragraphs

\setupparagraphs
  [\c!n=3,
   \c!offset=\zeropoint,
   \c!before=\blank,
   \c!after=\blank,
   \c!distance=\emwidth,
   \c!height=\v!fit,
   \c!width=\availablehsize,
   \c!rule=\v!off,
   \c!command=,
   \c!align=,
   \c!tolerance=\v!tolerant, % obsolete
   \c!rulethickness=\linewidth,
   \c!rulecolor=,
   \c!style=,
   \c!color=,
   \c!frame=\v!off,
   \c!top=\vss,
   \c!bottom=\vfill]

\aliased\let\typo_paragraphs_setup_saved\setupparagraphs

\let\typo_paragraphs_setup_step\gobbleoneargument

\overloaded\permanent\tolerant\protected\def\setupparagraphs[#S#1]#*[#S#2]#*[#S#3]% we are downward compatible with [each] and [1,3]
  {\ifarguments\or
      \typo_paragraphs_setup_saved[#1]%
   \or
      \typo_paragraphs_setup_saved[#1][#2]%
   \or
     \ifcstok{#2}\v!each
       \localcontrolledrepeat \namedparagraphsparameter{#1}\c!n
         {\normalexpanded{\typo_paragraphs_setup_saved[#1:\the\currentloopiterator]}[#3]}%
     \else
       \def\typo_paragraphs_setup_step##1{\typo_paragraphs_setup_saved[#1:##1][#3]}%
       \processcommalist[#2]\typo_paragraphs_setup_step
     \fi
   \fi}

\appendtoks
    \frozen\instance\letcsname\e!next\currentparagraphs\endcsname\nextparagraphs
    \frozen\instance\letcsname\currentparagraphs\endcsname\nextparagraphs
    \frozen\instance\protected\edefcsname\e!start\currentparagraphs\endcsname{\startparagraphs[\currentparagraphs]}%
    \frozen\instance\letcsname\e!stop\currentparagraphs\endcsname\stopparagraphs
    \localcontrolledrepeat \paragraphsparameter\c!n
      {\normalexpanded
         {\typo_paragraphs_setup_saved
            [\currentparagraphs:\the\currentloopiterator]%
            [\c!width=,\s!parent=\??paragraphs\currentparagraphs]}}%
    \typo_paragraphs_setup_saved[\currentparagraphs:1][\c!distance=\zeropoint]%
\to \everydefineparagraphs

\newinteger  \c_typo_paragraphs_n
\newinteger  \c_typo_paragraphs_max
\newdimension\d_typo_paragraphs_width
\newdimension\d_typo_paragraphs_auto

\let\typo_paragraphs_start_cell\relax
\let\typo_paragraphs_stop_cell \relax

\permanent\protected\def\startparagraphs[#1]% quite slow
  {\bgroup % (1)
   \cdef\currentparagraphs{#1}%
 % \paragraphsparameter\c!before
   \edef\p_width{\paragraphsparameter\c!width}%
   \ifempty\p_width
     \d_typo_paragraphs_width\availablehsize
   \else
     \d_typo_paragraphs_width\p_width\relax
   \fi
   \d_typo_paragraphs_width{\d_typo_paragraphs_width-(\paragraphsparameter\c!offset)*2}%
   \c_typo_paragraphs_max{\paragraphsparameter\c!n}%
   \d_typo_paragraphs_auto\d_typo_paragraphs_width\relax
   \scratchcounter\zerocount
   \localcontrolledrepeat \c_typo_paragraphs_max
     {\edef\p_width{\namedparagraphsparameter{\currentparagraphs:\the\currentloopiterator}\c!width}%
      \ifempty\p_width
        \advanceby\scratchcounter\plusone
      \else
        \advanceby\d_typo_paragraphs_auto-\p_width\relax
      \fi
      \ifnum\currentloopiterator>\plusone
        \advanceby\d_typo_paragraphs_auto{-(\namedparagraphsparameter{\currentparagraphs:\number\currentloopiterator}\c!distance)}%
      \fi}%
   \ifnum\scratchcounter>\zerocount
     \divideby\d_typo_paragraphs_auto\scratchcounter
   \else
     \d_typo_paragraphs_auto\zeropoint
   \fi
   \parindent\zeropoint
   \c_typo_paragraphs_n\zerocount
   \enforced\let\\\nextparagraphs % downward compatible
  %\scratchoffset\ifchkdimension\paragraphsparameter\c!offset\or\lastchkdimension\else\zeropoint\fi
   \setbox\scratchbox\hpack
   \bgroup % (2)
   \forgetall
  %\advanceby\hsize-2\scratchoffset
   \ifchkdimension\paragraphsparameter\c!offset\or\advanceby\hsize-2\lastchkdimension\fi
   \let\typo_paragraphs_start_cell\typo_paragraphs_start_cell_indeed
   \let\typo_paragraphs_stop_cell \typo_paragraphs_stop_cell_indeed
   \typo_paragraphs_start_cell_indeed}

\permanent\protected\def\stopparagraphs
  {\stopparagraphscell
   \egroup  % (2)
   \letparagraphsparameter\c!align\v!flushleft % normal
   \paragraphsparameter\c!before
   \dontleavehmode\inheritedparagraphsframed{\box\scratchbox}%
   \paragraphsparameter\c!after
   \egroup} % (1)

\permanent\protected\def\nextparagraphs
  {\stopparagraphscell
   \startparagraphscell}

\permanent\protected\def\startparagraphscell
  {\typo_paragraphs_start_cell}

\def\typo_paragraphs_start_cell_indeed
  {\removeunwantedspaces
   \advanceby\c_typo_paragraphs_n\plusone
   \ifnum\c_typo_paragraphs_n>\c_typo_paragraphs_max
     \expandafter\typo_paragraphs_start_cell_nop
   \else
     \expandafter\typo_paragraphs_start_cell_yes
   \fi}

\def\typo_paragraphs_start_cell_nop
  {\begingroup
   % message: too many cells in paragraphs
   \let\typo_paragraphs_start_cell\relax
   \let\typo_paragraphs_stop_cell\typo_paragraphs_stop_cell_indeed
   \setbox\scratchbox\vbox\bgroup}

\def\typo_paragraphs_start_cell_yes
  {\begingroup
   \let\typo_paragraphs_start_cell\relax
   \let\typo_paragraphs_stop_cell\typo_paragraphs_stop_cell_indeed
   \cdef\currentparagraphs{\currentparagraphs:\the\c_typo_paragraphs_n}%
   \ifnum\c_typo_paragraphs_n>\plusone
     \typo_paragraphs_separator
   \fi
   \edef\p_height{\paragraphsparameter\c!height}%
   \edef\p_width {\paragraphsparameter\c!width }%
   \useparagraphsstyleandcolor\c!style\c!color
   \setbox\scratchbox\vtop \ifempty\p_height \orelse\ifx\p_height\v!fit \else to \p_height \fi
   \bgroup % (2)
   \blank[\v!disable]%
   \paragraphsparameter\c!top
   \hsize\ifempty\p_width \d_typo_paragraphs_auto \else \p_width \fi \relax
   \usealignparameter\paragraphsparameter
   \paragraphsparameter\c!inner
   \everypar{\begstrut\everypar\emptytoks}%
   \ignorespaces
   \paragraphsparameter\c!command}

\permanent\protected\def\stopparagraphscell
  {\typo_paragraphs_stop_cell
   \let\typo_paragraphs_stop_cell\relax}

\def\typo_paragraphs_stop_cell_indeed
  {\ifnum\c_typo_paragraphs_n>\c_typo_paragraphs_max
     \expandafter\typo_paragraphs_stop_cell_nop
   \else
     \expandafter\typo_paragraphs_stop_cell_yes
   \fi}

\def\typo_paragraphs_stop_cell_nop
  {\egroup
   \endgroup}

\def\typo_paragraphs_stop_cell_yes
  {\ifvmode
     \removelastskip
   \else
     \removeunwantedspaces
     \endstrut
     \endgraf
   \fi
   \paragraphsparameter\c!bottom
   \egroup % (2)
   \dontleavehmode\hpack{\raise\strutheight\box\scratchbox}%
   \endgroup}

\def\typo_paragraphs_separator
  {\scratchdistance{\paragraphsparameter\c!distance}%
   \ifcstok{\paragraphsparameter\c!rule}\v!on
     \scratchwidth{\paragraphsparameter\c!rulethickness}%
     \scratchdistance{(\scratchdistance-\scratchwidth)/2}%
     \hskip\scratchdistance
     \color[\paragraphsparameter\c!rulecolor]{\vrule\s!width\scratchwidth}%
   \fi
   \hskip\scratchdistance}

\protect \endinput