summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/ucharcat/ucharcat.dtx
blob: a5b06392c5c6477fa8dcc34b980a46dfb8f6f0a0 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
% \iffalse
%% Source File: textcase.dtx
%% Copyright 2015 David Carlisle
%%
%% This file may be distributed under the terms of the LPPL.
%% See README for details.
%
%<*dtx>
          \ProvidesFile{ucharcat.dtx}
%</dtx>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{ucharcat}
%<driver> \ProvidesFile{ucharcat.drv}
% \fi
%         \ProvidesFile{ucharcat.dtx}
       [2015/11/19 v0.03 ucharcat for luaLaTeX  (DPC)]
%
% \iffalse
%<*driver>
\documentclass{ltxdoc}
\ifx\directlua\undefined\else
\usepackage{ucharcat}
\fi
\begin{document}
\DocInput{ucharcat.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{ucharcat.dtx}
%
% \title{The \textsf{ucharcat} Package\thanks{This file
%        has version number \fileversion, last
%        revised \filedate.
% Please report any issues at https://github.com/davidcarlisle/dpctex/issues}}
% \author{David Carlisle}
% \maketitle
%
% \section{Introduction}
%
% The 2015 release of Xe\TeX\ introduced a new command |\Ucharcat|,
% this is an extension of the |\Uchar| comand that has been available
% in Xe\TeX\ and lua\TeX\ for some time. It takes a second integer
% value, that specifies the category code of the token to be
% produced. This allows character tokens to be constructed \emph{via
% expansion}, which has many potential uses in producing expandable
% case changing, numeric counter represntations, etc.
%
% |\Uchar 65 12| produces a catcode 12 |A| for example.
%
% This package provides a lua implementation of |\Ucharcat| for use
% with luatex, it silently accepts Xe\TeX\ and does nothing in that
% case if |\Ucharcat| is defined.
%
% The main difference between the lua implementation and the Xe\TeX\
% primitive is that the lua implementation takes \emph{two} expansions
% to produce the token.
%
%\begin{verbatim}
% \edef\tmp{\Uchar 65 11 }
%\end{verbatim}
% is the same as |\def\tmp{A}| with both
% systems but
%
%\begin{verbatim}
% \expandafter\def\expandafter\tmp\expandafter{\Uchar 65 11 }
%\end{verbatim}
% the same as
%\begin{verbatim}
% \def\tmp{A}
%\end{verbatim}
% with Xe\TeX, but in lua\TeX\ it is
% equivalent to
%\begin{verbatim}
% \def\tmp{\directlua{UcharcatLua() 65 11 }
%\end{verbatim}
%
% \ifx\Ucharcat\undefined\else
% \section{Examples}
%
% This section will be omitted if this document is not processed with
% a suitable format.
%
% \begin{itemize}
% \item |\Ucharcat 65 11|
%
% \Ucharcat 65 11\ is a capital A.
% 
% \item  |\Ucharcat 65 12|
% \edef\tmpa{\Ucharcat 65 12}
% \edef\tmpb{\string A}
% 
% This is a catcode 12 A: \ifx\tmpa\tmpb yes\else no\fi.
%
% \item% |\Ucharcat 65 1| and |\Ucharcat 65 2| 
%
% \Ucharcat 65 1  \large\bfseries Bold is grouped \Ucharcat 65 2 
% by catcode 1 and 2 A.
% \end{itemize}
% 
% \fi
% \section{Implementation}
%
% Note that the current implementation uses |\directlua| and a
% dedicated luatex catcode array. Hans hagen made some useful comments
% and pointers to alternative implementation using |\luafunction|
% in the luatex list thread
% \texttt{http://tug.org/pipermail/luatex/2015-May/005199.html}
% For now keeping with the simpler initial approach as there
% is no built in support for |\luafunction| in the \LaTeX\ format yet.
% (No hook to save function definition in the format)
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
% On classic \TeX{} or old Xe\TeX, stop.
%    \begin{macrocode}
\ifx\directlua\@undefined
\ifx\Ucharcat\@undefined
\ifx\XeTeXinterchartokenstate\@undefined
\PackageError{ucharcat}
{\string\Ucharcat\space may only be used with xetex and luatex}
{skipping package}
\else
\PackageError{ucharcat}
{\string\Ucharcat\space is defined in xetex releases from 2015 only}
{skipping package}
\fi
\fi
\expandafter\endinput
\fi
%    \end{macrocode}
%
% Current latex formats (from 2015/11/01) define an allocator for catcode tables
% otherwise just use one (near) the top of the range (hex 7FFF).
%
%    \begin{macrocode}
\ifx\newcatcodetable\@undefined
  \chardef\ucharcat@table"7000
  \directlua{tex.enableprimitives("",{"initcatcodetable"})}
  \initcatcodetable\ucharcat@table
\else
  \newcatcodetable\ucharcat@table
\fi
%    \end{macrocode}
%
% 
% lua print function
%    \begin{macrocode}
\directlua{%
local nt = newtoken or token
function UcharcatLua()
  local mych = nt.scan_int() 
  local mycat = nt.scan_int()
  tex.setcatcode(\the\numexpr\ucharcat@table\relax,mych,mycat)
  tex.sprint(\the\numexpr\ucharcat@table\relax,unicode.utf8.char(mych))
end
}
%    \end{macrocode}
%
% \TeX\ wrapper.
%    \begin{macrocode}
\def\Ucharcat{\directlua{UcharcatLua()}}
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \Finale
%