Patch 8.2.3615

30 views
Skip to first unread message

Bram Moolenaar

unread,
Nov 18, 2021, 8:57:08 AM11/18/21
to vim...@googlegroups.com

Patch 8.2.3615
Problem: When re-formatting with an indent expression the first line of a
paragraph may get the wrong indent. (Martin F. Krafft)
Solution: Apply the correct indenting function for the first line.
(Christian Brabandt, closes #9150, closes #9056)
Files: src/textformat.c, src/testdir/test_indent.vim


*** ../vim-8.2.3614/src/textformat.c 2020-10-24 19:49:37.502683026 +0100
--- src/textformat.c 2021-11-18 13:54:06.595953779 +0000
***************
*** 1070,1078 ****
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);

// put cursor on last non-space
State = NORMAL; // don't go past end-of-line
--- 1070,1101 ----
if (is_end_par || force_format)
{
if (need_set_indent)
+ {
+ int indent = 0; // amount of indent needed
+
// replace indent in first line with minimal number of
// tabs and spaces, according to current options
! # ifdef FEAT_LISP
! if (curbuf->b_p_lisp)
! indent = get_lisp_indent();
! else
! # endif
! {
! #ifdef FEAT_CINDENT
! if (cindent_on())
! {
! indent =
! # ifdef FEAT_EVAL
! *curbuf->b_p_inde != NUL ? get_expr_indent() :
! # endif
! get_c_indent();
! }
! else
! #endif
! indent = get_indent();
! }
! (void)set_indent(indent, SIN_CHANGED);
! }

// put cursor on last non-space
State = NORMAL; // don't go past end-of-line
*** ../vim-8.2.3614/src/testdir/test_indent.vim 2020-03-19 19:33:29.785091518 +0000
--- src/testdir/test_indent.vim 2021-11-18 13:52:52.240069084 +0000
***************
*** 142,145 ****
--- 142,220 ----
call delete('Xfile.txt')
endfunc

+ func Test_indent_func_with_gq()
+
+ function GetTeXIndent()
+ " Sample indent expression for TeX files
+ let lnum = prevnonblank(v:lnum - 1)
+ " At the start of the file use zero indent.
+ if lnum == 0
+ return 0
+ endif
+ let line = getline(lnum)
+ let ind = indent(lnum)
+ " Add a 'shiftwidth' after beginning of environments.
+ if line =~ '\\begin{center}'
+ let ind = ind + shiftwidth()
+ endif
+ return ind
+ endfunction
+
+ new
+ setl et sw=2 sts=2 ts=2 tw=50 indentexpr=GetTeXIndent()
+ put =[ '\documentclass{article}', '', '\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;', '', '']
+ 1d_
+ call cursor(5, 1)
+ ka
+ call cursor(15, 1)
+ kb
+ norm! 'agqap
+ norm! 'bgqap
+ let expected = [ '\documentclass{article}', '', '\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;', '', '']
+ call assert_equal(expected, getline(1, '$'))
+
+ bwipe!
+ delmark ab
+ delfunction GetTeXIndent
+ endfu
+
" vim: shiftwidth=2 sts=2 expandtab
*** ../vim-8.2.3614/src/version.c 2021-11-17 20:39:29.135019718 +0000
--- src/version.c 2021-11-18 13:55:10.011855329 +0000
***************
*** 759,760 ****
--- 759,762 ----
{ /* Add new patch number below this line */
+ /**/
+ 3615,
/**/

--
There are 10 kinds of people: Those who understand binary and those who don't.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Gary Johnson

unread,
Dec 5, 2021, 2:50:57 PM12/5/21
to vim...@googlegroups.com
On 2021-11-18, Bram Moolenaar wrote:
> Patch 8.2.3615
> Problem: When re-formatting with an indent expression the first line of a
> paragraph may get the wrong indent. (Martin F. Krafft)
> Solution: Apply the correct indenting function for the first line.
> (Christian Brabandt, closes #9150, closes #9056)
> Files: src/textformat.c, src/testdir/test_indent.vim

This new behavior is a bug. It has made vim less useful.

When I want to format, I want to format. When I want to indent,
I want to indent. I don't want formatting, e.g., gq, to also
indent.

I frequently have text that I have manually indented to my liking,
and I then want to format it so that the lines are properly wrapped.
I want those lines wrapped according to the current indentation, not
the current indentation rules. If I want it indented according to
the current indentation rules, I will indent it with = first.

Please fix this.

Regards,
Gary

Christian Brabandt

unread,
Dec 6, 2021, 3:12:42 AM12/6/21
to vim...@googlegroups.com
The old behaviour already applied the indenting settings to all lines
*except* for the very first line, which caused inconsistent indented
lines. The patch simply makes sure to also apply the indenting to the
very first line.

I was also a bit confused that indenting applied to lines being
formatted because I always thought indenting and auto-formatting are
some different features. But I believe when auto-formatting and Vim is
breaking a very long line, it does make sense to apply the indenting
settings to the line being split.

Changing this behaviour now may break other users expectations. So I am
not sure how to fix this.

Best,
Christian
--
Der Teich kippt um, die Fische üben bereits Rückenschwimmen.

Bram Moolenaar

unread,
Dec 6, 2021, 6:04:35 AM12/6/21
to vim...@googlegroups.com, Christian Brabandt
I'm not sure why you set up auto-indenting and do not want it to be
applied. Auto-indenting triggers quite often, especially when adding a
linke break, since then the indent for the next line has to be decided.
And the line before is now done, so usually it also needs the indent to
be recomputed.

I guess the auto-indenting is not doing what you want, so you would need
to fix how it works.

--
Q: What's a light-year?
A: One-third less calories than a regular year.

Gary Johnson

unread,
Dec 6, 2021, 12:13:25 PM12/6/21
to vim...@googlegroups.com
The way it used to work, the way it worked for as long as I can
remember, was to follow the indentation of the first line. So
I could set the first line to whatever indentation I wanted, then
reformat that line and any subsequent lines to have the desired
indent, text width and comment leader.

Most of the time, Vim's auto-indenting creates the desired
indentation, so most of the time it does do what I want. But there
are exceptions.

Here is a common example in C.

foo()
{
int x; // Here is an end-of-line comment
// beginning at column 41 and
// extending for a few more lines.

If I want to edit that comment after the first line, or change the
indent amount, I could do so and reformat the comment with gq
starting on the second line. The rest of the lines would be aligned
under that line. Now the indentation is shifted to be under the
"int". The only way I know of to correct the indentation is to
shift that line back where I want it, manually find the right place
to break the line, break it there, then reformat the third and
subsequent lines with gq.

Vim used to do most of that for me automatically. At least what it
did helped me and didn't hinder my editing.

Another example is Vim script.

set tw=66 " Comment explaining why 66.
map this that
" Can't put an end-of-line comment on the
" same line as a mapping, so put it here.

I can't reformat that second comment anymore because Vim now insists
on changing the indent.

The indentation rules were pretty simple: Vim chose a place for the
indentation of a line. If I didn't like that place, for whatever
reason, I could move the indentation to where I wanted it. Then
I could reformat that line and any subsequent lines with gq and
everything was good. Now, gq re-indents the first line of the
region, which is not its purpose, and there is no way that I know of
to fix that.

I haven't read the initial discussion recently, but I don't really
understand the problem with the way indentation and formatting used
to work. If I was just typing code or text, they "just worked". If
I needed to change the indentation of something, gq also "just
worked" afterward because it reformatted according to the
indentation of the first line of the region.

My expectation is that the behavior of gq not be broken. Add an
option to set the behavior if you want. Add a plugin to change it
if you want. But don't change the way it has always has always been
in a way that is not an improvement for everyone without a way to
have the traditional behavior.

Regards,
Gary

Bram Moolenaar

unread,
Dec 6, 2021, 1:09:04 PM12/6/21
to vim...@googlegroups.com, Gary Johnson
But if you try to indent the second line, or use "gq" on the first, then
it doesn't line up as you like. This is a missing feature in 'cindent'.
Even if we fix the problem you refer to.

> The only way I know of to correct the indentation is to
> shift that line back where I want it, manually find the right place
> to break the line, break it there, then reformat the third and
> subsequent lines with gq.
>
> Vim used to do most of that for me automatically. At least what it
> did helped me and didn't hinder my editing.
>
> Another example is Vim script.
>
> set tw=66 " Comment explaining why 66.
> map this that
> " Can't put an end-of-line comment on the
> " same line as a mapping, so put it here.
>
> I can't reformat that second comment anymore because Vim now insists
> on changing the indent.

True, this one can't be fixed, the indent is set manually.

> The indentation rules were pretty simple: Vim chose a place for the
> indentation of a line. If I didn't like that place, for whatever
> reason, I could move the indentation to where I wanted it. Then
> I could reformat that line and any subsequent lines with gq and
> everything was good. Now, gq re-indents the first line of the
> region, which is not its purpose, and there is no way that I know of
> to fix that.

In the second example I have to agree.

> I haven't read the initial discussion recently, but I don't really
> understand the problem with the way indentation and formatting used
> to work. If I was just typing code or text, they "just worked". If
> I needed to change the indentation of something, gq also "just
> worked" afterward because it reformatted according to the
> indentation of the first line of the region.
>
> My expectation is that the behavior of gq not be broken. Add an
> option to set the behavior if you want. Add a plugin to change it
> if you want. But don't change the way it has always has always been
> in a way that is not an improvement for everyone without a way to
> have the traditional behavior.

Please check issue https://github.com/vim/vim/issues/9056
This is a valid problem, when formatting several lines of text the
indent of the first line of a block is wrong, because the formatting
doesn't change it. You can indent all the lines, but then the
formatting is wrong, the lines will be too long.

We need to have a way to indent and format at the same time. Perhaps a
solution is to not change the indent of the very first line, since we
can assume it's either correct or the user could have set it manually.
But do indent all the lines after it. I think that solves both
problems.

--
If someone questions your market projections, simply point out that your
target market is "People who are nuts" and "People who will buy any damn
thing". Nobody is going to tell you there aren't enough of those people
to go around.
(Scott Adams - The Dilbert principle)

Gary Johnson

unread,
Dec 6, 2021, 7:57:05 PM12/6/21
to vim...@googlegroups.com
I had a look at the issue and couldn't understand it at first
because I didn't see the problem--the OP's example file indented
just fine, without any hanging indents after gggqG. Then I repeated
the experiment without my usual configuration:

$ vim -N -u NONE -i NONE --cmd 'filetype plugin indent on' indent_issue.tex

gggqG left the hanging indents. Then I checked 'indentexpr'. For
the bad-indentation case:

:verbose set indentexpr?
indentexpr=GetTeXIndent()
Last set from /usr/local/share/vim/vim82/indent/tex.vim line 166

but for my usual setup, which reformatted without the hanging
indents:

:verbose set indentexpr?
indentexpr=GetTeXIndent()
Last set from ~/.vim/vim-latex/indent/tex.vim line 125

I was using a Vim without patch 8.2.3615.

So the issue may be with Vim's default GetTeXIndent() and not
necessarily with gq.

Regards,
Gary

John Little

unread,
Dec 9, 2021, 4:02:03 PM12/9/21
to vim_dev
This change was an unexpected problem for me.  Until I stumbled on this thread I'd no idea is was due to a change in vim.  gq started unindenting the yaml subset files I'm working with:

    doc:
      Lorem ipsum dolor sit amet,
      consectetur adipiscing elit

The doc tags often have to be reformatted.
Turning off cindent is a fix, but the files have source code in some elements and these are affected.

Regards, John Little

Bram Moolenaar

unread,
Dec 9, 2021, 4:29:30 PM12/9/21
to vim...@googlegroups.com, John Little
Can you be more specific about what the problem is?
Can you give an example file and the commands you use?
What worked in the past and what stopped working?

--
Computers are useless. They can only give you answers.
-- Pablo Picasso

Christian Brabandt

unread,
Dec 10, 2021, 4:07:25 AM12/10/21
to vim_dev
Patch 8.2.3615 has been amended a bit by Patch 8.2.3754 so please check
latest version if this is still problematic for you.

Thanks,
Chris
--
Ein Rezensent lieset alle Satiren gegen Rezensenten, die früher als er
geschaffen worden, kalt.
-- Jean Paul

John Little

unread,
Dec 11, 2021, 12:31:10 AM12/11/21
to vim_dev
On Friday, December 10, 2021 at 10:07:25 PM UTC+13 cbl...@256bit.org wrote:

Patch 8.2.3615 has been amended a bit by Patch 8.2.3754 so please check
latest version if this is still problematic for you.

Yes, after 8.2.3754 the problem ended.  I was unlucky in that my vim update cadence stepped in between those patches.
Reply all
Reply to author
Forward
0 new messages