blob: e1c6f522a9bb0339db4ac91b2a5a2614c3e27d50 [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;
857
858 p = wp->w_p_briopt;
859 while (*p != NUL)
860 {
861 if (STRNCMP(p, "shift:", 6) == 0
862 && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
863 {
864 p += 6;
865 bri_shift = getdigits(&p);
866 }
867 else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4]))
868 {
869 p += 4;
870 bri_min = getdigits(&p);
871 }
872 else if (STRNCMP(p, "sbr", 3) == 0)
873 {
874 p += 3;
875 bri_sbr = TRUE;
876 }
877 if (*p != ',' && *p != NUL)
878 return FAIL;
879 if (*p == ',')
880 ++p;
881 }
882
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100883 wp->w_briopt_shift = bri_shift;
884 wp->w_briopt_min = bri_min;
885 wp->w_briopt_sbr = bri_sbr;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100886
887 return OK;
888}
889
890/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200891 * Return appropriate space number for breakindent, taking influencing
892 * parameters into account. Window must be specified, since it is not
893 * necessarily always the current one.
894 */
895 int
896get_breakindent_win(
897 win_T *wp,
898 char_u *line) // start of the line
899{
900 static int prev_indent = 0; // cached indent value
901 static long prev_ts = 0L; // cached tabstop value
902 static char_u *prev_line = NULL; // cached pointer to line
903 static varnumber_T prev_tick = 0; // changedtick of cached value
904# ifdef FEAT_VARTABS
905 static int *prev_vts = NULL; // cached vartabs values
906# endif
907 int bri = 0;
908 // window width minus window margin space, i.e. what rests for text
909 const int eff_wwidth = wp->w_width
910 - ((wp->w_p_nu || wp->w_p_rnu)
911 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
912 ? number_width(wp) + 1 : 0);
913
914 // used cached indent, unless pointer or 'tabstop' changed
915 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
916 || prev_tick != CHANGEDTICK(wp->w_buffer)
917# ifdef FEAT_VARTABS
918 || prev_vts != wp->w_buffer->b_p_vts_array
919# endif
920 )
921 {
922 prev_line = line;
923 prev_ts = wp->w_buffer->b_p_ts;
924 prev_tick = CHANGEDTICK(wp->w_buffer);
925# ifdef FEAT_VARTABS
926 prev_vts = wp->w_buffer->b_p_vts_array;
927 prev_indent = get_indent_str_vtab(line,
928 (int)wp->w_buffer->b_p_ts,
929 wp->w_buffer->b_p_vts_array, wp->w_p_list);
930# else
931 prev_indent = get_indent_str(line,
932 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
933# endif
934 }
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100935 bri = prev_indent + wp->w_briopt_shift;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200936
937 // indent minus the length of the showbreak string
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100938 if (wp->w_briopt_sbr)
Bram Moolenaar91e22eb2019-11-10 00:19:12 +0100939 bri -= vim_strsize(get_showbreak_value(wp));
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200940
941 // Add offset for number column, if 'n' is in 'cpoptions'
942 bri += win_col_off2(wp);
943
944 // never indent past left window margin
945 if (bri < 0)
946 bri = 0;
947 // always leave at least bri_min characters on the left,
948 // if text width is sufficient
Bram Moolenaarb81f56f2020-02-23 15:29:46 +0100949 else if (bri > eff_wwidth - wp->w_briopt_min)
950 bri = (eff_wwidth - wp->w_briopt_min < 0)
951 ? 0 : eff_wwidth - wp->w_briopt_min;
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200952
953 return bri;
954}
955#endif
956
957/*
958 * When extra == 0: Return TRUE if the cursor is before or on the first
959 * non-blank in the line.
960 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
961 * the line.
962 */
963 int
964inindent(int extra)
965{
966 char_u *ptr;
967 colnr_T col;
968
969 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
970 ++ptr;
971 if (col >= curwin->w_cursor.col + extra)
972 return TRUE;
973 else
974 return FALSE;
975}
976
977#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
978/*
979 * op_reindent - handle reindenting a block of lines.
980 */
981 void
982op_reindent(oparg_T *oap, int (*how)(void))
983{
984 long i;
985 char_u *l;
986 int amount;
987 linenr_T first_changed = 0;
988 linenr_T last_changed = 0;
989 linenr_T start_lnum = curwin->w_cursor.lnum;
990
991 // Don't even try when 'modifiable' is off.
992 if (!curbuf->b_p_ma)
993 {
994 emsg(_(e_modifiable));
995 return;
996 }
997
998 for (i = oap->line_count; --i >= 0 && !got_int; )
999 {
1000 // it's a slow thing to do, so give feedback so there's no worry that
1001 // the computer's just hung.
1002
1003 if (i > 1
1004 && (i % 50 == 0 || i == oap->line_count - 1)
1005 && oap->line_count > p_report)
1006 smsg(_("%ld lines to indent... "), i);
1007
1008 // Be vi-compatible: For lisp indenting the first line is not
1009 // indented, unless there is only one line.
1010# ifdef FEAT_LISP
1011 if (i != oap->line_count - 1 || oap->line_count == 1
1012 || how != get_lisp_indent)
1013# endif
1014 {
1015 l = skipwhite(ml_get_curline());
1016 if (*l == NUL) // empty or blank line
1017 amount = 0;
1018 else
1019 amount = how(); // get the indent for this line
1020
1021 if (amount >= 0 && set_indent(amount, SIN_UNDO))
1022 {
1023 // did change the indent, call changed_lines() later
1024 if (first_changed == 0)
1025 first_changed = curwin->w_cursor.lnum;
1026 last_changed = curwin->w_cursor.lnum;
1027 }
1028 }
1029 ++curwin->w_cursor.lnum;
1030 curwin->w_cursor.col = 0; // make sure it's valid
1031 }
1032
1033 // put cursor on first non-blank of indented line
1034 curwin->w_cursor.lnum = start_lnum;
1035 beginline(BL_SOL | BL_FIX);
1036
1037 // Mark changed lines so that they will be redrawn. When Visual
1038 // highlighting was present, need to continue until the last line. When
1039 // there is no change still need to remove the Visual highlighting.
1040 if (last_changed != 0)
1041 changed_lines(first_changed, 0,
1042 oap->is_VIsual ? start_lnum + oap->line_count :
1043 last_changed + 1, 0L);
1044 else if (oap->is_VIsual)
1045 redraw_curbuf_later(INVERTED);
1046
1047 if (oap->line_count > p_report)
1048 {
1049 i = oap->line_count - (i + 1);
1050 smsg(NGETTEXT("%ld line indented ",
1051 "%ld lines indented ", i), i);
1052 }
Bram Moolenaare1004402020-10-24 20:49:43 +02001053 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001054 {
1055 // set '[ and '] marks
1056 curbuf->b_op_start = oap->start;
1057 curbuf->b_op_end = oap->end;
1058 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001059}
1060#endif // defined(FEAT_LISP) || defined(FEAT_CINDENT)
1061
1062#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
1063/*
1064 * Return TRUE if lines starting with '#' should be left aligned.
1065 */
1066 int
1067preprocs_left(void)
1068{
1069 return
1070# ifdef FEAT_SMARTINDENT
1071# ifdef FEAT_CINDENT
1072 (curbuf->b_p_si && !curbuf->b_p_cin) ||
1073# else
1074 curbuf->b_p_si
1075# endif
1076# endif
1077# ifdef FEAT_CINDENT
1078 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1079 && curbuf->b_ind_hash_comment == 0)
1080# endif
1081 ;
1082}
1083#endif
1084
1085#ifdef FEAT_SMARTINDENT
1086/*
1087 * Try to do some very smart auto-indenting.
1088 * Used when inserting a "normal" character.
1089 */
1090 void
1091ins_try_si(int c)
1092{
1093 pos_T *pos, old_pos;
1094 char_u *ptr;
1095 int i;
1096 int temp;
1097
1098 // do some very smart indenting when entering '{' or '}'
1099 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
1100 {
1101 // for '}' set indent equal to indent of line containing matching '{'
1102 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1103 {
1104 old_pos = curwin->w_cursor;
1105 // If the matching '{' has a ')' immediately before it (ignoring
1106 // white-space), then line up with the start of the line
1107 // containing the matching '(' if there is one. This handles the
1108 // case where an "if (..\n..) {" statement continues over multiple
1109 // lines -- webb
1110 ptr = ml_get(pos->lnum);
1111 i = pos->col;
1112 if (i > 0) // skip blanks before '{'
1113 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1114 ;
1115 curwin->w_cursor.lnum = pos->lnum;
1116 curwin->w_cursor.col = i;
1117 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1118 curwin->w_cursor = *pos;
1119 i = get_indent();
1120 curwin->w_cursor = old_pos;
1121 if (State & VREPLACE_FLAG)
1122 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1123 else
1124 (void)set_indent(i, SIN_CHANGED);
1125 }
1126 else if (curwin->w_cursor.col > 0)
1127 {
1128 // when inserting '{' after "O" reduce indent, but not
1129 // more than indent of previous line
1130 temp = TRUE;
1131 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1132 {
1133 old_pos = curwin->w_cursor;
1134 i = get_indent();
1135 while (curwin->w_cursor.lnum > 1)
1136 {
1137 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1138
1139 // ignore empty lines and lines starting with '#'.
1140 if (*ptr != '#' && *ptr != NUL)
1141 break;
1142 }
1143 if (get_indent() >= i)
1144 temp = FALSE;
1145 curwin->w_cursor = old_pos;
1146 }
1147 if (temp)
1148 shift_line(TRUE, FALSE, 1, TRUE);
1149 }
1150 }
1151
1152 // set indent of '#' always to 0
1153 if (curwin->w_cursor.col > 0 && can_si && c == '#')
1154 {
1155 // remember current indent for next line
1156 old_indent = get_indent();
1157 (void)set_indent(0, SIN_CHANGED);
1158 }
1159
1160 // Adjust ai_col, the char at this position can be deleted.
1161 if (ai_col > curwin->w_cursor.col)
1162 ai_col = curwin->w_cursor.col;
1163}
1164#endif
1165
1166/*
1167 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1168 * Keep the cursor on the same character.
1169 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1170 * type == INDENT_DEC decrease indent (for CTRL-D)
1171 * type == INDENT_SET set indent to "amount"
1172 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1173 */
1174 void
1175change_indent(
1176 int type,
1177 int amount,
1178 int round,
1179 int replaced, // replaced character, put on replace stack
1180 int call_changed_bytes) // call changed_bytes()
1181{
1182 int vcol;
1183 int last_vcol;
1184 int insstart_less; // reduction for Insstart.col
1185 int new_cursor_col;
1186 int i;
1187 char_u *ptr;
1188 int save_p_list;
1189 int start_col;
1190 colnr_T vc;
1191 colnr_T orig_col = 0; // init for GCC
1192 char_u *new_line, *orig_line = NULL; // init for GCC
1193
1194 // VREPLACE mode needs to know what the line was like before changing
1195 if (State & VREPLACE_FLAG)
1196 {
1197 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1198 orig_col = curwin->w_cursor.col;
1199 }
1200
1201 // for the following tricks we don't want list mode
1202 save_p_list = curwin->w_p_list;
1203 curwin->w_p_list = FALSE;
1204 vc = getvcol_nolist(&curwin->w_cursor);
1205 vcol = vc;
1206
1207 // For Replace mode we need to fix the replace stack later, which is only
1208 // possible when the cursor is in the indent. Remember the number of
1209 // characters before the cursor if it's possible.
1210 start_col = curwin->w_cursor.col;
1211
1212 // determine offset from first non-blank
1213 new_cursor_col = curwin->w_cursor.col;
1214 beginline(BL_WHITE);
1215 new_cursor_col -= curwin->w_cursor.col;
1216
1217 insstart_less = curwin->w_cursor.col;
1218
1219 // If the cursor is in the indent, compute how many screen columns the
1220 // cursor is to the left of the first non-blank.
1221 if (new_cursor_col < 0)
1222 vcol = get_indent() - vcol;
1223
1224 if (new_cursor_col > 0) // can't fix replace stack
1225 start_col = -1;
1226
1227 // Set the new indent. The cursor will be put on the first non-blank.
1228 if (type == INDENT_SET)
1229 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1230 else
1231 {
1232 int save_State = State;
1233
1234 // Avoid being called recursively.
1235 if (State & VREPLACE_FLAG)
1236 State = INSERT;
1237 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1238 State = save_State;
1239 }
1240 insstart_less -= curwin->w_cursor.col;
1241
1242 // Try to put cursor on same character.
1243 // If the cursor is at or after the first non-blank in the line,
1244 // compute the cursor column relative to the column of the first
1245 // non-blank character.
1246 // If we are not in insert mode, leave the cursor on the first non-blank.
1247 // If the cursor is before the first non-blank, position it relative
1248 // to the first non-blank, counted in screen columns.
1249 if (new_cursor_col >= 0)
1250 {
1251 // When changing the indent while the cursor is touching it, reset
1252 // Insstart_col to 0.
1253 if (new_cursor_col == 0)
1254 insstart_less = MAXCOL;
1255 new_cursor_col += curwin->w_cursor.col;
1256 }
1257 else if (!(State & INSERT))
1258 new_cursor_col = curwin->w_cursor.col;
1259 else
1260 {
1261 // Compute the screen column where the cursor should be.
1262 vcol = get_indent() - vcol;
1263 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1264
1265 // Advance the cursor until we reach the right screen column.
1266 vcol = last_vcol = 0;
1267 new_cursor_col = -1;
1268 ptr = ml_get_curline();
1269 while (vcol <= (int)curwin->w_virtcol)
1270 {
1271 last_vcol = vcol;
1272 if (has_mbyte && new_cursor_col >= 0)
1273 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
1274 else
1275 ++new_cursor_col;
1276 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
1277 }
1278 vcol = last_vcol;
1279
1280 // May need to insert spaces to be able to position the cursor on
1281 // the right screen column.
1282 if (vcol != (int)curwin->w_virtcol)
1283 {
1284 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1285 i = (int)curwin->w_virtcol - vcol;
1286 ptr = alloc(i + 1);
1287 if (ptr != NULL)
1288 {
1289 new_cursor_col += i;
1290 ptr[i] = NUL;
1291 while (--i >= 0)
1292 ptr[i] = ' ';
1293 ins_str(ptr);
1294 vim_free(ptr);
1295 }
1296 }
1297
1298 // When changing the indent while the cursor is in it, reset
1299 // Insstart_col to 0.
1300 insstart_less = MAXCOL;
1301 }
1302
1303 curwin->w_p_list = save_p_list;
1304
1305 if (new_cursor_col <= 0)
1306 curwin->w_cursor.col = 0;
1307 else
1308 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1309 curwin->w_set_curswant = TRUE;
1310 changed_cline_bef_curs();
1311
1312 // May have to adjust the start of the insert.
1313 if (State & INSERT)
1314 {
1315 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1316 {
1317 if ((int)Insstart.col <= insstart_less)
1318 Insstart.col = 0;
1319 else
1320 Insstart.col -= insstart_less;
1321 }
1322 if ((int)ai_col <= insstart_less)
1323 ai_col = 0;
1324 else
1325 ai_col -= insstart_less;
1326 }
1327
1328 // For REPLACE mode, may have to fix the replace stack, if it's possible.
1329 // If the number of characters before the cursor decreased, need to pop a
1330 // few characters from the replace stack.
1331 // If the number of characters before the cursor increased, need to push a
1332 // few NULs onto the replace stack.
1333 if (REPLACE_NORMAL(State) && start_col >= 0)
1334 {
1335 while (start_col > (int)curwin->w_cursor.col)
1336 {
1337 replace_join(0); // remove a NUL from the replace stack
1338 --start_col;
1339 }
1340 while (start_col < (int)curwin->w_cursor.col || replaced)
1341 {
1342 replace_push(NUL);
1343 if (replaced)
1344 {
1345 replace_push(replaced);
1346 replaced = NUL;
1347 }
1348 ++start_col;
1349 }
1350 }
1351
1352 // For VREPLACE mode, we also have to fix the replace stack. In this case
1353 // it is always possible because we backspace over the whole line and then
1354 // put it back again the way we wanted it.
1355 if (State & VREPLACE_FLAG)
1356 {
1357 // If orig_line didn't allocate, just return. At least we did the job,
1358 // even if you can't backspace.
1359 if (orig_line == NULL)
1360 return;
1361
1362 // Save new line
1363 new_line = vim_strsave(ml_get_curline());
1364 if (new_line == NULL)
1365 return;
1366
1367 // We only put back the new line up to the cursor
1368 new_line[curwin->w_cursor.col] = NUL;
1369
1370 // Put back original line
1371 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1372 curwin->w_cursor.col = orig_col;
1373
1374 // Backspace from cursor to start of line
1375 backspace_until_column(0);
1376
1377 // Insert new stuff into line again
1378 ins_bytes(new_line);
1379
1380 vim_free(new_line);
1381 }
1382}
1383
1384/*
1385 * Copy the indent from ptr to the current line (and fill to size)
1386 * Leaves the cursor on the first non-blank in the line.
1387 * Returns TRUE if the line was changed.
1388 */
1389 int
1390copy_indent(int size, char_u *src)
1391{
1392 char_u *p = NULL;
1393 char_u *line = NULL;
1394 char_u *s;
1395 int todo;
1396 int ind_len;
1397 int line_len = 0;
1398 int tab_pad;
1399 int ind_done;
1400 int round;
1401#ifdef FEAT_VARTABS
1402 int ind_col;
1403#endif
1404
1405 // Round 1: compute the number of characters needed for the indent
1406 // Round 2: copy the characters.
1407 for (round = 1; round <= 2; ++round)
1408 {
1409 todo = size;
1410 ind_len = 0;
1411 ind_done = 0;
1412#ifdef FEAT_VARTABS
1413 ind_col = 0;
1414#endif
1415 s = src;
1416
1417 // Count/copy the usable portion of the source line
1418 while (todo > 0 && VIM_ISWHITE(*s))
1419 {
1420 if (*s == TAB)
1421 {
1422#ifdef FEAT_VARTABS
1423 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1424 curbuf->b_p_vts_array);
1425#else
1426 tab_pad = (int)curbuf->b_p_ts
1427 - (ind_done % (int)curbuf->b_p_ts);
1428#endif
1429 // Stop if this tab will overshoot the target
1430 if (todo < tab_pad)
1431 break;
1432 todo -= tab_pad;
1433 ind_done += tab_pad;
1434#ifdef FEAT_VARTABS
1435 ind_col += tab_pad;
1436#endif
1437 }
1438 else
1439 {
1440 --todo;
1441 ++ind_done;
1442#ifdef FEAT_VARTABS
1443 ++ind_col;
1444#endif
1445 }
1446 ++ind_len;
1447 if (p != NULL)
1448 *p++ = *s;
1449 ++s;
1450 }
1451
1452 // Fill to next tabstop with a tab, if possible
1453#ifdef FEAT_VARTABS
1454 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1455 curbuf->b_p_vts_array);
1456#else
1457 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1458#endif
1459 if (todo >= tab_pad && !curbuf->b_p_et)
1460 {
1461 todo -= tab_pad;
1462 ++ind_len;
1463#ifdef FEAT_VARTABS
1464 ind_col += tab_pad;
1465#endif
1466 if (p != NULL)
1467 *p++ = TAB;
1468 }
1469
1470 // Add tabs required for indent
1471 if (!curbuf->b_p_et)
1472 {
1473#ifdef FEAT_VARTABS
1474 for (;;)
1475 {
1476 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1477 curbuf->b_p_vts_array);
1478 if (todo < tab_pad)
1479 break;
1480 todo -= tab_pad;
1481 ++ind_len;
1482 ind_col += tab_pad;
1483 if (p != NULL)
1484 *p++ = TAB;
1485 }
1486#else
1487 while (todo >= (int)curbuf->b_p_ts)
1488 {
1489 todo -= (int)curbuf->b_p_ts;
1490 ++ind_len;
1491 if (p != NULL)
1492 *p++ = TAB;
1493 }
1494#endif
1495 }
1496
1497 // Count/add spaces required for indent
1498 while (todo > 0)
1499 {
1500 --todo;
1501 ++ind_len;
1502 if (p != NULL)
1503 *p++ = ' ';
1504 }
1505
1506 if (p == NULL)
1507 {
1508 // Allocate memory for the result: the copied indent, new indent
1509 // and the rest of the line.
1510 line_len = (int)STRLEN(ml_get_curline()) + 1;
1511 line = alloc(ind_len + line_len);
1512 if (line == NULL)
1513 return FALSE;
1514 p = line;
1515 }
1516 }
1517
1518 // Append the original line
1519 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1520
1521 // Replace the line
1522 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1523
1524 // Put the cursor after the indent.
1525 curwin->w_cursor.col = ind_len;
1526 return TRUE;
1527}
1528
1529/*
1530 * ":retab".
1531 */
1532 void
1533ex_retab(exarg_T *eap)
1534{
1535 linenr_T lnum;
1536 int got_tab = FALSE;
1537 long num_spaces = 0;
1538 long num_tabs;
1539 long len;
1540 long col;
1541 long vcol;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001542 long start_col = 0; // For start of white-space string
1543 long start_vcol = 0; // For start of white-space string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001544 long old_len;
1545 char_u *ptr;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001546 char_u *new_line = (char_u *)1; // init to non-NULL
1547 int did_undo; // called u_save for current line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001548#ifdef FEAT_VARTABS
1549 int *new_vts_array = NULL;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001550 char_u *new_ts_str; // string value of tab argument
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001551#else
1552 int temp;
1553 int new_ts;
1554#endif
1555 int save_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001556 linenr_T first_line = 0; // first changed line
1557 linenr_T last_line = 0; // last changed line
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001558
1559 save_list = curwin->w_p_list;
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001560 curwin->w_p_list = 0; // don't want list mode here
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001561
1562#ifdef FEAT_VARTABS
1563 new_ts_str = eap->arg;
1564 if (!tabstop_set(eap->arg, &new_vts_array))
1565 return;
1566 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1567 ++(eap->arg);
1568
1569 // This ensures that either new_vts_array and new_ts_str are freshly
1570 // allocated, or new_vts_array points to an existing array and new_ts_str
1571 // is null.
1572 if (new_vts_array == NULL)
1573 {
1574 new_vts_array = curbuf->b_p_vts_array;
1575 new_ts_str = NULL;
1576 }
1577 else
1578 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1579#else
1580 new_ts = getdigits(&(eap->arg));
1581 if (new_ts < 0)
1582 {
1583 emsg(_(e_positive));
1584 return;
1585 }
1586 if (new_ts == 0)
1587 new_ts = curbuf->b_p_ts;
1588#endif
1589 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1590 {
1591 ptr = ml_get(lnum);
1592 col = 0;
1593 vcol = 0;
1594 did_undo = FALSE;
1595 for (;;)
1596 {
1597 if (VIM_ISWHITE(ptr[col]))
1598 {
1599 if (!got_tab && num_spaces == 0)
1600 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001601 // First consecutive white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001602 start_vcol = vcol;
1603 start_col = col;
1604 }
1605 if (ptr[col] == ' ')
1606 num_spaces++;
1607 else
1608 got_tab = TRUE;
1609 }
1610 else
1611 {
1612 if (got_tab || (eap->forceit && num_spaces > 1))
1613 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001614 // Retabulate this string of white-space
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001615
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001616 // len is virtual length of white string
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001617 len = num_spaces = vcol - start_vcol;
1618 num_tabs = 0;
1619 if (!curbuf->b_p_et)
1620 {
1621#ifdef FEAT_VARTABS
1622 int t, s;
1623
1624 tabstop_fromto(start_vcol, vcol,
1625 curbuf->b_p_ts, new_vts_array, &t, &s);
1626 num_tabs = t;
1627 num_spaces = s;
1628#else
1629 temp = new_ts - (start_vcol % new_ts);
1630 if (num_spaces >= temp)
1631 {
1632 num_spaces -= temp;
1633 num_tabs++;
1634 }
1635 num_tabs += num_spaces / new_ts;
1636 num_spaces -= (num_spaces / new_ts) * new_ts;
1637#endif
1638 }
1639 if (curbuf->b_p_et || got_tab ||
1640 (num_spaces + num_tabs < len))
1641 {
1642 if (did_undo == FALSE)
1643 {
1644 did_undo = TRUE;
1645 if (u_save((linenr_T)(lnum - 1),
1646 (linenr_T)(lnum + 1)) == FAIL)
1647 {
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001648 new_line = NULL; // flag out-of-memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001649 break;
1650 }
1651 }
1652
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001653 // len is actual number of white characters used
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001654 len = num_spaces + num_tabs;
1655 old_len = (long)STRLEN(ptr);
1656 new_line = alloc(old_len - col + start_col + len + 1);
1657 if (new_line == NULL)
1658 break;
1659 if (start_col > 0)
1660 mch_memmove(new_line, ptr, (size_t)start_col);
1661 mch_memmove(new_line + start_col + len,
1662 ptr + col, (size_t)(old_len - col + 1));
1663 ptr = new_line + start_col;
1664 for (col = 0; col < len; col++)
1665 ptr[col] = (col < num_tabs) ? '\t' : ' ';
Bram Moolenaar0dcd39b2021-02-03 19:44:25 +01001666 if (ml_replace(lnum, new_line, FALSE) == OK)
1667 // "new_line" may have been copied
1668 new_line = curbuf->b_ml.ml_line_ptr;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001669 if (first_line == 0)
1670 first_line = lnum;
1671 last_line = lnum;
1672 ptr = new_line;
1673 col = start_col + len;
1674 }
1675 }
1676 got_tab = FALSE;
1677 num_spaces = 0;
1678 }
1679 if (ptr[col] == NUL)
1680 break;
1681 vcol += chartabsize(ptr + col, (colnr_T)vcol);
1682 if (has_mbyte)
1683 col += (*mb_ptr2len)(ptr + col);
1684 else
1685 ++col;
1686 }
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001687 if (new_line == NULL) // out of memory
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001688 break;
1689 line_breakcheck();
1690 }
1691 if (got_int)
1692 emsg(_(e_interr));
1693
1694#ifdef FEAT_VARTABS
1695 // If a single value was given then it can be considered equal to
1696 // either the value of 'tabstop' or the value of 'vartabstop'.
1697 if (tabstop_count(curbuf->b_p_vts_array) == 0
1698 && tabstop_count(new_vts_array) == 1
1699 && curbuf->b_p_ts == tabstop_first(new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001700 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001701 else if (tabstop_count(curbuf->b_p_vts_array) > 0
1702 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001703 ; // not changed
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001704 else
1705 redraw_curbuf_later(NOT_VALID);
1706#else
1707 if (curbuf->b_p_ts != new_ts)
1708 redraw_curbuf_later(NOT_VALID);
1709#endif
1710 if (first_line != 0)
1711 changed_lines(first_line, 0, last_line + 1, 0L);
1712
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001713 curwin->w_p_list = save_list; // restore 'list'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001714
1715#ifdef FEAT_VARTABS
Bram Moolenaaraa2f0ee2019-12-21 18:47:26 +01001716 if (new_ts_str != NULL) // set the new tabstop
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001717 {
1718 // If 'vartabstop' is in use or if the value given to retab has more
1719 // than one tabstop then update 'vartabstop'.
1720 int *old_vts_ary = curbuf->b_p_vts_array;
1721
1722 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1723 {
1724 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1725 OPT_FREE|OPT_LOCAL, 0);
1726 curbuf->b_p_vts_array = new_vts_array;
1727 vim_free(old_vts_ary);
1728 }
1729 else
1730 {
1731 // 'vartabstop' wasn't in use and a single value was given to
1732 // retab then update 'tabstop'.
1733 curbuf->b_p_ts = tabstop_first(new_vts_array);
1734 vim_free(new_vts_array);
1735 }
1736 vim_free(new_ts_str);
1737 }
1738#else
1739 curbuf->b_p_ts = new_ts;
1740#endif
1741 coladvance(curwin->w_curswant);
1742
1743 u_clearline();
1744}
1745
1746#if (defined(FEAT_CINDENT) && defined(FEAT_EVAL)) || defined(PROTO)
1747/*
1748 * Get indent level from 'indentexpr'.
1749 */
1750 int
1751get_expr_indent(void)
1752{
1753 int indent = -1;
1754 char_u *inde_copy;
1755 pos_T save_pos;
1756 colnr_T save_curswant;
1757 int save_set_curswant;
1758 int save_State;
1759 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1760 OPT_LOCAL);
1761
1762 // Save and restore cursor position and curswant, in case it was changed
1763 // via :normal commands
1764 save_pos = curwin->w_cursor;
1765 save_curswant = curwin->w_curswant;
1766 save_set_curswant = curwin->w_set_curswant;
1767 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1768 if (use_sandbox)
1769 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001770 ++textwinlock;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001771
1772 // Need to make a copy, the 'indentexpr' option could be changed while
1773 // evaluating it.
1774 inde_copy = vim_strsave(curbuf->b_p_inde);
1775 if (inde_copy != NULL)
1776 {
1777 indent = (int)eval_to_number(inde_copy);
1778 vim_free(inde_copy);
1779 }
1780
1781 if (use_sandbox)
1782 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001783 --textwinlock;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001784
1785 // Restore the cursor position so that 'indentexpr' doesn't need to.
1786 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1787 // command.
1788 save_State = State;
1789 State = INSERT;
1790 curwin->w_cursor = save_pos;
1791 curwin->w_curswant = save_curswant;
1792 curwin->w_set_curswant = save_set_curswant;
1793 check_cursor();
1794 State = save_State;
1795
1796 // If there is an error, just keep the current indent.
1797 if (indent < 0)
1798 indent = get_indent();
1799
1800 return indent;
1801}
1802#endif
1803
1804#if defined(FEAT_LISP) || defined(PROTO)
1805
1806 static int
1807lisp_match(char_u *p)
1808{
1809 char_u buf[LSIZE];
1810 int len;
1811 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1812
1813 while (*word != NUL)
1814 {
1815 (void)copy_option_part(&word, buf, LSIZE, ",");
1816 len = (int)STRLEN(buf);
1817 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
1818 return TRUE;
1819 }
1820 return FALSE;
1821}
1822
1823/*
1824 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
1825 * The incompatible newer method is quite a bit better at indenting
1826 * code in lisp-like languages than the traditional one; it's still
1827 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
1828 *
1829 * TODO:
1830 * Findmatch() should be adapted for lisp, also to make showmatch
1831 * work correctly: now (v5.3) it seems all C/C++ oriented:
1832 * - it does not recognize the #\( and #\) notations as character literals
1833 * - it doesn't know about comments starting with a semicolon
1834 * - it incorrectly interprets '(' as a character literal
1835 * All this messes up get_lisp_indent in some rare cases.
1836 * Update from Sergey Khorev:
1837 * I tried to fix the first two issues.
1838 */
1839 int
1840get_lisp_indent(void)
1841{
1842 pos_T *pos, realpos, paren;
1843 int amount;
1844 char_u *that;
1845 colnr_T col;
1846 colnr_T firsttry;
1847 int parencount, quotecount;
1848 int vi_lisp;
1849
1850 // Set vi_lisp to use the vi-compatible method
1851 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
1852
1853 realpos = curwin->w_cursor;
1854 curwin->w_cursor.col = 0;
1855
1856 if ((pos = findmatch(NULL, '(')) == NULL)
1857 pos = findmatch(NULL, '[');
1858 else
1859 {
1860 paren = *pos;
1861 pos = findmatch(NULL, '[');
1862 if (pos == NULL || LT_POSP(pos, &paren))
1863 pos = &paren;
1864 }
1865 if (pos != NULL)
1866 {
1867 // Extra trick: Take the indent of the first previous non-white
1868 // line that is at the same () level.
1869 amount = -1;
1870 parencount = 0;
1871
1872 while (--curwin->w_cursor.lnum >= pos->lnum)
1873 {
1874 if (linewhite(curwin->w_cursor.lnum))
1875 continue;
1876 for (that = ml_get_curline(); *that != NUL; ++that)
1877 {
1878 if (*that == ';')
1879 {
1880 while (*(that + 1) != NUL)
1881 ++that;
1882 continue;
1883 }
1884 if (*that == '\\')
1885 {
1886 if (*(that + 1) != NUL)
1887 ++that;
1888 continue;
1889 }
1890 if (*that == '"' && *(that + 1) != NUL)
1891 {
1892 while (*++that && *that != '"')
1893 {
1894 // skipping escaped characters in the string
1895 if (*that == '\\')
1896 {
1897 if (*++that == NUL)
1898 break;
1899 if (that[1] == NUL)
1900 {
1901 ++that;
1902 break;
1903 }
1904 }
1905 }
1906 }
1907 if (*that == '(' || *that == '[')
1908 ++parencount;
1909 else if (*that == ')' || *that == ']')
1910 --parencount;
1911 }
1912 if (parencount == 0)
1913 {
1914 amount = get_indent();
1915 break;
1916 }
1917 }
1918
1919 if (amount == -1)
1920 {
1921 curwin->w_cursor.lnum = pos->lnum;
1922 curwin->w_cursor.col = pos->col;
1923 col = pos->col;
1924
1925 that = ml_get_curline();
1926
1927 if (vi_lisp && get_indent() == 0)
1928 amount = 2;
1929 else
1930 {
1931 char_u *line = that;
1932
1933 amount = 0;
1934 while (*that && col)
1935 {
1936 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
1937 col--;
1938 }
1939
1940 // Some keywords require "body" indenting rules (the
1941 // non-standard-lisp ones are Scheme special forms):
1942 //
1943 // (let ((a 1)) instead (let ((a 1))
1944 // (...)) of (...))
1945
1946 if (!vi_lisp && (*that == '(' || *that == '[')
1947 && lisp_match(that + 1))
1948 amount += 2;
1949 else
1950 {
1951 that++;
1952 amount++;
1953 firsttry = amount;
1954
1955 while (VIM_ISWHITE(*that))
1956 {
1957 amount += lbr_chartabsize(line, that, (colnr_T)amount);
1958 ++that;
1959 }
1960
1961 if (*that && *that != ';') // not a comment line
1962 {
1963 // test *that != '(' to accommodate first let/do
1964 // argument if it is more than one line
1965 if (!vi_lisp && *that != '(' && *that != '[')
1966 firsttry++;
1967
1968 parencount = 0;
1969 quotecount = 0;
1970
1971 if (vi_lisp
1972 || (*that != '"'
1973 && *that != '\''
1974 && *that != '#'
1975 && (*that < '0' || *that > '9')))
1976 {
1977 while (*that
1978 && (!VIM_ISWHITE(*that)
1979 || quotecount
1980 || parencount)
1981 && (!((*that == '(' || *that == '[')
1982 && !quotecount
1983 && !parencount
1984 && vi_lisp)))
1985 {
1986 if (*that == '"')
1987 quotecount = !quotecount;
1988 if ((*that == '(' || *that == '[')
1989 && !quotecount)
1990 ++parencount;
1991 if ((*that == ')' || *that == ']')
1992 && !quotecount)
1993 --parencount;
1994 if (*that == '\\' && *(that+1) != NUL)
1995 amount += lbr_chartabsize_adv(
1996 line, &that, (colnr_T)amount);
1997 amount += lbr_chartabsize_adv(
1998 line, &that, (colnr_T)amount);
1999 }
2000 }
2001 while (VIM_ISWHITE(*that))
2002 {
2003 amount += lbr_chartabsize(
2004 line, that, (colnr_T)amount);
2005 that++;
2006 }
2007 if (!*that || *that == ';')
2008 amount = firsttry;
2009 }
2010 }
2011 }
2012 }
2013 }
2014 else
2015 amount = 0; // no matching '(' or '[' found, use zero indent
2016
2017 curwin->w_cursor = realpos;
2018
2019 return amount;
2020}
2021#endif // FEAT_LISP
2022
2023#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
2024/*
2025 * Re-indent the current line, based on the current contents of it and the
2026 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
2027 * confused what all the part that handles Control-T is doing that I'm not.
2028 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
2029 */
2030
2031 void
2032fixthisline(int (*get_the_indent)(void))
2033{
2034 int amount = get_the_indent();
2035
2036 if (amount >= 0)
2037 {
2038 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
2039 if (linewhite(curwin->w_cursor.lnum))
2040 did_ai = TRUE; // delete the indent if the line stays empty
2041 }
2042}
2043
2044 void
2045fix_indent(void)
2046{
2047 if (p_paste)
2048 return;
2049# ifdef FEAT_LISP
2050 if (curbuf->b_p_lisp && curbuf->b_p_ai)
2051 fixthisline(get_lisp_indent);
2052# endif
2053# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
2054 else
2055# endif
2056# ifdef FEAT_CINDENT
2057 if (cindent_on())
2058 do_c_expr_indent();
2059# endif
2060}
2061#endif
2062
2063#if defined(FEAT_EVAL) || defined(PROTO)
2064/*
2065 * "indent()" function
2066 */
2067 void
2068f_indent(typval_T *argvars, typval_T *rettv)
2069{
2070 linenr_T lnum;
2071
2072 lnum = tv_get_lnum(argvars);
2073 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2074 rettv->vval.v_number = get_indent_lnum(lnum);
2075 else
2076 rettv->vval.v_number = -1;
2077}
2078
2079/*
2080 * "lispindent(lnum)" function
2081 */
2082 void
2083f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2084{
2085#ifdef FEAT_LISP
2086 pos_T pos;
2087 linenr_T lnum;
2088
2089 pos = curwin->w_cursor;
2090 lnum = tv_get_lnum(argvars);
2091 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2092 {
2093 curwin->w_cursor.lnum = lnum;
2094 rettv->vval.v_number = get_lisp_indent();
2095 curwin->w_cursor = pos;
2096 }
2097 else
2098#endif
2099 rettv->vval.v_number = -1;
2100}
2101#endif