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