\startmodule[pararef]

\unprotect

% variable and functions
%
% paragraphNumber		running number starts at every heading with 1
%
% paragraphMark[]		Marks the paragraph to make references to it 
% tmpparagraphMark{}[]		replaces temporary paragraphMark until 
%				presentheadnumber is working
% getParagraphNumber		Writes paragraphNumber (for itmize-enviorement
% startParagraph		Starts and stops a paragraph
% 	stopParagraph
% paragraphHeading{}		Defines the margin-heading of that paragraph
% crossref{}[]			common command for internal references

\definenumber[paragraphNumber]

\setuphead[chapter,section,subsection,subsubsection][after={\resetnumber[paragraphNumber]}]


\def\presentheadnumber
	{\doifelse{\headnumber[section]}{0}%
		{\headnumber[chapter]}%
		{\headnumber[section]}%
	}

\def\paragraphMark[#1]
	{\expanded{\textreference[#1]{\presentheadnumber/\getnumber[paragraphNumber]}}}

\define\getParagraphNumber
	{\incrementnumber[paragraphNumber]%
	\inothermargin{\color[grey]%
	{\tfx\getnumber[paragraphNumber]~}}
	}

\definestartstop[paragraph]
  [before=
	{\blank[medium]\incrementnumber[paragraphNumber]%
	\inothermargin{\color[grey]%
	{\tfx\getnumber[paragraphNumber]~}}},
   after={\blank[medium]}]

\def\paragraphHeading#1%
	{\inmargin{#1}%
	}

\def\crossref#1[#2]%
	{\color[grey]{#1 \in[#2]}%
	}

\protect
\stopmodule
\endinput