blob: b74bae6e698c3595008b61196f149b8551c160b8 [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 Moolenaarb7081e12021-09-04 18:47:28 +020074 if (n < 0 || n > 9999)
75 {
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;
164 int excess;
165
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...
831 if ((State & INSERT) || has_format_option(FO_Q_COMS))
832 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;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100869
870 p = wp->w_p_briopt;
871 while (*p != NUL)
872 {
873 if (STRNCMP(p, "shift:", 6) == 0
874 && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
875 {
876 p += 6;
877 bri_shift = getdigits(&p);
878 }
879 else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4]))
880 {
881 p += 4;
882 bri_min = getdigits(&p);
883 }
884 else if (STRNCMP(p, "sbr", 3) == 0)
885 {
886 p += 3;
887 bri_sbr = TRUE;
888 }
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200889 else if (STRNCMP(p, "list:", 5) == 0)
890 {
891 p += 5;
892 bri_list = getdigits(&p);
893 }
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100894 if (*p != ',' && *p != NUL)
895 return FAIL;
896 if (*p == ',')
897 ++p;
898 }
899
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100900 wp->w_briopt_shift = bri_shift;
901 wp->w_briopt_min = bri_min;
902 wp->w_briopt_sbr = bri_sbr;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200903 wp->w_briopt_list = bri_list;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100904
905 return OK;
906}
907
908/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200909 * Return appropriate space number for breakindent, taking influencing
910 * parameters into account. Window must be specified, since it is not
911 * necessarily always the current one.
912 */
913 int
914get_breakindent_win(
915 win_T *wp,
916 char_u *line) // start of the line
917{
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000918 static int prev_indent = 0; // cached indent value
919 static long prev_ts = 0L; // cached tabstop value
920 static char_u *prev_line = NULL; // cached pointer to line
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200921 static varnumber_T prev_tick = 0; // changedtick of cached value
922# ifdef FEAT_VARTABS
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000923 static int *prev_vts = NULL; // cached vartabs values
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200924# endif
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000925 static int prev_list = 0; // cached list value
926 static int prev_listopt = 0; // cached w_p_briopt_list value
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200927 int bri = 0;
928 // window width minus window margin space, i.e. what rests for text
929 const int eff_wwidth = wp->w_width
930 - ((wp->w_p_nu || wp->w_p_rnu)
931 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
932 ? number_width(wp) + 1 : 0);
933
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000934 // used cached indent, unless line, 'tabstop' or briopt_list changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200935 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
936 || prev_tick != CHANGEDTICK(wp->w_buffer)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000937 || prev_listopt != wp->w_briopt_list
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200938# ifdef FEAT_VARTABS
939 || prev_vts != wp->w_buffer->b_p_vts_array
940# endif
941 )
942 {
943 prev_line = line;
944 prev_ts = wp->w_buffer->b_p_ts;
945 prev_tick = CHANGEDTICK(wp->w_buffer);
946# ifdef FEAT_VARTABS
947 prev_vts = wp->w_buffer->b_p_vts_array;
948 prev_indent = get_indent_str_vtab(line,
949 (int)wp->w_buffer->b_p_ts,
950 wp->w_buffer->b_p_vts_array, wp->w_p_list);
951# else
952 prev_indent = get_indent_str(line,
953 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
954# endif
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000955 prev_listopt = wp->w_briopt_list;
956 // add additional indent for numbered lists
957 if (wp->w_briopt_list != 0)
958 {
959 regmatch_T regmatch;
960
961 regmatch.regprog = vim_regcomp(curbuf->b_p_flp,
962 RE_MAGIC + RE_STRING + RE_AUTO + RE_STRICT);
963
964 if (regmatch.regprog != NULL)
965 {
966 regmatch.rm_ic = FALSE;
967 if (vim_regexec(&regmatch, line, 0))
968 {
969 if (wp->w_briopt_list > 0)
970 prev_list = wp->w_briopt_list;
971 else
972 prev_list = (*regmatch.endp - *regmatch.startp);
973 }
974 vim_regfree(regmatch.regprog);
975 }
976 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200977 }
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100978 bri = prev_indent + wp->w_briopt_shift;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200979
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200980 // Add offset for number column, if 'n' is in 'cpoptions'
981 bri += win_col_off2(wp);
982
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200983 // add additional indent for numbered lists
Maxim Kimf674b352021-07-22 11:46:59 +0200984 if (wp->w_briopt_list != 0)
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200985 {
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000986 if (wp->w_briopt_list > 0)
987 bri += prev_list;
988 else
989 bri = prev_list;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200990 }
991
Maxim Kimf674b352021-07-22 11:46:59 +0200992 // indent minus the length of the showbreak string
993 if (wp->w_briopt_sbr)
994 bri -= vim_strsize(get_showbreak_value(wp));
995
996
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200997 // never indent past left window margin
998 if (bri < 0)
999 bri = 0;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001000
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001001 // always leave at least bri_min characters on the left,
1002 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001003 else if (bri > eff_wwidth - wp->w_briopt_min)
1004 bri = (eff_wwidth - wp->w_briopt_min < 0)
1005 ? 0 : eff_wwidth - wp->w_briopt_min;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001006
1007 return bri;
1008}
1009#endif
1010
1011/*
1012 * When extra == 0: Return TRUE if the cursor is before or on the first
1013 * non-blank in the line.
1014 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1015 * the line.
1016 */
1017 int
1018inindent(int extra)
1019{
1020 char_u *ptr;
1021 colnr_T col;
1022
1023 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
1024 ++ptr;
1025 if (col >= curwin->w_cursor.col + extra)
1026 return TRUE;
1027 else
1028 return FALSE;
1029}
1030
1031#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
1032/*
1033 * op_reindent - handle reindenting a block of lines.
1034 */
1035 void
1036op_reindent(oparg_T *oap, int (*how)(void))
1037{
1038 long i;
1039 char_u *l;
1040 int amount;
1041 linenr_T first_changed = 0;
1042 linenr_T last_changed = 0;
1043 linenr_T start_lnum = curwin->w_cursor.lnum;
1044
1045 // Don't even try when 'modifiable' is off.
1046 if (!curbuf->b_p_ma)
1047 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001048 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001049 return;
1050 }
1051
1052 for (i = oap->line_count; --i >= 0 && !got_int; )
1053 {
1054 // it's a slow thing to do, so give feedback so there's no worry that
1055 // the computer's just hung.
1056
1057 if (i > 1
1058 && (i % 50 == 0 || i == oap->line_count - 1)
1059 && oap->line_count > p_report)
1060 smsg(_("%ld lines to indent... "), i);
1061
1062 // Be vi-compatible: For lisp indenting the first line is not
1063 // indented, unless there is only one line.
1064# ifdef FEAT_LISP
1065 if (i != oap->line_count - 1 || oap->line_count == 1
1066 || how != get_lisp_indent)
1067# endif
1068 {
1069 l = skipwhite(ml_get_curline());
1070 if (*l == NUL) // empty or blank line
1071 amount = 0;
1072 else
1073 amount = how(); // get the indent for this line
1074
1075 if (amount >= 0 && set_indent(amount, SIN_UNDO))
1076 {
1077 // did change the indent, call changed_lines() later
1078 if (first_changed == 0)
1079 first_changed = curwin->w_cursor.lnum;
1080 last_changed = curwin->w_cursor.lnum;
1081 }
1082 }
1083 ++curwin->w_cursor.lnum;
1084 curwin->w_cursor.col = 0; // make sure it's valid
1085 }
1086
1087 // put cursor on first non-blank of indented line
1088 curwin->w_cursor.lnum = start_lnum;
1089 beginline(BL_SOL | BL_FIX);
1090
1091 // Mark changed lines so that they will be redrawn. When Visual
1092 // highlighting was present, need to continue until the last line. When
1093 // there is no change still need to remove the Visual highlighting.
1094 if (last_changed != 0)
1095 changed_lines(first_changed, 0,
1096 oap->is_VIsual ? start_lnum + oap->line_count :
1097 last_changed + 1, 0L);
1098 else if (oap->is_VIsual)
1099 redraw_curbuf_later(INVERTED);
1100
1101 if (oap->line_count > p_report)
1102 {
1103 i = oap->line_count - (i + 1);
1104 smsg(NGETTEXT("%ld line indented ",
1105 "%ld lines indented ", i), i);
1106 }
Bram Moolenaare1004402020-10-24 20:49:43 +02001107 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001108 {
1109 // set '[ and '] marks
1110 curbuf->b_op_start = oap->start;
1111 curbuf->b_op_end = oap->end;
1112 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001113}
1114#endif // defined(FEAT_LISP) || defined(FEAT_CINDENT)
1115
1116#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
1117/*
1118 * Return TRUE if lines starting with '#' should be left aligned.
1119 */
1120 int
1121preprocs_left(void)
1122{
1123 return
1124# ifdef FEAT_SMARTINDENT
1125# ifdef FEAT_CINDENT
1126 (curbuf->b_p_si && !curbuf->b_p_cin) ||
1127# else
1128 curbuf->b_p_si
1129# endif
1130# endif
1131# ifdef FEAT_CINDENT
1132 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1133 && curbuf->b_ind_hash_comment == 0)
1134# endif
1135 ;
1136}
1137#endif
1138
1139#ifdef FEAT_SMARTINDENT
1140/*
1141 * Try to do some very smart auto-indenting.
1142 * Used when inserting a "normal" character.
1143 */
1144 void
1145ins_try_si(int c)
1146{
1147 pos_T *pos, old_pos;
1148 char_u *ptr;
1149 int i;
1150 int temp;
1151
1152 // do some very smart indenting when entering '{' or '}'
1153 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
1154 {
1155 // for '}' set indent equal to indent of line containing matching '{'
1156 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1157 {
1158 old_pos = curwin->w_cursor;
1159 // If the matching '{' has a ')' immediately before it (ignoring
1160 // white-space), then line up with the start of the line
1161 // containing the matching '(' if there is one. This handles the
1162 // case where an "if (..\n..) {" statement continues over multiple
1163 // lines -- webb
1164 ptr = ml_get(pos->lnum);
1165 i = pos->col;
1166 if (i > 0) // skip blanks before '{'
1167 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1168 ;
1169 curwin->w_cursor.lnum = pos->lnum;
1170 curwin->w_cursor.col = i;
1171 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1172 curwin->w_cursor = *pos;
1173 i = get_indent();
1174 curwin->w_cursor = old_pos;
1175 if (State & VREPLACE_FLAG)
1176 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1177 else
1178 (void)set_indent(i, SIN_CHANGED);
1179 }
1180 else if (curwin->w_cursor.col > 0)
1181 {
1182 // when inserting '{' after "O" reduce indent, but not
1183 // more than indent of previous line
1184 temp = TRUE;
1185 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1186 {
1187 old_pos = curwin->w_cursor;
1188 i = get_indent();
1189 while (curwin->w_cursor.lnum > 1)
1190 {
1191 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1192
1193 // ignore empty lines and lines starting with '#'.
1194 if (*ptr != '#' && *ptr != NUL)
1195 break;
1196 }
1197 if (get_indent() >= i)
1198 temp = FALSE;
1199 curwin->w_cursor = old_pos;
1200 }
1201 if (temp)
1202 shift_line(TRUE, FALSE, 1, TRUE);
1203 }
1204 }
1205
1206 // set indent of '#' always to 0
1207 if (curwin->w_cursor.col > 0 && can_si && c == '#')
1208 {
1209 // remember current indent for next line
1210 old_indent = get_indent();
1211 (void)set_indent(0, SIN_CHANGED);
1212 }
1213
1214 // Adjust ai_col, the char at this position can be deleted.
1215 if (ai_col > curwin->w_cursor.col)
1216 ai_col = curwin->w_cursor.col;
1217}
1218#endif
1219
1220/*
1221 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1222 * Keep the cursor on the same character.
1223 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1224 * type == INDENT_DEC decrease indent (for CTRL-D)
1225 * type == INDENT_SET set indent to "amount"
1226 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1227 */
1228 void
1229change_indent(
1230 int type,
1231 int amount,
1232 int round,
1233 int replaced, // replaced character, put on replace stack
1234 int call_changed_bytes) // call changed_bytes()
1235{
1236 int vcol;
1237 int last_vcol;
1238 int insstart_less; // reduction for Insstart.col
1239 int new_cursor_col;
1240 int i;
1241 char_u *ptr;
1242 int save_p_list;
1243 int start_col;
1244 colnr_T vc;
1245 colnr_T orig_col = 0; // init for GCC
1246 char_u *new_line, *orig_line = NULL; // init for GCC
1247
1248 // VREPLACE mode needs to know what the line was like before changing
1249 if (State & VREPLACE_FLAG)
1250 {
1251 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1252 orig_col = curwin->w_cursor.col;
1253 }
1254
1255 // for the following tricks we don't want list mode
1256 save_p_list = curwin->w_p_list;
1257 curwin->w_p_list = FALSE;
1258 vc = getvcol_nolist(&curwin->w_cursor);
1259 vcol = vc;
1260
1261 // For Replace mode we need to fix the replace stack later, which is only
1262 // possible when the cursor is in the indent. Remember the number of
1263 // characters before the cursor if it's possible.
1264 start_col = curwin->w_cursor.col;
1265
1266 // determine offset from first non-blank
1267 new_cursor_col = curwin->w_cursor.col;
1268 beginline(BL_WHITE);
1269 new_cursor_col -= curwin->w_cursor.col;
1270
1271 insstart_less = curwin->w_cursor.col;
1272
1273 // If the cursor is in the indent, compute how many screen columns the
1274 // cursor is to the left of the first non-blank.
1275 if (new_cursor_col < 0)
1276 vcol = get_indent() - vcol;
1277
1278 if (new_cursor_col > 0) // can't fix replace stack
1279 start_col = -1;
1280
1281 // Set the new indent. The cursor will be put on the first non-blank.
1282 if (type == INDENT_SET)
1283 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1284 else
1285 {
1286 int save_State = State;
1287
1288 // Avoid being called recursively.
1289 if (State & VREPLACE_FLAG)
1290 State = INSERT;
1291 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1292 State = save_State;
1293 }
1294 insstart_less -= curwin->w_cursor.col;
1295
1296 // Try to put cursor on same character.
1297 // If the cursor is at or after the first non-blank in the line,
1298 // compute the cursor column relative to the column of the first
1299 // non-blank character.
1300 // If we are not in insert mode, leave the cursor on the first non-blank.
1301 // If the cursor is before the first non-blank, position it relative
1302 // to the first non-blank, counted in screen columns.
1303 if (new_cursor_col >= 0)
1304 {
1305 // When changing the indent while the cursor is touching it, reset
1306 // Insstart_col to 0.
1307 if (new_cursor_col == 0)
1308 insstart_less = MAXCOL;
1309 new_cursor_col += curwin->w_cursor.col;
1310 }
1311 else if (!(State & INSERT))
1312 new_cursor_col = curwin->w_cursor.col;
1313 else
1314 {
1315 // Compute the screen column where the cursor should be.
1316 vcol = get_indent() - vcol;
1317 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1318
1319 // Advance the cursor until we reach the right screen column.
1320 vcol = last_vcol = 0;
1321 new_cursor_col = -1;
1322 ptr = ml_get_curline();
1323 while (vcol <= (int)curwin->w_virtcol)
1324 {
1325 last_vcol = vcol;
1326 if (has_mbyte && new_cursor_col >= 0)
1327 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
1328 else
1329 ++new_cursor_col;
1330 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
1331 }
1332 vcol = last_vcol;
1333
1334 // May need to insert spaces to be able to position the cursor on
1335 // the right screen column.
1336 if (vcol != (int)curwin->w_virtcol)
1337 {
1338 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1339 i = (int)curwin->w_virtcol - vcol;
1340 ptr = alloc(i + 1);
1341 if (ptr != NULL)
1342 {
1343 new_cursor_col += i;
1344 ptr[i] = NUL;
1345 while (--i >= 0)
1346 ptr[i] = ' ';
1347 ins_str(ptr);
1348 vim_free(ptr);
1349 }
1350 }
1351
1352 // When changing the indent while the cursor is in it, reset
1353 // Insstart_col to 0.
1354 insstart_less = MAXCOL;
1355 }
1356
1357 curwin->w_p_list = save_p_list;
1358
1359 if (new_cursor_col <= 0)
1360 curwin->w_cursor.col = 0;
1361 else
1362 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1363 curwin->w_set_curswant = TRUE;
1364 changed_cline_bef_curs();
1365
1366 // May have to adjust the start of the insert.
1367 if (State & INSERT)
1368 {
1369 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1370 {
1371 if ((int)Insstart.col <= insstart_less)
1372 Insstart.col = 0;
1373 else
1374 Insstart.col -= insstart_less;
1375 }
1376 if ((int)ai_col <= insstart_less)
1377 ai_col = 0;
1378 else
1379 ai_col -= insstart_less;
1380 }
1381
1382 // For REPLACE mode, may have to fix the replace stack, if it's possible.
1383 // If the number of characters before the cursor decreased, need to pop a
1384 // few characters from the replace stack.
1385 // If the number of characters before the cursor increased, need to push a
1386 // few NULs onto the replace stack.
1387 if (REPLACE_NORMAL(State) && start_col >= 0)
1388 {
1389 while (start_col > (int)curwin->w_cursor.col)
1390 {
1391 replace_join(0); // remove a NUL from the replace stack
1392 --start_col;
1393 }
1394 while (start_col < (int)curwin->w_cursor.col || replaced)
1395 {
1396 replace_push(NUL);
1397 if (replaced)
1398 {
1399 replace_push(replaced);
1400 replaced = NUL;
1401 }
1402 ++start_col;
1403 }
1404 }
1405
1406 // For VREPLACE mode, we also have to fix the replace stack. In this case
1407 // it is always possible because we backspace over the whole line and then
1408 // put it back again the way we wanted it.
1409 if (State & VREPLACE_FLAG)
1410 {
1411 // If orig_line didn't allocate, just return. At least we did the job,
1412 // even if you can't backspace.
1413 if (orig_line == NULL)
1414 return;
1415
1416 // Save new line
1417 new_line = vim_strsave(ml_get_curline());
1418 if (new_line == NULL)
1419 return;
1420
1421 // We only put back the new line up to the cursor
1422 new_line[curwin->w_cursor.col] = NUL;
1423
1424 // Put back original line
1425 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1426 curwin->w_cursor.col = orig_col;
1427
1428 // Backspace from cursor to start of line
1429 backspace_until_column(0);
1430
1431 // Insert new stuff into line again
1432 ins_bytes(new_line);
1433
1434 vim_free(new_line);
1435 }
1436}
1437
1438/*
1439 * Copy the indent from ptr to the current line (and fill to size)
1440 * Leaves the cursor on the first non-blank in the line.
1441 * Returns TRUE if the line was changed.
1442 */
1443 int
1444copy_indent(int size, char_u *src)
1445{
1446 char_u *p = NULL;
1447 char_u *line = NULL;
1448 char_u *s;
1449 int todo;
1450 int ind_len;
1451 int line_len = 0;
1452 int tab_pad;
1453 int ind_done;
1454 int round;
1455#ifdef FEAT_VARTABS
1456 int ind_col;
1457#endif
1458
1459 // Round 1: compute the number of characters needed for the indent
1460 // Round 2: copy the characters.
1461 for (round = 1; round <= 2; ++round)
1462 {
1463 todo = size;
1464 ind_len = 0;
1465 ind_done = 0;
1466#ifdef FEAT_VARTABS
1467 ind_col = 0;
1468#endif
1469 s = src;
1470
1471 // Count/copy the usable portion of the source line
1472 while (todo > 0 && VIM_ISWHITE(*s))
1473 {
1474 if (*s == TAB)
1475 {
1476#ifdef FEAT_VARTABS
1477 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1478 curbuf->b_p_vts_array);
1479#else
1480 tab_pad = (int)curbuf->b_p_ts
1481 - (ind_done % (int)curbuf->b_p_ts);
1482#endif
1483 // Stop if this tab will overshoot the target
1484 if (todo < tab_pad)
1485 break;
1486 todo -= tab_pad;
1487 ind_done += tab_pad;
1488#ifdef FEAT_VARTABS
1489 ind_col += tab_pad;
1490#endif
1491 }
1492 else
1493 {
1494 --todo;
1495 ++ind_done;
1496#ifdef FEAT_VARTABS
1497 ++ind_col;
1498#endif
1499 }
1500 ++ind_len;
1501 if (p != NULL)
1502 *p++ = *s;
1503 ++s;
1504 }
1505
1506 // Fill to next tabstop with a tab, if possible
1507#ifdef FEAT_VARTABS
1508 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1509 curbuf->b_p_vts_array);
1510#else
1511 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1512#endif
1513 if (todo >= tab_pad && !curbuf->b_p_et)
1514 {
1515 todo -= tab_pad;
1516 ++ind_len;
1517#ifdef FEAT_VARTABS
1518 ind_col += tab_pad;
1519#endif
1520 if (p != NULL)
1521 *p++ = TAB;
1522 }
1523
1524 // Add tabs required for indent
1525 if (!curbuf->b_p_et)
1526 {
1527#ifdef FEAT_VARTABS
1528 for (;;)
1529 {
1530 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1531 curbuf->b_p_vts_array);
1532 if (todo < tab_pad)
1533 break;
1534 todo -= tab_pad;
1535 ++ind_len;
1536 ind_col += tab_pad;
1537 if (p != NULL)
1538 *p++ = TAB;
1539 }
1540#else
1541 while (todo >= (int)curbuf->b_p_ts)
1542 {
1543 todo -= (int)curbuf->b_p_ts;
1544 ++ind_len;
1545 if (p != NULL)
1546 *p++ = TAB;
1547 }
1548#endif
1549 }
1550
1551 // Count/add spaces required for indent
1552 while (todo > 0)
1553 {
1554 --todo;
1555 ++ind_len;
1556 if (p != NULL)
1557 *p++ = ' ';
1558 }
1559
1560 if (p == NULL)
1561 {
1562 // Allocate memory for the result: the copied indent, new indent
1563 // and the rest of the line.
1564 line_len = (int)STRLEN(ml_get_curline()) + 1;
1565 line = alloc(ind_len + line_len);
1566 if (line == NULL)
1567 return FALSE;
1568 p = line;
1569 }
1570 }
1571
1572 // Append the original line
1573 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1574
1575 // Replace the line
1576 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1577
1578 // Put the cursor after the indent.
1579 curwin->w_cursor.col = ind_len;
1580 return TRUE;
1581}
1582
1583/*
1584 * ":retab".
1585 */
1586 void
1587ex_retab(exarg_T *eap)
1588{
1589 linenr_T lnum;
1590 int got_tab = FALSE;
1591 long num_spaces = 0;
1592 long num_tabs;
1593 long len;
1594 long col;
1595 long vcol;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001596 long start_col = 0; // For start of white-space string
1597 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001598 long old_len;
1599 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001600 char_u *new_line = (char_u *)1; // init to non-NULL
1601 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001602#ifdef FEAT_VARTABS
1603 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001604 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001605#else
1606 int temp;
1607 int new_ts;
1608#endif
1609 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001610 linenr_T first_line = 0; // first changed line
1611 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001612
1613 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001614 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001615
1616#ifdef FEAT_VARTABS
1617 new_ts_str = eap->arg;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02001618 if (tabstop_set(eap->arg, &new_vts_array) == FAIL)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001619 return;
1620 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1621 ++(eap->arg);
1622
1623 // This ensures that either new_vts_array and new_ts_str are freshly
1624 // allocated, or new_vts_array points to an existing array and new_ts_str
1625 // is null.
1626 if (new_vts_array == NULL)
1627 {
1628 new_vts_array = curbuf->b_p_vts_array;
1629 new_ts_str = NULL;
1630 }
1631 else
1632 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1633#else
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001634 ptr = eap->arg;
1635 new_ts = getdigits(&ptr);
1636 if (new_ts < 0 && *eap->arg == '-')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001637 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001638 emsg(_(e_argument_must_be_positive));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001639 return;
1640 }
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001641 if (new_ts < 0 || new_ts > 9999)
1642 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001643 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001644 return;
1645 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001646 if (new_ts == 0)
1647 new_ts = curbuf->b_p_ts;
1648#endif
1649 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1650 {
1651 ptr = ml_get(lnum);
1652 col = 0;
1653 vcol = 0;
1654 did_undo = FALSE;
1655 for (;;)
1656 {
1657 if (VIM_ISWHITE(ptr[col]))
1658 {
1659 if (!got_tab && num_spaces == 0)
1660 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001661 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001662 start_vcol = vcol;
1663 start_col = col;
1664 }
1665 if (ptr[col] == ' ')
1666 num_spaces++;
1667 else
1668 got_tab = TRUE;
1669 }
1670 else
1671 {
1672 if (got_tab || (eap->forceit && num_spaces > 1))
1673 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001674 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001675
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001676 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001677 len = num_spaces = vcol - start_vcol;
1678 num_tabs = 0;
1679 if (!curbuf->b_p_et)
1680 {
1681#ifdef FEAT_VARTABS
1682 int t, s;
1683
1684 tabstop_fromto(start_vcol, vcol,
1685 curbuf->b_p_ts, new_vts_array, &t, &s);
1686 num_tabs = t;
1687 num_spaces = s;
1688#else
1689 temp = new_ts - (start_vcol % new_ts);
1690 if (num_spaces >= temp)
1691 {
1692 num_spaces -= temp;
1693 num_tabs++;
1694 }
1695 num_tabs += num_spaces / new_ts;
1696 num_spaces -= (num_spaces / new_ts) * new_ts;
1697#endif
1698 }
1699 if (curbuf->b_p_et || got_tab ||
1700 (num_spaces + num_tabs < len))
1701 {
1702 if (did_undo == FALSE)
1703 {
1704 did_undo = TRUE;
1705 if (u_save((linenr_T)(lnum - 1),
1706 (linenr_T)(lnum + 1)) == FAIL)
1707 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001708 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001709 break;
1710 }
1711 }
1712
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001713 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001714 len = num_spaces + num_tabs;
1715 old_len = (long)STRLEN(ptr);
1716 new_line = alloc(old_len - col + start_col + len + 1);
1717 if (new_line == NULL)
1718 break;
1719 if (start_col > 0)
1720 mch_memmove(new_line, ptr, (size_t)start_col);
1721 mch_memmove(new_line + start_col + len,
1722 ptr + col, (size_t)(old_len - col + 1));
1723 ptr = new_line + start_col;
1724 for (col = 0; col < len; col++)
1725 ptr[col] = (col < num_tabs) ? '\t' : ' ';
Bram Moolenaar0dcd39b2021-02-03 19:44:25 +01001726 if (ml_replace(lnum, new_line, FALSE) == OK)
1727 // "new_line" may have been copied
1728 new_line = curbuf->b_ml.ml_line_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001729 if (first_line == 0)
1730 first_line = lnum;
1731 last_line = lnum;
1732 ptr = new_line;
1733 col = start_col + len;
1734 }
1735 }
1736 got_tab = FALSE;
1737 num_spaces = 0;
1738 }
1739 if (ptr[col] == NUL)
1740 break;
1741 vcol += chartabsize(ptr + col, (colnr_T)vcol);
1742 if (has_mbyte)
1743 col += (*mb_ptr2len)(ptr + col);
1744 else
1745 ++col;
1746 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001747 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001748 break;
1749 line_breakcheck();
1750 }
1751 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001752 emsg(_(e_interrupted));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001753
1754#ifdef FEAT_VARTABS
1755 // If a single value was given then it can be considered equal to
1756 // either the value of 'tabstop' or the value of 'vartabstop'.
1757 if (tabstop_count(curbuf->b_p_vts_array) == 0
1758 && tabstop_count(new_vts_array) == 1
1759 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001760 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001761 else if (tabstop_count(curbuf->b_p_vts_array) > 0
1762 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001763 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001764 else
1765 redraw_curbuf_later(NOT_VALID);
1766#else
1767 if (curbuf->b_p_ts != new_ts)
1768 redraw_curbuf_later(NOT_VALID);
1769#endif
1770 if (first_line != 0)
1771 changed_lines(first_line, 0, last_line + 1, 0L);
1772
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001773 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001774
1775#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001776 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001777 {
1778 // If 'vartabstop' is in use or if the value given to retab has more
1779 // than one tabstop then update 'vartabstop'.
1780 int *old_vts_ary = curbuf->b_p_vts_array;
1781
1782 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1783 {
1784 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1785 OPT_FREE|OPT_LOCAL, 0);
1786 curbuf->b_p_vts_array = new_vts_array;
1787 vim_free(old_vts_ary);
1788 }
1789 else
1790 {
1791 // 'vartabstop' wasn't in use and a single value was given to
1792 // retab then update 'tabstop'.
1793 curbuf->b_p_ts = tabstop_first(new_vts_array);
1794 vim_free(new_vts_array);
1795 }
1796 vim_free(new_ts_str);
1797 }
1798#else
1799 curbuf->b_p_ts = new_ts;
1800#endif
1801 coladvance(curwin->w_curswant);
1802
1803 u_clearline();
1804}
1805
1806#if (defined(FEAT_CINDENT) && defined(FEAT_EVAL)) || defined(PROTO)
1807/*
1808 * Get indent level from 'indentexpr'.
1809 */
1810 int
1811get_expr_indent(void)
1812{
1813 int indent = -1;
1814 char_u *inde_copy;
1815 pos_T save_pos;
1816 colnr_T save_curswant;
1817 int save_set_curswant;
1818 int save_State;
1819 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1820 OPT_LOCAL);
1821
1822 // Save and restore cursor position and curswant, in case it was changed
1823 // via :normal commands
1824 save_pos = curwin->w_cursor;
1825 save_curswant = curwin->w_curswant;
1826 save_set_curswant = curwin->w_set_curswant;
1827 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1828 if (use_sandbox)
1829 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001830 ++textwinlock;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001831
1832 // Need to make a copy, the 'indentexpr' option could be changed while
1833 // evaluating it.
1834 inde_copy = vim_strsave(curbuf->b_p_inde);
1835 if (inde_copy != NULL)
1836 {
1837 indent = (int)eval_to_number(inde_copy);
1838 vim_free(inde_copy);
1839 }
1840
1841 if (use_sandbox)
1842 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001843 --textwinlock;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001844
1845 // Restore the cursor position so that 'indentexpr' doesn't need to.
1846 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1847 // command.
1848 save_State = State;
1849 State = INSERT;
1850 curwin->w_cursor = save_pos;
1851 curwin->w_curswant = save_curswant;
1852 curwin->w_set_curswant = save_set_curswant;
1853 check_cursor();
1854 State = save_State;
1855
Bram Moolenaar620c9592021-07-31 21:32:31 +02001856 // Reset did_throw, unless 'debug' has "throw" and inside a try/catch.
1857 if (did_throw && (vim_strchr(p_debug, 't') == NULL || trylevel == 0))
1858 {
1859 handle_did_throw();
1860 did_throw = FALSE;
1861 }
1862
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001863 // If there is an error, just keep the current indent.
1864 if (indent < 0)
1865 indent = get_indent();
1866
1867 return indent;
1868}
1869#endif
1870
1871#if defined(FEAT_LISP) || defined(PROTO)
1872
1873 static int
1874lisp_match(char_u *p)
1875{
1876 char_u buf[LSIZE];
1877 int len;
1878 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1879
1880 while (*word != NUL)
1881 {
1882 (void)copy_option_part(&word, buf, LSIZE, ",");
1883 len = (int)STRLEN(buf);
1884 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
1885 return TRUE;
1886 }
1887 return FALSE;
1888}
1889
1890/*
1891 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
1892 * The incompatible newer method is quite a bit better at indenting
1893 * code in lisp-like languages than the traditional one; it's still
1894 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
1895 *
1896 * TODO:
1897 * Findmatch() should be adapted for lisp, also to make showmatch
1898 * work correctly: now (v5.3) it seems all C/C++ oriented:
1899 * - it does not recognize the #\( and #\) notations as character literals
1900 * - it doesn't know about comments starting with a semicolon
1901 * - it incorrectly interprets '(' as a character literal
1902 * All this messes up get_lisp_indent in some rare cases.
1903 * Update from Sergey Khorev:
1904 * I tried to fix the first two issues.
1905 */
1906 int
1907get_lisp_indent(void)
1908{
1909 pos_T *pos, realpos, paren;
1910 int amount;
1911 char_u *that;
1912 colnr_T col;
1913 colnr_T firsttry;
1914 int parencount, quotecount;
1915 int vi_lisp;
1916
1917 // Set vi_lisp to use the vi-compatible method
1918 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
1919
1920 realpos = curwin->w_cursor;
1921 curwin->w_cursor.col = 0;
1922
1923 if ((pos = findmatch(NULL, '(')) == NULL)
1924 pos = findmatch(NULL, '[');
1925 else
1926 {
1927 paren = *pos;
1928 pos = findmatch(NULL, '[');
1929 if (pos == NULL || LT_POSP(pos, &paren))
1930 pos = &paren;
1931 }
1932 if (pos != NULL)
1933 {
1934 // Extra trick: Take the indent of the first previous non-white
1935 // line that is at the same () level.
1936 amount = -1;
1937 parencount = 0;
1938
1939 while (--curwin->w_cursor.lnum >= pos->lnum)
1940 {
1941 if (linewhite(curwin->w_cursor.lnum))
1942 continue;
1943 for (that = ml_get_curline(); *that != NUL; ++that)
1944 {
1945 if (*that == ';')
1946 {
1947 while (*(that + 1) != NUL)
1948 ++that;
1949 continue;
1950 }
1951 if (*that == '\\')
1952 {
1953 if (*(that + 1) != NUL)
1954 ++that;
1955 continue;
1956 }
1957 if (*that == '"' && *(that + 1) != NUL)
1958 {
1959 while (*++that && *that != '"')
1960 {
1961 // skipping escaped characters in the string
1962 if (*that == '\\')
1963 {
1964 if (*++that == NUL)
1965 break;
1966 if (that[1] == NUL)
1967 {
1968 ++that;
1969 break;
1970 }
1971 }
1972 }
1973 }
1974 if (*that == '(' || *that == '[')
1975 ++parencount;
1976 else if (*that == ')' || *that == ']')
1977 --parencount;
1978 }
1979 if (parencount == 0)
1980 {
1981 amount = get_indent();
1982 break;
1983 }
1984 }
1985
1986 if (amount == -1)
1987 {
1988 curwin->w_cursor.lnum = pos->lnum;
1989 curwin->w_cursor.col = pos->col;
1990 col = pos->col;
1991
1992 that = ml_get_curline();
1993
1994 if (vi_lisp && get_indent() == 0)
1995 amount = 2;
1996 else
1997 {
1998 char_u *line = that;
1999
2000 amount = 0;
2001 while (*that && col)
2002 {
2003 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
2004 col--;
2005 }
2006
2007 // Some keywords require "body" indenting rules (the
2008 // non-standard-lisp ones are Scheme special forms):
2009 //
2010 // (let ((a 1)) instead (let ((a 1))
2011 // (...)) of (...))
2012
2013 if (!vi_lisp && (*that == '(' || *that == '[')
2014 && lisp_match(that + 1))
2015 amount += 2;
2016 else
2017 {
2018 that++;
2019 amount++;
2020 firsttry = amount;
2021
2022 while (VIM_ISWHITE(*that))
2023 {
2024 amount += lbr_chartabsize(line, that, (colnr_T)amount);
2025 ++that;
2026 }
2027
2028 if (*that && *that != ';') // not a comment line
2029 {
2030 // test *that != '(' to accommodate first let/do
2031 // argument if it is more than one line
2032 if (!vi_lisp && *that != '(' && *that != '[')
2033 firsttry++;
2034
2035 parencount = 0;
2036 quotecount = 0;
2037
2038 if (vi_lisp
2039 || (*that != '"'
2040 && *that != '\''
2041 && *that != '#'
2042 && (*that < '0' || *that > '9')))
2043 {
2044 while (*that
2045 && (!VIM_ISWHITE(*that)
2046 || quotecount
2047 || parencount)
2048 && (!((*that == '(' || *that == '[')
2049 && !quotecount
2050 && !parencount
2051 && vi_lisp)))
2052 {
2053 if (*that == '"')
2054 quotecount = !quotecount;
2055 if ((*that == '(' || *that == '[')
2056 && !quotecount)
2057 ++parencount;
2058 if ((*that == ')' || *that == ']')
2059 && !quotecount)
2060 --parencount;
2061 if (*that == '\\' && *(that+1) != NUL)
2062 amount += lbr_chartabsize_adv(
2063 line, &that, (colnr_T)amount);
2064 amount += lbr_chartabsize_adv(
2065 line, &that, (colnr_T)amount);
2066 }
2067 }
2068 while (VIM_ISWHITE(*that))
2069 {
2070 amount += lbr_chartabsize(
2071 line, that, (colnr_T)amount);
2072 that++;
2073 }
2074 if (!*that || *that == ';')
2075 amount = firsttry;
2076 }
2077 }
2078 }
2079 }
2080 }
2081 else
2082 amount = 0; // no matching '(' or '[' found, use zero indent
2083
2084 curwin->w_cursor = realpos;
2085
2086 return amount;
2087}
2088#endif // FEAT_LISP
2089
2090#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
2091/*
2092 * Re-indent the current line, based on the current contents of it and the
2093 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
2094 * confused what all the part that handles Control-T is doing that I'm not.
2095 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
2096 */
2097
2098 void
2099fixthisline(int (*get_the_indent)(void))
2100{
2101 int amount = get_the_indent();
2102
2103 if (amount >= 0)
2104 {
2105 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
2106 if (linewhite(curwin->w_cursor.lnum))
2107 did_ai = TRUE; // delete the indent if the line stays empty
2108 }
2109}
2110
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002111/*
2112 * Fix indent for 'lisp' and 'cindent'.
2113 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002114 void
2115fix_indent(void)
2116{
2117 if (p_paste)
2118 return;
2119# ifdef FEAT_LISP
2120 if (curbuf->b_p_lisp && curbuf->b_p_ai)
2121 fixthisline(get_lisp_indent);
2122# endif
2123# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
2124 else
2125# endif
2126# ifdef FEAT_CINDENT
2127 if (cindent_on())
2128 do_c_expr_indent();
2129# endif
2130}
2131#endif
2132
2133#if defined(FEAT_EVAL) || defined(PROTO)
2134/*
2135 * "indent()" function
2136 */
2137 void
2138f_indent(typval_T *argvars, typval_T *rettv)
2139{
2140 linenr_T lnum;
2141
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002142 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2143 return;
2144
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002145 lnum = tv_get_lnum(argvars);
2146 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2147 rettv->vval.v_number = get_indent_lnum(lnum);
2148 else
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002149 {
2150 if (in_vim9script())
2151 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002152 rettv->vval.v_number = -1;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002153 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002154}
2155
2156/*
2157 * "lispindent(lnum)" function
2158 */
2159 void
2160f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2161{
2162#ifdef FEAT_LISP
2163 pos_T pos;
2164 linenr_T lnum;
2165
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002166 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2167 return;
2168
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002169 pos = curwin->w_cursor;
2170 lnum = tv_get_lnum(argvars);
2171 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2172 {
2173 curwin->w_cursor.lnum = lnum;
2174 rettv->vval.v_number = get_lisp_indent();
2175 curwin->w_cursor = pos;
2176 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002177 else if (in_vim9script())
2178 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002179 else
2180#endif
2181 rettv->vval.v_number = -1;
2182}
2183#endif