blob: 134336c12de559534b16d1a1b90b73ad80b72534 [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 Moolenaar8a77d202022-08-15 16:29:37 +0100800 added > 0 ? old_offset : (colnr_T)ind_len,
801 added, APC_INDENT);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200802 }
803#endif
804 retval = TRUE;
805 }
806 else
807 vim_free(newline);
808
809 curwin->w_cursor.col = ind_len;
810 return retval;
811}
812
813/*
814 * Return the indent of the current line after a number. Return -1 if no
815 * number was found. Used for 'n' in 'formatoptions': numbered list.
816 * Since a pattern is used it can actually handle more than numbers.
817 */
818 int
819get_number_indent(linenr_T lnum)
820{
821 colnr_T col;
822 pos_T pos;
823
824 regmatch_T regmatch;
825 int lead_len = 0; // length of comment leader
826
827 if (lnum > curbuf->b_ml.ml_line_count)
828 return -1;
829 pos.lnum = 0;
830
831 // In format_lines() (i.e. not insert mode), fo+=q is needed too...
Bram Moolenaar24959102022-05-07 20:01:16 +0100832 if ((State & MODE_INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200833 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
834
835 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
836 if (regmatch.regprog != NULL)
837 {
838 regmatch.rm_ic = FALSE;
839
840 // vim_regexec() expects a pointer to a line. This lets us
841 // start matching for the flp beyond any comment leader...
842 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
843 {
844 pos.lnum = lnum;
845 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
846 pos.coladd = 0;
847 }
848 vim_regfree(regmatch.regprog);
849 }
850
851 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
852 return -1;
853 getvcol(curwin, &pos, &col, NULL, NULL);
854 return (int)col;
855}
856
857#if defined(FEAT_LINEBREAK) || defined(PROTO)
858/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100859 * This is called when 'breakindentopt' is changed and when a window is
860 * initialized.
861 */
862 int
863briopt_check(win_T *wp)
864{
865 char_u *p;
866 int bri_shift = 0;
867 long bri_min = 20;
868 int bri_sbr = FALSE;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200869 int bri_list = 0;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100870 int bri_vcol = 0;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100871
872 p = wp->w_p_briopt;
873 while (*p != NUL)
874 {
875 if (STRNCMP(p, "shift:", 6) == 0
876 && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
877 {
878 p += 6;
879 bri_shift = getdigits(&p);
880 }
881 else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4]))
882 {
883 p += 4;
884 bri_min = getdigits(&p);
885 }
886 else if (STRNCMP(p, "sbr", 3) == 0)
887 {
888 p += 3;
889 bri_sbr = TRUE;
890 }
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200891 else if (STRNCMP(p, "list:", 5) == 0)
892 {
893 p += 5;
894 bri_list = getdigits(&p);
895 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100896 else if (STRNCMP(p, "column:", 7) == 0)
897 {
898 p += 7;
899 bri_vcol = getdigits(&p);
900 }
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100901 if (*p != ',' && *p != NUL)
902 return FAIL;
903 if (*p == ',')
904 ++p;
905 }
906
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100907 wp->w_briopt_shift = bri_shift;
908 wp->w_briopt_min = bri_min;
909 wp->w_briopt_sbr = bri_sbr;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +0200910 wp->w_briopt_list = bri_list;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100911 wp->w_briopt_vcol = bri_vcol;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100912
913 return OK;
914}
915
916/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200917 * Return appropriate space number for breakindent, taking influencing
918 * parameters into account. Window must be specified, since it is not
919 * necessarily always the current one.
920 */
921 int
922get_breakindent_win(
923 win_T *wp,
924 char_u *line) // start of the line
925{
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000926 static int prev_indent = 0; // cached indent value
927 static long prev_ts = 0L; // cached tabstop value
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100928 static int prev_fnum = 0; // cached buffer number
929 static char_u *prev_line = NULL; // cached copy of "line"
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200930 static varnumber_T prev_tick = 0; // changedtick of cached value
931# ifdef FEAT_VARTABS
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000932 static int *prev_vts = NULL; // cached vartabs values
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200933# endif
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000934 static int prev_list = 0; // cached list value
935 static int prev_listopt = 0; // cached w_p_briopt_list value
Christian Brabandtc53b4672022-01-15 10:01:05 +0000936 // cached formatlistpat value
937 static char_u *prev_flp = NULL;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200938 int bri = 0;
939 // window width minus window margin space, i.e. what rests for text
940 const int eff_wwidth = wp->w_width
941 - ((wp->w_p_nu || wp->w_p_rnu)
942 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
943 ? number_width(wp) + 1 : 0);
944
Christian Brabandtc53b4672022-01-15 10:01:05 +0000945 // used cached indent, unless
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100946 // - buffer changed
Christian Brabandtc53b4672022-01-15 10:01:05 +0000947 // - 'tabstop' changed
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100948 // - buffer was changed
Christian Brabandtc53b4672022-01-15 10:01:05 +0000949 // - 'briopt_list changed' changed or
950 // - 'formatlistpattern' changed
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100951 // - line changed
952 // - 'vartabs' changed
953 if (prev_fnum != wp->w_buffer->b_fnum
954 || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200955 || prev_tick != CHANGEDTICK(wp->w_buffer)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000956 || prev_listopt != wp->w_briopt_list
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100957 || prev_flp == NULL
958 || STRCMP(prev_flp, get_flp_value(wp->w_buffer)) != 0
959 || prev_line == NULL || STRCMP(prev_line, line) != 0
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200960# ifdef FEAT_VARTABS
961 || prev_vts != wp->w_buffer->b_p_vts_array
962# endif
963 )
964 {
Bram Moolenaarc2a79b82022-07-01 13:15:35 +0100965 prev_fnum = wp->w_buffer->b_fnum;
966 vim_free(prev_line);
967 prev_line = vim_strsave(line);
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200968 prev_ts = wp->w_buffer->b_p_ts;
969 prev_tick = CHANGEDTICK(wp->w_buffer);
970# ifdef FEAT_VARTABS
971 prev_vts = wp->w_buffer->b_p_vts_array;
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100972 if (wp->w_briopt_vcol == 0)
973 prev_indent = get_indent_str_vtab(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200974 (int)wp->w_buffer->b_p_ts,
975 wp->w_buffer->b_p_vts_array, wp->w_p_list);
976# else
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100977 if (wp->w_briopt_vcol == 0)
978 prev_indent = get_indent_str(line,
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200979 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
980# endif
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000981 prev_listopt = wp->w_briopt_list;
Christian Brabandtc53b4672022-01-15 10:01:05 +0000982 prev_list = 0;
983 vim_free(prev_flp);
984 prev_flp = vim_strsave(get_flp_value(wp->w_buffer));
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000985 // add additional indent for numbered lists
Christian Brabandte7d6dbc2022-05-06 12:21:04 +0100986 if (wp->w_briopt_list != 0 && wp->w_briopt_vcol == 0)
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000987 {
988 regmatch_T regmatch;
989
Christian Brabandtc53b4672022-01-15 10:01:05 +0000990 regmatch.regprog = vim_regcomp(prev_flp,
991 RE_MAGIC + RE_STRING + RE_AUTO + RE_STRICT);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000992
993 if (regmatch.regprog != NULL)
994 {
995 regmatch.rm_ic = FALSE;
996 if (vim_regexec(&regmatch, line, 0))
997 {
998 if (wp->w_briopt_list > 0)
999 prev_list = wp->w_briopt_list;
1000 else
Maxim Kim11916722022-09-02 14:08:53 +01001001 prev_indent = (*regmatch.endp - *regmatch.startp);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00001002 }
1003 vim_regfree(regmatch.regprog);
1004 }
1005 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001006 }
Christian Brabandte7d6dbc2022-05-06 12:21:04 +01001007 if (wp->w_briopt_vcol != 0)
1008 {
1009 // column value has priority
1010 bri = wp->w_briopt_vcol;
1011 prev_list = 0;
1012 }
1013 else
1014 bri = prev_indent + wp->w_briopt_shift;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001015
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001016 // Add offset for number column, if 'n' is in 'cpoptions'
1017 bri += win_col_off2(wp);
1018
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001019 // add additional indent for numbered lists
Maxim Kim11916722022-09-02 14:08:53 +01001020 if (wp->w_briopt_list > 0)
1021 bri += prev_list;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001022
Maxim Kimf674b352021-07-22 11:46:59 +02001023 // indent minus the length of the showbreak string
1024 if (wp->w_briopt_sbr)
1025 bri -= vim_strsize(get_showbreak_value(wp));
1026
1027
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001028 // never indent past left window margin
1029 if (bri < 0)
1030 bri = 0;
Christian Brabandt4a0b85a2021-07-14 20:00:27 +02001031
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001032 // always leave at least bri_min characters on the left,
1033 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +01001034 else if (bri > eff_wwidth - wp->w_briopt_min)
1035 bri = (eff_wwidth - wp->w_briopt_min < 0)
1036 ? 0 : eff_wwidth - wp->w_briopt_min;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001037
1038 return bri;
1039}
1040#endif
1041
1042/*
1043 * When extra == 0: Return TRUE if the cursor is before or on the first
1044 * non-blank in the line.
1045 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1046 * the line.
1047 */
1048 int
1049inindent(int extra)
1050{
1051 char_u *ptr;
1052 colnr_T col;
1053
1054 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
1055 ++ptr;
1056 if (col >= curwin->w_cursor.col + extra)
1057 return TRUE;
1058 else
1059 return FALSE;
1060}
1061
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001062/*
1063 * op_reindent - handle reindenting a block of lines.
1064 */
1065 void
1066op_reindent(oparg_T *oap, int (*how)(void))
1067{
Bram Moolenaar4c84dd32022-04-20 10:22:54 +01001068 long i = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001069 char_u *l;
1070 int amount;
1071 linenr_T first_changed = 0;
1072 linenr_T last_changed = 0;
1073 linenr_T start_lnum = curwin->w_cursor.lnum;
1074
1075 // Don't even try when 'modifiable' is off.
1076 if (!curbuf->b_p_ma)
1077 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001078 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001079 return;
1080 }
1081
Bram Moolenaare4686982022-04-19 18:28:45 +01001082 // Save for undo. Do this once for all lines, much faster than doing this
1083 // for each line separately, especially when undoing.
1084 if (u_savecommon(start_lnum - 1, start_lnum + oap->line_count,
1085 start_lnum + oap->line_count, FALSE) == OK)
1086 for (i = oap->line_count; --i >= 0 && !got_int; )
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001087 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001088 // it's a slow thing to do, so give feedback so there's no worry
1089 // that the computer's just hung.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001090
Bram Moolenaare4686982022-04-19 18:28:45 +01001091 if (i > 1
1092 && (i % 50 == 0 || i == oap->line_count - 1)
1093 && oap->line_count > p_report)
1094 smsg(_("%ld lines to indent... "), i);
1095
1096 // Be vi-compatible: For lisp indenting the first line is not
1097 // indented, unless there is only one line.
Bram Moolenaare4686982022-04-19 18:28:45 +01001098 if (i != oap->line_count - 1 || oap->line_count == 1
1099 || how != get_lisp_indent)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001100 {
Bram Moolenaare4686982022-04-19 18:28:45 +01001101 l = skipwhite(ml_get_curline());
1102 if (*l == NUL) // empty or blank line
1103 amount = 0;
1104 else
1105 amount = how(); // get the indent for this line
1106
1107 if (amount >= 0 && set_indent(amount, 0))
1108 {
1109 // did change the indent, call changed_lines() later
1110 if (first_changed == 0)
1111 first_changed = curwin->w_cursor.lnum;
1112 last_changed = curwin->w_cursor.lnum;
1113 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001114 }
Bram Moolenaare4686982022-04-19 18:28:45 +01001115 ++curwin->w_cursor.lnum;
1116 curwin->w_cursor.col = 0; // make sure it's valid
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001117 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001118
1119 // put cursor on first non-blank of indented line
1120 curwin->w_cursor.lnum = start_lnum;
1121 beginline(BL_SOL | BL_FIX);
1122
1123 // Mark changed lines so that they will be redrawn. When Visual
1124 // highlighting was present, need to continue until the last line. When
1125 // there is no change still need to remove the Visual highlighting.
1126 if (last_changed != 0)
1127 changed_lines(first_changed, 0,
1128 oap->is_VIsual ? start_lnum + oap->line_count :
1129 last_changed + 1, 0L);
1130 else if (oap->is_VIsual)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001131 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001132
1133 if (oap->line_count > p_report)
1134 {
1135 i = oap->line_count - (i + 1);
1136 smsg(NGETTEXT("%ld line indented ",
1137 "%ld lines indented ", i), i);
1138 }
Bram Moolenaare1004402020-10-24 20:49:43 +02001139 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001140 {
1141 // set '[ and '] marks
1142 curbuf->b_op_start = oap->start;
1143 curbuf->b_op_end = oap->end;
1144 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001145}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001146
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001147/*
1148 * Return TRUE if lines starting with '#' should be left aligned.
1149 */
1150 int
1151preprocs_left(void)
1152{
1153 return
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001154 (curbuf->b_p_si && !curbuf->b_p_cin) ||
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001155 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1156 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001157 ;
1158}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001159
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001160/*
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001161 * Return TRUE if the conditions are OK for smart indenting.
1162 */
1163 int
1164may_do_si()
1165{
1166 return curbuf->b_p_si
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001167 && !curbuf->b_p_cin
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001168# ifdef FEAT_EVAL
1169 && *curbuf->b_p_inde == NUL
1170# endif
1171 && !p_paste;
1172}
1173
1174/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001175 * Try to do some very smart auto-indenting.
1176 * Used when inserting a "normal" character.
1177 */
1178 void
1179ins_try_si(int c)
1180{
1181 pos_T *pos, old_pos;
1182 char_u *ptr;
1183 int i;
1184 int temp;
1185
1186 // do some very smart indenting when entering '{' or '}'
Bram Moolenaar2e444bb2022-05-14 12:54:23 +01001187 if (((did_si || can_si_back) && c == '{')
1188 || (can_si && c == '}' && inindent(0)))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001189 {
1190 // for '}' set indent equal to indent of line containing matching '{'
1191 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1192 {
1193 old_pos = curwin->w_cursor;
1194 // If the matching '{' has a ')' immediately before it (ignoring
1195 // white-space), then line up with the start of the line
1196 // containing the matching '(' if there is one. This handles the
1197 // case where an "if (..\n..) {" statement continues over multiple
1198 // lines -- webb
1199 ptr = ml_get(pos->lnum);
1200 i = pos->col;
1201 if (i > 0) // skip blanks before '{'
1202 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1203 ;
1204 curwin->w_cursor.lnum = pos->lnum;
1205 curwin->w_cursor.col = i;
1206 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1207 curwin->w_cursor = *pos;
1208 i = get_indent();
1209 curwin->w_cursor = old_pos;
1210 if (State & VREPLACE_FLAG)
1211 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1212 else
1213 (void)set_indent(i, SIN_CHANGED);
1214 }
1215 else if (curwin->w_cursor.col > 0)
1216 {
1217 // when inserting '{' after "O" reduce indent, but not
1218 // more than indent of previous line
1219 temp = TRUE;
1220 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1221 {
1222 old_pos = curwin->w_cursor;
1223 i = get_indent();
1224 while (curwin->w_cursor.lnum > 1)
1225 {
1226 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1227
1228 // ignore empty lines and lines starting with '#'.
1229 if (*ptr != '#' && *ptr != NUL)
1230 break;
1231 }
1232 if (get_indent() >= i)
1233 temp = FALSE;
1234 curwin->w_cursor = old_pos;
1235 }
1236 if (temp)
1237 shift_line(TRUE, FALSE, 1, TRUE);
1238 }
1239 }
1240
1241 // set indent of '#' always to 0
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001242 if (curwin->w_cursor.col > 0 && can_si && c == '#' && inindent(0))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001243 {
1244 // remember current indent for next line
1245 old_indent = get_indent();
1246 (void)set_indent(0, SIN_CHANGED);
1247 }
1248
1249 // Adjust ai_col, the char at this position can be deleted.
1250 if (ai_col > curwin->w_cursor.col)
1251 ai_col = curwin->w_cursor.col;
1252}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001253
1254/*
1255 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1256 * Keep the cursor on the same character.
1257 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1258 * type == INDENT_DEC decrease indent (for CTRL-D)
1259 * type == INDENT_SET set indent to "amount"
1260 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1261 */
1262 void
1263change_indent(
1264 int type,
1265 int amount,
1266 int round,
1267 int replaced, // replaced character, put on replace stack
1268 int call_changed_bytes) // call changed_bytes()
1269{
1270 int vcol;
1271 int last_vcol;
1272 int insstart_less; // reduction for Insstart.col
1273 int new_cursor_col;
1274 int i;
1275 char_u *ptr;
1276 int save_p_list;
1277 int start_col;
1278 colnr_T vc;
1279 colnr_T orig_col = 0; // init for GCC
1280 char_u *new_line, *orig_line = NULL; // init for GCC
1281
Bram Moolenaar24959102022-05-07 20:01:16 +01001282 // MODE_VREPLACE state needs to know what the line was like before changing
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001283 if (State & VREPLACE_FLAG)
1284 {
1285 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1286 orig_col = curwin->w_cursor.col;
1287 }
1288
1289 // for the following tricks we don't want list mode
1290 save_p_list = curwin->w_p_list;
1291 curwin->w_p_list = FALSE;
1292 vc = getvcol_nolist(&curwin->w_cursor);
1293 vcol = vc;
1294
1295 // For Replace mode we need to fix the replace stack later, which is only
1296 // possible when the cursor is in the indent. Remember the number of
1297 // characters before the cursor if it's possible.
1298 start_col = curwin->w_cursor.col;
1299
1300 // determine offset from first non-blank
1301 new_cursor_col = curwin->w_cursor.col;
1302 beginline(BL_WHITE);
1303 new_cursor_col -= curwin->w_cursor.col;
1304
1305 insstart_less = curwin->w_cursor.col;
1306
1307 // If the cursor is in the indent, compute how many screen columns the
1308 // cursor is to the left of the first non-blank.
1309 if (new_cursor_col < 0)
1310 vcol = get_indent() - vcol;
1311
1312 if (new_cursor_col > 0) // can't fix replace stack
1313 start_col = -1;
1314
1315 // Set the new indent. The cursor will be put on the first non-blank.
1316 if (type == INDENT_SET)
1317 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1318 else
1319 {
1320 int save_State = State;
1321
1322 // Avoid being called recursively.
1323 if (State & VREPLACE_FLAG)
Bram Moolenaar24959102022-05-07 20:01:16 +01001324 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001325 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1326 State = save_State;
1327 }
1328 insstart_less -= curwin->w_cursor.col;
1329
1330 // Try to put cursor on same character.
1331 // If the cursor is at or after the first non-blank in the line,
1332 // compute the cursor column relative to the column of the first
1333 // non-blank character.
1334 // If we are not in insert mode, leave the cursor on the first non-blank.
1335 // If the cursor is before the first non-blank, position it relative
1336 // to the first non-blank, counted in screen columns.
1337 if (new_cursor_col >= 0)
1338 {
1339 // When changing the indent while the cursor is touching it, reset
1340 // Insstart_col to 0.
1341 if (new_cursor_col == 0)
1342 insstart_less = MAXCOL;
1343 new_cursor_col += curwin->w_cursor.col;
1344 }
Bram Moolenaar24959102022-05-07 20:01:16 +01001345 else if (!(State & MODE_INSERT))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001346 new_cursor_col = curwin->w_cursor.col;
1347 else
1348 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001349 chartabsize_T cts;
1350
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001351 // Compute the screen column where the cursor should be.
1352 vcol = get_indent() - vcol;
1353 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1354
1355 // Advance the cursor until we reach the right screen column.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001356 last_vcol = 0;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001357 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001358 init_chartabsize_arg(&cts, curwin, 0, 0, ptr, ptr);
1359 while (cts.cts_vcol <= (int)curwin->w_virtcol)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001360 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001361 last_vcol = cts.cts_vcol;
1362 if (cts.cts_vcol > 0)
1363 MB_PTR_ADV(cts.cts_ptr);
1364 if (*cts.cts_ptr == NUL)
Bram Moolenaar4e889f92022-02-21 19:36:12 +00001365 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001366 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001367 }
1368 vcol = last_vcol;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001369 new_cursor_col = cts.cts_ptr - cts.cts_line;
1370 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001371
1372 // May need to insert spaces to be able to position the cursor on
1373 // the right screen column.
1374 if (vcol != (int)curwin->w_virtcol)
1375 {
1376 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1377 i = (int)curwin->w_virtcol - vcol;
1378 ptr = alloc(i + 1);
1379 if (ptr != NULL)
1380 {
1381 new_cursor_col += i;
1382 ptr[i] = NUL;
1383 while (--i >= 0)
1384 ptr[i] = ' ';
1385 ins_str(ptr);
1386 vim_free(ptr);
1387 }
1388 }
1389
1390 // When changing the indent while the cursor is in it, reset
1391 // Insstart_col to 0.
1392 insstart_less = MAXCOL;
1393 }
1394
1395 curwin->w_p_list = save_p_list;
1396
1397 if (new_cursor_col <= 0)
1398 curwin->w_cursor.col = 0;
1399 else
1400 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1401 curwin->w_set_curswant = TRUE;
1402 changed_cline_bef_curs();
1403
1404 // May have to adjust the start of the insert.
Bram Moolenaar24959102022-05-07 20:01:16 +01001405 if (State & MODE_INSERT)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001406 {
1407 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1408 {
1409 if ((int)Insstart.col <= insstart_less)
1410 Insstart.col = 0;
1411 else
1412 Insstart.col -= insstart_less;
1413 }
1414 if ((int)ai_col <= insstart_less)
1415 ai_col = 0;
1416 else
1417 ai_col -= insstart_less;
1418 }
1419
Bram Moolenaar24959102022-05-07 20:01:16 +01001420 // For MODE_REPLACE state, may have to fix the replace stack, if it's
1421 // possible. If the number of characters before the cursor decreased, need
1422 // to pop a few characters from the replace stack.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001423 // If the number of characters before the cursor increased, need to push a
1424 // few NULs onto the replace stack.
1425 if (REPLACE_NORMAL(State) && start_col >= 0)
1426 {
1427 while (start_col > (int)curwin->w_cursor.col)
1428 {
1429 replace_join(0); // remove a NUL from the replace stack
1430 --start_col;
1431 }
1432 while (start_col < (int)curwin->w_cursor.col || replaced)
1433 {
1434 replace_push(NUL);
1435 if (replaced)
1436 {
1437 replace_push(replaced);
1438 replaced = NUL;
1439 }
1440 ++start_col;
1441 }
1442 }
1443
Bram Moolenaar24959102022-05-07 20:01:16 +01001444 // For MODE_VREPLACE state, we also have to fix the replace stack. In this
1445 // case it is always possible because we backspace over the whole line and
1446 // then put it back again the way we wanted it.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001447 if (State & VREPLACE_FLAG)
1448 {
1449 // If orig_line didn't allocate, just return. At least we did the job,
1450 // even if you can't backspace.
1451 if (orig_line == NULL)
1452 return;
1453
1454 // Save new line
1455 new_line = vim_strsave(ml_get_curline());
1456 if (new_line == NULL)
1457 return;
1458
1459 // We only put back the new line up to the cursor
1460 new_line[curwin->w_cursor.col] = NUL;
1461
1462 // Put back original line
1463 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1464 curwin->w_cursor.col = orig_col;
1465
1466 // Backspace from cursor to start of line
1467 backspace_until_column(0);
1468
1469 // Insert new stuff into line again
1470 ins_bytes(new_line);
1471
1472 vim_free(new_line);
1473 }
1474}
1475
1476/*
1477 * Copy the indent from ptr to the current line (and fill to size)
1478 * Leaves the cursor on the first non-blank in the line.
1479 * Returns TRUE if the line was changed.
1480 */
1481 int
1482copy_indent(int size, char_u *src)
1483{
1484 char_u *p = NULL;
1485 char_u *line = NULL;
1486 char_u *s;
1487 int todo;
1488 int ind_len;
1489 int line_len = 0;
1490 int tab_pad;
1491 int ind_done;
1492 int round;
1493#ifdef FEAT_VARTABS
1494 int ind_col;
1495#endif
1496
1497 // Round 1: compute the number of characters needed for the indent
1498 // Round 2: copy the characters.
1499 for (round = 1; round <= 2; ++round)
1500 {
1501 todo = size;
1502 ind_len = 0;
1503 ind_done = 0;
1504#ifdef FEAT_VARTABS
1505 ind_col = 0;
1506#endif
1507 s = src;
1508
1509 // Count/copy the usable portion of the source line
1510 while (todo > 0 && VIM_ISWHITE(*s))
1511 {
1512 if (*s == TAB)
1513 {
1514#ifdef FEAT_VARTABS
1515 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1516 curbuf->b_p_vts_array);
1517#else
1518 tab_pad = (int)curbuf->b_p_ts
1519 - (ind_done % (int)curbuf->b_p_ts);
1520#endif
1521 // Stop if this tab will overshoot the target
1522 if (todo < tab_pad)
1523 break;
1524 todo -= tab_pad;
1525 ind_done += tab_pad;
1526#ifdef FEAT_VARTABS
1527 ind_col += tab_pad;
1528#endif
1529 }
1530 else
1531 {
1532 --todo;
1533 ++ind_done;
1534#ifdef FEAT_VARTABS
1535 ++ind_col;
1536#endif
1537 }
1538 ++ind_len;
1539 if (p != NULL)
1540 *p++ = *s;
1541 ++s;
1542 }
1543
1544 // Fill to next tabstop with a tab, if possible
1545#ifdef FEAT_VARTABS
1546 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1547 curbuf->b_p_vts_array);
1548#else
1549 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1550#endif
1551 if (todo >= tab_pad && !curbuf->b_p_et)
1552 {
1553 todo -= tab_pad;
1554 ++ind_len;
1555#ifdef FEAT_VARTABS
1556 ind_col += tab_pad;
1557#endif
1558 if (p != NULL)
1559 *p++ = TAB;
1560 }
1561
1562 // Add tabs required for indent
1563 if (!curbuf->b_p_et)
1564 {
1565#ifdef FEAT_VARTABS
1566 for (;;)
1567 {
1568 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1569 curbuf->b_p_vts_array);
1570 if (todo < tab_pad)
1571 break;
1572 todo -= tab_pad;
1573 ++ind_len;
1574 ind_col += tab_pad;
1575 if (p != NULL)
1576 *p++ = TAB;
1577 }
1578#else
1579 while (todo >= (int)curbuf->b_p_ts)
1580 {
1581 todo -= (int)curbuf->b_p_ts;
1582 ++ind_len;
1583 if (p != NULL)
1584 *p++ = TAB;
1585 }
1586#endif
1587 }
1588
1589 // Count/add spaces required for indent
1590 while (todo > 0)
1591 {
1592 --todo;
1593 ++ind_len;
1594 if (p != NULL)
1595 *p++ = ' ';
1596 }
1597
1598 if (p == NULL)
1599 {
1600 // Allocate memory for the result: the copied indent, new indent
1601 // and the rest of the line.
1602 line_len = (int)STRLEN(ml_get_curline()) + 1;
1603 line = alloc(ind_len + line_len);
1604 if (line == NULL)
1605 return FALSE;
1606 p = line;
1607 }
1608 }
1609
1610 // Append the original line
1611 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1612
1613 // Replace the line
1614 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1615
1616 // Put the cursor after the indent.
1617 curwin->w_cursor.col = ind_len;
1618 return TRUE;
1619}
1620
1621/*
Bram Moolenaar308660b2022-06-16 12:10:48 +01001622 * Give a "resulting text too long" error and maybe set got_int.
1623 */
1624 static void
1625emsg_text_too_long(void)
1626{
1627 emsg(_(e_resulting_text_too_long));
1628#ifdef FEAT_EVAL
1629 // when not inside a try/catch set got_int to break out of any loop
1630 if (trylevel == 0)
1631#endif
1632 got_int = TRUE;
1633}
1634
1635/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001636 * ":retab".
1637 */
1638 void
1639ex_retab(exarg_T *eap)
1640{
1641 linenr_T lnum;
1642 int got_tab = FALSE;
1643 long num_spaces = 0;
1644 long num_tabs;
1645 long len;
1646 long col;
1647 long vcol;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001648 long start_col = 0; // For start of white-space string
1649 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001650 long old_len;
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001651 long new_len;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001652 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001653 char_u *new_line = (char_u *)1; // init to non-NULL
1654 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001655#ifdef FEAT_VARTABS
1656 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001657 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001658#else
1659 int temp;
1660 int new_ts;
1661#endif
1662 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001663 linenr_T first_line = 0; // first changed line
1664 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001665
1666 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001667 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001668
1669#ifdef FEAT_VARTABS
1670 new_ts_str = eap->arg;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02001671 if (tabstop_set(eap->arg, &new_vts_array) == FAIL)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001672 return;
1673 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1674 ++(eap->arg);
1675
1676 // This ensures that either new_vts_array and new_ts_str are freshly
1677 // allocated, or new_vts_array points to an existing array and new_ts_str
1678 // is null.
1679 if (new_vts_array == NULL)
1680 {
1681 new_vts_array = curbuf->b_p_vts_array;
1682 new_ts_str = NULL;
1683 }
1684 else
1685 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1686#else
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001687 ptr = eap->arg;
1688 new_ts = getdigits(&ptr);
1689 if (new_ts < 0 && *eap->arg == '-')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001690 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001691 emsg(_(e_argument_must_be_positive));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001692 return;
1693 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00001694 if (new_ts < 0 || new_ts > TABSTOP_MAX)
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001695 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001696 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar2ddb89f2021-09-04 21:20:41 +02001697 return;
1698 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001699 if (new_ts == 0)
1700 new_ts = curbuf->b_p_ts;
1701#endif
1702 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1703 {
1704 ptr = ml_get(lnum);
1705 col = 0;
1706 vcol = 0;
1707 did_undo = FALSE;
1708 for (;;)
1709 {
1710 if (VIM_ISWHITE(ptr[col]))
1711 {
1712 if (!got_tab && num_spaces == 0)
1713 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001714 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001715 start_vcol = vcol;
1716 start_col = col;
1717 }
1718 if (ptr[col] == ' ')
1719 num_spaces++;
1720 else
1721 got_tab = TRUE;
1722 }
1723 else
1724 {
1725 if (got_tab || (eap->forceit && num_spaces > 1))
1726 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001727 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001728
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001729 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001730 len = num_spaces = vcol - start_vcol;
1731 num_tabs = 0;
1732 if (!curbuf->b_p_et)
1733 {
1734#ifdef FEAT_VARTABS
1735 int t, s;
1736
1737 tabstop_fromto(start_vcol, vcol,
1738 curbuf->b_p_ts, new_vts_array, &t, &s);
1739 num_tabs = t;
1740 num_spaces = s;
1741#else
1742 temp = new_ts - (start_vcol % new_ts);
1743 if (num_spaces >= temp)
1744 {
1745 num_spaces -= temp;
1746 num_tabs++;
1747 }
1748 num_tabs += num_spaces / new_ts;
1749 num_spaces -= (num_spaces / new_ts) * new_ts;
1750#endif
1751 }
1752 if (curbuf->b_p_et || got_tab ||
1753 (num_spaces + num_tabs < len))
1754 {
1755 if (did_undo == FALSE)
1756 {
1757 did_undo = TRUE;
1758 if (u_save((linenr_T)(lnum - 1),
1759 (linenr_T)(lnum + 1)) == FAIL)
1760 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001761 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001762 break;
1763 }
1764 }
1765
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001766 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001767 len = num_spaces + num_tabs;
1768 old_len = (long)STRLEN(ptr);
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001769 new_len = old_len - col + start_col + len + 1;
Bram Moolenaar45491662022-02-12 21:59:51 +00001770 if (new_len <= 0 || new_len >= MAXCOL)
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001771 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001772 emsg_text_too_long();
Bram Moolenaar33f3c592022-02-12 20:46:15 +00001773 break;
1774 }
1775 new_line = alloc(new_len);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001776 if (new_line == NULL)
1777 break;
1778 if (start_col > 0)
1779 mch_memmove(new_line, ptr, (size_t)start_col);
1780 mch_memmove(new_line + start_col + len,
1781 ptr + col, (size_t)(old_len - col + 1));
1782 ptr = new_line + start_col;
1783 for (col = 0; col < len; col++)
1784 ptr[col] = (col < num_tabs) ? '\t' : ' ';
Bram Moolenaar0dcd39b2021-02-03 19:44:25 +01001785 if (ml_replace(lnum, new_line, FALSE) == OK)
1786 // "new_line" may have been copied
1787 new_line = curbuf->b_ml.ml_line_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001788 if (first_line == 0)
1789 first_line = lnum;
1790 last_line = lnum;
1791 ptr = new_line;
1792 col = start_col + len;
1793 }
1794 }
1795 got_tab = FALSE;
1796 num_spaces = 0;
1797 }
1798 if (ptr[col] == NUL)
1799 break;
1800 vcol += chartabsize(ptr + col, (colnr_T)vcol);
Bram Moolenaar6e287032022-02-12 15:42:18 +00001801 if (vcol >= MAXCOL)
1802 {
Bram Moolenaar308660b2022-06-16 12:10:48 +01001803 emsg_text_too_long();
Bram Moolenaar6e287032022-02-12 15:42:18 +00001804 break;
1805 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001806 if (has_mbyte)
1807 col += (*mb_ptr2len)(ptr + col);
1808 else
1809 ++col;
1810 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001811 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001812 break;
1813 line_breakcheck();
1814 }
1815 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001816 emsg(_(e_interrupted));
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001817
1818#ifdef FEAT_VARTABS
1819 // If a single value was given then it can be considered equal to
1820 // either the value of 'tabstop' or the value of 'vartabstop'.
1821 if (tabstop_count(curbuf->b_p_vts_array) == 0
1822 && tabstop_count(new_vts_array) == 1
1823 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001824 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001825 else if (tabstop_count(curbuf->b_p_vts_array) > 0
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001826 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001827 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001828 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001829 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001830#else
1831 if (curbuf->b_p_ts != new_ts)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001832 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001833#endif
1834 if (first_line != 0)
1835 changed_lines(first_line, 0, last_line + 1, 0L);
1836
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001837 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001838
1839#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001840 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001841 {
1842 // If 'vartabstop' is in use or if the value given to retab has more
1843 // than one tabstop then update 'vartabstop'.
1844 int *old_vts_ary = curbuf->b_p_vts_array;
1845
1846 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1847 {
1848 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1849 OPT_FREE|OPT_LOCAL, 0);
1850 curbuf->b_p_vts_array = new_vts_array;
1851 vim_free(old_vts_ary);
1852 }
1853 else
1854 {
1855 // 'vartabstop' wasn't in use and a single value was given to
1856 // retab then update 'tabstop'.
1857 curbuf->b_p_ts = tabstop_first(new_vts_array);
1858 vim_free(new_vts_array);
1859 }
1860 vim_free(new_ts_str);
1861 }
1862#else
1863 curbuf->b_p_ts = new_ts;
1864#endif
1865 coladvance(curwin->w_curswant);
1866
1867 u_clearline();
1868}
1869
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001870#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001871/*
1872 * Get indent level from 'indentexpr'.
1873 */
1874 int
1875get_expr_indent(void)
1876{
1877 int indent = -1;
1878 char_u *inde_copy;
1879 pos_T save_pos;
1880 colnr_T save_curswant;
1881 int save_set_curswant;
1882 int save_State;
1883 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1884 OPT_LOCAL);
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001885 sctx_T save_sctx = current_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001886
1887 // Save and restore cursor position and curswant, in case it was changed
1888 // via :normal commands
1889 save_pos = curwin->w_cursor;
1890 save_curswant = curwin->w_curswant;
1891 save_set_curswant = curwin->w_set_curswant;
1892 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1893 if (use_sandbox)
1894 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001895 ++textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001896 current_sctx = curbuf->b_p_script_ctx[BV_INDE];
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001897
1898 // Need to make a copy, the 'indentexpr' option could be changed while
1899 // evaluating it.
1900 inde_copy = vim_strsave(curbuf->b_p_inde);
1901 if (inde_copy != NULL)
1902 {
1903 indent = (int)eval_to_number(inde_copy);
1904 vim_free(inde_copy);
1905 }
1906
1907 if (use_sandbox)
1908 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001909 --textlock;
Bram Moolenaar28e60cc2022-01-22 20:32:00 +00001910 current_sctx = save_sctx;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001911
1912 // Restore the cursor position so that 'indentexpr' doesn't need to.
1913 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1914 // command.
1915 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001916 State = MODE_INSERT;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001917 curwin->w_cursor = save_pos;
1918 curwin->w_curswant = save_curswant;
1919 curwin->w_set_curswant = save_set_curswant;
1920 check_cursor();
1921 State = save_State;
1922
Bram Moolenaar620c9592021-07-31 21:32:31 +02001923 // Reset did_throw, unless 'debug' has "throw" and inside a try/catch.
1924 if (did_throw && (vim_strchr(p_debug, 't') == NULL || trylevel == 0))
1925 {
1926 handle_did_throw();
1927 did_throw = FALSE;
1928 }
1929
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001930 // If there is an error, just keep the current indent.
1931 if (indent < 0)
1932 indent = get_indent();
1933
1934 return indent;
1935}
1936#endif
1937
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001938 static int
1939lisp_match(char_u *p)
1940{
1941 char_u buf[LSIZE];
1942 int len;
1943 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1944
1945 while (*word != NUL)
1946 {
1947 (void)copy_option_part(&word, buf, LSIZE, ",");
1948 len = (int)STRLEN(buf);
1949 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
1950 return TRUE;
1951 }
1952 return FALSE;
1953}
1954
1955/*
1956 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
1957 * The incompatible newer method is quite a bit better at indenting
1958 * code in lisp-like languages than the traditional one; it's still
1959 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
1960 *
1961 * TODO:
1962 * Findmatch() should be adapted for lisp, also to make showmatch
1963 * work correctly: now (v5.3) it seems all C/C++ oriented:
1964 * - it does not recognize the #\( and #\) notations as character literals
1965 * - it doesn't know about comments starting with a semicolon
1966 * - it incorrectly interprets '(' as a character literal
1967 * All this messes up get_lisp_indent in some rare cases.
1968 * Update from Sergey Khorev:
1969 * I tried to fix the first two issues.
1970 */
1971 int
1972get_lisp_indent(void)
1973{
1974 pos_T *pos, realpos, paren;
1975 int amount;
1976 char_u *that;
1977 colnr_T col;
1978 colnr_T firsttry;
1979 int parencount, quotecount;
1980 int vi_lisp;
1981
1982 // Set vi_lisp to use the vi-compatible method
1983 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
1984
1985 realpos = curwin->w_cursor;
1986 curwin->w_cursor.col = 0;
1987
1988 if ((pos = findmatch(NULL, '(')) == NULL)
1989 pos = findmatch(NULL, '[');
1990 else
1991 {
1992 paren = *pos;
1993 pos = findmatch(NULL, '[');
1994 if (pos == NULL || LT_POSP(pos, &paren))
1995 pos = &paren;
1996 }
1997 if (pos != NULL)
1998 {
1999 // Extra trick: Take the indent of the first previous non-white
2000 // line that is at the same () level.
2001 amount = -1;
2002 parencount = 0;
2003
2004 while (--curwin->w_cursor.lnum >= pos->lnum)
2005 {
2006 if (linewhite(curwin->w_cursor.lnum))
2007 continue;
2008 for (that = ml_get_curline(); *that != NUL; ++that)
2009 {
2010 if (*that == ';')
2011 {
2012 while (*(that + 1) != NUL)
2013 ++that;
2014 continue;
2015 }
2016 if (*that == '\\')
2017 {
2018 if (*(that + 1) != NUL)
2019 ++that;
2020 continue;
2021 }
2022 if (*that == '"' && *(that + 1) != NUL)
2023 {
2024 while (*++that && *that != '"')
2025 {
2026 // skipping escaped characters in the string
2027 if (*that == '\\')
2028 {
2029 if (*++that == NUL)
2030 break;
2031 if (that[1] == NUL)
2032 {
2033 ++that;
2034 break;
2035 }
2036 }
2037 }
Bram Moolenaar0e8e9382022-06-18 12:51:11 +01002038 if (*that == NUL)
2039 break;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002040 }
2041 if (*that == '(' || *that == '[')
2042 ++parencount;
2043 else if (*that == ')' || *that == ']')
2044 --parencount;
2045 }
2046 if (parencount == 0)
2047 {
2048 amount = get_indent();
2049 break;
2050 }
2051 }
2052
2053 if (amount == -1)
2054 {
2055 curwin->w_cursor.lnum = pos->lnum;
2056 curwin->w_cursor.col = pos->col;
2057 col = pos->col;
2058
2059 that = ml_get_curline();
2060
2061 if (vi_lisp && get_indent() == 0)
2062 amount = 2;
2063 else
2064 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002065 char_u *line = that;
2066 chartabsize_T cts;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002067
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002068 init_chartabsize_arg(&cts, curwin, pos->lnum, 0, line, line);
2069 while (*cts.cts_ptr != NUL && col > 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002070 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002071 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002072 col--;
2073 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002074 amount = cts.cts_vcol;
2075 that = cts.cts_ptr;
2076 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002077
2078 // Some keywords require "body" indenting rules (the
2079 // non-standard-lisp ones are Scheme special forms):
2080 //
2081 // (let ((a 1)) instead (let ((a 1))
2082 // (...)) of (...))
2083
2084 if (!vi_lisp && (*that == '(' || *that == '[')
2085 && lisp_match(that + 1))
2086 amount += 2;
2087 else
2088 {
Bram Moolenaar8eba2bd2022-06-22 19:59:28 +01002089 if (*that != NUL)
2090 {
2091 that++;
2092 amount++;
2093 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002094 firsttry = amount;
2095
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002096 init_chartabsize_arg(&cts, curwin, (colnr_T)(that - line),
2097 amount, line, that);
2098 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002099 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002100 cts.cts_vcol += lbr_chartabsize(&cts);
2101 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002102 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002103 that = cts.cts_ptr;
2104 amount = cts.cts_vcol;
2105 clear_chartabsize_arg(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002106
2107 if (*that && *that != ';') // not a comment line
2108 {
2109 // test *that != '(' to accommodate first let/do
2110 // argument if it is more than one line
2111 if (!vi_lisp && *that != '(' && *that != '[')
2112 firsttry++;
2113
2114 parencount = 0;
2115 quotecount = 0;
2116
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002117 init_chartabsize_arg(&cts, curwin,
2118 (colnr_T)(that - line), amount, line, that);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002119 if (vi_lisp
2120 || (*that != '"'
2121 && *that != '\''
2122 && *that != '#'
2123 && (*that < '0' || *that > '9')))
2124 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002125 while (*cts.cts_ptr
2126 && (!VIM_ISWHITE(*cts.cts_ptr)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002127 || quotecount
2128 || parencount)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002129 && (!((*cts.cts_ptr == '('
2130 || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002131 && !quotecount
2132 && !parencount
2133 && vi_lisp)))
2134 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002135 if (*cts.cts_ptr == '"')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002136 quotecount = !quotecount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002137 if ((*cts.cts_ptr == '(' || *cts.cts_ptr == '[')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002138 && !quotecount)
2139 ++parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002140 if ((*cts.cts_ptr == ')' || *cts.cts_ptr == ']')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002141 && !quotecount)
2142 --parencount;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002143 if (*cts.cts_ptr == '\\'
2144 && *(cts.cts_ptr+1) != NUL)
2145 cts.cts_vcol += lbr_chartabsize_adv(&cts);
2146 cts.cts_vcol += lbr_chartabsize_adv(&cts);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002147 }
2148 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002149 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002150 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002151 cts.cts_vcol += lbr_chartabsize(&cts);
2152 ++cts.cts_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002153 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002154 that = cts.cts_ptr;
2155 amount = cts.cts_vcol;
2156 clear_chartabsize_arg(&cts);
2157
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002158 if (!*that || *that == ';')
2159 amount = firsttry;
2160 }
2161 }
2162 }
2163 }
2164 }
2165 else
2166 amount = 0; // no matching '(' or '[' found, use zero indent
2167
2168 curwin->w_cursor = realpos;
2169
2170 return amount;
2171}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002172
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002173/*
2174 * Re-indent the current line, based on the current contents of it and the
2175 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
2176 * confused what all the part that handles Control-T is doing that I'm not.
2177 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
2178 */
2179
2180 void
2181fixthisline(int (*get_the_indent)(void))
2182{
2183 int amount = get_the_indent();
2184
2185 if (amount >= 0)
2186 {
2187 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
2188 if (linewhite(curwin->w_cursor.lnum))
2189 did_ai = TRUE; // delete the indent if the line stays empty
2190 }
2191}
2192
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002193/*
2194 * Fix indent for 'lisp' and 'cindent'.
2195 */
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002196 void
2197fix_indent(void)
2198{
2199 if (p_paste)
2200 return;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002201 if (curbuf->b_p_lisp && curbuf->b_p_ai)
2202 fixthisline(get_lisp_indent);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002203 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002204 if (cindent_on())
2205 do_c_expr_indent();
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002206}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002207
2208#if defined(FEAT_EVAL) || defined(PROTO)
2209/*
2210 * "indent()" function
2211 */
2212 void
2213f_indent(typval_T *argvars, typval_T *rettv)
2214{
2215 linenr_T lnum;
2216
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002217 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2218 return;
2219
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002220 lnum = tv_get_lnum(argvars);
2221 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2222 rettv->vval.v_number = get_indent_lnum(lnum);
2223 else
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002224 {
2225 if (in_vim9script())
2226 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002227 rettv->vval.v_number = -1;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002228 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002229}
2230
2231/*
2232 * "lispindent(lnum)" function
2233 */
2234 void
2235f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2236{
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002237 pos_T pos;
2238 linenr_T lnum;
2239
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002240 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
2241 return;
2242
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002243 pos = curwin->w_cursor;
2244 lnum = tv_get_lnum(argvars);
2245 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2246 {
2247 curwin->w_cursor.lnum = lnum;
2248 rettv->vval.v_number = get_lisp_indent();
2249 curwin->w_cursor = pos;
2250 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00002251 else if (in_vim9script())
2252 semsg(_(e_invalid_line_number_nr), lnum);
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002253 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002254 rettv->vval.v_number = -1;
2255}
2256#endif