changeset 26167:7108cee19a2c v8.2.3615

patch 8.2.3615: wrong indent in first line if re-formatting with indent expr Commit: https://github.com/vim/vim/commit/818ff25cd1aabf60b9cd239da2f1328a959954f7 Author: Christian Brabandt <cb@256bit.org> Date: Thu Nov 18 13:56:37 2021 +0000 patch 8.2.3615: wrong indent in first line if re-formatting with indent expr 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)
author Bram Moolenaar <Bram@vim.org>
date Thu, 18 Nov 2021 15:00:08 +0100
parents 1eea75611a20
children 105ab8907ae2
files src/testdir/test_indent.vim src/textformat.c src/version.c
diffstat 3 files changed, 101 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/testdir/test_indent.vim
+++ b/src/testdir/test_indent.vim
@@ -142,4 +142,79 @@ func Test_modeline_indent_expr()
   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
--- a/src/textformat.c
+++ b/src/textformat.c
@@ -1070,9 +1070,32 @@ format_lines(
 	    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
-		    (void)set_indent(get_indent(), SIN_CHANGED);
+# 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
--- a/src/version.c
+++ b/src/version.c
@@ -758,6 +758,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    3615,
+/**/
     3614,
 /**/
     3613,