blob: dfc481a7851c5175de65b9a22ed7d56240582ca6 [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);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +020077 vim_free(*array);
78 *array = NULL;
Bram Moolenaarb7081e12021-09-04 18:47:28 +020079 return FAIL;
80 }
81 (*array)[t++] = n;
82 while (*cp != NUL && *cp != ',')
Bram Moolenaare677df82019-09-02 22:31:11 +020083 ++cp;
84 if (*cp != NUL)
85 ++cp;
86 }
87
Bram Moolenaarb7081e12021-09-04 18:47:28 +020088 return OK;
Bram Moolenaare677df82019-09-02 22:31:11 +020089}
90
91/*
92 * Calculate the number of screen spaces a tab will occupy.
93 * If "vts" is set then the tab widths are taken from that array,
94 * otherwise the value of ts is used.
95 */
96 int
97tabstop_padding(colnr_T col, int ts_arg, int *vts)
98{
99 int ts = ts_arg == 0 ? 8 : ts_arg;
100 int tabcount;
101 colnr_T tabcol = 0;
102 int t;
103 int padding = 0;
104
105 if (vts == NULL || vts[0] == 0)
106 return ts - (col % ts);
107
108 tabcount = vts[0];
109
110 for (t = 1; t <= tabcount; ++t)
111 {
112 tabcol += vts[t];
113 if (tabcol > col)
114 {
115 padding = (int)(tabcol - col);
116 break;
117 }
118 }
119 if (t > tabcount)
120 padding = vts[tabcount] - (int)((col - tabcol) % vts[tabcount]);
121
122 return padding;
123}
124
125/*
126 * Find the size of the tab that covers a particular column.
127 */
128 int
129tabstop_at(colnr_T col, int ts, int *vts)
130{
131 int tabcount;
132 colnr_T tabcol = 0;
133 int t;
134 int tab_size = 0;
135
136 if (vts == 0 || vts[0] == 0)
137 return ts;
138
139 tabcount = vts[0];
140 for (t = 1; t <= tabcount; ++t)
141 {
142 tabcol += vts[t];
143 if (tabcol > col)
144 {
145 tab_size = vts[t];
146 break;
147 }
148 }
149 if (t > tabcount)
150 tab_size = vts[tabcount];
151
152 return tab_size;
153}
154
155/*
156 * Find the column on which a tab starts.
157 */
158 colnr_T
159tabstop_start(colnr_T col, int ts, int *vts)
160{
161 int tabcount;
162 colnr_T tabcol = 0;
163 int t;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100164 int excess;
Bram Moolenaare677df82019-09-02 22:31:11 +0200165
166 if (vts == NULL || vts[0] == 0)
167 return (col / ts) * ts;
168
169 tabcount = vts[0];
170 for (t = 1; t <= tabcount; ++t)
171 {
172 tabcol += vts[t];
173 if (tabcol > col)
174 return tabcol - vts[t];
175 }
176
177 excess = tabcol % vts[tabcount];
178 return excess + ((col - excess) / vts[tabcount]) * vts[tabcount];
179}
180
181/*
182 * Find the number of tabs and spaces necessary to get from one column
183 * to another.
184 */
185 void
186tabstop_fromto(
187 colnr_T start_col,
188 colnr_T end_col,
189 int ts_arg,
190 int *vts,
191 int *ntabs,
192 int *nspcs)
193{
194 int spaces = end_col - start_col;
195 colnr_T tabcol = 0;
196 int padding = 0;
197 int tabcount;
198 int t;
199 int ts = ts_arg == 0 ? curbuf->b_p_ts : ts_arg;
200
201 if (vts == NULL || vts[0] == 0)
202 {
203 int tabs = 0;
204 int initspc = 0;
205
206 initspc = ts - (start_col % ts);
207 if (spaces >= initspc)
208 {
209 spaces -= initspc;
210 tabs++;
211 }
212 tabs += spaces / ts;
213 spaces -= (spaces / ts) * ts;
214
215 *ntabs = tabs;
216 *nspcs = spaces;
217 return;
218 }
219
220 // Find the padding needed to reach the next tabstop.
221 tabcount = vts[0];
222 for (t = 1; t <= tabcount; ++t)
223 {
224 tabcol += vts[t];
225 if (tabcol > start_col)
226 {
227 padding = (int)(tabcol - start_col);
228 break;
229 }
230 }
231 if (t > tabcount)
232 padding = vts[tabcount] - (int)((start_col - tabcol) % vts[tabcount]);
233
234 // If the space needed is less than the padding no tabs can be used.
235 if (spaces < padding)
236 {
237 *ntabs = 0;
238 *nspcs = spaces;
239 return;
240 }
241
242 *ntabs = 1;
243 spaces -= padding;
244
245 // At least one tab has been used. See if any more will fit.
246 while (spaces != 0 && ++t <= tabcount)
247 {
248 padding = vts[t];
249 if (spaces < padding)
250 {
251 *nspcs = spaces;
252 return;
253 }
254 ++*ntabs;
255 spaces -= padding;
256 }
257
258 *ntabs += spaces / vts[tabcount];
259 *nspcs = spaces % vts[tabcount];
260}
261
262/*
263 * See if two tabstop arrays contain the same values.
264 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200265 static int
Bram Moolenaare677df82019-09-02 22:31:11 +0200266tabstop_eq(int *ts1, int *ts2)
267{
268 int t;
269
270 if ((ts1 == 0 && ts2) || (ts1 && ts2 == 0))
271 return FALSE;
272 if (ts1 == ts2)
273 return TRUE;
274 if (ts1[0] != ts2[0])
275 return FALSE;
276
277 for (t = 1; t <= ts1[0]; ++t)
278 if (ts1[t] != ts2[t])
279 return FALSE;
280
281 return TRUE;
282}
283
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200284# if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaare677df82019-09-02 22:31:11 +0200285/*
286 * Copy a tabstop array, allocating space for the new array.
287 */
288 int *
289tabstop_copy(int *oldts)
290{
291 int *newts;
292 int t;
293
294 if (oldts == NULL)
295 return NULL;
296 newts = ALLOC_MULT(int, oldts[0] + 1);
297 if (newts != NULL)
298 for (t = 0; t <= oldts[0]; ++t)
299 newts[t] = oldts[t];
300 return newts;
301}
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200302# endif
Bram Moolenaare677df82019-09-02 22:31:11 +0200303
304/*
305 * Return a count of the number of tabstops.
306 */
307 int
308tabstop_count(int *ts)
309{
310 return ts != NULL ? ts[0] : 0;
311}
312
313/*
314 * Return the first tabstop, or 8 if there are no tabstops defined.
315 */
316 int
317tabstop_first(int *ts)
318{
319 return ts != NULL ? ts[1] : 8;
320}
321
322#endif
323
324/*
325 * Return the effective shiftwidth value for current buffer, using the
326 * 'tabstop' value when 'shiftwidth' is zero.
327 */
328 long
329get_sw_value(buf_T *buf)
330{
331 return get_sw_value_col(buf, 0);
332}
333
334/*
335 * Idem, using "pos".
336 */
337 static long
338get_sw_value_pos(buf_T *buf, pos_T *pos)
339{
340 pos_T save_cursor = curwin->w_cursor;
341 long sw_value;
342
343 curwin->w_cursor = *pos;
344 sw_value = get_sw_value_col(buf, get_nolist_virtcol());
345 curwin->w_cursor = save_cursor;
346 return sw_value;
347}
348
349/*
350 * Idem, using the first non-black in the current line.
351 */
352 long
353get_sw_value_indent(buf_T *buf)
354{
355 pos_T pos = curwin->w_cursor;
356
357 pos.col = getwhitecols_curline();
358 return get_sw_value_pos(buf, &pos);
359}
360
361/*
362 * Idem, using virtual column "col".
363 */
364 long
365get_sw_value_col(buf_T *buf, colnr_T col UNUSED)
366{
367 return buf->b_p_sw ? buf->b_p_sw :
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200368#ifdef FEAT_VARTABS
Bram Moolenaare677df82019-09-02 22:31:11 +0200369 tabstop_at(col, buf->b_p_ts, buf->b_p_vts_array);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200370#else
Bram Moolenaare677df82019-09-02 22:31:11 +0200371 buf->b_p_ts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200372#endif
Bram Moolenaare677df82019-09-02 22:31:11 +0200373}
374
375/*
376 * Return the effective softtabstop value for the current buffer, using the
377 * 'shiftwidth' value when 'softtabstop' is negative.
378 */
379 long
380get_sts_value(void)
381{
382 return curbuf->b_p_sts < 0 ? get_sw_value(curbuf) : curbuf->b_p_sts;
383}
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200384
385/*
386 * Count the size (in window cells) of the indent in the current line.
387 */
388 int
389get_indent(void)
390{
391#ifdef FEAT_VARTABS
392 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
393 curbuf->b_p_vts_array, FALSE);
394#else
395 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
396#endif
397}
398
399/*
400 * Count the size (in window cells) of the indent in line "lnum".
401 */
402 int
403get_indent_lnum(linenr_T lnum)
404{
405#ifdef FEAT_VARTABS
406 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
407 curbuf->b_p_vts_array, FALSE);
408#else
409 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
410#endif
411}
412
413#if defined(FEAT_FOLDING) || defined(PROTO)
414/*
415 * Count the size (in window cells) of the indent in line "lnum" of buffer
416 * "buf".
417 */
418 int
419get_indent_buf(buf_T *buf, linenr_T lnum)
420{
421# ifdef FEAT_VARTABS
422 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
423 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
424# else
425 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
426# endif
427}
428#endif
429
430/*
431 * count the size (in window cells) of the indent in line "ptr", with
432 * 'tabstop' at "ts"
433 */
434 int
435get_indent_str(
436 char_u *ptr,
437 int ts,
438 int list) // if TRUE, count only screen size for tabs
439{
440 int count = 0;
441
442 for ( ; *ptr; ++ptr)
443 {
444 if (*ptr == TAB)
445 {
Bram Moolenaareed9d462021-02-15 20:38:25 +0100446 if (!list || curwin->w_lcs_chars.tab1)
447 // count a tab for what it is worth
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200448 count += ts - (count % ts);
449 else
450 // In list mode, when tab is not set, count screen char width
451 // for Tab, displays: ^I
452 count += ptr2cells(ptr);
453 }
454 else if (*ptr == ' ')
455 ++count; // count a space for one
456 else
457 break;
458 }
459 return count;
460}
461
462#ifdef FEAT_VARTABS
463/*
464 * Count the size (in window cells) of the indent in line "ptr", using
465 * variable tabstops.
466 * if "list" is TRUE, count only screen size for tabs.
467 */
468 int
469get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
470{
471 int count = 0;
472
473 for ( ; *ptr; ++ptr)
474 {
475 if (*ptr == TAB) // count a tab for what it is worth
476 {
Bram Moolenaareed9d462021-02-15 20:38:25 +0100477 if (!list || curwin->w_lcs_chars.tab1)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200478 count += tabstop_padding(count, ts, vts);
479 else
480 // In list mode, when tab is not set, count screen char width
481 // for Tab, displays: ^I
482 count += ptr2cells(ptr);
483 }
484 else if (*ptr == ' ')
485 ++count; // count a space for one
486 else
487 break;
488 }
489 return count;
490}
491#endif
492
493/*
494 * Set the indent of the current line.
495 * Leaves the cursor on the first non-blank in the line.
496 * Caller must take care of undo.
497 * "flags":
498 * SIN_CHANGED: call changed_bytes() if the line was changed.
499 * SIN_INSERT: insert the indent in front of the line.
500 * SIN_UNDO: save line for undo before changing it.
501 * Returns TRUE if the line was changed.
502 */
503 int
504set_indent(
505 int size, // measured in spaces
506 int flags)
507{
508 char_u *p;
509 char_u *newline;
510 char_u *oldline;
511 char_u *s;
512 int todo;
513 int ind_len; // measured in characters
514 int line_len;
515 int doit = FALSE;
516 int ind_done = 0; // measured in spaces
517#ifdef FEAT_VARTABS
518 int ind_col = 0;
519#endif
520 int tab_pad;
521 int retval = FALSE;
522 int orig_char_len = -1; // number of initial whitespace chars when
523 // 'et' and 'pi' are both set
524
525 // First check if there is anything to do and compute the number of
526 // characters needed for the indent.
527 todo = size;
528 ind_len = 0;
529 p = oldline = ml_get_curline();
530
531 // Calculate the buffer size for the new indent, and check to see if it
532 // isn't already set
533
534 // if 'expandtab' isn't set: use TABs; if both 'expandtab' and
535 // 'preserveindent' are set count the number of characters at the
536 // beginning of the line to be copied
537 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
538 {
539 // If 'preserveindent' is set then reuse as much as possible of
540 // the existing indent structure for the new indent
541 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
542 {
543 ind_done = 0;
544
545 // count as many characters as we can use
546 while (todo > 0 && VIM_ISWHITE(*p))
547 {
548 if (*p == TAB)
549 {
550#ifdef FEAT_VARTABS
551 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
552 curbuf->b_p_vts_array);
553#else
554 tab_pad = (int)curbuf->b_p_ts
555 - (ind_done % (int)curbuf->b_p_ts);
556#endif
557 // stop if this tab will overshoot the target
558 if (todo < tab_pad)
559 break;
560 todo -= tab_pad;
561 ++ind_len;
562 ind_done += tab_pad;
563 }
564 else
565 {
566 --todo;
567 ++ind_len;
568 ++ind_done;
569 }
570 ++p;
571 }
572
573#ifdef FEAT_VARTABS
574 // These diverge from this point.
575 ind_col = ind_done;
576#endif
577 // Set initial number of whitespace chars to copy if we are
578 // preserving indent but expandtab is set
579 if (curbuf->b_p_et)
580 orig_char_len = ind_len;
581
582 // Fill to next tabstop with a tab, if possible
583#ifdef FEAT_VARTABS
584 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
585 curbuf->b_p_vts_array);
586#else
587 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
588#endif
589 if (todo >= tab_pad && orig_char_len == -1)
590 {
591 doit = TRUE;
592 todo -= tab_pad;
593 ++ind_len;
594 // ind_done += tab_pad;
595#ifdef FEAT_VARTABS
596 ind_col += tab_pad;
597#endif
598 }
599 }
600
601 // count tabs required for indent
602#ifdef FEAT_VARTABS
603 for (;;)
604 {
605 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
606 curbuf->b_p_vts_array);
607 if (todo < tab_pad)
608 break;
609 if (*p != TAB)
610 doit = TRUE;
611 else
612 ++p;
613 todo -= tab_pad;
614 ++ind_len;
615 ind_col += tab_pad;
616 }
617#else
618 while (todo >= (int)curbuf->b_p_ts)
619 {
620 if (*p != TAB)
621 doit = TRUE;
622 else
623 ++p;
624 todo -= (int)curbuf->b_p_ts;
625 ++ind_len;
626 // ind_done += (int)curbuf->b_p_ts;
627 }
628#endif
629 }
630 // count spaces required for indent
631 while (todo > 0)
632 {
633 if (*p != ' ')
634 doit = TRUE;
635 else
636 ++p;
637 --todo;
638 ++ind_len;
639 // ++ind_done;
640 }
641
642 // Return if the indent is OK already.
643 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
644 return FALSE;
645
646 // Allocate memory for the new line.
647 if (flags & SIN_INSERT)
648 p = oldline;
649 else
650 p = skipwhite(p);
651 line_len = (int)STRLEN(p) + 1;
652
653 // If 'preserveindent' and 'expandtab' are both set keep the original
654 // characters and allocate accordingly. We will fill the rest with spaces
655 // after the if (!curbuf->b_p_et) below.
656 if (orig_char_len != -1)
657 {
658 newline = alloc(orig_char_len + size - ind_done + line_len);
659 if (newline == NULL)
660 return FALSE;
661 todo = size - ind_done;
662 ind_len = orig_char_len + todo; // Set total length of indent in
663 // characters, which may have been
664 // undercounted until now
665 p = oldline;
666 s = newline;
667 while (orig_char_len > 0)
668 {
669 *s++ = *p++;
670 orig_char_len--;
671 }
672
673 // Skip over any additional white space (useful when newindent is less
674 // than old)
675 while (VIM_ISWHITE(*p))
676 ++p;
677
678 }
679 else
680 {
681 todo = size;
682 newline = alloc(ind_len + line_len);
683 if (newline == NULL)
684 return FALSE;
685 s = newline;
686 }
687
688 // Put the characters in the new line.
689 // if 'expandtab' isn't set: use TABs
690 if (!curbuf->b_p_et)
691 {
692 // If 'preserveindent' is set then reuse as much as possible of
693 // the existing indent structure for the new indent
694 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
695 {
696 p = oldline;
697 ind_done = 0;
698
699 while (todo > 0 && VIM_ISWHITE(*p))
700 {
701 if (*p == TAB)
702 {
703#ifdef FEAT_VARTABS
704 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
705 curbuf->b_p_vts_array);
706#else
707 tab_pad = (int)curbuf->b_p_ts
708 - (ind_done % (int)curbuf->b_p_ts);
709#endif
710 // stop if this tab will overshoot the target
711 if (todo < tab_pad)
712 break;
713 todo -= tab_pad;
714 ind_done += tab_pad;
715 }
716 else
717 {
718 --todo;
719 ++ind_done;
720 }
721 *s++ = *p++;
722 }
723
724 // Fill to next tabstop with a tab, if possible
725#ifdef FEAT_VARTABS
726 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
727 curbuf->b_p_vts_array);
728#else
729 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
730#endif
731 if (todo >= tab_pad)
732 {
733 *s++ = TAB;
734 todo -= tab_pad;
735#ifdef FEAT_VARTABS
736 ind_done += tab_pad;
737#endif
738 }
739
740 p = skipwhite(p);
741 }
742
743#ifdef FEAT_VARTABS
744 for (;;)
745 {
746 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
747 curbuf->b_p_vts_array);
748 if (todo < tab_pad)
749 break;
750 *s++ = TAB;
751 todo -= tab_pad;
752 ind_done += tab_pad;
753 }
754#else
755 while (todo >= (int)curbuf->b_p_ts)
756 {
757 *s++ = TAB;
758 todo -= (int)curbuf->b_p_ts;
759 }
760#endif
761 }
762 while (todo > 0)
763 {
764 *s++ = ' ';
765 --todo;
766 }
767 mch_memmove(s, p, (size_t)line_len);
768
769 // Replace the line (unless undo fails).
770 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
771 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200772 colnr_T old_offset = (colnr_T)(p - oldline);
773 colnr_T new_offset = (colnr_T)(s - newline);
774
775 // this may free "newline"
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200776 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
777 if (flags & SIN_CHANGED)
778 changed_bytes(curwin->w_cursor.lnum, 0);
779
780 // Correct saved cursor position if it is in this line.
781 if (saved_cursor.lnum == curwin->w_cursor.lnum)
782 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200783 if (saved_cursor.col >= old_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200784 // cursor was after the indent, adjust for the number of
785 // bytes added/removed
Bram Moolenaarcf306432020-06-29 20:40:37 +0200786 saved_cursor.col += ind_len - old_offset;
787 else if (saved_cursor.col >= new_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200788 // cursor was in the indent, and is now after it, put it back
789 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaarcf306432020-06-29 20:40:37 +0200790 saved_cursor.col = new_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200791 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100792#ifdef FEAT_PROP_POPUP
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200793 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200794 int added = ind_len - old_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200795
796 // When increasing indent this behaves like spaces were inserted at
797 // the old indent, when decreasing indent it behaves like spaces
798 // were deleted at the new indent.
799 adjust_prop_columns(curwin->w_cursor.lnum,
Bram Moolenaarcf306432020-06-29 20:40:37 +0200800 added > 0 ? old_offset : (colnr_T)ind_len, added, 0);
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 {
874 if (STRNCMP(p, "shift:", 6) == 0
875 && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
876 {
877 p += 6;
878 bri_shift = getdigits(&p);
879 }
880 else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4]))
881 {
882 p += 4;
883 bri_min = getdigits(&p);
884 }
885 else if (STRNCMP(p, "sbr", 3) == 0)
886 {
887 p += 3;
888 bri_sbr = TRUE;
889 }
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200890 else if (STRNCMP(p, "list:", 5) == 0)
891 {
892 p += 5;
893 bri_list = getdigits(&p);
894 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100895 else if (STRNCMP(p, "column:", 7) == 0)
896 {
897 p += 7;
898 bri_vcol = getdigits(&p);
899 }
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100900 if (*p != ',' && *p != NUL)
901 return FAIL;
902 if (*p == ',')
903 ++p;
904 }
905
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100906 wp->w_briopt_shift = bri_shift;
907 wp->w_briopt_min = bri_min;
908 wp->w_briopt_sbr = bri_sbr;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200909 wp->w_briopt_list = bri_list;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100910 wp->w_briopt_vcol = bri_vcol;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100911
912 return OK;
913}
914
915/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200916 * Return appropriate space number for breakindent, taking influencing
917 * parameters into account. Window must be specified, since it is not
918 * necessarily always the current one.
919 */
920 int
921get_breakindent_win(
922 win_T *wp,
923 char_u *line) // start of the line
924{
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000925 static int prev_indent = 0; // cached indent value
926 static long prev_ts = 0L; // cached tabstop value
927 static char_u *prev_line = NULL; // cached pointer to line
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200928 static varnumber_T prev_tick = 0; // changedtick of cached value
929# ifdef FEAT_VARTABS
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000930 static int *prev_vts = NULL; // cached vartabs values
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200931# endif
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000932 static int prev_list = 0; // cached list value
933 static int prev_listopt = 0; // cached w_p_briopt_list value
Christian Brabandtc53b4672022-01-15 10:01:05 +0000934 // cached formatlistpat value
935 static char_u *prev_flp = NULL;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200936 int bri = 0;
937 // window width minus window margin space, i.e. what rests for text
938 const int eff_wwidth = wp->w_width
939 - ((wp->w_p_nu || wp->w_p_rnu)
940 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
941 ? number_width(wp) + 1 : 0);
942
Christian Brabandtc53b4672022-01-15 10:01:05 +0000943 // used cached indent, unless
944 // - line pointer changed
945 // - 'tabstop' changed
946 // - 'briopt_list changed' changed or
947 // - 'formatlistpattern' changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200948 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
949 || prev_tick != CHANGEDTICK(wp->w_buffer)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000950 || prev_listopt != wp->w_briopt_list
Christian Brabandtc53b4672022-01-15 10:01:05 +0000951 || (prev_flp == NULL
952 || (STRCMP(prev_flp, get_flp_value(wp->w_buffer)) != 0))
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200953# ifdef FEAT_VARTABS
954 || prev_vts != wp->w_buffer->b_p_vts_array
955# endif
956 )
957 {
958 prev_line = line;
959 prev_ts = wp->w_buffer->b_p_ts;
960 prev_tick = CHANGEDTICK(wp->w_buffer);
961# ifdef FEAT_VARTABS
962 prev_vts = wp->w_buffer->b_p_vts_array;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100963 if (wp->w_briopt_vcol == 0)
964 prev_indent = get_indent_str_vtab(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200965 (int)wp->w_buffer->b_p_ts,
966 wp->w_buffer->b_p_vts_array, wp->w_p_list);
967# else
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100968 if (wp->w_briopt_vcol == 0)
969 prev_indent = get_indent_str(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200970 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
971# endif
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000972 prev_listopt = wp->w_briopt_list;
Christian Brabandtc53b4672022-01-15 10:01:05 +0000973 prev_list = 0;
974 vim_free(prev_flp);
975 prev_flp = vim_strsave(get_flp_value(wp->w_buffer));
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000976 // add additional indent for numbered lists
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100977 if (wp->w_briopt_list != 0 && wp->w_briopt_vcol == 0)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000978 {
979 regmatch_T regmatch;
980
Christian Brabandtc53b4672022-01-15 10:01:05 +0000981 regmatch.regprog = vim_regcomp(prev_flp,
982 RE_MAGIC + RE_STRING + RE_AUTO + RE_STRICT);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000983
984 if (regmatch.regprog != NULL)
985 {
986 regmatch.rm_ic = FALSE;
987 if (vim_regexec(&regmatch, line, 0))
988 {
989 if (wp->w_briopt_list > 0)
990 prev_list = wp->w_briopt_list;
991 else
992 prev_list = (*regmatch.endp - *regmatch.startp);
993 }
994 vim_regfree(regmatch.regprog);
995 }
996 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200997 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100998 if (wp->w_briopt_vcol != 0)
999 {
1000 // column value has priority
1001 bri = wp->w_briopt_vcol;
1002 prev_list = 0;
1003 }
1004 else
1005 bri = prev_indent + wp->w_briopt_shift;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001006
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001007 // Add offset for number column, if 'n' is in 'cpoptions'
1008 bri += win_col_off2(wp);
1009
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001010 // add additional indent for numbered lists
Maxim Kimf674b352021-07-22 11:46:59 +02001011 if (wp->w_briopt_list != 0)
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001012 {
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001013 if (wp->w_briopt_list > 0)
1014 bri += prev_list;
1015 else
1016 bri = prev_list;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001017 }
1018
Maxim Kimf674b352021-07-22 11:46:59 +02001019 // indent minus the length of the showbreak string
1020 if (wp->w_briopt_sbr)
1021 bri -= vim_strsize(get_showbreak_value(wp));
1022
1023
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001024 // never indent past left window margin
1025 if (bri < 0)
1026 bri = 0;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001027
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001028 // always leave at least bri_min characters on the left,
1029 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001030 else if (bri > eff_wwidth - wp->w_briopt_min)
1031 bri = (eff_wwidth - wp->w_briopt_min < 0)
1032 ? 0 : eff_wwidth - wp->w_briopt_min;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001033
1034 return bri;
1035}
1036#endif
1037
1038/*
1039 * When extra == 0: Return TRUE if the cursor is before or on the first
1040 * non-blank in the line.
1041 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1042 * the line.
1043 */
1044 int
1045inindent(int extra)
1046{
1047 char_u *ptr;
1048 colnr_T col;
1049
1050 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
1051 ++ptr;
1052 if (col >= curwin->w_cursor.col + extra)
1053 return TRUE;
1054 else
1055 return FALSE;
1056}
1057
1058#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
1059/*
1060 * op_reindent - handle reindenting a block of lines.
1061 */
1062 void
1063op_reindent(oparg_T *oap, int (*how)(void))
1064{
Bram Moolenaar4c84dd32022-04-20 10:22:54 +01001065 long i = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001066 char_u *l;
1067 int amount;
1068 linenr_T first_changed = 0;
1069 linenr_T last_changed = 0;
1070 linenr_T start_lnum = curwin->w_cursor.lnum;
1071
1072 // Don't even try when 'modifiable' is off.
1073 if (!curbuf->b_p_ma)
1074 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001075 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001076 return;
1077 }
1078
Bram Moolenaare4686982022-04-19 18:28:45 +01001079 // Save for undo. Do this once for all lines, much faster than doing this
1080 // for each line separately, especially when undoing.
1081 if (u_savecommon(start_lnum - 1, start_lnum + oap->line_count,
1082 start_lnum + oap->line_count, FALSE) == OK)
1083 for (i = oap->line_count; --i >= 0 && !got_int; )
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001084 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001085 // it's a slow thing to do, so give feedback so there's no worry
1086 // that the computer's just hung.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001087
Bram Moolenaare4686982022-04-19 18:28:45 +01001088 if (i > 1
1089 && (i % 50 == 0 || i == oap->line_count - 1)
1090 && oap->line_count > p_report)
1091 smsg(_("%ld lines to indent... "), i);
1092
1093 // Be vi-compatible: For lisp indenting the first line is not
1094 // indented, unless there is only one line.
1095# ifdef FEAT_LISP
1096 if (i != oap->line_count - 1 || oap->line_count == 1
1097 || how != get_lisp_indent)
1098# endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001099 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001100 l = skipwhite(ml_get_curline());
1101 if (*l == NUL) // empty or blank line
1102 amount = 0;
1103 else
1104 amount = how(); // get the indent for this line
1105
1106 if (amount >= 0 && set_indent(amount, 0))
1107 {
1108 // did change the indent, call changed_lines() later
1109 if (first_changed == 0)
1110 first_changed = curwin->w_cursor.lnum;
1111 last_changed = curwin->w_cursor.lnum;
1112 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001113 }
Bram Moolenaare4686982022-04-19 18:28:45 +01001114 ++curwin->w_cursor.lnum;
1115 curwin->w_cursor.col = 0; // make sure it's valid
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001116 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001117
1118 // put cursor on first non-blank of indented line
1119 curwin->w_cursor.lnum = start_lnum;
1120 beginline(BL_SOL | BL_FIX);
1121
1122 // Mark changed lines so that they will be redrawn. When Visual
1123 // highlighting was present, need to continue until the last line. When
1124 // there is no change still need to remove the Visual highlighting.
1125 if (last_changed != 0)
1126 changed_lines(first_changed, 0,
1127 oap->is_VIsual ? start_lnum + oap->line_count :
1128 last_changed + 1, 0L);
1129 else if (oap->is_VIsual)
1130 redraw_curbuf_later(INVERTED);
1131
1132 if (oap->line_count > p_report)
1133 {
1134 i = oap->line_count - (i + 1);
1135 smsg(NGETTEXT("%ld line indented ",
1136 "%ld lines indented ", i), i);
1137 }
Bram Moolenaare1004402020-10-24 20:49:43 +02001138 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001139 {
1140 // set '[ and '] marks
1141 curbuf->b_op_start = oap->start;
1142 curbuf->b_op_end = oap->end;
1143 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001144}
1145#endif // defined(FEAT_LISP) || defined(FEAT_CINDENT)
1146
1147#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
1148/*
1149 * Return TRUE if lines starting with '#' should be left aligned.
1150 */
1151 int
1152preprocs_left(void)
1153{
1154 return
1155# ifdef FEAT_SMARTINDENT
1156# ifdef FEAT_CINDENT
1157 (curbuf->b_p_si && !curbuf->b_p_cin) ||
1158# else
1159 curbuf->b_p_si
1160# endif
1161# endif
1162# ifdef FEAT_CINDENT
1163 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1164 && curbuf->b_ind_hash_comment == 0)
1165# endif
1166 ;
1167}
1168#endif
1169
1170#ifdef FEAT_SMARTINDENT
1171/*
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001172 * Return TRUE if the conditions are OK for smart indenting.
1173 */
1174 int
1175may_do_si()
1176{
1177 return curbuf->b_p_si
1178# ifdef FEAT_CINDENT
1179 && !curbuf->b_p_cin
1180# endif
1181# ifdef FEAT_EVAL
1182 && *curbuf->b_p_inde == NUL
1183# endif
1184 && !p_paste;
1185}
1186
1187/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001188 * Try to do some very smart auto-indenting.
1189 * Used when inserting a "normal" character.
1190 */
1191 void
1192ins_try_si(int c)
1193{
1194 pos_T *pos, old_pos;
1195 char_u *ptr;
1196 int i;
1197 int temp;
1198
1199 // do some very smart indenting when entering '{' or '}'
1200 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
1201 {
1202 // for '}' set indent equal to indent of line containing matching '{'
1203 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1204 {
1205 old_pos = curwin->w_cursor;
1206 // If the matching '{' has a ')' immediately before it (ignoring
1207 // white-space), then line up with the start of the line
1208 // containing the matching '(' if there is one. This handles the
1209 // case where an "if (..\n..) {" statement continues over multiple
1210 // lines -- webb
1211 ptr = ml_get(pos->lnum);
1212 i = pos->col;
1213 if (i > 0) // skip blanks before '{'
1214 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1215 ;
1216 curwin->w_cursor.lnum = pos->lnum;
1217 curwin->w_cursor.col = i;
1218 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1219 curwin->w_cursor = *pos;
1220 i = get_indent();
1221 curwin->w_cursor = old_pos;
1222 if (State & VREPLACE_FLAG)
1223 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1224 else
1225 (void)set_indent(i, SIN_CHANGED);
1226 }
1227 else if (curwin->w_cursor.col > 0)
1228 {
1229 // when inserting '{' after "O" reduce indent, but not
1230 // more than indent of previous line
1231 temp = TRUE;
1232 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1233 {
1234 old_pos = curwin->w_cursor;
1235 i = get_indent();
1236 while (curwin->w_cursor.lnum > 1)
1237 {
1238 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1239
1240 // ignore empty lines and lines starting with '#'.
1241 if (*ptr != '#' && *ptr != NUL)
1242 break;
1243 }
1244 if (get_indent() >= i)
1245 temp = FALSE;
1246 curwin->w_cursor = old_pos;
1247 }
1248 if (temp)
1249 shift_line(TRUE, FALSE, 1, TRUE);
1250 }
1251 }
1252
1253 // set indent of '#' always to 0
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001254 if (curwin->w_cursor.col > 0 && can_si && c == '#' && inindent(0))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001255 {
1256 // remember current indent for next line
1257 old_indent = get_indent();
1258 (void)set_indent(0, SIN_CHANGED);
1259 }
1260
1261 // Adjust ai_col, the char at this position can be deleted.
1262 if (ai_col > curwin->w_cursor.col)
1263 ai_col = curwin->w_cursor.col;
1264}
1265#endif
1266
1267/*
1268 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1269 * Keep the cursor on the same character.
1270 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1271 * type == INDENT_DEC decrease indent (for CTRL-D)
1272 * type == INDENT_SET set indent to "amount"
1273 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1274 */
1275 void
1276change_indent(
1277 int type,
1278 int amount,
1279 int round,
1280 int replaced, // replaced character, put on replace stack
1281 int call_changed_bytes) // call changed_bytes()
1282{
1283 int vcol;
1284 int last_vcol;
1285 int insstart_less; // reduction for Insstart.col
1286 int new_cursor_col;
1287 int i;
1288 char_u *ptr;
1289 int save_p_list;
1290 int start_col;
1291 colnr_T vc;
1292 colnr_T orig_col = 0; // init for GCC
1293 char_u *new_line, *orig_line = NULL; // init for GCC
1294
Bram Moolenaar24959102022-05-07 20:01:16 +01001295 // MODE_VREPLACE state needs to know what the line was like before changing
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001296 if (State & VREPLACE_FLAG)
1297 {
1298 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1299 orig_col = curwin->w_cursor.col;
1300 }
1301
1302 // for the following tricks we don't want list mode
1303 save_p_list = curwin->w_p_list;
1304 curwin->w_p_list = FALSE;
1305 vc = getvcol_nolist(&curwin->w_cursor);
1306 vcol = vc;
1307
1308 // For Replace mode we need to fix the replace stack later, which is only
1309 // possible when the cursor is in the indent. Remember the number of
1310 // characters before the cursor if it's possible.
1311 start_col = curwin->w_cursor.col;
1312
1313 // determine offset from first non-blank
1314 new_cursor_col = curwin->w_cursor.col;
1315 beginline(BL_WHITE);
1316 new_cursor_col -= curwin->w_cursor.col;
1317
1318 insstart_less = curwin->w_cursor.col;
1319
1320 // If the cursor is in the indent, compute how many screen columns the
1321 // cursor is to the left of the first non-blank.
1322 if (new_cursor_col < 0)
1323 vcol = get_indent() - vcol;
1324
1325 if (new_cursor_col > 0) // can't fix replace stack
1326 start_col = -1;
1327
1328 // Set the new indent. The cursor will be put on the first non-blank.
1329 if (type == INDENT_SET)
1330 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1331 else
1332 {
1333 int save_State = State;
1334
1335 // Avoid being called recursively.
1336 if (State & VREPLACE_FLAG)
Bram Moolenaar24959102022-05-07 20:01:16 +01001337 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001338 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1339 State = save_State;
1340 }
1341 insstart_less -= curwin->w_cursor.col;
1342
1343 // Try to put cursor on same character.
1344 // If the cursor is at or after the first non-blank in the line,
1345 // compute the cursor column relative to the column of the first
1346 // non-blank character.
1347 // If we are not in insert mode, leave the cursor on the first non-blank.
1348 // If the cursor is before the first non-blank, position it relative
1349 // to the first non-blank, counted in screen columns.
1350 if (new_cursor_col >= 0)
1351 {
1352 // When changing the indent while the cursor is touching it, reset
1353 // Insstart_col to 0.
1354 if (new_cursor_col == 0)
1355 insstart_less = MAXCOL;
1356 new_cursor_col += curwin->w_cursor.col;
1357 }
Bram Moolenaar24959102022-05-07 20:01:16 +01001358 else if (!(State & MODE_INSERT))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001359 new_cursor_col = curwin->w_cursor.col;
1360 else
1361 {
1362 // Compute the screen column where the cursor should be.
1363 vcol = get_indent() - vcol;
1364 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1365
1366 // Advance the cursor until we reach the right screen column.
1367 vcol = last_vcol = 0;
1368 new_cursor_col = -1;
1369 ptr = ml_get_curline();
1370 while (vcol <= (int)curwin->w_virtcol)
1371 {
1372 last_vcol = vcol;
1373 if (has_mbyte && new_cursor_col >= 0)
1374 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
1375 else
1376 ++new_cursor_col;
Bram Moolenaar4e889f92022-02-21 19:36:12 +00001377 if (ptr[new_cursor_col] == NUL)
1378 break;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001379 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
1380 }
1381 vcol = last_vcol;
1382
1383 // May need to insert spaces to be able to position the cursor on
1384 // the right screen column.
1385 if (vcol != (int)curwin->w_virtcol)
1386 {
1387 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1388 i = (int)curwin->w_virtcol - vcol;
1389 ptr = alloc(i + 1);
1390 if (ptr != NULL)
1391 {
1392 new_cursor_col += i;
1393 ptr[i] = NUL;
1394 while (--i >= 0)
1395 ptr[i] = ' ';
1396 ins_str(ptr);
1397 vim_free(ptr);
1398 }
1399 }
1400
1401 // When changing the indent while the cursor is in it, reset
1402 // Insstart_col to 0.
1403 insstart_less = MAXCOL;
1404 }
1405
1406 curwin->w_p_list = save_p_list;
1407
1408 if (new_cursor_col <= 0)
1409 curwin->w_cursor.col = 0;
1410 else
1411 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1412 curwin->w_set_curswant = TRUE;
1413 changed_cline_bef_curs();
1414
1415 // May have to adjust the start of the insert.
Bram Moolenaar24959102022-05-07 20:01:16 +01001416 if (State & MODE_INSERT)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001417 {
1418 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1419 {
1420 if ((int)Insstart.col <= insstart_less)
1421 Insstart.col = 0;
1422 else
1423 Insstart.col -= insstart_less;
1424 }
1425 if ((int)ai_col <= insstart_less)
1426 ai_col = 0;
1427 else
1428 ai_col -= insstart_less;
1429 }
1430
Bram Moolenaar24959102022-05-07 20:01:16 +01001431 // For MODE_REPLACE state, may have to fix the replace stack, if it's
1432 // possible. If the number of characters before the cursor decreased, need
1433 // to pop a few characters from the replace stack.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001434 // If the number of characters before the cursor increased, need to push a
1435 // few NULs onto the replace stack.
1436 if (REPLACE_NORMAL(State) && start_col >= 0)
1437 {
1438 while (start_col > (int)curwin->w_cursor.col)
1439 {
1440 replace_join(0); // remove a NUL from the replace stack
1441 --start_col;
1442 }
1443 while (start_col < (int)curwin->w_cursor.col || replaced)
1444 {
1445 replace_push(NUL);
1446 if (replaced)
1447 {
1448 replace_push(replaced);
1449 replaced = NUL;
1450 }
1451 ++start_col;
1452 }
1453 }
1454
Bram Moolenaar24959102022-05-07 20:01:16 +01001455 // For MODE_VREPLACE state, we also have to fix the replace stack. In this
1456 // case it is always possible because we backspace over the whole line and
1457 // then put it back again the way we wanted it.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001458 if (State & VREPLACE_FLAG)
1459 {
1460 // If orig_line didn't allocate, just return. At least we did the job,
1461 // even if you can't backspace.
1462 if (orig_line == NULL)
1463 return;
1464
1465 // Save new line
1466 new_line = vim_strsave(ml_get_curline());
1467 if (new_line == NULL)
1468 return;
1469
1470 // We only put back the new line up to the cursor
1471 new_line[curwin->w_cursor.col] = NUL;
1472
1473 // Put back original line
1474 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1475 curwin->w_cursor.col = orig_col;
1476
1477 // Backspace from cursor to start of line
1478 backspace_until_column(0);
1479
1480 // Insert new stuff into line again
1481 ins_bytes(new_line);
1482
1483 vim_free(new_line);
1484 }
1485}
1486
1487/*
1488 * Copy the indent from ptr to the current line (and fill to size)
1489 * Leaves the cursor on the first non-blank in the line.
1490 * Returns TRUE if the line was changed.
1491 */
1492 int
1493copy_indent(int size, char_u *src)
1494{
1495 char_u *p = NULL;
1496 char_u *line = NULL;
1497 char_u *s;
1498 int todo;
1499 int ind_len;
1500 int line_len = 0;
1501 int tab_pad;
1502 int ind_done;
1503 int round;
1504#ifdef FEAT_VARTABS
1505 int ind_col;
1506#endif
1507
1508 // Round 1: compute the number of characters needed for the indent
1509 // Round 2: copy the characters.
1510 for (round = 1; round <= 2; ++round)
1511 {
1512 todo = size;
1513 ind_len = 0;
1514 ind_done = 0;
1515#ifdef FEAT_VARTABS
1516 ind_col = 0;
1517#endif
1518 s = src;
1519
1520 // Count/copy the usable portion of the source line
1521 while (todo > 0 && VIM_ISWHITE(*s))
1522 {
1523 if (*s == TAB)
1524 {
1525#ifdef FEAT_VARTABS
1526 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1527 curbuf->b_p_vts_array);
1528#else
1529 tab_pad = (int)curbuf->b_p_ts
1530 - (ind_done % (int)curbuf->b_p_ts);
1531#endif
1532 // Stop if this tab will overshoot the target
1533 if (todo < tab_pad)
1534 break;
1535 todo -= tab_pad;
1536 ind_done += tab_pad;
1537#ifdef FEAT_VARTABS
1538 ind_col += tab_pad;
1539#endif
1540 }
1541 else
1542 {
1543 --todo;
1544 ++ind_done;
1545#ifdef FEAT_VARTABS
1546 ++ind_col;
1547#endif
1548 }
1549 ++ind_len;
1550 if (p != NULL)
1551 *p++ = *s;
1552 ++s;
1553 }
1554
1555 // Fill to next tabstop with a tab, if possible
1556#ifdef FEAT_VARTABS
1557 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1558 curbuf->b_p_vts_array);
1559#else
1560 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1561#endif
1562 if (todo >= tab_pad && !curbuf->b_p_et)
1563 {
1564 todo -= tab_pad;
1565 ++ind_len;
1566#ifdef FEAT_VARTABS
1567 ind_col += tab_pad;
1568#endif
1569 if (p != NULL)
1570 *p++ = TAB;
1571 }
1572
1573 // Add tabs required for indent
1574 if (!curbuf->b_p_et)
1575 {
1576#ifdef FEAT_VARTABS
1577 for (;;)
1578 {
1579 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1580 curbuf->b_p_vts_array);
1581 if (todo < tab_pad)
1582 break;
1583 todo -= tab_pad;
1584 ++ind_len;
1585 ind_col += tab_pad;
1586 if (p != NULL)
1587 *p++ = TAB;
1588 }
1589#else
1590 while (todo >= (int)curbuf->b_p_ts)
1591 {
1592 todo -= (int)curbuf->b_p_ts;
1593 ++ind_len;
1594 if (p != NULL)
1595 *p++ = TAB;
1596 }
1597#endif
1598 }
1599
1600 // Count/add spaces required for indent
1601 while (todo > 0)
1602 {
1603 --todo;
1604 ++ind_len;
1605 if (p != NULL)
1606 *p++ = ' ';
1607 }
1608
1609 if (p == NULL)
1610 {
1611 // Allocate memory for the result: the copied indent, new indent
1612 // and the rest of the line.
1613 line_len = (int)STRLEN(ml_get_curline()) + 1;
1614 line = alloc(ind_len + line_len);
1615 if (line == NULL)
1616 return FALSE;
1617 p = line;
1618 }
1619 }
1620
1621 // Append the original line
1622 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1623
1624 // Replace the line
1625 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1626
1627 // Put the cursor after the indent.
1628 curwin->w_cursor.col = ind_len;
1629 return TRUE;
1630}
1631
1632/*
1633 * ":retab".
1634 */
1635 void
1636ex_retab(exarg_T *eap)
1637{
1638 linenr_T lnum;
1639 int got_tab = FALSE;
1640 long num_spaces = 0;
1641 long num_tabs;
1642 long len;
1643 long col;
1644 long vcol;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001645 long start_col = 0; // For start of white-space string
1646 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001647 long old_len;
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001648 long new_len;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001649 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001650 char_u *new_line = (char_u *)1; // init to non-NULL
1651 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001652#ifdef FEAT_VARTABS
1653 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001654 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001655#else
1656 int temp;
1657 int new_ts;
1658#endif
1659 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001660 linenr_T first_line = 0; // first changed line
1661 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001662
1663 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001664 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001665
1666#ifdef FEAT_VARTABS
1667 new_ts_str = eap->arg;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02001668 if (tabstop_set(eap->arg, &new_vts_array) == FAIL)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001669 return;
1670 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1671 ++(eap->arg);
1672
1673 // This ensures that either new_vts_array and new_ts_str are freshly
1674 // allocated, or new_vts_array points to an existing array and new_ts_str
1675 // is null.
1676 if (new_vts_array == NULL)
1677 {
1678 new_vts_array = curbuf->b_p_vts_array;
1679 new_ts_str = NULL;
1680 }
1681 else
1682 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1683#else
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001684 ptr = eap->arg;
1685 new_ts = getdigits(&ptr);
1686 if (new_ts < 0 && *eap->arg == '-')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001687 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001688 emsg(_(e_argument_must_be_positive));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001689 return;
1690 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00001691 if (new_ts < 0 || new_ts > TABSTOP_MAX)
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001692 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001693 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001694 return;
1695 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001696 if (new_ts == 0)
1697 new_ts = curbuf->b_p_ts;
1698#endif
1699 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1700 {
1701 ptr = ml_get(lnum);
1702 col = 0;
1703 vcol = 0;
1704 did_undo = FALSE;
1705 for (;;)
1706 {
1707 if (VIM_ISWHITE(ptr[col]))
1708 {
1709 if (!got_tab && num_spaces == 0)
1710 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001711 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001712 start_vcol = vcol;
1713 start_col = col;
1714 }
1715 if (ptr[col] == ' ')
1716 num_spaces++;
1717 else
1718 got_tab = TRUE;
1719 }
1720 else
1721 {
1722 if (got_tab || (eap->forceit && num_spaces > 1))
1723 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001724 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001725
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001726 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001727 len = num_spaces = vcol - start_vcol;
1728 num_tabs = 0;
1729 if (!curbuf->b_p_et)
1730 {
1731#ifdef FEAT_VARTABS
1732 int t, s;
1733
1734 tabstop_fromto(start_vcol, vcol,
1735 curbuf->b_p_ts, new_vts_array, &t, &s);
1736 num_tabs = t;
1737 num_spaces = s;
1738#else
1739 temp = new_ts - (start_vcol % new_ts);
1740 if (num_spaces >= temp)
1741 {
1742 num_spaces -= temp;
1743 num_tabs++;
1744 }
1745 num_tabs += num_spaces / new_ts;
1746 num_spaces -= (num_spaces / new_ts) * new_ts;
1747#endif
1748 }
1749 if (curbuf->b_p_et || got_tab ||
1750 (num_spaces + num_tabs < len))
1751 {
1752 if (did_undo == FALSE)
1753 {
1754 did_undo = TRUE;
1755 if (u_save((linenr_T)(lnum - 1),
1756 (linenr_T)(lnum + 1)) == FAIL)
1757 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001758 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001759 break;
1760 }
1761 }
1762
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001763 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001764 len = num_spaces + num_tabs;
1765 old_len = (long)STRLEN(ptr);
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001766 new_len = old_len - col + start_col + len + 1;
Bram Moolenaar45491662022-02-12 21:59:51 +00001767 if (new_len <= 0 || new_len >= MAXCOL)
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001768 {
1769 emsg(_(e_resulting_text_too_long));
1770 break;
1771 }
1772 new_line = alloc(new_len);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001773 if (new_line == NULL)
1774 break;
1775 if (start_col > 0)
1776 mch_memmove(new_line, ptr, (size_t)start_col);
1777 mch_memmove(new_line + start_col + len,
1778 ptr + col, (size_t)(old_len - col + 1));
1779 ptr = new_line + start_col;
1780 for (col = 0; col < len; col++)
1781 ptr[col] = (col < num_tabs) ? '\t' : ' ';
Bram Moolenaar0dcd39b2021-02-03 19:44:25 +01001782 if (ml_replace(lnum, new_line, FALSE) == OK)
1783 // "new_line" may have been copied
1784 new_line = curbuf->b_ml.ml_line_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001785 if (first_line == 0)
1786 first_line = lnum;
1787 last_line = lnum;
1788 ptr = new_line;
1789 col = start_col + len;
1790 }
1791 }
1792 got_tab = FALSE;
1793 num_spaces = 0;
1794 }
1795 if (ptr[col] == NUL)
1796 break;
1797 vcol += chartabsize(ptr + col, (colnr_T)vcol);
Bram Moolenaar6e287032022-02-12 15:42:18 +00001798 if (vcol >= MAXCOL)
1799 {
1800 emsg(_(e_resulting_text_too_long));
1801 break;
1802 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001803 if (has_mbyte)
1804 col += (*mb_ptr2len)(ptr + col);
1805 else
1806 ++col;
1807 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001808 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001809 break;
1810 line_breakcheck();
1811 }
1812 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001813 emsg(_(e_interrupted));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001814
1815#ifdef FEAT_VARTABS
1816 // If a single value was given then it can be considered equal to
1817 // either the value of 'tabstop' or the value of 'vartabstop'.
1818 if (tabstop_count(curbuf->b_p_vts_array) == 0
1819 && tabstop_count(new_vts_array) == 1
1820 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001821 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001822 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001823 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001824 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001825 else
1826 redraw_curbuf_later(NOT_VALID);
1827#else
1828 if (curbuf->b_p_ts != new_ts)
1829 redraw_curbuf_later(NOT_VALID);
1830#endif
1831 if (first_line != 0)
1832 changed_lines(first_line, 0, last_line + 1, 0L);
1833
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001834 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001835
1836#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001837 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001838 {
1839 // If 'vartabstop' is in use or if the value given to retab has more
1840 // than one tabstop then update 'vartabstop'.
1841 int *old_vts_ary = curbuf->b_p_vts_array;
1842
1843 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1844 {
1845 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1846 OPT_FREE|OPT_LOCAL, 0);
1847 curbuf->b_p_vts_array = new_vts_array;
1848 vim_free(old_vts_ary);
1849 }
1850 else
1851 {
1852 // 'vartabstop' wasn't in use and a single value was given to
1853 // retab then update 'tabstop'.
1854 curbuf->b_p_ts = tabstop_first(new_vts_array);
1855 vim_free(new_vts_array);
1856 }
1857 vim_free(new_ts_str);
1858 }
1859#else
1860 curbuf->b_p_ts = new_ts;
1861#endif
1862 coladvance(curwin->w_curswant);
1863
1864 u_clearline();
1865}
1866
1867#if (defined(FEAT_CINDENT) && defined(FEAT_EVAL)) || defined(PROTO)
1868/*
1869 * Get indent level from 'indentexpr'.
1870 */
1871 int
1872get_expr_indent(void)
1873{
1874 int indent = -1;
1875 char_u *inde_copy;
1876 pos_T save_pos;
1877 colnr_T save_curswant;
1878 int save_set_curswant;
1879 int save_State;
1880 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1881 OPT_LOCAL);
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001882 sctx_T save_sctx = current_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001883
1884 // Save and restore cursor position and curswant, in case it was changed
1885 // via :normal commands
1886 save_pos = curwin->w_cursor;
1887 save_curswant = curwin->w_curswant;
1888 save_set_curswant = curwin->w_set_curswant;
1889 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1890 if (use_sandbox)
1891 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001892 ++textwinlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001893 current_sctx = curbuf->b_p_script_ctx[BV_INDE];
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001894
1895 // Need to make a copy, the 'indentexpr' option could be changed while
1896 // evaluating it.
1897 inde_copy = vim_strsave(curbuf->b_p_inde);
1898 if (inde_copy != NULL)
1899 {
1900 indent = (int)eval_to_number(inde_copy);
1901 vim_free(inde_copy);
1902 }
1903
1904 if (use_sandbox)
1905 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001906 --textwinlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001907 current_sctx = save_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001908
1909 // Restore the cursor position so that 'indentexpr' doesn't need to.
1910 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1911 // command.
1912 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001913 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001914 curwin->w_cursor = save_pos;
1915 curwin->w_curswant = save_curswant;
1916 curwin->w_set_curswant = save_set_curswant;
1917 check_cursor();
1918 State = save_State;
1919
Bram Moolenaar620c9592021-07-31 21:32:31 +02001920 // Reset did_throw, unless 'debug' has "throw" and inside a try/catch.
1921 if (did_throw && (vim_strchr(p_debug, 't') == NULL || trylevel == 0))
1922 {
1923 handle_did_throw();
1924 did_throw = FALSE;
1925 }
1926
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001927 // If there is an error, just keep the current indent.
1928 if (indent < 0)
1929 indent = get_indent();
1930
1931 return indent;
1932}
1933#endif
1934
1935#if defined(FEAT_LISP) || defined(PROTO)
1936
1937 static int
1938lisp_match(char_u *p)
1939{
1940 char_u buf[LSIZE];
1941 int len;
1942 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1943
1944 while (*word != NUL)
1945 {
1946 (void)copy_option_part(&word, buf, LSIZE, ",");
1947 len = (int)STRLEN(buf);
1948 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
1949 return TRUE;
1950 }
1951 return FALSE;
1952}
1953
1954/*
1955 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
1956 * The incompatible newer method is quite a bit better at indenting
1957 * code in lisp-like languages than the traditional one; it's still
1958 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
1959 *
1960 * TODO:
1961 * Findmatch() should be adapted for lisp, also to make showmatch
1962 * work correctly: now (v5.3) it seems all C/C++ oriented:
1963 * - it does not recognize the #\( and #\) notations as character literals
1964 * - it doesn't know about comments starting with a semicolon
1965 * - it incorrectly interprets '(' as a character literal
1966 * All this messes up get_lisp_indent in some rare cases.
1967 * Update from Sergey Khorev:
1968 * I tried to fix the first two issues.
1969 */
1970 int
1971get_lisp_indent(void)
1972{
1973 pos_T *pos, realpos, paren;
1974 int amount;
1975 char_u *that;
1976 colnr_T col;
1977 colnr_T firsttry;
1978 int parencount, quotecount;
1979 int vi_lisp;
1980
1981 // Set vi_lisp to use the vi-compatible method
1982 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
1983
1984 realpos = curwin->w_cursor;
1985 curwin->w_cursor.col = 0;
1986
1987 if ((pos = findmatch(NULL, '(')) == NULL)
1988 pos = findmatch(NULL, '[');
1989 else
1990 {
1991 paren = *pos;
1992 pos = findmatch(NULL, '[');
1993 if (pos == NULL || LT_POSP(pos, &paren))
1994 pos = &paren;
1995 }
1996 if (pos != NULL)
1997 {
1998 // Extra trick: Take the indent of the first previous non-white
1999 // line that is at the same () level.
2000 amount = -1;
2001 parencount = 0;
2002
2003 while (--curwin->w_cursor.lnum >= pos->lnum)
2004 {
2005 if (linewhite(curwin->w_cursor.lnum))
2006 continue;
2007 for (that = ml_get_curline(); *that != NUL; ++that)
2008 {
2009 if (*that == ';')
2010 {
2011 while (*(that + 1) != NUL)
2012 ++that;
2013 continue;
2014 }
2015 if (*that == '\\')
2016 {
2017 if (*(that + 1) != NUL)
2018 ++that;
2019 continue;
2020 }
2021 if (*that == '"' && *(that + 1) != NUL)
2022 {
2023 while (*++that && *that != '"')
2024 {
2025 // skipping escaped characters in the string
2026 if (*that == '\\')
2027 {
2028 if (*++that == NUL)
2029 break;
2030 if (that[1] == NUL)
2031 {
2032 ++that;
2033 break;
2034 }
2035 }
2036 }
2037 }
2038 if (*that == '(' || *that == '[')
2039 ++parencount;
2040 else if (*that == ')' || *that == ']')
2041 --parencount;
2042 }
2043 if (parencount == 0)
2044 {
2045 amount = get_indent();
2046 break;
2047 }
2048 }
2049
2050 if (amount == -1)
2051 {
2052 curwin->w_cursor.lnum = pos->lnum;
2053 curwin->w_cursor.col = pos->col;
2054 col = pos->col;
2055
2056 that = ml_get_curline();
2057
2058 if (vi_lisp && get_indent() == 0)
2059 amount = 2;
2060 else
2061 {
2062 char_u *line = that;
2063
2064 amount = 0;
2065 while (*that && col)
2066 {
2067 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
2068 col--;
2069 }
2070
2071 // Some keywords require "body" indenting rules (the
2072 // non-standard-lisp ones are Scheme special forms):
2073 //
2074 // (let ((a 1)) instead (let ((a 1))
2075 // (...)) of (...))
2076
2077 if (!vi_lisp && (*that == '(' || *that == '[')
2078 && lisp_match(that + 1))
2079 amount += 2;
2080 else
2081 {
2082 that++;
2083 amount++;
2084 firsttry = amount;
2085
2086 while (VIM_ISWHITE(*that))
2087 {
2088 amount += lbr_chartabsize(line, that, (colnr_T)amount);
2089 ++that;
2090 }
2091
2092 if (*that && *that != ';') // not a comment line
2093 {
2094 // test *that != '(' to accommodate first let/do
2095 // argument if it is more than one line
2096 if (!vi_lisp && *that != '(' && *that != '[')
2097 firsttry++;
2098
2099 parencount = 0;
2100 quotecount = 0;
2101
2102 if (vi_lisp
2103 || (*that != '"'
2104 && *that != '\''
2105 && *that != '#'
2106 && (*that < '0' || *that > '9')))
2107 {
2108 while (*that
2109 && (!VIM_ISWHITE(*that)
2110 || quotecount
2111 || parencount)
2112 && (!((*that == '(' || *that == '[')
2113 && !quotecount
2114 && !parencount
2115 && vi_lisp)))
2116 {
2117 if (*that == '"')
2118 quotecount = !quotecount;
2119 if ((*that == '(' || *that == '[')
2120 && !quotecount)
2121 ++parencount;
2122 if ((*that == ')' || *that == ']')
2123 && !quotecount)
2124 --parencount;
2125 if (*that == '\\' && *(that+1) != NUL)
2126 amount += lbr_chartabsize_adv(
2127 line, &that, (colnr_T)amount);
2128 amount += lbr_chartabsize_adv(
2129 line, &that, (colnr_T)amount);
2130 }
2131 }
2132 while (VIM_ISWHITE(*that))
2133 {
2134 amount += lbr_chartabsize(
2135 line, that, (colnr_T)amount);
2136 that++;
2137 }
2138 if (!*that || *that == ';')
2139 amount = firsttry;
2140 }
2141 }
2142 }
2143 }
2144 }
2145 else
2146 amount = 0; // no matching '(' or '[' found, use zero indent
2147
2148 curwin->w_cursor = realpos;
2149
2150 return amount;
2151}
2152#endif // FEAT_LISP
2153
2154#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
2155/*
2156 * Re-indent the current line, based on the current contents of it and the
2157 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
2158 * confused what all the part that handles Control-T is doing that I'm not.
2159 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
2160 */
2161
2162 void
2163fixthisline(int (*get_the_indent)(void))
2164{
2165 int amount = get_the_indent();
2166
2167 if (amount >= 0)
2168 {
2169 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
2170 if (linewhite(curwin->w_cursor.lnum))
2171 did_ai = TRUE; // delete the indent if the line stays empty
2172 }
2173}
2174
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002175/*
2176 * Fix indent for 'lisp' and 'cindent'.
2177 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002178 void
2179fix_indent(void)
2180{
2181 if (p_paste)
2182 return;
2183# ifdef FEAT_LISP
2184 if (curbuf->b_p_lisp && curbuf->b_p_ai)
2185 fixthisline(get_lisp_indent);
2186# endif
2187# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
2188 else
2189# endif
2190# ifdef FEAT_CINDENT
2191 if (cindent_on())
2192 do_c_expr_indent();
2193# endif
2194}
2195#endif
2196
2197#if defined(FEAT_EVAL) || defined(PROTO)
2198/*
2199 * "indent()" function
2200 */
2201 void
2202f_indent(typval_T *argvars, typval_T *rettv)
2203{
2204 linenr_T lnum;
2205
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002206 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2207 return;
2208
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002209 lnum = tv_get_lnum(argvars);
2210 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2211 rettv->vval.v_number = get_indent_lnum(lnum);
2212 else
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002213 {
2214 if (in_vim9script())
2215 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002216 rettv->vval.v_number = -1;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002217 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002218}
2219
2220/*
2221 * "lispindent(lnum)" function
2222 */
2223 void
2224f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2225{
K.Takata6e1d31e2022-02-03 13:05:32 +00002226# ifdef FEAT_LISP
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002227 pos_T pos;
2228 linenr_T lnum;
2229
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002230 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2231 return;
2232
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002233 pos = curwin->w_cursor;
2234 lnum = tv_get_lnum(argvars);
2235 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2236 {
2237 curwin->w_cursor.lnum = lnum;
2238 rettv->vval.v_number = get_lisp_indent();
2239 curwin->w_cursor = pos;
2240 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002241 else if (in_vim9script())
2242 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002243 else
K.Takata6e1d31e2022-02-03 13:05:32 +00002244# endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002245 rettv->vval.v_number = -1;
2246}
2247#endif