Debian Codegraph @

texmf-dist/source/latex/ucharcat/ucharcat.dtx

raw
1 % \iffalse
2 %% Source File: textcase.dtx
3 %% Copyright 2015 David Carlisle
4 %%
5 %% This file may be distributed under the terms of the LPPL.
6 %% See README for details.
7 %
8 %<*dtx>
9 \ProvidesFile{ucharcat.dtx}
10 %</dtx>
11 %<package>\NeedsTeXFormat{LaTeX2e}
12 %<package>\ProvidesPackage{ucharcat}
13 %<driver> \ProvidesFile{ucharcat.drv}
14 % \fi
15 % \ProvidesFile{ucharcat.dtx}
16 [2015/11/19 v0.03 ucharcat for luaLaTeX (DPC)]
17 %
18 % \iffalse
19 %<*driver>
20 \documentclass{ltxdoc}
21 \ifx\directlua\undefined\else
22 \usepackage{ucharcat}
23 \fi
24 \begin{document}
25 \DocInput{ucharcat.dtx}
26 \end{document}
27 %</driver>
28 % \fi
29 %
30 % \GetFileInfo{ucharcat.dtx}
31 %
32 % \title{The \textsf{ucharcat} Package\thanks{This file
33 % has version number \fileversion, last
34 % revised \filedate.
35 % Please report any issues at https://github.com/davidcarlisle/dpctex/issues}}
36 % \author{David Carlisle}
37 % \maketitle
38 %
39 % \section{Introduction}
40 %
41 % The 2015 release of Xe\TeX\ introduced a new command |\Ucharcat|,
42 % this is an extension of the |\Uchar| comand that has been available
43 % in Xe\TeX\ and lua\TeX\ for some time. It takes a second integer
44 % value, that specifies the category code of the token to be
45 % produced. This allows character tokens to be constructed \emph{via
46 % expansion}, which has many potential uses in producing expandable
47 % case changing, numeric counter represntations, etc.
48 %
49 % |\Uchar 65 12| produces a catcode 12 |A| for example.
50 %
51 % This package provides a lua implementation of |\Ucharcat| for use
52 % with luatex, it silently accepts Xe\TeX\ and does nothing in that
53 % case if |\Ucharcat| is defined.
54 %
55 % The main difference between the lua implementation and the Xe\TeX\
56 % primitive is that the lua implementation takes \emph{two} expansions
57 % to produce the token.
58 %
59 %\begin{verbatim}
60 % \edef\tmp{\Uchar 65 11 }
61 %\end{verbatim}
62 % is the same as |\def\tmp{A}| with both
63 % systems but
64 %
65 %\begin{verbatim}
66 % \expandafter\def\expandafter\tmp\expandafter{\Uchar 65 11 }
67 %\end{verbatim}
68 % the same as
69 %\begin{verbatim}
70 % \def\tmp{A}
71 %\end{verbatim}
72 % with Xe\TeX, but in lua\TeX\ it is
73 % equivalent to
74 %\begin{verbatim}
75 % \def\tmp{\directlua{UcharcatLua() 65 11 }
76 %\end{verbatim}
77 %
78 % \ifx\Ucharcat\undefined\else
79 % \section{Examples}
80 %
81 % This section will be omitted if this document is not processed with
82 % a suitable format.
83 %
84 % \begin{itemize}
85 % \item |\Ucharcat 65 11|
86 %
87 % \Ucharcat 65 11\ is a capital A.
88 %
89 % \item |\Ucharcat 65 12|
90 % \edef\tmpa{\Ucharcat 65 12}
91 % \edef\tmpb{\string A}
92 %
93 % This is a catcode 12 A: \ifx\tmpa\tmpb yes\else no\fi.
94 %
95 % \item% |\Ucharcat 65 1| and |\Ucharcat 65 2|
96 %
97 % \Ucharcat 65 1 \large\bfseries Bold is grouped \Ucharcat 65 2
98 % by catcode 1 and 2 A.
99 % \end{itemize}
100 %
101 % \fi
102 % \section{Implementation}
103 %
104 % Note that the current implementation uses |\directlua| and a
105 % dedicated luatex catcode array. Hans hagen made some useful comments
106 % and pointers to alternative implementation using |\luafunction|
107 % in the luatex list thread
108 % \texttt{http://tug.org/pipermail/luatex/2015-May/005199.html}
109 % For now keeping with the simpler initial approach as there
110 % is no built in support for |\luafunction| in the \LaTeX\ format yet.
111 % (No hook to save function definition in the format)
112 %
113 % \begin{macrocode}
114 %<*package>
115 % \end{macrocode}
116 % On classic \TeX{} or old Xe\TeX, stop.
117 % \begin{macrocode}
118 \ifx\directlua\@undefined
119 \ifx\Ucharcat\@undefined
120 \ifx\XeTeXinterchartokenstate\@undefined
121 \PackageError{ucharcat}
122 {\string\Ucharcat\space may only be used with xetex and luatex}
123 {skipping package}
124 \else
125 \PackageError{ucharcat}
126 {\string\Ucharcat\space is defined in xetex releases from 2015 only}
127 {skipping package}
128 \fi
129 \fi
130 \expandafter\endinput
131 \fi
132 % \end{macrocode}
133 %
134 % Current latex formats (from 2015/11/01) define an allocator for catcode tables
135 % otherwise just use one (near) the top of the range (hex 7FFF).
136 %
137 % \begin{macrocode}
138 \ifx\newcatcodetable\@undefined
139 \chardef\ucharcat@table"7000
140 \directlua{tex.enableprimitives("",{"initcatcodetable"})}
141 \initcatcodetable\ucharcat@table
142 \else
143 \newcatcodetable\ucharcat@table
144 \fi
145 % \end{macrocode}
146 %
147 %
148 % lua print function
149 % \begin{macrocode}
150 \directlua{%
151 local nt = newtoken or token
152 function UcharcatLua()
153 local mych = nt.scan_int()
154 local mycat = nt.scan_int()
155 tex.setcatcode(\the\numexpr\ucharcat@table\relax,mych,mycat)
156 tex.sprint(\the\numexpr\ucharcat@table\relax,unicode.utf8.char(mych))
157 end
158 }
159 % \end{macrocode}
160 %
161 % \TeX\ wrapper.
162 % \begin{macrocode}
163 \def\Ucharcat{\directlua{UcharcatLua()}}
164 % \end{macrocode}
165 %
166 % \begin{macrocode}
167 %</package>
168 % \end{macrocode}
169 %
170 % \Finale
171 %