By default, you lose all indentation if you copy and past a code listing into a .tex file in TeXStudio. I found the way around this problem, set a config file option: https://sourceforge.net/p/texstudio/bugs/1209/
My suggestion is that, for code at least, maintaining the indentation should be the default. I can imagine that you want to autoindent other latex elements, but for computer source code I think the most common case is that pasting code should maintain the indentation. You could add yet another checkbox for "maintain indentation in code," on by default. I suspect this idea will be shot down, but I hope you consider more creative solutions (maybe a pop up when you first paste a code listing into a window?) - right now TeXStudio looks broken with this "remove code indentation" default in place.
See reproduction steps at bottom.
\begin{lstlisting}[
caption={This short example function illustrates the copy and paste problem}
language=someCode
]
static some_func() {
// indented code - when pasted into TeXStudio, indentation goes away.
float sqrt2 = sqrt(2.0f);
float radperdeg = M_PI / 180.f;
return;
}
\end{lstlisting}
\begin{lstlisting}[
caption={This short example function illustrates the copy and paste problem}
language=someCode
]
static some_func() {
// indented code - when pasted into TeXStudio, indentation goes away.
float sqrt2 = sqrt(2.0f);
float radperdeg = M_PI / 180.f;
return;
}
\end{lstlisting}
Take the lines below and copy them. Paste into TeXStudio. The indentation disappears.
\begin{lstlisting}[
caption={This short example function illustrates the copy and paste problem}
language=someCode
]
static some_func() {
// indented code - when pasted into TeXStudio, indentation goes away.
float sqrt2 = sqrt(2.0f);
float radperdeg = M_PI / 180.f;
return;
}
\end{lstlisting}
The feature you request could be nice. Meanwhile, you could make use of \lstinputlisting{<filename>}.
I can confirm this -- and I must admit I was surprised. At its core TS is a great text editor and a text editor should paste in whatever text you .. well... paste in. Whether it has preceding spaces in a line or otherwise. That is not a "feature".
Edit: OK I have now read your link. If you can change this behaviour I cannot see that it is a major problem. However I agree that normal text editor operation should be the default for something like this. Mangling or altering code should be a positive decision not the default.
Still present in 2.12.6.
I do not understand where I have to change something in the config file to have default indentation copying? I need help.
Thank you!
Configuration ----- Editor --- Indentation mode -- Keep indentation.
Works fine here. If not working for you you need to explain exactly what is not working and what you expect.
Thank you, I am blind you know sometimes!
Configuration ----- Editor --- Indentation mode -- Keep indentation.
Works fine here. If not working for you you need to explain exactly what is not working and what you expect.
The problem with this solution is in typing with autocomplete (e.g. new begin{itemize} block autocomplete), texStudio no longer indents the item for you.
The problem described in the first message in fact happens for any environment containing indented lines when the indentation mode is not set to Keep indentation
Configuration ----- Editor --- Indentation mode -- Keep indentation.
Works fine here. If not working for you you need to explain exactly what is not working and what you expect.
I don't think it's the right solution (morally). The indentation mode should be about what happens when you are typing (in particular, pressing "enter").
Copy-pasting is something different: the text should be kept as it is and not be reformatted automatically. I could imagine an option to reformat or a special pasting option, but not by default. So I think relating pasting and the indentation mode is a real problem.
Most helpful comment
Configuration ----- Editor --- Indentation mode -- Keep indentation.
Works fine here. If not working for you you need to explain exactly what is not working and what you expect.