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