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