5

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}

1 Answer 1

4

(I've modified my original answer in order to restore functioning of the OP's macro \tduseoutputastex.)

You may achieve your objective of automatically printing the contents of the verbatim block -- which is understood to be sandwiched between lines that say \tdlongoutput and \tdendlongoutput, resp. -- as follows: in the Lua function store_lines, upon reaching a line that says \tdendlongoutput, return the string \tdlongoutputprint rather than just a blank line. The macro \tdlongoutputprint is defined to print out the contents of the verbatiim buffer.

Note that I've also changed your Lua code as follows: the line numbers of the verbatim material are no longer stored with the verbatim material itself. Instead, they are inserted dynamically at print time, i.e., during the operation of \tdlongoutputprint.

If you want to simply store the verbatim block without printing out its contents automatically, you should terminate the verbatim block with the \tdendlongoutputnoprint instead of \tdendlongoutput.

enter image description here

%!TeX TS-program=lualatex
\documentclass{scrreprt}

%% Lua-side code
\usepackage{luacode}
\begin{luacode}
verb_table = {}
i = 1
function store_lines (str)
    if str:find ( "\\tdendlongoutputnoprint" ) then
        luatexbase.remove_from_callback ("process_input_buffer" , "store_lines")
        return ""
    elseif str:find ( "\\tdendlongoutput" ) then
        luatexbase.remove_from_callback ("process_input_buffer" , "store_lines")
        return ( "\\tdlongoutputprint" )
    else
        table.insert ( verb_table , str )
        return ""
    end
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
        for i = 1 , #verb_table do
            tex.print ( catcode , i.." "..verb_table[i])
        end
    else
        tex.print ( verb_table )
    end
end
\end{luacode}

%% TeX-side code
\def\tdlongoutput{\directlua{register_verbatim()}}
\newcommand\tdlongoutputprint{%
  \bgroup
  \setlength\parindent{0pt}
  \ttfamily
  \directlua{print_lines(1)}
  \egroup}

\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}
\tdlongoutput % start of verbatim block
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  %% print contents of verbatim block

\tdlongoutput  % define a macro called "\lululu"
\def\lululu{%
  Lua\kern-.01em
       \LaTeX%
  }
\tdendlongoutputnoprint %% no AUTOMATIC printing of verb. block

\bigskip
\tdlongoutputprint %% print contents of latest verb. block "by hand"

\bigskip
\tduseoutputastex % "register" the macro "\lululu"
\noindent\lululu

\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .