blob: afa94d881209f2510e1f6ed87f1bb7c42687a53e [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);
59#ifdef FEAT_LINEBREAK
60 int has_lbr = curwin->w_p_lbr;
61
62 // make sure win_lbr_chartabsize() counts correctly
63 curwin->w_p_lbr = FALSE;
64#endif
65
66 // When 'ai' is off we don't want a space under the cursor to be
67 // deleted. Replace it with an 'x' temporarily.
68 if (!curbuf->b_p_ai && !(State & VREPLACE_FLAG))
69 {
70 cc = gchar_cursor();
71 if (VIM_ISWHITE(cc))
72 {
73 save_char = cc;
74 pchar_cursor('x');
75 }
76 }
77
78 // Repeat breaking lines, until the current line is not too long.
79 while (!got_int)
80 {
81 int startcol; // Cursor column at entry
82 int wantcol; // column at textwidth border
83 int foundcol; // column for start of spaces
84 int end_foundcol = 0; // column for start of word
85 colnr_T len;
86 colnr_T virtcol;
87 int orig_col = 0;
88 char_u *saved_text = NULL;
89 colnr_T col;
90 colnr_T end_col;
91 int wcc; // counter for whitespace chars
Bram Moolenaar6e371ec2021-12-12 14:16:39 +000092 int did_do_comment = FALSE;
Bram Moolenaar11abd092020-05-01 14:26:37 +020093
94 virtcol = get_nolist_virtcol()
Bram Moolenaarb8329db2022-07-06 13:31:28 +010095 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaar11abd092020-05-01 14:26:37 +020096 if (virtcol <= (colnr_T)textwidth)
97 break;
98
99 if (no_leader)
100 do_comments = FALSE;
101 else if (!(flags & INSCHAR_FORMAT)
102 && has_format_option(FO_WRAP_COMS))
103 do_comments = TRUE;
104
105 // Don't break until after the comment leader
106 if (do_comments)
Bram Moolenaar48a8a832022-05-07 15:43:52 +0100107 {
108 char_u *line = ml_get_curline();
109
110 leader_len = get_leader_len(line, NULL, FALSE, TRUE);
Bram Moolenaar48a8a832022-05-07 15:43:52 +0100111 if (leader_len == 0 && curbuf->b_p_cin)
112 {
113 int comment_start;
114
115 // Check for a line comment after code.
116 comment_start = check_linecomment(line);
117 if (comment_start != MAXCOL)
118 {
119 leader_len = get_leader_len(
120 line + comment_start, NULL, FALSE, TRUE);
121 if (leader_len != 0)
122 leader_len += comment_start;
123 }
124 }
Bram Moolenaar48a8a832022-05-07 15:43:52 +0100125 }
Bram Moolenaar11abd092020-05-01 14:26:37 +0200126 else
127 leader_len = 0;
128
129 // If the line doesn't start with a comment leader, then don't
130 // start one in a following broken line. Avoids that a %word
131 // moved to the start of the next line causes all following lines
132 // to start with %.
133 if (leader_len == 0)
134 no_leader = TRUE;
135 if (!(flags & INSCHAR_FORMAT)
136 && leader_len == 0
137 && !has_format_option(FO_WRAP))
138
139 break;
140 if ((startcol = curwin->w_cursor.col) == 0)
141 break;
142
143 // find column of textwidth border
144 coladvance((colnr_T)textwidth);
145 wantcol = curwin->w_cursor.col;
146
147 curwin->w_cursor.col = startcol;
148 foundcol = 0;
Bram Moolenaare52702f2020-06-04 18:22:13 +0200149 skip_pos = 0;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200150
151 // Find position to break at.
152 // Stop at first entered white when 'formatoptions' has 'v'
153 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
154 || (flags & INSCHAR_FORMAT)
155 || curwin->w_cursor.lnum != Insstart.lnum
156 || curwin->w_cursor.col >= Insstart.col)
157 {
158 if (curwin->w_cursor.col == startcol && c != NUL)
159 cc = c;
160 else
161 cc = gchar_cursor();
162 if (WHITECHAR(cc))
163 {
164 // remember position of blank just before text
165 end_col = curwin->w_cursor.col;
166
167 // find start of sequence of blanks
168 wcc = 0;
169 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
170 {
171 dec_cursor();
172 cc = gchar_cursor();
173
174 // Increment count of how many whitespace chars in this
175 // group; we only need to know if it's more than one.
176 if (wcc < 2)
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100177 wcc++;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200178 }
179 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
180 break; // only spaces in front of text
181
182 // Don't break after a period when 'formatoptions' has 'p' and
183 // there are less than two spaces.
184 if (has_format_option(FO_PERIOD_ABBR) && cc == '.' && wcc < 2)
185 continue;
186
187 // Don't break until after the comment leader
188 if (curwin->w_cursor.col < leader_len)
189 break;
190 if (has_format_option(FO_ONE_LETTER))
191 {
192 // do not break after one-letter words
193 if (curwin->w_cursor.col == 0)
194 break; // one-letter word at begin
195 // do not break "#a b" when 'tw' is 2
196 if (curwin->w_cursor.col <= leader_len)
197 break;
198 col = curwin->w_cursor.col;
199 dec_cursor();
200 cc = gchar_cursor();
201
202 if (WHITECHAR(cc))
203 continue; // one-letter, continue
204 curwin->w_cursor.col = col;
205 }
206
207 inc_cursor();
208
209 end_foundcol = end_col + 1;
210 foundcol = curwin->w_cursor.col;
211 if (curwin->w_cursor.col <= (colnr_T)wantcol)
212 break;
213 }
Bram Moolenaar264d3dd2021-12-29 14:09:32 +0000214 else if ((cc >= 0x100 || !utf_allow_break_before(cc))
215 && fo_multibyte)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200216 {
Bram Moolenaare52702f2020-06-04 18:22:13 +0200217 int ncc;
218 int allow_break;
219
Bram Moolenaar11abd092020-05-01 14:26:37 +0200220 // Break after or before a multi-byte character.
221 if (curwin->w_cursor.col != startcol)
222 {
223 // Don't break until after the comment leader
224 if (curwin->w_cursor.col < leader_len)
225 break;
226 col = curwin->w_cursor.col;
227 inc_cursor();
Bram Moolenaare52702f2020-06-04 18:22:13 +0200228 ncc = gchar_cursor();
229
230 allow_break =
231 (enc_utf8 && utf_allow_break(cc, ncc))
232 || enc_dbcs;
233
234 // If we have already checked this position, skip!
235 if (curwin->w_cursor.col != skip_pos && allow_break)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200236 {
237 foundcol = curwin->w_cursor.col;
238 end_foundcol = foundcol;
239 if (curwin->w_cursor.col <= (colnr_T)wantcol)
240 break;
241 }
242 curwin->w_cursor.col = col;
243 }
244
245 if (curwin->w_cursor.col == 0)
246 break;
247
Bram Moolenaare52702f2020-06-04 18:22:13 +0200248 ncc = cc;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200249 col = curwin->w_cursor.col;
250
251 dec_cursor();
252 cc = gchar_cursor();
253
254 if (WHITECHAR(cc))
255 continue; // break with space
Bram Moolenaare52702f2020-06-04 18:22:13 +0200256 // Don't break until after the comment leader.
Bram Moolenaar11abd092020-05-01 14:26:37 +0200257 if (curwin->w_cursor.col < leader_len)
258 break;
259
260 curwin->w_cursor.col = col;
Bram Moolenaare52702f2020-06-04 18:22:13 +0200261 skip_pos = curwin->w_cursor.col;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200262
Bram Moolenaare52702f2020-06-04 18:22:13 +0200263 allow_break =
264 (enc_utf8 && utf_allow_break(cc, ncc))
265 || enc_dbcs;
266
267 // Must handle this to respect line break prohibition.
268 if (allow_break)
269 {
270 foundcol = curwin->w_cursor.col;
271 end_foundcol = foundcol;
272 }
Bram Moolenaar11abd092020-05-01 14:26:37 +0200273 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaare52702f2020-06-04 18:22:13 +0200274 {
275 int ncc_allow_break =
276 (enc_utf8 && utf_allow_break_before(ncc)) || enc_dbcs;
277
278 if (allow_break)
279 break;
280 if (!ncc_allow_break && !fo_rigor_tw)
281 {
282 // Enable at most 1 punct hang outside of textwidth.
283 if (curwin->w_cursor.col == startcol)
284 {
285 // We are inserting a non-breakable char, postpone
286 // line break check to next insert.
287 end_foundcol = foundcol = 0;
288 break;
289 }
290
291 // Neither cc nor ncc is NUL if we are here, so
292 // it's safe to inc_cursor.
293 col = curwin->w_cursor.col;
294
295 inc_cursor();
296 cc = ncc;
297 ncc = gchar_cursor();
298 // handle insert
299 ncc = (ncc != NUL) ? ncc : c;
300
301 allow_break =
302 (enc_utf8 && utf_allow_break(cc, ncc))
303 || enc_dbcs;
304
305 if (allow_break)
306 {
307 // Break only when we are not at end of line.
308 end_foundcol = foundcol =
309 ncc == NUL? 0 : curwin->w_cursor.col;
310 break;
311 }
312 curwin->w_cursor.col = col;
313 }
314 }
Bram Moolenaar11abd092020-05-01 14:26:37 +0200315 }
316 if (curwin->w_cursor.col == 0)
317 break;
318 dec_cursor();
319 }
320
321 if (foundcol == 0) // no spaces, cannot break line
322 {
323 curwin->w_cursor.col = startcol;
324 break;
325 }
326
327 // Going to break the line, remove any "$" now.
328 undisplay_dollar();
329
330 // Offset between cursor position and line break is used by replace
Bram Moolenaar24959102022-05-07 20:01:16 +0100331 // stack functions. MODE_VREPLACE does not use this, and backspaces
Bram Moolenaar11abd092020-05-01 14:26:37 +0200332 // over the text instead.
333 if (State & VREPLACE_FLAG)
334 orig_col = startcol; // Will start backspacing from here
335 else
336 replace_offset = startcol - end_foundcol;
337
338 // adjust startcol for spaces that will be deleted and
339 // characters that will remain on top line
340 curwin->w_cursor.col = foundcol;
341 while ((cc = gchar_cursor(), WHITECHAR(cc))
342 && (!fo_white_par || curwin->w_cursor.col < startcol))
343 inc_cursor();
344 startcol -= curwin->w_cursor.col;
345 if (startcol < 0)
346 startcol = 0;
347
348 if (State & VREPLACE_FLAG)
349 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100350 // In MODE_VREPLACE state, we will backspace over the text to be
Bram Moolenaar11abd092020-05-01 14:26:37 +0200351 // wrapped, so save a copy now to put on the next line.
352 saved_text = vim_strsave(ml_get_cursor());
353 curwin->w_cursor.col = orig_col;
354 if (saved_text == NULL)
355 break; // Can't do it, out of memory
356 saved_text[startcol] = NUL;
357
358 // Backspace over characters that will move to the next line
359 if (!fo_white_par)
360 backspace_until_column(foundcol);
361 }
362 else
363 {
364 // put cursor after pos. to break line
365 if (!fo_white_par)
366 curwin->w_cursor.col = foundcol;
367 }
368
369 // Split the line just before the margin.
370 // Only insert/delete lines, but don't really redraw the window.
371 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
372 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
373 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaar7e667782022-05-23 13:10:48 +0100374 + OPENLINE_FORMAT
Bram Moolenaar11abd092020-05-01 14:26:37 +0200375 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar6e371ec2021-12-12 14:16:39 +0000376 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent),
377 &did_do_comment);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200378 if (!(flags & INSCHAR_COM_LIST))
379 old_indent = 0;
380
Bram Moolenaar6e371ec2021-12-12 14:16:39 +0000381 // If a comment leader was inserted, may also do this on a following
382 // line.
383 if (did_do_comment)
384 no_leader = FALSE;
385
Bram Moolenaar11abd092020-05-01 14:26:37 +0200386 replace_offset = 0;
387 if (first_line)
388 {
389 if (!(flags & INSCHAR_COM_LIST))
390 {
391 // This section is for auto-wrap of numeric lists. When not
392 // in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
393 // flag will be set and open_line() will handle it (as seen
394 // above). The code here (and in get_number_indent()) will
395 // recognize comments if needed...
396 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
397 second_indent =
398 get_number_indent(curwin->w_cursor.lnum - 1);
399 if (second_indent >= 0)
400 {
401 if (State & VREPLACE_FLAG)
402 change_indent(INDENT_SET, second_indent,
403 FALSE, NUL, TRUE);
404 else
405 if (leader_len > 0 && second_indent - leader_len > 0)
406 {
407 int i;
408 int padding = second_indent - leader_len;
409
410 // We started at the first_line of a numbered list
411 // that has a comment. the open_line() function has
412 // inserted the proper comment leader and positioned
413 // the cursor at the end of the split line. Now we
414 // add the additional whitespace needed after the
415 // comment leader for the numbered list.
416 for (i = 0; i < padding; i++)
417 ins_str((char_u *)" ");
418 }
419 else
420 {
421 (void)set_indent(second_indent, SIN_CHANGED);
422 }
423 }
424 }
425 first_line = FALSE;
426 }
427
428 if (State & VREPLACE_FLAG)
429 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100430 // In MODE_VREPLACE state we have backspaced over the text to be
Bram Moolenaar11abd092020-05-01 14:26:37 +0200431 // moved, now we re-insert it into the new line.
432 ins_bytes(saved_text);
433 vim_free(saved_text);
434 }
435 else
436 {
437 // Check if cursor is not past the NUL off the line, cindent
438 // may have added or removed indent.
439 curwin->w_cursor.col += startcol;
440 len = (colnr_T)STRLEN(ml_get_curline());
441 if (curwin->w_cursor.col > len)
442 curwin->w_cursor.col = len;
443 }
444
445 haveto_redraw = TRUE;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200446 set_can_cindent(TRUE);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200447 // moved the cursor, don't autoindent or cindent now
448 did_ai = FALSE;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200449 did_si = FALSE;
450 can_si = FALSE;
451 can_si_back = FALSE;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200452 line_breakcheck();
453 }
454
455 if (save_char != NUL) // put back space after cursor
456 pchar_cursor(save_char);
457
458#ifdef FEAT_LINEBREAK
459 curwin->w_p_lbr = has_lbr;
460#endif
461 if (!format_only && haveto_redraw)
462 {
463 update_topline();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100464 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200465 }
466}
467
468/*
469 * Blank lines, and lines containing only the comment leader, are left
470 * untouched by the formatting. The function returns TRUE in this
471 * case. It also returns TRUE when a line starts with the end of a comment
472 * ('e' in comment flags), so that this line is skipped, and not joined to the
473 * previous line. A new paragraph starts after a blank line, or when the
474 * comment leader changes -- webb.
475 */
476 static int
477fmt_check_par(
478 linenr_T lnum,
479 int *leader_len,
480 char_u **leader_flags,
481 int do_comments)
482{
483 char_u *flags = NULL; // init for GCC
484 char_u *ptr;
485
486 ptr = ml_get(lnum);
487 if (do_comments)
488 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
489 else
490 *leader_len = 0;
491
492 if (*leader_len > 0)
493 {
494 // Search for 'e' flag in comment leader flags.
495 flags = *leader_flags;
496 while (*flags && *flags != ':' && *flags != COM_END)
497 ++flags;
498 }
499
500 return (*skipwhite(ptr + *leader_len) == NUL
501 || (*leader_len > 0 && *flags == COM_END)
502 || startPS(lnum, NUL, FALSE));
503}
504
505/*
506 * Return TRUE if line "lnum" ends in a white character.
507 */
508 static int
509ends_in_white(linenr_T lnum)
510{
511 char_u *s = ml_get(lnum);
512 size_t l;
513
514 if (*s == NUL)
515 return FALSE;
516 // Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
517 // invocation may call function multiple times".
518 l = STRLEN(s) - 1;
519 return VIM_ISWHITE(s[l]);
520}
521
522/*
523 * Return TRUE if the two comment leaders given are the same. "lnum" is
524 * the first line. White-space is ignored. Note that the whole of
525 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
526 */
527 static int
528same_leader(
529 linenr_T lnum,
530 int leader1_len,
531 char_u *leader1_flags,
532 int leader2_len,
533 char_u *leader2_flags)
534{
535 int idx1 = 0, idx2 = 0;
536 char_u *p;
537 char_u *line1;
538 char_u *line2;
539
540 if (leader1_len == 0)
541 return (leader2_len == 0);
542
543 // If first leader has 'f' flag, the lines can be joined only if the
544 // second line does not have a leader.
545 // If first leader has 'e' flag, the lines can never be joined.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000546 // If first leader has 's' flag, the lines can only be joined if there is
Bram Moolenaar11abd092020-05-01 14:26:37 +0200547 // some text after it and the second line has the 'm' flag.
548 if (leader1_flags != NULL)
549 {
550 for (p = leader1_flags; *p && *p != ':'; ++p)
551 {
552 if (*p == COM_FIRST)
553 return (leader2_len == 0);
554 if (*p == COM_END)
555 return FALSE;
556 if (*p == COM_START)
557 {
zeertzjq9cbf7912023-01-23 16:57:08 +0000558 int line_len = (int)STRLEN(ml_get(lnum));
Bram Moolenaar11977f92023-01-21 13:09:19 +0000559 if (line_len <= leader1_len)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200560 return FALSE;
561 if (leader2_flags == NULL || leader2_len == 0)
562 return FALSE;
563 for (p = leader2_flags; *p && *p != ':'; ++p)
564 if (*p == COM_MIDDLE)
565 return TRUE;
566 return FALSE;
567 }
568 }
569 }
570
571 // Get current line and next line, compare the leaders.
572 // The first line has to be saved, only one line can be locked at a time.
573 line1 = vim_strsave(ml_get(lnum));
574 if (line1 != NULL)
575 {
576 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
577 ;
578 line2 = ml_get(lnum + 1);
579 for (idx2 = 0; idx2 < leader2_len; ++idx2)
580 {
581 if (!VIM_ISWHITE(line2[idx2]))
582 {
583 if (line1[idx1++] != line2[idx2])
584 break;
585 }
586 else
587 while (VIM_ISWHITE(line1[idx1]))
588 ++idx1;
589 }
590 vim_free(line1);
591 }
592 return (idx2 == leader2_len && idx1 == leader1_len);
593}
594
595/*
596 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
597 * previous line is in the same paragraph. Used for auto-formatting.
598 */
599 static int
600paragraph_start(linenr_T lnum)
601{
602 char_u *p;
603 int leader_len = 0; // leader len of current line
604 char_u *leader_flags = NULL; // flags for leader of current line
605 int next_leader_len; // leader len of next line
606 char_u *next_leader_flags; // flags for leader of next line
607 int do_comments; // format comments
608
609 if (lnum <= 1)
610 return TRUE; // start of the file
611
612 p = ml_get(lnum - 1);
613 if (*p == NUL)
614 return TRUE; // after empty line
615
616 do_comments = has_format_option(FO_Q_COMS);
617 if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments))
618 return TRUE; // after non-paragraph line
619
620 if (fmt_check_par(lnum, &next_leader_len, &next_leader_flags, do_comments))
621 return TRUE; // "lnum" is not a paragraph line
622
623 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
624 return TRUE; // missing trailing space in previous line.
625
626 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
627 return TRUE; // numbered item starts in "lnum".
628
629 if (!same_leader(lnum - 1, leader_len, leader_flags,
630 next_leader_len, next_leader_flags))
631 return TRUE; // change of comment leader.
632
633 return FALSE;
634}
635
636/*
637 * Called after inserting or deleting text: When 'formatoptions' includes the
638 * 'a' flag format from the current line until the end of the paragraph.
639 * Keep the cursor at the same position relative to the text.
640 * The caller must have saved the cursor line for undo, following ones will be
641 * saved here.
642 */
643 void
644auto_format(
645 int trailblank, // when TRUE also format with trailing blank
646 int prev_line) // may start in previous line
647{
648 pos_T pos;
649 colnr_T len;
650 char_u *old;
651 char_u *new, *pnew;
652 int wasatend;
653 int cc;
654
655 if (!has_format_option(FO_AUTO))
656 return;
657
658 pos = curwin->w_cursor;
659 old = ml_get_curline();
660
661 // may remove added space
662 check_auto_format(FALSE);
663
664 // Don't format in Insert mode when the cursor is on a trailing blank, the
665 // user might insert normal text next. Also skip formatting when "1" is
666 // in 'formatoptions' and there is a single character before the cursor.
667 // Otherwise the line would be broken and when typing another non-white
668 // next they are not joined back together.
669 wasatend = (pos.col == (colnr_T)STRLEN(old));
670 if (*old != NUL && !trailblank && wasatend)
671 {
672 dec_cursor();
673 cc = gchar_cursor();
674 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
675 && has_format_option(FO_ONE_LETTER))
676 dec_cursor();
677 cc = gchar_cursor();
678 if (WHITECHAR(cc))
679 {
680 curwin->w_cursor = pos;
681 return;
682 }
683 curwin->w_cursor = pos;
684 }
685
686 // With the 'c' flag in 'formatoptions' and 't' missing: only format
687 // comments.
688 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
689 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
690 return;
691
692 // May start formatting in a previous line, so that after "x" a word is
693 // moved to the previous line if it fits there now. Only when this is not
694 // the start of a paragraph.
695 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
696 {
697 --curwin->w_cursor.lnum;
698 if (u_save_cursor() == FAIL)
699 return;
700 }
701
702 // Do the formatting and restore the cursor position. "saved_cursor" will
703 // be adjusted for the text formatting.
704 saved_cursor = pos;
705 format_lines((linenr_T)-1, FALSE);
706 curwin->w_cursor = saved_cursor;
707 saved_cursor.lnum = 0;
708
709 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
710 {
711 // "cannot happen"
712 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
713 coladvance((colnr_T)MAXCOL);
714 }
715 else
716 check_cursor_col();
717
718 // Insert mode: If the cursor is now after the end of the line while it
719 // previously wasn't, the line was broken. Because of the rule above we
720 // need to add a space when 'w' is in 'formatoptions' to keep a paragraph
721 // formatted.
722 if (!wasatend && has_format_option(FO_WHITE_PAR))
723 {
724 new = ml_get_curline();
725 len = (colnr_T)STRLEN(new);
726 if (curwin->w_cursor.col == len)
727 {
728 pnew = vim_strnsave(new, len + 2);
729 pnew[len] = ' ';
730 pnew[len + 1] = NUL;
731 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
732 // remove the space later
733 did_add_space = TRUE;
734 }
735 else
736 // may remove added space
737 check_auto_format(FALSE);
738 }
739
740 check_cursor();
741}
742
743/*
744 * When an extra space was added to continue a paragraph for auto-formatting,
745 * delete it now. The space must be under the cursor, just after the insert
746 * position.
747 */
748 void
749check_auto_format(
750 int end_insert) // TRUE when ending Insert mode
751{
752 int c = ' ';
753 int cc;
754
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000755 if (!did_add_space)
756 return;
757
758 cc = gchar_cursor();
759 if (!WHITECHAR(cc))
760 // Somehow the space was removed already.
761 did_add_space = FALSE;
762 else
Bram Moolenaar11abd092020-05-01 14:26:37 +0200763 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000764 if (!end_insert)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200765 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000766 inc_cursor();
767 c = gchar_cursor();
768 dec_cursor();
769 }
770 if (c != NUL)
771 {
772 // The space is no longer at the end of the line, delete it.
773 del_char(FALSE);
774 did_add_space = FALSE;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200775 }
776 }
777}
778
779/*
780 * Find out textwidth to be used for formatting:
781 * if 'textwidth' option is set, use it
782 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
783 * if invalid value, use 0.
784 * Set default to window width (maximum 79) for "gq" operator.
785 */
786 int
787comp_textwidth(
788 int ff) // force formatting (for "gq" command)
789{
790 int textwidth;
791
792 textwidth = curbuf->b_p_tw;
793 if (textwidth == 0 && curbuf->b_p_wm)
794 {
795 // The width is the window width minus 'wrapmargin' minus all the
796 // things that add to the margin.
797 textwidth = curwin->w_width - curbuf->b_p_wm;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200798 if (cmdwin_type != 0)
799 textwidth -= 1;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200800#ifdef FEAT_FOLDING
801 textwidth -= curwin->w_p_fdc;
802#endif
803#ifdef FEAT_SIGNS
804 if (signcolumn_on(curwin))
805 textwidth -= 1;
806#endif
807 if (curwin->w_p_nu || curwin->w_p_rnu)
808 textwidth -= 8;
809 }
810 if (textwidth < 0)
811 textwidth = 0;
812 if (ff && textwidth == 0)
813 {
814 textwidth = curwin->w_width - 1;
815 if (textwidth > 79)
816 textwidth = 79;
817 }
818 return textwidth;
819}
820
821/*
822 * Implementation of the format operator 'gq'.
823 */
824 void
825op_format(
826 oparg_T *oap,
827 int keep_cursor) // keep cursor on same text char
828{
829 long old_line_count = curbuf->b_ml.ml_line_count;
830
831 // Place the cursor where the "gq" or "gw" command was given, so that "u"
832 // can put it back there.
833 curwin->w_cursor = oap->cursor_start;
834
835 if (u_save((linenr_T)(oap->start.lnum - 1),
836 (linenr_T)(oap->end.lnum + 1)) == FAIL)
837 return;
838 curwin->w_cursor = oap->start;
839
840 if (oap->is_VIsual)
841 // When there is no change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100842 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200843
Bram Moolenaare1004402020-10-24 20:49:43 +0200844 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200845 // Set '[ mark at the start of the formatted area
846 curbuf->b_op_start = oap->start;
847
848 // For "gw" remember the cursor position and put it back below (adjusted
849 // for joined and split lines).
850 if (keep_cursor)
851 saved_cursor = oap->cursor_start;
852
853 format_lines(oap->line_count, keep_cursor);
854
855 // Leave the cursor at the first non-blank of the last formatted line.
856 // If the cursor was moved one line back (e.g. with "Q}") go to the next
857 // line, so "." will do the next lines.
858 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
859 ++curwin->w_cursor.lnum;
860 beginline(BL_WHITE | BL_FIX);
861 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
862 msgmore(old_line_count);
863
Bram Moolenaare1004402020-10-24 20:49:43 +0200864 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200865 // put '] mark on the end of the formatted area
866 curbuf->b_op_end = curwin->w_cursor;
867
868 if (keep_cursor)
869 {
870 curwin->w_cursor = saved_cursor;
871 saved_cursor.lnum = 0;
Bram Moolenaar78d52882022-05-24 13:57:54 +0100872
873 // formatting may have made the cursor position invalid
874 check_cursor();
Bram Moolenaar11abd092020-05-01 14:26:37 +0200875 }
876
877 if (oap->is_VIsual)
878 {
879 win_T *wp;
880
881 FOR_ALL_WINDOWS(wp)
882 {
883 if (wp->w_old_cursor_lnum != 0)
884 {
885 // When lines have been inserted or deleted, adjust the end of
886 // the Visual area to be redrawn.
887 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
888 wp->w_old_cursor_lnum += old_line_count;
889 else
890 wp->w_old_visual_lnum += old_line_count;
891 }
892 }
893 }
894}
895
896#if defined(FEAT_EVAL) || defined(PROTO)
897/*
898 * Implementation of the format operator 'gq' for when using 'formatexpr'.
899 */
900 void
901op_formatexpr(oparg_T *oap)
902{
903 if (oap->is_VIsual)
904 // When there is no change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100905 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200906
907 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
908 // As documented: when 'formatexpr' returns non-zero fall back to
909 // internal formatting.
910 op_format(oap, FALSE);
911}
912
913 int
914fex_format(
915 linenr_T lnum,
916 long count,
917 int c) // character to be inserted
918{
919 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
920 OPT_LOCAL);
921 int r;
922 char_u *fex;
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000923 sctx_T save_sctx = current_sctx;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200924
925 // Set v:lnum to the first line number and v:count to the number of lines.
926 // Set v:char to the character to be inserted (can be NUL).
927 set_vim_var_nr(VV_LNUM, lnum);
928 set_vim_var_nr(VV_COUNT, count);
929 set_vim_var_char(c);
930
931 // Make a copy, the option could be changed while calling it.
932 fex = vim_strsave(curbuf->b_p_fex);
933 if (fex == NULL)
934 return 0;
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000935 current_sctx = curbuf->b_p_script_ctx[BV_FEX];
Bram Moolenaar11abd092020-05-01 14:26:37 +0200936
937 // Evaluate the function.
938 if (use_sandbox)
939 ++sandbox;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100940 r = (int)eval_to_number(fex, TRUE);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200941 if (use_sandbox)
942 --sandbox;
943
944 set_vim_var_string(VV_CHAR, NULL, -1);
945 vim_free(fex);
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000946 current_sctx = save_sctx;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200947
948 return r;
949}
950#endif
951
952/*
953 * Format "line_count" lines, starting at the cursor position.
954 * When "line_count" is negative, format until the end of the paragraph.
955 * Lines after the cursor line are saved for undo, caller must have saved the
956 * first line.
957 */
958 void
959format_lines(
960 linenr_T line_count,
961 int avoid_fex) // don't use 'formatexpr'
962{
963 int max_len;
964 int is_not_par; // current line not part of parag.
965 int next_is_not_par; // next line not part of paragraph
966 int is_end_par; // at end of paragraph
967 int prev_is_end_par = FALSE;// prev. line not part of parag.
968 int next_is_start_par = FALSE;
969 int leader_len = 0; // leader len of current line
970 int next_leader_len; // leader len of next line
971 char_u *leader_flags = NULL; // flags for leader of current line
Bram Moolenaar264d3dd2021-12-29 14:09:32 +0000972 char_u *next_leader_flags = NULL; // flags for leader of next line
Bram Moolenaar11abd092020-05-01 14:26:37 +0200973 int do_comments; // format comments
974 int do_comments_list = 0; // format comments with 'n' or '2'
975 int advance = TRUE;
976 int second_indent = -1; // indent for second line (comment
977 // aware)
978 int do_second_indent;
979 int do_number_indent;
980 int do_trail_white;
981 int first_par_line = TRUE;
982 int smd_save;
983 long count;
984 int need_set_indent = TRUE; // set indent of next paragraph
Bram Moolenaarecabb512021-12-06 19:51:01 +0000985 linenr_T first_line = curwin->w_cursor.lnum;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200986 int force_format = FALSE;
987 int old_State = State;
988
989 // length of a line to force formatting: 3 * 'tw'
990 max_len = comp_textwidth(TRUE) * 3;
991
992 // check for 'q', '2' and '1' in 'formatoptions'
993 do_comments = has_format_option(FO_Q_COMS);
994 do_second_indent = has_format_option(FO_Q_SECOND);
995 do_number_indent = has_format_option(FO_Q_NUMBER);
996 do_trail_white = has_format_option(FO_WHITE_PAR);
997
998 // Get info about the previous and current line.
999 if (curwin->w_cursor.lnum > 1)
1000 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
1001 , &leader_len, &leader_flags, do_comments);
1002 else
1003 is_not_par = TRUE;
1004 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
1005 , &next_leader_len, &next_leader_flags, do_comments);
1006 is_end_par = (is_not_par || next_is_not_par);
1007 if (!is_end_par && do_trail_white)
1008 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
1009
1010 curwin->w_cursor.lnum--;
1011 for (count = line_count; count != 0 && !got_int; --count)
1012 {
1013 // Advance to next paragraph.
1014 if (advance)
1015 {
1016 curwin->w_cursor.lnum++;
1017 prev_is_end_par = is_end_par;
1018 is_not_par = next_is_not_par;
1019 leader_len = next_leader_len;
1020 leader_flags = next_leader_flags;
1021 }
1022
1023 // The last line to be formatted.
1024 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
1025 {
1026 next_is_not_par = TRUE;
1027 next_leader_len = 0;
1028 next_leader_flags = NULL;
1029 }
1030 else
1031 {
1032 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
1033 , &next_leader_len, &next_leader_flags, do_comments);
1034 if (do_number_indent)
1035 next_is_start_par =
1036 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
1037 }
1038 advance = TRUE;
1039 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
1040 if (!is_end_par && do_trail_white)
1041 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
1042
1043 // Skip lines that are not in a paragraph.
1044 if (is_not_par)
1045 {
1046 if (line_count < 0)
1047 break;
1048 }
1049 else
1050 {
1051 // For the first line of a paragraph, check indent of second line.
1052 // Don't do this for comments and empty lines.
1053 if (first_par_line
1054 && (do_second_indent || do_number_indent)
1055 && prev_is_end_par
1056 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1057 {
1058 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
1059 {
1060 if (leader_len == 0 && next_leader_len == 0)
1061 {
1062 // no comment found
1063 second_indent =
1064 get_indent_lnum(curwin->w_cursor.lnum + 1);
1065 }
1066 else
1067 {
1068 second_indent = next_leader_len;
1069 do_comments_list = 1;
1070 }
1071 }
1072 else if (do_number_indent)
1073 {
1074 if (leader_len == 0 && next_leader_len == 0)
1075 {
1076 // no comment found
1077 second_indent =
1078 get_number_indent(curwin->w_cursor.lnum);
1079 }
1080 else
1081 {
1082 // get_number_indent() is now "comment aware"...
1083 second_indent =
1084 get_number_indent(curwin->w_cursor.lnum);
1085 do_comments_list = 1;
1086 }
1087 }
1088 }
1089
1090 // When the comment leader changes, it's the end of the paragraph.
1091 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
1092 || !same_leader(curwin->w_cursor.lnum,
1093 leader_len, leader_flags,
1094 next_leader_len, next_leader_flags))
Bram Moolenaar264d3dd2021-12-29 14:09:32 +00001095 {
1096 // Special case: If the next line starts with a line comment
1097 // and this line has a line comment after some text, the
1098 // paragraph doesn't really end.
1099 if (next_leader_flags == NULL
1100 || STRNCMP(next_leader_flags, "://", 3) != 0
1101 || check_linecomment(ml_get_curline()) == MAXCOL)
Bram Moolenaar11abd092020-05-01 14:26:37 +02001102 is_end_par = TRUE;
Bram Moolenaar264d3dd2021-12-29 14:09:32 +00001103 }
Bram Moolenaar11abd092020-05-01 14:26:37 +02001104
1105 // If we have got to the end of a paragraph, or the line is
1106 // getting long, format it.
1107 if (is_end_par || force_format)
1108 {
1109 if (need_set_indent)
Christian Brabandt818ff252021-11-18 13:56:37 +00001110 {
1111 int indent = 0; // amount of indent needed
1112
Bram Moolenaarecabb512021-12-06 19:51:01 +00001113 // Replace indent in first line of a paragraph with minimal
1114 // number of tabs and spaces, according to current options.
1115 // For the very first formatted line keep the current
1116 // indent.
1117 if (curwin->w_cursor.lnum == first_line)
1118 indent = get_indent();
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001119 else if (curbuf->b_p_lisp)
Christian Brabandt818ff252021-11-18 13:56:37 +00001120 indent = get_lisp_indent();
1121 else
Christian Brabandt818ff252021-11-18 13:56:37 +00001122 {
Christian Brabandt818ff252021-11-18 13:56:37 +00001123 if (cindent_on())
1124 {
1125 indent =
1126# ifdef FEAT_EVAL
1127 *curbuf->b_p_inde != NUL ? get_expr_indent() :
1128# endif
1129 get_c_indent();
1130 }
1131 else
Christian Brabandt818ff252021-11-18 13:56:37 +00001132 indent = get_indent();
1133 }
1134 (void)set_indent(indent, SIN_CHANGED);
1135 }
Bram Moolenaar11abd092020-05-01 14:26:37 +02001136
1137 // put cursor on last non-space
Bram Moolenaar24959102022-05-07 20:01:16 +01001138 State = MODE_NORMAL; // don't go past end-of-line
Bram Moolenaar11abd092020-05-01 14:26:37 +02001139 coladvance((colnr_T)MAXCOL);
1140 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
1141 dec_cursor();
1142
1143 // do the formatting, without 'showmode'
Bram Moolenaar24959102022-05-07 20:01:16 +01001144 State = MODE_INSERT; // for open_line()
Bram Moolenaar11abd092020-05-01 14:26:37 +02001145 smd_save = p_smd;
1146 p_smd = FALSE;
1147 insertchar(NUL, INSCHAR_FORMAT
1148 + (do_comments ? INSCHAR_DO_COM : 0)
1149 + (do_comments && do_comments_list
1150 ? INSCHAR_COM_LIST : 0)
1151 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
1152 State = old_State;
1153 p_smd = smd_save;
1154 second_indent = -1;
1155 // at end of par.: need to set indent of next par.
1156 need_set_indent = is_end_par;
1157 if (is_end_par)
1158 {
1159 // When called with a negative line count, break at the
1160 // end of the paragraph.
1161 if (line_count < 0)
1162 break;
1163 first_par_line = TRUE;
1164 }
1165 force_format = FALSE;
1166 }
1167
1168 // When still in same paragraph, join the lines together. But
1169 // first delete the leader from the second line.
1170 if (!is_end_par)
1171 {
1172 advance = FALSE;
1173 curwin->w_cursor.lnum++;
1174 curwin->w_cursor.col = 0;
1175 if (line_count < 0 && u_save_cursor() == FAIL)
1176 break;
1177 if (next_leader_len > 0)
1178 {
1179 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
1180 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
1181 (long)-next_leader_len, 0);
1182 }
1183 else if (second_indent > 0) // the "leader" for FO_Q_SECOND
1184 {
1185 int indent = getwhitecols_curline();
1186
1187 if (indent > 0)
1188 {
1189 (void)del_bytes(indent, FALSE, FALSE);
1190 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001191 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar11abd092020-05-01 14:26:37 +02001192 }
1193 }
1194 curwin->w_cursor.lnum--;
1195 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
1196 {
1197 beep_flush();
1198 break;
1199 }
1200 first_par_line = FALSE;
1201 // If the line is getting long, format it next time
1202 if (STRLEN(ml_get_curline()) > (size_t)max_len)
1203 force_format = TRUE;
1204 else
1205 force_format = FALSE;
1206 }
1207 }
1208 line_breakcheck();
1209 }
1210}