Q:

Redefine obeyspaces to newline

I want to typeset code snippets from different programming languages. I couldn't get listings to do what I want (one complete height of an empty line takes up too much space for my liking) and neither did I manage to define everything I want myself. 
I'd like to define a new environment where return calls \newline, and where an empty line calls \par (this one is already present in normal text mode) so that I can differentiate between them. In addition, every space inserted should be printed, but that is taken care of by \obeyspaces.
MWE:
\documentclass{article}

\newenvironment{code}{
\ttfamily
\parindent=0pt\parskip=5pt
\obeyspaces\obeylines
}{}

\begin{document}
\begin{code}
text 1space  2spaces
new line

empty line before this line
\end{code}
\end{document}    

I found 
\def\obeypar{\catcode`\^^M\active \let ^^M\par }`    

and tried to define \obeylines (LaTeX tells me it's undefined) but since these are TeX primitives (?) they give an error. 
Can I tell LaTeX that this part should be treated as TeX?
What am I missing or where I can read about these things? 

A:

If I understand the question, you need to distinguish the empty and non-empty lines in code environment. You can try the following:
\def\emptyline{\hbox to\hsize{\dotfill empty line\dotfill}}
%\def\emptyline{\vskip.7\baselineskip} % ... another alternative ...
\def\printemptyline#1{\def\par{\ifvmode\emptyline\fi\endgraf}\obeylines}

\begin{code}\printemptyline
text 1space  2spaces
new line

empty line before this line
\end{code}

This gives the result:

