blob: 0d6fadb26dcbe8bbe18494ead989480f54558639 [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
527 int line_len;
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();
543
544 // Calculate the buffer size for the new indent, and check to see if it
545 // isn't already set
546
547 // if 'expandtab' isn't set: use TABs; if both 'expandtab' and
548 // 'preserveindent' are set count the number of characters at the
549 // beginning of the line to be copied
550 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
551 {
552 // If 'preserveindent' is set then reuse as much as possible of
553 // the existing indent structure for the new indent
554 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
555 {
556 ind_done = 0;
557
558 // count as many characters as we can use
559 while (todo > 0 && VIM_ISWHITE(*p))
560 {
561 if (*p == TAB)
562 {
563#ifdef FEAT_VARTABS
564 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
565 curbuf->b_p_vts_array);
566#else
567 tab_pad = (int)curbuf->b_p_ts
568 - (ind_done % (int)curbuf->b_p_ts);
569#endif
570 // stop if this tab will overshoot the target
571 if (todo < tab_pad)
572 break;
573 todo -= tab_pad;
574 ++ind_len;
575 ind_done += tab_pad;
576 }
577 else
578 {
579 --todo;
580 ++ind_len;
581 ++ind_done;
582 }
583 ++p;
584 }
585
586#ifdef FEAT_VARTABS
587 // These diverge from this point.
588 ind_col = ind_done;
589#endif
590 // Set initial number of whitespace chars to copy if we are
591 // preserving indent but expandtab is set
592 if (curbuf->b_p_et)
593 orig_char_len = ind_len;
594
595 // Fill to next tabstop with a tab, if possible
596#ifdef FEAT_VARTABS
597 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
598 curbuf->b_p_vts_array);
599#else
600 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
601#endif
602 if (todo >= tab_pad && orig_char_len == -1)
603 {
604 doit = TRUE;
605 todo -= tab_pad;
606 ++ind_len;
607 // ind_done += tab_pad;
608#ifdef FEAT_VARTABS
609 ind_col += tab_pad;
610#endif
611 }
612 }
613
614 // count tabs required for indent
615#ifdef FEAT_VARTABS
616 for (;;)
617 {
618 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
619 curbuf->b_p_vts_array);
620 if (todo < tab_pad)
621 break;
622 if (*p != TAB)
623 doit = TRUE;
624 else
625 ++p;
626 todo -= tab_pad;
627 ++ind_len;
628 ind_col += tab_pad;
629 }
630#else
631 while (todo >= (int)curbuf->b_p_ts)
632 {
633 if (*p != TAB)
634 doit = TRUE;
635 else
636 ++p;
637 todo -= (int)curbuf->b_p_ts;
638 ++ind_len;
639 // ind_done += (int)curbuf->b_p_ts;
640 }
641#endif
642 }
643 // count spaces required for indent
644 while (todo > 0)
645 {
646 if (*p != ' ')
647 doit = TRUE;
648 else
649 ++p;
650 --todo;
651 ++ind_len;
652 // ++ind_done;
653 }
654
655 // Return if the indent is OK already.
656 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
657 return FALSE;
658
659 // Allocate memory for the new line.
660 if (flags & SIN_INSERT)
661 p = oldline;
662 else
663 p = skipwhite(p);
664 line_len = (int)STRLEN(p) + 1;
665
666 // If 'preserveindent' and 'expandtab' are both set keep the original
667 // characters and allocate accordingly. We will fill the rest with spaces
668 // after the if (!curbuf->b_p_et) below.
669 if (orig_char_len != -1)
670 {
671 newline = alloc(orig_char_len + size - ind_done + line_len);
672 if (newline == NULL)
673 return FALSE;
674 todo = size - ind_done;
675 ind_len = orig_char_len + todo; // Set total length of indent in
676 // characters, which may have been
677 // undercounted until now
678 p = oldline;
679 s = newline;
680 while (orig_char_len > 0)
681 {
682 *s++ = *p++;
683 orig_char_len--;
684 }
685
686 // Skip over any additional white space (useful when newindent is less
687 // than old)
688 while (VIM_ISWHITE(*p))
689 ++p;
690
691 }
692 else
693 {
694 todo = size;
695 newline = alloc(ind_len + line_len);
696 if (newline == NULL)
697 return FALSE;
698 s = newline;
699 }
700
701 // Put the characters in the new line.
702 // if 'expandtab' isn't set: use TABs
703 if (!curbuf->b_p_et)
704 {
705 // If 'preserveindent' is set then reuse as much as possible of
706 // the existing indent structure for the new indent
707 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
708 {
709 p = oldline;
710 ind_done = 0;
711
712 while (todo > 0 && VIM_ISWHITE(*p))
713 {
714 if (*p == TAB)
715 {
716#ifdef FEAT_VARTABS
717 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
718 curbuf->b_p_vts_array);
719#else
720 tab_pad = (int)curbuf->b_p_ts
721 - (ind_done % (int)curbuf->b_p_ts);
722#endif
723 // stop if this tab will overshoot the target
724 if (todo < tab_pad)
725 break;
726 todo -= tab_pad;
727 ind_done += tab_pad;
728 }
729 else
730 {
731 --todo;
732 ++ind_done;
733 }
734 *s++ = *p++;
735 }
736
737 // Fill to next tabstop with a tab, if possible
738#ifdef FEAT_VARTABS
739 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
740 curbuf->b_p_vts_array);
741#else
742 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
743#endif
744 if (todo >= tab_pad)
745 {
746 *s++ = TAB;
747 todo -= tab_pad;
748#ifdef FEAT_VARTABS
749 ind_done += tab_pad;
750#endif
751 }
752
753 p = skipwhite(p);
754 }
755
756#ifdef FEAT_VARTABS
757 for (;;)
758 {
759 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
760 curbuf->b_p_vts_array);
761 if (todo < tab_pad)
762 break;
763 *s++ = TAB;
764 todo -= tab_pad;
765 ind_done += tab_pad;
766 }
767#else
768 while (todo >= (int)curbuf->b_p_ts)
769 {
770 *s++ = TAB;
771 todo -= (int)curbuf->b_p_ts;
772 }
773#endif
774 }
775 while (todo > 0)
776 {
777 *s++ = ' ';
778 --todo;
779 }
780 mch_memmove(s, p, (size_t)line_len);
781
782 // Replace the line (unless undo fails).
783 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
784 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200785 colnr_T old_offset = (colnr_T)(p - oldline);
786 colnr_T new_offset = (colnr_T)(s - newline);
787
788 // this may free "newline"
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200789 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
790 if (flags & SIN_CHANGED)
791 changed_bytes(curwin->w_cursor.lnum, 0);
792
793 // Correct saved cursor position if it is in this line.
794 if (saved_cursor.lnum == curwin->w_cursor.lnum)
795 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200796 if (saved_cursor.col >= old_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200797 // cursor was after the indent, adjust for the number of
798 // bytes added/removed
Bram Moolenaarcf306432020-06-29 20:40:37 +0200799 saved_cursor.col += ind_len - old_offset;
800 else if (saved_cursor.col >= new_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200801 // cursor was in the indent, and is now after it, put it back
802 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaarcf306432020-06-29 20:40:37 +0200803 saved_cursor.col = new_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200804 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100805#ifdef FEAT_PROP_POPUP
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200806 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200807 int added = ind_len - old_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200808
809 // When increasing indent this behaves like spaces were inserted at
810 // the old indent, when decreasing indent it behaves like spaces
811 // were deleted at the new indent.
812 adjust_prop_columns(curwin->w_cursor.lnum,
Bram Moolenaar8a77d202022-08-15 16:29:37 +0100813 added > 0 ? old_offset : (colnr_T)ind_len,
814 added, APC_INDENT);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200815 }
816#endif
817 retval = TRUE;
818 }
819 else
820 vim_free(newline);
821
822 curwin->w_cursor.col = ind_len;
823 return retval;
824}
825
826/*
827 * Return the indent of the current line after a number. Return -1 if no
828 * number was found. Used for 'n' in 'formatoptions': numbered list.
829 * Since a pattern is used it can actually handle more than numbers.
830 */
831 int
832get_number_indent(linenr_T lnum)
833{
834 colnr_T col;
835 pos_T pos;
836
837 regmatch_T regmatch;
838 int lead_len = 0; // length of comment leader
839
840 if (lnum > curbuf->b_ml.ml_line_count)
841 return -1;
842 pos.lnum = 0;
843
844 // In format_lines() (i.e. not insert mode), fo+=q is needed too...
Bram Moolenaar24959102022-05-07 20:01:16 +0100845 if ((State & MODE_INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200846 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
847
848 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
849 if (regmatch.regprog != NULL)
850 {
851 regmatch.rm_ic = FALSE;
852
853 // vim_regexec() expects a pointer to a line. This lets us
854 // start matching for the flp beyond any comment leader...
855 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
856 {
857 pos.lnum = lnum;
858 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
859 pos.coladd = 0;
860 }
861 vim_regfree(regmatch.regprog);
862 }
863
864 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
865 return -1;
866 getvcol(curwin, &pos, &col, NULL, NULL);
867 return (int)col;
868}
869
870#if defined(FEAT_LINEBREAK) || defined(PROTO)
871/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100872 * This is called when 'breakindentopt' is changed and when a window is
873 * initialized.
874 */
875 int
876briopt_check(win_T *wp)
877{
878 char_u *p;
879 int bri_shift = 0;
880 long bri_min = 20;
881 int bri_sbr = FALSE;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200882 int bri_list = 0;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100883 int bri_vcol = 0;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100884
885 p = wp->w_p_briopt;
886 while (*p != NUL)
887 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200888 // Note: Keep this in sync with p_briopt_values
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100889 if (STRNCMP(p, "shift:", 6) == 0
890 && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
891 {
892 p += 6;
893 bri_shift = getdigits(&p);
894 }
895 else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4]))
896 {
897 p += 4;
898 bri_min = getdigits(&p);
899 }
900 else if (STRNCMP(p, "sbr", 3) == 0)
901 {
902 p += 3;
903 bri_sbr = TRUE;
904 }
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200905 else if (STRNCMP(p, "list:", 5) == 0)
906 {
907 p += 5;
908 bri_list = getdigits(&p);
909 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100910 else if (STRNCMP(p, "column:", 7) == 0)
911 {
912 p += 7;
913 bri_vcol = getdigits(&p);
914 }
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100915 if (*p != ',' && *p != NUL)
916 return FAIL;
917 if (*p == ',')
918 ++p;
919 }
920
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100921 wp->w_briopt_shift = bri_shift;
922 wp->w_briopt_min = bri_min;
923 wp->w_briopt_sbr = bri_sbr;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200924 wp->w_briopt_list = bri_list;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100925 wp->w_briopt_vcol = bri_vcol;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100926
927 return OK;
928}
929
930/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200931 * Return appropriate space number for breakindent, taking influencing
932 * parameters into account. Window must be specified, since it is not
933 * necessarily always the current one.
934 */
935 int
936get_breakindent_win(
937 win_T *wp,
938 char_u *line) // start of the line
939{
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000940 static int prev_indent = 0; // cached indent value
941 static long prev_ts = 0L; // cached tabstop value
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200942# ifdef FEAT_VARTABS
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000943 static int *prev_vts = NULL; // cached vartabs values
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200944# endif
zeertzjqefabd7c2024-02-11 17:16:19 +0100945 static int prev_fnum = 0; // cached buffer number
946 static char_u *prev_line = NULL; // cached copy of "line"
947 static varnumber_T prev_tick = 0; // changedtick of cached value
948 static int prev_list = 0; // cached list indent
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000949 static int prev_listopt = 0; // cached w_p_briopt_list value
zeertzjqefabd7c2024-02-11 17:16:19 +0100950 static int prev_no_ts = FALSE; // cached no_ts value
951 static unsigned prev_dy_uhex = 0; // cached 'display' "uhex" value
Christian Brabandtc53b4672022-01-15 10:01:05 +0000952 // cached formatlistpat value
953 static char_u *prev_flp = NULL;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200954 int bri = 0;
955 // window width minus window margin space, i.e. what rests for text
956 const int eff_wwidth = wp->w_width
zeertzjqf0a9d652024-02-12 22:53:20 +0100957 - win_col_off(wp) + win_col_off2(wp);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200958
zeertzjqefabd7c2024-02-11 17:16:19 +0100959 // In list mode, if 'listchars' "tab" isn't set, a TAB is displayed as ^I.
960 int no_ts = wp->w_p_list && wp->w_lcs_chars.tab1 == NUL;
961
962 // Used cached indent, unless
963 // - buffer changed, or
964 // - 'tabstop' changed, or
965 // - 'vartabstop' changed, or
966 // - buffer was changed, or
967 // - 'breakindentopt' "list" changed, or
968 // - 'list' or 'listchars' "tab" changed, or
969 // - 'display' "uhex" flag changed, or
970 // - 'formatlistpat' changed, or
971 // - line changed.
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100972 if (prev_fnum != wp->w_buffer->b_fnum
973 || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200974# ifdef FEAT_VARTABS
975 || prev_vts != wp->w_buffer->b_p_vts_array
976# endif
zeertzjqefabd7c2024-02-11 17:16:19 +0100977 || prev_tick != CHANGEDTICK(wp->w_buffer)
978 || prev_listopt != wp->w_briopt_list
979 || prev_no_ts != no_ts
980 || prev_dy_uhex != (dy_flags & DY_UHEX)
981 || prev_flp == NULL
982 || STRCMP(prev_flp, get_flp_value(wp->w_buffer)) != 0
983 || prev_line == NULL || STRCMP(prev_line, line) != 0
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200984 )
985 {
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100986 prev_fnum = wp->w_buffer->b_fnum;
987 vim_free(prev_line);
988 prev_line = vim_strsave(line);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200989 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200990# ifdef FEAT_VARTABS
991 prev_vts = wp->w_buffer->b_p_vts_array;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100992 if (wp->w_briopt_vcol == 0)
993 prev_indent = get_indent_str_vtab(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200994 (int)wp->w_buffer->b_p_ts,
zeertzjqefabd7c2024-02-11 17:16:19 +0100995 wp->w_buffer->b_p_vts_array, no_ts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200996# else
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100997 if (wp->w_briopt_vcol == 0)
998 prev_indent = get_indent_str(line,
zeertzjqd9be94c2024-07-14 10:20:20 +0200999 (int)wp->w_buffer->b_p_ts, no_ts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001000# endif
zeertzjqefabd7c2024-02-11 17:16:19 +01001001 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001002 prev_listopt = wp->w_briopt_list;
Christian Brabandtc53b4672022-01-15 10:01:05 +00001003 prev_list = 0;
zeertzjqefabd7c2024-02-11 17:16:19 +01001004 prev_no_ts = no_ts;
1005 prev_dy_uhex = (dy_flags & DY_UHEX);
Christian Brabandtc53b4672022-01-15 10:01:05 +00001006 vim_free(prev_flp);
1007 prev_flp = vim_strsave(get_flp_value(wp->w_buffer));
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001008 // add additional indent for numbered lists
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001009 if (wp->w_briopt_list != 0 && wp->w_briopt_vcol == 0)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001010 {
1011 regmatch_T regmatch;
1012
Christian Brabandtc53b4672022-01-15 10:01:05 +00001013 regmatch.regprog = vim_regcomp(prev_flp,
1014 RE_MAGIC + RE_STRING + RE_AUTO + RE_STRICT);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001015
1016 if (regmatch.regprog != NULL)
1017 {
1018 regmatch.rm_ic = FALSE;
1019 if (vim_regexec(&regmatch, line, 0))
1020 {
1021 if (wp->w_briopt_list > 0)
1022 prev_list = wp->w_briopt_list;
1023 else
Maxim Kim11916722022-09-02 14:08:53 +01001024 prev_indent = (*regmatch.endp - *regmatch.startp);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001025 }
1026 vim_regfree(regmatch.regprog);
1027 }
1028 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001029 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001030 if (wp->w_briopt_vcol != 0)
1031 {
1032 // column value has priority
1033 bri = wp->w_briopt_vcol;
1034 prev_list = 0;
1035 }
1036 else
1037 bri = prev_indent + wp->w_briopt_shift;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001038
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001039 // Add offset for number column, if 'n' is in 'cpoptions'
1040 bri += win_col_off2(wp);
1041
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001042 // add additional indent for numbered lists
Maxim Kim11916722022-09-02 14:08:53 +01001043 if (wp->w_briopt_list > 0)
1044 bri += prev_list;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001045
Maxim Kimf674b352021-07-22 11:46:59 +02001046 // indent minus the length of the showbreak string
1047 if (wp->w_briopt_sbr)
1048 bri -= vim_strsize(get_showbreak_value(wp));
1049
1050
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001051 // never indent past left window margin
1052 if (bri < 0)
1053 bri = 0;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001054
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001055 // always leave at least bri_min characters on the left,
1056 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001057 else if (bri > eff_wwidth - wp->w_briopt_min)
1058 bri = (eff_wwidth - wp->w_briopt_min < 0)
1059 ? 0 : eff_wwidth - wp->w_briopt_min;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001060
1061 return bri;
1062}
1063#endif
1064
1065/*
1066 * When extra == 0: Return TRUE if the cursor is before or on the first
1067 * non-blank in the line.
1068 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1069 * the line.
1070 */
1071 int
1072inindent(int extra)
1073{
1074 char_u *ptr;
1075 colnr_T col;
1076
1077 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
1078 ++ptr;
1079 if (col >= curwin->w_cursor.col + extra)
1080 return TRUE;
1081 else
1082 return FALSE;
1083}
1084
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001085/*
1086 * op_reindent - handle reindenting a block of lines.
1087 */
1088 void
1089op_reindent(oparg_T *oap, int (*how)(void))
1090{
Bram Moolenaar4c84dd32022-04-20 10:22:54 +01001091 long i = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001092 char_u *l;
1093 int amount;
1094 linenr_T first_changed = 0;
1095 linenr_T last_changed = 0;
1096 linenr_T start_lnum = curwin->w_cursor.lnum;
1097
1098 // Don't even try when 'modifiable' is off.
1099 if (!curbuf->b_p_ma)
1100 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001101 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001102 return;
1103 }
1104
Bram Moolenaare4686982022-04-19 18:28:45 +01001105 // Save for undo. Do this once for all lines, much faster than doing this
1106 // for each line separately, especially when undoing.
1107 if (u_savecommon(start_lnum - 1, start_lnum + oap->line_count,
1108 start_lnum + oap->line_count, FALSE) == OK)
1109 for (i = oap->line_count; --i >= 0 && !got_int; )
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001110 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001111 // it's a slow thing to do, so give feedback so there's no worry
1112 // that the computer's just hung.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001113
Bram Moolenaare4686982022-04-19 18:28:45 +01001114 if (i > 1
1115 && (i % 50 == 0 || i == oap->line_count - 1)
1116 && oap->line_count > p_report)
1117 smsg(_("%ld lines to indent... "), i);
1118
1119 // Be vi-compatible: For lisp indenting the first line is not
1120 // indented, unless there is only one line.
Bram Moolenaare4686982022-04-19 18:28:45 +01001121 if (i != oap->line_count - 1 || oap->line_count == 1
1122 || how != get_lisp_indent)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001123 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001124 l = skipwhite(ml_get_curline());
1125 if (*l == NUL) // empty or blank line
1126 amount = 0;
1127 else
1128 amount = how(); // get the indent for this line
1129
1130 if (amount >= 0 && set_indent(amount, 0))
1131 {
1132 // did change the indent, call changed_lines() later
1133 if (first_changed == 0)
1134 first_changed = curwin->w_cursor.lnum;
1135 last_changed = curwin->w_cursor.lnum;
1136 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001137 }
Bram Moolenaare4686982022-04-19 18:28:45 +01001138 ++curwin->w_cursor.lnum;
1139 curwin->w_cursor.col = 0; // make sure it's valid
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001140 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001141
1142 // put cursor on first non-blank of indented line
1143 curwin->w_cursor.lnum = start_lnum;
1144 beginline(BL_SOL | BL_FIX);
1145
1146 // Mark changed lines so that they will be redrawn. When Visual
1147 // highlighting was present, need to continue until the last line. When
1148 // there is no change still need to remove the Visual highlighting.
1149 if (last_changed != 0)
1150 changed_lines(first_changed, 0,
1151 oap->is_VIsual ? start_lnum + oap->line_count :
1152 last_changed + 1, 0L);
1153 else if (oap->is_VIsual)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001154 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001155
1156 if (oap->line_count > p_report)
1157 {
1158 i = oap->line_count - (i + 1);
1159 smsg(NGETTEXT("%ld line indented ",
1160 "%ld lines indented ", i), i);
1161 }
Bram Moolenaare1004402020-10-24 20:49:43 +02001162 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001163 {
1164 // set '[ and '] marks
1165 curbuf->b_op_start = oap->start;
1166 curbuf->b_op_end = oap->end;
1167 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001168}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001169
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001170/*
1171 * Return TRUE if lines starting with '#' should be left aligned.
1172 */
1173 int
1174preprocs_left(void)
1175{
1176 return
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001177 (curbuf->b_p_si && !curbuf->b_p_cin) ||
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001178 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1179 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001180 ;
1181}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001182
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001183/*
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001184 * Return TRUE if the conditions are OK for smart indenting.
1185 */
1186 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001187may_do_si(void)
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001188{
1189 return curbuf->b_p_si
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001190 && !curbuf->b_p_cin
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001191# ifdef FEAT_EVAL
1192 && *curbuf->b_p_inde == NUL
1193# endif
1194 && !p_paste;
1195}
1196
1197/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001198 * Try to do some very smart auto-indenting.
1199 * Used when inserting a "normal" character.
1200 */
1201 void
1202ins_try_si(int c)
1203{
1204 pos_T *pos, old_pos;
1205 char_u *ptr;
1206 int i;
1207 int temp;
1208
1209 // do some very smart indenting when entering '{' or '}'
Bram Moolenaar2e444bb2022-05-14 12:54:23 +01001210 if (((did_si || can_si_back) && c == '{')
1211 || (can_si && c == '}' && inindent(0)))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001212 {
1213 // for '}' set indent equal to indent of line containing matching '{'
1214 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1215 {
1216 old_pos = curwin->w_cursor;
1217 // If the matching '{' has a ')' immediately before it (ignoring
1218 // white-space), then line up with the start of the line
1219 // containing the matching '(' if there is one. This handles the
1220 // case where an "if (..\n..) {" statement continues over multiple
1221 // lines -- webb
1222 ptr = ml_get(pos->lnum);
1223 i = pos->col;
1224 if (i > 0) // skip blanks before '{'
1225 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1226 ;
1227 curwin->w_cursor.lnum = pos->lnum;
1228 curwin->w_cursor.col = i;
1229 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1230 curwin->w_cursor = *pos;
1231 i = get_indent();
1232 curwin->w_cursor = old_pos;
1233 if (State & VREPLACE_FLAG)
1234 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1235 else
1236 (void)set_indent(i, SIN_CHANGED);
1237 }
1238 else if (curwin->w_cursor.col > 0)
1239 {
1240 // when inserting '{' after "O" reduce indent, but not
1241 // more than indent of previous line
1242 temp = TRUE;
1243 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1244 {
1245 old_pos = curwin->w_cursor;
1246 i = get_indent();
1247 while (curwin->w_cursor.lnum > 1)
1248 {
1249 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1250
1251 // ignore empty lines and lines starting with '#'.
1252 if (*ptr != '#' && *ptr != NUL)
1253 break;
1254 }
1255 if (get_indent() >= i)
1256 temp = FALSE;
1257 curwin->w_cursor = old_pos;
1258 }
1259 if (temp)
1260 shift_line(TRUE, FALSE, 1, TRUE);
1261 }
1262 }
1263
1264 // set indent of '#' always to 0
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001265 if (curwin->w_cursor.col > 0 && can_si && c == '#' && inindent(0))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001266 {
1267 // remember current indent for next line
1268 old_indent = get_indent();
1269 (void)set_indent(0, SIN_CHANGED);
1270 }
1271
1272 // Adjust ai_col, the char at this position can be deleted.
1273 if (ai_col > curwin->w_cursor.col)
1274 ai_col = curwin->w_cursor.col;
1275}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001276
1277/*
1278 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1279 * Keep the cursor on the same character.
1280 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1281 * type == INDENT_DEC decrease indent (for CTRL-D)
1282 * type == INDENT_SET set indent to "amount"
1283 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1284 */
1285 void
1286change_indent(
1287 int type,
1288 int amount,
1289 int round,
1290 int replaced, // replaced character, put on replace stack
1291 int call_changed_bytes) // call changed_bytes()
1292{
1293 int vcol;
1294 int last_vcol;
1295 int insstart_less; // reduction for Insstart.col
1296 int new_cursor_col;
1297 int i;
1298 char_u *ptr;
1299 int save_p_list;
1300 int start_col;
1301 colnr_T vc;
1302 colnr_T orig_col = 0; // init for GCC
1303 char_u *new_line, *orig_line = NULL; // init for GCC
1304
Bram Moolenaar24959102022-05-07 20:01:16 +01001305 // MODE_VREPLACE state needs to know what the line was like before changing
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001306 if (State & VREPLACE_FLAG)
1307 {
1308 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1309 orig_col = curwin->w_cursor.col;
1310 }
1311
1312 // for the following tricks we don't want list mode
1313 save_p_list = curwin->w_p_list;
1314 curwin->w_p_list = FALSE;
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001315#ifdef FEAT_PROP_POPUP
1316 ignore_text_props = TRUE;
1317#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001318 vc = getvcol_nolist(&curwin->w_cursor);
1319 vcol = vc;
1320
1321 // For Replace mode we need to fix the replace stack later, which is only
1322 // possible when the cursor is in the indent. Remember the number of
1323 // characters before the cursor if it's possible.
1324 start_col = curwin->w_cursor.col;
1325
1326 // determine offset from first non-blank
1327 new_cursor_col = curwin->w_cursor.col;
1328 beginline(BL_WHITE);
1329 new_cursor_col -= curwin->w_cursor.col;
1330
1331 insstart_less = curwin->w_cursor.col;
1332
1333 // If the cursor is in the indent, compute how many screen columns the
1334 // cursor is to the left of the first non-blank.
1335 if (new_cursor_col < 0)
1336 vcol = get_indent() - vcol;
1337
1338 if (new_cursor_col > 0) // can't fix replace stack
1339 start_col = -1;
1340
1341 // Set the new indent. The cursor will be put on the first non-blank.
1342 if (type == INDENT_SET)
1343 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1344 else
1345 {
1346 int save_State = State;
1347
1348 // Avoid being called recursively.
1349 if (State & VREPLACE_FLAG)
Bram Moolenaar24959102022-05-07 20:01:16 +01001350 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001351 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1352 State = save_State;
1353 }
1354 insstart_less -= curwin->w_cursor.col;
1355
1356 // Try to put cursor on same character.
1357 // If the cursor is at or after the first non-blank in the line,
1358 // compute the cursor column relative to the column of the first
1359 // non-blank character.
1360 // If we are not in insert mode, leave the cursor on the first non-blank.
1361 // If the cursor is before the first non-blank, position it relative
1362 // to the first non-blank, counted in screen columns.
1363 if (new_cursor_col >= 0)
1364 {
1365 // When changing the indent while the cursor is touching it, reset
1366 // Insstart_col to 0.
1367 if (new_cursor_col == 0)
1368 insstart_less = MAXCOL;
1369 new_cursor_col += curwin->w_cursor.col;
1370 }
Bram Moolenaar24959102022-05-07 20:01:16 +01001371 else if (!(State & MODE_INSERT))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001372 new_cursor_col = curwin->w_cursor.col;
1373 else
1374 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001375 chartabsize_T cts;
1376
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001377 // Compute the screen column where the cursor should be.
1378 vcol = get_indent() - vcol;
1379 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1380
1381 // Advance the cursor until we reach the right screen column.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001382 last_vcol = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001383 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001384 init_chartabsize_arg(&cts, curwin, 0, 0, ptr, ptr);
1385 while (cts.cts_vcol <= (int)curwin->w_virtcol)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001386 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001387 last_vcol = cts.cts_vcol;
1388 if (cts.cts_vcol > 0)
1389 MB_PTR_ADV(cts.cts_ptr);
1390 if (*cts.cts_ptr == NUL)
Bram Moolenaar4e889f92022-02-21 19:36:12 +00001391 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001392 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001393 }
1394 vcol = last_vcol;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001395 new_cursor_col = cts.cts_ptr - cts.cts_line;
1396 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001397
1398 // May need to insert spaces to be able to position the cursor on
1399 // the right screen column.
1400 if (vcol != (int)curwin->w_virtcol)
1401 {
1402 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1403 i = (int)curwin->w_virtcol - vcol;
1404 ptr = alloc(i + 1);
1405 if (ptr != NULL)
1406 {
1407 new_cursor_col += i;
1408 ptr[i] = NUL;
1409 while (--i >= 0)
1410 ptr[i] = ' ';
1411 ins_str(ptr);
1412 vim_free(ptr);
1413 }
1414 }
1415
1416 // When changing the indent while the cursor is in it, reset
1417 // Insstart_col to 0.
1418 insstart_less = MAXCOL;
1419 }
1420
1421 curwin->w_p_list = save_p_list;
1422
1423 if (new_cursor_col <= 0)
1424 curwin->w_cursor.col = 0;
1425 else
1426 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1427 curwin->w_set_curswant = TRUE;
1428 changed_cline_bef_curs();
1429
1430 // May have to adjust the start of the insert.
Bram Moolenaar24959102022-05-07 20:01:16 +01001431 if (State & MODE_INSERT)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001432 {
1433 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1434 {
1435 if ((int)Insstart.col <= insstart_less)
1436 Insstart.col = 0;
1437 else
1438 Insstart.col -= insstart_less;
1439 }
1440 if ((int)ai_col <= insstart_less)
1441 ai_col = 0;
1442 else
1443 ai_col -= insstart_less;
1444 }
1445
Bram Moolenaar24959102022-05-07 20:01:16 +01001446 // For MODE_REPLACE state, may have to fix the replace stack, if it's
1447 // possible. If the number of characters before the cursor decreased, need
1448 // to pop a few characters from the replace stack.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001449 // If the number of characters before the cursor increased, need to push a
1450 // few NULs onto the replace stack.
1451 if (REPLACE_NORMAL(State) && start_col >= 0)
1452 {
1453 while (start_col > (int)curwin->w_cursor.col)
1454 {
1455 replace_join(0); // remove a NUL from the replace stack
1456 --start_col;
1457 }
1458 while (start_col < (int)curwin->w_cursor.col || replaced)
1459 {
1460 replace_push(NUL);
1461 if (replaced)
1462 {
1463 replace_push(replaced);
1464 replaced = NUL;
1465 }
1466 ++start_col;
1467 }
1468 }
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001469#ifdef FEAT_PROP_POPUP
1470 ignore_text_props = FALSE;
1471#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001472
Bram Moolenaar24959102022-05-07 20:01:16 +01001473 // For MODE_VREPLACE state, we also have to fix the replace stack. In this
1474 // case it is always possible because we backspace over the whole line and
1475 // then put it back again the way we wanted it.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001476 if (State & VREPLACE_FLAG)
1477 {
1478 // If orig_line didn't allocate, just return. At least we did the job,
1479 // even if you can't backspace.
1480 if (orig_line == NULL)
1481 return;
1482
1483 // Save new line
1484 new_line = vim_strsave(ml_get_curline());
1485 if (new_line == NULL)
1486 return;
1487
1488 // We only put back the new line up to the cursor
1489 new_line[curwin->w_cursor.col] = NUL;
1490
1491 // Put back original line
1492 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1493 curwin->w_cursor.col = orig_col;
1494
1495 // Backspace from cursor to start of line
1496 backspace_until_column(0);
1497
1498 // Insert new stuff into line again
1499 ins_bytes(new_line);
1500
1501 vim_free(new_line);
1502 }
1503}
1504
1505/*
1506 * Copy the indent from ptr to the current line (and fill to size)
1507 * Leaves the cursor on the first non-blank in the line.
1508 * Returns TRUE if the line was changed.
1509 */
1510 int
1511copy_indent(int size, char_u *src)
1512{
1513 char_u *p = NULL;
1514 char_u *line = NULL;
1515 char_u *s;
1516 int todo;
1517 int ind_len;
1518 int line_len = 0;
1519 int tab_pad;
1520 int ind_done;
1521 int round;
1522#ifdef FEAT_VARTABS
1523 int ind_col;
1524#endif
1525
1526 // Round 1: compute the number of characters needed for the indent
1527 // Round 2: copy the characters.
1528 for (round = 1; round <= 2; ++round)
1529 {
1530 todo = size;
1531 ind_len = 0;
1532 ind_done = 0;
1533#ifdef FEAT_VARTABS
1534 ind_col = 0;
1535#endif
1536 s = src;
1537
1538 // Count/copy the usable portion of the source line
1539 while (todo > 0 && VIM_ISWHITE(*s))
1540 {
1541 if (*s == TAB)
1542 {
1543#ifdef FEAT_VARTABS
1544 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1545 curbuf->b_p_vts_array);
1546#else
1547 tab_pad = (int)curbuf->b_p_ts
1548 - (ind_done % (int)curbuf->b_p_ts);
1549#endif
1550 // Stop if this tab will overshoot the target
1551 if (todo < tab_pad)
1552 break;
1553 todo -= tab_pad;
1554 ind_done += tab_pad;
1555#ifdef FEAT_VARTABS
1556 ind_col += tab_pad;
1557#endif
1558 }
1559 else
1560 {
1561 --todo;
1562 ++ind_done;
1563#ifdef FEAT_VARTABS
1564 ++ind_col;
1565#endif
1566 }
1567 ++ind_len;
1568 if (p != NULL)
1569 *p++ = *s;
1570 ++s;
1571 }
1572
1573 // Fill to next tabstop with a tab, if possible
1574#ifdef FEAT_VARTABS
1575 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1576 curbuf->b_p_vts_array);
1577#else
1578 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1579#endif
1580 if (todo >= tab_pad && !curbuf->b_p_et)
1581 {
1582 todo -= tab_pad;
1583 ++ind_len;
1584#ifdef FEAT_VARTABS
1585 ind_col += tab_pad;
1586#endif
1587 if (p != NULL)
1588 *p++ = TAB;
1589 }
1590
1591 // Add tabs required for indent
1592 if (!curbuf->b_p_et)
1593 {
1594#ifdef FEAT_VARTABS
1595 for (;;)
1596 {
1597 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1598 curbuf->b_p_vts_array);
1599 if (todo < tab_pad)
1600 break;
1601 todo -= tab_pad;
1602 ++ind_len;
1603 ind_col += tab_pad;
1604 if (p != NULL)
1605 *p++ = TAB;
1606 }
1607#else
1608 while (todo >= (int)curbuf->b_p_ts)
1609 {
1610 todo -= (int)curbuf->b_p_ts;
1611 ++ind_len;
1612 if (p != NULL)
1613 *p++ = TAB;
1614 }
1615#endif
1616 }
1617
1618 // Count/add spaces required for indent
1619 while (todo > 0)
1620 {
1621 --todo;
1622 ++ind_len;
1623 if (p != NULL)
1624 *p++ = ' ';
1625 }
1626
1627 if (p == NULL)
1628 {
1629 // Allocate memory for the result: the copied indent, new indent
1630 // and the rest of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01001631 line_len = ml_get_curline_len() + 1;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001632 line = alloc(ind_len + line_len);
1633 if (line == NULL)
1634 return FALSE;
1635 p = line;
1636 }
1637 }
1638
1639 // Append the original line
1640 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1641
1642 // Replace the line
1643 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1644
1645 // Put the cursor after the indent.
1646 curwin->w_cursor.col = ind_len;
1647 return TRUE;
1648}
1649
1650/*
Bram Moolenaar308660b2022-06-16 12:10:48 +01001651 * Give a "resulting text too long" error and maybe set got_int.
1652 */
1653 static void
1654emsg_text_too_long(void)
1655{
1656 emsg(_(e_resulting_text_too_long));
1657#ifdef FEAT_EVAL
1658 // when not inside a try/catch set got_int to break out of any loop
1659 if (trylevel == 0)
1660#endif
1661 got_int = TRUE;
1662}
1663
1664/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001665 * ":retab".
1666 */
1667 void
1668ex_retab(exarg_T *eap)
1669{
1670 linenr_T lnum;
1671 int got_tab = FALSE;
1672 long num_spaces = 0;
1673 long num_tabs;
1674 long len;
1675 long col;
1676 long vcol;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001677 long start_col = 0; // For start of white-space string
1678 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001679 long old_len;
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001680 long new_len;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001681 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001682 char_u *new_line = (char_u *)1; // init to non-NULL
1683 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001684#ifdef FEAT_VARTABS
1685 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001686 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001687#else
1688 int temp;
1689 int new_ts;
1690#endif
1691 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001692 linenr_T first_line = 0; // first changed line
1693 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001694
1695 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001696 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001697
1698#ifdef FEAT_VARTABS
1699 new_ts_str = eap->arg;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02001700 if (tabstop_set(eap->arg, &new_vts_array) == FAIL)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001701 return;
1702 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1703 ++(eap->arg);
1704
1705 // This ensures that either new_vts_array and new_ts_str are freshly
1706 // allocated, or new_vts_array points to an existing array and new_ts_str
1707 // is null.
1708 if (new_vts_array == NULL)
1709 {
1710 new_vts_array = curbuf->b_p_vts_array;
1711 new_ts_str = NULL;
1712 }
1713 else
1714 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1715#else
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001716 ptr = eap->arg;
1717 new_ts = getdigits(&ptr);
1718 if (new_ts < 0 && *eap->arg == '-')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001719 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001720 emsg(_(e_argument_must_be_positive));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001721 return;
1722 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00001723 if (new_ts < 0 || new_ts > TABSTOP_MAX)
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001724 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001725 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001726 return;
1727 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001728 if (new_ts == 0)
1729 new_ts = curbuf->b_p_ts;
1730#endif
1731 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1732 {
1733 ptr = ml_get(lnum);
1734 col = 0;
1735 vcol = 0;
1736 did_undo = FALSE;
1737 for (;;)
1738 {
1739 if (VIM_ISWHITE(ptr[col]))
1740 {
1741 if (!got_tab && num_spaces == 0)
1742 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001743 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001744 start_vcol = vcol;
1745 start_col = col;
1746 }
1747 if (ptr[col] == ' ')
1748 num_spaces++;
1749 else
1750 got_tab = TRUE;
1751 }
1752 else
1753 {
1754 if (got_tab || (eap->forceit && num_spaces > 1))
1755 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001756 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001757
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001758 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001759 len = num_spaces = vcol - start_vcol;
1760 num_tabs = 0;
1761 if (!curbuf->b_p_et)
1762 {
1763#ifdef FEAT_VARTABS
1764 int t, s;
1765
1766 tabstop_fromto(start_vcol, vcol,
1767 curbuf->b_p_ts, new_vts_array, &t, &s);
1768 num_tabs = t;
1769 num_spaces = s;
1770#else
1771 temp = new_ts - (start_vcol % new_ts);
1772 if (num_spaces >= temp)
1773 {
1774 num_spaces -= temp;
1775 num_tabs++;
1776 }
1777 num_tabs += num_spaces / new_ts;
1778 num_spaces -= (num_spaces / new_ts) * new_ts;
1779#endif
1780 }
1781 if (curbuf->b_p_et || got_tab ||
1782 (num_spaces + num_tabs < len))
1783 {
1784 if (did_undo == FALSE)
1785 {
1786 did_undo = TRUE;
1787 if (u_save((linenr_T)(lnum - 1),
1788 (linenr_T)(lnum + 1)) == FAIL)
1789 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001790 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001791 break;
1792 }
1793 }
1794
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001795 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001796 len = num_spaces + num_tabs;
1797 old_len = (long)STRLEN(ptr);
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001798 new_len = old_len - col + start_col + len + 1;
Bram Moolenaar45491662022-02-12 21:59:51 +00001799 if (new_len <= 0 || new_len >= MAXCOL)
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001800 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001801 emsg_text_too_long();
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001802 break;
1803 }
1804 new_line = alloc(new_len);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001805 if (new_line == NULL)
1806 break;
1807 if (start_col > 0)
1808 mch_memmove(new_line, ptr, (size_t)start_col);
1809 mch_memmove(new_line + start_col + len,
1810 ptr + col, (size_t)(old_len - col + 1));
1811 ptr = new_line + start_col;
1812 for (col = 0; col < len; col++)
1813 ptr[col] = (col < num_tabs) ? '\t' : ' ';
Bram Moolenaar0dcd39b2021-02-03 19:44:25 +01001814 if (ml_replace(lnum, new_line, FALSE) == OK)
1815 // "new_line" may have been copied
1816 new_line = curbuf->b_ml.ml_line_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001817 if (first_line == 0)
1818 first_line = lnum;
1819 last_line = lnum;
1820 ptr = new_line;
1821 col = start_col + len;
1822 }
1823 }
1824 got_tab = FALSE;
1825 num_spaces = 0;
1826 }
1827 if (ptr[col] == NUL)
1828 break;
1829 vcol += chartabsize(ptr + col, (colnr_T)vcol);
Bram Moolenaar6e287032022-02-12 15:42:18 +00001830 if (vcol >= MAXCOL)
1831 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001832 emsg_text_too_long();
Bram Moolenaar6e287032022-02-12 15:42:18 +00001833 break;
1834 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001835 if (has_mbyte)
1836 col += (*mb_ptr2len)(ptr + col);
1837 else
1838 ++col;
1839 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001840 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001841 break;
1842 line_breakcheck();
1843 }
1844 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001845 emsg(_(e_interrupted));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001846
1847#ifdef FEAT_VARTABS
1848 // If a single value was given then it can be considered equal to
1849 // either the value of 'tabstop' or the value of 'vartabstop'.
1850 if (tabstop_count(curbuf->b_p_vts_array) == 0
1851 && tabstop_count(new_vts_array) == 1
1852 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001853 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001854 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001855 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001856 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001857 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001858 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001859#else
1860 if (curbuf->b_p_ts != new_ts)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001861 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001862#endif
1863 if (first_line != 0)
1864 changed_lines(first_line, 0, last_line + 1, 0L);
1865
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001866 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001867
1868#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001869 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001870 {
1871 // If 'vartabstop' is in use or if the value given to retab has more
1872 // than one tabstop then update 'vartabstop'.
1873 int *old_vts_ary = curbuf->b_p_vts_array;
1874
1875 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1876 {
1877 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1878 OPT_FREE|OPT_LOCAL, 0);
1879 curbuf->b_p_vts_array = new_vts_array;
1880 vim_free(old_vts_ary);
1881 }
1882 else
1883 {
1884 // 'vartabstop' wasn't in use and a single value was given to
1885 // retab then update 'tabstop'.
1886 curbuf->b_p_ts = tabstop_first(new_vts_array);
1887 vim_free(new_vts_array);
1888 }
1889 vim_free(new_ts_str);
1890 }
1891#else
1892 curbuf->b_p_ts = new_ts;
1893#endif
1894 coladvance(curwin->w_curswant);
1895
1896 u_clearline();
1897}
1898
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001899#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001900/*
1901 * Get indent level from 'indentexpr'.
1902 */
1903 int
1904get_expr_indent(void)
1905{
1906 int indent = -1;
1907 char_u *inde_copy;
1908 pos_T save_pos;
1909 colnr_T save_curswant;
1910 int save_set_curswant;
1911 int save_State;
1912 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1913 OPT_LOCAL);
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001914 sctx_T save_sctx = current_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001915
1916 // Save and restore cursor position and curswant, in case it was changed
1917 // via :normal commands
1918 save_pos = curwin->w_cursor;
1919 save_curswant = curwin->w_curswant;
1920 save_set_curswant = curwin->w_set_curswant;
1921 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1922 if (use_sandbox)
1923 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001924 ++textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001925 current_sctx = curbuf->b_p_script_ctx[BV_INDE];
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001926
1927 // Need to make a copy, the 'indentexpr' option could be changed while
1928 // evaluating it.
1929 inde_copy = vim_strsave(curbuf->b_p_inde);
1930 if (inde_copy != NULL)
1931 {
Bram Moolenaar3292a222022-10-01 20:17:17 +01001932 indent = (int)eval_to_number(inde_copy, TRUE);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001933 vim_free(inde_copy);
1934 }
1935
1936 if (use_sandbox)
1937 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001938 --textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001939 current_sctx = save_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001940
1941 // Restore the cursor position so that 'indentexpr' doesn't need to.
1942 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1943 // command.
1944 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001945 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001946 curwin->w_cursor = save_pos;
1947 curwin->w_curswant = save_curswant;
1948 curwin->w_set_curswant = save_set_curswant;
1949 check_cursor();
1950 State = save_State;
1951
Bram Moolenaar620c9592021-07-31 21:32:31 +02001952 // Reset did_throw, unless 'debug' has "throw" and inside a try/catch.
1953 if (did_throw && (vim_strchr(p_debug, 't') == NULL || trylevel == 0))
1954 {
1955 handle_did_throw();
1956 did_throw = FALSE;
1957 }
1958
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001959 // If there is an error, just keep the current indent.
1960 if (indent < 0)
1961 indent = get_indent();
1962
1963 return indent;
1964}
1965#endif
1966
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001967 static int
1968lisp_match(char_u *p)
1969{
1970 char_u buf[LSIZE];
1971 int len;
1972 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1973
1974 while (*word != NUL)
1975 {
1976 (void)copy_option_part(&word, buf, LSIZE, ",");
1977 len = (int)STRLEN(buf);
Bram Moolenaard26c5802022-10-13 12:30:08 +01001978 if (STRNCMP(buf, p, len) == 0 && IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001979 return TRUE;
1980 }
1981 return FALSE;
1982}
1983
1984/*
1985 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
1986 * The incompatible newer method is quite a bit better at indenting
1987 * code in lisp-like languages than the traditional one; it's still
1988 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
1989 *
1990 * TODO:
1991 * Findmatch() should be adapted for lisp, also to make showmatch
1992 * work correctly: now (v5.3) it seems all C/C++ oriented:
1993 * - it does not recognize the #\( and #\) notations as character literals
1994 * - it doesn't know about comments starting with a semicolon
1995 * - it incorrectly interprets '(' as a character literal
1996 * All this messes up get_lisp_indent in some rare cases.
1997 * Update from Sergey Khorev:
1998 * I tried to fix the first two issues.
1999 */
2000 int
2001get_lisp_indent(void)
2002{
2003 pos_T *pos, realpos, paren;
2004 int amount;
2005 char_u *that;
2006 colnr_T col;
2007 colnr_T firsttry;
2008 int parencount, quotecount;
2009 int vi_lisp;
2010
2011 // Set vi_lisp to use the vi-compatible method
2012 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
2013
2014 realpos = curwin->w_cursor;
2015 curwin->w_cursor.col = 0;
2016
2017 if ((pos = findmatch(NULL, '(')) == NULL)
2018 pos = findmatch(NULL, '[');
2019 else
2020 {
2021 paren = *pos;
2022 pos = findmatch(NULL, '[');
2023 if (pos == NULL || LT_POSP(pos, &paren))
2024 pos = &paren;
2025 }
2026 if (pos != NULL)
2027 {
2028 // Extra trick: Take the indent of the first previous non-white
2029 // line that is at the same () level.
2030 amount = -1;
2031 parencount = 0;
2032
2033 while (--curwin->w_cursor.lnum >= pos->lnum)
2034 {
2035 if (linewhite(curwin->w_cursor.lnum))
2036 continue;
2037 for (that = ml_get_curline(); *that != NUL; ++that)
2038 {
2039 if (*that == ';')
2040 {
2041 while (*(that + 1) != NUL)
2042 ++that;
2043 continue;
2044 }
2045 if (*that == '\\')
2046 {
2047 if (*(that + 1) != NUL)
2048 ++that;
2049 continue;
2050 }
2051 if (*that == '"' && *(that + 1) != NUL)
2052 {
2053 while (*++that && *that != '"')
2054 {
2055 // skipping escaped characters in the string
2056 if (*that == '\\')
2057 {
2058 if (*++that == NUL)
2059 break;
2060 if (that[1] == NUL)
2061 {
2062 ++that;
2063 break;
2064 }
2065 }
2066 }
Bram Moolenaar0e8e9382022-06-18 12:51:11 +01002067 if (*that == NUL)
2068 break;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002069 }
2070 if (*that == '(' || *that == '[')
2071 ++parencount;
2072 else if (*that == ')' || *that == ']')
2073 --parencount;
2074 }
2075 if (parencount == 0)
2076 {
2077 amount = get_indent();
2078 break;
2079 }
2080 }
2081
2082 if (amount == -1)
2083 {
2084 curwin->w_cursor.lnum = pos->lnum;
2085 curwin->w_cursor.col = pos->col;
2086 col = pos->col;
2087
2088 that = ml_get_curline();
2089
2090 if (vi_lisp && get_indent() == 0)
2091 amount = 2;
2092 else
2093 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002094 char_u *line = that;
2095 chartabsize_T cts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002096
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002097 init_chartabsize_arg(&cts, curwin, pos->lnum, 0, line, line);
2098 while (*cts.cts_ptr != NUL && col > 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002099 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002100 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002101 col--;
2102 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002103 amount = cts.cts_vcol;
2104 that = cts.cts_ptr;
2105 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002106
2107 // Some keywords require "body" indenting rules (the
2108 // non-standard-lisp ones are Scheme special forms):
2109 //
2110 // (let ((a 1)) instead (let ((a 1))
2111 // (...)) of (...))
2112
2113 if (!vi_lisp && (*that == '(' || *that == '[')
2114 && lisp_match(that + 1))
2115 amount += 2;
2116 else
2117 {
Bram Moolenaar8eba2bd2022-06-22 19:59:28 +01002118 if (*that != NUL)
2119 {
2120 that++;
2121 amount++;
2122 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002123 firsttry = amount;
2124
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002125 init_chartabsize_arg(&cts, curwin, (colnr_T)(that - line),
2126 amount, line, that);
2127 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002128 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002129 cts.cts_vcol += lbr_chartabsize(&cts);
2130 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002131 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002132 that = cts.cts_ptr;
2133 amount = cts.cts_vcol;
2134 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002135
2136 if (*that && *that != ';') // not a comment line
2137 {
2138 // test *that != '(' to accommodate first let/do
2139 // argument if it is more than one line
2140 if (!vi_lisp && *that != '(' && *that != '[')
2141 firsttry++;
2142
2143 parencount = 0;
2144 quotecount = 0;
2145
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002146 init_chartabsize_arg(&cts, curwin,
2147 (colnr_T)(that - line), amount, line, that);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002148 if (vi_lisp
2149 || (*that != '"'
2150 && *that != '\''
2151 && *that != '#'
2152 && (*that < '0' || *that > '9')))
2153 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002154 while (*cts.cts_ptr
2155 && (!VIM_ISWHITE(*cts.cts_ptr)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002156 || quotecount
2157 || parencount)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002158 && (!((*cts.cts_ptr == '('
2159 || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002160 && !quotecount
2161 && !parencount
2162 && vi_lisp)))
2163 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002164 if (*cts.cts_ptr == '"')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002165 quotecount = !quotecount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002166 if ((*cts.cts_ptr == '(' || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002167 && !quotecount)
2168 ++parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002169 if ((*cts.cts_ptr == ')' || *cts.cts_ptr == ']')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002170 && !quotecount)
2171 --parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002172 if (*cts.cts_ptr == '\\'
2173 && *(cts.cts_ptr+1) != NUL)
2174 cts.cts_vcol += lbr_chartabsize_adv(&cts);
2175 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002176 }
2177 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002178 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002179 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002180 cts.cts_vcol += lbr_chartabsize(&cts);
2181 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002182 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002183 that = cts.cts_ptr;
2184 amount = cts.cts_vcol;
2185 clear_chartabsize_arg(&cts);
2186
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002187 if (!*that || *that == ';')
2188 amount = firsttry;
2189 }
2190 }
2191 }
2192 }
2193 }
2194 else
2195 amount = 0; // no matching '(' or '[' found, use zero indent
2196
2197 curwin->w_cursor = realpos;
2198
2199 return amount;
2200}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002201
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002202/*
2203 * Re-indent the current line, based on the current contents of it and the
2204 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
2205 * confused what all the part that handles Control-T is doing that I'm not.
2206 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
2207 */
2208
2209 void
2210fixthisline(int (*get_the_indent)(void))
2211{
2212 int amount = get_the_indent();
2213
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002214 if (amount < 0)
2215 return;
2216
2217 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
2218 if (linewhite(curwin->w_cursor.lnum))
2219 did_ai = TRUE; // delete the indent if the line stays empty
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002220}
2221
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002222/*
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002223 * Return TRUE if 'indentexpr' should be used for Lisp indenting.
2224 * Caller may want to check 'autoindent'.
2225 */
2226 int
2227use_indentexpr_for_lisp(void)
2228{
2229#ifdef FEAT_EVAL
2230 return curbuf->b_p_lisp
2231 && *curbuf->b_p_inde != NUL
2232 && STRCMP(curbuf->b_p_lop, "expr:1") == 0;
2233#else
2234 return FALSE;
2235#endif
2236}
2237
2238/*
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002239 * Fix indent for 'lisp' and 'cindent'.
2240 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002241 void
2242fix_indent(void)
2243{
2244 if (p_paste)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002245 return; // no auto-indenting when 'paste' is set
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002246 if (curbuf->b_p_lisp && curbuf->b_p_ai)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002247 {
2248 if (use_indentexpr_for_lisp())
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002249 do_c_expr_indent();
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002250 else
2251 fixthisline(get_lisp_indent);
2252 }
2253 else if (cindent_on())
2254 do_c_expr_indent();
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002255}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002256
2257#if defined(FEAT_EVAL) || defined(PROTO)
2258/*
2259 * "indent()" function
2260 */
2261 void
2262f_indent(typval_T *argvars, typval_T *rettv)
2263{
2264 linenr_T lnum;
2265
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002266 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2267 return;
2268
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002269 lnum = tv_get_lnum(argvars);
2270 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2271 rettv->vval.v_number = get_indent_lnum(lnum);
2272 else
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002273 {
2274 if (in_vim9script())
2275 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002276 rettv->vval.v_number = -1;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002277 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002278}
2279
2280/*
2281 * "lispindent(lnum)" function
2282 */
2283 void
2284f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2285{
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002286 pos_T pos;
2287 linenr_T lnum;
2288
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002289 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2290 return;
2291
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002292 pos = curwin->w_cursor;
2293 lnum = tv_get_lnum(argvars);
2294 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2295 {
2296 curwin->w_cursor.lnum = lnum;
2297 rettv->vval.v_number = get_lisp_indent();
2298 curwin->w_cursor = pos;
2299 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002300 else if (in_vim9script())
2301 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002302 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002303 rettv->vval.v_number = -1;
2304}
2305#endif