I am writing a \bashDemo macro that will (a) execute its argument in a shell, e.g., bash, script, and (b) display the argument in a listings. So,
\bashDemo
ls -ls > myfiles
\END
will both run ls -ls >myfiles using the bash command shell, and will display using lstlistings this command within the document. The problem I have is that in consuming characters in a verbatim fashion, I have a newline both at the beginning and at the end of the argument. How do I get rid of these?
Here is my code (it is based on tobiShell.sty):
\lstdefinestyle{bash}{basicstyle={\ttfamily}}
\newwrite\ScriptFile
\edef\ScriptFileName{\jobname.sh}
% Store the argument into our script file.
\newcommand\generateScriptFile[1]{%
\immediate\openout\ScriptFile\ScriptFileName
\immediate\write\ScriptFile{#1}%
\immediate\closeout\ScriptFile
}
% Store the list of tokens in the argument into our script file
% and then list the contents of that file, prefixed by a "%".
\newcommand\listScriptFile[1]{%
% The following does not work as expected.
\generateScriptFile{\%\ignorespaces #1\unskip}\relax
\lstinputlisting[style=bash]{\bashescScriptFileName}\relax
\relax
}
\newcommand\bashDemo{\bashDemoI}
{% Define \bashDemoI
\catcode`\^^M=13%
\gdef\bashDemoI{%
\bgroup
\def\do##1{\catcode`##1 12\relax}%
\catcode`\^^M=13%
\def^^M{^^J}%
\dospecials%
\bashDemoII%
}%
}
{% Define \bashDemoII
\catcode`\@=0\relax%
@catcode`@\=12@relax%
@gdef@bashDemoII#1\END{@relax%
@generateScriptFile{#1}@relax%
@immediate@write18{bash@space@ScriptFileName@space}@relax%
@listScriptFile{#1}@relax%
@egroup@relax%
}@relax%
}
ydocwhich does something similar. I remove the trailing and leading new lines as well there. – Martin Scharrer Feb 18 '11 at 18:20\def\do##1{\catcode\##1 12\relax}with\let\do@makeotherand\catcode`^^M=13with\catcode`^^M\active. I think you want@percentcharinstead of%. The verbatim\ENDcould be more easily inserted using the common\lcchar/\lowercasetrick. I like\begingroup/\endgroupmuch more then the{,}` for this kind of definitions, because they are much more readable. – Martin Scharrer Feb 18 '11 at 18:25ydocI have an own definition of themacrocodeenvironment. If it is empty the leading and trailing new-lines are the same! :-) – Martin Scharrer Feb 18 '11 at 18:44