There's a method. For the documentation, see texdoc luatex and texdoc ltluatex (and the packages being imported).
Disadvantage: it's necessary to write to a real external file (not \scantokens)
Search for !! in the code for the important parts. (note that directlua instead of luacode* is used, so be careful with the catcode)
%! TEX program = lualatex
\documentclass{article}
\usepackage{currfile} % !! need this package for currfilename to be defined
\begin{document}
% write the content to a separate file
\directlua{magic_offset=1 inputlineno_offset=tex.inputlineno+magic_offset} % !!
\begin{filecontents*}[overwrite]{b.tex}
\typeout{1}
abc
abc
abc
\typeout{2}
\end{filecontents*}
% it's also possible to typeset something between the "write" part and the "rescan" part
first line
\directlua{ --[[ !! ]]
saved_synctex_tag=tex.get_synctex_tag()
function handler()
if token.get_macro("currfilename")=="b.tex" and tex.get_synctex_tag()>0 then
if not (saved_synctex_tag==nil) then
tex.set_synctex_tag(saved_synctex_tag)
saved_synctex_tag=nil
end
tex.set_synctex_line(tex.inputlineno+inputlineno_offset)
end
end
tex.set_synctex_mode(2)
luatexbase.add_to_callback('process_input_buffer', handler, "synctex patch callback")
}\input{b.tex}\directlua{luatexbase.remove_from_callback('process_input_buffer', "synctex patch callback") tex.set_synctex_mode(0) tex.set_synctex_line(0) }
last line
last line
\end{document}
Note that for some weird reason, if [abspath] option is provided to currfile package the handler will be called several times with tex.get_synctex_tag()==0.
Thus the check is added so that it's only called once, but on the actual file.
(I guess that it's because the primitive expand-only the \input command or something)
Removing saved_synctex_tag=nil line; or the get_macro("currfilename")=="b.tex" check will make something worse because
(I think, not tested) if b.tex includes e.g. c.tex, then the set_synctex_line should not be run for c.tex.
Alternative to callback, setting the tag within the input-ed file itself always work.
process_input_buffercallback, but that would change\makeboxto\mbkeboxis that OK? – David Carlisle Jan 29 '22 at 11:03