patch 9.1.1145: multi-line completion has wrong indentation for last line

Problem:  When expanding omni completion items with newlines (e.g.
          `then\n\t\nend`), the end statement gets wrong indentation.
Solution: Add OPENLINE_FORCE_INDENT flag to make open_line() use
          second_line_indent directly (glepnir)

closes: #16614

Signed-off-by: glepnir <glephunter@gmail.com>
Signed-off-by: Justin M. Keyes <justinkz@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/change.c b/src/change.c
index 294adf7..a3329bd 100644
--- a/src/change.c
+++ b/src/change.c
@@ -1385,6 +1385,8 @@
  *	    OPENLINE_KEEPTRAIL	keep trailing spaces
  *	    OPENLINE_MARKFIX	adjust mark positions after the line break
  *	    OPENLINE_COM_LIST	format comments with list or 2nd line indent
+ *	    OPENLINE_FORCE_INDENT  set indent from second_line_indent, ignore
+ *				   'autoindent'
  *
  * "second_line_indent": indent for after ^^D in Insert mode or if flag
  *			  OPENLINE_COM_LIST
@@ -1498,9 +1500,11 @@
     if (dir == FORWARD && did_ai)
 	trunc_line = TRUE;
 
+    if ((flags & OPENLINE_FORCE_INDENT) && second_line_indent >= 0)
+	newindent = second_line_indent;
     // If 'autoindent' and/or 'smartindent' is set, try to figure out what
     // indent to use for the new line.
-    if (curbuf->b_p_ai || do_si)
+    else if (curbuf->b_p_ai || do_si)
     {
 	// count white space on current line
 #ifdef FEAT_VARTABS
diff --git a/src/insexpand.c b/src/insexpand.c
index deae1ce..edc4265 100644
--- a/src/insexpand.c
+++ b/src/insexpand.c
@@ -4473,6 +4473,7 @@
 {
     char_u	*start = str;
     char_u	*curr = str;
+    int		base_indent = get_indent();
 
     while (*curr != NUL)
     {
@@ -4483,7 +4484,7 @@
 		ins_char_bytes(start, (int)(curr - start));
 
 	    // Handle newline
-	    open_line(FORWARD, OPENLINE_KEEPTRAIL, FALSE, NULL);
+	    open_line(FORWARD, OPENLINE_KEEPTRAIL | OPENLINE_FORCE_INDENT, base_indent, NULL);
 	    start = curr + 1;
 	}
 	curr++;
diff --git a/src/testdir/dumps/Test_pum_with_special_characters_09.dump b/src/testdir/dumps/Test_pum_with_special_characters_09.dump
new file mode 100644
index 0000000..7a6cac7
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_with_special_characters_09.dump
@@ -0,0 +1,12 @@
+|l+0&#ffffff0|o|c|a|l| |a| |=| |f+0#ff404010&|u|n|c| |(|)| +0#0000000&@57
+| +0#ff404010&@1| +0#0000000&@72
+|e+0#ff404010&|n|d> +0#0000000&@71
+|~+0#4040ff13&| | +0#0000001#e0e0e08|f|u|n|c|t|i|o|n| |(|)| @3| +0#4040ff13#ffffff0@56
+|~| | +0#0000001#ffd7ff255|f|o@1|b|a|r| @8| +0#4040ff13#ffffff0@56
+|~| | +0#0000001#ffd7ff255|你*&|好|^+&|@| @1|^|@|我*&|好| +&| +0#4040ff13#ffffff0@56
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |O|m|n|i| |c|o|m|p|l|e|t|i|o|n| |(|^|O|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |1| |o|f| |3| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_with_special_characters_10.dump b/src/testdir/dumps/Test_pum_with_special_characters_10.dump
new file mode 100644
index 0000000..ccaea76
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_with_special_characters_10.dump
@@ -0,0 +1,12 @@
+|l+0&#ffffff0|o|c|a|l| |a| |=| |f|u|n|c| |(|)| @57
+@75
+|e|n|d> @71
+|~+0#4040ff13&| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |I|N|S|E|R|T| |-@1| +0&&@44|3|,|4| @10|A|l@1| 
diff --git a/src/testdir/dumps/Test_pum_with_special_characters_11.dump b/src/testdir/dumps/Test_pum_with_special_characters_11.dump
new file mode 100644
index 0000000..f6d8387
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_with_special_characters_11.dump
@@ -0,0 +1,12 @@
+|l+0&#ffffff0|o|c|a|l| |a| |=| |f|u|n|c| |(|)| @57
+@2|l|o|c|a|l| |b| |=| |f+0#ff404010&|u|n|c| |(|)| +0#0000000&@55
+| +0#ff404010&@3| +0#0000000&@70
+| +0#ff404010&@1|e|n|d> +0#0000000&@69
+|e|n|d| | +0#0000001#e0e0e08|f|u|n|c|t|i|o|n| |(|)| @3| +0#0000000#ffffff0@54
+|~+0#4040ff13&| @2| +0#0000001#ffd7ff255|f|o@1|b|a|r| @8| +0#4040ff13#ffffff0@54
+|~| @2| +0#0000001#ffd7ff255|你*&|好|^+&|@| @1|^|@|我*&|好| +&| +0#4040ff13#ffffff0@54
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |O|m|n|i| |c|o|m|p|l|e|t|i|o|n| |(|^|O|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |1| |o|f| |3| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_with_special_characters_12.dump b/src/testdir/dumps/Test_pum_with_special_characters_12.dump
new file mode 100644
index 0000000..5807526
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_with_special_characters_12.dump
@@ -0,0 +1,12 @@
+|l+0&#ffffff0|o|c|a|l| |a| |=| |f|u|n|c| |(|)| @57
+@2|l|o|c|a|l| |b| |=| |f|u|n|c| |(|)| @55
+@75
+@2|e|n|d> @69
+|e|n|d| @71
+|~+0#4040ff13&| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |I|N|S|E|R|T| |-@1| +0&&@44|4|,|5|-|6| @8|A|l@1| 
diff --git a/src/testdir/test_popup.vim b/src/testdir/test_popup.vim
index 63f0016..82d2ef0 100644
--- a/src/testdir/test_popup.vim
+++ b/src/testdir/test_popup.vim
@@ -1946,6 +1946,23 @@
   call VerifyScreenDump(buf, 'Test_pum_with_special_characters_08', {})
   call term_sendkeys(buf, "\<C-E>\<Esc>")
 
+  call term_sendkeys(buf, ":setlocal autoindent tabstop=2 shiftwidth=2\<CR>")
+  call term_sendkeys(buf, "Slocal a = \<C-X>\<C-O>")
+  call TermWait(buf, 50)
+  call VerifyScreenDump(buf, 'Test_pum_with_special_characters_09', {})
+
+  call term_sendkeys(buf, "\<C-Y>")
+  call TermWait(buf, 50)
+  call VerifyScreenDump(buf, 'Test_pum_with_special_characters_10', {})
+
+  call term_sendkeys(buf, "\<ESC>kAlocal b = \<C-X>\<C-O>")
+  call TermWait(buf, 50)
+  call VerifyScreenDump(buf, 'Test_pum_with_special_characters_11', {})
+
+  call term_sendkeys(buf, "\<C-Y>")
+  call TermWait(buf, 50)
+  call VerifyScreenDump(buf, 'Test_pum_with_special_characters_12', {})
+
   call StopVimInTerminal(buf)
 endfunc
 
diff --git a/src/version.c b/src/version.c
index 5652fc3..0adb652 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1145,
+/**/
     1144,
 /**/
     1143,
diff --git a/src/vim.h b/src/vim.h
index 2ebc8fe..da2835c 100644
--- a/src/vim.h
+++ b/src/vim.h
@@ -1175,12 +1175,13 @@
 #define INSCHAR_COM_LIST 16	// format comments with list/2nd line indent
 
 // flags for open_line()
-#define OPENLINE_DELSPACES  0x01    // delete spaces after cursor
-#define OPENLINE_DO_COM	    0x02    // format comments
-#define OPENLINE_KEEPTRAIL  0x04    // keep trailing spaces
-#define OPENLINE_MARKFIX    0x08    // fix mark positions
-#define OPENLINE_COM_LIST   0x10    // format comments with list/2nd line indent
-#define OPENLINE_FORMAT	    0x20    // formatting long comment
+#define OPENLINE_DELSPACES	0x01    // delete spaces after cursor
+#define OPENLINE_DO_COM		0x02    // format comments
+#define OPENLINE_KEEPTRAIL	0x04    // keep trailing spaces
+#define OPENLINE_MARKFIX	0x08    // fix mark positions
+#define OPENLINE_COM_LIST	0x10    // format comments with list/2nd line indent
+#define OPENLINE_FORMAT		0x20    // formatting long comment
+#define OPENLINE_FORCE_INDENT	0x40    // use second_line_indent without indent logic
 
 // There are five history tables:
 #define HIST_CMD	0	// colon commands