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