I want to put a label in the lower right corner of my listings, in this case to indicate the kind of code listed there. I have a bad hack that works somewhat and illustrates what I want:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\definecolor{light-gray}{gray}{0.95}
\newcommand{\mylabel}[1]{\raisebox{1em}[0pt][0pt]{\makebox[0pt][l]{\makebox[\linewidth][r]{\color{gray}{\sffamily #1}\hspace{3.5em}}}}\ignorespaces}
\lstnewenvironment{code}{\lstset{
,frame=single
,xleftmargin=2em
,xrightmargin=2em
,backgroundcolor=\color{light-gray}
,belowskip=0pt
}}{\mylabel{Code}}
\begin{document}
\begin{code}
This is
some code
set with lstlisting
\end{code}
This is again regular text.
\end{document}
Which looks like this:

The problem is that it
- does not work well with page breaks directly after the listing, i.e. the label may appear on the next page
- If the listing is followed by a blank line, the spacing is increased twice (due to the zero-sized but still existing content between the listing and the paragraph).
- It is not elegant.
Are there nicer ways of obtaining this result?

