[vim/vim] ft=tex — hanging indent within \begin{}/\end{end} (Issue #9056)

26 views
Skip to first unread message

martin f. krafft

unread,
Oct 26, 2021, 11:18:42 PM10/26/21
to vim/vim, Subscribed

Steps to reproduce

  1. Open file like below;
  2. Ensure ft=tex is recognised;
  3. Run gq on the various paragraphs

Expected behaviour

I expect all paragraphs to be indented properly without a hanging indent, unless I configure such a hanging indent somehow.

I do not expect that the paragraphs within the \begin{center}/\end{center} keywords are indented differently. Why?

Sample file:

\documentclass{article}

\usepackage[latin]{babel}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut enim non
libero efficitur aliquet. Maecenas metus justo, facilisis convallis blandit
non, semper eu urna. Suspendisse diam diam, iaculis faucibus lorem eu,
fringilla condimentum lectus. Quisque euismod diam at convallis vulputate.
Pellentesque laoreet tortor sit amet mauris euismod ornare. Sed varius
bibendum orci vel vehicula. Pellentesque tempor, ipsum et auctor accumsan,
metus lectus ultrices odio, sed elementum mi ante at arcu.

\begin{center}

Proin nec risus consequat nunc dapibus consectetur. Mauris lacinia est a augue
  tristique accumsan. Morbi pretium, felis molestie eleifend condimentum, arcu
  ipsum congue nisl, quis euismod purus libero in ante. Donec id semper purus.
  Suspendisse eget aliquam nunc. Maecenas fringilla mauris vitae maximus
  condimentum. Cras a quam in mi dictum eleifend at a lorem. Sed convallis
  ante a commodo facilisis. Nam suscipit vulputate odio, vel dapibus nisl
  dignissim facilisis. Vestibulum ante ipsum primis in faucibus orci luctus et
  ultrices posuere cubilia curae;

Vestibulum imperdiet maximus mi, id elementum odio. Aenean et odio tristique
  urna lacinia iaculis. Integer facilisis finibus nisl at porttitor. Aenean
  sollicitudin nibh sed arcu tincidunt volutpat. Vestibulum id sem ornare,
  auctor augue in, finibus lacus. Curabitur sed varius lorem. Fusce et
  consequat orci. Phasellus hendrerit, risus vitae condimentum volutpat,
  turpis risus tristique quam, a bibendum eros tortor id dolor. Integer diam
  augue, fermentum et nisi sed, facilisis mollis massa. Praesent arcu urna,
  maximus vulputate vehicula eu, rutrum a elit. Quisque in semper tellus.
  Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere
  cubilia curae;

\end{center}

Cras malesuada mi nulla, non laoreet massa gravida pretium. Proin id suscipit
orci. Suspendisse luctus ultrices dolor. Phasellus quis bibendum risus. Donec
at ex cursus, condimentum mi non, efficitur augue. In hendrerit libero lorem,
sollicitudin ornare ante imperdiet vel. Maecenas pellentesque, dolor ac
maximus egestas, urna felis finibus velit, at blandit magna turpis non dui.

\end{documnt}

Operating system

Debian unstable

Version of Vim

8.2.2434

Logs and stack traces

No response


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

Bram Moolenaar

unread,
Nov 5, 2021, 8:12:40 PM11/5/21
to vim/vim, Subscribed

Christian Brabandt

unread,
Nov 6, 2021, 3:30:52 AM11/6/21
to vim/vim, Subscribed

I think this is not an indenting issue. This is about gq to format the paragraph. So Vim uses formatexpr/formatprg or falls back to internal formatting. So I think this is not about a runtime issue.

martin f. krafft

unread,
Nov 6, 2021, 7:34:50 PM11/6/21
to vim/vim, Subscribed

I think this is not an indenting issue. This is about gq to format the paragraph. So Vim uses formatexpr/formatprg or falls back to internal formatting. So I think this is not about a runtime issue.

Neither formatprg nor formatexpr are set. In that case, "formatting is done internally". If I set ft=plaintex, then gq works fine, and it also doesn't do this funky indenting anywhere else, so I do think this is specific to ft=tex.

Christian Brabandt

unread,
Nov 7, 2021, 8:49:02 AM11/7/21
to vim/vim, Subscribed

Neither formatprg nor formatexpr are set. In that case, "formatting is done internally". If I set ft=plaintex, then gq works fine, and it also doesn't do this funky indenting anywhere else, so I do think this is specific to ft=tex.

What I was trying to say is: It's not the indent script to blame here, since we are using gq to format and therefore the ping to the indentscript author is not helping. BTW: I see the same issue with ft=plaintex and vim --clean. And the filetype plugins for tex and plaintex do not show anything obvious that might cause this. So I think it's the internal formatting of Vim that is causing this for some reason.

Christian Brabandt

unread,
Nov 7, 2021, 10:13:59 AM11/7/21
to vim/vim, Subscribed

Oh, I stand corrected. When Vim is formatting the lines, it will make use of the indentexpr. So it calls GetTexIndent() which will eventually call GetLastBeginIndentation() which will then return an additional indentation level of +1. So this is an issue of the indent script for tex, therefore pinging @zhou13 is correct and this needs to be fixed in the indent script. Sorry for the confusion.

So a workaround is to set :set indentexpr= before doing gq

Yichao Zhou

unread,
Nov 8, 2021, 1:23:21 PM11/8/21
to vim/vim, Subscribed

This is strange to me. If I run gg=G on this file, everything is indented properly. If I place my cursor on the first line of the paragraph and press ==, it also indents properly. I am not sure things do not work out with gq. Maybe gq does not run the indentexpr for the first line?

Christian Brabandt

unread,
Nov 8, 2021, 2:02:18 PM11/8/21
to vim/vim, Subscribed

yes, I have run it through a debugger. Vim joins the lines together and adds a linebreak and then uses the indentexpression for the line after the newly inserted line break. so not running the indentexpression through the first line of the paragraph.

Christian Brabandt

unread,
Nov 11, 2021, 4:38:58 PM11/11/21
to vim/vim, Subscribed

Okay, so how about the following patch:

diff --git a/src/textformat.c b/src/textformat.c
index 9b96b7fdb..c0bbaac5b 100644
--- a/src/textformat.c
+++ b/src/textformat.c
@@ -956,6 +956,7 @@ format_lines(
     int                need_set_indent = TRUE; // set indent of next paragraph
     int                force_format = FALSE;
     int                old_State = State;
+    int                indent = 0; // do indent

     // length of a line to force formatting: 3 * 'tw'
     max_len = comp_textwidth(TRUE) * 3;
@@ -1070,9 +1071,23 @@ format_lines(
            if (is_end_par || force_format)
            {
                if (need_set_indent)
+               {
                    // replace indent in first line with minimal number of
                    // tabs and spaces, according to current options
-                   (void)set_indent(get_indent(), SIN_CHANGED);
+# ifdef FEAT_LISP
+                   if (curbuf->b_p_lisp)
+                       indent = get_lisp_indent();
+                   else
+# endif
+#ifdef FEAT_CINDENT
+                       indent =
+#ifdef FEAT_EVAL
+                       *curbuf->b_p_inde != NUL ? get_expr_indent() :
+#endif
+                       get_c_indent();
+                   set_indent(indent, SIN_CHANGED);
+#endif
+               }

                // put cursor on last non-space
                State = NORMAL; // don't go past end-of-line

This should make sure to use proper indentation for the first line.

Christian Brabandt

unread,
Nov 11, 2021, 4:40:42 PM11/11/21
to vim/vim, Subscribed

I'll prepare a PR for this with a test

Bram Moolenaar

unread,
Nov 16, 2021, 1:01:23 PM11/16/21
to vim/vim, Subscribed

Before using b_p_inde and calling get_c_indent() it should check that cindent_on() returns TRUE.
If not, then it should call get_indent() as before.
Compare with the fix_indent() function.

Christian Brabandt

unread,
Nov 17, 2021, 3:06:01 PM11/17/21
to vim/vim, Subscribed

thanks, I have adjusted the logic and created PR #9150

Bram Moolenaar

unread,
Nov 18, 2021, 8:57:31 AM11/18/21
to vim/vim, Subscribed

Closed #9056 via 818ff25.

Reply all
Reply to author
Forward
0 new messages