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