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