I want to create a custom listings environment and want to set its width as something like 90% of textwidth, centred.
Per an earlier question for algorithms, the first thing I tried was to embed the listing in a minipage environment. However, custom listing environments must be done through the \lstnewenvironment command. I found a way to still make standard environment work, but it requires to use the escape command every time before ending the environment.
The second thing I tried was this latter command, but as far as I can see, it doesn't offer a feature to set the width of the listing or centre it.
\documentclass{article}
\usepackage{listings}
\usepackage{lipsum}
\lstnewenvironment{queryl} %% APPROACH 1
{\lstset{frame=shadowbox,escapechar=`}}
{}
\newenvironment{query} %% APPROACH 2
{\begin{minipage}{4cm}\centering\begin{queryl}}
{\end{queryl}\end{minipage}}
\begin{document}
%% APPROACH 1
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis.
\begin{queryl}
begin
{ do nothing }
end ;
\end{queryl}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis.
%% APPROACH 2
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis.
\begin{query}
begin
{ do nothing }
end ;`%<- needs escape-to-LaTeX character to work
\end{query}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis.
\end{document}

One can note that for the first environment, the frame extends beyond the width of the text. Also, puzzlingly the minipage environment doesn't seem to want to centre and swallows space (though I guess I could perhaps at least get that to work with tinkering).
In any case, does anyone have any ideas on how to set a width for a custom listings environment and centre it without requiring use of the escape character each time to end the environment?

\newenvironment{query} %% APPROACH 2 {\begin{center}\begin{minipage}{4cm}\centering\begin{queryl}} {\end{queryl}\end{minipage}\end{center}}– hpesoj626 Nov 21 '12 at 02:03