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