The following MCE points out that it is possible to make the listings' line numbers non selectable (thanks to the accsupp package) in case of the beamer class.
But, in case of the standard classes (e.g. article) and if hyperref is used, the following error is thrown:
! TeX capacity exceeded, sorry [input stack size=5000].
\normalfont ...ault \seriesdefault \shapedefault
\relax
l.18 Foo
Do you know why and how to circumvent this limitation?
\documentclass{beamer}
% \documentclass{article}
\usepackage{listings}
\usepackage{accsupp}
\usepackage{hyperref}
%
\DeclareRobustCommand\nonselectable[1]{%
\BeginAccSupp{method=plain,ActualText={}}#1\EndAccSupp{}%
}
\renewcommand*\thelstnumber{%
\nonselectable{\the\value{lstnumber}}%
}
%
\begin{document}
\begin{lstlisting}[numbers=left]
Foo
\end{lstlisting}
\end{document}