blob: f4c398219a40aa7f653a5e2da5e90d13c5c64072 [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.
21 */
22 int
23tabstop_set(char_u *var, int **array)
24{
25 int valcount = 1;
26 int t;
27 char_u *cp;
28
29 if (var[0] == NUL || (var[0] == '0' && var[1] == NUL))
30 {
31 *array = NULL;
32 return TRUE;
33 }
34
35 for (cp = var; *cp != NUL; ++cp)
36 {
37 if (cp == var || cp[-1] == ',')
38 {
39 char_u *end;
40
41 if (strtol((char *)cp, (char **)&end, 10) <= 0)
42 {
43 if (cp != end)
44 emsg(_(e_positive));
45 else
46 emsg(_(e_invarg));
47 return FALSE;
48 }
49 }
50
51 if (VIM_ISDIGIT(*cp))
52 continue;
53 if (cp[0] == ',' && cp > var && cp[-1] != ',' && cp[1] != NUL)
54 {
55 ++valcount;
56 continue;
57 }
58 emsg(_(e_invarg));
59 return FALSE;
60 }
61
62 *array = ALLOC_MULT(int, valcount + 1);
63 if (*array == NULL)
64 return FALSE;
65 (*array)[0] = valcount;
66
67 t = 1;
68 for (cp = var; *cp != NUL;)
69 {
70 (*array)[t++] = atoi((char *)cp);
71 while (*cp != NUL && *cp != ',')
72 ++cp;
73 if (*cp != NUL)
74 ++cp;
75 }
76
77 return TRUE;
78}
79
80/*
81 * Calculate the number of screen spaces a tab will occupy.
82 * If "vts" is set then the tab widths are taken from that array,
83 * otherwise the value of ts is used.
84 */
85 int
86tabstop_padding(colnr_T col, int ts_arg, int *vts)
87{
88 int ts = ts_arg == 0 ? 8 : ts_arg;
89 int tabcount;
90 colnr_T tabcol = 0;
91 int t;
92 int padding = 0;
93
94 if (vts == NULL || vts[0] == 0)
95 return ts - (col % ts);
96
97 tabcount = vts[0];
98
99 for (t = 1; t <= tabcount; ++t)
100 {
101 tabcol += vts[t];
102 if (tabcol > col)
103 {
104 padding = (int)(tabcol - col);
105 break;
106 }
107 }
108 if (t > tabcount)
109 padding = vts[tabcount] - (int)((col - tabcol) % vts[tabcount]);
110
111 return padding;
112}
113
114/*
115 * Find the size of the tab that covers a particular column.
116 */
117 int
118tabstop_at(colnr_T col, int ts, int *vts)
119{
120 int tabcount;
121 colnr_T tabcol = 0;
122 int t;
123 int tab_size = 0;
124
125 if (vts == 0 || vts[0] == 0)
126 return ts;
127
128 tabcount = vts[0];
129 for (t = 1; t <= tabcount; ++t)
130 {
131 tabcol += vts[t];
132 if (tabcol > col)
133 {
134 tab_size = vts[t];
135 break;
136 }
137 }
138 if (t > tabcount)
139 tab_size = vts[tabcount];
140
141 return tab_size;
142}
143
144/*
145 * Find the column on which a tab starts.
146 */
147 colnr_T
148tabstop_start(colnr_T col, int ts, int *vts)
149{
150 int tabcount;
151 colnr_T tabcol = 0;
152 int t;
153 int excess;
154
155 if (vts == NULL || vts[0] == 0)
156 return (col / ts) * ts;
157
158 tabcount = vts[0];
159 for (t = 1; t <= tabcount; ++t)
160 {
161 tabcol += vts[t];
162 if (tabcol > col)
163 return tabcol - vts[t];
164 }
165
166 excess = tabcol % vts[tabcount];
167 return excess + ((col - excess) / vts[tabcount]) * vts[tabcount];
168}
169
170/*
171 * Find the number of tabs and spaces necessary to get from one column
172 * to another.
173 */
174 void
175tabstop_fromto(
176 colnr_T start_col,
177 colnr_T end_col,
178 int ts_arg,
179 int *vts,
180 int *ntabs,
181 int *nspcs)
182{
183 int spaces = end_col - start_col;
184 colnr_T tabcol = 0;
185 int padding = 0;
186 int tabcount;
187 int t;
188 int ts = ts_arg == 0 ? curbuf->b_p_ts : ts_arg;
189
190 if (vts == NULL || vts[0] == 0)
191 {
192 int tabs = 0;
193 int initspc = 0;
194
195 initspc = ts - (start_col % ts);
196 if (spaces >= initspc)
197 {
198 spaces -= initspc;
199 tabs++;
200 }
201 tabs += spaces / ts;
202 spaces -= (spaces / ts) * ts;
203
204 *ntabs = tabs;
205 *nspcs = spaces;
206 return;
207 }
208
209 // Find the padding needed to reach the next tabstop.
210 tabcount = vts[0];
211 for (t = 1; t <= tabcount; ++t)
212 {
213 tabcol += vts[t];
214 if (tabcol > start_col)
215 {
216 padding = (int)(tabcol - start_col);
217 break;
218 }
219 }
220 if (t > tabcount)
221 padding = vts[tabcount] - (int)((start_col - tabcol) % vts[tabcount]);
222
223 // If the space needed is less than the padding no tabs can be used.
224 if (spaces < padding)
225 {
226 *ntabs = 0;
227 *nspcs = spaces;
228 return;
229 }
230
231 *ntabs = 1;
232 spaces -= padding;
233
234 // At least one tab has been used. See if any more will fit.
235 while (spaces != 0 && ++t <= tabcount)
236 {
237 padding = vts[t];
238 if (spaces < padding)
239 {
240 *nspcs = spaces;
241 return;
242 }
243 ++*ntabs;
244 spaces -= padding;
245 }
246
247 *ntabs += spaces / vts[tabcount];
248 *nspcs = spaces % vts[tabcount];
249}
250
251/*
252 * See if two tabstop arrays contain the same values.
253 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200254 static int
Bram Moolenaare677df82019-09-02 22:31:11 +0200255tabstop_eq(int *ts1, int *ts2)
256{
257 int t;
258
259 if ((ts1 == 0 && ts2) || (ts1 && ts2 == 0))
260 return FALSE;
261 if (ts1 == ts2)
262 return TRUE;
263 if (ts1[0] != ts2[0])
264 return FALSE;
265
266 for (t = 1; t <= ts1[0]; ++t)
267 if (ts1[t] != ts2[t])
268 return FALSE;
269
270 return TRUE;
271}
272
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200273# if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaare677df82019-09-02 22:31:11 +0200274/*
275 * Copy a tabstop array, allocating space for the new array.
276 */
277 int *
278tabstop_copy(int *oldts)
279{
280 int *newts;
281 int t;
282
283 if (oldts == NULL)
284 return NULL;
285 newts = ALLOC_MULT(int, oldts[0] + 1);
286 if (newts != NULL)
287 for (t = 0; t <= oldts[0]; ++t)
288 newts[t] = oldts[t];
289 return newts;
290}
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200291# endif
Bram Moolenaare677df82019-09-02 22:31:11 +0200292
293/*
294 * Return a count of the number of tabstops.
295 */
296 int
297tabstop_count(int *ts)
298{
299 return ts != NULL ? ts[0] : 0;
300}
301
302/*
303 * Return the first tabstop, or 8 if there are no tabstops defined.
304 */
305 int
306tabstop_first(int *ts)
307{
308 return ts != NULL ? ts[1] : 8;
309}
310
311#endif
312
313/*
314 * Return the effective shiftwidth value for current buffer, using the
315 * 'tabstop' value when 'shiftwidth' is zero.
316 */
317 long
318get_sw_value(buf_T *buf)
319{
320 return get_sw_value_col(buf, 0);
321}
322
323/*
324 * Idem, using "pos".
325 */
326 static long
327get_sw_value_pos(buf_T *buf, pos_T *pos)
328{
329 pos_T save_cursor = curwin->w_cursor;
330 long sw_value;
331
332 curwin->w_cursor = *pos;
333 sw_value = get_sw_value_col(buf, get_nolist_virtcol());
334 curwin->w_cursor = save_cursor;
335 return sw_value;
336}
337
338/*
339 * Idem, using the first non-black in the current line.
340 */
341 long
342get_sw_value_indent(buf_T *buf)
343{
344 pos_T pos = curwin->w_cursor;
345
346 pos.col = getwhitecols_curline();
347 return get_sw_value_pos(buf, &pos);
348}
349
350/*
351 * Idem, using virtual column "col".
352 */
353 long
354get_sw_value_col(buf_T *buf, colnr_T col UNUSED)
355{
356 return buf->b_p_sw ? buf->b_p_sw :
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200357#ifdef FEAT_VARTABS
Bram Moolenaare677df82019-09-02 22:31:11 +0200358 tabstop_at(col, buf->b_p_ts, buf->b_p_vts_array);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200359#else
Bram Moolenaare677df82019-09-02 22:31:11 +0200360 buf->b_p_ts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200361#endif
Bram Moolenaare677df82019-09-02 22:31:11 +0200362}
363
364/*
365 * Return the effective softtabstop value for the current buffer, using the
366 * 'shiftwidth' value when 'softtabstop' is negative.
367 */
368 long
369get_sts_value(void)
370{
371 return curbuf->b_p_sts < 0 ? get_sw_value(curbuf) : curbuf->b_p_sts;
372}
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200373
374/*
375 * Count the size (in window cells) of the indent in the current line.
376 */
377 int
378get_indent(void)
379{
380#ifdef FEAT_VARTABS
381 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
382 curbuf->b_p_vts_array, FALSE);
383#else
384 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
385#endif
386}
387
388/*
389 * Count the size (in window cells) of the indent in line "lnum".
390 */
391 int
392get_indent_lnum(linenr_T lnum)
393{
394#ifdef FEAT_VARTABS
395 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
396 curbuf->b_p_vts_array, FALSE);
397#else
398 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
399#endif
400}
401
402#if defined(FEAT_FOLDING) || defined(PROTO)
403/*
404 * Count the size (in window cells) of the indent in line "lnum" of buffer
405 * "buf".
406 */
407 int
408get_indent_buf(buf_T *buf, linenr_T lnum)
409{
410# ifdef FEAT_VARTABS
411 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
412 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
413# else
414 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
415# endif
416}
417#endif
418
419/*
420 * count the size (in window cells) of the indent in line "ptr", with
421 * 'tabstop' at "ts"
422 */
423 int
424get_indent_str(
425 char_u *ptr,
426 int ts,
427 int list) // if TRUE, count only screen size for tabs
428{
429 int count = 0;
430
431 for ( ; *ptr; ++ptr)
432 {
433 if (*ptr == TAB)
434 {
Bram Moolenaareed9d462021-02-15 20:38:25 +0100435 if (!list || curwin->w_lcs_chars.tab1)
436 // count a tab for what it is worth
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200437 count += ts - (count % ts);
438 else
439 // In list mode, when tab is not set, count screen char width
440 // for Tab, displays: ^I
441 count += ptr2cells(ptr);
442 }
443 else if (*ptr == ' ')
444 ++count; // count a space for one
445 else
446 break;
447 }
448 return count;
449}
450
451#ifdef FEAT_VARTABS
452/*
453 * Count the size (in window cells) of the indent in line "ptr", using
454 * variable tabstops.
455 * if "list" is TRUE, count only screen size for tabs.
456 */
457 int
458get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
459{
460 int count = 0;
461
462 for ( ; *ptr; ++ptr)
463 {
464 if (*ptr == TAB) // count a tab for what it is worth
465 {
Bram Moolenaareed9d462021-02-15 20:38:25 +0100466 if (!list || curwin->w_lcs_chars.tab1)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200467 count += tabstop_padding(count, ts, vts);
468 else
469 // In list mode, when tab is not set, count screen char width
470 // for Tab, displays: ^I
471 count += ptr2cells(ptr);
472 }
473 else if (*ptr == ' ')
474 ++count; // count a space for one
475 else
476 break;
477 }
478 return count;
479}
480#endif
481
482/*
483 * Set the indent of the current line.
484 * Leaves the cursor on the first non-blank in the line.
485 * Caller must take care of undo.
486 * "flags":
487 * SIN_CHANGED: call changed_bytes() if the line was changed.
488 * SIN_INSERT: insert the indent in front of the line.
489 * SIN_UNDO: save line for undo before changing it.
490 * Returns TRUE if the line was changed.
491 */
492 int
493set_indent(
494 int size, // measured in spaces
495 int flags)
496{
497 char_u *p;
498 char_u *newline;
499 char_u *oldline;
500 char_u *s;
501 int todo;
502 int ind_len; // measured in characters
503 int line_len;
504 int doit = FALSE;
505 int ind_done = 0; // measured in spaces
506#ifdef FEAT_VARTABS
507 int ind_col = 0;
508#endif
509 int tab_pad;
510 int retval = FALSE;
511 int orig_char_len = -1; // number of initial whitespace chars when
512 // 'et' and 'pi' are both set
513
514 // First check if there is anything to do and compute the number of
515 // characters needed for the indent.
516 todo = size;
517 ind_len = 0;
518 p = oldline = ml_get_curline();
519
520 // Calculate the buffer size for the new indent, and check to see if it
521 // isn't already set
522
523 // if 'expandtab' isn't set: use TABs; if both 'expandtab' and
524 // 'preserveindent' are set count the number of characters at the
525 // beginning of the line to be copied
526 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
527 {
528 // If 'preserveindent' is set then reuse as much as possible of
529 // the existing indent structure for the new indent
530 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
531 {
532 ind_done = 0;
533
534 // count as many characters as we can use
535 while (todo > 0 && VIM_ISWHITE(*p))
536 {
537 if (*p == TAB)
538 {
539#ifdef FEAT_VARTABS
540 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
541 curbuf->b_p_vts_array);
542#else
543 tab_pad = (int)curbuf->b_p_ts
544 - (ind_done % (int)curbuf->b_p_ts);
545#endif
546 // stop if this tab will overshoot the target
547 if (todo < tab_pad)
548 break;
549 todo -= tab_pad;
550 ++ind_len;
551 ind_done += tab_pad;
552 }
553 else
554 {
555 --todo;
556 ++ind_len;
557 ++ind_done;
558 }
559 ++p;
560 }
561
562#ifdef FEAT_VARTABS
563 // These diverge from this point.
564 ind_col = ind_done;
565#endif
566 // Set initial number of whitespace chars to copy if we are
567 // preserving indent but expandtab is set
568 if (curbuf->b_p_et)
569 orig_char_len = ind_len;
570
571 // Fill to next tabstop with a tab, if possible
572#ifdef FEAT_VARTABS
573 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
574 curbuf->b_p_vts_array);
575#else
576 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
577#endif
578 if (todo >= tab_pad && orig_char_len == -1)
579 {
580 doit = TRUE;
581 todo -= tab_pad;
582 ++ind_len;
583 // ind_done += tab_pad;
584#ifdef FEAT_VARTABS
585 ind_col += tab_pad;
586#endif
587 }
588 }
589
590 // count tabs required for indent
591#ifdef FEAT_VARTABS
592 for (;;)
593 {
594 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
595 curbuf->b_p_vts_array);
596 if (todo < tab_pad)
597 break;
598 if (*p != TAB)
599 doit = TRUE;
600 else
601 ++p;
602 todo -= tab_pad;
603 ++ind_len;
604 ind_col += tab_pad;
605 }
606#else
607 while (todo >= (int)curbuf->b_p_ts)
608 {
609 if (*p != TAB)
610 doit = TRUE;
611 else
612 ++p;
613 todo -= (int)curbuf->b_p_ts;
614 ++ind_len;
615 // ind_done += (int)curbuf->b_p_ts;
616 }
617#endif
618 }
619 // count spaces required for indent
620 while (todo > 0)
621 {
622 if (*p != ' ')
623 doit = TRUE;
624 else
625 ++p;
626 --todo;
627 ++ind_len;
628 // ++ind_done;
629 }
630
631 // Return if the indent is OK already.
632 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
633 return FALSE;
634
635 // Allocate memory for the new line.
636 if (flags & SIN_INSERT)
637 p = oldline;
638 else
639 p = skipwhite(p);
640 line_len = (int)STRLEN(p) + 1;
641
642 // If 'preserveindent' and 'expandtab' are both set keep the original
643 // characters and allocate accordingly. We will fill the rest with spaces
644 // after the if (!curbuf->b_p_et) below.
645 if (orig_char_len != -1)
646 {
647 newline = alloc(orig_char_len + size - ind_done + line_len);
648 if (newline == NULL)
649 return FALSE;
650 todo = size - ind_done;
651 ind_len = orig_char_len + todo; // Set total length of indent in
652 // characters, which may have been
653 // undercounted until now
654 p = oldline;
655 s = newline;
656 while (orig_char_len > 0)
657 {
658 *s++ = *p++;
659 orig_char_len--;
660 }
661
662 // Skip over any additional white space (useful when newindent is less
663 // than old)
664 while (VIM_ISWHITE(*p))
665 ++p;
666
667 }
668 else
669 {
670 todo = size;
671 newline = alloc(ind_len + line_len);
672 if (newline == NULL)
673 return FALSE;
674 s = newline;
675 }
676
677 // Put the characters in the new line.
678 // if 'expandtab' isn't set: use TABs
679 if (!curbuf->b_p_et)
680 {
681 // If 'preserveindent' is set then reuse as much as possible of
682 // the existing indent structure for the new indent
683 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
684 {
685 p = oldline;
686 ind_done = 0;
687
688 while (todo > 0 && VIM_ISWHITE(*p))
689 {
690 if (*p == TAB)
691 {
692#ifdef FEAT_VARTABS
693 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
694 curbuf->b_p_vts_array);
695#else
696 tab_pad = (int)curbuf->b_p_ts
697 - (ind_done % (int)curbuf->b_p_ts);
698#endif
699 // stop if this tab will overshoot the target
700 if (todo < tab_pad)
701 break;
702 todo -= tab_pad;
703 ind_done += tab_pad;
704 }
705 else
706 {
707 --todo;
708 ++ind_done;
709 }
710 *s++ = *p++;
711 }
712
713 // Fill to next tabstop with a tab, if possible
714#ifdef FEAT_VARTABS
715 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
716 curbuf->b_p_vts_array);
717#else
718 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
719#endif
720 if (todo >= tab_pad)
721 {
722 *s++ = TAB;
723 todo -= tab_pad;
724#ifdef FEAT_VARTABS
725 ind_done += tab_pad;
726#endif
727 }
728
729 p = skipwhite(p);
730 }
731
732#ifdef FEAT_VARTABS
733 for (;;)
734 {
735 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
736 curbuf->b_p_vts_array);
737 if (todo < tab_pad)
738 break;
739 *s++ = TAB;
740 todo -= tab_pad;
741 ind_done += tab_pad;
742 }
743#else
744 while (todo >= (int)curbuf->b_p_ts)
745 {
746 *s++ = TAB;
747 todo -= (int)curbuf->b_p_ts;
748 }
749#endif
750 }
751 while (todo > 0)
752 {
753 *s++ = ' ';
754 --todo;
755 }
756 mch_memmove(s, p, (size_t)line_len);
757
758 // Replace the line (unless undo fails).
759 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
760 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200761 colnr_T old_offset = (colnr_T)(p - oldline);
762 colnr_T new_offset = (colnr_T)(s - newline);
763
764 // this may free "newline"
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200765 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
766 if (flags & SIN_CHANGED)
767 changed_bytes(curwin->w_cursor.lnum, 0);
768
769 // Correct saved cursor position if it is in this line.
770 if (saved_cursor.lnum == curwin->w_cursor.lnum)
771 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200772 if (saved_cursor.col >= old_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200773 // cursor was after the indent, adjust for the number of
774 // bytes added/removed
Bram Moolenaarcf306432020-06-29 20:40:37 +0200775 saved_cursor.col += ind_len - old_offset;
776 else if (saved_cursor.col >= new_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200777 // cursor was in the indent, and is now after it, put it back
778 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaarcf306432020-06-29 20:40:37 +0200779 saved_cursor.col = new_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200780 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100781#ifdef FEAT_PROP_POPUP
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200782 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200783 int added = ind_len - old_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200784
785 // When increasing indent this behaves like spaces were inserted at
786 // the old indent, when decreasing indent it behaves like spaces
787 // were deleted at the new indent.
788 adjust_prop_columns(curwin->w_cursor.lnum,
Bram Moolenaarcf306432020-06-29 20:40:37 +0200789 added > 0 ? old_offset : (colnr_T)ind_len, added, 0);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200790 }
791#endif
792 retval = TRUE;
793 }
794 else
795 vim_free(newline);
796
797 curwin->w_cursor.col = ind_len;
798 return retval;
799}
800
801/*
802 * Return the indent of the current line after a number. Return -1 if no
803 * number was found. Used for 'n' in 'formatoptions': numbered list.
804 * Since a pattern is used it can actually handle more than numbers.
805 */
806 int
807get_number_indent(linenr_T lnum)
808{
809 colnr_T col;
810 pos_T pos;
811
812 regmatch_T regmatch;
813 int lead_len = 0; // length of comment leader
814
815 if (lnum > curbuf->b_ml.ml_line_count)
816 return -1;
817 pos.lnum = 0;
818
819 // In format_lines() (i.e. not insert mode), fo+=q is needed too...
820 if ((State & INSERT) || has_format_option(FO_Q_COMS))
821 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
822
823 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
824 if (regmatch.regprog != NULL)
825 {
826 regmatch.rm_ic = FALSE;
827
828 // vim_regexec() expects a pointer to a line. This lets us
829 // start matching for the flp beyond any comment leader...
830 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
831 {
832 pos.lnum = lnum;
833 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
834 pos.coladd = 0;
835 }
836 vim_regfree(regmatch.regprog);
837 }
838
839 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
840 return -1;
841 getvcol(curwin, &pos, &col, NULL, NULL);
842 return (int)col;
843}
844
845#if defined(FEAT_LINEBREAK) || defined(PROTO)
846/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100847 * This is called when 'breakindentopt' is changed and when a window is
848 * initialized.
849 */
850 int
851briopt_check(win_T *wp)
852{
853 char_u *p;
854 int bri_shift = 0;
855 long bri_min = 20;
856 int bri_sbr = FALSE;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200857 int bri_list = 0;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100858
859 p = wp->w_p_briopt;
860 while (*p != NUL)
861 {
862 if (STRNCMP(p, "shift:", 6) == 0
863 && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
864 {
865 p += 6;
866 bri_shift = getdigits(&p);
867 }
868 else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4]))
869 {
870 p += 4;
871 bri_min = getdigits(&p);
872 }
873 else if (STRNCMP(p, "sbr", 3) == 0)
874 {
875 p += 3;
876 bri_sbr = TRUE;
877 }
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200878 else if (STRNCMP(p, "list:", 5) == 0)
879 {
880 p += 5;
881 bri_list = getdigits(&p);
882 }
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100883 if (*p != ',' && *p != NUL)
884 return FAIL;
885 if (*p == ',')
886 ++p;
887 }
888
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100889 wp->w_briopt_shift = bri_shift;
890 wp->w_briopt_min = bri_min;
891 wp->w_briopt_sbr = bri_sbr;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200892 wp->w_briopt_list = bri_list;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100893
894 return OK;
895}
896
897/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200898 * Return appropriate space number for breakindent, taking influencing
899 * parameters into account. Window must be specified, since it is not
900 * necessarily always the current one.
901 */
902 int
903get_breakindent_win(
904 win_T *wp,
905 char_u *line) // start of the line
906{
907 static int prev_indent = 0; // cached indent value
908 static long prev_ts = 0L; // cached tabstop value
909 static char_u *prev_line = NULL; // cached pointer to line
910 static varnumber_T prev_tick = 0; // changedtick of cached value
911# ifdef FEAT_VARTABS
912 static int *prev_vts = NULL; // cached vartabs values
913# endif
914 int bri = 0;
915 // window width minus window margin space, i.e. what rests for text
916 const int eff_wwidth = wp->w_width
917 - ((wp->w_p_nu || wp->w_p_rnu)
918 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
919 ? number_width(wp) + 1 : 0);
920
921 // used cached indent, unless pointer or 'tabstop' changed
922 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
923 || prev_tick != CHANGEDTICK(wp->w_buffer)
924# ifdef FEAT_VARTABS
925 || prev_vts != wp->w_buffer->b_p_vts_array
926# endif
927 )
928 {
929 prev_line = line;
930 prev_ts = wp->w_buffer->b_p_ts;
931 prev_tick = CHANGEDTICK(wp->w_buffer);
932# ifdef FEAT_VARTABS
933 prev_vts = wp->w_buffer->b_p_vts_array;
934 prev_indent = get_indent_str_vtab(line,
935 (int)wp->w_buffer->b_p_ts,
936 wp->w_buffer->b_p_vts_array, wp->w_p_list);
937# else
938 prev_indent = get_indent_str(line,
939 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
940# endif
941 }
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100942 bri = prev_indent + wp->w_briopt_shift;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200943
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200944 // Add offset for number column, if 'n' is in 'cpoptions'
945 bri += win_col_off2(wp);
946
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200947 // add additional indent for numbered lists
Maxim Kimf674b352021-07-22 11:46:59 +0200948 if (wp->w_briopt_list != 0)
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200949 {
950 regmatch_T regmatch;
951
952 regmatch.regprog = vim_regcomp(curbuf->b_p_flp,
953 RE_MAGIC + RE_STRING + RE_AUTO + RE_STRICT);
954 if (regmatch.regprog != NULL)
955 {
Dominique Pellea9187122021-08-29 22:12:56 +0200956 regmatch.rm_ic = FALSE;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200957 if (vim_regexec(&regmatch, line, 0))
Maxim Kimf674b352021-07-22 11:46:59 +0200958 {
959 if (wp->w_briopt_list > 0)
960 bri += wp->w_briopt_list;
961 else
962 bri = (*regmatch.endp - *regmatch.startp);
963 }
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200964 vim_regfree(regmatch.regprog);
965 }
966 }
967
Maxim Kimf674b352021-07-22 11:46:59 +0200968 // indent minus the length of the showbreak string
969 if (wp->w_briopt_sbr)
970 bri -= vim_strsize(get_showbreak_value(wp));
971
972
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200973 // never indent past left window margin
974 if (bri < 0)
975 bri = 0;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200976
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200977 // always leave at least bri_min characters on the left,
978 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100979 else if (bri > eff_wwidth - wp->w_briopt_min)
980 bri = (eff_wwidth - wp->w_briopt_min < 0)
981 ? 0 : eff_wwidth - wp->w_briopt_min;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200982
983 return bri;
984}
985#endif
986
987/*
988 * When extra == 0: Return TRUE if the cursor is before or on the first
989 * non-blank in the line.
990 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
991 * the line.
992 */
993 int
994inindent(int extra)
995{
996 char_u *ptr;
997 colnr_T col;
998
999 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
1000 ++ptr;
1001 if (col >= curwin->w_cursor.col + extra)
1002 return TRUE;
1003 else
1004 return FALSE;
1005}
1006
1007#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
1008/*
1009 * op_reindent - handle reindenting a block of lines.
1010 */
1011 void
1012op_reindent(oparg_T *oap, int (*how)(void))
1013{
1014 long i;
1015 char_u *l;
1016 int amount;
1017 linenr_T first_changed = 0;
1018 linenr_T last_changed = 0;
1019 linenr_T start_lnum = curwin->w_cursor.lnum;
1020
1021 // Don't even try when 'modifiable' is off.
1022 if (!curbuf->b_p_ma)
1023 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001024 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001025 return;
1026 }
1027
1028 for (i = oap->line_count; --i >= 0 && !got_int; )
1029 {
1030 // it's a slow thing to do, so give feedback so there's no worry that
1031 // the computer's just hung.
1032
1033 if (i > 1
1034 && (i % 50 == 0 || i == oap->line_count - 1)
1035 && oap->line_count > p_report)
1036 smsg(_("%ld lines to indent... "), i);
1037
1038 // Be vi-compatible: For lisp indenting the first line is not
1039 // indented, unless there is only one line.
1040# ifdef FEAT_LISP
1041 if (i != oap->line_count - 1 || oap->line_count == 1
1042 || how != get_lisp_indent)
1043# endif
1044 {
1045 l = skipwhite(ml_get_curline());
1046 if (*l == NUL) // empty or blank line
1047 amount = 0;
1048 else
1049 amount = how(); // get the indent for this line
1050
1051 if (amount >= 0 && set_indent(amount, SIN_UNDO))
1052 {
1053 // did change the indent, call changed_lines() later
1054 if (first_changed == 0)
1055 first_changed = curwin->w_cursor.lnum;
1056 last_changed = curwin->w_cursor.lnum;
1057 }
1058 }
1059 ++curwin->w_cursor.lnum;
1060 curwin->w_cursor.col = 0; // make sure it's valid
1061 }
1062
1063 // put cursor on first non-blank of indented line
1064 curwin->w_cursor.lnum = start_lnum;
1065 beginline(BL_SOL | BL_FIX);
1066
1067 // Mark changed lines so that they will be redrawn. When Visual
1068 // highlighting was present, need to continue until the last line. When
1069 // there is no change still need to remove the Visual highlighting.
1070 if (last_changed != 0)
1071 changed_lines(first_changed, 0,
1072 oap->is_VIsual ? start_lnum + oap->line_count :
1073 last_changed + 1, 0L);
1074 else if (oap->is_VIsual)
1075 redraw_curbuf_later(INVERTED);
1076
1077 if (oap->line_count > p_report)
1078 {
1079 i = oap->line_count - (i + 1);
1080 smsg(NGETTEXT("%ld line indented ",
1081 "%ld lines indented ", i), i);
1082 }
Bram Moolenaare1004402020-10-24 20:49:43 +02001083 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001084 {
1085 // set '[ and '] marks
1086 curbuf->b_op_start = oap->start;
1087 curbuf->b_op_end = oap->end;
1088 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001089}
1090#endif // defined(FEAT_LISP) || defined(FEAT_CINDENT)
1091
1092#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
1093/*
1094 * Return TRUE if lines starting with '#' should be left aligned.
1095 */
1096 int
1097preprocs_left(void)
1098{
1099 return
1100# ifdef FEAT_SMARTINDENT
1101# ifdef FEAT_CINDENT
1102 (curbuf->b_p_si && !curbuf->b_p_cin) ||
1103# else
1104 curbuf->b_p_si
1105# endif
1106# endif
1107# ifdef FEAT_CINDENT
1108 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1109 && curbuf->b_ind_hash_comment == 0)
1110# endif
1111 ;
1112}
1113#endif
1114
1115#ifdef FEAT_SMARTINDENT
1116/*
1117 * Try to do some very smart auto-indenting.
1118 * Used when inserting a "normal" character.
1119 */
1120 void
1121ins_try_si(int c)
1122{
1123 pos_T *pos, old_pos;
1124 char_u *ptr;
1125 int i;
1126 int temp;
1127
1128 // do some very smart indenting when entering '{' or '}'
1129 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
1130 {
1131 // for '}' set indent equal to indent of line containing matching '{'
1132 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1133 {
1134 old_pos = curwin->w_cursor;
1135 // If the matching '{' has a ')' immediately before it (ignoring
1136 // white-space), then line up with the start of the line
1137 // containing the matching '(' if there is one. This handles the
1138 // case where an "if (..\n..) {" statement continues over multiple
1139 // lines -- webb
1140 ptr = ml_get(pos->lnum);
1141 i = pos->col;
1142 if (i > 0) // skip blanks before '{'
1143 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1144 ;
1145 curwin->w_cursor.lnum = pos->lnum;
1146 curwin->w_cursor.col = i;
1147 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1148 curwin->w_cursor = *pos;
1149 i = get_indent();
1150 curwin->w_cursor = old_pos;
1151 if (State & VREPLACE_FLAG)
1152 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1153 else
1154 (void)set_indent(i, SIN_CHANGED);
1155 }
1156 else if (curwin->w_cursor.col > 0)
1157 {
1158 // when inserting '{' after "O" reduce indent, but not
1159 // more than indent of previous line
1160 temp = TRUE;
1161 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1162 {
1163 old_pos = curwin->w_cursor;
1164 i = get_indent();
1165 while (curwin->w_cursor.lnum > 1)
1166 {
1167 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1168
1169 // ignore empty lines and lines starting with '#'.
1170 if (*ptr != '#' && *ptr != NUL)
1171 break;
1172 }
1173 if (get_indent() >= i)
1174 temp = FALSE;
1175 curwin->w_cursor = old_pos;
1176 }
1177 if (temp)
1178 shift_line(TRUE, FALSE, 1, TRUE);
1179 }
1180 }
1181
1182 // set indent of '#' always to 0
1183 if (curwin->w_cursor.col > 0 && can_si && c == '#')
1184 {
1185 // remember current indent for next line
1186 old_indent = get_indent();
1187 (void)set_indent(0, SIN_CHANGED);
1188 }
1189
1190 // Adjust ai_col, the char at this position can be deleted.
1191 if (ai_col > curwin->w_cursor.col)
1192 ai_col = curwin->w_cursor.col;
1193}
1194#endif
1195
1196/*
1197 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1198 * Keep the cursor on the same character.
1199 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1200 * type == INDENT_DEC decrease indent (for CTRL-D)
1201 * type == INDENT_SET set indent to "amount"
1202 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1203 */
1204 void
1205change_indent(
1206 int type,
1207 int amount,
1208 int round,
1209 int replaced, // replaced character, put on replace stack
1210 int call_changed_bytes) // call changed_bytes()
1211{
1212 int vcol;
1213 int last_vcol;
1214 int insstart_less; // reduction for Insstart.col
1215 int new_cursor_col;
1216 int i;
1217 char_u *ptr;
1218 int save_p_list;
1219 int start_col;
1220 colnr_T vc;
1221 colnr_T orig_col = 0; // init for GCC
1222 char_u *new_line, *orig_line = NULL; // init for GCC
1223
1224 // VREPLACE mode needs to know what the line was like before changing
1225 if (State & VREPLACE_FLAG)
1226 {
1227 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1228 orig_col = curwin->w_cursor.col;
1229 }
1230
1231 // for the following tricks we don't want list mode
1232 save_p_list = curwin->w_p_list;
1233 curwin->w_p_list = FALSE;
1234 vc = getvcol_nolist(&curwin->w_cursor);
1235 vcol = vc;
1236
1237 // For Replace mode we need to fix the replace stack later, which is only
1238 // possible when the cursor is in the indent. Remember the number of
1239 // characters before the cursor if it's possible.
1240 start_col = curwin->w_cursor.col;
1241
1242 // determine offset from first non-blank
1243 new_cursor_col = curwin->w_cursor.col;
1244 beginline(BL_WHITE);
1245 new_cursor_col -= curwin->w_cursor.col;
1246
1247 insstart_less = curwin->w_cursor.col;
1248
1249 // If the cursor is in the indent, compute how many screen columns the
1250 // cursor is to the left of the first non-blank.
1251 if (new_cursor_col < 0)
1252 vcol = get_indent() - vcol;
1253
1254 if (new_cursor_col > 0) // can't fix replace stack
1255 start_col = -1;
1256
1257 // Set the new indent. The cursor will be put on the first non-blank.
1258 if (type == INDENT_SET)
1259 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1260 else
1261 {
1262 int save_State = State;
1263
1264 // Avoid being called recursively.
1265 if (State & VREPLACE_FLAG)
1266 State = INSERT;
1267 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1268 State = save_State;
1269 }
1270 insstart_less -= curwin->w_cursor.col;
1271
1272 // Try to put cursor on same character.
1273 // If the cursor is at or after the first non-blank in the line,
1274 // compute the cursor column relative to the column of the first
1275 // non-blank character.
1276 // If we are not in insert mode, leave the cursor on the first non-blank.
1277 // If the cursor is before the first non-blank, position it relative
1278 // to the first non-blank, counted in screen columns.
1279 if (new_cursor_col >= 0)
1280 {
1281 // When changing the indent while the cursor is touching it, reset
1282 // Insstart_col to 0.
1283 if (new_cursor_col == 0)
1284 insstart_less = MAXCOL;
1285 new_cursor_col += curwin->w_cursor.col;
1286 }
1287 else if (!(State & INSERT))
1288 new_cursor_col = curwin->w_cursor.col;
1289 else
1290 {
1291 // Compute the screen column where the cursor should be.
1292 vcol = get_indent() - vcol;
1293 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1294
1295 // Advance the cursor until we reach the right screen column.
1296 vcol = last_vcol = 0;
1297 new_cursor_col = -1;
1298 ptr = ml_get_curline();
1299 while (vcol <= (int)curwin->w_virtcol)
1300 {
1301 last_vcol = vcol;
1302 if (has_mbyte && new_cursor_col >= 0)
1303 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
1304 else
1305 ++new_cursor_col;
1306 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
1307 }
1308 vcol = last_vcol;
1309
1310 // May need to insert spaces to be able to position the cursor on
1311 // the right screen column.
1312 if (vcol != (int)curwin->w_virtcol)
1313 {
1314 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1315 i = (int)curwin->w_virtcol - vcol;
1316 ptr = alloc(i + 1);
1317 if (ptr != NULL)
1318 {
1319 new_cursor_col += i;
1320 ptr[i] = NUL;
1321 while (--i >= 0)
1322 ptr[i] = ' ';
1323 ins_str(ptr);
1324 vim_free(ptr);
1325 }
1326 }
1327
1328 // When changing the indent while the cursor is in it, reset
1329 // Insstart_col to 0.
1330 insstart_less = MAXCOL;
1331 }
1332
1333 curwin->w_p_list = save_p_list;
1334
1335 if (new_cursor_col <= 0)
1336 curwin->w_cursor.col = 0;
1337 else
1338 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1339 curwin->w_set_curswant = TRUE;
1340 changed_cline_bef_curs();
1341
1342 // May have to adjust the start of the insert.
1343 if (State & INSERT)
1344 {
1345 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1346 {
1347 if ((int)Insstart.col <= insstart_less)
1348 Insstart.col = 0;
1349 else
1350 Insstart.col -= insstart_less;
1351 }
1352 if ((int)ai_col <= insstart_less)
1353 ai_col = 0;
1354 else
1355 ai_col -= insstart_less;
1356 }
1357
1358 // For REPLACE mode, may have to fix the replace stack, if it's possible.
1359 // If the number of characters before the cursor decreased, need to pop a
1360 // few characters from the replace stack.
1361 // If the number of characters before the cursor increased, need to push a
1362 // few NULs onto the replace stack.
1363 if (REPLACE_NORMAL(State) && start_col >= 0)
1364 {
1365 while (start_col > (int)curwin->w_cursor.col)
1366 {
1367 replace_join(0); // remove a NUL from the replace stack
1368 --start_col;
1369 }
1370 while (start_col < (int)curwin->w_cursor.col || replaced)
1371 {
1372 replace_push(NUL);
1373 if (replaced)
1374 {
1375 replace_push(replaced);
1376 replaced = NUL;
1377 }
1378 ++start_col;
1379 }
1380 }
1381
1382 // For VREPLACE mode, we also have to fix the replace stack. In this case
1383 // it is always possible because we backspace over the whole line and then
1384 // put it back again the way we wanted it.
1385 if (State & VREPLACE_FLAG)
1386 {
1387 // If orig_line didn't allocate, just return. At least we did the job,
1388 // even if you can't backspace.
1389 if (orig_line == NULL)
1390 return;
1391
1392 // Save new line
1393 new_line = vim_strsave(ml_get_curline());
1394 if (new_line == NULL)
1395 return;
1396
1397 // We only put back the new line up to the cursor
1398 new_line[curwin->w_cursor.col] = NUL;
1399
1400 // Put back original line
1401 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1402 curwin->w_cursor.col = orig_col;
1403
1404 // Backspace from cursor to start of line
1405 backspace_until_column(0);
1406
1407 // Insert new stuff into line again
1408 ins_bytes(new_line);
1409
1410 vim_free(new_line);
1411 }
1412}
1413
1414/*
1415 * Copy the indent from ptr to the current line (and fill to size)
1416 * Leaves the cursor on the first non-blank in the line.
1417 * Returns TRUE if the line was changed.
1418 */
1419 int
1420copy_indent(int size, char_u *src)
1421{
1422 char_u *p = NULL;
1423 char_u *line = NULL;
1424 char_u *s;
1425 int todo;
1426 int ind_len;
1427 int line_len = 0;
1428 int tab_pad;
1429 int ind_done;
1430 int round;
1431#ifdef FEAT_VARTABS
1432 int ind_col;
1433#endif
1434
1435 // Round 1: compute the number of characters needed for the indent
1436 // Round 2: copy the characters.
1437 for (round = 1; round <= 2; ++round)
1438 {
1439 todo = size;
1440 ind_len = 0;
1441 ind_done = 0;
1442#ifdef FEAT_VARTABS
1443 ind_col = 0;
1444#endif
1445 s = src;
1446
1447 // Count/copy the usable portion of the source line
1448 while (todo > 0 && VIM_ISWHITE(*s))
1449 {
1450 if (*s == TAB)
1451 {
1452#ifdef FEAT_VARTABS
1453 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1454 curbuf->b_p_vts_array);
1455#else
1456 tab_pad = (int)curbuf->b_p_ts
1457 - (ind_done % (int)curbuf->b_p_ts);
1458#endif
1459 // Stop if this tab will overshoot the target
1460 if (todo < tab_pad)
1461 break;
1462 todo -= tab_pad;
1463 ind_done += tab_pad;
1464#ifdef FEAT_VARTABS
1465 ind_col += tab_pad;
1466#endif
1467 }
1468 else
1469 {
1470 --todo;
1471 ++ind_done;
1472#ifdef FEAT_VARTABS
1473 ++ind_col;
1474#endif
1475 }
1476 ++ind_len;
1477 if (p != NULL)
1478 *p++ = *s;
1479 ++s;
1480 }
1481
1482 // Fill to next tabstop with a tab, if possible
1483#ifdef FEAT_VARTABS
1484 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1485 curbuf->b_p_vts_array);
1486#else
1487 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1488#endif
1489 if (todo >= tab_pad && !curbuf->b_p_et)
1490 {
1491 todo -= tab_pad;
1492 ++ind_len;
1493#ifdef FEAT_VARTABS
1494 ind_col += tab_pad;
1495#endif
1496 if (p != NULL)
1497 *p++ = TAB;
1498 }
1499
1500 // Add tabs required for indent
1501 if (!curbuf->b_p_et)
1502 {
1503#ifdef FEAT_VARTABS
1504 for (;;)
1505 {
1506 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1507 curbuf->b_p_vts_array);
1508 if (todo < tab_pad)
1509 break;
1510 todo -= tab_pad;
1511 ++ind_len;
1512 ind_col += tab_pad;
1513 if (p != NULL)
1514 *p++ = TAB;
1515 }
1516#else
1517 while (todo >= (int)curbuf->b_p_ts)
1518 {
1519 todo -= (int)curbuf->b_p_ts;
1520 ++ind_len;
1521 if (p != NULL)
1522 *p++ = TAB;
1523 }
1524#endif
1525 }
1526
1527 // Count/add spaces required for indent
1528 while (todo > 0)
1529 {
1530 --todo;
1531 ++ind_len;
1532 if (p != NULL)
1533 *p++ = ' ';
1534 }
1535
1536 if (p == NULL)
1537 {
1538 // Allocate memory for the result: the copied indent, new indent
1539 // and the rest of the line.
1540 line_len = (int)STRLEN(ml_get_curline()) + 1;
1541 line = alloc(ind_len + line_len);
1542 if (line == NULL)
1543 return FALSE;
1544 p = line;
1545 }
1546 }
1547
1548 // Append the original line
1549 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1550
1551 // Replace the line
1552 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1553
1554 // Put the cursor after the indent.
1555 curwin->w_cursor.col = ind_len;
1556 return TRUE;
1557}
1558
1559/*
1560 * ":retab".
1561 */
1562 void
1563ex_retab(exarg_T *eap)
1564{
1565 linenr_T lnum;
1566 int got_tab = FALSE;
1567 long num_spaces = 0;
1568 long num_tabs;
1569 long len;
1570 long col;
1571 long vcol;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001572 long start_col = 0; // For start of white-space string
1573 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001574 long old_len;
1575 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001576 char_u *new_line = (char_u *)1; // init to non-NULL
1577 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001578#ifdef FEAT_VARTABS
1579 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001580 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001581#else
1582 int temp;
1583 int new_ts;
1584#endif
1585 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001586 linenr_T first_line = 0; // first changed line
1587 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001588
1589 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001590 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001591
1592#ifdef FEAT_VARTABS
1593 new_ts_str = eap->arg;
1594 if (!tabstop_set(eap->arg, &new_vts_array))
1595 return;
1596 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1597 ++(eap->arg);
1598
1599 // This ensures that either new_vts_array and new_ts_str are freshly
1600 // allocated, or new_vts_array points to an existing array and new_ts_str
1601 // is null.
1602 if (new_vts_array == NULL)
1603 {
1604 new_vts_array = curbuf->b_p_vts_array;
1605 new_ts_str = NULL;
1606 }
1607 else
1608 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1609#else
1610 new_ts = getdigits(&(eap->arg));
1611 if (new_ts < 0)
1612 {
1613 emsg(_(e_positive));
1614 return;
1615 }
1616 if (new_ts == 0)
1617 new_ts = curbuf->b_p_ts;
1618#endif
1619 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1620 {
1621 ptr = ml_get(lnum);
1622 col = 0;
1623 vcol = 0;
1624 did_undo = FALSE;
1625 for (;;)
1626 {
1627 if (VIM_ISWHITE(ptr[col]))
1628 {
1629 if (!got_tab && num_spaces == 0)
1630 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001631 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001632 start_vcol = vcol;
1633 start_col = col;
1634 }
1635 if (ptr[col] == ' ')
1636 num_spaces++;
1637 else
1638 got_tab = TRUE;
1639 }
1640 else
1641 {
1642 if (got_tab || (eap->forceit && num_spaces > 1))
1643 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001644 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001645
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001646 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001647 len = num_spaces = vcol - start_vcol;
1648 num_tabs = 0;
1649 if (!curbuf->b_p_et)
1650 {
1651#ifdef FEAT_VARTABS
1652 int t, s;
1653
1654 tabstop_fromto(start_vcol, vcol,
1655 curbuf->b_p_ts, new_vts_array, &t, &s);
1656 num_tabs = t;
1657 num_spaces = s;
1658#else
1659 temp = new_ts - (start_vcol % new_ts);
1660 if (num_spaces >= temp)
1661 {
1662 num_spaces -= temp;
1663 num_tabs++;
1664 }
1665 num_tabs += num_spaces / new_ts;
1666 num_spaces -= (num_spaces / new_ts) * new_ts;
1667#endif
1668 }
1669 if (curbuf->b_p_et || got_tab ||
1670 (num_spaces + num_tabs < len))
1671 {
1672 if (did_undo == FALSE)
1673 {
1674 did_undo = TRUE;
1675 if (u_save((linenr_T)(lnum - 1),
1676 (linenr_T)(lnum + 1)) == FAIL)
1677 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001678 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001679 break;
1680 }
1681 }
1682
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001683 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001684 len = num_spaces + num_tabs;
1685 old_len = (long)STRLEN(ptr);
1686 new_line = alloc(old_len - col + start_col + len + 1);
1687 if (new_line == NULL)
1688 break;
1689 if (start_col > 0)
1690 mch_memmove(new_line, ptr, (size_t)start_col);
1691 mch_memmove(new_line + start_col + len,
1692 ptr + col, (size_t)(old_len - col + 1));
1693 ptr = new_line + start_col;
1694 for (col = 0; col < len; col++)
1695 ptr[col] = (col < num_tabs) ? '\t' : ' ';
Bram Moolenaar0dcd39b2021-02-03 19:44:25 +01001696 if (ml_replace(lnum, new_line, FALSE) == OK)
1697 // "new_line" may have been copied
1698 new_line = curbuf->b_ml.ml_line_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001699 if (first_line == 0)
1700 first_line = lnum;
1701 last_line = lnum;
1702 ptr = new_line;
1703 col = start_col + len;
1704 }
1705 }
1706 got_tab = FALSE;
1707 num_spaces = 0;
1708 }
1709 if (ptr[col] == NUL)
1710 break;
1711 vcol += chartabsize(ptr + col, (colnr_T)vcol);
1712 if (has_mbyte)
1713 col += (*mb_ptr2len)(ptr + col);
1714 else
1715 ++col;
1716 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001717 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001718 break;
1719 line_breakcheck();
1720 }
1721 if (got_int)
1722 emsg(_(e_interr));
1723
1724#ifdef FEAT_VARTABS
1725 // If a single value was given then it can be considered equal to
1726 // either the value of 'tabstop' or the value of 'vartabstop'.
1727 if (tabstop_count(curbuf->b_p_vts_array) == 0
1728 && tabstop_count(new_vts_array) == 1
1729 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001730 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001731 else if (tabstop_count(curbuf->b_p_vts_array) > 0
1732 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001733 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001734 else
1735 redraw_curbuf_later(NOT_VALID);
1736#else
1737 if (curbuf->b_p_ts != new_ts)
1738 redraw_curbuf_later(NOT_VALID);
1739#endif
1740 if (first_line != 0)
1741 changed_lines(first_line, 0, last_line + 1, 0L);
1742
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001743 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001744
1745#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001746 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001747 {
1748 // If 'vartabstop' is in use or if the value given to retab has more
1749 // than one tabstop then update 'vartabstop'.
1750 int *old_vts_ary = curbuf->b_p_vts_array;
1751
1752 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1753 {
1754 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1755 OPT_FREE|OPT_LOCAL, 0);
1756 curbuf->b_p_vts_array = new_vts_array;
1757 vim_free(old_vts_ary);
1758 }
1759 else
1760 {
1761 // 'vartabstop' wasn't in use and a single value was given to
1762 // retab then update 'tabstop'.
1763 curbuf->b_p_ts = tabstop_first(new_vts_array);
1764 vim_free(new_vts_array);
1765 }
1766 vim_free(new_ts_str);
1767 }
1768#else
1769 curbuf->b_p_ts = new_ts;
1770#endif
1771 coladvance(curwin->w_curswant);
1772
1773 u_clearline();
1774}
1775
1776#if (defined(FEAT_CINDENT) && defined(FEAT_EVAL)) || defined(PROTO)
1777/*
1778 * Get indent level from 'indentexpr'.
1779 */
1780 int
1781get_expr_indent(void)
1782{
1783 int indent = -1;
1784 char_u *inde_copy;
1785 pos_T save_pos;
1786 colnr_T save_curswant;
1787 int save_set_curswant;
1788 int save_State;
1789 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1790 OPT_LOCAL);
1791
1792 // Save and restore cursor position and curswant, in case it was changed
1793 // via :normal commands
1794 save_pos = curwin->w_cursor;
1795 save_curswant = curwin->w_curswant;
1796 save_set_curswant = curwin->w_set_curswant;
1797 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1798 if (use_sandbox)
1799 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001800 ++textwinlock;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001801
1802 // Need to make a copy, the 'indentexpr' option could be changed while
1803 // evaluating it.
1804 inde_copy = vim_strsave(curbuf->b_p_inde);
1805 if (inde_copy != NULL)
1806 {
1807 indent = (int)eval_to_number(inde_copy);
1808 vim_free(inde_copy);
1809 }
1810
1811 if (use_sandbox)
1812 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001813 --textwinlock;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001814
1815 // Restore the cursor position so that 'indentexpr' doesn't need to.
1816 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1817 // command.
1818 save_State = State;
1819 State = INSERT;
1820 curwin->w_cursor = save_pos;
1821 curwin->w_curswant = save_curswant;
1822 curwin->w_set_curswant = save_set_curswant;
1823 check_cursor();
1824 State = save_State;
1825
Bram Moolenaar620c9592021-07-31 21:32:31 +02001826 // Reset did_throw, unless 'debug' has "throw" and inside a try/catch.
1827 if (did_throw && (vim_strchr(p_debug, 't') == NULL || trylevel == 0))
1828 {
1829 handle_did_throw();
1830 did_throw = FALSE;
1831 }
1832
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001833 // If there is an error, just keep the current indent.
1834 if (indent < 0)
1835 indent = get_indent();
1836
1837 return indent;
1838}
1839#endif
1840
1841#if defined(FEAT_LISP) || defined(PROTO)
1842
1843 static int
1844lisp_match(char_u *p)
1845{
1846 char_u buf[LSIZE];
1847 int len;
1848 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1849
1850 while (*word != NUL)
1851 {
1852 (void)copy_option_part(&word, buf, LSIZE, ",");
1853 len = (int)STRLEN(buf);
1854 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
1855 return TRUE;
1856 }
1857 return FALSE;
1858}
1859
1860/*
1861 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
1862 * The incompatible newer method is quite a bit better at indenting
1863 * code in lisp-like languages than the traditional one; it's still
1864 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
1865 *
1866 * TODO:
1867 * Findmatch() should be adapted for lisp, also to make showmatch
1868 * work correctly: now (v5.3) it seems all C/C++ oriented:
1869 * - it does not recognize the #\( and #\) notations as character literals
1870 * - it doesn't know about comments starting with a semicolon
1871 * - it incorrectly interprets '(' as a character literal
1872 * All this messes up get_lisp_indent in some rare cases.
1873 * Update from Sergey Khorev:
1874 * I tried to fix the first two issues.
1875 */
1876 int
1877get_lisp_indent(void)
1878{
1879 pos_T *pos, realpos, paren;
1880 int amount;
1881 char_u *that;
1882 colnr_T col;
1883 colnr_T firsttry;
1884 int parencount, quotecount;
1885 int vi_lisp;
1886
1887 // Set vi_lisp to use the vi-compatible method
1888 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
1889
1890 realpos = curwin->w_cursor;
1891 curwin->w_cursor.col = 0;
1892
1893 if ((pos = findmatch(NULL, '(')) == NULL)
1894 pos = findmatch(NULL, '[');
1895 else
1896 {
1897 paren = *pos;
1898 pos = findmatch(NULL, '[');
1899 if (pos == NULL || LT_POSP(pos, &paren))
1900 pos = &paren;
1901 }
1902 if (pos != NULL)
1903 {
1904 // Extra trick: Take the indent of the first previous non-white
1905 // line that is at the same () level.
1906 amount = -1;
1907 parencount = 0;
1908
1909 while (--curwin->w_cursor.lnum >= pos->lnum)
1910 {
1911 if (linewhite(curwin->w_cursor.lnum))
1912 continue;
1913 for (that = ml_get_curline(); *that != NUL; ++that)
1914 {
1915 if (*that == ';')
1916 {
1917 while (*(that + 1) != NUL)
1918 ++that;
1919 continue;
1920 }
1921 if (*that == '\\')
1922 {
1923 if (*(that + 1) != NUL)
1924 ++that;
1925 continue;
1926 }
1927 if (*that == '"' && *(that + 1) != NUL)
1928 {
1929 while (*++that && *that != '"')
1930 {
1931 // skipping escaped characters in the string
1932 if (*that == '\\')
1933 {
1934 if (*++that == NUL)
1935 break;
1936 if (that[1] == NUL)
1937 {
1938 ++that;
1939 break;
1940 }
1941 }
1942 }
1943 }
1944 if (*that == '(' || *that == '[')
1945 ++parencount;
1946 else if (*that == ')' || *that == ']')
1947 --parencount;
1948 }
1949 if (parencount == 0)
1950 {
1951 amount = get_indent();
1952 break;
1953 }
1954 }
1955
1956 if (amount == -1)
1957 {
1958 curwin->w_cursor.lnum = pos->lnum;
1959 curwin->w_cursor.col = pos->col;
1960 col = pos->col;
1961
1962 that = ml_get_curline();
1963
1964 if (vi_lisp && get_indent() == 0)
1965 amount = 2;
1966 else
1967 {
1968 char_u *line = that;
1969
1970 amount = 0;
1971 while (*that && col)
1972 {
1973 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
1974 col--;
1975 }
1976
1977 // Some keywords require "body" indenting rules (the
1978 // non-standard-lisp ones are Scheme special forms):
1979 //
1980 // (let ((a 1)) instead (let ((a 1))
1981 // (...)) of (...))
1982
1983 if (!vi_lisp && (*that == '(' || *that == '[')
1984 && lisp_match(that + 1))
1985 amount += 2;
1986 else
1987 {
1988 that++;
1989 amount++;
1990 firsttry = amount;
1991
1992 while (VIM_ISWHITE(*that))
1993 {
1994 amount += lbr_chartabsize(line, that, (colnr_T)amount);
1995 ++that;
1996 }
1997
1998 if (*that && *that != ';') // not a comment line
1999 {
2000 // test *that != '(' to accommodate first let/do
2001 // argument if it is more than one line
2002 if (!vi_lisp && *that != '(' && *that != '[')
2003 firsttry++;
2004
2005 parencount = 0;
2006 quotecount = 0;
2007
2008 if (vi_lisp
2009 || (*that != '"'
2010 && *that != '\''
2011 && *that != '#'
2012 && (*that < '0' || *that > '9')))
2013 {
2014 while (*that
2015 && (!VIM_ISWHITE(*that)
2016 || quotecount
2017 || parencount)
2018 && (!((*that == '(' || *that == '[')
2019 && !quotecount
2020 && !parencount
2021 && vi_lisp)))
2022 {
2023 if (*that == '"')
2024 quotecount = !quotecount;
2025 if ((*that == '(' || *that == '[')
2026 && !quotecount)
2027 ++parencount;
2028 if ((*that == ')' || *that == ']')
2029 && !quotecount)
2030 --parencount;
2031 if (*that == '\\' && *(that+1) != NUL)
2032 amount += lbr_chartabsize_adv(
2033 line, &that, (colnr_T)amount);
2034 amount += lbr_chartabsize_adv(
2035 line, &that, (colnr_T)amount);
2036 }
2037 }
2038 while (VIM_ISWHITE(*that))
2039 {
2040 amount += lbr_chartabsize(
2041 line, that, (colnr_T)amount);
2042 that++;
2043 }
2044 if (!*that || *that == ';')
2045 amount = firsttry;
2046 }
2047 }
2048 }
2049 }
2050 }
2051 else
2052 amount = 0; // no matching '(' or '[' found, use zero indent
2053
2054 curwin->w_cursor = realpos;
2055
2056 return amount;
2057}
2058#endif // FEAT_LISP
2059
2060#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
2061/*
2062 * Re-indent the current line, based on the current contents of it and the
2063 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
2064 * confused what all the part that handles Control-T is doing that I'm not.
2065 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
2066 */
2067
2068 void
2069fixthisline(int (*get_the_indent)(void))
2070{
2071 int amount = get_the_indent();
2072
2073 if (amount >= 0)
2074 {
2075 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
2076 if (linewhite(curwin->w_cursor.lnum))
2077 did_ai = TRUE; // delete the indent if the line stays empty
2078 }
2079}
2080
2081 void
2082fix_indent(void)
2083{
2084 if (p_paste)
2085 return;
2086# ifdef FEAT_LISP
2087 if (curbuf->b_p_lisp && curbuf->b_p_ai)
2088 fixthisline(get_lisp_indent);
2089# endif
2090# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
2091 else
2092# endif
2093# ifdef FEAT_CINDENT
2094 if (cindent_on())
2095 do_c_expr_indent();
2096# endif
2097}
2098#endif
2099
2100#if defined(FEAT_EVAL) || defined(PROTO)
2101/*
2102 * "indent()" function
2103 */
2104 void
2105f_indent(typval_T *argvars, typval_T *rettv)
2106{
2107 linenr_T lnum;
2108
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002109 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2110 return;
2111
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002112 lnum = tv_get_lnum(argvars);
2113 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2114 rettv->vval.v_number = get_indent_lnum(lnum);
2115 else
2116 rettv->vval.v_number = -1;
2117}
2118
2119/*
2120 * "lispindent(lnum)" function
2121 */
2122 void
2123f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2124{
2125#ifdef FEAT_LISP
2126 pos_T pos;
2127 linenr_T lnum;
2128
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002129 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2130 return;
2131
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002132 pos = curwin->w_cursor;
2133 lnum = tv_get_lnum(argvars);
2134 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2135 {
2136 curwin->w_cursor.lnum = lnum;
2137 rettv->vval.v_number = get_lisp_indent();
2138 curwin->w_cursor = pos;
2139 }
2140 else
2141#endif
2142 rettv->vval.v_number = -1;
2143}
2144#endif