Consider the following MWE:
% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{fontspec}
\setmainfont[Ligatures=NoCommon]{Latin Modern Roman}
\usepackage{luacode,luatexbase}
\begin{luacode}
function dosub ( s )
s = string.gsub ( s , 'ff', '\char64256{}')
return ( s )
end
--luatexbase.add_to_callback ( "process_input_buffer", dosub, "dosub" )
\end{luacode}
\begin{document}
off \directlua{ tex.sprint ( dosub ( \luastring{off} ) ) } off
\end{document}
The heart of the code is the function dosub, which employs the Lua function string.gsub. It is set to replace instances of ff with the glyph that contains the ff ligature. (You'll have to trust me that, for the font at hand, the ff-ligature glyph is located in "slot" 64256.) Note that, for now, the instruction luatexbase.add_to_callback instruction is commented out. (A -- (double dash) string initiates a Lua comment.)
When this MWE is run, one gets:
Observe that the middle word, which is generated via a \directlua call to dosub, correctly contains the ff-ligature, whereas the first and third words do not (once again correctly, since automatic ligature generation is disabled).
The trouble starts when I uncomment the instruction
luatexbase.add_to_callback ( "process_input_buffer", dosub, "dosub" )
Upon recompiling, the following, fairly incomprehensible, error message results:
(/usr/local/texlive/2015/texmf-dist/tex/context/base/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
\scratchcounter=\count290
\scratchdimen=\dimen261
\scratchbox=\box256
! Missing number, treated as zero.
\let
l.275 \let
\pdflastform=\pdflastxform
?
I suspect this is somewhat related to the presence of a TeX macro -- \char -- in the replacement string part of the string.gsub function. To wit, if I replace '\\char64256{}' with gg (i.e., a constant string), no error message is generated (and the three instances of "ff" in the body of the document are automatically replaced with "gg").
Do I need to "wrap" or "protect" the TeX macro in some special way in order to enable the successful use of "luatexbase.add_to_callback"? Is there something else I should do? About my computing setup: I'm running MacTeX2015 (with all available updates thru this morning applied) on a MacBookPro running MacOSX 10.10.5 "Yosemite".


\charisn't a macro it's a non expandable command related to typesetting, it does not generate character tokens (what you are calling strings) you could use\uchar– David Carlisle Aug 22 '15 at 13:24\charis non-expandable. I'll definitely make use of either\ucharor of Michal's suggestion, which is to use the LuaTeX functionunicode.utf8.char. – Mico Aug 22 '15 at 13:32