This is a follow-up question to this answer by Mico. I try to adjust his Lua code to my needs and line numbering already works.
Actually there is one real usability issue and that's my question: How can I print the buffer at \tdendlongoutput
or the removal from callback respectively? Currently I need an extra command for that.
Here's what I tried so far:
- Inserting the directive right below the callback removal,
- Using
tex.print
to print a LaTeX macro that uses runs that function.
MWE:
%!TeX TS-program=lualatex+shellescape
\documentclass{scrreprt}
\directlua{%
verb_table = {}
i = 1
function store_lines (str)
if string.find (str , "\noexpand\\tdendlongoutput" ) then
luatexbase.remove_from_callback ("process_input_buffer" , "store_lines")
else
table.insert(verb_table, tostring(i) .. " " .. str)
i = i + 1
end
return ""
end
function register_verbatim ()
verb_table = {}
i = 1
luatexbase.add_to_callback("process_input_buffer" , store_lines , "store_lines")
end
function print_lines ( catcode )
if catcode then
tex.print ( catcode , verb_table)
else
tex.print ( verb_table )
end
end
}
\def\tdlongoutput{\directlua{register_verbatim()}}
\def\tduseoutputastex{\directlua{print_lines()}}
\def\createcatcodes{%
\bgroup%
\catcode`\\=12 \catcode`\{=12 \catcode`\}=12%
\catcode`\$=12 \catcode`\&=12 \catcode`\^^M=13%
\catcode`\#=12 \catcode`\^=12 \catcode`\_=12%
\catcode`\ =13 \catcode`\~=12 \catcode`\%=12%
\savecatcodetable 1%
\egroup%
}
\createcatcodes
\bgroup%
\catcode`\^^M=13\gdef^^M{\quitvmode\par}%
\catcode`\ = 13\gdef {\quitvmode\Space}%
\egroup%
\def\Space{ }
\begin{document}\ttfamily
\tdlongoutput
total 10244
drwxr-xr-x 47 User Users 16384 Mar 31 18:26 .
dr-xr-xr-x 8 User Users 4096 Jun 27 2015 ..
drwxr-xr-x 9 User Users 4096 Dec 23 12:49 .editor
-rw-r--r-- 1 User Users 2950 Mar 31 09:01 .bash_history
\tdendlongoutput
\directlua{print_lines(1)}
\end{document}