buff-ver.mkxl /size: 32 Kb    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=buff-ver, % was core-ver
3%D        version=2000.05.09,
4%D          title=\CONTEXT\ Buffer Macros,
5%D       subtitle=Verbatim,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14% check after-first and before-last breaks
15
16\writestatus{loading}{ConTeXt Buffer Macros / Verbatim}
17
18\registerctxluafile{buff-ver}{}
19
20\unprotect
21
22\startcontextdefinitioncode
23
24\definesystemattribute[verbatimline][public]
25
26\appendtoksonce
27    \c_attr_verbatimline\attributeunsetvalue
28\to \everyforgetall
29
30%D Initializations.
31
32\installcorenamespace{typinglines}
33\installcorenamespace{typingspace}
34\installcorenamespace{typingblank} % needs checking ... used?
35
36\newtoks\everyinitializeverbatim
37
38\appendtoks
39    \the\everyresettypesetting
40    \resetfontfeature
41    \resetcharacterspacing
42    \resetbreakpoints
43    \nohyphencollapsing
44\to \everyinitializeverbatim
45
46\permanent\protected\def\setverbatimspaceskip % to be checked: must happen after font switch
47  {\spaceskip\fontcharwd\font`x\relax
48   \xspaceskip\spaceskip}
49
50\defcsname\??typinglines\v!no        \endcsname{\buff_verbatim_ignore_hyphens}
51\defcsname\??typinglines\v!normal    \endcsname{\buff_verbatim_ignore_hyphens}
52\defcsname\??typinglines\v!yes       \endcsname{\buff_verbatim_obey_breakpoints}
53\defcsname\??typinglines\v!hyphenated\endcsname{\buff_verbatim_obey_hyphens}
54
55\defcsname\??typingspace\v!on        \endcsname{\enforced\let\obeyedspace\specialcontrolspace}
56\defcsname\??typingspace\v!stretch   \endcsname{\enforced\let\obeyedspace\specialstretchedspace}
57\letcsname\??typingspace\v!normal    \endcsname\donothing
58\defcsname\??typingspace\v!fixed     \endcsname{\enforced\let\obeyedspace\specialfixedspace}
59
60\defcsname\??typingblank\v!standard  \endcsname{\s_spac_whitespace_parskip}
61\defcsname\??typingblank\v!small     \endcsname{\smallskipamount}
62\defcsname\??typingblank\v!medium    \endcsname{\medskipamount}
63\defcsname\??typingblank\v!big       \endcsname{\bigskipamount}
64\defcsname\??typingblank\v!halfline  \endcsname{.5\baselineskip}
65\defcsname\??typingblank\v!line      \endcsname{\baselineskip}
66\defcsname\??typingblank\v!none      \endcsname{\zeropoint}
67
68\protected\def\buff_verbatim_obey_hyphens
69  {}
70
71\protected\def\buff_verbatim_obey_breakpoints
72  {\nohyphens
73   \veryraggedright}
74
75\protected\def\buff_verbatim_ignore_hyphens
76  {\nohyphens}
77
78\def\buff_verbatim_initialize_type_one
79  {\enforced\let\obeylines\ignorelines
80   \edef\p_buff_option{\typeparameter\c!option}%
81   \ifx\p_buff_option\v!none
82     \usetypestyleandcolor\c!style\c!color
83   \orelse\ifempty\p_buff_option
84     \usetypestyleandcolor\c!style\c!color
85   \else
86     \usetypestyleparameter\c!style % no color
87   \fi
88   \setcatcodetable\vrbcatcodes}
89
90\def\buff_verbatim_initialize_type_two
91  {\spaceskip.5\emwidth\relax
92   \enforced\let\obeyedspace\specialobeyedspace
93   \enforced\let\controlspace\specialcontrolspace
94   \edef\p_buff_lines{\typeparameter\c!lines}%
95   \begincsname\??typinglines\p_buff_lines\endcsname
96   \edef\p_buff_space{\typeparameter\c!space}%
97   \begincsname\??typingspace\p_buff_space\endcsname
98   \relax\the\everyinitializeverbatim\relax}
99
100\permanent\protected\def\doinitializeverbatim % for use elsewhere .. temp hack (see lxml-ini)
101  {\buff_verbatim_initialize_type_one
102   \buff_verbatim_initialize_type_two}
103
104\let\buff_verbatim_set_line_margin\relax
105
106\def\buff_verbatim_set_line_margin_indeed
107  {\hskip\doifelseoddpage{\typingparameter\c!oddmargin}{\typingparameter\c!evenmargin}\relax}
108
109\def\buff_verbatim_initialize_typing_one
110  {\usebodyfontparameter\typingparameter
111   \scratchskip\typingparameter\c!oddmargin\relax
112   \ifzeropt\scratchskip \else
113     \let\buff_verbatim_set_line_margin\buff_verbatim_set_line_margin_indeed
114   \fi
115   \scratchskip\typingparameter\c!evenmargin\relax
116   \ifzeropt\scratchskip \else
117     \let\buff_verbatim_set_line_margin\buff_verbatim_set_line_margin_indeed
118   \fi
119   \ifrelax\buff_verbatim_set_line_margin
120     \doadaptleftskip{\typingparameter\c!margin}%
121   \fi
122   \edef\p_buff_option{\typingparameter\c!option}%
123   \ifx\p_buff_option\v!none
124     \usetypingstyleandcolor\c!style\c!color
125   \orelse\ifempty\p_buff_option
126     \usetypingstyleandcolor\c!style\c!color
127   \else
128     \usetypingstyleparameter\c!style % no color !
129   \fi
130   \usealignparameter\typingparameter}
131
132\def\buff_verbatim_initialize_typing_two
133  {\spaceskip.5\emwidth\relax
134   \enforced\let\obeyedspace\specialobeyedspace
135   \enforced\let\controlspace\specialcontrolspace
136   \edef\p_buff_lines{\typingparameter\c!lines}%
137   \begincsname\??typinglines\p_buff_lines\endcsname
138   \edef\p_buff_space{\typingparameter\c!space}%
139   \begincsname\??typingspace\p_buff_space\endcsname
140   \relax\the\everyinitializeverbatim\relax}
141
142%D \macros
143%D   {definetype,setuptype}
144%D
145%D Specific inline verbatim commands can be defined with the following command.
146
147\installcorenamespace{type}
148
149\installcommandhandler \??type {type} \??type
150
151\appendtoks
152    \frozen\instance\protected\edefcsname\currenttype\endcsname{\buff_verbatim_type{\currenttype}}%
153\to \everydefinetype
154
155\appendtoks
156   \buff_verbatim_initialize_visualizer{\typeparameter\c!option}%
157\to \everysetuptype
158
159%D \macros
160%D   {definetyping,setuptyping}
161%D
162%D For most users the standard \type {\start}||\type {\stop}||pair will suffice, but
163%D for documentation purposes the next definition command can be of use:
164%D
165%D \starttyping
166%D \definetyping[extratyping][margin=3em]
167%D
168%D \startextratyping
169%D these extra ones are indented by 1 em
170%D \stopextratyping
171%D \stoptyping
172%D
173%D The definitions default to the standard typing values.
174
175\installcorenamespace{typing}
176
177\installcommandhandler \??typing {typing} \??typing
178
179\appendtoks
180    \frozen\protected\instance\edefcsname\e!start\currenttyping\endcsname{\buff_verbatim_typing_start{\currenttyping}{\currenttyping}}%
181    \frozen\protected\instance\edefcsname\e!stop \currenttyping\endcsname{\buff_verbatim_typing_stop}%
182    \ifempty\currenttypingparent
183      \normalexpanded{\definelinenumbering[\currenttyping]}%
184    \else
185      \normalexpanded{\definelinenumbering[\currenttyping][\currenttypingparent]}%
186    \fi
187\to \everydefinetyping
188
189\appendtoks
190   \buff_verbatim_initialize_visualizer{\typingparameter\c!option}% we can check at the tex end
191\to \everysetuptyping
192
193\def\buff_verbatim_initialize_visualizer#1%
194  {\ifproductionrun\clf_loadvisualizer{#1}\fi}
195
196\permanent\protected\def\doifelsevisualizer#1%
197  {\clf_doifelsevisualizer{#1}}
198
199\aliased\let\doifvisualizerelse\doifelsevisualizer
200
201%D Verbatim command are very sensitive to argument processing, which is a direct
202%D result of the \CATCODES\ being fixed at reading time.
203%D
204%D Typesetting inline verbatim can be accomplished by \type {\type}, which in this
205%D sentence was typeset by saying just \type {\type {\type}}, which in turn was
206%D typeset by \unknown. Using the normal grouping characters \type {{}} is the most
207%D natural way of using this command.
208%D
209%D A second, more or less redundant, alternative is delimiting the argument with an
210%D own character. This method was implemented in the context of a publication in the
211%D \MAPS, where this way of delimiting is recognized by \LATEX\ users.
212%D
213%D The third, more original alternative, is the one using \type {<<} and \type {>>}
214%D as delimiters. This alternative can be used in situations where slanted
215%D typeseting is needed.
216%D
217%D \macros
218%D   {type}
219%D
220%D We define \type {\type} as a protected command. This command has several
221%D invocations: grouped, wirt boundary characters, and with font switches.
222%D
223%D \starttyping
224%D normal: \par \type{xx<<..xx..<<xx <<xx>> >>..>>xx} \par \type<<....>> \par \type<<..<<xx>>..>> \par
225%D normal: \par \type{xx<..xx..<xx <slanted> >..>xx} \par \type{<....>} \par \type{<..<xx>..>}
226%D \setuptype[option=slanted]
227%D slanted: \par \type{xx<<..sl..<<xx <<sl>> xx>>..sl..>>xx} \par \type<<..xx..>> \par \type<<..<<sl>>..>> \par
228%D slanted: \par \type{xx<<..sl..<xx <sl> xx>..sl..>>xx} \par \type<<..xx..>> \par \type<<..<sl>..>> \par
229%D \setuptype[option=none]
230%D none: \par \type{xx<<..xx..<<xx <<xx>> >>..>>xx} \par \type<<....>> \par \type<<..<<xx>>..>> \par
231%D \stoptyping
232%D
233%D When writing the manual to \CONTEXT\ and documenting this source we needed to
234%D typeset \type {<<} and \type {>>}. Because we wanted to do this in the natural
235%D way, we've adapted the original definition a bit. This implementation went
236%D through several live cycles. The final implementation looks a bit further and
237%D treats the lone \type {<<} and \type {>>} a bit different. The \type {\null}
238%D prevents ligatures, which unfortunately turn up in Lucida fonts.
239%D
240%D The following lines show what happens when we set \type {option=commands}.
241%D
242%D \startbuffer
243%D \starttyping
244%D test//test test/BTEX \footnote{test test test}/ETEX test
245%D test//test test/BTEX \footnote{test test test}/ETEX  test
246%D test test test/BTEX \bf(nota bene)/ETEX test
247%D test test test /BTEX \bf(nota bene)/ETEX  test
248%D \stoptyping
249%D \stopbuffer
250%D
251%D % \bgroup\setuptyping[option=commands]\getbuffer\egroup
252%D
253%D this was keyed in as:
254%D
255%D \typebuffer
256
257% Beware, the command is supposed to do something like \framed and not manipulate
258% content i.e. it gets no argument but something b/egrouped instead:
259
260\def\buff_verbatim_left_of_type
261  {\typeparameter\c!left
262   \typeparameter\c!command\bgroup}
263
264\def\buff_verbatim_right_of_type
265  {\egroup
266   \typeparameter\c!right}
267
268\permanent\protected\def\type{\buff_verbatim_type\empty}
269\permanent\protected\def\typ {\buff_verbatim_typ \empty}
270
271\protected\def\buff_verbatim_type#1%
272  {\dontleavehmode
273   \bgroup
274   \edef\currenttype{#1}%
275   \buff_verbatim_type_indeed}
276
277\def\buff_verbatim_typ#1%
278  {\dontleavehmode
279   \bgroup
280   \edef\currenttype{#1}%
281   \lettypeparameter\c!lines\v!hyphenated
282   \enforced\let\specialobeyedspace\specialstretchedspace
283   \buff_verbatim_type_indeed}
284
285\tolerant\def\buff_verbatim_type_indeed[#1]%
286  {\ifarguments\or\setupcurrenttype[#1]\fi
287   \buff_verbatim_left_of_type
288   \futureexpand\bgroup\buff_verbatim_type_a\buff_verbatim_type_one}
289
290\def\buff_verbatim_type_one
291  {\catcode`<=\othercatcode % old precaution
292   \catcode`>=\othercatcode % old precaution
293   \futureexpandis<\buff_verbatim_type_b\buff_verbatim_type_c}
294
295\def\buff_verbatim_type_three
296  {\if\next<%
297     \expandafter\buff_verbatim_type_b
298   \else
299     \expandafter\buff_verbatim_type_c
300   \fi}
301
302\def\buff_verbatim_type_a
303  {\buff_verbatim_initialize_type_one
304   \setcatcodetable\tpacatcodes
305   \buff_verbatim_type_normal}
306
307\def\buff_verbatim_type_b#1%
308  {\buff_verbatim_initialize_type_one
309   \setcatcodetable\tpbcatcodes
310   \buff_verbatim_type_nested}
311
312\def\buff_verbatim_type_c#1%
313  {\buff_verbatim_initialize_type_one
314   \setcatcodetable\vrbcatcodes
315   \def\next##1#1{\buff_verbatim_type_normal{##1}}%
316   \next}
317
318\def\buff_verbatim_type_normal#1%
319  {\buff_verbatim_initialize_type_two
320   \dostarttaggedchained\t!verbatim\currenttype\??type
321   \clf_type
322     data    {\detokenize{#1}}%
323     tab     {\typeparameter\c!tab}%
324     method  {\p_buff_option}%
325     compact {\typeparameter\c!compact}% % none | all | last (all needed in tabulate etc for manuals)
326     escape  {\typeparameter\c!escape}%  % new but rather useless imo (escaping in general is not used much)
327   % nature  {inline}% is default
328   \relax
329   \dostoptagged
330   \buff_verbatim_right_of_type
331   \egroup}
332
333\def\buff_verbatim_type_nested#1%
334  {\buff_verbatim_initialize_type_two
335   \dostarttaggedchained\t!verbatim\currenttype\??type
336   \clf_type
337     data   {\detokenize{#1}}%
338     tab    {\typeparameter\c!tab}%
339     method {\p_buff_option}% % extra visualizer (maybe: nested,\typeparameter\c!option)
340     escape {\typeparameter\c!escape}%  % new but rather useless imo (escaping in general is not used much)
341   % nature {inline}% is default
342     method {nested}%
343   \relax
344   \dostoptagged
345   \buff_verbatim_right_of_type
346   \egroup
347   \gobbleoneargument} % grab last >
348
349%D The \type {compact} option can come in handy in the case of inline argument passing
350%D
351%D \starttyping
352%D \definetype[TeXcodeA][option=TEX]
353%D \definetype[TeXcodeB][option=TEX,compact=all]
354%D \definetype[TeXcodeC][option=TEX,compact=absolute]
355%D
356%D \def\argA#1{\TeXcodeA{{#1}}}
357%D \def\argB#1{\TeXcodeB{{#1}}}
358%D \def\argC#1{\TeXcodeC{{#1}}}
359%D \stoptyping
360
361%D \macros
362%D   {obeyhyphens,obeybreakpoints}
363%D
364%D Although it's not clear from the macros, one character trait of this macros,
365%D which are build on top of the support module, is that they don't hyphenate. We
366%D therefore offer the alternative \type{\typ}. The current implementation works all
367%D right, but a decent hyphenation support of \type{\tt} text will be implemented
368%D soon.
369
370\permanent\protected\def\specialfixedspace     {\kern\interwordspace\relax}
371\permanent\protected\def\specialobeyedspace    {\hskip\interwordspace\relax} % better than spaceskip
372\permanent\protected\def\specialstretchedspace {\hskip.5\interwordspace\s!plus.125\interwordspace\relax} % \interwordstretch can be zero
373\permanent\protected\def\specialcontrolspace   {\normalcontrolspace\allowbreak} % uses fallback
374
375\permanent\protected\def\explicitfixedspace    {\asciispacechar}
376\permanent\protected\def\explicitobeyedspace   {\asciispacechar\allowbreak}
377\permanent\protected\def\explicitstretchedspace{\asciispacechar\hskip\zeropoint\s!plus.125\interwordspace\relax}%
378\permanent\protected\def\explicitcontrolspace  {\optionalcontrolspace\allowbreak} % uses asciispace
379
380\appendtoks
381    \enforced\protected\def\obeyedspace{\hskip\zeropoint\relax\asciispacechar\hskip\zeropoint\relax}%
382\to \everyenableelements
383
384\permanent\protected\def\obeyhyphens
385  {\enforced\let\obeyedspace \specialobeyedspace % maybe \specialstretchedspace
386   \enforced\let\controlspace\specialcontrolspace
387   \spaceskip.25\emwidth\relax} % hm a bit of stretch !
388
389\permanent\protected\def\obeybreakpoints
390  {\ignorehyphens
391   \veryraggedright}
392
393\permanent\protected\def\ignorehyphens
394  {\nohyphens
395   \enforced\let\obeyedspace \specialobeyedspace
396   \enforced\let\controlspace\specialcontrolspace
397   \spaceskip.5\emwidth\relax}
398
399%D \macros
400%D   {tex,arg,mat,dis,astype}
401%D
402%D Sometimes, for instance when we pass verbatim text as an argument, the fixed
403%D \CATCODES\ interfere with our wishes. An experimental implementation of character
404%D by character processing of verbatim text did overcome this limitation, but we've
405%D decided not to use that slow and sometimes troublesome solution. Instead we stick
406%D to some 'old' \CONTEXT\ macros for typesetting typical \TEX\ characters.
407
408\immutable\def\lesscharacter    {<} % obsolete
409\immutable\def\morecharacter    {>} % obsolete
410
411\immutable\let\texescape        \textbackslash
412\immutable\let\leftargument     \textbraceleft
413\immutable\let\rightargument    \textbraceright
414\immutable\let\inlinemathmarker \textdollar
415\immutable\def\displaymathmarker{\textdollar\textdollar}
416
417\def\buff_verbatim_special_type#1#2#% # gobbles spaces
418  {\dontleavehmode\bgroup
419   \buff_verbatim_initialize_type_one
420   \catcode\leftbraceasciicode \begingroupcatcode
421   \catcode\rightbraceasciicode\endgroupcatcode
422   #1%
423   \bgroup % else wrong font for #2
424   \aftergroup#2%
425   \aftergroup\egroup
426   \let\nexttoken}
427
428\mutable\protected\def\tex{\buff_verbatim_special_type\texescape        \relax}
429\mutable\protected\def\arg{\buff_verbatim_special_type\leftargument     \rightargument}
430\mutable\protected\def\mat{\buff_verbatim_special_type\inlinemathmarker \inlinemathmarker}
431\mutable\protected\def\dis{\buff_verbatim_special_type\displaymathmarker\displaymathmarker}
432
433\aliased\let\normaltexttex\tex
434\aliased\let\normaltextarg\arg
435\aliased\let\normaltextmat\mat
436\aliased\let\normaltextdis\dis
437
438\permanent\protected\def\astype
439  {\dontleavehmode
440   \bgroup\usetypestyleandcolor\c!style\c!color\let\nexttoken}
441
442\permanent\protected\def\asciistr#1% used in some old styles
443  {\dontleavehmode\begingroup
444   \dostarttagged\t!verbatim\empty
445   \usetypestyleandcolor\c!style\c!color\detokenize{#1}%
446   \dostoptagged
447   \endgroup}
448
449%D The basic display verbatim commands are defined in an indirect way. As we
450%D will see, they are a specific case of a more general mechanism.
451
452\mutable\let\beginofverbatimlines\relax % hooks
453\mutable\let\endofverbatimlines  \relax % hooks
454
455\protected\def\buff_verbatim_typing_start#1#2% tricky non standard lookahead
456  {\begingroup
457   \edef\currenttyping       {#1}%
458   \edef\currenttypingwrapper{#2}%
459   \obeylines
460   \futureexpandis[\buff_verbatim_typing_start_yes\buff_verbatim_typing_start_nop}
461
462\def\buff_verbatim_typing_start_yes[#1]%
463  {\ifcondition\validassignment{#1}%
464     \setupcurrenttyping[#1]%
465   \else
466     \doif\v!continue{#1}{\lettypingparameter\c!continue\v!yes}%
467   \fi
468   \typingparameter\c!before\relax % moved down
469   \startpacked[\v!blank]%         % moved down
470   \buff_verbatim_setup_line_numbering
471   \buff_verbatim_initialize_typing_one
472   \buff_verbatim_setup_keep_together
473   \normalexpanded{\buff_verbatim_type_block{\e!start\currenttypingwrapper}{\e!stop\currenttypingwrapper}}}
474
475\def\buff_verbatim_typing_start_nop
476  {\typingparameter\c!before\relax
477   \startpacked[\v!blank]%
478   \buff_verbatim_setup_line_numbering
479   \buff_verbatim_initialize_typing_one
480   \buff_verbatim_setup_keep_together
481   \normalexpanded{\buff_verbatim_type_block{\e!start\currenttypingwrapper}{\e!stop \currenttypingwrapper}}}
482
483\protected\def\buff_verbatim_setup_keep_together
484  {\edef\p_keeptogether{\typingparameter\c!keeptogether}%
485   \ifx\p_keeptogether\v!yes
486     \settrue \c_buff_optimize_linebreaks
487   \else
488     \setfalse\c_buff_optimize_linebreaks
489   \fi}
490
491\protected\def\buff_verbatim_type_block#1#2%
492  {\edef\p_strip{\typingparameter\c!strip}%
493   \normalexpanded{\buff_pickup
494     {_typing_}%
495     {#1}%
496     {#2}%
497     {}%
498     {\buff_verbatim_type_block_verbatim_indeed{#1}{#2}}%
499     \ifx\p_strip\v!no\zerocount\else\plusone\fi}}
500
501\protected\def\buff_verbatim_type_block_verbatim_indeed#1#2%
502  {\buff_verbatim_initialize_typing_two
503   \dostarttaggedchained\t!verbatimblock\currenttyping\??typing
504   \beginofverbatimlines
505   \dostarttagged\t!verbatimlines\empty
506   \clf_typebuffer
507     name    {_typing_}%
508     strip   {\typingparameter\c!strip}%
509     range   {\typingparameter\c!range}%
510     tab     {\typingparameter\c!tab}%
511     method  {\p_buff_option}%
512     escape  {\typingparameter\c!escape}%
513     nature  {display}%
514   \relax
515   \dostoptagged
516   \endofverbatimlines
517   \dostoptagged
518   \begincsname#2\endcsname}
519
520\protected\def\buff_verbatim_typing_stop
521  {\stoppacked
522   \typingparameter\c!after\relax
523   \useindentnextparameter\typingparameter
524   \endgroup
525   \dorechecknextindentation}
526
527\permanent\protected\def\startnamedtyping[#1]%
528  {\buff_verbatim_typing_start{#1}\e!namedtyping}
529
530\permanent\let\stopnamedtyping\buff_verbatim_typing_stop
531
532%D Line numbering for files is combined with filtering, while display verbatim has
533%D the ability to continue.
534%D
535%D \starttyping
536%D \typefile[numbering=file,start=10,stop=12]{test.tex}
537%D
538%D \definetyping[code][numbering=line]
539%D
540%D \starttext
541%D \startcode
542%D ...
543%D ...
544%D \stopcode
545%D
546%D \startcode[start=continue]
547%D ...
548%D ...
549%D \stopcode
550%D
551%D \startcode[start=10]
552%D ...
553%D \stopcode
554%D \stoptyping
555
556%D Ranges:
557%D
558%D \starttyping
559%D % label:start:one
560%D
561%D \def\MyMacro
562%D   {just an example}
563%D
564%D % label:stop:one
565%D
566%D \starttext
567%D
568%D % \typefile[file][range={3,6}] {whatever.tex}
569%D % \typefile[file][range={3,+2}]{whatever.tex}
570%D
571%D \typefile[file][range={label:start:one,label:stop:one}]{whatever.tex}
572%D
573%D \stoptext
574%D \stoptyping
575
576%D \macros
577%D   {typefile}
578%D
579%D Typesetting files verbatim (for the moment) only supports colorization of \TEX\
580%D sources as valid option. The other setup values are inherited from display
581%D verbatim. The implementation of \type {\typefile} is straightforward:
582
583% [category] [settings] {name} % for historic reasons, all filenames are {}
584
585\mutable\let\currenttypingfile\empty
586
587\appendtoks
588    \frozen\protected\instance\edefcsname\e!type\currenttyping\v!file\endcsname{\typefile[\currenttyping]}%
589\to \everydefinetyping
590
591\permanent\tolerant\protected\def\typefile[#1]#*[#2]#:#3%
592  {\begingroup
593   \ifparameter#2\or
594     \setuptyping[#1][#2]%
595     \buff_verbatim_type_file_checked{#1}{#3}%
596   \orelse\ifparameter#1\or
597     \ifhastok={#1}%
598       \setuptyping[\v!file][#1]%
599       \buff_verbatim_type_file_checked\v!file{#3}%
600     \else
601       \buff_verbatim_type_file_checked{#1}{#3}%
602     \fi
603   \else
604     \buff_verbatim_type_file_checked\v!file{#3}%
605   \fi
606   \useindentnextparameter\typingparameter % needs checking
607   \endgroup
608   \dorechecknextindentation} % needs checking
609
610\def\buff_verbatim_type_file_checked#1#2%
611  {\doifelsetypingfile{#2}
612     {\buff_verbatim_type_file_indeed{#1}\currenttypingfile}
613     {\showmessage\m!verbatims1{#2}}}
614
615\permanent\protected\def\doifelsetypingfile#1%
616  {\edef\currenttypingfile{\locfilename{#1}}%
617   \ifempty\currenttypingfile
618     \edef\currenttypingfile{\locfilename{#1.tex}}% downward compatible
619   \fi
620   \ifempty\currenttypingfile
621     \expandafter\secondoftwoarguments
622   \else
623     \expandafter\firstoftwoarguments
624   \fi}
625
626\aliased\let\doiftypingfileelse\doifelsetypingfile
627
628\def\buff_verbatim_type_file_indeed#1#2% category name
629  {\edef\currenttyping{#1}%
630   \typingparameter\c!before\relax
631   \startpacked[\v!blank]
632   \buff_verbatim_setup_line_numbering
633   \buff_verbatim_initialize_typing_one
634   \buff_verbatim_initialize_typing_two
635   \dostarttaggedchained\t!verbatimblock\currenttyping\??typing
636   \beginofverbatimlines
637   \dostarttagged\t!verbatimlines\empty
638   \clf_typefile
639      name   {#2}%
640      strip  {\typingparameter\c!strip}%
641      range  {\typingparameter\c!range}%
642      regime {\currentregime}%
643      tab    {\typingparameter\c!tab}%
644      method {\p_buff_option}%
645      nature {display}%
646   \relax
647   \dostoptagged
648   \endofverbatimlines
649   \dostoptagged
650   \stoppacked
651   \typingparameter\c!after\relax}
652
653%D Line numbering:
654
655\newconditional\verbatimnumberinglines
656
657\permanent\protected\def\beginofverbatimlinenumbering
658  {\startlinenumbering
659     [\currenttyping]%
660     [\c!continue=\typingparameter\c!continue,
661        \c!method=\v!type,
662         \c!start=\typingparameter\c!start,
663          \c!stop=\typingparameter\c!stop, % ?
664          \c!step=\typingparameter\c!step]}
665
666\permanent\protected\def\endofverbatimlinenumbering
667  {\stoplinenumbering}
668
669\def\buff_verbatim_setup_line_numbering
670  {\ifdefined\startlinenumbering
671     \edef\p_numbering{\typingparameter\c!numbering}%
672     \ifx\p_numbering\v!file
673       \setuplinenumbering[\currenttyping][\c!method=\v!file]%
674       \settrue\verbatimnumberinglines
675     \orelse\ifx\p_numbering\v!line
676       \settrue\verbatimnumberinglines
677     \else
678       \setfalse\verbatimnumberinglines
679     \fi
680     \ifconditional\verbatimnumberinglines
681       \enforced\let\beginofverbatimlines\beginofverbatimlinenumbering
682       \enforced\let\endofverbatimlines  \endofverbatimlinenumbering
683     \fi
684   \fi}
685
686%D \macros
687%D   {filename}
688%D
689%D Typesetting filenames in monospaced fonts is possible with
690%D
691%D \starttyping
692%D \filename{here/there/filename.suffix}
693%D \stoptyping
694%D
695%D The definition is not that spectacular.
696
697\permanent\protected\def\filename#1{{\tttf\hyphenatedfilename{#1}}}
698
699%D \macros
700%D   {verbatim}
701%d
702%D And a bonus macro, an unexpanded detokenize:
703
704\permanent\protected\def\verbatim#1{\detokenize{#1}}
705
706%D The setups for display verbatim and file verbatim are shared. One can adapt the
707%D extra defined typing environments, but they also default to the values below.
708%D Watch the alternative escape character.
709
710% \tttf   gives problems with {\tx \type...}
711% \tt\tf  does not adapt to e.g. \bf
712
713\setuptyping
714  [\c!before=\blank,
715   \c!after=\blank,
716  %\c!keeptogether=yes, % this will become default (when tested enough)
717  %\c!bodyfont=,
718  %\c!color=,
719   \c!space=\v!off,
720   \c!lines=\v!no,
721   \c!page=\v!no,
722   \c!tab=\v!yes, % what is this: \s!ascii ?
723   \c!option=\v!none,
724   \c!text=\v!no,
725   \c!style=\tt,
726   \c!indentnext=\v!yes,
727   \c!margin=\zeropoint,
728   \c!evenmargin=\zeropoint,
729   \c!oddmargin=\zeropoint,
730   \c!blank=\v!line,
731  %\c!escape=, % yes | no | {START,STOP} | default when yes: {BTEX,ETEX}
732   \c!numbering=\v!no,
733  %\c!range=,
734   \c!start=1,
735  %\c!stop=,
736   \c!step=1,
737  %\c!continue=,
738   \c!strip=\v!no] % auto or number
739
740\definetyping[\v!typing]
741
742% the \zeropoint forces the number to the margin but also works ok in text
743
744\setuplinenumbering[\v!typing][\c!location=\v!left,\c!width=\zeropoint]
745
746\setuptyping       [\v!file]  [\s!parent=\??typing       \v!typing] % we don't want \start..\stop overload
747\setuplinenumbering[\v!file]  [\s!parent=\??linenumbering\v!typing]
748
749\setuptyping       [\v!buffer][\s!parent=\??typing       \v!file]   % we don't want \start..\stop overload
750\setuplinenumbering[\v!buffer][\s!parent=\??linenumbering\v!file]
751
752%D The setups for inline verbatim default to:
753
754\setuptype
755  [\c!space=\v!off,
756   \c!lines=\v!no,
757  %\c!color=,
758   \c!style=\tt,
759  %\c!option=\v!normal,
760   \c!page=\v!no,
761   \c!tab=\v!yes,
762   \c!compact=\v!all]
763
764%D Buffers
765
766% [name] [settings] | [name] | [settings]
767
768\permanent\protected\def\typebuffer
769  {\begingroup
770   \let\currenttyping\v!buffer
771   \buff_verbatim_type_buffer}
772
773\permanent\protected\def\typedefinedbuffer[#1]%
774  {\buff_verbatim_type_defined_buffer[\v!buffer][\thedefinedbuffer{#1}]}%
775
776\appendtoks
777    \frozen\instance\setuevalue{\e!type\currentbuffer}{\buff_verbatim_type_defined_buffer[\v!buffer][\currentdefinedbuffer]}%
778\to \everydefinebuffer
779
780\appendtoks % \e!buffer
781    \frozen\instance\setuevalue{\e!type\currenttyping\v!buffer}{\buff_verbatim_type_buffer_class{\currenttyping}}%
782\to \everydefinetyping
783
784\tolerant\protected\def\buff_verbatim_type_buffer[#1]#*[#2]%
785  {\ifarguments
786     \buff_verbatim_type_buffer_indeed\currenttyping\empty% []
787   \or
788     \ifhastok={#1}%
789       \setupcurrenttyping[#1]%
790       \buff_verbatim_type_buffer_indeed\currenttyping\empty% [settings]
791     \else
792       \processcommalist[#1]{\buff_verbatim_type_buffer_indeed\currenttyping}% [name]
793     \fi
794   \or
795     \setupcurrenttyping[#2]%
796     \processcommalist[#1]{\buff_verbatim_type_buffer_indeed\currenttyping}% [name] [settings]
797   \fi
798   \useindentnextparameter\typingparameter
799   \endgroup
800   \dorechecknextindentation}
801
802\protected\def\buff_verbatim_type_buffer_class#1%
803  {\begingroup
804   \edef\currenttyping{#1}%
805   \buff_verbatim_type_buffer}
806
807\tolerant\protected\def\buff_verbatim_type_defined_buffer[#1]#*[#2]#*[#3]% category name settings
808  {\begingroup
809   \ifarguments\or\or\or
810     \setuptyping[#1][#3]%
811   \fi
812   \buff_verbatim_type_buffer_indeed{#1}{#2}%
813   \useindentnextparameter\typingparameter
814   \endgroup
815   \dorechecknextindentation}
816
817\protected\def\buff_verbatim_type_buffer_indeed#1#2% category name
818  {\edef\currenttyping{#1}%
819   \typingparameter\c!before\relax
820   \startpacked[\v!blank]
821   \buff_verbatim_setup_line_numbering
822   \buff_verbatim_initialize_typing_one
823   \buff_verbatim_initialize_typing_two
824   \dostarttaggedchained\t!verbatimblock{#1}\??typing
825   \beginofverbatimlines
826   \dostarttagged\t!verbatimlines\empty
827   \clf_typebuffer
828     name   {#2}%
829     strip  {\typingparameter\c!strip}%
830     range  {\typingparameter\c!range}%
831     regime {\currentregime}%
832     tab    {\typingparameter\c!tab}%
833     method {\p_buff_option}%
834     escape {\typingparameter\c!escape}%
835     nature {display}%
836   \relax
837   \dostoptagged
838   \endofverbatimlines
839   \dostoptagged
840   \stoppacked
841   \typingparameter\c!after\relax}
842
843\permanent\tolerant\protected\def\typeinlinebuffer[#1]#,[#2]%
844  {\dontleavehmode
845   \begingroup
846   \enforced\let\setupcurrenttyping\setupcurrenttype % a terrible hack but it saves code
847   \let\currenttype\empty
848   \ifparameters
849     \buff_verbatim_type_buffer_indeed_inline\currenttyping\empty% []
850   \or
851     \ifhastok={#1}%
852       \setupcurrenttyping[#1]%
853       \buff_verbatim_type_buffer_indeed_inline\currenttyping\empty% [settings]
854     \else
855       \processcommalist[#1]{\buff_verbatim_type_buffer_indeed_inline\currenttyping}% [name]
856     \fi
857   \or
858     \setupcurrenttyping[#2]%
859     \processcommalist[#1]{\buff_verbatim_type_buffer_indeed_inline\currenttyping}% [name] [settings]
860   \fi
861   \endgroup}
862
863\protected\def\buff_verbatim_type_buffer_indeed_inline#1#2% category name
864  {\edef\currenttype{#1}%
865   \buff_verbatim_initialize_type_one
866   \dostarttaggedchained\t!verbatim\currenttype\??type
867   \clf_typebuffer
868     name   {#2}%
869     strip  {\v!yes}%
870     regime {\currentregime}%
871     method {\p_buff_option}%
872     nature {inline}% is default anyway
873   \relax
874   \dostoptagged}
875
876% get     : before/after of buffer
877% typing  : before/after of typing
878% process : no before/after (special case anyway)
879
880% the next one is obsolete (bad name and used nowhere) so if it stays
881% the name will change
882
883\permanent\tolerant\protected\def\processbuffer[#1]#*[#2]%
884  {\writestatus{nota bene}{\string\processbuffer\space is obsolete}%
885   \begingroup
886   \ifarguments
887     \buff_verbatim_process_indeed\v!buffer\empty% []
888   \or
889    %\ifcondition\validassignment{#1}%
890     \ifhastok={#1}%
891       \setuptyping[\v!buffer][#1]%
892       \buff_verbatim_process_indeed\v!buffer\empty% [settings]
893     \else
894       \processcommalist[#1]{\buff_verbatim_process_indeed\v!buffer}% [name]
895     \fi
896   \or
897     \setuptyping[\v!buffer][#2]%
898     \processcommalist[#1]{\buff_verbatim_process_indeed\v!buffer}% [name] [settings]
899   \fi
900   \endgroup}
901
902\def\buff_verbatim_process_indeed#1#2%
903  {\edef\currenttyping{#1}%
904   \clf_processbuffer
905     name   {#2}%
906     strip  {\typingparameter\c!strip}%
907     tab    {\typingparameter\c!tab}%
908   % method {\p_buff_option}%
909     nature {direct}%
910   \relax}
911
912% so far for obsolete
913
914% line numbering, keep broken lines together
915
916\newcount\nofverbatimlines
917
918\protected\def\buff_verbatim_begin_of_line
919  {\ifconditional\verbatimnumberinglines
920     \global\advance\nofverbatimlines\plusone
921     \c_attr_verbatimline\nofverbatimlines
922   \fi
923   \noindent
924   \buff_verbatim_set_line_margin
925   \the\everyline % maybe also after starttagged
926   \strut % after starttagged, else break !
927   \dostarttagged\t!verbatimline\empty}
928
929\protected\def\buff_verbatim_end_of_line
930  {\dostoptagged
931   \obeyedline % still needed?
932   \par
933   \ifconditional\verbatimnumberinglines
934     \c_attr_verbatimline\attributeunsetvalue
935   \fi}
936
937\protected\def\buff_verbatim_empty_line
938  {\dostoptagged
939   \ifconditional\verbatimnumberinglines
940     \par\strut\par % this will be an option where we use a signal instead of a strut
941   \else
942     \blank[\typingparameter\c!blank]%
943   \fi
944   \dostarttagged\t!verbatimlines\empty}
945
946% hooks (todo: frozen):
947
948\newcount      \c_buff_verbatim_noflines
949\newcount      \c_buff_verbatim_current
950\newconditional\c_buff_optimize_linebreaks
951
952\permanent\protected\def\doverbatimspace           {\obeyedspace}
953
954\permanent\protected\def\doinlineverbatimstart     {}
955\permanent\protected\def\doinlineverbatimstop      {}
956\permanent\protected\def\doinlineverbatimbeginline {}
957\permanent\protected\def\doinlineverbatimnewline   {\obeyedspace}
958\permanent\protected\def\doinlineverbatimemptyline {\obeyedspace}
959
960\permanent\protected\def\dodisplayverbatimstart    {\advance\c_buff_verbatim_current\plusone
961                                                    \buff_verbatim_begin_of_line}
962\permanent\protected\def\dodisplayverbatimstop     {\buff_verbatim_end_of_line}
963\permanent\protected\def\dodisplayverbatimbeginline{\advance\c_buff_verbatim_current\plusone
964                                                    \buff_verbatim_begin_of_line}
965\permanent\protected\def\dodisplayverbatimnewline  {\buff_verbatim_end_of_line
966                                                    \par
967                                                    \ifconditional\c_buff_optimize_linebreaks
968                                                      \buff_verbatim_inject_breaks
969                                                    \fi}
970\permanent\protected\def\dodisplayverbatimemptyline{\buff_verbatim_empty_line}
971
972\protected\def\buff_verbatim_inject_breaks
973  {\ifnum\c_buff_verbatim_current=\plusone
974     \nobreak
975   \orelse\ifnum\c_buff_verbatim_noflines=\plusthree
976     \nobreak
977   \orelse\ifnum\c_buff_verbatim_current=\numexpr\c_buff_verbatim_noflines-\plusone\relax
978     \nobreak
979   \fi}
980
981\permanent\protected\def\dodisplayverbatiminitialize#1%
982  {\forgetparindent % maybe more
983   \c_buff_verbatim_noflines#1\relax
984   \c_buff_verbatim_current\zerocount}
985
986\loadmarkfile{buff-imp-default}    % preloaded as otherwise spurious spaces inline due to loading
987\loadmarkfile{buff-imp-nested}     % preloaded as otherwise spurious spaces inline due to loading
988\loadmarkfile{buff-imp-escaped}    % for completeness
989
990%D To be considered:
991
992% \installcorenamespace{visualizer}
993%
994% \installcommandhandler \??visualizer {visualizer} \??visualizer
995%
996% \appendtoks
997%     \frozen\instance\setuevalue{\currentvisualizer}{\buff_verbatim_visualize{\currentvisualizer}}%
998% \to \everydefinevisualizer
999%
1000% \protected\def\buff_verbatim_visualize#1%
1001%   {\bgroup
1002%    \def\currentvisualizer{#1}%
1003%    \usevisualizerstyleandcolor\c!style\c!color
1004%    \let\next}
1005
1006\appendtoks
1007    \enforced\def\type#1{\detokenize\expandafter{\csstring#1}}% or maybe detokenize
1008    \enforced\def\tex #1{\letterbackslash#1}%
1009\to \everysimplifycommands
1010
1011\stopcontextdefinitioncode
1012
1013%D This is \type {\asciimode} without the double comment hackery:
1014
1015\permanent\protected\def\literalmode{\setcatcodetable\txtcatcodes}
1016
1017\protect \endinput
1018