Suppose I want to typeset a screen session in some programming language (GAP in my case, but it could be Python, BASH, or something else with an interactive read prompt just as well). So on my screen, I might have something like this:
gap> for i in [1..10] do
> Print(i, ":");
> od;
1:2:3:4:5:6:7:8:9:10:
gap> 2^10;
1024
gap> 1/0;
Error, Rational operations: <divisor> must not be zero
not in any function at line 5 of *stdin*
you can replace <divisor> via 'return <divisor>;'
brk> 2^10;
1024
brk>
That is, there is a command prompt gap>
, after which the user enters a multi line input. After that comes a line of output, then another prompt gap>
. This repeats, until a command triggers an error, at which point the prompt changes to brk>
.
I would like to use the listings
package (or something else) to
- color the prompts
gap>
,>
, andbrk>
; say, blue and red; - highlight keywords like
for
,do
, andod
; say, by making them bold; - but only highlight the keywords in lines starting with a prompt, hence not inside the output of my commands.
The last point is the one that is causing me troubles. I couldn't figure out a way to do that. The result is often quite ugly when words like and
, not
, and in
are highlighted as keywords in, e.g., an error message.
Similar questions have been asked before, but of those I managed to find, none asked for exactly this, resp. got an answer that does what I want.
For the record, here is the language definition I am using for now:
\lstdefinelanguage{GAP}{% morekeywords=[2]{and,break,continue,do,elif,else,end,fail,false,fi,for,% function,if,in,local,mod,not,od,or,rec,repeat,return,then,true,% until,while},% moredelim=[s][\color{blue}]{gap}{>},% moredelim=[s][\color{red}]{brk}{>},% %moredelim=*[l][\color{blue}]{gap>},% %moredelim=*[l][\color{red}]{brk>},% sensitive=true,% morecomment=[l]\#,% morestring=[b]',% morestring=[b]",% }%
PS: To illustrate what I want, consider this image showing how my example from above could look (in addition to what I described above, I also set all user inputs in italics):