blob: 4ba31d2aa0fe03d5c89629ce186e28bda7b660e3 [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
zeertzjq61a6ac42024-09-07 11:23:54 +02001024 {
1025 char_u *ptr = *regmatch.startp;
1026 char_u *end_ptr = *regmatch.endp;
1027 int indent = 0;
1028
1029 // Compute the width of the matched text.
1030 // Use win_chartabsize() so that TAB size is correct,
1031 // while wrapping is ignored.
1032 while (ptr < end_ptr)
1033 {
1034 indent += win_chartabsize(wp, ptr, indent);
1035 MB_PTR_ADV(ptr);
1036 }
1037 prev_indent = indent;
1038 }
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001039 }
1040 vim_regfree(regmatch.regprog);
1041 }
1042 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001043 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001044 if (wp->w_briopt_vcol != 0)
1045 {
1046 // column value has priority
1047 bri = wp->w_briopt_vcol;
1048 prev_list = 0;
1049 }
1050 else
1051 bri = prev_indent + wp->w_briopt_shift;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001052
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001053 // Add offset for number column, if 'n' is in 'cpoptions'
1054 bri += win_col_off2(wp);
1055
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001056 // add additional indent for numbered lists
Maxim Kim11916722022-09-02 14:08:53 +01001057 if (wp->w_briopt_list > 0)
1058 bri += prev_list;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001059
Maxim Kimf674b352021-07-22 11:46:59 +02001060 // indent minus the length of the showbreak string
1061 if (wp->w_briopt_sbr)
1062 bri -= vim_strsize(get_showbreak_value(wp));
1063
1064
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001065 // never indent past left window margin
1066 if (bri < 0)
1067 bri = 0;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001068
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001069 // always leave at least bri_min characters on the left,
1070 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001071 else if (bri > eff_wwidth - wp->w_briopt_min)
1072 bri = (eff_wwidth - wp->w_briopt_min < 0)
1073 ? 0 : eff_wwidth - wp->w_briopt_min;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001074
1075 return bri;
1076}
1077#endif
1078
1079/*
1080 * When extra == 0: Return TRUE if the cursor is before or on the first
1081 * non-blank in the line.
1082 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1083 * the line.
1084 */
1085 int
1086inindent(int extra)
1087{
1088 char_u *ptr;
1089 colnr_T col;
1090
1091 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
1092 ++ptr;
1093 if (col >= curwin->w_cursor.col + extra)
1094 return TRUE;
1095 else
1096 return FALSE;
1097}
1098
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001099/*
1100 * op_reindent - handle reindenting a block of lines.
1101 */
1102 void
1103op_reindent(oparg_T *oap, int (*how)(void))
1104{
Bram Moolenaar4c84dd32022-04-20 10:22:54 +01001105 long i = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001106 char_u *l;
1107 int amount;
1108 linenr_T first_changed = 0;
1109 linenr_T last_changed = 0;
1110 linenr_T start_lnum = curwin->w_cursor.lnum;
1111
1112 // Don't even try when 'modifiable' is off.
1113 if (!curbuf->b_p_ma)
1114 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001115 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001116 return;
1117 }
1118
Bram Moolenaare4686982022-04-19 18:28:45 +01001119 // Save for undo. Do this once for all lines, much faster than doing this
1120 // for each line separately, especially when undoing.
1121 if (u_savecommon(start_lnum - 1, start_lnum + oap->line_count,
1122 start_lnum + oap->line_count, FALSE) == OK)
1123 for (i = oap->line_count; --i >= 0 && !got_int; )
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001124 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001125 // it's a slow thing to do, so give feedback so there's no worry
1126 // that the computer's just hung.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001127
Bram Moolenaare4686982022-04-19 18:28:45 +01001128 if (i > 1
1129 && (i % 50 == 0 || i == oap->line_count - 1)
1130 && oap->line_count > p_report)
1131 smsg(_("%ld lines to indent... "), i);
1132
1133 // Be vi-compatible: For lisp indenting the first line is not
1134 // indented, unless there is only one line.
Bram Moolenaare4686982022-04-19 18:28:45 +01001135 if (i != oap->line_count - 1 || oap->line_count == 1
1136 || how != get_lisp_indent)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001137 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001138 l = skipwhite(ml_get_curline());
1139 if (*l == NUL) // empty or blank line
1140 amount = 0;
1141 else
1142 amount = how(); // get the indent for this line
1143
1144 if (amount >= 0 && set_indent(amount, 0))
1145 {
1146 // did change the indent, call changed_lines() later
1147 if (first_changed == 0)
1148 first_changed = curwin->w_cursor.lnum;
1149 last_changed = curwin->w_cursor.lnum;
1150 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001151 }
Bram Moolenaare4686982022-04-19 18:28:45 +01001152 ++curwin->w_cursor.lnum;
1153 curwin->w_cursor.col = 0; // make sure it's valid
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001154 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001155
1156 // put cursor on first non-blank of indented line
1157 curwin->w_cursor.lnum = start_lnum;
1158 beginline(BL_SOL | BL_FIX);
1159
1160 // Mark changed lines so that they will be redrawn. When Visual
1161 // highlighting was present, need to continue until the last line. When
1162 // there is no change still need to remove the Visual highlighting.
1163 if (last_changed != 0)
1164 changed_lines(first_changed, 0,
1165 oap->is_VIsual ? start_lnum + oap->line_count :
1166 last_changed + 1, 0L);
1167 else if (oap->is_VIsual)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001168 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001169
1170 if (oap->line_count > p_report)
1171 {
1172 i = oap->line_count - (i + 1);
1173 smsg(NGETTEXT("%ld line indented ",
1174 "%ld lines indented ", i), i);
1175 }
Bram Moolenaare1004402020-10-24 20:49:43 +02001176 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001177 {
1178 // set '[ and '] marks
1179 curbuf->b_op_start = oap->start;
1180 curbuf->b_op_end = oap->end;
1181 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001182}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001183
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001184/*
1185 * Return TRUE if lines starting with '#' should be left aligned.
1186 */
1187 int
1188preprocs_left(void)
1189{
1190 return
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001191 (curbuf->b_p_si && !curbuf->b_p_cin) ||
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001192 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1193 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001194 ;
1195}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001196
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001197/*
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001198 * Return TRUE if the conditions are OK for smart indenting.
1199 */
1200 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001201may_do_si(void)
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001202{
1203 return curbuf->b_p_si
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001204 && !curbuf->b_p_cin
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001205# ifdef FEAT_EVAL
1206 && *curbuf->b_p_inde == NUL
1207# endif
1208 && !p_paste;
1209}
1210
1211/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001212 * Try to do some very smart auto-indenting.
1213 * Used when inserting a "normal" character.
1214 */
1215 void
1216ins_try_si(int c)
1217{
1218 pos_T *pos, old_pos;
1219 char_u *ptr;
1220 int i;
1221 int temp;
1222
1223 // do some very smart indenting when entering '{' or '}'
Bram Moolenaar2e444bb2022-05-14 12:54:23 +01001224 if (((did_si || can_si_back) && c == '{')
1225 || (can_si && c == '}' && inindent(0)))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001226 {
1227 // for '}' set indent equal to indent of line containing matching '{'
1228 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1229 {
1230 old_pos = curwin->w_cursor;
1231 // If the matching '{' has a ')' immediately before it (ignoring
1232 // white-space), then line up with the start of the line
1233 // containing the matching '(' if there is one. This handles the
1234 // case where an "if (..\n..) {" statement continues over multiple
1235 // lines -- webb
1236 ptr = ml_get(pos->lnum);
1237 i = pos->col;
1238 if (i > 0) // skip blanks before '{'
1239 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1240 ;
1241 curwin->w_cursor.lnum = pos->lnum;
1242 curwin->w_cursor.col = i;
1243 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1244 curwin->w_cursor = *pos;
1245 i = get_indent();
1246 curwin->w_cursor = old_pos;
1247 if (State & VREPLACE_FLAG)
1248 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1249 else
1250 (void)set_indent(i, SIN_CHANGED);
1251 }
1252 else if (curwin->w_cursor.col > 0)
1253 {
1254 // when inserting '{' after "O" reduce indent, but not
1255 // more than indent of previous line
1256 temp = TRUE;
1257 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1258 {
1259 old_pos = curwin->w_cursor;
1260 i = get_indent();
1261 while (curwin->w_cursor.lnum > 1)
1262 {
1263 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1264
1265 // ignore empty lines and lines starting with '#'.
1266 if (*ptr != '#' && *ptr != NUL)
1267 break;
1268 }
1269 if (get_indent() >= i)
1270 temp = FALSE;
1271 curwin->w_cursor = old_pos;
1272 }
1273 if (temp)
1274 shift_line(TRUE, FALSE, 1, TRUE);
1275 }
1276 }
1277
1278 // set indent of '#' always to 0
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001279 if (curwin->w_cursor.col > 0 && can_si && c == '#' && inindent(0))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001280 {
1281 // remember current indent for next line
1282 old_indent = get_indent();
1283 (void)set_indent(0, SIN_CHANGED);
1284 }
1285
1286 // Adjust ai_col, the char at this position can be deleted.
1287 if (ai_col > curwin->w_cursor.col)
1288 ai_col = curwin->w_cursor.col;
1289}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001290
1291/*
1292 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1293 * Keep the cursor on the same character.
1294 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1295 * type == INDENT_DEC decrease indent (for CTRL-D)
1296 * type == INDENT_SET set indent to "amount"
1297 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1298 */
1299 void
1300change_indent(
1301 int type,
1302 int amount,
1303 int round,
1304 int replaced, // replaced character, put on replace stack
1305 int call_changed_bytes) // call changed_bytes()
1306{
1307 int vcol;
1308 int last_vcol;
1309 int insstart_less; // reduction for Insstart.col
1310 int new_cursor_col;
1311 int i;
1312 char_u *ptr;
1313 int save_p_list;
1314 int start_col;
1315 colnr_T vc;
1316 colnr_T orig_col = 0; // init for GCC
1317 char_u *new_line, *orig_line = NULL; // init for GCC
1318
Bram Moolenaar24959102022-05-07 20:01:16 +01001319 // MODE_VREPLACE state needs to know what the line was like before changing
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001320 if (State & VREPLACE_FLAG)
1321 {
1322 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1323 orig_col = curwin->w_cursor.col;
1324 }
1325
1326 // for the following tricks we don't want list mode
1327 save_p_list = curwin->w_p_list;
1328 curwin->w_p_list = FALSE;
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001329#ifdef FEAT_PROP_POPUP
1330 ignore_text_props = TRUE;
1331#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001332 vc = getvcol_nolist(&curwin->w_cursor);
1333 vcol = vc;
1334
1335 // For Replace mode we need to fix the replace stack later, which is only
1336 // possible when the cursor is in the indent. Remember the number of
1337 // characters before the cursor if it's possible.
1338 start_col = curwin->w_cursor.col;
1339
1340 // determine offset from first non-blank
1341 new_cursor_col = curwin->w_cursor.col;
1342 beginline(BL_WHITE);
1343 new_cursor_col -= curwin->w_cursor.col;
1344
1345 insstart_less = curwin->w_cursor.col;
1346
1347 // If the cursor is in the indent, compute how many screen columns the
1348 // cursor is to the left of the first non-blank.
1349 if (new_cursor_col < 0)
1350 vcol = get_indent() - vcol;
1351
1352 if (new_cursor_col > 0) // can't fix replace stack
1353 start_col = -1;
1354
1355 // Set the new indent. The cursor will be put on the first non-blank.
1356 if (type == INDENT_SET)
1357 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1358 else
1359 {
1360 int save_State = State;
1361
1362 // Avoid being called recursively.
1363 if (State & VREPLACE_FLAG)
Bram Moolenaar24959102022-05-07 20:01:16 +01001364 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001365 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1366 State = save_State;
1367 }
1368 insstart_less -= curwin->w_cursor.col;
1369
1370 // Try to put cursor on same character.
1371 // If the cursor is at or after the first non-blank in the line,
1372 // compute the cursor column relative to the column of the first
1373 // non-blank character.
1374 // If we are not in insert mode, leave the cursor on the first non-blank.
1375 // If the cursor is before the first non-blank, position it relative
1376 // to the first non-blank, counted in screen columns.
1377 if (new_cursor_col >= 0)
1378 {
1379 // When changing the indent while the cursor is touching it, reset
1380 // Insstart_col to 0.
1381 if (new_cursor_col == 0)
1382 insstart_less = MAXCOL;
1383 new_cursor_col += curwin->w_cursor.col;
1384 }
Bram Moolenaar24959102022-05-07 20:01:16 +01001385 else if (!(State & MODE_INSERT))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001386 new_cursor_col = curwin->w_cursor.col;
1387 else
1388 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001389 chartabsize_T cts;
1390
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001391 // Compute the screen column where the cursor should be.
1392 vcol = get_indent() - vcol;
1393 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1394
1395 // Advance the cursor until we reach the right screen column.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001396 last_vcol = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001397 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001398 init_chartabsize_arg(&cts, curwin, 0, 0, ptr, ptr);
1399 while (cts.cts_vcol <= (int)curwin->w_virtcol)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001400 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001401 last_vcol = cts.cts_vcol;
1402 if (cts.cts_vcol > 0)
1403 MB_PTR_ADV(cts.cts_ptr);
1404 if (*cts.cts_ptr == NUL)
Bram Moolenaar4e889f92022-02-21 19:36:12 +00001405 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001406 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001407 }
1408 vcol = last_vcol;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001409 new_cursor_col = cts.cts_ptr - cts.cts_line;
1410 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001411
1412 // May need to insert spaces to be able to position the cursor on
1413 // the right screen column.
1414 if (vcol != (int)curwin->w_virtcol)
1415 {
1416 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1417 i = (int)curwin->w_virtcol - vcol;
1418 ptr = alloc(i + 1);
1419 if (ptr != NULL)
1420 {
1421 new_cursor_col += i;
1422 ptr[i] = NUL;
1423 while (--i >= 0)
1424 ptr[i] = ' ';
1425 ins_str(ptr);
1426 vim_free(ptr);
1427 }
1428 }
1429
1430 // When changing the indent while the cursor is in it, reset
1431 // Insstart_col to 0.
1432 insstart_less = MAXCOL;
1433 }
1434
1435 curwin->w_p_list = save_p_list;
1436
1437 if (new_cursor_col <= 0)
1438 curwin->w_cursor.col = 0;
1439 else
1440 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1441 curwin->w_set_curswant = TRUE;
1442 changed_cline_bef_curs();
1443
1444 // May have to adjust the start of the insert.
Bram Moolenaar24959102022-05-07 20:01:16 +01001445 if (State & MODE_INSERT)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001446 {
1447 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1448 {
1449 if ((int)Insstart.col <= insstart_less)
1450 Insstart.col = 0;
1451 else
1452 Insstart.col -= insstart_less;
1453 }
1454 if ((int)ai_col <= insstart_less)
1455 ai_col = 0;
1456 else
1457 ai_col -= insstart_less;
1458 }
1459
Bram Moolenaar24959102022-05-07 20:01:16 +01001460 // For MODE_REPLACE state, may have to fix the replace stack, if it's
1461 // possible. If the number of characters before the cursor decreased, need
1462 // to pop a few characters from the replace stack.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001463 // If the number of characters before the cursor increased, need to push a
1464 // few NULs onto the replace stack.
1465 if (REPLACE_NORMAL(State) && start_col >= 0)
1466 {
1467 while (start_col > (int)curwin->w_cursor.col)
1468 {
1469 replace_join(0); // remove a NUL from the replace stack
1470 --start_col;
1471 }
1472 while (start_col < (int)curwin->w_cursor.col || replaced)
1473 {
1474 replace_push(NUL);
1475 if (replaced)
1476 {
1477 replace_push(replaced);
1478 replaced = NUL;
1479 }
1480 ++start_col;
1481 }
1482 }
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001483#ifdef FEAT_PROP_POPUP
1484 ignore_text_props = FALSE;
1485#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001486
Bram Moolenaar24959102022-05-07 20:01:16 +01001487 // For MODE_VREPLACE state, we also have to fix the replace stack. In this
1488 // case it is always possible because we backspace over the whole line and
1489 // then put it back again the way we wanted it.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001490 if (State & VREPLACE_FLAG)
1491 {
1492 // If orig_line didn't allocate, just return. At least we did the job,
1493 // even if you can't backspace.
1494 if (orig_line == NULL)
1495 return;
1496
1497 // Save new line
1498 new_line = vim_strsave(ml_get_curline());
1499 if (new_line == NULL)
1500 return;
1501
1502 // We only put back the new line up to the cursor
1503 new_line[curwin->w_cursor.col] = NUL;
1504
1505 // Put back original line
1506 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1507 curwin->w_cursor.col = orig_col;
1508
1509 // Backspace from cursor to start of line
1510 backspace_until_column(0);
1511
1512 // Insert new stuff into line again
1513 ins_bytes(new_line);
1514
1515 vim_free(new_line);
1516 }
1517}
1518
1519/*
1520 * Copy the indent from ptr to the current line (and fill to size)
1521 * Leaves the cursor on the first non-blank in the line.
1522 * Returns TRUE if the line was changed.
1523 */
1524 int
1525copy_indent(int size, char_u *src)
1526{
1527 char_u *p = NULL;
1528 char_u *line = NULL;
1529 char_u *s;
1530 int todo;
1531 int ind_len;
1532 int line_len = 0;
1533 int tab_pad;
1534 int ind_done;
1535 int round;
1536#ifdef FEAT_VARTABS
1537 int ind_col;
1538#endif
1539
1540 // Round 1: compute the number of characters needed for the indent
1541 // Round 2: copy the characters.
1542 for (round = 1; round <= 2; ++round)
1543 {
1544 todo = size;
1545 ind_len = 0;
1546 ind_done = 0;
1547#ifdef FEAT_VARTABS
1548 ind_col = 0;
1549#endif
1550 s = src;
1551
1552 // Count/copy the usable portion of the source line
1553 while (todo > 0 && VIM_ISWHITE(*s))
1554 {
1555 if (*s == TAB)
1556 {
1557#ifdef FEAT_VARTABS
1558 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1559 curbuf->b_p_vts_array);
1560#else
1561 tab_pad = (int)curbuf->b_p_ts
1562 - (ind_done % (int)curbuf->b_p_ts);
1563#endif
1564 // Stop if this tab will overshoot the target
1565 if (todo < tab_pad)
1566 break;
1567 todo -= tab_pad;
1568 ind_done += tab_pad;
1569#ifdef FEAT_VARTABS
1570 ind_col += tab_pad;
1571#endif
1572 }
1573 else
1574 {
1575 --todo;
1576 ++ind_done;
1577#ifdef FEAT_VARTABS
1578 ++ind_col;
1579#endif
1580 }
1581 ++ind_len;
1582 if (p != NULL)
1583 *p++ = *s;
1584 ++s;
1585 }
1586
1587 // Fill to next tabstop with a tab, if possible
1588#ifdef FEAT_VARTABS
1589 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1590 curbuf->b_p_vts_array);
1591#else
1592 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1593#endif
1594 if (todo >= tab_pad && !curbuf->b_p_et)
1595 {
1596 todo -= tab_pad;
1597 ++ind_len;
1598#ifdef FEAT_VARTABS
1599 ind_col += tab_pad;
1600#endif
1601 if (p != NULL)
1602 *p++ = TAB;
1603 }
1604
1605 // Add tabs required for indent
1606 if (!curbuf->b_p_et)
1607 {
1608#ifdef FEAT_VARTABS
1609 for (;;)
1610 {
1611 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1612 curbuf->b_p_vts_array);
1613 if (todo < tab_pad)
1614 break;
1615 todo -= tab_pad;
1616 ++ind_len;
1617 ind_col += tab_pad;
1618 if (p != NULL)
1619 *p++ = TAB;
1620 }
1621#else
1622 while (todo >= (int)curbuf->b_p_ts)
1623 {
1624 todo -= (int)curbuf->b_p_ts;
1625 ++ind_len;
1626 if (p != NULL)
1627 *p++ = TAB;
1628 }
1629#endif
1630 }
1631
1632 // Count/add spaces required for indent
1633 while (todo > 0)
1634 {
1635 --todo;
1636 ++ind_len;
1637 if (p != NULL)
1638 *p++ = ' ';
1639 }
1640
1641 if (p == NULL)
1642 {
1643 // Allocate memory for the result: the copied indent, new indent
1644 // and the rest of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01001645 line_len = ml_get_curline_len() + 1;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001646 line = alloc(ind_len + line_len);
1647 if (line == NULL)
1648 return FALSE;
1649 p = line;
1650 }
1651 }
1652
1653 // Append the original line
1654 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1655
1656 // Replace the line
1657 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1658
1659 // Put the cursor after the indent.
1660 curwin->w_cursor.col = ind_len;
1661 return TRUE;
1662}
1663
1664/*
Bram Moolenaar308660b2022-06-16 12:10:48 +01001665 * Give a "resulting text too long" error and maybe set got_int.
1666 */
1667 static void
1668emsg_text_too_long(void)
1669{
1670 emsg(_(e_resulting_text_too_long));
1671#ifdef FEAT_EVAL
1672 // when not inside a try/catch set got_int to break out of any loop
1673 if (trylevel == 0)
1674#endif
1675 got_int = TRUE;
1676}
1677
1678/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001679 * ":retab".
1680 */
1681 void
1682ex_retab(exarg_T *eap)
1683{
1684 linenr_T lnum;
1685 int got_tab = FALSE;
1686 long num_spaces = 0;
1687 long num_tabs;
1688 long len;
1689 long col;
1690 long vcol;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001691 long start_col = 0; // For start of white-space string
1692 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001693 long old_len;
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001694 long new_len;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001695 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001696 char_u *new_line = (char_u *)1; // init to non-NULL
1697 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001698#ifdef FEAT_VARTABS
1699 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001700 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001701#else
1702 int temp;
1703 int new_ts;
1704#endif
1705 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001706 linenr_T first_line = 0; // first changed line
1707 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001708
1709 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001710 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001711
1712#ifdef FEAT_VARTABS
1713 new_ts_str = eap->arg;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02001714 if (tabstop_set(eap->arg, &new_vts_array) == FAIL)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001715 return;
1716 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1717 ++(eap->arg);
1718
1719 // This ensures that either new_vts_array and new_ts_str are freshly
1720 // allocated, or new_vts_array points to an existing array and new_ts_str
1721 // is null.
1722 if (new_vts_array == NULL)
1723 {
1724 new_vts_array = curbuf->b_p_vts_array;
1725 new_ts_str = NULL;
1726 }
1727 else
1728 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1729#else
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001730 ptr = eap->arg;
1731 new_ts = getdigits(&ptr);
1732 if (new_ts < 0 && *eap->arg == '-')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001733 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001734 emsg(_(e_argument_must_be_positive));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001735 return;
1736 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00001737 if (new_ts < 0 || new_ts > TABSTOP_MAX)
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001738 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001739 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001740 return;
1741 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001742 if (new_ts == 0)
1743 new_ts = curbuf->b_p_ts;
1744#endif
1745 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1746 {
1747 ptr = ml_get(lnum);
1748 col = 0;
1749 vcol = 0;
1750 did_undo = FALSE;
1751 for (;;)
1752 {
1753 if (VIM_ISWHITE(ptr[col]))
1754 {
1755 if (!got_tab && num_spaces == 0)
1756 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001757 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001758 start_vcol = vcol;
1759 start_col = col;
1760 }
1761 if (ptr[col] == ' ')
1762 num_spaces++;
1763 else
1764 got_tab = TRUE;
1765 }
1766 else
1767 {
1768 if (got_tab || (eap->forceit && num_spaces > 1))
1769 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001770 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001771
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001772 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001773 len = num_spaces = vcol - start_vcol;
1774 num_tabs = 0;
1775 if (!curbuf->b_p_et)
1776 {
1777#ifdef FEAT_VARTABS
1778 int t, s;
1779
1780 tabstop_fromto(start_vcol, vcol,
1781 curbuf->b_p_ts, new_vts_array, &t, &s);
1782 num_tabs = t;
1783 num_spaces = s;
1784#else
1785 temp = new_ts - (start_vcol % new_ts);
1786 if (num_spaces >= temp)
1787 {
1788 num_spaces -= temp;
1789 num_tabs++;
1790 }
1791 num_tabs += num_spaces / new_ts;
1792 num_spaces -= (num_spaces / new_ts) * new_ts;
1793#endif
1794 }
1795 if (curbuf->b_p_et || got_tab ||
1796 (num_spaces + num_tabs < len))
1797 {
1798 if (did_undo == FALSE)
1799 {
1800 did_undo = TRUE;
1801 if (u_save((linenr_T)(lnum - 1),
1802 (linenr_T)(lnum + 1)) == FAIL)
1803 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001804 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001805 break;
1806 }
1807 }
1808
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001809 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001810 len = num_spaces + num_tabs;
1811 old_len = (long)STRLEN(ptr);
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001812 new_len = old_len - col + start_col + len + 1;
Bram Moolenaar45491662022-02-12 21:59:51 +00001813 if (new_len <= 0 || new_len >= MAXCOL)
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001814 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001815 emsg_text_too_long();
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001816 break;
1817 }
1818 new_line = alloc(new_len);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001819 if (new_line == NULL)
1820 break;
1821 if (start_col > 0)
1822 mch_memmove(new_line, ptr, (size_t)start_col);
1823 mch_memmove(new_line + start_col + len,
1824 ptr + col, (size_t)(old_len - col + 1));
1825 ptr = new_line + start_col;
1826 for (col = 0; col < len; col++)
1827 ptr[col] = (col < num_tabs) ? '\t' : ' ';
Bram Moolenaar0dcd39b2021-02-03 19:44:25 +01001828 if (ml_replace(lnum, new_line, FALSE) == OK)
1829 // "new_line" may have been copied
1830 new_line = curbuf->b_ml.ml_line_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001831 if (first_line == 0)
1832 first_line = lnum;
1833 last_line = lnum;
1834 ptr = new_line;
1835 col = start_col + len;
1836 }
1837 }
1838 got_tab = FALSE;
1839 num_spaces = 0;
1840 }
1841 if (ptr[col] == NUL)
1842 break;
1843 vcol += chartabsize(ptr + col, (colnr_T)vcol);
Bram Moolenaar6e287032022-02-12 15:42:18 +00001844 if (vcol >= MAXCOL)
1845 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001846 emsg_text_too_long();
Bram Moolenaar6e287032022-02-12 15:42:18 +00001847 break;
1848 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001849 if (has_mbyte)
1850 col += (*mb_ptr2len)(ptr + col);
1851 else
1852 ++col;
1853 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001854 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001855 break;
1856 line_breakcheck();
1857 }
1858 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001859 emsg(_(e_interrupted));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001860
1861#ifdef FEAT_VARTABS
1862 // If a single value was given then it can be considered equal to
1863 // either the value of 'tabstop' or the value of 'vartabstop'.
1864 if (tabstop_count(curbuf->b_p_vts_array) == 0
1865 && tabstop_count(new_vts_array) == 1
1866 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001867 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001868 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001869 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001870 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001871 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001872 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001873#else
1874 if (curbuf->b_p_ts != new_ts)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001875 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001876#endif
1877 if (first_line != 0)
1878 changed_lines(first_line, 0, last_line + 1, 0L);
1879
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001880 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001881
1882#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001883 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001884 {
1885 // If 'vartabstop' is in use or if the value given to retab has more
1886 // than one tabstop then update 'vartabstop'.
1887 int *old_vts_ary = curbuf->b_p_vts_array;
1888
1889 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1890 {
1891 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1892 OPT_FREE|OPT_LOCAL, 0);
1893 curbuf->b_p_vts_array = new_vts_array;
1894 vim_free(old_vts_ary);
1895 }
1896 else
1897 {
1898 // 'vartabstop' wasn't in use and a single value was given to
1899 // retab then update 'tabstop'.
1900 curbuf->b_p_ts = tabstop_first(new_vts_array);
1901 vim_free(new_vts_array);
1902 }
1903 vim_free(new_ts_str);
1904 }
1905#else
1906 curbuf->b_p_ts = new_ts;
1907#endif
1908 coladvance(curwin->w_curswant);
1909
1910 u_clearline();
1911}
1912
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001913#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001914/*
1915 * Get indent level from 'indentexpr'.
1916 */
1917 int
1918get_expr_indent(void)
1919{
1920 int indent = -1;
1921 char_u *inde_copy;
1922 pos_T save_pos;
1923 colnr_T save_curswant;
1924 int save_set_curswant;
1925 int save_State;
1926 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1927 OPT_LOCAL);
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001928 sctx_T save_sctx = current_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001929
1930 // Save and restore cursor position and curswant, in case it was changed
1931 // via :normal commands
1932 save_pos = curwin->w_cursor;
1933 save_curswant = curwin->w_curswant;
1934 save_set_curswant = curwin->w_set_curswant;
1935 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1936 if (use_sandbox)
1937 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001938 ++textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001939 current_sctx = curbuf->b_p_script_ctx[BV_INDE];
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001940
1941 // Need to make a copy, the 'indentexpr' option could be changed while
1942 // evaluating it.
1943 inde_copy = vim_strsave(curbuf->b_p_inde);
1944 if (inde_copy != NULL)
1945 {
Bram Moolenaar3292a222022-10-01 20:17:17 +01001946 indent = (int)eval_to_number(inde_copy, TRUE);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001947 vim_free(inde_copy);
1948 }
1949
1950 if (use_sandbox)
1951 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001952 --textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001953 current_sctx = save_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001954
1955 // Restore the cursor position so that 'indentexpr' doesn't need to.
1956 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1957 // command.
1958 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001959 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001960 curwin->w_cursor = save_pos;
1961 curwin->w_curswant = save_curswant;
1962 curwin->w_set_curswant = save_set_curswant;
1963 check_cursor();
1964 State = save_State;
1965
Bram Moolenaar620c9592021-07-31 21:32:31 +02001966 // Reset did_throw, unless 'debug' has "throw" and inside a try/catch.
1967 if (did_throw && (vim_strchr(p_debug, 't') == NULL || trylevel == 0))
1968 {
1969 handle_did_throw();
1970 did_throw = FALSE;
1971 }
1972
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001973 // If there is an error, just keep the current indent.
1974 if (indent < 0)
1975 indent = get_indent();
1976
1977 return indent;
1978}
1979#endif
1980
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001981 static int
1982lisp_match(char_u *p)
1983{
1984 char_u buf[LSIZE];
1985 int len;
1986 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1987
1988 while (*word != NUL)
1989 {
1990 (void)copy_option_part(&word, buf, LSIZE, ",");
1991 len = (int)STRLEN(buf);
Bram Moolenaard26c5802022-10-13 12:30:08 +01001992 if (STRNCMP(buf, p, len) == 0 && IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001993 return TRUE;
1994 }
1995 return FALSE;
1996}
1997
1998/*
1999 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
2000 * The incompatible newer method is quite a bit better at indenting
2001 * code in lisp-like languages than the traditional one; it's still
2002 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
2003 *
2004 * TODO:
2005 * Findmatch() should be adapted for lisp, also to make showmatch
2006 * work correctly: now (v5.3) it seems all C/C++ oriented:
2007 * - it does not recognize the #\( and #\) notations as character literals
2008 * - it doesn't know about comments starting with a semicolon
2009 * - it incorrectly interprets '(' as a character literal
2010 * All this messes up get_lisp_indent in some rare cases.
2011 * Update from Sergey Khorev:
2012 * I tried to fix the first two issues.
2013 */
2014 int
2015get_lisp_indent(void)
2016{
2017 pos_T *pos, realpos, paren;
2018 int amount;
2019 char_u *that;
2020 colnr_T col;
2021 colnr_T firsttry;
2022 int parencount, quotecount;
2023 int vi_lisp;
2024
2025 // Set vi_lisp to use the vi-compatible method
2026 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
2027
2028 realpos = curwin->w_cursor;
2029 curwin->w_cursor.col = 0;
2030
2031 if ((pos = findmatch(NULL, '(')) == NULL)
2032 pos = findmatch(NULL, '[');
2033 else
2034 {
2035 paren = *pos;
2036 pos = findmatch(NULL, '[');
2037 if (pos == NULL || LT_POSP(pos, &paren))
2038 pos = &paren;
2039 }
2040 if (pos != NULL)
2041 {
2042 // Extra trick: Take the indent of the first previous non-white
2043 // line that is at the same () level.
2044 amount = -1;
2045 parencount = 0;
2046
2047 while (--curwin->w_cursor.lnum >= pos->lnum)
2048 {
2049 if (linewhite(curwin->w_cursor.lnum))
2050 continue;
2051 for (that = ml_get_curline(); *that != NUL; ++that)
2052 {
2053 if (*that == ';')
2054 {
2055 while (*(that + 1) != NUL)
2056 ++that;
2057 continue;
2058 }
2059 if (*that == '\\')
2060 {
2061 if (*(that + 1) != NUL)
2062 ++that;
2063 continue;
2064 }
2065 if (*that == '"' && *(that + 1) != NUL)
2066 {
2067 while (*++that && *that != '"')
2068 {
2069 // skipping escaped characters in the string
2070 if (*that == '\\')
2071 {
2072 if (*++that == NUL)
2073 break;
2074 if (that[1] == NUL)
2075 {
2076 ++that;
2077 break;
2078 }
2079 }
2080 }
Bram Moolenaar0e8e9382022-06-18 12:51:11 +01002081 if (*that == NUL)
2082 break;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002083 }
2084 if (*that == '(' || *that == '[')
2085 ++parencount;
2086 else if (*that == ')' || *that == ']')
2087 --parencount;
2088 }
2089 if (parencount == 0)
2090 {
2091 amount = get_indent();
2092 break;
2093 }
2094 }
2095
2096 if (amount == -1)
2097 {
2098 curwin->w_cursor.lnum = pos->lnum;
2099 curwin->w_cursor.col = pos->col;
2100 col = pos->col;
2101
2102 that = ml_get_curline();
2103
2104 if (vi_lisp && get_indent() == 0)
2105 amount = 2;
2106 else
2107 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002108 char_u *line = that;
2109 chartabsize_T cts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002110
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002111 init_chartabsize_arg(&cts, curwin, pos->lnum, 0, line, line);
2112 while (*cts.cts_ptr != NUL && col > 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002113 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002114 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002115 col--;
2116 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002117 amount = cts.cts_vcol;
2118 that = cts.cts_ptr;
2119 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002120
2121 // Some keywords require "body" indenting rules (the
2122 // non-standard-lisp ones are Scheme special forms):
2123 //
2124 // (let ((a 1)) instead (let ((a 1))
2125 // (...)) of (...))
2126
2127 if (!vi_lisp && (*that == '(' || *that == '[')
2128 && lisp_match(that + 1))
2129 amount += 2;
2130 else
2131 {
Bram Moolenaar8eba2bd2022-06-22 19:59:28 +01002132 if (*that != NUL)
2133 {
2134 that++;
2135 amount++;
2136 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002137 firsttry = amount;
2138
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002139 init_chartabsize_arg(&cts, curwin, (colnr_T)(that - line),
2140 amount, line, that);
2141 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002142 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002143 cts.cts_vcol += lbr_chartabsize(&cts);
2144 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002145 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002146 that = cts.cts_ptr;
2147 amount = cts.cts_vcol;
2148 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002149
2150 if (*that && *that != ';') // not a comment line
2151 {
2152 // test *that != '(' to accommodate first let/do
2153 // argument if it is more than one line
2154 if (!vi_lisp && *that != '(' && *that != '[')
2155 firsttry++;
2156
2157 parencount = 0;
2158 quotecount = 0;
2159
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002160 init_chartabsize_arg(&cts, curwin,
2161 (colnr_T)(that - line), amount, line, that);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002162 if (vi_lisp
2163 || (*that != '"'
2164 && *that != '\''
2165 && *that != '#'
2166 && (*that < '0' || *that > '9')))
2167 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002168 while (*cts.cts_ptr
2169 && (!VIM_ISWHITE(*cts.cts_ptr)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002170 || quotecount
2171 || parencount)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002172 && (!((*cts.cts_ptr == '('
2173 || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002174 && !quotecount
2175 && !parencount
2176 && vi_lisp)))
2177 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002178 if (*cts.cts_ptr == '"')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002179 quotecount = !quotecount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002180 if ((*cts.cts_ptr == '(' || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002181 && !quotecount)
2182 ++parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002183 if ((*cts.cts_ptr == ')' || *cts.cts_ptr == ']')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002184 && !quotecount)
2185 --parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002186 if (*cts.cts_ptr == '\\'
2187 && *(cts.cts_ptr+1) != NUL)
2188 cts.cts_vcol += lbr_chartabsize_adv(&cts);
2189 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002190 }
2191 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002192 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002193 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002194 cts.cts_vcol += lbr_chartabsize(&cts);
2195 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002196 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002197 that = cts.cts_ptr;
2198 amount = cts.cts_vcol;
2199 clear_chartabsize_arg(&cts);
2200
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002201 if (!*that || *that == ';')
2202 amount = firsttry;
2203 }
2204 }
2205 }
2206 }
2207 }
2208 else
2209 amount = 0; // no matching '(' or '[' found, use zero indent
2210
2211 curwin->w_cursor = realpos;
2212
2213 return amount;
2214}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002215
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002216/*
2217 * Re-indent the current line, based on the current contents of it and the
2218 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
2219 * confused what all the part that handles Control-T is doing that I'm not.
2220 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
2221 */
2222
2223 void
2224fixthisline(int (*get_the_indent)(void))
2225{
2226 int amount = get_the_indent();
2227
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002228 if (amount < 0)
2229 return;
2230
2231 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
2232 if (linewhite(curwin->w_cursor.lnum))
2233 did_ai = TRUE; // delete the indent if the line stays empty
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002234}
2235
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002236/*
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002237 * Return TRUE if 'indentexpr' should be used for Lisp indenting.
2238 * Caller may want to check 'autoindent'.
2239 */
2240 int
2241use_indentexpr_for_lisp(void)
2242{
2243#ifdef FEAT_EVAL
2244 return curbuf->b_p_lisp
2245 && *curbuf->b_p_inde != NUL
2246 && STRCMP(curbuf->b_p_lop, "expr:1") == 0;
2247#else
2248 return FALSE;
2249#endif
2250}
2251
2252/*
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002253 * Fix indent for 'lisp' and 'cindent'.
2254 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002255 void
2256fix_indent(void)
2257{
2258 if (p_paste)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002259 return; // no auto-indenting when 'paste' is set
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002260 if (curbuf->b_p_lisp && curbuf->b_p_ai)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002261 {
2262 if (use_indentexpr_for_lisp())
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002263 do_c_expr_indent();
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002264 else
2265 fixthisline(get_lisp_indent);
2266 }
2267 else if (cindent_on())
2268 do_c_expr_indent();
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002269}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002270
2271#if defined(FEAT_EVAL) || defined(PROTO)
2272/*
2273 * "indent()" function
2274 */
2275 void
2276f_indent(typval_T *argvars, typval_T *rettv)
2277{
2278 linenr_T lnum;
2279
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002280 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2281 return;
2282
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002283 lnum = tv_get_lnum(argvars);
2284 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2285 rettv->vval.v_number = get_indent_lnum(lnum);
2286 else
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002287 {
2288 if (in_vim9script())
2289 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002290 rettv->vval.v_number = -1;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002291 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002292}
2293
2294/*
2295 * "lispindent(lnum)" function
2296 */
2297 void
2298f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2299{
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002300 pos_T pos;
2301 linenr_T lnum;
2302
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002303 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2304 return;
2305
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002306 pos = curwin->w_cursor;
2307 lnum = tv_get_lnum(argvars);
2308 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2309 {
2310 curwin->w_cursor.lnum = lnum;
2311 rettv->vval.v_number = get_lisp_indent();
2312 curwin->w_cursor = pos;
2313 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002314 else if (in_vim9script())
2315 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002316 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002317 rettv->vval.v_number = -1;
2318}
2319#endif