blob: b2861e967483b5462fa9e11e6fc67ea0f0945ae9 [file] [log] [blame]
Bram Moolenaar4b471622019-01-31 13:48:09 +01001/* 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 Moolenaare677df82019-09-02 22:31:11 +020016#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 Moolenaarb7081e12021-09-04 18:47:28 +020021 * Return FAIL for an error.
Bram Moolenaare677df82019-09-02 22:31:11 +020022 */
23 int
24tabstop_set(char_u *var, int **array)
25{
Bram Moolenaarb7081e12021-09-04 18:47:28 +020026 int valcount = 1;
27 int t;
28 char_u *cp;
Bram Moolenaare677df82019-09-02 22:31:11 +020029
30 if (var[0] == NUL || (var[0] == '0' && var[1] == NUL))
31 {
32 *array = NULL;
Bram Moolenaarb7081e12021-09-04 18:47:28 +020033 return OK;
Bram Moolenaare677df82019-09-02 22:31:11 +020034 }
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 Moolenaar460ae5d2022-01-01 14:19:49 +000045 emsg(_(e_argument_must_be_positive));
Bram Moolenaare677df82019-09-02 22:31:11 +020046 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000047 semsg(_(e_invalid_argument_str), cp);
Bram Moolenaarb7081e12021-09-04 18:47:28 +020048 return FAIL;
Bram Moolenaare677df82019-09-02 22:31:11 +020049 }
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 Moolenaar436b5ad2021-12-31 22:49:24 +000059 semsg(_(e_invalid_argument_str), var);
Bram Moolenaarb7081e12021-09-04 18:47:28 +020060 return FAIL;
Bram Moolenaare677df82019-09-02 22:31:11 +020061 }
62
63 *array = ALLOC_MULT(int, valcount + 1);
64 if (*array == NULL)
Bram Moolenaarb7081e12021-09-04 18:47:28 +020065 return FAIL;
Bram Moolenaare677df82019-09-02 22:31:11 +020066 (*array)[0] = valcount;
67
68 t = 1;
69 for (cp = var; *cp != NUL;)
70 {
Bram Moolenaarb7081e12021-09-04 18:47:28 +020071 int n = atoi((char *)cp);
72
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +020073 // Catch negative values, overflow and ridiculous big values.
Bram Moolenaarfc88df42022-02-05 11:13:05 +000074 if (n <= 0 || n > TABSTOP_MAX)
Bram Moolenaarb7081e12021-09-04 18:47:28 +020075 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000076 semsg(_(e_invalid_argument_str), cp);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +000077 VIM_CLEAR(*array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +020078 return FAIL;
79 }
80 (*array)[t++] = n;
81 while (*cp != NUL && *cp != ',')
Bram Moolenaare677df82019-09-02 22:31:11 +020082 ++cp;
83 if (*cp != NUL)
84 ++cp;
85 }
86
Bram Moolenaarb7081e12021-09-04 18:47:28 +020087 return OK;
Bram Moolenaare677df82019-09-02 22:31:11 +020088}
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
96tabstop_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 Johnson88d4f252024-06-01 20:51:33 +0200126 *
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 Moolenaare677df82019-09-02 22:31:11 +0200131 */
132 int
Gary Johnson88d4f252024-06-01 20:51:33 +0200133tabstop_at(colnr_T col, int ts, int *vts, int left)
Bram Moolenaare677df82019-09-02 22:31:11 +0200134{
Gary Johnson88d4f252024-06-01 20:51:33 +0200135 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 Moolenaare677df82019-09-02 22:31:11 +0200142
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 Johnson88d4f252024-06-01 20:51:33 +0200152 // 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 Moolenaare677df82019-09-02 22:31:11 +0200162 break;
163 }
164 }
Gary Johnson88d4f252024-06-01 20:51:33 +0200165 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 Moolenaare677df82019-09-02 22:31:11 +0200168 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
177tabstop_start(colnr_T col, int ts, int *vts)
178{
179 int tabcount;
180 colnr_T tabcol = 0;
181 int t;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100182 int excess;
Bram Moolenaare677df82019-09-02 22:31:11 +0200183
184 if (vts == NULL || vts[0] == 0)
zeertzjq8ede7a02024-03-28 10:30:08 +0100185 return col - col % ts;
Bram Moolenaare677df82019-09-02 22:31:11 +0200186
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];
zeertzjq8ede7a02024-03-28 10:30:08 +0100196 return col - (col - excess) % vts[tabcount];
Bram Moolenaare677df82019-09-02 22:31:11 +0200197}
198
199/*
200 * Find the number of tabs and spaces necessary to get from one column
201 * to another.
202 */
203 void
204tabstop_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 Moolenaar14c01f82019-10-09 22:53:08 +0200283 static int
Bram Moolenaare677df82019-09-02 22:31:11 +0200284tabstop_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 Moolenaar14c01f82019-10-09 22:53:08 +0200302# if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaare677df82019-09-02 22:31:11 +0200303/*
304 * Copy a tabstop array, allocating space for the new array.
305 */
306 int *
307tabstop_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 Moolenaar14c01f82019-10-09 22:53:08 +0200320# endif
Bram Moolenaare677df82019-09-02 22:31:11 +0200321
322/*
323 * Return a count of the number of tabstops.
324 */
325 int
326tabstop_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
335tabstop_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
347get_sw_value(buf_T *buf)
348{
Gary Johnson88d4f252024-06-01 20:51:33 +0200349 return get_sw_value_col(buf, 0, FALSE);
Bram Moolenaare677df82019-09-02 22:31:11 +0200350}
351
352/*
353 * Idem, using "pos".
354 */
355 static long
Gary Johnson88d4f252024-06-01 20:51:33 +0200356get_sw_value_pos(buf_T *buf, pos_T *pos, int left)
Bram Moolenaare677df82019-09-02 22:31:11 +0200357{
358 pos_T save_cursor = curwin->w_cursor;
359 long sw_value;
360
361 curwin->w_cursor = *pos;
Gary Johnson88d4f252024-06-01 20:51:33 +0200362 sw_value = get_sw_value_col(buf, get_nolist_virtcol(), left);
Bram Moolenaare677df82019-09-02 22:31:11 +0200363 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 Johnson88d4f252024-06-01 20:51:33 +0200371get_sw_value_indent(buf_T *buf, int left)
Bram Moolenaare677df82019-09-02 22:31:11 +0200372{
373 pos_T pos = curwin->w_cursor;
374
375 pos.col = getwhitecols_curline();
Gary Johnson88d4f252024-06-01 20:51:33 +0200376 return get_sw_value_pos(buf, &pos, left);
Bram Moolenaare677df82019-09-02 22:31:11 +0200377}
378
379/*
380 * Idem, using virtual column "col".
381 */
382 long
Gary Johnson88d4f252024-06-01 20:51:33 +0200383get_sw_value_col(buf_T *buf, colnr_T col UNUSED, int left UNUSED)
Bram Moolenaare677df82019-09-02 22:31:11 +0200384{
385 return buf->b_p_sw ? buf->b_p_sw :
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200386#ifdef FEAT_VARTABS
Gary Johnson88d4f252024-06-01 20:51:33 +0200387 tabstop_at(col, buf->b_p_ts, buf->b_p_vts_array, left);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200388#else
Bram Moolenaare677df82019-09-02 22:31:11 +0200389 buf->b_p_ts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200390#endif
Bram Moolenaare677df82019-09-02 22:31:11 +0200391}
392
393/*
394 * Return the effective softtabstop value for the current buffer, using the
395 * 'shiftwidth' value when 'softtabstop' is negative.
396 */
397 long
398get_sts_value(void)
399{
400 return curbuf->b_p_sts < 0 ? get_sw_value(curbuf) : curbuf->b_p_sts;
401}
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200402
403/*
404 * Count the size (in window cells) of the indent in the current line.
405 */
406 int
407get_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
421get_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
437get_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),
zeertzjq07146ad2022-12-19 15:51:44 +0000441 (int)buf->b_p_ts, buf->b_p_vts_array, FALSE);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200442# 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
453get_indent_str(
454 char_u *ptr,
455 int ts,
zeertzjqefabd7c2024-02-11 17:16:19 +0100456 int no_ts) // if TRUE, count a tab as ^I
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200457{
458 int count = 0;
459
460 for ( ; *ptr; ++ptr)
461 {
zeertzjqefabd7c2024-02-11 17:16:19 +0100462 if (*ptr == TAB) // count a tab for what it is worth
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200463 {
zeertzjqefabd7c2024-02-11 17:16:19 +0100464 if (!no_ts)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200465 count += ts - (count % ts);
466 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200467 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.
zeertzjqefabd7c2024-02-11 17:16:19 +0100481 * If "no_ts" is TRUE, count a tab as ^I.
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200482 */
483 int
zeertzjqefabd7c2024-02-11 17:16:19 +0100484get_indent_str_vtab(char_u *ptr, int ts, int *vts, int no_ts)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200485{
486 int count = 0;
487
488 for ( ; *ptr; ++ptr)
489 {
490 if (*ptr == TAB) // count a tab for what it is worth
491 {
zeertzjqefabd7c2024-02-11 17:16:19 +0100492 if (!no_ts)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200493 count += tabstop_padding(count, ts, vts);
494 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200495 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
517set_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 Marriotteac45c52025-04-21 11:01:53 +0200527 int line_len; // size of the line (including the NUL)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200528 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 Marriotteac45c52025-04-21 11:01:53 +0200543 line_len = ml_get_curline_len() + 1;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200544
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 Marriotteac45c52025-04-21 11:01:53 +0200664 {
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200665 p = skipwhite(p);
John Marriotteac45c52025-04-21 11:01:53 +0200666 line_len -= (int)(p - oldline);
667 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200668
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 Moolenaarcf306432020-06-29 20:40:37 +0200788 colnr_T old_offset = (colnr_T)(p - oldline);
789 colnr_T new_offset = (colnr_T)(s - newline);
790
791 // this may free "newline"
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200792 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 Moolenaarcf306432020-06-29 20:40:37 +0200799 if (saved_cursor.col >= old_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200800 // cursor was after the indent, adjust for the number of
801 // bytes added/removed
Bram Moolenaarcf306432020-06-29 20:40:37 +0200802 saved_cursor.col += ind_len - old_offset;
803 else if (saved_cursor.col >= new_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200804 // 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 Moolenaarcf306432020-06-29 20:40:37 +0200806 saved_cursor.col = new_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200807 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100808#ifdef FEAT_PROP_POPUP
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200809 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200810 int added = ind_len - old_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200811
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 Moolenaar8a77d202022-08-15 16:29:37 +0100816 added > 0 ? old_offset : (colnr_T)ind_len,
817 added, APC_INDENT);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200818 }
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
835get_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 Moolenaar24959102022-05-07 20:01:16 +0100848 if ((State & MODE_INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200849 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(&regmatch, 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/*
Millyb38700a2024-10-22 22:59:39 +0200875 * Check "briopt" as 'breakindentopt' and update the members of "wp".
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100876 * This is called when 'breakindentopt' is changed and when a window is
877 * initialized.
Millyb38700a2024-10-22 22:59:39 +0200878 * Returns FAIL for failure, OK otherwise.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100879 */
880 int
Millyb38700a2024-10-22 22:59:39 +0200881briopt_check(
882 char_u *briopt, // when NULL: use "wp->w_p_briopt"
883 win_T *wp) // when NULL: only check "briopt"
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100884{
885 char_u *p;
886 int bri_shift = 0;
887 long bri_min = 20;
888 int bri_sbr = FALSE;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200889 int bri_list = 0;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100890 int bri_vcol = 0;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100891
Millyb38700a2024-10-22 22:59:39 +0200892 if (briopt != NULL)
893 p = briopt;
894 else
895 p = wp->w_p_briopt;
896
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100897 while (*p != NUL)
898 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200899 // Note: Keep this in sync with p_briopt_values
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100900 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 Brabandt4a0b85a2021-07-14 20:00:27 +0200916 else if (STRNCMP(p, "list:", 5) == 0)
917 {
918 p += 5;
919 bri_list = getdigits(&p);
920 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100921 else if (STRNCMP(p, "column:", 7) == 0)
922 {
923 p += 7;
924 bri_vcol = getdigits(&p);
925 }
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100926 if (*p != ',' && *p != NUL)
927 return FAIL;
928 if (*p == ',')
929 ++p;
930 }
931
Millyb38700a2024-10-22 22:59:39 +0200932 if (wp == NULL)
933 return OK;
934
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100935 wp->w_briopt_shift = bri_shift;
936 wp->w_briopt_min = bri_min;
937 wp->w_briopt_sbr = bri_sbr;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200938 wp->w_briopt_list = bri_list;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100939 wp->w_briopt_vcol = bri_vcol;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100940
941 return OK;
942}
943
944/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200945 * 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
950get_breakindent_win(
951 win_T *wp,
952 char_u *line) // start of the line
953{
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000954 static int prev_indent = 0; // cached indent value
955 static long prev_ts = 0L; // cached tabstop value
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200956# ifdef FEAT_VARTABS
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000957 static int *prev_vts = NULL; // cached vartabs values
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200958# endif
zeertzjqefabd7c2024-02-11 17:16:19 +0100959 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 Moolenaarb2d85e32022-01-07 16:55:32 +0000963 static int prev_listopt = 0; // cached w_p_briopt_list value
zeertzjqefabd7c2024-02-11 17:16:19 +0100964 static int prev_no_ts = FALSE; // cached no_ts value
965 static unsigned prev_dy_uhex = 0; // cached 'display' "uhex" value
Christian Brabandtc53b4672022-01-15 10:01:05 +0000966 // cached formatlistpat value
967 static char_u *prev_flp = NULL;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200968 int bri = 0;
969 // window width minus window margin space, i.e. what rests for text
970 const int eff_wwidth = wp->w_width
zeertzjqf0a9d652024-02-12 22:53:20 +0100971 - win_col_off(wp) + win_col_off2(wp);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200972
zeertzjqefabd7c2024-02-11 17:16:19 +0100973 // 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 Moolenaarc2a79b82022-07-01 13:15:35 +0100986 if (prev_fnum != wp->w_buffer->b_fnum
987 || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200988# ifdef FEAT_VARTABS
989 || prev_vts != wp->w_buffer->b_p_vts_array
990# endif
zeertzjqefabd7c2024-02-11 17:16:19 +0100991 || 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 Moolenaar14c01f82019-10-09 22:53:08 +0200998 )
999 {
Bram Moolenaarc2a79b82022-07-01 13:15:35 +01001000 prev_fnum = wp->w_buffer->b_fnum;
1001 vim_free(prev_line);
1002 prev_line = vim_strsave(line);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001003 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001004# ifdef FEAT_VARTABS
1005 prev_vts = wp->w_buffer->b_p_vts_array;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001006 if (wp->w_briopt_vcol == 0)
1007 prev_indent = get_indent_str_vtab(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001008 (int)wp->w_buffer->b_p_ts,
zeertzjqefabd7c2024-02-11 17:16:19 +01001009 wp->w_buffer->b_p_vts_array, no_ts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001010# else
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001011 if (wp->w_briopt_vcol == 0)
1012 prev_indent = get_indent_str(line,
zeertzjqd9be94c2024-07-14 10:20:20 +02001013 (int)wp->w_buffer->b_p_ts, no_ts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001014# endif
zeertzjqefabd7c2024-02-11 17:16:19 +01001015 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001016 prev_listopt = wp->w_briopt_list;
Christian Brabandtc53b4672022-01-15 10:01:05 +00001017 prev_list = 0;
zeertzjqefabd7c2024-02-11 17:16:19 +01001018 prev_no_ts = no_ts;
1019 prev_dy_uhex = (dy_flags & DY_UHEX);
Christian Brabandtc53b4672022-01-15 10:01:05 +00001020 vim_free(prev_flp);
1021 prev_flp = vim_strsave(get_flp_value(wp->w_buffer));
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001022 // add additional indent for numbered lists
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001023 if (wp->w_briopt_list != 0 && wp->w_briopt_vcol == 0)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001024 {
1025 regmatch_T regmatch;
1026
Christian Brabandtc53b4672022-01-15 10:01:05 +00001027 regmatch.regprog = vim_regcomp(prev_flp,
1028 RE_MAGIC + RE_STRING + RE_AUTO + RE_STRICT);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001029
1030 if (regmatch.regprog != NULL)
1031 {
1032 regmatch.rm_ic = FALSE;
1033 if (vim_regexec(&regmatch, line, 0))
1034 {
1035 if (wp->w_briopt_list > 0)
1036 prev_list = wp->w_briopt_list;
1037 else
zeertzjq61a6ac42024-09-07 11:23:54 +02001038 {
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 Moolenaarb2d85e32022-01-07 16:55:32 +00001053 }
1054 vim_regfree(regmatch.regprog);
1055 }
1056 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001057 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001058 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 Moolenaar14c01f82019-10-09 22:53:08 +02001066
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001067 // Add offset for number column, if 'n' is in 'cpoptions'
1068 bri += win_col_off2(wp);
1069
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001070 // add additional indent for numbered lists
Maxim Kim11916722022-09-02 14:08:53 +01001071 if (wp->w_briopt_list > 0)
1072 bri += prev_list;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001073
Maxim Kimf674b352021-07-22 11:46:59 +02001074 // 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 Moolenaar14c01f82019-10-09 22:53:08 +02001079 // never indent past left window margin
1080 if (bri < 0)
1081 bri = 0;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001082
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001083 // always leave at least bri_min characters on the left,
1084 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001085 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 Moolenaar14c01f82019-10-09 22:53:08 +02001088
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
1100inindent(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 Moolenaar14c01f82019-10-09 22:53:08 +02001113/*
1114 * op_reindent - handle reindenting a block of lines.
1115 */
1116 void
1117op_reindent(oparg_T *oap, int (*how)(void))
1118{
Bram Moolenaar4c84dd32022-04-20 10:22:54 +01001119 long i = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001120 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 Moolenaar108010a2021-06-27 22:03:33 +02001129 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001130 return;
1131 }
1132
Bram Moolenaare4686982022-04-19 18:28:45 +01001133 // 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 Moolenaar14c01f82019-10-09 22:53:08 +02001138 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001139 // it's a slow thing to do, so give feedback so there's no worry
1140 // that the computer's just hung.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001141
Bram Moolenaare4686982022-04-19 18:28:45 +01001142 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 Moolenaare4686982022-04-19 18:28:45 +01001149 if (i != oap->line_count - 1 || oap->line_count == 1
1150 || how != get_lisp_indent)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001151 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001152 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 Moolenaar14c01f82019-10-09 22:53:08 +02001165 }
Bram Moolenaare4686982022-04-19 18:28:45 +01001166 ++curwin->w_cursor.lnum;
1167 curwin->w_cursor.col = 0; // make sure it's valid
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001168 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001169
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 Moolenaara4d158b2022-08-14 14:17:45 +01001182 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001183
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 Moolenaare1004402020-10-24 20:49:43 +02001190 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001191 {
1192 // set '[ and '] marks
1193 curbuf->b_op_start = oap->start;
1194 curbuf->b_op_end = oap->end;
1195 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001196}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001197
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001198/*
1199 * Return TRUE if lines starting with '#' should be left aligned.
1200 */
1201 int
1202preprocs_left(void)
1203{
1204 return
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001205 (curbuf->b_p_si && !curbuf->b_p_cin) ||
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001206 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1207 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001208 ;
1209}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001210
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001211/*
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001212 * Return TRUE if the conditions are OK for smart indenting.
1213 */
1214 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001215may_do_si(void)
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001216{
1217 return curbuf->b_p_si
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001218 && !curbuf->b_p_cin
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001219# ifdef FEAT_EVAL
1220 && *curbuf->b_p_inde == NUL
1221# endif
1222 && !p_paste;
1223}
1224
1225/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001226 * Try to do some very smart auto-indenting.
1227 * Used when inserting a "normal" character.
1228 */
1229 void
1230ins_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 Moolenaar2e444bb2022-05-14 12:54:23 +01001238 if (((did_si || can_si_back) && c == '{')
1239 || (can_si && c == '}' && inindent(0)))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001240 {
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 Moolenaarde5cf282022-05-14 11:52:23 +01001293 if (curwin->w_cursor.col > 0 && can_si && c == '#' && inindent(0))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001294 {
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 Moolenaar14c01f82019-10-09 22:53:08 +02001304
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
1314change_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 Moolenaar24959102022-05-07 20:01:16 +01001333 // MODE_VREPLACE state needs to know what the line was like before changing
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001334 if (State & VREPLACE_FLAG)
1335 {
John Marriotteac45c52025-04-21 11:01:53 +02001336 orig_line = vim_strnsave(ml_get_curline(), ml_get_curline_len()); // Deal with NULL below
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001337 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 Moolenaar702bd6c2022-09-14 16:09:57 +01001343#ifdef FEAT_PROP_POPUP
1344 ignore_text_props = TRUE;
1345#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001346 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 Moolenaar24959102022-05-07 20:01:16 +01001378 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001379 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 Moolenaar24959102022-05-07 20:01:16 +01001399 else if (!(State & MODE_INSERT))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001400 new_cursor_col = curwin->w_cursor.col;
1401 else
1402 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001403 chartabsize_T cts;
1404
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001405 // 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 Moolenaar7f9969c2022-07-25 18:13:54 +01001410 last_vcol = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001411 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001412 init_chartabsize_arg(&cts, curwin, 0, 0, ptr, ptr);
1413 while (cts.cts_vcol <= (int)curwin->w_virtcol)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001414 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001415 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 Moolenaar4e889f92022-02-21 19:36:12 +00001419 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001420 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001421 }
1422 vcol = last_vcol;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001423 new_cursor_col = cts.cts_ptr - cts.cts_line;
1424 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001425
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 Marriottf4b36412025-02-23 09:09:59 +01001435 size_t ptrlen;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001436 new_cursor_col += i;
1437 ptr[i] = NUL;
John Marriottf4b36412025-02-23 09:09:59 +01001438 ptrlen = i;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001439 while (--i >= 0)
1440 ptr[i] = ' ';
John Marriottf4b36412025-02-23 09:09:59 +01001441 ins_str(ptr, ptrlen);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001442 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 Moolenaar24959102022-05-07 20:01:16 +01001461 if (State & MODE_INSERT)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001462 {
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 Moolenaar24959102022-05-07 20:01:16 +01001476 // 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 Moolenaar14c01f82019-10-09 22:53:08 +02001479 // 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 Moolenaar702bd6c2022-09-14 16:09:57 +01001499#ifdef FEAT_PROP_POPUP
1500 ignore_text_props = FALSE;
1501#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001502
Bram Moolenaar24959102022-05-07 20:01:16 +01001503 // 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 Moolenaar14c01f82019-10-09 22:53:08 +02001506 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 Marriotteac45c52025-04-21 11:01:53 +02001514 new_line = vim_strnsave(ml_get_curline(), ml_get_curline_len());
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001515 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
1541copy_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.
zeertzjq94b7c322024-03-12 21:50:32 +01001661 line_len = ml_get_curline_len() + 1;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001662 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 Moolenaar308660b2022-06-16 12:10:48 +01001681 * Give a "resulting text too long" error and maybe set got_int.
1682 */
1683 static void
1684emsg_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 Moolenaar14c01f82019-10-09 22:53:08 +02001695 * ":retab".
1696 */
1697 void
1698ex_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 Moolenaaraa2f0ee2019-12-21 18:47:26 +01001707 long start_col = 0; // For start of white-space string
1708 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001709 long old_len;
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001710 long new_len;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001711 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001712 char_u *new_line = (char_u *)1; // init to non-NULL
1713 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001714#ifdef FEAT_VARTABS
1715 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001716 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001717#else
1718 int temp;
1719 int new_ts;
1720#endif
1721 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001722 linenr_T first_line = 0; // first changed line
1723 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001724
1725 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001726 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001727
1728#ifdef FEAT_VARTABS
1729 new_ts_str = eap->arg;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02001730 if (tabstop_set(eap->arg, &new_vts_array) == FAIL)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001731 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 Moolenaar2ddb89f2021-09-04 21:20:41 +02001746 ptr = eap->arg;
1747 new_ts = getdigits(&ptr);
1748 if (new_ts < 0 && *eap->arg == '-')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001749 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001750 emsg(_(e_argument_must_be_positive));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001751 return;
1752 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00001753 if (new_ts < 0 || new_ts > TABSTOP_MAX)
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001754 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001755 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001756 return;
1757 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001758 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 Marriotteac45c52025-04-21 11:01:53 +02001764 old_len = ml_get_len(lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001765 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 Moolenaaraa2f0ee2019-12-21 18:47:26 +01001774 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001775 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 Moolenaaraa2f0ee2019-12-21 18:47:26 +01001787 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001788
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001789 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001790 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 Moolenaaraa2f0ee2019-12-21 18:47:26 +01001821 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001822 break;
1823 }
1824 }
1825
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001826 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001827 len = num_spaces + num_tabs;
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001828 new_len = old_len - col + start_col + len + 1;
Bram Moolenaar45491662022-02-12 21:59:51 +00001829 if (new_len <= 0 || new_len >= MAXCOL)
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001830 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001831 emsg_text_too_long();
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001832 break;
1833 }
1834 new_line = alloc(new_len);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001835 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 Moolenaar0dcd39b2021-02-03 19:44:25 +01001844 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 Moolenaar14c01f82019-10-09 22:53:08 +02001847 if (first_line == 0)
1848 first_line = lnum;
1849 last_line = lnum;
1850 ptr = new_line;
John Marriotteac45c52025-04-21 11:01:53 +02001851 old_len = new_len - 1;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001852 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 Moolenaar6e287032022-02-12 15:42:18 +00001861 if (vcol >= MAXCOL)
1862 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001863 emsg_text_too_long();
Bram Moolenaar6e287032022-02-12 15:42:18 +00001864 break;
1865 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001866 if (has_mbyte)
1867 col += (*mb_ptr2len)(ptr + col);
1868 else
1869 ++col;
1870 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001871 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001872 break;
1873 line_breakcheck();
1874 }
1875 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001876 emsg(_(e_interrupted));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001877
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 Moolenaaraa2f0ee2019-12-21 18:47:26 +01001884 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001885 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001886 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001887 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001888 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001889 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001890#else
1891 if (curbuf->b_p_ts != new_ts)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001892 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001893#endif
1894 if (first_line != 0)
1895 changed_lines(first_line, 0, last_line + 1, 0L);
1896
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001897 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001898
1899#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001900 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001901 {
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 Moolenaar8e145b82022-05-21 20:17:31 +01001930#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001931/*
1932 * Get indent level from 'indentexpr'.
1933 */
1934 int
1935get_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 Moolenaar28e60cc2022-01-22 20:32:00 +00001945 sctx_T save_sctx = current_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001946
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;
zeertzjqcfe45652022-05-27 17:26:55 +01001955 ++textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001956 current_sctx = curbuf->b_p_script_ctx[BV_INDE];
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001957
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 Moolenaar3292a222022-10-01 20:17:17 +01001963 indent = (int)eval_to_number(inde_copy, TRUE);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001964 vim_free(inde_copy);
1965 }
1966
1967 if (use_sandbox)
1968 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001969 --textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001970 current_sctx = save_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001971
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 Moolenaar24959102022-05-07 20:01:16 +01001976 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001977 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 Moolenaar620c9592021-07-31 21:32:31 +02001983 // 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 Moolenaar14c01f82019-10-09 22:53:08 +02001990 // 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 Moolenaar14c01f82019-10-09 22:53:08 +02001998 static int
1999lisp_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 Marriotteac45c52025-04-21 11:01:53 +02002007 len = copy_option_part(&word, buf, LSIZE, ",");
Bram Moolenaard26c5802022-10-13 12:30:08 +01002008 if (STRNCMP(buf, p, len) == 0 && IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002009 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
2031get_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 Moolenaar0e8e9382022-06-18 12:51:11 +01002097 if (*that == NUL)
2098 break;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002099 }
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 Moolenaar7f9969c2022-07-25 18:13:54 +01002124 char_u *line = that;
2125 chartabsize_T cts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002126
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002127 init_chartabsize_arg(&cts, curwin, pos->lnum, 0, line, line);
2128 while (*cts.cts_ptr != NUL && col > 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002129 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002130 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002131 col--;
2132 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002133 amount = cts.cts_vcol;
2134 that = cts.cts_ptr;
2135 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002136
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 Moolenaar8eba2bd2022-06-22 19:59:28 +01002148 if (*that != NUL)
2149 {
2150 that++;
2151 amount++;
2152 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002153 firsttry = amount;
2154
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002155 init_chartabsize_arg(&cts, curwin, (colnr_T)(that - line),
2156 amount, line, that);
2157 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002158 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002159 cts.cts_vcol += lbr_chartabsize(&cts);
2160 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002161 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002162 that = cts.cts_ptr;
2163 amount = cts.cts_vcol;
2164 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002165
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 Moolenaar7f9969c2022-07-25 18:13:54 +01002176 init_chartabsize_arg(&cts, curwin,
2177 (colnr_T)(that - line), amount, line, that);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002178 if (vi_lisp
2179 || (*that != '"'
2180 && *that != '\''
2181 && *that != '#'
2182 && (*that < '0' || *that > '9')))
2183 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002184 while (*cts.cts_ptr
2185 && (!VIM_ISWHITE(*cts.cts_ptr)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002186 || quotecount
2187 || parencount)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002188 && (!((*cts.cts_ptr == '('
2189 || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002190 && !quotecount
2191 && !parencount
2192 && vi_lisp)))
2193 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002194 if (*cts.cts_ptr == '"')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002195 quotecount = !quotecount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002196 if ((*cts.cts_ptr == '(' || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002197 && !quotecount)
2198 ++parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002199 if ((*cts.cts_ptr == ')' || *cts.cts_ptr == ']')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002200 && !quotecount)
2201 --parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002202 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 Moolenaar14c01f82019-10-09 22:53:08 +02002206 }
2207 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002208 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002209 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002210 cts.cts_vcol += lbr_chartabsize(&cts);
2211 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002212 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002213 that = cts.cts_ptr;
2214 amount = cts.cts_vcol;
2215 clear_chartabsize_arg(&cts);
2216
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002217 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 Moolenaar14c01f82019-10-09 22:53:08 +02002231
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002232/*
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
2240fixthisline(int (*get_the_indent)(void))
2241{
2242 int amount = get_the_indent();
2243
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002244 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 Moolenaar14c01f82019-10-09 22:53:08 +02002250}
2251
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002252/*
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002253 * Return TRUE if 'indentexpr' should be used for Lisp indenting.
2254 * Caller may want to check 'autoindent'.
2255 */
2256 int
2257use_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 Moolenaar436b5ad2021-12-31 22:49:24 +00002269 * Fix indent for 'lisp' and 'cindent'.
2270 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002271 void
2272fix_indent(void)
2273{
2274 if (p_paste)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002275 return; // no auto-indenting when 'paste' is set
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002276 if (curbuf->b_p_lisp && curbuf->b_p_ai)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002277 {
2278 if (use_indentexpr_for_lisp())
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002279 do_c_expr_indent();
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002280 else
2281 fixthisline(get_lisp_indent);
2282 }
2283 else if (cindent_on())
2284 do_c_expr_indent();
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002285}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002286
2287#if defined(FEAT_EVAL) || defined(PROTO)
2288/*
2289 * "indent()" function
2290 */
2291 void
2292f_indent(typval_T *argvars, typval_T *rettv)
2293{
2294 linenr_T lnum;
2295
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002296 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2297 return;
2298
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002299 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 Moolenaar8dac2ac2021-12-27 20:57:06 +00002303 {
2304 if (in_vim9script())
2305 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002306 rettv->vval.v_number = -1;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002307 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002308}
2309
2310/*
2311 * "lispindent(lnum)" function
2312 */
2313 void
2314f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2315{
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002316 pos_T pos;
2317 linenr_T lnum;
2318
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002319 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2320 return;
2321
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002322 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 Moolenaar8dac2ac2021-12-27 20:57:06 +00002330 else if (in_vim9script())
2331 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002332 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002333 rettv->vval.v_number = -1;
2334}
2335#endif