blob: 1dfde7dddafb838cc9212fb7ded2ec297831370d [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.
126 */
127 int
128tabstop_at(colnr_T col, int ts, int *vts)
129{
130 int tabcount;
131 colnr_T tabcol = 0;
132 int t;
133 int tab_size = 0;
134
135 if (vts == 0 || vts[0] == 0)
136 return ts;
137
138 tabcount = vts[0];
139 for (t = 1; t <= tabcount; ++t)
140 {
141 tabcol += vts[t];
142 if (tabcol > col)
143 {
144 tab_size = vts[t];
145 break;
146 }
147 }
148 if (t > tabcount)
149 tab_size = vts[tabcount];
150
151 return tab_size;
152}
153
154/*
155 * Find the column on which a tab starts.
156 */
157 colnr_T
158tabstop_start(colnr_T col, int ts, int *vts)
159{
160 int tabcount;
161 colnr_T tabcol = 0;
162 int t;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100163 int excess;
Bram Moolenaare677df82019-09-02 22:31:11 +0200164
165 if (vts == NULL || vts[0] == 0)
zeertzjq8ede7a02024-03-28 10:30:08 +0100166 return col - col % ts;
Bram Moolenaare677df82019-09-02 22:31:11 +0200167
168 tabcount = vts[0];
169 for (t = 1; t <= tabcount; ++t)
170 {
171 tabcol += vts[t];
172 if (tabcol > col)
173 return tabcol - vts[t];
174 }
175
176 excess = tabcol % vts[tabcount];
zeertzjq8ede7a02024-03-28 10:30:08 +0100177 return col - (col - excess) % vts[tabcount];
Bram Moolenaare677df82019-09-02 22:31:11 +0200178}
179
180/*
181 * Find the number of tabs and spaces necessary to get from one column
182 * to another.
183 */
184 void
185tabstop_fromto(
186 colnr_T start_col,
187 colnr_T end_col,
188 int ts_arg,
189 int *vts,
190 int *ntabs,
191 int *nspcs)
192{
193 int spaces = end_col - start_col;
194 colnr_T tabcol = 0;
195 int padding = 0;
196 int tabcount;
197 int t;
198 int ts = ts_arg == 0 ? curbuf->b_p_ts : ts_arg;
199
200 if (vts == NULL || vts[0] == 0)
201 {
202 int tabs = 0;
203 int initspc = 0;
204
205 initspc = ts - (start_col % ts);
206 if (spaces >= initspc)
207 {
208 spaces -= initspc;
209 tabs++;
210 }
211 tabs += spaces / ts;
212 spaces -= (spaces / ts) * ts;
213
214 *ntabs = tabs;
215 *nspcs = spaces;
216 return;
217 }
218
219 // Find the padding needed to reach the next tabstop.
220 tabcount = vts[0];
221 for (t = 1; t <= tabcount; ++t)
222 {
223 tabcol += vts[t];
224 if (tabcol > start_col)
225 {
226 padding = (int)(tabcol - start_col);
227 break;
228 }
229 }
230 if (t > tabcount)
231 padding = vts[tabcount] - (int)((start_col - tabcol) % vts[tabcount]);
232
233 // If the space needed is less than the padding no tabs can be used.
234 if (spaces < padding)
235 {
236 *ntabs = 0;
237 *nspcs = spaces;
238 return;
239 }
240
241 *ntabs = 1;
242 spaces -= padding;
243
244 // At least one tab has been used. See if any more will fit.
245 while (spaces != 0 && ++t <= tabcount)
246 {
247 padding = vts[t];
248 if (spaces < padding)
249 {
250 *nspcs = spaces;
251 return;
252 }
253 ++*ntabs;
254 spaces -= padding;
255 }
256
257 *ntabs += spaces / vts[tabcount];
258 *nspcs = spaces % vts[tabcount];
259}
260
261/*
262 * See if two tabstop arrays contain the same values.
263 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200264 static int
Bram Moolenaare677df82019-09-02 22:31:11 +0200265tabstop_eq(int *ts1, int *ts2)
266{
267 int t;
268
269 if ((ts1 == 0 && ts2) || (ts1 && ts2 == 0))
270 return FALSE;
271 if (ts1 == ts2)
272 return TRUE;
273 if (ts1[0] != ts2[0])
274 return FALSE;
275
276 for (t = 1; t <= ts1[0]; ++t)
277 if (ts1[t] != ts2[t])
278 return FALSE;
279
280 return TRUE;
281}
282
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200283# if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaare677df82019-09-02 22:31:11 +0200284/*
285 * Copy a tabstop array, allocating space for the new array.
286 */
287 int *
288tabstop_copy(int *oldts)
289{
290 int *newts;
291 int t;
292
293 if (oldts == NULL)
294 return NULL;
295 newts = ALLOC_MULT(int, oldts[0] + 1);
296 if (newts != NULL)
297 for (t = 0; t <= oldts[0]; ++t)
298 newts[t] = oldts[t];
299 return newts;
300}
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200301# endif
Bram Moolenaare677df82019-09-02 22:31:11 +0200302
303/*
304 * Return a count of the number of tabstops.
305 */
306 int
307tabstop_count(int *ts)
308{
309 return ts != NULL ? ts[0] : 0;
310}
311
312/*
313 * Return the first tabstop, or 8 if there are no tabstops defined.
314 */
315 int
316tabstop_first(int *ts)
317{
318 return ts != NULL ? ts[1] : 8;
319}
320
321#endif
322
323/*
324 * Return the effective shiftwidth value for current buffer, using the
325 * 'tabstop' value when 'shiftwidth' is zero.
326 */
327 long
328get_sw_value(buf_T *buf)
329{
330 return get_sw_value_col(buf, 0);
331}
332
333/*
334 * Idem, using "pos".
335 */
336 static long
337get_sw_value_pos(buf_T *buf, pos_T *pos)
338{
339 pos_T save_cursor = curwin->w_cursor;
340 long sw_value;
341
342 curwin->w_cursor = *pos;
343 sw_value = get_sw_value_col(buf, get_nolist_virtcol());
344 curwin->w_cursor = save_cursor;
345 return sw_value;
346}
347
348/*
349 * Idem, using the first non-black in the current line.
350 */
351 long
352get_sw_value_indent(buf_T *buf)
353{
354 pos_T pos = curwin->w_cursor;
355
356 pos.col = getwhitecols_curline();
357 return get_sw_value_pos(buf, &pos);
358}
359
360/*
361 * Idem, using virtual column "col".
362 */
363 long
364get_sw_value_col(buf_T *buf, colnr_T col UNUSED)
365{
366 return buf->b_p_sw ? buf->b_p_sw :
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200367#ifdef FEAT_VARTABS
Bram Moolenaare677df82019-09-02 22:31:11 +0200368 tabstop_at(col, buf->b_p_ts, buf->b_p_vts_array);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200369#else
Bram Moolenaare677df82019-09-02 22:31:11 +0200370 buf->b_p_ts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200371#endif
Bram Moolenaare677df82019-09-02 22:31:11 +0200372}
373
374/*
375 * Return the effective softtabstop value for the current buffer, using the
376 * 'shiftwidth' value when 'softtabstop' is negative.
377 */
378 long
379get_sts_value(void)
380{
381 return curbuf->b_p_sts < 0 ? get_sw_value(curbuf) : curbuf->b_p_sts;
382}
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200383
384/*
385 * Count the size (in window cells) of the indent in the current line.
386 */
387 int
388get_indent(void)
389{
390#ifdef FEAT_VARTABS
391 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
392 curbuf->b_p_vts_array, FALSE);
393#else
394 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
395#endif
396}
397
398/*
399 * Count the size (in window cells) of the indent in line "lnum".
400 */
401 int
402get_indent_lnum(linenr_T lnum)
403{
404#ifdef FEAT_VARTABS
405 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
406 curbuf->b_p_vts_array, FALSE);
407#else
408 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
409#endif
410}
411
412#if defined(FEAT_FOLDING) || defined(PROTO)
413/*
414 * Count the size (in window cells) of the indent in line "lnum" of buffer
415 * "buf".
416 */
417 int
418get_indent_buf(buf_T *buf, linenr_T lnum)
419{
420# ifdef FEAT_VARTABS
421 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
zeertzjq07146ad2022-12-19 15:51:44 +0000422 (int)buf->b_p_ts, buf->b_p_vts_array, FALSE);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200423# else
424 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
425# endif
426}
427#endif
428
429/*
430 * count the size (in window cells) of the indent in line "ptr", with
431 * 'tabstop' at "ts"
432 */
433 int
434get_indent_str(
435 char_u *ptr,
436 int ts,
zeertzjqefabd7c2024-02-11 17:16:19 +0100437 int no_ts) // if TRUE, count a tab as ^I
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200438{
439 int count = 0;
440
441 for ( ; *ptr; ++ptr)
442 {
zeertzjqefabd7c2024-02-11 17:16:19 +0100443 if (*ptr == TAB) // count a tab for what it is worth
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200444 {
zeertzjqefabd7c2024-02-11 17:16:19 +0100445 if (!no_ts)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200446 count += ts - (count % ts);
447 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200448 count += ptr2cells(ptr);
449 }
450 else if (*ptr == ' ')
451 ++count; // count a space for one
452 else
453 break;
454 }
455 return count;
456}
457
458#ifdef FEAT_VARTABS
459/*
460 * Count the size (in window cells) of the indent in line "ptr", using
461 * variable tabstops.
zeertzjqefabd7c2024-02-11 17:16:19 +0100462 * If "no_ts" is TRUE, count a tab as ^I.
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200463 */
464 int
zeertzjqefabd7c2024-02-11 17:16:19 +0100465get_indent_str_vtab(char_u *ptr, int ts, int *vts, int no_ts)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200466{
467 int count = 0;
468
469 for ( ; *ptr; ++ptr)
470 {
471 if (*ptr == TAB) // count a tab for what it is worth
472 {
zeertzjqefabd7c2024-02-11 17:16:19 +0100473 if (!no_ts)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200474 count += tabstop_padding(count, ts, vts);
475 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200476 count += ptr2cells(ptr);
477 }
478 else if (*ptr == ' ')
479 ++count; // count a space for one
480 else
481 break;
482 }
483 return count;
484}
485#endif
486
487/*
488 * Set the indent of the current line.
489 * Leaves the cursor on the first non-blank in the line.
490 * Caller must take care of undo.
491 * "flags":
492 * SIN_CHANGED: call changed_bytes() if the line was changed.
493 * SIN_INSERT: insert the indent in front of the line.
494 * SIN_UNDO: save line for undo before changing it.
495 * Returns TRUE if the line was changed.
496 */
497 int
498set_indent(
499 int size, // measured in spaces
500 int flags)
501{
502 char_u *p;
503 char_u *newline;
504 char_u *oldline;
505 char_u *s;
506 int todo;
507 int ind_len; // measured in characters
508 int line_len;
509 int doit = FALSE;
510 int ind_done = 0; // measured in spaces
511#ifdef FEAT_VARTABS
512 int ind_col = 0;
513#endif
514 int tab_pad;
515 int retval = FALSE;
516 int orig_char_len = -1; // number of initial whitespace chars when
517 // 'et' and 'pi' are both set
518
519 // First check if there is anything to do and compute the number of
520 // characters needed for the indent.
521 todo = size;
522 ind_len = 0;
523 p = oldline = ml_get_curline();
524
525 // Calculate the buffer size for the new indent, and check to see if it
526 // isn't already set
527
528 // if 'expandtab' isn't set: use TABs; if both 'expandtab' and
529 // 'preserveindent' are set count the number of characters at the
530 // beginning of the line to be copied
531 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
532 {
533 // If 'preserveindent' is set then reuse as much as possible of
534 // the existing indent structure for the new indent
535 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
536 {
537 ind_done = 0;
538
539 // count as many characters as we can use
540 while (todo > 0 && VIM_ISWHITE(*p))
541 {
542 if (*p == TAB)
543 {
544#ifdef FEAT_VARTABS
545 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
546 curbuf->b_p_vts_array);
547#else
548 tab_pad = (int)curbuf->b_p_ts
549 - (ind_done % (int)curbuf->b_p_ts);
550#endif
551 // stop if this tab will overshoot the target
552 if (todo < tab_pad)
553 break;
554 todo -= tab_pad;
555 ++ind_len;
556 ind_done += tab_pad;
557 }
558 else
559 {
560 --todo;
561 ++ind_len;
562 ++ind_done;
563 }
564 ++p;
565 }
566
567#ifdef FEAT_VARTABS
568 // These diverge from this point.
569 ind_col = ind_done;
570#endif
571 // Set initial number of whitespace chars to copy if we are
572 // preserving indent but expandtab is set
573 if (curbuf->b_p_et)
574 orig_char_len = ind_len;
575
576 // Fill to next tabstop with a tab, if possible
577#ifdef FEAT_VARTABS
578 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
579 curbuf->b_p_vts_array);
580#else
581 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
582#endif
583 if (todo >= tab_pad && orig_char_len == -1)
584 {
585 doit = TRUE;
586 todo -= tab_pad;
587 ++ind_len;
588 // ind_done += tab_pad;
589#ifdef FEAT_VARTABS
590 ind_col += tab_pad;
591#endif
592 }
593 }
594
595 // count tabs required for indent
596#ifdef FEAT_VARTABS
597 for (;;)
598 {
599 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
600 curbuf->b_p_vts_array);
601 if (todo < tab_pad)
602 break;
603 if (*p != TAB)
604 doit = TRUE;
605 else
606 ++p;
607 todo -= tab_pad;
608 ++ind_len;
609 ind_col += tab_pad;
610 }
611#else
612 while (todo >= (int)curbuf->b_p_ts)
613 {
614 if (*p != TAB)
615 doit = TRUE;
616 else
617 ++p;
618 todo -= (int)curbuf->b_p_ts;
619 ++ind_len;
620 // ind_done += (int)curbuf->b_p_ts;
621 }
622#endif
623 }
624 // count spaces required for indent
625 while (todo > 0)
626 {
627 if (*p != ' ')
628 doit = TRUE;
629 else
630 ++p;
631 --todo;
632 ++ind_len;
633 // ++ind_done;
634 }
635
636 // Return if the indent is OK already.
637 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
638 return FALSE;
639
640 // Allocate memory for the new line.
641 if (flags & SIN_INSERT)
642 p = oldline;
643 else
644 p = skipwhite(p);
645 line_len = (int)STRLEN(p) + 1;
646
647 // If 'preserveindent' and 'expandtab' are both set keep the original
648 // characters and allocate accordingly. We will fill the rest with spaces
649 // after the if (!curbuf->b_p_et) below.
650 if (orig_char_len != -1)
651 {
652 newline = alloc(orig_char_len + size - ind_done + line_len);
653 if (newline == NULL)
654 return FALSE;
655 todo = size - ind_done;
656 ind_len = orig_char_len + todo; // Set total length of indent in
657 // characters, which may have been
658 // undercounted until now
659 p = oldline;
660 s = newline;
661 while (orig_char_len > 0)
662 {
663 *s++ = *p++;
664 orig_char_len--;
665 }
666
667 // Skip over any additional white space (useful when newindent is less
668 // than old)
669 while (VIM_ISWHITE(*p))
670 ++p;
671
672 }
673 else
674 {
675 todo = size;
676 newline = alloc(ind_len + line_len);
677 if (newline == NULL)
678 return FALSE;
679 s = newline;
680 }
681
682 // Put the characters in the new line.
683 // if 'expandtab' isn't set: use TABs
684 if (!curbuf->b_p_et)
685 {
686 // If 'preserveindent' is set then reuse as much as possible of
687 // the existing indent structure for the new indent
688 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
689 {
690 p = oldline;
691 ind_done = 0;
692
693 while (todo > 0 && VIM_ISWHITE(*p))
694 {
695 if (*p == TAB)
696 {
697#ifdef FEAT_VARTABS
698 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
699 curbuf->b_p_vts_array);
700#else
701 tab_pad = (int)curbuf->b_p_ts
702 - (ind_done % (int)curbuf->b_p_ts);
703#endif
704 // stop if this tab will overshoot the target
705 if (todo < tab_pad)
706 break;
707 todo -= tab_pad;
708 ind_done += tab_pad;
709 }
710 else
711 {
712 --todo;
713 ++ind_done;
714 }
715 *s++ = *p++;
716 }
717
718 // Fill to next tabstop with a tab, if possible
719#ifdef FEAT_VARTABS
720 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
721 curbuf->b_p_vts_array);
722#else
723 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
724#endif
725 if (todo >= tab_pad)
726 {
727 *s++ = TAB;
728 todo -= tab_pad;
729#ifdef FEAT_VARTABS
730 ind_done += tab_pad;
731#endif
732 }
733
734 p = skipwhite(p);
735 }
736
737#ifdef FEAT_VARTABS
738 for (;;)
739 {
740 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
741 curbuf->b_p_vts_array);
742 if (todo < tab_pad)
743 break;
744 *s++ = TAB;
745 todo -= tab_pad;
746 ind_done += tab_pad;
747 }
748#else
749 while (todo >= (int)curbuf->b_p_ts)
750 {
751 *s++ = TAB;
752 todo -= (int)curbuf->b_p_ts;
753 }
754#endif
755 }
756 while (todo > 0)
757 {
758 *s++ = ' ';
759 --todo;
760 }
761 mch_memmove(s, p, (size_t)line_len);
762
763 // Replace the line (unless undo fails).
764 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
765 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200766 colnr_T old_offset = (colnr_T)(p - oldline);
767 colnr_T new_offset = (colnr_T)(s - newline);
768
769 // this may free "newline"
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200770 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
771 if (flags & SIN_CHANGED)
772 changed_bytes(curwin->w_cursor.lnum, 0);
773
774 // Correct saved cursor position if it is in this line.
775 if (saved_cursor.lnum == curwin->w_cursor.lnum)
776 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200777 if (saved_cursor.col >= old_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200778 // cursor was after the indent, adjust for the number of
779 // bytes added/removed
Bram Moolenaarcf306432020-06-29 20:40:37 +0200780 saved_cursor.col += ind_len - old_offset;
781 else if (saved_cursor.col >= new_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200782 // cursor was in the indent, and is now after it, put it back
783 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaarcf306432020-06-29 20:40:37 +0200784 saved_cursor.col = new_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200785 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100786#ifdef FEAT_PROP_POPUP
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200787 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200788 int added = ind_len - old_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200789
790 // When increasing indent this behaves like spaces were inserted at
791 // the old indent, when decreasing indent it behaves like spaces
792 // were deleted at the new indent.
793 adjust_prop_columns(curwin->w_cursor.lnum,
Bram Moolenaar8a77d202022-08-15 16:29:37 +0100794 added > 0 ? old_offset : (colnr_T)ind_len,
795 added, APC_INDENT);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200796 }
797#endif
798 retval = TRUE;
799 }
800 else
801 vim_free(newline);
802
803 curwin->w_cursor.col = ind_len;
804 return retval;
805}
806
807/*
808 * Return the indent of the current line after a number. Return -1 if no
809 * number was found. Used for 'n' in 'formatoptions': numbered list.
810 * Since a pattern is used it can actually handle more than numbers.
811 */
812 int
813get_number_indent(linenr_T lnum)
814{
815 colnr_T col;
816 pos_T pos;
817
818 regmatch_T regmatch;
819 int lead_len = 0; // length of comment leader
820
821 if (lnum > curbuf->b_ml.ml_line_count)
822 return -1;
823 pos.lnum = 0;
824
825 // In format_lines() (i.e. not insert mode), fo+=q is needed too...
Bram Moolenaar24959102022-05-07 20:01:16 +0100826 if ((State & MODE_INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200827 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
828
829 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
830 if (regmatch.regprog != NULL)
831 {
832 regmatch.rm_ic = FALSE;
833
834 // vim_regexec() expects a pointer to a line. This lets us
835 // start matching for the flp beyond any comment leader...
836 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
837 {
838 pos.lnum = lnum;
839 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
840 pos.coladd = 0;
841 }
842 vim_regfree(regmatch.regprog);
843 }
844
845 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
846 return -1;
847 getvcol(curwin, &pos, &col, NULL, NULL);
848 return (int)col;
849}
850
851#if defined(FEAT_LINEBREAK) || defined(PROTO)
852/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100853 * This is called when 'breakindentopt' is changed and when a window is
854 * initialized.
855 */
856 int
857briopt_check(win_T *wp)
858{
859 char_u *p;
860 int bri_shift = 0;
861 long bri_min = 20;
862 int bri_sbr = FALSE;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200863 int bri_list = 0;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100864 int bri_vcol = 0;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100865
866 p = wp->w_p_briopt;
867 while (*p != NUL)
868 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200869 // Note: Keep this in sync with p_briopt_values
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100870 if (STRNCMP(p, "shift:", 6) == 0
871 && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
872 {
873 p += 6;
874 bri_shift = getdigits(&p);
875 }
876 else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4]))
877 {
878 p += 4;
879 bri_min = getdigits(&p);
880 }
881 else if (STRNCMP(p, "sbr", 3) == 0)
882 {
883 p += 3;
884 bri_sbr = TRUE;
885 }
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200886 else if (STRNCMP(p, "list:", 5) == 0)
887 {
888 p += 5;
889 bri_list = getdigits(&p);
890 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100891 else if (STRNCMP(p, "column:", 7) == 0)
892 {
893 p += 7;
894 bri_vcol = getdigits(&p);
895 }
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100896 if (*p != ',' && *p != NUL)
897 return FAIL;
898 if (*p == ',')
899 ++p;
900 }
901
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100902 wp->w_briopt_shift = bri_shift;
903 wp->w_briopt_min = bri_min;
904 wp->w_briopt_sbr = bri_sbr;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200905 wp->w_briopt_list = bri_list;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100906 wp->w_briopt_vcol = bri_vcol;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100907
908 return OK;
909}
910
911/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200912 * Return appropriate space number for breakindent, taking influencing
913 * parameters into account. Window must be specified, since it is not
914 * necessarily always the current one.
915 */
916 int
917get_breakindent_win(
918 win_T *wp,
919 char_u *line) // start of the line
920{
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000921 static int prev_indent = 0; // cached indent value
922 static long prev_ts = 0L; // cached tabstop value
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200923# ifdef FEAT_VARTABS
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000924 static int *prev_vts = NULL; // cached vartabs values
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200925# endif
zeertzjqefabd7c2024-02-11 17:16:19 +0100926 static int prev_fnum = 0; // cached buffer number
927 static char_u *prev_line = NULL; // cached copy of "line"
928 static varnumber_T prev_tick = 0; // changedtick of cached value
929 static int prev_list = 0; // cached list indent
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000930 static int prev_listopt = 0; // cached w_p_briopt_list value
zeertzjqefabd7c2024-02-11 17:16:19 +0100931 static int prev_no_ts = FALSE; // cached no_ts value
932 static unsigned prev_dy_uhex = 0; // cached 'display' "uhex" value
Christian Brabandtc53b4672022-01-15 10:01:05 +0000933 // cached formatlistpat value
934 static char_u *prev_flp = NULL;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200935 int bri = 0;
936 // window width minus window margin space, i.e. what rests for text
937 const int eff_wwidth = wp->w_width
zeertzjqf0a9d652024-02-12 22:53:20 +0100938 - win_col_off(wp) + win_col_off2(wp);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200939
zeertzjqefabd7c2024-02-11 17:16:19 +0100940 // In list mode, if 'listchars' "tab" isn't set, a TAB is displayed as ^I.
941 int no_ts = wp->w_p_list && wp->w_lcs_chars.tab1 == NUL;
942
943 // Used cached indent, unless
944 // - buffer changed, or
945 // - 'tabstop' changed, or
946 // - 'vartabstop' changed, or
947 // - buffer was changed, or
948 // - 'breakindentopt' "list" changed, or
949 // - 'list' or 'listchars' "tab" changed, or
950 // - 'display' "uhex" flag changed, or
951 // - 'formatlistpat' changed, or
952 // - line changed.
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100953 if (prev_fnum != wp->w_buffer->b_fnum
954 || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200955# ifdef FEAT_VARTABS
956 || prev_vts != wp->w_buffer->b_p_vts_array
957# endif
zeertzjqefabd7c2024-02-11 17:16:19 +0100958 || prev_tick != CHANGEDTICK(wp->w_buffer)
959 || prev_listopt != wp->w_briopt_list
960 || prev_no_ts != no_ts
961 || prev_dy_uhex != (dy_flags & DY_UHEX)
962 || prev_flp == NULL
963 || STRCMP(prev_flp, get_flp_value(wp->w_buffer)) != 0
964 || prev_line == NULL || STRCMP(prev_line, line) != 0
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200965 )
966 {
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100967 prev_fnum = wp->w_buffer->b_fnum;
968 vim_free(prev_line);
969 prev_line = vim_strsave(line);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200970 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200971# ifdef FEAT_VARTABS
972 prev_vts = wp->w_buffer->b_p_vts_array;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100973 if (wp->w_briopt_vcol == 0)
974 prev_indent = get_indent_str_vtab(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200975 (int)wp->w_buffer->b_p_ts,
zeertzjqefabd7c2024-02-11 17:16:19 +0100976 wp->w_buffer->b_p_vts_array, no_ts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200977# else
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100978 if (wp->w_briopt_vcol == 0)
979 prev_indent = get_indent_str(line,
zeertzjqefabd7c2024-02-11 17:16:19 +0100980 (int)wp->w_buffer->b_p_ts, no_ts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200981# endif
zeertzjqefabd7c2024-02-11 17:16:19 +0100982 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000983 prev_listopt = wp->w_briopt_list;
Christian Brabandtc53b4672022-01-15 10:01:05 +0000984 prev_list = 0;
zeertzjqefabd7c2024-02-11 17:16:19 +0100985 prev_no_ts = no_ts;
986 prev_dy_uhex = (dy_flags & DY_UHEX);
Christian Brabandtc53b4672022-01-15 10:01:05 +0000987 vim_free(prev_flp);
988 prev_flp = vim_strsave(get_flp_value(wp->w_buffer));
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000989 // add additional indent for numbered lists
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100990 if (wp->w_briopt_list != 0 && wp->w_briopt_vcol == 0)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000991 {
992 regmatch_T regmatch;
993
Christian Brabandtc53b4672022-01-15 10:01:05 +0000994 regmatch.regprog = vim_regcomp(prev_flp,
995 RE_MAGIC + RE_STRING + RE_AUTO + RE_STRICT);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000996
997 if (regmatch.regprog != NULL)
998 {
999 regmatch.rm_ic = FALSE;
1000 if (vim_regexec(&regmatch, line, 0))
1001 {
1002 if (wp->w_briopt_list > 0)
1003 prev_list = wp->w_briopt_list;
1004 else
Maxim Kim11916722022-09-02 14:08:53 +01001005 prev_indent = (*regmatch.endp - *regmatch.startp);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001006 }
1007 vim_regfree(regmatch.regprog);
1008 }
1009 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001010 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001011 if (wp->w_briopt_vcol != 0)
1012 {
1013 // column value has priority
1014 bri = wp->w_briopt_vcol;
1015 prev_list = 0;
1016 }
1017 else
1018 bri = prev_indent + wp->w_briopt_shift;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001019
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001020 // Add offset for number column, if 'n' is in 'cpoptions'
1021 bri += win_col_off2(wp);
1022
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001023 // add additional indent for numbered lists
Maxim Kim11916722022-09-02 14:08:53 +01001024 if (wp->w_briopt_list > 0)
1025 bri += prev_list;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001026
Maxim Kimf674b352021-07-22 11:46:59 +02001027 // indent minus the length of the showbreak string
1028 if (wp->w_briopt_sbr)
1029 bri -= vim_strsize(get_showbreak_value(wp));
1030
1031
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001032 // never indent past left window margin
1033 if (bri < 0)
1034 bri = 0;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001035
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001036 // always leave at least bri_min characters on the left,
1037 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001038 else if (bri > eff_wwidth - wp->w_briopt_min)
1039 bri = (eff_wwidth - wp->w_briopt_min < 0)
1040 ? 0 : eff_wwidth - wp->w_briopt_min;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001041
1042 return bri;
1043}
1044#endif
1045
1046/*
1047 * When extra == 0: Return TRUE if the cursor is before or on the first
1048 * non-blank in the line.
1049 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1050 * the line.
1051 */
1052 int
1053inindent(int extra)
1054{
1055 char_u *ptr;
1056 colnr_T col;
1057
1058 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
1059 ++ptr;
1060 if (col >= curwin->w_cursor.col + extra)
1061 return TRUE;
1062 else
1063 return FALSE;
1064}
1065
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001066/*
1067 * op_reindent - handle reindenting a block of lines.
1068 */
1069 void
1070op_reindent(oparg_T *oap, int (*how)(void))
1071{
Bram Moolenaar4c84dd32022-04-20 10:22:54 +01001072 long i = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001073 char_u *l;
1074 int amount;
1075 linenr_T first_changed = 0;
1076 linenr_T last_changed = 0;
1077 linenr_T start_lnum = curwin->w_cursor.lnum;
1078
1079 // Don't even try when 'modifiable' is off.
1080 if (!curbuf->b_p_ma)
1081 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001082 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001083 return;
1084 }
1085
Bram Moolenaare4686982022-04-19 18:28:45 +01001086 // Save for undo. Do this once for all lines, much faster than doing this
1087 // for each line separately, especially when undoing.
1088 if (u_savecommon(start_lnum - 1, start_lnum + oap->line_count,
1089 start_lnum + oap->line_count, FALSE) == OK)
1090 for (i = oap->line_count; --i >= 0 && !got_int; )
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001091 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001092 // it's a slow thing to do, so give feedback so there's no worry
1093 // that the computer's just hung.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001094
Bram Moolenaare4686982022-04-19 18:28:45 +01001095 if (i > 1
1096 && (i % 50 == 0 || i == oap->line_count - 1)
1097 && oap->line_count > p_report)
1098 smsg(_("%ld lines to indent... "), i);
1099
1100 // Be vi-compatible: For lisp indenting the first line is not
1101 // indented, unless there is only one line.
Bram Moolenaare4686982022-04-19 18:28:45 +01001102 if (i != oap->line_count - 1 || oap->line_count == 1
1103 || how != get_lisp_indent)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001104 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001105 l = skipwhite(ml_get_curline());
1106 if (*l == NUL) // empty or blank line
1107 amount = 0;
1108 else
1109 amount = how(); // get the indent for this line
1110
1111 if (amount >= 0 && set_indent(amount, 0))
1112 {
1113 // did change the indent, call changed_lines() later
1114 if (first_changed == 0)
1115 first_changed = curwin->w_cursor.lnum;
1116 last_changed = curwin->w_cursor.lnum;
1117 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001118 }
Bram Moolenaare4686982022-04-19 18:28:45 +01001119 ++curwin->w_cursor.lnum;
1120 curwin->w_cursor.col = 0; // make sure it's valid
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001121 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001122
1123 // put cursor on first non-blank of indented line
1124 curwin->w_cursor.lnum = start_lnum;
1125 beginline(BL_SOL | BL_FIX);
1126
1127 // Mark changed lines so that they will be redrawn. When Visual
1128 // highlighting was present, need to continue until the last line. When
1129 // there is no change still need to remove the Visual highlighting.
1130 if (last_changed != 0)
1131 changed_lines(first_changed, 0,
1132 oap->is_VIsual ? start_lnum + oap->line_count :
1133 last_changed + 1, 0L);
1134 else if (oap->is_VIsual)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001135 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001136
1137 if (oap->line_count > p_report)
1138 {
1139 i = oap->line_count - (i + 1);
1140 smsg(NGETTEXT("%ld line indented ",
1141 "%ld lines indented ", i), i);
1142 }
Bram Moolenaare1004402020-10-24 20:49:43 +02001143 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001144 {
1145 // set '[ and '] marks
1146 curbuf->b_op_start = oap->start;
1147 curbuf->b_op_end = oap->end;
1148 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001149}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001150
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001151/*
1152 * Return TRUE if lines starting with '#' should be left aligned.
1153 */
1154 int
1155preprocs_left(void)
1156{
1157 return
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001158 (curbuf->b_p_si && !curbuf->b_p_cin) ||
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001159 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1160 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001161 ;
1162}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001163
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001164/*
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001165 * Return TRUE if the conditions are OK for smart indenting.
1166 */
1167 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001168may_do_si(void)
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001169{
1170 return curbuf->b_p_si
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001171 && !curbuf->b_p_cin
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001172# ifdef FEAT_EVAL
1173 && *curbuf->b_p_inde == NUL
1174# endif
1175 && !p_paste;
1176}
1177
1178/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001179 * Try to do some very smart auto-indenting.
1180 * Used when inserting a "normal" character.
1181 */
1182 void
1183ins_try_si(int c)
1184{
1185 pos_T *pos, old_pos;
1186 char_u *ptr;
1187 int i;
1188 int temp;
1189
1190 // do some very smart indenting when entering '{' or '}'
Bram Moolenaar2e444bb2022-05-14 12:54:23 +01001191 if (((did_si || can_si_back) && c == '{')
1192 || (can_si && c == '}' && inindent(0)))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001193 {
1194 // for '}' set indent equal to indent of line containing matching '{'
1195 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1196 {
1197 old_pos = curwin->w_cursor;
1198 // If the matching '{' has a ')' immediately before it (ignoring
1199 // white-space), then line up with the start of the line
1200 // containing the matching '(' if there is one. This handles the
1201 // case where an "if (..\n..) {" statement continues over multiple
1202 // lines -- webb
1203 ptr = ml_get(pos->lnum);
1204 i = pos->col;
1205 if (i > 0) // skip blanks before '{'
1206 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1207 ;
1208 curwin->w_cursor.lnum = pos->lnum;
1209 curwin->w_cursor.col = i;
1210 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1211 curwin->w_cursor = *pos;
1212 i = get_indent();
1213 curwin->w_cursor = old_pos;
1214 if (State & VREPLACE_FLAG)
1215 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1216 else
1217 (void)set_indent(i, SIN_CHANGED);
1218 }
1219 else if (curwin->w_cursor.col > 0)
1220 {
1221 // when inserting '{' after "O" reduce indent, but not
1222 // more than indent of previous line
1223 temp = TRUE;
1224 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1225 {
1226 old_pos = curwin->w_cursor;
1227 i = get_indent();
1228 while (curwin->w_cursor.lnum > 1)
1229 {
1230 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1231
1232 // ignore empty lines and lines starting with '#'.
1233 if (*ptr != '#' && *ptr != NUL)
1234 break;
1235 }
1236 if (get_indent() >= i)
1237 temp = FALSE;
1238 curwin->w_cursor = old_pos;
1239 }
1240 if (temp)
1241 shift_line(TRUE, FALSE, 1, TRUE);
1242 }
1243 }
1244
1245 // set indent of '#' always to 0
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001246 if (curwin->w_cursor.col > 0 && can_si && c == '#' && inindent(0))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001247 {
1248 // remember current indent for next line
1249 old_indent = get_indent();
1250 (void)set_indent(0, SIN_CHANGED);
1251 }
1252
1253 // Adjust ai_col, the char at this position can be deleted.
1254 if (ai_col > curwin->w_cursor.col)
1255 ai_col = curwin->w_cursor.col;
1256}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001257
1258/*
1259 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1260 * Keep the cursor on the same character.
1261 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1262 * type == INDENT_DEC decrease indent (for CTRL-D)
1263 * type == INDENT_SET set indent to "amount"
1264 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1265 */
1266 void
1267change_indent(
1268 int type,
1269 int amount,
1270 int round,
1271 int replaced, // replaced character, put on replace stack
1272 int call_changed_bytes) // call changed_bytes()
1273{
1274 int vcol;
1275 int last_vcol;
1276 int insstart_less; // reduction for Insstart.col
1277 int new_cursor_col;
1278 int i;
1279 char_u *ptr;
1280 int save_p_list;
1281 int start_col;
1282 colnr_T vc;
1283 colnr_T orig_col = 0; // init for GCC
1284 char_u *new_line, *orig_line = NULL; // init for GCC
1285
Bram Moolenaar24959102022-05-07 20:01:16 +01001286 // MODE_VREPLACE state needs to know what the line was like before changing
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001287 if (State & VREPLACE_FLAG)
1288 {
1289 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1290 orig_col = curwin->w_cursor.col;
1291 }
1292
1293 // for the following tricks we don't want list mode
1294 save_p_list = curwin->w_p_list;
1295 curwin->w_p_list = FALSE;
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001296#ifdef FEAT_PROP_POPUP
1297 ignore_text_props = TRUE;
1298#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001299 vc = getvcol_nolist(&curwin->w_cursor);
1300 vcol = vc;
1301
1302 // For Replace mode we need to fix the replace stack later, which is only
1303 // possible when the cursor is in the indent. Remember the number of
1304 // characters before the cursor if it's possible.
1305 start_col = curwin->w_cursor.col;
1306
1307 // determine offset from first non-blank
1308 new_cursor_col = curwin->w_cursor.col;
1309 beginline(BL_WHITE);
1310 new_cursor_col -= curwin->w_cursor.col;
1311
1312 insstart_less = curwin->w_cursor.col;
1313
1314 // If the cursor is in the indent, compute how many screen columns the
1315 // cursor is to the left of the first non-blank.
1316 if (new_cursor_col < 0)
1317 vcol = get_indent() - vcol;
1318
1319 if (new_cursor_col > 0) // can't fix replace stack
1320 start_col = -1;
1321
1322 // Set the new indent. The cursor will be put on the first non-blank.
1323 if (type == INDENT_SET)
1324 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1325 else
1326 {
1327 int save_State = State;
1328
1329 // Avoid being called recursively.
1330 if (State & VREPLACE_FLAG)
Bram Moolenaar24959102022-05-07 20:01:16 +01001331 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001332 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1333 State = save_State;
1334 }
1335 insstart_less -= curwin->w_cursor.col;
1336
1337 // Try to put cursor on same character.
1338 // If the cursor is at or after the first non-blank in the line,
1339 // compute the cursor column relative to the column of the first
1340 // non-blank character.
1341 // If we are not in insert mode, leave the cursor on the first non-blank.
1342 // If the cursor is before the first non-blank, position it relative
1343 // to the first non-blank, counted in screen columns.
1344 if (new_cursor_col >= 0)
1345 {
1346 // When changing the indent while the cursor is touching it, reset
1347 // Insstart_col to 0.
1348 if (new_cursor_col == 0)
1349 insstart_less = MAXCOL;
1350 new_cursor_col += curwin->w_cursor.col;
1351 }
Bram Moolenaar24959102022-05-07 20:01:16 +01001352 else if (!(State & MODE_INSERT))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001353 new_cursor_col = curwin->w_cursor.col;
1354 else
1355 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001356 chartabsize_T cts;
1357
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001358 // Compute the screen column where the cursor should be.
1359 vcol = get_indent() - vcol;
1360 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1361
1362 // Advance the cursor until we reach the right screen column.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001363 last_vcol = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001364 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001365 init_chartabsize_arg(&cts, curwin, 0, 0, ptr, ptr);
1366 while (cts.cts_vcol <= (int)curwin->w_virtcol)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001367 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001368 last_vcol = cts.cts_vcol;
1369 if (cts.cts_vcol > 0)
1370 MB_PTR_ADV(cts.cts_ptr);
1371 if (*cts.cts_ptr == NUL)
Bram Moolenaar4e889f92022-02-21 19:36:12 +00001372 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001373 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001374 }
1375 vcol = last_vcol;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001376 new_cursor_col = cts.cts_ptr - cts.cts_line;
1377 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001378
1379 // May need to insert spaces to be able to position the cursor on
1380 // the right screen column.
1381 if (vcol != (int)curwin->w_virtcol)
1382 {
1383 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1384 i = (int)curwin->w_virtcol - vcol;
1385 ptr = alloc(i + 1);
1386 if (ptr != NULL)
1387 {
1388 new_cursor_col += i;
1389 ptr[i] = NUL;
1390 while (--i >= 0)
1391 ptr[i] = ' ';
1392 ins_str(ptr);
1393 vim_free(ptr);
1394 }
1395 }
1396
1397 // When changing the indent while the cursor is in it, reset
1398 // Insstart_col to 0.
1399 insstart_less = MAXCOL;
1400 }
1401
1402 curwin->w_p_list = save_p_list;
1403
1404 if (new_cursor_col <= 0)
1405 curwin->w_cursor.col = 0;
1406 else
1407 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1408 curwin->w_set_curswant = TRUE;
1409 changed_cline_bef_curs();
1410
1411 // May have to adjust the start of the insert.
Bram Moolenaar24959102022-05-07 20:01:16 +01001412 if (State & MODE_INSERT)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001413 {
1414 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1415 {
1416 if ((int)Insstart.col <= insstart_less)
1417 Insstart.col = 0;
1418 else
1419 Insstart.col -= insstart_less;
1420 }
1421 if ((int)ai_col <= insstart_less)
1422 ai_col = 0;
1423 else
1424 ai_col -= insstart_less;
1425 }
1426
Bram Moolenaar24959102022-05-07 20:01:16 +01001427 // For MODE_REPLACE state, may have to fix the replace stack, if it's
1428 // possible. If the number of characters before the cursor decreased, need
1429 // to pop a few characters from the replace stack.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001430 // If the number of characters before the cursor increased, need to push a
1431 // few NULs onto the replace stack.
1432 if (REPLACE_NORMAL(State) && start_col >= 0)
1433 {
1434 while (start_col > (int)curwin->w_cursor.col)
1435 {
1436 replace_join(0); // remove a NUL from the replace stack
1437 --start_col;
1438 }
1439 while (start_col < (int)curwin->w_cursor.col || replaced)
1440 {
1441 replace_push(NUL);
1442 if (replaced)
1443 {
1444 replace_push(replaced);
1445 replaced = NUL;
1446 }
1447 ++start_col;
1448 }
1449 }
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001450#ifdef FEAT_PROP_POPUP
1451 ignore_text_props = FALSE;
1452#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001453
Bram Moolenaar24959102022-05-07 20:01:16 +01001454 // For MODE_VREPLACE state, we also have to fix the replace stack. In this
1455 // case it is always possible because we backspace over the whole line and
1456 // then put it back again the way we wanted it.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001457 if (State & VREPLACE_FLAG)
1458 {
1459 // If orig_line didn't allocate, just return. At least we did the job,
1460 // even if you can't backspace.
1461 if (orig_line == NULL)
1462 return;
1463
1464 // Save new line
1465 new_line = vim_strsave(ml_get_curline());
1466 if (new_line == NULL)
1467 return;
1468
1469 // We only put back the new line up to the cursor
1470 new_line[curwin->w_cursor.col] = NUL;
1471
1472 // Put back original line
1473 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1474 curwin->w_cursor.col = orig_col;
1475
1476 // Backspace from cursor to start of line
1477 backspace_until_column(0);
1478
1479 // Insert new stuff into line again
1480 ins_bytes(new_line);
1481
1482 vim_free(new_line);
1483 }
1484}
1485
1486/*
1487 * Copy the indent from ptr to the current line (and fill to size)
1488 * Leaves the cursor on the first non-blank in the line.
1489 * Returns TRUE if the line was changed.
1490 */
1491 int
1492copy_indent(int size, char_u *src)
1493{
1494 char_u *p = NULL;
1495 char_u *line = NULL;
1496 char_u *s;
1497 int todo;
1498 int ind_len;
1499 int line_len = 0;
1500 int tab_pad;
1501 int ind_done;
1502 int round;
1503#ifdef FEAT_VARTABS
1504 int ind_col;
1505#endif
1506
1507 // Round 1: compute the number of characters needed for the indent
1508 // Round 2: copy the characters.
1509 for (round = 1; round <= 2; ++round)
1510 {
1511 todo = size;
1512 ind_len = 0;
1513 ind_done = 0;
1514#ifdef FEAT_VARTABS
1515 ind_col = 0;
1516#endif
1517 s = src;
1518
1519 // Count/copy the usable portion of the source line
1520 while (todo > 0 && VIM_ISWHITE(*s))
1521 {
1522 if (*s == TAB)
1523 {
1524#ifdef FEAT_VARTABS
1525 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1526 curbuf->b_p_vts_array);
1527#else
1528 tab_pad = (int)curbuf->b_p_ts
1529 - (ind_done % (int)curbuf->b_p_ts);
1530#endif
1531 // Stop if this tab will overshoot the target
1532 if (todo < tab_pad)
1533 break;
1534 todo -= tab_pad;
1535 ind_done += tab_pad;
1536#ifdef FEAT_VARTABS
1537 ind_col += tab_pad;
1538#endif
1539 }
1540 else
1541 {
1542 --todo;
1543 ++ind_done;
1544#ifdef FEAT_VARTABS
1545 ++ind_col;
1546#endif
1547 }
1548 ++ind_len;
1549 if (p != NULL)
1550 *p++ = *s;
1551 ++s;
1552 }
1553
1554 // Fill to next tabstop with a tab, if possible
1555#ifdef FEAT_VARTABS
1556 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1557 curbuf->b_p_vts_array);
1558#else
1559 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1560#endif
1561 if (todo >= tab_pad && !curbuf->b_p_et)
1562 {
1563 todo -= tab_pad;
1564 ++ind_len;
1565#ifdef FEAT_VARTABS
1566 ind_col += tab_pad;
1567#endif
1568 if (p != NULL)
1569 *p++ = TAB;
1570 }
1571
1572 // Add tabs required for indent
1573 if (!curbuf->b_p_et)
1574 {
1575#ifdef FEAT_VARTABS
1576 for (;;)
1577 {
1578 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1579 curbuf->b_p_vts_array);
1580 if (todo < tab_pad)
1581 break;
1582 todo -= tab_pad;
1583 ++ind_len;
1584 ind_col += tab_pad;
1585 if (p != NULL)
1586 *p++ = TAB;
1587 }
1588#else
1589 while (todo >= (int)curbuf->b_p_ts)
1590 {
1591 todo -= (int)curbuf->b_p_ts;
1592 ++ind_len;
1593 if (p != NULL)
1594 *p++ = TAB;
1595 }
1596#endif
1597 }
1598
1599 // Count/add spaces required for indent
1600 while (todo > 0)
1601 {
1602 --todo;
1603 ++ind_len;
1604 if (p != NULL)
1605 *p++ = ' ';
1606 }
1607
1608 if (p == NULL)
1609 {
1610 // Allocate memory for the result: the copied indent, new indent
1611 // and the rest of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01001612 line_len = ml_get_curline_len() + 1;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001613 line = alloc(ind_len + line_len);
1614 if (line == NULL)
1615 return FALSE;
1616 p = line;
1617 }
1618 }
1619
1620 // Append the original line
1621 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1622
1623 // Replace the line
1624 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1625
1626 // Put the cursor after the indent.
1627 curwin->w_cursor.col = ind_len;
1628 return TRUE;
1629}
1630
1631/*
Bram Moolenaar308660b2022-06-16 12:10:48 +01001632 * Give a "resulting text too long" error and maybe set got_int.
1633 */
1634 static void
1635emsg_text_too_long(void)
1636{
1637 emsg(_(e_resulting_text_too_long));
1638#ifdef FEAT_EVAL
1639 // when not inside a try/catch set got_int to break out of any loop
1640 if (trylevel == 0)
1641#endif
1642 got_int = TRUE;
1643}
1644
1645/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001646 * ":retab".
1647 */
1648 void
1649ex_retab(exarg_T *eap)
1650{
1651 linenr_T lnum;
1652 int got_tab = FALSE;
1653 long num_spaces = 0;
1654 long num_tabs;
1655 long len;
1656 long col;
1657 long vcol;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001658 long start_col = 0; // For start of white-space string
1659 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001660 long old_len;
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001661 long new_len;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001662 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001663 char_u *new_line = (char_u *)1; // init to non-NULL
1664 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001665#ifdef FEAT_VARTABS
1666 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001667 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001668#else
1669 int temp;
1670 int new_ts;
1671#endif
1672 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001673 linenr_T first_line = 0; // first changed line
1674 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001675
1676 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001677 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001678
1679#ifdef FEAT_VARTABS
1680 new_ts_str = eap->arg;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02001681 if (tabstop_set(eap->arg, &new_vts_array) == FAIL)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001682 return;
1683 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1684 ++(eap->arg);
1685
1686 // This ensures that either new_vts_array and new_ts_str are freshly
1687 // allocated, or new_vts_array points to an existing array and new_ts_str
1688 // is null.
1689 if (new_vts_array == NULL)
1690 {
1691 new_vts_array = curbuf->b_p_vts_array;
1692 new_ts_str = NULL;
1693 }
1694 else
1695 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1696#else
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001697 ptr = eap->arg;
1698 new_ts = getdigits(&ptr);
1699 if (new_ts < 0 && *eap->arg == '-')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001700 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001701 emsg(_(e_argument_must_be_positive));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001702 return;
1703 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00001704 if (new_ts < 0 || new_ts > TABSTOP_MAX)
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001705 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001706 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001707 return;
1708 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001709 if (new_ts == 0)
1710 new_ts = curbuf->b_p_ts;
1711#endif
1712 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1713 {
1714 ptr = ml_get(lnum);
1715 col = 0;
1716 vcol = 0;
1717 did_undo = FALSE;
1718 for (;;)
1719 {
1720 if (VIM_ISWHITE(ptr[col]))
1721 {
1722 if (!got_tab && num_spaces == 0)
1723 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001724 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001725 start_vcol = vcol;
1726 start_col = col;
1727 }
1728 if (ptr[col] == ' ')
1729 num_spaces++;
1730 else
1731 got_tab = TRUE;
1732 }
1733 else
1734 {
1735 if (got_tab || (eap->forceit && num_spaces > 1))
1736 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001737 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001738
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001739 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001740 len = num_spaces = vcol - start_vcol;
1741 num_tabs = 0;
1742 if (!curbuf->b_p_et)
1743 {
1744#ifdef FEAT_VARTABS
1745 int t, s;
1746
1747 tabstop_fromto(start_vcol, vcol,
1748 curbuf->b_p_ts, new_vts_array, &t, &s);
1749 num_tabs = t;
1750 num_spaces = s;
1751#else
1752 temp = new_ts - (start_vcol % new_ts);
1753 if (num_spaces >= temp)
1754 {
1755 num_spaces -= temp;
1756 num_tabs++;
1757 }
1758 num_tabs += num_spaces / new_ts;
1759 num_spaces -= (num_spaces / new_ts) * new_ts;
1760#endif
1761 }
1762 if (curbuf->b_p_et || got_tab ||
1763 (num_spaces + num_tabs < len))
1764 {
1765 if (did_undo == FALSE)
1766 {
1767 did_undo = TRUE;
1768 if (u_save((linenr_T)(lnum - 1),
1769 (linenr_T)(lnum + 1)) == FAIL)
1770 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001771 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001772 break;
1773 }
1774 }
1775
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001776 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001777 len = num_spaces + num_tabs;
1778 old_len = (long)STRLEN(ptr);
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001779 new_len = old_len - col + start_col + len + 1;
Bram Moolenaar45491662022-02-12 21:59:51 +00001780 if (new_len <= 0 || new_len >= MAXCOL)
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001781 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001782 emsg_text_too_long();
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001783 break;
1784 }
1785 new_line = alloc(new_len);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001786 if (new_line == NULL)
1787 break;
1788 if (start_col > 0)
1789 mch_memmove(new_line, ptr, (size_t)start_col);
1790 mch_memmove(new_line + start_col + len,
1791 ptr + col, (size_t)(old_len - col + 1));
1792 ptr = new_line + start_col;
1793 for (col = 0; col < len; col++)
1794 ptr[col] = (col < num_tabs) ? '\t' : ' ';
Bram Moolenaar0dcd39b2021-02-03 19:44:25 +01001795 if (ml_replace(lnum, new_line, FALSE) == OK)
1796 // "new_line" may have been copied
1797 new_line = curbuf->b_ml.ml_line_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001798 if (first_line == 0)
1799 first_line = lnum;
1800 last_line = lnum;
1801 ptr = new_line;
1802 col = start_col + len;
1803 }
1804 }
1805 got_tab = FALSE;
1806 num_spaces = 0;
1807 }
1808 if (ptr[col] == NUL)
1809 break;
1810 vcol += chartabsize(ptr + col, (colnr_T)vcol);
Bram Moolenaar6e287032022-02-12 15:42:18 +00001811 if (vcol >= MAXCOL)
1812 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001813 emsg_text_too_long();
Bram Moolenaar6e287032022-02-12 15:42:18 +00001814 break;
1815 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001816 if (has_mbyte)
1817 col += (*mb_ptr2len)(ptr + col);
1818 else
1819 ++col;
1820 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001821 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001822 break;
1823 line_breakcheck();
1824 }
1825 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001826 emsg(_(e_interrupted));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001827
1828#ifdef FEAT_VARTABS
1829 // If a single value was given then it can be considered equal to
1830 // either the value of 'tabstop' or the value of 'vartabstop'.
1831 if (tabstop_count(curbuf->b_p_vts_array) == 0
1832 && tabstop_count(new_vts_array) == 1
1833 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001834 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001835 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001836 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001837 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001838 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001839 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001840#else
1841 if (curbuf->b_p_ts != new_ts)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001842 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001843#endif
1844 if (first_line != 0)
1845 changed_lines(first_line, 0, last_line + 1, 0L);
1846
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001847 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001848
1849#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001850 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001851 {
1852 // If 'vartabstop' is in use or if the value given to retab has more
1853 // than one tabstop then update 'vartabstop'.
1854 int *old_vts_ary = curbuf->b_p_vts_array;
1855
1856 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1857 {
1858 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1859 OPT_FREE|OPT_LOCAL, 0);
1860 curbuf->b_p_vts_array = new_vts_array;
1861 vim_free(old_vts_ary);
1862 }
1863 else
1864 {
1865 // 'vartabstop' wasn't in use and a single value was given to
1866 // retab then update 'tabstop'.
1867 curbuf->b_p_ts = tabstop_first(new_vts_array);
1868 vim_free(new_vts_array);
1869 }
1870 vim_free(new_ts_str);
1871 }
1872#else
1873 curbuf->b_p_ts = new_ts;
1874#endif
1875 coladvance(curwin->w_curswant);
1876
1877 u_clearline();
1878}
1879
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001880#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001881/*
1882 * Get indent level from 'indentexpr'.
1883 */
1884 int
1885get_expr_indent(void)
1886{
1887 int indent = -1;
1888 char_u *inde_copy;
1889 pos_T save_pos;
1890 colnr_T save_curswant;
1891 int save_set_curswant;
1892 int save_State;
1893 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1894 OPT_LOCAL);
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001895 sctx_T save_sctx = current_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001896
1897 // Save and restore cursor position and curswant, in case it was changed
1898 // via :normal commands
1899 save_pos = curwin->w_cursor;
1900 save_curswant = curwin->w_curswant;
1901 save_set_curswant = curwin->w_set_curswant;
1902 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1903 if (use_sandbox)
1904 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001905 ++textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001906 current_sctx = curbuf->b_p_script_ctx[BV_INDE];
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001907
1908 // Need to make a copy, the 'indentexpr' option could be changed while
1909 // evaluating it.
1910 inde_copy = vim_strsave(curbuf->b_p_inde);
1911 if (inde_copy != NULL)
1912 {
Bram Moolenaar3292a222022-10-01 20:17:17 +01001913 indent = (int)eval_to_number(inde_copy, TRUE);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001914 vim_free(inde_copy);
1915 }
1916
1917 if (use_sandbox)
1918 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001919 --textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001920 current_sctx = save_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001921
1922 // Restore the cursor position so that 'indentexpr' doesn't need to.
1923 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1924 // command.
1925 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001926 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001927 curwin->w_cursor = save_pos;
1928 curwin->w_curswant = save_curswant;
1929 curwin->w_set_curswant = save_set_curswant;
1930 check_cursor();
1931 State = save_State;
1932
Bram Moolenaar620c9592021-07-31 21:32:31 +02001933 // Reset did_throw, unless 'debug' has "throw" and inside a try/catch.
1934 if (did_throw && (vim_strchr(p_debug, 't') == NULL || trylevel == 0))
1935 {
1936 handle_did_throw();
1937 did_throw = FALSE;
1938 }
1939
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001940 // If there is an error, just keep the current indent.
1941 if (indent < 0)
1942 indent = get_indent();
1943
1944 return indent;
1945}
1946#endif
1947
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001948 static int
1949lisp_match(char_u *p)
1950{
1951 char_u buf[LSIZE];
1952 int len;
1953 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1954
1955 while (*word != NUL)
1956 {
1957 (void)copy_option_part(&word, buf, LSIZE, ",");
1958 len = (int)STRLEN(buf);
Bram Moolenaard26c5802022-10-13 12:30:08 +01001959 if (STRNCMP(buf, p, len) == 0 && IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001960 return TRUE;
1961 }
1962 return FALSE;
1963}
1964
1965/*
1966 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
1967 * The incompatible newer method is quite a bit better at indenting
1968 * code in lisp-like languages than the traditional one; it's still
1969 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
1970 *
1971 * TODO:
1972 * Findmatch() should be adapted for lisp, also to make showmatch
1973 * work correctly: now (v5.3) it seems all C/C++ oriented:
1974 * - it does not recognize the #\( and #\) notations as character literals
1975 * - it doesn't know about comments starting with a semicolon
1976 * - it incorrectly interprets '(' as a character literal
1977 * All this messes up get_lisp_indent in some rare cases.
1978 * Update from Sergey Khorev:
1979 * I tried to fix the first two issues.
1980 */
1981 int
1982get_lisp_indent(void)
1983{
1984 pos_T *pos, realpos, paren;
1985 int amount;
1986 char_u *that;
1987 colnr_T col;
1988 colnr_T firsttry;
1989 int parencount, quotecount;
1990 int vi_lisp;
1991
1992 // Set vi_lisp to use the vi-compatible method
1993 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
1994
1995 realpos = curwin->w_cursor;
1996 curwin->w_cursor.col = 0;
1997
1998 if ((pos = findmatch(NULL, '(')) == NULL)
1999 pos = findmatch(NULL, '[');
2000 else
2001 {
2002 paren = *pos;
2003 pos = findmatch(NULL, '[');
2004 if (pos == NULL || LT_POSP(pos, &paren))
2005 pos = &paren;
2006 }
2007 if (pos != NULL)
2008 {
2009 // Extra trick: Take the indent of the first previous non-white
2010 // line that is at the same () level.
2011 amount = -1;
2012 parencount = 0;
2013
2014 while (--curwin->w_cursor.lnum >= pos->lnum)
2015 {
2016 if (linewhite(curwin->w_cursor.lnum))
2017 continue;
2018 for (that = ml_get_curline(); *that != NUL; ++that)
2019 {
2020 if (*that == ';')
2021 {
2022 while (*(that + 1) != NUL)
2023 ++that;
2024 continue;
2025 }
2026 if (*that == '\\')
2027 {
2028 if (*(that + 1) != NUL)
2029 ++that;
2030 continue;
2031 }
2032 if (*that == '"' && *(that + 1) != NUL)
2033 {
2034 while (*++that && *that != '"')
2035 {
2036 // skipping escaped characters in the string
2037 if (*that == '\\')
2038 {
2039 if (*++that == NUL)
2040 break;
2041 if (that[1] == NUL)
2042 {
2043 ++that;
2044 break;
2045 }
2046 }
2047 }
Bram Moolenaar0e8e9382022-06-18 12:51:11 +01002048 if (*that == NUL)
2049 break;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002050 }
2051 if (*that == '(' || *that == '[')
2052 ++parencount;
2053 else if (*that == ')' || *that == ']')
2054 --parencount;
2055 }
2056 if (parencount == 0)
2057 {
2058 amount = get_indent();
2059 break;
2060 }
2061 }
2062
2063 if (amount == -1)
2064 {
2065 curwin->w_cursor.lnum = pos->lnum;
2066 curwin->w_cursor.col = pos->col;
2067 col = pos->col;
2068
2069 that = ml_get_curline();
2070
2071 if (vi_lisp && get_indent() == 0)
2072 amount = 2;
2073 else
2074 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002075 char_u *line = that;
2076 chartabsize_T cts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002077
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002078 init_chartabsize_arg(&cts, curwin, pos->lnum, 0, line, line);
2079 while (*cts.cts_ptr != NUL && col > 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002080 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002081 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002082 col--;
2083 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002084 amount = cts.cts_vcol;
2085 that = cts.cts_ptr;
2086 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002087
2088 // Some keywords require "body" indenting rules (the
2089 // non-standard-lisp ones are Scheme special forms):
2090 //
2091 // (let ((a 1)) instead (let ((a 1))
2092 // (...)) of (...))
2093
2094 if (!vi_lisp && (*that == '(' || *that == '[')
2095 && lisp_match(that + 1))
2096 amount += 2;
2097 else
2098 {
Bram Moolenaar8eba2bd2022-06-22 19:59:28 +01002099 if (*that != NUL)
2100 {
2101 that++;
2102 amount++;
2103 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002104 firsttry = amount;
2105
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002106 init_chartabsize_arg(&cts, curwin, (colnr_T)(that - line),
2107 amount, line, that);
2108 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002109 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002110 cts.cts_vcol += lbr_chartabsize(&cts);
2111 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002112 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002113 that = cts.cts_ptr;
2114 amount = cts.cts_vcol;
2115 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002116
2117 if (*that && *that != ';') // not a comment line
2118 {
2119 // test *that != '(' to accommodate first let/do
2120 // argument if it is more than one line
2121 if (!vi_lisp && *that != '(' && *that != '[')
2122 firsttry++;
2123
2124 parencount = 0;
2125 quotecount = 0;
2126
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002127 init_chartabsize_arg(&cts, curwin,
2128 (colnr_T)(that - line), amount, line, that);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002129 if (vi_lisp
2130 || (*that != '"'
2131 && *that != '\''
2132 && *that != '#'
2133 && (*that < '0' || *that > '9')))
2134 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002135 while (*cts.cts_ptr
2136 && (!VIM_ISWHITE(*cts.cts_ptr)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002137 || quotecount
2138 || parencount)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002139 && (!((*cts.cts_ptr == '('
2140 || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002141 && !quotecount
2142 && !parencount
2143 && vi_lisp)))
2144 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002145 if (*cts.cts_ptr == '"')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002146 quotecount = !quotecount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002147 if ((*cts.cts_ptr == '(' || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002148 && !quotecount)
2149 ++parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002150 if ((*cts.cts_ptr == ')' || *cts.cts_ptr == ']')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002151 && !quotecount)
2152 --parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002153 if (*cts.cts_ptr == '\\'
2154 && *(cts.cts_ptr+1) != NUL)
2155 cts.cts_vcol += lbr_chartabsize_adv(&cts);
2156 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002157 }
2158 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002159 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002160 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002161 cts.cts_vcol += lbr_chartabsize(&cts);
2162 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002163 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002164 that = cts.cts_ptr;
2165 amount = cts.cts_vcol;
2166 clear_chartabsize_arg(&cts);
2167
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002168 if (!*that || *that == ';')
2169 amount = firsttry;
2170 }
2171 }
2172 }
2173 }
2174 }
2175 else
2176 amount = 0; // no matching '(' or '[' found, use zero indent
2177
2178 curwin->w_cursor = realpos;
2179
2180 return amount;
2181}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002182
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002183/*
2184 * Re-indent the current line, based on the current contents of it and the
2185 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
2186 * confused what all the part that handles Control-T is doing that I'm not.
2187 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
2188 */
2189
2190 void
2191fixthisline(int (*get_the_indent)(void))
2192{
2193 int amount = get_the_indent();
2194
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002195 if (amount < 0)
2196 return;
2197
2198 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
2199 if (linewhite(curwin->w_cursor.lnum))
2200 did_ai = TRUE; // delete the indent if the line stays empty
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002201}
2202
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002203/*
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002204 * Return TRUE if 'indentexpr' should be used for Lisp indenting.
2205 * Caller may want to check 'autoindent'.
2206 */
2207 int
2208use_indentexpr_for_lisp(void)
2209{
2210#ifdef FEAT_EVAL
2211 return curbuf->b_p_lisp
2212 && *curbuf->b_p_inde != NUL
2213 && STRCMP(curbuf->b_p_lop, "expr:1") == 0;
2214#else
2215 return FALSE;
2216#endif
2217}
2218
2219/*
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002220 * Fix indent for 'lisp' and 'cindent'.
2221 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002222 void
2223fix_indent(void)
2224{
2225 if (p_paste)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002226 return; // no auto-indenting when 'paste' is set
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002227 if (curbuf->b_p_lisp && curbuf->b_p_ai)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002228 {
2229 if (use_indentexpr_for_lisp())
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002230 do_c_expr_indent();
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002231 else
2232 fixthisline(get_lisp_indent);
2233 }
2234 else if (cindent_on())
2235 do_c_expr_indent();
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002236}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002237
2238#if defined(FEAT_EVAL) || defined(PROTO)
2239/*
2240 * "indent()" function
2241 */
2242 void
2243f_indent(typval_T *argvars, typval_T *rettv)
2244{
2245 linenr_T lnum;
2246
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002247 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2248 return;
2249
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002250 lnum = tv_get_lnum(argvars);
2251 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2252 rettv->vval.v_number = get_indent_lnum(lnum);
2253 else
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002254 {
2255 if (in_vim9script())
2256 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002257 rettv->vval.v_number = -1;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002258 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002259}
2260
2261/*
2262 * "lispindent(lnum)" function
2263 */
2264 void
2265f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2266{
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002267 pos_T pos;
2268 linenr_T lnum;
2269
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002270 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2271 return;
2272
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002273 pos = curwin->w_cursor;
2274 lnum = tv_get_lnum(argvars);
2275 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2276 {
2277 curwin->w_cursor.lnum = lnum;
2278 rettv->vval.v_number = get_lisp_indent();
2279 curwin->w_cursor = pos;
2280 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002281 else if (in_vim9script())
2282 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002283 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002284 rettv->vval.v_number = -1;
2285}
2286#endif