blob: f34c9984817024ac05ad74845e9806b4b8c2c30f [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();
464 redraw_curbuf_later(VALID);
465 }
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 {
558 if (*(ml_get(lnum) + leader1_len) == NUL)
559 return FALSE;
560 if (leader2_flags == NULL || leader2_len == 0)
561 return FALSE;
562 for (p = leader2_flags; *p && *p != ':'; ++p)
563 if (*p == COM_MIDDLE)
564 return TRUE;
565 return FALSE;
566 }
567 }
568 }
569
570 // Get current line and next line, compare the leaders.
571 // The first line has to be saved, only one line can be locked at a time.
572 line1 = vim_strsave(ml_get(lnum));
573 if (line1 != NULL)
574 {
575 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
576 ;
577 line2 = ml_get(lnum + 1);
578 for (idx2 = 0; idx2 < leader2_len; ++idx2)
579 {
580 if (!VIM_ISWHITE(line2[idx2]))
581 {
582 if (line1[idx1++] != line2[idx2])
583 break;
584 }
585 else
586 while (VIM_ISWHITE(line1[idx1]))
587 ++idx1;
588 }
589 vim_free(line1);
590 }
591 return (idx2 == leader2_len && idx1 == leader1_len);
592}
593
594/*
595 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
596 * previous line is in the same paragraph. Used for auto-formatting.
597 */
598 static int
599paragraph_start(linenr_T lnum)
600{
601 char_u *p;
602 int leader_len = 0; // leader len of current line
603 char_u *leader_flags = NULL; // flags for leader of current line
604 int next_leader_len; // leader len of next line
605 char_u *next_leader_flags; // flags for leader of next line
606 int do_comments; // format comments
607
608 if (lnum <= 1)
609 return TRUE; // start of the file
610
611 p = ml_get(lnum - 1);
612 if (*p == NUL)
613 return TRUE; // after empty line
614
615 do_comments = has_format_option(FO_Q_COMS);
616 if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments))
617 return TRUE; // after non-paragraph line
618
619 if (fmt_check_par(lnum, &next_leader_len, &next_leader_flags, do_comments))
620 return TRUE; // "lnum" is not a paragraph line
621
622 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
623 return TRUE; // missing trailing space in previous line.
624
625 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
626 return TRUE; // numbered item starts in "lnum".
627
628 if (!same_leader(lnum - 1, leader_len, leader_flags,
629 next_leader_len, next_leader_flags))
630 return TRUE; // change of comment leader.
631
632 return FALSE;
633}
634
635/*
636 * Called after inserting or deleting text: When 'formatoptions' includes the
637 * 'a' flag format from the current line until the end of the paragraph.
638 * Keep the cursor at the same position relative to the text.
639 * The caller must have saved the cursor line for undo, following ones will be
640 * saved here.
641 */
642 void
643auto_format(
644 int trailblank, // when TRUE also format with trailing blank
645 int prev_line) // may start in previous line
646{
647 pos_T pos;
648 colnr_T len;
649 char_u *old;
650 char_u *new, *pnew;
651 int wasatend;
652 int cc;
653
654 if (!has_format_option(FO_AUTO))
655 return;
656
657 pos = curwin->w_cursor;
658 old = ml_get_curline();
659
660 // may remove added space
661 check_auto_format(FALSE);
662
663 // Don't format in Insert mode when the cursor is on a trailing blank, the
664 // user might insert normal text next. Also skip formatting when "1" is
665 // in 'formatoptions' and there is a single character before the cursor.
666 // Otherwise the line would be broken and when typing another non-white
667 // next they are not joined back together.
668 wasatend = (pos.col == (colnr_T)STRLEN(old));
669 if (*old != NUL && !trailblank && wasatend)
670 {
671 dec_cursor();
672 cc = gchar_cursor();
673 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
674 && has_format_option(FO_ONE_LETTER))
675 dec_cursor();
676 cc = gchar_cursor();
677 if (WHITECHAR(cc))
678 {
679 curwin->w_cursor = pos;
680 return;
681 }
682 curwin->w_cursor = pos;
683 }
684
685 // With the 'c' flag in 'formatoptions' and 't' missing: only format
686 // comments.
687 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
688 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
689 return;
690
691 // May start formatting in a previous line, so that after "x" a word is
692 // moved to the previous line if it fits there now. Only when this is not
693 // the start of a paragraph.
694 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
695 {
696 --curwin->w_cursor.lnum;
697 if (u_save_cursor() == FAIL)
698 return;
699 }
700
701 // Do the formatting and restore the cursor position. "saved_cursor" will
702 // be adjusted for the text formatting.
703 saved_cursor = pos;
704 format_lines((linenr_T)-1, FALSE);
705 curwin->w_cursor = saved_cursor;
706 saved_cursor.lnum = 0;
707
708 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
709 {
710 // "cannot happen"
711 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
712 coladvance((colnr_T)MAXCOL);
713 }
714 else
715 check_cursor_col();
716
717 // Insert mode: If the cursor is now after the end of the line while it
718 // previously wasn't, the line was broken. Because of the rule above we
719 // need to add a space when 'w' is in 'formatoptions' to keep a paragraph
720 // formatted.
721 if (!wasatend && has_format_option(FO_WHITE_PAR))
722 {
723 new = ml_get_curline();
724 len = (colnr_T)STRLEN(new);
725 if (curwin->w_cursor.col == len)
726 {
727 pnew = vim_strnsave(new, len + 2);
728 pnew[len] = ' ';
729 pnew[len + 1] = NUL;
730 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
731 // remove the space later
732 did_add_space = TRUE;
733 }
734 else
735 // may remove added space
736 check_auto_format(FALSE);
737 }
738
739 check_cursor();
740}
741
742/*
743 * When an extra space was added to continue a paragraph for auto-formatting,
744 * delete it now. The space must be under the cursor, just after the insert
745 * position.
746 */
747 void
748check_auto_format(
749 int end_insert) // TRUE when ending Insert mode
750{
751 int c = ' ';
752 int cc;
753
754 if (did_add_space)
755 {
756 cc = gchar_cursor();
757 if (!WHITECHAR(cc))
758 // Somehow the space was removed already.
759 did_add_space = FALSE;
760 else
761 {
762 if (!end_insert)
763 {
764 inc_cursor();
765 c = gchar_cursor();
766 dec_cursor();
767 }
768 if (c != NUL)
769 {
770 // The space is no longer at the end of the line, delete it.
771 del_char(FALSE);
772 did_add_space = FALSE;
773 }
774 }
775 }
776}
777
778/*
779 * Find out textwidth to be used for formatting:
780 * if 'textwidth' option is set, use it
781 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
782 * if invalid value, use 0.
783 * Set default to window width (maximum 79) for "gq" operator.
784 */
785 int
786comp_textwidth(
787 int ff) // force formatting (for "gq" command)
788{
789 int textwidth;
790
791 textwidth = curbuf->b_p_tw;
792 if (textwidth == 0 && curbuf->b_p_wm)
793 {
794 // The width is the window width minus 'wrapmargin' minus all the
795 // things that add to the margin.
796 textwidth = curwin->w_width - curbuf->b_p_wm;
797#ifdef FEAT_CMDWIN
798 if (cmdwin_type != 0)
799 textwidth -= 1;
800#endif
801#ifdef FEAT_FOLDING
802 textwidth -= curwin->w_p_fdc;
803#endif
804#ifdef FEAT_SIGNS
805 if (signcolumn_on(curwin))
806 textwidth -= 1;
807#endif
808 if (curwin->w_p_nu || curwin->w_p_rnu)
809 textwidth -= 8;
810 }
811 if (textwidth < 0)
812 textwidth = 0;
813 if (ff && textwidth == 0)
814 {
815 textwidth = curwin->w_width - 1;
816 if (textwidth > 79)
817 textwidth = 79;
818 }
819 return textwidth;
820}
821
822/*
823 * Implementation of the format operator 'gq'.
824 */
825 void
826op_format(
827 oparg_T *oap,
828 int keep_cursor) // keep cursor on same text char
829{
830 long old_line_count = curbuf->b_ml.ml_line_count;
831
832 // Place the cursor where the "gq" or "gw" command was given, so that "u"
833 // can put it back there.
834 curwin->w_cursor = oap->cursor_start;
835
836 if (u_save((linenr_T)(oap->start.lnum - 1),
837 (linenr_T)(oap->end.lnum + 1)) == FAIL)
838 return;
839 curwin->w_cursor = oap->start;
840
841 if (oap->is_VIsual)
842 // When there is no change: need to remove the Visual selection
843 redraw_curbuf_later(INVERTED);
844
Bram Moolenaare1004402020-10-24 20:49:43 +0200845 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200846 // Set '[ mark at the start of the formatted area
847 curbuf->b_op_start = oap->start;
848
849 // For "gw" remember the cursor position and put it back below (adjusted
850 // for joined and split lines).
851 if (keep_cursor)
852 saved_cursor = oap->cursor_start;
853
854 format_lines(oap->line_count, keep_cursor);
855
856 // Leave the cursor at the first non-blank of the last formatted line.
857 // If the cursor was moved one line back (e.g. with "Q}") go to the next
858 // line, so "." will do the next lines.
859 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
860 ++curwin->w_cursor.lnum;
861 beginline(BL_WHITE | BL_FIX);
862 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
863 msgmore(old_line_count);
864
Bram Moolenaare1004402020-10-24 20:49:43 +0200865 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200866 // put '] mark on the end of the formatted area
867 curbuf->b_op_end = curwin->w_cursor;
868
869 if (keep_cursor)
870 {
871 curwin->w_cursor = saved_cursor;
872 saved_cursor.lnum = 0;
Bram Moolenaar78d52882022-05-24 13:57:54 +0100873
874 // formatting may have made the cursor position invalid
875 check_cursor();
Bram Moolenaar11abd092020-05-01 14:26:37 +0200876 }
877
878 if (oap->is_VIsual)
879 {
880 win_T *wp;
881
882 FOR_ALL_WINDOWS(wp)
883 {
884 if (wp->w_old_cursor_lnum != 0)
885 {
886 // When lines have been inserted or deleted, adjust the end of
887 // the Visual area to be redrawn.
888 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
889 wp->w_old_cursor_lnum += old_line_count;
890 else
891 wp->w_old_visual_lnum += old_line_count;
892 }
893 }
894 }
895}
896
897#if defined(FEAT_EVAL) || defined(PROTO)
898/*
899 * Implementation of the format operator 'gq' for when using 'formatexpr'.
900 */
901 void
902op_formatexpr(oparg_T *oap)
903{
904 if (oap->is_VIsual)
905 // When there is no change: need to remove the Visual selection
906 redraw_curbuf_later(INVERTED);
907
908 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
909 // As documented: when 'formatexpr' returns non-zero fall back to
910 // internal formatting.
911 op_format(oap, FALSE);
912}
913
914 int
915fex_format(
916 linenr_T lnum,
917 long count,
918 int c) // character to be inserted
919{
920 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
921 OPT_LOCAL);
922 int r;
923 char_u *fex;
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000924 sctx_T save_sctx = current_sctx;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200925
926 // Set v:lnum to the first line number and v:count to the number of lines.
927 // Set v:char to the character to be inserted (can be NUL).
928 set_vim_var_nr(VV_LNUM, lnum);
929 set_vim_var_nr(VV_COUNT, count);
930 set_vim_var_char(c);
931
932 // Make a copy, the option could be changed while calling it.
933 fex = vim_strsave(curbuf->b_p_fex);
934 if (fex == NULL)
935 return 0;
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000936 current_sctx = curbuf->b_p_script_ctx[BV_FEX];
Bram Moolenaar11abd092020-05-01 14:26:37 +0200937
938 // Evaluate the function.
939 if (use_sandbox)
940 ++sandbox;
941 r = (int)eval_to_number(fex);
942 if (use_sandbox)
943 --sandbox;
944
945 set_vim_var_string(VV_CHAR, NULL, -1);
946 vim_free(fex);
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000947 current_sctx = save_sctx;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200948
949 return r;
950}
951#endif
952
953/*
954 * Format "line_count" lines, starting at the cursor position.
955 * When "line_count" is negative, format until the end of the paragraph.
956 * Lines after the cursor line are saved for undo, caller must have saved the
957 * first line.
958 */
959 void
960format_lines(
961 linenr_T line_count,
962 int avoid_fex) // don't use 'formatexpr'
963{
964 int max_len;
965 int is_not_par; // current line not part of parag.
966 int next_is_not_par; // next line not part of paragraph
967 int is_end_par; // at end of paragraph
968 int prev_is_end_par = FALSE;// prev. line not part of parag.
969 int next_is_start_par = FALSE;
970 int leader_len = 0; // leader len of current line
971 int next_leader_len; // leader len of next line
972 char_u *leader_flags = NULL; // flags for leader of current line
Bram Moolenaar264d3dd2021-12-29 14:09:32 +0000973 char_u *next_leader_flags = NULL; // flags for leader of next line
Bram Moolenaar11abd092020-05-01 14:26:37 +0200974 int do_comments; // format comments
975 int do_comments_list = 0; // format comments with 'n' or '2'
976 int advance = TRUE;
977 int second_indent = -1; // indent for second line (comment
978 // aware)
979 int do_second_indent;
980 int do_number_indent;
981 int do_trail_white;
982 int first_par_line = TRUE;
983 int smd_save;
984 long count;
985 int need_set_indent = TRUE; // set indent of next paragraph
Bram Moolenaarecabb512021-12-06 19:51:01 +0000986 linenr_T first_line = curwin->w_cursor.lnum;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200987 int force_format = FALSE;
988 int old_State = State;
989
990 // length of a line to force formatting: 3 * 'tw'
991 max_len = comp_textwidth(TRUE) * 3;
992
993 // check for 'q', '2' and '1' in 'formatoptions'
994 do_comments = has_format_option(FO_Q_COMS);
995 do_second_indent = has_format_option(FO_Q_SECOND);
996 do_number_indent = has_format_option(FO_Q_NUMBER);
997 do_trail_white = has_format_option(FO_WHITE_PAR);
998
999 // Get info about the previous and current line.
1000 if (curwin->w_cursor.lnum > 1)
1001 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
1002 , &leader_len, &leader_flags, do_comments);
1003 else
1004 is_not_par = TRUE;
1005 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
1006 , &next_leader_len, &next_leader_flags, do_comments);
1007 is_end_par = (is_not_par || next_is_not_par);
1008 if (!is_end_par && do_trail_white)
1009 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
1010
1011 curwin->w_cursor.lnum--;
1012 for (count = line_count; count != 0 && !got_int; --count)
1013 {
1014 // Advance to next paragraph.
1015 if (advance)
1016 {
1017 curwin->w_cursor.lnum++;
1018 prev_is_end_par = is_end_par;
1019 is_not_par = next_is_not_par;
1020 leader_len = next_leader_len;
1021 leader_flags = next_leader_flags;
1022 }
1023
1024 // The last line to be formatted.
1025 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
1026 {
1027 next_is_not_par = TRUE;
1028 next_leader_len = 0;
1029 next_leader_flags = NULL;
1030 }
1031 else
1032 {
1033 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
1034 , &next_leader_len, &next_leader_flags, do_comments);
1035 if (do_number_indent)
1036 next_is_start_par =
1037 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
1038 }
1039 advance = TRUE;
1040 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
1041 if (!is_end_par && do_trail_white)
1042 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
1043
1044 // Skip lines that are not in a paragraph.
1045 if (is_not_par)
1046 {
1047 if (line_count < 0)
1048 break;
1049 }
1050 else
1051 {
1052 // For the first line of a paragraph, check indent of second line.
1053 // Don't do this for comments and empty lines.
1054 if (first_par_line
1055 && (do_second_indent || do_number_indent)
1056 && prev_is_end_par
1057 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1058 {
1059 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
1060 {
1061 if (leader_len == 0 && next_leader_len == 0)
1062 {
1063 // no comment found
1064 second_indent =
1065 get_indent_lnum(curwin->w_cursor.lnum + 1);
1066 }
1067 else
1068 {
1069 second_indent = next_leader_len;
1070 do_comments_list = 1;
1071 }
1072 }
1073 else if (do_number_indent)
1074 {
1075 if (leader_len == 0 && next_leader_len == 0)
1076 {
1077 // no comment found
1078 second_indent =
1079 get_number_indent(curwin->w_cursor.lnum);
1080 }
1081 else
1082 {
1083 // get_number_indent() is now "comment aware"...
1084 second_indent =
1085 get_number_indent(curwin->w_cursor.lnum);
1086 do_comments_list = 1;
1087 }
1088 }
1089 }
1090
1091 // When the comment leader changes, it's the end of the paragraph.
1092 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
1093 || !same_leader(curwin->w_cursor.lnum,
1094 leader_len, leader_flags,
1095 next_leader_len, next_leader_flags))
Bram Moolenaar264d3dd2021-12-29 14:09:32 +00001096 {
1097 // Special case: If the next line starts with a line comment
1098 // and this line has a line comment after some text, the
1099 // paragraph doesn't really end.
1100 if (next_leader_flags == NULL
1101 || STRNCMP(next_leader_flags, "://", 3) != 0
1102 || check_linecomment(ml_get_curline()) == MAXCOL)
Bram Moolenaar11abd092020-05-01 14:26:37 +02001103 is_end_par = TRUE;
Bram Moolenaar264d3dd2021-12-29 14:09:32 +00001104 }
Bram Moolenaar11abd092020-05-01 14:26:37 +02001105
1106 // If we have got to the end of a paragraph, or the line is
1107 // getting long, format it.
1108 if (is_end_par || force_format)
1109 {
1110 if (need_set_indent)
Christian Brabandt818ff252021-11-18 13:56:37 +00001111 {
1112 int indent = 0; // amount of indent needed
1113
Bram Moolenaarecabb512021-12-06 19:51:01 +00001114 // Replace indent in first line of a paragraph with minimal
1115 // number of tabs and spaces, according to current options.
1116 // For the very first formatted line keep the current
1117 // indent.
1118 if (curwin->w_cursor.lnum == first_line)
1119 indent = get_indent();
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001120 else if (curbuf->b_p_lisp)
Christian Brabandt818ff252021-11-18 13:56:37 +00001121 indent = get_lisp_indent();
1122 else
Christian Brabandt818ff252021-11-18 13:56:37 +00001123 {
Christian Brabandt818ff252021-11-18 13:56:37 +00001124 if (cindent_on())
1125 {
1126 indent =
1127# ifdef FEAT_EVAL
1128 *curbuf->b_p_inde != NUL ? get_expr_indent() :
1129# endif
1130 get_c_indent();
1131 }
1132 else
Christian Brabandt818ff252021-11-18 13:56:37 +00001133 indent = get_indent();
1134 }
1135 (void)set_indent(indent, SIN_CHANGED);
1136 }
Bram Moolenaar11abd092020-05-01 14:26:37 +02001137
1138 // put cursor on last non-space
Bram Moolenaar24959102022-05-07 20:01:16 +01001139 State = MODE_NORMAL; // don't go past end-of-line
Bram Moolenaar11abd092020-05-01 14:26:37 +02001140 coladvance((colnr_T)MAXCOL);
1141 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
1142 dec_cursor();
1143
1144 // do the formatting, without 'showmode'
Bram Moolenaar24959102022-05-07 20:01:16 +01001145 State = MODE_INSERT; // for open_line()
Bram Moolenaar11abd092020-05-01 14:26:37 +02001146 smd_save = p_smd;
1147 p_smd = FALSE;
1148 insertchar(NUL, INSCHAR_FORMAT
1149 + (do_comments ? INSCHAR_DO_COM : 0)
1150 + (do_comments && do_comments_list
1151 ? INSCHAR_COM_LIST : 0)
1152 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
1153 State = old_State;
1154 p_smd = smd_save;
1155 second_indent = -1;
1156 // at end of par.: need to set indent of next par.
1157 need_set_indent = is_end_par;
1158 if (is_end_par)
1159 {
1160 // When called with a negative line count, break at the
1161 // end of the paragraph.
1162 if (line_count < 0)
1163 break;
1164 first_par_line = TRUE;
1165 }
1166 force_format = FALSE;
1167 }
1168
1169 // When still in same paragraph, join the lines together. But
1170 // first delete the leader from the second line.
1171 if (!is_end_par)
1172 {
1173 advance = FALSE;
1174 curwin->w_cursor.lnum++;
1175 curwin->w_cursor.col = 0;
1176 if (line_count < 0 && u_save_cursor() == FAIL)
1177 break;
1178 if (next_leader_len > 0)
1179 {
1180 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
1181 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
1182 (long)-next_leader_len, 0);
1183 }
1184 else if (second_indent > 0) // the "leader" for FO_Q_SECOND
1185 {
1186 int indent = getwhitecols_curline();
1187
1188 if (indent > 0)
1189 {
1190 (void)del_bytes(indent, FALSE, FALSE);
1191 mark_col_adjust(curwin->w_cursor.lnum,
1192 (colnr_T)0, 0L, (long)-indent, 0);
1193 }
1194 }
1195 curwin->w_cursor.lnum--;
1196 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
1197 {
1198 beep_flush();
1199 break;
1200 }
1201 first_par_line = FALSE;
1202 // If the line is getting long, format it next time
1203 if (STRLEN(ml_get_curline()) > (size_t)max_len)
1204 force_format = TRUE;
1205 else
1206 force_format = FALSE;
1207 }
1208 }
1209 line_breakcheck();
1210 }
1211}