blob: 1858ecf8f3ae2d74e30d1acfbb02333c13e1595f [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,
437 int list) // if TRUE, count only screen size for tabs
438{
439 int count = 0;
440
441 for ( ; *ptr; ++ptr)
442 {
443 if (*ptr == TAB)
444 {
Bram Moolenaareed9d462021-02-15 20:38:25 +0100445 if (!list || curwin->w_lcs_chars.tab1)
446 // count a tab for what it is worth
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200447 count += ts - (count % ts);
448 else
449 // In list mode, when tab is not set, count screen char width
450 // for Tab, displays: ^I
451 count += ptr2cells(ptr);
452 }
453 else if (*ptr == ' ')
454 ++count; // count a space for one
455 else
456 break;
457 }
458 return count;
459}
460
461#ifdef FEAT_VARTABS
462/*
463 * Count the size (in window cells) of the indent in line "ptr", using
464 * variable tabstops.
465 * if "list" is TRUE, count only screen size for tabs.
466 */
467 int
468get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
469{
470 int count = 0;
471
472 for ( ; *ptr; ++ptr)
473 {
474 if (*ptr == TAB) // count a tab for what it is worth
475 {
Bram Moolenaareed9d462021-02-15 20:38:25 +0100476 if (!list || curwin->w_lcs_chars.tab1)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200477 count += tabstop_padding(count, ts, vts);
478 else
479 // In list mode, when tab is not set, count screen char width
480 // for Tab, displays: ^I
481 count += ptr2cells(ptr);
482 }
483 else if (*ptr == ' ')
484 ++count; // count a space for one
485 else
486 break;
487 }
488 return count;
489}
490#endif
491
492/*
493 * Set the indent of the current line.
494 * Leaves the cursor on the first non-blank in the line.
495 * Caller must take care of undo.
496 * "flags":
497 * SIN_CHANGED: call changed_bytes() if the line was changed.
498 * SIN_INSERT: insert the indent in front of the line.
499 * SIN_UNDO: save line for undo before changing it.
500 * Returns TRUE if the line was changed.
501 */
502 int
503set_indent(
504 int size, // measured in spaces
505 int flags)
506{
507 char_u *p;
508 char_u *newline;
509 char_u *oldline;
510 char_u *s;
511 int todo;
512 int ind_len; // measured in characters
513 int line_len;
514 int doit = FALSE;
515 int ind_done = 0; // measured in spaces
516#ifdef FEAT_VARTABS
517 int ind_col = 0;
518#endif
519 int tab_pad;
520 int retval = FALSE;
521 int orig_char_len = -1; // number of initial whitespace chars when
522 // 'et' and 'pi' are both set
523
524 // First check if there is anything to do and compute the number of
525 // characters needed for the indent.
526 todo = size;
527 ind_len = 0;
528 p = oldline = ml_get_curline();
529
530 // Calculate the buffer size for the new indent, and check to see if it
531 // isn't already set
532
533 // if 'expandtab' isn't set: use TABs; if both 'expandtab' and
534 // 'preserveindent' are set count the number of characters at the
535 // beginning of the line to be copied
536 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
537 {
538 // If 'preserveindent' is set then reuse as much as possible of
539 // the existing indent structure for the new indent
540 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
541 {
542 ind_done = 0;
543
544 // count as many characters as we can use
545 while (todo > 0 && VIM_ISWHITE(*p))
546 {
547 if (*p == TAB)
548 {
549#ifdef FEAT_VARTABS
550 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
551 curbuf->b_p_vts_array);
552#else
553 tab_pad = (int)curbuf->b_p_ts
554 - (ind_done % (int)curbuf->b_p_ts);
555#endif
556 // stop if this tab will overshoot the target
557 if (todo < tab_pad)
558 break;
559 todo -= tab_pad;
560 ++ind_len;
561 ind_done += tab_pad;
562 }
563 else
564 {
565 --todo;
566 ++ind_len;
567 ++ind_done;
568 }
569 ++p;
570 }
571
572#ifdef FEAT_VARTABS
573 // These diverge from this point.
574 ind_col = ind_done;
575#endif
576 // Set initial number of whitespace chars to copy if we are
577 // preserving indent but expandtab is set
578 if (curbuf->b_p_et)
579 orig_char_len = ind_len;
580
581 // Fill to next tabstop with a tab, if possible
582#ifdef FEAT_VARTABS
583 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
584 curbuf->b_p_vts_array);
585#else
586 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
587#endif
588 if (todo >= tab_pad && orig_char_len == -1)
589 {
590 doit = TRUE;
591 todo -= tab_pad;
592 ++ind_len;
593 // ind_done += tab_pad;
594#ifdef FEAT_VARTABS
595 ind_col += tab_pad;
596#endif
597 }
598 }
599
600 // count tabs required for indent
601#ifdef FEAT_VARTABS
602 for (;;)
603 {
604 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
605 curbuf->b_p_vts_array);
606 if (todo < tab_pad)
607 break;
608 if (*p != TAB)
609 doit = TRUE;
610 else
611 ++p;
612 todo -= tab_pad;
613 ++ind_len;
614 ind_col += tab_pad;
615 }
616#else
617 while (todo >= (int)curbuf->b_p_ts)
618 {
619 if (*p != TAB)
620 doit = TRUE;
621 else
622 ++p;
623 todo -= (int)curbuf->b_p_ts;
624 ++ind_len;
625 // ind_done += (int)curbuf->b_p_ts;
626 }
627#endif
628 }
629 // count spaces required for indent
630 while (todo > 0)
631 {
632 if (*p != ' ')
633 doit = TRUE;
634 else
635 ++p;
636 --todo;
637 ++ind_len;
638 // ++ind_done;
639 }
640
641 // Return if the indent is OK already.
642 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
643 return FALSE;
644
645 // Allocate memory for the new line.
646 if (flags & SIN_INSERT)
647 p = oldline;
648 else
649 p = skipwhite(p);
650 line_len = (int)STRLEN(p) + 1;
651
652 // If 'preserveindent' and 'expandtab' are both set keep the original
653 // characters and allocate accordingly. We will fill the rest with spaces
654 // after the if (!curbuf->b_p_et) below.
655 if (orig_char_len != -1)
656 {
657 newline = alloc(orig_char_len + size - ind_done + line_len);
658 if (newline == NULL)
659 return FALSE;
660 todo = size - ind_done;
661 ind_len = orig_char_len + todo; // Set total length of indent in
662 // characters, which may have been
663 // undercounted until now
664 p = oldline;
665 s = newline;
666 while (orig_char_len > 0)
667 {
668 *s++ = *p++;
669 orig_char_len--;
670 }
671
672 // Skip over any additional white space (useful when newindent is less
673 // than old)
674 while (VIM_ISWHITE(*p))
675 ++p;
676
677 }
678 else
679 {
680 todo = size;
681 newline = alloc(ind_len + line_len);
682 if (newline == NULL)
683 return FALSE;
684 s = newline;
685 }
686
687 // Put the characters in the new line.
688 // if 'expandtab' isn't set: use TABs
689 if (!curbuf->b_p_et)
690 {
691 // If 'preserveindent' is set then reuse as much as possible of
692 // the existing indent structure for the new indent
693 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
694 {
695 p = oldline;
696 ind_done = 0;
697
698 while (todo > 0 && VIM_ISWHITE(*p))
699 {
700 if (*p == TAB)
701 {
702#ifdef FEAT_VARTABS
703 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
704 curbuf->b_p_vts_array);
705#else
706 tab_pad = (int)curbuf->b_p_ts
707 - (ind_done % (int)curbuf->b_p_ts);
708#endif
709 // stop if this tab will overshoot the target
710 if (todo < tab_pad)
711 break;
712 todo -= tab_pad;
713 ind_done += tab_pad;
714 }
715 else
716 {
717 --todo;
718 ++ind_done;
719 }
720 *s++ = *p++;
721 }
722
723 // Fill to next tabstop with a tab, if possible
724#ifdef FEAT_VARTABS
725 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
726 curbuf->b_p_vts_array);
727#else
728 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
729#endif
730 if (todo >= tab_pad)
731 {
732 *s++ = TAB;
733 todo -= tab_pad;
734#ifdef FEAT_VARTABS
735 ind_done += tab_pad;
736#endif
737 }
738
739 p = skipwhite(p);
740 }
741
742#ifdef FEAT_VARTABS
743 for (;;)
744 {
745 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
746 curbuf->b_p_vts_array);
747 if (todo < tab_pad)
748 break;
749 *s++ = TAB;
750 todo -= tab_pad;
751 ind_done += tab_pad;
752 }
753#else
754 while (todo >= (int)curbuf->b_p_ts)
755 {
756 *s++ = TAB;
757 todo -= (int)curbuf->b_p_ts;
758 }
759#endif
760 }
761 while (todo > 0)
762 {
763 *s++ = ' ';
764 --todo;
765 }
766 mch_memmove(s, p, (size_t)line_len);
767
768 // Replace the line (unless undo fails).
769 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
770 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200771 colnr_T old_offset = (colnr_T)(p - oldline);
772 colnr_T new_offset = (colnr_T)(s - newline);
773
774 // this may free "newline"
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200775 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
776 if (flags & SIN_CHANGED)
777 changed_bytes(curwin->w_cursor.lnum, 0);
778
779 // Correct saved cursor position if it is in this line.
780 if (saved_cursor.lnum == curwin->w_cursor.lnum)
781 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200782 if (saved_cursor.col >= old_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200783 // cursor was after the indent, adjust for the number of
784 // bytes added/removed
Bram Moolenaarcf306432020-06-29 20:40:37 +0200785 saved_cursor.col += ind_len - old_offset;
786 else if (saved_cursor.col >= new_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200787 // cursor was in the indent, and is now after it, put it back
788 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaarcf306432020-06-29 20:40:37 +0200789 saved_cursor.col = new_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200790 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100791#ifdef FEAT_PROP_POPUP
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200792 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200793 int added = ind_len - old_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200794
795 // When increasing indent this behaves like spaces were inserted at
796 // the old indent, when decreasing indent it behaves like spaces
797 // were deleted at the new indent.
798 adjust_prop_columns(curwin->w_cursor.lnum,
Bram Moolenaar8a77d202022-08-15 16:29:37 +0100799 added > 0 ? old_offset : (colnr_T)ind_len,
800 added, APC_INDENT);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200801 }
802#endif
803 retval = TRUE;
804 }
805 else
806 vim_free(newline);
807
808 curwin->w_cursor.col = ind_len;
809 return retval;
810}
811
812/*
813 * Return the indent of the current line after a number. Return -1 if no
814 * number was found. Used for 'n' in 'formatoptions': numbered list.
815 * Since a pattern is used it can actually handle more than numbers.
816 */
817 int
818get_number_indent(linenr_T lnum)
819{
820 colnr_T col;
821 pos_T pos;
822
823 regmatch_T regmatch;
824 int lead_len = 0; // length of comment leader
825
826 if (lnum > curbuf->b_ml.ml_line_count)
827 return -1;
828 pos.lnum = 0;
829
830 // In format_lines() (i.e. not insert mode), fo+=q is needed too...
Bram Moolenaar24959102022-05-07 20:01:16 +0100831 if ((State & MODE_INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200832 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
833
834 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
835 if (regmatch.regprog != NULL)
836 {
837 regmatch.rm_ic = FALSE;
838
839 // vim_regexec() expects a pointer to a line. This lets us
840 // start matching for the flp beyond any comment leader...
841 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
842 {
843 pos.lnum = lnum;
844 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
845 pos.coladd = 0;
846 }
847 vim_regfree(regmatch.regprog);
848 }
849
850 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
851 return -1;
852 getvcol(curwin, &pos, &col, NULL, NULL);
853 return (int)col;
854}
855
856#if defined(FEAT_LINEBREAK) || defined(PROTO)
857/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100858 * This is called when 'breakindentopt' is changed and when a window is
859 * initialized.
860 */
861 int
862briopt_check(win_T *wp)
863{
864 char_u *p;
865 int bri_shift = 0;
866 long bri_min = 20;
867 int bri_sbr = FALSE;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200868 int bri_list = 0;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100869 int bri_vcol = 0;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100870
871 p = wp->w_p_briopt;
872 while (*p != NUL)
873 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200874 // Note: Keep this in sync with p_briopt_values
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100875 if (STRNCMP(p, "shift:", 6) == 0
876 && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
877 {
878 p += 6;
879 bri_shift = getdigits(&p);
880 }
881 else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4]))
882 {
883 p += 4;
884 bri_min = getdigits(&p);
885 }
886 else if (STRNCMP(p, "sbr", 3) == 0)
887 {
888 p += 3;
889 bri_sbr = TRUE;
890 }
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200891 else if (STRNCMP(p, "list:", 5) == 0)
892 {
893 p += 5;
894 bri_list = getdigits(&p);
895 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100896 else if (STRNCMP(p, "column:", 7) == 0)
897 {
898 p += 7;
899 bri_vcol = getdigits(&p);
900 }
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100901 if (*p != ',' && *p != NUL)
902 return FAIL;
903 if (*p == ',')
904 ++p;
905 }
906
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100907 wp->w_briopt_shift = bri_shift;
908 wp->w_briopt_min = bri_min;
909 wp->w_briopt_sbr = bri_sbr;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200910 wp->w_briopt_list = bri_list;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100911 wp->w_briopt_vcol = bri_vcol;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100912
913 return OK;
914}
915
916/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200917 * Return appropriate space number for breakindent, taking influencing
918 * parameters into account. Window must be specified, since it is not
919 * necessarily always the current one.
920 */
921 int
922get_breakindent_win(
923 win_T *wp,
924 char_u *line) // start of the line
925{
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000926 static int prev_indent = 0; // cached indent value
927 static long prev_ts = 0L; // cached tabstop value
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100928 static int prev_fnum = 0; // cached buffer number
929 static char_u *prev_line = NULL; // cached copy of "line"
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200930 static varnumber_T prev_tick = 0; // changedtick of cached value
931# ifdef FEAT_VARTABS
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000932 static int *prev_vts = NULL; // cached vartabs values
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200933# endif
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000934 static int prev_list = 0; // cached list value
935 static int prev_listopt = 0; // cached w_p_briopt_list value
Christian Brabandtc53b4672022-01-15 10:01:05 +0000936 // cached formatlistpat value
937 static char_u *prev_flp = NULL;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200938 int bri = 0;
939 // window width minus window margin space, i.e. what rests for text
940 const int eff_wwidth = wp->w_width
941 - ((wp->w_p_nu || wp->w_p_rnu)
942 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
943 ? number_width(wp) + 1 : 0);
944
Christian Brabandtc53b4672022-01-15 10:01:05 +0000945 // used cached indent, unless
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100946 // - buffer changed
Christian Brabandtc53b4672022-01-15 10:01:05 +0000947 // - 'tabstop' changed
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100948 // - buffer was changed
Christian Brabandtc53b4672022-01-15 10:01:05 +0000949 // - 'briopt_list changed' changed or
950 // - 'formatlistpattern' changed
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100951 // - line changed
952 // - 'vartabs' changed
953 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 || prev_tick != CHANGEDTICK(wp->w_buffer)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000956 || prev_listopt != wp->w_briopt_list
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100957 || prev_flp == NULL
958 || STRCMP(prev_flp, get_flp_value(wp->w_buffer)) != 0
959 || prev_line == NULL || STRCMP(prev_line, line) != 0
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200960# ifdef FEAT_VARTABS
961 || prev_vts != wp->w_buffer->b_p_vts_array
962# endif
963 )
964 {
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100965 prev_fnum = wp->w_buffer->b_fnum;
966 vim_free(prev_line);
967 prev_line = vim_strsave(line);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200968 prev_ts = wp->w_buffer->b_p_ts;
969 prev_tick = CHANGEDTICK(wp->w_buffer);
970# ifdef FEAT_VARTABS
971 prev_vts = wp->w_buffer->b_p_vts_array;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100972 if (wp->w_briopt_vcol == 0)
973 prev_indent = get_indent_str_vtab(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200974 (int)wp->w_buffer->b_p_ts,
975 wp->w_buffer->b_p_vts_array, wp->w_p_list);
976# else
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100977 if (wp->w_briopt_vcol == 0)
978 prev_indent = get_indent_str(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200979 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
980# endif
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000981 prev_listopt = wp->w_briopt_list;
Christian Brabandtc53b4672022-01-15 10:01:05 +0000982 prev_list = 0;
983 vim_free(prev_flp);
984 prev_flp = vim_strsave(get_flp_value(wp->w_buffer));
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000985 // add additional indent for numbered lists
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100986 if (wp->w_briopt_list != 0 && wp->w_briopt_vcol == 0)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000987 {
988 regmatch_T regmatch;
989
Christian Brabandtc53b4672022-01-15 10:01:05 +0000990 regmatch.regprog = vim_regcomp(prev_flp,
991 RE_MAGIC + RE_STRING + RE_AUTO + RE_STRICT);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000992
993 if (regmatch.regprog != NULL)
994 {
995 regmatch.rm_ic = FALSE;
996 if (vim_regexec(&regmatch, line, 0))
997 {
998 if (wp->w_briopt_list > 0)
999 prev_list = wp->w_briopt_list;
1000 else
Maxim Kim11916722022-09-02 14:08:53 +01001001 prev_indent = (*regmatch.endp - *regmatch.startp);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001002 }
1003 vim_regfree(regmatch.regprog);
1004 }
1005 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001006 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001007 if (wp->w_briopt_vcol != 0)
1008 {
1009 // column value has priority
1010 bri = wp->w_briopt_vcol;
1011 prev_list = 0;
1012 }
1013 else
1014 bri = prev_indent + wp->w_briopt_shift;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001015
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001016 // Add offset for number column, if 'n' is in 'cpoptions'
1017 bri += win_col_off2(wp);
1018
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001019 // add additional indent for numbered lists
Maxim Kim11916722022-09-02 14:08:53 +01001020 if (wp->w_briopt_list > 0)
1021 bri += prev_list;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001022
Maxim Kimf674b352021-07-22 11:46:59 +02001023 // indent minus the length of the showbreak string
1024 if (wp->w_briopt_sbr)
1025 bri -= vim_strsize(get_showbreak_value(wp));
1026
1027
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001028 // never indent past left window margin
1029 if (bri < 0)
1030 bri = 0;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001031
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001032 // always leave at least bri_min characters on the left,
1033 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001034 else if (bri > eff_wwidth - wp->w_briopt_min)
1035 bri = (eff_wwidth - wp->w_briopt_min < 0)
1036 ? 0 : eff_wwidth - wp->w_briopt_min;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001037
1038 return bri;
1039}
1040#endif
1041
1042/*
1043 * When extra == 0: Return TRUE if the cursor is before or on the first
1044 * non-blank in the line.
1045 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1046 * the line.
1047 */
1048 int
1049inindent(int extra)
1050{
1051 char_u *ptr;
1052 colnr_T col;
1053
1054 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
1055 ++ptr;
1056 if (col >= curwin->w_cursor.col + extra)
1057 return TRUE;
1058 else
1059 return FALSE;
1060}
1061
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001062/*
1063 * op_reindent - handle reindenting a block of lines.
1064 */
1065 void
1066op_reindent(oparg_T *oap, int (*how)(void))
1067{
Bram Moolenaar4c84dd32022-04-20 10:22:54 +01001068 long i = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001069 char_u *l;
1070 int amount;
1071 linenr_T first_changed = 0;
1072 linenr_T last_changed = 0;
1073 linenr_T start_lnum = curwin->w_cursor.lnum;
1074
1075 // Don't even try when 'modifiable' is off.
1076 if (!curbuf->b_p_ma)
1077 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001078 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001079 return;
1080 }
1081
Bram Moolenaare4686982022-04-19 18:28:45 +01001082 // Save for undo. Do this once for all lines, much faster than doing this
1083 // for each line separately, especially when undoing.
1084 if (u_savecommon(start_lnum - 1, start_lnum + oap->line_count,
1085 start_lnum + oap->line_count, FALSE) == OK)
1086 for (i = oap->line_count; --i >= 0 && !got_int; )
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001087 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001088 // it's a slow thing to do, so give feedback so there's no worry
1089 // that the computer's just hung.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001090
Bram Moolenaare4686982022-04-19 18:28:45 +01001091 if (i > 1
1092 && (i % 50 == 0 || i == oap->line_count - 1)
1093 && oap->line_count > p_report)
1094 smsg(_("%ld lines to indent... "), i);
1095
1096 // Be vi-compatible: For lisp indenting the first line is not
1097 // indented, unless there is only one line.
Bram Moolenaare4686982022-04-19 18:28:45 +01001098 if (i != oap->line_count - 1 || oap->line_count == 1
1099 || how != get_lisp_indent)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001100 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001101 l = skipwhite(ml_get_curline());
1102 if (*l == NUL) // empty or blank line
1103 amount = 0;
1104 else
1105 amount = how(); // get the indent for this line
1106
1107 if (amount >= 0 && set_indent(amount, 0))
1108 {
1109 // did change the indent, call changed_lines() later
1110 if (first_changed == 0)
1111 first_changed = curwin->w_cursor.lnum;
1112 last_changed = curwin->w_cursor.lnum;
1113 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001114 }
Bram Moolenaare4686982022-04-19 18:28:45 +01001115 ++curwin->w_cursor.lnum;
1116 curwin->w_cursor.col = 0; // make sure it's valid
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001117 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001118
1119 // put cursor on first non-blank of indented line
1120 curwin->w_cursor.lnum = start_lnum;
1121 beginline(BL_SOL | BL_FIX);
1122
1123 // Mark changed lines so that they will be redrawn. When Visual
1124 // highlighting was present, need to continue until the last line. When
1125 // there is no change still need to remove the Visual highlighting.
1126 if (last_changed != 0)
1127 changed_lines(first_changed, 0,
1128 oap->is_VIsual ? start_lnum + oap->line_count :
1129 last_changed + 1, 0L);
1130 else if (oap->is_VIsual)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001131 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001132
1133 if (oap->line_count > p_report)
1134 {
1135 i = oap->line_count - (i + 1);
1136 smsg(NGETTEXT("%ld line indented ",
1137 "%ld lines indented ", i), i);
1138 }
Bram Moolenaare1004402020-10-24 20:49:43 +02001139 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001140 {
1141 // set '[ and '] marks
1142 curbuf->b_op_start = oap->start;
1143 curbuf->b_op_end = oap->end;
1144 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001145}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001146
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001147/*
1148 * Return TRUE if lines starting with '#' should be left aligned.
1149 */
1150 int
1151preprocs_left(void)
1152{
1153 return
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001154 (curbuf->b_p_si && !curbuf->b_p_cin) ||
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001155 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1156 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001157 ;
1158}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001159
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001160/*
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001161 * Return TRUE if the conditions are OK for smart indenting.
1162 */
1163 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001164may_do_si(void)
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001165{
1166 return curbuf->b_p_si
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001167 && !curbuf->b_p_cin
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001168# ifdef FEAT_EVAL
1169 && *curbuf->b_p_inde == NUL
1170# endif
1171 && !p_paste;
1172}
1173
1174/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001175 * Try to do some very smart auto-indenting.
1176 * Used when inserting a "normal" character.
1177 */
1178 void
1179ins_try_si(int c)
1180{
1181 pos_T *pos, old_pos;
1182 char_u *ptr;
1183 int i;
1184 int temp;
1185
1186 // do some very smart indenting when entering '{' or '}'
Bram Moolenaar2e444bb2022-05-14 12:54:23 +01001187 if (((did_si || can_si_back) && c == '{')
1188 || (can_si && c == '}' && inindent(0)))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001189 {
1190 // for '}' set indent equal to indent of line containing matching '{'
1191 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1192 {
1193 old_pos = curwin->w_cursor;
1194 // If the matching '{' has a ')' immediately before it (ignoring
1195 // white-space), then line up with the start of the line
1196 // containing the matching '(' if there is one. This handles the
1197 // case where an "if (..\n..) {" statement continues over multiple
1198 // lines -- webb
1199 ptr = ml_get(pos->lnum);
1200 i = pos->col;
1201 if (i > 0) // skip blanks before '{'
1202 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1203 ;
1204 curwin->w_cursor.lnum = pos->lnum;
1205 curwin->w_cursor.col = i;
1206 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1207 curwin->w_cursor = *pos;
1208 i = get_indent();
1209 curwin->w_cursor = old_pos;
1210 if (State & VREPLACE_FLAG)
1211 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1212 else
1213 (void)set_indent(i, SIN_CHANGED);
1214 }
1215 else if (curwin->w_cursor.col > 0)
1216 {
1217 // when inserting '{' after "O" reduce indent, but not
1218 // more than indent of previous line
1219 temp = TRUE;
1220 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1221 {
1222 old_pos = curwin->w_cursor;
1223 i = get_indent();
1224 while (curwin->w_cursor.lnum > 1)
1225 {
1226 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1227
1228 // ignore empty lines and lines starting with '#'.
1229 if (*ptr != '#' && *ptr != NUL)
1230 break;
1231 }
1232 if (get_indent() >= i)
1233 temp = FALSE;
1234 curwin->w_cursor = old_pos;
1235 }
1236 if (temp)
1237 shift_line(TRUE, FALSE, 1, TRUE);
1238 }
1239 }
1240
1241 // set indent of '#' always to 0
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001242 if (curwin->w_cursor.col > 0 && can_si && c == '#' && inindent(0))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001243 {
1244 // remember current indent for next line
1245 old_indent = get_indent();
1246 (void)set_indent(0, SIN_CHANGED);
1247 }
1248
1249 // Adjust ai_col, the char at this position can be deleted.
1250 if (ai_col > curwin->w_cursor.col)
1251 ai_col = curwin->w_cursor.col;
1252}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001253
1254/*
1255 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1256 * Keep the cursor on the same character.
1257 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1258 * type == INDENT_DEC decrease indent (for CTRL-D)
1259 * type == INDENT_SET set indent to "amount"
1260 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1261 */
1262 void
1263change_indent(
1264 int type,
1265 int amount,
1266 int round,
1267 int replaced, // replaced character, put on replace stack
1268 int call_changed_bytes) // call changed_bytes()
1269{
1270 int vcol;
1271 int last_vcol;
1272 int insstart_less; // reduction for Insstart.col
1273 int new_cursor_col;
1274 int i;
1275 char_u *ptr;
1276 int save_p_list;
1277 int start_col;
1278 colnr_T vc;
1279 colnr_T orig_col = 0; // init for GCC
1280 char_u *new_line, *orig_line = NULL; // init for GCC
1281
Bram Moolenaar24959102022-05-07 20:01:16 +01001282 // MODE_VREPLACE state needs to know what the line was like before changing
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001283 if (State & VREPLACE_FLAG)
1284 {
1285 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1286 orig_col = curwin->w_cursor.col;
1287 }
1288
1289 // for the following tricks we don't want list mode
1290 save_p_list = curwin->w_p_list;
1291 curwin->w_p_list = FALSE;
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001292#ifdef FEAT_PROP_POPUP
1293 ignore_text_props = TRUE;
1294#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001295 vc = getvcol_nolist(&curwin->w_cursor);
1296 vcol = vc;
1297
1298 // For Replace mode we need to fix the replace stack later, which is only
1299 // possible when the cursor is in the indent. Remember the number of
1300 // characters before the cursor if it's possible.
1301 start_col = curwin->w_cursor.col;
1302
1303 // determine offset from first non-blank
1304 new_cursor_col = curwin->w_cursor.col;
1305 beginline(BL_WHITE);
1306 new_cursor_col -= curwin->w_cursor.col;
1307
1308 insstart_less = curwin->w_cursor.col;
1309
1310 // If the cursor is in the indent, compute how many screen columns the
1311 // cursor is to the left of the first non-blank.
1312 if (new_cursor_col < 0)
1313 vcol = get_indent() - vcol;
1314
1315 if (new_cursor_col > 0) // can't fix replace stack
1316 start_col = -1;
1317
1318 // Set the new indent. The cursor will be put on the first non-blank.
1319 if (type == INDENT_SET)
1320 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1321 else
1322 {
1323 int save_State = State;
1324
1325 // Avoid being called recursively.
1326 if (State & VREPLACE_FLAG)
Bram Moolenaar24959102022-05-07 20:01:16 +01001327 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001328 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1329 State = save_State;
1330 }
1331 insstart_less -= curwin->w_cursor.col;
1332
1333 // Try to put cursor on same character.
1334 // If the cursor is at or after the first non-blank in the line,
1335 // compute the cursor column relative to the column of the first
1336 // non-blank character.
1337 // If we are not in insert mode, leave the cursor on the first non-blank.
1338 // If the cursor is before the first non-blank, position it relative
1339 // to the first non-blank, counted in screen columns.
1340 if (new_cursor_col >= 0)
1341 {
1342 // When changing the indent while the cursor is touching it, reset
1343 // Insstart_col to 0.
1344 if (new_cursor_col == 0)
1345 insstart_less = MAXCOL;
1346 new_cursor_col += curwin->w_cursor.col;
1347 }
Bram Moolenaar24959102022-05-07 20:01:16 +01001348 else if (!(State & MODE_INSERT))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001349 new_cursor_col = curwin->w_cursor.col;
1350 else
1351 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001352 chartabsize_T cts;
1353
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001354 // Compute the screen column where the cursor should be.
1355 vcol = get_indent() - vcol;
1356 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1357
1358 // Advance the cursor until we reach the right screen column.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001359 last_vcol = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001360 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001361 init_chartabsize_arg(&cts, curwin, 0, 0, ptr, ptr);
1362 while (cts.cts_vcol <= (int)curwin->w_virtcol)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001363 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001364 last_vcol = cts.cts_vcol;
1365 if (cts.cts_vcol > 0)
1366 MB_PTR_ADV(cts.cts_ptr);
1367 if (*cts.cts_ptr == NUL)
Bram Moolenaar4e889f92022-02-21 19:36:12 +00001368 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001369 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001370 }
1371 vcol = last_vcol;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001372 new_cursor_col = cts.cts_ptr - cts.cts_line;
1373 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001374
1375 // May need to insert spaces to be able to position the cursor on
1376 // the right screen column.
1377 if (vcol != (int)curwin->w_virtcol)
1378 {
1379 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1380 i = (int)curwin->w_virtcol - vcol;
1381 ptr = alloc(i + 1);
1382 if (ptr != NULL)
1383 {
1384 new_cursor_col += i;
1385 ptr[i] = NUL;
1386 while (--i >= 0)
1387 ptr[i] = ' ';
1388 ins_str(ptr);
1389 vim_free(ptr);
1390 }
1391 }
1392
1393 // When changing the indent while the cursor is in it, reset
1394 // Insstart_col to 0.
1395 insstart_less = MAXCOL;
1396 }
1397
1398 curwin->w_p_list = save_p_list;
1399
1400 if (new_cursor_col <= 0)
1401 curwin->w_cursor.col = 0;
1402 else
1403 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1404 curwin->w_set_curswant = TRUE;
1405 changed_cline_bef_curs();
1406
1407 // May have to adjust the start of the insert.
Bram Moolenaar24959102022-05-07 20:01:16 +01001408 if (State & MODE_INSERT)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001409 {
1410 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1411 {
1412 if ((int)Insstart.col <= insstart_less)
1413 Insstart.col = 0;
1414 else
1415 Insstart.col -= insstart_less;
1416 }
1417 if ((int)ai_col <= insstart_less)
1418 ai_col = 0;
1419 else
1420 ai_col -= insstart_less;
1421 }
1422
Bram Moolenaar24959102022-05-07 20:01:16 +01001423 // For MODE_REPLACE state, may have to fix the replace stack, if it's
1424 // possible. If the number of characters before the cursor decreased, need
1425 // to pop a few characters from the replace stack.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001426 // If the number of characters before the cursor increased, need to push a
1427 // few NULs onto the replace stack.
1428 if (REPLACE_NORMAL(State) && start_col >= 0)
1429 {
1430 while (start_col > (int)curwin->w_cursor.col)
1431 {
1432 replace_join(0); // remove a NUL from the replace stack
1433 --start_col;
1434 }
1435 while (start_col < (int)curwin->w_cursor.col || replaced)
1436 {
1437 replace_push(NUL);
1438 if (replaced)
1439 {
1440 replace_push(replaced);
1441 replaced = NUL;
1442 }
1443 ++start_col;
1444 }
1445 }
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01001446#ifdef FEAT_PROP_POPUP
1447 ignore_text_props = FALSE;
1448#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001449
Bram Moolenaar24959102022-05-07 20:01:16 +01001450 // For MODE_VREPLACE state, we also have to fix the replace stack. In this
1451 // case it is always possible because we backspace over the whole line and
1452 // then put it back again the way we wanted it.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001453 if (State & VREPLACE_FLAG)
1454 {
1455 // If orig_line didn't allocate, just return. At least we did the job,
1456 // even if you can't backspace.
1457 if (orig_line == NULL)
1458 return;
1459
1460 // Save new line
1461 new_line = vim_strsave(ml_get_curline());
1462 if (new_line == NULL)
1463 return;
1464
1465 // We only put back the new line up to the cursor
1466 new_line[curwin->w_cursor.col] = NUL;
1467
1468 // Put back original line
1469 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1470 curwin->w_cursor.col = orig_col;
1471
1472 // Backspace from cursor to start of line
1473 backspace_until_column(0);
1474
1475 // Insert new stuff into line again
1476 ins_bytes(new_line);
1477
1478 vim_free(new_line);
1479 }
1480}
1481
1482/*
1483 * Copy the indent from ptr to the current line (and fill to size)
1484 * Leaves the cursor on the first non-blank in the line.
1485 * Returns TRUE if the line was changed.
1486 */
1487 int
1488copy_indent(int size, char_u *src)
1489{
1490 char_u *p = NULL;
1491 char_u *line = NULL;
1492 char_u *s;
1493 int todo;
1494 int ind_len;
1495 int line_len = 0;
1496 int tab_pad;
1497 int ind_done;
1498 int round;
1499#ifdef FEAT_VARTABS
1500 int ind_col;
1501#endif
1502
1503 // Round 1: compute the number of characters needed for the indent
1504 // Round 2: copy the characters.
1505 for (round = 1; round <= 2; ++round)
1506 {
1507 todo = size;
1508 ind_len = 0;
1509 ind_done = 0;
1510#ifdef FEAT_VARTABS
1511 ind_col = 0;
1512#endif
1513 s = src;
1514
1515 // Count/copy the usable portion of the source line
1516 while (todo > 0 && VIM_ISWHITE(*s))
1517 {
1518 if (*s == TAB)
1519 {
1520#ifdef FEAT_VARTABS
1521 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1522 curbuf->b_p_vts_array);
1523#else
1524 tab_pad = (int)curbuf->b_p_ts
1525 - (ind_done % (int)curbuf->b_p_ts);
1526#endif
1527 // Stop if this tab will overshoot the target
1528 if (todo < tab_pad)
1529 break;
1530 todo -= tab_pad;
1531 ind_done += tab_pad;
1532#ifdef FEAT_VARTABS
1533 ind_col += tab_pad;
1534#endif
1535 }
1536 else
1537 {
1538 --todo;
1539 ++ind_done;
1540#ifdef FEAT_VARTABS
1541 ++ind_col;
1542#endif
1543 }
1544 ++ind_len;
1545 if (p != NULL)
1546 *p++ = *s;
1547 ++s;
1548 }
1549
1550 // Fill to next tabstop with a tab, if possible
1551#ifdef FEAT_VARTABS
1552 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1553 curbuf->b_p_vts_array);
1554#else
1555 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1556#endif
1557 if (todo >= tab_pad && !curbuf->b_p_et)
1558 {
1559 todo -= tab_pad;
1560 ++ind_len;
1561#ifdef FEAT_VARTABS
1562 ind_col += tab_pad;
1563#endif
1564 if (p != NULL)
1565 *p++ = TAB;
1566 }
1567
1568 // Add tabs required for indent
1569 if (!curbuf->b_p_et)
1570 {
1571#ifdef FEAT_VARTABS
1572 for (;;)
1573 {
1574 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1575 curbuf->b_p_vts_array);
1576 if (todo < tab_pad)
1577 break;
1578 todo -= tab_pad;
1579 ++ind_len;
1580 ind_col += tab_pad;
1581 if (p != NULL)
1582 *p++ = TAB;
1583 }
1584#else
1585 while (todo >= (int)curbuf->b_p_ts)
1586 {
1587 todo -= (int)curbuf->b_p_ts;
1588 ++ind_len;
1589 if (p != NULL)
1590 *p++ = TAB;
1591 }
1592#endif
1593 }
1594
1595 // Count/add spaces required for indent
1596 while (todo > 0)
1597 {
1598 --todo;
1599 ++ind_len;
1600 if (p != NULL)
1601 *p++ = ' ';
1602 }
1603
1604 if (p == NULL)
1605 {
1606 // Allocate memory for the result: the copied indent, new indent
1607 // and the rest of the line.
1608 line_len = (int)STRLEN(ml_get_curline()) + 1;
1609 line = alloc(ind_len + line_len);
1610 if (line == NULL)
1611 return FALSE;
1612 p = line;
1613 }
1614 }
1615
1616 // Append the original line
1617 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1618
1619 // Replace the line
1620 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1621
1622 // Put the cursor after the indent.
1623 curwin->w_cursor.col = ind_len;
1624 return TRUE;
1625}
1626
1627/*
Bram Moolenaar308660b2022-06-16 12:10:48 +01001628 * Give a "resulting text too long" error and maybe set got_int.
1629 */
1630 static void
1631emsg_text_too_long(void)
1632{
1633 emsg(_(e_resulting_text_too_long));
1634#ifdef FEAT_EVAL
1635 // when not inside a try/catch set got_int to break out of any loop
1636 if (trylevel == 0)
1637#endif
1638 got_int = TRUE;
1639}
1640
1641/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001642 * ":retab".
1643 */
1644 void
1645ex_retab(exarg_T *eap)
1646{
1647 linenr_T lnum;
1648 int got_tab = FALSE;
1649 long num_spaces = 0;
1650 long num_tabs;
1651 long len;
1652 long col;
1653 long vcol;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001654 long start_col = 0; // For start of white-space string
1655 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001656 long old_len;
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001657 long new_len;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001658 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001659 char_u *new_line = (char_u *)1; // init to non-NULL
1660 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001661#ifdef FEAT_VARTABS
1662 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001663 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001664#else
1665 int temp;
1666 int new_ts;
1667#endif
1668 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001669 linenr_T first_line = 0; // first changed line
1670 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001671
1672 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001673 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001674
1675#ifdef FEAT_VARTABS
1676 new_ts_str = eap->arg;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02001677 if (tabstop_set(eap->arg, &new_vts_array) == FAIL)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001678 return;
1679 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1680 ++(eap->arg);
1681
1682 // This ensures that either new_vts_array and new_ts_str are freshly
1683 // allocated, or new_vts_array points to an existing array and new_ts_str
1684 // is null.
1685 if (new_vts_array == NULL)
1686 {
1687 new_vts_array = curbuf->b_p_vts_array;
1688 new_ts_str = NULL;
1689 }
1690 else
1691 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1692#else
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001693 ptr = eap->arg;
1694 new_ts = getdigits(&ptr);
1695 if (new_ts < 0 && *eap->arg == '-')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001696 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001697 emsg(_(e_argument_must_be_positive));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001698 return;
1699 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00001700 if (new_ts < 0 || new_ts > TABSTOP_MAX)
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001701 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001702 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001703 return;
1704 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001705 if (new_ts == 0)
1706 new_ts = curbuf->b_p_ts;
1707#endif
1708 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1709 {
1710 ptr = ml_get(lnum);
1711 col = 0;
1712 vcol = 0;
1713 did_undo = FALSE;
1714 for (;;)
1715 {
1716 if (VIM_ISWHITE(ptr[col]))
1717 {
1718 if (!got_tab && num_spaces == 0)
1719 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001720 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001721 start_vcol = vcol;
1722 start_col = col;
1723 }
1724 if (ptr[col] == ' ')
1725 num_spaces++;
1726 else
1727 got_tab = TRUE;
1728 }
1729 else
1730 {
1731 if (got_tab || (eap->forceit && num_spaces > 1))
1732 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001733 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001734
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001735 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001736 len = num_spaces = vcol - start_vcol;
1737 num_tabs = 0;
1738 if (!curbuf->b_p_et)
1739 {
1740#ifdef FEAT_VARTABS
1741 int t, s;
1742
1743 tabstop_fromto(start_vcol, vcol,
1744 curbuf->b_p_ts, new_vts_array, &t, &s);
1745 num_tabs = t;
1746 num_spaces = s;
1747#else
1748 temp = new_ts - (start_vcol % new_ts);
1749 if (num_spaces >= temp)
1750 {
1751 num_spaces -= temp;
1752 num_tabs++;
1753 }
1754 num_tabs += num_spaces / new_ts;
1755 num_spaces -= (num_spaces / new_ts) * new_ts;
1756#endif
1757 }
1758 if (curbuf->b_p_et || got_tab ||
1759 (num_spaces + num_tabs < len))
1760 {
1761 if (did_undo == FALSE)
1762 {
1763 did_undo = TRUE;
1764 if (u_save((linenr_T)(lnum - 1),
1765 (linenr_T)(lnum + 1)) == FAIL)
1766 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001767 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001768 break;
1769 }
1770 }
1771
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001772 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001773 len = num_spaces + num_tabs;
1774 old_len = (long)STRLEN(ptr);
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001775 new_len = old_len - col + start_col + len + 1;
Bram Moolenaar45491662022-02-12 21:59:51 +00001776 if (new_len <= 0 || new_len >= MAXCOL)
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001777 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001778 emsg_text_too_long();
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001779 break;
1780 }
1781 new_line = alloc(new_len);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001782 if (new_line == NULL)
1783 break;
1784 if (start_col > 0)
1785 mch_memmove(new_line, ptr, (size_t)start_col);
1786 mch_memmove(new_line + start_col + len,
1787 ptr + col, (size_t)(old_len - col + 1));
1788 ptr = new_line + start_col;
1789 for (col = 0; col < len; col++)
1790 ptr[col] = (col < num_tabs) ? '\t' : ' ';
Bram Moolenaar0dcd39b2021-02-03 19:44:25 +01001791 if (ml_replace(lnum, new_line, FALSE) == OK)
1792 // "new_line" may have been copied
1793 new_line = curbuf->b_ml.ml_line_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001794 if (first_line == 0)
1795 first_line = lnum;
1796 last_line = lnum;
1797 ptr = new_line;
1798 col = start_col + len;
1799 }
1800 }
1801 got_tab = FALSE;
1802 num_spaces = 0;
1803 }
1804 if (ptr[col] == NUL)
1805 break;
1806 vcol += chartabsize(ptr + col, (colnr_T)vcol);
Bram Moolenaar6e287032022-02-12 15:42:18 +00001807 if (vcol >= MAXCOL)
1808 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001809 emsg_text_too_long();
Bram Moolenaar6e287032022-02-12 15:42:18 +00001810 break;
1811 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001812 if (has_mbyte)
1813 col += (*mb_ptr2len)(ptr + col);
1814 else
1815 ++col;
1816 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001817 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001818 break;
1819 line_breakcheck();
1820 }
1821 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001822 emsg(_(e_interrupted));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001823
1824#ifdef FEAT_VARTABS
1825 // If a single value was given then it can be considered equal to
1826 // either the value of 'tabstop' or the value of 'vartabstop'.
1827 if (tabstop_count(curbuf->b_p_vts_array) == 0
1828 && tabstop_count(new_vts_array) == 1
1829 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001830 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001831 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001832 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001833 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001834 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001835 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001836#else
1837 if (curbuf->b_p_ts != new_ts)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001838 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001839#endif
1840 if (first_line != 0)
1841 changed_lines(first_line, 0, last_line + 1, 0L);
1842
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001843 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001844
1845#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001846 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001847 {
1848 // If 'vartabstop' is in use or if the value given to retab has more
1849 // than one tabstop then update 'vartabstop'.
1850 int *old_vts_ary = curbuf->b_p_vts_array;
1851
1852 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1853 {
1854 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1855 OPT_FREE|OPT_LOCAL, 0);
1856 curbuf->b_p_vts_array = new_vts_array;
1857 vim_free(old_vts_ary);
1858 }
1859 else
1860 {
1861 // 'vartabstop' wasn't in use and a single value was given to
1862 // retab then update 'tabstop'.
1863 curbuf->b_p_ts = tabstop_first(new_vts_array);
1864 vim_free(new_vts_array);
1865 }
1866 vim_free(new_ts_str);
1867 }
1868#else
1869 curbuf->b_p_ts = new_ts;
1870#endif
1871 coladvance(curwin->w_curswant);
1872
1873 u_clearline();
1874}
1875
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001876#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001877/*
1878 * Get indent level from 'indentexpr'.
1879 */
1880 int
1881get_expr_indent(void)
1882{
1883 int indent = -1;
1884 char_u *inde_copy;
1885 pos_T save_pos;
1886 colnr_T save_curswant;
1887 int save_set_curswant;
1888 int save_State;
1889 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1890 OPT_LOCAL);
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001891 sctx_T save_sctx = current_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001892
1893 // Save and restore cursor position and curswant, in case it was changed
1894 // via :normal commands
1895 save_pos = curwin->w_cursor;
1896 save_curswant = curwin->w_curswant;
1897 save_set_curswant = curwin->w_set_curswant;
1898 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1899 if (use_sandbox)
1900 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001901 ++textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001902 current_sctx = curbuf->b_p_script_ctx[BV_INDE];
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001903
1904 // Need to make a copy, the 'indentexpr' option could be changed while
1905 // evaluating it.
1906 inde_copy = vim_strsave(curbuf->b_p_inde);
1907 if (inde_copy != NULL)
1908 {
Bram Moolenaar3292a222022-10-01 20:17:17 +01001909 indent = (int)eval_to_number(inde_copy, TRUE);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001910 vim_free(inde_copy);
1911 }
1912
1913 if (use_sandbox)
1914 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001915 --textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001916 current_sctx = save_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001917
1918 // Restore the cursor position so that 'indentexpr' doesn't need to.
1919 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1920 // command.
1921 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001922 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001923 curwin->w_cursor = save_pos;
1924 curwin->w_curswant = save_curswant;
1925 curwin->w_set_curswant = save_set_curswant;
1926 check_cursor();
1927 State = save_State;
1928
Bram Moolenaar620c9592021-07-31 21:32:31 +02001929 // Reset did_throw, unless 'debug' has "throw" and inside a try/catch.
1930 if (did_throw && (vim_strchr(p_debug, 't') == NULL || trylevel == 0))
1931 {
1932 handle_did_throw();
1933 did_throw = FALSE;
1934 }
1935
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001936 // If there is an error, just keep the current indent.
1937 if (indent < 0)
1938 indent = get_indent();
1939
1940 return indent;
1941}
1942#endif
1943
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001944 static int
1945lisp_match(char_u *p)
1946{
1947 char_u buf[LSIZE];
1948 int len;
1949 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1950
1951 while (*word != NUL)
1952 {
1953 (void)copy_option_part(&word, buf, LSIZE, ",");
1954 len = (int)STRLEN(buf);
Bram Moolenaard26c5802022-10-13 12:30:08 +01001955 if (STRNCMP(buf, p, len) == 0 && IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001956 return TRUE;
1957 }
1958 return FALSE;
1959}
1960
1961/*
1962 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
1963 * The incompatible newer method is quite a bit better at indenting
1964 * code in lisp-like languages than the traditional one; it's still
1965 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
1966 *
1967 * TODO:
1968 * Findmatch() should be adapted for lisp, also to make showmatch
1969 * work correctly: now (v5.3) it seems all C/C++ oriented:
1970 * - it does not recognize the #\( and #\) notations as character literals
1971 * - it doesn't know about comments starting with a semicolon
1972 * - it incorrectly interprets '(' as a character literal
1973 * All this messes up get_lisp_indent in some rare cases.
1974 * Update from Sergey Khorev:
1975 * I tried to fix the first two issues.
1976 */
1977 int
1978get_lisp_indent(void)
1979{
1980 pos_T *pos, realpos, paren;
1981 int amount;
1982 char_u *that;
1983 colnr_T col;
1984 colnr_T firsttry;
1985 int parencount, quotecount;
1986 int vi_lisp;
1987
1988 // Set vi_lisp to use the vi-compatible method
1989 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
1990
1991 realpos = curwin->w_cursor;
1992 curwin->w_cursor.col = 0;
1993
1994 if ((pos = findmatch(NULL, '(')) == NULL)
1995 pos = findmatch(NULL, '[');
1996 else
1997 {
1998 paren = *pos;
1999 pos = findmatch(NULL, '[');
2000 if (pos == NULL || LT_POSP(pos, &paren))
2001 pos = &paren;
2002 }
2003 if (pos != NULL)
2004 {
2005 // Extra trick: Take the indent of the first previous non-white
2006 // line that is at the same () level.
2007 amount = -1;
2008 parencount = 0;
2009
2010 while (--curwin->w_cursor.lnum >= pos->lnum)
2011 {
2012 if (linewhite(curwin->w_cursor.lnum))
2013 continue;
2014 for (that = ml_get_curline(); *that != NUL; ++that)
2015 {
2016 if (*that == ';')
2017 {
2018 while (*(that + 1) != NUL)
2019 ++that;
2020 continue;
2021 }
2022 if (*that == '\\')
2023 {
2024 if (*(that + 1) != NUL)
2025 ++that;
2026 continue;
2027 }
2028 if (*that == '"' && *(that + 1) != NUL)
2029 {
2030 while (*++that && *that != '"')
2031 {
2032 // skipping escaped characters in the string
2033 if (*that == '\\')
2034 {
2035 if (*++that == NUL)
2036 break;
2037 if (that[1] == NUL)
2038 {
2039 ++that;
2040 break;
2041 }
2042 }
2043 }
Bram Moolenaar0e8e9382022-06-18 12:51:11 +01002044 if (*that == NUL)
2045 break;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002046 }
2047 if (*that == '(' || *that == '[')
2048 ++parencount;
2049 else if (*that == ')' || *that == ']')
2050 --parencount;
2051 }
2052 if (parencount == 0)
2053 {
2054 amount = get_indent();
2055 break;
2056 }
2057 }
2058
2059 if (amount == -1)
2060 {
2061 curwin->w_cursor.lnum = pos->lnum;
2062 curwin->w_cursor.col = pos->col;
2063 col = pos->col;
2064
2065 that = ml_get_curline();
2066
2067 if (vi_lisp && get_indent() == 0)
2068 amount = 2;
2069 else
2070 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002071 char_u *line = that;
2072 chartabsize_T cts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002073
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002074 init_chartabsize_arg(&cts, curwin, pos->lnum, 0, line, line);
2075 while (*cts.cts_ptr != NUL && col > 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002076 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002077 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002078 col--;
2079 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002080 amount = cts.cts_vcol;
2081 that = cts.cts_ptr;
2082 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002083
2084 // Some keywords require "body" indenting rules (the
2085 // non-standard-lisp ones are Scheme special forms):
2086 //
2087 // (let ((a 1)) instead (let ((a 1))
2088 // (...)) of (...))
2089
2090 if (!vi_lisp && (*that == '(' || *that == '[')
2091 && lisp_match(that + 1))
2092 amount += 2;
2093 else
2094 {
Bram Moolenaar8eba2bd2022-06-22 19:59:28 +01002095 if (*that != NUL)
2096 {
2097 that++;
2098 amount++;
2099 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002100 firsttry = amount;
2101
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002102 init_chartabsize_arg(&cts, curwin, (colnr_T)(that - line),
2103 amount, line, that);
2104 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002105 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002106 cts.cts_vcol += lbr_chartabsize(&cts);
2107 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002108 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002109 that = cts.cts_ptr;
2110 amount = cts.cts_vcol;
2111 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002112
2113 if (*that && *that != ';') // not a comment line
2114 {
2115 // test *that != '(' to accommodate first let/do
2116 // argument if it is more than one line
2117 if (!vi_lisp && *that != '(' && *that != '[')
2118 firsttry++;
2119
2120 parencount = 0;
2121 quotecount = 0;
2122
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002123 init_chartabsize_arg(&cts, curwin,
2124 (colnr_T)(that - line), amount, line, that);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002125 if (vi_lisp
2126 || (*that != '"'
2127 && *that != '\''
2128 && *that != '#'
2129 && (*that < '0' || *that > '9')))
2130 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002131 while (*cts.cts_ptr
2132 && (!VIM_ISWHITE(*cts.cts_ptr)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002133 || quotecount
2134 || parencount)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002135 && (!((*cts.cts_ptr == '('
2136 || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002137 && !quotecount
2138 && !parencount
2139 && vi_lisp)))
2140 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002141 if (*cts.cts_ptr == '"')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002142 quotecount = !quotecount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002143 if ((*cts.cts_ptr == '(' || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002144 && !quotecount)
2145 ++parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002146 if ((*cts.cts_ptr == ')' || *cts.cts_ptr == ']')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002147 && !quotecount)
2148 --parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002149 if (*cts.cts_ptr == '\\'
2150 && *(cts.cts_ptr+1) != NUL)
2151 cts.cts_vcol += lbr_chartabsize_adv(&cts);
2152 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002153 }
2154 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002155 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002156 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002157 cts.cts_vcol += lbr_chartabsize(&cts);
2158 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002159 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002160 that = cts.cts_ptr;
2161 amount = cts.cts_vcol;
2162 clear_chartabsize_arg(&cts);
2163
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002164 if (!*that || *that == ';')
2165 amount = firsttry;
2166 }
2167 }
2168 }
2169 }
2170 }
2171 else
2172 amount = 0; // no matching '(' or '[' found, use zero indent
2173
2174 curwin->w_cursor = realpos;
2175
2176 return amount;
2177}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002178
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002179/*
2180 * Re-indent the current line, based on the current contents of it and the
2181 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
2182 * confused what all the part that handles Control-T is doing that I'm not.
2183 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
2184 */
2185
2186 void
2187fixthisline(int (*get_the_indent)(void))
2188{
2189 int amount = get_the_indent();
2190
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002191 if (amount < 0)
2192 return;
2193
2194 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
2195 if (linewhite(curwin->w_cursor.lnum))
2196 did_ai = TRUE; // delete the indent if the line stays empty
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002197}
2198
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002199/*
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002200 * Return TRUE if 'indentexpr' should be used for Lisp indenting.
2201 * Caller may want to check 'autoindent'.
2202 */
2203 int
2204use_indentexpr_for_lisp(void)
2205{
2206#ifdef FEAT_EVAL
2207 return curbuf->b_p_lisp
2208 && *curbuf->b_p_inde != NUL
2209 && STRCMP(curbuf->b_p_lop, "expr:1") == 0;
2210#else
2211 return FALSE;
2212#endif
2213}
2214
2215/*
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002216 * Fix indent for 'lisp' and 'cindent'.
2217 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002218 void
2219fix_indent(void)
2220{
2221 if (p_paste)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002222 return; // no auto-indenting when 'paste' is set
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002223 if (curbuf->b_p_lisp && curbuf->b_p_ai)
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002224 {
2225 if (use_indentexpr_for_lisp())
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002226 do_c_expr_indent();
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002227 else
2228 fixthisline(get_lisp_indent);
2229 }
2230 else if (cindent_on())
2231 do_c_expr_indent();
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002232}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002233
2234#if defined(FEAT_EVAL) || defined(PROTO)
2235/*
2236 * "indent()" function
2237 */
2238 void
2239f_indent(typval_T *argvars, typval_T *rettv)
2240{
2241 linenr_T lnum;
2242
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002243 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2244 return;
2245
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002246 lnum = tv_get_lnum(argvars);
2247 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2248 rettv->vval.v_number = get_indent_lnum(lnum);
2249 else
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002250 {
2251 if (in_vim9script())
2252 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002253 rettv->vval.v_number = -1;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002254 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002255}
2256
2257/*
2258 * "lispindent(lnum)" function
2259 */
2260 void
2261f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2262{
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002263 pos_T pos;
2264 linenr_T lnum;
2265
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002266 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2267 return;
2268
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002269 pos = curwin->w_cursor;
2270 lnum = tv_get_lnum(argvars);
2271 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2272 {
2273 curwin->w_cursor.lnum = lnum;
2274 rettv->vval.v_number = get_lisp_indent();
2275 curwin->w_cursor = pos;
2276 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002277 else if (in_vim9script())
2278 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002279 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002280 rettv->vval.v_number = -1;
2281}
2282#endif