blob: 8d574c8a7115f3454b293a5f0dd322e26162cecc [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 {
435 if (!list || lcs_tab1) // count a tab for what it is worth
436 count += ts - (count % ts);
437 else
438 // In list mode, when tab is not set, count screen char width
439 // for Tab, displays: ^I
440 count += ptr2cells(ptr);
441 }
442 else if (*ptr == ' ')
443 ++count; // count a space for one
444 else
445 break;
446 }
447 return count;
448}
449
450#ifdef FEAT_VARTABS
451/*
452 * Count the size (in window cells) of the indent in line "ptr", using
453 * variable tabstops.
454 * if "list" is TRUE, count only screen size for tabs.
455 */
456 int
457get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
458{
459 int count = 0;
460
461 for ( ; *ptr; ++ptr)
462 {
463 if (*ptr == TAB) // count a tab for what it is worth
464 {
465 if (!list || lcs_tab1)
466 count += tabstop_padding(count, ts, vts);
467 else
468 // In list mode, when tab is not set, count screen char width
469 // for Tab, displays: ^I
470 count += ptr2cells(ptr);
471 }
472 else if (*ptr == ' ')
473 ++count; // count a space for one
474 else
475 break;
476 }
477 return count;
478}
479#endif
480
481/*
482 * Set the indent of the current line.
483 * Leaves the cursor on the first non-blank in the line.
484 * Caller must take care of undo.
485 * "flags":
486 * SIN_CHANGED: call changed_bytes() if the line was changed.
487 * SIN_INSERT: insert the indent in front of the line.
488 * SIN_UNDO: save line for undo before changing it.
489 * Returns TRUE if the line was changed.
490 */
491 int
492set_indent(
493 int size, // measured in spaces
494 int flags)
495{
496 char_u *p;
497 char_u *newline;
498 char_u *oldline;
499 char_u *s;
500 int todo;
501 int ind_len; // measured in characters
502 int line_len;
503 int doit = FALSE;
504 int ind_done = 0; // measured in spaces
505#ifdef FEAT_VARTABS
506 int ind_col = 0;
507#endif
508 int tab_pad;
509 int retval = FALSE;
510 int orig_char_len = -1; // number of initial whitespace chars when
511 // 'et' and 'pi' are both set
512
513 // First check if there is anything to do and compute the number of
514 // characters needed for the indent.
515 todo = size;
516 ind_len = 0;
517 p = oldline = ml_get_curline();
518
519 // Calculate the buffer size for the new indent, and check to see if it
520 // isn't already set
521
522 // if 'expandtab' isn't set: use TABs; if both 'expandtab' and
523 // 'preserveindent' are set count the number of characters at the
524 // beginning of the line to be copied
525 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
526 {
527 // If 'preserveindent' is set then reuse as much as possible of
528 // the existing indent structure for the new indent
529 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
530 {
531 ind_done = 0;
532
533 // count as many characters as we can use
534 while (todo > 0 && VIM_ISWHITE(*p))
535 {
536 if (*p == TAB)
537 {
538#ifdef FEAT_VARTABS
539 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
540 curbuf->b_p_vts_array);
541#else
542 tab_pad = (int)curbuf->b_p_ts
543 - (ind_done % (int)curbuf->b_p_ts);
544#endif
545 // stop if this tab will overshoot the target
546 if (todo < tab_pad)
547 break;
548 todo -= tab_pad;
549 ++ind_len;
550 ind_done += tab_pad;
551 }
552 else
553 {
554 --todo;
555 ++ind_len;
556 ++ind_done;
557 }
558 ++p;
559 }
560
561#ifdef FEAT_VARTABS
562 // These diverge from this point.
563 ind_col = ind_done;
564#endif
565 // Set initial number of whitespace chars to copy if we are
566 // preserving indent but expandtab is set
567 if (curbuf->b_p_et)
568 orig_char_len = ind_len;
569
570 // Fill to next tabstop with a tab, if possible
571#ifdef FEAT_VARTABS
572 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
573 curbuf->b_p_vts_array);
574#else
575 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
576#endif
577 if (todo >= tab_pad && orig_char_len == -1)
578 {
579 doit = TRUE;
580 todo -= tab_pad;
581 ++ind_len;
582 // ind_done += tab_pad;
583#ifdef FEAT_VARTABS
584 ind_col += tab_pad;
585#endif
586 }
587 }
588
589 // count tabs required for indent
590#ifdef FEAT_VARTABS
591 for (;;)
592 {
593 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
594 curbuf->b_p_vts_array);
595 if (todo < tab_pad)
596 break;
597 if (*p != TAB)
598 doit = TRUE;
599 else
600 ++p;
601 todo -= tab_pad;
602 ++ind_len;
603 ind_col += tab_pad;
604 }
605#else
606 while (todo >= (int)curbuf->b_p_ts)
607 {
608 if (*p != TAB)
609 doit = TRUE;
610 else
611 ++p;
612 todo -= (int)curbuf->b_p_ts;
613 ++ind_len;
614 // ind_done += (int)curbuf->b_p_ts;
615 }
616#endif
617 }
618 // count spaces required for indent
619 while (todo > 0)
620 {
621 if (*p != ' ')
622 doit = TRUE;
623 else
624 ++p;
625 --todo;
626 ++ind_len;
627 // ++ind_done;
628 }
629
630 // Return if the indent is OK already.
631 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
632 return FALSE;
633
634 // Allocate memory for the new line.
635 if (flags & SIN_INSERT)
636 p = oldline;
637 else
638 p = skipwhite(p);
639 line_len = (int)STRLEN(p) + 1;
640
641 // If 'preserveindent' and 'expandtab' are both set keep the original
642 // characters and allocate accordingly. We will fill the rest with spaces
643 // after the if (!curbuf->b_p_et) below.
644 if (orig_char_len != -1)
645 {
646 newline = alloc(orig_char_len + size - ind_done + line_len);
647 if (newline == NULL)
648 return FALSE;
649 todo = size - ind_done;
650 ind_len = orig_char_len + todo; // Set total length of indent in
651 // characters, which may have been
652 // undercounted until now
653 p = oldline;
654 s = newline;
655 while (orig_char_len > 0)
656 {
657 *s++ = *p++;
658 orig_char_len--;
659 }
660
661 // Skip over any additional white space (useful when newindent is less
662 // than old)
663 while (VIM_ISWHITE(*p))
664 ++p;
665
666 }
667 else
668 {
669 todo = size;
670 newline = alloc(ind_len + line_len);
671 if (newline == NULL)
672 return FALSE;
673 s = newline;
674 }
675
676 // Put the characters in the new line.
677 // if 'expandtab' isn't set: use TABs
678 if (!curbuf->b_p_et)
679 {
680 // If 'preserveindent' is set then reuse as much as possible of
681 // the existing indent structure for the new indent
682 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
683 {
684 p = oldline;
685 ind_done = 0;
686
687 while (todo > 0 && VIM_ISWHITE(*p))
688 {
689 if (*p == TAB)
690 {
691#ifdef FEAT_VARTABS
692 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
693 curbuf->b_p_vts_array);
694#else
695 tab_pad = (int)curbuf->b_p_ts
696 - (ind_done % (int)curbuf->b_p_ts);
697#endif
698 // stop if this tab will overshoot the target
699 if (todo < tab_pad)
700 break;
701 todo -= tab_pad;
702 ind_done += tab_pad;
703 }
704 else
705 {
706 --todo;
707 ++ind_done;
708 }
709 *s++ = *p++;
710 }
711
712 // Fill to next tabstop with a tab, if possible
713#ifdef FEAT_VARTABS
714 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
715 curbuf->b_p_vts_array);
716#else
717 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
718#endif
719 if (todo >= tab_pad)
720 {
721 *s++ = TAB;
722 todo -= tab_pad;
723#ifdef FEAT_VARTABS
724 ind_done += tab_pad;
725#endif
726 }
727
728 p = skipwhite(p);
729 }
730
731#ifdef FEAT_VARTABS
732 for (;;)
733 {
734 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
735 curbuf->b_p_vts_array);
736 if (todo < tab_pad)
737 break;
738 *s++ = TAB;
739 todo -= tab_pad;
740 ind_done += tab_pad;
741 }
742#else
743 while (todo >= (int)curbuf->b_p_ts)
744 {
745 *s++ = TAB;
746 todo -= (int)curbuf->b_p_ts;
747 }
748#endif
749 }
750 while (todo > 0)
751 {
752 *s++ = ' ';
753 --todo;
754 }
755 mch_memmove(s, p, (size_t)line_len);
756
757 // Replace the line (unless undo fails).
758 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
759 {
760 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
761 if (flags & SIN_CHANGED)
762 changed_bytes(curwin->w_cursor.lnum, 0);
763
764 // Correct saved cursor position if it is in this line.
765 if (saved_cursor.lnum == curwin->w_cursor.lnum)
766 {
767 if (saved_cursor.col >= (colnr_T)(p - oldline))
768 // cursor was after the indent, adjust for the number of
769 // bytes added/removed
770 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
771 else if (saved_cursor.col >= (colnr_T)(s - newline))
772 // cursor was in the indent, and is now after it, put it back
773 // at the start of the indent (replacing spaces with TAB)
774 saved_cursor.col = (colnr_T)(s - newline);
775 }
776#ifdef FEAT_TEXT_PROP
777 {
778 int added = ind_len - (colnr_T)(p - oldline);
779
780 // When increasing indent this behaves like spaces were inserted at
781 // the old indent, when decreasing indent it behaves like spaces
782 // were deleted at the new indent.
783 adjust_prop_columns(curwin->w_cursor.lnum,
784 (colnr_T)(added > 0 ? (p - oldline) : ind_len), added, 0);
785 }
786#endif
787 retval = TRUE;
788 }
789 else
790 vim_free(newline);
791
792 curwin->w_cursor.col = ind_len;
793 return retval;
794}
795
796/*
797 * Return the indent of the current line after a number. Return -1 if no
798 * number was found. Used for 'n' in 'formatoptions': numbered list.
799 * Since a pattern is used it can actually handle more than numbers.
800 */
801 int
802get_number_indent(linenr_T lnum)
803{
804 colnr_T col;
805 pos_T pos;
806
807 regmatch_T regmatch;
808 int lead_len = 0; // length of comment leader
809
810 if (lnum > curbuf->b_ml.ml_line_count)
811 return -1;
812 pos.lnum = 0;
813
814 // In format_lines() (i.e. not insert mode), fo+=q is needed too...
815 if ((State & INSERT) || has_format_option(FO_Q_COMS))
816 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
817
818 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
819 if (regmatch.regprog != NULL)
820 {
821 regmatch.rm_ic = FALSE;
822
823 // vim_regexec() expects a pointer to a line. This lets us
824 // start matching for the flp beyond any comment leader...
825 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
826 {
827 pos.lnum = lnum;
828 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
829 pos.coladd = 0;
830 }
831 vim_regfree(regmatch.regprog);
832 }
833
834 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
835 return -1;
836 getvcol(curwin, &pos, &col, NULL, NULL);
837 return (int)col;
838}
839
840#if defined(FEAT_LINEBREAK) || defined(PROTO)
841/*
842 * Return appropriate space number for breakindent, taking influencing
843 * parameters into account. Window must be specified, since it is not
844 * necessarily always the current one.
845 */
846 int
847get_breakindent_win(
848 win_T *wp,
849 char_u *line) // start of the line
850{
851 static int prev_indent = 0; // cached indent value
852 static long prev_ts = 0L; // cached tabstop value
853 static char_u *prev_line = NULL; // cached pointer to line
854 static varnumber_T prev_tick = 0; // changedtick of cached value
855# ifdef FEAT_VARTABS
856 static int *prev_vts = NULL; // cached vartabs values
857# endif
858 int bri = 0;
859 // window width minus window margin space, i.e. what rests for text
860 const int eff_wwidth = wp->w_width
861 - ((wp->w_p_nu || wp->w_p_rnu)
862 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
863 ? number_width(wp) + 1 : 0);
864
865 // used cached indent, unless pointer or 'tabstop' changed
866 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
867 || prev_tick != CHANGEDTICK(wp->w_buffer)
868# ifdef FEAT_VARTABS
869 || prev_vts != wp->w_buffer->b_p_vts_array
870# endif
871 )
872 {
873 prev_line = line;
874 prev_ts = wp->w_buffer->b_p_ts;
875 prev_tick = CHANGEDTICK(wp->w_buffer);
876# ifdef FEAT_VARTABS
877 prev_vts = wp->w_buffer->b_p_vts_array;
878 prev_indent = get_indent_str_vtab(line,
879 (int)wp->w_buffer->b_p_ts,
880 wp->w_buffer->b_p_vts_array, wp->w_p_list);
881# else
882 prev_indent = get_indent_str(line,
883 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
884# endif
885 }
886 bri = prev_indent + wp->w_p_brishift;
887
888 // indent minus the length of the showbreak string
889 if (wp->w_p_brisbr)
Bram Moolenaar91e22eb2019-11-10 00:19:12 +0100890 bri -= vim_strsize(get_showbreak_value(wp));
Bram Moolenaar14c01f82019-10-09 22:53:08 +0200891
892 // Add offset for number column, if 'n' is in 'cpoptions'
893 bri += win_col_off2(wp);
894
895 // never indent past left window margin
896 if (bri < 0)
897 bri = 0;
898 // always leave at least bri_min characters on the left,
899 // if text width is sufficient
900 else if (bri > eff_wwidth - wp->w_p_brimin)
901 bri = (eff_wwidth - wp->w_p_brimin < 0)
902 ? 0 : eff_wwidth - wp->w_p_brimin;
903
904 return bri;
905}
906#endif
907
908/*
909 * When extra == 0: Return TRUE if the cursor is before or on the first
910 * non-blank in the line.
911 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
912 * the line.
913 */
914 int
915inindent(int extra)
916{
917 char_u *ptr;
918 colnr_T col;
919
920 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
921 ++ptr;
922 if (col >= curwin->w_cursor.col + extra)
923 return TRUE;
924 else
925 return FALSE;
926}
927
928#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
929/*
930 * op_reindent - handle reindenting a block of lines.
931 */
932 void
933op_reindent(oparg_T *oap, int (*how)(void))
934{
935 long i;
936 char_u *l;
937 int amount;
938 linenr_T first_changed = 0;
939 linenr_T last_changed = 0;
940 linenr_T start_lnum = curwin->w_cursor.lnum;
941
942 // Don't even try when 'modifiable' is off.
943 if (!curbuf->b_p_ma)
944 {
945 emsg(_(e_modifiable));
946 return;
947 }
948
949 for (i = oap->line_count; --i >= 0 && !got_int; )
950 {
951 // it's a slow thing to do, so give feedback so there's no worry that
952 // the computer's just hung.
953
954 if (i > 1
955 && (i % 50 == 0 || i == oap->line_count - 1)
956 && oap->line_count > p_report)
957 smsg(_("%ld lines to indent... "), i);
958
959 // Be vi-compatible: For lisp indenting the first line is not
960 // indented, unless there is only one line.
961# ifdef FEAT_LISP
962 if (i != oap->line_count - 1 || oap->line_count == 1
963 || how != get_lisp_indent)
964# endif
965 {
966 l = skipwhite(ml_get_curline());
967 if (*l == NUL) // empty or blank line
968 amount = 0;
969 else
970 amount = how(); // get the indent for this line
971
972 if (amount >= 0 && set_indent(amount, SIN_UNDO))
973 {
974 // did change the indent, call changed_lines() later
975 if (first_changed == 0)
976 first_changed = curwin->w_cursor.lnum;
977 last_changed = curwin->w_cursor.lnum;
978 }
979 }
980 ++curwin->w_cursor.lnum;
981 curwin->w_cursor.col = 0; // make sure it's valid
982 }
983
984 // put cursor on first non-blank of indented line
985 curwin->w_cursor.lnum = start_lnum;
986 beginline(BL_SOL | BL_FIX);
987
988 // Mark changed lines so that they will be redrawn. When Visual
989 // highlighting was present, need to continue until the last line. When
990 // there is no change still need to remove the Visual highlighting.
991 if (last_changed != 0)
992 changed_lines(first_changed, 0,
993 oap->is_VIsual ? start_lnum + oap->line_count :
994 last_changed + 1, 0L);
995 else if (oap->is_VIsual)
996 redraw_curbuf_later(INVERTED);
997
998 if (oap->line_count > p_report)
999 {
1000 i = oap->line_count - (i + 1);
1001 smsg(NGETTEXT("%ld line indented ",
1002 "%ld lines indented ", i), i);
1003 }
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001004 if (!cmdmod.lockmarks)
1005 {
1006 // set '[ and '] marks
1007 curbuf->b_op_start = oap->start;
1008 curbuf->b_op_end = oap->end;
1009 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001010}
1011#endif // defined(FEAT_LISP) || defined(FEAT_CINDENT)
1012
1013#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
1014/*
1015 * Return TRUE if lines starting with '#' should be left aligned.
1016 */
1017 int
1018preprocs_left(void)
1019{
1020 return
1021# ifdef FEAT_SMARTINDENT
1022# ifdef FEAT_CINDENT
1023 (curbuf->b_p_si && !curbuf->b_p_cin) ||
1024# else
1025 curbuf->b_p_si
1026# endif
1027# endif
1028# ifdef FEAT_CINDENT
1029 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1030 && curbuf->b_ind_hash_comment == 0)
1031# endif
1032 ;
1033}
1034#endif
1035
1036#ifdef FEAT_SMARTINDENT
1037/*
1038 * Try to do some very smart auto-indenting.
1039 * Used when inserting a "normal" character.
1040 */
1041 void
1042ins_try_si(int c)
1043{
1044 pos_T *pos, old_pos;
1045 char_u *ptr;
1046 int i;
1047 int temp;
1048
1049 // do some very smart indenting when entering '{' or '}'
1050 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
1051 {
1052 // for '}' set indent equal to indent of line containing matching '{'
1053 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
1054 {
1055 old_pos = curwin->w_cursor;
1056 // If the matching '{' has a ')' immediately before it (ignoring
1057 // white-space), then line up with the start of the line
1058 // containing the matching '(' if there is one. This handles the
1059 // case where an "if (..\n..) {" statement continues over multiple
1060 // lines -- webb
1061 ptr = ml_get(pos->lnum);
1062 i = pos->col;
1063 if (i > 0) // skip blanks before '{'
1064 while (--i > 0 && VIM_ISWHITE(ptr[i]))
1065 ;
1066 curwin->w_cursor.lnum = pos->lnum;
1067 curwin->w_cursor.col = i;
1068 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
1069 curwin->w_cursor = *pos;
1070 i = get_indent();
1071 curwin->w_cursor = old_pos;
1072 if (State & VREPLACE_FLAG)
1073 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
1074 else
1075 (void)set_indent(i, SIN_CHANGED);
1076 }
1077 else if (curwin->w_cursor.col > 0)
1078 {
1079 // when inserting '{' after "O" reduce indent, but not
1080 // more than indent of previous line
1081 temp = TRUE;
1082 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
1083 {
1084 old_pos = curwin->w_cursor;
1085 i = get_indent();
1086 while (curwin->w_cursor.lnum > 1)
1087 {
1088 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
1089
1090 // ignore empty lines and lines starting with '#'.
1091 if (*ptr != '#' && *ptr != NUL)
1092 break;
1093 }
1094 if (get_indent() >= i)
1095 temp = FALSE;
1096 curwin->w_cursor = old_pos;
1097 }
1098 if (temp)
1099 shift_line(TRUE, FALSE, 1, TRUE);
1100 }
1101 }
1102
1103 // set indent of '#' always to 0
1104 if (curwin->w_cursor.col > 0 && can_si && c == '#')
1105 {
1106 // remember current indent for next line
1107 old_indent = get_indent();
1108 (void)set_indent(0, SIN_CHANGED);
1109 }
1110
1111 // Adjust ai_col, the char at this position can be deleted.
1112 if (ai_col > curwin->w_cursor.col)
1113 ai_col = curwin->w_cursor.col;
1114}
1115#endif
1116
1117/*
1118 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1119 * Keep the cursor on the same character.
1120 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1121 * type == INDENT_DEC decrease indent (for CTRL-D)
1122 * type == INDENT_SET set indent to "amount"
1123 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1124 */
1125 void
1126change_indent(
1127 int type,
1128 int amount,
1129 int round,
1130 int replaced, // replaced character, put on replace stack
1131 int call_changed_bytes) // call changed_bytes()
1132{
1133 int vcol;
1134 int last_vcol;
1135 int insstart_less; // reduction for Insstart.col
1136 int new_cursor_col;
1137 int i;
1138 char_u *ptr;
1139 int save_p_list;
1140 int start_col;
1141 colnr_T vc;
1142 colnr_T orig_col = 0; // init for GCC
1143 char_u *new_line, *orig_line = NULL; // init for GCC
1144
1145 // VREPLACE mode needs to know what the line was like before changing
1146 if (State & VREPLACE_FLAG)
1147 {
1148 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below
1149 orig_col = curwin->w_cursor.col;
1150 }
1151
1152 // for the following tricks we don't want list mode
1153 save_p_list = curwin->w_p_list;
1154 curwin->w_p_list = FALSE;
1155 vc = getvcol_nolist(&curwin->w_cursor);
1156 vcol = vc;
1157
1158 // For Replace mode we need to fix the replace stack later, which is only
1159 // possible when the cursor is in the indent. Remember the number of
1160 // characters before the cursor if it's possible.
1161 start_col = curwin->w_cursor.col;
1162
1163 // determine offset from first non-blank
1164 new_cursor_col = curwin->w_cursor.col;
1165 beginline(BL_WHITE);
1166 new_cursor_col -= curwin->w_cursor.col;
1167
1168 insstart_less = curwin->w_cursor.col;
1169
1170 // If the cursor is in the indent, compute how many screen columns the
1171 // cursor is to the left of the first non-blank.
1172 if (new_cursor_col < 0)
1173 vcol = get_indent() - vcol;
1174
1175 if (new_cursor_col > 0) // can't fix replace stack
1176 start_col = -1;
1177
1178 // Set the new indent. The cursor will be put on the first non-blank.
1179 if (type == INDENT_SET)
1180 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
1181 else
1182 {
1183 int save_State = State;
1184
1185 // Avoid being called recursively.
1186 if (State & VREPLACE_FLAG)
1187 State = INSERT;
1188 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
1189 State = save_State;
1190 }
1191 insstart_less -= curwin->w_cursor.col;
1192
1193 // Try to put cursor on same character.
1194 // If the cursor is at or after the first non-blank in the line,
1195 // compute the cursor column relative to the column of the first
1196 // non-blank character.
1197 // If we are not in insert mode, leave the cursor on the first non-blank.
1198 // If the cursor is before the first non-blank, position it relative
1199 // to the first non-blank, counted in screen columns.
1200 if (new_cursor_col >= 0)
1201 {
1202 // When changing the indent while the cursor is touching it, reset
1203 // Insstart_col to 0.
1204 if (new_cursor_col == 0)
1205 insstart_less = MAXCOL;
1206 new_cursor_col += curwin->w_cursor.col;
1207 }
1208 else if (!(State & INSERT))
1209 new_cursor_col = curwin->w_cursor.col;
1210 else
1211 {
1212 // Compute the screen column where the cursor should be.
1213 vcol = get_indent() - vcol;
1214 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
1215
1216 // Advance the cursor until we reach the right screen column.
1217 vcol = last_vcol = 0;
1218 new_cursor_col = -1;
1219 ptr = ml_get_curline();
1220 while (vcol <= (int)curwin->w_virtcol)
1221 {
1222 last_vcol = vcol;
1223 if (has_mbyte && new_cursor_col >= 0)
1224 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
1225 else
1226 ++new_cursor_col;
1227 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
1228 }
1229 vcol = last_vcol;
1230
1231 // May need to insert spaces to be able to position the cursor on
1232 // the right screen column.
1233 if (vcol != (int)curwin->w_virtcol)
1234 {
1235 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1236 i = (int)curwin->w_virtcol - vcol;
1237 ptr = alloc(i + 1);
1238 if (ptr != NULL)
1239 {
1240 new_cursor_col += i;
1241 ptr[i] = NUL;
1242 while (--i >= 0)
1243 ptr[i] = ' ';
1244 ins_str(ptr);
1245 vim_free(ptr);
1246 }
1247 }
1248
1249 // When changing the indent while the cursor is in it, reset
1250 // Insstart_col to 0.
1251 insstart_less = MAXCOL;
1252 }
1253
1254 curwin->w_p_list = save_p_list;
1255
1256 if (new_cursor_col <= 0)
1257 curwin->w_cursor.col = 0;
1258 else
1259 curwin->w_cursor.col = (colnr_T)new_cursor_col;
1260 curwin->w_set_curswant = TRUE;
1261 changed_cline_bef_curs();
1262
1263 // May have to adjust the start of the insert.
1264 if (State & INSERT)
1265 {
1266 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1267 {
1268 if ((int)Insstart.col <= insstart_less)
1269 Insstart.col = 0;
1270 else
1271 Insstart.col -= insstart_less;
1272 }
1273 if ((int)ai_col <= insstart_less)
1274 ai_col = 0;
1275 else
1276 ai_col -= insstart_less;
1277 }
1278
1279 // For REPLACE mode, may have to fix the replace stack, if it's possible.
1280 // If the number of characters before the cursor decreased, need to pop a
1281 // few characters from the replace stack.
1282 // If the number of characters before the cursor increased, need to push a
1283 // few NULs onto the replace stack.
1284 if (REPLACE_NORMAL(State) && start_col >= 0)
1285 {
1286 while (start_col > (int)curwin->w_cursor.col)
1287 {
1288 replace_join(0); // remove a NUL from the replace stack
1289 --start_col;
1290 }
1291 while (start_col < (int)curwin->w_cursor.col || replaced)
1292 {
1293 replace_push(NUL);
1294 if (replaced)
1295 {
1296 replace_push(replaced);
1297 replaced = NUL;
1298 }
1299 ++start_col;
1300 }
1301 }
1302
1303 // For VREPLACE mode, we also have to fix the replace stack. In this case
1304 // it is always possible because we backspace over the whole line and then
1305 // put it back again the way we wanted it.
1306 if (State & VREPLACE_FLAG)
1307 {
1308 // If orig_line didn't allocate, just return. At least we did the job,
1309 // even if you can't backspace.
1310 if (orig_line == NULL)
1311 return;
1312
1313 // Save new line
1314 new_line = vim_strsave(ml_get_curline());
1315 if (new_line == NULL)
1316 return;
1317
1318 // We only put back the new line up to the cursor
1319 new_line[curwin->w_cursor.col] = NUL;
1320
1321 // Put back original line
1322 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1323 curwin->w_cursor.col = orig_col;
1324
1325 // Backspace from cursor to start of line
1326 backspace_until_column(0);
1327
1328 // Insert new stuff into line again
1329 ins_bytes(new_line);
1330
1331 vim_free(new_line);
1332 }
1333}
1334
1335/*
1336 * Copy the indent from ptr to the current line (and fill to size)
1337 * Leaves the cursor on the first non-blank in the line.
1338 * Returns TRUE if the line was changed.
1339 */
1340 int
1341copy_indent(int size, char_u *src)
1342{
1343 char_u *p = NULL;
1344 char_u *line = NULL;
1345 char_u *s;
1346 int todo;
1347 int ind_len;
1348 int line_len = 0;
1349 int tab_pad;
1350 int ind_done;
1351 int round;
1352#ifdef FEAT_VARTABS
1353 int ind_col;
1354#endif
1355
1356 // Round 1: compute the number of characters needed for the indent
1357 // Round 2: copy the characters.
1358 for (round = 1; round <= 2; ++round)
1359 {
1360 todo = size;
1361 ind_len = 0;
1362 ind_done = 0;
1363#ifdef FEAT_VARTABS
1364 ind_col = 0;
1365#endif
1366 s = src;
1367
1368 // Count/copy the usable portion of the source line
1369 while (todo > 0 && VIM_ISWHITE(*s))
1370 {
1371 if (*s == TAB)
1372 {
1373#ifdef FEAT_VARTABS
1374 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1375 curbuf->b_p_vts_array);
1376#else
1377 tab_pad = (int)curbuf->b_p_ts
1378 - (ind_done % (int)curbuf->b_p_ts);
1379#endif
1380 // Stop if this tab will overshoot the target
1381 if (todo < tab_pad)
1382 break;
1383 todo -= tab_pad;
1384 ind_done += tab_pad;
1385#ifdef FEAT_VARTABS
1386 ind_col += tab_pad;
1387#endif
1388 }
1389 else
1390 {
1391 --todo;
1392 ++ind_done;
1393#ifdef FEAT_VARTABS
1394 ++ind_col;
1395#endif
1396 }
1397 ++ind_len;
1398 if (p != NULL)
1399 *p++ = *s;
1400 ++s;
1401 }
1402
1403 // Fill to next tabstop with a tab, if possible
1404#ifdef FEAT_VARTABS
1405 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1406 curbuf->b_p_vts_array);
1407#else
1408 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1409#endif
1410 if (todo >= tab_pad && !curbuf->b_p_et)
1411 {
1412 todo -= tab_pad;
1413 ++ind_len;
1414#ifdef FEAT_VARTABS
1415 ind_col += tab_pad;
1416#endif
1417 if (p != NULL)
1418 *p++ = TAB;
1419 }
1420
1421 // Add tabs required for indent
1422 if (!curbuf->b_p_et)
1423 {
1424#ifdef FEAT_VARTABS
1425 for (;;)
1426 {
1427 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1428 curbuf->b_p_vts_array);
1429 if (todo < tab_pad)
1430 break;
1431 todo -= tab_pad;
1432 ++ind_len;
1433 ind_col += tab_pad;
1434 if (p != NULL)
1435 *p++ = TAB;
1436 }
1437#else
1438 while (todo >= (int)curbuf->b_p_ts)
1439 {
1440 todo -= (int)curbuf->b_p_ts;
1441 ++ind_len;
1442 if (p != NULL)
1443 *p++ = TAB;
1444 }
1445#endif
1446 }
1447
1448 // Count/add spaces required for indent
1449 while (todo > 0)
1450 {
1451 --todo;
1452 ++ind_len;
1453 if (p != NULL)
1454 *p++ = ' ';
1455 }
1456
1457 if (p == NULL)
1458 {
1459 // Allocate memory for the result: the copied indent, new indent
1460 // and the rest of the line.
1461 line_len = (int)STRLEN(ml_get_curline()) + 1;
1462 line = alloc(ind_len + line_len);
1463 if (line == NULL)
1464 return FALSE;
1465 p = line;
1466 }
1467 }
1468
1469 // Append the original line
1470 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1471
1472 // Replace the line
1473 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1474
1475 // Put the cursor after the indent.
1476 curwin->w_cursor.col = ind_len;
1477 return TRUE;
1478}
1479
1480/*
1481 * ":retab".
1482 */
1483 void
1484ex_retab(exarg_T *eap)
1485{
1486 linenr_T lnum;
1487 int got_tab = FALSE;
1488 long num_spaces = 0;
1489 long num_tabs;
1490 long len;
1491 long col;
1492 long vcol;
1493 long start_col = 0; /* For start of white-space string */
1494 long start_vcol = 0; /* For start of white-space string */
1495 long old_len;
1496 char_u *ptr;
1497 char_u *new_line = (char_u *)1; /* init to non-NULL */
1498 int did_undo; /* called u_save for current line */
1499#ifdef FEAT_VARTABS
1500 int *new_vts_array = NULL;
1501 char_u *new_ts_str; /* string value of tab argument */
1502#else
1503 int temp;
1504 int new_ts;
1505#endif
1506 int save_list;
1507 linenr_T first_line = 0; /* first changed line */
1508 linenr_T last_line = 0; /* last changed line */
1509
1510 save_list = curwin->w_p_list;
1511 curwin->w_p_list = 0; /* don't want list mode here */
1512
1513#ifdef FEAT_VARTABS
1514 new_ts_str = eap->arg;
1515 if (!tabstop_set(eap->arg, &new_vts_array))
1516 return;
1517 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',')
1518 ++(eap->arg);
1519
1520 // This ensures that either new_vts_array and new_ts_str are freshly
1521 // allocated, or new_vts_array points to an existing array and new_ts_str
1522 // is null.
1523 if (new_vts_array == NULL)
1524 {
1525 new_vts_array = curbuf->b_p_vts_array;
1526 new_ts_str = NULL;
1527 }
1528 else
1529 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str);
1530#else
1531 new_ts = getdigits(&(eap->arg));
1532 if (new_ts < 0)
1533 {
1534 emsg(_(e_positive));
1535 return;
1536 }
1537 if (new_ts == 0)
1538 new_ts = curbuf->b_p_ts;
1539#endif
1540 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
1541 {
1542 ptr = ml_get(lnum);
1543 col = 0;
1544 vcol = 0;
1545 did_undo = FALSE;
1546 for (;;)
1547 {
1548 if (VIM_ISWHITE(ptr[col]))
1549 {
1550 if (!got_tab && num_spaces == 0)
1551 {
1552 /* First consecutive white-space */
1553 start_vcol = vcol;
1554 start_col = col;
1555 }
1556 if (ptr[col] == ' ')
1557 num_spaces++;
1558 else
1559 got_tab = TRUE;
1560 }
1561 else
1562 {
1563 if (got_tab || (eap->forceit && num_spaces > 1))
1564 {
1565 /* Retabulate this string of white-space */
1566
1567 /* len is virtual length of white string */
1568 len = num_spaces = vcol - start_vcol;
1569 num_tabs = 0;
1570 if (!curbuf->b_p_et)
1571 {
1572#ifdef FEAT_VARTABS
1573 int t, s;
1574
1575 tabstop_fromto(start_vcol, vcol,
1576 curbuf->b_p_ts, new_vts_array, &t, &s);
1577 num_tabs = t;
1578 num_spaces = s;
1579#else
1580 temp = new_ts - (start_vcol % new_ts);
1581 if (num_spaces >= temp)
1582 {
1583 num_spaces -= temp;
1584 num_tabs++;
1585 }
1586 num_tabs += num_spaces / new_ts;
1587 num_spaces -= (num_spaces / new_ts) * new_ts;
1588#endif
1589 }
1590 if (curbuf->b_p_et || got_tab ||
1591 (num_spaces + num_tabs < len))
1592 {
1593 if (did_undo == FALSE)
1594 {
1595 did_undo = TRUE;
1596 if (u_save((linenr_T)(lnum - 1),
1597 (linenr_T)(lnum + 1)) == FAIL)
1598 {
1599 new_line = NULL; /* flag out-of-memory */
1600 break;
1601 }
1602 }
1603
1604 /* len is actual number of white characters used */
1605 len = num_spaces + num_tabs;
1606 old_len = (long)STRLEN(ptr);
1607 new_line = alloc(old_len - col + start_col + len + 1);
1608 if (new_line == NULL)
1609 break;
1610 if (start_col > 0)
1611 mch_memmove(new_line, ptr, (size_t)start_col);
1612 mch_memmove(new_line + start_col + len,
1613 ptr + col, (size_t)(old_len - col + 1));
1614 ptr = new_line + start_col;
1615 for (col = 0; col < len; col++)
1616 ptr[col] = (col < num_tabs) ? '\t' : ' ';
1617 ml_replace(lnum, new_line, FALSE);
1618 if (first_line == 0)
1619 first_line = lnum;
1620 last_line = lnum;
1621 ptr = new_line;
1622 col = start_col + len;
1623 }
1624 }
1625 got_tab = FALSE;
1626 num_spaces = 0;
1627 }
1628 if (ptr[col] == NUL)
1629 break;
1630 vcol += chartabsize(ptr + col, (colnr_T)vcol);
1631 if (has_mbyte)
1632 col += (*mb_ptr2len)(ptr + col);
1633 else
1634 ++col;
1635 }
1636 if (new_line == NULL) /* out of memory */
1637 break;
1638 line_breakcheck();
1639 }
1640 if (got_int)
1641 emsg(_(e_interr));
1642
1643#ifdef FEAT_VARTABS
1644 // If a single value was given then it can be considered equal to
1645 // either the value of 'tabstop' or the value of 'vartabstop'.
1646 if (tabstop_count(curbuf->b_p_vts_array) == 0
1647 && tabstop_count(new_vts_array) == 1
1648 && curbuf->b_p_ts == tabstop_first(new_vts_array))
1649 ; /* not changed */
1650 else if (tabstop_count(curbuf->b_p_vts_array) > 0
1651 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array))
1652 ; /* not changed */
1653 else
1654 redraw_curbuf_later(NOT_VALID);
1655#else
1656 if (curbuf->b_p_ts != new_ts)
1657 redraw_curbuf_later(NOT_VALID);
1658#endif
1659 if (first_line != 0)
1660 changed_lines(first_line, 0, last_line + 1, 0L);
1661
1662 curwin->w_p_list = save_list; /* restore 'list' */
1663
1664#ifdef FEAT_VARTABS
1665 if (new_ts_str != NULL) /* set the new tabstop */
1666 {
1667 // If 'vartabstop' is in use or if the value given to retab has more
1668 // than one tabstop then update 'vartabstop'.
1669 int *old_vts_ary = curbuf->b_p_vts_array;
1670
1671 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1)
1672 {
1673 set_string_option_direct((char_u *)"vts", -1, new_ts_str,
1674 OPT_FREE|OPT_LOCAL, 0);
1675 curbuf->b_p_vts_array = new_vts_array;
1676 vim_free(old_vts_ary);
1677 }
1678 else
1679 {
1680 // 'vartabstop' wasn't in use and a single value was given to
1681 // retab then update 'tabstop'.
1682 curbuf->b_p_ts = tabstop_first(new_vts_array);
1683 vim_free(new_vts_array);
1684 }
1685 vim_free(new_ts_str);
1686 }
1687#else
1688 curbuf->b_p_ts = new_ts;
1689#endif
1690 coladvance(curwin->w_curswant);
1691
1692 u_clearline();
1693}
1694
1695#if (defined(FEAT_CINDENT) && defined(FEAT_EVAL)) || defined(PROTO)
1696/*
1697 * Get indent level from 'indentexpr'.
1698 */
1699 int
1700get_expr_indent(void)
1701{
1702 int indent = -1;
1703 char_u *inde_copy;
1704 pos_T save_pos;
1705 colnr_T save_curswant;
1706 int save_set_curswant;
1707 int save_State;
1708 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
1709 OPT_LOCAL);
1710
1711 // Save and restore cursor position and curswant, in case it was changed
1712 // via :normal commands
1713 save_pos = curwin->w_cursor;
1714 save_curswant = curwin->w_curswant;
1715 save_set_curswant = curwin->w_set_curswant;
1716 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
1717 if (use_sandbox)
1718 ++sandbox;
1719 ++textlock;
1720
1721 // Need to make a copy, the 'indentexpr' option could be changed while
1722 // evaluating it.
1723 inde_copy = vim_strsave(curbuf->b_p_inde);
1724 if (inde_copy != NULL)
1725 {
1726 indent = (int)eval_to_number(inde_copy);
1727 vim_free(inde_copy);
1728 }
1729
1730 if (use_sandbox)
1731 --sandbox;
1732 --textlock;
1733
1734 // Restore the cursor position so that 'indentexpr' doesn't need to.
1735 // Pretend to be in Insert mode, allow cursor past end of line for "o"
1736 // command.
1737 save_State = State;
1738 State = INSERT;
1739 curwin->w_cursor = save_pos;
1740 curwin->w_curswant = save_curswant;
1741 curwin->w_set_curswant = save_set_curswant;
1742 check_cursor();
1743 State = save_State;
1744
1745 // If there is an error, just keep the current indent.
1746 if (indent < 0)
1747 indent = get_indent();
1748
1749 return indent;
1750}
1751#endif
1752
1753#if defined(FEAT_LISP) || defined(PROTO)
1754
1755 static int
1756lisp_match(char_u *p)
1757{
1758 char_u buf[LSIZE];
1759 int len;
1760 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
1761
1762 while (*word != NUL)
1763 {
1764 (void)copy_option_part(&word, buf, LSIZE, ",");
1765 len = (int)STRLEN(buf);
1766 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
1767 return TRUE;
1768 }
1769 return FALSE;
1770}
1771
1772/*
1773 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
1774 * The incompatible newer method is quite a bit better at indenting
1775 * code in lisp-like languages than the traditional one; it's still
1776 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
1777 *
1778 * TODO:
1779 * Findmatch() should be adapted for lisp, also to make showmatch
1780 * work correctly: now (v5.3) it seems all C/C++ oriented:
1781 * - it does not recognize the #\( and #\) notations as character literals
1782 * - it doesn't know about comments starting with a semicolon
1783 * - it incorrectly interprets '(' as a character literal
1784 * All this messes up get_lisp_indent in some rare cases.
1785 * Update from Sergey Khorev:
1786 * I tried to fix the first two issues.
1787 */
1788 int
1789get_lisp_indent(void)
1790{
1791 pos_T *pos, realpos, paren;
1792 int amount;
1793 char_u *that;
1794 colnr_T col;
1795 colnr_T firsttry;
1796 int parencount, quotecount;
1797 int vi_lisp;
1798
1799 // Set vi_lisp to use the vi-compatible method
1800 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
1801
1802 realpos = curwin->w_cursor;
1803 curwin->w_cursor.col = 0;
1804
1805 if ((pos = findmatch(NULL, '(')) == NULL)
1806 pos = findmatch(NULL, '[');
1807 else
1808 {
1809 paren = *pos;
1810 pos = findmatch(NULL, '[');
1811 if (pos == NULL || LT_POSP(pos, &paren))
1812 pos = &paren;
1813 }
1814 if (pos != NULL)
1815 {
1816 // Extra trick: Take the indent of the first previous non-white
1817 // line that is at the same () level.
1818 amount = -1;
1819 parencount = 0;
1820
1821 while (--curwin->w_cursor.lnum >= pos->lnum)
1822 {
1823 if (linewhite(curwin->w_cursor.lnum))
1824 continue;
1825 for (that = ml_get_curline(); *that != NUL; ++that)
1826 {
1827 if (*that == ';')
1828 {
1829 while (*(that + 1) != NUL)
1830 ++that;
1831 continue;
1832 }
1833 if (*that == '\\')
1834 {
1835 if (*(that + 1) != NUL)
1836 ++that;
1837 continue;
1838 }
1839 if (*that == '"' && *(that + 1) != NUL)
1840 {
1841 while (*++that && *that != '"')
1842 {
1843 // skipping escaped characters in the string
1844 if (*that == '\\')
1845 {
1846 if (*++that == NUL)
1847 break;
1848 if (that[1] == NUL)
1849 {
1850 ++that;
1851 break;
1852 }
1853 }
1854 }
1855 }
1856 if (*that == '(' || *that == '[')
1857 ++parencount;
1858 else if (*that == ')' || *that == ']')
1859 --parencount;
1860 }
1861 if (parencount == 0)
1862 {
1863 amount = get_indent();
1864 break;
1865 }
1866 }
1867
1868 if (amount == -1)
1869 {
1870 curwin->w_cursor.lnum = pos->lnum;
1871 curwin->w_cursor.col = pos->col;
1872 col = pos->col;
1873
1874 that = ml_get_curline();
1875
1876 if (vi_lisp && get_indent() == 0)
1877 amount = 2;
1878 else
1879 {
1880 char_u *line = that;
1881
1882 amount = 0;
1883 while (*that && col)
1884 {
1885 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
1886 col--;
1887 }
1888
1889 // Some keywords require "body" indenting rules (the
1890 // non-standard-lisp ones are Scheme special forms):
1891 //
1892 // (let ((a 1)) instead (let ((a 1))
1893 // (...)) of (...))
1894
1895 if (!vi_lisp && (*that == '(' || *that == '[')
1896 && lisp_match(that + 1))
1897 amount += 2;
1898 else
1899 {
1900 that++;
1901 amount++;
1902 firsttry = amount;
1903
1904 while (VIM_ISWHITE(*that))
1905 {
1906 amount += lbr_chartabsize(line, that, (colnr_T)amount);
1907 ++that;
1908 }
1909
1910 if (*that && *that != ';') // not a comment line
1911 {
1912 // test *that != '(' to accommodate first let/do
1913 // argument if it is more than one line
1914 if (!vi_lisp && *that != '(' && *that != '[')
1915 firsttry++;
1916
1917 parencount = 0;
1918 quotecount = 0;
1919
1920 if (vi_lisp
1921 || (*that != '"'
1922 && *that != '\''
1923 && *that != '#'
1924 && (*that < '0' || *that > '9')))
1925 {
1926 while (*that
1927 && (!VIM_ISWHITE(*that)
1928 || quotecount
1929 || parencount)
1930 && (!((*that == '(' || *that == '[')
1931 && !quotecount
1932 && !parencount
1933 && vi_lisp)))
1934 {
1935 if (*that == '"')
1936 quotecount = !quotecount;
1937 if ((*that == '(' || *that == '[')
1938 && !quotecount)
1939 ++parencount;
1940 if ((*that == ')' || *that == ']')
1941 && !quotecount)
1942 --parencount;
1943 if (*that == '\\' && *(that+1) != NUL)
1944 amount += lbr_chartabsize_adv(
1945 line, &that, (colnr_T)amount);
1946 amount += lbr_chartabsize_adv(
1947 line, &that, (colnr_T)amount);
1948 }
1949 }
1950 while (VIM_ISWHITE(*that))
1951 {
1952 amount += lbr_chartabsize(
1953 line, that, (colnr_T)amount);
1954 that++;
1955 }
1956 if (!*that || *that == ';')
1957 amount = firsttry;
1958 }
1959 }
1960 }
1961 }
1962 }
1963 else
1964 amount = 0; // no matching '(' or '[' found, use zero indent
1965
1966 curwin->w_cursor = realpos;
1967
1968 return amount;
1969}
1970#endif // FEAT_LISP
1971
1972#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
1973/*
1974 * Re-indent the current line, based on the current contents of it and the
1975 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
1976 * confused what all the part that handles Control-T is doing that I'm not.
1977 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
1978 */
1979
1980 void
1981fixthisline(int (*get_the_indent)(void))
1982{
1983 int amount = get_the_indent();
1984
1985 if (amount >= 0)
1986 {
1987 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
1988 if (linewhite(curwin->w_cursor.lnum))
1989 did_ai = TRUE; // delete the indent if the line stays empty
1990 }
1991}
1992
1993 void
1994fix_indent(void)
1995{
1996 if (p_paste)
1997 return;
1998# ifdef FEAT_LISP
1999 if (curbuf->b_p_lisp && curbuf->b_p_ai)
2000 fixthisline(get_lisp_indent);
2001# endif
2002# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
2003 else
2004# endif
2005# ifdef FEAT_CINDENT
2006 if (cindent_on())
2007 do_c_expr_indent();
2008# endif
2009}
2010#endif
2011
2012#if defined(FEAT_EVAL) || defined(PROTO)
2013/*
2014 * "indent()" function
2015 */
2016 void
2017f_indent(typval_T *argvars, typval_T *rettv)
2018{
2019 linenr_T lnum;
2020
2021 lnum = tv_get_lnum(argvars);
2022 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2023 rettv->vval.v_number = get_indent_lnum(lnum);
2024 else
2025 rettv->vval.v_number = -1;
2026}
2027
2028/*
2029 * "lispindent(lnum)" function
2030 */
2031 void
2032f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
2033{
2034#ifdef FEAT_LISP
2035 pos_T pos;
2036 linenr_T lnum;
2037
2038 pos = curwin->w_cursor;
2039 lnum = tv_get_lnum(argvars);
2040 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2041 {
2042 curwin->w_cursor.lnum = lnum;
2043 rettv->vval.v_number = get_lisp_indent();
2044 curwin->w_cursor = pos;
2045 }
2046 else
2047#endif
2048 rettv->vval.v_number = -1;
2049}
2050#endif