blob: a6a3b4dc6748755c142e910809c9e5890f1206bc [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()
95 + char2cells(c != NUL ? c : gchar_cursor());
96 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)
374 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar6e371ec2021-12-12 14:16:39 +0000375 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent),
376 &did_do_comment);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200377 if (!(flags & INSCHAR_COM_LIST))
378 old_indent = 0;
379
Bram Moolenaar6e371ec2021-12-12 14:16:39 +0000380 // If a comment leader was inserted, may also do this on a following
381 // line.
382 if (did_do_comment)
383 no_leader = FALSE;
384
Bram Moolenaar11abd092020-05-01 14:26:37 +0200385 replace_offset = 0;
386 if (first_line)
387 {
388 if (!(flags & INSCHAR_COM_LIST))
389 {
390 // This section is for auto-wrap of numeric lists. When not
391 // in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
392 // flag will be set and open_line() will handle it (as seen
393 // above). The code here (and in get_number_indent()) will
394 // recognize comments if needed...
395 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
396 second_indent =
397 get_number_indent(curwin->w_cursor.lnum - 1);
398 if (second_indent >= 0)
399 {
400 if (State & VREPLACE_FLAG)
401 change_indent(INDENT_SET, second_indent,
402 FALSE, NUL, TRUE);
403 else
404 if (leader_len > 0 && second_indent - leader_len > 0)
405 {
406 int i;
407 int padding = second_indent - leader_len;
408
409 // We started at the first_line of a numbered list
410 // that has a comment. the open_line() function has
411 // inserted the proper comment leader and positioned
412 // the cursor at the end of the split line. Now we
413 // add the additional whitespace needed after the
414 // comment leader for the numbered list.
415 for (i = 0; i < padding; i++)
416 ins_str((char_u *)" ");
417 }
418 else
419 {
420 (void)set_indent(second_indent, SIN_CHANGED);
421 }
422 }
423 }
424 first_line = FALSE;
425 }
426
427 if (State & VREPLACE_FLAG)
428 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100429 // In MODE_VREPLACE state we have backspaced over the text to be
Bram Moolenaar11abd092020-05-01 14:26:37 +0200430 // moved, now we re-insert it into the new line.
431 ins_bytes(saved_text);
432 vim_free(saved_text);
433 }
434 else
435 {
436 // Check if cursor is not past the NUL off the line, cindent
437 // may have added or removed indent.
438 curwin->w_cursor.col += startcol;
439 len = (colnr_T)STRLEN(ml_get_curline());
440 if (curwin->w_cursor.col > len)
441 curwin->w_cursor.col = len;
442 }
443
444 haveto_redraw = TRUE;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200445 set_can_cindent(TRUE);
Bram Moolenaar11abd092020-05-01 14:26:37 +0200446 // moved the cursor, don't autoindent or cindent now
447 did_ai = FALSE;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200448 did_si = FALSE;
449 can_si = FALSE;
450 can_si_back = FALSE;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200451 line_breakcheck();
452 }
453
454 if (save_char != NUL) // put back space after cursor
455 pchar_cursor(save_char);
456
457#ifdef FEAT_LINEBREAK
458 curwin->w_p_lbr = has_lbr;
459#endif
460 if (!format_only && haveto_redraw)
461 {
462 update_topline();
463 redraw_curbuf_later(VALID);
464 }
465}
466
467/*
468 * Blank lines, and lines containing only the comment leader, are left
469 * untouched by the formatting. The function returns TRUE in this
470 * case. It also returns TRUE when a line starts with the end of a comment
471 * ('e' in comment flags), so that this line is skipped, and not joined to the
472 * previous line. A new paragraph starts after a blank line, or when the
473 * comment leader changes -- webb.
474 */
475 static int
476fmt_check_par(
477 linenr_T lnum,
478 int *leader_len,
479 char_u **leader_flags,
480 int do_comments)
481{
482 char_u *flags = NULL; // init for GCC
483 char_u *ptr;
484
485 ptr = ml_get(lnum);
486 if (do_comments)
487 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
488 else
489 *leader_len = 0;
490
491 if (*leader_len > 0)
492 {
493 // Search for 'e' flag in comment leader flags.
494 flags = *leader_flags;
495 while (*flags && *flags != ':' && *flags != COM_END)
496 ++flags;
497 }
498
499 return (*skipwhite(ptr + *leader_len) == NUL
500 || (*leader_len > 0 && *flags == COM_END)
501 || startPS(lnum, NUL, FALSE));
502}
503
504/*
505 * Return TRUE if line "lnum" ends in a white character.
506 */
507 static int
508ends_in_white(linenr_T lnum)
509{
510 char_u *s = ml_get(lnum);
511 size_t l;
512
513 if (*s == NUL)
514 return FALSE;
515 // Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
516 // invocation may call function multiple times".
517 l = STRLEN(s) - 1;
518 return VIM_ISWHITE(s[l]);
519}
520
521/*
522 * Return TRUE if the two comment leaders given are the same. "lnum" is
523 * the first line. White-space is ignored. Note that the whole of
524 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
525 */
526 static int
527same_leader(
528 linenr_T lnum,
529 int leader1_len,
530 char_u *leader1_flags,
531 int leader2_len,
532 char_u *leader2_flags)
533{
534 int idx1 = 0, idx2 = 0;
535 char_u *p;
536 char_u *line1;
537 char_u *line2;
538
539 if (leader1_len == 0)
540 return (leader2_len == 0);
541
542 // If first leader has 'f' flag, the lines can be joined only if the
543 // second line does not have a leader.
544 // If first leader has 'e' flag, the lines can never be joined.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000545 // If first leader has 's' flag, the lines can only be joined if there is
Bram Moolenaar11abd092020-05-01 14:26:37 +0200546 // some text after it and the second line has the 'm' flag.
547 if (leader1_flags != NULL)
548 {
549 for (p = leader1_flags; *p && *p != ':'; ++p)
550 {
551 if (*p == COM_FIRST)
552 return (leader2_len == 0);
553 if (*p == COM_END)
554 return FALSE;
555 if (*p == COM_START)
556 {
557 if (*(ml_get(lnum) + leader1_len) == NUL)
558 return FALSE;
559 if (leader2_flags == NULL || leader2_len == 0)
560 return FALSE;
561 for (p = leader2_flags; *p && *p != ':'; ++p)
562 if (*p == COM_MIDDLE)
563 return TRUE;
564 return FALSE;
565 }
566 }
567 }
568
569 // Get current line and next line, compare the leaders.
570 // The first line has to be saved, only one line can be locked at a time.
571 line1 = vim_strsave(ml_get(lnum));
572 if (line1 != NULL)
573 {
574 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
575 ;
576 line2 = ml_get(lnum + 1);
577 for (idx2 = 0; idx2 < leader2_len; ++idx2)
578 {
579 if (!VIM_ISWHITE(line2[idx2]))
580 {
581 if (line1[idx1++] != line2[idx2])
582 break;
583 }
584 else
585 while (VIM_ISWHITE(line1[idx1]))
586 ++idx1;
587 }
588 vim_free(line1);
589 }
590 return (idx2 == leader2_len && idx1 == leader1_len);
591}
592
593/*
594 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
595 * previous line is in the same paragraph. Used for auto-formatting.
596 */
597 static int
598paragraph_start(linenr_T lnum)
599{
600 char_u *p;
601 int leader_len = 0; // leader len of current line
602 char_u *leader_flags = NULL; // flags for leader of current line
603 int next_leader_len; // leader len of next line
604 char_u *next_leader_flags; // flags for leader of next line
605 int do_comments; // format comments
606
607 if (lnum <= 1)
608 return TRUE; // start of the file
609
610 p = ml_get(lnum - 1);
611 if (*p == NUL)
612 return TRUE; // after empty line
613
614 do_comments = has_format_option(FO_Q_COMS);
615 if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments))
616 return TRUE; // after non-paragraph line
617
618 if (fmt_check_par(lnum, &next_leader_len, &next_leader_flags, do_comments))
619 return TRUE; // "lnum" is not a paragraph line
620
621 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
622 return TRUE; // missing trailing space in previous line.
623
624 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
625 return TRUE; // numbered item starts in "lnum".
626
627 if (!same_leader(lnum - 1, leader_len, leader_flags,
628 next_leader_len, next_leader_flags))
629 return TRUE; // change of comment leader.
630
631 return FALSE;
632}
633
634/*
635 * Called after inserting or deleting text: When 'formatoptions' includes the
636 * 'a' flag format from the current line until the end of the paragraph.
637 * Keep the cursor at the same position relative to the text.
638 * The caller must have saved the cursor line for undo, following ones will be
639 * saved here.
640 */
641 void
642auto_format(
643 int trailblank, // when TRUE also format with trailing blank
644 int prev_line) // may start in previous line
645{
646 pos_T pos;
647 colnr_T len;
648 char_u *old;
649 char_u *new, *pnew;
650 int wasatend;
651 int cc;
652
653 if (!has_format_option(FO_AUTO))
654 return;
655
656 pos = curwin->w_cursor;
657 old = ml_get_curline();
658
659 // may remove added space
660 check_auto_format(FALSE);
661
662 // Don't format in Insert mode when the cursor is on a trailing blank, the
663 // user might insert normal text next. Also skip formatting when "1" is
664 // in 'formatoptions' and there is a single character before the cursor.
665 // Otherwise the line would be broken and when typing another non-white
666 // next they are not joined back together.
667 wasatend = (pos.col == (colnr_T)STRLEN(old));
668 if (*old != NUL && !trailblank && wasatend)
669 {
670 dec_cursor();
671 cc = gchar_cursor();
672 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
673 && has_format_option(FO_ONE_LETTER))
674 dec_cursor();
675 cc = gchar_cursor();
676 if (WHITECHAR(cc))
677 {
678 curwin->w_cursor = pos;
679 return;
680 }
681 curwin->w_cursor = pos;
682 }
683
684 // With the 'c' flag in 'formatoptions' and 't' missing: only format
685 // comments.
686 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
687 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
688 return;
689
690 // May start formatting in a previous line, so that after "x" a word is
691 // moved to the previous line if it fits there now. Only when this is not
692 // the start of a paragraph.
693 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
694 {
695 --curwin->w_cursor.lnum;
696 if (u_save_cursor() == FAIL)
697 return;
698 }
699
700 // Do the formatting and restore the cursor position. "saved_cursor" will
701 // be adjusted for the text formatting.
702 saved_cursor = pos;
703 format_lines((linenr_T)-1, FALSE);
704 curwin->w_cursor = saved_cursor;
705 saved_cursor.lnum = 0;
706
707 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
708 {
709 // "cannot happen"
710 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
711 coladvance((colnr_T)MAXCOL);
712 }
713 else
714 check_cursor_col();
715
716 // Insert mode: If the cursor is now after the end of the line while it
717 // previously wasn't, the line was broken. Because of the rule above we
718 // need to add a space when 'w' is in 'formatoptions' to keep a paragraph
719 // formatted.
720 if (!wasatend && has_format_option(FO_WHITE_PAR))
721 {
722 new = ml_get_curline();
723 len = (colnr_T)STRLEN(new);
724 if (curwin->w_cursor.col == len)
725 {
726 pnew = vim_strnsave(new, len + 2);
727 pnew[len] = ' ';
728 pnew[len + 1] = NUL;
729 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
730 // remove the space later
731 did_add_space = TRUE;
732 }
733 else
734 // may remove added space
735 check_auto_format(FALSE);
736 }
737
738 check_cursor();
739}
740
741/*
742 * When an extra space was added to continue a paragraph for auto-formatting,
743 * delete it now. The space must be under the cursor, just after the insert
744 * position.
745 */
746 void
747check_auto_format(
748 int end_insert) // TRUE when ending Insert mode
749{
750 int c = ' ';
751 int cc;
752
753 if (did_add_space)
754 {
755 cc = gchar_cursor();
756 if (!WHITECHAR(cc))
757 // Somehow the space was removed already.
758 did_add_space = FALSE;
759 else
760 {
761 if (!end_insert)
762 {
763 inc_cursor();
764 c = gchar_cursor();
765 dec_cursor();
766 }
767 if (c != NUL)
768 {
769 // The space is no longer at the end of the line, delete it.
770 del_char(FALSE);
771 did_add_space = FALSE;
772 }
773 }
774 }
775}
776
777/*
778 * Find out textwidth to be used for formatting:
779 * if 'textwidth' option is set, use it
780 * else if 'wrapmargin' option is set, use curwin->w_width - 'wrapmargin'
781 * if invalid value, use 0.
782 * Set default to window width (maximum 79) for "gq" operator.
783 */
784 int
785comp_textwidth(
786 int ff) // force formatting (for "gq" command)
787{
788 int textwidth;
789
790 textwidth = curbuf->b_p_tw;
791 if (textwidth == 0 && curbuf->b_p_wm)
792 {
793 // The width is the window width minus 'wrapmargin' minus all the
794 // things that add to the margin.
795 textwidth = curwin->w_width - curbuf->b_p_wm;
796#ifdef FEAT_CMDWIN
797 if (cmdwin_type != 0)
798 textwidth -= 1;
799#endif
800#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
842 redraw_curbuf_later(INVERTED);
843
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;
872 }
873
874 if (oap->is_VIsual)
875 {
876 win_T *wp;
877
878 FOR_ALL_WINDOWS(wp)
879 {
880 if (wp->w_old_cursor_lnum != 0)
881 {
882 // When lines have been inserted or deleted, adjust the end of
883 // the Visual area to be redrawn.
884 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
885 wp->w_old_cursor_lnum += old_line_count;
886 else
887 wp->w_old_visual_lnum += old_line_count;
888 }
889 }
890 }
891}
892
893#if defined(FEAT_EVAL) || defined(PROTO)
894/*
895 * Implementation of the format operator 'gq' for when using 'formatexpr'.
896 */
897 void
898op_formatexpr(oparg_T *oap)
899{
900 if (oap->is_VIsual)
901 // When there is no change: need to remove the Visual selection
902 redraw_curbuf_later(INVERTED);
903
904 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
905 // As documented: when 'formatexpr' returns non-zero fall back to
906 // internal formatting.
907 op_format(oap, FALSE);
908}
909
910 int
911fex_format(
912 linenr_T lnum,
913 long count,
914 int c) // character to be inserted
915{
916 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
917 OPT_LOCAL);
918 int r;
919 char_u *fex;
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000920 sctx_T save_sctx = current_sctx;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200921
922 // Set v:lnum to the first line number and v:count to the number of lines.
923 // Set v:char to the character to be inserted (can be NUL).
924 set_vim_var_nr(VV_LNUM, lnum);
925 set_vim_var_nr(VV_COUNT, count);
926 set_vim_var_char(c);
927
928 // Make a copy, the option could be changed while calling it.
929 fex = vim_strsave(curbuf->b_p_fex);
930 if (fex == NULL)
931 return 0;
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000932 current_sctx = curbuf->b_p_script_ctx[BV_FEX];
Bram Moolenaar11abd092020-05-01 14:26:37 +0200933
934 // Evaluate the function.
935 if (use_sandbox)
936 ++sandbox;
937 r = (int)eval_to_number(fex);
938 if (use_sandbox)
939 --sandbox;
940
941 set_vim_var_string(VV_CHAR, NULL, -1);
942 vim_free(fex);
Bram Moolenaar3ba685e2022-01-22 19:17:31 +0000943 current_sctx = save_sctx;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200944
945 return r;
946}
947#endif
948
949/*
950 * Format "line_count" lines, starting at the cursor position.
951 * When "line_count" is negative, format until the end of the paragraph.
952 * Lines after the cursor line are saved for undo, caller must have saved the
953 * first line.
954 */
955 void
956format_lines(
957 linenr_T line_count,
958 int avoid_fex) // don't use 'formatexpr'
959{
960 int max_len;
961 int is_not_par; // current line not part of parag.
962 int next_is_not_par; // next line not part of paragraph
963 int is_end_par; // at end of paragraph
964 int prev_is_end_par = FALSE;// prev. line not part of parag.
965 int next_is_start_par = FALSE;
966 int leader_len = 0; // leader len of current line
967 int next_leader_len; // leader len of next line
968 char_u *leader_flags = NULL; // flags for leader of current line
Bram Moolenaar264d3dd2021-12-29 14:09:32 +0000969 char_u *next_leader_flags = NULL; // flags for leader of next line
Bram Moolenaar11abd092020-05-01 14:26:37 +0200970 int do_comments; // format comments
971 int do_comments_list = 0; // format comments with 'n' or '2'
972 int advance = TRUE;
973 int second_indent = -1; // indent for second line (comment
974 // aware)
975 int do_second_indent;
976 int do_number_indent;
977 int do_trail_white;
978 int first_par_line = TRUE;
979 int smd_save;
980 long count;
981 int need_set_indent = TRUE; // set indent of next paragraph
Bram Moolenaarecabb512021-12-06 19:51:01 +0000982 linenr_T first_line = curwin->w_cursor.lnum;
Bram Moolenaar11abd092020-05-01 14:26:37 +0200983 int force_format = FALSE;
984 int old_State = State;
985
986 // length of a line to force formatting: 3 * 'tw'
987 max_len = comp_textwidth(TRUE) * 3;
988
989 // check for 'q', '2' and '1' in 'formatoptions'
990 do_comments = has_format_option(FO_Q_COMS);
991 do_second_indent = has_format_option(FO_Q_SECOND);
992 do_number_indent = has_format_option(FO_Q_NUMBER);
993 do_trail_white = has_format_option(FO_WHITE_PAR);
994
995 // Get info about the previous and current line.
996 if (curwin->w_cursor.lnum > 1)
997 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
998 , &leader_len, &leader_flags, do_comments);
999 else
1000 is_not_par = TRUE;
1001 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
1002 , &next_leader_len, &next_leader_flags, do_comments);
1003 is_end_par = (is_not_par || next_is_not_par);
1004 if (!is_end_par && do_trail_white)
1005 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
1006
1007 curwin->w_cursor.lnum--;
1008 for (count = line_count; count != 0 && !got_int; --count)
1009 {
1010 // Advance to next paragraph.
1011 if (advance)
1012 {
1013 curwin->w_cursor.lnum++;
1014 prev_is_end_par = is_end_par;
1015 is_not_par = next_is_not_par;
1016 leader_len = next_leader_len;
1017 leader_flags = next_leader_flags;
1018 }
1019
1020 // The last line to be formatted.
1021 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
1022 {
1023 next_is_not_par = TRUE;
1024 next_leader_len = 0;
1025 next_leader_flags = NULL;
1026 }
1027 else
1028 {
1029 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
1030 , &next_leader_len, &next_leader_flags, do_comments);
1031 if (do_number_indent)
1032 next_is_start_par =
1033 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
1034 }
1035 advance = TRUE;
1036 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
1037 if (!is_end_par && do_trail_white)
1038 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
1039
1040 // Skip lines that are not in a paragraph.
1041 if (is_not_par)
1042 {
1043 if (line_count < 0)
1044 break;
1045 }
1046 else
1047 {
1048 // For the first line of a paragraph, check indent of second line.
1049 // Don't do this for comments and empty lines.
1050 if (first_par_line
1051 && (do_second_indent || do_number_indent)
1052 && prev_is_end_par
1053 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1054 {
1055 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
1056 {
1057 if (leader_len == 0 && next_leader_len == 0)
1058 {
1059 // no comment found
1060 second_indent =
1061 get_indent_lnum(curwin->w_cursor.lnum + 1);
1062 }
1063 else
1064 {
1065 second_indent = next_leader_len;
1066 do_comments_list = 1;
1067 }
1068 }
1069 else if (do_number_indent)
1070 {
1071 if (leader_len == 0 && next_leader_len == 0)
1072 {
1073 // no comment found
1074 second_indent =
1075 get_number_indent(curwin->w_cursor.lnum);
1076 }
1077 else
1078 {
1079 // get_number_indent() is now "comment aware"...
1080 second_indent =
1081 get_number_indent(curwin->w_cursor.lnum);
1082 do_comments_list = 1;
1083 }
1084 }
1085 }
1086
1087 // When the comment leader changes, it's the end of the paragraph.
1088 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
1089 || !same_leader(curwin->w_cursor.lnum,
1090 leader_len, leader_flags,
1091 next_leader_len, next_leader_flags))
Bram Moolenaar264d3dd2021-12-29 14:09:32 +00001092 {
1093 // Special case: If the next line starts with a line comment
1094 // and this line has a line comment after some text, the
1095 // paragraph doesn't really end.
1096 if (next_leader_flags == NULL
1097 || STRNCMP(next_leader_flags, "://", 3) != 0
1098 || check_linecomment(ml_get_curline()) == MAXCOL)
Bram Moolenaar11abd092020-05-01 14:26:37 +02001099 is_end_par = TRUE;
Bram Moolenaar264d3dd2021-12-29 14:09:32 +00001100 }
Bram Moolenaar11abd092020-05-01 14:26:37 +02001101
1102 // If we have got to the end of a paragraph, or the line is
1103 // getting long, format it.
1104 if (is_end_par || force_format)
1105 {
1106 if (need_set_indent)
Christian Brabandt818ff252021-11-18 13:56:37 +00001107 {
1108 int indent = 0; // amount of indent needed
1109
Bram Moolenaarecabb512021-12-06 19:51:01 +00001110 // Replace indent in first line of a paragraph with minimal
1111 // number of tabs and spaces, according to current options.
1112 // For the very first formatted line keep the current
1113 // indent.
1114 if (curwin->w_cursor.lnum == first_line)
1115 indent = get_indent();
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001116 else if (curbuf->b_p_lisp)
Christian Brabandt818ff252021-11-18 13:56:37 +00001117 indent = get_lisp_indent();
1118 else
Christian Brabandt818ff252021-11-18 13:56:37 +00001119 {
Christian Brabandt818ff252021-11-18 13:56:37 +00001120 if (cindent_on())
1121 {
1122 indent =
1123# ifdef FEAT_EVAL
1124 *curbuf->b_p_inde != NUL ? get_expr_indent() :
1125# endif
1126 get_c_indent();
1127 }
1128 else
Christian Brabandt818ff252021-11-18 13:56:37 +00001129 indent = get_indent();
1130 }
1131 (void)set_indent(indent, SIN_CHANGED);
1132 }
Bram Moolenaar11abd092020-05-01 14:26:37 +02001133
1134 // put cursor on last non-space
Bram Moolenaar24959102022-05-07 20:01:16 +01001135 State = MODE_NORMAL; // don't go past end-of-line
Bram Moolenaar11abd092020-05-01 14:26:37 +02001136 coladvance((colnr_T)MAXCOL);
1137 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
1138 dec_cursor();
1139
1140 // do the formatting, without 'showmode'
Bram Moolenaar24959102022-05-07 20:01:16 +01001141 State = MODE_INSERT; // for open_line()
Bram Moolenaar11abd092020-05-01 14:26:37 +02001142 smd_save = p_smd;
1143 p_smd = FALSE;
1144 insertchar(NUL, INSCHAR_FORMAT
1145 + (do_comments ? INSCHAR_DO_COM : 0)
1146 + (do_comments && do_comments_list
1147 ? INSCHAR_COM_LIST : 0)
1148 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
1149 State = old_State;
1150 p_smd = smd_save;
1151 second_indent = -1;
1152 // at end of par.: need to set indent of next par.
1153 need_set_indent = is_end_par;
1154 if (is_end_par)
1155 {
1156 // When called with a negative line count, break at the
1157 // end of the paragraph.
1158 if (line_count < 0)
1159 break;
1160 first_par_line = TRUE;
1161 }
1162 force_format = FALSE;
1163 }
1164
1165 // When still in same paragraph, join the lines together. But
1166 // first delete the leader from the second line.
1167 if (!is_end_par)
1168 {
1169 advance = FALSE;
1170 curwin->w_cursor.lnum++;
1171 curwin->w_cursor.col = 0;
1172 if (line_count < 0 && u_save_cursor() == FAIL)
1173 break;
1174 if (next_leader_len > 0)
1175 {
1176 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
1177 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
1178 (long)-next_leader_len, 0);
1179 }
1180 else if (second_indent > 0) // the "leader" for FO_Q_SECOND
1181 {
1182 int indent = getwhitecols_curline();
1183
1184 if (indent > 0)
1185 {
1186 (void)del_bytes(indent, FALSE, FALSE);
1187 mark_col_adjust(curwin->w_cursor.lnum,
1188 (colnr_T)0, 0L, (long)-indent, 0);
1189 }
1190 }
1191 curwin->w_cursor.lnum--;
1192 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
1193 {
1194 beep_flush();
1195 break;
1196 }
1197 first_par_line = FALSE;
1198 // If the line is getting long, format it next time
1199 if (STRLEN(ml_get_curline()) > (size_t)max_len)
1200 force_format = TRUE;
1201 else
1202 force_format = FALSE;
1203 }
1204 }
1205 line_breakcheck();
1206 }
1207}