Bram Moolenaar | 4b47162 | 2019-01-31 13:48:09 +0100 | [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 | * indent.c: Indentation related functions |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 16 | #if defined(FEAT_VARTABS) || defined(PROTO) |
| 17 | |
| 18 | /* |
| 19 | * Set the integer values corresponding to the string setting of 'vartabstop'. |
| 20 | * "array" will be set, caller must free it if needed. |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 21 | * Return FAIL for an error. |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 22 | */ |
| 23 | int |
| 24 | tabstop_set(char_u *var, int **array) |
| 25 | { |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 26 | int valcount = 1; |
| 27 | int t; |
| 28 | char_u *cp; |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 29 | |
| 30 | if (var[0] == NUL || (var[0] == '0' && var[1] == NUL)) |
| 31 | { |
| 32 | *array = NULL; |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 33 | return OK; |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | for (cp = var; *cp != NUL; ++cp) |
| 37 | { |
| 38 | if (cp == var || cp[-1] == ',') |
| 39 | { |
| 40 | char_u *end; |
| 41 | |
| 42 | if (strtol((char *)cp, (char **)&end, 10) <= 0) |
| 43 | { |
| 44 | if (cp != end) |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 45 | emsg(_(e_argument_must_be_positive)); |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 46 | else |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 47 | semsg(_(e_invalid_argument_str), cp); |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 48 | return FAIL; |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | if (VIM_ISDIGIT(*cp)) |
| 53 | continue; |
| 54 | if (cp[0] == ',' && cp > var && cp[-1] != ',' && cp[1] != NUL) |
| 55 | { |
| 56 | ++valcount; |
| 57 | continue; |
| 58 | } |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 59 | semsg(_(e_invalid_argument_str), var); |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 60 | return FAIL; |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | *array = ALLOC_MULT(int, valcount + 1); |
| 64 | if (*array == NULL) |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 65 | return FAIL; |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 66 | (*array)[0] = valcount; |
| 67 | |
| 68 | t = 1; |
| 69 | for (cp = var; *cp != NUL;) |
| 70 | { |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 71 | int n = atoi((char *)cp); |
| 72 | |
Bram Moolenaar | 2ddb89f | 2021-09-04 21:20:41 +0200 | [diff] [blame] | 73 | // Catch negative values, overflow and ridiculous big values. |
Bram Moolenaar | fc88df4 | 2022-02-05 11:13:05 +0000 | [diff] [blame] | 74 | if (n <= 0 || n > TABSTOP_MAX) |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 75 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 76 | semsg(_(e_invalid_argument_str), cp); |
Yegappan Lakshmanan | 960dcbd | 2023-03-07 17:45:11 +0000 | [diff] [blame] | 77 | VIM_CLEAR(*array); |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 78 | return FAIL; |
| 79 | } |
| 80 | (*array)[t++] = n; |
| 81 | while (*cp != NUL && *cp != ',') |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 82 | ++cp; |
| 83 | if (*cp != NUL) |
| 84 | ++cp; |
| 85 | } |
| 86 | |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 87 | return OK; |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Calculate the number of screen spaces a tab will occupy. |
| 92 | * If "vts" is set then the tab widths are taken from that array, |
| 93 | * otherwise the value of ts is used. |
| 94 | */ |
| 95 | int |
| 96 | tabstop_padding(colnr_T col, int ts_arg, int *vts) |
| 97 | { |
| 98 | int ts = ts_arg == 0 ? 8 : ts_arg; |
| 99 | int tabcount; |
| 100 | colnr_T tabcol = 0; |
| 101 | int t; |
| 102 | int padding = 0; |
| 103 | |
| 104 | if (vts == NULL || vts[0] == 0) |
| 105 | return ts - (col % ts); |
| 106 | |
| 107 | tabcount = vts[0]; |
| 108 | |
| 109 | for (t = 1; t <= tabcount; ++t) |
| 110 | { |
| 111 | tabcol += vts[t]; |
| 112 | if (tabcol > col) |
| 113 | { |
| 114 | padding = (int)(tabcol - col); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | if (t > tabcount) |
| 119 | padding = vts[tabcount] - (int)((col - tabcol) % vts[tabcount]); |
| 120 | |
| 121 | return padding; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Find the size of the tab that covers a particular column. |
| 126 | */ |
| 127 | int |
| 128 | tabstop_at(colnr_T col, int ts, int *vts) |
| 129 | { |
| 130 | int tabcount; |
| 131 | colnr_T tabcol = 0; |
| 132 | int t; |
| 133 | int tab_size = 0; |
| 134 | |
| 135 | if (vts == 0 || vts[0] == 0) |
| 136 | return ts; |
| 137 | |
| 138 | tabcount = vts[0]; |
| 139 | for (t = 1; t <= tabcount; ++t) |
| 140 | { |
| 141 | tabcol += vts[t]; |
| 142 | if (tabcol > col) |
| 143 | { |
| 144 | tab_size = vts[t]; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | if (t > tabcount) |
| 149 | tab_size = vts[tabcount]; |
| 150 | |
| 151 | return tab_size; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Find the column on which a tab starts. |
| 156 | */ |
| 157 | colnr_T |
| 158 | tabstop_start(colnr_T col, int ts, int *vts) |
| 159 | { |
| 160 | int tabcount; |
| 161 | colnr_T tabcol = 0; |
| 162 | int t; |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 163 | int excess; |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 164 | |
| 165 | if (vts == NULL || vts[0] == 0) |
| 166 | return (col / ts) * ts; |
| 167 | |
| 168 | tabcount = vts[0]; |
| 169 | for (t = 1; t <= tabcount; ++t) |
| 170 | { |
| 171 | tabcol += vts[t]; |
| 172 | if (tabcol > col) |
| 173 | return tabcol - vts[t]; |
| 174 | } |
| 175 | |
| 176 | excess = tabcol % vts[tabcount]; |
| 177 | return excess + ((col - excess) / vts[tabcount]) * vts[tabcount]; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Find the number of tabs and spaces necessary to get from one column |
| 182 | * to another. |
| 183 | */ |
| 184 | void |
| 185 | tabstop_fromto( |
| 186 | colnr_T start_col, |
| 187 | colnr_T end_col, |
| 188 | int ts_arg, |
| 189 | int *vts, |
| 190 | int *ntabs, |
| 191 | int *nspcs) |
| 192 | { |
| 193 | int spaces = end_col - start_col; |
| 194 | colnr_T tabcol = 0; |
| 195 | int padding = 0; |
| 196 | int tabcount; |
| 197 | int t; |
| 198 | int ts = ts_arg == 0 ? curbuf->b_p_ts : ts_arg; |
| 199 | |
| 200 | if (vts == NULL || vts[0] == 0) |
| 201 | { |
| 202 | int tabs = 0; |
| 203 | int initspc = 0; |
| 204 | |
| 205 | initspc = ts - (start_col % ts); |
| 206 | if (spaces >= initspc) |
| 207 | { |
| 208 | spaces -= initspc; |
| 209 | tabs++; |
| 210 | } |
| 211 | tabs += spaces / ts; |
| 212 | spaces -= (spaces / ts) * ts; |
| 213 | |
| 214 | *ntabs = tabs; |
| 215 | *nspcs = spaces; |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | // Find the padding needed to reach the next tabstop. |
| 220 | tabcount = vts[0]; |
| 221 | for (t = 1; t <= tabcount; ++t) |
| 222 | { |
| 223 | tabcol += vts[t]; |
| 224 | if (tabcol > start_col) |
| 225 | { |
| 226 | padding = (int)(tabcol - start_col); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | if (t > tabcount) |
| 231 | padding = vts[tabcount] - (int)((start_col - tabcol) % vts[tabcount]); |
| 232 | |
| 233 | // If the space needed is less than the padding no tabs can be used. |
| 234 | if (spaces < padding) |
| 235 | { |
| 236 | *ntabs = 0; |
| 237 | *nspcs = spaces; |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | *ntabs = 1; |
| 242 | spaces -= padding; |
| 243 | |
| 244 | // At least one tab has been used. See if any more will fit. |
| 245 | while (spaces != 0 && ++t <= tabcount) |
| 246 | { |
| 247 | padding = vts[t]; |
| 248 | if (spaces < padding) |
| 249 | { |
| 250 | *nspcs = spaces; |
| 251 | return; |
| 252 | } |
| 253 | ++*ntabs; |
| 254 | spaces -= padding; |
| 255 | } |
| 256 | |
| 257 | *ntabs += spaces / vts[tabcount]; |
| 258 | *nspcs = spaces % vts[tabcount]; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * See if two tabstop arrays contain the same values. |
| 263 | */ |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 264 | static int |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 265 | tabstop_eq(int *ts1, int *ts2) |
| 266 | { |
| 267 | int t; |
| 268 | |
| 269 | if ((ts1 == 0 && ts2) || (ts1 && ts2 == 0)) |
| 270 | return FALSE; |
| 271 | if (ts1 == ts2) |
| 272 | return TRUE; |
| 273 | if (ts1[0] != ts2[0]) |
| 274 | return FALSE; |
| 275 | |
| 276 | for (t = 1; t <= ts1[0]; ++t) |
| 277 | if (ts1[t] != ts2[t]) |
| 278 | return FALSE; |
| 279 | |
| 280 | return TRUE; |
| 281 | } |
| 282 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 283 | # if defined(FEAT_BEVAL) || defined(PROTO) |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 284 | /* |
| 285 | * Copy a tabstop array, allocating space for the new array. |
| 286 | */ |
| 287 | int * |
| 288 | tabstop_copy(int *oldts) |
| 289 | { |
| 290 | int *newts; |
| 291 | int t; |
| 292 | |
| 293 | if (oldts == NULL) |
| 294 | return NULL; |
| 295 | newts = ALLOC_MULT(int, oldts[0] + 1); |
| 296 | if (newts != NULL) |
| 297 | for (t = 0; t <= oldts[0]; ++t) |
| 298 | newts[t] = oldts[t]; |
| 299 | return newts; |
| 300 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 301 | # endif |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * Return a count of the number of tabstops. |
| 305 | */ |
| 306 | int |
| 307 | tabstop_count(int *ts) |
| 308 | { |
| 309 | return ts != NULL ? ts[0] : 0; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Return the first tabstop, or 8 if there are no tabstops defined. |
| 314 | */ |
| 315 | int |
| 316 | tabstop_first(int *ts) |
| 317 | { |
| 318 | return ts != NULL ? ts[1] : 8; |
| 319 | } |
| 320 | |
| 321 | #endif |
| 322 | |
| 323 | /* |
| 324 | * Return the effective shiftwidth value for current buffer, using the |
| 325 | * 'tabstop' value when 'shiftwidth' is zero. |
| 326 | */ |
| 327 | long |
| 328 | get_sw_value(buf_T *buf) |
| 329 | { |
| 330 | return get_sw_value_col(buf, 0); |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Idem, using "pos". |
| 335 | */ |
| 336 | static long |
| 337 | get_sw_value_pos(buf_T *buf, pos_T *pos) |
| 338 | { |
| 339 | pos_T save_cursor = curwin->w_cursor; |
| 340 | long sw_value; |
| 341 | |
| 342 | curwin->w_cursor = *pos; |
| 343 | sw_value = get_sw_value_col(buf, get_nolist_virtcol()); |
| 344 | curwin->w_cursor = save_cursor; |
| 345 | return sw_value; |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Idem, using the first non-black in the current line. |
| 350 | */ |
| 351 | long |
| 352 | get_sw_value_indent(buf_T *buf) |
| 353 | { |
| 354 | pos_T pos = curwin->w_cursor; |
| 355 | |
| 356 | pos.col = getwhitecols_curline(); |
| 357 | return get_sw_value_pos(buf, &pos); |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Idem, using virtual column "col". |
| 362 | */ |
| 363 | long |
| 364 | get_sw_value_col(buf_T *buf, colnr_T col UNUSED) |
| 365 | { |
| 366 | return buf->b_p_sw ? buf->b_p_sw : |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 367 | #ifdef FEAT_VARTABS |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 368 | tabstop_at(col, buf->b_p_ts, buf->b_p_vts_array); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 369 | #else |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 370 | buf->b_p_ts; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 371 | #endif |
Bram Moolenaar | e677df8 | 2019-09-02 22:31:11 +0200 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /* |
| 375 | * Return the effective softtabstop value for the current buffer, using the |
| 376 | * 'shiftwidth' value when 'softtabstop' is negative. |
| 377 | */ |
| 378 | long |
| 379 | get_sts_value(void) |
| 380 | { |
| 381 | return curbuf->b_p_sts < 0 ? get_sw_value(curbuf) : curbuf->b_p_sts; |
| 382 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 383 | |
| 384 | /* |
| 385 | * Count the size (in window cells) of the indent in the current line. |
| 386 | */ |
| 387 | int |
| 388 | get_indent(void) |
| 389 | { |
| 390 | #ifdef FEAT_VARTABS |
| 391 | return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts, |
| 392 | curbuf->b_p_vts_array, FALSE); |
| 393 | #else |
| 394 | return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE); |
| 395 | #endif |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Count the size (in window cells) of the indent in line "lnum". |
| 400 | */ |
| 401 | int |
| 402 | get_indent_lnum(linenr_T lnum) |
| 403 | { |
| 404 | #ifdef FEAT_VARTABS |
| 405 | return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts, |
| 406 | curbuf->b_p_vts_array, FALSE); |
| 407 | #else |
| 408 | return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE); |
| 409 | #endif |
| 410 | } |
| 411 | |
| 412 | #if defined(FEAT_FOLDING) || defined(PROTO) |
| 413 | /* |
| 414 | * Count the size (in window cells) of the indent in line "lnum" of buffer |
| 415 | * "buf". |
| 416 | */ |
| 417 | int |
| 418 | get_indent_buf(buf_T *buf, linenr_T lnum) |
| 419 | { |
| 420 | # ifdef FEAT_VARTABS |
| 421 | return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE), |
zeertzjq | 07146ad | 2022-12-19 15:51:44 +0000 | [diff] [blame] | 422 | (int)buf->b_p_ts, buf->b_p_vts_array, FALSE); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 423 | # else |
| 424 | return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE); |
| 425 | # endif |
| 426 | } |
| 427 | #endif |
| 428 | |
| 429 | /* |
| 430 | * count the size (in window cells) of the indent in line "ptr", with |
| 431 | * 'tabstop' at "ts" |
| 432 | */ |
| 433 | int |
| 434 | get_indent_str( |
| 435 | char_u *ptr, |
| 436 | int ts, |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 437 | int no_ts) // if TRUE, count a tab as ^I |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 438 | { |
| 439 | int count = 0; |
| 440 | |
| 441 | for ( ; *ptr; ++ptr) |
| 442 | { |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 443 | if (*ptr == TAB) // count a tab for what it is worth |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 444 | { |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 445 | if (!no_ts) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 446 | count += ts - (count % ts); |
| 447 | else |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 448 | count += ptr2cells(ptr); |
| 449 | } |
| 450 | else if (*ptr == ' ') |
| 451 | ++count; // count a space for one |
| 452 | else |
| 453 | break; |
| 454 | } |
| 455 | return count; |
| 456 | } |
| 457 | |
| 458 | #ifdef FEAT_VARTABS |
| 459 | /* |
| 460 | * Count the size (in window cells) of the indent in line "ptr", using |
| 461 | * variable tabstops. |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 462 | * If "no_ts" is TRUE, count a tab as ^I. |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 463 | */ |
| 464 | int |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 465 | get_indent_str_vtab(char_u *ptr, int ts, int *vts, int no_ts) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 466 | { |
| 467 | int count = 0; |
| 468 | |
| 469 | for ( ; *ptr; ++ptr) |
| 470 | { |
| 471 | if (*ptr == TAB) // count a tab for what it is worth |
| 472 | { |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 473 | if (!no_ts) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 474 | count += tabstop_padding(count, ts, vts); |
| 475 | else |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 476 | count += ptr2cells(ptr); |
| 477 | } |
| 478 | else if (*ptr == ' ') |
| 479 | ++count; // count a space for one |
| 480 | else |
| 481 | break; |
| 482 | } |
| 483 | return count; |
| 484 | } |
| 485 | #endif |
| 486 | |
| 487 | /* |
| 488 | * Set the indent of the current line. |
| 489 | * Leaves the cursor on the first non-blank in the line. |
| 490 | * Caller must take care of undo. |
| 491 | * "flags": |
| 492 | * SIN_CHANGED: call changed_bytes() if the line was changed. |
| 493 | * SIN_INSERT: insert the indent in front of the line. |
| 494 | * SIN_UNDO: save line for undo before changing it. |
| 495 | * Returns TRUE if the line was changed. |
| 496 | */ |
| 497 | int |
| 498 | set_indent( |
| 499 | int size, // measured in spaces |
| 500 | int flags) |
| 501 | { |
| 502 | char_u *p; |
| 503 | char_u *newline; |
| 504 | char_u *oldline; |
| 505 | char_u *s; |
| 506 | int todo; |
| 507 | int ind_len; // measured in characters |
| 508 | int line_len; |
| 509 | int doit = FALSE; |
| 510 | int ind_done = 0; // measured in spaces |
| 511 | #ifdef FEAT_VARTABS |
| 512 | int ind_col = 0; |
| 513 | #endif |
| 514 | int tab_pad; |
| 515 | int retval = FALSE; |
| 516 | int orig_char_len = -1; // number of initial whitespace chars when |
| 517 | // 'et' and 'pi' are both set |
| 518 | |
| 519 | // First check if there is anything to do and compute the number of |
| 520 | // characters needed for the indent. |
| 521 | todo = size; |
| 522 | ind_len = 0; |
| 523 | p = oldline = ml_get_curline(); |
| 524 | |
| 525 | // Calculate the buffer size for the new indent, and check to see if it |
| 526 | // isn't already set |
| 527 | |
| 528 | // if 'expandtab' isn't set: use TABs; if both 'expandtab' and |
| 529 | // 'preserveindent' are set count the number of characters at the |
| 530 | // beginning of the line to be copied |
| 531 | if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi)) |
| 532 | { |
| 533 | // If 'preserveindent' is set then reuse as much as possible of |
| 534 | // the existing indent structure for the new indent |
| 535 | if (!(flags & SIN_INSERT) && curbuf->b_p_pi) |
| 536 | { |
| 537 | ind_done = 0; |
| 538 | |
| 539 | // count as many characters as we can use |
| 540 | while (todo > 0 && VIM_ISWHITE(*p)) |
| 541 | { |
| 542 | if (*p == TAB) |
| 543 | { |
| 544 | #ifdef FEAT_VARTABS |
| 545 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 546 | curbuf->b_p_vts_array); |
| 547 | #else |
| 548 | tab_pad = (int)curbuf->b_p_ts |
| 549 | - (ind_done % (int)curbuf->b_p_ts); |
| 550 | #endif |
| 551 | // stop if this tab will overshoot the target |
| 552 | if (todo < tab_pad) |
| 553 | break; |
| 554 | todo -= tab_pad; |
| 555 | ++ind_len; |
| 556 | ind_done += tab_pad; |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | --todo; |
| 561 | ++ind_len; |
| 562 | ++ind_done; |
| 563 | } |
| 564 | ++p; |
| 565 | } |
| 566 | |
| 567 | #ifdef FEAT_VARTABS |
| 568 | // These diverge from this point. |
| 569 | ind_col = ind_done; |
| 570 | #endif |
| 571 | // Set initial number of whitespace chars to copy if we are |
| 572 | // preserving indent but expandtab is set |
| 573 | if (curbuf->b_p_et) |
| 574 | orig_char_len = ind_len; |
| 575 | |
| 576 | // Fill to next tabstop with a tab, if possible |
| 577 | #ifdef FEAT_VARTABS |
| 578 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 579 | curbuf->b_p_vts_array); |
| 580 | #else |
| 581 | tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
| 582 | #endif |
| 583 | if (todo >= tab_pad && orig_char_len == -1) |
| 584 | { |
| 585 | doit = TRUE; |
| 586 | todo -= tab_pad; |
| 587 | ++ind_len; |
| 588 | // ind_done += tab_pad; |
| 589 | #ifdef FEAT_VARTABS |
| 590 | ind_col += tab_pad; |
| 591 | #endif |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | // count tabs required for indent |
| 596 | #ifdef FEAT_VARTABS |
| 597 | for (;;) |
| 598 | { |
| 599 | tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts, |
| 600 | curbuf->b_p_vts_array); |
| 601 | if (todo < tab_pad) |
| 602 | break; |
| 603 | if (*p != TAB) |
| 604 | doit = TRUE; |
| 605 | else |
| 606 | ++p; |
| 607 | todo -= tab_pad; |
| 608 | ++ind_len; |
| 609 | ind_col += tab_pad; |
| 610 | } |
| 611 | #else |
| 612 | while (todo >= (int)curbuf->b_p_ts) |
| 613 | { |
| 614 | if (*p != TAB) |
| 615 | doit = TRUE; |
| 616 | else |
| 617 | ++p; |
| 618 | todo -= (int)curbuf->b_p_ts; |
| 619 | ++ind_len; |
| 620 | // ind_done += (int)curbuf->b_p_ts; |
| 621 | } |
| 622 | #endif |
| 623 | } |
| 624 | // count spaces required for indent |
| 625 | while (todo > 0) |
| 626 | { |
| 627 | if (*p != ' ') |
| 628 | doit = TRUE; |
| 629 | else |
| 630 | ++p; |
| 631 | --todo; |
| 632 | ++ind_len; |
| 633 | // ++ind_done; |
| 634 | } |
| 635 | |
| 636 | // Return if the indent is OK already. |
| 637 | if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT)) |
| 638 | return FALSE; |
| 639 | |
| 640 | // Allocate memory for the new line. |
| 641 | if (flags & SIN_INSERT) |
| 642 | p = oldline; |
| 643 | else |
| 644 | p = skipwhite(p); |
| 645 | line_len = (int)STRLEN(p) + 1; |
| 646 | |
| 647 | // If 'preserveindent' and 'expandtab' are both set keep the original |
| 648 | // characters and allocate accordingly. We will fill the rest with spaces |
| 649 | // after the if (!curbuf->b_p_et) below. |
| 650 | if (orig_char_len != -1) |
| 651 | { |
| 652 | newline = alloc(orig_char_len + size - ind_done + line_len); |
| 653 | if (newline == NULL) |
| 654 | return FALSE; |
| 655 | todo = size - ind_done; |
| 656 | ind_len = orig_char_len + todo; // Set total length of indent in |
| 657 | // characters, which may have been |
| 658 | // undercounted until now |
| 659 | p = oldline; |
| 660 | s = newline; |
| 661 | while (orig_char_len > 0) |
| 662 | { |
| 663 | *s++ = *p++; |
| 664 | orig_char_len--; |
| 665 | } |
| 666 | |
| 667 | // Skip over any additional white space (useful when newindent is less |
| 668 | // than old) |
| 669 | while (VIM_ISWHITE(*p)) |
| 670 | ++p; |
| 671 | |
| 672 | } |
| 673 | else |
| 674 | { |
| 675 | todo = size; |
| 676 | newline = alloc(ind_len + line_len); |
| 677 | if (newline == NULL) |
| 678 | return FALSE; |
| 679 | s = newline; |
| 680 | } |
| 681 | |
| 682 | // Put the characters in the new line. |
| 683 | // if 'expandtab' isn't set: use TABs |
| 684 | if (!curbuf->b_p_et) |
| 685 | { |
| 686 | // If 'preserveindent' is set then reuse as much as possible of |
| 687 | // the existing indent structure for the new indent |
| 688 | if (!(flags & SIN_INSERT) && curbuf->b_p_pi) |
| 689 | { |
| 690 | p = oldline; |
| 691 | ind_done = 0; |
| 692 | |
| 693 | while (todo > 0 && VIM_ISWHITE(*p)) |
| 694 | { |
| 695 | if (*p == TAB) |
| 696 | { |
| 697 | #ifdef FEAT_VARTABS |
| 698 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 699 | curbuf->b_p_vts_array); |
| 700 | #else |
| 701 | tab_pad = (int)curbuf->b_p_ts |
| 702 | - (ind_done % (int)curbuf->b_p_ts); |
| 703 | #endif |
| 704 | // stop if this tab will overshoot the target |
| 705 | if (todo < tab_pad) |
| 706 | break; |
| 707 | todo -= tab_pad; |
| 708 | ind_done += tab_pad; |
| 709 | } |
| 710 | else |
| 711 | { |
| 712 | --todo; |
| 713 | ++ind_done; |
| 714 | } |
| 715 | *s++ = *p++; |
| 716 | } |
| 717 | |
| 718 | // Fill to next tabstop with a tab, if possible |
| 719 | #ifdef FEAT_VARTABS |
| 720 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 721 | curbuf->b_p_vts_array); |
| 722 | #else |
| 723 | tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
| 724 | #endif |
| 725 | if (todo >= tab_pad) |
| 726 | { |
| 727 | *s++ = TAB; |
| 728 | todo -= tab_pad; |
| 729 | #ifdef FEAT_VARTABS |
| 730 | ind_done += tab_pad; |
| 731 | #endif |
| 732 | } |
| 733 | |
| 734 | p = skipwhite(p); |
| 735 | } |
| 736 | |
| 737 | #ifdef FEAT_VARTABS |
| 738 | for (;;) |
| 739 | { |
| 740 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 741 | curbuf->b_p_vts_array); |
| 742 | if (todo < tab_pad) |
| 743 | break; |
| 744 | *s++ = TAB; |
| 745 | todo -= tab_pad; |
| 746 | ind_done += tab_pad; |
| 747 | } |
| 748 | #else |
| 749 | while (todo >= (int)curbuf->b_p_ts) |
| 750 | { |
| 751 | *s++ = TAB; |
| 752 | todo -= (int)curbuf->b_p_ts; |
| 753 | } |
| 754 | #endif |
| 755 | } |
| 756 | while (todo > 0) |
| 757 | { |
| 758 | *s++ = ' '; |
| 759 | --todo; |
| 760 | } |
| 761 | mch_memmove(s, p, (size_t)line_len); |
| 762 | |
| 763 | // Replace the line (unless undo fails). |
| 764 | if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK) |
| 765 | { |
Bram Moolenaar | cf30643 | 2020-06-29 20:40:37 +0200 | [diff] [blame] | 766 | colnr_T old_offset = (colnr_T)(p - oldline); |
| 767 | colnr_T new_offset = (colnr_T)(s - newline); |
| 768 | |
| 769 | // this may free "newline" |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 770 | ml_replace(curwin->w_cursor.lnum, newline, FALSE); |
| 771 | if (flags & SIN_CHANGED) |
| 772 | changed_bytes(curwin->w_cursor.lnum, 0); |
| 773 | |
| 774 | // Correct saved cursor position if it is in this line. |
| 775 | if (saved_cursor.lnum == curwin->w_cursor.lnum) |
| 776 | { |
Bram Moolenaar | cf30643 | 2020-06-29 20:40:37 +0200 | [diff] [blame] | 777 | if (saved_cursor.col >= old_offset) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 778 | // cursor was after the indent, adjust for the number of |
| 779 | // bytes added/removed |
Bram Moolenaar | cf30643 | 2020-06-29 20:40:37 +0200 | [diff] [blame] | 780 | saved_cursor.col += ind_len - old_offset; |
| 781 | else if (saved_cursor.col >= new_offset) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 782 | // cursor was in the indent, and is now after it, put it back |
| 783 | // at the start of the indent (replacing spaces with TAB) |
Bram Moolenaar | cf30643 | 2020-06-29 20:40:37 +0200 | [diff] [blame] | 784 | saved_cursor.col = new_offset; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 785 | } |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 786 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 787 | { |
Bram Moolenaar | cf30643 | 2020-06-29 20:40:37 +0200 | [diff] [blame] | 788 | int added = ind_len - old_offset; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 789 | |
| 790 | // When increasing indent this behaves like spaces were inserted at |
| 791 | // the old indent, when decreasing indent it behaves like spaces |
| 792 | // were deleted at the new indent. |
| 793 | adjust_prop_columns(curwin->w_cursor.lnum, |
Bram Moolenaar | 8a77d20 | 2022-08-15 16:29:37 +0100 | [diff] [blame] | 794 | added > 0 ? old_offset : (colnr_T)ind_len, |
| 795 | added, APC_INDENT); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 796 | } |
| 797 | #endif |
| 798 | retval = TRUE; |
| 799 | } |
| 800 | else |
| 801 | vim_free(newline); |
| 802 | |
| 803 | curwin->w_cursor.col = ind_len; |
| 804 | return retval; |
| 805 | } |
| 806 | |
| 807 | /* |
| 808 | * Return the indent of the current line after a number. Return -1 if no |
| 809 | * number was found. Used for 'n' in 'formatoptions': numbered list. |
| 810 | * Since a pattern is used it can actually handle more than numbers. |
| 811 | */ |
| 812 | int |
| 813 | get_number_indent(linenr_T lnum) |
| 814 | { |
| 815 | colnr_T col; |
| 816 | pos_T pos; |
| 817 | |
| 818 | regmatch_T regmatch; |
| 819 | int lead_len = 0; // length of comment leader |
| 820 | |
| 821 | if (lnum > curbuf->b_ml.ml_line_count) |
| 822 | return -1; |
| 823 | pos.lnum = 0; |
| 824 | |
| 825 | // In format_lines() (i.e. not insert mode), fo+=q is needed too... |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 826 | if ((State & MODE_INSERT) || has_format_option(FO_Q_COMS)) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 827 | lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE); |
| 828 | |
| 829 | regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); |
| 830 | if (regmatch.regprog != NULL) |
| 831 | { |
| 832 | regmatch.rm_ic = FALSE; |
| 833 | |
| 834 | // vim_regexec() expects a pointer to a line. This lets us |
| 835 | // start matching for the flp beyond any comment leader... |
| 836 | if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0)) |
| 837 | { |
| 838 | pos.lnum = lnum; |
| 839 | pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum)); |
| 840 | pos.coladd = 0; |
| 841 | } |
| 842 | vim_regfree(regmatch.regprog); |
| 843 | } |
| 844 | |
| 845 | if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL) |
| 846 | return -1; |
| 847 | getvcol(curwin, &pos, &col, NULL, NULL); |
| 848 | return (int)col; |
| 849 | } |
| 850 | |
| 851 | #if defined(FEAT_LINEBREAK) || defined(PROTO) |
| 852 | /* |
Bram Moolenaar | 7bae0b1 | 2019-11-21 22:14:18 +0100 | [diff] [blame] | 853 | * This is called when 'breakindentopt' is changed and when a window is |
| 854 | * initialized. |
| 855 | */ |
| 856 | int |
| 857 | briopt_check(win_T *wp) |
| 858 | { |
| 859 | char_u *p; |
| 860 | int bri_shift = 0; |
| 861 | long bri_min = 20; |
| 862 | int bri_sbr = FALSE; |
Christian Brabandt | 4a0b85a | 2021-07-14 20:00:27 +0200 | [diff] [blame] | 863 | int bri_list = 0; |
Christian Brabandt | e7d6dbc | 2022-05-06 12:21:04 +0100 | [diff] [blame] | 864 | int bri_vcol = 0; |
Bram Moolenaar | 7bae0b1 | 2019-11-21 22:14:18 +0100 | [diff] [blame] | 865 | |
| 866 | p = wp->w_p_briopt; |
| 867 | while (*p != NUL) |
| 868 | { |
Yee Cheng Chin | 900894b | 2023-09-29 20:42:32 +0200 | [diff] [blame] | 869 | // Note: Keep this in sync with p_briopt_values |
Bram Moolenaar | 7bae0b1 | 2019-11-21 22:14:18 +0100 | [diff] [blame] | 870 | if (STRNCMP(p, "shift:", 6) == 0 |
| 871 | && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6]))) |
| 872 | { |
| 873 | p += 6; |
| 874 | bri_shift = getdigits(&p); |
| 875 | } |
| 876 | else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4])) |
| 877 | { |
| 878 | p += 4; |
| 879 | bri_min = getdigits(&p); |
| 880 | } |
| 881 | else if (STRNCMP(p, "sbr", 3) == 0) |
| 882 | { |
| 883 | p += 3; |
| 884 | bri_sbr = TRUE; |
| 885 | } |
Christian Brabandt | 4a0b85a | 2021-07-14 20:00:27 +0200 | [diff] [blame] | 886 | else if (STRNCMP(p, "list:", 5) == 0) |
| 887 | { |
| 888 | p += 5; |
| 889 | bri_list = getdigits(&p); |
| 890 | } |
Christian Brabandt | e7d6dbc | 2022-05-06 12:21:04 +0100 | [diff] [blame] | 891 | else if (STRNCMP(p, "column:", 7) == 0) |
| 892 | { |
| 893 | p += 7; |
| 894 | bri_vcol = getdigits(&p); |
| 895 | } |
Bram Moolenaar | 7bae0b1 | 2019-11-21 22:14:18 +0100 | [diff] [blame] | 896 | if (*p != ',' && *p != NUL) |
| 897 | return FAIL; |
| 898 | if (*p == ',') |
| 899 | ++p; |
| 900 | } |
| 901 | |
Bram Moolenaar | b81f56f | 2020-02-23 15:29:46 +0100 | [diff] [blame] | 902 | wp->w_briopt_shift = bri_shift; |
| 903 | wp->w_briopt_min = bri_min; |
| 904 | wp->w_briopt_sbr = bri_sbr; |
Christian Brabandt | 4a0b85a | 2021-07-14 20:00:27 +0200 | [diff] [blame] | 905 | wp->w_briopt_list = bri_list; |
Christian Brabandt | e7d6dbc | 2022-05-06 12:21:04 +0100 | [diff] [blame] | 906 | wp->w_briopt_vcol = bri_vcol; |
Bram Moolenaar | 7bae0b1 | 2019-11-21 22:14:18 +0100 | [diff] [blame] | 907 | |
| 908 | return OK; |
| 909 | } |
| 910 | |
| 911 | /* |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 912 | * Return appropriate space number for breakindent, taking influencing |
| 913 | * parameters into account. Window must be specified, since it is not |
| 914 | * necessarily always the current one. |
| 915 | */ |
| 916 | int |
| 917 | get_breakindent_win( |
| 918 | win_T *wp, |
| 919 | char_u *line) // start of the line |
| 920 | { |
Bram Moolenaar | b2d85e3 | 2022-01-07 16:55:32 +0000 | [diff] [blame] | 921 | static int prev_indent = 0; // cached indent value |
| 922 | static long prev_ts = 0L; // cached tabstop value |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 923 | # ifdef FEAT_VARTABS |
Bram Moolenaar | b2d85e3 | 2022-01-07 16:55:32 +0000 | [diff] [blame] | 924 | static int *prev_vts = NULL; // cached vartabs values |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 925 | # endif |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 926 | static int prev_fnum = 0; // cached buffer number |
| 927 | static char_u *prev_line = NULL; // cached copy of "line" |
| 928 | static varnumber_T prev_tick = 0; // changedtick of cached value |
| 929 | static int prev_list = 0; // cached list indent |
Bram Moolenaar | b2d85e3 | 2022-01-07 16:55:32 +0000 | [diff] [blame] | 930 | static int prev_listopt = 0; // cached w_p_briopt_list value |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 931 | static int prev_no_ts = FALSE; // cached no_ts value |
| 932 | static unsigned prev_dy_uhex = 0; // cached 'display' "uhex" value |
Christian Brabandt | c53b467 | 2022-01-15 10:01:05 +0000 | [diff] [blame] | 933 | // cached formatlistpat value |
| 934 | static char_u *prev_flp = NULL; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 935 | int bri = 0; |
| 936 | // window width minus window margin space, i.e. what rests for text |
| 937 | const int eff_wwidth = wp->w_width |
| 938 | - ((wp->w_p_nu || wp->w_p_rnu) |
| 939 | && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL) |
| 940 | ? number_width(wp) + 1 : 0); |
| 941 | |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 942 | // In list mode, if 'listchars' "tab" isn't set, a TAB is displayed as ^I. |
| 943 | int no_ts = wp->w_p_list && wp->w_lcs_chars.tab1 == NUL; |
| 944 | |
| 945 | // Used cached indent, unless |
| 946 | // - buffer changed, or |
| 947 | // - 'tabstop' changed, or |
| 948 | // - 'vartabstop' changed, or |
| 949 | // - buffer was changed, or |
| 950 | // - 'breakindentopt' "list" changed, or |
| 951 | // - 'list' or 'listchars' "tab" changed, or |
| 952 | // - 'display' "uhex" flag changed, or |
| 953 | // - 'formatlistpat' changed, or |
| 954 | // - line changed. |
Bram Moolenaar | c2a79b8 | 2022-07-01 13:15:35 +0100 | [diff] [blame] | 955 | if (prev_fnum != wp->w_buffer->b_fnum |
| 956 | || prev_ts != wp->w_buffer->b_p_ts |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 957 | # ifdef FEAT_VARTABS |
| 958 | || prev_vts != wp->w_buffer->b_p_vts_array |
| 959 | # endif |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 960 | || prev_tick != CHANGEDTICK(wp->w_buffer) |
| 961 | || prev_listopt != wp->w_briopt_list |
| 962 | || prev_no_ts != no_ts |
| 963 | || prev_dy_uhex != (dy_flags & DY_UHEX) |
| 964 | || prev_flp == NULL |
| 965 | || STRCMP(prev_flp, get_flp_value(wp->w_buffer)) != 0 |
| 966 | || prev_line == NULL || STRCMP(prev_line, line) != 0 |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 967 | ) |
| 968 | { |
Bram Moolenaar | c2a79b8 | 2022-07-01 13:15:35 +0100 | [diff] [blame] | 969 | prev_fnum = wp->w_buffer->b_fnum; |
| 970 | vim_free(prev_line); |
| 971 | prev_line = vim_strsave(line); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 972 | prev_ts = wp->w_buffer->b_p_ts; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 973 | # ifdef FEAT_VARTABS |
| 974 | prev_vts = wp->w_buffer->b_p_vts_array; |
Christian Brabandt | e7d6dbc | 2022-05-06 12:21:04 +0100 | [diff] [blame] | 975 | if (wp->w_briopt_vcol == 0) |
| 976 | prev_indent = get_indent_str_vtab(line, |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 977 | (int)wp->w_buffer->b_p_ts, |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 978 | wp->w_buffer->b_p_vts_array, no_ts); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 979 | # else |
Christian Brabandt | e7d6dbc | 2022-05-06 12:21:04 +0100 | [diff] [blame] | 980 | if (wp->w_briopt_vcol == 0) |
| 981 | prev_indent = get_indent_str(line, |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 982 | (int)wp->w_buffer->b_p_ts, no_ts); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 983 | # endif |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 984 | prev_tick = CHANGEDTICK(wp->w_buffer); |
Bram Moolenaar | b2d85e3 | 2022-01-07 16:55:32 +0000 | [diff] [blame] | 985 | prev_listopt = wp->w_briopt_list; |
Christian Brabandt | c53b467 | 2022-01-15 10:01:05 +0000 | [diff] [blame] | 986 | prev_list = 0; |
zeertzjq | efabd7c | 2024-02-11 17:16:19 +0100 | [diff] [blame] | 987 | prev_no_ts = no_ts; |
| 988 | prev_dy_uhex = (dy_flags & DY_UHEX); |
Christian Brabandt | c53b467 | 2022-01-15 10:01:05 +0000 | [diff] [blame] | 989 | vim_free(prev_flp); |
| 990 | prev_flp = vim_strsave(get_flp_value(wp->w_buffer)); |
Bram Moolenaar | b2d85e3 | 2022-01-07 16:55:32 +0000 | [diff] [blame] | 991 | // add additional indent for numbered lists |
Christian Brabandt | e7d6dbc | 2022-05-06 12:21:04 +0100 | [diff] [blame] | 992 | if (wp->w_briopt_list != 0 && wp->w_briopt_vcol == 0) |
Bram Moolenaar | b2d85e3 | 2022-01-07 16:55:32 +0000 | [diff] [blame] | 993 | { |
| 994 | regmatch_T regmatch; |
| 995 | |
Christian Brabandt | c53b467 | 2022-01-15 10:01:05 +0000 | [diff] [blame] | 996 | regmatch.regprog = vim_regcomp(prev_flp, |
| 997 | RE_MAGIC + RE_STRING + RE_AUTO + RE_STRICT); |
Bram Moolenaar | b2d85e3 | 2022-01-07 16:55:32 +0000 | [diff] [blame] | 998 | |
| 999 | if (regmatch.regprog != NULL) |
| 1000 | { |
| 1001 | regmatch.rm_ic = FALSE; |
| 1002 | if (vim_regexec(®match, line, 0)) |
| 1003 | { |
| 1004 | if (wp->w_briopt_list > 0) |
| 1005 | prev_list = wp->w_briopt_list; |
| 1006 | else |
Maxim Kim | 1191672 | 2022-09-02 14:08:53 +0100 | [diff] [blame] | 1007 | prev_indent = (*regmatch.endp - *regmatch.startp); |
Bram Moolenaar | b2d85e3 | 2022-01-07 16:55:32 +0000 | [diff] [blame] | 1008 | } |
| 1009 | vim_regfree(regmatch.regprog); |
| 1010 | } |
| 1011 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1012 | } |
Christian Brabandt | e7d6dbc | 2022-05-06 12:21:04 +0100 | [diff] [blame] | 1013 | if (wp->w_briopt_vcol != 0) |
| 1014 | { |
| 1015 | // column value has priority |
| 1016 | bri = wp->w_briopt_vcol; |
| 1017 | prev_list = 0; |
| 1018 | } |
| 1019 | else |
| 1020 | bri = prev_indent + wp->w_briopt_shift; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1021 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1022 | // Add offset for number column, if 'n' is in 'cpoptions' |
| 1023 | bri += win_col_off2(wp); |
| 1024 | |
Christian Brabandt | 4a0b85a | 2021-07-14 20:00:27 +0200 | [diff] [blame] | 1025 | // add additional indent for numbered lists |
Maxim Kim | 1191672 | 2022-09-02 14:08:53 +0100 | [diff] [blame] | 1026 | if (wp->w_briopt_list > 0) |
| 1027 | bri += prev_list; |
Christian Brabandt | 4a0b85a | 2021-07-14 20:00:27 +0200 | [diff] [blame] | 1028 | |
Maxim Kim | f674b35 | 2021-07-22 11:46:59 +0200 | [diff] [blame] | 1029 | // indent minus the length of the showbreak string |
| 1030 | if (wp->w_briopt_sbr) |
| 1031 | bri -= vim_strsize(get_showbreak_value(wp)); |
| 1032 | |
| 1033 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1034 | // never indent past left window margin |
| 1035 | if (bri < 0) |
| 1036 | bri = 0; |
Christian Brabandt | 4a0b85a | 2021-07-14 20:00:27 +0200 | [diff] [blame] | 1037 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1038 | // always leave at least bri_min characters on the left, |
| 1039 | // if text width is sufficient |
Bram Moolenaar | b81f56f | 2020-02-23 15:29:46 +0100 | [diff] [blame] | 1040 | else if (bri > eff_wwidth - wp->w_briopt_min) |
| 1041 | bri = (eff_wwidth - wp->w_briopt_min < 0) |
| 1042 | ? 0 : eff_wwidth - wp->w_briopt_min; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1043 | |
| 1044 | return bri; |
| 1045 | } |
| 1046 | #endif |
| 1047 | |
| 1048 | /* |
| 1049 | * When extra == 0: Return TRUE if the cursor is before or on the first |
| 1050 | * non-blank in the line. |
| 1051 | * When extra == 1: Return TRUE if the cursor is before the first non-blank in |
| 1052 | * the line. |
| 1053 | */ |
| 1054 | int |
| 1055 | inindent(int extra) |
| 1056 | { |
| 1057 | char_u *ptr; |
| 1058 | colnr_T col; |
| 1059 | |
| 1060 | for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col) |
| 1061 | ++ptr; |
| 1062 | if (col >= curwin->w_cursor.col + extra) |
| 1063 | return TRUE; |
| 1064 | else |
| 1065 | return FALSE; |
| 1066 | } |
| 1067 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1068 | /* |
| 1069 | * op_reindent - handle reindenting a block of lines. |
| 1070 | */ |
| 1071 | void |
| 1072 | op_reindent(oparg_T *oap, int (*how)(void)) |
| 1073 | { |
Bram Moolenaar | 4c84dd3 | 2022-04-20 10:22:54 +0100 | [diff] [blame] | 1074 | long i = 0; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1075 | char_u *l; |
| 1076 | int amount; |
| 1077 | linenr_T first_changed = 0; |
| 1078 | linenr_T last_changed = 0; |
| 1079 | linenr_T start_lnum = curwin->w_cursor.lnum; |
| 1080 | |
| 1081 | // Don't even try when 'modifiable' is off. |
| 1082 | if (!curbuf->b_p_ma) |
| 1083 | { |
Bram Moolenaar | 108010a | 2021-06-27 22:03:33 +0200 | [diff] [blame] | 1084 | emsg(_(e_cannot_make_changes_modifiable_is_off)); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1085 | return; |
| 1086 | } |
| 1087 | |
Bram Moolenaar | e468698 | 2022-04-19 18:28:45 +0100 | [diff] [blame] | 1088 | // Save for undo. Do this once for all lines, much faster than doing this |
| 1089 | // for each line separately, especially when undoing. |
| 1090 | if (u_savecommon(start_lnum - 1, start_lnum + oap->line_count, |
| 1091 | start_lnum + oap->line_count, FALSE) == OK) |
| 1092 | for (i = oap->line_count; --i >= 0 && !got_int; ) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1093 | { |
Bram Moolenaar | e468698 | 2022-04-19 18:28:45 +0100 | [diff] [blame] | 1094 | // it's a slow thing to do, so give feedback so there's no worry |
| 1095 | // that the computer's just hung. |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1096 | |
Bram Moolenaar | e468698 | 2022-04-19 18:28:45 +0100 | [diff] [blame] | 1097 | if (i > 1 |
| 1098 | && (i % 50 == 0 || i == oap->line_count - 1) |
| 1099 | && oap->line_count > p_report) |
| 1100 | smsg(_("%ld lines to indent... "), i); |
| 1101 | |
| 1102 | // Be vi-compatible: For lisp indenting the first line is not |
| 1103 | // indented, unless there is only one line. |
Bram Moolenaar | e468698 | 2022-04-19 18:28:45 +0100 | [diff] [blame] | 1104 | if (i != oap->line_count - 1 || oap->line_count == 1 |
| 1105 | || how != get_lisp_indent) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1106 | { |
Bram Moolenaar | e468698 | 2022-04-19 18:28:45 +0100 | [diff] [blame] | 1107 | l = skipwhite(ml_get_curline()); |
| 1108 | if (*l == NUL) // empty or blank line |
| 1109 | amount = 0; |
| 1110 | else |
| 1111 | amount = how(); // get the indent for this line |
| 1112 | |
| 1113 | if (amount >= 0 && set_indent(amount, 0)) |
| 1114 | { |
| 1115 | // did change the indent, call changed_lines() later |
| 1116 | if (first_changed == 0) |
| 1117 | first_changed = curwin->w_cursor.lnum; |
| 1118 | last_changed = curwin->w_cursor.lnum; |
| 1119 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1120 | } |
Bram Moolenaar | e468698 | 2022-04-19 18:28:45 +0100 | [diff] [blame] | 1121 | ++curwin->w_cursor.lnum; |
| 1122 | curwin->w_cursor.col = 0; // make sure it's valid |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1123 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1124 | |
| 1125 | // put cursor on first non-blank of indented line |
| 1126 | curwin->w_cursor.lnum = start_lnum; |
| 1127 | beginline(BL_SOL | BL_FIX); |
| 1128 | |
| 1129 | // Mark changed lines so that they will be redrawn. When Visual |
| 1130 | // highlighting was present, need to continue until the last line. When |
| 1131 | // there is no change still need to remove the Visual highlighting. |
| 1132 | if (last_changed != 0) |
| 1133 | changed_lines(first_changed, 0, |
| 1134 | oap->is_VIsual ? start_lnum + oap->line_count : |
| 1135 | last_changed + 1, 0L); |
| 1136 | else if (oap->is_VIsual) |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 1137 | redraw_curbuf_later(UPD_INVERTED); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1138 | |
| 1139 | if (oap->line_count > p_report) |
| 1140 | { |
| 1141 | i = oap->line_count - (i + 1); |
| 1142 | smsg(NGETTEXT("%ld line indented ", |
| 1143 | "%ld lines indented ", i), i); |
| 1144 | } |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 1145 | if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0) |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 1146 | { |
| 1147 | // set '[ and '] marks |
| 1148 | curbuf->b_op_start = oap->start; |
| 1149 | curbuf->b_op_end = oap->end; |
| 1150 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1151 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1152 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1153 | /* |
| 1154 | * Return TRUE if lines starting with '#' should be left aligned. |
| 1155 | */ |
| 1156 | int |
| 1157 | preprocs_left(void) |
| 1158 | { |
| 1159 | return |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1160 | (curbuf->b_p_si && !curbuf->b_p_cin) || |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1161 | (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE) |
| 1162 | && curbuf->b_ind_hash_comment == 0) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1163 | ; |
| 1164 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1165 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1166 | /* |
Bram Moolenaar | de5cf28 | 2022-05-14 11:52:23 +0100 | [diff] [blame] | 1167 | * Return TRUE if the conditions are OK for smart indenting. |
| 1168 | */ |
| 1169 | int |
Yegappan Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 1170 | may_do_si(void) |
Bram Moolenaar | de5cf28 | 2022-05-14 11:52:23 +0100 | [diff] [blame] | 1171 | { |
| 1172 | return curbuf->b_p_si |
Bram Moolenaar | de5cf28 | 2022-05-14 11:52:23 +0100 | [diff] [blame] | 1173 | && !curbuf->b_p_cin |
Bram Moolenaar | de5cf28 | 2022-05-14 11:52:23 +0100 | [diff] [blame] | 1174 | # ifdef FEAT_EVAL |
| 1175 | && *curbuf->b_p_inde == NUL |
| 1176 | # endif |
| 1177 | && !p_paste; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1181 | * Try to do some very smart auto-indenting. |
| 1182 | * Used when inserting a "normal" character. |
| 1183 | */ |
| 1184 | void |
| 1185 | ins_try_si(int c) |
| 1186 | { |
| 1187 | pos_T *pos, old_pos; |
| 1188 | char_u *ptr; |
| 1189 | int i; |
| 1190 | int temp; |
| 1191 | |
| 1192 | // do some very smart indenting when entering '{' or '}' |
Bram Moolenaar | 2e444bb | 2022-05-14 12:54:23 +0100 | [diff] [blame] | 1193 | if (((did_si || can_si_back) && c == '{') |
| 1194 | || (can_si && c == '}' && inindent(0))) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1195 | { |
| 1196 | // for '}' set indent equal to indent of line containing matching '{' |
| 1197 | if (c == '}' && (pos = findmatch(NULL, '{')) != NULL) |
| 1198 | { |
| 1199 | old_pos = curwin->w_cursor; |
| 1200 | // If the matching '{' has a ')' immediately before it (ignoring |
| 1201 | // white-space), then line up with the start of the line |
| 1202 | // containing the matching '(' if there is one. This handles the |
| 1203 | // case where an "if (..\n..) {" statement continues over multiple |
| 1204 | // lines -- webb |
| 1205 | ptr = ml_get(pos->lnum); |
| 1206 | i = pos->col; |
| 1207 | if (i > 0) // skip blanks before '{' |
| 1208 | while (--i > 0 && VIM_ISWHITE(ptr[i])) |
| 1209 | ; |
| 1210 | curwin->w_cursor.lnum = pos->lnum; |
| 1211 | curwin->w_cursor.col = i; |
| 1212 | if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL) |
| 1213 | curwin->w_cursor = *pos; |
| 1214 | i = get_indent(); |
| 1215 | curwin->w_cursor = old_pos; |
| 1216 | if (State & VREPLACE_FLAG) |
| 1217 | change_indent(INDENT_SET, i, FALSE, NUL, TRUE); |
| 1218 | else |
| 1219 | (void)set_indent(i, SIN_CHANGED); |
| 1220 | } |
| 1221 | else if (curwin->w_cursor.col > 0) |
| 1222 | { |
| 1223 | // when inserting '{' after "O" reduce indent, but not |
| 1224 | // more than indent of previous line |
| 1225 | temp = TRUE; |
| 1226 | if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1) |
| 1227 | { |
| 1228 | old_pos = curwin->w_cursor; |
| 1229 | i = get_indent(); |
| 1230 | while (curwin->w_cursor.lnum > 1) |
| 1231 | { |
| 1232 | ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum))); |
| 1233 | |
| 1234 | // ignore empty lines and lines starting with '#'. |
| 1235 | if (*ptr != '#' && *ptr != NUL) |
| 1236 | break; |
| 1237 | } |
| 1238 | if (get_indent() >= i) |
| 1239 | temp = FALSE; |
| 1240 | curwin->w_cursor = old_pos; |
| 1241 | } |
| 1242 | if (temp) |
| 1243 | shift_line(TRUE, FALSE, 1, TRUE); |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | // set indent of '#' always to 0 |
Bram Moolenaar | de5cf28 | 2022-05-14 11:52:23 +0100 | [diff] [blame] | 1248 | if (curwin->w_cursor.col > 0 && can_si && c == '#' && inindent(0)) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1249 | { |
| 1250 | // remember current indent for next line |
| 1251 | old_indent = get_indent(); |
| 1252 | (void)set_indent(0, SIN_CHANGED); |
| 1253 | } |
| 1254 | |
| 1255 | // Adjust ai_col, the char at this position can be deleted. |
| 1256 | if (ai_col > curwin->w_cursor.col) |
| 1257 | ai_col = curwin->w_cursor.col; |
| 1258 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1259 | |
| 1260 | /* |
| 1261 | * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D). |
| 1262 | * Keep the cursor on the same character. |
| 1263 | * type == INDENT_INC increase indent (for CTRL-T or <Tab>) |
| 1264 | * type == INDENT_DEC decrease indent (for CTRL-D) |
| 1265 | * type == INDENT_SET set indent to "amount" |
| 1266 | * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec). |
| 1267 | */ |
| 1268 | void |
| 1269 | change_indent( |
| 1270 | int type, |
| 1271 | int amount, |
| 1272 | int round, |
| 1273 | int replaced, // replaced character, put on replace stack |
| 1274 | int call_changed_bytes) // call changed_bytes() |
| 1275 | { |
| 1276 | int vcol; |
| 1277 | int last_vcol; |
| 1278 | int insstart_less; // reduction for Insstart.col |
| 1279 | int new_cursor_col; |
| 1280 | int i; |
| 1281 | char_u *ptr; |
| 1282 | int save_p_list; |
| 1283 | int start_col; |
| 1284 | colnr_T vc; |
| 1285 | colnr_T orig_col = 0; // init for GCC |
| 1286 | char_u *new_line, *orig_line = NULL; // init for GCC |
| 1287 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1288 | // MODE_VREPLACE state needs to know what the line was like before changing |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1289 | if (State & VREPLACE_FLAG) |
| 1290 | { |
| 1291 | orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below |
| 1292 | orig_col = curwin->w_cursor.col; |
| 1293 | } |
| 1294 | |
| 1295 | // for the following tricks we don't want list mode |
| 1296 | save_p_list = curwin->w_p_list; |
| 1297 | curwin->w_p_list = FALSE; |
Bram Moolenaar | 702bd6c | 2022-09-14 16:09:57 +0100 | [diff] [blame] | 1298 | #ifdef FEAT_PROP_POPUP |
| 1299 | ignore_text_props = TRUE; |
| 1300 | #endif |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1301 | vc = getvcol_nolist(&curwin->w_cursor); |
| 1302 | vcol = vc; |
| 1303 | |
| 1304 | // For Replace mode we need to fix the replace stack later, which is only |
| 1305 | // possible when the cursor is in the indent. Remember the number of |
| 1306 | // characters before the cursor if it's possible. |
| 1307 | start_col = curwin->w_cursor.col; |
| 1308 | |
| 1309 | // determine offset from first non-blank |
| 1310 | new_cursor_col = curwin->w_cursor.col; |
| 1311 | beginline(BL_WHITE); |
| 1312 | new_cursor_col -= curwin->w_cursor.col; |
| 1313 | |
| 1314 | insstart_less = curwin->w_cursor.col; |
| 1315 | |
| 1316 | // If the cursor is in the indent, compute how many screen columns the |
| 1317 | // cursor is to the left of the first non-blank. |
| 1318 | if (new_cursor_col < 0) |
| 1319 | vcol = get_indent() - vcol; |
| 1320 | |
| 1321 | if (new_cursor_col > 0) // can't fix replace stack |
| 1322 | start_col = -1; |
| 1323 | |
| 1324 | // Set the new indent. The cursor will be put on the first non-blank. |
| 1325 | if (type == INDENT_SET) |
| 1326 | (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0); |
| 1327 | else |
| 1328 | { |
| 1329 | int save_State = State; |
| 1330 | |
| 1331 | // Avoid being called recursively. |
| 1332 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1333 | State = MODE_INSERT; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1334 | shift_line(type == INDENT_DEC, round, 1, call_changed_bytes); |
| 1335 | State = save_State; |
| 1336 | } |
| 1337 | insstart_less -= curwin->w_cursor.col; |
| 1338 | |
| 1339 | // Try to put cursor on same character. |
| 1340 | // If the cursor is at or after the first non-blank in the line, |
| 1341 | // compute the cursor column relative to the column of the first |
| 1342 | // non-blank character. |
| 1343 | // If we are not in insert mode, leave the cursor on the first non-blank. |
| 1344 | // If the cursor is before the first non-blank, position it relative |
| 1345 | // to the first non-blank, counted in screen columns. |
| 1346 | if (new_cursor_col >= 0) |
| 1347 | { |
| 1348 | // When changing the indent while the cursor is touching it, reset |
| 1349 | // Insstart_col to 0. |
| 1350 | if (new_cursor_col == 0) |
| 1351 | insstart_less = MAXCOL; |
| 1352 | new_cursor_col += curwin->w_cursor.col; |
| 1353 | } |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1354 | else if (!(State & MODE_INSERT)) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1355 | new_cursor_col = curwin->w_cursor.col; |
| 1356 | else |
| 1357 | { |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 1358 | chartabsize_T cts; |
| 1359 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1360 | // Compute the screen column where the cursor should be. |
| 1361 | vcol = get_indent() - vcol; |
| 1362 | curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol); |
| 1363 | |
| 1364 | // Advance the cursor until we reach the right screen column. |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 1365 | last_vcol = 0; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1366 | ptr = ml_get_curline(); |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 1367 | init_chartabsize_arg(&cts, curwin, 0, 0, ptr, ptr); |
| 1368 | while (cts.cts_vcol <= (int)curwin->w_virtcol) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1369 | { |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 1370 | last_vcol = cts.cts_vcol; |
| 1371 | if (cts.cts_vcol > 0) |
| 1372 | MB_PTR_ADV(cts.cts_ptr); |
| 1373 | if (*cts.cts_ptr == NUL) |
Bram Moolenaar | 4e889f9 | 2022-02-21 19:36:12 +0000 | [diff] [blame] | 1374 | break; |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 1375 | cts.cts_vcol += lbr_chartabsize(&cts); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1376 | } |
| 1377 | vcol = last_vcol; |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 1378 | new_cursor_col = cts.cts_ptr - cts.cts_line; |
| 1379 | clear_chartabsize_arg(&cts); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1380 | |
| 1381 | // May need to insert spaces to be able to position the cursor on |
| 1382 | // the right screen column. |
| 1383 | if (vcol != (int)curwin->w_virtcol) |
| 1384 | { |
| 1385 | curwin->w_cursor.col = (colnr_T)new_cursor_col; |
| 1386 | i = (int)curwin->w_virtcol - vcol; |
| 1387 | ptr = alloc(i + 1); |
| 1388 | if (ptr != NULL) |
| 1389 | { |
| 1390 | new_cursor_col += i; |
| 1391 | ptr[i] = NUL; |
| 1392 | while (--i >= 0) |
| 1393 | ptr[i] = ' '; |
| 1394 | ins_str(ptr); |
| 1395 | vim_free(ptr); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | // When changing the indent while the cursor is in it, reset |
| 1400 | // Insstart_col to 0. |
| 1401 | insstart_less = MAXCOL; |
| 1402 | } |
| 1403 | |
| 1404 | curwin->w_p_list = save_p_list; |
| 1405 | |
| 1406 | if (new_cursor_col <= 0) |
| 1407 | curwin->w_cursor.col = 0; |
| 1408 | else |
| 1409 | curwin->w_cursor.col = (colnr_T)new_cursor_col; |
| 1410 | curwin->w_set_curswant = TRUE; |
| 1411 | changed_cline_bef_curs(); |
| 1412 | |
| 1413 | // May have to adjust the start of the insert. |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1414 | if (State & MODE_INSERT) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1415 | { |
| 1416 | if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0) |
| 1417 | { |
| 1418 | if ((int)Insstart.col <= insstart_less) |
| 1419 | Insstart.col = 0; |
| 1420 | else |
| 1421 | Insstart.col -= insstart_less; |
| 1422 | } |
| 1423 | if ((int)ai_col <= insstart_less) |
| 1424 | ai_col = 0; |
| 1425 | else |
| 1426 | ai_col -= insstart_less; |
| 1427 | } |
| 1428 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1429 | // For MODE_REPLACE state, may have to fix the replace stack, if it's |
| 1430 | // possible. If the number of characters before the cursor decreased, need |
| 1431 | // to pop a few characters from the replace stack. |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1432 | // If the number of characters before the cursor increased, need to push a |
| 1433 | // few NULs onto the replace stack. |
| 1434 | if (REPLACE_NORMAL(State) && start_col >= 0) |
| 1435 | { |
| 1436 | while (start_col > (int)curwin->w_cursor.col) |
| 1437 | { |
| 1438 | replace_join(0); // remove a NUL from the replace stack |
| 1439 | --start_col; |
| 1440 | } |
| 1441 | while (start_col < (int)curwin->w_cursor.col || replaced) |
| 1442 | { |
| 1443 | replace_push(NUL); |
| 1444 | if (replaced) |
| 1445 | { |
| 1446 | replace_push(replaced); |
| 1447 | replaced = NUL; |
| 1448 | } |
| 1449 | ++start_col; |
| 1450 | } |
| 1451 | } |
Bram Moolenaar | 702bd6c | 2022-09-14 16:09:57 +0100 | [diff] [blame] | 1452 | #ifdef FEAT_PROP_POPUP |
| 1453 | ignore_text_props = FALSE; |
| 1454 | #endif |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1455 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1456 | // For MODE_VREPLACE state, we also have to fix the replace stack. In this |
| 1457 | // case it is always possible because we backspace over the whole line and |
| 1458 | // then put it back again the way we wanted it. |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1459 | if (State & VREPLACE_FLAG) |
| 1460 | { |
| 1461 | // If orig_line didn't allocate, just return. At least we did the job, |
| 1462 | // even if you can't backspace. |
| 1463 | if (orig_line == NULL) |
| 1464 | return; |
| 1465 | |
| 1466 | // Save new line |
| 1467 | new_line = vim_strsave(ml_get_curline()); |
| 1468 | if (new_line == NULL) |
| 1469 | return; |
| 1470 | |
| 1471 | // We only put back the new line up to the cursor |
| 1472 | new_line[curwin->w_cursor.col] = NUL; |
| 1473 | |
| 1474 | // Put back original line |
| 1475 | ml_replace(curwin->w_cursor.lnum, orig_line, FALSE); |
| 1476 | curwin->w_cursor.col = orig_col; |
| 1477 | |
| 1478 | // Backspace from cursor to start of line |
| 1479 | backspace_until_column(0); |
| 1480 | |
| 1481 | // Insert new stuff into line again |
| 1482 | ins_bytes(new_line); |
| 1483 | |
| 1484 | vim_free(new_line); |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 | * Copy the indent from ptr to the current line (and fill to size) |
| 1490 | * Leaves the cursor on the first non-blank in the line. |
| 1491 | * Returns TRUE if the line was changed. |
| 1492 | */ |
| 1493 | int |
| 1494 | copy_indent(int size, char_u *src) |
| 1495 | { |
| 1496 | char_u *p = NULL; |
| 1497 | char_u *line = NULL; |
| 1498 | char_u *s; |
| 1499 | int todo; |
| 1500 | int ind_len; |
| 1501 | int line_len = 0; |
| 1502 | int tab_pad; |
| 1503 | int ind_done; |
| 1504 | int round; |
| 1505 | #ifdef FEAT_VARTABS |
| 1506 | int ind_col; |
| 1507 | #endif |
| 1508 | |
| 1509 | // Round 1: compute the number of characters needed for the indent |
| 1510 | // Round 2: copy the characters. |
| 1511 | for (round = 1; round <= 2; ++round) |
| 1512 | { |
| 1513 | todo = size; |
| 1514 | ind_len = 0; |
| 1515 | ind_done = 0; |
| 1516 | #ifdef FEAT_VARTABS |
| 1517 | ind_col = 0; |
| 1518 | #endif |
| 1519 | s = src; |
| 1520 | |
| 1521 | // Count/copy the usable portion of the source line |
| 1522 | while (todo > 0 && VIM_ISWHITE(*s)) |
| 1523 | { |
| 1524 | if (*s == TAB) |
| 1525 | { |
| 1526 | #ifdef FEAT_VARTABS |
| 1527 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 1528 | curbuf->b_p_vts_array); |
| 1529 | #else |
| 1530 | tab_pad = (int)curbuf->b_p_ts |
| 1531 | - (ind_done % (int)curbuf->b_p_ts); |
| 1532 | #endif |
| 1533 | // Stop if this tab will overshoot the target |
| 1534 | if (todo < tab_pad) |
| 1535 | break; |
| 1536 | todo -= tab_pad; |
| 1537 | ind_done += tab_pad; |
| 1538 | #ifdef FEAT_VARTABS |
| 1539 | ind_col += tab_pad; |
| 1540 | #endif |
| 1541 | } |
| 1542 | else |
| 1543 | { |
| 1544 | --todo; |
| 1545 | ++ind_done; |
| 1546 | #ifdef FEAT_VARTABS |
| 1547 | ++ind_col; |
| 1548 | #endif |
| 1549 | } |
| 1550 | ++ind_len; |
| 1551 | if (p != NULL) |
| 1552 | *p++ = *s; |
| 1553 | ++s; |
| 1554 | } |
| 1555 | |
| 1556 | // Fill to next tabstop with a tab, if possible |
| 1557 | #ifdef FEAT_VARTABS |
| 1558 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 1559 | curbuf->b_p_vts_array); |
| 1560 | #else |
| 1561 | tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
| 1562 | #endif |
| 1563 | if (todo >= tab_pad && !curbuf->b_p_et) |
| 1564 | { |
| 1565 | todo -= tab_pad; |
| 1566 | ++ind_len; |
| 1567 | #ifdef FEAT_VARTABS |
| 1568 | ind_col += tab_pad; |
| 1569 | #endif |
| 1570 | if (p != NULL) |
| 1571 | *p++ = TAB; |
| 1572 | } |
| 1573 | |
| 1574 | // Add tabs required for indent |
| 1575 | if (!curbuf->b_p_et) |
| 1576 | { |
| 1577 | #ifdef FEAT_VARTABS |
| 1578 | for (;;) |
| 1579 | { |
| 1580 | tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts, |
| 1581 | curbuf->b_p_vts_array); |
| 1582 | if (todo < tab_pad) |
| 1583 | break; |
| 1584 | todo -= tab_pad; |
| 1585 | ++ind_len; |
| 1586 | ind_col += tab_pad; |
| 1587 | if (p != NULL) |
| 1588 | *p++ = TAB; |
| 1589 | } |
| 1590 | #else |
| 1591 | while (todo >= (int)curbuf->b_p_ts) |
| 1592 | { |
| 1593 | todo -= (int)curbuf->b_p_ts; |
| 1594 | ++ind_len; |
| 1595 | if (p != NULL) |
| 1596 | *p++ = TAB; |
| 1597 | } |
| 1598 | #endif |
| 1599 | } |
| 1600 | |
| 1601 | // Count/add spaces required for indent |
| 1602 | while (todo > 0) |
| 1603 | { |
| 1604 | --todo; |
| 1605 | ++ind_len; |
| 1606 | if (p != NULL) |
| 1607 | *p++ = ' '; |
| 1608 | } |
| 1609 | |
| 1610 | if (p == NULL) |
| 1611 | { |
| 1612 | // Allocate memory for the result: the copied indent, new indent |
| 1613 | // and the rest of the line. |
| 1614 | line_len = (int)STRLEN(ml_get_curline()) + 1; |
| 1615 | line = alloc(ind_len + line_len); |
| 1616 | if (line == NULL) |
| 1617 | return FALSE; |
| 1618 | p = line; |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | // Append the original line |
| 1623 | mch_memmove(p, ml_get_curline(), (size_t)line_len); |
| 1624 | |
| 1625 | // Replace the line |
| 1626 | ml_replace(curwin->w_cursor.lnum, line, FALSE); |
| 1627 | |
| 1628 | // Put the cursor after the indent. |
| 1629 | curwin->w_cursor.col = ind_len; |
| 1630 | return TRUE; |
| 1631 | } |
| 1632 | |
| 1633 | /* |
Bram Moolenaar | 308660b | 2022-06-16 12:10:48 +0100 | [diff] [blame] | 1634 | * Give a "resulting text too long" error and maybe set got_int. |
| 1635 | */ |
| 1636 | static void |
| 1637 | emsg_text_too_long(void) |
| 1638 | { |
| 1639 | emsg(_(e_resulting_text_too_long)); |
| 1640 | #ifdef FEAT_EVAL |
| 1641 | // when not inside a try/catch set got_int to break out of any loop |
| 1642 | if (trylevel == 0) |
| 1643 | #endif |
| 1644 | got_int = TRUE; |
| 1645 | } |
| 1646 | |
| 1647 | /* |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1648 | * ":retab". |
| 1649 | */ |
| 1650 | void |
| 1651 | ex_retab(exarg_T *eap) |
| 1652 | { |
| 1653 | linenr_T lnum; |
| 1654 | int got_tab = FALSE; |
| 1655 | long num_spaces = 0; |
| 1656 | long num_tabs; |
| 1657 | long len; |
| 1658 | long col; |
| 1659 | long vcol; |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1660 | long start_col = 0; // For start of white-space string |
| 1661 | long start_vcol = 0; // For start of white-space string |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1662 | long old_len; |
Bram Moolenaar | 33f3c59 | 2022-02-12 20:46:15 +0000 | [diff] [blame] | 1663 | long new_len; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1664 | char_u *ptr; |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1665 | char_u *new_line = (char_u *)1; // init to non-NULL |
| 1666 | int did_undo; // called u_save for current line |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1667 | #ifdef FEAT_VARTABS |
| 1668 | int *new_vts_array = NULL; |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1669 | char_u *new_ts_str; // string value of tab argument |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1670 | #else |
| 1671 | int temp; |
| 1672 | int new_ts; |
| 1673 | #endif |
| 1674 | int save_list; |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1675 | linenr_T first_line = 0; // first changed line |
| 1676 | linenr_T last_line = 0; // last changed line |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1677 | |
| 1678 | save_list = curwin->w_p_list; |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1679 | curwin->w_p_list = 0; // don't want list mode here |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1680 | |
| 1681 | #ifdef FEAT_VARTABS |
| 1682 | new_ts_str = eap->arg; |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 1683 | if (tabstop_set(eap->arg, &new_vts_array) == FAIL) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1684 | return; |
| 1685 | while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',') |
| 1686 | ++(eap->arg); |
| 1687 | |
| 1688 | // This ensures that either new_vts_array and new_ts_str are freshly |
| 1689 | // allocated, or new_vts_array points to an existing array and new_ts_str |
| 1690 | // is null. |
| 1691 | if (new_vts_array == NULL) |
| 1692 | { |
| 1693 | new_vts_array = curbuf->b_p_vts_array; |
| 1694 | new_ts_str = NULL; |
| 1695 | } |
| 1696 | else |
| 1697 | new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str); |
| 1698 | #else |
Bram Moolenaar | 2ddb89f | 2021-09-04 21:20:41 +0200 | [diff] [blame] | 1699 | ptr = eap->arg; |
| 1700 | new_ts = getdigits(&ptr); |
| 1701 | if (new_ts < 0 && *eap->arg == '-') |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1702 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1703 | emsg(_(e_argument_must_be_positive)); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1704 | return; |
| 1705 | } |
Bram Moolenaar | 652dee4 | 2022-01-28 20:47:49 +0000 | [diff] [blame] | 1706 | if (new_ts < 0 || new_ts > TABSTOP_MAX) |
Bram Moolenaar | 2ddb89f | 2021-09-04 21:20:41 +0200 | [diff] [blame] | 1707 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1708 | semsg(_(e_invalid_argument_str), eap->arg); |
Bram Moolenaar | 2ddb89f | 2021-09-04 21:20:41 +0200 | [diff] [blame] | 1709 | return; |
| 1710 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1711 | if (new_ts == 0) |
| 1712 | new_ts = curbuf->b_p_ts; |
| 1713 | #endif |
| 1714 | for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum) |
| 1715 | { |
| 1716 | ptr = ml_get(lnum); |
| 1717 | col = 0; |
| 1718 | vcol = 0; |
| 1719 | did_undo = FALSE; |
| 1720 | for (;;) |
| 1721 | { |
| 1722 | if (VIM_ISWHITE(ptr[col])) |
| 1723 | { |
| 1724 | if (!got_tab && num_spaces == 0) |
| 1725 | { |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1726 | // First consecutive white-space |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1727 | start_vcol = vcol; |
| 1728 | start_col = col; |
| 1729 | } |
| 1730 | if (ptr[col] == ' ') |
| 1731 | num_spaces++; |
| 1732 | else |
| 1733 | got_tab = TRUE; |
| 1734 | } |
| 1735 | else |
| 1736 | { |
| 1737 | if (got_tab || (eap->forceit && num_spaces > 1)) |
| 1738 | { |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1739 | // Retabulate this string of white-space |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1740 | |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1741 | // len is virtual length of white string |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1742 | len = num_spaces = vcol - start_vcol; |
| 1743 | num_tabs = 0; |
| 1744 | if (!curbuf->b_p_et) |
| 1745 | { |
| 1746 | #ifdef FEAT_VARTABS |
| 1747 | int t, s; |
| 1748 | |
| 1749 | tabstop_fromto(start_vcol, vcol, |
| 1750 | curbuf->b_p_ts, new_vts_array, &t, &s); |
| 1751 | num_tabs = t; |
| 1752 | num_spaces = s; |
| 1753 | #else |
| 1754 | temp = new_ts - (start_vcol % new_ts); |
| 1755 | if (num_spaces >= temp) |
| 1756 | { |
| 1757 | num_spaces -= temp; |
| 1758 | num_tabs++; |
| 1759 | } |
| 1760 | num_tabs += num_spaces / new_ts; |
| 1761 | num_spaces -= (num_spaces / new_ts) * new_ts; |
| 1762 | #endif |
| 1763 | } |
| 1764 | if (curbuf->b_p_et || got_tab || |
| 1765 | (num_spaces + num_tabs < len)) |
| 1766 | { |
| 1767 | if (did_undo == FALSE) |
| 1768 | { |
| 1769 | did_undo = TRUE; |
| 1770 | if (u_save((linenr_T)(lnum - 1), |
| 1771 | (linenr_T)(lnum + 1)) == FAIL) |
| 1772 | { |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1773 | new_line = NULL; // flag out-of-memory |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1774 | break; |
| 1775 | } |
| 1776 | } |
| 1777 | |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1778 | // len is actual number of white characters used |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1779 | len = num_spaces + num_tabs; |
| 1780 | old_len = (long)STRLEN(ptr); |
Bram Moolenaar | 33f3c59 | 2022-02-12 20:46:15 +0000 | [diff] [blame] | 1781 | new_len = old_len - col + start_col + len + 1; |
Bram Moolenaar | 4549166 | 2022-02-12 21:59:51 +0000 | [diff] [blame] | 1782 | if (new_len <= 0 || new_len >= MAXCOL) |
Bram Moolenaar | 33f3c59 | 2022-02-12 20:46:15 +0000 | [diff] [blame] | 1783 | { |
Bram Moolenaar | 308660b | 2022-06-16 12:10:48 +0100 | [diff] [blame] | 1784 | emsg_text_too_long(); |
Bram Moolenaar | 33f3c59 | 2022-02-12 20:46:15 +0000 | [diff] [blame] | 1785 | break; |
| 1786 | } |
| 1787 | new_line = alloc(new_len); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1788 | if (new_line == NULL) |
| 1789 | break; |
| 1790 | if (start_col > 0) |
| 1791 | mch_memmove(new_line, ptr, (size_t)start_col); |
| 1792 | mch_memmove(new_line + start_col + len, |
| 1793 | ptr + col, (size_t)(old_len - col + 1)); |
| 1794 | ptr = new_line + start_col; |
| 1795 | for (col = 0; col < len; col++) |
| 1796 | ptr[col] = (col < num_tabs) ? '\t' : ' '; |
Bram Moolenaar | 0dcd39b | 2021-02-03 19:44:25 +0100 | [diff] [blame] | 1797 | if (ml_replace(lnum, new_line, FALSE) == OK) |
| 1798 | // "new_line" may have been copied |
| 1799 | new_line = curbuf->b_ml.ml_line_ptr; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1800 | if (first_line == 0) |
| 1801 | first_line = lnum; |
| 1802 | last_line = lnum; |
| 1803 | ptr = new_line; |
| 1804 | col = start_col + len; |
| 1805 | } |
| 1806 | } |
| 1807 | got_tab = FALSE; |
| 1808 | num_spaces = 0; |
| 1809 | } |
| 1810 | if (ptr[col] == NUL) |
| 1811 | break; |
| 1812 | vcol += chartabsize(ptr + col, (colnr_T)vcol); |
Bram Moolenaar | 6e28703 | 2022-02-12 15:42:18 +0000 | [diff] [blame] | 1813 | if (vcol >= MAXCOL) |
| 1814 | { |
Bram Moolenaar | 308660b | 2022-06-16 12:10:48 +0100 | [diff] [blame] | 1815 | emsg_text_too_long(); |
Bram Moolenaar | 6e28703 | 2022-02-12 15:42:18 +0000 | [diff] [blame] | 1816 | break; |
| 1817 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1818 | if (has_mbyte) |
| 1819 | col += (*mb_ptr2len)(ptr + col); |
| 1820 | else |
| 1821 | ++col; |
| 1822 | } |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1823 | if (new_line == NULL) // out of memory |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1824 | break; |
| 1825 | line_breakcheck(); |
| 1826 | } |
| 1827 | if (got_int) |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1828 | emsg(_(e_interrupted)); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1829 | |
| 1830 | #ifdef FEAT_VARTABS |
| 1831 | // If a single value was given then it can be considered equal to |
| 1832 | // either the value of 'tabstop' or the value of 'vartabstop'. |
| 1833 | if (tabstop_count(curbuf->b_p_vts_array) == 0 |
| 1834 | && tabstop_count(new_vts_array) == 1 |
| 1835 | && curbuf->b_p_ts == tabstop_first(new_vts_array)) |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1836 | ; // not changed |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1837 | else if (tabstop_count(curbuf->b_p_vts_array) > 0 |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 1838 | && tabstop_eq(curbuf->b_p_vts_array, new_vts_array)) |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1839 | ; // not changed |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1840 | else |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 1841 | redraw_curbuf_later(UPD_NOT_VALID); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1842 | #else |
| 1843 | if (curbuf->b_p_ts != new_ts) |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 1844 | redraw_curbuf_later(UPD_NOT_VALID); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1845 | #endif |
| 1846 | if (first_line != 0) |
| 1847 | changed_lines(first_line, 0, last_line + 1, 0L); |
| 1848 | |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1849 | curwin->w_p_list = save_list; // restore 'list' |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1850 | |
| 1851 | #ifdef FEAT_VARTABS |
Bram Moolenaar | aa2f0ee | 2019-12-21 18:47:26 +0100 | [diff] [blame] | 1852 | if (new_ts_str != NULL) // set the new tabstop |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1853 | { |
| 1854 | // If 'vartabstop' is in use or if the value given to retab has more |
| 1855 | // than one tabstop then update 'vartabstop'. |
| 1856 | int *old_vts_ary = curbuf->b_p_vts_array; |
| 1857 | |
| 1858 | if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1) |
| 1859 | { |
| 1860 | set_string_option_direct((char_u *)"vts", -1, new_ts_str, |
| 1861 | OPT_FREE|OPT_LOCAL, 0); |
| 1862 | curbuf->b_p_vts_array = new_vts_array; |
| 1863 | vim_free(old_vts_ary); |
| 1864 | } |
| 1865 | else |
| 1866 | { |
| 1867 | // 'vartabstop' wasn't in use and a single value was given to |
| 1868 | // retab then update 'tabstop'. |
| 1869 | curbuf->b_p_ts = tabstop_first(new_vts_array); |
| 1870 | vim_free(new_vts_array); |
| 1871 | } |
| 1872 | vim_free(new_ts_str); |
| 1873 | } |
| 1874 | #else |
| 1875 | curbuf->b_p_ts = new_ts; |
| 1876 | #endif |
| 1877 | coladvance(curwin->w_curswant); |
| 1878 | |
| 1879 | u_clearline(); |
| 1880 | } |
| 1881 | |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 1882 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1883 | /* |
| 1884 | * Get indent level from 'indentexpr'. |
| 1885 | */ |
| 1886 | int |
| 1887 | get_expr_indent(void) |
| 1888 | { |
| 1889 | int indent = -1; |
| 1890 | char_u *inde_copy; |
| 1891 | pos_T save_pos; |
| 1892 | colnr_T save_curswant; |
| 1893 | int save_set_curswant; |
| 1894 | int save_State; |
| 1895 | int use_sandbox = was_set_insecurely((char_u *)"indentexpr", |
| 1896 | OPT_LOCAL); |
Bram Moolenaar | 28e60cc | 2022-01-22 20:32:00 +0000 | [diff] [blame] | 1897 | sctx_T save_sctx = current_sctx; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1898 | |
| 1899 | // Save and restore cursor position and curswant, in case it was changed |
| 1900 | // via :normal commands |
| 1901 | save_pos = curwin->w_cursor; |
| 1902 | save_curswant = curwin->w_curswant; |
| 1903 | save_set_curswant = curwin->w_set_curswant; |
| 1904 | set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum); |
| 1905 | if (use_sandbox) |
| 1906 | ++sandbox; |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 1907 | ++textlock; |
Bram Moolenaar | 28e60cc | 2022-01-22 20:32:00 +0000 | [diff] [blame] | 1908 | current_sctx = curbuf->b_p_script_ctx[BV_INDE]; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1909 | |
| 1910 | // Need to make a copy, the 'indentexpr' option could be changed while |
| 1911 | // evaluating it. |
| 1912 | inde_copy = vim_strsave(curbuf->b_p_inde); |
| 1913 | if (inde_copy != NULL) |
| 1914 | { |
Bram Moolenaar | 3292a22 | 2022-10-01 20:17:17 +0100 | [diff] [blame] | 1915 | indent = (int)eval_to_number(inde_copy, TRUE); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1916 | vim_free(inde_copy); |
| 1917 | } |
| 1918 | |
| 1919 | if (use_sandbox) |
| 1920 | --sandbox; |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 1921 | --textlock; |
Bram Moolenaar | 28e60cc | 2022-01-22 20:32:00 +0000 | [diff] [blame] | 1922 | current_sctx = save_sctx; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1923 | |
| 1924 | // Restore the cursor position so that 'indentexpr' doesn't need to. |
| 1925 | // Pretend to be in Insert mode, allow cursor past end of line for "o" |
| 1926 | // command. |
| 1927 | save_State = State; |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1928 | State = MODE_INSERT; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1929 | curwin->w_cursor = save_pos; |
| 1930 | curwin->w_curswant = save_curswant; |
| 1931 | curwin->w_set_curswant = save_set_curswant; |
| 1932 | check_cursor(); |
| 1933 | State = save_State; |
| 1934 | |
Bram Moolenaar | 620c959 | 2021-07-31 21:32:31 +0200 | [diff] [blame] | 1935 | // Reset did_throw, unless 'debug' has "throw" and inside a try/catch. |
| 1936 | if (did_throw && (vim_strchr(p_debug, 't') == NULL || trylevel == 0)) |
| 1937 | { |
| 1938 | handle_did_throw(); |
| 1939 | did_throw = FALSE; |
| 1940 | } |
| 1941 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1942 | // If there is an error, just keep the current indent. |
| 1943 | if (indent < 0) |
| 1944 | indent = get_indent(); |
| 1945 | |
| 1946 | return indent; |
| 1947 | } |
| 1948 | #endif |
| 1949 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1950 | static int |
| 1951 | lisp_match(char_u *p) |
| 1952 | { |
| 1953 | char_u buf[LSIZE]; |
| 1954 | int len; |
| 1955 | char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords; |
| 1956 | |
| 1957 | while (*word != NUL) |
| 1958 | { |
| 1959 | (void)copy_option_part(&word, buf, LSIZE, ","); |
| 1960 | len = (int)STRLEN(buf); |
Bram Moolenaar | d26c580 | 2022-10-13 12:30:08 +0100 | [diff] [blame] | 1961 | if (STRNCMP(buf, p, len) == 0 && IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 1962 | return TRUE; |
| 1963 | } |
| 1964 | return FALSE; |
| 1965 | } |
| 1966 | |
| 1967 | /* |
| 1968 | * When 'p' is present in 'cpoptions, a Vi compatible method is used. |
| 1969 | * The incompatible newer method is quite a bit better at indenting |
| 1970 | * code in lisp-like languages than the traditional one; it's still |
| 1971 | * mostly heuristics however -- Dirk van Deun, dirk@rave.org |
| 1972 | * |
| 1973 | * TODO: |
| 1974 | * Findmatch() should be adapted for lisp, also to make showmatch |
| 1975 | * work correctly: now (v5.3) it seems all C/C++ oriented: |
| 1976 | * - it does not recognize the #\( and #\) notations as character literals |
| 1977 | * - it doesn't know about comments starting with a semicolon |
| 1978 | * - it incorrectly interprets '(' as a character literal |
| 1979 | * All this messes up get_lisp_indent in some rare cases. |
| 1980 | * Update from Sergey Khorev: |
| 1981 | * I tried to fix the first two issues. |
| 1982 | */ |
| 1983 | int |
| 1984 | get_lisp_indent(void) |
| 1985 | { |
| 1986 | pos_T *pos, realpos, paren; |
| 1987 | int amount; |
| 1988 | char_u *that; |
| 1989 | colnr_T col; |
| 1990 | colnr_T firsttry; |
| 1991 | int parencount, quotecount; |
| 1992 | int vi_lisp; |
| 1993 | |
| 1994 | // Set vi_lisp to use the vi-compatible method |
| 1995 | vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL); |
| 1996 | |
| 1997 | realpos = curwin->w_cursor; |
| 1998 | curwin->w_cursor.col = 0; |
| 1999 | |
| 2000 | if ((pos = findmatch(NULL, '(')) == NULL) |
| 2001 | pos = findmatch(NULL, '['); |
| 2002 | else |
| 2003 | { |
| 2004 | paren = *pos; |
| 2005 | pos = findmatch(NULL, '['); |
| 2006 | if (pos == NULL || LT_POSP(pos, &paren)) |
| 2007 | pos = &paren; |
| 2008 | } |
| 2009 | if (pos != NULL) |
| 2010 | { |
| 2011 | // Extra trick: Take the indent of the first previous non-white |
| 2012 | // line that is at the same () level. |
| 2013 | amount = -1; |
| 2014 | parencount = 0; |
| 2015 | |
| 2016 | while (--curwin->w_cursor.lnum >= pos->lnum) |
| 2017 | { |
| 2018 | if (linewhite(curwin->w_cursor.lnum)) |
| 2019 | continue; |
| 2020 | for (that = ml_get_curline(); *that != NUL; ++that) |
| 2021 | { |
| 2022 | if (*that == ';') |
| 2023 | { |
| 2024 | while (*(that + 1) != NUL) |
| 2025 | ++that; |
| 2026 | continue; |
| 2027 | } |
| 2028 | if (*that == '\\') |
| 2029 | { |
| 2030 | if (*(that + 1) != NUL) |
| 2031 | ++that; |
| 2032 | continue; |
| 2033 | } |
| 2034 | if (*that == '"' && *(that + 1) != NUL) |
| 2035 | { |
| 2036 | while (*++that && *that != '"') |
| 2037 | { |
| 2038 | // skipping escaped characters in the string |
| 2039 | if (*that == '\\') |
| 2040 | { |
| 2041 | if (*++that == NUL) |
| 2042 | break; |
| 2043 | if (that[1] == NUL) |
| 2044 | { |
| 2045 | ++that; |
| 2046 | break; |
| 2047 | } |
| 2048 | } |
| 2049 | } |
Bram Moolenaar | 0e8e938 | 2022-06-18 12:51:11 +0100 | [diff] [blame] | 2050 | if (*that == NUL) |
| 2051 | break; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2052 | } |
| 2053 | if (*that == '(' || *that == '[') |
| 2054 | ++parencount; |
| 2055 | else if (*that == ')' || *that == ']') |
| 2056 | --parencount; |
| 2057 | } |
| 2058 | if (parencount == 0) |
| 2059 | { |
| 2060 | amount = get_indent(); |
| 2061 | break; |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | if (amount == -1) |
| 2066 | { |
| 2067 | curwin->w_cursor.lnum = pos->lnum; |
| 2068 | curwin->w_cursor.col = pos->col; |
| 2069 | col = pos->col; |
| 2070 | |
| 2071 | that = ml_get_curline(); |
| 2072 | |
| 2073 | if (vi_lisp && get_indent() == 0) |
| 2074 | amount = 2; |
| 2075 | else |
| 2076 | { |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2077 | char_u *line = that; |
| 2078 | chartabsize_T cts; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2079 | |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2080 | init_chartabsize_arg(&cts, curwin, pos->lnum, 0, line, line); |
| 2081 | while (*cts.cts_ptr != NUL && col > 0) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2082 | { |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2083 | cts.cts_vcol += lbr_chartabsize_adv(&cts); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2084 | col--; |
| 2085 | } |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2086 | amount = cts.cts_vcol; |
| 2087 | that = cts.cts_ptr; |
| 2088 | clear_chartabsize_arg(&cts); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2089 | |
| 2090 | // Some keywords require "body" indenting rules (the |
| 2091 | // non-standard-lisp ones are Scheme special forms): |
| 2092 | // |
| 2093 | // (let ((a 1)) instead (let ((a 1)) |
| 2094 | // (...)) of (...)) |
| 2095 | |
| 2096 | if (!vi_lisp && (*that == '(' || *that == '[') |
| 2097 | && lisp_match(that + 1)) |
| 2098 | amount += 2; |
| 2099 | else |
| 2100 | { |
Bram Moolenaar | 8eba2bd | 2022-06-22 19:59:28 +0100 | [diff] [blame] | 2101 | if (*that != NUL) |
| 2102 | { |
| 2103 | that++; |
| 2104 | amount++; |
| 2105 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2106 | firsttry = amount; |
| 2107 | |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2108 | init_chartabsize_arg(&cts, curwin, (colnr_T)(that - line), |
| 2109 | amount, line, that); |
| 2110 | while (VIM_ISWHITE(*cts.cts_ptr)) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2111 | { |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2112 | cts.cts_vcol += lbr_chartabsize(&cts); |
| 2113 | ++cts.cts_ptr; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2114 | } |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2115 | that = cts.cts_ptr; |
| 2116 | amount = cts.cts_vcol; |
| 2117 | clear_chartabsize_arg(&cts); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2118 | |
| 2119 | if (*that && *that != ';') // not a comment line |
| 2120 | { |
| 2121 | // test *that != '(' to accommodate first let/do |
| 2122 | // argument if it is more than one line |
| 2123 | if (!vi_lisp && *that != '(' && *that != '[') |
| 2124 | firsttry++; |
| 2125 | |
| 2126 | parencount = 0; |
| 2127 | quotecount = 0; |
| 2128 | |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2129 | init_chartabsize_arg(&cts, curwin, |
| 2130 | (colnr_T)(that - line), amount, line, that); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2131 | if (vi_lisp |
| 2132 | || (*that != '"' |
| 2133 | && *that != '\'' |
| 2134 | && *that != '#' |
| 2135 | && (*that < '0' || *that > '9'))) |
| 2136 | { |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2137 | while (*cts.cts_ptr |
| 2138 | && (!VIM_ISWHITE(*cts.cts_ptr) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2139 | || quotecount |
| 2140 | || parencount) |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2141 | && (!((*cts.cts_ptr == '(' |
| 2142 | || *cts.cts_ptr == '[') |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2143 | && !quotecount |
| 2144 | && !parencount |
| 2145 | && vi_lisp))) |
| 2146 | { |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2147 | if (*cts.cts_ptr == '"') |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2148 | quotecount = !quotecount; |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2149 | if ((*cts.cts_ptr == '(' || *cts.cts_ptr == '[') |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2150 | && !quotecount) |
| 2151 | ++parencount; |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2152 | if ((*cts.cts_ptr == ')' || *cts.cts_ptr == ']') |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2153 | && !quotecount) |
| 2154 | --parencount; |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2155 | if (*cts.cts_ptr == '\\' |
| 2156 | && *(cts.cts_ptr+1) != NUL) |
| 2157 | cts.cts_vcol += lbr_chartabsize_adv(&cts); |
| 2158 | cts.cts_vcol += lbr_chartabsize_adv(&cts); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2159 | } |
| 2160 | } |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2161 | while (VIM_ISWHITE(*cts.cts_ptr)) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2162 | { |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2163 | cts.cts_vcol += lbr_chartabsize(&cts); |
| 2164 | ++cts.cts_ptr; |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2165 | } |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 2166 | that = cts.cts_ptr; |
| 2167 | amount = cts.cts_vcol; |
| 2168 | clear_chartabsize_arg(&cts); |
| 2169 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2170 | if (!*that || *that == ';') |
| 2171 | amount = firsttry; |
| 2172 | } |
| 2173 | } |
| 2174 | } |
| 2175 | } |
| 2176 | } |
| 2177 | else |
| 2178 | amount = 0; // no matching '(' or '[' found, use zero indent |
| 2179 | |
| 2180 | curwin->w_cursor = realpos; |
| 2181 | |
| 2182 | return amount; |
| 2183 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2184 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2185 | /* |
| 2186 | * Re-indent the current line, based on the current contents of it and the |
| 2187 | * surrounding lines. Fixing the cursor position seems really easy -- I'm very |
| 2188 | * confused what all the part that handles Control-T is doing that I'm not. |
| 2189 | * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent. |
| 2190 | */ |
| 2191 | |
| 2192 | void |
| 2193 | fixthisline(int (*get_the_indent)(void)) |
| 2194 | { |
| 2195 | int amount = get_the_indent(); |
| 2196 | |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2197 | if (amount < 0) |
| 2198 | return; |
| 2199 | |
| 2200 | change_indent(INDENT_SET, amount, FALSE, 0, TRUE); |
| 2201 | if (linewhite(curwin->w_cursor.lnum)) |
| 2202 | did_ai = TRUE; // delete the indent if the line stays empty |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2203 | } |
| 2204 | |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 2205 | /* |
Bram Moolenaar | 49846fb | 2022-10-15 16:05:33 +0100 | [diff] [blame] | 2206 | * Return TRUE if 'indentexpr' should be used for Lisp indenting. |
| 2207 | * Caller may want to check 'autoindent'. |
| 2208 | */ |
| 2209 | int |
| 2210 | use_indentexpr_for_lisp(void) |
| 2211 | { |
| 2212 | #ifdef FEAT_EVAL |
| 2213 | return curbuf->b_p_lisp |
| 2214 | && *curbuf->b_p_inde != NUL |
| 2215 | && STRCMP(curbuf->b_p_lop, "expr:1") == 0; |
| 2216 | #else |
| 2217 | return FALSE; |
| 2218 | #endif |
| 2219 | } |
| 2220 | |
| 2221 | /* |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 2222 | * Fix indent for 'lisp' and 'cindent'. |
| 2223 | */ |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2224 | void |
| 2225 | fix_indent(void) |
| 2226 | { |
| 2227 | if (p_paste) |
Bram Moolenaar | 49846fb | 2022-10-15 16:05:33 +0100 | [diff] [blame] | 2228 | return; // no auto-indenting when 'paste' is set |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2229 | if (curbuf->b_p_lisp && curbuf->b_p_ai) |
Bram Moolenaar | 49846fb | 2022-10-15 16:05:33 +0100 | [diff] [blame] | 2230 | { |
| 2231 | if (use_indentexpr_for_lisp()) |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2232 | do_c_expr_indent(); |
Bram Moolenaar | 49846fb | 2022-10-15 16:05:33 +0100 | [diff] [blame] | 2233 | else |
| 2234 | fixthisline(get_lisp_indent); |
| 2235 | } |
| 2236 | else if (cindent_on()) |
| 2237 | do_c_expr_indent(); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2238 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2239 | |
| 2240 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 2241 | /* |
| 2242 | * "indent()" function |
| 2243 | */ |
| 2244 | void |
| 2245 | f_indent(typval_T *argvars, typval_T *rettv) |
| 2246 | { |
| 2247 | linenr_T lnum; |
| 2248 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 2249 | if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL) |
| 2250 | return; |
| 2251 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2252 | lnum = tv_get_lnum(argvars); |
| 2253 | if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) |
| 2254 | rettv->vval.v_number = get_indent_lnum(lnum); |
| 2255 | else |
Bram Moolenaar | 8dac2ac | 2021-12-27 20:57:06 +0000 | [diff] [blame] | 2256 | { |
| 2257 | if (in_vim9script()) |
| 2258 | semsg(_(e_invalid_line_number_nr), lnum); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2259 | rettv->vval.v_number = -1; |
Bram Moolenaar | 8dac2ac | 2021-12-27 20:57:06 +0000 | [diff] [blame] | 2260 | } |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | /* |
| 2264 | * "lispindent(lnum)" function |
| 2265 | */ |
| 2266 | void |
| 2267 | f_lispindent(typval_T *argvars UNUSED, typval_T *rettv) |
| 2268 | { |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2269 | pos_T pos; |
| 2270 | linenr_T lnum; |
| 2271 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 2272 | if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL) |
| 2273 | return; |
| 2274 | |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2275 | pos = curwin->w_cursor; |
| 2276 | lnum = tv_get_lnum(argvars); |
| 2277 | if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) |
| 2278 | { |
| 2279 | curwin->w_cursor.lnum = lnum; |
| 2280 | rettv->vval.v_number = get_lisp_indent(); |
| 2281 | curwin->w_cursor = pos; |
| 2282 | } |
Bram Moolenaar | 8dac2ac | 2021-12-27 20:57:06 +0000 | [diff] [blame] | 2283 | else if (in_vim9script()) |
| 2284 | semsg(_(e_invalid_line_number_nr), lnum); |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2285 | else |
Bram Moolenaar | 14c01f8 | 2019-10-09 22:53:08 +0200 | [diff] [blame] | 2286 | rettv->vval.v_number = -1; |
| 2287 | } |
| 2288 | #endif |