Debian Codegraph @

texmf-dist/tex/plain/pitex/verbatim.ptx

raw
1 % Verbatim facilities.
2 \newcatcodetable\verbcatcodes{\\\{\}\$\&\#\^\_\~\%=12,\ \^^M=13}
3
4 \def\verb#1{%
5 \def\ptx@verb##1#1{\ptx@verb_punc{}{##1}\egroup}%
6 \bgroup
7 \catcodetable\verbcatcodes
8 \iffcommand\codefont\codefont
9 \ptx@verb
10 }
11
12 % Hack to prevent French spacing before punctuation. I should find
13 % something better (attributes marking material to be left alone when
14 % the French punctuation kicks in in the "kerning" callback).
15 \def\ptx@verb_punc#1#2{%
16 \ifelseif{%
17 {\ifcontains:{#2}} {\splitstringat:{#2}{\ptx@verb_glue{#1}:}}
18 {\ifcontains;{#2}} {\splitstringat;{#2}{\ptx@verb_glue{#1};}}
19 {\ifcontains!{#2}} {\splitstringat!{#2}{\ptx@verb_glue{#1}!}}
20 {\ifcontains?{#2}} {\splitstringat?{#2}{\ptx@verb_glue{#1}?}}
21 \iftrue {#1#2}}%
22 }
23 \def\ptx@verb_glue#1#2#3#4{%
24 \ptx@verb_punc{#1#3\kern0pt#2}{#4}%
25 }
26
27 \bgroup
28 \setcatcodes{\ \^^M=13}%
29 \gdef {\quitvmode\spacecs}%
30 \gdef^^M{\quitvmode\par}%
31 \egroup
32
33 \def\tcode#1{{\codefont#1}}
34 \long\def\com#1{%
35 \bgroup
36 \codefont
37 \string#1%
38 \egroup
39 \antigobblespace
40 }
41 \freedef\arg{{\codefont\it<#1>}\iffnext\spacechar{\kern.2ex }}
42 \freedef\barg{{\codefont\char"007B\relax\arg{#1}\char"007D\relax}}
43 \freedef\oarg{{\codefont[\arg{#1}]}}
44
45 \luacode
46 pitex.verbatims = {}
47 local function prepare_verbatim (chunk, name)
48 return function (str)
49 if not str:match("^%s*\noexpand\\" .. chunk .. "/") then
50 table.insert(pitex.verbatims[name], str)
51 return "%"
52 else
53 pitex.callback.remove("process_verbatim", "process_input_buffer")
54 return "\noexpand\\usecs{ptx@inner_verbatimstop:" .. chunk .. "}"
55 end
56 end
57 end
58
59 function install_verbatim (chunk, regime, name)
60 pitex.verbatims[name] = { regime = regime }
61 pitex.callback.process_verbatim = prepare_verbatim(chunk, name)
62 pitex.callback.register("process_input_buffer", "process_verbatim")
63 end
64
65 function do_verbatim(name, exec)
66 if exec then
67 tex.print(pitex.verbatims[name])
68 else
69 tex.print(pitex.verbatims[name].regime, pitex.verbatims[name])
70 end
71 end
72 \luacode/
73
74 \def\ptx@verbatim_last{}
75
76 \def\newverbatim#1{%
77 \ifnext[
78 {\ptx@newverbatim#1}
79 {\ptx@newverbatim#1[\verbcatcodes]}%
80 }
81
82 \long\def\ptx@newverbatim#1[#2]#3#4{%
83 \edef#1{%
84 \bgroup\primunexpanded{#3}%
85 \bgroup\primunexpanded{\setcatcodes{\^^M=12}}%
86 \noexpand\ptx@verbatim{\commandtoname#1}{#2}}%
87 \defcs{ptx@inner_verbatimstop:\commandtoname#1}{#4\egroup}%
88 }
89
90 \bgroup
91 \setcatcodes{\^^M=12}%
92 \gdef\ptx@verbatim#1^^M{%
93 \egroup%
94 \ptx@verbatim_prepare#1\relax%
95 }%
96 \egroup
97
98 \def\ptx@verbatim_prepare#1#2{%
99 \ifnext[
100 {\ptx@verbatim_do{#1}{#2}}
101 {\ptx@verbatim_do{#1}{#2}[last]}%
102 }
103
104 \def\ptx@verbatim_do#1#2[#3]{%
105 \gdef\ptx@verbatim_last{#3}%
106 \directlua{install_verbatim("#1", \the#2, "#3")}%
107 }
108
109 \newverbatim\verbatim{}
110 {\codefont\parindent0pt %\hsize=\maxdimen
111 \directlua{pitex.callback.close("french_punctuation", "kerning")}\relax
112 \vskip\baselineskip\printverbatim\relax
113 \directlua{pitex.callback.open("french_punctuation", "kerning")}
114 \vskip\baselineskip\removenextindent}
115
116 \def\doverbatim{%
117 \ifnext[
118 {\ptx@doverbatim{true}}
119 {\ptx@doverbatim{true}[\ptx@verbatim_last]}%
120 }
121 \def\printverbatim{%
122 \ifnext[
123 {\ptx@doverbatim{nil}}
124 {\ptx@doverbatim{nil}[\ptx@verbatim_last]}%
125 }
126 \def\ptx@doverbatim#1[#2]{%
127 \directlua{do_verbatim("#2", #1)}%
128 }