Debian Codegraph @

texmf-dist/tex/plain/enigma/enigma.tex

raw
1 \newif\ifenigmaisrunningplain
2 \ifcsname [email protected]\endcsname\else
3 \enigmaisrunningplaintrue
4 \input{luatexbase.sty}
5 \catcode`\@=11
6 % \else latex
7 \fi
8 \catcode`\_=11 % There’s no reason why this shouldn’t be the case.
9 \catcode`\!=11
10 %D Nice tool from luat-ini.mkiv. This really helps with those annoying
11 %D string separators of Lua’s that clutter the source.
12 % this permits \typefile{self} otherwise nested b/e sep problems
13 \def\luastringsep{===}
14 \edef\!!bs{[\luastringsep[}
15 \edef\!!es{]\luastringsep]}
16 %D \startdocsection[title=Prerequisites]
17 %D \startparagraph
18 %D Package loading and the namespacing issue are commented on in
19 %D \identifier{enigma.lua}.
20 %D \stopparagraph
21 \directlua{
22 packagedata = packagedata or { }
23 dofile(kpse.find_file\!!bs enigma.lua\!!es)
24 }
25
26 %D \startparagraph
27 %D First, create somthing like \CONTEXT’s asciimode. We found
28 %D \texmacro{newluatexcatcodetable} in \identifier{luacode.sty} and it
29 %D seems to get the job done.
30 %D \stopparagraph
31 \newluatexcatcodetable \enigmasetupcatcodes
32 \bgroup
33 \def\escapecatcode {0}
34 \def\begingroupcatcode {1}
35 \def\endgroupcatcode {2}
36 \def\spacecatcode {10}
37 \def\lettercatcode {11}
38 \setluatexcatcodetable\enigmasetupcatcodes {
39 \catcode`\^^I = \spacecatcode % tab
40 \catcode`\ = \spacecatcode
41 \catcode`\{ = \begingroupcatcode
42 \catcode`\} = \endgroupcatcode
43 \catcode`\^^L = \lettercatcode % form feed
44 \catcode`\^^M = \lettercatcode % eol
45 }
46 \egroup
47 %D \stopdocsection
48
49 %D \startdocsection[title=Setups]
50 %D \startparagraph
51 %D Once the proper catcodes are in place, the setup macro
52 %D \texmacro{do_setup_enigma} doesn’t to anything besides passing stuff
53 %D through to Lua.
54 %D \stopparagraph
55 \def\do_setup_enigma#1{%
56 \directlua{
57 local enigma = packagedata.enigma
58 local current_args = enigma.parse_args(\!!bs\detokenize{#1}\!!es)
59 enigma.save_raw_args(current_args, \!!bs\current_enigma_id\!!es)
60 enigma.new_callback(
61 enigma.new_machine(\!!bs\current_enigma_id\!!es),
62 \!!bs\current_enigma_id\!!es)
63 }%
64 \egroup%
65 }
66
67 %D The module setup \texmacro{setupenigma} expects key=value, notation.
68 %D All the logic is at the Lua end, not much to see here …
69 \def\setupenigma#1{%
70 \bgroup
71 \edef\current_enigma_id{#1}
72 \luatexcatcodetable \enigmasetupcatcodes
73 \do_setup_enigma%
74 }
75 %D \stopdocsection
76
77 %D \startdocsection[title=Encoding Macros]
78 %D \startparagraph
79 %D The environment of \texmacro{begin<enigmaid>} and
80 %D \texmacro{end<enigmaid>} toggles Enigma encoding.
81 %D (Regarding environment delimiters we adhere to Knuth’s
82 %D practice of prefixing with \type{begin}/\type{end}.)
83 %D \stopparagraph
84
85 \def\e!start{begin} %{start}
86 \def \e!stop{end} %{stop}
87 \edef\c!pre_linebreak_filter{pre_linebreak_filter}
88 \def\do_define_enigma#1{%
89 \@EA\gdef\csname \e!start\current_enigma_id\endcsname{%
90 \endgraf
91 \bgroup%
92 \directlua{%
93 if packagedata.enigma and
94 packagedata.enigma.machines[ \!!bs#1\!!es] then
95 luatexbase.add_to_callback(
96 \!!bs\c!pre_linebreak_filter\!!es,
97 packagedata.enigma.callbacks[ \!!bs#1\!!es],
98 \!!bs#1\!!es)
99 else
100 print\!!bs ENIGMA: No machine of that name: #1!\!!es
101 os.exit()
102 end
103 }%
104 }%
105 \@EA\gdef\csname \e!stop\current_enigma_id\endcsname{%
106 \endgraf
107 \directlua{
108 luatexbase.remove_from_callback(
109 \!!bs\c!pre_linebreak_filter\!!es,
110 \!!bs#1\!!es)
111 packagedata.enigma.machines[ \!!bs#1\!!es]:processed_chars()
112 }%
113 \egroup%
114 }%
115 }
116
117 \def\defineenigma#1{%
118 \begingroup
119 \let\@EA\expandafter
120 \edef\current_enigma_id{#1}%
121 \@EA\do_define_enigma\@EA{\current_enigma_id}%
122 \endgroup%
123 }
124
125 %D \stopdocsection
126
127 \catcode`\_=8 % \popcatcodes
128 \catcode`\!=12 % reserved according to source2e
129 \ifenigmaisrunningplain\catcode`\@=12\fi
130 % vim:ft=plaintex:sw=2:ts=2:expandtab:tw=71