| 1 |
%% memoize-code.sty |
| 2 |
%% |
| 3 |
%% This file is a part of Memoize, a TeX package for externalization of |
| 4 |
%% graphics and memoization of compilation results in general, available at |
| 5 |
%% https://ctan.org/pkg/memoize and https://github.com/sasozivanovic/memoize. |
| 6 |
%% |
| 7 |
%% Copyright (c) 2020- Saso Zivanovic <[email protected]> |
| 8 |
%% (Sa\v{s}o \v{Z}ivanovi\'{c}) |
| 9 |
%% |
| 10 |
%% This work may be distributed and/or modified under the conditions of the |
| 11 |
%% LaTeX Project Public License, either version 1.3c of this license or (at |
| 12 |
%% your option) any later version. The latest version of this license is in |
| 13 |
%% https://www.latex-project.org/lppl.txt and version 1.3c or later is part of |
| 14 |
%% all distributions of LaTeX version 2008 or later. |
| 15 |
%% |
| 16 |
%% This work has the LPPL maintenance status `maintained'. |
| 17 |
%% The Current Maintainer of this work is Saso Zivanovic. |
| 18 |
%% |
| 19 |
%% The files belonging to this work and covered by LPPL are listed in |
| 20 |
%% (<texmf>/doc/generic/memoize/)FILES. |
| 21 |
|
| 22 |
\ProvidesPackage{memoize-code} |
| 23 |
\RequirePackage{memoize-doc-common} |
| 24 |
|
| 25 |
\def\TikZ;{{\upshape Ti\textit{k}Z}} |
| 26 |
\def\PGF;{PGF} |
| 27 |
|
| 28 |
% Typesetting the code & indentation |
| 29 |
%\MacroIndent=0pt |
| 30 |
\AddToHook{begindocument}{\MacroIndent=0pt } |
| 31 |
\MacroTopsep = 0pt |
| 32 |
\MacrocodeTopsep = 7pt plus 2pt minus 2pt |
| 33 |
\AddToHook{env/macrocode/before}{\macrocodepar}% |
| 34 |
\let\macrocodepar\par |
| 35 |
|
| 36 |
% A mark for extraction into the manual. We need to remove some vertical space |
| 37 |
% when this environment occurs alone in a doc block. |
| 38 |
\newenvironment{listingregion}[1]{% |
| 39 |
\ifvmode |
| 40 |
\let\macrocodepar\relax |
| 41 |
\AddToHookNext{env/macrocode/begin}{\MacrocodeTopsep=0pt\relax}% |
| 42 |
\def\listingregionskip{\vspace{-\baselineskip}}% |
| 43 |
\listingregionskip |
| 44 |
\else |
| 45 |
\def\listingregionskip{}% |
| 46 |
\fi |
| 47 |
}{\listingregionskip} |
| 48 |
|
| 49 |
\setlist{nosep} |
| 50 |
|
| 51 |
% Paragraph headings in the left margin, same as macro environments. |
| 52 |
\RenewDocumentCommand\paragraph{s t. m}{% |
| 53 |
\setbox0=\hbox{% |
| 54 |
\mbox{% |
| 55 |
\textcolor{blue}{% |
| 56 |
#3% |
| 57 |
\IfBooleanT{#1}{\mbox{\ ---}}% |
| 58 |
\IfBooleanT{#2}{\hspace{0.7em}}% |
| 59 |
}% |
| 60 |
}% |
| 61 |
}% |
| 62 |
\noindent |
| 63 |
\ifdim\wd0>\marginparwidth |
| 64 |
\hangindent\dimexpr\wd0-\marginparwidth\relax |
| 65 |
\hangafter-1\relax |
| 66 |
\fi |
| 67 |
\marginpar{\ifdim\wd0<\marginparwidth\hfill\fi\box0}% |
| 68 |
\AddToHookNext{env/macrocode/before}{\par\nohang}% |
| 69 |
\pgf@keys@utilifnextchar\par{\myparagraph@gobblepar}{\ignorespaces}% |
| 70 |
} |
| 71 |
\long\def\myparagraph@gobblepar#1{\ignorespaces} |
| 72 |
|
| 73 |
\def\macrocodeenvname{macrocode} |
| 74 |
\def\MacroFont{\fontencoding\encodingdefault |
| 75 |
\fontfamily\ttdefault |
| 76 |
\fontseries\mddefault |
| 77 |
\fontshape\shapedefault |
| 78 |
\small |
| 79 |
\ifx\@currenvir\macrocodeenvname\else\color{blue}\fi |
| 80 |
} |
| 81 |
\let\AltMacroFont\MacroFont % No special font for guarded code. |
| 82 |
|
| 83 |
% Typeset one-line guards to the left, |
| 84 |
% indent multi-line guard begin/ends by the guard level, |
| 85 |
% and let guards be orange. |
| 86 |
% Move the codeline number left. |
| 87 |
\def\Module#1{% |
| 88 |
\begingroup |
| 89 |
\IfBeginWith{#1}{*}{% |
| 90 |
\def\next{}% |
| 91 |
}{% |
| 92 |
\IfBeginWith{#1}{/}{% |
| 93 |
\def\next{}% |
| 94 |
\advance\guard@level-1 |
| 95 |
} |
| 96 |
{% |
| 97 |
\def\next##1{% |
| 98 |
\guard@level0 |
| 99 |
\setbox0=\lastbox |
| 100 |
\llap{\unhbox0 ##1}% |
| 101 |
}% |
| 102 |
}% |
| 103 |
}% |
| 104 |
\next{% |
| 105 |
\loop |
| 106 |
\ifnum\guard@level>0 |
| 107 |
\hskip 1em |
| 108 |
\advance\guard@level-1 |
| 109 |
\repeat |
| 110 |
\color{orange}\mod@math@codes$\langle\mathsf{#1}\rangle$\hspace{0.1em}% |
| 111 |
}% |
| 112 |
\endgroup |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
% doc elements |
| 117 |
\NewDocElement[idxgroup={\texttt{/mmz} keys}, idxtype={\texttt{/mmz}}]{Key}{key} |
| 118 |
\NewDocElement[idxgroup={\texttt{/collargs} keys}, idxtype={\texttt{/collargs}}]{CollargsKey}{collargskey} |
| 119 |
\NewDocElement[idxgroup={\texttt{/advice} keys}, idxtype={\texttt{/advice}}]{AdviceKey}{advicekey} |
| 120 |
\NewDocElement[idxgroup={\texttt{/mmz/auto} keys}, idxtype={\texttt{/mmz/auto}}]{MmzAutoKey}{mmzautokey} |
| 121 |
\NewDocElement[idxgroup={\texttt{/handlers} keys}, idxtype={\texttt{/handlers}}]{HandlersKey}{handlerskey} |
| 122 |
\NewDocElement[idxgroup={\texttt{/advice/install} keys}, idxtype={\texttt{/advice/install}}]{AdviceInstallkey}{adviceinstallkey} |
| 123 |
|
| 124 |
% Funny: without repeating this here, I can't use commas to separate several macros. |
| 125 |
\RenewDocElement[macrolike=true, idxtype=,idxgroup=,printtype=]{Macro}{macro} |
| 126 |
\NewDocElement[idxtype={Lua function}, idxgroup=Lua functions]{LuaFunction}{luafun} |
| 127 |
|
| 128 |
|
| 129 |
\newcommand\PrintMainMacro[1]{{\MacroFont\string#1}\SpecialMainMacroIndex{#1}} |
| 130 |
|
| 131 |
\newcommand\MyIndex[3]{% |
| 132 |
\global\advance\c@CodelineNo 1 |
| 133 |
\csuse{special@index}{#1\actualchar#2\encapchar#3}% |
| 134 |
\global\advance\c@CodelineNo -1 |
| 135 |
\ignorespaces |
| 136 |
} |
| 137 |
\NewDocumentCommand\exclamationmark{}{!} |
| 138 |
|
| 139 |
|
| 140 |
% hanging indent for "key" and "macro" environments |
| 141 |
\newcommand\hang[2]{% |
| 142 |
\setbox0=\hbox{\MacroFont \string#2\relax}% "\relax" is short enough to never trigger a hanging indent |
| 143 |
\ifdim\wd0>\dimexpr\marginparwidth\relax |
| 144 |
\hangindent\dimexpr\wd0-\marginparwidth+\hang@next@extra\relax |
| 145 |
\hangafter-#1\relax |
| 146 |
\gdef\hang@next@extra{0pt}% |
| 147 |
\AddToHookNext{env/macrocode/before}{\par\nohang}% |
| 148 |
\AddToHookNext{para/after}{\aftergroup\nohang}% |
| 149 |
\fi |
| 150 |
\ignorespaces |
| 151 |
} |
| 152 |
\def\AdjustNextHang#1{\gdef\hang@next@extra{#1}\ignorespaces} |
| 153 |
\gdef\hang@next@extra{0pt} |
| 154 |
\newcommand\nohang{% |
| 155 |
\hangindent 0pt\relax |
| 156 |
\gdef\hang@next@extra{0pt}% |
| 157 |
} |
| 158 |
\newcommand\indentmacrocode[1][0pt]{% |
| 159 |
\settowidth\@tempdima{\reset@font\scriptsize\arabic{CodelineNo}\ }% |
| 160 |
\setlength\ExtraMacroIndent{#1}% |
| 161 |
\addtolength\MacroIndent{\dimexpr\hangindent+\@tempdima+\ExtraMacroIndent}% |
| 162 |
} |
| 163 |
\newdimen\ExtraMacroIndent |
| 164 |
\newcommand\noindentmacrocode{% |
| 165 |
\setlength\MacroIndent{0em}% |
| 166 |
\AddToHookNext{env/macrocode/begin}{\MacrocodeTopsep 0pt\relax}% |
| 167 |
} |
| 168 |
\def\theCodelineNo{% |
| 169 |
\reset@font\scriptsize\textcolor{gray}{\arabic{CodelineNo}}% |
| 170 |
\hspace*{\dimexpr\leftskip-\MacroIndent+\ExtraMacroIndent}}% |
| 171 |
|
| 172 |
% and now ... autohang! |
| 173 |
% (the macros should be listed without spaces after commas) |
| 174 |
\usepackage{advice} |
| 175 |
\pgfkeys{ |
| 176 |
/my/.cd, |
| 177 |
.install advice, |
| 178 |
advice/hang/.style={outer handler=\autohang}, |
| 179 |
advice=\key{hang}, |
| 180 |
advice=\macro{hang}, |
| 181 |
advice=\autokey{hang}, |
| 182 |
advice=\mmzautokey{hang}, |
| 183 |
advice=\advicekey{hang}, |
| 184 |
advice=\collargskey{hang}, |
| 185 |
advice=\handlerskey{hang}, |
| 186 |
advice=\adviceinstallkey{hang}, |
| 187 |
advice=\luafun{hang}, |
| 188 |
} |
| 189 |
\def\hangn{0} |
| 190 |
\def\hanglength{0} |
| 191 |
\def\hangstring{} |
| 192 |
\newcommand\autohang[2][]{% |
| 193 |
\ifstrequal{#1}{noprint}{}{% |
| 194 |
\def\temp{#2}% |
| 195 |
\@for\hangtemp:=\temp\do{\computehang}% |
| 196 |
\edef\temp{\noexpand\hang{\hangn}{\expandonce\hangstring}}\temp |
| 197 |
}% |
| 198 |
\AdviceOriginal[#1]{#2}% |
| 199 |
} |
| 200 |
\def\computehang{% |
| 201 |
\StrLen{\expandafter\string\hangtemp}[\temp]% |
| 202 |
\edef\hangn{\the\numexpr\hangn+1} |
| 203 |
\ifnum\temp>\hanglength\relax |
| 204 |
\let\hanglength\temp |
| 205 |
\let\hangstring\hangtemp |
| 206 |
\fi |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
%%% Various: |
| 212 |
\newcommand\MS{\textsuperscript{M}\S} % manual section |
| 213 |
|
| 214 |
% Read in the manual .aux, for \MS cross-references. |
| 215 |
\begingroup |
| 216 |
\let\orig@newlabel\newlabel |
| 217 |
\def\newlabel#1#2{% |
| 218 |
\IfBeginWith{#1}{sec:}{\orig@newlabel{#1}{#2}}{}% |
| 219 |
} |
| 220 |
\def\doline{% |
| 221 |
\expandafter\futurelet\expandafter\firsttoken\expandafter\dolinei\line\fi} |
| 222 |
\def\dolinei{\ifx\firsttoken\newlabel}% |
| 223 |
\openin0=memoize-doc.aux |
| 224 |
\loop |
| 225 |
\unless\ifeof0 |
| 226 |
\read0 to \line |
| 227 |
\doline |
| 228 |
\repeat |
| 229 |
\closein0 |
| 230 |
\endgroup |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
%%%% Filter the index, keeping only the entries with a corresponding "main" |
| 235 |
%%%% entry. |
| 236 |
|
| 237 |
\AddToHook{enddocument/end}{% |
| 238 |
\immediate\closeout\@indexfile % it is never explicitly closed! |
| 239 |
\filterindex |
| 240 |
} |
| 241 |
|
| 242 |
\newwrite\tempidx |
| 243 |
\def\filterindex{% |
| 244 |
\IfFileExists{\jobname.idx}{% |
| 245 |
\begingroup |
| 246 |
\catcode`\%=12 |
| 247 |
\def\indexentry{\filterindex@parse\filterindex@first@pass}% |
| 248 |
\immediate\openout\tempidx \jobname.idx.orig |
| 249 |
\input \jobname.idx |
| 250 |
\immediate\closeout\tempidx |
| 251 |
\def\indexentry{\filterindex@parse\filterindex@second@pass}% |
| 252 |
\immediate\openout\tempidx \jobname.idx |
| 253 |
% \tracingall |
| 254 |
\input \jobname.idx.orig |
| 255 |
\immediate\closeout\tempidx |
| 256 |
\endgroup |
| 257 |
}{}% |
| 258 |
} |
| 259 |
|
| 260 |
\def\filterindex@parse{% |
| 261 |
\catcode`\\=12 |
| 262 |
\filterindex@parse@i |
| 263 |
} |
| 264 |
\long\def\filterindex@parse@i#1#2#3{\filterindex@parse@ii#1#2{#3}} |
| 265 |
\long\def\filterindex@parse@ii#1#2=#3hdclindex#4#5#6{% |
| 266 |
\catcode`\\=0 |
| 267 |
#1{#2}{#3}{#4}{#5}{#6}% |
| 268 |
} |
| 269 |
|
| 270 |
\long\def\filterindex@first@pass#1#2#3#4#5{% |
| 271 |
\filterindex@write{#1}{#2}{#3}{#4}{#5}% |
| 272 |
\IfStrEq{#4}{main}{% |
| 273 |
\csdef{\detokenize{filt@#1}}{}% |
| 274 |
}{}% |
| 275 |
} |
| 276 |
\long\def\filterindex@second@pass#1#2#3#4#5{% |
| 277 |
\ifcsdef{\detokenize{filt@#1}}{% |
| 278 |
\filterindex@write{#1}{#2}{#3}{#4}{#5}% |
| 279 |
}{% |
| 280 |
}% |
| 281 |
} |
| 282 |
\long\def\filterindex@write#1#2#3#4#5{% |
| 283 |
\immediate\write\tempidx{\string\indexentry\detokenize{{#1=#2hdclindex{#3}{#4}}{#5}}}% |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
% Local Variables: |
| 289 |
% TeX-engine: luatex |
| 290 |
% TeX-master: "memoize-code.tex" |
| 291 |
% TeX-auto-save: nil |
| 292 |
% End: |