blob: 61497eb4f5083d4ff628ac1c36e9919fd5bf185c [file] [log] [blame]
Bram Moolenaar4b471622019-01-31 13:48:09 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * indent.c: Indentation related functions
12 */
13
14#include "vim.h"
15
Bram Moolenaare677df82019-09-02 22:31:11 +020016#if defined(FEAT_VARTABS) || defined(PROTO)
17
18/*
19 * Set the integer values corresponding to the string setting of 'vartabstop'.
20 * "array" will be set, caller must free it if needed.
Bram Moolenaarb7081e12021-09-04 18:47:28 +020021 * Return FAIL for an error.
Bram Moolenaare677df82019-09-02 22:31:11 +020022 */
23 int
24tabstop_set(char_u *var, int **array)
25{
Bram Moolenaarb7081e12021-09-04 18:47:28 +020026 int valcount = 1;
27 int t;
28 char_u *cp;
Bram Moolenaare677df82019-09-02 22:31:11 +020029
30 if (var[0] == NUL || (var[0] == '0' && var[1] == NUL))
31 {
32 *array = NULL;
Bram Moolenaarb7081e12021-09-04 18:47:28 +020033 return OK;
Bram Moolenaare677df82019-09-02 22:31:11 +020034 }
35
36 for (cp = var; *cp != NUL; ++cp)
37 {
38 if (cp == var || cp[-1] == ',')
39 {
40 char_u *end;
41
42 if (strtol((char *)cp, (char **)&end, 10) <= 0)
43 {
44 if (cp != end)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +000045 emsg(_(e_argument_must_be_positive));
Bram Moolenaare677df82019-09-02 22:31:11 +020046 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000047 semsg(_(e_invalid_argument_str), cp);
Bram Moolenaarb7081e12021-09-04 18:47:28 +020048 return FAIL;
Bram Moolenaare677df82019-09-02 22:31:11 +020049 }
50 }
51
52 if (VIM_ISDIGIT(*cp))
53 continue;
54 if (cp[0] == ',' && cp > var && cp[-1] != ',' && cp[1] != NUL)
55 {
56 ++valcount;
57 continue;
58 }
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000059 semsg(_(e_invalid_argument_str), var);
Bram Moolenaarb7081e12021-09-04 18:47:28 +020060 return FAIL;
Bram Moolenaare677df82019-09-02 22:31:11 +020061 }
62
63 *array = ALLOC_MULT(int, valcount + 1);
64 if (*array == NULL)
Bram Moolenaarb7081e12021-09-04 18:47:28 +020065 return FAIL;
Bram Moolenaare677df82019-09-02 22:31:11 +020066 (*array)[0] = valcount;
67
68 t = 1;
69 for (cp = var; *cp != NUL;)
70 {
Bram Moolenaarb7081e12021-09-04 18:47:28 +020071 int n = atoi((char *)cp);
72
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +020073 // Catch negative values, overflow and ridiculous big values.
Bram Moolenaarfc88df42022-02-05 11:13:05 +000074 if (n <= 0 || n > TABSTOP_MAX)
Bram Moolenaarb7081e12021-09-04 18:47:28 +020075 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000076 semsg(_(e_invalid_argument_str), cp);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +020077 vim_free(*array);
78 *array = NULL;
Bram Moolenaarb7081e12021-09-04 18:47:28 +020079 return FAIL;
80 }
81 (*array)[t++] = n;
82 while (*cp != NUL && *cp != ',')
Bram Moolenaare677df82019-09-02 22:31:11 +020083 ++cp;
84 if (*cp != NUL)
85 ++cp;
86 }
87
Bram Moolenaarb7081e12021-09-04 18:47:28 +020088 return OK;
Bram Moolenaare677df82019-09-02 22:31:11 +020089}
90
91/*
92 * Calculate the number of screen spaces a tab will occupy.
93 * If "vts" is set then the tab widths are taken from that array,
94 * otherwise the value of ts is used.
95 */
96 int
97tabstop_padding(colnr_T col, int ts_arg, int *vts)
98{
99 int ts = ts_arg == 0 ? 8 : ts_arg;
100 int tabcount;
101 colnr_T tabcol = 0;
102 int t;
103 int padding = 0;
104
105 if (vts == NULL || vts[0] == 0)
106 return ts - (col % ts);
107
108 tabcount = vts[0];
109
110 for (t = 1; t <= tabcount; ++t)
111 {
112 tabcol += vts[t];
113 if (tabcol > col)
114 {
115 padding = (int)(tabcol - col);
116 break;
117 }
118 }
119 if (t > tabcount)
120 padding = vts[tabcount] - (int)((col - tabcol) % vts[tabcount]);
121
122 return padding;
123}
124
125/*
126 * Find the size of the tab that covers a particular column.
127 */
128 int
129tabstop_at(colnr_T col, int ts, int *vts)
130{
131 int tabcount;
132 colnr_T tabcol = 0;
133 int t;
134 int tab_size = 0;
135
136 if (vts == 0 || vts[0] == 0)
137 return ts;
138
139 tabcount = vts[0];
140 for (t = 1; t <= tabcount; ++t)
141 {
142 tabcol += vts[t];
143 if (tabcol > col)
144 {
145 tab_size = vts[t];
146 break;
147 }
148 }
149 if (t > tabcount)
150 tab_size = vts[tabcount];
151
152 return tab_size;
153}
154
155/*
156 * Find the column on which a tab starts.
157 */
158 colnr_T
159tabstop_start(colnr_T col, int ts, int *vts)
160{
161 int tabcount;
162 colnr_T tabcol = 0;
163 int t;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100164 int excess;
Bram Moolenaare677df82019-09-02 22:31:11 +0200165
166 if (vts == NULL || vts[0] == 0)
167 return (col / ts) * ts;
168
169 tabcount = vts[0];
170 for (t = 1; t <= tabcount; ++t)
171 {
172 tabcol += vts[t];
173 if (tabcol > col)
174 return tabcol - vts[t];
175 }
176
177 excess = tabcol % vts[tabcount];
178 return excess + ((col - excess) / vts[tabcount]) * vts[tabcount];
179}
180
181/*
182 * Find the number of tabs and spaces necessary to get from one column
183 * to another.
184 */
185 void
186tabstop_fromto(
187 colnr_T start_col,
188 colnr_T end_col,
189 int ts_arg,
190 int *vts,
191 int *ntabs,
192 int *nspcs)
193{
194 int spaces = end_col - start_col;
195 colnr_T tabcol = 0;
196 int padding = 0;
197 int tabcount;
198 int t;
199 int ts = ts_arg == 0 ? curbuf->b_p_ts : ts_arg;
200
201 if (vts == NULL || vts[0] == 0)
202 {
203 int tabs = 0;
204 int initspc = 0;
205
206 initspc = ts - (start_col % ts);
207 if (spaces >= initspc)
208 {
209 spaces -= initspc;
210 tabs++;
211 }
212 tabs += spaces / ts;
213 spaces -= (spaces / ts) * ts;
214
215 *ntabs = tabs;
216 *nspcs = spaces;
217 return;
218 }
219
220 // Find the padding needed to reach the next tabstop.
221 tabcount = vts[0];
222 for (t = 1; t <= tabcount; ++t)
223 {
224 tabcol += vts[t];
225 if (tabcol > start_col)
226 {
227 padding = (int)(tabcol - start_col);
228 break;
229 }
230 }
231 if (t > tabcount)
232 padding = vts[tabcount] - (int)((start_col - tabcol) % vts[tabcount]);
233
234 // If the space needed is less than the padding no tabs can be used.
235 if (spaces < padding)
236 {
237 *ntabs = 0;
238 *nspcs = spaces;
239 return;
240 }
241
242 *ntabs = 1;
243 spaces -= padding;
244
245 // At least one tab has been used. See if any more will fit.
246 while (spaces != 0 && ++t <= tabcount)
247 {
248 padding = vts[t];
249 if (spaces < padding)
250 {
251 *nspcs = spaces;
252 return;
253 }
254 ++*ntabs;
255 spaces -= padding;
256 }
257
258 *ntabs += spaces / vts[tabcount];
259 *nspcs = spaces % vts[tabcount];
260}
261
262/*
263 * See if two tabstop arrays contain the same values.
264 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200265 static int
Bram Moolenaare677df82019-09-02 22:31:11 +0200266tabstop_eq(int *ts1, int *ts2)
267{
268 int t;
269
270 if ((ts1 == 0 && ts2) || (ts1 && ts2 == 0))
271 return FALSE;
272 if (ts1 == ts2)
273 return TRUE;
274 if (ts1[0] != ts2[0])
275 return FALSE;
276
277 for (t = 1; t <= ts1[0]; ++t)
278 if (ts1[t] != ts2[t])
279 return FALSE;
280
281 return TRUE;
282}
283
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200284# if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaare677df82019-09-02 22:31:11 +0200285/*
286 * Copy a tabstop array, allocating space for the new array.
287 */
288 int *
289tabstop_copy(int *oldts)
290{
291 int *newts;
292 int t;
293
294 if (oldts == NULL)
295 return NULL;
296 newts = ALLOC_MULT(int, oldts[0] + 1);
297 if (newts != NULL)
298 for (t = 0; t <= oldts[0]; ++t)
299 newts[t] = oldts[t];
300 return newts;
301}
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200302# endif
Bram Moolenaare677df82019-09-02 22:31:11 +0200303
304/*
305 * Return a count of the number of tabstops.
306 */
307 int
308tabstop_count(int *ts)
309{
310 return ts != NULL ? ts[0] : 0;
311}
312
313/*
314 * Return the first tabstop, or 8 if there are no tabstops defined.
315 */
316 int
317tabstop_first(int *ts)
318{
319 return ts != NULL ? ts[1] : 8;
320}
321
322#endif
323
324/*
325 * Return the effective shiftwidth value for current buffer, using the
326 * 'tabstop' value when 'shiftwidth' is zero.
327 */
328 long
329get_sw_value(buf_T *buf)
330{
331 return get_sw_value_col(buf, 0);
332}
333
334/*
335 * Idem, using "pos".
336 */
337 static long
338get_sw_value_pos(buf_T *buf, pos_T *pos)
339{
340 pos_T save_cursor = curwin->w_cursor;
341 long sw_value;
342
343 curwin->w_cursor = *pos;
344 sw_value = get_sw_value_col(buf, get_nolist_virtcol());
345 curwin->w_cursor = save_cursor;
346 return sw_value;
347}
348
349/*
350 * Idem, using the first non-black in the current line.
351 */
352 long
353get_sw_value_indent(buf_T *buf)
354{
355 pos_T pos = curwin->w_cursor;
356
357 pos.col = getwhitecols_curline();
358 return get_sw_value_pos(buf, &pos);
359}
360
361/*
362 * Idem, using virtual column "col".
363 */
364 long
365get_sw_value_col(buf_T *buf, colnr_T col UNUSED)
366{
367 return buf->b_p_sw ? buf->b_p_sw :
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200368#ifdef FEAT_VARTABS
Bram Moolenaare677df82019-09-02 22:31:11 +0200369 tabstop_at(col, buf->b_p_ts, buf->b_p_vts_array);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200370#else
Bram Moolenaare677df82019-09-02 22:31:11 +0200371 buf->b_p_ts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200372#endif
Bram Moolenaare677df82019-09-02 22:31:11 +0200373}
374
375/*
376 * Return the effective softtabstop value for the current buffer, using the
377 * 'shiftwidth' value when 'softtabstop' is negative.
378 */
379 long
380get_sts_value(void)
381{
382 return curbuf->b_p_sts < 0 ? get_sw_value(curbuf) : curbuf->b_p_sts;
383}
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200384
385/*
386 * Count the size (in window cells) of the indent in the current line.
387 */
388 int
389get_indent(void)
390{
391#ifdef FEAT_VARTABS
392 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
393 curbuf->b_p_vts_array, FALSE);
394#else
395 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
396#endif
397}
398
399/*
400 * Count the size (in window cells) of the indent in line "lnum".
401 */
402 int
403get_indent_lnum(linenr_T lnum)
404{
405#ifdef FEAT_VARTABS
406 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
407 curbuf->b_p_vts_array, FALSE);
408#else
409 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
410#endif
411}
412
413#if defined(FEAT_FOLDING) || defined(PROTO)
414/*
415 * Count the size (in window cells) of the indent in line "lnum" of buffer
416 * "buf".
417 */
418 int
419get_indent_buf(buf_T *buf, linenr_T lnum)
420{
421# ifdef FEAT_VARTABS
422 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
423 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
424# else
425 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
426# endif
427}
428#endif
429
430/*
431 * count the size (in window cells) of the indent in line "ptr", with
432 * 'tabstop' at "ts"
433 */
434 int
435get_indent_str(
436 char_u *ptr,
437 int ts,
438 int list) // if TRUE, count only screen size for tabs
439{
440 int count = 0;
441
442 for ( ; *ptr; ++ptr)
443 {
444 if (*ptr == TAB)
445 {
Bram Moolenaareed9d462021-02-15 20:38:25 +0100446 if (!list || curwin->w_lcs_chars.tab1)
447 // count a tab for what it is worth
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200448 count += ts - (count % ts);
449 else
450 // In list mode, when tab is not set, count screen char width
451 // for Tab, displays: ^I
452 count += ptr2cells(ptr);
453 }
454 else if (*ptr == ' ')
455 ++count; // count a space for one
456 else
457 break;
458 }
459 return count;
460}
461
462#ifdef FEAT_VARTABS
463/*
464 * Count the size (in window cells) of the indent in line "ptr", using
465 * variable tabstops.
466 * if "list" is TRUE, count only screen size for tabs.
467 */
468 int
469get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
470{
471 int count = 0;
472
473 for ( ; *ptr; ++ptr)
474 {
475 if (*ptr == TAB) // count a tab for what it is worth
476 {
Bram Moolenaareed9d462021-02-15 20:38:25 +0100477 if (!list || curwin->w_lcs_chars.tab1)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200478 count += tabstop_padding(count, ts, vts);
479 else
480 // In list mode, when tab is not set, count screen char width
481 // for Tab, displays: ^I
482 count += ptr2cells(ptr);
483 }
484 else if (*ptr == ' ')
485 ++count; // count a space for one
486 else
487 break;
488 }
489 return count;
490}
491#endif
492
493/*
494 * Set the indent of the current line.
495 * Leaves the cursor on the first non-blank in the line.
496 * Caller must take care of undo.
497 * "flags":
498 * SIN_CHANGED: call changed_bytes() if the line was changed.
499 * SIN_INSERT: insert the indent in front of the line.
500 * SIN_UNDO: save line for undo before changing it.
501 * Returns TRUE if the line was changed.
502 */
503 int
504set_indent(
505 int size, // measured in spaces
506 int flags)
507{
508 char_u *p;
509 char_u *newline;
510 char_u *oldline;
511 char_u *s;
512 int todo;
513 int ind_len; // measured in characters
514 int line_len;
515 int doit = FALSE;
516 int ind_done = 0; // measured in spaces
517#ifdef FEAT_VARTABS
518 int ind_col = 0;
519#endif
520 int tab_pad;
521 int retval = FALSE;
522 int orig_char_len = -1; // number of initial whitespace chars when
523 // 'et' and 'pi' are both set
524
525 // First check if there is anything to do and compute the number of
526 // characters needed for the indent.
527 todo = size;
528 ind_len = 0;
529 p = oldline = ml_get_curline();
530
531 // Calculate the buffer size for the new indent, and check to see if it
532 // isn't already set
533
534 // if 'expandtab' isn't set: use TABs; if both 'expandtab' and
535 // 'preserveindent' are set count the number of characters at the
536 // beginning of the line to be copied
537 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
538 {
539 // If 'preserveindent' is set then reuse as much as possible of
540 // the existing indent structure for the new indent
541 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
542 {
543 ind_done = 0;
544
545 // count as many characters as we can use
546 while (todo > 0 && VIM_ISWHITE(*p))
547 {
548 if (*p == TAB)
549 {
550#ifdef FEAT_VARTABS
551 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
552 curbuf->b_p_vts_array);
553#else
554 tab_pad = (int)curbuf->b_p_ts
555 - (ind_done % (int)curbuf->b_p_ts);
556#endif
557 // stop if this tab will overshoot the target
558 if (todo < tab_pad)
559 break;
560 todo -= tab_pad;
561 ++ind_len;
562 ind_done += tab_pad;
563 }
564 else
565 {
566 --todo;
567 ++ind_len;
568 ++ind_done;
569 }
570 ++p;
571 }
572
573#ifdef FEAT_VARTABS
574 // These diverge from this point.
575 ind_col = ind_done;
576#endif
577 // Set initial number of whitespace chars to copy if we are
578 // preserving indent but expandtab is set
579 if (curbuf->b_p_et)
580 orig_char_len = ind_len;
581
582 // Fill to next tabstop with a tab, if possible
583#ifdef FEAT_VARTABS
584 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
585 curbuf->b_p_vts_array);
586#else
587 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
588#endif
589 if (todo >= tab_pad && orig_char_len == -1)
590 {
591 doit = TRUE;
592 todo -= tab_pad;
593 ++ind_len;
594 // ind_done += tab_pad;
595#ifdef FEAT_VARTABS
596 ind_col += tab_pad;
597#endif
598 }
599 }
600
601 // count tabs required for indent
602#ifdef FEAT_VARTABS
603 for (;;)
604 {
605 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
606 curbuf->b_p_vts_array);
607 if (todo < tab_pad)
608 break;
609 if (*p != TAB)
610 doit = TRUE;
611 else
612 ++p;
613 todo -= tab_pad;
614 ++ind_len;
615 ind_col += tab_pad;
616 }
617#else
618 while (todo >= (int)curbuf->b_p_ts)
619 {
620 if (*p != TAB)
621 doit = TRUE;
622 else
623 ++p;
624 todo -= (int)curbuf->b_p_ts;
625 ++ind_len;
626 // ind_done += (int)curbuf->b_p_ts;
627 }
628#endif
629 }
630 // count spaces required for indent
631 while (todo > 0)
632 {
633 if (*p != ' ')
634 doit = TRUE;
635 else
636 ++p;
637 --todo;
638 ++ind_len;
639 // ++ind_done;
640 }
641
642 // Return if the indent is OK already.
643 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
644 return FALSE;
645
646 // Allocate memory for the new line.
647 if (flags & SIN_INSERT)
648 p = oldline;
649 else
650 p = skipwhite(p);
651 line_len = (int)STRLEN(p) + 1;
652
653 // If 'preserveindent' and 'expandtab' are both set keep the original
654 // characters and allocate accordingly. We will fill the rest with spaces
655 // after the if (!curbuf->b_p_et) below.
656 if (orig_char_len != -1)
657 {
658 newline = alloc(orig_char_len + size - ind_done + line_len);
659 if (newline == NULL)
660 return FALSE;
661 todo = size - ind_done;
662 ind_len = orig_char_len + todo; // Set total length of indent in
663 // characters, which may have been
664 // undercounted until now
665 p = oldline;
666 s = newline;
667 while (orig_char_len > 0)
668 {
669 *s++ = *p++;
670 orig_char_len--;
671 }
672
673 // Skip over any additional white space (useful when newindent is less
674 // than old)
675 while (VIM_ISWHITE(*p))
676 ++p;
677
678 }
679 else
680 {
681 todo = size;
682 newline = alloc(ind_len + line_len);
683 if (newline == NULL)
684 return FALSE;
685 s = newline;
686 }
687
688 // Put the characters in the new line.
689 // if 'expandtab' isn't set: use TABs
690 if (!curbuf->b_p_et)
691 {
692 // If 'preserveindent' is set then reuse as much as possible of
693 // the existing indent structure for the new indent
694 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
695 {
696 p = oldline;
697 ind_done = 0;
698
699 while (todo > 0 && VIM_ISWHITE(*p))
700 {
701 if (*p == TAB)
702 {
703#ifdef FEAT_VARTABS
704 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
705 curbuf->b_p_vts_array);
706#else
707 tab_pad = (int)curbuf->b_p_ts
708 - (ind_done % (int)curbuf->b_p_ts);
709#endif
710 // stop if this tab will overshoot the target
711 if (todo < tab_pad)
712 break;
713 todo -= tab_pad;
714 ind_done += tab_pad;
715 }
716 else
717 {
718 --todo;
719 ++ind_done;
720 }
721 *s++ = *p++;
722 }
723
724 // Fill to next tabstop with a tab, if possible
725#ifdef FEAT_VARTABS
726 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
727 curbuf->b_p_vts_array);
728#else
729 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
730#endif
731 if (todo >= tab_pad)
732 {
733 *s++ = TAB;
734 todo -= tab_pad;
735#ifdef FEAT_VARTABS
736 ind_done += tab_pad;
737#endif
738 }
739
740 p = skipwhite(p);
741 }
742
743#ifdef FEAT_VARTABS
744 for (;;)
745 {
746 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
747 curbuf->b_p_vts_array);
748 if (todo < tab_pad)
749 break;
750 *s++ = TAB;
751 todo -= tab_pad;
752 ind_done += tab_pad;
753 }
754#else
755 while (todo >= (int)curbuf->b_p_ts)
756 {
757 *s++ = TAB;
758 todo -= (int)curbuf->b_p_ts;
759 }
760#endif
761 }
762 while (todo > 0)
763 {
764 *s++ = ' ';
765 --todo;
766 }
767 mch_memmove(s, p, (size_t)line_len);
768
769 // Replace the line (unless undo fails).
770 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
771 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200772 colnr_T old_offset = (colnr_T)(p - oldline);
773 colnr_T new_offset = (colnr_T)(s - newline);
774
775 // this may free "newline"
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200776 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
777 if (flags & SIN_CHANGED)
778 changed_bytes(curwin->w_cursor.lnum, 0);
779
780 // Correct saved cursor position if it is in this line.
781 if (saved_cursor.lnum == curwin->w_cursor.lnum)
782 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200783 if (saved_cursor.col >= old_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200784 // cursor was after the indent, adjust for the number of
785 // bytes added/removed
Bram Moolenaarcf306432020-06-29 20:40:37 +0200786 saved_cursor.col += ind_len - old_offset;
787 else if (saved_cursor.col >= new_offset)
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200788 // cursor was in the indent, and is now after it, put it back
789 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaarcf306432020-06-29 20:40:37 +0200790 saved_cursor.col = new_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200791 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100792#ifdef FEAT_PROP_POPUP
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200793 {
Bram Moolenaarcf306432020-06-29 20:40:37 +0200794 int added = ind_len - old_offset;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200795
796 // When increasing indent this behaves like spaces were inserted at
797 // the old indent, when decreasing indent it behaves like spaces
798 // were deleted at the new indent.
799 adjust_prop_columns(curwin->w_cursor.lnum,
Bram Moolenaarcf306432020-06-29 20:40:37 +0200800 added > 0 ? old_offset : (colnr_T)ind_len, added, 0);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200801 }
802#endif
803 retval = TRUE;
804 }
805 else
806 vim_free(newline);
807
808 curwin->w_cursor.col = ind_len;
809 return retval;
810}
811
812/*
813 * Return the indent of the current line after a number. Return -1 if no
814 * number was found. Used for 'n' in 'formatoptions': numbered list.
815 * Since a pattern is used it can actually handle more than numbers.
816 */
817 int
818get_number_indent(linenr_T lnum)
819{
820 colnr_T col;
821 pos_T pos;
822
823 regmatch_T regmatch;
824 int lead_len = 0; // length of comment leader
825
826 if (lnum > curbuf->b_ml.ml_line_count)
827 return -1;
828 pos.lnum = 0;
829
830 // In format_lines() (i.e. not insert mode), fo+=q is needed too...
Bram Moolenaar24959102022-05-07 20:01:16 +0100831 if ((State & MODE_INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200832 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
833
834 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
835 if (regmatch.regprog != NULL)
836 {
837 regmatch.rm_ic = FALSE;
838
839 // vim_regexec() expects a pointer to a line. This lets us
840 // start matching for the flp beyond any comment leader...
841 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
842 {
843 pos.lnum = lnum;
844 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
845 pos.coladd = 0;
846 }
847 vim_regfree(regmatch.regprog);
848 }
849
850 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
851 return -1;
852 getvcol(curwin, &pos, &col, NULL, NULL);
853 return (int)col;
854}
855
856#if defined(FEAT_LINEBREAK) || defined(PROTO)
857/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100858 * This is called when 'breakindentopt' is changed and when a window is
859 * initialized.
860 */
861 int
862briopt_check(win_T *wp)
863{
864 char_u *p;
865 int bri_shift = 0;
866 long bri_min = 20;
867 int bri_sbr = FALSE;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200868 int bri_list = 0;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100869 int bri_vcol = 0;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100870
871 p = wp->w_p_briopt;
872 while (*p != NUL)
873 {
874 if (STRNCMP(p, "shift:", 6) == 0
875 && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
876 {
877 p += 6;
878 bri_shift = getdigits(&p);
879 }
880 else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4]))
881 {
882 p += 4;
883 bri_min = getdigits(&p);
884 }
885 else if (STRNCMP(p, "sbr", 3) == 0)
886 {
887 p += 3;
888 bri_sbr = TRUE;
889 }
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200890 else if (STRNCMP(p, "list:", 5) == 0)
891 {
892 p += 5;
893 bri_list = getdigits(&p);
894 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100895 else if (STRNCMP(p, "column:", 7) == 0)
896 {
897 p += 7;
898 bri_vcol = getdigits(&p);
899 }
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100900 if (*p != ',' && *p != NUL)
901 return FAIL;
902 if (*p == ',')
903 ++p;
904 }
905
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100906 wp->w_briopt_shift = bri_shift;
907 wp->w_briopt_min = bri_min;
908 wp->w_briopt_sbr = bri_sbr;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200909 wp->w_briopt_list = bri_list;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100910 wp->w_briopt_vcol = bri_vcol;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100911
912 return OK;
913}
914
915/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200916 * Return appropriate space number for breakindent, taking influencing
917 * parameters into account. Window must be specified, since it is not
918 * necessarily always the current one.
919 */
920 int
921get_breakindent_win(
922 win_T *wp,
923 char_u *line) // start of the line
924{
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000925 static int prev_indent = 0; // cached indent value
926 static long prev_ts = 0L; // cached tabstop value
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100927 static int prev_fnum = 0; // cached buffer number
928 static char_u *prev_line = NULL; // cached copy of "line"
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200929 static varnumber_T prev_tick = 0; // changedtick of cached value
930# ifdef FEAT_VARTABS
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000931 static int *prev_vts = NULL; // cached vartabs values
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200932# endif
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000933 static int prev_list = 0; // cached list value
934 static int prev_listopt = 0; // cached w_p_briopt_list value
Christian Brabandtc53b4672022-01-15 10:01:05 +0000935 // cached formatlistpat value
936 static char_u *prev_flp = NULL;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200937 int bri = 0;
938 // window width minus window margin space, i.e. what rests for text
939 const int eff_wwidth = wp->w_width
940 - ((wp->w_p_nu || wp->w_p_rnu)
941 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
942 ? number_width(wp) + 1 : 0);
943
Christian Brabandtc53b4672022-01-15 10:01:05 +0000944 // used cached indent, unless
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100945 // - buffer changed
Christian Brabandtc53b4672022-01-15 10:01:05 +0000946 // - 'tabstop' changed
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100947 // - buffer was changed
Christian Brabandtc53b4672022-01-15 10:01:05 +0000948 // - 'briopt_list changed' changed or
949 // - 'formatlistpattern' changed
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100950 // - line changed
951 // - 'vartabs' changed
952 if (prev_fnum != wp->w_buffer->b_fnum
953 || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200954 || prev_tick != CHANGEDTICK(wp->w_buffer)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000955 || prev_listopt != wp->w_briopt_list
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100956 || prev_flp == NULL
957 || STRCMP(prev_flp, get_flp_value(wp->w_buffer)) != 0
958 || prev_line == NULL || STRCMP(prev_line, line) != 0
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200959# ifdef FEAT_VARTABS
960 || prev_vts != wp->w_buffer->b_p_vts_array
961# endif
962 )
963 {
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100964 prev_fnum = wp->w_buffer->b_fnum;
965 vim_free(prev_line);
966 prev_line = vim_strsave(line);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200967 prev_ts = wp->w_buffer->b_p_ts;
968 prev_tick = CHANGEDTICK(wp->w_buffer);
969# ifdef FEAT_VARTABS
970 prev_vts = wp->w_buffer->b_p_vts_array;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100971 if (wp->w_briopt_vcol == 0)
972 prev_indent = get_indent_str_vtab(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200973 (int)wp->w_buffer->b_p_ts,
974 wp->w_buffer->b_p_vts_array, wp->w_p_list);
975# else
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100976 if (wp->w_briopt_vcol == 0)
977 prev_indent = get_indent_str(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200978 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
979# endif
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000980 prev_listopt = wp->w_briopt_list;
Christian Brabandtc53b4672022-01-15 10:01:05 +0000981 prev_list = 0;
982 vim_free(prev_flp);
983 prev_flp = vim_strsave(get_flp_value(wp->w_buffer));
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000984 // add additional indent for numbered lists
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100985 if (wp->w_briopt_list != 0 && wp->w_briopt_vcol == 0)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000986 {
987 regmatch_T regmatch;
988
Christian Brabandtc53b4672022-01-15 10:01:05 +0000989 regmatch.regprog = vim_regcomp(prev_flp,
990 RE_MAGIC + RE_STRING + RE_AUTO + RE_STRICT);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000991
992 if (regmatch.regprog != NULL)
993 {
994 regmatch.rm_ic = FALSE;
995 if (vim_regexec(&regmatch, line, 0))
996 {
997 if (wp->w_briopt_list > 0)
998 prev_list = wp->w_briopt_list;
999 else
1000 prev_list = (*regmatch.endp - *regmatch.startp);
1001 }
1002 vim_regfree(regmatch.regprog);
1003 }
1004 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001005 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001006 if (wp->w_briopt_vcol != 0)
1007 {
1008 // column value has priority
1009 bri = wp->w_briopt_vcol;
1010 prev_list = 0;
1011 }
1012 else
1013 bri = prev_indent + wp->w_briopt_shift;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001014
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001015 // Add offset for number column, if 'n' is in 'cpoptions'
1016 bri += win_col_off2(wp);
1017
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001018 // add additional indent for numbered lists
Maxim Kimf674b352021-07-22 11:46:59 +02001019 if (wp->w_briopt_list != 0)
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001020 {
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001021 if (wp->w_briopt_list > 0)
1022 bri += prev_list;
1023 else
1024 bri = prev_list;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001025 }
1026
Maxim Kimf674b352021-07-22 11:46:59 +02001027 // indent minus the length of the showbreak string
1028 if (wp->w_briopt_sbr)
1029 bri -= vim_strsize(get_showbreak_value(wp));
1030
1031
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001032 // never indent past left window margin
1033 if (bri < 0)
1034 bri = 0;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001035
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001036 // always leave at least bri_min characters on the left,
1037 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001038 else if (bri > eff_wwidth - wp->w_briopt_min)
1039 bri = (eff_wwidth - wp->w_briopt_min < 0)
1040 ? 0 : eff_wwidth - wp->w_briopt_min;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001041
1042 return bri;
1043}
1044#endif
1045
1046/*
1047 * When extra == 0: Return TRUE if the cursor is before or on the first
1048 * non-blank in the line.
1049 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1050 * the line.
1051 */
1052 int
1053inindent(int extra)
1054{
1055 char_u *ptr;
1056 colnr_T col;
1057
1058 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
1059 ++ptr;
1060 if (col >= curwin->w_cursor.col + extra)
1061 return TRUE;
1062 else
1063 return FALSE;
1064}
1065
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001066/*
1067 * op_reindent - handle reindenting a block of lines.
1068 */
1069 void
1070op_reindent(oparg_T *oap, int (*how)(void))
1071{
Bram Moolenaar4c84dd32022-04-20 10:22:54 +01001072 long i = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001073 char_u *l;
1074 int amount;
1075 linenr_T first_changed = 0;
1076 linenr_T last_changed = 0;
1077 linenr_T start_lnum = curwin->w_cursor.lnum;
1078
1079 // Don't even try when 'modifiable' is off.
1080 if (!curbuf->b_p_ma)
1081 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001082 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001083 return;
1084 }
1085
Bram Moolenaare4686982022-04-19 18:28:45 +01001086 // Save for undo. Do this once for all lines, much faster than doing this
1087 // for each line separately, especially when undoing.
1088 if (u_savecommon(start_lnum - 1, start_lnum + oap->line_count,
1089 start_lnum + oap->line_count, FALSE) == OK)
1090 for (i = oap->line_count; --i >= 0 && !got_int; )
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001091 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001092 // it's a slow thing to do, so give feedback so there's no worry
1093 // that the computer's just hung.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001094
Bram Moolenaare4686982022-04-19 18:28:45 +01001095 if (i > 1
1096 && (i % 50 == 0 || i == oap->line_count - 1)
1097 && oap->line_count > p_report)
1098 smsg(_("%ld lines to indent... "), i);
1099
1100 // Be vi-compatible: For lisp indenting the first line is not
1101 // indented, unless there is only one line.
Bram Moolenaare4686982022-04-19 18:28:45 +01001102 if (i != oap->line_count - 1 || oap->line_count == 1
1103 || how != get_lisp_indent)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001104 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001105 l = skipwhite(ml_get_curline());
1106 if (*l == NUL) // empty or blank line
1107 amount = 0;
1108 else
1109 amount = how(); // get the indent for this line
1110
1111 if (amount >= 0 && set_indent(amount, 0))
1112 {
1113 // did change the indent, call changed_lines() later
1114 if (first_changed == 0)
1115 first_changed = curwin->w_cursor.lnum;
1116 last_changed = curwin->w_cursor.lnum;
1117 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001118 }
Bram Moolenaare4686982022-04-19 18:28:45 +01001119 ++curwin->w_cursor.lnum;
1120 curwin->w_cursor.col = 0; // make sure it's valid
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001121 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001122
1123 // put cursor on first non-blank of indented line
1124 curwin->w_cursor.lnum = start_lnum;
1125 beginline(BL_SOL | BL_FIX);
1126
1127 // Mark changed lines so that they will be redrawn. When Visual
1128 // highlighting was present, need to continue until the last line. When
1129 // there is no change still need to remove the Visual highlighting.
1130 if (last_changed != 0)
1131 changed_lines(first_changed, 0,
1132 oap->is_VIsual ? start_lnum + oap->line_count :
1133 last_changed + 1, 0L);
1134 else if (oap->is_VIsual)
1135 redraw_curbuf_later(INVERTED);
1136
1137 if (oap->line_count > p_report)
1138 {
1139 i = oap->line_count - (i + 1);
1140 smsg(NGETTEXT("%ld line indented ",
1141 "%ld lines indented ", i), i);
1142 }
Bram Moolenaare1004402020-10-24 20:49:43 +02001143 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001144 {
1145 // set '[ and '] marks
1146 curbuf->b_op_start = oap->start;
1147 curbuf->b_op_end = oap->end;
1148 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001149}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001150
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001151/*
1152 * Return TRUE if lines starting with '#' should be left aligned.
1153 */
1154 int
1155preprocs_left(void)
1156{
1157 return
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001158 (curbuf->b_p_si && !curbuf->b_p_cin) ||
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001159 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1160 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001161 ;
1162}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001163
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001164/*
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001165 * Return TRUE if the conditions are OK for smart indenting.
1166 */
1167 int
1168may_do_si()
1169{
1170 return curbuf->b_p_si
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001171 && !curbuf->b_p_cin
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001172# ifdef FEAT_EVAL
1173 && *curbuf->b_p_inde == NUL
1174# endif
1175 && !p_paste;
1176}
1177
1178/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001179 * Try to do some very smart auto-indenting.
1180 * Used when inserting a "normal" character.
1181 */
1182 void
1183ins_try_si(int c)
1184{
1185 pos_T *pos, old_pos;
1186 char_u *ptr;
1187 int i;
1188 int temp;
1189
1190 // do some very smart indenting when entering '{' or '}'
Bram Moolenaar2e444bb2022-05-14 12:54:23 +01001191 if (((did_si || can_si_back) && c == '{')
1192 || (can_si && c == '}' && inindent(0)))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001193 {
1194 // for '}' set indent equal to indent of line containing matching '{'
1195 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1196 {
1197 old_pos = curwin->w_cursor;
1198 // If the matching '{' has a ')' immediately before it (ignoring
1199 // white-space), then line up with the start of the line
1200 // containing the matching '(' if there is one. This handles the
1201 // case where an "if (..\n..) {" statement continues over multiple
1202 // lines -- webb
1203 ptr = ml_get(pos->lnum);
1204 i = pos->col;
1205 if (i > 0) // skip blanks before '{'
1206 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1207 ;
1208 curwin->w_cursor.lnum = pos->lnum;
1209 curwin->w_cursor.col = i;
1210 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1211 curwin->w_cursor = *pos;
1212 i = get_indent();
1213 curwin->w_cursor = old_pos;
1214 if (State & VREPLACE_FLAG)
1215 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1216 else
1217 (void)set_indent(i, SIN_CHANGED);
1218 }
1219 else if (curwin->w_cursor.col > 0)
1220 {
1221 // when inserting '{' after "O" reduce indent, but not
1222 // more than indent of previous line
1223 temp = TRUE;
1224 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1225 {
1226 old_pos = curwin->w_cursor;
1227 i = get_indent();
1228 while (curwin->w_cursor.lnum > 1)
1229 {
1230 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1231
1232 // ignore empty lines and lines starting with '#'.
1233 if (*ptr != '#' && *ptr != NUL)
1234 break;
1235 }
1236 if (get_indent() >= i)
1237 temp = FALSE;
1238 curwin->w_cursor = old_pos;
1239 }
1240 if (temp)
1241 shift_line(TRUE, FALSE, 1, TRUE);
1242 }
1243 }
1244
1245 // set indent of '#' always to 0
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001246 if (curwin->w_cursor.col > 0 && can_si && c == '#' && inindent(0))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001247 {
1248 // remember current indent for next line
1249 old_indent = get_indent();
1250 (void)set_indent(0, SIN_CHANGED);
1251 }
1252
1253 // Adjust ai_col, the char at this position can be deleted.
1254 if (ai_col > curwin->w_cursor.col)
1255 ai_col = curwin->w_cursor.col;
1256}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001257
1258/*
1259 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1260 * Keep the cursor on the same character.
1261 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1262 * type == INDENT_DEC decrease indent (for CTRL-D)
1263 * type == INDENT_SET set indent to "amount"
1264 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1265 */
1266 void
1267change_indent(
1268 int type,
1269 int amount,
1270 int round,
1271 int replaced, // replaced character, put on replace stack
1272 int call_changed_bytes) // call changed_bytes()
1273{
1274 int vcol;
1275 int last_vcol;
1276 int insstart_less; // reduction for Insstart.col
1277 int new_cursor_col;
1278 int i;
1279 char_u *ptr;
1280 int save_p_list;
1281 int start_col;
1282 colnr_T vc;
1283 colnr_T orig_col = 0; // init for GCC
1284 char_u *new_line, *orig_line = NULL; // init for GCC
1285
Bram Moolenaar24959102022-05-07 20:01:16 +01001286 // MODE_VREPLACE state needs to know what the line was like before changing
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001287 if (State & VREPLACE_FLAG)
1288 {
1289 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1290 orig_col = curwin->w_cursor.col;
1291 }
1292
1293 // for the following tricks we don't want list mode
1294 save_p_list = curwin->w_p_list;
1295 curwin->w_p_list = FALSE;
1296 vc = getvcol_nolist(&curwin->w_cursor);
1297 vcol = vc;
1298
1299 // For Replace mode we need to fix the replace stack later, which is only
1300 // possible when the cursor is in the indent. Remember the number of
1301 // characters before the cursor if it's possible.
1302 start_col = curwin->w_cursor.col;
1303
1304 // determine offset from first non-blank
1305 new_cursor_col = curwin->w_cursor.col;
1306 beginline(BL_WHITE);
1307 new_cursor_col -= curwin->w_cursor.col;
1308
1309 insstart_less = curwin->w_cursor.col;
1310
1311 // If the cursor is in the indent, compute how many screen columns the
1312 // cursor is to the left of the first non-blank.
1313 if (new_cursor_col < 0)
1314 vcol = get_indent() - vcol;
1315
1316 if (new_cursor_col > 0) // can't fix replace stack
1317 start_col = -1;
1318
1319 // Set the new indent. The cursor will be put on the first non-blank.
1320 if (type == INDENT_SET)
1321 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1322 else
1323 {
1324 int save_State = State;
1325
1326 // Avoid being called recursively.
1327 if (State & VREPLACE_FLAG)
Bram Moolenaar24959102022-05-07 20:01:16 +01001328 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001329 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1330 State = save_State;
1331 }
1332 insstart_less -= curwin->w_cursor.col;
1333
1334 // Try to put cursor on same character.
1335 // If the cursor is at or after the first non-blank in the line,
1336 // compute the cursor column relative to the column of the first
1337 // non-blank character.
1338 // If we are not in insert mode, leave the cursor on the first non-blank.
1339 // If the cursor is before the first non-blank, position it relative
1340 // to the first non-blank, counted in screen columns.
1341 if (new_cursor_col >= 0)
1342 {
1343 // When changing the indent while the cursor is touching it, reset
1344 // Insstart_col to 0.
1345 if (new_cursor_col == 0)
1346 insstart_less = MAXCOL;
1347 new_cursor_col += curwin->w_cursor.col;
1348 }
Bram Moolenaar24959102022-05-07 20:01:16 +01001349 else if (!(State & MODE_INSERT))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001350 new_cursor_col = curwin->w_cursor.col;
1351 else
1352 {
1353 // Compute the screen column where the cursor should be.
1354 vcol = get_indent() - vcol;
1355 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1356
1357 // Advance the cursor until we reach the right screen column.
1358 vcol = last_vcol = 0;
1359 new_cursor_col = -1;
1360 ptr = ml_get_curline();
1361 while (vcol <= (int)curwin->w_virtcol)
1362 {
1363 last_vcol = vcol;
1364 if (has_mbyte && new_cursor_col >= 0)
1365 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
1366 else
1367 ++new_cursor_col;
Bram Moolenaar4e889f92022-02-21 19:36:12 +00001368 if (ptr[new_cursor_col] == NUL)
1369 break;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001370 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
1371 }
1372 vcol = last_vcol;
1373
1374 // May need to insert spaces to be able to position the cursor on
1375 // the right screen column.
1376 if (vcol != (int)curwin->w_virtcol)
1377 {
1378 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1379 i = (int)curwin->w_virtcol - vcol;
1380 ptr = alloc(i + 1);
1381 if (ptr != NULL)
1382 {
1383 new_cursor_col += i;
1384 ptr[i] = NUL;
1385 while (--i >= 0)
1386 ptr[i] = ' ';
1387 ins_str(ptr);
1388 vim_free(ptr);
1389 }
1390 }
1391
1392 // When changing the indent while the cursor is in it, reset
1393 // Insstart_col to 0.
1394 insstart_less = MAXCOL;
1395 }
1396
1397 curwin->w_p_list = save_p_list;
1398
1399 if (new_cursor_col <= 0)
1400 curwin->w_cursor.col = 0;
1401 else
1402 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1403 curwin->w_set_curswant = TRUE;
1404 changed_cline_bef_curs();
1405
1406 // May have to adjust the start of the insert.
Bram Moolenaar24959102022-05-07 20:01:16 +01001407 if (State & MODE_INSERT)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001408 {
1409 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1410 {
1411 if ((int)Insstart.col <= insstart_less)
1412 Insstart.col = 0;
1413 else
1414 Insstart.col -= insstart_less;
1415 }
1416 if ((int)ai_col <= insstart_less)
1417 ai_col = 0;
1418 else
1419 ai_col -= insstart_less;
1420 }
1421
Bram Moolenaar24959102022-05-07 20:01:16 +01001422 // For MODE_REPLACE state, may have to fix the replace stack, if it's
1423 // possible. If the number of characters before the cursor decreased, need
1424 // to pop a few characters from the replace stack.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001425 // If the number of characters before the cursor increased, need to push a
1426 // few NULs onto the replace stack.
1427 if (REPLACE_NORMAL(State) && start_col >= 0)
1428 {
1429 while (start_col > (int)curwin->w_cursor.col)
1430 {
1431 replace_join(0); // remove a NUL from the replace stack
1432 --start_col;
1433 }
1434 while (start_col < (int)curwin->w_cursor.col || replaced)
1435 {
1436 replace_push(NUL);
1437 if (replaced)
1438 {
1439 replace_push(replaced);
1440 replaced = NUL;
1441 }
1442 ++start_col;
1443 }
1444 }
1445
Bram Moolenaar24959102022-05-07 20:01:16 +01001446 // For MODE_VREPLACE state, we also have to fix the replace stack. In this
1447 // case it is always possible because we backspace over the whole line and
1448 // then put it back again the way we wanted it.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001449 if (State & VREPLACE_FLAG)
1450 {
1451 // If orig_line didn't allocate, just return. At least we did the job,
1452 // even if you can't backspace.
1453 if (orig_line == NULL)
1454 return;
1455
1456 // Save new line
1457 new_line = vim_strsave(ml_get_curline());
1458 if (new_line == NULL)
1459 return;
1460
1461 // We only put back the new line up to the cursor
1462 new_line[curwin->w_cursor.col] = NUL;
1463
1464 // Put back original line
1465 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1466 curwin->w_cursor.col = orig_col;
1467
1468 // Backspace from cursor to start of line
1469 backspace_until_column(0);
1470
1471 // Insert new stuff into line again
1472 ins_bytes(new_line);
1473
1474 vim_free(new_line);
1475 }
1476}
1477
1478/*
1479 * Copy the indent from ptr to the current line (and fill to size)
1480 * Leaves the cursor on the first non-blank in the line.
1481 * Returns TRUE if the line was changed.
1482 */
1483 int
1484copy_indent(int size, char_u *src)
1485{
1486 char_u *p = NULL;
1487 char_u *line = NULL;
1488 char_u *s;
1489 int todo;
1490 int ind_len;
1491 int line_len = 0;
1492 int tab_pad;
1493 int ind_done;
1494 int round;
1495#ifdef FEAT_VARTABS
1496 int ind_col;
1497#endif
1498
1499 // Round 1: compute the number of characters needed for the indent
1500 // Round 2: copy the characters.
1501 for (round = 1; round <= 2; ++round)
1502 {
1503 todo = size;
1504 ind_len = 0;
1505 ind_done = 0;
1506#ifdef FEAT_VARTABS
1507 ind_col = 0;
1508#endif
1509 s = src;
1510
1511 // Count/copy the usable portion of the source line
1512 while (todo > 0 && VIM_ISWHITE(*s))
1513 {
1514 if (*s == TAB)
1515 {
1516#ifdef FEAT_VARTABS
1517 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1518 curbuf->b_p_vts_array);
1519#else
1520 tab_pad = (int)curbuf->b_p_ts
1521 - (ind_done % (int)curbuf->b_p_ts);
1522#endif
1523 // Stop if this tab will overshoot the target
1524 if (todo < tab_pad)
1525 break;
1526 todo -= tab_pad;
1527 ind_done += tab_pad;
1528#ifdef FEAT_VARTABS
1529 ind_col += tab_pad;
1530#endif
1531 }
1532 else
1533 {
1534 --todo;
1535 ++ind_done;
1536#ifdef FEAT_VARTABS
1537 ++ind_col;
1538#endif
1539 }
1540 ++ind_len;
1541 if (p != NULL)
1542 *p++ = *s;
1543 ++s;
1544 }
1545
1546 // Fill to next tabstop with a tab, if possible
1547#ifdef FEAT_VARTABS
1548 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1549 curbuf->b_p_vts_array);
1550#else
1551 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1552#endif
1553 if (todo >= tab_pad && !curbuf->b_p_et)
1554 {
1555 todo -= tab_pad;
1556 ++ind_len;
1557#ifdef FEAT_VARTABS
1558 ind_col += tab_pad;
1559#endif
1560 if (p != NULL)
1561 *p++ = TAB;
1562 }
1563
1564 // Add tabs required for indent
1565 if (!curbuf->b_p_et)
1566 {
1567#ifdef FEAT_VARTABS
1568 for (;;)
1569 {
1570 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1571 curbuf->b_p_vts_array);
1572 if (todo < tab_pad)
1573 break;
1574 todo -= tab_pad;
1575 ++ind_len;
1576 ind_col += tab_pad;
1577 if (p != NULL)
1578 *p++ = TAB;
1579 }
1580#else
1581 while (todo >= (int)curbuf->b_p_ts)
1582 {
1583 todo -= (int)curbuf->b_p_ts;
1584 ++ind_len;
1585 if (p != NULL)
1586 *p++ = TAB;
1587 }
1588#endif
1589 }
1590
1591 // Count/add spaces required for indent
1592 while (todo > 0)
1593 {
1594 --todo;
1595 ++ind_len;
1596 if (p != NULL)
1597 *p++ = ' ';
1598 }
1599
1600 if (p == NULL)
1601 {
1602 // Allocate memory for the result: the copied indent, new indent
1603 // and the rest of the line.
1604 line_len = (int)STRLEN(ml_get_curline()) + 1;
1605 line = alloc(ind_len + line_len);
1606 if (line == NULL)
1607 return FALSE;
1608 p = line;
1609 }
1610 }
1611
1612 // Append the original line
1613 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1614
1615 // Replace the line
1616 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1617
1618 // Put the cursor after the indent.
1619 curwin->w_cursor.col = ind_len;
1620 return TRUE;
1621}
1622
1623/*
Bram Moolenaar308660b2022-06-16 12:10:48 +01001624 * Give a "resulting text too long" error and maybe set got_int.
1625 */
1626 static void
1627emsg_text_too_long(void)
1628{
1629 emsg(_(e_resulting_text_too_long));
1630#ifdef FEAT_EVAL
1631 // when not inside a try/catch set got_int to break out of any loop
1632 if (trylevel == 0)
1633#endif
1634 got_int = TRUE;
1635}
1636
1637/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001638 * ":retab".
1639 */
1640 void
1641ex_retab(exarg_T *eap)
1642{
1643 linenr_T lnum;
1644 int got_tab = FALSE;
1645 long num_spaces = 0;
1646 long num_tabs;
1647 long len;
1648 long col;
1649 long vcol;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001650 long start_col = 0; // For start of white-space string
1651 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001652 long old_len;
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001653 long new_len;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001654 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001655 char_u *new_line = (char_u *)1; // init to non-NULL
1656 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001657#ifdef FEAT_VARTABS
1658 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001659 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001660#else
1661 int temp;
1662 int new_ts;
1663#endif
1664 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001665 linenr_T first_line = 0; // first changed line
1666 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001667
1668 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001669 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001670
1671#ifdef FEAT_VARTABS
1672 new_ts_str = eap->arg;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02001673 if (tabstop_set(eap->arg, &new_vts_array) == FAIL)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001674 return;
1675 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1676 ++(eap->arg);
1677
1678 // This ensures that either new_vts_array and new_ts_str are freshly
1679 // allocated, or new_vts_array points to an existing array and new_ts_str
1680 // is null.
1681 if (new_vts_array == NULL)
1682 {
1683 new_vts_array = curbuf->b_p_vts_array;
1684 new_ts_str = NULL;
1685 }
1686 else
1687 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1688#else
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001689 ptr = eap->arg;
1690 new_ts = getdigits(&ptr);
1691 if (new_ts < 0 && *eap->arg == '-')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001692 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001693 emsg(_(e_argument_must_be_positive));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001694 return;
1695 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00001696 if (new_ts < 0 || new_ts > TABSTOP_MAX)
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001697 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001698 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001699 return;
1700 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001701 if (new_ts == 0)
1702 new_ts = curbuf->b_p_ts;
1703#endif
1704 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1705 {
1706 ptr = ml_get(lnum);
1707 col = 0;
1708 vcol = 0;
1709 did_undo = FALSE;
1710 for (;;)
1711 {
1712 if (VIM_ISWHITE(ptr[col]))
1713 {
1714 if (!got_tab && num_spaces == 0)
1715 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001716 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001717 start_vcol = vcol;
1718 start_col = col;
1719 }
1720 if (ptr[col] == ' ')
1721 num_spaces++;
1722 else
1723 got_tab = TRUE;
1724 }
1725 else
1726 {
1727 if (got_tab || (eap->forceit && num_spaces > 1))
1728 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001729 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001730
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001731 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001732 len = num_spaces = vcol - start_vcol;
1733 num_tabs = 0;
1734 if (!curbuf->b_p_et)
1735 {
1736#ifdef FEAT_VARTABS
1737 int t, s;
1738
1739 tabstop_fromto(start_vcol, vcol,
1740 curbuf->b_p_ts, new_vts_array, &t, &s);
1741 num_tabs = t;
1742 num_spaces = s;
1743#else
1744 temp = new_ts - (start_vcol % new_ts);
1745 if (num_spaces >= temp)
1746 {
1747 num_spaces -= temp;
1748 num_tabs++;
1749 }
1750 num_tabs += num_spaces / new_ts;
1751 num_spaces -= (num_spaces / new_ts) * new_ts;
1752#endif
1753 }
1754 if (curbuf->b_p_et || got_tab ||
1755 (num_spaces + num_tabs < len))
1756 {
1757 if (did_undo == FALSE)
1758 {
1759 did_undo = TRUE;
1760 if (u_save((linenr_T)(lnum - 1),
1761 (linenr_T)(lnum + 1)) == FAIL)
1762 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001763 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001764 break;
1765 }
1766 }
1767
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001768 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001769 len = num_spaces + num_tabs;
1770 old_len = (long)STRLEN(ptr);
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001771 new_len = old_len - col + start_col + len + 1;
Bram Moolenaar45491662022-02-12 21:59:51 +00001772 if (new_len <= 0 || new_len >= MAXCOL)
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001773 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001774 emsg_text_too_long();
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001775 break;
1776 }
1777 new_line = alloc(new_len);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001778 if (new_line == NULL)
1779 break;
1780 if (start_col > 0)
1781 mch_memmove(new_line, ptr, (size_t)start_col);
1782 mch_memmove(new_line + start_col + len,
1783 ptr + col, (size_t)(old_len - col + 1));
1784 ptr = new_line + start_col;
1785 for (col = 0; col < len; col++)
1786 ptr[col] = (col < num_tabs) ? '\t' : ' ';
Bram Moolenaar0dcd39b2021-02-03 19:44:25 +01001787 if (ml_replace(lnum, new_line, FALSE) == OK)
1788 // "new_line" may have been copied
1789 new_line = curbuf->b_ml.ml_line_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001790 if (first_line == 0)
1791 first_line = lnum;
1792 last_line = lnum;
1793 ptr = new_line;
1794 col = start_col + len;
1795 }
1796 }
1797 got_tab = FALSE;
1798 num_spaces = 0;
1799 }
1800 if (ptr[col] == NUL)
1801 break;
1802 vcol += chartabsize(ptr + col, (colnr_T)vcol);
Bram Moolenaar6e287032022-02-12 15:42:18 +00001803 if (vcol >= MAXCOL)
1804 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001805 emsg_text_too_long();
Bram Moolenaar6e287032022-02-12 15:42:18 +00001806 break;
1807 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001808 if (has_mbyte)
1809 col += (*mb_ptr2len)(ptr + col);
1810 else
1811 ++col;
1812 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001813 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001814 break;
1815 line_breakcheck();
1816 }
1817 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001818 emsg(_(e_interrupted));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001819
1820#ifdef FEAT_VARTABS
1821 // If a single value was given then it can be considered equal to
1822 // either the value of 'tabstop' or the value of 'vartabstop'.
1823 if (tabstop_count(curbuf->b_p_vts_array) == 0
1824 && tabstop_count(new_vts_array) == 1
1825 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001826 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001827 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001828 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001829 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001830 else
1831 redraw_curbuf_later(NOT_VALID);
1832#else
1833 if (curbuf->b_p_ts != new_ts)
1834 redraw_curbuf_later(NOT_VALID);
1835#endif
1836 if (first_line != 0)
1837 changed_lines(first_line, 0, last_line + 1, 0L);
1838
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001839 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001840
1841#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001842 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001843 {
1844 // If 'vartabstop' is in use or if the value given to retab has more
1845 // than one tabstop then update 'vartabstop'.
1846 int *old_vts_ary = curbuf->b_p_vts_array;
1847
1848 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1849 {
1850 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1851 OPT_FREE|OPT_LOCAL, 0);
1852 curbuf->b_p_vts_array = new_vts_array;
1853 vim_free(old_vts_ary);
1854 }
1855 else
1856 {
1857 // 'vartabstop' wasn't in use and a single value was given to
1858 // retab then update 'tabstop'.
1859 curbuf->b_p_ts = tabstop_first(new_vts_array);
1860 vim_free(new_vts_array);
1861 }
1862 vim_free(new_ts_str);
1863 }
1864#else
1865 curbuf->b_p_ts = new_ts;
1866#endif
1867 coladvance(curwin->w_curswant);
1868
1869 u_clearline();
1870}
1871
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001872#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001873/*
1874 * Get indent level from 'indentexpr'.
1875 */
1876 int
1877get_expr_indent(void)
1878{
1879 int indent = -1;
1880 char_u *inde_copy;
1881 pos_T save_pos;
1882 colnr_T save_curswant;
1883 int save_set_curswant;
1884 int save_State;
1885 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1886 OPT_LOCAL);
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001887 sctx_T save_sctx = current_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001888
1889 // Save and restore cursor position and curswant, in case it was changed
1890 // via :normal commands
1891 save_pos = curwin->w_cursor;
1892 save_curswant = curwin->w_curswant;
1893 save_set_curswant = curwin->w_set_curswant;
1894 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1895 if (use_sandbox)
1896 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001897 ++textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001898 current_sctx = curbuf->b_p_script_ctx[BV_INDE];
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001899
1900 // Need to make a copy, the 'indentexpr' option could be changed while
1901 // evaluating it.
1902 inde_copy = vim_strsave(curbuf->b_p_inde);
1903 if (inde_copy != NULL)
1904 {
1905 indent = (int)eval_to_number(inde_copy);
1906 vim_free(inde_copy);
1907 }
1908
1909 if (use_sandbox)
1910 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001911 --textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001912 current_sctx = save_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001913
1914 // Restore the cursor position so that 'indentexpr' doesn't need to.
1915 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1916 // command.
1917 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001918 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001919 curwin->w_cursor = save_pos;
1920 curwin->w_curswant = save_curswant;
1921 curwin->w_set_curswant = save_set_curswant;
1922 check_cursor();
1923 State = save_State;
1924
Bram Moolenaar620c9592021-07-31 21:32:31 +02001925 // Reset did_throw, unless 'debug' has "throw" and inside a try/catch.
1926 if (did_throw && (vim_strchr(p_debug, 't') == NULL || trylevel == 0))
1927 {
1928 handle_did_throw();
1929 did_throw = FALSE;
1930 }
1931
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001932 // If there is an error, just keep the current indent.
1933 if (indent < 0)
1934 indent = get_indent();
1935
1936 return indent;
1937}
1938#endif
1939
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001940 static int
1941lisp_match(char_u *p)
1942{
1943 char_u buf[LSIZE];
1944 int len;
1945 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1946
1947 while (*word != NUL)
1948 {
1949 (void)copy_option_part(&word, buf, LSIZE, ",");
1950 len = (int)STRLEN(buf);
1951 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
1952 return TRUE;
1953 }
1954 return FALSE;
1955}
1956
1957/*
1958 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
1959 * The incompatible newer method is quite a bit better at indenting
1960 * code in lisp-like languages than the traditional one; it's still
1961 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
1962 *
1963 * TODO:
1964 * Findmatch() should be adapted for lisp, also to make showmatch
1965 * work correctly: now (v5.3) it seems all C/C++ oriented:
1966 * - it does not recognize the #\( and #\) notations as character literals
1967 * - it doesn't know about comments starting with a semicolon
1968 * - it incorrectly interprets '(' as a character literal
1969 * All this messes up get_lisp_indent in some rare cases.
1970 * Update from Sergey Khorev:
1971 * I tried to fix the first two issues.
1972 */
1973 int
1974get_lisp_indent(void)
1975{
1976 pos_T *pos, realpos, paren;
1977 int amount;
1978 char_u *that;
1979 colnr_T col;
1980 colnr_T firsttry;
1981 int parencount, quotecount;
1982 int vi_lisp;
1983
1984 // Set vi_lisp to use the vi-compatible method
1985 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
1986
1987 realpos = curwin->w_cursor;
1988 curwin->w_cursor.col = 0;
1989
1990 if ((pos = findmatch(NULL, '(')) == NULL)
1991 pos = findmatch(NULL, '[');
1992 else
1993 {
1994 paren = *pos;
1995 pos = findmatch(NULL, '[');
1996 if (pos == NULL || LT_POSP(pos, &paren))
1997 pos = &paren;
1998 }
1999 if (pos != NULL)
2000 {
2001 // Extra trick: Take the indent of the first previous non-white
2002 // line that is at the same () level.
2003 amount = -1;
2004 parencount = 0;
2005
2006 while (--curwin->w_cursor.lnum >= pos->lnum)
2007 {
2008 if (linewhite(curwin->w_cursor.lnum))
2009 continue;
2010 for (that = ml_get_curline(); *that != NUL; ++that)
2011 {
2012 if (*that == ';')
2013 {
2014 while (*(that + 1) != NUL)
2015 ++that;
2016 continue;
2017 }
2018 if (*that == '\\')
2019 {
2020 if (*(that + 1) != NUL)
2021 ++that;
2022 continue;
2023 }
2024 if (*that == '"' && *(that + 1) != NUL)
2025 {
2026 while (*++that && *that != '"')
2027 {
2028 // skipping escaped characters in the string
2029 if (*that == '\\')
2030 {
2031 if (*++that == NUL)
2032 break;
2033 if (that[1] == NUL)
2034 {
2035 ++that;
2036 break;
2037 }
2038 }
2039 }
Bram Moolenaar0e8e9382022-06-18 12:51:11 +01002040 if (*that == NUL)
2041 break;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002042 }
2043 if (*that == '(' || *that == '[')
2044 ++parencount;
2045 else if (*that == ')' || *that == ']')
2046 --parencount;
2047 }
2048 if (parencount == 0)
2049 {
2050 amount = get_indent();
2051 break;
2052 }
2053 }
2054
2055 if (amount == -1)
2056 {
2057 curwin->w_cursor.lnum = pos->lnum;
2058 curwin->w_cursor.col = pos->col;
2059 col = pos->col;
2060
2061 that = ml_get_curline();
2062
2063 if (vi_lisp && get_indent() == 0)
2064 amount = 2;
2065 else
2066 {
2067 char_u *line = that;
2068
2069 amount = 0;
2070 while (*that && col)
2071 {
2072 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
2073 col--;
2074 }
2075
2076 // Some keywords require "body" indenting rules (the
2077 // non-standard-lisp ones are Scheme special forms):
2078 //
2079 // (let ((a 1)) instead (let ((a 1))
2080 // (...)) of (...))
2081
2082 if (!vi_lisp && (*that == '(' || *that == '[')
2083 && lisp_match(that + 1))
2084 amount += 2;
2085 else
2086 {
Bram Moolenaar8eba2bd2022-06-22 19:59:28 +01002087 if (*that != NUL)
2088 {
2089 that++;
2090 amount++;
2091 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002092 firsttry = amount;
2093
2094 while (VIM_ISWHITE(*that))
2095 {
2096 amount += lbr_chartabsize(line, that, (colnr_T)amount);
2097 ++that;
2098 }
2099
2100 if (*that && *that != ';') // not a comment line
2101 {
2102 // test *that != '(' to accommodate first let/do
2103 // argument if it is more than one line
2104 if (!vi_lisp && *that != '(' && *that != '[')
2105 firsttry++;
2106
2107 parencount = 0;
2108 quotecount = 0;
2109
2110 if (vi_lisp
2111 || (*that != '"'
2112 && *that != '\''
2113 && *that != '#'
2114 && (*that < '0' || *that > '9')))
2115 {
2116 while (*that
2117 && (!VIM_ISWHITE(*that)
2118 || quotecount
2119 || parencount)
2120 && (!((*that == '(' || *that == '[')
2121 && !quotecount
2122 && !parencount
2123 && vi_lisp)))
2124 {
2125 if (*that == '"')
2126 quotecount = !quotecount;
2127 if ((*that == '(' || *that == '[')
2128 && !quotecount)
2129 ++parencount;
2130 if ((*that == ')' || *that == ']')
2131 && !quotecount)
2132 --parencount;
2133 if (*that == '\\' && *(that+1) != NUL)
2134 amount += lbr_chartabsize_adv(
2135 line, &that, (colnr_T)amount);
2136 amount += lbr_chartabsize_adv(
2137 line, &that, (colnr_T)amount);
2138 }
2139 }
2140 while (VIM_ISWHITE(*that))
2141 {
2142 amount += lbr_chartabsize(
2143 line, that, (colnr_T)amount);
2144 that++;
2145 }
2146 if (!*that || *that == ';')
2147 amount = firsttry;
2148 }
2149 }
2150 }
2151 }
2152 }
2153 else
2154 amount = 0; // no matching '(' or '[' found, use zero indent
2155
2156 curwin->w_cursor = realpos;
2157
2158 return amount;
2159}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002160
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002161/*
2162 * Re-indent the current line, based on the current contents of it and the
2163 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
2164 * confused what all the part that handles Control-T is doing that I'm not.
2165 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
2166 */
2167
2168 void
2169fixthisline(int (*get_the_indent)(void))
2170{
2171 int amount = get_the_indent();
2172
2173 if (amount >= 0)
2174 {
2175 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
2176 if (linewhite(curwin->w_cursor.lnum))
2177 did_ai = TRUE; // delete the indent if the line stays empty
2178 }
2179}
2180
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002181/*
2182 * Fix indent for 'lisp' and 'cindent'.
2183 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002184 void
2185fix_indent(void)
2186{
2187 if (p_paste)
2188 return;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002189 if (curbuf->b_p_lisp && curbuf->b_p_ai)
2190 fixthisline(get_lisp_indent);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002191 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002192 if (cindent_on())
2193 do_c_expr_indent();
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002194}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002195
2196#if defined(FEAT_EVAL) || defined(PROTO)
2197/*
2198 * "indent()" function
2199 */
2200 void
2201f_indent(typval_T *argvars, typval_T *rettv)
2202{
2203 linenr_T lnum;
2204
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002205 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2206 return;
2207
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002208 lnum = tv_get_lnum(argvars);
2209 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2210 rettv->vval.v_number = get_indent_lnum(lnum);
2211 else
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002212 {
2213 if (in_vim9script())
2214 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002215 rettv->vval.v_number = -1;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002216 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002217}
2218
2219/*
2220 * "lispindent(lnum)" function
2221 */
2222 void
2223f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2224{
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002225 pos_T pos;
2226 linenr_T lnum;
2227
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002228 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2229 return;
2230
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002231 pos = curwin->w_cursor;
2232 lnum = tv_get_lnum(argvars);
2233 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2234 {
2235 curwin->w_cursor.lnum = lnum;
2236 rettv->vval.v_number = get_lisp_indent();
2237 curwin->w_cursor = pos;
2238 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002239 else if (in_vim9script())
2240 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002241 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002242 rettv->vval.v_number = -1;
2243}
2244#endif