blob: e7de005ae0eb3e4bcd2ffe40c8592ff6fd8da2a3 [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/*
Millyb38700a2024-10-22 22:59:39 +0200872 * Check "briopt" as 'breakindentopt' and update the members of "wp".
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100873 * This is called when 'breakindentopt' is changed and when a window is
874 * initialized.
Millyb38700a2024-10-22 22:59:39 +0200875 * Returns FAIL for failure, OK otherwise.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100876 */
877 int
Millyb38700a2024-10-22 22:59:39 +0200878briopt_check(
879 char_u *briopt, // when NULL: use "wp->w_p_briopt"
880 win_T *wp) // when NULL: only check "briopt"
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100881{
882 char_u *p;
883 int bri_shift = 0;
884 long bri_min = 20;
885 int bri_sbr = FALSE;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200886 int bri_list = 0;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100887 int bri_vcol = 0;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100888
Millyb38700a2024-10-22 22:59:39 +0200889 if (briopt != NULL)
890 p = briopt;
891 else
892 p = wp->w_p_briopt;
893
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100894 while (*p != NUL)
895 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200896 // Note: Keep this in sync with p_briopt_values
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100897 if (STRNCMP(p, "shift:", 6) == 0
898 && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
899 {
900 p += 6;
901 bri_shift = getdigits(&p);
902 }
903 else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4]))
904 {
905 p += 4;
906 bri_min = getdigits(&p);
907 }
908 else if (STRNCMP(p, "sbr", 3) == 0)
909 {
910 p += 3;
911 bri_sbr = TRUE;
912 }
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200913 else if (STRNCMP(p, "list:", 5) == 0)
914 {
915 p += 5;
916 bri_list = getdigits(&p);
917 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100918 else if (STRNCMP(p, "column:", 7) == 0)
919 {
920 p += 7;
921 bri_vcol = getdigits(&p);
922 }
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100923 if (*p != ',' && *p != NUL)
924 return FAIL;
925 if (*p == ',')
926 ++p;
927 }
928
Millyb38700a2024-10-22 22:59:39 +0200929 if (wp == NULL)
930 return OK;
931
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100932 wp->w_briopt_shift = bri_shift;
933 wp->w_briopt_min = bri_min;
934 wp->w_briopt_sbr = bri_sbr;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200935 wp->w_briopt_list = bri_list;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100936 wp->w_briopt_vcol = bri_vcol;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100937
938 return OK;
939}
940
941/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200942 * Return appropriate space number for breakindent, taking influencing
943 * parameters into account. Window must be specified, since it is not
944 * necessarily always the current one.
945 */
946 int
947get_breakindent_win(
948 win_T *wp,
949 char_u *line) // start of the line
950{
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000951 static int prev_indent = 0; // cached indent value
952 static long prev_ts = 0L; // cached tabstop value
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200953# ifdef FEAT_VARTABS
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000954 static int *prev_vts = NULL; // cached vartabs values
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200955# endif
zeertzjqefabd7c2024-02-11 17:16:19 +0100956 static int prev_fnum = 0; // cached buffer number
957 static char_u *prev_line = NULL; // cached copy of "line"
958 static varnumber_T prev_tick = 0; // changedtick of cached value
959 static int prev_list = 0; // cached list indent
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000960 static int prev_listopt = 0; // cached w_p_briopt_list value
zeertzjqefabd7c2024-02-11 17:16:19 +0100961 static int prev_no_ts = FALSE; // cached no_ts value
962 static unsigned prev_dy_uhex = 0; // cached 'display' "uhex" value
Christian Brabandtc53b4672022-01-15 10:01:05 +0000963 // cached formatlistpat value
964 static char_u *prev_flp = NULL;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200965 int bri = 0;
966 // window width minus window margin space, i.e. what rests for text
967 const int eff_wwidth = wp->w_width
zeertzjqf0a9d652024-02-12 22:53:20 +0100968 - win_col_off(wp) + win_col_off2(wp);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200969
zeertzjqefabd7c2024-02-11 17:16:19 +0100970 // In list mode, if 'listchars' "tab" isn't set, a TAB is displayed as ^I.
971 int no_ts = wp->w_p_list && wp->w_lcs_chars.tab1 == NUL;
972
973 // Used cached indent, unless
974 // - buffer changed, or
975 // - 'tabstop' changed, or
976 // - 'vartabstop' changed, or
977 // - buffer was changed, or
978 // - 'breakindentopt' "list" changed, or
979 // - 'list' or 'listchars' "tab" changed, or
980 // - 'display' "uhex" flag changed, or
981 // - 'formatlistpat' changed, or
982 // - line changed.
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100983 if (prev_fnum != wp->w_buffer->b_fnum
984 || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200985# ifdef FEAT_VARTABS
986 || prev_vts != wp->w_buffer->b_p_vts_array
987# endif
zeertzjqefabd7c2024-02-11 17:16:19 +0100988 || prev_tick != CHANGEDTICK(wp->w_buffer)
989 || prev_listopt != wp->w_briopt_list
990 || prev_no_ts != no_ts
991 || prev_dy_uhex != (dy_flags & DY_UHEX)
992 || prev_flp == NULL
993 || STRCMP(prev_flp, get_flp_value(wp->w_buffer)) != 0
994 || prev_line == NULL || STRCMP(prev_line, line) != 0
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200995 )
996 {
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100997 prev_fnum = wp->w_buffer->b_fnum;
998 vim_free(prev_line);
999 prev_line = vim_strsave(line);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001000 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001001# ifdef FEAT_VARTABS
1002 prev_vts = wp->w_buffer->b_p_vts_array;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001003 if (wp->w_briopt_vcol == 0)
1004 prev_indent = get_indent_str_vtab(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001005 (int)wp->w_buffer->b_p_ts,
zeertzjqefabd7c2024-02-11 17:16:19 +01001006 wp->w_buffer->b_p_vts_array, no_ts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001007# else
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001008 if (wp->w_briopt_vcol == 0)
1009 prev_indent = get_indent_str(line,
zeertzjqd9be94c2024-07-14 10:20:20 +02001010 (int)wp->w_buffer->b_p_ts, no_ts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001011# endif
zeertzjqefabd7c2024-02-11 17:16:19 +01001012 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001013 prev_listopt = wp->w_briopt_list;
Christian Brabandtc53b4672022-01-15 10:01:05 +00001014 prev_list = 0;
zeertzjqefabd7c2024-02-11 17:16:19 +01001015 prev_no_ts = no_ts;
1016 prev_dy_uhex = (dy_flags & DY_UHEX);
Christian Brabandtc53b4672022-01-15 10:01:05 +00001017 vim_free(prev_flp);
1018 prev_flp = vim_strsave(get_flp_value(wp->w_buffer));
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001019 // add additional indent for numbered lists
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001020 if (wp->w_briopt_list != 0 && wp->w_briopt_vcol == 0)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001021 {
1022 regmatch_T regmatch;
1023
Christian Brabandtc53b4672022-01-15 10:01:05 +00001024 regmatch.regprog = vim_regcomp(prev_flp,
1025 RE_MAGIC + RE_STRING + RE_AUTO + RE_STRICT);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001026
1027 if (regmatch.regprog != NULL)
1028 {
1029 regmatch.rm_ic = FALSE;
1030 if (vim_regexec(&regmatch, line, 0))
1031 {
1032 if (wp->w_briopt_list > 0)
1033 prev_list = wp->w_briopt_list;
1034 else
zeertzjq61a6ac42024-09-07 11:23:54 +02001035 {
1036 char_u *ptr = *regmatch.startp;
1037 char_u *end_ptr = *regmatch.endp;
1038 int indent = 0;
1039
1040 // Compute the width of the matched text.
1041 // Use win_chartabsize() so that TAB size is correct,
1042 // while wrapping is ignored.
1043 while (ptr < end_ptr)
1044 {
1045 indent += win_chartabsize(wp, ptr, indent);
1046 MB_PTR_ADV(ptr);
1047 }
1048 prev_indent = indent;
1049 }
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001050 }
1051 vim_regfree(regmatch.regprog);
1052 }
1053 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001054 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001055 if (wp->w_briopt_vcol != 0)
1056 {
1057 // column value has priority
1058 bri = wp->w_briopt_vcol;
1059 prev_list = 0;
1060 }
1061 else
1062 bri = prev_indent + wp->w_briopt_shift;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001063
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001064 // Add offset for number column, if 'n' is in 'cpoptions'
1065 bri += win_col_off2(wp);
1066
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001067 // add additional indent for numbered lists
Maxim Kim11916722022-09-02 14:08:53 +01001068 if (wp->w_briopt_list > 0)
1069 bri += prev_list;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001070
Maxim Kimf674b352021-07-22 11:46:59 +02001071 // indent minus the length of the showbreak string
1072 if (wp->w_briopt_sbr)
1073 bri -= vim_strsize(get_showbreak_value(wp));
1074
1075
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001076 // never indent past left window margin
1077 if (bri < 0)
1078 bri = 0;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001079
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001080 // always leave at least bri_min characters on the left,
1081 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001082 else if (bri > eff_wwidth - wp->w_briopt_min)
1083 bri = (eff_wwidth - wp->w_briopt_min < 0)
1084 ? 0 : eff_wwidth - wp->w_briopt_min;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001085
1086 return bri;
1087}
1088#endif
1089
1090/*
1091 * When extra == 0: Return TRUE if the cursor is before or on the first
1092 * non-blank in the line.
1093 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1094 * the line.
1095 */
1096 int
1097inindent(int extra)
1098{
1099 char_u *ptr;
1100 colnr_T col;
1101
1102 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
1103 ++ptr;
1104 if (col >= curwin->w_cursor.col + extra)
1105 return TRUE;
1106 else
1107 return FALSE;
1108}
1109
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001110/*
1111 * op_reindent - handle reindenting a block of lines.
1112 */
1113 void
1114op_reindent(oparg_T *oap, int (*how)(void))
1115{
Bram Moolenaar4c84dd32022-04-20 10:22:54 +01001116 long i = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001117 char_u *l;
1118 int amount;
1119 linenr_T first_changed = 0;
1120 linenr_T last_changed = 0;
1121 linenr_T start_lnum = curwin->w_cursor.lnum;
1122
1123 // Don't even try when 'modifiable' is off.
1124 if (!curbuf->b_p_ma)
1125 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001126 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001127 return;
1128 }
1129
Bram Moolenaare4686982022-04-19 18:28:45 +01001130 // Save for undo. Do this once for all lines, much faster than doing this
1131 // for each line separately, especially when undoing.
1132 if (u_savecommon(start_lnum - 1, start_lnum + oap->line_count,
1133 start_lnum + oap->line_count, FALSE) == OK)
1134 for (i = oap->line_count; --i >= 0 && !got_int; )
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001135 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001136 // it's a slow thing to do, so give feedback so there's no worry
1137 // that the computer's just hung.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001138
Bram Moolenaare4686982022-04-19 18:28:45 +01001139 if (i > 1
1140 && (i % 50 == 0 || i == oap->line_count - 1)
1141 && oap->line_count > p_report)
1142 smsg(_("%ld lines to indent... "), i);
1143
1144 // Be vi-compatible: For lisp indenting the first line is not
1145 // indented, unless there is only one line.
Bram Moolenaare4686982022-04-19 18:28:45 +01001146 if (i != oap->line_count - 1 || oap->line_count == 1
1147 || how != get_lisp_indent)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001148 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001149 l = skipwhite(ml_get_curline());
1150 if (*l == NUL) // empty or blank line
1151 amount = 0;
1152 else
1153 amount = how(); // get the indent for this line
1154
1155 if (amount >= 0 && set_indent(amount, 0))
1156 {
1157 // did change the indent, call changed_lines() later
1158 if (first_changed == 0)
1159 first_changed = curwin->w_cursor.lnum;
1160 last_changed = curwin->w_cursor.lnum;
1161 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001162 }
Bram Moolenaare4686982022-04-19 18:28:45 +01001163 ++curwin->w_cursor.lnum;
1164 curwin->w_cursor.col = 0; // make sure it's valid
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001165 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001166
1167 // put cursor on first non-blank of indented line
1168 curwin->w_cursor.lnum = start_lnum;
1169 beginline(BL_SOL | BL_FIX);
1170
1171 // Mark changed lines so that they will be redrawn. When Visual
1172 // highlighting was present, need to continue until the last line. When
1173 // there is no change still need to remove the Visual highlighting.
1174 if (last_changed != 0)
1175 changed_lines(first_changed, 0,
1176 oap->is_VIsual ? start_lnum + oap->line_count :
1177 last_changed + 1, 0L);
1178 else if (oap->is_VIsual)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001179 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001180
1181 if (oap->line_count > p_report)
1182 {
1183 i = oap->line_count - (i + 1);
1184 smsg(NGETTEXT("%ld line indented ",
1185 "%ld lines indented ", i), i);
1186 }
Bram Moolenaare1004402020-10-24 20:49:43 +02001187 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001188 {
1189 // set '[ and '] marks
1190 curbuf->b_op_start = oap->start;
1191 curbuf->b_op_end = oap->end;
1192 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001193}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001194
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001195/*
1196 * Return TRUE if lines starting with '#' should be left aligned.
1197 */
1198 int
1199preprocs_left(void)
1200{
1201 return
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001202 (curbuf->b_p_si && !curbuf->b_p_cin) ||
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001203 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1204 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001205 ;
1206}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001207
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001208/*
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001209 * Return TRUE if the conditions are OK for smart indenting.
1210 */
1211 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001212may_do_si(void)
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001213{
1214 return curbuf->b_p_si
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001215 && !curbuf->b_p_cin
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001216# ifdef FEAT_EVAL
1217 && *curbuf->b_p_inde == NUL
1218# endif
1219 && !p_paste;
1220}
1221
1222/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001223 * Try to do some very smart auto-indenting.
1224 * Used when inserting a "normal" character.
1225 */
1226 void
1227ins_try_si(int c)
1228{
1229 pos_T *pos, old_pos;
1230 char_u *ptr;
1231 int i;
1232 int temp;
1233
1234 // do some very smart indenting when entering '{' or '}'
Bram Moolenaar2e444bb2022-05-14 12:54:23 +01001235 if (((did_si || can_si_back) && c == '{')
1236 || (can_si && c == '}' && inindent(0)))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001237 {
1238 // for '}' set indent equal to indent of line containing matching '{'
1239 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1240 {
1241 old_pos = curwin->w_cursor;
1242 // If the matching '{' has a ')' immediately before it (ignoring
1243 // white-space), then line up with the start of the line
1244 // containing the matching '(' if there is one. This handles the
1245 // case where an "if (..\n..) {" statement continues over multiple
1246 // lines -- webb
1247 ptr = ml_get(pos->lnum);
1248 i = pos->col;
1249 if (i > 0) // skip blanks before '{'
1250 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1251 ;
1252 curwin->w_cursor.lnum = pos->lnum;
1253 curwin->w_cursor.col = i;
1254 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1255 curwin->w_cursor = *pos;
1256 i = get_indent();
1257 curwin->w_cursor = old_pos;
1258 if (State & VREPLACE_FLAG)
1259 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1260 else
1261 (void)set_indent(i, SIN_CHANGED);
1262 }
1263 else if (curwin->w_cursor.col > 0)
1264 {
1265 // when inserting '{' after "O" reduce indent, but not
1266 // more than indent of previous line
1267 temp = TRUE;
1268 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1269 {
1270 old_pos = curwin->w_cursor;
1271 i = get_indent();
1272 while (curwin->w_cursor.lnum > 1)
1273 {
1274 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1275
1276 // ignore empty lines and lines starting with '#'.
1277 if (*ptr != '#' && *ptr != NUL)
1278 break;
1279 }
1280 if (get_indent() >= i)
1281 temp = FALSE;
1282 curwin->w_cursor = old_pos;
1283 }
1284 if (temp)
1285 shift_line(TRUE, FALSE, 1, TRUE);
1286 }
1287 }
1288
1289 // set indent of '#' always to 0
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001290 if (curwin->w_cursor.col > 0 && can_si && c == '#' && inindent(0))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001291 {
1292 // remember current indent for next line
1293 old_indent = get_indent();
1294 (void)set_indent(0, SIN_CHANGED);
1295 }
1296
1297 // Adjust ai_col, the char at this position can be deleted.
1298 if (ai_col > curwin->w_cursor.col)
1299 ai_col = curwin->w_cursor.col;
1300}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001301
1302/*
1303 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1304 * Keep the cursor on the same character.
1305 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1306 * type == INDENT_DEC decrease indent (for CTRL-D)
1307 * type == INDENT_SET set indent to "amount"
1308 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1309 */
1310 void
1311change_indent(
1312 int type,
1313 int amount,
1314 int round,
1315 int replaced, // replaced character, put on replace stack
1316 int call_changed_bytes) // call changed_bytes()
1317{
1318 int vcol;
1319 int last_vcol;
1320 int insstart_less; // reduction for Insstart.col
1321 int new_cursor_col;
1322 int i;
1323 char_u *ptr;
1324 int save_p_list;
1325 int start_col;
1326 colnr_T vc;
1327 colnr_T orig_col = 0; // init for GCC
1328 char_u *new_line, *orig_line = NULL; // init for GCC
1329
Bram Moolenaar24959102022-05-07 20:01:16 +01001330 // MODE_VREPLACE state needs to know what the line was like before changing
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001331 if (State & VREPLACE_FLAG)
1332 {
1333 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1334 orig_col = curwin->w_cursor.col;
1335 }
1336
1337 // for the following tricks we don't want list mode
1338 save_p_list = curwin->w_p_list;
1339 curwin->w_p_list = FALSE;
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001340#ifdef FEAT_PROP_POPUP
1341 ignore_text_props = TRUE;
1342#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001343 vc = getvcol_nolist(&curwin->w_cursor);
1344 vcol = vc;
1345
1346 // For Replace mode we need to fix the replace stack later, which is only
1347 // possible when the cursor is in the indent. Remember the number of
1348 // characters before the cursor if it's possible.
1349 start_col = curwin->w_cursor.col;
1350
1351 // determine offset from first non-blank
1352 new_cursor_col = curwin->w_cursor.col;
1353 beginline(BL_WHITE);
1354 new_cursor_col -= curwin->w_cursor.col;
1355
1356 insstart_less = curwin->w_cursor.col;
1357
1358 // If the cursor is in the indent, compute how many screen columns the
1359 // cursor is to the left of the first non-blank.
1360 if (new_cursor_col < 0)
1361 vcol = get_indent() - vcol;
1362
1363 if (new_cursor_col > 0) // can't fix replace stack
1364 start_col = -1;
1365
1366 // Set the new indent. The cursor will be put on the first non-blank.
1367 if (type == INDENT_SET)
1368 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1369 else
1370 {
1371 int save_State = State;
1372
1373 // Avoid being called recursively.
1374 if (State & VREPLACE_FLAG)
Bram Moolenaar24959102022-05-07 20:01:16 +01001375 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001376 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1377 State = save_State;
1378 }
1379 insstart_less -= curwin->w_cursor.col;
1380
1381 // Try to put cursor on same character.
1382 // If the cursor is at or after the first non-blank in the line,
1383 // compute the cursor column relative to the column of the first
1384 // non-blank character.
1385 // If we are not in insert mode, leave the cursor on the first non-blank.
1386 // If the cursor is before the first non-blank, position it relative
1387 // to the first non-blank, counted in screen columns.
1388 if (new_cursor_col >= 0)
1389 {
1390 // When changing the indent while the cursor is touching it, reset
1391 // Insstart_col to 0.
1392 if (new_cursor_col == 0)
1393 insstart_less = MAXCOL;
1394 new_cursor_col += curwin->w_cursor.col;
1395 }
Bram Moolenaar24959102022-05-07 20:01:16 +01001396 else if (!(State & MODE_INSERT))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001397 new_cursor_col = curwin->w_cursor.col;
1398 else
1399 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001400 chartabsize_T cts;
1401
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001402 // Compute the screen column where the cursor should be.
1403 vcol = get_indent() - vcol;
1404 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1405
1406 // Advance the cursor until we reach the right screen column.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001407 last_vcol = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001408 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001409 init_chartabsize_arg(&cts, curwin, 0, 0, ptr, ptr);
1410 while (cts.cts_vcol <= (int)curwin->w_virtcol)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001411 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001412 last_vcol = cts.cts_vcol;
1413 if (cts.cts_vcol > 0)
1414 MB_PTR_ADV(cts.cts_ptr);
1415 if (*cts.cts_ptr == NUL)
Bram Moolenaar4e889f92022-02-21 19:36:12 +00001416 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001417 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001418 }
1419 vcol = last_vcol;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001420 new_cursor_col = cts.cts_ptr - cts.cts_line;
1421 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001422
1423 // May need to insert spaces to be able to position the cursor on
1424 // the right screen column.
1425 if (vcol != (int)curwin->w_virtcol)
1426 {
1427 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1428 i = (int)curwin->w_virtcol - vcol;
1429 ptr = alloc(i + 1);
1430 if (ptr != NULL)
1431 {
1432 new_cursor_col += i;
1433 ptr[i] = NUL;
1434 while (--i >= 0)
1435 ptr[i] = ' ';
1436 ins_str(ptr);
1437 vim_free(ptr);
1438 }
1439 }
1440
1441 // When changing the indent while the cursor is in it, reset
1442 // Insstart_col to 0.
1443 insstart_less = MAXCOL;
1444 }
1445
1446 curwin->w_p_list = save_p_list;
1447
1448 if (new_cursor_col <= 0)
1449 curwin->w_cursor.col = 0;
1450 else
1451 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1452 curwin->w_set_curswant = TRUE;
1453 changed_cline_bef_curs();
1454
1455 // May have to adjust the start of the insert.
Bram Moolenaar24959102022-05-07 20:01:16 +01001456 if (State & MODE_INSERT)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001457 {
1458 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1459 {
1460 if ((int)Insstart.col <= insstart_less)
1461 Insstart.col = 0;
1462 else
1463 Insstart.col -= insstart_less;
1464 }
1465 if ((int)ai_col <= insstart_less)
1466 ai_col = 0;
1467 else
1468 ai_col -= insstart_less;
1469 }
1470
Bram Moolenaar24959102022-05-07 20:01:16 +01001471 // For MODE_REPLACE state, may have to fix the replace stack, if it's
1472 // possible. If the number of characters before the cursor decreased, need
1473 // to pop a few characters from the replace stack.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001474 // If the number of characters before the cursor increased, need to push a
1475 // few NULs onto the replace stack.
1476 if (REPLACE_NORMAL(State) && start_col >= 0)
1477 {
1478 while (start_col > (int)curwin->w_cursor.col)
1479 {
1480 replace_join(0); // remove a NUL from the replace stack
1481 --start_col;
1482 }
1483 while (start_col < (int)curwin->w_cursor.col || replaced)
1484 {
1485 replace_push(NUL);
1486 if (replaced)
1487 {
1488 replace_push(replaced);
1489 replaced = NUL;
1490 }
1491 ++start_col;
1492 }
1493 }
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001494#ifdef FEAT_PROP_POPUP
1495 ignore_text_props = FALSE;
1496#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001497
Bram Moolenaar24959102022-05-07 20:01:16 +01001498 // For MODE_VREPLACE state, we also have to fix the replace stack. In this
1499 // case it is always possible because we backspace over the whole line and
1500 // then put it back again the way we wanted it.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001501 if (State & VREPLACE_FLAG)
1502 {
1503 // If orig_line didn't allocate, just return. At least we did the job,
1504 // even if you can't backspace.
1505 if (orig_line == NULL)
1506 return;
1507
1508 // Save new line
1509 new_line = vim_strsave(ml_get_curline());
1510 if (new_line == NULL)
1511 return;
1512
1513 // We only put back the new line up to the cursor
1514 new_line[curwin->w_cursor.col] = NUL;
1515
1516 // Put back original line
1517 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1518 curwin->w_cursor.col = orig_col;
1519
1520 // Backspace from cursor to start of line
1521 backspace_until_column(0);
1522
1523 // Insert new stuff into line again
1524 ins_bytes(new_line);
1525
1526 vim_free(new_line);
1527 }
1528}
1529
1530/*
1531 * Copy the indent from ptr to the current line (and fill to size)
1532 * Leaves the cursor on the first non-blank in the line.
1533 * Returns TRUE if the line was changed.
1534 */
1535 int
1536copy_indent(int size, char_u *src)
1537{
1538 char_u *p = NULL;
1539 char_u *line = NULL;
1540 char_u *s;
1541 int todo;
1542 int ind_len;
1543 int line_len = 0;
1544 int tab_pad;
1545 int ind_done;
1546 int round;
1547#ifdef FEAT_VARTABS
1548 int ind_col;
1549#endif
1550
1551 // Round 1: compute the number of characters needed for the indent
1552 // Round 2: copy the characters.
1553 for (round = 1; round <= 2; ++round)
1554 {
1555 todo = size;
1556 ind_len = 0;
1557 ind_done = 0;
1558#ifdef FEAT_VARTABS
1559 ind_col = 0;
1560#endif
1561 s = src;
1562
1563 // Count/copy the usable portion of the source line
1564 while (todo > 0 && VIM_ISWHITE(*s))
1565 {
1566 if (*s == TAB)
1567 {
1568#ifdef FEAT_VARTABS
1569 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1570 curbuf->b_p_vts_array);
1571#else
1572 tab_pad = (int)curbuf->b_p_ts
1573 - (ind_done % (int)curbuf->b_p_ts);
1574#endif
1575 // Stop if this tab will overshoot the target
1576 if (todo < tab_pad)
1577 break;
1578 todo -= tab_pad;
1579 ind_done += tab_pad;
1580#ifdef FEAT_VARTABS
1581 ind_col += tab_pad;
1582#endif
1583 }
1584 else
1585 {
1586 --todo;
1587 ++ind_done;
1588#ifdef FEAT_VARTABS
1589 ++ind_col;
1590#endif
1591 }
1592 ++ind_len;
1593 if (p != NULL)
1594 *p++ = *s;
1595 ++s;
1596 }
1597
1598 // Fill to next tabstop with a tab, if possible
1599#ifdef FEAT_VARTABS
1600 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1601 curbuf->b_p_vts_array);
1602#else
1603 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1604#endif
1605 if (todo >= tab_pad && !curbuf->b_p_et)
1606 {
1607 todo -= tab_pad;
1608 ++ind_len;
1609#ifdef FEAT_VARTABS
1610 ind_col += tab_pad;
1611#endif
1612 if (p != NULL)
1613 *p++ = TAB;
1614 }
1615
1616 // Add tabs required for indent
1617 if (!curbuf->b_p_et)
1618 {
1619#ifdef FEAT_VARTABS
1620 for (;;)
1621 {
1622 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1623 curbuf->b_p_vts_array);
1624 if (todo < tab_pad)
1625 break;
1626 todo -= tab_pad;
1627 ++ind_len;
1628 ind_col += tab_pad;
1629 if (p != NULL)
1630 *p++ = TAB;
1631 }
1632#else
1633 while (todo >= (int)curbuf->b_p_ts)
1634 {
1635 todo -= (int)curbuf->b_p_ts;
1636 ++ind_len;
1637 if (p != NULL)
1638 *p++ = TAB;
1639 }
1640#endif
1641 }
1642
1643 // Count/add spaces required for indent
1644 while (todo > 0)
1645 {
1646 --todo;
1647 ++ind_len;
1648 if (p != NULL)
1649 *p++ = ' ';
1650 }
1651
1652 if (p == NULL)
1653 {
1654 // Allocate memory for the result: the copied indent, new indent
1655 // and the rest of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01001656 line_len = ml_get_curline_len() + 1;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001657 line = alloc(ind_len + line_len);
1658 if (line == NULL)
1659 return FALSE;
1660 p = line;
1661 }
1662 }
1663
1664 // Append the original line
1665 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1666
1667 // Replace the line
1668 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1669
1670 // Put the cursor after the indent.
1671 curwin->w_cursor.col = ind_len;
1672 return TRUE;
1673}
1674
1675/*
Bram Moolenaar308660b2022-06-16 12:10:48 +01001676 * Give a "resulting text too long" error and maybe set got_int.
1677 */
1678 static void
1679emsg_text_too_long(void)
1680{
1681 emsg(_(e_resulting_text_too_long));
1682#ifdef FEAT_EVAL
1683 // when not inside a try/catch set got_int to break out of any loop
1684 if (trylevel == 0)
1685#endif
1686 got_int = TRUE;
1687}
1688
1689/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001690 * ":retab".
1691 */
1692 void
1693ex_retab(exarg_T *eap)
1694{
1695 linenr_T lnum;
1696 int got_tab = FALSE;
1697 long num_spaces = 0;
1698 long num_tabs;
1699 long len;
1700 long col;
1701 long vcol;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001702 long start_col = 0; // For start of white-space string
1703 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001704 long old_len;
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001705 long new_len;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001706 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001707 char_u *new_line = (char_u *)1; // init to non-NULL
1708 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001709#ifdef FEAT_VARTABS
1710 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001711 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001712#else
1713 int temp;
1714 int new_ts;
1715#endif
1716 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001717 linenr_T first_line = 0; // first changed line
1718 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001719
1720 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001721 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001722
1723#ifdef FEAT_VARTABS
1724 new_ts_str = eap->arg;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02001725 if (tabstop_set(eap->arg, &new_vts_array) == FAIL)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001726 return;
1727 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1728 ++(eap->arg);
1729
1730 // This ensures that either new_vts_array and new_ts_str are freshly
1731 // allocated, or new_vts_array points to an existing array and new_ts_str
1732 // is null.
1733 if (new_vts_array == NULL)
1734 {
1735 new_vts_array = curbuf->b_p_vts_array;
1736 new_ts_str = NULL;
1737 }
1738 else
1739 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1740#else
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001741 ptr = eap->arg;
1742 new_ts = getdigits(&ptr);
1743 if (new_ts < 0 && *eap->arg == '-')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001744 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001745 emsg(_(e_argument_must_be_positive));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001746 return;
1747 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00001748 if (new_ts < 0 || new_ts > TABSTOP_MAX)
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001749 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001750 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001751 return;
1752 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001753 if (new_ts == 0)
1754 new_ts = curbuf->b_p_ts;
1755#endif
1756 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1757 {
1758 ptr = ml_get(lnum);
1759 col = 0;
1760 vcol = 0;
1761 did_undo = FALSE;
1762 for (;;)
1763 {
1764 if (VIM_ISWHITE(ptr[col]))
1765 {
1766 if (!got_tab && num_spaces == 0)
1767 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001768 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001769 start_vcol = vcol;
1770 start_col = col;
1771 }
1772 if (ptr[col] == ' ')
1773 num_spaces++;
1774 else
1775 got_tab = TRUE;
1776 }
1777 else
1778 {
1779 if (got_tab || (eap->forceit && num_spaces > 1))
1780 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001781 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001782
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001783 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001784 len = num_spaces = vcol - start_vcol;
1785 num_tabs = 0;
1786 if (!curbuf->b_p_et)
1787 {
1788#ifdef FEAT_VARTABS
1789 int t, s;
1790
1791 tabstop_fromto(start_vcol, vcol,
1792 curbuf->b_p_ts, new_vts_array, &t, &s);
1793 num_tabs = t;
1794 num_spaces = s;
1795#else
1796 temp = new_ts - (start_vcol % new_ts);
1797 if (num_spaces >= temp)
1798 {
1799 num_spaces -= temp;
1800 num_tabs++;
1801 }
1802 num_tabs += num_spaces / new_ts;
1803 num_spaces -= (num_spaces / new_ts) * new_ts;
1804#endif
1805 }
1806 if (curbuf->b_p_et || got_tab ||
1807 (num_spaces + num_tabs < len))
1808 {
1809 if (did_undo == FALSE)
1810 {
1811 did_undo = TRUE;
1812 if (u_save((linenr_T)(lnum - 1),
1813 (linenr_T)(lnum + 1)) == FAIL)
1814 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001815 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001816 break;
1817 }
1818 }
1819
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001820 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001821 len = num_spaces + num_tabs;
1822 old_len = (long)STRLEN(ptr);
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001823 new_len = old_len - col + start_col + len + 1;
Bram Moolenaar45491662022-02-12 21:59:51 +00001824 if (new_len <= 0 || new_len >= MAXCOL)
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001825 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001826 emsg_text_too_long();
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001827 break;
1828 }
1829 new_line = alloc(new_len);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001830 if (new_line == NULL)
1831 break;
1832 if (start_col > 0)
1833 mch_memmove(new_line, ptr, (size_t)start_col);
1834 mch_memmove(new_line + start_col + len,
1835 ptr + col, (size_t)(old_len - col + 1));
1836 ptr = new_line + start_col;
1837 for (col = 0; col < len; col++)
1838 ptr[col] = (col < num_tabs) ? '\t' : ' ';
Bram Moolenaar0dcd39b2021-02-03 19:44:25 +01001839 if (ml_replace(lnum, new_line, FALSE) == OK)
1840 // "new_line" may have been copied
1841 new_line = curbuf->b_ml.ml_line_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001842 if (first_line == 0)
1843 first_line = lnum;
1844 last_line = lnum;
1845 ptr = new_line;
1846 col = start_col + len;
1847 }
1848 }
1849 got_tab = FALSE;
1850 num_spaces = 0;
1851 }
1852 if (ptr[col] == NUL)
1853 break;
1854 vcol += chartabsize(ptr + col, (colnr_T)vcol);
Bram Moolenaar6e287032022-02-12 15:42:18 +00001855 if (vcol >= MAXCOL)
1856 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001857 emsg_text_too_long();
Bram Moolenaar6e287032022-02-12 15:42:18 +00001858 break;
1859 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001860 if (has_mbyte)
1861 col += (*mb_ptr2len)(ptr + col);
1862 else
1863 ++col;
1864 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001865 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001866 break;
1867 line_breakcheck();
1868 }
1869 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001870 emsg(_(e_interrupted));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001871
1872#ifdef FEAT_VARTABS
1873 // If a single value was given then it can be considered equal to
1874 // either the value of 'tabstop' or the value of 'vartabstop'.
1875 if (tabstop_count(curbuf->b_p_vts_array) == 0
1876 && tabstop_count(new_vts_array) == 1
1877 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001878 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001879 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001880 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001881 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001882 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001883 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001884#else
1885 if (curbuf->b_p_ts != new_ts)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001886 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001887#endif
1888 if (first_line != 0)
1889 changed_lines(first_line, 0, last_line + 1, 0L);
1890
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001891 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001892
1893#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001894 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001895 {
1896 // If 'vartabstop' is in use or if the value given to retab has more
1897 // than one tabstop then update 'vartabstop'.
1898 int *old_vts_ary = curbuf->b_p_vts_array;
1899
1900 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1901 {
1902 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1903 OPT_FREE|OPT_LOCAL, 0);
1904 curbuf->b_p_vts_array = new_vts_array;
1905 vim_free(old_vts_ary);
1906 }
1907 else
1908 {
1909 // 'vartabstop' wasn't in use and a single value was given to
1910 // retab then update 'tabstop'.
1911 curbuf->b_p_ts = tabstop_first(new_vts_array);
1912 vim_free(new_vts_array);
1913 }
1914 vim_free(new_ts_str);
1915 }
1916#else
1917 curbuf->b_p_ts = new_ts;
1918#endif
1919 coladvance(curwin->w_curswant);
1920
1921 u_clearline();
1922}
1923
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001924#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001925/*
1926 * Get indent level from 'indentexpr'.
1927 */
1928 int
1929get_expr_indent(void)
1930{
1931 int indent = -1;
1932 char_u *inde_copy;
1933 pos_T save_pos;
1934 colnr_T save_curswant;
1935 int save_set_curswant;
1936 int save_State;
1937 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1938 OPT_LOCAL);
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001939 sctx_T save_sctx = current_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001940
1941 // Save and restore cursor position and curswant, in case it was changed
1942 // via :normal commands
1943 save_pos = curwin->w_cursor;
1944 save_curswant = curwin->w_curswant;
1945 save_set_curswant = curwin->w_set_curswant;
1946 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1947 if (use_sandbox)
1948 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001949 ++textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001950 current_sctx = curbuf->b_p_script_ctx[BV_INDE];
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001951
1952 // Need to make a copy, the 'indentexpr' option could be changed while
1953 // evaluating it.
1954 inde_copy = vim_strsave(curbuf->b_p_inde);
1955 if (inde_copy != NULL)
1956 {
Bram Moolenaar3292a222022-10-01 20:17:17 +01001957 indent = (int)eval_to_number(inde_copy, TRUE);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001958 vim_free(inde_copy);
1959 }
1960
1961 if (use_sandbox)
1962 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001963 --textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001964 current_sctx = save_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001965
1966 // Restore the cursor position so that 'indentexpr' doesn't need to.
1967 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1968 // command.
1969 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001970 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001971 curwin->w_cursor = save_pos;
1972 curwin->w_curswant = save_curswant;
1973 curwin->w_set_curswant = save_set_curswant;
1974 check_cursor();
1975 State = save_State;
1976
Bram Moolenaar620c9592021-07-31 21:32:31 +02001977 // Reset did_throw, unless 'debug' has "throw" and inside a try/catch.
1978 if (did_throw && (vim_strchr(p_debug, 't') == NULL || trylevel == 0))
1979 {
1980 handle_did_throw();
1981 did_throw = FALSE;
1982 }
1983
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001984 // If there is an error, just keep the current indent.
1985 if (indent < 0)
1986 indent = get_indent();
1987
1988 return indent;
1989}
1990#endif
1991
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001992 static int
1993lisp_match(char_u *p)
1994{
1995 char_u buf[LSIZE];
1996 int len;
1997 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1998
1999 while (*word != NUL)
2000 {
2001 (void)copy_option_part(&word, buf, LSIZE, ",");
2002 len = (int)STRLEN(buf);
Bram Moolenaard26c5802022-10-13 12:30:08 +01002003 if (STRNCMP(buf, p, len) == 0 && IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002004 return TRUE;
2005 }
2006 return FALSE;
2007}
2008
2009/*
2010 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
2011 * The incompatible newer method is quite a bit better at indenting
2012 * code in lisp-like languages than the traditional one; it's still
2013 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
2014 *
2015 * TODO:
2016 * Findmatch() should be adapted for lisp, also to make showmatch
2017 * work correctly: now (v5.3) it seems all C/C++ oriented:
2018 * - it does not recognize the #\( and #\) notations as character literals
2019 * - it doesn't know about comments starting with a semicolon
2020 * - it incorrectly interprets '(' as a character literal
2021 * All this messes up get_lisp_indent in some rare cases.
2022 * Update from Sergey Khorev:
2023 * I tried to fix the first two issues.
2024 */
2025 int
2026get_lisp_indent(void)
2027{
2028 pos_T *pos, realpos, paren;
2029 int amount;
2030 char_u *that;
2031 colnr_T col;
2032 colnr_T firsttry;
2033 int parencount, quotecount;
2034 int vi_lisp;
2035
2036 // Set vi_lisp to use the vi-compatible method
2037 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
2038
2039 realpos = curwin->w_cursor;
2040 curwin->w_cursor.col = 0;
2041
2042 if ((pos = findmatch(NULL, '(')) == NULL)
2043 pos = findmatch(NULL, '[');
2044 else
2045 {
2046 paren = *pos;
2047 pos = findmatch(NULL, '[');
2048 if (pos == NULL || LT_POSP(pos, &paren))
2049 pos = &paren;
2050 }
2051 if (pos != NULL)
2052 {
2053 // Extra trick: Take the indent of the first previous non-white
2054 // line that is at the same () level.
2055 amount = -1;
2056 parencount = 0;
2057
2058 while (--curwin->w_cursor.lnum >= pos->lnum)
2059 {
2060 if (linewhite(curwin->w_cursor.lnum))
2061 continue;
2062 for (that = ml_get_curline(); *that != NUL; ++that)
2063 {
2064 if (*that == ';')
2065 {
2066 while (*(that + 1) != NUL)
2067 ++that;
2068 continue;
2069 }
2070 if (*that == '\\')
2071 {
2072 if (*(that + 1) != NUL)
2073 ++that;
2074 continue;
2075 }
2076 if (*that == '"' && *(that + 1) != NUL)
2077 {
2078 while (*++that && *that != '"')
2079 {
2080 // skipping escaped characters in the string
2081 if (*that == '\\')
2082 {
2083 if (*++that == NUL)
2084 break;
2085 if (that[1] == NUL)
2086 {
2087 ++that;
2088 break;
2089 }
2090 }
2091 }
Bram Moolenaar0e8e9382022-06-18 12:51:11 +01002092 if (*that == NUL)
2093 break;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002094 }
2095 if (*that == '(' || *that == '[')
2096 ++parencount;
2097 else if (*that == ')' || *that == ']')
2098 --parencount;
2099 }
2100 if (parencount == 0)
2101 {
2102 amount = get_indent();
2103 break;
2104 }
2105 }
2106
2107 if (amount == -1)
2108 {
2109 curwin->w_cursor.lnum = pos->lnum;
2110 curwin->w_cursor.col = pos->col;
2111 col = pos->col;
2112
2113 that = ml_get_curline();
2114
2115 if (vi_lisp && get_indent() == 0)
2116 amount = 2;
2117 else
2118 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002119 char_u *line = that;
2120 chartabsize_T cts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002121
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002122 init_chartabsize_arg(&cts, curwin, pos->lnum, 0, line, line);
2123 while (*cts.cts_ptr != NUL && col > 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002124 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002125 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002126 col--;
2127 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002128 amount = cts.cts_vcol;
2129 that = cts.cts_ptr;
2130 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002131
2132 // Some keywords require "body" indenting rules (the
2133 // non-standard-lisp ones are Scheme special forms):
2134 //
2135 // (let ((a 1)) instead (let ((a 1))
2136 // (...)) of (...))
2137
2138 if (!vi_lisp && (*that == '(' || *that == '[')
2139 && lisp_match(that + 1))
2140 amount += 2;
2141 else
2142 {
Bram Moolenaar8eba2bd2022-06-22 19:59:28 +01002143 if (*that != NUL)
2144 {
2145 that++;
2146 amount++;
2147 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002148 firsttry = amount;
2149
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002150 init_chartabsize_arg(&cts, curwin, (colnr_T)(that - line),
2151 amount, line, that);
2152 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002153 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002154 cts.cts_vcol += lbr_chartabsize(&cts);
2155 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002156 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002157 that = cts.cts_ptr;
2158 amount = cts.cts_vcol;
2159 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002160
2161 if (*that && *that != ';') // not a comment line
2162 {
2163 // test *that != '(' to accommodate first let/do
2164 // argument if it is more than one line
2165 if (!vi_lisp && *that != '(' && *that != '[')
2166 firsttry++;
2167
2168 parencount = 0;
2169 quotecount = 0;
2170
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002171 init_chartabsize_arg(&cts, curwin,
2172 (colnr_T)(that - line), amount, line, that);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002173 if (vi_lisp
2174 || (*that != '"'
2175 && *that != '\''
2176 && *that != '#'
2177 && (*that < '0' || *that > '9')))
2178 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002179 while (*cts.cts_ptr
2180 && (!VIM_ISWHITE(*cts.cts_ptr)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002181 || quotecount
2182 || parencount)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002183 && (!((*cts.cts_ptr == '('
2184 || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002185 && !quotecount
2186 && !parencount
2187 && vi_lisp)))
2188 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002189 if (*cts.cts_ptr == '"')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002190 quotecount = !quotecount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002191 if ((*cts.cts_ptr == '(' || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002192 && !quotecount)
2193 ++parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002194 if ((*cts.cts_ptr == ')' || *cts.cts_ptr == ']')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002195 && !quotecount)
2196 --parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002197 if (*cts.cts_ptr == '\\'
2198 && *(cts.cts_ptr+1) != NUL)
2199 cts.cts_vcol += lbr_chartabsize_adv(&cts);
2200 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002201 }
2202 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002203 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002204 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002205 cts.cts_vcol += lbr_chartabsize(&cts);
2206 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002207 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002208 that = cts.cts_ptr;
2209 amount = cts.cts_vcol;
2210 clear_chartabsize_arg(&cts);
2211
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002212 if (!*that || *that == ';')
2213 amount = firsttry;
2214 }
2215 }
2216 }
2217 }
2218 }
2219 else
2220 amount = 0; // no matching '(' or '[' found, use zero indent
2221
2222 curwin->w_cursor = realpos;
2223
2224 return amount;
2225}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002226
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002227/*
2228 * Re-indent the current line, based on the current contents of it and the
2229 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
2230 * confused what all the part that handles Control-T is doing that I'm not.
2231 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
2232 */
2233
2234 void
2235fixthisline(int (*get_the_indent)(void))
2236{
2237 int amount = get_the_indent();
2238
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002239 if (amount < 0)
2240 return;
2241
2242 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
2243 if (linewhite(curwin->w_cursor.lnum))
2244 did_ai = TRUE; // delete the indent if the line stays empty
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002245}
2246
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002247/*
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002248 * Return TRUE if 'indentexpr' should be used for Lisp indenting.
2249 * Caller may want to check 'autoindent'.
2250 */
2251 int
2252use_indentexpr_for_lisp(void)
2253{
2254#ifdef FEAT_EVAL
2255 return curbuf->b_p_lisp
2256 && *curbuf->b_p_inde != NUL
2257 && STRCMP(curbuf->b_p_lop, "expr:1") == 0;
2258#else
2259 return FALSE;
2260#endif
2261}
2262
2263/*
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002264 * Fix indent for 'lisp' and 'cindent'.
2265 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002266 void
2267fix_indent(void)
2268{
2269 if (p_paste)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002270 return; // no auto-indenting when 'paste' is set
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002271 if (curbuf->b_p_lisp && curbuf->b_p_ai)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002272 {
2273 if (use_indentexpr_for_lisp())
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002274 do_c_expr_indent();
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002275 else
2276 fixthisline(get_lisp_indent);
2277 }
2278 else if (cindent_on())
2279 do_c_expr_indent();
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002280}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002281
2282#if defined(FEAT_EVAL) || defined(PROTO)
2283/*
2284 * "indent()" function
2285 */
2286 void
2287f_indent(typval_T *argvars, typval_T *rettv)
2288{
2289 linenr_T lnum;
2290
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002291 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2292 return;
2293
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002294 lnum = tv_get_lnum(argvars);
2295 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2296 rettv->vval.v_number = get_indent_lnum(lnum);
2297 else
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002298 {
2299 if (in_vim9script())
2300 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002301 rettv->vval.v_number = -1;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002302 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002303}
2304
2305/*
2306 * "lispindent(lnum)" function
2307 */
2308 void
2309f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2310{
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002311 pos_T pos;
2312 linenr_T lnum;
2313
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002314 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2315 return;
2316
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002317 pos = curwin->w_cursor;
2318 lnum = tv_get_lnum(argvars);
2319 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2320 {
2321 curwin->w_cursor.lnum = lnum;
2322 rettv->vval.v_number = get_lisp_indent();
2323 curwin->w_cursor = pos;
2324 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002325 else if (in_vim9script())
2326 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002327 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002328 rettv->vval.v_number = -1;
2329}
2330#endif