annotate src/indent.c @ 25701:357a3bee56f5 v8.2.3386

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