blob: 34d31579bd89c5629ba1781f2c2ee7d39b74e9df [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)
166 return (col / ts) * ts;
167
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];
177 return excess + ((col - excess) / vts[tabcount]) * vts[tabcount];
178}
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
938 - ((wp->w_p_nu || wp->w_p_rnu)
939 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
940 ? number_width(wp) + 1 : 0);
941
zeertzjqefabd7c2024-02-11 17:16:19 +0100942 // In list mode, if 'listchars' "tab" isn't set, a TAB is displayed as ^I.
943 int no_ts = wp->w_p_list && wp->w_lcs_chars.tab1 == NUL;
944
945 // Used cached indent, unless
946 // - buffer changed, or
947 // - 'tabstop' changed, or
948 // - 'vartabstop' changed, or
949 // - buffer was changed, or
950 // - 'breakindentopt' "list" changed, or
951 // - 'list' or 'listchars' "tab" changed, or
952 // - 'display' "uhex" flag changed, or
953 // - 'formatlistpat' changed, or
954 // - line changed.
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100955 if (prev_fnum != wp->w_buffer->b_fnum
956 || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200957# ifdef FEAT_VARTABS
958 || prev_vts != wp->w_buffer->b_p_vts_array
959# endif
zeertzjqefabd7c2024-02-11 17:16:19 +0100960 || prev_tick != CHANGEDTICK(wp->w_buffer)
961 || prev_listopt != wp->w_briopt_list
962 || prev_no_ts != no_ts
963 || prev_dy_uhex != (dy_flags & DY_UHEX)
964 || prev_flp == NULL
965 || STRCMP(prev_flp, get_flp_value(wp->w_buffer)) != 0
966 || prev_line == NULL || STRCMP(prev_line, line) != 0
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200967 )
968 {
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100969 prev_fnum = wp->w_buffer->b_fnum;
970 vim_free(prev_line);
971 prev_line = vim_strsave(line);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200972 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200973# ifdef FEAT_VARTABS
974 prev_vts = wp->w_buffer->b_p_vts_array;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100975 if (wp->w_briopt_vcol == 0)
976 prev_indent = get_indent_str_vtab(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200977 (int)wp->w_buffer->b_p_ts,
zeertzjqefabd7c2024-02-11 17:16:19 +0100978 wp->w_buffer->b_p_vts_array, no_ts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200979# else
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100980 if (wp->w_briopt_vcol == 0)
981 prev_indent = get_indent_str(line,
zeertzjqefabd7c2024-02-11 17:16:19 +0100982 (int)wp->w_buffer->b_p_ts, no_ts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200983# endif
zeertzjqefabd7c2024-02-11 17:16:19 +0100984 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000985 prev_listopt = wp->w_briopt_list;
Christian Brabandtc53b4672022-01-15 10:01:05 +0000986 prev_list = 0;
zeertzjqefabd7c2024-02-11 17:16:19 +0100987 prev_no_ts = no_ts;
988 prev_dy_uhex = (dy_flags & DY_UHEX);
Christian Brabandtc53b4672022-01-15 10:01:05 +0000989 vim_free(prev_flp);
990 prev_flp = vim_strsave(get_flp_value(wp->w_buffer));
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000991 // add additional indent for numbered lists
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100992 if (wp->w_briopt_list != 0 && wp->w_briopt_vcol == 0)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000993 {
994 regmatch_T regmatch;
995
Christian Brabandtc53b4672022-01-15 10:01:05 +0000996 regmatch.regprog = vim_regcomp(prev_flp,
997 RE_MAGIC + RE_STRING + RE_AUTO + RE_STRICT);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000998
999 if (regmatch.regprog != NULL)
1000 {
1001 regmatch.rm_ic = FALSE;
1002 if (vim_regexec(&regmatch, line, 0))
1003 {
1004 if (wp->w_briopt_list > 0)
1005 prev_list = wp->w_briopt_list;
1006 else
Maxim Kim11916722022-09-02 14:08:53 +01001007 prev_indent = (*regmatch.endp - *regmatch.startp);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001008 }
1009 vim_regfree(regmatch.regprog);
1010 }
1011 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001012 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001013 if (wp->w_briopt_vcol != 0)
1014 {
1015 // column value has priority
1016 bri = wp->w_briopt_vcol;
1017 prev_list = 0;
1018 }
1019 else
1020 bri = prev_indent + wp->w_briopt_shift;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001021
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001022 // Add offset for number column, if 'n' is in 'cpoptions'
1023 bri += win_col_off2(wp);
1024
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001025 // add additional indent for numbered lists
Maxim Kim11916722022-09-02 14:08:53 +01001026 if (wp->w_briopt_list > 0)
1027 bri += prev_list;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001028
Maxim Kimf674b352021-07-22 11:46:59 +02001029 // indent minus the length of the showbreak string
1030 if (wp->w_briopt_sbr)
1031 bri -= vim_strsize(get_showbreak_value(wp));
1032
1033
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001034 // never indent past left window margin
1035 if (bri < 0)
1036 bri = 0;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001037
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001038 // always leave at least bri_min characters on the left,
1039 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001040 else if (bri > eff_wwidth - wp->w_briopt_min)
1041 bri = (eff_wwidth - wp->w_briopt_min < 0)
1042 ? 0 : eff_wwidth - wp->w_briopt_min;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001043
1044 return bri;
1045}
1046#endif
1047
1048/*
1049 * When extra == 0: Return TRUE if the cursor is before or on the first
1050 * non-blank in the line.
1051 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1052 * the line.
1053 */
1054 int
1055inindent(int extra)
1056{
1057 char_u *ptr;
1058 colnr_T col;
1059
1060 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
1061 ++ptr;
1062 if (col >= curwin->w_cursor.col + extra)
1063 return TRUE;
1064 else
1065 return FALSE;
1066}
1067
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001068/*
1069 * op_reindent - handle reindenting a block of lines.
1070 */
1071 void
1072op_reindent(oparg_T *oap, int (*how)(void))
1073{
Bram Moolenaar4c84dd32022-04-20 10:22:54 +01001074 long i = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001075 char_u *l;
1076 int amount;
1077 linenr_T first_changed = 0;
1078 linenr_T last_changed = 0;
1079 linenr_T start_lnum = curwin->w_cursor.lnum;
1080
1081 // Don't even try when 'modifiable' is off.
1082 if (!curbuf->b_p_ma)
1083 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001084 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001085 return;
1086 }
1087
Bram Moolenaare4686982022-04-19 18:28:45 +01001088 // Save for undo. Do this once for all lines, much faster than doing this
1089 // for each line separately, especially when undoing.
1090 if (u_savecommon(start_lnum - 1, start_lnum + oap->line_count,
1091 start_lnum + oap->line_count, FALSE) == OK)
1092 for (i = oap->line_count; --i >= 0 && !got_int; )
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001093 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001094 // it's a slow thing to do, so give feedback so there's no worry
1095 // that the computer's just hung.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001096
Bram Moolenaare4686982022-04-19 18:28:45 +01001097 if (i > 1
1098 && (i % 50 == 0 || i == oap->line_count - 1)
1099 && oap->line_count > p_report)
1100 smsg(_("%ld lines to indent... "), i);
1101
1102 // Be vi-compatible: For lisp indenting the first line is not
1103 // indented, unless there is only one line.
Bram Moolenaare4686982022-04-19 18:28:45 +01001104 if (i != oap->line_count - 1 || oap->line_count == 1
1105 || how != get_lisp_indent)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001106 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001107 l = skipwhite(ml_get_curline());
1108 if (*l == NUL) // empty or blank line
1109 amount = 0;
1110 else
1111 amount = how(); // get the indent for this line
1112
1113 if (amount >= 0 && set_indent(amount, 0))
1114 {
1115 // did change the indent, call changed_lines() later
1116 if (first_changed == 0)
1117 first_changed = curwin->w_cursor.lnum;
1118 last_changed = curwin->w_cursor.lnum;
1119 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001120 }
Bram Moolenaare4686982022-04-19 18:28:45 +01001121 ++curwin->w_cursor.lnum;
1122 curwin->w_cursor.col = 0; // make sure it's valid
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001123 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001124
1125 // put cursor on first non-blank of indented line
1126 curwin->w_cursor.lnum = start_lnum;
1127 beginline(BL_SOL | BL_FIX);
1128
1129 // Mark changed lines so that they will be redrawn. When Visual
1130 // highlighting was present, need to continue until the last line. When
1131 // there is no change still need to remove the Visual highlighting.
1132 if (last_changed != 0)
1133 changed_lines(first_changed, 0,
1134 oap->is_VIsual ? start_lnum + oap->line_count :
1135 last_changed + 1, 0L);
1136 else if (oap->is_VIsual)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001137 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001138
1139 if (oap->line_count > p_report)
1140 {
1141 i = oap->line_count - (i + 1);
1142 smsg(NGETTEXT("%ld line indented ",
1143 "%ld lines indented ", i), i);
1144 }
Bram Moolenaare1004402020-10-24 20:49:43 +02001145 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001146 {
1147 // set '[ and '] marks
1148 curbuf->b_op_start = oap->start;
1149 curbuf->b_op_end = oap->end;
1150 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001151}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001152
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001153/*
1154 * Return TRUE if lines starting with '#' should be left aligned.
1155 */
1156 int
1157preprocs_left(void)
1158{
1159 return
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001160 (curbuf->b_p_si && !curbuf->b_p_cin) ||
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001161 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1162 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001163 ;
1164}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001165
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001166/*
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001167 * Return TRUE if the conditions are OK for smart indenting.
1168 */
1169 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001170may_do_si(void)
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001171{
1172 return curbuf->b_p_si
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001173 && !curbuf->b_p_cin
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001174# ifdef FEAT_EVAL
1175 && *curbuf->b_p_inde == NUL
1176# endif
1177 && !p_paste;
1178}
1179
1180/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001181 * Try to do some very smart auto-indenting.
1182 * Used when inserting a "normal" character.
1183 */
1184 void
1185ins_try_si(int c)
1186{
1187 pos_T *pos, old_pos;
1188 char_u *ptr;
1189 int i;
1190 int temp;
1191
1192 // do some very smart indenting when entering '{' or '}'
Bram Moolenaar2e444bb2022-05-14 12:54:23 +01001193 if (((did_si || can_si_back) && c == '{')
1194 || (can_si && c == '}' && inindent(0)))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001195 {
1196 // for '}' set indent equal to indent of line containing matching '{'
1197 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1198 {
1199 old_pos = curwin->w_cursor;
1200 // If the matching '{' has a ')' immediately before it (ignoring
1201 // white-space), then line up with the start of the line
1202 // containing the matching '(' if there is one. This handles the
1203 // case where an "if (..\n..) {" statement continues over multiple
1204 // lines -- webb
1205 ptr = ml_get(pos->lnum);
1206 i = pos->col;
1207 if (i > 0) // skip blanks before '{'
1208 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1209 ;
1210 curwin->w_cursor.lnum = pos->lnum;
1211 curwin->w_cursor.col = i;
1212 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1213 curwin->w_cursor = *pos;
1214 i = get_indent();
1215 curwin->w_cursor = old_pos;
1216 if (State & VREPLACE_FLAG)
1217 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1218 else
1219 (void)set_indent(i, SIN_CHANGED);
1220 }
1221 else if (curwin->w_cursor.col > 0)
1222 {
1223 // when inserting '{' after "O" reduce indent, but not
1224 // more than indent of previous line
1225 temp = TRUE;
1226 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1227 {
1228 old_pos = curwin->w_cursor;
1229 i = get_indent();
1230 while (curwin->w_cursor.lnum > 1)
1231 {
1232 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1233
1234 // ignore empty lines and lines starting with '#'.
1235 if (*ptr != '#' && *ptr != NUL)
1236 break;
1237 }
1238 if (get_indent() >= i)
1239 temp = FALSE;
1240 curwin->w_cursor = old_pos;
1241 }
1242 if (temp)
1243 shift_line(TRUE, FALSE, 1, TRUE);
1244 }
1245 }
1246
1247 // set indent of '#' always to 0
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001248 if (curwin->w_cursor.col > 0 && can_si && c == '#' && inindent(0))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001249 {
1250 // remember current indent for next line
1251 old_indent = get_indent();
1252 (void)set_indent(0, SIN_CHANGED);
1253 }
1254
1255 // Adjust ai_col, the char at this position can be deleted.
1256 if (ai_col > curwin->w_cursor.col)
1257 ai_col = curwin->w_cursor.col;
1258}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001259
1260/*
1261 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1262 * Keep the cursor on the same character.
1263 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1264 * type == INDENT_DEC decrease indent (for CTRL-D)
1265 * type == INDENT_SET set indent to "amount"
1266 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1267 */
1268 void
1269change_indent(
1270 int type,
1271 int amount,
1272 int round,
1273 int replaced, // replaced character, put on replace stack
1274 int call_changed_bytes) // call changed_bytes()
1275{
1276 int vcol;
1277 int last_vcol;
1278 int insstart_less; // reduction for Insstart.col
1279 int new_cursor_col;
1280 int i;
1281 char_u *ptr;
1282 int save_p_list;
1283 int start_col;
1284 colnr_T vc;
1285 colnr_T orig_col = 0; // init for GCC
1286 char_u *new_line, *orig_line = NULL; // init for GCC
1287
Bram Moolenaar24959102022-05-07 20:01:16 +01001288 // MODE_VREPLACE state needs to know what the line was like before changing
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001289 if (State & VREPLACE_FLAG)
1290 {
1291 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1292 orig_col = curwin->w_cursor.col;
1293 }
1294
1295 // for the following tricks we don't want list mode
1296 save_p_list = curwin->w_p_list;
1297 curwin->w_p_list = FALSE;
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001298#ifdef FEAT_PROP_POPUP
1299 ignore_text_props = TRUE;
1300#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001301 vc = getvcol_nolist(&curwin->w_cursor);
1302 vcol = vc;
1303
1304 // For Replace mode we need to fix the replace stack later, which is only
1305 // possible when the cursor is in the indent. Remember the number of
1306 // characters before the cursor if it's possible.
1307 start_col = curwin->w_cursor.col;
1308
1309 // determine offset from first non-blank
1310 new_cursor_col = curwin->w_cursor.col;
1311 beginline(BL_WHITE);
1312 new_cursor_col -= curwin->w_cursor.col;
1313
1314 insstart_less = curwin->w_cursor.col;
1315
1316 // If the cursor is in the indent, compute how many screen columns the
1317 // cursor is to the left of the first non-blank.
1318 if (new_cursor_col < 0)
1319 vcol = get_indent() - vcol;
1320
1321 if (new_cursor_col > 0) // can't fix replace stack
1322 start_col = -1;
1323
1324 // Set the new indent. The cursor will be put on the first non-blank.
1325 if (type == INDENT_SET)
1326 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1327 else
1328 {
1329 int save_State = State;
1330
1331 // Avoid being called recursively.
1332 if (State & VREPLACE_FLAG)
Bram Moolenaar24959102022-05-07 20:01:16 +01001333 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001334 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1335 State = save_State;
1336 }
1337 insstart_less -= curwin->w_cursor.col;
1338
1339 // Try to put cursor on same character.
1340 // If the cursor is at or after the first non-blank in the line,
1341 // compute the cursor column relative to the column of the first
1342 // non-blank character.
1343 // If we are not in insert mode, leave the cursor on the first non-blank.
1344 // If the cursor is before the first non-blank, position it relative
1345 // to the first non-blank, counted in screen columns.
1346 if (new_cursor_col >= 0)
1347 {
1348 // When changing the indent while the cursor is touching it, reset
1349 // Insstart_col to 0.
1350 if (new_cursor_col == 0)
1351 insstart_less = MAXCOL;
1352 new_cursor_col += curwin->w_cursor.col;
1353 }
Bram Moolenaar24959102022-05-07 20:01:16 +01001354 else if (!(State & MODE_INSERT))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001355 new_cursor_col = curwin->w_cursor.col;
1356 else
1357 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001358 chartabsize_T cts;
1359
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001360 // Compute the screen column where the cursor should be.
1361 vcol = get_indent() - vcol;
1362 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1363
1364 // Advance the cursor until we reach the right screen column.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001365 last_vcol = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001366 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001367 init_chartabsize_arg(&cts, curwin, 0, 0, ptr, ptr);
1368 while (cts.cts_vcol <= (int)curwin->w_virtcol)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001369 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001370 last_vcol = cts.cts_vcol;
1371 if (cts.cts_vcol > 0)
1372 MB_PTR_ADV(cts.cts_ptr);
1373 if (*cts.cts_ptr == NUL)
Bram Moolenaar4e889f92022-02-21 19:36:12 +00001374 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001375 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001376 }
1377 vcol = last_vcol;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001378 new_cursor_col = cts.cts_ptr - cts.cts_line;
1379 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001380
1381 // May need to insert spaces to be able to position the cursor on
1382 // the right screen column.
1383 if (vcol != (int)curwin->w_virtcol)
1384 {
1385 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1386 i = (int)curwin->w_virtcol - vcol;
1387 ptr = alloc(i + 1);
1388 if (ptr != NULL)
1389 {
1390 new_cursor_col += i;
1391 ptr[i] = NUL;
1392 while (--i >= 0)
1393 ptr[i] = ' ';
1394 ins_str(ptr);
1395 vim_free(ptr);
1396 }
1397 }
1398
1399 // When changing the indent while the cursor is in it, reset
1400 // Insstart_col to 0.
1401 insstart_less = MAXCOL;
1402 }
1403
1404 curwin->w_p_list = save_p_list;
1405
1406 if (new_cursor_col <= 0)
1407 curwin->w_cursor.col = 0;
1408 else
1409 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1410 curwin->w_set_curswant = TRUE;
1411 changed_cline_bef_curs();
1412
1413 // May have to adjust the start of the insert.
Bram Moolenaar24959102022-05-07 20:01:16 +01001414 if (State & MODE_INSERT)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001415 {
1416 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1417 {
1418 if ((int)Insstart.col <= insstart_less)
1419 Insstart.col = 0;
1420 else
1421 Insstart.col -= insstart_less;
1422 }
1423 if ((int)ai_col <= insstart_less)
1424 ai_col = 0;
1425 else
1426 ai_col -= insstart_less;
1427 }
1428
Bram Moolenaar24959102022-05-07 20:01:16 +01001429 // For MODE_REPLACE state, may have to fix the replace stack, if it's
1430 // possible. If the number of characters before the cursor decreased, need
1431 // to pop a few characters from the replace stack.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001432 // If the number of characters before the cursor increased, need to push a
1433 // few NULs onto the replace stack.
1434 if (REPLACE_NORMAL(State) && start_col >= 0)
1435 {
1436 while (start_col > (int)curwin->w_cursor.col)
1437 {
1438 replace_join(0); // remove a NUL from the replace stack
1439 --start_col;
1440 }
1441 while (start_col < (int)curwin->w_cursor.col || replaced)
1442 {
1443 replace_push(NUL);
1444 if (replaced)
1445 {
1446 replace_push(replaced);
1447 replaced = NUL;
1448 }
1449 ++start_col;
1450 }
1451 }
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001452#ifdef FEAT_PROP_POPUP
1453 ignore_text_props = FALSE;
1454#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001455
Bram Moolenaar24959102022-05-07 20:01:16 +01001456 // For MODE_VREPLACE state, we also have to fix the replace stack. In this
1457 // case it is always possible because we backspace over the whole line and
1458 // then put it back again the way we wanted it.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001459 if (State & VREPLACE_FLAG)
1460 {
1461 // If orig_line didn't allocate, just return. At least we did the job,
1462 // even if you can't backspace.
1463 if (orig_line == NULL)
1464 return;
1465
1466 // Save new line
1467 new_line = vim_strsave(ml_get_curline());
1468 if (new_line == NULL)
1469 return;
1470
1471 // We only put back the new line up to the cursor
1472 new_line[curwin->w_cursor.col] = NUL;
1473
1474 // Put back original line
1475 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1476 curwin->w_cursor.col = orig_col;
1477
1478 // Backspace from cursor to start of line
1479 backspace_until_column(0);
1480
1481 // Insert new stuff into line again
1482 ins_bytes(new_line);
1483
1484 vim_free(new_line);
1485 }
1486}
1487
1488/*
1489 * Copy the indent from ptr to the current line (and fill to size)
1490 * Leaves the cursor on the first non-blank in the line.
1491 * Returns TRUE if the line was changed.
1492 */
1493 int
1494copy_indent(int size, char_u *src)
1495{
1496 char_u *p = NULL;
1497 char_u *line = NULL;
1498 char_u *s;
1499 int todo;
1500 int ind_len;
1501 int line_len = 0;
1502 int tab_pad;
1503 int ind_done;
1504 int round;
1505#ifdef FEAT_VARTABS
1506 int ind_col;
1507#endif
1508
1509 // Round 1: compute the number of characters needed for the indent
1510 // Round 2: copy the characters.
1511 for (round = 1; round <= 2; ++round)
1512 {
1513 todo = size;
1514 ind_len = 0;
1515 ind_done = 0;
1516#ifdef FEAT_VARTABS
1517 ind_col = 0;
1518#endif
1519 s = src;
1520
1521 // Count/copy the usable portion of the source line
1522 while (todo > 0 && VIM_ISWHITE(*s))
1523 {
1524 if (*s == TAB)
1525 {
1526#ifdef FEAT_VARTABS
1527 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1528 curbuf->b_p_vts_array);
1529#else
1530 tab_pad = (int)curbuf->b_p_ts
1531 - (ind_done % (int)curbuf->b_p_ts);
1532#endif
1533 // Stop if this tab will overshoot the target
1534 if (todo < tab_pad)
1535 break;
1536 todo -= tab_pad;
1537 ind_done += tab_pad;
1538#ifdef FEAT_VARTABS
1539 ind_col += tab_pad;
1540#endif
1541 }
1542 else
1543 {
1544 --todo;
1545 ++ind_done;
1546#ifdef FEAT_VARTABS
1547 ++ind_col;
1548#endif
1549 }
1550 ++ind_len;
1551 if (p != NULL)
1552 *p++ = *s;
1553 ++s;
1554 }
1555
1556 // Fill to next tabstop with a tab, if possible
1557#ifdef FEAT_VARTABS
1558 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1559 curbuf->b_p_vts_array);
1560#else
1561 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1562#endif
1563 if (todo >= tab_pad && !curbuf->b_p_et)
1564 {
1565 todo -= tab_pad;
1566 ++ind_len;
1567#ifdef FEAT_VARTABS
1568 ind_col += tab_pad;
1569#endif
1570 if (p != NULL)
1571 *p++ = TAB;
1572 }
1573
1574 // Add tabs required for indent
1575 if (!curbuf->b_p_et)
1576 {
1577#ifdef FEAT_VARTABS
1578 for (;;)
1579 {
1580 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1581 curbuf->b_p_vts_array);
1582 if (todo < tab_pad)
1583 break;
1584 todo -= tab_pad;
1585 ++ind_len;
1586 ind_col += tab_pad;
1587 if (p != NULL)
1588 *p++ = TAB;
1589 }
1590#else
1591 while (todo >= (int)curbuf->b_p_ts)
1592 {
1593 todo -= (int)curbuf->b_p_ts;
1594 ++ind_len;
1595 if (p != NULL)
1596 *p++ = TAB;
1597 }
1598#endif
1599 }
1600
1601 // Count/add spaces required for indent
1602 while (todo > 0)
1603 {
1604 --todo;
1605 ++ind_len;
1606 if (p != NULL)
1607 *p++ = ' ';
1608 }
1609
1610 if (p == NULL)
1611 {
1612 // Allocate memory for the result: the copied indent, new indent
1613 // and the rest of the line.
1614 line_len = (int)STRLEN(ml_get_curline()) + 1;
1615 line = alloc(ind_len + line_len);
1616 if (line == NULL)
1617 return FALSE;
1618 p = line;
1619 }
1620 }
1621
1622 // Append the original line
1623 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1624
1625 // Replace the line
1626 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1627
1628 // Put the cursor after the indent.
1629 curwin->w_cursor.col = ind_len;
1630 return TRUE;
1631}
1632
1633/*
Bram Moolenaar308660b2022-06-16 12:10:48 +01001634 * Give a "resulting text too long" error and maybe set got_int.
1635 */
1636 static void
1637emsg_text_too_long(void)
1638{
1639 emsg(_(e_resulting_text_too_long));
1640#ifdef FEAT_EVAL
1641 // when not inside a try/catch set got_int to break out of any loop
1642 if (trylevel == 0)
1643#endif
1644 got_int = TRUE;
1645}
1646
1647/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001648 * ":retab".
1649 */
1650 void
1651ex_retab(exarg_T *eap)
1652{
1653 linenr_T lnum;
1654 int got_tab = FALSE;
1655 long num_spaces = 0;
1656 long num_tabs;
1657 long len;
1658 long col;
1659 long vcol;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001660 long start_col = 0; // For start of white-space string
1661 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001662 long old_len;
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001663 long new_len;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001664 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001665 char_u *new_line = (char_u *)1; // init to non-NULL
1666 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001667#ifdef FEAT_VARTABS
1668 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001669 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001670#else
1671 int temp;
1672 int new_ts;
1673#endif
1674 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001675 linenr_T first_line = 0; // first changed line
1676 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001677
1678 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001679 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001680
1681#ifdef FEAT_VARTABS
1682 new_ts_str = eap->arg;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02001683 if (tabstop_set(eap->arg, &new_vts_array) == FAIL)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001684 return;
1685 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1686 ++(eap->arg);
1687
1688 // This ensures that either new_vts_array and new_ts_str are freshly
1689 // allocated, or new_vts_array points to an existing array and new_ts_str
1690 // is null.
1691 if (new_vts_array == NULL)
1692 {
1693 new_vts_array = curbuf->b_p_vts_array;
1694 new_ts_str = NULL;
1695 }
1696 else
1697 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1698#else
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001699 ptr = eap->arg;
1700 new_ts = getdigits(&ptr);
1701 if (new_ts < 0 && *eap->arg == '-')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001702 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001703 emsg(_(e_argument_must_be_positive));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001704 return;
1705 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00001706 if (new_ts < 0 || new_ts > TABSTOP_MAX)
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001707 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001708 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001709 return;
1710 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001711 if (new_ts == 0)
1712 new_ts = curbuf->b_p_ts;
1713#endif
1714 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1715 {
1716 ptr = ml_get(lnum);
1717 col = 0;
1718 vcol = 0;
1719 did_undo = FALSE;
1720 for (;;)
1721 {
1722 if (VIM_ISWHITE(ptr[col]))
1723 {
1724 if (!got_tab && num_spaces == 0)
1725 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001726 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001727 start_vcol = vcol;
1728 start_col = col;
1729 }
1730 if (ptr[col] == ' ')
1731 num_spaces++;
1732 else
1733 got_tab = TRUE;
1734 }
1735 else
1736 {
1737 if (got_tab || (eap->forceit && num_spaces > 1))
1738 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001739 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001740
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001741 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001742 len = num_spaces = vcol - start_vcol;
1743 num_tabs = 0;
1744 if (!curbuf->b_p_et)
1745 {
1746#ifdef FEAT_VARTABS
1747 int t, s;
1748
1749 tabstop_fromto(start_vcol, vcol,
1750 curbuf->b_p_ts, new_vts_array, &t, &s);
1751 num_tabs = t;
1752 num_spaces = s;
1753#else
1754 temp = new_ts - (start_vcol % new_ts);
1755 if (num_spaces >= temp)
1756 {
1757 num_spaces -= temp;
1758 num_tabs++;
1759 }
1760 num_tabs += num_spaces / new_ts;
1761 num_spaces -= (num_spaces / new_ts) * new_ts;
1762#endif
1763 }
1764 if (curbuf->b_p_et || got_tab ||
1765 (num_spaces + num_tabs < len))
1766 {
1767 if (did_undo == FALSE)
1768 {
1769 did_undo = TRUE;
1770 if (u_save((linenr_T)(lnum - 1),
1771 (linenr_T)(lnum + 1)) == FAIL)
1772 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001773 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001774 break;
1775 }
1776 }
1777
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001778 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001779 len = num_spaces + num_tabs;
1780 old_len = (long)STRLEN(ptr);
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001781 new_len = old_len - col + start_col + len + 1;
Bram Moolenaar45491662022-02-12 21:59:51 +00001782 if (new_len <= 0 || new_len >= MAXCOL)
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001783 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001784 emsg_text_too_long();
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001785 break;
1786 }
1787 new_line = alloc(new_len);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001788 if (new_line == NULL)
1789 break;
1790 if (start_col > 0)
1791 mch_memmove(new_line, ptr, (size_t)start_col);
1792 mch_memmove(new_line + start_col + len,
1793 ptr + col, (size_t)(old_len - col + 1));
1794 ptr = new_line + start_col;
1795 for (col = 0; col < len; col++)
1796 ptr[col] = (col < num_tabs) ? '\t' : ' ';
Bram Moolenaar0dcd39b2021-02-03 19:44:25 +01001797 if (ml_replace(lnum, new_line, FALSE) == OK)
1798 // "new_line" may have been copied
1799 new_line = curbuf->b_ml.ml_line_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001800 if (first_line == 0)
1801 first_line = lnum;
1802 last_line = lnum;
1803 ptr = new_line;
1804 col = start_col + len;
1805 }
1806 }
1807 got_tab = FALSE;
1808 num_spaces = 0;
1809 }
1810 if (ptr[col] == NUL)
1811 break;
1812 vcol += chartabsize(ptr + col, (colnr_T)vcol);
Bram Moolenaar6e287032022-02-12 15:42:18 +00001813 if (vcol >= MAXCOL)
1814 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001815 emsg_text_too_long();
Bram Moolenaar6e287032022-02-12 15:42:18 +00001816 break;
1817 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001818 if (has_mbyte)
1819 col += (*mb_ptr2len)(ptr + col);
1820 else
1821 ++col;
1822 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001823 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001824 break;
1825 line_breakcheck();
1826 }
1827 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001828 emsg(_(e_interrupted));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001829
1830#ifdef FEAT_VARTABS
1831 // If a single value was given then it can be considered equal to
1832 // either the value of 'tabstop' or the value of 'vartabstop'.
1833 if (tabstop_count(curbuf->b_p_vts_array) == 0
1834 && tabstop_count(new_vts_array) == 1
1835 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001836 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001837 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001838 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001839 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001840 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001841 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001842#else
1843 if (curbuf->b_p_ts != new_ts)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001844 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001845#endif
1846 if (first_line != 0)
1847 changed_lines(first_line, 0, last_line + 1, 0L);
1848
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001849 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001850
1851#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001852 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001853 {
1854 // If 'vartabstop' is in use or if the value given to retab has more
1855 // than one tabstop then update 'vartabstop'.
1856 int *old_vts_ary = curbuf->b_p_vts_array;
1857
1858 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1859 {
1860 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1861 OPT_FREE|OPT_LOCAL, 0);
1862 curbuf->b_p_vts_array = new_vts_array;
1863 vim_free(old_vts_ary);
1864 }
1865 else
1866 {
1867 // 'vartabstop' wasn't in use and a single value was given to
1868 // retab then update 'tabstop'.
1869 curbuf->b_p_ts = tabstop_first(new_vts_array);
1870 vim_free(new_vts_array);
1871 }
1872 vim_free(new_ts_str);
1873 }
1874#else
1875 curbuf->b_p_ts = new_ts;
1876#endif
1877 coladvance(curwin->w_curswant);
1878
1879 u_clearline();
1880}
1881
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001882#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001883/*
1884 * Get indent level from 'indentexpr'.
1885 */
1886 int
1887get_expr_indent(void)
1888{
1889 int indent = -1;
1890 char_u *inde_copy;
1891 pos_T save_pos;
1892 colnr_T save_curswant;
1893 int save_set_curswant;
1894 int save_State;
1895 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1896 OPT_LOCAL);
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001897 sctx_T save_sctx = current_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001898
1899 // Save and restore cursor position and curswant, in case it was changed
1900 // via :normal commands
1901 save_pos = curwin->w_cursor;
1902 save_curswant = curwin->w_curswant;
1903 save_set_curswant = curwin->w_set_curswant;
1904 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1905 if (use_sandbox)
1906 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001907 ++textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001908 current_sctx = curbuf->b_p_script_ctx[BV_INDE];
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001909
1910 // Need to make a copy, the 'indentexpr' option could be changed while
1911 // evaluating it.
1912 inde_copy = vim_strsave(curbuf->b_p_inde);
1913 if (inde_copy != NULL)
1914 {
Bram Moolenaar3292a222022-10-01 20:17:17 +01001915 indent = (int)eval_to_number(inde_copy, TRUE);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001916 vim_free(inde_copy);
1917 }
1918
1919 if (use_sandbox)
1920 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001921 --textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001922 current_sctx = save_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001923
1924 // Restore the cursor position so that 'indentexpr' doesn't need to.
1925 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1926 // command.
1927 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001928 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001929 curwin->w_cursor = save_pos;
1930 curwin->w_curswant = save_curswant;
1931 curwin->w_set_curswant = save_set_curswant;
1932 check_cursor();
1933 State = save_State;
1934
Bram Moolenaar620c9592021-07-31 21:32:31 +02001935 // Reset did_throw, unless 'debug' has "throw" and inside a try/catch.
1936 if (did_throw && (vim_strchr(p_debug, 't') == NULL || trylevel == 0))
1937 {
1938 handle_did_throw();
1939 did_throw = FALSE;
1940 }
1941
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001942 // If there is an error, just keep the current indent.
1943 if (indent < 0)
1944 indent = get_indent();
1945
1946 return indent;
1947}
1948#endif
1949
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001950 static int
1951lisp_match(char_u *p)
1952{
1953 char_u buf[LSIZE];
1954 int len;
1955 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1956
1957 while (*word != NUL)
1958 {
1959 (void)copy_option_part(&word, buf, LSIZE, ",");
1960 len = (int)STRLEN(buf);
Bram Moolenaard26c5802022-10-13 12:30:08 +01001961 if (STRNCMP(buf, p, len) == 0 && IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001962 return TRUE;
1963 }
1964 return FALSE;
1965}
1966
1967/*
1968 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
1969 * The incompatible newer method is quite a bit better at indenting
1970 * code in lisp-like languages than the traditional one; it's still
1971 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
1972 *
1973 * TODO:
1974 * Findmatch() should be adapted for lisp, also to make showmatch
1975 * work correctly: now (v5.3) it seems all C/C++ oriented:
1976 * - it does not recognize the #\( and #\) notations as character literals
1977 * - it doesn't know about comments starting with a semicolon
1978 * - it incorrectly interprets '(' as a character literal
1979 * All this messes up get_lisp_indent in some rare cases.
1980 * Update from Sergey Khorev:
1981 * I tried to fix the first two issues.
1982 */
1983 int
1984get_lisp_indent(void)
1985{
1986 pos_T *pos, realpos, paren;
1987 int amount;
1988 char_u *that;
1989 colnr_T col;
1990 colnr_T firsttry;
1991 int parencount, quotecount;
1992 int vi_lisp;
1993
1994 // Set vi_lisp to use the vi-compatible method
1995 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
1996
1997 realpos = curwin->w_cursor;
1998 curwin->w_cursor.col = 0;
1999
2000 if ((pos = findmatch(NULL, '(')) == NULL)
2001 pos = findmatch(NULL, '[');
2002 else
2003 {
2004 paren = *pos;
2005 pos = findmatch(NULL, '[');
2006 if (pos == NULL || LT_POSP(pos, &paren))
2007 pos = &paren;
2008 }
2009 if (pos != NULL)
2010 {
2011 // Extra trick: Take the indent of the first previous non-white
2012 // line that is at the same () level.
2013 amount = -1;
2014 parencount = 0;
2015
2016 while (--curwin->w_cursor.lnum >= pos->lnum)
2017 {
2018 if (linewhite(curwin->w_cursor.lnum))
2019 continue;
2020 for (that = ml_get_curline(); *that != NUL; ++that)
2021 {
2022 if (*that == ';')
2023 {
2024 while (*(that + 1) != NUL)
2025 ++that;
2026 continue;
2027 }
2028 if (*that == '\\')
2029 {
2030 if (*(that + 1) != NUL)
2031 ++that;
2032 continue;
2033 }
2034 if (*that == '"' && *(that + 1) != NUL)
2035 {
2036 while (*++that && *that != '"')
2037 {
2038 // skipping escaped characters in the string
2039 if (*that == '\\')
2040 {
2041 if (*++that == NUL)
2042 break;
2043 if (that[1] == NUL)
2044 {
2045 ++that;
2046 break;
2047 }
2048 }
2049 }
Bram Moolenaar0e8e9382022-06-18 12:51:11 +01002050 if (*that == NUL)
2051 break;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002052 }
2053 if (*that == '(' || *that == '[')
2054 ++parencount;
2055 else if (*that == ')' || *that == ']')
2056 --parencount;
2057 }
2058 if (parencount == 0)
2059 {
2060 amount = get_indent();
2061 break;
2062 }
2063 }
2064
2065 if (amount == -1)
2066 {
2067 curwin->w_cursor.lnum = pos->lnum;
2068 curwin->w_cursor.col = pos->col;
2069 col = pos->col;
2070
2071 that = ml_get_curline();
2072
2073 if (vi_lisp && get_indent() == 0)
2074 amount = 2;
2075 else
2076 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002077 char_u *line = that;
2078 chartabsize_T cts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002079
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002080 init_chartabsize_arg(&cts, curwin, pos->lnum, 0, line, line);
2081 while (*cts.cts_ptr != NUL && col > 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002082 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002083 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002084 col--;
2085 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002086 amount = cts.cts_vcol;
2087 that = cts.cts_ptr;
2088 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002089
2090 // Some keywords require "body" indenting rules (the
2091 // non-standard-lisp ones are Scheme special forms):
2092 //
2093 // (let ((a 1)) instead (let ((a 1))
2094 // (...)) of (...))
2095
2096 if (!vi_lisp && (*that == '(' || *that == '[')
2097 && lisp_match(that + 1))
2098 amount += 2;
2099 else
2100 {
Bram Moolenaar8eba2bd2022-06-22 19:59:28 +01002101 if (*that != NUL)
2102 {
2103 that++;
2104 amount++;
2105 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002106 firsttry = amount;
2107
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002108 init_chartabsize_arg(&cts, curwin, (colnr_T)(that - line),
2109 amount, line, that);
2110 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002111 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002112 cts.cts_vcol += lbr_chartabsize(&cts);
2113 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002114 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002115 that = cts.cts_ptr;
2116 amount = cts.cts_vcol;
2117 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002118
2119 if (*that && *that != ';') // not a comment line
2120 {
2121 // test *that != '(' to accommodate first let/do
2122 // argument if it is more than one line
2123 if (!vi_lisp && *that != '(' && *that != '[')
2124 firsttry++;
2125
2126 parencount = 0;
2127 quotecount = 0;
2128
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002129 init_chartabsize_arg(&cts, curwin,
2130 (colnr_T)(that - line), amount, line, that);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002131 if (vi_lisp
2132 || (*that != '"'
2133 && *that != '\''
2134 && *that != '#'
2135 && (*that < '0' || *that > '9')))
2136 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002137 while (*cts.cts_ptr
2138 && (!VIM_ISWHITE(*cts.cts_ptr)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002139 || quotecount
2140 || parencount)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002141 && (!((*cts.cts_ptr == '('
2142 || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002143 && !quotecount
2144 && !parencount
2145 && vi_lisp)))
2146 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002147 if (*cts.cts_ptr == '"')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002148 quotecount = !quotecount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002149 if ((*cts.cts_ptr == '(' || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002150 && !quotecount)
2151 ++parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002152 if ((*cts.cts_ptr == ')' || *cts.cts_ptr == ']')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002153 && !quotecount)
2154 --parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002155 if (*cts.cts_ptr == '\\'
2156 && *(cts.cts_ptr+1) != NUL)
2157 cts.cts_vcol += lbr_chartabsize_adv(&cts);
2158 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002159 }
2160 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002161 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002162 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002163 cts.cts_vcol += lbr_chartabsize(&cts);
2164 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002165 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002166 that = cts.cts_ptr;
2167 amount = cts.cts_vcol;
2168 clear_chartabsize_arg(&cts);
2169
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002170 if (!*that || *that == ';')
2171 amount = firsttry;
2172 }
2173 }
2174 }
2175 }
2176 }
2177 else
2178 amount = 0; // no matching '(' or '[' found, use zero indent
2179
2180 curwin->w_cursor = realpos;
2181
2182 return amount;
2183}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002184
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002185/*
2186 * Re-indent the current line, based on the current contents of it and the
2187 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
2188 * confused what all the part that handles Control-T is doing that I'm not.
2189 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
2190 */
2191
2192 void
2193fixthisline(int (*get_the_indent)(void))
2194{
2195 int amount = get_the_indent();
2196
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002197 if (amount < 0)
2198 return;
2199
2200 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
2201 if (linewhite(curwin->w_cursor.lnum))
2202 did_ai = TRUE; // delete the indent if the line stays empty
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002203}
2204
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002205/*
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002206 * Return TRUE if 'indentexpr' should be used for Lisp indenting.
2207 * Caller may want to check 'autoindent'.
2208 */
2209 int
2210use_indentexpr_for_lisp(void)
2211{
2212#ifdef FEAT_EVAL
2213 return curbuf->b_p_lisp
2214 && *curbuf->b_p_inde != NUL
2215 && STRCMP(curbuf->b_p_lop, "expr:1") == 0;
2216#else
2217 return FALSE;
2218#endif
2219}
2220
2221/*
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002222 * Fix indent for 'lisp' and 'cindent'.
2223 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002224 void
2225fix_indent(void)
2226{
2227 if (p_paste)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002228 return; // no auto-indenting when 'paste' is set
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002229 if (curbuf->b_p_lisp && curbuf->b_p_ai)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002230 {
2231 if (use_indentexpr_for_lisp())
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002232 do_c_expr_indent();
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002233 else
2234 fixthisline(get_lisp_indent);
2235 }
2236 else if (cindent_on())
2237 do_c_expr_indent();
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002238}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002239
2240#if defined(FEAT_EVAL) || defined(PROTO)
2241/*
2242 * "indent()" function
2243 */
2244 void
2245f_indent(typval_T *argvars, typval_T *rettv)
2246{
2247 linenr_T lnum;
2248
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002249 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2250 return;
2251
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002252 lnum = tv_get_lnum(argvars);
2253 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2254 rettv->vval.v_number = get_indent_lnum(lnum);
2255 else
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002256 {
2257 if (in_vim9script())
2258 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002259 rettv->vval.v_number = -1;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002260 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002261}
2262
2263/*
2264 * "lispindent(lnum)" function
2265 */
2266 void
2267f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2268{
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002269 pos_T pos;
2270 linenr_T lnum;
2271
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002272 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2273 return;
2274
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002275 pos = curwin->w_cursor;
2276 lnum = tv_get_lnum(argvars);
2277 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2278 {
2279 curwin->w_cursor.lnum = lnum;
2280 rettv->vval.v_number = get_lisp_indent();
2281 curwin->w_cursor = pos;
2282 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002283 else if (in_vim9script())
2284 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002285 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002286 rettv->vval.v_number = -1;
2287}
2288#endif