I learned how to create a list of codes on my thesis on the question Setting Entries of List of Listings in LaTeX. Package Listings and I put that command on another file with english tiles as \lstlistingname{Code} and \lstlistlistingname{List of Codes}.
However my thesis is written in multiple languages, therefore later I need to recreate the code table titles \lstlistingname{Códigos} and \lstlistlistingname{Lista de Códigos} when I switch the language of the thesis as explained on the question Is it possible to keep my translation together with original text?
% Calculate the size of the header
\newcommand{\calculatelisteningsheader}
{
\renewcommand\cftlstlistingpresnum{\lstlistingname~}
\settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}
\addtolength\cftlstlistingnumwidth{\mylen} %
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}
}
On this following example, I created the command \calculatelisteningsheader to reevaluate the headings size of the titles:
\documentclass[12pt,openright,oneside,a4paper]{abntex2}
\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
% Setting Entries of List of Listings in LaTeX. Package Listings
% http://tex.stackexchange.com/questions/228936/setting-entries-of-list-of-listings
\newlength\mylen
\renewcommand\lstlistingname{Code}
\renewcommand\lstlistlistingname{List of Codes}
\begingroup
\makeatletter
\let\newcounter\@gobble\let\setcounter\@gobbletwo
\globaldefs\@ne \let\c@loldepth\@ne
\newlistof{listings}{lol}{\lstlistlistingname}
\newlistof{lstlistoflistings}{lol}{\lstlistlistingname}
\newlistentry{lstlisting}{lol}{0}
\makeatother
\endgroup
% Calculate the size of the header
\newcommand{\calculatelisteningsheader}
{
\renewcommand\cftlstlistingpresnum{\lstlistingname~}
\settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}
\addtolength\cftlstlistingnumwidth{\mylen} %
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}
}
% Ensure it is called at least one time
\calculatelisteningsheader
% Later...
\renewcommand\lstlistingname{Code}
\renewcommand\lstlistlistingname{List of Codes}
\calculatelisteningsheader
\begin{document}
\lstlistoflistings
\begin{lstlisting}[caption={First Code}]
code1
\end{lstlisting}
\begin{lstlisting}[caption={Second Code}]
code2
\end{lstlisting}
\end{document}
However something is going wrong. The size of the empty space is more than doubled on the second time I call \calculatelisteningsheader:
Why the space size is increasing each call to \calculatelisteningsheader?
Update
As suggested by @koleygrso, I already tried removing the indentation and adding a % on the \calculatelisteningsheader but it stills the same:
\newcommand{\calculatelisteningsheader}
{%
\renewcommand\cftlstlistingpresnum{\lstlistingname~}
\settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}
\addtolength\cftlstlistingnumwidth{\mylen} %
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}
}


