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