How to deal with very long lines that do not contain spaces?

\begin{lstlisting}[language=TeX,breaklines]
...
\end{lstlisting}

works, depending however on the language used. For instance, with TeX as language.

Another solution, similar to the one proposed by projetmbc is

\makeatletter
{\obeylines\gdef\bt@eol{^^M}}
\newenvironment{breakabletexttt}
  {\ttfamily\hfuzz=0.4em
   \list{}{\leftmargin=2em
           \itemindent=-\leftmargin
           \listparindent=-\leftmargin
           \parsep=0pt}
   \item\relax\obeylines\breakable@texttt}
  {\endlist}
\def\breakable@texttt#1{%
  \ifx#1\end
  \expandafter\end
  \else
    \expandafter\ifx\bt@eol#1%
      #1%
    \else
      \string#1\hskip1sp
    \fi
    \expandafter\breakable@texttt
  \fi}
\makeatother

Then

\begin{breakabletexttt}
<long line 1>
<long line 2>
...
\end{breakabletexttt}

will print the lines breaking them when at the right margin. The \hfuzz=0.4em allows at most one character to stick out (the line width should be made an integer multiple of the monospaced font characters, or a flexible space should be added between characters instead of \hskip1sp).

EDIT: the following variant will respect spaces

\makeatletter
{\obeylines\gdef\bt@eol{^^M}}
\newenvironment{breakabletexttt}
  {\ttfamily\hfuzz=0.4em
   \list{}{\leftmargin=2em
           \itemindent=-\leftmargin
           \listparindent=-\leftmargin
           \parsep=0pt}
   \item\relax\obeylines\obeyspaces\expandafter\breakable@texttt\@gobble}
  {\endlist}
\def\breakable@texttt{\futurelet\@let@token\breakable@texttti}
\def\breakable@texttti#1{%
  \ifx\@let@token\end
  \expandafter\end
  \else
    \expandafter\ifx\bt@eol\@let@token
      \par
    \else
      \string#1\hskip1sp
    \fi
    \expandafter\breakable@texttt
  \fi}
\makeatother

It's important to code as

\begin{breakabletexttt}
line
...
\end{breakabletexttt}

with a new line after the \begin.


I give you one partial solution given to me here. The problem is that it does not work with special character like _. One partial solution is to change locally the catcode of _.

I've asked one question about this so as to automatiser automate this here.

% Source : http://forum.mathematex.net/latex-f6/forcer-le-retour-a-la-ligne-dans-texttt-t13246.html#p127511

\documentclass{article}
    \makeatletter
        \newcommand\breakabletexttt[1]{\texttt{\breakable@texttt#1\@nil}}
        \def\@gobble@fi#1\fi{\fi#1}
        \def\breakable@texttt#1#2\@nil{%
            #1\hspace{0pt plus 0.1pt minus 0.1pt}%
            \ifx\relax#2\relax
            %
            \else
            \@gobble@fi\breakable@texttt#2\@nil
            \fi
        }
    \makeatother


\begin{document}

\catcode`_=11
\breakabletexttt{33b7a2f7c4cc93c46dd4ee2ed81aa1eb?9409135542c79d1ed50c9fde07fa600a?cce5a2fe76bfbd0c48d79fb43a7106f0?263e9a8711c1400fb2a716a1b820ac9a}
\catcode`_=8        

\end{document}

Finally, here is one possible solution :

% Sources : 
%   * http://forum.mathematex.net/latex-f6/forcer-le-retour-a-la-ligne-dans-texttt-t13246.html#p127511
%   * https://tex.stackexchange.com/questions/33465/changing-the-catcode-of-in-one-command

\documentclass{article}
    \makeatletter
        \newcommand\breakabletexttt{\begingroup\catcode`\_12 \breakabletexttt@i}
        \newcommand\breakabletexttt@i[1]{\texttt{\breakable@texttt#1\@nil}\endgroup}
        \def\@gobble@fi#1\fi{\fi#1}
        \def\breakable@texttt#1#2\@nil{%
            #1\hspace{0pt plus 0.1pt minus 0.1pt}%
                \ifx\relax#2\relax
            \else
                \@gobble@fi\breakable@texttt#2\@nil
            \fi
        }
    \makeatother


\begin{document}


\breakabletexttt{33b7a2f7c4cc93c46dd4ee2ed81aa1eb?9409135542c79d1ed50c9fde07fa600a?cce5a2fe76bfbd0c48d79fb43a7106f0?263e9a8711c1400fb2a716a1b820ac9a}

\end{document}

I tried the solution in the update to the question but that didn't work when I tried it in a \lstset (it didn't compile). Afer a little searching, I came up with this and it seems to do the trick:

\lstset{various options,
        breaklines=true,
        literate={\-}{}{0\discretionary{-}{}{-}},
        various other options}