blob: 018565f72417c7b6b922ea4e4b091359bc16ed51 [file] [log] [blame]
Bram Moolenaar11abd092020-05-01 14:26:37 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * textformat.c: text formatting functions
12 */
13
14#include "vim.h"
15
16static int did_add_space = FALSE; // auto_format() added an extra space
17 // under the cursor
18
19#define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
20
21/*
22 * Return TRUE if format option 'x' is in effect.
23 * Take care of no formatting when 'paste' is set.
24 */
25 int
26has_format_option(int x)
27{
28 if (p_paste)
29 return FALSE;
30 return (vim_strchr(curbuf->b_p_fo, x) != NULL);
31}
32
33/*
34 * Format text at the current insert position.
35 *
36 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
37 * will be the comment leader length sent to open_line().
38 */
39 void
40internal_format(
41 int textwidth,
42 int second_indent,
43 int flags,
44 int format_only,
45 int c) // character to be inserted (can be NUL)
46{
47 int cc;
Bram Moolenaare52702f2020-06-04 18:22:13 +020048 int skip_pos;
Bram Moolenaar11abd092020-05-01 14:26:37 +020049 int save_char = NUL;
50 int haveto_redraw = FALSE;
51 int fo_ins_blank = has_format_option(FO_INS_BLANK);
52 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
Bram Moolenaare52702f2020-06-04 18:22:13 +020053 int fo_rigor_tw = has_format_option(FO_RIGOROUS_TW);
Bram Moolenaar11abd092020-05-01 14:26:37 +020054 int fo_white_par = has_format_option(FO_WHITE_PAR);
55 int first_line = TRUE;
56 colnr_T leader_len;
57 int no_leader = FALSE;
58 int do_comments = (flags & INSCHAR_DO_COM);
Christian Brabandtbba79802024-04-11 22:54:44 +020059 int safe_tw = trim_to_int(8 * (vimlong_T)textwidth);
Bram Moolenaar11abd092020-05-01 14:26:37 +020060#ifdef FEAT_LINEBREAK
61 int has_lbr = curwin->w_p_lbr;
Christian Brabandt86ef8152024-04-27 11:55:08 +020062 int has_bri = curwin->w_p_bri;
Bram Moolenaar11abd092020-05-01 14:26:37 +020063
64 // make sure win_lbr_chartabsize() counts correctly
65 curwin->w_p_lbr = FALSE;
Christian Brabandt86ef8152024-04-27 11:55:08 +020066 curwin->w_p_bri = FALSE;
Bram Moolenaar11abd092020-05-01 14:26:37 +020067#endif
68
69 // When 'ai' is off we don't want a space under the cursor to be
70 // deleted. Replace it with an 'x' temporarily.
71 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
72 {
73 cc = gchar_cursor();
74 if (VIM_ISWHITE(cc))
75 {
76 save_char = cc;
77 pchar_cursor('x');
78 }
79 }
80
81 // Repeat breaking lines, until the current line is not too long.
82 while (!got_int)
83 {
84 int startcol; // Cursor column at entry
85 int wantcol; // column at textwidth border
86 int foundcol; // column for start of spaces
87 int end_foundcol = 0; // column for start of word
88 colnr_T len;
89 colnr_T virtcol;
90 int orig_col = 0;
91 char_u *saved_text = NULL;
92 colnr_T col;
93 colnr_T end_col;
94 int wcc; // counter for whitespace chars
Bram Moolenaar6e371ec2021-12-12 14:16:39 +000095 int did_do_comment = FALSE;
kawaii-Code78019df2024-01-25 21:40:05 +010096 int first_pass;
Bram Moolenaar11abd092020-05-01 14:26:37 +020097
kawaii-Code78019df2024-01-25 21:40:05 +010098 // Cursor is currently at the end of line. No need to format
99 // if line length is less than textwidth (8 * textwidth for
100 // utf safety)
Christian Brabandtbba79802024-04-11 22:54:44 +0200101 if (curwin->w_cursor.col < safe_tw)
kawaii-Code78019df2024-01-25 21:40:05 +0100102 {
103 virtcol = get_nolist_virtcol()
104 + char2cells(c != NUL ? c : gchar_cursor());
105 if (virtcol <= (colnr_T)textwidth)
106 break;
107 }
Bram Moolenaar11abd092020-05-01 14:26:37 +0200108
109 if (no_leader)
110 do_comments = FALSE;
111 else if (!(flags & INSCHAR_FORMAT)
112 && has_format_option(FO_WRAP_COMS))
113 do_comments = TRUE;
114
115 // Don't break until after the comment leader
116 if (do_comments)
Bram Moolenaar48a8a832022-05-07 15:43:52 +0100117 {
118 char_u *line = ml_get_curline();
119
120 leader_len = get_leader_len(line, NULL, FALSE, TRUE);
Bram Moolenaar48a8a832022-05-07 15:43:52 +0100121 if (leader_len == 0 && curbuf->b_p_cin)
122 {
123 int comment_start;
124
125 // Check for a line comment after code.
126 comment_start = check_linecomment(line);
127 if (comment_start != MAXCOL)
128 {
129 leader_len = get_leader_len(
130 line + comment_start, NULL, FALSE, TRUE);
131 if (leader_len != 0)
132 leader_len += comment_start;
133 }
134 }
Bram Moolenaar48a8a832022-05-07 15:43:52 +0100135 }
Bram Moolenaar11abd092020-05-01 14:26:37 +0200136 else
137 leader_len = 0;
138
139 // If the line doesn't start with a comment leader, then don't
140 // start one in a following broken line. Avoids that a %word
141 // moved to the start of the next line causes all following lines
142 // to start with %.
143 if (leader_len == 0)
144 no_leader = TRUE;
145 if (!(flags & INSCHAR_FORMAT)
146 && leader_len == 0
147 && !has_format_option(FO_WRAP))
148
149 break;
150 if ((startcol = curwin->w_cursor.col) == 0)
151 break;
152
153 // find column of textwidth border
154 coladvance((colnr_T)textwidth);
155 wantcol = curwin->w_cursor.col;
156
kawaii-Code78019df2024-01-25 21:40:05 +0100157 // If startcol is large (a long line), formatting takes too much
158 // time. The algorithm is O(n^2), it walks from the end of the
159 // line to textwidth border every time for each line break.
160 //
161 // Ceil to 8 * textwidth to optimize.
Christian Brabandtbba79802024-04-11 22:54:44 +0200162 curwin->w_cursor.col = startcol < safe_tw ? startcol : safe_tw;
kawaii-Code78019df2024-01-25 21:40:05 +0100163
Bram Moolenaar11abd092020-05-01 14:26:37 +0200164 foundcol = 0;
Bram Moolenaare52702f2020-06-04 18:22:13 +0200165 skip_pos = 0;
kawaii-Code78019df2024-01-25 21:40:05 +0100166 first_pass = TRUE;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200167
168 // Find position to break at.
169 // Stop at first entered white when 'formatoptions' has 'v'
170 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
171 || (flags & INSCHAR_FORMAT)
172 || curwin->w_cursor.lnum != Insstart.lnum
173 || curwin->w_cursor.col >= Insstart.col)
174 {
kawaii-Code78019df2024-01-25 21:40:05 +0100175 if (first_pass && c != NUL)
176 {
Bram Moolenaar11abd092020-05-01 14:26:37 +0200177 cc = c;
kawaii-Code78019df2024-01-25 21:40:05 +0100178 first_pass = FALSE;
179 }
Bram Moolenaar11abd092020-05-01 14:26:37 +0200180 else
181 cc = gchar_cursor();
182 if (WHITECHAR(cc))
183 {
184 // remember position of blank just before text
185 end_col = curwin->w_cursor.col;
186
187 // find start of sequence of blanks
188 wcc = 0;
189 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
190 {
191 dec_cursor();
192 cc = gchar_cursor();
193
194 // Increment count of how many whitespace chars in this
195 // group; we only need to know if it's more than one.
196 if (wcc < 2)
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100197 wcc++;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200198 }
199 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
200 break; // only spaces in front of text
201
202 // Don't break after a period when 'formatoptions' has 'p' and
203 // there are less than two spaces.
204 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
205 continue;
206
207 // Don't break until after the comment leader
208 if (curwin->w_cursor.col < leader_len)
209 break;
210 if (has_format_option(FO_ONE_LETTER))
211 {
212 // do not break after one-letter words
213 if (curwin->w_cursor.col == 0)
214 break; // one-letter word at begin
215 // do not break "#a b" when 'tw' is 2
216 if (curwin->w_cursor.col <= leader_len)
217 break;
218 col = curwin->w_cursor.col;
219 dec_cursor();
220 cc = gchar_cursor();
221
222 if (WHITECHAR(cc))
223 continue; // one-letter, continue
224 curwin->w_cursor.col = col;
225 }
226
227 inc_cursor();
228
229 end_foundcol = end_col + 1;
230 foundcol = curwin->w_cursor.col;
231 if (curwin->w_cursor.col <= (colnr_T)wantcol)
232 break;
233 }
Bram Moolenaar264d3dd2021-12-29 14:09:32 +0000234 else if ((cc >= 0x100 || !utf_allow_break_before(cc))
235 && fo_multibyte)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200236 {
Bram Moolenaare52702f2020-06-04 18:22:13 +0200237 int ncc;
238 int allow_break;
239
Bram Moolenaar11abd092020-05-01 14:26:37 +0200240 // Break after or before a multi-byte character.
241 if (curwin->w_cursor.col != startcol)
242 {
243 // Don't break until after the comment leader
244 if (curwin->w_cursor.col < leader_len)
245 break;
246 col = curwin->w_cursor.col;
247 inc_cursor();
Bram Moolenaare52702f2020-06-04 18:22:13 +0200248 ncc = gchar_cursor();
249
250 allow_break =
251 (enc_utf8 && utf_allow_break(cc, ncc))
252 || enc_dbcs;
253
254 // If we have already checked this position, skip!
255 if (curwin->w_cursor.col != skip_pos && allow_break)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200256 {
257 foundcol = curwin->w_cursor.col;
258 end_foundcol = foundcol;
259 if (curwin->w_cursor.col <= (colnr_T)wantcol)
260 break;
261 }
262 curwin->w_cursor.col = col;
263 }
264
265 if (curwin->w_cursor.col == 0)
266 break;
267
Bram Moolenaare52702f2020-06-04 18:22:13 +0200268 ncc = cc;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200269 col = curwin->w_cursor.col;
270
271 dec_cursor();
272 cc = gchar_cursor();
273
274 if (WHITECHAR(cc))
275 continue; // break with space
Bram Moolenaare52702f2020-06-04 18:22:13 +0200276 // Don't break until after the comment leader.
Bram Moolenaar11abd092020-05-01 14:26:37 +0200277 if (curwin->w_cursor.col < leader_len)
278 break;
279
280 curwin->w_cursor.col = col;
Bram Moolenaare52702f2020-06-04 18:22:13 +0200281 skip_pos = curwin->w_cursor.col;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200282
Bram Moolenaare52702f2020-06-04 18:22:13 +0200283 allow_break =
284 (enc_utf8 && utf_allow_break(cc, ncc))
285 || enc_dbcs;
286
287 // Must handle this to respect line break prohibition.
288 if (allow_break)
289 {
290 foundcol = curwin->w_cursor.col;
291 end_foundcol = foundcol;
292 }
Bram Moolenaar11abd092020-05-01 14:26:37 +0200293 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaare52702f2020-06-04 18:22:13 +0200294 {
295 int ncc_allow_break =
296 (enc_utf8 && utf_allow_break_before(ncc)) || enc_dbcs;
297
298 if (allow_break)
299 break;
300 if (!ncc_allow_break && !fo_rigor_tw)
301 {
302 // Enable at most 1 punct hang outside of textwidth.
303 if (curwin->w_cursor.col == startcol)
304 {
305 // We are inserting a non-breakable char, postpone
306 // line break check to next insert.
307 end_foundcol = foundcol = 0;
308 break;
309 }
310
311 // Neither cc nor ncc is NUL if we are here, so
312 // it's safe to inc_cursor.
313 col = curwin->w_cursor.col;
314
315 inc_cursor();
316 cc = ncc;
317 ncc = gchar_cursor();
318 // handle insert
319 ncc = (ncc != NUL) ? ncc : c;
320
321 allow_break =
322 (enc_utf8 && utf_allow_break(cc, ncc))
323 || enc_dbcs;
324
325 if (allow_break)
326 {
327 // Break only when we are not at end of line.
328 end_foundcol = foundcol =
329 ncc == NUL? 0 : curwin->w_cursor.col;
330 break;
331 }
332 curwin->w_cursor.col = col;
333 }
334 }
Bram Moolenaar11abd092020-05-01 14:26:37 +0200335 }
336 if (curwin->w_cursor.col == 0)
337 break;
338 dec_cursor();
339 }
340
341 if (foundcol == 0) // no spaces, cannot break line
342 {
343 curwin->w_cursor.col = startcol;
344 break;
345 }
346
347 // Going to break the line, remove any "$" now.
348 undisplay_dollar();
349
350 // Offset between cursor position and line break is used by replace
Bram Moolenaar24959102022-05-07 20:01:16 +0100351 // stack functions. MODE_VREPLACE does not use this, and backspaces
Bram Moolenaar11abd092020-05-01 14:26:37 +0200352 // over the text instead.
353 if (State & VREPLACE_FLAG)
354 orig_col = startcol; // Will start backspacing from here
355 else
356 replace_offset = startcol - end_foundcol;
357
358 // adjust startcol for spaces that will be deleted and
359 // characters that will remain on top line
360 curwin->w_cursor.col = foundcol;
361 while ((cc = gchar_cursor(), WHITECHAR(cc))
362 && (!fo_white_par || curwin->w_cursor.col < startcol))
363 inc_cursor();
364 startcol -= curwin->w_cursor.col;
365 if (startcol < 0)
366 startcol = 0;
367
368 if (State & VREPLACE_FLAG)
369 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100370 // In MODE_VREPLACE state, we will backspace over the text to be
Bram Moolenaar11abd092020-05-01 14:26:37 +0200371 // wrapped, so save a copy now to put on the next line.
372 saved_text = vim_strsave(ml_get_cursor());
373 curwin->w_cursor.col = orig_col;
374 if (saved_text == NULL)
375 break; // Can't do it, out of memory
376 saved_text[startcol] = NUL;
377
378 // Backspace over characters that will move to the next line
379 if (!fo_white_par)
380 backspace_until_column(foundcol);
381 }
382 else
383 {
384 // put cursor after pos. to break line
385 if (!fo_white_par)
386 curwin->w_cursor.col = foundcol;
387 }
388
389 // Split the line just before the margin.
390 // Only insert/delete lines, but don't really redraw the window.
391 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
392 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
393 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaar7e667782022-05-23 13:10:48 +0100394 + OPENLINE_FORMAT
Bram Moolenaar11abd092020-05-01 14:26:37 +0200395 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar6e371ec2021-12-12 14:16:39 +0000396 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent),
397 &did_do_comment);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200398 if (!(flags & INSCHAR_COM_LIST))
399 old_indent = 0;
400
Bram Moolenaar6e371ec2021-12-12 14:16:39 +0000401 // If a comment leader was inserted, may also do this on a following
402 // line.
403 if (did_do_comment)
404 no_leader = FALSE;
405
Bram Moolenaar11abd092020-05-01 14:26:37 +0200406 replace_offset = 0;
407 if (first_line)
408 {
409 if (!(flags & INSCHAR_COM_LIST))
410 {
411 // This section is for auto-wrap of numeric lists. When not
412 // in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
413 // flag will be set and open_line() will handle it (as seen
414 // above). The code here (and in get_number_indent()) will
415 // recognize comments if needed...
416 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
417 second_indent =
418 get_number_indent(curwin->w_cursor.lnum - 1);
419 if (second_indent >= 0)
420 {
421 if (State & VREPLACE_FLAG)
422 change_indent(INDENT_SET, second_indent,
423 FALSE, NUL, TRUE);
424 else
425 if (leader_len > 0 && second_indent - leader_len > 0)
426 {
427 int i;
428 int padding = second_indent - leader_len;
429
430 // We started at the first_line of a numbered list
431 // that has a comment. the open_line() function has
432 // inserted the proper comment leader and positioned
433 // the cursor at the end of the split line. Now we
434 // add the additional whitespace needed after the
435 // comment leader for the numbered list.
436 for (i = 0; i < padding; i++)
John Marriottf4b36412025-02-23 09:09:59 +0100437 ins_str((char_u *)" ", 1);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200438 }
439 else
440 {
441 (void)set_indent(second_indent, SIN_CHANGED);
442 }
443 }
444 }
445 first_line = FALSE;
446 }
447
448 if (State & VREPLACE_FLAG)
449 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100450 // In MODE_VREPLACE state we have backspaced over the text to be
Bram Moolenaar11abd092020-05-01 14:26:37 +0200451 // moved, now we re-insert it into the new line.
452 ins_bytes(saved_text);
453 vim_free(saved_text);
454 }
455 else
456 {
457 // Check if cursor is not past the NUL off the line, cindent
458 // may have added or removed indent.
459 curwin->w_cursor.col += startcol;
zeertzjq94b7c322024-03-12 21:50:32 +0100460 len = ml_get_curline_len();
Bram Moolenaar11abd092020-05-01 14:26:37 +0200461 if (curwin->w_cursor.col > len)
462 curwin->w_cursor.col = len;
463 }
464
465 haveto_redraw = TRUE;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200466 set_can_cindent(TRUE);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200467 // moved the cursor, don't autoindent or cindent now
468 did_ai = FALSE;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200469 did_si = FALSE;
470 can_si = FALSE;
471 can_si_back = FALSE;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200472 line_breakcheck();
473 }
474
475 if (save_char != NUL) // put back space after cursor
476 pchar_cursor(save_char);
477
478#ifdef FEAT_LINEBREAK
479 curwin->w_p_lbr = has_lbr;
Christian Brabandt86ef8152024-04-27 11:55:08 +0200480 curwin->w_p_bri = has_bri;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200481#endif
482 if (!format_only && haveto_redraw)
483 {
484 update_topline();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100485 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200486 }
487}
488
489/*
490 * Blank lines, and lines containing only the comment leader, are left
491 * untouched by the formatting. The function returns TRUE in this
492 * case. It also returns TRUE when a line starts with the end of a comment
493 * ('e' in comment flags), so that this line is skipped, and not joined to the
494 * previous line. A new paragraph starts after a blank line, or when the
495 * comment leader changes -- webb.
496 */
497 static int
498fmt_check_par(
499 linenr_T lnum,
500 int *leader_len,
501 char_u **leader_flags,
502 int do_comments)
503{
504 char_u *flags = NULL; // init for GCC
505 char_u *ptr;
506
507 ptr = ml_get(lnum);
508 if (do_comments)
509 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
510 else
511 *leader_len = 0;
512
513 if (*leader_len > 0)
514 {
515 // Search for 'e' flag in comment leader flags.
516 flags = *leader_flags;
517 while (*flags && *flags != ':' && *flags != COM_END)
518 ++flags;
519 }
520
521 return (*skipwhite(ptr + *leader_len) == NUL
522 || (*leader_len > 0 && *flags == COM_END)
523 || startPS(lnum, NUL, FALSE));
524}
525
526/*
527 * Return TRUE if line "lnum" ends in a white character.
528 */
529 static int
530ends_in_white(linenr_T lnum)
531{
532 char_u *s = ml_get(lnum);
533 size_t l;
534
535 if (*s == NUL)
536 return FALSE;
zeertzjq94b7c322024-03-12 21:50:32 +0100537 l = ml_get_len(lnum) - 1;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200538 return VIM_ISWHITE(s[l]);
539}
540
541/*
542 * Return TRUE if the two comment leaders given are the same. "lnum" is
543 * the first line. White-space is ignored. Note that the whole of
544 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
545 */
546 static int
547same_leader(
548 linenr_T lnum,
549 int leader1_len,
550 char_u *leader1_flags,
551 int leader2_len,
552 char_u *leader2_flags)
553{
554 int idx1 = 0, idx2 = 0;
555 char_u *p;
556 char_u *line1;
557 char_u *line2;
558
559 if (leader1_len == 0)
560 return (leader2_len == 0);
561
562 // If first leader has 'f' flag, the lines can be joined only if the
563 // second line does not have a leader.
564 // If first leader has 'e' flag, the lines can never be joined.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000565 // If first leader has 's' flag, the lines can only be joined if there is
Bram Moolenaar11abd092020-05-01 14:26:37 +0200566 // some text after it and the second line has the 'm' flag.
567 if (leader1_flags != NULL)
568 {
569 for (p = leader1_flags; *p && *p != ':'; ++p)
570 {
571 if (*p == COM_FIRST)
572 return (leader2_len == 0);
573 if (*p == COM_END)
574 return FALSE;
575 if (*p == COM_START)
576 {
zeertzjq94b7c322024-03-12 21:50:32 +0100577 int line_len = ml_get_len(lnum);
Bram Moolenaar11977f92023-01-21 13:09:19 +0000578 if (line_len <= leader1_len)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200579 return FALSE;
580 if (leader2_flags == NULL || leader2_len == 0)
581 return FALSE;
582 for (p = leader2_flags; *p && *p != ':'; ++p)
583 if (*p == COM_MIDDLE)
584 return TRUE;
585 return FALSE;
586 }
587 }
588 }
589
590 // Get current line and next line, compare the leaders.
591 // The first line has to be saved, only one line can be locked at a time.
592 line1 = vim_strsave(ml_get(lnum));
593 if (line1 != NULL)
594 {
595 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
596 ;
597 line2 = ml_get(lnum + 1);
598 for (idx2 = 0; idx2 < leader2_len; ++idx2)
599 {
600 if (!VIM_ISWHITE(line2[idx2]))
601 {
602 if (line1[idx1++] != line2[idx2])
603 break;
604 }
605 else
606 while (VIM_ISWHITE(line1[idx1]))
607 ++idx1;
608 }
609 vim_free(line1);
610 }
611 return (idx2 == leader2_len && idx1 == leader1_len);
612}
613
614/*
615 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
616 * previous line is in the same paragraph. Used for auto-formatting.
617 */
618 static int
619paragraph_start(linenr_T lnum)
620{
621 char_u *p;
622 int leader_len = 0; // leader len of current line
623 char_u *leader_flags = NULL; // flags for leader of current line
624 int next_leader_len; // leader len of next line
625 char_u *next_leader_flags; // flags for leader of next line
626 int do_comments; // format comments
627
628 if (lnum <= 1)
629 return TRUE; // start of the file
630
631 p = ml_get(lnum - 1);
632 if (*p == NUL)
633 return TRUE; // after empty line
634
635 do_comments = has_format_option(FO_Q_COMS);
636 if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments))
637 return TRUE; // after non-paragraph line
638
639 if (fmt_check_par(lnum, &next_leader_len, &next_leader_flags, do_comments))
640 return TRUE; // "lnum" is not a paragraph line
641
642 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
643 return TRUE; // missing trailing space in previous line.
644
645 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
646 return TRUE; // numbered item starts in "lnum".
647
648 if (!same_leader(lnum - 1, leader_len, leader_flags,
649 next_leader_len, next_leader_flags))
650 return TRUE; // change of comment leader.
651
652 return FALSE;
653}
654
655/*
656 * Called after inserting or deleting text: When 'formatoptions' includes the
657 * 'a' flag format from the current line until the end of the paragraph.
658 * Keep the cursor at the same position relative to the text.
659 * The caller must have saved the cursor line for undo, following ones will be
660 * saved here.
661 */
662 void
663auto_format(
664 int trailblank, // when TRUE also format with trailing blank
665 int prev_line) // may start in previous line
666{
667 pos_T pos;
668 colnr_T len;
669 char_u *old;
670 char_u *new, *pnew;
671 int wasatend;
672 int cc;
673
674 if (!has_format_option(FO_AUTO))
675 return;
676
677 pos = curwin->w_cursor;
678 old = ml_get_curline();
679
680 // may remove added space
681 check_auto_format(FALSE);
682
683 // Don't format in Insert mode when the cursor is on a trailing blank, the
684 // user might insert normal text next. Also skip formatting when "1" is
685 // in 'formatoptions' and there is a single character before the cursor.
686 // Otherwise the line would be broken and when typing another non-white
687 // next they are not joined back together.
zeertzjq94b7c322024-03-12 21:50:32 +0100688 wasatend = (pos.col == ml_get_curline_len());
Bram Moolenaar11abd092020-05-01 14:26:37 +0200689 if (*old != NUL && !trailblank && wasatend)
690 {
691 dec_cursor();
692 cc = gchar_cursor();
693 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
694 && has_format_option(FO_ONE_LETTER))
695 dec_cursor();
696 cc = gchar_cursor();
697 if (WHITECHAR(cc))
698 {
699 curwin->w_cursor = pos;
700 return;
701 }
702 curwin->w_cursor = pos;
703 }
704
705 // With the 'c' flag in 'formatoptions' and 't' missing: only format
706 // comments.
707 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
708 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
709 return;
710
711 // May start formatting in a previous line, so that after "x" a word is
712 // moved to the previous line if it fits there now. Only when this is not
713 // the start of a paragraph.
714 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
715 {
716 --curwin->w_cursor.lnum;
717 if (u_save_cursor() == FAIL)
718 return;
719 }
720
721 // Do the formatting and restore the cursor position. "saved_cursor" will
722 // be adjusted for the text formatting.
723 saved_cursor = pos;
724 format_lines((linenr_T)-1, FALSE);
725 curwin->w_cursor = saved_cursor;
726 saved_cursor.lnum = 0;
727
728 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
729 {
730 // "cannot happen"
731 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
732 coladvance((colnr_T)MAXCOL);
733 }
734 else
735 check_cursor_col();
736
737 // Insert mode: If the cursor is now after the end of the line while it
738 // previously wasn't, the line was broken. Because of the rule above we
739 // need to add a space when 'w' is in 'formatoptions' to keep a paragraph
740 // formatted.
741 if (!wasatend && has_format_option(FO_WHITE_PAR))
742 {
743 new = ml_get_curline();
zeertzjq94b7c322024-03-12 21:50:32 +0100744 len = ml_get_curline_len();
Bram Moolenaar11abd092020-05-01 14:26:37 +0200745 if (curwin->w_cursor.col == len)
746 {
747 pnew = vim_strnsave(new, len + 2);
748 pnew[len] = ' ';
749 pnew[len + 1] = NUL;
750 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
751 // remove the space later
752 did_add_space = TRUE;
753 }
754 else
755 // may remove added space
756 check_auto_format(FALSE);
757 }
758
759 check_cursor();
760}
761
762/*
763 * When an extra space was added to continue a paragraph for auto-formatting,
764 * delete it now. The space must be under the cursor, just after the insert
765 * position.
766 */
767 void
768check_auto_format(
769 int end_insert) // TRUE when ending Insert mode
770{
771 int c = ' ';
772 int cc;
773
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000774 if (!did_add_space)
775 return;
776
777 cc = gchar_cursor();
778 if (!WHITECHAR(cc))
779 // Somehow the space was removed already.
780 did_add_space = FALSE;
781 else
Bram Moolenaar11abd092020-05-01 14:26:37 +0200782 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000783 if (!end_insert)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200784 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000785 inc_cursor();
786 c = gchar_cursor();
787 dec_cursor();
788 }
789 if (c != NUL)
790 {
791 // The space is no longer at the end of the line, delete it.
792 del_char(FALSE);
793 did_add_space = FALSE;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200794 }
795 }
796}
797
798/*
799 * Find out textwidth to be used for formatting:
800 * if 'textwidth' option is set, use it
801 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
802 * if invalid value, use 0.
803 * Set default to window width (maximum 79) for "gq" operator.
804 */
805 int
806comp_textwidth(
807 int ff) // force formatting (for "gq" command)
808{
809 int textwidth;
810
811 textwidth = curbuf->b_p_tw;
812 if (textwidth == 0 && curbuf->b_p_wm)
813 {
814 // The width is the window width minus 'wrapmargin' minus all the
815 // things that add to the margin.
816 textwidth = curwin->w_width - curbuf->b_p_wm;
Sean Dewar988f7432023-08-16 14:17:36 +0100817 if (curbuf == cmdwin_buf)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200818 textwidth -= 1;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200819#ifdef FEAT_FOLDING
820 textwidth -= curwin->w_p_fdc;
821#endif
822#ifdef FEAT_SIGNS
823 if (signcolumn_on(curwin))
824 textwidth -= 1;
825#endif
826 if (curwin->w_p_nu || curwin->w_p_rnu)
827 textwidth -= 8;
828 }
829 if (textwidth < 0)
830 textwidth = 0;
831 if (ff && textwidth == 0)
832 {
833 textwidth = curwin->w_width - 1;
834 if (textwidth > 79)
835 textwidth = 79;
836 }
837 return textwidth;
838}
839
840/*
841 * Implementation of the format operator 'gq'.
842 */
843 void
844op_format(
845 oparg_T *oap,
846 int keep_cursor) // keep cursor on same text char
847{
848 long old_line_count = curbuf->b_ml.ml_line_count;
849
850 // Place the cursor where the "gq" or "gw" command was given, so that "u"
851 // can put it back there.
852 curwin->w_cursor = oap->cursor_start;
853
854 if (u_save((linenr_T)(oap->start.lnum - 1),
855 (linenr_T)(oap->end.lnum + 1)) == FAIL)
856 return;
857 curwin->w_cursor = oap->start;
858
859 if (oap->is_VIsual)
860 // When there is no change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100861 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200862
Bram Moolenaare1004402020-10-24 20:49:43 +0200863 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200864 // Set '[ mark at the start of the formatted area
865 curbuf->b_op_start = oap->start;
866
867 // For "gw" remember the cursor position and put it back below (adjusted
868 // for joined and split lines).
869 if (keep_cursor)
870 saved_cursor = oap->cursor_start;
871
872 format_lines(oap->line_count, keep_cursor);
873
874 // Leave the cursor at the first non-blank of the last formatted line.
875 // If the cursor was moved one line back (e.g. with "Q}") go to the next
876 // line, so "." will do the next lines.
877 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
878 ++curwin->w_cursor.lnum;
879 beginline(BL_WHITE | BL_FIX);
880 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
881 msgmore(old_line_count);
882
Bram Moolenaare1004402020-10-24 20:49:43 +0200883 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200884 // put '] mark on the end of the formatted area
885 curbuf->b_op_end = curwin->w_cursor;
886
887 if (keep_cursor)
888 {
889 curwin->w_cursor = saved_cursor;
890 saved_cursor.lnum = 0;
Bram Moolenaar78d52882022-05-24 13:57:54 +0100891
892 // formatting may have made the cursor position invalid
893 check_cursor();
Bram Moolenaar11abd092020-05-01 14:26:37 +0200894 }
895
896 if (oap->is_VIsual)
897 {
898 win_T *wp;
899
900 FOR_ALL_WINDOWS(wp)
901 {
902 if (wp->w_old_cursor_lnum != 0)
903 {
904 // When lines have been inserted or deleted, adjust the end of
905 // the Visual area to be redrawn.
906 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
907 wp->w_old_cursor_lnum += old_line_count;
908 else
909 wp->w_old_visual_lnum += old_line_count;
910 }
911 }
912 }
913}
914
915#if defined(FEAT_EVAL) || defined(PROTO)
916/*
917 * Implementation of the format operator 'gq' for when using 'formatexpr'.
918 */
919 void
920op_formatexpr(oparg_T *oap)
921{
922 if (oap->is_VIsual)
923 // When there is no change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100924 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200925
926 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
927 // As documented: when 'formatexpr' returns non-zero fall back to
928 // internal formatting.
929 op_format(oap, FALSE);
930}
931
932 int
933fex_format(
934 linenr_T lnum,
935 long count,
936 int c) // character to be inserted
937{
938 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
939 OPT_LOCAL);
940 int r;
941 char_u *fex;
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000942 sctx_T save_sctx = current_sctx;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200943
944 // Set v:lnum to the first line number and v:count to the number of lines.
945 // Set v:char to the character to be inserted (can be NUL).
946 set_vim_var_nr(VV_LNUM, lnum);
947 set_vim_var_nr(VV_COUNT, count);
948 set_vim_var_char(c);
949
950 // Make a copy, the option could be changed while calling it.
951 fex = vim_strsave(curbuf->b_p_fex);
952 if (fex == NULL)
953 return 0;
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000954 current_sctx = curbuf->b_p_script_ctx[BV_FEX];
Bram Moolenaar11abd092020-05-01 14:26:37 +0200955
956 // Evaluate the function.
957 if (use_sandbox)
958 ++sandbox;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100959 r = (int)eval_to_number(fex, TRUE);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200960 if (use_sandbox)
961 --sandbox;
962
963 set_vim_var_string(VV_CHAR, NULL, -1);
964 vim_free(fex);
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000965 current_sctx = save_sctx;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200966
967 return r;
968}
969#endif
970
971/*
972 * Format "line_count" lines, starting at the cursor position.
973 * When "line_count" is negative, format until the end of the paragraph.
974 * Lines after the cursor line are saved for undo, caller must have saved the
975 * first line.
976 */
977 void
978format_lines(
979 linenr_T line_count,
980 int avoid_fex) // don't use 'formatexpr'
981{
982 int max_len;
983 int is_not_par; // current line not part of parag.
984 int next_is_not_par; // next line not part of paragraph
985 int is_end_par; // at end of paragraph
986 int prev_is_end_par = FALSE;// prev. line not part of parag.
987 int next_is_start_par = FALSE;
988 int leader_len = 0; // leader len of current line
989 int next_leader_len; // leader len of next line
990 char_u *leader_flags = NULL; // flags for leader of current line
Bram Moolenaar264d3dd2021-12-29 14:09:32 +0000991 char_u *next_leader_flags = NULL; // flags for leader of next line
Bram Moolenaar11abd092020-05-01 14:26:37 +0200992 int do_comments; // format comments
993 int do_comments_list = 0; // format comments with 'n' or '2'
994 int advance = TRUE;
995 int second_indent = -1; // indent for second line (comment
996 // aware)
997 int do_second_indent;
998 int do_number_indent;
999 int do_trail_white;
1000 int first_par_line = TRUE;
1001 int smd_save;
1002 long count;
1003 int need_set_indent = TRUE; // set indent of next paragraph
Bram Moolenaarecabb512021-12-06 19:51:01 +00001004 linenr_T first_line = curwin->w_cursor.lnum;
Bram Moolenaar11abd092020-05-01 14:26:37 +02001005 int force_format = FALSE;
1006 int old_State = State;
1007
1008 // length of a line to force formatting: 3 * 'tw'
1009 max_len = comp_textwidth(TRUE) * 3;
1010
Christian Brabandt305127f2023-11-11 18:59:33 +01001011 // check for 'q', '2', 'n' and 'w' in 'formatoptions'
Bram Moolenaar11abd092020-05-01 14:26:37 +02001012 do_comments = has_format_option(FO_Q_COMS);
1013 do_second_indent = has_format_option(FO_Q_SECOND);
1014 do_number_indent = has_format_option(FO_Q_NUMBER);
1015 do_trail_white = has_format_option(FO_WHITE_PAR);
1016
1017 // Get info about the previous and current line.
1018 if (curwin->w_cursor.lnum > 1)
1019 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
1020 , &leader_len, &leader_flags, do_comments);
1021 else
1022 is_not_par = TRUE;
1023 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
1024 , &next_leader_len, &next_leader_flags, do_comments);
1025 is_end_par = (is_not_par || next_is_not_par);
1026 if (!is_end_par && do_trail_white)
1027 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
1028
1029 curwin->w_cursor.lnum--;
1030 for (count = line_count; count != 0 && !got_int; --count)
1031 {
1032 // Advance to next paragraph.
1033 if (advance)
1034 {
1035 curwin->w_cursor.lnum++;
1036 prev_is_end_par = is_end_par;
1037 is_not_par = next_is_not_par;
1038 leader_len = next_leader_len;
1039 leader_flags = next_leader_flags;
1040 }
1041
1042 // The last line to be formatted.
1043 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
1044 {
1045 next_is_not_par = TRUE;
1046 next_leader_len = 0;
1047 next_leader_flags = NULL;
1048 }
1049 else
1050 {
1051 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
1052 , &next_leader_len, &next_leader_flags, do_comments);
1053 if (do_number_indent)
1054 next_is_start_par =
1055 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
1056 }
1057 advance = TRUE;
1058 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
1059 if (!is_end_par && do_trail_white)
1060 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
1061
1062 // Skip lines that are not in a paragraph.
1063 if (is_not_par)
1064 {
1065 if (line_count < 0)
1066 break;
1067 }
1068 else
1069 {
1070 // For the first line of a paragraph, check indent of second line.
1071 // Don't do this for comments and empty lines.
1072 if (first_par_line
1073 && (do_second_indent || do_number_indent)
1074 && prev_is_end_par
1075 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1076 {
1077 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
1078 {
1079 if (leader_len == 0 && next_leader_len == 0)
1080 {
1081 // no comment found
1082 second_indent =
1083 get_indent_lnum(curwin->w_cursor.lnum + 1);
1084 }
1085 else
1086 {
1087 second_indent = next_leader_len;
1088 do_comments_list = 1;
1089 }
1090 }
1091 else if (do_number_indent)
1092 {
1093 if (leader_len == 0 && next_leader_len == 0)
1094 {
1095 // no comment found
1096 second_indent =
1097 get_number_indent(curwin->w_cursor.lnum);
1098 }
1099 else
1100 {
1101 // get_number_indent() is now "comment aware"...
1102 second_indent =
1103 get_number_indent(curwin->w_cursor.lnum);
1104 do_comments_list = 1;
1105 }
1106 }
1107 }
1108
1109 // When the comment leader changes, it's the end of the paragraph.
1110 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
1111 || !same_leader(curwin->w_cursor.lnum,
1112 leader_len, leader_flags,
1113 next_leader_len, next_leader_flags))
Bram Moolenaar264d3dd2021-12-29 14:09:32 +00001114 {
1115 // Special case: If the next line starts with a line comment
1116 // and this line has a line comment after some text, the
1117 // paragraph doesn't really end.
1118 if (next_leader_flags == NULL
1119 || STRNCMP(next_leader_flags, "://", 3) != 0
1120 || check_linecomment(ml_get_curline()) == MAXCOL)
Bram Moolenaar11abd092020-05-01 14:26:37 +02001121 is_end_par = TRUE;
Bram Moolenaar264d3dd2021-12-29 14:09:32 +00001122 }
Bram Moolenaar11abd092020-05-01 14:26:37 +02001123
1124 // If we have got to the end of a paragraph, or the line is
1125 // getting long, format it.
1126 if (is_end_par || force_format)
1127 {
1128 if (need_set_indent)
Christian Brabandt818ff252021-11-18 13:56:37 +00001129 {
1130 int indent = 0; // amount of indent needed
1131
Bram Moolenaarecabb512021-12-06 19:51:01 +00001132 // Replace indent in first line of a paragraph with minimal
1133 // number of tabs and spaces, according to current options.
1134 // For the very first formatted line keep the current
1135 // indent.
1136 if (curwin->w_cursor.lnum == first_line)
1137 indent = get_indent();
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001138 else if (curbuf->b_p_lisp)
Christian Brabandt818ff252021-11-18 13:56:37 +00001139 indent = get_lisp_indent();
1140 else
Christian Brabandt818ff252021-11-18 13:56:37 +00001141 {
Christian Brabandt818ff252021-11-18 13:56:37 +00001142 if (cindent_on())
1143 {
1144 indent =
1145# ifdef FEAT_EVAL
1146 *curbuf->b_p_inde != NUL ? get_expr_indent() :
1147# endif
1148 get_c_indent();
1149 }
1150 else
Christian Brabandt818ff252021-11-18 13:56:37 +00001151 indent = get_indent();
1152 }
1153 (void)set_indent(indent, SIN_CHANGED);
1154 }
Bram Moolenaar11abd092020-05-01 14:26:37 +02001155
1156 // put cursor on last non-space
Bram Moolenaar24959102022-05-07 20:01:16 +01001157 State = MODE_NORMAL; // don't go past end-of-line
Bram Moolenaar11abd092020-05-01 14:26:37 +02001158 coladvance((colnr_T)MAXCOL);
1159 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
1160 dec_cursor();
1161
1162 // do the formatting, without 'showmode'
Bram Moolenaar24959102022-05-07 20:01:16 +01001163 State = MODE_INSERT; // for open_line()
Bram Moolenaar11abd092020-05-01 14:26:37 +02001164 smd_save = p_smd;
1165 p_smd = FALSE;
zeertzjq6c302772024-12-17 20:26:45 +01001166
Bram Moolenaar11abd092020-05-01 14:26:37 +02001167 insertchar(NUL, INSCHAR_FORMAT
1168 + (do_comments ? INSCHAR_DO_COM : 0)
1169 + (do_comments && do_comments_list
1170 ? INSCHAR_COM_LIST : 0)
1171 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
zeertzjq6c302772024-12-17 20:26:45 +01001172
Bram Moolenaar11abd092020-05-01 14:26:37 +02001173 State = old_State;
1174 p_smd = smd_save;
zeertzjq6c302772024-12-17 20:26:45 +01001175 // Cursor and mouse shape shapes may have been updated (e.g. by
1176 // :normal) in insertchar(), so they need to be updated here.
1177#ifdef CURSOR_SHAPE
1178 ui_cursor_shape();
1179#endif
1180#ifdef FEAT_MOUSESHAPE
1181 update_mouseshape(-1);
1182#endif
1183
Bram Moolenaar11abd092020-05-01 14:26:37 +02001184 second_indent = -1;
1185 // at end of par.: need to set indent of next par.
1186 need_set_indent = is_end_par;
1187 if (is_end_par)
1188 {
1189 // When called with a negative line count, break at the
1190 // end of the paragraph.
1191 if (line_count < 0)
1192 break;
1193 first_par_line = TRUE;
1194 }
1195 force_format = FALSE;
1196 }
1197
1198 // When still in same paragraph, join the lines together. But
1199 // first delete the leader from the second line.
1200 if (!is_end_par)
1201 {
1202 advance = FALSE;
1203 curwin->w_cursor.lnum++;
1204 curwin->w_cursor.col = 0;
1205 if (line_count < 0 && u_save_cursor() == FAIL)
1206 break;
1207 if (next_leader_len > 0)
1208 {
1209 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
1210 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
1211 (long)-next_leader_len, 0);
1212 }
1213 else if (second_indent > 0) // the "leader" for FO_Q_SECOND
1214 {
1215 int indent = getwhitecols_curline();
1216
1217 if (indent > 0)
1218 {
1219 (void)del_bytes(indent, FALSE, FALSE);
1220 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001221 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar11abd092020-05-01 14:26:37 +02001222 }
1223 }
1224 curwin->w_cursor.lnum--;
1225 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
1226 {
1227 beep_flush();
1228 break;
1229 }
1230 first_par_line = FALSE;
1231 // If the line is getting long, format it next time
zeertzjq94b7c322024-03-12 21:50:32 +01001232 if (ml_get_curline_len() > max_len)
Bram Moolenaar11abd092020-05-01 14:26:37 +02001233 force_format = TRUE;
1234 else
1235 force_format = FALSE;
1236 }
1237 }
1238 line_breakcheck();
1239 }
1240}