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