summaryrefslogtreecommitdiff
path: root/tex/plain/enigma/enigma.tex
blob: 2299890dca4c8822019a788031a2f55d84cdcb2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
\input{luatexbase.sty}
\catcode`\_=11 % There’s no reason why this shouldn’t be the case.
%D \startdocsection[title=Prerequisites]
%D \startparagraph
%D Package loading and the namespacing issue are commented on in
%D \identifier{enigma.lua}.
%D \stopparagraph
\directlua{%
  packagedata = packagedata or { }
  dofile(kpse.find_file"enigma.lua")
}

%D \startparagraph
%D First, create somthing like \CONTEXT’s asciimode. We found
%D \texmacro{newluatexcatcodetable} in \identifier{luacode.sty} and it
%D seems to get the job done.
%D \stopparagraph
\newluatexcatcodetable \enigmasetupcatcodes
\bgroup
  \def\escapecatcode      {0}
  \def\begingroupcatcode  {1}
  \def\endgroupcatcode    {2}
  \def\spacecatcode      {10}
  \def\lettercatcode     {11}
  \setluatexcatcodetable\enigmasetupcatcodes {
      \catcode`\^^I = \spacecatcode % tab
      \catcode`\    = \spacecatcode
      \catcode`\{   = \begingroupcatcode
      \catcode`\}   = \endgroupcatcode
      \catcode`\^^L = \lettercatcode    % form feed
      \catcode`\^^M = \lettercatcode    % eol
  }
\egroup
%D \stopdocsection

%D \startdocsection[title=Setups]
%D \startparagraph
%D Once the proper catcodes are in place, the setup macro
%D \texmacro{do-setup_enigma} doesn’t to anything besides passing stuff
%D through to Lua.
%D \stopparagraph
\def\do_setup_enigma#1{%
    \directlua{
      local enigma = packagedata.enigma
      local current_args = enigma.parse_args([====[\detokenize{#1}]====])
      enigma.new_callback(enigma.new_machine(current_args,
                                             [====[\currentenigmaid]====]),
                          [====[\currentenigmaid]====])
    }%
  \egroup%
}

%D The module setup \texmacro{setupenigma} expects key=value, notation.
%D All the logic is at the Lua end, not much to see here …
\def\setupenigma#1{%
  \bgroup
    \edef\currentenigmaid{#1}
    \luatexcatcodetable \enigmasetupcatcodes
    \do_setup_enigma%
}
%D \stopdocsection

%D \startdocsection[title=Encoding Macros]
%D \startparagraph
%D The environment of \texmacro{start<enigmaid>} and
%D \texmacro{stop<enigmaid>} allow enabling of Enigma encoding in
%D different parts of the document.
%D \stopparagraph

\def\do_define_enigma#1{%
  \@EA\gdef\csname start\enigmaid\endcsname{%
    \bgroup%
    \directlua{%
      if packagedata.enigma and packagedata.enigma.machines["#1"] then
        luatexbase.add_to_callback("pre_linebreak_filter",
                                   packagedata.enigma.callbacks["#1"],
                                   "#1")
      else
        print([[ENIGMA: No machine of that name: #1!]])
        os.exit()
      end
    }%
  }%
  \@EA\gdef\csname stop\enigmaid\endcsname{%
    \directlua{
      luatexbase.remove_from_callback("pre_linebreak_filter", "#1")
      packagedata.enigma.machines["#1"]:processed_chars()
    }%
    \egroup%
  }%
}

\def\defineenigma#1{%
  \begingroup
  \let\@EA\expandafter
  \edef\enigmaid{#1}%
  \@EA\do_define_enigma\@EA{\enigmaid}%
  \endgroup%
}
%D \stopdocsection

\catcode`\_=8
% vim:ft=tex:sw=2:ts=2:expandtab:tw=72