blob: 1db7496c0762ed0d7788504b78df433abf7e9d0f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaar828c3d72018-06-19 18:58:07 +020017#if defined(FEAT_CMDL_COMPL) && defined(WIN3264)
18# include <lm.h>
19#endif
20
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static char_u *vim_version_dir(char_u *vimdir);
22static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar24305862012-08-15 14:05:05 +020024/* All user names (for ~user completion as done by shell). */
25#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
26static garray_T ga_users;
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029/*
30 * Count the size (in window cells) of the indent in the current line.
31 */
32 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010033get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000034{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020035#ifdef FEAT_VARTABS
36 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
37 curbuf->b_p_vts_array, FALSE);
38#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020039 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041}
42
43/*
44 * Count the size (in window cells) of the indent in line "lnum".
45 */
46 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010047get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000048{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020049#ifdef FEAT_VARTABS
50 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
51 curbuf->b_p_vts_array, FALSE);
52#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020053 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000055}
56
57#if defined(FEAT_FOLDING) || defined(PROTO)
58/*
59 * Count the size (in window cells) of the indent in line "lnum" of buffer
60 * "buf".
61 */
62 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010063get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000064{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020065#ifdef FEAT_VARTABS
66 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
67 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
68#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020069 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000071}
72#endif
73
74/*
75 * count the size (in window cells) of the indent in line "ptr", with
76 * 'tabstop' at "ts"
77 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000078 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010079get_indent_str(
80 char_u *ptr,
81 int ts,
82 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083{
84 int count = 0;
85
86 for ( ; *ptr; ++ptr)
87 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020088 if (*ptr == TAB)
89 {
90 if (!list || lcs_tab1) /* count a tab for what it is worth */
91 count += ts - (count % ts);
92 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020093 /* In list mode, when tab is not set, count screen char width
94 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020095 count += ptr2cells(ptr);
96 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 else if (*ptr == ' ')
98 ++count; /* count a space for one */
99 else
100 break;
101 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000102 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103}
104
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200105#ifdef FEAT_VARTABS
106/*
107 * Count the size (in window cells) of the indent in line "ptr", using
108 * variable tabstops.
109 * if "list" is TRUE, count only screen size for tabs.
110 */
111 int
112get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
113{
114 int count = 0;
115
116 for ( ; *ptr; ++ptr)
117 {
118 if (*ptr == TAB) /* count a tab for what it is worth */
119 {
120 if (!list || lcs_tab1)
121 count += tabstop_padding(count, ts, vts);
122 else
123 /* In list mode, when tab is not set, count screen char width
124 * for Tab, displays: ^I */
125 count += ptr2cells(ptr);
126 }
127 else if (*ptr == ' ')
128 ++count; /* count a space for one */
129 else
130 break;
131 }
132 return count;
133}
134#endif
135
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136/*
137 * Set the indent of the current line.
138 * Leaves the cursor on the first non-blank in the line.
139 * Caller must take care of undo.
140 * "flags":
141 * SIN_CHANGED: call changed_bytes() if the line was changed.
142 * SIN_INSERT: insert the indent in front of the line.
143 * SIN_UNDO: save line for undo before changing it.
144 * Returns TRUE if the line was changed.
145 */
146 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100147set_indent(
148 int size, /* measured in spaces */
149 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150{
151 char_u *p;
152 char_u *newline;
153 char_u *oldline;
154 char_u *s;
155 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000156 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 int line_len;
158 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000159 int ind_done = 0; /* measured in spaces */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200160#ifdef FEAT_VARTABS
161 int ind_col = 0;
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000164 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000165 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000166 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
168 /*
169 * First check if there is anything to do and compute the number of
170 * characters needed for the indent.
171 */
172 todo = size;
173 ind_len = 0;
174 p = oldline = ml_get_curline();
175
176 /* Calculate the buffer size for the new indent, and check to see if it
177 * isn't already set */
178
Bram Moolenaar5002c292007-07-24 13:26:15 +0000179 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
180 * 'preserveindent' are set count the number of characters at the
181 * beginning of the line to be copied */
182 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 {
184 /* If 'preserveindent' is set then reuse as much as possible of
185 * the existing indent structure for the new indent */
186 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
187 {
188 ind_done = 0;
189
190 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100191 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {
193 if (*p == TAB)
194 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200195#ifdef FEAT_VARTABS
196 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
197 curbuf->b_p_vts_array);
198#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 tab_pad = (int)curbuf->b_p_ts
200 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 /* stop if this tab will overshoot the target */
203 if (todo < tab_pad)
204 break;
205 todo -= tab_pad;
206 ++ind_len;
207 ind_done += tab_pad;
208 }
209 else
210 {
211 --todo;
212 ++ind_len;
213 ++ind_done;
214 }
215 ++p;
216 }
217
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200218#ifdef FEAT_VARTABS
219 /* These diverge from this point. */
220 ind_col = ind_done;
221#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +0000222 /* Set initial number of whitespace chars to copy if we are
223 * preserving indent but expandtab is set */
224 if (curbuf->b_p_et)
225 orig_char_len = ind_len;
226
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200228#ifdef FEAT_VARTABS
229 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
230 curbuf->b_p_vts_array);
231#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200233#endif
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000234 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {
236 doit = TRUE;
237 todo -= tab_pad;
238 ++ind_len;
239 /* ind_done += tab_pad; */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200240#ifdef FEAT_VARTABS
241 ind_col += tab_pad;
242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 }
244 }
245
246 /* count tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200247#ifdef FEAT_VARTABS
248 for (;;)
249 {
250 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
251 curbuf->b_p_vts_array);
252 if (todo < tab_pad)
253 break;
254 if (*p != TAB)
255 doit = TRUE;
256 else
257 ++p;
258 todo -= tab_pad;
259 ++ind_len;
260 ind_col += tab_pad;
261 }
262#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 while (todo >= (int)curbuf->b_p_ts)
264 {
265 if (*p != TAB)
266 doit = TRUE;
267 else
268 ++p;
269 todo -= (int)curbuf->b_p_ts;
270 ++ind_len;
271 /* ind_done += (int)curbuf->b_p_ts; */
272 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 }
275 /* count spaces required for indent */
276 while (todo > 0)
277 {
278 if (*p != ' ')
279 doit = TRUE;
280 else
281 ++p;
282 --todo;
283 ++ind_len;
284 /* ++ind_done; */
285 }
286
287 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100288 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 return FALSE;
290
291 /* Allocate memory for the new line. */
292 if (flags & SIN_INSERT)
293 p = oldline;
294 else
295 p = skipwhite(p);
296 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000297
298 /* If 'preserveindent' and 'expandtab' are both set keep the original
299 * characters and allocate accordingly. We will fill the rest with spaces
300 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000301 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000302 {
303 newline = alloc(orig_char_len + size - ind_done + line_len);
304 if (newline == NULL)
305 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000306 todo = size - ind_done;
307 ind_len = orig_char_len + todo; /* Set total length of indent in
308 * characters, which may have been
309 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000310 p = oldline;
311 s = newline;
312 while (orig_char_len > 0)
313 {
314 *s++ = *p++;
315 orig_char_len--;
316 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000317
Bram Moolenaar5002c292007-07-24 13:26:15 +0000318 /* Skip over any additional white space (useful when newindent is less
319 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100320 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000321 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000322
Bram Moolenaar5002c292007-07-24 13:26:15 +0000323 }
324 else
325 {
326 todo = size;
327 newline = alloc(ind_len + line_len);
328 if (newline == NULL)
329 return FALSE;
330 s = newline;
331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332
333 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 /* if 'expandtab' isn't set: use TABs */
335 if (!curbuf->b_p_et)
336 {
337 /* If 'preserveindent' is set then reuse as much as possible of
338 * the existing indent structure for the new indent */
339 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
340 {
341 p = oldline;
342 ind_done = 0;
343
Bram Moolenaar1c465442017-03-12 20:10:05 +0100344 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {
346 if (*p == TAB)
347 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200348#ifdef FEAT_VARTABS
349 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
350 curbuf->b_p_vts_array);
351#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 tab_pad = (int)curbuf->b_p_ts
353 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 /* stop if this tab will overshoot the target */
356 if (todo < tab_pad)
357 break;
358 todo -= tab_pad;
359 ind_done += tab_pad;
360 }
361 else
362 {
363 --todo;
364 ++ind_done;
365 }
366 *s++ = *p++;
367 }
368
369 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200370#ifdef FEAT_VARTABS
371 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
372 curbuf->b_p_vts_array);
373#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (todo >= tab_pad)
377 {
378 *s++ = TAB;
379 todo -= tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200380#ifdef FEAT_VARTABS
381 ind_done += tab_pad;
382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 }
384
385 p = skipwhite(p);
386 }
387
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200388#ifdef FEAT_VARTABS
389 for (;;)
390 {
391 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
392 curbuf->b_p_vts_array);
393 if (todo < tab_pad)
394 break;
395 *s++ = TAB;
396 todo -= tab_pad;
397 ind_done += tab_pad;
398 }
399#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 while (todo >= (int)curbuf->b_p_ts)
401 {
402 *s++ = TAB;
403 todo -= (int)curbuf->b_p_ts;
404 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 }
407 while (todo > 0)
408 {
409 *s++ = ' ';
410 --todo;
411 }
412 mch_memmove(s, p, (size_t)line_len);
413
Bram Moolenaar663bc892019-01-08 23:07:24 +0100414 // Replace the line (unless undo fails).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
416 {
417 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
418 if (flags & SIN_CHANGED)
419 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar663bc892019-01-08 23:07:24 +0100420
421 // Correct saved cursor position if it is in this line.
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200422 if (saved_cursor.lnum == curwin->w_cursor.lnum)
423 {
424 if (saved_cursor.col >= (colnr_T)(p - oldline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100425 // cursor was after the indent, adjust for the number of
426 // bytes added/removed
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200427 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
428 else if (saved_cursor.col >= (colnr_T)(s - newline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100429 // cursor was in the indent, and is now after it, put it back
430 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200431 saved_cursor.col = (colnr_T)(s - newline);
432 }
Bram Moolenaar663bc892019-01-08 23:07:24 +0100433#ifdef FEAT_TEXT_PROP
434 adjust_prop_columns(curwin->w_cursor.lnum, (colnr_T)(p - oldline),
435 ind_len - (colnr_T)(p - oldline));
436#endif
Bram Moolenaar5409c052005-03-18 20:27:04 +0000437 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 }
439 else
440 vim_free(newline);
441
442 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000443 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444}
445
446/*
447 * Copy the indent from ptr to the current line (and fill to size)
448 * Leaves the cursor on the first non-blank in the line.
449 * Returns TRUE if the line was changed.
450 */
451 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100452copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453{
454 char_u *p = NULL;
455 char_u *line = NULL;
456 char_u *s;
457 int todo;
458 int ind_len;
459 int line_len = 0;
460 int tab_pad;
461 int ind_done;
462 int round;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200463#ifdef FEAT_VARTABS
464 int ind_col;
465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466
467 /* Round 1: compute the number of characters needed for the indent
468 * Round 2: copy the characters. */
469 for (round = 1; round <= 2; ++round)
470 {
471 todo = size;
472 ind_len = 0;
473 ind_done = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200474#ifdef FEAT_VARTABS
475 ind_col = 0;
476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 s = src;
478
479 /* Count/copy the usable portion of the source line */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100480 while (todo > 0 && VIM_ISWHITE(*s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 {
482 if (*s == TAB)
483 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200484#ifdef FEAT_VARTABS
485 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
486 curbuf->b_p_vts_array);
487#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 tab_pad = (int)curbuf->b_p_ts
489 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 /* Stop if this tab will overshoot the target */
492 if (todo < tab_pad)
493 break;
494 todo -= tab_pad;
495 ind_done += tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200496#ifdef FEAT_VARTABS
497 ind_col += tab_pad;
498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 }
500 else
501 {
502 --todo;
503 ++ind_done;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200504#ifdef FEAT_VARTABS
505 ++ind_col;
506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 }
508 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000509 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 *p++ = *s;
511 ++s;
512 }
513
514 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200515#ifdef FEAT_VARTABS
516 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
517 curbuf->b_p_vts_array);
518#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200520#endif
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200521 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
523 todo -= tab_pad;
524 ++ind_len;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200525#ifdef FEAT_VARTABS
526 ind_col += tab_pad;
527#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000528 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 *p++ = TAB;
530 }
531
532 /* Add tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200533 if (!curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200535#ifdef FEAT_VARTABS
536 for (;;)
537 {
538 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
539 curbuf->b_p_vts_array);
540 if (todo < tab_pad)
541 break;
542 todo -= tab_pad;
543 ++ind_len;
544 ind_col += tab_pad;
545 if (p != NULL)
546 *p++ = TAB;
547 }
548#else
549 while (todo >= (int)curbuf->b_p_ts)
550 {
551 todo -= (int)curbuf->b_p_ts;
552 ++ind_len;
553 if (p != NULL)
554 *p++ = TAB;
555 }
556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 }
558
559 /* Count/add spaces required for indent */
560 while (todo > 0)
561 {
562 --todo;
563 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000564 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 *p++ = ' ';
566 }
567
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000568 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {
570 /* Allocate memory for the result: the copied indent, new indent
571 * and the rest of the line. */
572 line_len = (int)STRLEN(ml_get_curline()) + 1;
573 line = alloc(ind_len + line_len);
574 if (line == NULL)
575 return FALSE;
576 p = line;
577 }
578 }
579
580 /* Append the original line */
581 mch_memmove(p, ml_get_curline(), (size_t)line_len);
582
583 /* Replace the line */
584 ml_replace(curwin->w_cursor.lnum, line, FALSE);
585
586 /* Put the cursor after the indent. */
587 curwin->w_cursor.col = ind_len;
588 return TRUE;
589}
590
591/*
592 * Return the indent of the current line after a number. Return -1 if no
593 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000594 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 */
596 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100597get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 colnr_T col;
600 pos_T pos;
601
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200602 regmatch_T regmatch;
603 int lead_len = 0; /* length of comment leader */
604
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 if (lnum > curbuf->b_ml.ml_line_count)
606 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000607 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200608
609#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200610 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
611 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200612 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000613#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200614 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
615 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200616 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200617 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200618
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200619 /* vim_regexec() expects a pointer to a line. This lets us
620 * start matching for the flp beyond any comment leader... */
621 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200622 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200623 pos.lnum = lnum;
624 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200625 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200626 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200627 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200628 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000629
630 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 getvcol(curwin, &pos, &col, NULL, NULL);
633 return (int)col;
634}
635
Bram Moolenaar597a4222014-06-25 14:39:50 +0200636#if defined(FEAT_LINEBREAK) || defined(PROTO)
637/*
638 * Return appropriate space number for breakindent, taking influencing
639 * parameters into account. Window must be specified, since it is not
640 * necessarily always the current one.
641 */
642 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100643get_breakindent_win(
644 win_T *wp,
645 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200646{
647 static int prev_indent = 0; /* cached indent value */
648 static long prev_ts = 0L; /* cached tabstop value */
649 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100650 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200651#ifdef FEAT_VARTABS
652 static int *prev_vts = NULL; /* cached vartabs values */
653#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200654 int bri = 0;
655 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200656 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200657 - ((wp->w_p_nu || wp->w_p_rnu)
658 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
659 ? number_width(wp) + 1 : 0);
660
661 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200662 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200663 || prev_tick != CHANGEDTICK(wp->w_buffer)
664#ifdef FEAT_VARTABS
665 || prev_vts != wp->w_buffer->b_p_vts_array
666#endif
667 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200668 {
669 prev_line = line;
670 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100671 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200672#ifdef FEAT_VARTABS
673 prev_vts = wp->w_buffer->b_p_vts_array;
674 prev_indent = get_indent_str_vtab(line,
675 (int)wp->w_buffer->b_p_ts,
676 wp->w_buffer->b_p_vts_array, wp->w_p_list);
677#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200678 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200679 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200680#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200681 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200682 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200683
684 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200685 if (wp->w_p_brisbr)
686 bri -= vim_strsize(p_sbr);
687
688 /* Add offset for number column, if 'n' is in 'cpoptions' */
689 bri += win_col_off2(wp);
690
691 /* never indent past left window margin */
692 if (bri < 0)
693 bri = 0;
694 /* always leave at least bri_min characters on the left,
695 * if text width is sufficient */
696 else if (bri > eff_wwidth - wp->w_p_brimin)
697 bri = (eff_wwidth - wp->w_p_brimin < 0)
698 ? 0 : eff_wwidth - wp->w_p_brimin;
699
700 return bri;
701}
702#endif
703
704
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707/*
708 * Return TRUE if the string "line" starts with a word from 'cinwords'.
709 */
710 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100711cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712{
713 char_u *cinw;
714 char_u *cinw_buf;
715 int cinw_len;
716 int retval = FALSE;
717 int len;
718
719 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
720 cinw_buf = alloc((unsigned)cinw_len);
721 if (cinw_buf != NULL)
722 {
723 line = skipwhite(line);
724 for (cinw = curbuf->b_p_cinw; *cinw; )
725 {
726 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
727 if (STRNCMP(line, cinw_buf, len) == 0
728 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
729 {
730 retval = TRUE;
731 break;
732 }
733 }
734 vim_free(cinw_buf);
735 }
736 return retval;
737}
738#endif
739
740/*
741 * open_line: Add a new line below or above the current line.
742 *
743 * For VREPLACE mode, we only add a new line when we get to the end of the
744 * file, otherwise we just start replacing the next line.
745 *
746 * Caller must take care of undo. Since VREPLACE may affect any number of
747 * lines however, it may call u_save_cursor() again when starting to change a
748 * new line.
749 * "flags": OPENLINE_DELSPACES delete spaces after cursor
750 * OPENLINE_DO_COM format comments
751 * OPENLINE_KEEPTRAIL keep trailing spaces
752 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200753 * OPENLINE_COM_LIST format comments with list or 2nd line indent
754 *
755 * "second_line_indent": indent for after ^^D in Insert mode or if flag
756 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 *
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200758 * Return OK for success, FAIL for failure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 */
760 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100761open_line(
762 int dir, /* FORWARD or BACKWARD */
763 int flags,
764 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
766 char_u *saved_line; /* copy of the original line */
767 char_u *next_line = NULL; /* copy of the next line */
768 char_u *p_extra = NULL; /* what goes to next line */
769 int less_cols = 0; /* less columns for mark in new line */
770 int less_cols_off = 0; /* columns to skip for mark adjust */
771 pos_T old_cursor; /* old cursor position */
772 int newcol = 0; /* new cursor column */
773 int newindent = 0; /* auto-indent of the new line */
774 int n;
775 int trunc_line = FALSE; /* truncate current line afterwards */
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200776 int retval = FAIL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#ifdef FEAT_COMMENTS
778 int extra_len = 0; /* length of p_extra string */
779 int lead_len; /* length of comment leader */
780 char_u *lead_flags; /* position in 'comments' for comment leader */
781 char_u *leader = NULL; /* copy of comment leader */
782#endif
783 char_u *allocated = NULL; /* allocated memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 int saved_char = NUL; /* init for GCC */
786#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
787 pos_T *pos;
788#endif
789#ifdef FEAT_SMARTINDENT
790 int do_si = (!p_paste && curbuf->b_p_si
791# ifdef FEAT_CINDENT
792 && !curbuf->b_p_cin
793# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200794# ifdef FEAT_EVAL
795 && *curbuf->b_p_inde == NUL
796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 );
798 int no_si = FALSE; /* reset did_si afterwards */
799 int first_char = NUL; /* init for GCC */
800#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200801#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 int vreplace_mode;
803#endif
804 int did_append; /* appended a new line */
805 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
806
807 /*
808 * make a copy of the current line so we can mess with it
809 */
810 saved_line = vim_strsave(ml_get_curline());
811 if (saved_line == NULL) /* out of memory! */
812 return FALSE;
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 if (State & VREPLACE_FLAG)
815 {
816 /*
817 * With VREPLACE we make a copy of the next line, which we will be
818 * starting to replace. First make the new line empty and let vim play
819 * with the indenting and comment leader to its heart's content. Then
820 * we grab what it ended up putting on the new line, put back the
821 * original line, and call ins_char() to put each new character onto
822 * the line, replacing what was there before and pushing the right
823 * stuff onto the replace stack. -- webb.
824 */
825 if (curwin->w_cursor.lnum < orig_line_count)
826 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
827 else
828 next_line = vim_strsave((char_u *)"");
829 if (next_line == NULL) /* out of memory! */
830 goto theend;
831
832 /*
833 * In VREPLACE mode, a NL replaces the rest of the line, and starts
834 * replacing the next line, so push all of the characters left on the
835 * line onto the replace stack. We'll push any other characters that
836 * might be replaced at the start of the next line (due to autoindent
837 * etc) a bit later.
838 */
839 replace_push(NUL); /* Call twice because BS over NL expects it */
840 replace_push(NUL);
841 p = saved_line + curwin->w_cursor.col;
842 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000843 {
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000844 if (has_mbyte)
845 p += replace_push_mb(p);
846 else
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000847 replace_push(*p++);
848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 saved_line[curwin->w_cursor.col] = NUL;
850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200852 if ((State & INSERT) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
854 p_extra = saved_line + curwin->w_cursor.col;
855#ifdef FEAT_SMARTINDENT
856 if (do_si) /* need first char after new line break */
857 {
858 p = skipwhite(p_extra);
859 first_char = *p;
860 }
861#endif
862#ifdef FEAT_COMMENTS
863 extra_len = (int)STRLEN(p_extra);
864#endif
865 saved_char = *p_extra;
866 *p_extra = NUL;
867 }
868
869 u_clearline(); /* cannot do "U" command when adding lines */
870#ifdef FEAT_SMARTINDENT
871 did_si = FALSE;
872#endif
873 ai_col = 0;
874
875 /*
876 * If we just did an auto-indent, then we didn't type anything on
877 * the prior line, and it should be truncated. Do this even if 'ai' is not
878 * set because automatically inserting a comment leader also sets did_ai.
879 */
880 if (dir == FORWARD && did_ai)
881 trunc_line = TRUE;
882
883 /*
884 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
885 * indent to use for the new line.
886 */
887 if (curbuf->b_p_ai
888#ifdef FEAT_SMARTINDENT
889 || do_si
890#endif
891 )
892 {
893 /*
894 * count white space on current line
895 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200896#ifdef FEAT_VARTABS
897 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
898 curbuf->b_p_vts_array, FALSE);
899#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200900 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200901#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200902 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
903 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
905#ifdef FEAT_SMARTINDENT
906 /*
907 * Do smart indenting.
908 * In insert/replace mode (only when dir == FORWARD)
909 * we may move some text to the next line. If it starts with '{'
910 * don't add an indent. Fixes inserting a NL before '{' in line
911 * "if (condition) {"
912 */
913 if (!trunc_line && do_si && *saved_line != NUL
914 && (p_extra == NULL || first_char != '{'))
915 {
916 char_u *ptr;
917 char_u last_char;
918
919 old_cursor = curwin->w_cursor;
920 ptr = saved_line;
921# ifdef FEAT_COMMENTS
922 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200923 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 else
925 lead_len = 0;
926# endif
927 if (dir == FORWARD)
928 {
929 /*
930 * Skip preprocessor directives, unless they are
931 * recognised as comments.
932 */
933 if (
934# ifdef FEAT_COMMENTS
935 lead_len == 0 &&
936# endif
937 ptr[0] == '#')
938 {
939 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
940 ptr = ml_get(--curwin->w_cursor.lnum);
941 newindent = get_indent();
942 }
943# ifdef FEAT_COMMENTS
944 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200945 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 else
947 lead_len = 0;
948 if (lead_len > 0)
949 {
950 /*
951 * This case gets the following right:
952 * \*
953 * * A comment (read '\' as '/').
954 * *\
955 * #define IN_THE_WAY
956 * This should line up here;
957 */
958 p = skipwhite(ptr);
959 if (p[0] == '/' && p[1] == '*')
960 p++;
961 if (p[0] == '*')
962 {
963 for (p++; *p; p++)
964 {
965 if (p[0] == '/' && p[-1] == '*')
966 {
967 /*
968 * End of C comment, indent should line up
969 * with the line containing the start of
970 * the comment
971 */
972 curwin->w_cursor.col = (colnr_T)(p - ptr);
973 if ((pos = findmatch(NULL, NUL)) != NULL)
974 {
975 curwin->w_cursor.lnum = pos->lnum;
976 newindent = get_indent();
977 }
978 }
979 }
980 }
981 }
982 else /* Not a comment line */
983# endif
984 {
985 /* Find last non-blank in line */
986 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100987 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 --p;
989 last_char = *p;
990
991 /*
992 * find the character just before the '{' or ';'
993 */
994 if (last_char == '{' || last_char == ';')
995 {
996 if (p > ptr)
997 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100998 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 --p;
1000 }
1001 /*
1002 * Try to catch lines that are split over multiple
1003 * lines. eg:
1004 * if (condition &&
1005 * condition) {
1006 * Should line up here!
1007 * }
1008 */
1009 if (*p == ')')
1010 {
1011 curwin->w_cursor.col = (colnr_T)(p - ptr);
1012 if ((pos = findmatch(NULL, '(')) != NULL)
1013 {
1014 curwin->w_cursor.lnum = pos->lnum;
1015 newindent = get_indent();
1016 ptr = ml_get_curline();
1017 }
1018 }
1019 /*
1020 * If last character is '{' do indent, without
1021 * checking for "if" and the like.
1022 */
1023 if (last_char == '{')
1024 {
1025 did_si = TRUE; /* do indent */
1026 no_si = TRUE; /* don't delete it when '{' typed */
1027 }
1028 /*
1029 * Look for "if" and the like, use 'cinwords'.
1030 * Don't do this if the previous line ended in ';' or
1031 * '}'.
1032 */
1033 else if (last_char != ';' && last_char != '}'
1034 && cin_is_cinword(ptr))
1035 did_si = TRUE;
1036 }
1037 }
1038 else /* dir == BACKWARD */
1039 {
1040 /*
1041 * Skip preprocessor directives, unless they are
1042 * recognised as comments.
1043 */
1044 if (
1045# ifdef FEAT_COMMENTS
1046 lead_len == 0 &&
1047# endif
1048 ptr[0] == '#')
1049 {
1050 int was_backslashed = FALSE;
1051
1052 while ((ptr[0] == '#' || was_backslashed) &&
1053 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1054 {
1055 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1056 was_backslashed = TRUE;
1057 else
1058 was_backslashed = FALSE;
1059 ptr = ml_get(++curwin->w_cursor.lnum);
1060 }
1061 if (was_backslashed)
1062 newindent = 0; /* Got to end of file */
1063 else
1064 newindent = get_indent();
1065 }
1066 p = skipwhite(ptr);
1067 if (*p == '}') /* if line starts with '}': do indent */
1068 did_si = TRUE;
1069 else /* can delete indent when '{' typed */
1070 can_si_back = TRUE;
1071 }
1072 curwin->w_cursor = old_cursor;
1073 }
1074 if (do_si)
1075 can_si = TRUE;
1076#endif /* FEAT_SMARTINDENT */
1077
1078 did_ai = TRUE;
1079 }
1080
1081#ifdef FEAT_COMMENTS
1082 /*
1083 * Find out if the current line starts with a comment leader.
1084 * This may then be inserted in front of the new line.
1085 */
1086 end_comment_pending = NUL;
1087 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +02001088 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 else
1090 lead_len = 0;
1091 if (lead_len > 0)
1092 {
1093 char_u *lead_repl = NULL; /* replaces comment leader */
1094 int lead_repl_len = 0; /* length of *lead_repl */
1095 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
1096 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
1097 char_u *comment_end = NULL; /* where lead_end has been found */
1098 int extra_space = FALSE; /* append extra space */
1099 int current_flag;
1100 int require_blank = FALSE; /* requires blank after middle */
1101 char_u *p2;
1102
1103 /*
1104 * If the comment leader has the start, middle or end flag, it may not
1105 * be used or may be replaced with the middle leader.
1106 */
1107 for (p = lead_flags; *p && *p != ':'; ++p)
1108 {
1109 if (*p == COM_BLANK)
1110 {
1111 require_blank = TRUE;
1112 continue;
1113 }
1114 if (*p == COM_START || *p == COM_MIDDLE)
1115 {
1116 current_flag = *p;
1117 if (*p == COM_START)
1118 {
1119 /*
1120 * Doing "O" on a start of comment does not insert leader.
1121 */
1122 if (dir == BACKWARD)
1123 {
1124 lead_len = 0;
1125 break;
1126 }
1127
1128 /* find start of middle part */
1129 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1130 require_blank = FALSE;
1131 }
1132
1133 /*
1134 * Isolate the strings of the middle and end leader.
1135 */
1136 while (*p && p[-1] != ':') /* find end of middle flags */
1137 {
1138 if (*p == COM_BLANK)
1139 require_blank = TRUE;
1140 ++p;
1141 }
1142 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1143
1144 while (*p && p[-1] != ':') /* find end of end flags */
1145 {
1146 /* Check whether we allow automatic ending of comments */
1147 if (*p == COM_AUTO_END)
1148 end_comment_pending = -1; /* means we want to set it */
1149 ++p;
1150 }
1151 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1152
1153 if (end_comment_pending == -1) /* we can set it now */
1154 end_comment_pending = lead_end[n - 1];
1155
1156 /*
1157 * If the end of the comment is in the same line, don't use
1158 * the comment leader.
1159 */
1160 if (dir == FORWARD)
1161 {
1162 for (p = saved_line + lead_len; *p; ++p)
1163 if (STRNCMP(p, lead_end, n) == 0)
1164 {
1165 comment_end = p;
1166 lead_len = 0;
1167 break;
1168 }
1169 }
1170
1171 /*
1172 * Doing "o" on a start of comment inserts the middle leader.
1173 */
1174 if (lead_len > 0)
1175 {
1176 if (current_flag == COM_START)
1177 {
1178 lead_repl = lead_middle;
1179 lead_repl_len = (int)STRLEN(lead_middle);
1180 }
1181
1182 /*
1183 * If we have hit RETURN immediately after the start
1184 * comment leader, then put a space after the middle
1185 * comment leader on the next line.
1186 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001187 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 && ((p_extra != NULL
1189 && (int)curwin->w_cursor.col == lead_len)
1190 || (p_extra == NULL
1191 && saved_line[lead_len] == NUL)
1192 || require_blank))
1193 extra_space = TRUE;
1194 }
1195 break;
1196 }
1197 if (*p == COM_END)
1198 {
1199 /*
1200 * Doing "o" on the end of a comment does not insert leader.
1201 * Remember where the end is, might want to use it to find the
1202 * start (for C-comments).
1203 */
1204 if (dir == FORWARD)
1205 {
1206 comment_end = skipwhite(saved_line);
1207 lead_len = 0;
1208 break;
1209 }
1210
1211 /*
1212 * Doing "O" on the end of a comment inserts the middle leader.
1213 * Find the string for the middle leader, searching backwards.
1214 */
1215 while (p > curbuf->b_p_com && *p != ',')
1216 --p;
1217 for (lead_repl = p; lead_repl > curbuf->b_p_com
1218 && lead_repl[-1] != ':'; --lead_repl)
1219 ;
1220 lead_repl_len = (int)(p - lead_repl);
1221
1222 /* We can probably always add an extra space when doing "O" on
1223 * the comment-end */
1224 extra_space = TRUE;
1225
1226 /* Check whether we allow automatic ending of comments */
1227 for (p2 = p; *p2 && *p2 != ':'; p2++)
1228 {
1229 if (*p2 == COM_AUTO_END)
1230 end_comment_pending = -1; /* means we want to set it */
1231 }
1232 if (end_comment_pending == -1)
1233 {
1234 /* Find last character in end-comment string */
1235 while (*p2 && *p2 != ',')
1236 p2++;
1237 end_comment_pending = p2[-1];
1238 }
1239 break;
1240 }
1241 if (*p == COM_FIRST)
1242 {
1243 /*
1244 * Comment leader for first line only: Don't repeat leader
1245 * when using "O", blank out leader when using "o".
1246 */
1247 if (dir == BACKWARD)
1248 lead_len = 0;
1249 else
1250 {
1251 lead_repl = (char_u *)"";
1252 lead_repl_len = 0;
1253 }
1254 break;
1255 }
1256 }
1257 if (lead_len)
1258 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001259 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001260 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001261 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 allocated = leader; /* remember to free it later */
1263
1264 if (leader == NULL)
1265 lead_len = 0;
1266 else
1267 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001268 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
1270 /*
1271 * Replace leader with lead_repl, right or left adjusted
1272 */
1273 if (lead_repl != NULL)
1274 {
1275 int c = 0;
1276 int off = 0;
1277
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001278 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
1280 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001281 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 else if (VIM_ISDIGIT(*p) || *p == '-')
1283 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001284 else
1285 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287 if (c == COM_RIGHT) /* right adjusted leader */
1288 {
1289 /* find last non-white in the leader to line up with */
1290 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001291 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001294
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001295 /* Compute the length of the replaced characters in
1296 * screen characters, not bytes. */
1297 {
1298 int repl_size = vim_strnsize(lead_repl,
1299 lead_repl_len);
1300 int old_size = 0;
1301 char_u *endp = p;
1302 int l;
1303
1304 while (old_size < repl_size && p > leader)
1305 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001306 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001307 old_size += ptr2cells(p);
1308 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001309 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001310 if (l != 0)
1311 mch_memmove(endp + l, endp,
1312 (size_t)((leader + lead_len) - endp));
1313 lead_len += l;
1314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1316 if (p + lead_repl_len > leader + lead_len)
1317 p[lead_repl_len] = NUL;
1318
1319 /* blank-out any other chars from the old leader. */
1320 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001321 {
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001322 int l = mb_head_off(leader, p);
1323
1324 if (l > 1)
1325 {
1326 p -= l;
1327 if (ptr2cells(p) > 1)
1328 {
1329 p[1] = ' ';
1330 --l;
1331 }
1332 mch_memmove(p + 1, p + l + 1,
1333 (size_t)((leader + lead_len) - (p + l + 1)));
1334 lead_len -= l;
1335 *p = ' ';
1336 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001337 else if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 }
1341 else /* left adjusted leader */
1342 {
1343 p = skipwhite(leader);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001344
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001345 /* Compute the length of the replaced characters in
1346 * screen characters, not bytes. Move the part that is
1347 * not to be overwritten. */
1348 {
1349 int repl_size = vim_strnsize(lead_repl,
1350 lead_repl_len);
1351 int i;
1352 int l;
1353
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001354 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001355 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001356 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001357 if (vim_strnsize(p, i + l) > repl_size)
1358 break;
1359 }
1360 if (i != lead_repl_len)
1361 {
1362 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001363 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001364 lead_len += lead_repl_len - i;
1365 }
1366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1368
1369 /* Replace any remaining non-white chars in the old
1370 * leader by spaces. Keep Tabs, the indent must
1371 * remain the same. */
1372 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001373 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
1375 /* Don't put a space before a TAB. */
1376 if (p + 1 < leader + lead_len && p[1] == TAB)
1377 {
1378 --lead_len;
1379 mch_memmove(p, p + 1,
1380 (leader + lead_len) - p);
1381 }
1382 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001383 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001384 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001385
1386 if (l > 1)
1387 {
1388 if (ptr2cells(p) > 1)
1389 {
1390 /* Replace a double-wide char with
1391 * two spaces */
1392 --l;
1393 *p++ = ' ';
1394 }
1395 mch_memmove(p + 1, p + l,
1396 (leader + lead_len) - p);
1397 lead_len -= l - 1;
1398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402 *p = NUL;
1403 }
1404
1405 /* Recompute the indent, it may have changed. */
1406 if (curbuf->b_p_ai
1407#ifdef FEAT_SMARTINDENT
1408 || do_si
1409#endif
1410 )
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001411#ifdef FEAT_VARTABS
1412 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1413 curbuf->b_p_vts_array, FALSE);
1414#else
1415 newindent = get_indent_str(leader,
1416 (int)curbuf->b_p_ts, FALSE);
1417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
1419 /* Add the indent offset */
1420 if (newindent + off < 0)
1421 {
1422 off = -newindent;
1423 newindent = 0;
1424 }
1425 else
1426 newindent += off;
1427
1428 /* Correct trailing spaces for the shift, so that
1429 * alignment remains equal. */
1430 while (off > 0 && lead_len > 0
1431 && leader[lead_len - 1] == ' ')
1432 {
1433 /* Don't do it when there is a tab before the space */
1434 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1435 break;
1436 --lead_len;
1437 --off;
1438 }
1439
1440 /* If the leader ends in white space, don't add an
1441 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001442 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 extra_space = FALSE;
1444 leader[lead_len] = NUL;
1445 }
1446
1447 if (extra_space)
1448 {
1449 leader[lead_len++] = ' ';
1450 leader[lead_len] = NUL;
1451 }
1452
1453 newcol = lead_len;
1454
1455 /*
1456 * if a new indent will be set below, remove the indent that
1457 * is in the comment leader
1458 */
1459 if (newindent
1460#ifdef FEAT_SMARTINDENT
1461 || did_si
1462#endif
1463 )
1464 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001465 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 {
1467 --lead_len;
1468 --newcol;
1469 ++leader;
1470 }
1471 }
1472
1473 }
1474#ifdef FEAT_SMARTINDENT
1475 did_si = can_si = FALSE;
1476#endif
1477 }
1478 else if (comment_end != NULL)
1479 {
1480 /*
1481 * We have finished a comment, so we don't use the leader.
1482 * If this was a C-comment and 'ai' or 'si' is set do a normal
1483 * indent to align with the line containing the start of the
1484 * comment.
1485 */
1486 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1487 (curbuf->b_p_ai
1488#ifdef FEAT_SMARTINDENT
1489 || do_si
1490#endif
1491 ))
1492 {
1493 old_cursor = curwin->w_cursor;
1494 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1495 if ((pos = findmatch(NULL, NUL)) != NULL)
1496 {
1497 curwin->w_cursor.lnum = pos->lnum;
1498 newindent = get_indent();
1499 }
1500 curwin->w_cursor = old_cursor;
1501 }
1502 }
1503 }
1504#endif
1505
1506 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1507 if (p_extra != NULL)
1508 {
1509 *p_extra = saved_char; /* restore char that NUL replaced */
1510
1511 /*
1512 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1513 * non-blank.
1514 *
1515 * When in REPLACE mode, put the deleted blanks on the replace stack,
1516 * preceded by a NUL, so they can be put back when a BS is entered.
1517 */
1518 if (REPLACE_NORMAL(State))
1519 replace_push(NUL); /* end of extra blanks */
1520 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1521 {
1522 while ((*p_extra == ' ' || *p_extra == '\t')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 && (!enc_utf8
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001524 || !utf_iscomposing(utf_ptr2char(p_extra + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
1526 if (REPLACE_NORMAL(State))
1527 replace_push(*p_extra);
1528 ++p_extra;
1529 ++less_cols_off;
1530 }
1531 }
1532 if (*p_extra != NUL)
1533 did_ai = FALSE; /* append some text, don't truncate now */
1534
1535 /* columns for marks adjusted for removed columns */
1536 less_cols = (int)(p_extra - saved_line);
1537 }
1538
1539 if (p_extra == NULL)
1540 p_extra = (char_u *)""; /* append empty line */
1541
1542#ifdef FEAT_COMMENTS
1543 /* concatenate leader and p_extra, if there is a leader */
1544 if (lead_len)
1545 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001546 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1547 {
1548 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001549 int padding = second_line_indent
1550 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001551
1552 /* Here whitespace is inserted after the comment char.
1553 * Below, set_indent(newindent, SIN_INSERT) will insert the
1554 * whitespace needed before the comment char. */
1555 for (i = 0; i < padding; i++)
1556 {
1557 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001558 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001559 newcol++;
1560 }
1561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 STRCAT(leader, p_extra);
1563 p_extra = leader;
1564 did_ai = TRUE; /* So truncating blanks works with comments */
1565 less_cols -= lead_len;
1566 }
1567 else
1568 end_comment_pending = NUL; /* turns out there was no leader */
1569#endif
1570
1571 old_cursor = curwin->w_cursor;
1572 if (dir == BACKWARD)
1573 --curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 {
1576 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1577 == FAIL)
1578 goto theend;
1579 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001580 * with markers.
1581 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001582 * be marks there. But still needed in diff mode. */
1583 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1584#ifdef FEAT_DIFF
1585 || curwin->w_p_diff
1586#endif
1587 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001588 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 did_append = TRUE;
1590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 else
1592 {
1593 /*
1594 * In VREPLACE mode we are starting to replace the next line.
1595 */
1596 curwin->w_cursor.lnum++;
1597 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1598 {
1599 /* In case we NL to a new line, BS to the previous one, and NL
1600 * again, we don't want to save the new line for undo twice.
1601 */
1602 (void)u_save_cursor(); /* errors are ignored! */
1603 vr_lines_changed++;
1604 }
1605 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1606 changed_bytes(curwin->w_cursor.lnum, 0);
1607 curwin->w_cursor.lnum--;
1608 did_append = FALSE;
1609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
1611 if (newindent
1612#ifdef FEAT_SMARTINDENT
1613 || did_si
1614#endif
1615 )
1616 {
1617 ++curwin->w_cursor.lnum;
1618#ifdef FEAT_SMARTINDENT
1619 if (did_si)
1620 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001621 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001624 newindent -= newindent % sw;
1625 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
1627#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001628 /* Copy the indent */
1629 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 {
1631 (void)copy_indent(newindent, saved_line);
1632
1633 /*
1634 * Set the 'preserveindent' option so that any further screwing
1635 * with the line doesn't entirely destroy our efforts to preserve
1636 * it. It gets restored at the function end.
1637 */
1638 curbuf->b_p_pi = TRUE;
1639 }
1640 else
1641 (void)set_indent(newindent, SIN_INSERT);
1642 less_cols -= curwin->w_cursor.col;
1643
1644 ai_col = curwin->w_cursor.col;
1645
1646 /*
1647 * In REPLACE mode, for each character in the new indent, there must
1648 * be a NUL on the replace stack, for when it is deleted with BS
1649 */
1650 if (REPLACE_NORMAL(State))
1651 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1652 replace_push(NUL);
1653 newcol += curwin->w_cursor.col;
1654#ifdef FEAT_SMARTINDENT
1655 if (no_si)
1656 did_si = FALSE;
1657#endif
1658 }
1659
1660#ifdef FEAT_COMMENTS
1661 /*
1662 * In REPLACE mode, for each character in the extra leader, there must be
1663 * a NUL on the replace stack, for when it is deleted with BS.
1664 */
1665 if (REPLACE_NORMAL(State))
1666 while (lead_len-- > 0)
1667 replace_push(NUL);
1668#endif
1669
1670 curwin->w_cursor = old_cursor;
1671
1672 if (dir == FORWARD)
1673 {
1674 if (trunc_line || (State & INSERT))
1675 {
1676 /* truncate current line at cursor */
1677 saved_line[curwin->w_cursor.col] = NUL;
1678 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1679 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1680 truncate_spaces(saved_line);
1681 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1682 saved_line = NULL;
1683 if (did_append)
1684 {
1685 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1686 curwin->w_cursor.lnum + 1, 1L);
1687 did_append = FALSE;
1688
1689 /* Move marks after the line break to the new line. */
1690 if (flags & OPENLINE_MARKFIX)
1691 mark_col_adjust(curwin->w_cursor.lnum,
1692 curwin->w_cursor.col + less_cols_off,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01001693 1L, (long)-less_cols, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 }
1695 else
1696 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1697 }
1698
1699 /*
1700 * Put the cursor on the new line. Careful: the scrollup() above may
1701 * have moved w_cursor, we must use old_cursor.
1702 */
1703 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1704 }
1705 if (did_append)
1706 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1707
1708 curwin->w_cursor.col = newcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001711#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 /*
1713 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1714 * fixthisline() from doing it (via change_indent()) by telling it we're in
1715 * normal INSERT mode.
1716 */
1717 if (State & VREPLACE_FLAG)
1718 {
1719 vreplace_mode = State; /* So we know to put things right later */
1720 State = INSERT;
1721 }
1722 else
1723 vreplace_mode = 0;
1724#endif
1725#ifdef FEAT_LISP
1726 /*
1727 * May do lisp indenting.
1728 */
1729 if (!p_paste
1730# ifdef FEAT_COMMENTS
1731 && leader == NULL
1732# endif
1733 && curbuf->b_p_lisp
1734 && curbuf->b_p_ai)
1735 {
1736 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001737 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
1739#endif
1740#ifdef FEAT_CINDENT
1741 /*
1742 * May do indenting after opening a new line.
1743 */
1744 if (!p_paste
1745 && (curbuf->b_p_cin
1746# ifdef FEAT_EVAL
1747 || *curbuf->b_p_inde != NUL
1748# endif
1749 )
1750 && in_cinkeys(dir == FORWARD
1751 ? KEY_OPEN_FORW
1752 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1753 {
1754 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001755 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 }
1757#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001758#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 if (vreplace_mode != 0)
1760 State = vreplace_mode;
1761#endif
1762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 /*
1764 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1765 * original line, and inserts the new stuff char by char, pushing old stuff
1766 * onto the replace stack (via ins_char()).
1767 */
1768 if (State & VREPLACE_FLAG)
1769 {
1770 /* Put new line in p_extra */
1771 p_extra = vim_strsave(ml_get_curline());
1772 if (p_extra == NULL)
1773 goto theend;
1774
1775 /* Put back original line */
1776 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1777
1778 /* Insert new stuff into line again */
1779 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 ins_bytes(p_extra); /* will call changed_bytes() */
1782 vim_free(p_extra);
1783 next_line = NULL;
1784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001786 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787theend:
1788 curbuf->b_p_pi = saved_pi;
1789 vim_free(saved_line);
1790 vim_free(next_line);
1791 vim_free(allocated);
1792 return retval;
1793}
1794
1795#if defined(FEAT_COMMENTS) || defined(PROTO)
1796/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001797 * get_leader_len() returns the length in bytes of the prefix of the given
1798 * string which introduces a comment. If this string is not a comment then
1799 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 * When "flags" is not NULL, it is set to point to the flags of the recognized
1801 * comment leader.
1802 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001803 * If "include_space" is set, include trailing whitespace while calculating the
1804 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 */
1806 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001807get_leader_len(
1808 char_u *line,
1809 char_u **flags,
1810 int backward,
1811 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812{
1813 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001814 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 int got_com = FALSE;
1816 int found_one;
1817 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1818 char_u *string; /* pointer to comment string */
1819 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001820 int middle_match_len = 0;
1821 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001822 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
Bram Moolenaar81340392012-06-06 16:12:59 +02001824 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001825 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 ++i;
1827
1828 /*
1829 * Repeat to match several nested comment strings.
1830 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001831 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 {
1833 /*
1834 * scan through the 'comments' option for a match
1835 */
1836 found_one = FALSE;
1837 for (list = curbuf->b_p_com; *list; )
1838 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001839 /* Get one option part into part_buf[]. Advance "list" to next
1840 * one. Put "string" at start of string. */
1841 if (!got_com && flags != NULL)
1842 *flags = list; /* remember where flags started */
1843 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1845 string = vim_strchr(part_buf, ':');
1846 if (string == NULL) /* missing ':', ignore this part */
1847 continue;
1848 *string++ = NUL; /* isolate flags from string */
1849
Bram Moolenaara4271d52011-05-10 13:38:27 +02001850 /* If we found a middle match previously, use that match when this
1851 * is not a middle or end. */
1852 if (middle_match_len != 0
1853 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1854 && vim_strchr(part_buf, COM_END) == NULL)
1855 break;
1856
1857 /* When we already found a nested comment, only accept further
1858 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1860 continue;
1861
Bram Moolenaara4271d52011-05-10 13:38:27 +02001862 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1864 continue;
1865
Bram Moolenaara4271d52011-05-10 13:38:27 +02001866 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 * When string starts with white space, must have some white space
1868 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001869 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001870 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001872 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001873 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001874 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 ++string;
1876 }
1877 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1878 ;
1879 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001880 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881
Bram Moolenaara4271d52011-05-10 13:38:27 +02001882 /* When 'b' flag used, there must be white space or an
1883 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001885 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 continue;
1887
Bram Moolenaara4271d52011-05-10 13:38:27 +02001888 /* We have found a match, stop searching unless this is a middle
1889 * comment. The middle comment can be a substring of the end
1890 * comment in which case it's better to return the length of the
1891 * end comment and its flags. Thus we keep searching with middle
1892 * and end matches and use an end match if it matches better. */
1893 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1894 {
1895 if (middle_match_len == 0)
1896 {
1897 middle_match_len = j;
1898 saved_flags = prev_list;
1899 }
1900 continue;
1901 }
1902 if (middle_match_len != 0 && j > middle_match_len)
1903 /* Use this match instead of the middle match, since it's a
1904 * longer thus better match. */
1905 middle_match_len = 0;
1906
1907 if (middle_match_len == 0)
1908 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 found_one = TRUE;
1910 break;
1911 }
1912
Bram Moolenaara4271d52011-05-10 13:38:27 +02001913 if (middle_match_len != 0)
1914 {
1915 /* Use the previously found middle match after failing to find a
1916 * match with an end. */
1917 if (!got_com && flags != NULL)
1918 *flags = saved_flags;
1919 i += middle_match_len;
1920 found_one = TRUE;
1921 }
1922
1923 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (!found_one)
1925 break;
1926
Bram Moolenaar81340392012-06-06 16:12:59 +02001927 result = i;
1928
Bram Moolenaara4271d52011-05-10 13:38:27 +02001929 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001930 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 ++i;
1932
Bram Moolenaar81340392012-06-06 16:12:59 +02001933 if (include_space)
1934 result = i;
1935
Bram Moolenaara4271d52011-05-10 13:38:27 +02001936 /* If this comment doesn't nest, stop here. */
1937 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if (vim_strchr(part_buf, COM_NEST) == NULL)
1939 break;
1940 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001941 return result;
1942}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001943
Bram Moolenaar81340392012-06-06 16:12:59 +02001944/*
1945 * Return the offset at which the last comment in line starts. If there is no
1946 * comment in the whole line, -1 is returned.
1947 *
1948 * When "flags" is not null, it is set to point to the flags describing the
1949 * recognized comment leader.
1950 */
1951 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001952get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001953{
1954 int result = -1;
1955 int i, j;
1956 int lower_check_bound = 0;
1957 char_u *string;
1958 char_u *com_leader;
1959 char_u *com_flags;
1960 char_u *list;
1961 int found_one;
1962 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1963
1964 /*
1965 * Repeat to match several nested comment strings.
1966 */
1967 i = (int)STRLEN(line);
1968 while (--i >= lower_check_bound)
1969 {
1970 /*
1971 * scan through the 'comments' option for a match
1972 */
1973 found_one = FALSE;
1974 for (list = curbuf->b_p_com; *list; )
1975 {
1976 char_u *flags_save = list;
1977
1978 /*
1979 * Get one option part into part_buf[]. Advance list to next one.
1980 * put string at start of string.
1981 */
1982 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1983 string = vim_strchr(part_buf, ':');
1984 if (string == NULL) /* If everything is fine, this cannot actually
1985 * happen. */
1986 {
1987 continue;
1988 }
1989 *string++ = NUL; /* Isolate flags from string. */
1990 com_leader = string;
1991
1992 /*
1993 * Line contents and string must match.
1994 * When string starts with white space, must have some white space
1995 * (but the amount does not need to match, there might be a mix of
1996 * TABs and spaces).
1997 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001998 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001999 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002000 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002001 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002002 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002003 ++string;
2004 }
2005 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
2006 /* do nothing */;
2007 if (string[j] != NUL)
2008 continue;
2009
2010 /*
2011 * When 'b' flag used, there must be white space or an
2012 * end-of-line after the string in the line.
2013 */
2014 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002015 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02002016 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002017
Bram Moolenaar4af72592018-12-09 15:00:52 +01002018 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +01002019 {
Bram Moolenaar4af72592018-12-09 15:00:52 +01002020 // For a middlepart comment, only consider it to match if
2021 // everything before the current position in the line is
2022 // whitespace. Otherwise we would think we are inside a
2023 // comment if the middle part appears somewhere in the middle
2024 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +01002025 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
2026 ;
2027 if (j < i)
2028 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +02002029 }
2030
2031 /*
2032 * We have found a match, stop searching.
2033 */
2034 found_one = TRUE;
2035
2036 if (flags)
2037 *flags = flags_save;
2038 com_flags = flags_save;
2039
2040 break;
2041 }
2042
2043 if (found_one)
2044 {
2045 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2046 int len1, len2, off;
2047
2048 result = i;
2049 /*
2050 * If this comment nests, continue searching.
2051 */
2052 if (vim_strchr(part_buf, COM_NEST) != NULL)
2053 continue;
2054
2055 lower_check_bound = i;
2056
2057 /* Let's verify whether the comment leader found is a substring
2058 * of other comment leaders. If it is, let's adjust the
2059 * lower_check_bound so that we make sure that we have determined
2060 * the comment leader correctly.
2061 */
2062
Bram Moolenaar1c465442017-03-12 20:10:05 +01002063 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002064 ++com_leader;
2065 len1 = (int)STRLEN(com_leader);
2066
2067 for (list = curbuf->b_p_com; *list; )
2068 {
2069 char_u *flags_save = list;
2070
2071 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2072 if (flags_save == com_flags)
2073 continue;
2074 string = vim_strchr(part_buf2, ':');
2075 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002076 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002077 ++string;
2078 len2 = (int)STRLEN(string);
2079 if (len2 == 0)
2080 continue;
2081
2082 /* Now we have to verify whether string ends with a substring
2083 * beginning the com_leader. */
2084 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2085 {
2086 --off;
2087 if (!STRNCMP(string + off, com_leader, len2 - off))
2088 {
2089 if (i - off < lower_check_bound)
2090 lower_check_bound = i - off;
2091 }
2092 }
2093 }
2094 }
2095 }
2096 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097}
2098#endif
2099
2100/*
2101 * Return the number of window lines occupied by buffer line "lnum".
2102 */
2103 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002104plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105{
2106 return plines_win(curwin, lnum, TRUE);
2107}
2108
2109 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002110plines_win(
2111 win_T *wp,
2112 linenr_T lnum,
2113 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114{
2115#if defined(FEAT_DIFF) || defined(PROTO)
2116 /* Check for filler lines above this buffer line. When folded the result
2117 * is one line anyway. */
2118 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2119}
2120
2121 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002122plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123{
2124 return plines_win_nofill(curwin, lnum, TRUE);
2125}
2126
2127 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002128plines_win_nofill(
2129 win_T *wp,
2130 linenr_T lnum,
2131 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
2133#endif
2134 int lines;
2135
2136 if (!wp->w_p_wrap)
2137 return 1;
2138
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (wp->w_width == 0)
2140 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142#ifdef FEAT_FOLDING
2143 /* A folded lines is handled just like an empty line. */
2144 /* NOTE: Caller must handle lines that are MAYBE folded. */
2145 if (lineFolded(wp, lnum) == TRUE)
2146 return 1;
2147#endif
2148
2149 lines = plines_win_nofold(wp, lnum);
2150 if (winheight > 0 && lines > wp->w_height)
2151 return (int)wp->w_height;
2152 return lines;
2153}
2154
2155/*
2156 * Return number of window lines physical line "lnum" will occupy in window
2157 * "wp". Does not care about folding, 'wrap' or 'diff'.
2158 */
2159 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002160plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161{
2162 char_u *s;
2163 long col;
2164 int width;
2165
2166 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2167 if (*s == NUL) /* empty line */
2168 return 1;
2169 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2170
2171 /*
2172 * If list mode is on, then the '$' at the end of the line may take up one
2173 * extra column.
2174 */
2175 if (wp->w_p_list && lcs_eol != NUL)
2176 col += 1;
2177
2178 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002179 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002181 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (width <= 0)
2183 return 32000;
2184 if (col <= width)
2185 return 1;
2186 col -= width;
2187 width += win_col_off2(wp);
2188 return (col + (width - 1)) / width + 1;
2189}
2190
2191/*
2192 * Like plines_win(), but only reports the number of physical screen lines
2193 * used from the start of the line to the given column number.
2194 */
2195 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002196plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198 long col;
2199 char_u *s;
2200 int lines = 0;
2201 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002202 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203
2204#ifdef FEAT_DIFF
2205 /* Check for filler lines above this buffer line. When folded the result
2206 * is one line anyway. */
2207 lines = diff_check_fill(wp, lnum);
2208#endif
2209
2210 if (!wp->w_p_wrap)
2211 return lines + 1;
2212
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 if (wp->w_width == 0)
2214 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
Bram Moolenaar597a4222014-06-25 14:39:50 +02002216 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
2218 col = 0;
2219 while (*s != NUL && --column >= 0)
2220 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002221 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002222 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
2224
2225 /*
2226 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2227 * INSERT mode, then col must be adjusted so that it represents the last
2228 * screen position of the TAB. This only fixes an error when the TAB wraps
2229 * from one screen line to the next (when 'columns' is not a multiple of
2230 * 'ts') -- webb.
2231 */
2232 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002233 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234
2235 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002236 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002238 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002239 if (width <= 0)
2240 return 9999;
2241
2242 lines += 1;
2243 if (col > width)
2244 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2245 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246}
2247
2248 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002249plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
2251 int count = 0;
2252
2253 while (first <= last)
2254 {
2255#ifdef FEAT_FOLDING
2256 int x;
2257
2258 /* Check if there are any really folded lines, but also included lines
2259 * that are maybe folded. */
2260 x = foldedCount(wp, first, NULL);
2261 if (x > 0)
2262 {
2263 ++count; /* count 1 for "+-- folded" line */
2264 first += x;
2265 }
2266 else
2267#endif
2268 {
2269#ifdef FEAT_DIFF
2270 if (first == wp->w_topline)
2271 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2272 else
2273#endif
2274 count += plines_win(wp, first, TRUE);
2275 ++first;
2276 }
2277 }
2278 return (count);
2279}
2280
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281/*
2282 * Insert string "p" at the cursor position. Stops at a NUL byte.
2283 * Handles Replace mode and multi-byte characters.
2284 */
2285 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002286ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 ins_bytes_len(p, (int)STRLEN(p));
2289}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291/*
2292 * Insert string "p" with length "len" at the cursor position.
2293 * Handles Replace mode and multi-byte characters.
2294 */
2295 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002296ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
2298 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 int n;
2300
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002301 if (has_mbyte)
2302 for (i = 0; i < len; i += n)
2303 {
2304 if (enc_utf8)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002305 // avoid reading past p[len]
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002306 n = utfc_ptr2len_len(p + i, len - i);
2307 else
2308 n = (*mb_ptr2len)(p + i);
2309 ins_char_bytes(p + i, n);
2310 }
2311 else
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002312 for (i = 0; i < len; ++i)
2313 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315
2316/*
2317 * Insert or replace a single character at the cursor position.
2318 * When in REPLACE or VREPLACE mode, replace any existing character.
2319 * Caller must have prepared for undo.
2320 * For multi-byte characters we get the whole character, the caller must
2321 * convert bytes to a character.
2322 */
2323 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002324ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002326 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002327 int n = (*mb_char2bytes)(c, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
2329 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2330 * Happens for CTRL-Vu9900. */
2331 if (buf[0] == 0)
2332 buf[0] = '\n';
2333
2334 ins_char_bytes(buf, n);
2335}
2336
2337 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002338ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339{
2340 int c = buf[0];
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002341 int newlen; // nr of bytes inserted
2342 int oldlen; // nr of bytes deleted (0 when not replacing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 char_u *p;
2344 char_u *newp;
2345 char_u *oldp;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002346 int linelen; // length of old line including NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 colnr_T col;
2348 linenr_T lnum = curwin->w_cursor.lnum;
2349 int i;
2350
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 /* Break tabs if needed. */
2352 if (virtual_active() && curwin->w_cursor.coladd > 0)
2353 coladvance_force(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355 col = curwin->w_cursor.col;
2356 oldp = ml_get(lnum);
2357 linelen = (int)STRLEN(oldp) + 1;
2358
2359 /* The lengths default to the values for when not replacing. */
2360 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
2363 if (State & REPLACE_FLAG)
2364 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 if (State & VREPLACE_FLAG)
2366 {
2367 colnr_T new_vcol = 0; /* init for GCC */
2368 colnr_T vcol;
2369 int old_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371 /*
2372 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2373 * Returns the old value of list, so when finished,
2374 * curwin->w_p_list should be set back to this.
2375 */
2376 old_list = curwin->w_p_list;
2377 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2378 curwin->w_p_list = FALSE;
2379
2380 /*
2381 * In virtual replace mode each character may replace one or more
2382 * characters (zero if it's a TAB). Count the number of bytes to
2383 * be deleted to make room for the new character, counting screen
2384 * cells. May result in adding spaces to fill a gap.
2385 */
2386 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 new_vcol = vcol + chartabsize(buf, vcol);
2388 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2389 {
2390 vcol += chartabsize(oldp + col + oldlen, vcol);
2391 /* Don't need to remove a TAB that takes us to the right
2392 * position. */
2393 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2394 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002395 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 /* Deleted a bit too much, insert spaces. */
2397 if (vcol > new_vcol)
2398 newlen += vcol - new_vcol;
2399 }
2400 curwin->w_p_list = old_list;
2401 }
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002402 else if (oldp[col] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
2404 /* normal replace */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002405 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407
2408
2409 /* Push the replaced bytes onto the replace stack, so that they can be
2410 * put back when BS is used. The bytes of a multi-byte character are
2411 * done the other way around, so that the first byte is popped off
2412 * first (it tells the byte length of the character). */
2413 replace_push(NUL);
2414 for (i = 0; i < oldlen; ++i)
2415 {
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002416 if (has_mbyte)
2417 i += replace_push_mb(oldp + col + i) - 1;
2418 else
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002419 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 }
2421 }
2422
2423 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2424 if (newp == NULL)
2425 return;
2426
2427 /* Copy bytes before the cursor. */
2428 if (col > 0)
2429 mch_memmove(newp, oldp, (size_t)col);
2430
2431 /* Copy bytes after the changed character(s). */
2432 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002433 if (linelen > col + oldlen)
2434 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 (size_t)(linelen - col - oldlen));
2436
2437 /* Insert or overwrite the new character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 mch_memmove(p, buf, charlen);
2439 i = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
2441 /* Fill with spaces when necessary. */
2442 while (i < newlen)
2443 p[i++] = ' ';
2444
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002445 // Replace the line in the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 ml_replace(lnum, newp, FALSE);
2447
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002448 // mark the buffer as changed and prepare for displaying
2449 inserted_bytes(lnum, col, newlen - oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
2451 /*
2452 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2453 * show the match for right parens and braces.
2454 */
2455 if (p_sm && (State & INSERT)
2456 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002457#ifdef FEAT_INS_EXPAND
2458 && !ins_compl_active()
2459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002461 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002462 if (has_mbyte)
2463 showmatch(mb_ptr2char(buf));
2464 else
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002465 showmatch(c);
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467
2468#ifdef FEAT_RIGHTLEFT
2469 if (!p_ri || (State & REPLACE_FLAG))
2470#endif
2471 {
2472 /* Normal insert: move cursor right */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 curwin->w_cursor.col += charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 }
2475 /*
2476 * TODO: should try to update w_row here, to avoid recomputing it later.
2477 */
2478}
2479
2480/*
2481 * Insert a string at the cursor position.
2482 * Note: Does NOT handle Replace mode.
2483 * Caller must have prepared for undo.
2484 */
2485 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002486ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
2488 char_u *oldp, *newp;
2489 int newlen = (int)STRLEN(s);
2490 int oldlen;
2491 colnr_T col;
2492 linenr_T lnum = curwin->w_cursor.lnum;
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 if (virtual_active() && curwin->w_cursor.coladd > 0)
2495 coladvance_force(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
2497 col = curwin->w_cursor.col;
2498 oldp = ml_get(lnum);
2499 oldlen = (int)STRLEN(oldp);
2500
2501 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2502 if (newp == NULL)
2503 return;
2504 if (col > 0)
2505 mch_memmove(newp, oldp, (size_t)col);
2506 mch_memmove(newp + col, s, (size_t)newlen);
2507 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2508 ml_replace(lnum, newp, FALSE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002509 inserted_bytes(lnum, col, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 curwin->w_cursor.col += newlen;
2511}
2512
2513/*
2514 * Delete one character under the cursor.
2515 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2516 * Caller must have prepared for undo.
2517 *
2518 * return FAIL for failure, OK otherwise
2519 */
2520 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002521del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (has_mbyte)
2524 {
2525 /* Make sure the cursor is at the start of a character. */
2526 mb_adjust_cursor();
2527 if (*ml_get_cursor() == NUL)
2528 return FAIL;
2529 return del_chars(1L, fixpos);
2530 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002531 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532}
2533
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534/*
2535 * Like del_bytes(), but delete characters instead of bytes.
2536 */
2537 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002538del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
2540 long bytes = 0;
2541 long i;
2542 char_u *p;
2543 int l;
2544
2545 p = ml_get_cursor();
2546 for (i = 0; i < count && *p != NUL; ++i)
2547 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002548 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 bytes += l;
2550 p += l;
2551 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002552 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
2555/*
2556 * Delete "count" bytes under the cursor.
2557 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2558 * Caller must have prepared for undo.
2559 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002560 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 */
2562 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002563del_bytes(
2564 long count,
2565 int fixpos_arg,
2566 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567{
2568 char_u *oldp, *newp;
2569 colnr_T oldlen;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002570 colnr_T newlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 linenr_T lnum = curwin->w_cursor.lnum;
2572 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002573 int alloc_newp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002575 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576
2577 oldp = ml_get(lnum);
2578 oldlen = (int)STRLEN(oldp);
2579
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002580 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 if (col >= oldlen)
2582 return FAIL;
2583
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002584 /* If "count" is zero there is nothing to do. */
2585 if (count == 0)
2586 return OK;
2587
2588 /* If "count" is negative the caller must be doing something wrong. */
2589 if (count < 1)
2590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002591 siemsg("E950: Invalid count for del_bytes(): %ld", count);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002592 return FAIL;
2593 }
2594
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 /* If 'delcombine' is set and deleting (less than) one character, only
2596 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002597 if (p_deco && use_delcombine && enc_utf8
2598 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002600 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 int n;
2602
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002603 (void)utfc_ptr2char(oldp + col, cc);
2604 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
2606 /* Find the last composing char, there can be several. */
2607 n = col;
2608 do
2609 {
2610 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002611 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 n += count;
2613 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2614 fixpos = 0;
2615 }
2616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
2618 /*
2619 * When count is too big, reduce it.
2620 */
2621 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2622 if (movelen <= 1)
2623 {
2624 /*
2625 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002626 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2627 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002629 if (col > 0 && fixpos && restart_edit == 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002630 && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
2632 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 if (has_mbyte)
2635 curwin->w_cursor.col -=
2636 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
2638 count = oldlen - col;
2639 movelen = 1;
2640 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002641 newlen = oldlen - count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
2643 /*
2644 * If the old line has been allocated the deletion can be done in the
2645 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002646 * Can't do this when using Netbeans, because we would need to invoke
2647 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002648 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002651 if (netbeans_active())
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002652 alloc_newp = TRUE;
Bram Moolenaare21877a2008-02-13 09:58:14 +00002653 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002655 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
2656 if (!alloc_newp)
2657 newp = oldp; // use same allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 else
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002659 { // need to allocate a new line
2660 newp = alloc((unsigned)(newlen + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (newp == NULL)
2662 return FAIL;
2663 mch_memmove(newp, oldp, (size_t)col);
2664 }
2665 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002666 if (alloc_newp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 ml_replace(lnum, newp, FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002668#ifdef FEAT_TEXT_PROP
2669 else
2670 {
2671 // Also move any following text properties.
2672 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
2673 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
2674 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
2675 curbuf->b_ml.ml_line_len -= count;
2676 }
2677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002679 // mark the buffer as changed and prepare for displaying
Bram Moolenaar33c8ca92019-01-02 18:00:27 +01002680 inserted_bytes(lnum, curwin->w_cursor.col, -count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
2682 return OK;
2683}
2684
2685/*
2686 * Delete from cursor to end of line.
2687 * Caller must have prepared for undo.
2688 *
2689 * return FAIL for failure, OK otherwise
2690 */
2691 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002692truncate_line(
2693 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 char_u *newp;
2696 linenr_T lnum = curwin->w_cursor.lnum;
2697 colnr_T col = curwin->w_cursor.col;
2698
2699 if (col == 0)
2700 newp = vim_strsave((char_u *)"");
2701 else
2702 newp = vim_strnsave(ml_get(lnum), col);
2703
2704 if (newp == NULL)
2705 return FAIL;
2706
2707 ml_replace(lnum, newp, FALSE);
2708
2709 /* mark the buffer as changed and prepare for displaying */
2710 changed_bytes(lnum, curwin->w_cursor.col);
2711
2712 /*
2713 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2714 */
2715 if (fixpos && curwin->w_cursor.col > 0)
2716 --curwin->w_cursor.col;
2717
2718 return OK;
2719}
2720
2721/*
2722 * Delete "nlines" lines at the cursor.
2723 * Saves the lines for undo first if "undo" is TRUE.
2724 */
2725 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002726del_lines(
2727 long nlines, /* number of lines to delete */
2728 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729{
2730 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002731 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
2733 if (nlines <= 0)
2734 return;
2735
2736 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002737 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 return;
2739
2740 for (n = 0; n < nlines; )
2741 {
2742 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2743 break;
2744
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002745 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 ++n;
2747
2748 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002749 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 break;
2751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002753 /* Correct the cursor position before calling deleted_lines_mark(), it may
2754 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 curwin->w_cursor.col = 0;
2756 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002757
2758 /* adjust marks, mark the buffer as changed and prepare for displaying */
2759 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760}
2761
2762 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002763gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002765 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002767 /* When searching columns is sometimes put at the end of a line. */
2768 if (pos->col == MAXCOL)
2769 return NUL;
2770 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 if (has_mbyte)
2772 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 return (int)*ptr;
2774}
2775
2776 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002777gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (has_mbyte)
2780 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 return (int)*ml_get_cursor();
2782}
2783
2784/*
2785 * Write a character at the current cursor position.
2786 * It is directly written into the block.
2787 */
2788 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002789pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2792 + curwin->w_cursor.col) = c;
2793}
2794
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795/*
2796 * When extra == 0: Return TRUE if the cursor is before or on the first
2797 * non-blank in the line.
2798 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2799 * the line.
2800 */
2801 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002802inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803{
2804 char_u *ptr;
2805 colnr_T col;
2806
Bram Moolenaar1c465442017-03-12 20:10:05 +01002807 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 ++ptr;
2809 if (col >= curwin->w_cursor.col + extra)
2810 return TRUE;
2811 else
2812 return FALSE;
2813}
2814
2815/*
2816 * Skip to next part of an option argument: Skip space and comma.
2817 */
2818 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002819skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
2821 if (*p == ',')
2822 ++p;
2823 while (*p == ' ')
2824 ++p;
2825 return p;
2826}
2827
2828/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002829 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 *
2831 * Most often called through changed_bytes() and changed_lines(), which also
2832 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002833 *
2834 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 */
2836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002837changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
2839#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002840 if (p_imst == IM_ON_THE_SPOT)
2841 {
2842 /* The text of the preediting area is inserted, but this doesn't
2843 * mean a change of the buffer yet. That is delayed until the
2844 * text is committed. (this means preedit becomes empty) */
2845 if (im_is_preediting() && !xim_changed_while_preediting)
2846 return;
2847 xim_changed_while_preediting = FALSE;
2848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#endif
2850
2851 if (!curbuf->b_changed)
2852 {
2853 int save_msg_scroll = msg_scroll;
2854
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002855 /* Give a warning about changing a read-only file. This may also
2856 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002858
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 /* Create a swap file if that is wanted.
2860 * Don't do this for "nofile" and "nowrite" buffer types. */
2861 if (curbuf->b_may_swap
2862#ifdef FEAT_QUICKFIX
2863 && !bt_dontwrite(curbuf)
2864#endif
2865 )
2866 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002867 int save_need_wait_return = need_wait_return;
2868
2869 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 ml_open_file(curbuf);
2871
2872 /* The ml_open_file() can cause an ATTENTION message.
2873 * Wait two seconds, to make sure the user reads this unexpected
2874 * message. Since we could be anywhere, call wait_return() now,
2875 * and don't let the emsg() set msg_scroll. */
2876 if (need_wait_return && emsg_silent == 0)
2877 {
2878 out_flush();
2879 ui_delay(2000L, TRUE);
2880 wait_return(TRUE);
2881 msg_scroll = save_msg_scroll;
2882 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002883 else
2884 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002886 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002888 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889}
2890
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002891/*
2892 * Internal part of changed(), no user interaction.
2893 */
2894 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002895changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002896{
2897 curbuf->b_changed = TRUE;
2898 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002899 check_status(curbuf);
2900 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002901#ifdef FEAT_TITLE
2902 need_maketitle = TRUE; /* set window title later */
2903#endif
2904}
2905
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002906static void changedOneline(buf_T *buf, linenr_T lnum);
2907static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2908static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
2910/*
2911 * Changed bytes within a single line for the current buffer.
2912 * - marks the windows on this buffer to be redisplayed
2913 * - marks the buffer changed by calling changed()
2914 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002915 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 */
2917 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002918changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002920 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002922
2923#ifdef FEAT_DIFF
2924 /* Diff highlighting in other diff windows may need to be updated too. */
2925 if (curwin->w_p_diff)
2926 {
2927 win_T *wp;
2928 linenr_T wlnum;
2929
Bram Moolenaar29323592016-07-24 22:04:11 +02002930 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002931 if (wp->w_p_diff && wp != curwin)
2932 {
2933 redraw_win_later(wp, VALID);
2934 wlnum = diff_lnum_win(lnum, wp);
2935 if (wlnum > 0)
2936 changedOneline(wp->w_buffer, wlnum);
2937 }
2938 }
2939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940}
2941
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002942/*
2943 * Like changed_bytes() but also adjust text properties for "added" bytes.
2944 * When "added" is negative text was deleted.
2945 */
2946 void
Bram Moolenaar402385a2019-01-11 14:10:03 +01002947inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002948{
2949 changed_bytes(lnum, col);
2950
2951#ifdef FEAT_TEXT_PROP
2952 if (curbuf->b_has_textprop && added != 0)
2953 adjust_prop_columns(lnum, col, added);
2954#endif
2955}
2956
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002958changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002960 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
2962 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002963 if (lnum < buf->b_mod_top)
2964 buf->b_mod_top = lnum;
2965 else if (lnum >= buf->b_mod_bot)
2966 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 }
2968 else
2969 {
2970 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002971 buf->b_mod_set = TRUE;
2972 buf->b_mod_top = lnum;
2973 buf->b_mod_bot = lnum + 1;
2974 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
2976}
2977
2978/*
2979 * Appended "count" lines below line "lnum" in the current buffer.
2980 * Must be called AFTER the change and after mark_adjust().
2981 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2982 */
2983 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002984appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
2986 changed_lines(lnum + 1, 0, lnum + 1, count);
2987}
2988
2989/*
2990 * Like appended_lines(), but adjust marks first.
2991 */
2992 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002993appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002995 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01002996 * be marks there. But it's still needed in diff mode. */
2997 if (lnum + count < curbuf->b_ml.ml_line_count
2998#ifdef FEAT_DIFF
2999 || curwin->w_p_diff
3000#endif
3001 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003002 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 changed_lines(lnum + 1, 0, lnum + 1, count);
3004}
3005
3006/*
3007 * Deleted "count" lines at line "lnum" in the current buffer.
3008 * Must be called AFTER the change and after mark_adjust().
3009 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3010 */
3011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003012deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 changed_lines(lnum, 0, lnum + count, -count);
3015}
3016
3017/*
3018 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003019 * Make sure the cursor is on a valid line before calling, a GUI callback may
3020 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 */
3022 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003023deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3026 changed_lines(lnum, 0, lnum + count, -count);
3027}
3028
3029/*
3030 * Changed lines for the current buffer.
3031 * Must be called AFTER the change and after mark_adjust().
3032 * - mark the buffer changed by calling changed()
3033 * - mark the windows on this buffer to be redisplayed
3034 * - invalidate cached values
3035 * "lnum" is the first line that needs displaying, "lnume" the first line
3036 * below the changed lines (BEFORE the change).
3037 * When only inserting lines, "lnum" and "lnume" are equal.
3038 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003039 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 */
3041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003042changed_lines(
3043 linenr_T lnum, /* first line with change */
3044 colnr_T col, /* column in first line with change */
3045 linenr_T lnume, /* line below last changed line */
3046 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003048 changed_lines_buf(curbuf, lnum, lnume, xtra);
3049
3050#ifdef FEAT_DIFF
Bram Moolenaare3521d92018-09-16 14:10:31 +02003051 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
Bram Moolenaardba8a912005-04-24 22:08:39 +00003052 {
3053 /* When the number of lines doesn't change then mark_adjust() isn't
3054 * called and other diff buffers still need to be marked for
3055 * displaying. */
3056 win_T *wp;
3057 linenr_T wlnum;
3058
Bram Moolenaar29323592016-07-24 22:04:11 +02003059 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003060 if (wp->w_p_diff && wp != curwin)
3061 {
3062 redraw_win_later(wp, VALID);
3063 wlnum = diff_lnum_win(lnum, wp);
3064 if (wlnum > 0)
3065 changed_lines_buf(wp->w_buffer, wlnum,
3066 lnume - lnum + wlnum, 0L);
3067 }
3068 }
3069#endif
3070
3071 changed_common(lnum, col, lnume, xtra);
3072}
3073
3074 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003075changed_lines_buf(
3076 buf_T *buf,
3077 linenr_T lnum, /* first line with change */
3078 linenr_T lnume, /* line below last changed line */
3079 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003080{
3081 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 {
3083 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003084 if (lnum < buf->b_mod_top)
3085 buf->b_mod_top = lnum;
3086 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 {
3088 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003089 buf->b_mod_bot += xtra;
3090 if (buf->b_mod_bot < lnum)
3091 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003093 if (lnume + xtra > buf->b_mod_bot)
3094 buf->b_mod_bot = lnume + xtra;
3095 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 }
3097 else
3098 {
3099 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003100 buf->b_mod_set = TRUE;
3101 buf->b_mod_top = lnum;
3102 buf->b_mod_bot = lnume + xtra;
3103 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105}
3106
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003107/*
3108 * Common code for when a change is was made.
3109 * See changed_lines() for the arguments.
3110 * Careful: may trigger autocommands that reload the buffer.
3111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003113changed_common(
3114 linenr_T lnum,
3115 colnr_T col,
3116 linenr_T lnume,
3117 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
3119 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003120 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 int i;
3122#ifdef FEAT_JUMPLIST
3123 int cols;
3124 pos_T *p;
3125 int add;
3126#endif
3127
3128 /* mark the buffer as modified */
3129 changed();
3130
Bram Moolenaare3521d92018-09-16 14:10:31 +02003131#ifdef FEAT_DIFF
3132 if (curwin->w_p_diff && diff_internal())
3133 curtab->tp_diff_update = TRUE;
3134#endif
3135
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 /* set the '. mark */
3137 if (!cmdmod.keepjumps)
3138 {
3139 curbuf->b_last_change.lnum = lnum;
3140 curbuf->b_last_change.col = col;
3141
3142#ifdef FEAT_JUMPLIST
3143 /* Create a new entry if a new undo-able change was started or we
3144 * don't have an entry yet. */
3145 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3146 {
3147 if (curbuf->b_changelistlen == 0)
3148 add = TRUE;
3149 else
3150 {
3151 /* Don't create a new entry when the line number is the same
3152 * as the last one and the column is not too far away. Avoids
3153 * creating many entries for typing "xxxxx". */
3154 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3155 if (p->lnum != lnum)
3156 add = TRUE;
3157 else
3158 {
3159 cols = comp_textwidth(FALSE);
3160 if (cols == 0)
3161 cols = 79;
3162 add = (p->col + cols < col || col + cols < p->col);
3163 }
3164 }
3165 if (add)
3166 {
3167 /* This is the first of a new sequence of undo-able changes
3168 * and it's at some distance of the last change. Use a new
3169 * position in the changelist. */
3170 curbuf->b_new_change = FALSE;
3171
3172 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3173 {
3174 /* changelist is full: remove oldest entry */
3175 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3176 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3177 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003178 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
3180 /* Correct position in changelist for other windows on
3181 * this buffer. */
3182 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3183 --wp->w_changelistidx;
3184 }
3185 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003186 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 /* For other windows, if the position in the changelist is
3189 * at the end it stays at the end. */
3190 if (wp->w_buffer == curbuf
3191 && wp->w_changelistidx == curbuf->b_changelistlen)
3192 ++wp->w_changelistidx;
3193 }
3194 ++curbuf->b_changelistlen;
3195 }
3196 }
3197 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3198 curbuf->b_last_change;
3199 /* The current window is always after the last change, so that "g,"
3200 * takes you back to it. */
3201 curwin->w_changelistidx = curbuf->b_changelistlen;
3202#endif
3203 }
3204
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003205 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 {
3207 if (wp->w_buffer == curbuf)
3208 {
3209 /* Mark this window to be redrawn later. */
3210 if (wp->w_redr_type < VALID)
3211 wp->w_redr_type = VALID;
3212
3213 /* Check if a change in the buffer has invalidated the cached
3214 * values for the cursor. */
3215#ifdef FEAT_FOLDING
3216 /*
3217 * Update the folds for this window. Can't postpone this, because
3218 * a following operator might work on the whole fold: ">>dd".
3219 */
3220 foldUpdate(wp, lnum, lnume + xtra - 1);
3221
3222 /* The change may cause lines above or below the change to become
3223 * included in a fold. Set lnum/lnume to the first/last line that
3224 * might be displayed differently.
3225 * Set w_cline_folded here as an efficient way to update it when
3226 * inserting lines just above a closed fold. */
3227 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3228 if (wp->w_cursor.lnum == lnum)
3229 wp->w_cline_folded = i;
3230 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3231 if (wp->w_cursor.lnum == lnume)
3232 wp->w_cline_folded = i;
3233
3234 /* If the changed line is in a range of previously folded lines,
3235 * compare with the first line in that range. */
3236 if (wp->w_cursor.lnum <= lnum)
3237 {
3238 i = find_wl_entry(wp, lnum);
3239 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3240 changed_line_abv_curs_win(wp);
3241 }
3242#endif
3243
3244 if (wp->w_cursor.lnum > lnum)
3245 changed_line_abv_curs_win(wp);
3246 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3247 changed_cline_bef_curs_win(wp);
3248 if (wp->w_botline >= lnum)
3249 {
3250 /* Assume that botline doesn't change (inserted lines make
3251 * other lines scroll down below botline). */
3252 approximate_botline_win(wp);
3253 }
3254
3255 /* Check if any w_lines[] entries have become invalid.
3256 * For entries below the change: Correct the lnums for
3257 * inserted/deleted lines. Makes it possible to stop displaying
3258 * after the change. */
3259 for (i = 0; i < wp->w_lines_valid; ++i)
3260 if (wp->w_lines[i].wl_valid)
3261 {
3262 if (wp->w_lines[i].wl_lnum >= lnum)
3263 {
3264 if (wp->w_lines[i].wl_lnum < lnume)
3265 {
3266 /* line included in change */
3267 wp->w_lines[i].wl_valid = FALSE;
3268 }
3269 else if (xtra != 0)
3270 {
3271 /* line below change */
3272 wp->w_lines[i].wl_lnum += xtra;
3273#ifdef FEAT_FOLDING
3274 wp->w_lines[i].wl_lastlnum += xtra;
3275#endif
3276 }
3277 }
3278#ifdef FEAT_FOLDING
3279 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3280 {
3281 /* change somewhere inside this range of folded lines,
3282 * may need to be redrawn */
3283 wp->w_lines[i].wl_valid = FALSE;
3284 }
3285#endif
3286 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003287
3288#ifdef FEAT_FOLDING
3289 /* Take care of side effects for setting w_topline when folds have
3290 * changed. Esp. when the buffer was changed in another window. */
3291 if (hasAnyFolding(wp))
3292 set_topline(wp, wp->w_topline);
3293#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003294 /* relative numbering may require updating more */
3295 if (wp->w_p_rnu)
3296 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
3298 }
3299
3300 /* Call update_screen() later, which checks out what needs to be redrawn,
3301 * since it notices b_mod_set and then uses b_mod_*. */
3302 if (must_redraw < VALID)
3303 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003304
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003305 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003306 if (lnum <= curwin->w_cursor.lnum
3307 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003308 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309}
3310
3311/*
3312 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3313 */
3314 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003315unchanged(
3316 buf_T *buf,
3317 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003319 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
3321 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003322 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (ff)
3324 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003326 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#ifdef FEAT_TITLE
3328 need_maketitle = TRUE; /* set window title later */
3329#endif
3330 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003331 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332#ifdef FEAT_NETBEANS_INTG
3333 netbeans_unmodified(buf);
3334#endif
3335}
3336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337/*
3338 * check_status: called when the status bars for the buffer 'buf'
3339 * need to be updated
3340 */
3341 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003342check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 win_T *wp;
3345
Bram Moolenaar29323592016-07-24 22:04:11 +02003346 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (wp->w_buffer == buf && wp->w_status_height)
3348 {
3349 wp->w_redr_status = TRUE;
3350 if (must_redraw < VALID)
3351 must_redraw = VALID;
3352 }
3353}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
3355/*
3356 * If the file is readonly, give a warning message with the first change.
3357 * Don't do this for autocommands.
3358 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003359 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003361 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 */
3363 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003364change_warning(
3365 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 mode and 'showmode' is on */
3367{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003368 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3369
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if (curbuf->b_did_warn == FALSE
3371 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 && curbuf->b_p_ro)
3374 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003375 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003377 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 if (!curbuf->b_p_ro)
3379 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 /*
3381 * Do what msg() does, but with a column offset if the warning should
3382 * be after the mode message.
3383 */
3384 msg_start();
3385 if (msg_row == Rows - 1)
3386 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003387 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003388 msg_puts_attr(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003389#ifdef FEAT_EVAL
3390 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 msg_clr_eos();
3393 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003394 if (msg_silent == 0 && !silent_mode
3395#ifdef FEAT_EVAL
3396 && time_for_testing != 1
3397#endif
3398 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 {
3400 out_flush();
3401 ui_delay(1000L, TRUE); /* give the user time to think about it */
3402 }
3403 curbuf->b_did_warn = TRUE;
3404 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3405 if (msg_row < Rows - 1)
3406 showmode();
3407 }
3408}
3409
3410/*
3411 * Ask for a reply from the user, a 'y' or a 'n'.
3412 * No other characters are accepted, the message is repeated until a valid
3413 * reply is entered or CTRL-C is hit.
3414 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3415 * from any buffers but directly from the user.
3416 *
3417 * return the 'y' or 'n'
3418 */
3419 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003420ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
3422 int r = ' ';
3423 int save_State = State;
3424
3425 if (exiting) /* put terminal in raw mode for this question */
3426 settmode(TMODE_RAW);
3427 ++no_wait_return;
3428#ifdef USE_ON_FLY_SCROLL
3429 dont_scroll = TRUE; /* disallow scrolling here */
3430#endif
3431 State = CONFIRM; /* mouse behaves like with :confirm */
3432#ifdef FEAT_MOUSE
3433 setmouse(); /* disables mouse for xterm */
3434#endif
3435 ++no_mapping;
3436 ++allow_keys; /* no mapping here, but recognize keys */
3437
3438 while (r != 'y' && r != 'n')
3439 {
3440 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003441 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (direct)
3443 r = get_keystroke();
3444 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003445 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (r == Ctrl_C || r == ESC)
3447 r = 'n';
3448 msg_putchar(r); /* show what you typed */
3449 out_flush();
3450 }
3451 --no_wait_return;
3452 State = save_State;
3453#ifdef FEAT_MOUSE
3454 setmouse();
3455#endif
3456 --no_mapping;
3457 --allow_keys;
3458
3459 return r;
3460}
3461
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003462#if defined(FEAT_MOUSE) || defined(PROTO)
3463/*
3464 * Return TRUE if "c" is a mouse key.
3465 */
3466 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003467is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003468{
3469 return c == K_LEFTMOUSE
3470 || c == K_LEFTMOUSE_NM
3471 || c == K_LEFTDRAG
3472 || c == K_LEFTRELEASE
3473 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003474 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003475 || c == K_MIDDLEMOUSE
3476 || c == K_MIDDLEDRAG
3477 || c == K_MIDDLERELEASE
3478 || c == K_RIGHTMOUSE
3479 || c == K_RIGHTDRAG
3480 || c == K_RIGHTRELEASE
3481 || c == K_MOUSEDOWN
3482 || c == K_MOUSEUP
3483 || c == K_MOUSELEFT
3484 || c == K_MOUSERIGHT
3485 || c == K_X1MOUSE
3486 || c == K_X1DRAG
3487 || c == K_X1RELEASE
3488 || c == K_X2MOUSE
3489 || c == K_X2DRAG
3490 || c == K_X2RELEASE;
3491}
3492#endif
3493
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494/*
3495 * Get a key stroke directly from the user.
3496 * Ignores mouse clicks and scrollbar events, except a click for the left
3497 * button (used at the more prompt).
3498 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3499 * Disadvantage: typeahead is ignored.
3500 * Translates the interrupt character for unix to ESC.
3501 */
3502 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003503get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003505 char_u *buf = NULL;
3506 int buflen = 150;
3507 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 int len = 0;
3509 int n;
3510 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003511 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513 mapped_ctrl_c = FALSE; /* mappings are not used here */
3514 for (;;)
3515 {
3516 cursor_on();
3517 out_flush();
3518
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003519 /* Leave some room for check_termcode() to insert a key code into (max
3520 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3521 * bytes. */
3522 maxlen = (buflen - 6 - len) / 3;
3523 if (buf == NULL)
3524 buf = alloc(buflen);
3525 else if (maxlen < 10)
3526 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003527 char_u *t_buf = buf;
3528
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003529 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003530 * escape sequence. */
3531 buflen += 100;
3532 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003533 if (buf == NULL)
3534 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003535 maxlen = (buflen - 6 - len) / 3;
3536 }
3537 if (buf == NULL)
3538 {
3539 do_outofmem_msg((long_u)buflen);
3540 return ESC; /* panic! */
3541 }
3542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003544 * terminal code to complete. */
3545 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 if (n > 0)
3547 {
3548 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003549 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003551 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003553 else if (len > 0)
3554 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
Bram Moolenaar4395a712006-09-05 18:57:57 +00003556 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003557 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003558 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003560
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003561 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003562 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003563 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003564 {
3565 /* Redrawing was postponed, do it now. */
3566 update_screen(0);
3567 setcursor(); /* put cursor back where it belongs */
3568 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003569 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003570 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003571 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003573 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 continue;
3575
3576 /* Handle modifier and/or special key code. */
3577 n = buf[0];
3578 if (n == K_SPECIAL)
3579 {
3580 n = TO_SPECIAL(buf[1], buf[2]);
3581 if (buf[1] == KS_MODIFIER
3582 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003583#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003584 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003585#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003586#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 || n == K_VER_SCROLLBAR
3588 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589#endif
3590 )
3591 {
3592 if (buf[1] == KS_MODIFIER)
3593 mod_mask = buf[2];
3594 len -= 3;
3595 if (len > 0)
3596 mch_memmove(buf, buf + 3, (size_t)len);
3597 continue;
3598 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003599 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 if (has_mbyte)
3602 {
3603 if (MB_BYTE2LEN(n) > len)
3604 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003605 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 n = (*mb_ptr2char)(buf);
3607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608#ifdef UNIX
3609 if (n == intr_char)
3610 n = ESC;
3611#endif
3612 break;
3613 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003614 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
3616 mapped_ctrl_c = save_mapped_ctrl_c;
3617 return n;
3618}
3619
3620/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003621 * Get a number from the user.
3622 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 */
3624 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003625get_number(
3626 int colon, /* allow colon to abort */
3627 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628{
3629 int n = 0;
3630 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003631 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003633 if (mouse_used != NULL)
3634 *mouse_used = FALSE;
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 /* When not printing messages, the user won't know what to type, return a
3637 * zero (as if CR was hit). */
3638 if (msg_silent != 0)
3639 return 0;
3640
3641#ifdef USE_ON_FLY_SCROLL
3642 dont_scroll = TRUE; /* disallow scrolling here */
3643#endif
3644 ++no_mapping;
3645 ++allow_keys; /* no mapping here, but recognize keys */
3646 for (;;)
3647 {
3648 windgoto(msg_row, msg_col);
3649 c = safe_vgetc();
3650 if (VIM_ISDIGIT(c))
3651 {
3652 n = n * 10 + c - '0';
3653 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003654 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 }
3656 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3657 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003658 if (typed > 0)
3659 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003660 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003661 --typed;
3662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003665#ifdef FEAT_MOUSE
3666 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3667 {
3668 *mouse_used = TRUE;
3669 n = mouse_row + 1;
3670 break;
3671 }
3672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 else if (n == 0 && c == ':' && colon)
3674 {
3675 stuffcharReadbuff(':');
3676 if (!exmode_active)
3677 cmdline_row = msg_row;
3678 skip_redraw = TRUE; /* skip redraw once */
3679 do_redraw = FALSE;
3680 break;
3681 }
3682 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3683 break;
3684 }
3685 --no_mapping;
3686 --allow_keys;
3687 return n;
3688}
3689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003690/*
3691 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003692 * When "mouse_used" is not NULL allow using the mouse and in that case return
3693 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003694 */
3695 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003696prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003697{
3698 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003699 int save_cmdline_row;
3700 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003701
3702 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003703 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003704 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003705 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003706 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003707
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003708 // Set the state such that text can be selected/copied/pasted and we still
3709 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
3710 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003711 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003712 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003713 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003714 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02003715#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003716 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003717 setmouse();
3718#endif
3719
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003720 i = get_number(TRUE, mouse_used);
3721 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003722 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003723 /* don't call wait_return() now */
3724 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003725 cmdline_row = msg_row - 1;
3726 need_wait_return = FALSE;
3727 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003728 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003729 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003730 else
3731 cmdline_row = save_cmdline_row;
3732 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003733#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003734 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003735 setmouse();
3736#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003738 return i;
3739}
3740
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003742msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
3744 long pn;
3745
3746 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3748 return;
3749
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003750 /* We don't want to overwrite another important message, but do overwrite
3751 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3752 * then "put" reports the last action. */
3753 if (keep_msg != NULL && !keep_msg_more)
3754 return;
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 if (n > 0)
3757 pn = n;
3758 else
3759 pn = -n;
3760
3761 if (pn > p_report)
3762 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003763 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003764 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003765 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003767 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003768 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003770 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
3771 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (msg(msg_buf))
3773 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003774 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003775 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
3777 }
3778}
3779
3780/*
3781 * flush map and typeahead buffers and give a warning for an error
3782 */
3783 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003784beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
3786 if (emsg_silent == 0)
3787 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003788 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003789 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791}
3792
3793/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003794 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 */
3796 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003797vim_beep(
3798 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003800#ifdef FEAT_EVAL
3801 called_vim_beep = TRUE;
3802#endif
3803
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (emsg_silent == 0)
3805 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003806 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3807 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003808#ifdef ELAPSED_FUNC
3809 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01003810 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003811
3812 /* Only beep once per half a second, otherwise a sequence of beeps
3813 * would freeze Vim. */
3814 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3815 {
3816 did_init = TRUE;
3817 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003819 if (p_vb
3820#ifdef FEAT_GUI
3821 /* While the GUI is starting up the termcap is set for
3822 * the GUI but the output still goes to a terminal. */
3823 && !(gui.in_use && gui.starting)
3824#endif
3825 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003826 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003827 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003828#ifdef FEAT_VTP
3829 /* No restore color information, refresh the screen. */
3830 if (has_vtp_working() != 0
3831# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003832 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003833# endif
3834 )
3835 {
3836 redraw_later(CLEAR);
3837 update_screen(0);
3838 redrawcmd();
3839 }
3840#endif
3841 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003842 else
3843 out_char(BELL);
3844#ifdef ELAPSED_FUNC
3845 }
3846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003848
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003849 /* When 'debug' contains "beep" produce a message. If we are sourcing
3850 * a script or executing a function give the user a hint where the beep
3851 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003852 if (vim_strchr(p_debug, 'e') != NULL)
3853 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003854 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003855 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
3858}
3859
3860/*
3861 * To get the "real" home directory:
3862 * - get value of $HOME
3863 * For Unix:
3864 * - go to that directory
3865 * - do mch_dirname() to get the real name of that directory.
3866 * This also works with mounts and links.
3867 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01003868 * For Windows:
3869 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 */
3871static char_u *homedir = NULL;
3872
3873 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003874init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
3876 char_u *var;
3877
Bram Moolenaar05159a02005-02-26 23:04:13 +00003878 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003879 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003880
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881#ifdef VMS
3882 var = mch_getenv((char_u *)"SYS$LOGIN");
3883#else
3884 var = mch_getenv((char_u *)"HOME");
3885#endif
3886
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887#ifdef WIN3264
3888 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003889 * Typically, $HOME is not defined on Windows, unless the user has
3890 * specifically defined it for Vim's sake. However, on Windows NT
3891 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3892 * each user. Try constructing $HOME from these.
3893 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003894 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003895 {
3896 char_u *homedrive, *homepath;
3897
3898 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3899 homepath = mch_getenv((char_u *)"HOMEPATH");
3900 if (homepath == NULL || *homepath == NUL)
3901 homepath = (char_u *)"\\";
3902 if (homedrive != NULL
3903 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3904 {
3905 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3906 if (NameBuff[0] != NUL)
3907 var = NameBuff;
3908 }
3909 }
3910
3911 if (var == NULL)
3912 var = mch_getenv((char_u *)"USERPROFILE");
3913
3914 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 * Weird but true: $HOME may contain an indirect reference to another
3916 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3917 * when $HOME is being set.
3918 */
3919 if (var != NULL && *var == '%')
3920 {
3921 char_u *p;
3922 char_u *exp;
3923
3924 p = vim_strchr(var + 1, '%');
3925 if (p != NULL)
3926 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003927 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 exp = mch_getenv(NameBuff);
3929 if (exp != NULL && *exp != NUL
3930 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3931 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003932 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
3935 }
3936 }
3937
Bram Moolenaar48340b62017-08-29 22:08:53 +02003938 if (var != NULL && *var == NUL) /* empty is same as not set */
3939 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaar05159a02005-02-26 23:04:13 +00003941 if (enc_utf8 && var != NULL)
3942 {
3943 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003944 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003945
3946 /* Convert from active codepage to UTF-8. Other conversions are
3947 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003948 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003949 if (pp != NULL)
3950 {
3951 homedir = pp;
3952 return;
3953 }
3954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 /*
3957 * Default home dir is C:/
3958 * Best assumption we can make in such a situation.
3959 */
3960 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003961 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 if (var != NULL)
3965 {
3966#ifdef UNIX
3967 /*
3968 * Change to the directory and get the actual path. This resolves
3969 * links. Don't do it when we can't return.
3970 */
3971 if (mch_dirname(NameBuff, MAXPATHL) == OK
3972 && mch_chdir((char *)NameBuff) == 0)
3973 {
3974 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3975 var = IObuff;
3976 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003977 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
3979#endif
3980 homedir = vim_strsave(var);
3981 }
3982}
3983
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003984#if defined(EXITFREE) || defined(PROTO)
3985 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003986free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003987{
3988 vim_free(homedir);
3989}
Bram Moolenaar24305862012-08-15 14:05:05 +02003990
3991# ifdef FEAT_CMDL_COMPL
3992 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003993free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003994{
3995 ga_clear_strings(&ga_users);
3996}
3997# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003998#endif
3999
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004001 * Call expand_env() and store the result in an allocated string.
4002 * This is not very memory efficient, this expects the result to be freed
4003 * again soon.
4004 */
4005 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004006expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004007{
4008 return expand_env_save_opt(src, FALSE);
4009}
4010
4011/*
4012 * Idem, but when "one" is TRUE handle the string as one file name, only
4013 * expand "~" at the start.
4014 */
4015 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004016expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004017{
4018 char_u *p;
4019
4020 p = alloc(MAXPATHL);
4021 if (p != NULL)
4022 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4023 return p;
4024}
4025
4026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 * Expand environment variable with path name.
4028 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004029 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 * If anything fails no expansion is done and dst equals src.
4031 */
4032 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004033expand_env(
4034 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4035 char_u *dst, /* where to put the result */
4036 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004038 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039}
4040
4041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004042expand_env_esc(
4043 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4044 char_u *dst, /* where to put the result */
4045 int dstlen, /* maximum length of the result */
4046 int esc, /* escape spaces in expanded variables */
4047 int one, /* "srcp" is one file name */
4048 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004050 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 char_u *tail;
4052 int c;
4053 char_u *var;
4054 int copy_char;
4055 int mustfree; /* var was allocated, need to free it later */
4056 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004057 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004059 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004060 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004061
4062 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 --dstlen; /* leave one char space for "\," */
4064 while (*src && dstlen > 0)
4065 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004066#ifdef FEAT_EVAL
4067 /* Skip over `=expr`. */
4068 if (src[0] == '`' && src[1] == '=')
4069 {
4070 size_t len;
4071
4072 var = src;
4073 src += 2;
4074 (void)skip_expr(&src);
4075 if (*src == '`')
4076 ++src;
4077 len = src - var;
4078 if (len > (size_t)dstlen)
4079 len = dstlen;
4080 vim_strncpy(dst, var, len);
4081 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004082 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004083 continue;
4084 }
4085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004087 if ((*src == '$'
4088#ifdef VMS
4089 && at_start
4090#endif
4091 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004092#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 || *src == '%'
4094#endif
4095 || (*src == '~' && at_start))
4096 {
4097 mustfree = FALSE;
4098
4099 /*
4100 * The variable name is copied into dst temporarily, because it may
4101 * be a string in read-only memory and a NUL needs to be appended.
4102 */
4103 if (*src != '~') /* environment var */
4104 {
4105 tail = src + 1;
4106 var = dst;
4107 c = dstlen - 1;
4108
4109#ifdef UNIX
4110 /* Unix has ${var-name} type environment vars */
4111 if (*tail == '{' && !vim_isIDc('{'))
4112 {
4113 tail++; /* ignore '{' */
4114 while (c-- > 0 && *tail && *tail != '}')
4115 *var++ = *tail++;
4116 }
4117 else
4118#endif
4119 {
4120 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004121#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 || (*src == '%' && *tail != '%')
4123#endif
4124 ))
4125 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128 }
4129
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004130#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131# ifdef UNIX
4132 if (src[1] == '{' && *tail != '}')
4133# else
4134 if (*src == '%' && *tail != '%')
4135# endif
4136 var = NULL;
4137 else
4138 {
4139# ifdef UNIX
4140 if (src[1] == '{')
4141# else
4142 if (*src == '%')
4143#endif
4144 ++tail;
4145#endif
4146 *var = NUL;
4147 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004148#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
4150#endif
4151 }
4152 /* home directory */
4153 else if ( src[1] == NUL
4154 || vim_ispathsep(src[1])
4155 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4156 {
4157 var = homedir;
4158 tail = src + 1;
4159 }
4160 else /* user directory */
4161 {
4162#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4163 /*
4164 * Copy ~user to dst[], so we can put a NUL after it.
4165 */
4166 tail = src;
4167 var = dst;
4168 c = dstlen - 1;
4169 while ( c-- > 0
4170 && *tail
4171 && vim_isfilec(*tail)
4172 && !vim_ispathsep(*tail))
4173 *var++ = *tail++;
4174 *var = NUL;
4175# ifdef UNIX
4176 /*
4177 * If the system supports getpwnam(), use it.
4178 * Otherwise, or if getpwnam() fails, the shell is used to
4179 * expand ~user. This is slower and may fail if the shell
4180 * does not support ~user (old versions of /bin/sh).
4181 */
4182# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4183 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004184 /* Note: memory allocated by getpwnam() is never freed.
4185 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004186 struct passwd *pw = (*dst == NUL)
4187 ? NULL : getpwnam((char *)dst + 1);
4188
4189 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 if (var == NULL)
4192# endif
4193 {
4194 expand_T xpc;
4195
4196 ExpandInit(&xpc);
4197 xpc.xp_context = EXPAND_FILES;
4198 var = ExpandOne(&xpc, dst, NULL,
4199 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 mustfree = TRUE;
4201 }
4202
4203# else /* !UNIX, thus VMS */
4204 /*
4205 * USER_HOME is a comma-separated list of
4206 * directories to search for the user account in.
4207 */
4208 {
4209 char_u test[MAXPATHL], paths[MAXPATHL];
4210 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004211 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
4213 STRCPY(paths, USER_HOME);
4214 next_path = paths;
4215 while (*next_path)
4216 {
4217 for (path = next_path; *next_path && *next_path != ',';
4218 next_path++);
4219 if (*next_path)
4220 *next_path++ = NUL;
4221 STRCPY(test, path);
4222 STRCAT(test, "/");
4223 STRCAT(test, dst + 1);
4224 if (mch_stat(test, &st) == 0)
4225 {
4226 var = alloc(STRLEN(test) + 1);
4227 STRCPY(var, test);
4228 mustfree = TRUE;
4229 break;
4230 }
4231 }
4232 }
4233# endif /* UNIX */
4234#else
4235 /* cannot expand user's home directory, so don't try */
4236 var = NULL;
4237 tail = (char_u *)""; /* for gcc */
4238#endif /* UNIX || VMS */
4239 }
4240
4241#ifdef BACKSLASH_IN_FILENAME
4242 /* If 'shellslash' is set change backslashes to forward slashes.
4243 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4244 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4245 {
4246 char_u *p = vim_strsave(var);
4247
4248 if (p != NULL)
4249 {
4250 if (mustfree)
4251 vim_free(var);
4252 var = p;
4253 mustfree = TRUE;
4254 forward_slash(var);
4255 }
4256 }
4257#endif
4258
4259 /* If "var" contains white space, escape it with a backslash.
4260 * Required for ":e ~/tt" when $HOME includes a space. */
4261 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4262 {
4263 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4264
4265 if (p != NULL)
4266 {
4267 if (mustfree)
4268 vim_free(var);
4269 var = p;
4270 mustfree = TRUE;
4271 }
4272 }
4273
4274 if (var != NULL && *var != NUL
4275 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4276 {
4277 STRCPY(dst, var);
4278 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004279 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 /* if var[] ends in a path separator and tail[] starts
4281 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004282 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4284 && dst[-1] != ':'
4285#endif
4286 && vim_ispathsep(*tail))
4287 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004288 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 src = tail;
4290 copy_char = FALSE;
4291 }
4292 if (mustfree)
4293 vim_free(var);
4294 }
4295
4296 if (copy_char) /* copy at least one char */
4297 {
4298 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004299 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004300 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4301 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 */
4303 at_start = FALSE;
4304 if (src[0] == '\\' && src[1] != NUL)
4305 {
4306 *dst++ = *src++;
4307 --dstlen;
4308 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004309 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004311 if (dstlen > 0)
4312 {
4313 *dst++ = *src++;
4314 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004315
Bram Moolenaar1c864092017-08-06 18:15:45 +02004316 if (startstr != NULL && src - startstr_len >= srcp
4317 && STRNCMP(src - startstr_len, startstr,
4318 startstr_len) == 0)
4319 at_start = TRUE;
4320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004322
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 *dst = NUL;
4325}
4326
4327/*
4328 * Vim's version of getenv().
4329 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004330 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004331 * "mustfree" is set to TRUE when returned is allocated, it must be
4332 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 */
4334 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004335vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
4337 char_u *p;
4338 char_u *pend;
4339 int vimruntime;
4340
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004341#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 /* use "C:/" when $HOME is not set */
4343 if (STRCMP(name, "HOME") == 0)
4344 return homedir;
4345#endif
4346
4347 p = mch_getenv(name);
4348 if (p != NULL && *p == NUL) /* empty is the same as not set */
4349 p = NULL;
4350
4351 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004352 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004353#if defined(WIN3264)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004354 if (enc_utf8)
4355 {
4356 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004357 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004358
4359 /* Convert from active codepage to UTF-8. Other conversions are
4360 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004361 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004362 if (pp != NULL)
4363 {
4364 p = pp;
4365 *mustfree = TRUE;
4366 }
4367 }
4368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371
4372 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4373 if (!vimruntime && STRCMP(name, "VIM") != 0)
4374 return NULL;
4375
4376 /*
4377 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4378 * Don't do this when default_vimruntime_dir is non-empty.
4379 */
4380 if (vimruntime
4381#ifdef HAVE_PATHDEF
4382 && *default_vimruntime_dir == NUL
4383#endif
4384 )
4385 {
4386 p = mch_getenv((char_u *)"VIM");
4387 if (p != NULL && *p == NUL) /* empty is the same as not set */
4388 p = NULL;
4389 if (p != NULL)
4390 {
4391 p = vim_version_dir(p);
4392 if (p != NULL)
4393 *mustfree = TRUE;
4394 else
4395 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004396
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004397#if defined(WIN3264)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004398 if (enc_utf8)
4399 {
4400 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004401 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004402
4403 /* Convert from active codepage to UTF-8. Other conversions
4404 * are not done, because they would fail for non-ASCII
4405 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004406 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004407 if (pp != NULL)
4408 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004409 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004410 vim_free(p);
4411 p = pp;
4412 *mustfree = TRUE;
4413 }
4414 }
4415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
4417 }
4418
4419 /*
4420 * When expanding $VIM or $VIMRUNTIME fails, try using:
4421 * - the directory name from 'helpfile' (unless it contains '$')
4422 * - the executable name from argv[0]
4423 */
4424 if (p == NULL)
4425 {
4426 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4427 p = p_hf;
4428#ifdef USE_EXE_NAME
4429 /*
4430 * Use the name of the executable, obtained from argv[0].
4431 */
4432 else
4433 p = exe_name;
4434#endif
4435 if (p != NULL)
4436 {
4437 /* remove the file name */
4438 pend = gettail(p);
4439
4440 /* remove "doc/" from 'helpfile', if present */
4441 if (p == p_hf)
4442 pend = remove_tail(p, pend, (char_u *)"doc");
4443
4444#ifdef USE_EXE_NAME
4445# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004446 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 if (p == exe_name)
4448 {
4449 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004450 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004452 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4453 if (pend1 != pend)
4454 {
4455 pnew = alloc((unsigned)(pend1 - p) + 15);
4456 if (pnew != NULL)
4457 {
4458 STRNCPY(pnew, p, (pend1 - p));
4459 STRCPY(pnew + (pend1 - p), "Resources/vim");
4460 p = pnew;
4461 pend = p + STRLEN(p);
4462 }
4463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465# endif
4466 /* remove "src/" from exe_name, if present */
4467 if (p == exe_name)
4468 pend = remove_tail(p, pend, (char_u *)"src");
4469#endif
4470
4471 /* for $VIM, remove "runtime/" or "vim54/", if present */
4472 if (!vimruntime)
4473 {
4474 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4475 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4476 }
4477
4478 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004479 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004482#ifdef MACOS_X
4483 if (p == exe_name || p == p_hf)
4484#endif
4485 /* check that the result is a directory name */
4486 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487
4488 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004489 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 else
4491 {
4492#ifdef USE_EXE_NAME
4493 /* may add "/vim54" or "/runtime" if it exists */
4494 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4495 {
4496 vim_free(p);
4497 p = pend;
4498 }
4499#endif
4500 *mustfree = TRUE;
4501 }
4502 }
4503 }
4504
4505#ifdef HAVE_PATHDEF
4506 /* When there is a pathdef.c file we can use default_vim_dir and
4507 * default_vimruntime_dir */
4508 if (p == NULL)
4509 {
4510 /* Only use default_vimruntime_dir when it is not empty */
4511 if (vimruntime && *default_vimruntime_dir != NUL)
4512 {
4513 p = default_vimruntime_dir;
4514 *mustfree = FALSE;
4515 }
4516 else if (*default_vim_dir != NUL)
4517 {
4518 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4519 *mustfree = TRUE;
4520 else
4521 {
4522 p = default_vim_dir;
4523 *mustfree = FALSE;
4524 }
4525 }
4526 }
4527#endif
4528
4529 /*
4530 * Set the environment variable, so that the new value can be found fast
4531 * next time, and others can also use it (e.g. Perl).
4532 */
4533 if (p != NULL)
4534 {
4535 if (vimruntime)
4536 {
4537 vim_setenv((char_u *)"VIMRUNTIME", p);
4538 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540 else
4541 {
4542 vim_setenv((char_u *)"VIM", p);
4543 didset_vim = TRUE;
4544 }
4545 }
4546 return p;
4547}
4548
4549/*
4550 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4551 * Return NULL if not, return its name in allocated memory otherwise.
4552 */
4553 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004554vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555{
4556 char_u *p;
4557
4558 if (vimdir == NULL || *vimdir == NUL)
4559 return NULL;
4560 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4561 if (p != NULL && mch_isdir(p))
4562 return p;
4563 vim_free(p);
4564 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4565 if (p != NULL && mch_isdir(p))
4566 return p;
4567 vim_free(p);
4568 return NULL;
4569}
4570
4571/*
4572 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4573 * the length of "name/". Otherwise return "pend".
4574 */
4575 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004576remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577{
4578 int len = (int)STRLEN(name) + 1;
4579 char_u *newend = pend - len;
4580
4581 if (newend >= p
4582 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004583 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 return newend;
4585 return pend;
4586}
4587
Bram Moolenaar113e1072019-01-20 15:30:40 +01004588#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02004589 void
4590vim_unsetenv(char_u *var)
4591{
4592#ifdef HAVE_UNSETENV
4593 unsetenv((char *)var);
4594#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004595 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004596#endif
4597}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004598#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02004599
4600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 * Our portable version of setenv.
4603 */
4604 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004605vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606{
4607#ifdef HAVE_SETENV
4608 mch_setenv((char *)name, (char *)val, 1);
4609#else
4610 char_u *envbuf;
4611
4612 /*
4613 * Putenv does not copy the string, it has to remain
4614 * valid. The allocated memory will never be freed.
4615 */
4616 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4617 if (envbuf != NULL)
4618 {
4619 sprintf((char *)envbuf, "%s=%s", name, val);
4620 putenv((char *)envbuf);
4621 }
4622#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004623#ifdef FEAT_GETTEXT
4624 /*
4625 * When setting $VIMRUNTIME adjust the directory to find message
4626 * translations to $VIMRUNTIME/lang.
4627 */
4628 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4629 {
4630 char_u *buf = concat_str(val, (char_u *)"/lang");
4631
4632 if (buf != NULL)
4633 {
4634 bindtextdomain(VIMPACKAGE, (char *)buf);
4635 vim_free(buf);
4636 }
4637 }
4638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639}
4640
4641#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4642/*
4643 * Function given to ExpandGeneric() to obtain an environment variable name.
4644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004646get_env_name(
4647 expand_T *xp UNUSED,
4648 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649{
Bram Moolenaard0573012017-10-28 21:11:06 +02004650# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004652 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 */
4654 return NULL;
4655# else
4656# ifndef __WIN32__
4657 /* Borland C++ 5.2 has this in a header file. */
4658 extern char **environ;
4659# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004660# define ENVNAMELEN 100
4661 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 char_u *str;
4663 int n;
4664
4665 str = (char_u *)environ[idx];
4666 if (str == NULL)
4667 return NULL;
4668
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004669 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
4671 if (str[n] == '=' || str[n] == NUL)
4672 break;
4673 name[n] = str[n];
4674 }
4675 name[n] = NUL;
4676 return name;
4677# endif
4678}
Bram Moolenaar24305862012-08-15 14:05:05 +02004679
4680/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004681 * Add a user name to the list of users in ga_users.
4682 * Do nothing if user name is NULL or empty.
4683 */
4684 static void
4685add_user(char_u *user, int need_copy)
4686{
4687 char_u *user_copy = (user != NULL && need_copy)
4688 ? vim_strsave(user) : user;
4689
4690 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4691 {
4692 if (need_copy)
4693 vim_free(user);
4694 return;
4695 }
4696 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4697}
4698
4699/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004700 * Find all user names for user completion.
4701 * Done only once and then cached.
4702 */
4703 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004704init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004705{
Bram Moolenaar24305862012-08-15 14:05:05 +02004706 static int lazy_init_done = FALSE;
4707
4708 if (lazy_init_done)
4709 return;
4710
4711 lazy_init_done = TRUE;
4712 ga_init2(&ga_users, sizeof(char_u *), 20);
4713
4714# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4715 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004716 struct passwd* pw;
4717
4718 setpwent();
4719 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004720 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004721 endpwent();
4722 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004723# elif defined(WIN3264)
4724 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004725 DWORD nusers = 0, ntotal = 0, i;
4726 PUSER_INFO_0 uinfo;
4727
4728 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4729 &nusers, &ntotal, NULL) == NERR_Success)
4730 {
4731 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004732 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004733
4734 NetApiBufferFree(uinfo);
4735 }
4736 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004737# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004738# if defined(HAVE_GETPWNAM)
4739 {
4740 char_u *user_env = mch_getenv((char_u *)"USER");
4741
4742 // The $USER environment variable may be a valid remote user name (NIS,
4743 // LDAP) not already listed by getpwent(), as getpwent() only lists
4744 // local user names. If $USER is not already listed, check whether it
4745 // is a valid remote user name using getpwnam() and if it is, add it to
4746 // the list of user names.
4747
4748 if (user_env != NULL && *user_env != NUL)
4749 {
4750 int i;
4751
4752 for (i = 0; i < ga_users.ga_len; i++)
4753 {
4754 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4755
4756 if (STRCMP(local_user, user_env) == 0)
4757 break;
4758 }
4759
4760 if (i == ga_users.ga_len)
4761 {
4762 struct passwd *pw = getpwnam((char *)user_env);
4763
4764 if (pw != NULL)
4765 add_user((char_u *)pw->pw_name, TRUE);
4766 }
4767 }
4768 }
4769# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004770}
4771
4772/*
4773 * Function given to ExpandGeneric() to obtain an user names.
4774 */
4775 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004776get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004777{
4778 init_users();
4779 if (idx < ga_users.ga_len)
4780 return ((char_u **)ga_users.ga_data)[idx];
4781 return NULL;
4782}
4783
4784/*
4785 * Check whether name matches a user name. Return:
4786 * 0 if name does not match any user name.
4787 * 1 if name partially matches the beginning of a user name.
4788 * 2 is name fully matches a user name.
4789 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004790 int
4791match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004792{
4793 int i;
4794 int n = (int)STRLEN(name);
4795 int result = 0;
4796
4797 init_users();
4798 for (i = 0; i < ga_users.ga_len; i++)
4799 {
4800 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4801 return 2; /* full match */
4802 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4803 result = 1; /* partial match */
4804 }
4805 return result;
4806}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807#endif
4808
4809/*
4810 * Replace home directory by "~" in each space or comma separated file name in
4811 * 'src'.
4812 * If anything fails (except when out of space) dst equals src.
4813 */
4814 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004815home_replace(
4816 buf_T *buf, /* when not NULL, check for help files */
4817 char_u *src, /* input file name */
4818 char_u *dst, /* where to put the result */
4819 int dstlen, /* maximum length of the result */
4820 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 spaces and commas in the file name. */
4822{
4823 size_t dirlen = 0, envlen = 0;
4824 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004825 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 char_u *p;
4827
4828 if (src == NULL)
4829 {
4830 *dst = NUL;
4831 return;
4832 }
4833
4834 /*
4835 * If the file is a help file, remove the path completely.
4836 */
4837 if (buf != NULL && buf->b_help)
4838 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004839 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 return;
4841 }
4842
4843 /*
4844 * We check both the value of the $HOME environment variable and the
4845 * "real" home directory.
4846 */
4847 if (homedir != NULL)
4848 dirlen = STRLEN(homedir);
4849
4850#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004851 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004853 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4854#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004855#ifdef WIN3264
4856 if (homedir_env == NULL)
4857 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4858#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004859 /* Empty is the same as not set. */
4860 if (homedir_env != NULL && *homedir_env == NUL)
4861 homedir_env = NULL;
4862
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004863#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaaref0a1d52018-12-30 11:38:57 +01004864 if (homedir_env != NULL && *homedir_env == '~')
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004865 {
4866 int usedlen = 0;
4867 int flen;
4868 char_u *fbuf = NULL;
4869
4870 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004871 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004872 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004873 flen = (int)STRLEN(homedir_env);
4874 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4875 /* Remove the trailing / that is added to a directory. */
4876 homedir_env[flen - 1] = NUL;
4877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878#endif
4879
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if (homedir_env != NULL)
4881 envlen = STRLEN(homedir_env);
4882
4883 if (!one)
4884 src = skipwhite(src);
4885 while (*src && dstlen > 0)
4886 {
4887 /*
4888 * Here we are at the beginning of a file name.
4889 * First, check to see if the beginning of the file name matches
4890 * $HOME or the "real" home directory. Check that there is a '/'
4891 * after the match (so that if e.g. the file is "/home/pieter/bla",
4892 * and the home directory is "/home/piet", the file does not end up
4893 * as "~er/bla" (which would seem to indicate the file "bla" in user
4894 * er's home directory)).
4895 */
4896 p = homedir;
4897 len = dirlen;
4898 for (;;)
4899 {
4900 if ( len
4901 && fnamencmp(src, p, len) == 0
4902 && (vim_ispathsep(src[len])
4903 || (!one && (src[len] == ',' || src[len] == ' '))
4904 || src[len] == NUL))
4905 {
4906 src += len;
4907 if (--dstlen > 0)
4908 *dst++ = '~';
4909
4910 /*
4911 * If it's just the home directory, add "/".
4912 */
4913 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4914 *dst++ = '/';
4915 break;
4916 }
4917 if (p == homedir_env)
4918 break;
4919 p = homedir_env;
4920 len = envlen;
4921 }
4922
4923 /* if (!one) skip to separator: space or comma */
4924 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4925 *dst++ = *src++;
4926 /* skip separator */
4927 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4928 *dst++ = *src++;
4929 }
4930 /* if (dstlen == 0) out of space, what to do??? */
4931
4932 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004933
4934 if (homedir_env != homedir_env_orig)
4935 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936}
4937
4938/*
4939 * Like home_replace, store the replaced string in allocated memory.
4940 * When something fails, NULL is returned.
4941 */
4942 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004943home_replace_save(
4944 buf_T *buf, /* when not NULL, check for help files */
4945 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 char_u *dst;
4948 unsigned len;
4949
4950 len = 3; /* space for "~/" and trailing NUL */
4951 if (src != NULL) /* just in case */
4952 len += (unsigned)STRLEN(src);
4953 dst = alloc(len);
4954 if (dst != NULL)
4955 home_replace(buf, src, dst, len, TRUE);
4956 return dst;
4957}
4958
4959/*
4960 * Compare two file names and return:
4961 * FPC_SAME if they both exist and are the same file.
4962 * FPC_SAMEX if they both don't exist and have the same file name.
4963 * FPC_DIFF if they both exist and are different files.
4964 * FPC_NOTX if they both don't exist.
4965 * FPC_DIFFX if one of them doesn't exist.
4966 * For the first name environment variables are expanded
4967 */
4968 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004969fullpathcmp(
4970 char_u *s1,
4971 char_u *s2,
4972 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
4974#ifdef UNIX
4975 char_u exp1[MAXPATHL];
4976 char_u full1[MAXPATHL];
4977 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004978 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 int r1, r2;
4980
4981 expand_env(s1, exp1, MAXPATHL);
4982 r1 = mch_stat((char *)exp1, &st1);
4983 r2 = mch_stat((char *)s2, &st2);
4984 if (r1 != 0 && r2 != 0)
4985 {
4986 /* if mch_stat() doesn't work, may compare the names */
4987 if (checkname)
4988 {
4989 if (fnamecmp(exp1, s2) == 0)
4990 return FPC_SAMEX;
4991 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4992 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4993 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4994 return FPC_SAMEX;
4995 }
4996 return FPC_NOTX;
4997 }
4998 if (r1 != 0 || r2 != 0)
4999 return FPC_DIFFX;
5000 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5001 return FPC_SAME;
5002 return FPC_DIFF;
5003#else
5004 char_u *exp1; /* expanded s1 */
5005 char_u *full1; /* full path of s1 */
5006 char_u *full2; /* full path of s2 */
5007 int retval = FPC_DIFF;
5008 int r1, r2;
5009
5010 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5011 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5012 {
5013 full1 = exp1 + MAXPATHL;
5014 full2 = full1 + MAXPATHL;
5015
5016 expand_env(s1, exp1, MAXPATHL);
5017 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5018 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5019
5020 /* If vim_FullName() fails, the file probably doesn't exist. */
5021 if (r1 != OK && r2 != OK)
5022 {
5023 if (checkname && fnamecmp(exp1, s2) == 0)
5024 retval = FPC_SAMEX;
5025 else
5026 retval = FPC_NOTX;
5027 }
5028 else if (r1 != OK || r2 != OK)
5029 retval = FPC_DIFFX;
5030 else if (fnamecmp(full1, full2))
5031 retval = FPC_DIFF;
5032 else
5033 retval = FPC_SAME;
5034 vim_free(exp1);
5035 }
5036 return retval;
5037#endif
5038}
5039
5040/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005041 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005042 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005043 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 */
5045 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005046gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047{
5048 char_u *p1, *p2;
5049
5050 if (fname == NULL)
5051 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005052 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005054 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005056 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058 return p1;
5059}
5060
Bram Moolenaar31710262010-08-13 13:36:15 +02005061#if defined(FEAT_SEARCHPATH)
Bram Moolenaar31710262010-08-13 13:36:15 +02005062/*
5063 * Return the end of the directory name, on the first path
5064 * separator:
5065 * "/path/file", "/path/dir/", "/path//dir", "/file"
5066 * ^ ^ ^ ^
5067 */
5068 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005069gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005070{
5071 char_u *dir_end = fname;
5072 char_u *next_dir_end = fname;
5073 int look_for_sep = TRUE;
5074 char_u *p;
5075
5076 for (p = fname; *p != NUL; )
5077 {
5078 if (vim_ispathsep(*p))
5079 {
5080 if (look_for_sep)
5081 {
5082 next_dir_end = p;
5083 look_for_sep = FALSE;
5084 }
5085 }
5086 else
5087 {
5088 if (!look_for_sep)
5089 dir_end = next_dir_end;
5090 look_for_sep = TRUE;
5091 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005092 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005093 }
5094 return dir_end;
5095}
5096#endif
5097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005099 * Get pointer to tail of "fname", including path separators. Putting a NUL
5100 * here leaves the directory name. Takes care of "c:/" and "//".
5101 * Always returns a valid pointer.
5102 */
5103 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005104gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005105{
5106 char_u *p;
5107 char_u *t;
5108
5109 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5110 t = gettail(fname);
5111 while (t > p && after_pathsep(fname, t))
5112 --t;
5113#ifdef VMS
5114 /* path separator is part of the path */
5115 ++t;
5116#endif
5117 return t;
5118}
5119
5120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 * get the next path component (just after the next path separator).
5122 */
5123 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005124getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125{
5126 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005127 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (*fname)
5129 ++fname;
5130 return fname;
5131}
5132
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133/*
5134 * Get a pointer to one character past the head of a path name.
5135 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5136 * If there is no head, path is returned.
5137 */
5138 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005139get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140{
5141 char_u *retval;
5142
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005143#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 /* may skip "c:" */
5145 if (isalpha(path[0]) && path[1] == ':')
5146 retval = path + 2;
5147 else
5148 retval = path;
5149#else
5150# if defined(AMIGA)
5151 /* may skip "label:" */
5152 retval = vim_strchr(path, ':');
5153 if (retval == NULL)
5154 retval = path;
5155# else /* Unix */
5156 retval = path;
5157# endif
5158#endif
5159
5160 while (vim_ispathsep(*retval))
5161 ++retval;
5162
5163 return retval;
5164}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
5166/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005167 * Return TRUE if 'c' is a path separator.
5168 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 */
5170 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005171vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005173#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005175#else
5176# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005178# else
5179# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5181 return (c == ':' || c == '[' || c == ']' || c == '/'
5182 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005183# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005185# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188}
5189
Bram Moolenaar69c35002013-11-04 02:54:12 +01005190/*
5191 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5192 */
5193 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005194vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005195{
5196 return vim_ispathsep(c)
5197#ifdef BACKSLASH_IN_FILENAME
5198 && c != ':'
5199#endif
5200 ;
5201}
5202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5204/*
5205 * return TRUE if 'c' is a path list separator.
5206 */
5207 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005208vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209{
5210#ifdef UNIX
5211 return (c == ':');
5212#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005213 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214#endif
5215}
5216#endif
5217
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005218/*
5219 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5220 * It's done in-place.
5221 */
5222 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005223shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005224{
5225 char_u *tail, *s, *d;
5226 int skip = FALSE;
5227
5228 tail = gettail(str);
5229 d = str;
5230 for (s = str; ; ++s)
5231 {
5232 if (s >= tail) /* copy the whole tail */
5233 {
5234 *d++ = *s;
5235 if (*s == NUL)
5236 break;
5237 }
5238 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5239 {
5240 *d++ = *s;
5241 skip = FALSE;
5242 }
5243 else if (!skip)
5244 {
5245 *d++ = *s; /* copy next char */
5246 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5247 skip = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005248 if (has_mbyte)
5249 {
5250 int l = mb_ptr2len(s);
5251
5252 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005253 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005254 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005255 }
5256 }
5257}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005258
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005259/*
5260 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5261 * Also returns TRUE if there is no directory name.
5262 * "fname" must be writable!.
5263 */
5264 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005265dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005266{
5267 char_u *p;
5268 int c;
5269 int retval;
5270
5271 p = gettail_sep(fname);
5272 if (p == fname)
5273 return TRUE;
5274 c = *p;
5275 *p = NUL;
5276 retval = mch_isdir(fname);
5277 *p = c;
5278 return retval;
5279}
5280
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005282 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5283 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 */
5285 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005286vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005288#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005290#else
5291 if (p_fic)
5292 return MB_STRICMP(x, y);
5293 return STRCMP(x, y);
5294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295}
5296
5297 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005298vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005300#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005301 char_u *px = x;
5302 char_u *py = y;
5303 int cx = NUL;
5304 int cy = NUL;
5305
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005306 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005308 cx = PTR2CHAR(px);
5309 cy = PTR2CHAR(py);
5310 if (cx == NUL || cy == NUL
5311 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5312 && !(cx == '/' && cy == '\\')
5313 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005315 len -= MB_PTR2LEN(px);
5316 px += MB_PTR2LEN(px);
5317 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
5319 if (len == 0)
5320 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005321 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005322#else
5323 if (p_fic)
5324 return MB_STRNICMP(x, y, len);
5325 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005327}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329/*
5330 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005331 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 */
5333 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005334concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
5336 char_u *dest;
5337
5338 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5339 if (dest != NULL)
5340 {
5341 STRCPY(dest, fname1);
5342 if (sep)
5343 add_pathsep(dest);
5344 STRCAT(dest, fname2);
5345 }
5346 return dest;
5347}
5348
Bram Moolenaard6754642005-01-17 22:18:45 +00005349/*
5350 * Concatenate two strings and return the result in allocated memory.
5351 * Returns NULL when out of memory.
5352 */
5353 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005354concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005355{
5356 char_u *dest;
5357 size_t l = STRLEN(str1);
5358
5359 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5360 if (dest != NULL)
5361 {
5362 STRCPY(dest, str1);
5363 STRCPY(dest + l, str2);
5364 }
5365 return dest;
5366}
Bram Moolenaard6754642005-01-17 22:18:45 +00005367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368/*
5369 * Add a path separator to a file name, unless it already ends in a path
5370 * separator.
5371 */
5372 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005373add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005375 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 STRCAT(p, PATHSEPSTR);
5377}
5378
5379/*
5380 * FullName_save - Make an allocated copy of a full file name.
5381 * Returns NULL when out of memory.
5382 */
5383 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005384FullName_save(
5385 char_u *fname,
5386 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005387 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389 char_u *buf;
5390 char_u *new_fname = NULL;
5391
5392 if (fname == NULL)
5393 return NULL;
5394
5395 buf = alloc((unsigned)MAXPATHL);
5396 if (buf != NULL)
5397 {
5398 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5399 new_fname = vim_strsave(buf);
5400 else
5401 new_fname = vim_strsave(fname);
5402 vim_free(buf);
5403 }
5404 return new_fname;
5405}
5406
5407#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5408
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005409static char_u *skip_string(char_u *p);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005410static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
5412/*
5413 * Find the start of a comment, not knowing if we are in a comment right now.
5414 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005415 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005417 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005418ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005419{
5420 return find_start_comment(curbuf->b_ind_maxcomment);
5421}
5422
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005424find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425{
5426 pos_T *pos;
5427 char_u *line;
5428 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005429 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005431 for (;;)
5432 {
5433 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5434 if (pos == NULL)
5435 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005437 /*
5438 * Check if the comment start we found is inside a string.
5439 * If it is then restrict the search to below this line and try again.
5440 */
5441 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005442 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005443 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005444 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005445 break;
5446 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5447 if (cur_maxcomment <= 0)
5448 {
5449 pos = NULL;
5450 break;
5451 }
5452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 return pos;
5454}
5455
5456/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005457 * Find the start of a comment or raw string, not knowing if we are in a
5458 * comment or raw string right now.
5459 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005460 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005461 * Return NULL when not inside a comment or raw string.
5462 * "CORS" -> Comment Or Raw String
5463 */
5464 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005465ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005466{
Bram Moolenaar089af182015-10-07 11:41:49 +02005467 static pos_T comment_pos_copy;
5468 pos_T *comment_pos;
5469 pos_T *rs_pos;
5470
5471 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5472 if (comment_pos != NULL)
5473 {
5474 /* Need to make a copy of the static pos in findmatchlimit(),
5475 * calling find_start_rawstring() may change it. */
5476 comment_pos_copy = *comment_pos;
5477 comment_pos = &comment_pos_copy;
5478 }
5479 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005480
5481 /* If comment_pos is before rs_pos the raw string is inside the comment.
5482 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005483 if (comment_pos == NULL || (rs_pos != NULL
5484 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005485 {
5486 if (is_raw != NULL && rs_pos != NULL)
5487 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005488 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005489 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005490 return comment_pos;
5491}
5492
5493/*
5494 * Find the start of a raw string, not knowing if we are in one right now.
5495 * Search starts at w_cursor.lnum and goes backwards.
5496 * Return NULL when not inside a raw string.
5497 */
5498 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005499find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005500{
5501 pos_T *pos;
5502 char_u *line;
5503 char_u *p;
5504 int cur_maxcomment = ind_maxcomment;
5505
5506 for (;;)
5507 {
5508 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5509 if (pos == NULL)
5510 break;
5511
5512 /*
5513 * Check if the raw string start we found is inside a string.
5514 * If it is then restrict the search to below this line and try again.
5515 */
5516 line = ml_get(pos->lnum);
5517 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5518 p = skip_string(p);
5519 if ((colnr_T)(p - line) <= pos->col)
5520 break;
5521 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5522 if (cur_maxcomment <= 0)
5523 {
5524 pos = NULL;
5525 break;
5526 }
5527 }
5528 return pos;
5529}
5530
5531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 * Skip to the end of a "string" and a 'c' character.
5533 * If there is no string or character, return argument unmodified.
5534 */
5535 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005536skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
5538 int i;
5539
5540 /*
5541 * We loop, because strings may be concatenated: "date""time".
5542 */
5543 for ( ; ; ++p)
5544 {
5545 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5546 {
5547 if (!p[1]) /* ' at end of line */
5548 break;
5549 i = 2;
5550 if (p[1] == '\\') /* '\n' or '\000' */
5551 {
5552 ++i;
5553 while (vim_isdigit(p[i - 1])) /* '\000' */
5554 ++i;
5555 }
5556 if (p[i] == '\'') /* check for trailing ' */
5557 {
5558 p += i;
5559 continue;
5560 }
5561 }
5562 else if (p[0] == '"') /* start of string */
5563 {
5564 for (++p; p[0]; ++p)
5565 {
5566 if (p[0] == '\\' && p[1] != NUL)
5567 ++p;
5568 else if (p[0] == '"') /* end of string */
5569 break;
5570 }
5571 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005572 continue; /* continue for another string */
5573 }
5574 else if (p[0] == 'R' && p[1] == '"')
5575 {
5576 /* Raw string: R"[delim](...)[delim]" */
5577 char_u *delim = p + 2;
5578 char_u *paren = vim_strchr(delim, '(');
5579
5580 if (paren != NULL)
5581 {
5582 size_t delim_len = paren - delim;
5583
5584 for (p += 3; *p; ++p)
5585 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5586 && p[delim_len + 1] == '"')
5587 {
5588 p += delim_len + 1;
5589 break;
5590 }
5591 if (p[0] == '"')
5592 continue; /* continue for another string */
5593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595 break; /* no string found */
5596 }
5597 if (!*p)
5598 --p; /* backup from NUL */
5599 return p;
5600}
5601#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5602
5603#if defined(FEAT_CINDENT) || defined(PROTO)
5604
5605/*
5606 * Do C or expression indenting on the current line.
5607 */
5608 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005609do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610{
5611# ifdef FEAT_EVAL
5612 if (*curbuf->b_p_inde != NUL)
5613 fixthisline(get_expr_indent);
5614 else
5615# endif
5616 fixthisline(get_c_indent);
5617}
5618
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005619/* Find result cache for cpp_baseclass */
5620typedef struct {
5621 int found;
5622 lpos_T lpos;
5623} cpp_baseclass_cache_T;
5624
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625/*
5626 * Functions for C-indenting.
5627 * Most of this originally comes from Eric Fischer.
5628 */
5629/*
5630 * Below "XXX" means that this function may unlock the current line.
5631 */
5632
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005633static int cin_isdefault(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005634static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005635static int cin_iscomment(char_u *);
5636static int cin_islinecomment(char_u *);
5637static int cin_isterminated(char_u *, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005638static int cin_iselse(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005639static int cin_ends_in(char_u *, char_u *, char_u *);
5640static int cin_starts_with(char_u *s, char *word);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005641static pos_T *find_match_paren(int);
5642static pos_T *find_match_char(int c, int ind_maxparen);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005643static int find_last_paren(char_u *l, int start, int end);
5644static int find_match(int lookfor, linenr_T ourscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645
5646/*
5647 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005648 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 */
5650 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005651cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652{
5653 while (*s)
5654 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005655 char_u *prev_s = s;
5656
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005658
5659 /* Perl/shell # comment comment continues until eol. Require a space
5660 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005661 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005662 {
5663 s += STRLEN(s);
5664 break;
5665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 if (*s != '/')
5667 break;
5668 ++s;
5669 if (*s == '/') /* slash-slash comment continues till eol */
5670 {
5671 s += STRLEN(s);
5672 break;
5673 }
5674 if (*s != '*')
5675 break;
5676 for (++s; *s; ++s) /* skip slash-star comment */
5677 if (s[0] == '*' && s[1] == '/')
5678 {
5679 s += 2;
5680 break;
5681 }
5682 }
5683 return s;
5684}
5685
5686/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005687 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 * not considered code.
5689 */
5690 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005691cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
5693 return *cin_skipcomment(s) == NUL;
5694}
5695
5696/*
5697 * Check previous lines for a "//" line comment, skipping over blank lines.
5698 */
5699 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005700find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
5702 static pos_T pos;
5703 char_u *line;
5704 char_u *p;
5705
5706 pos = curwin->w_cursor;
5707 while (--pos.lnum > 0)
5708 {
5709 line = ml_get(pos.lnum);
5710 p = skipwhite(line);
5711 if (cin_islinecomment(p))
5712 {
5713 pos.col = (int)(p - line);
5714 return &pos;
5715 }
5716 if (*p != NUL)
5717 break;
5718 }
5719 return NULL;
5720}
5721
5722/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005723 * Return TRUE if "text" starts with "key:".
5724 */
5725 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005726cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005727{
5728 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005729 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005730
5731 if (*s == '\'' || *s == '"')
5732 {
5733 /* can be 'key': or "key": */
5734 quote = *s;
5735 ++s;
5736 }
5737 if (!vim_isIDc(*s)) /* need at least one ID character */
5738 return FALSE;
5739
5740 while (vim_isIDc(*s))
5741 ++s;
5742 if (*s == quote)
5743 ++s;
5744
5745 s = cin_skipcomment(s);
5746
5747 /* "::" is not a label, it's C++ */
5748 return (*s == ':' && s[1] != ':');
5749}
5750
5751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005753 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 */
5755 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005756cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757{
5758 if (!vim_isIDc(**s)) /* need at least one ID character */
5759 return FALSE;
5760
5761 while (vim_isIDc(**s))
5762 (*s)++;
5763
5764 *s = cin_skipcomment(*s);
5765
5766 /* "::" is not a label, it's C++ */
5767 return (**s == ':' && *++*s != ':');
5768}
5769
5770/*
5771 * Recognize a label: "label:".
5772 * Note: curwin->w_cursor must be where we are looking for the label.
5773 */
5774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005775cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 char_u *s;
5778
5779 s = cin_skipcomment(ml_get_curline());
5780
5781 /*
5782 * Exclude "default" from labels, since it should be indented
5783 * like a switch label. Same for C++ scope declarations.
5784 */
5785 if (cin_isdefault(s))
5786 return FALSE;
5787 if (cin_isscopedecl(s))
5788 return FALSE;
5789
5790 if (cin_islabel_skip(&s))
5791 {
5792 /*
5793 * Only accept a label if the previous line is terminated or is a case
5794 * label.
5795 */
5796 pos_T cursor_save;
5797 pos_T *trypos;
5798 char_u *line;
5799
5800 cursor_save = curwin->w_cursor;
5801 while (curwin->w_cursor.lnum > 1)
5802 {
5803 --curwin->w_cursor.lnum;
5804
5805 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005806 * If we're in a comment or raw string now, skip to the start of
5807 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 */
5809 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005810 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 curwin->w_cursor = *trypos;
5812
5813 line = ml_get_curline();
5814 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5815 continue;
5816 if (*(line = cin_skipcomment(line)) == NUL)
5817 continue;
5818
5819 curwin->w_cursor = cursor_save;
5820 if (cin_isterminated(line, TRUE, FALSE)
5821 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005822 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 || (cin_islabel_skip(&line) && cin_nocode(line)))
5824 return TRUE;
5825 return FALSE;
5826 }
5827 curwin->w_cursor = cursor_save;
5828 return TRUE; /* label at start of file??? */
5829 }
5830 return FALSE;
5831}
5832
5833/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005834 * Recognize structure initialization and enumerations:
5835 * "[typedef] [static|public|protected|private] enum"
5836 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 */
5838 static int
5839cin_isinit(void)
5840{
5841 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005842 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
5844 s = cin_skipcomment(ml_get_curline());
5845
Bram Moolenaar75342212013-03-07 13:13:52 +01005846 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 s = cin_skipcomment(s + 7);
5848
Bram Moolenaar75342212013-03-07 13:13:52 +01005849 for (;;)
5850 {
5851 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005852
Bram Moolenaar75342212013-03-07 13:13:52 +01005853 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5854 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005855 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005856 if (cin_starts_with(s, skip[i]))
5857 {
5858 s = cin_skipcomment(s + l);
5859 l = 0;
5860 break;
5861 }
5862 }
5863 if (l != 0)
5864 break;
5865 }
5866
5867 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 return TRUE;
5869
5870 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5871 return TRUE;
5872
5873 return FALSE;
5874}
5875
5876/*
5877 * Recognize a switch label: "case .*:" or "default:".
5878 */
5879 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005880cin_iscase(
5881 char_u *s,
5882 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005885 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 {
5887 for (s += 4; *s; ++s)
5888 {
5889 s = cin_skipcomment(s);
5890 if (*s == ':')
5891 {
5892 if (s[1] == ':') /* skip over "::" for C++ */
5893 ++s;
5894 else
5895 return TRUE;
5896 }
5897 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005898 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5900 return FALSE; /* stop at comment */
5901 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005902 {
5903 /* JS etc. */
5904 if (strict)
5905 return FALSE; /* stop at string */
5906 else
5907 return TRUE;
5908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 }
5910 return FALSE;
5911 }
5912
5913 if (cin_isdefault(s))
5914 return TRUE;
5915 return FALSE;
5916}
5917
5918/*
5919 * Recognize a "default" switch label.
5920 */
5921 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005922cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 return (STRNCMP(s, "default", 7) == 0
5925 && *(s = cin_skipcomment(s + 7)) == ':'
5926 && s[1] != ':');
5927}
5928
5929/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005930 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 */
5932 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005933cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 int i;
5936
5937 s = cin_skipcomment(s);
5938 if (STRNCMP(s, "public", 6) == 0)
5939 i = 6;
5940 else if (STRNCMP(s, "protected", 9) == 0)
5941 i = 9;
5942 else if (STRNCMP(s, "private", 7) == 0)
5943 i = 7;
5944 else
5945 return FALSE;
5946 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5947}
5948
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005949/* Maximum number of lines to search back for a "namespace" line. */
5950#define FIND_NAMESPACE_LIM 20
5951
5952/*
5953 * Recognize a "namespace" scope declaration.
5954 */
5955 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005956cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005957{
5958 char_u *p;
5959 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005960 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005961
5962 s = cin_skipcomment(s);
5963 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5964 {
5965 p = cin_skipcomment(skipwhite(s + 9));
5966 while (*p != NUL)
5967 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005968 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005969 {
5970 has_name = TRUE; /* found end of a name */
5971 p = cin_skipcomment(skipwhite(p));
5972 }
5973 else if (*p == '{')
5974 {
5975 break;
5976 }
5977 else if (vim_iswordc(*p))
5978 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005979 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005980 if (has_name)
5981 return FALSE; /* word character after skipping past name */
5982 ++p;
5983 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005984 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5985 {
5986 if (!has_name_start || has_name)
5987 return FALSE;
5988 /* C++ 17 nested namespace */
5989 p += 3;
5990 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005991 else
5992 {
5993 return FALSE;
5994 }
5995 }
5996 return TRUE;
5997 }
5998 return FALSE;
5999}
6000
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006002 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6003 */
6004 static int
6005cin_is_cpp_extern_c(char_u *s)
6006{
6007 char_u *p;
6008 int has_string_literal = FALSE;
6009
6010 s = cin_skipcomment(s);
6011 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6012 {
6013 p = cin_skipcomment(skipwhite(s + 6));
6014 while (*p != NUL)
6015 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006016 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006017 {
6018 p = cin_skipcomment(skipwhite(p));
6019 }
6020 else if (*p == '{')
6021 {
6022 break;
6023 }
6024 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6025 {
6026 if (has_string_literal)
6027 return FALSE;
6028 has_string_literal = TRUE;
6029 p += 3;
6030 }
6031 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6032 && p[4] == '"')
6033 {
6034 if (has_string_literal)
6035 return FALSE;
6036 has_string_literal = TRUE;
6037 p += 5;
6038 }
6039 else
6040 {
6041 return FALSE;
6042 }
6043 }
6044 return has_string_literal ? TRUE : FALSE;
6045 }
6046 return FALSE;
6047}
6048
6049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 * Return a pointer to the first non-empty non-comment character after a ':'.
6051 * Return NULL if not found.
6052 * case 234: a = b;
6053 * ^
6054 */
6055 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006056after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057{
6058 for ( ; *l; ++l)
6059 {
6060 if (*l == ':')
6061 {
6062 if (l[1] == ':') /* skip over "::" for C++ */
6063 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006064 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 break;
6066 }
6067 else if (*l == '\'' && l[1] && l[2] == '\'')
6068 l += 2; /* skip over 'x' */
6069 }
6070 if (*l == NUL)
6071 return NULL;
6072 l = cin_skipcomment(l + 1);
6073 if (*l == NUL)
6074 return NULL;
6075 return l;
6076}
6077
6078/*
6079 * Get indent of line "lnum", skipping a label.
6080 * Return 0 if there is nothing after the label.
6081 */
6082 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006083get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084{
6085 char_u *l;
6086 pos_T fp;
6087 colnr_T col;
6088 char_u *p;
6089
6090 l = ml_get(lnum);
6091 p = after_label(l);
6092 if (p == NULL)
6093 return 0;
6094
6095 fp.col = (colnr_T)(p - l);
6096 fp.lnum = lnum;
6097 getvcol(curwin, &fp, &col, NULL, NULL);
6098 return (int)col;
6099}
6100
6101/*
6102 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006103 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 * label: if (asdf && asdfasdf)
6105 * ^
6106 */
6107 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006108skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109{
6110 char_u *l;
6111 int amount;
6112 pos_T cursor_save;
6113
6114 cursor_save = curwin->w_cursor;
6115 curwin->w_cursor.lnum = lnum;
6116 l = ml_get_curline();
6117 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006118 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {
6120 amount = get_indent_nolabel(lnum);
6121 l = after_label(ml_get_curline());
6122 if (l == NULL) /* just in case */
6123 l = ml_get_curline();
6124 }
6125 else
6126 {
6127 amount = get_indent();
6128 l = ml_get_curline();
6129 }
6130 *pp = l;
6131
6132 curwin->w_cursor = cursor_save;
6133 return amount;
6134}
6135
6136/*
6137 * Return the indent of the first variable name after a type in a declaration.
6138 * int a, indent of "a"
6139 * static struct foo b, indent of "b"
6140 * enum bla c, indent of "c"
6141 * Returns zero when it doesn't look like a declaration.
6142 */
6143 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006144cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145{
6146 char_u *line, *p, *s;
6147 int len;
6148 pos_T fp;
6149 colnr_T col;
6150
6151 line = ml_get_curline();
6152 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006153 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6155 {
6156 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006157 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 }
6159 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6160 p = skipwhite(p + 6);
6161 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6162 p = skipwhite(p + 4);
6163 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6164 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6165 {
6166 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006167 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6168 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6169 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6170 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 p = s;
6172 }
6173 for (len = 0; vim_isIDc(p[len]); ++len)
6174 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006175 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 return 0;
6177
6178 p = skipwhite(p + len);
6179 fp.lnum = curwin->w_cursor.lnum;
6180 fp.col = (colnr_T)(p - line);
6181 getvcol(curwin, &fp, &col, NULL, NULL);
6182 return (int)col;
6183}
6184
6185/*
6186 * Return the indent of the first non-blank after an equal sign.
6187 * char *foo = "here";
6188 * Return zero if no (useful) equal sign found.
6189 * Return -1 if the line above "lnum" ends in a backslash.
6190 * foo = "asdf\
6191 * asdf\
6192 * here";
6193 */
6194 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006195cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196{
6197 char_u *line;
6198 char_u *s;
6199 colnr_T col;
6200 pos_T fp;
6201
6202 if (lnum > 1)
6203 {
6204 line = ml_get(lnum - 1);
6205 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6206 return -1;
6207 }
6208
6209 line = s = ml_get(lnum);
6210 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6211 {
6212 if (cin_iscomment(s)) /* ignore comments */
6213 s = cin_skipcomment(s);
6214 else
6215 ++s;
6216 }
6217 if (*s != '=')
6218 return 0;
6219
6220 s = skipwhite(s + 1);
6221 if (cin_nocode(s))
6222 return 0;
6223
6224 if (*s == '"') /* nice alignment for continued strings */
6225 ++s;
6226
6227 fp.lnum = lnum;
6228 fp.col = (colnr_T)(s - line);
6229 getvcol(curwin, &fp, &col, NULL, NULL);
6230 return (int)col;
6231}
6232
6233/*
6234 * Recognize a preprocessor statement: Any line that starts with '#'.
6235 */
6236 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006237cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006239 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 return TRUE;
6241 return FALSE;
6242}
6243
6244/*
6245 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6246 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6247 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006248 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 */
6250 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006251cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252{
6253 char_u *line = *pp;
6254 linenr_T lnum = *lnump;
6255 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006256 int candidate_amount = *amount;
6257
6258 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6259 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006261 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
6263 if (cin_ispreproc(line))
6264 {
6265 retval = TRUE;
6266 *lnump = lnum;
6267 break;
6268 }
6269 if (lnum == 1)
6270 break;
6271 line = ml_get(--lnum);
6272 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6273 break;
6274 }
6275
6276 if (lnum != *lnump)
6277 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006278 if (retval)
6279 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 return retval;
6281}
6282
6283/*
6284 * Recognize the start of a C or C++ comment.
6285 */
6286 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006287cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288{
6289 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6290}
6291
6292/*
6293 * Recognize the start of a "//" comment.
6294 */
6295 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006296cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297{
6298 return (p[0] == '/' && p[1] == '/');
6299}
6300
6301/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006302 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6303 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006305 * If a line begins with an "else", only consider it terminated if no unmatched
6306 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 * Return the character terminating the line (ending char's have precedence if
6308 * both apply in order to determine initializations).
6309 */
6310 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006311cin_isterminated(
6312 char_u *s,
6313 int incl_open, /* include '{' at the end as terminator */
6314 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006316 char_u found_start = 0;
6317 unsigned n_open = 0;
6318 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319
6320 s = cin_skipcomment(s);
6321
6322 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6323 found_start = *s;
6324
Bram Moolenaar496f9512011-05-19 16:35:09 +02006325 if (!found_start)
6326 is_else = cin_iselse(s);
6327
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 while (*s)
6329 {
6330 /* skip over comments, "" strings and 'c'haracters */
6331 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006332 if (*s == '}' && n_open > 0)
6333 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006334 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006335 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 && cin_nocode(s + 1))
6337 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006338 else if (*s == '{')
6339 {
6340 if (incl_open && cin_nocode(s + 1))
6341 return *s;
6342 else
6343 ++n_open;
6344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345
6346 if (*s)
6347 s++;
6348 }
6349 return found_start;
6350}
6351
6352/*
6353 * Recognize the basic picture of a function declaration -- it needs to
6354 * have an open paren somewhere and a close paren at the end of the line and
6355 * no semicolons anywhere.
6356 * When a line ends in a comma we continue looking in the next line.
6357 * "sp" points to a string with the line. When looking at other lines it must
6358 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006359 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006360 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 */
6362 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006363cin_isfuncdecl(
6364 char_u **sp,
6365 linenr_T first_lnum,
6366 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 char_u *s;
6369 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006370 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006372 pos_T *trypos;
6373 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374
6375 if (sp == NULL)
6376 s = ml_get(lnum);
6377 else
6378 s = *sp;
6379
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006380 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006381 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006382 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006383 {
6384 lnum = trypos->lnum;
6385 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006386 {
6387 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006388 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006389 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006390
6391 s = ml_get(lnum);
6392 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006393 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006394
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006395 /* Ignore line starting with #. */
6396 if (cin_ispreproc(s))
6397 return FALSE;
6398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6400 {
6401 if (cin_iscomment(s)) /* ignore comments */
6402 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006403 else if (*s == ':')
6404 {
6405 if (*(s + 1) == ':')
6406 s += 2;
6407 else
6408 /* To avoid a mistake in the following situation:
6409 * A::A(int a, int b)
6410 * : a(0) // <--not a function decl
6411 * , b(0)
6412 * {...
6413 */
6414 return FALSE;
6415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 else
6417 ++s;
6418 }
6419 if (*s != '(')
6420 return FALSE; /* ';', ' or " before any () or no '(' */
6421
6422 while (*s && *s != ';' && *s != '\'' && *s != '"')
6423 {
6424 if (*s == ')' && cin_nocode(s + 1))
6425 {
6426 /* ')' at the end: may have found a match
6427 * Check for he previous line not to end in a backslash:
6428 * #if defined(x) && \
6429 * defined(y)
6430 */
6431 lnum = first_lnum - 1;
6432 s = ml_get(lnum);
6433 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6434 retval = TRUE;
6435 goto done;
6436 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006437 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006439 int comma = (*s == ',');
6440
6441 /* ',' at the end: continue looking in the next line.
6442 * At the end: check for ',' in the next line, for this style:
6443 * func(arg1
6444 * , arg2) */
6445 for (;;)
6446 {
6447 if (lnum >= curbuf->b_ml.ml_line_count)
6448 break;
6449 s = ml_get(++lnum);
6450 if (!cin_ispreproc(s))
6451 break;
6452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 if (lnum >= curbuf->b_ml.ml_line_count)
6454 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006455 /* Require a comma at end of the line or a comma or ')' at the
6456 * start of next line. */
6457 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006458 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006459 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006460 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 }
6462 else if (cin_iscomment(s)) /* ignore comments */
6463 s = cin_skipcomment(s);
6464 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006467 just_started = FALSE;
6468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 }
6470
6471done:
6472 if (lnum != first_lnum && sp != NULL)
6473 *sp = ml_get(first_lnum);
6474
6475 return retval;
6476}
6477
6478 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006479cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006481 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482}
6483
6484 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006485cin_iselse(
6486 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 if (*p == '}') /* accept "} else" */
6489 p = cin_skipcomment(p + 1);
6490 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6491}
6492
6493 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006494cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495{
6496 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6497}
6498
6499/*
6500 * Check if this is a "while" that should have a matching "do".
6501 * We only accept a "while (condition) ;", with only white space between the
6502 * ')' and ';'. The condition may be spread over several lines.
6503 */
6504 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006505cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
6507 pos_T cursor_save;
6508 pos_T *trypos;
6509 int retval = FALSE;
6510
6511 p = cin_skipcomment(p);
6512 if (*p == '}') /* accept "} while (cond);" */
6513 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006514 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 {
6516 cursor_save = curwin->w_cursor;
6517 curwin->w_cursor.lnum = lnum;
6518 curwin->w_cursor.col = 0;
6519 p = ml_get_curline();
6520 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6521 {
6522 ++p;
6523 ++curwin->w_cursor.col;
6524 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006525 if ((trypos = findmatchlimit(NULL, 0, 0,
6526 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6528 retval = TRUE;
6529 curwin->w_cursor = cursor_save;
6530 }
6531 return retval;
6532}
6533
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006534/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006535 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006536 * Return 0 if there is none.
6537 * Otherwise return !0 and update "*poffset" to point to the place where the
6538 * string was found.
6539 */
6540 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006541cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006542{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006543 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006544
6545 if (offset-- < 2)
6546 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006547 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006548 --offset;
6549
6550 offset -= 1;
6551 if (!STRNCMP(line + offset, "if", 2))
6552 goto probablyFound;
6553
6554 if (offset >= 1)
6555 {
6556 offset -= 1;
6557 if (!STRNCMP(line + offset, "for", 3))
6558 goto probablyFound;
6559
6560 if (offset >= 2)
6561 {
6562 offset -= 2;
6563 if (!STRNCMP(line + offset, "while", 5))
6564 goto probablyFound;
6565 }
6566 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006567 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006568
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006569probablyFound:
6570 if (!offset || !vim_isIDc(line[offset - 1]))
6571 {
6572 *poffset = offset;
6573 return 1;
6574 }
6575 return 0;
6576}
6577
6578/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006579 * Return TRUE if we are at the end of a do-while.
6580 * do
6581 * nothing;
6582 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006583 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006584 * Adjust the cursor to the line with "while".
6585 */
6586 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006587cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006588{
6589 char_u *line;
6590 char_u *p;
6591 char_u *s;
6592 pos_T *trypos;
6593 int i;
6594
6595 if (terminated != ';') /* there must be a ';' at the end */
6596 return FALSE;
6597
6598 p = line = ml_get_curline();
6599 while (*p != NUL)
6600 {
6601 p = cin_skipcomment(p);
6602 if (*p == ')')
6603 {
6604 s = skipwhite(p + 1);
6605 if (*s == ';' && cin_nocode(s + 1))
6606 {
6607 /* Found ");" at end of the line, now check there is "while"
6608 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006609 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006610 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006611 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006612 if (trypos != NULL)
6613 {
6614 s = cin_skipcomment(ml_get(trypos->lnum));
6615 if (*s == '}') /* accept "} while (cond);" */
6616 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006617 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006618 {
6619 curwin->w_cursor.lnum = trypos->lnum;
6620 return TRUE;
6621 }
6622 }
6623
6624 /* Searching may have made "line" invalid, get it again. */
6625 line = ml_get_curline();
6626 p = line + i;
6627 }
6628 }
6629 if (*p != NUL)
6630 ++p;
6631 }
6632 return FALSE;
6633}
6634
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006636cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
6638 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6639}
6640
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006641/*
6642 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 * constructor-initialization. eg:
6644 *
6645 * class MyClass :
6646 * baseClass <-- here
6647 * class MyClass : public baseClass,
6648 * anotherBaseClass <-- here (should probably lineup ??)
6649 * MyClass::MyClass(...) :
6650 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006651 *
6652 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 */
6654 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006655cin_is_cpp_baseclass(
6656 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006658 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 char_u *s;
6660 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006661 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006662 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006664 if (pos->lnum <= lnum)
6665 return cached->found; /* Use the cached result */
6666
6667 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006669 s = skipwhite(line);
6670 if (*s == '#') /* skip #define FOO x ? (x) : x */
6671 return FALSE;
6672 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 if (*s == NUL)
6674 return FALSE;
6675
6676 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6677
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006678 /* Search for a line starting with '#', empty, ending in ';' or containing
6679 * '{' or '}' and start below it. This handles the following situations:
6680 * a = cond ?
6681 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006682 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006683 * func::foo()
6684 * : something
6685 * {}
6686 * Foo::Foo (int one, int two)
6687 * : something(4),
6688 * somethingelse(3)
6689 * {}
6690 */
6691 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006693 line = ml_get(lnum - 1);
6694 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006695 if (*s == '#' || *s == NUL)
6696 break;
6697 while (*s != NUL)
6698 {
6699 s = cin_skipcomment(s);
6700 if (*s == '{' || *s == '}'
6701 || (*s == ';' && cin_nocode(s + 1)))
6702 break;
6703 if (*s != NUL)
6704 ++s;
6705 }
6706 if (*s != NUL)
6707 break;
6708 --lnum;
6709 }
6710
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006711 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006712 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006713 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006714 for (;;)
6715 {
6716 if (*s == NUL)
6717 {
6718 if (lnum == curwin->w_cursor.lnum)
6719 break;
6720 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006721 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006722 s = line;
6723 }
6724 if (s == line)
6725 {
6726 /* don't recognize "case (foo):" as a baseclass */
6727 if (cin_iscase(s, FALSE))
6728 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006729 s = cin_skipcomment(line);
6730 if (*s == NUL)
6731 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006732 }
6733
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006734 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006735 s = skip_string(s) + 1;
6736 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {
6738 if (s[1] == ':')
6739 {
6740 /* skip double colon. It can't be a constructor
6741 * initialization any more */
6742 lookfor_ctor_init = FALSE;
6743 s = cin_skipcomment(s + 2);
6744 }
6745 else if (lookfor_ctor_init || class_or_struct)
6746 {
6747 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006748 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 cpp_base_class = TRUE;
6750 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006751 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 s = cin_skipcomment(s + 1);
6753 }
6754 else
6755 s = cin_skipcomment(s + 1);
6756 }
6757 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6758 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6759 {
6760 class_or_struct = TRUE;
6761 lookfor_ctor_init = FALSE;
6762
6763 if (*s == 'c')
6764 s = cin_skipcomment(s + 5);
6765 else
6766 s = cin_skipcomment(s + 6);
6767 }
6768 else
6769 {
6770 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6771 {
6772 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6773 }
6774 else if (s[0] == ')')
6775 {
6776 /* Constructor-initialization is assumed if we come across
6777 * something like "):" */
6778 class_or_struct = FALSE;
6779 lookfor_ctor_init = TRUE;
6780 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006781 else if (s[0] == '?')
6782 {
6783 /* Avoid seeing '() :' after '?' as constructor init. */
6784 return FALSE;
6785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 else if (!vim_isIDc(s[0]))
6787 {
6788 /* if it is not an identifier, we are wrong */
6789 class_or_struct = FALSE;
6790 lookfor_ctor_init = FALSE;
6791 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006792 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
6794 /* it can't be a constructor-initialization any more */
6795 lookfor_ctor_init = FALSE;
6796
6797 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006798 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006799 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 }
6801
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006802 /* When the line ends in a comma don't align with it. */
6803 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006804 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006805
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 s = cin_skipcomment(s + 1);
6807 }
6808 }
6809
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006810 cached->found = cpp_base_class;
6811 if (cpp_base_class)
6812 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 return cpp_base_class;
6814}
6815
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006816 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006817get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006818{
6819 int amount;
6820 colnr_T vcol;
6821 pos_T *trypos;
6822
6823 if (col == 0)
6824 {
6825 amount = get_indent();
6826 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006827 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006828 amount = get_indent_lnum(trypos->lnum); /* XXX */
6829 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006830 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006831 }
6832 else
6833 {
6834 curwin->w_cursor.col = col;
6835 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6836 amount = (int)vcol;
6837 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006838 if (amount < curbuf->b_ind_cpp_baseclass)
6839 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006840 return amount;
6841}
6842
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843/*
6844 * Return TRUE if string "s" ends with the string "find", possibly followed by
6845 * white space and comments. Skip strings and comments.
6846 * Ignore "ignore" after "find" if it's not NULL.
6847 */
6848 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006849cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850{
6851 char_u *p = s;
6852 char_u *r;
6853 int len = (int)STRLEN(find);
6854
6855 while (*p != NUL)
6856 {
6857 p = cin_skipcomment(p);
6858 if (STRNCMP(p, find, len) == 0)
6859 {
6860 r = skipwhite(p + len);
6861 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6862 r = skipwhite(r + STRLEN(ignore));
6863 if (cin_nocode(r))
6864 return TRUE;
6865 }
6866 if (*p != NUL)
6867 ++p;
6868 }
6869 return FALSE;
6870}
6871
6872/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006873 * Return TRUE when "s" starts with "word" and then a non-ID character.
6874 */
6875 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006876cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006877{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006878 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006879
6880 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6881}
6882
6883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 * Skip strings, chars and comments until at or past "trypos".
6885 * Return the column found.
6886 */
6887 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006888cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889{
6890 char_u *line;
6891 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006892 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893
6894 p = line = ml_get(trypos->lnum);
6895 while (*p && (colnr_T)(p - line) < trypos->col)
6896 {
6897 if (cin_iscomment(p))
6898 p = cin_skipcomment(p);
6899 else
6900 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006901 new_p = skip_string(p);
6902 if (new_p == p)
6903 ++p;
6904 else
6905 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 }
6907 }
6908 return (int)(p - line);
6909}
6910
6911/*
6912 * Find the '{' at the start of the block we are in.
6913 * Return NULL if no match found.
6914 * Ignore a '{' that is in a comment, makes indenting the next three lines
6915 * work. */
6916/* foo() */
6917/* { */
6918/* } */
6919
6920 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006921find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922{
6923 pos_T cursor_save;
6924 pos_T *trypos;
6925 pos_T *pos;
6926 static pos_T pos_copy;
6927
6928 cursor_save = curwin->w_cursor;
6929 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6930 {
6931 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6932 trypos = &pos_copy;
6933 curwin->w_cursor = *trypos;
6934 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006935 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006937 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 break;
6939 if (pos != NULL)
6940 curwin->w_cursor.lnum = pos->lnum;
6941 }
6942 curwin->w_cursor = cursor_save;
6943 return trypos;
6944}
6945
6946/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006947 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006948 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 */
6950 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006951find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006953 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006954}
6955
6956 static pos_T *
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02006957find_match_char(int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006958{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 pos_T cursor_save;
6960 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006961 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006962 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
6964 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006965 ind_maxp_wk = ind_maxparen;
6966retry:
6967 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {
6969 /* check if the ( is in a // comment */
6970 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006971 {
6972 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6973 if (ind_maxp_wk > 0)
6974 {
6975 curwin->w_cursor = *trypos;
6976 curwin->w_cursor.col = 0; /* XXX */
6977 goto retry;
6978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 else
6982 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006983 pos_T *trypos_wk;
6984
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6986 trypos = &pos_copy;
6987 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02006988 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006989 {
6990 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6991 - trypos_wk->lnum);
6992 if (ind_maxp_wk > 0)
6993 {
6994 curwin->w_cursor = *trypos_wk;
6995 goto retry;
6996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 }
7000 }
7001 curwin->w_cursor = cursor_save;
7002 return trypos;
7003}
7004
7005/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007006 * Find the matching '(', ignoring it if it is in a comment or before an
7007 * unmatched {.
7008 * Return NULL if no match found.
7009 */
7010 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007011find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007012{
7013 pos_T *trypos = find_match_paren(ind_maxparen);
7014
7015 if (trypos != NULL)
7016 {
7017 pos_T *tryposBrace = find_start_brace();
7018
7019 /* If both an unmatched '(' and '{' is found. Ignore the '('
7020 * position if the '{' is further down. */
7021 if (tryposBrace != NULL
7022 && (trypos->lnum != tryposBrace->lnum
7023 ? trypos->lnum < tryposBrace->lnum
7024 : trypos->col < tryposBrace->col))
7025 trypos = NULL;
7026 }
7027 return trypos;
7028}
7029
7030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 * Return ind_maxparen corrected for the difference in line number between the
7032 * cursor position and "startpos". This makes sure that searching for a
7033 * matching paren above the cursor line doesn't find a match because of
7034 * looking a few lines further.
7035 */
7036 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007037corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
7039 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7040
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007041 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7042 return curbuf->b_ind_maxparen - (int)n;
7043 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044}
7045
7046/*
7047 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007048 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 */
7050 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007051find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
7053 int i;
7054 int retval = FALSE;
7055 int open_count = 0;
7056
7057 curwin->w_cursor.col = 0; /* default is start of line */
7058
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007059 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {
7061 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7062 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7063 if (l[i] == start)
7064 ++open_count;
7065 else if (l[i] == end)
7066 {
7067 if (open_count > 0)
7068 --open_count;
7069 else
7070 {
7071 curwin->w_cursor.col = i;
7072 retval = TRUE;
7073 }
7074 }
7075 }
7076 return retval;
7077}
7078
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007079/*
7080 * Parse 'cinoptions' and set the values in "curbuf".
7081 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7082 */
7083 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007084parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007085{
7086 char_u *p;
7087 char_u *l;
7088 char_u *digits;
7089 int n;
7090 int divider;
7091 int fraction = 0;
7092 int sw = (int)get_sw_value(buf);
7093
7094 /*
7095 * Set the default values.
7096 */
7097 /* Spaces from a block's opening brace the prevailing indent for that
7098 * block should be. */
7099 buf->b_ind_level = sw;
7100
7101 /* Spaces from the edge of the line an open brace that's at the end of a
7102 * line is imagined to be. */
7103 buf->b_ind_open_imag = 0;
7104
7105 /* Spaces from the prevailing indent for a line that is not preceded by
7106 * an opening brace. */
7107 buf->b_ind_no_brace = 0;
7108
7109 /* Column where the first { of a function should be located }. */
7110 buf->b_ind_first_open = 0;
7111
7112 /* Spaces from the prevailing indent a leftmost open brace should be
7113 * located. */
7114 buf->b_ind_open_extra = 0;
7115
7116 /* Spaces from the matching open brace (real location for one at the left
7117 * edge; imaginary location from one that ends a line) the matching close
7118 * brace should be located. */
7119 buf->b_ind_close_extra = 0;
7120
7121 /* Spaces from the edge of the line an open brace sitting in the leftmost
7122 * column is imagined to be. */
7123 buf->b_ind_open_left_imag = 0;
7124
7125 /* Spaces jump labels should be shifted to the left if N is non-negative,
7126 * otherwise the jump label will be put to column 1. */
7127 buf->b_ind_jump_label = -1;
7128
7129 /* Spaces from the switch() indent a "case xx" label should be located. */
7130 buf->b_ind_case = sw;
7131
7132 /* Spaces from the "case xx:" code after a switch() should be located. */
7133 buf->b_ind_case_code = sw;
7134
7135 /* Lineup break at end of case in switch() with case label. */
7136 buf->b_ind_case_break = 0;
7137
7138 /* Spaces from the class declaration indent a scope declaration label
7139 * should be located. */
7140 buf->b_ind_scopedecl = sw;
7141
7142 /* Spaces from the scope declaration label code should be located. */
7143 buf->b_ind_scopedecl_code = sw;
7144
7145 /* Amount K&R-style parameters should be indented. */
7146 buf->b_ind_param = sw;
7147
7148 /* Amount a function type spec should be indented. */
7149 buf->b_ind_func_type = sw;
7150
7151 /* Amount a cpp base class declaration or constructor initialization
7152 * should be indented. */
7153 buf->b_ind_cpp_baseclass = sw;
7154
7155 /* additional spaces beyond the prevailing indent a continuation line
7156 * should be located. */
7157 buf->b_ind_continuation = sw;
7158
7159 /* Spaces from the indent of the line with an unclosed parentheses. */
7160 buf->b_ind_unclosed = sw * 2;
7161
7162 /* Spaces from the indent of the line with an unclosed parentheses, which
7163 * itself is also unclosed. */
7164 buf->b_ind_unclosed2 = sw;
7165
7166 /* Suppress ignoring spaces from the indent of a line starting with an
7167 * unclosed parentheses. */
7168 buf->b_ind_unclosed_noignore = 0;
7169
7170 /* If the opening paren is the last nonwhite character on the line, and
7171 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7172 * context (for very long lines). */
7173 buf->b_ind_unclosed_wrapped = 0;
7174
7175 /* Suppress ignoring white space when lining up with the character after
7176 * an unclosed parentheses. */
7177 buf->b_ind_unclosed_whiteok = 0;
7178
7179 /* Indent a closing parentheses under the line start of the matching
7180 * opening parentheses. */
7181 buf->b_ind_matching_paren = 0;
7182
7183 /* Indent a closing parentheses under the previous line. */
7184 buf->b_ind_paren_prev = 0;
7185
7186 /* Extra indent for comments. */
7187 buf->b_ind_comment = 0;
7188
7189 /* Spaces from the comment opener when there is nothing after it. */
7190 buf->b_ind_in_comment = 3;
7191
7192 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7193 * after the comment opener. */
7194 buf->b_ind_in_comment2 = 0;
7195
7196 /* Max lines to search for an open paren. */
7197 buf->b_ind_maxparen = 20;
7198
7199 /* Max lines to search for an open comment. */
7200 buf->b_ind_maxcomment = 70;
7201
7202 /* Handle braces for java code. */
7203 buf->b_ind_java = 0;
7204
7205 /* Not to confuse JS object properties with labels. */
7206 buf->b_ind_js = 0;
7207
7208 /* Handle blocked cases correctly. */
7209 buf->b_ind_keep_case_label = 0;
7210
7211 /* Handle C++ namespace. */
7212 buf->b_ind_cpp_namespace = 0;
7213
7214 /* Handle continuation lines containing conditions of if(), for() and
7215 * while(). */
7216 buf->b_ind_if_for_while = 0;
7217
Bram Moolenaar6b643942017-03-05 19:44:06 +01007218 /* indentation for # comments */
7219 buf->b_ind_hash_comment = 0;
7220
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007221 /* Handle C++ extern "C" or "C++" */
7222 buf->b_ind_cpp_extern_c = 0;
7223
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007224 for (p = buf->b_p_cino; *p; )
7225 {
7226 l = p++;
7227 if (*p == '-')
7228 ++p;
7229 digits = p; /* remember where the digits start */
7230 n = getdigits(&p);
7231 divider = 0;
7232 if (*p == '.') /* ".5s" means a fraction */
7233 {
7234 fraction = atol((char *)++p);
7235 while (VIM_ISDIGIT(*p))
7236 {
7237 ++p;
7238 if (divider)
7239 divider *= 10;
7240 else
7241 divider = 10;
7242 }
7243 }
7244 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7245 {
7246 if (p == digits)
7247 n = sw; /* just "s" is one 'shiftwidth' */
7248 else
7249 {
7250 n *= sw;
7251 if (divider)
7252 n += (sw * fraction + divider / 2) / divider;
7253 }
7254 ++p;
7255 }
7256 if (l[1] == '-')
7257 n = -n;
7258
7259 /* When adding an entry here, also update the default 'cinoptions' in
7260 * doc/indent.txt, and add explanation for it! */
7261 switch (*l)
7262 {
7263 case '>': buf->b_ind_level = n; break;
7264 case 'e': buf->b_ind_open_imag = n; break;
7265 case 'n': buf->b_ind_no_brace = n; break;
7266 case 'f': buf->b_ind_first_open = n; break;
7267 case '{': buf->b_ind_open_extra = n; break;
7268 case '}': buf->b_ind_close_extra = n; break;
7269 case '^': buf->b_ind_open_left_imag = n; break;
7270 case 'L': buf->b_ind_jump_label = n; break;
7271 case ':': buf->b_ind_case = n; break;
7272 case '=': buf->b_ind_case_code = n; break;
7273 case 'b': buf->b_ind_case_break = n; break;
7274 case 'p': buf->b_ind_param = n; break;
7275 case 't': buf->b_ind_func_type = n; break;
7276 case '/': buf->b_ind_comment = n; break;
7277 case 'c': buf->b_ind_in_comment = n; break;
7278 case 'C': buf->b_ind_in_comment2 = n; break;
7279 case 'i': buf->b_ind_cpp_baseclass = n; break;
7280 case '+': buf->b_ind_continuation = n; break;
7281 case '(': buf->b_ind_unclosed = n; break;
7282 case 'u': buf->b_ind_unclosed2 = n; break;
7283 case 'U': buf->b_ind_unclosed_noignore = n; break;
7284 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7285 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7286 case 'm': buf->b_ind_matching_paren = n; break;
7287 case 'M': buf->b_ind_paren_prev = n; break;
7288 case ')': buf->b_ind_maxparen = n; break;
7289 case '*': buf->b_ind_maxcomment = n; break;
7290 case 'g': buf->b_ind_scopedecl = n; break;
7291 case 'h': buf->b_ind_scopedecl_code = n; break;
7292 case 'j': buf->b_ind_java = n; break;
7293 case 'J': buf->b_ind_js = n; break;
7294 case 'l': buf->b_ind_keep_case_label = n; break;
7295 case '#': buf->b_ind_hash_comment = n; break;
7296 case 'N': buf->b_ind_cpp_namespace = n; break;
7297 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007298 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007299 }
7300 if (*p == ',')
7301 ++p;
7302 }
7303}
7304
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007305/*
7306 * Return the desired indent for C code.
7307 * Return -1 if the indent should be left alone (inside a raw string).
7308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007310get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 pos_T cur_curpos;
7313 int amount;
7314 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007315 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 colnr_T col;
7317 char_u *theline;
7318 char_u *linecopy;
7319 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007320 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007322 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 pos_T our_paren_pos;
7324 char_u *start;
7325 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007326#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327#define BRACE_AT_START 2 /* '{' is at start of line */
7328#define BRACE_AT_END 3 /* '{' is at end of line */
7329 linenr_T ourscope;
7330 char_u *l;
7331 char_u *look;
7332 char_u terminated;
7333 int lookfor;
7334#define LOOKFOR_INITIAL 0
7335#define LOOKFOR_IF 1
7336#define LOOKFOR_DO 2
7337#define LOOKFOR_CASE 3
7338#define LOOKFOR_ANY 4
7339#define LOOKFOR_TERM 5
7340#define LOOKFOR_UNTERM 6
7341#define LOOKFOR_SCOPEDECL 7
7342#define LOOKFOR_NOBREAK 8
7343#define LOOKFOR_CPP_BASECLASS 9
7344#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007345#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007346#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
7348 int whilelevel;
7349 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 int n;
7351 int iscase;
7352 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007353 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007355 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007356 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007357 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007358 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007359 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007361 /* make a copy, value is changed below */
7362 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
7364 /* remember where the cursor was when we started */
7365 cur_curpos = curwin->w_cursor;
7366
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007367 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007368 if (cur_curpos.lnum == 1)
7369 return 0;
7370
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 /* Get a copy of the current contents of the line.
7372 * This is required, because only the most recent line obtained with
7373 * ml_get is valid! */
7374 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7375 if (linecopy == NULL)
7376 return 0;
7377
7378 /*
7379 * In insert mode and the cursor is on a ')' truncate the line at the
7380 * cursor position. We don't want to line up with the matching '(' when
7381 * inserting new stuff.
7382 * For unknown reasons the cursor might be past the end of the line, thus
7383 * check for that.
7384 */
7385 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007386 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 && linecopy[curwin->w_cursor.col] == ')')
7388 linecopy[curwin->w_cursor.col] = NUL;
7389
7390 theline = skipwhite(linecopy);
7391
7392 /* move the cursor to the start of the line */
7393
7394 curwin->w_cursor.col = 0;
7395
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007396 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007397
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007399 * If we are inside a raw string don't change the indent.
7400 * Ignore a raw string inside a comment.
7401 */
7402 comment_pos = ind_find_start_comment();
7403 if (comment_pos != NULL)
7404 {
7405 /* findmatchlimit() static pos is overwritten, make a copy */
7406 tryposCopy = *comment_pos;
7407 comment_pos = &tryposCopy;
7408 }
7409 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007410 if (trypos != NULL && (comment_pos == NULL
7411 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007412 {
7413 amount = -1;
7414 goto laterend;
7415 }
7416
7417 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 * #defines and so on always go at the left when included in 'cinkeys'.
7419 */
7420 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007421 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007422 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007423 goto theend;
7424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425
7426 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007427 * Is it a non-case label? Then that goes at the left margin too unless:
7428 * - JS flag is set.
7429 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007431 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007432 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 {
7434 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007435 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 }
7437
7438 /*
7439 * If we're inside a "//" comment and there is a "//" comment in a
7440 * previous line, lineup with that one.
7441 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007442 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 && (trypos = find_line_comment()) != NULL) /* XXX */
7444 {
7445 /* find how indented the line beginning the comment is */
7446 getvcol(curwin, trypos, &col, NULL, NULL);
7447 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007448 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 }
7450
7451 /*
7452 * If we're inside a comment and not looking at the start of the
7453 * comment, try using the 'comments' option.
7454 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007455 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 {
7457 int lead_start_len = 2;
7458 int lead_middle_len = 1;
7459 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7460 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7461 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7462 char_u *p;
7463 int start_align = 0;
7464 int start_off = 0;
7465 int done = FALSE;
7466
7467 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007468 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007470 *lead_start = NUL;
7471 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
7473 p = curbuf->b_p_com;
7474 while (*p != NUL)
7475 {
7476 int align = 0;
7477 int off = 0;
7478 int what = 0;
7479
7480 while (*p != NUL && *p != ':')
7481 {
7482 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7483 what = *p++;
7484 else if (*p == COM_LEFT || *p == COM_RIGHT)
7485 align = *p++;
7486 else if (VIM_ISDIGIT(*p) || *p == '-')
7487 off = getdigits(&p);
7488 else
7489 ++p;
7490 }
7491
7492 if (*p == ':')
7493 ++p;
7494 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7495 if (what == COM_START)
7496 {
7497 STRCPY(lead_start, lead_end);
7498 lead_start_len = (int)STRLEN(lead_start);
7499 start_off = off;
7500 start_align = align;
7501 }
7502 else if (what == COM_MIDDLE)
7503 {
7504 STRCPY(lead_middle, lead_end);
7505 lead_middle_len = (int)STRLEN(lead_middle);
7506 }
7507 else if (what == COM_END)
7508 {
7509 /* If our line starts with the middle comment string, line it
7510 * up with the comment opener per the 'comments' option. */
7511 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7512 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7513 {
7514 done = TRUE;
7515 if (curwin->w_cursor.lnum > 1)
7516 {
7517 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007518 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 * the middle comment string matches in the previous
7520 * line, use the indent of that line. XXX */
7521 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7522 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7523 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7524 else if (STRNCMP(look, lead_middle,
7525 lead_middle_len) == 0)
7526 {
7527 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7528 break;
7529 }
7530 /* If the start comment string doesn't match with the
7531 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007532 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 lead_start, lead_start_len) != 0)
7534 continue;
7535 }
7536 if (start_off != 0)
7537 amount += start_off;
7538 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007539 amount += vim_strsize(lead_start)
7540 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 break;
7542 }
7543
7544 /* If our line starts with the end comment string, line it up
7545 * with the middle comment */
7546 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7547 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7548 {
7549 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7550 /* XXX */
7551 if (off != 0)
7552 amount += off;
7553 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007554 amount += vim_strsize(lead_start)
7555 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 done = TRUE;
7557 break;
7558 }
7559 }
7560 }
7561
7562 /* If our line starts with an asterisk, line up with the
7563 * asterisk in the comment opener; otherwise, line up
7564 * with the first character of the comment text.
7565 */
7566 if (done)
7567 ;
7568 else if (theline[0] == '*')
7569 amount += 1;
7570 else
7571 {
7572 /*
7573 * If we are more than one line away from the comment opener, take
7574 * the indent of the previous non-empty line. If 'cino' has "CO"
7575 * and we are just below the comment opener and there are any
7576 * white characters after it line up with the text after it;
7577 * otherwise, add the amount specified by "c" in 'cino'
7578 */
7579 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007580 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 {
7582 if (linewhite(lnum)) /* skip blank lines */
7583 continue;
7584 amount = get_indent_lnum(lnum); /* XXX */
7585 break;
7586 }
7587 if (amount == -1) /* use the comment opener */
7588 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007589 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007591 start = ml_get(comment_pos->lnum);
7592 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007594 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007596 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007598 if (curbuf->b_ind_in_comment2 || *look == NUL)
7599 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 }
7601 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007602 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 }
7604
7605 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007606 * Are we looking at a ']' that has a match?
7607 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007608 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007609 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7610 {
7611 /* align with the line containing the '['. */
7612 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007613 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007614 }
7615
7616 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 * Are we inside parentheses or braces?
7618 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007619 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007620 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007621 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 || trypos != NULL)
7623 {
7624 if (trypos != NULL && tryposBrace != NULL)
7625 {
7626 /* Both an unmatched '(' and '{' is found. Use the one which is
7627 * closer to the current cursor position, set the other to NULL. */
7628 if (trypos->lnum != tryposBrace->lnum
7629 ? trypos->lnum < tryposBrace->lnum
7630 : trypos->col < tryposBrace->col)
7631 trypos = NULL;
7632 else
7633 tryposBrace = NULL;
7634 }
7635
7636 if (trypos != NULL)
7637 {
7638 /*
7639 * If the matching paren is more than one line away, use the indent of
7640 * a previous non-empty line that matches the same paren.
7641 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007642 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007644 /* Line up with the start of the matching paren line. */
7645 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7646 }
7647 else
7648 {
7649 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007650 our_paren_pos = *trypos;
7651 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007653 l = skipwhite(ml_get(lnum));
7654 if (cin_nocode(l)) /* skip comment lines */
7655 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007656 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007657 continue; /* ignore #define, #if, etc. */
7658 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007660 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007661 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007662 {
7663 lnum = trypos->lnum + 1;
7664 continue;
7665 }
7666
7667 /* XXX */
7668 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007669 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007670 && trypos->lnum == our_paren_pos.lnum
7671 && trypos->col == our_paren_pos.col)
7672 {
7673 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007675 if (theline[0] == ')')
7676 {
7677 if (our_paren_pos.lnum != lnum
7678 && cur_amount > amount)
7679 cur_amount = amount;
7680 amount = -1;
7681 }
7682 break;
7683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 }
7685 }
7686
7687 /*
7688 * Line up with line where the matching paren is. XXX
7689 * If the line starts with a '(' or the indent for unclosed
7690 * parentheses is zero, line up with the unclosed parentheses.
7691 */
7692 if (amount == -1)
7693 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007694 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007695 int is_if_for_while = 0;
7696
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007697 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007698 {
7699 /* Look for the outermost opening parenthesis on this line
7700 * and check whether it belongs to an "if", "for" or "while". */
7701
7702 pos_T cursor_save = curwin->w_cursor;
7703 pos_T outermost;
7704 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007705
7706 trypos = &our_paren_pos;
7707 do {
7708 outermost = *trypos;
7709 curwin->w_cursor.lnum = outermost.lnum;
7710 curwin->w_cursor.col = outermost.col;
7711
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007712 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007713 } while (trypos && trypos->lnum == outermost.lnum);
7714
7715 curwin->w_cursor = cursor_save;
7716
7717 line = ml_get(outermost.lnum);
7718
7719 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007720 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007721 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007722
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007723 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007724 look = skipwhite(look);
7725 if (*look == '(')
7726 {
7727 linenr_T save_lnum = curwin->w_cursor.lnum;
7728 char_u *line;
7729 int look_col;
7730
7731 /* Ignore a '(' in front of the line that has a match before
7732 * our matching '('. */
7733 curwin->w_cursor.lnum = our_paren_pos.lnum;
7734 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007735 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007736 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007737 if ((trypos = findmatchlimit(NULL, ')', 0,
7738 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007739 != NULL
7740 && trypos->lnum == our_paren_pos.lnum
7741 && trypos->col < our_paren_pos.col)
7742 ignore_paren_col = trypos->col + 1;
7743
7744 curwin->w_cursor.lnum = save_lnum;
7745 look = ml_get(our_paren_pos.lnum) + look_col;
7746 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007747 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7748 && is_if_for_while == 0)
7749 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007750 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {
7752 /*
7753 * If we're looking at a close paren, line up right there;
7754 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007755 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 * the last nonwhite character of the line, use either the
7757 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007758 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 * lines).
7760 */
7761 if (theline[0] != ')')
7762 {
7763 cur_amount = MAXCOL;
7764 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007765 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 && cin_ends_in(l, (char_u *)"(", NULL))
7767 {
7768 /* look for opening unmatched paren, indent one level
7769 * for each additional level */
7770 n = 1;
7771 for (col = 0; col < our_paren_pos.col; ++col)
7772 {
7773 switch (l[col])
7774 {
7775 case '(':
7776 case '{': ++n;
7777 break;
7778
7779 case ')':
7780 case '}': if (n > 1)
7781 --n;
7782 break;
7783 }
7784 }
7785
7786 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007787 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007789 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 our_paren_pos.col++;
7791 else
7792 {
7793 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007794 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 col++;
7796 if (l[col] != NUL) /* In case of trailing space */
7797 our_paren_pos.col = col;
7798 else
7799 our_paren_pos.col++;
7800 }
7801 }
7802
7803 /*
7804 * Find how indented the paren is, or the character after it
7805 * if we did the above "if".
7806 */
7807 if (our_paren_pos.col > 0)
7808 {
7809 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7810 if (cur_amount > (int)col)
7811 cur_amount = col;
7812 }
7813 }
7814
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007815 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {
7817 /* Line up with the start of the matching paren line. */
7818 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007819 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7820 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007821 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {
7823 if (cur_amount != MAXCOL)
7824 amount = cur_amount;
7825 }
7826 else
7827 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007828 /* Add b_ind_unclosed2 for each '(' before our matching one,
7829 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007831 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {
7833 --our_paren_pos.col;
7834 switch (*ml_get_pos(&our_paren_pos))
7835 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007836 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 col = our_paren_pos.col;
7838 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007839 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 col = MAXCOL;
7841 break;
7842 }
7843 }
7844
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007845 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 * braces */
7847 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007848 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 else
7850 {
7851 curwin->w_cursor.lnum = our_paren_pos.lnum;
7852 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007853 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7854 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007855 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007857 {
7858 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007859 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007860 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007861 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 }
7864 /*
7865 * For a line starting with ')' use the minimum of the two
7866 * positions, to avoid giving it more indent than the previous
7867 * lines:
7868 * func_long_name( if (x
7869 * arg && yy
7870 * ) ^ not here ) ^ not here
7871 */
7872 if (cur_amount < amount)
7873 amount = cur_amount;
7874 }
7875 }
7876
7877 /* add extra indent for a comment */
7878 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007879 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 else
7882 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007883 /*
7884 * We are inside braces, there is a { before this line at the position
7885 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007886 * Make a copy of tryposBrace, it may point to pos_copy inside
7887 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007888 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007889 tryposCopy = *tryposBrace;
7890 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 ourscope = trypos->lnum;
7893 start = ml_get(ourscope);
7894
7895 /*
7896 * Now figure out how indented the line is in general.
7897 * If the brace was at the start of the line, we use that;
7898 * otherwise, check out the indentation of the line as
7899 * a whole and then add the "imaginary indent" to that.
7900 */
7901 look = skipwhite(start);
7902 if (*look == '{')
7903 {
7904 getvcol(curwin, trypos, &col, NULL, NULL);
7905 amount = col;
7906 if (*start == '{')
7907 start_brace = BRACE_IN_COL0;
7908 else
7909 start_brace = BRACE_AT_START;
7910 }
7911 else
7912 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007913 /* That opening brace might have been on a continuation
7914 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 curwin->w_cursor.lnum = ourscope;
7916
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007917 /* Position the cursor over the rightmost paren, so that
7918 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 lnum = ourscope;
7920 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007921 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7922 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 lnum = trypos->lnum;
7924
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007925 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 * case 1: if (asdf &&
7927 * ldfd) {
7928 * }
7929 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007930 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7931 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007933 else if (curbuf->b_ind_js)
7934 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007936 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937
7938 start_brace = BRACE_AT_END;
7939 }
7940
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007941 /* For Javascript check if the line starts with "key:". */
7942 if (curbuf->b_ind_js)
7943 js_cur_has_key = cin_has_js_key(theline);
7944
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007946 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 * we want to be. otherwise, add the amount of room
7948 * that an indent is supposed to be.
7949 */
7950 if (theline[0] == '}')
7951 {
7952 /*
7953 * they may want closing braces to line up with something
7954 * other than the open brace. indulge them, if so.
7955 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007956 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 }
7958 else
7959 {
7960 /*
7961 * If we're looking at an "else", try to find an "if"
7962 * to match it with.
7963 * If we're looking at a "while", try to find a "do"
7964 * to match it with.
7965 */
7966 lookfor = LOOKFOR_INITIAL;
7967 if (cin_iselse(theline))
7968 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007969 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 lookfor = LOOKFOR_DO;
7971 if (lookfor != LOOKFOR_INITIAL)
7972 {
7973 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007974 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {
7976 amount = get_indent(); /* XXX */
7977 goto theend;
7978 }
7979 }
7980
7981 /*
7982 * We get here if we are not on an "while-of-do" or "else" (or
7983 * failed to find a matching "if").
7984 * Search backwards for something to line up with.
7985 * First set amount for when we don't find anything.
7986 */
7987
7988 /*
7989 * if the '{' is _really_ at the left margin, use the imaginary
7990 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007991 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 */
7993
7994 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7995 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007996 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007997 lookfor_cpp_namespace = TRUE;
7998 }
7999 else if (start_brace == BRACE_AT_START &&
8000 lookfor_cpp_namespace) /* '{' is at start */
8001 {
8002
8003 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 }
8005 else
8006 {
8007 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008008 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008009 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008010
8011 l = skipwhite(ml_get_curline());
8012 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008013 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008014 else if (cin_is_cpp_extern_c(l))
8015 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 else
8018 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008019 /* Compensate for adding b_ind_open_extra later. */
8020 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 if (amount < 0)
8022 amount = 0;
8023 }
8024 }
8025
8026 lookfor_break = FALSE;
8027
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008028 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {
8030 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008031 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 }
8033 else if (cin_isscopedecl(theline)) /* private:, ... */
8034 {
8035 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008036 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 }
8038 else
8039 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008040 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8041 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 lookfor_break = TRUE;
8043
8044 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008045 /* b_ind_level from start of block */
8046 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 }
8048 scope_amount = amount;
8049 whilelevel = 0;
8050
8051 /*
8052 * Search backwards. If we find something we recognize, line up
8053 * with that.
8054 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008055 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 * the usual amount relative to the conditional
8057 * that opens the block.
8058 */
8059 curwin->w_cursor = cur_curpos;
8060 for (;;)
8061 {
8062 curwin->w_cursor.lnum--;
8063 curwin->w_cursor.col = 0;
8064
8065 /*
8066 * If we went all the way back to the start of our scope, line
8067 * up with it.
8068 */
8069 if (curwin->w_cursor.lnum <= ourscope)
8070 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008071 /* We reached end of scope:
8072 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008074 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 * don't add ind_continuation, otherwise it is a variable
8076 * declaration:
8077 * int x,
8078 * here; <-- add ind_continuation
8079 */
8080 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8081 {
8082 if (curwin->w_cursor.lnum == 0
8083 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008084 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008086 /* nothing found (abuse curbuf->b_ind_maxparen as
8087 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 * initialization) */
8089 if (cont_amount > 0)
8090 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008091 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 amount += ind_continuation;
8093 break;
8094 }
8095
8096 l = ml_get_curline();
8097
8098 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008099 * If we're in a comment or raw string now, skip to
8100 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008102 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 if (trypos != NULL)
8104 {
8105 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008106 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 continue;
8108 }
8109
8110 /*
8111 * Skip preprocessor directives and blank lines.
8112 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008113 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8114 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 continue;
8116
8117 if (cin_nocode(l))
8118 continue;
8119
8120 terminated = cin_isterminated(l, FALSE, TRUE);
8121
8122 /*
8123 * If we are at top level and the line looks like a
8124 * function declaration, we are done
8125 * (it's a variable declaration).
8126 */
8127 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008128 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {
8130 /* if the line is terminated with another ','
8131 * it is a continued variable initialization.
8132 * don't add extra indent.
8133 * TODO: does not work, if a function
8134 * declaration is split over multiple lines:
8135 * cin_isfuncdecl returns FALSE then.
8136 */
8137 if (terminated == ',')
8138 break;
8139
8140 /* if it es a enum declaration or an assignment,
8141 * we are done.
8142 */
8143 if (terminated != ';' && cin_isinit())
8144 break;
8145
8146 /* nothing useful found */
8147 if (terminated == 0 || terminated == '{')
8148 continue;
8149 }
8150
8151 if (terminated != ';')
8152 {
8153 /* Skip parens and braces. Position the cursor
8154 * over the rightmost paren, so that matching it
8155 * will take us back to the start of the line.
8156 */ /* XXX */
8157 trypos = NULL;
8158 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008159 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008160 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161
8162 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008163 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164
8165 if (trypos != NULL)
8166 {
8167 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008168 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 continue;
8170 }
8171 }
8172
8173 /* it's a variable declaration, add indentation
8174 * like in
8175 * int a,
8176 * b;
8177 */
8178 if (cont_amount > 0)
8179 amount = cont_amount;
8180 else
8181 amount += ind_continuation;
8182 }
8183 else if (lookfor == LOOKFOR_UNTERM)
8184 {
8185 if (cont_amount > 0)
8186 amount = cont_amount;
8187 else
8188 amount += ind_continuation;
8189 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008190 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008191 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008192 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008193 && lookfor != LOOKFOR_CPP_BASECLASS
8194 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008195 {
8196 amount = scope_amount;
8197 if (theline[0] == '{')
8198 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008199 amount += curbuf->b_ind_open_extra;
8200 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008201 }
8202 }
8203
8204 if (lookfor_cpp_namespace)
8205 {
8206 /*
8207 * Looking for C++ namespace, need to look further
8208 * back.
8209 */
8210 if (curwin->w_cursor.lnum == ourscope)
8211 continue;
8212
8213 if (curwin->w_cursor.lnum == 0
8214 || curwin->w_cursor.lnum
8215 < ourscope - FIND_NAMESPACE_LIM)
8216 break;
8217
8218 l = ml_get_curline();
8219
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008220 /* If we're in a comment or raw string now, skip
8221 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008222 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008223 if (trypos != NULL)
8224 {
8225 curwin->w_cursor.lnum = trypos->lnum + 1;
8226 curwin->w_cursor.col = 0;
8227 continue;
8228 }
8229
8230 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008231 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8232 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008233 continue;
8234
8235 /* Finally the actual check for "namespace". */
8236 if (cin_is_cpp_namespace(l))
8237 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008238 amount += curbuf->b_ind_cpp_namespace
8239 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008240 break;
8241 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008242 else if (cin_is_cpp_extern_c(l))
8243 {
8244 amount += curbuf->b_ind_cpp_extern_c
8245 - added_to_amount;
8246 break;
8247 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008248
8249 if (cin_nocode(l))
8250 continue;
8251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 }
8253 break;
8254 }
8255
8256 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008257 * If we're in a comment or raw string now, skip to the start
8258 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008260 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {
8262 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008263 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 continue;
8265 }
8266
8267 l = ml_get_curline();
8268
8269 /*
8270 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008271 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008273 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 if (iscase || cin_isscopedecl(l))
8275 {
8276 /* we are only looking for cpp base class
8277 * declaration/initialization any longer */
8278 if (lookfor == LOOKFOR_CPP_BASECLASS)
8279 break;
8280
8281 /* When looking for a "do" we are not interested in
8282 * labels. */
8283 if (whilelevel > 0)
8284 continue;
8285
8286 /*
8287 * case xx:
8288 * c = 99 + <- this indent plus continuation
8289 *-> here;
8290 */
8291 if (lookfor == LOOKFOR_UNTERM
8292 || lookfor == LOOKFOR_ENUM_OR_INIT)
8293 {
8294 if (cont_amount > 0)
8295 amount = cont_amount;
8296 else
8297 amount += ind_continuation;
8298 break;
8299 }
8300
8301 /*
8302 * case xx: <- line up with this case
8303 * x = 333;
8304 * case yy:
8305 */
8306 if ( (iscase && lookfor == LOOKFOR_CASE)
8307 || (iscase && lookfor_break)
8308 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8309 {
8310 /*
8311 * Check that this case label is not for another
8312 * switch()
8313 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008314 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008315 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {
8317 amount = get_indent(); /* XXX */
8318 break;
8319 }
8320 continue;
8321 }
8322
8323 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8324
8325 /*
8326 * case xx: if (cond) <- line up with this if
8327 * y = y + 1;
8328 * -> s = 99;
8329 *
8330 * case xx:
8331 * if (cond) <- line up with this line
8332 * y = y + 1;
8333 * -> s = 99;
8334 */
8335 if (lookfor == LOOKFOR_TERM)
8336 {
8337 if (n)
8338 amount = n;
8339
8340 if (!lookfor_break)
8341 break;
8342 }
8343
8344 /*
8345 * case xx: x = x + 1; <- line up with this x
8346 * -> y = y + 1;
8347 *
8348 * case xx: if (cond) <- line up with this if
8349 * -> y = y + 1;
8350 */
8351 if (n)
8352 {
8353 amount = n;
8354 l = after_label(ml_get_curline());
8355 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008356 {
8357 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008358 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008359 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008360 amount += curbuf->b_ind_level
8361 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 break;
8364 }
8365
8366 /*
8367 * Try to get the indent of a statement before the switch
8368 * label. If nothing is found, line up relative to the
8369 * switch label.
8370 * break; <- may line up with this line
8371 * case xx:
8372 * -> y = 1;
8373 */
8374 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008375 ? curbuf->b_ind_case_code
8376 : curbuf->b_ind_scopedecl_code);
8377 lookfor = curbuf->b_ind_case_break
8378 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 continue;
8380 }
8381
8382 /*
8383 * Looking for a switch() label or C++ scope declaration,
8384 * ignore other lines, skip {}-blocks.
8385 */
8386 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8387 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008388 if (find_last_paren(l, '{', '}')
8389 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008392 curwin->w_cursor.col = 0;
8393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 continue;
8395 }
8396
8397 /*
8398 * Ignore jump labels with nothing after them.
8399 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008400 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 {
8402 l = after_label(ml_get_curline());
8403 if (l == NULL || cin_nocode(l))
8404 continue;
8405 }
8406
8407 /*
8408 * Ignore #defines, #if, etc.
8409 * Ignore comment and empty lines.
8410 * (need to get the line again, cin_islabel() may have
8411 * unlocked it)
8412 */
8413 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008414 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 || cin_nocode(l))
8416 continue;
8417
8418 /*
8419 * Are we at the start of a cpp base class declaration or
8420 * constructor initialization?
8421 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008422 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008423 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008424 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008425 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008426 l = ml_get_curline();
8427 }
8428 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {
8430 if (lookfor == LOOKFOR_UNTERM)
8431 {
8432 if (cont_amount > 0)
8433 amount = cont_amount;
8434 else
8435 amount += ind_continuation;
8436 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008437 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008439 /* Need to find start of the declaration. */
8440 lookfor = LOOKFOR_UNTERM;
8441 ind_continuation = 0;
8442 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 }
8444 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008445 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008446 amount = get_baseclass_amount(
8447 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 break;
8449 }
8450 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8451 {
8452 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008453 * declaration or initialization before the opening brace.
8454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 if (cin_isterminated(l, TRUE, FALSE))
8456 break;
8457 else
8458 continue;
8459 }
8460
8461 /*
8462 * What happens next depends on the line being terminated.
8463 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008464 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 * 123,
8466 * sizeof
8467 * here
8468 * Otherwise check whether it is a enumeration or structure
8469 * initialisation (not indented) or a variable declaration
8470 * (indented).
8471 */
8472 terminated = cin_isterminated(l, FALSE, TRUE);
8473
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008474 if (js_cur_has_key)
8475 {
8476 js_cur_has_key = 0; /* only check the first line */
8477 if (curbuf->b_ind_js && terminated == ',')
8478 {
8479 /* For Javascript we might be inside an object:
8480 * key: something, <- align with this
8481 * key: something
8482 * or:
8483 * key: something + <- align with this
8484 * something,
8485 * key: something
8486 */
8487 lookfor = LOOKFOR_JS_KEY;
8488 }
8489 }
8490 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8491 {
8492 amount = get_indent();
8493 break;
8494 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008495 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008496 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008497 if (tryposBrace != NULL && tryposBrace->lnum
8498 >= curwin->w_cursor.lnum)
8499 break;
8500 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008501 /* line below current line is the one that starts a
8502 * (possibly broken) line ending in a comma. */
8503 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008504 else
8505 {
8506 amount = get_indent();
8507 if (curwin->w_cursor.lnum - 1 == ourscope)
8508 /* line above is start of the scope, thus current
8509 * line is the one that stars a (possibly broken)
8510 * line ending in a comma. */
8511 break;
8512 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008513 }
8514
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8516 && terminated == ','))
8517 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008518 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8519 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008520 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 /*
8522 * if we're in the middle of a paren thing,
8523 * go back to the line that starts it so
8524 * we can get the right prevailing indent
8525 * if ( foo &&
8526 * bar )
8527 */
8528 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008529 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008531 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 */
8533 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008534 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008535 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8536 || (trypos->lnum == tryposBrace->lnum
8537 && trypos->col < tryposBrace->col)))
8538 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539
8540 /*
8541 * If we are looking for ',', we also look for matching
8542 * braces.
8543 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008544 if (trypos == NULL && terminated == ','
8545 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008546 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547
8548 if (trypos != NULL)
8549 {
8550 /*
8551 * Check if we are on a case label now. This is
8552 * handled above.
8553 * case xx: if ( asdf &&
8554 * asdf)
8555 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008556 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008558 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {
8560 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008561 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 continue;
8563 }
8564 }
8565
8566 /*
8567 * Skip over continuation lines to find the one to get the
8568 * indent from
8569 * char *usethis = "bla\
8570 * bla",
8571 * here;
8572 */
8573 if (terminated == ',')
8574 {
8575 while (curwin->w_cursor.lnum > 1)
8576 {
8577 l = ml_get(curwin->w_cursor.lnum - 1);
8578 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8579 break;
8580 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008581 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 }
8583 }
8584
8585 /*
8586 * Get indent and pointer to text for current line,
8587 * ignoring any jump label. XXX
8588 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008589 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008590 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008591 else
8592 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 /*
8594 * If this is just above the line we are indenting, and it
8595 * starts with a '{', line it up with this line.
8596 * while (not)
8597 * -> {
8598 * }
8599 */
8600 if (terminated != ',' && lookfor != LOOKFOR_TERM
8601 && theline[0] == '{')
8602 {
8603 amount = cur_amount;
8604 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008605 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 * doesn't start with a '{', which must have a match
8607 * in the same line (scope is the same). Probably:
8608 * { 1, 2 },
8609 * -> { 3, 4 }
8610 */
8611 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008612 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008614 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 {
8616 /* have to look back, whether it is a cpp base
8617 * class declaration or initialization */
8618 lookfor = LOOKFOR_CPP_BASECLASS;
8619 continue;
8620 }
8621 break;
8622 }
8623
8624 /*
8625 * Check if we are after an "if", "while", etc.
8626 * Also allow " } else".
8627 */
8628 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8629 {
8630 /*
8631 * Found an unterminated line after an if (), line up
8632 * with the last one.
8633 * if (cond)
8634 * 100 +
8635 * -> here;
8636 */
8637 if (lookfor == LOOKFOR_UNTERM
8638 || lookfor == LOOKFOR_ENUM_OR_INIT)
8639 {
8640 if (cont_amount > 0)
8641 amount = cont_amount;
8642 else
8643 amount += ind_continuation;
8644 break;
8645 }
8646
8647 /*
8648 * If this is just above the line we are indenting, we
8649 * are finished.
8650 * while (not)
8651 * -> here;
8652 * Otherwise this indent can be used when the line
8653 * before this is terminated.
8654 * yyy;
8655 * if (stat)
8656 * while (not)
8657 * xxx;
8658 * -> here;
8659 */
8660 amount = cur_amount;
8661 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008662 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 if (lookfor != LOOKFOR_TERM)
8664 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008665 amount += curbuf->b_ind_level
8666 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 break;
8668 }
8669
8670 /*
8671 * Special trick: when expecting the while () after a
8672 * do, line up with the while()
8673 * do
8674 * x = 1;
8675 * -> here
8676 */
8677 l = skipwhite(ml_get_curline());
8678 if (cin_isdo(l))
8679 {
8680 if (whilelevel == 0)
8681 break;
8682 --whilelevel;
8683 }
8684
8685 /*
8686 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008687 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 * Need to use the scope of this "else". XXX
8689 * If whilelevel != 0 continue looking for a "do {".
8690 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008691 if (cin_iselse(l) && whilelevel == 0)
8692 {
8693 /* If we're looking at "} else", let's make sure we
8694 * find the opening brace of the enclosing scope,
8695 * not the one from "if () {". */
8696 if (*l == '}')
8697 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008698 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008699
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008700 if ((trypos = find_start_brace()) == NULL
8701 || find_match(LOOKFOR_IF, trypos->lnum)
8702 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008703 break;
8704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 }
8706
8707 /*
8708 * If we're below an unterminated line that is not an
8709 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008710 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 * the line before this one.
8712 */
8713 else
8714 {
8715 /*
8716 * Found two unterminated lines on a row, line up with
8717 * the last one.
8718 * c = 99 +
8719 * 100 +
8720 * -> here;
8721 */
8722 if (lookfor == LOOKFOR_UNTERM)
8723 {
8724 /* When line ends in a comma add extra indent */
8725 if (terminated == ',')
8726 amount += ind_continuation;
8727 break;
8728 }
8729
8730 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8731 {
8732 /* Found two lines ending in ',', lineup with the
8733 * lowest one, but check for cpp base class
8734 * declaration/initialization, if it is an
8735 * opening brace or we are looking just for
8736 * enumerations/initializations. */
8737 if (terminated == ',')
8738 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008739 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 break;
8741
8742 lookfor = LOOKFOR_CPP_BASECLASS;
8743 continue;
8744 }
8745
8746 /* Ignore unterminated lines in between, but
8747 * reduce indent. */
8748 if (amount > cur_amount)
8749 amount = cur_amount;
8750 }
8751 else
8752 {
8753 /*
8754 * Found first unterminated line on a row, may
8755 * line up with this line, remember its indent
8756 * 100 +
8757 * -> here;
8758 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008759 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008761
8762 n = (int)STRLEN(l);
8763 if (terminated == ',' && (*skipwhite(l) == ']'
8764 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766
8767 /*
8768 * If previous line ends in ',', check whether we
8769 * are in an initialization or enum
8770 * struct xxx =
8771 * {
8772 * sizeof a,
8773 * 124 };
8774 * or a normal possible continuation line.
8775 * but only, of no other statement has been found
8776 * yet.
8777 */
8778 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8779 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008780 if (curbuf->b_ind_js)
8781 {
8782 /* Search for a line ending in a comma
8783 * and line up with the line below it
8784 * (could be the current line).
8785 * some = [
8786 * 1, <- line up here
8787 * 2,
8788 * some = [
8789 * 3 + <- line up here
8790 * 4 *
8791 * 5,
8792 * 6,
8793 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008794 if (cin_iscomment(skipwhite(l)))
8795 break;
8796 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008797 trypos = find_match_char('[',
8798 curbuf->b_ind_maxparen);
8799 if (trypos != NULL)
8800 {
8801 if (trypos->lnum
8802 == curwin->w_cursor.lnum - 1)
8803 {
8804 /* Current line is first inside
8805 * [], line up with it. */
8806 break;
8807 }
8808 ourscope = trypos->lnum;
8809 }
8810 }
8811 else
8812 {
8813 lookfor = LOOKFOR_ENUM_OR_INIT;
8814 cont_amount = cin_first_id_amount();
8815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 }
8817 else
8818 {
8819 if (lookfor == LOOKFOR_INITIAL
8820 && *l != NUL
8821 && l[STRLEN(l) - 1] == '\\')
8822 /* XXX */
8823 cont_amount = cin_get_equal_amount(
8824 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008825 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008826 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008827 && lookfor != LOOKFOR_COMMA
8828 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 lookfor = LOOKFOR_UNTERM;
8830 }
8831 }
8832 }
8833 }
8834
8835 /*
8836 * Check if we are after a while (cond);
8837 * If so: Ignore until the matching "do".
8838 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008839 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 {
8841 /*
8842 * Found an unterminated line after a while ();, line up
8843 * with the last one.
8844 * while (cond);
8845 * 100 + <- line up with this one
8846 * -> here;
8847 */
8848 if (lookfor == LOOKFOR_UNTERM
8849 || lookfor == LOOKFOR_ENUM_OR_INIT)
8850 {
8851 if (cont_amount > 0)
8852 amount = cont_amount;
8853 else
8854 amount += ind_continuation;
8855 break;
8856 }
8857
8858 if (whilelevel == 0)
8859 {
8860 lookfor = LOOKFOR_TERM;
8861 amount = get_indent(); /* XXX */
8862 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008863 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 }
8865 ++whilelevel;
8866 }
8867
8868 /*
8869 * We are after a "normal" statement.
8870 * If we had another statement we can stop now and use the
8871 * indent of that other statement.
8872 * Otherwise the indent of the current statement may be used,
8873 * search backwards for the next "normal" statement.
8874 */
8875 else
8876 {
8877 /*
8878 * Skip single break line, if before a switch label. It
8879 * may be lined up with the case label.
8880 */
8881 if (lookfor == LOOKFOR_NOBREAK
8882 && cin_isbreak(skipwhite(ml_get_curline())))
8883 {
8884 lookfor = LOOKFOR_ANY;
8885 continue;
8886 }
8887
8888 /*
8889 * Handle "do {" line.
8890 */
8891 if (whilelevel > 0)
8892 {
8893 l = cin_skipcomment(ml_get_curline());
8894 if (cin_isdo(l))
8895 {
8896 amount = get_indent(); /* XXX */
8897 --whilelevel;
8898 continue;
8899 }
8900 }
8901
8902 /*
8903 * Found a terminated line above an unterminated line. Add
8904 * the amount for a continuation line.
8905 * x = 1;
8906 * y = foo +
8907 * -> here;
8908 * or
8909 * int x = 1;
8910 * int foo,
8911 * -> here;
8912 */
8913 if (lookfor == LOOKFOR_UNTERM
8914 || lookfor == LOOKFOR_ENUM_OR_INIT)
8915 {
8916 if (cont_amount > 0)
8917 amount = cont_amount;
8918 else
8919 amount += ind_continuation;
8920 break;
8921 }
8922
8923 /*
8924 * Found a terminated line above a terminated line or "if"
8925 * etc. line. Use the amount of the line below us.
8926 * x = 1; x = 1;
8927 * if (asdf) y = 2;
8928 * while (asdf) ->here;
8929 * here;
8930 * ->foo;
8931 */
8932 if (lookfor == LOOKFOR_TERM)
8933 {
8934 if (!lookfor_break && whilelevel == 0)
8935 break;
8936 }
8937
8938 /*
8939 * First line above the one we're indenting is terminated.
8940 * To know what needs to be done look further backward for
8941 * a terminated line.
8942 */
8943 else
8944 {
8945 /*
8946 * position the cursor over the rightmost paren, so
8947 * that matching it will take us back to the start of
8948 * the line. Helps for:
8949 * func(asdr,
8950 * asdfasdf);
8951 * here;
8952 */
8953term_again:
8954 l = ml_get_curline();
8955 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008956 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008957 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 {
8959 /*
8960 * Check if we are on a case label now. This is
8961 * handled above.
8962 * case xx: if ( asdf &&
8963 * asdf)
8964 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008965 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008967 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 {
8969 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008970 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 continue;
8972 }
8973 }
8974
8975 /* When aligning with the case statement, don't align
8976 * with a statement after it.
8977 * case 1: { <-- don't use this { position
8978 * stat;
8979 * }
8980 * case 2:
8981 * stat;
8982 * }
8983 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008984 iscase = (curbuf->b_ind_keep_case_label
8985 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986
8987 /*
8988 * Get indent and pointer to text for current line,
8989 * ignoring any jump label.
8990 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008991 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992
8993 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008994 amount += curbuf->b_ind_open_extra;
8995 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008996 l = skipwhite(l);
8997 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008998 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9000
9001 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009002 * When a terminated line starts with "else" skip to
9003 * the matching "if":
9004 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009005 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009006 * Need to use the scope of this "else". XXX
9007 * If whilelevel != 0 continue looking for a "do {".
9008 */
9009 if (lookfor == LOOKFOR_TERM
9010 && *l != '}'
9011 && cin_iselse(l)
9012 && whilelevel == 0)
9013 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009014 if ((trypos = find_start_brace()) == NULL
9015 || find_match(LOOKFOR_IF, trypos->lnum)
9016 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009017 break;
9018 continue;
9019 }
9020
9021 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 * If we're at the end of a block, skip to the start of
9023 * that block.
9024 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009025 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009026 if (find_last_paren(l, '{', '}') /* XXX */
9027 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009029 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 /* if not "else {" check for terminated again */
9031 /* but skip block for "} else {" */
9032 l = cin_skipcomment(ml_get_curline());
9033 if (*l == '}' || !cin_iselse(l))
9034 goto term_again;
9035 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009036 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 }
9038 }
9039 }
9040 }
9041 }
9042 }
9043
9044 /* add extra indent for a comment */
9045 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009046 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009047
9048 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009049 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9050 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009051
9052 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009054
9055 /*
9056 * ok -- we're not inside any sort of structure at all!
9057 *
9058 * This means we're at the top level, and everything should
9059 * basically just match where the previous line is, except
9060 * for the lines immediately following a function declaration,
9061 * which are K&R-style parameters and need to be indented.
9062 *
9063 * if our line starts with an open brace, forget about any
9064 * prevailing indent and make sure it looks like the start
9065 * of a function
9066 */
9067
9068 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009070 amount = curbuf->b_ind_first_open;
9071 goto theend;
9072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009074 /*
9075 * If the NEXT line is a function declaration, the current
9076 * line needs to be indented as a function type spec.
9077 * Don't do this if the current line looks like a comment or if the
9078 * current line is terminated, ie. ends in ';', or if the current line
9079 * contains { or }: "void f() {\n if (1)"
9080 */
9081 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9082 && !cin_nocode(theline)
9083 && vim_strchr(theline, '{') == NULL
9084 && vim_strchr(theline, '}') == NULL
9085 && !cin_ends_in(theline, (char_u *)":", NULL)
9086 && !cin_ends_in(theline, (char_u *)",", NULL)
9087 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9088 cur_curpos.lnum + 1)
9089 && !cin_isterminated(theline, FALSE, TRUE))
9090 {
9091 amount = curbuf->b_ind_func_type;
9092 goto theend;
9093 }
9094
9095 /* search backwards until we find something we recognize */
9096 amount = 0;
9097 curwin->w_cursor = cur_curpos;
9098 while (curwin->w_cursor.lnum > 1)
9099 {
9100 curwin->w_cursor.lnum--;
9101 curwin->w_cursor.col = 0;
9102
9103 l = ml_get_curline();
9104
9105 /*
9106 * If we're in a comment or raw string now, skip to the start
9107 * of it.
9108 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009109 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009111 curwin->w_cursor.lnum = trypos->lnum + 1;
9112 curwin->w_cursor.col = 0;
9113 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 }
9115
9116 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009117 * Are we at the start of a cpp base class declaration or
9118 * constructor initialization?
9119 */ /* XXX */
9120 n = FALSE;
9121 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009123 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9124 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009126 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009128 /* XXX */
9129 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9130 break;
9131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009133 /*
9134 * Skip preprocessor directives and blank lines.
9135 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009136 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009137 continue;
9138
9139 if (cin_nocode(l))
9140 continue;
9141
9142 /*
9143 * If the previous line ends in ',', use one level of
9144 * indentation:
9145 * int foo,
9146 * bar;
9147 * do this before checking for '}' in case of eg.
9148 * enum foobar
9149 * {
9150 * ...
9151 * } foo,
9152 * bar;
9153 */
9154 n = 0;
9155 if (cin_ends_in(l, (char_u *)",", NULL)
9156 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9157 {
9158 /* take us back to opening paren */
9159 if (find_last_paren(l, '(', ')')
9160 && (trypos = find_match_paren(
9161 curbuf->b_ind_maxparen)) != NULL)
9162 curwin->w_cursor = *trypos;
9163
9164 /* For a line ending in ',' that is a continuation line go
9165 * back to the first line with a backslash:
9166 * char *foo = "bla\
9167 * bla",
9168 * here;
9169 */
9170 while (n == 0 && curwin->w_cursor.lnum > 1)
9171 {
9172 l = ml_get(curwin->w_cursor.lnum - 1);
9173 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9174 break;
9175 --curwin->w_cursor.lnum;
9176 curwin->w_cursor.col = 0;
9177 }
9178
9179 amount = get_indent(); /* XXX */
9180
9181 if (amount == 0)
9182 amount = cin_first_id_amount();
9183 if (amount == 0)
9184 amount = ind_continuation;
9185 break;
9186 }
9187
9188 /*
9189 * If the line looks like a function declaration, and we're
9190 * not in a comment, put it the left margin.
9191 */
9192 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9193 break;
9194 l = ml_get_curline();
9195
9196 /*
9197 * Finding the closing '}' of a previous function. Put
9198 * current line at the left margin. For when 'cino' has "fs".
9199 */
9200 if (*skipwhite(l) == '}')
9201 break;
9202
9203 /* (matching {)
9204 * If the previous line ends on '};' (maybe followed by
9205 * comments) align at column 0. For example:
9206 * char *string_array[] = { "foo",
9207 * / * x * / "b};ar" }; / * foobar * /
9208 */
9209 if (cin_ends_in(l, (char_u *)"};", NULL))
9210 break;
9211
9212 /*
9213 * If the previous line ends on '[' we are probably in an
9214 * array constant:
9215 * something = [
9216 * 234, <- extra indent
9217 */
9218 if (cin_ends_in(l, (char_u *)"[", NULL))
9219 {
9220 amount = get_indent() + ind_continuation;
9221 break;
9222 }
9223
9224 /*
9225 * Find a line only has a semicolon that belongs to a previous
9226 * line ending in '}', e.g. before an #endif. Don't increase
9227 * indent then.
9228 */
9229 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9230 {
9231 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
9233 while (curwin->w_cursor.lnum > 1)
9234 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009235 look = ml_get(--curwin->w_cursor.lnum);
9236 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009237 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009239 }
9240 if (curwin->w_cursor.lnum > 0
9241 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009244 curwin->w_cursor = curpos_save;
9245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009247 /*
9248 * If the PREVIOUS line is a function declaration, the current
9249 * line (and the ones that follow) needs to be indented as
9250 * parameters.
9251 */
9252 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9253 {
9254 amount = curbuf->b_ind_param;
9255 break;
9256 }
9257
9258 /*
9259 * If the previous line ends in ';' and the line before the
9260 * previous line ends in ',' or '\', ident to column zero:
9261 * int foo,
9262 * bar;
9263 * indent_to_0 here;
9264 */
9265 if (cin_ends_in(l, (char_u *)";", NULL))
9266 {
9267 l = ml_get(curwin->w_cursor.lnum - 1);
9268 if (cin_ends_in(l, (char_u *)",", NULL)
9269 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9270 break;
9271 l = ml_get_curline();
9272 }
9273
9274 /*
9275 * Doesn't look like anything interesting -- so just
9276 * use the indent of this line.
9277 *
9278 * Position the cursor over the rightmost paren, so that
9279 * matching it will take us back to the start of the line.
9280 */
9281 find_last_paren(l, '(', ')');
9282
9283 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9284 curwin->w_cursor = *trypos;
9285 amount = get_indent(); /* XXX */
9286 break;
9287 }
9288
9289 /* add extra indent for a comment */
9290 if (cin_iscomment(theline))
9291 amount += curbuf->b_ind_comment;
9292
9293 /* add extra indent if the previous line ended in a backslash:
9294 * "asdfasdf\
9295 * here";
9296 * char *foo = "asdf\
9297 * here";
9298 */
9299 if (cur_curpos.lnum > 1)
9300 {
9301 l = ml_get(cur_curpos.lnum - 1);
9302 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9303 {
9304 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9305 if (cur_amount > 0)
9306 amount = cur_amount;
9307 else if (cur_amount == 0)
9308 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 }
9310 }
9311
9312theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009313 if (amount < 0)
9314 amount = 0;
9315
9316laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 /* put the cursor back where it belongs */
9318 curwin->w_cursor = cur_curpos;
9319
9320 vim_free(linecopy);
9321
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 return amount;
9323}
9324
9325 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009326find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327{
9328 char_u *look;
9329 pos_T *theirscope;
9330 char_u *mightbeif;
9331 int elselevel;
9332 int whilelevel;
9333
9334 if (lookfor == LOOKFOR_IF)
9335 {
9336 elselevel = 1;
9337 whilelevel = 0;
9338 }
9339 else
9340 {
9341 elselevel = 0;
9342 whilelevel = 1;
9343 }
9344
9345 curwin->w_cursor.col = 0;
9346
9347 while (curwin->w_cursor.lnum > ourscope + 1)
9348 {
9349 curwin->w_cursor.lnum--;
9350 curwin->w_cursor.col = 0;
9351
9352 look = cin_skipcomment(ml_get_curline());
9353 if (cin_iselse(look)
9354 || cin_isif(look)
9355 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009356 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 {
9358 /*
9359 * if we've gone outside the braces entirely,
9360 * we must be out of scope...
9361 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009362 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 if (theirscope == NULL)
9364 break;
9365
9366 /*
9367 * and if the brace enclosing this is further
9368 * back than the one enclosing the else, we're
9369 * out of luck too.
9370 */
9371 if (theirscope->lnum < ourscope)
9372 break;
9373
9374 /*
9375 * and if they're enclosed in a *deeper* brace,
9376 * then we can ignore it because it's in a
9377 * different scope...
9378 */
9379 if (theirscope->lnum > ourscope)
9380 continue;
9381
9382 /*
9383 * if it was an "else" (that's not an "else if")
9384 * then we need to go back to another if, so
9385 * increment elselevel
9386 */
9387 look = cin_skipcomment(ml_get_curline());
9388 if (cin_iselse(look))
9389 {
9390 mightbeif = cin_skipcomment(look + 4);
9391 if (!cin_isif(mightbeif))
9392 ++elselevel;
9393 continue;
9394 }
9395
9396 /*
9397 * if it was a "while" then we need to go back to
9398 * another "do", so increment whilelevel. XXX
9399 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009400 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 {
9402 ++whilelevel;
9403 continue;
9404 }
9405
9406 /* If it's an "if" decrement elselevel */
9407 look = cin_skipcomment(ml_get_curline());
9408 if (cin_isif(look))
9409 {
9410 elselevel--;
9411 /*
9412 * When looking for an "if" ignore "while"s that
9413 * get in the way.
9414 */
9415 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9416 whilelevel = 0;
9417 }
9418
9419 /* If it's a "do" decrement whilelevel */
9420 if (cin_isdo(look))
9421 whilelevel--;
9422
9423 /*
9424 * if we've used up all the elses, then
9425 * this must be the if that we want!
9426 * match the indent level of that if.
9427 */
9428 if (elselevel <= 0 && whilelevel <= 0)
9429 {
9430 return OK;
9431 }
9432 }
9433 }
9434 return FAIL;
9435}
9436
9437# if defined(FEAT_EVAL) || defined(PROTO)
9438/*
9439 * Get indent level from 'indentexpr'.
9440 */
9441 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009442get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009444 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009445 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009446 pos_T save_pos;
9447 colnr_T save_curswant;
9448 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009450 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9451 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009453 /* Save and restore cursor position and curswant, in case it was changed
9454 * via :normal commands */
9455 save_pos = curwin->w_cursor;
9456 save_curswant = curwin->w_curswant;
9457 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009459 if (use_sandbox)
9460 ++sandbox;
9461 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009462
9463 /* Need to make a copy, the 'indentexpr' option could be changed while
9464 * evaluating it. */
9465 inde_copy = vim_strsave(curbuf->b_p_inde);
9466 if (inde_copy != NULL)
9467 {
9468 indent = (int)eval_to_number(inde_copy);
9469 vim_free(inde_copy);
9470 }
9471
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009472 if (use_sandbox)
9473 --sandbox;
9474 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475
9476 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9477 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9478 * command. */
9479 save_State = State;
9480 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009481 curwin->w_cursor = save_pos;
9482 curwin->w_curswant = save_curswant;
9483 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 check_cursor();
9485 State = save_State;
9486
9487 /* If there is an error, just keep the current indent. */
9488 if (indent < 0)
9489 indent = get_indent();
9490
9491 return indent;
9492}
9493# endif
9494
9495#endif /* FEAT_CINDENT */
9496
9497#if defined(FEAT_LISP) || defined(PROTO)
9498
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009500lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502 char_u buf[LSIZE];
9503 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009504 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505
9506 while (*word != NUL)
9507 {
9508 (void)copy_option_part(&word, buf, LSIZE, ",");
9509 len = (int)STRLEN(buf);
9510 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9511 return TRUE;
9512 }
9513 return FALSE;
9514}
9515
9516/*
9517 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9518 * The incompatible newer method is quite a bit better at indenting
9519 * code in lisp-like languages than the traditional one; it's still
9520 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9521 *
9522 * TODO:
9523 * Findmatch() should be adapted for lisp, also to make showmatch
9524 * work correctly: now (v5.3) it seems all C/C++ oriented:
9525 * - it does not recognize the #\( and #\) notations as character literals
9526 * - it doesn't know about comments starting with a semicolon
9527 * - it incorrectly interprets '(' as a character literal
9528 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009529 * Update from Sergey Khorev:
9530 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 */
9532 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009533get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009535 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 int amount;
9537 char_u *that;
9538 colnr_T col;
9539 colnr_T firsttry;
9540 int parencount, quotecount;
9541 int vi_lisp;
9542
9543 /* Set vi_lisp to use the vi-compatible method */
9544 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9545
9546 realpos = curwin->w_cursor;
9547 curwin->w_cursor.col = 0;
9548
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009549 if ((pos = findmatch(NULL, '(')) == NULL)
9550 pos = findmatch(NULL, '[');
9551 else
9552 {
9553 paren = *pos;
9554 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009555 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009556 pos = &paren;
9557 }
9558 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 {
9560 /* Extra trick: Take the indent of the first previous non-white
9561 * line that is at the same () level. */
9562 amount = -1;
9563 parencount = 0;
9564
9565 while (--curwin->w_cursor.lnum >= pos->lnum)
9566 {
9567 if (linewhite(curwin->w_cursor.lnum))
9568 continue;
9569 for (that = ml_get_curline(); *that != NUL; ++that)
9570 {
9571 if (*that == ';')
9572 {
9573 while (*(that + 1) != NUL)
9574 ++that;
9575 continue;
9576 }
9577 if (*that == '\\')
9578 {
9579 if (*(that + 1) != NUL)
9580 ++that;
9581 continue;
9582 }
9583 if (*that == '"' && *(that + 1) != NUL)
9584 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009585 while (*++that && *that != '"')
9586 {
9587 /* skipping escaped characters in the string */
9588 if (*that == '\\')
9589 {
9590 if (*++that == NUL)
9591 break;
9592 if (that[1] == NUL)
9593 {
9594 ++that;
9595 break;
9596 }
9597 }
9598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009600 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009602 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 --parencount;
9604 }
9605 if (parencount == 0)
9606 {
9607 amount = get_indent();
9608 break;
9609 }
9610 }
9611
9612 if (amount == -1)
9613 {
9614 curwin->w_cursor.lnum = pos->lnum;
9615 curwin->w_cursor.col = pos->col;
9616 col = pos->col;
9617
9618 that = ml_get_curline();
9619
9620 if (vi_lisp && get_indent() == 0)
9621 amount = 2;
9622 else
9623 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009624 char_u *line = that;
9625
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 amount = 0;
9627 while (*that && col)
9628 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009629 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 col--;
9631 }
9632
9633 /*
9634 * Some keywords require "body" indenting rules (the
9635 * non-standard-lisp ones are Scheme special forms):
9636 *
9637 * (let ((a 1)) instead (let ((a 1))
9638 * (...)) of (...))
9639 */
9640
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009641 if (!vi_lisp && (*that == '(' || *that == '[')
9642 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 amount += 2;
9644 else
9645 {
9646 that++;
9647 amount++;
9648 firsttry = amount;
9649
Bram Moolenaar1c465442017-03-12 20:10:05 +01009650 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009652 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 ++that;
9654 }
9655
9656 if (*that && *that != ';') /* not a comment line */
9657 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009658 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009660 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 firsttry++;
9662
9663 parencount = 0;
9664 quotecount = 0;
9665
9666 if (vi_lisp
9667 || (*that != '"'
9668 && *that != '\''
9669 && *that != '#'
9670 && (*that < '0' || *that > '9')))
9671 {
9672 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009673 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 || quotecount
9675 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009676 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 && !quotecount
9678 && !parencount
9679 && vi_lisp)))
9680 {
9681 if (*that == '"')
9682 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009683 if ((*that == '(' || *that == '[')
9684 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009686 if ((*that == ')' || *that == ']')
9687 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 --parencount;
9689 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009690 amount += lbr_chartabsize_adv(
9691 line, &that, (colnr_T)amount);
9692 amount += lbr_chartabsize_adv(
9693 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 }
9695 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009696 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009698 amount += lbr_chartabsize(
9699 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 that++;
9701 }
9702 if (!*that || *that == ';')
9703 amount = firsttry;
9704 }
9705 }
9706 }
9707 }
9708 }
9709 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009710 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711
9712 curwin->w_cursor = realpos;
9713
9714 return amount;
9715}
9716#endif /* FEAT_LISP */
9717
9718 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009719prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009721#if defined(SIGHUP) && defined(SIG_IGN)
9722 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9723 * makes Vim exit and then handling SIGHUP causes various reentrance
9724 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009725 signal(SIGHUP, SIG_IGN);
9726#endif
9727
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728#ifdef FEAT_GUI
9729 if (gui.in_use)
9730 {
9731 gui.dying = TRUE;
9732 out_trash(); /* trash any pending output */
9733 }
9734 else
9735#endif
9736 {
9737 windgoto((int)Rows - 1, 0);
9738
9739 /*
9740 * Switch terminal mode back now, so messages end up on the "normal"
9741 * screen (if there are two screens).
9742 */
9743 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009744 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 out_flush();
9746 }
9747}
9748
9749/*
9750 * Preserve files and exit.
9751 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009752 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9753 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 */
9755 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009756preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757{
9758 buf_T *buf;
9759
9760 prepare_to_exit();
9761
Bram Moolenaar4770d092006-01-12 23:22:24 +00009762 /* Setting this will prevent free() calls. That avoids calling free()
9763 * recursively when free() was invoked with a bad pointer. */
9764 really_exiting = TRUE;
9765
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 out_str(IObuff);
9767 screen_start(); /* don't know where cursor is now */
9768 out_flush();
9769
9770 ml_close_notmod(); /* close all not-modified buffers */
9771
Bram Moolenaar29323592016-07-24 22:04:11 +02009772 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 {
9774 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9775 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009776 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 screen_start(); /* don't know where cursor is now */
9778 out_flush();
9779 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9780 break;
9781 }
9782 }
9783
9784 ml_close_all(FALSE); /* close all memfiles, without deleting */
9785
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009786 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787
9788 getout(1);
9789}
9790
9791/*
9792 * return TRUE if "fname" exists.
9793 */
9794 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009795vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009797 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798
9799 if (mch_stat((char *)fname, &st))
9800 return FALSE;
9801 return TRUE;
9802}
9803
9804/*
9805 * Check for CTRL-C pressed, but only once in a while.
9806 * Should be used instead of ui_breakcheck() for functions that check for
9807 * each line in the file. Calling ui_breakcheck() each time takes too much
9808 * time, because it can be a system call.
9809 */
9810
9811#ifndef BREAKCHECK_SKIP
9812# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9813# define BREAKCHECK_SKIP 200
9814# else
9815# define BREAKCHECK_SKIP 32
9816# endif
9817#endif
9818
9819static int breakcheck_count = 0;
9820
9821 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009822line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823{
9824 if (++breakcheck_count >= BREAKCHECK_SKIP)
9825 {
9826 breakcheck_count = 0;
9827 ui_breakcheck();
9828 }
9829}
9830
9831/*
9832 * Like line_breakcheck() but check 10 times less often.
9833 */
9834 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009835fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9838 {
9839 breakcheck_count = 0;
9840 ui_breakcheck();
9841 }
9842}
9843
9844/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009845 * Invoke expand_wildcards() for one pattern.
9846 * Expand items like "%:h" before the expansion.
9847 * Returns OK or FAIL.
9848 */
9849 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009850expand_wildcards_eval(
9851 char_u **pat, /* pointer to input pattern */
9852 int *num_file, /* resulting number of files */
9853 char_u ***file, /* array of resulting files */
9854 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009855{
9856 int ret = FAIL;
9857 char_u *eval_pat = NULL;
9858 char_u *exp_pat = *pat;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009859 char *ignored_msg;
Bram Moolenaard7834d32009-12-02 16:14:36 +00009860 int usedlen;
9861
9862 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9863 {
9864 ++emsg_off;
9865 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9866 NULL, &ignored_msg, NULL);
9867 --emsg_off;
9868 if (eval_pat != NULL)
9869 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9870 }
9871
9872 if (exp_pat != NULL)
9873 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9874
9875 if (eval_pat != NULL)
9876 {
9877 vim_free(exp_pat);
9878 vim_free(eval_pat);
9879 }
9880
9881 return ret;
9882}
9883
9884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9886 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009887 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 */
9889 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009890expand_wildcards(
9891 int num_pat, /* number of input patterns */
9892 char_u **pat, /* array of input patterns */
9893 int *num_files, /* resulting number of files */
9894 char_u ***files, /* array of resulting files */
9895 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896{
9897 int retval;
9898 int i, j;
9899 char_u *p;
9900 int non_suf_match; /* number without matching suffix */
9901
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009902 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903
9904 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009905 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 return retval;
9907
9908#ifdef FEAT_WILDIGN
9909 /*
9910 * Remove names that match 'wildignore'.
9911 */
9912 if (*p_wig)
9913 {
9914 char_u *ffname;
9915
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009916 /* check all files in (*files)[] */
9917 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009919 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 if (ffname == NULL) /* out of memory */
9921 break;
9922# ifdef VMS
9923 vms_remove_version(ffname);
9924# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009925 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009927 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009928 vim_free((*files)[i]);
9929 for (j = i; j + 1 < *num_files; ++j)
9930 (*files)[j] = (*files)[j + 1];
9931 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 --i;
9933 }
9934 vim_free(ffname);
9935 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009936
9937 /* If the number of matches is now zero, we fail. */
9938 if (*num_files == 0)
9939 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01009940 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009941 return FAIL;
9942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 }
9944#endif
9945
9946 /*
9947 * Move the names where 'suffixes' match to the end.
9948 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009949 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 {
9951 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009952 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009954 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 {
9956 /*
9957 * Move the name without matching suffix to the front
9958 * of the list.
9959 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009960 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009962 (*files)[j] = (*files)[j - 1];
9963 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 }
9965 }
9966 }
9967
9968 return retval;
9969}
9970
9971/*
9972 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9973 */
9974 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009975match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976{
9977 int fnamelen, setsuflen;
9978 char_u *setsuf;
9979#define MAXSUFLEN 30 /* maximum length of a file suffix */
9980 char_u suf_buf[MAXSUFLEN];
9981
9982 fnamelen = (int)STRLEN(fname);
9983 setsuflen = 0;
9984 for (setsuf = p_su; *setsuf; )
9985 {
9986 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009987 if (setsuflen == 0)
9988 {
9989 char_u *tail = gettail(fname);
9990
9991 /* empty entry: match name without a '.' */
9992 if (vim_strchr(tail, '.') == NULL)
9993 {
9994 setsuflen = 1;
9995 break;
9996 }
9997 }
9998 else
9999 {
10000 if (fnamelen >= setsuflen
10001 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10002 (size_t)setsuflen) == 0)
10003 break;
10004 setsuflen = 0;
10005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 }
10007 return (setsuflen != 0);
10008}
10009
10010#if !defined(NO_EXPANDPATH) || defined(PROTO)
10011
10012# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010013static int vim_backtick(char_u *p);
10014static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015# endif
10016
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010017# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018/*
10019 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10020 * it's shared between these systems.
10021 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010022# if defined(PROTO)
10023# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024# else
10025# ifdef __BORLANDC__
10026# define _cdecl _RTLENTRYF
10027# endif
10028# endif
10029
10030/*
10031 * comparison function for qsort in dos_expandpath()
10032 */
10033 static int _cdecl
10034pstrcmp(const void *a, const void *b)
10035{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010036 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037}
10038
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010040 * Recursively expand one path component into all matching files and/or
10041 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 * Return the number of matches found.
10043 * "path" has backslashes before chars that are not to be expanded, starting
10044 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010045 * Return the number of matches found.
10046 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 */
10048 static int
10049dos_expandpath(
10050 garray_T *gap,
10051 char_u *path,
10052 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010053 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010054 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010056 char_u *buf;
10057 char_u *path_end;
10058 char_u *p, *s, *e;
10059 int start_len = gap->ga_len;
10060 char_u *pat;
10061 regmatch_T regmatch;
10062 int starts_with_dot;
10063 int matches;
10064 int len;
10065 int starstar = FALSE;
10066 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 WIN32_FIND_DATA fb;
10068 HANDLE hFind = (HANDLE)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 WIN32_FIND_DATAW wfb;
10070 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010072 int ok;
10073
10074 /* Expanding "**" may take a long time, check for CTRL-C. */
10075 if (stardepth > 0)
10076 {
10077 ui_breakcheck();
10078 if (got_int)
10079 return 0;
10080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081
Bram Moolenaarfc3abf42019-01-24 15:54:21 +010010082 // Make room for file name. When doing encoding conversion the actual
10083 // length may be quite a bit longer, thus use the maximum possible length.
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010084 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 if (buf == NULL)
10086 return 0;
10087
10088 /*
10089 * Find the first part in the path name that contains a wildcard or a ~1.
10090 * Copy it into buf, including the preceding characters.
10091 */
10092 p = buf;
10093 s = buf;
10094 e = NULL;
10095 path_end = path;
10096 while (*path_end != NUL)
10097 {
10098 /* May ignore a wildcard that has a backslash before it; it will
10099 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10100 if (path_end >= path + wildoff && rem_backslash(path_end))
10101 *p++ = *path_end++;
10102 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10103 {
10104 if (e != NULL)
10105 break;
10106 s = p + 1;
10107 }
10108 else if (path_end >= path + wildoff
10109 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10110 e = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 if (has_mbyte)
10112 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010113 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 STRNCPY(p, path_end, len);
10115 p += len;
10116 path_end += len;
10117 }
10118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 *p++ = *path_end++;
10120 }
10121 e = p;
10122 *e = NUL;
10123
10124 /* now we have one wildcard component between s and e */
10125 /* Remove backslashes between "wildoff" and the start of the wildcard
10126 * component. */
10127 for (p = buf + wildoff; p < s; ++p)
10128 if (rem_backslash(p))
10129 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010130 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 --e;
10132 --s;
10133 }
10134
Bram Moolenaar231334e2005-07-25 20:46:57 +000010135 /* Check for "**" between "s" and "e". */
10136 for (p = s; p < e; ++p)
10137 if (p[0] == '*' && p[1] == '*')
10138 starstar = TRUE;
10139
Bram Moolenaard82103e2016-01-17 17:04:05 +010010140 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10142 if (pat == NULL)
10143 {
10144 vim_free(buf);
10145 return 0;
10146 }
10147
10148 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010149 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010150 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 regmatch.rm_ic = TRUE; /* Always ignore case */
10152 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010153 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010154 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 vim_free(pat);
10156
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010157 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 {
10159 vim_free(buf);
10160 return 0;
10161 }
10162
10163 /* remember the pattern or file name being looked for */
10164 matchname = vim_strsave(s);
10165
Bram Moolenaar231334e2005-07-25 20:46:57 +000010166 /* If "**" is by itself, this is the first time we encounter it and more
10167 * is following then find matches without any directory. */
10168 if (!didstar && stardepth < 100 && starstar && e - s == 2
10169 && *path_end == '/')
10170 {
10171 STRCPY(s, path_end + 1);
10172 ++stardepth;
10173 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10174 --stardepth;
10175 }
10176
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 /* Scan all files in the directory with "dir/ *.*" */
10178 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10180 {
10181 /* The active codepage differs from 'encoding'. Attempt using the
10182 * wide function. If it fails because it is not implemented fall back
10183 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010184 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 if (wn != NULL)
10186 {
10187 hFind = FindFirstFileW(wn, &wfb);
10188 if (hFind == INVALID_HANDLE_VALUE
10189 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010190 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 }
10192 }
10193
10194 if (wn == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010195 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197
10198 while (ok)
10199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010201 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 /* Ignore entries starting with a dot, unless when asked for. Accept
10205 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010206 if ((p[0] != '.' || starts_with_dot
10207 || ((flags & EW_DODOT)
10208 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010210 || (regmatch.regprog != NULL
10211 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010212 || ((flags & EW_NOTWILD)
10213 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010217
10218 if (starstar && stardepth < 100)
10219 {
10220 /* For "**" in the pattern first go deeper in the tree to
10221 * find matches. */
10222 STRCPY(buf + len, "/**");
10223 STRCPY(buf + len + 3, path_end);
10224 ++stardepth;
10225 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10226 --stardepth;
10227 }
10228
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 STRCPY(buf + len, path_end);
10230 if (mch_has_exp_wildcard(path_end))
10231 {
10232 /* need to expand another component of the path */
10233 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010234 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 }
10236 else
10237 {
10238 /* no more wildcards, check if there is a match */
10239 /* remove backslashes for the remaining components only */
10240 if (*path_end != 0)
10241 backslash_halve(buf + len + 1);
10242 if (mch_getperm(buf) >= 0) /* add existing file */
10243 addfile(gap, buf, flags);
10244 }
10245 }
10246
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 if (wn != NULL)
10248 {
10249 vim_free(p);
10250 ok = FindNextFileW(hFind, &wfb);
10251 }
10252 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254
10255 /* If no more matches and no match was used, try expanding the name
10256 * itself. Finds the long name of a short filename. */
10257 if (!ok && matchname != NULL && gap->ga_len == start_len)
10258 {
10259 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 FindClose(hFind);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 if (wn != NULL)
10262 {
10263 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010264 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 if (wn != NULL)
10266 hFind = FindFirstFileW(wn, &wfb);
10267 }
10268 if (wn == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010269 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010271 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 }
10273 }
10274
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 FindClose(hFind);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010278 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 vim_free(matchname);
10280
10281 matches = gap->ga_len - start_len;
10282 if (matches > 0)
10283 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10284 sizeof(char_u *), pstrcmp);
10285 return matches;
10286}
10287
10288 int
10289mch_expandpath(
10290 garray_T *gap,
10291 char_u *path,
10292 int flags) /* EW_* flags */
10293{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010294 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010296# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297
Bram Moolenaar231334e2005-07-25 20:46:57 +000010298#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10299 || defined(PROTO)
10300/*
10301 * Unix style wildcard expansion code.
10302 * It's here because it's used both for Unix and Mac.
10303 */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010304 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010305pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010306{
10307 return (pathcmp(*(char **)a, *(char **)b, -1));
10308}
10309
10310/*
10311 * Recursively expand one path component into all matching files and/or
10312 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10313 * "path" has backslashes before chars that are not to be expanded, starting
10314 * at "path + wildoff".
10315 * Return the number of matches found.
10316 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10317 */
10318 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010319unix_expandpath(
10320 garray_T *gap,
10321 char_u *path,
10322 int wildoff,
10323 int flags, /* EW_* flags */
10324 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010325{
10326 char_u *buf;
10327 char_u *path_end;
10328 char_u *p, *s, *e;
10329 int start_len = gap->ga_len;
10330 char_u *pat;
10331 regmatch_T regmatch;
10332 int starts_with_dot;
10333 int matches;
10334 int len;
10335 int starstar = FALSE;
10336 static int stardepth = 0; /* depth for "**" expansion */
10337
10338 DIR *dirp;
10339 struct dirent *dp;
10340
10341 /* Expanding "**" may take a long time, check for CTRL-C. */
10342 if (stardepth > 0)
10343 {
10344 ui_breakcheck();
10345 if (got_int)
10346 return 0;
10347 }
10348
10349 /* make room for file name */
10350 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10351 if (buf == NULL)
10352 return 0;
10353
10354 /*
10355 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010356 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010357 * Copy it into "buf", including the preceding characters.
10358 */
10359 p = buf;
10360 s = buf;
10361 e = NULL;
10362 path_end = path;
10363 while (*path_end != NUL)
10364 {
10365 /* May ignore a wildcard that has a backslash before it; it will
10366 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10367 if (path_end >= path + wildoff && rem_backslash(path_end))
10368 *p++ = *path_end++;
10369 else if (*path_end == '/')
10370 {
10371 if (e != NULL)
10372 break;
10373 s = p + 1;
10374 }
10375 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010376 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010377 || (!p_fic && (flags & EW_ICASE)
10378 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010379 e = p;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010380 if (has_mbyte)
10381 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010382 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010383 STRNCPY(p, path_end, len);
10384 p += len;
10385 path_end += len;
10386 }
10387 else
Bram Moolenaar231334e2005-07-25 20:46:57 +000010388 *p++ = *path_end++;
10389 }
10390 e = p;
10391 *e = NUL;
10392
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010393 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010394 /* Remove backslashes between "wildoff" and the start of the wildcard
10395 * component. */
10396 for (p = buf + wildoff; p < s; ++p)
10397 if (rem_backslash(p))
10398 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010399 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010400 --e;
10401 --s;
10402 }
10403
10404 /* Check for "**" between "s" and "e". */
10405 for (p = s; p < e; ++p)
10406 if (p[0] == '*' && p[1] == '*')
10407 starstar = TRUE;
10408
10409 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010410 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010411 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10412 if (pat == NULL)
10413 {
10414 vim_free(buf);
10415 return 0;
10416 }
10417
10418 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010419 if (flags & EW_ICASE)
10420 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10421 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010422 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010423 if (flags & (EW_NOERROR | EW_NOTWILD))
10424 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010425 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010426 if (flags & (EW_NOERROR | EW_NOTWILD))
10427 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010428 vim_free(pat);
10429
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010430 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010431 {
10432 vim_free(buf);
10433 return 0;
10434 }
10435
10436 /* If "**" is by itself, this is the first time we encounter it and more
10437 * is following then find matches without any directory. */
10438 if (!didstar && stardepth < 100 && starstar && e - s == 2
10439 && *path_end == '/')
10440 {
10441 STRCPY(s, path_end + 1);
10442 ++stardepth;
10443 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10444 --stardepth;
10445 }
10446
10447 /* open the directory for scanning */
10448 *s = NUL;
10449 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10450
10451 /* Find all matching entries */
10452 if (dirp != NULL)
10453 {
10454 for (;;)
10455 {
10456 dp = readdir(dirp);
10457 if (dp == NULL)
10458 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010459 if ((dp->d_name[0] != '.' || starts_with_dot
10460 || ((flags & EW_DODOT)
10461 && dp->d_name[1] != NUL
10462 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010463 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10464 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010465 || ((flags & EW_NOTWILD)
10466 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010467 {
10468 STRCPY(s, dp->d_name);
10469 len = STRLEN(buf);
10470
10471 if (starstar && stardepth < 100)
10472 {
10473 /* For "**" in the pattern first go deeper in the tree to
10474 * find matches. */
10475 STRCPY(buf + len, "/**");
10476 STRCPY(buf + len + 3, path_end);
10477 ++stardepth;
10478 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10479 --stardepth;
10480 }
10481
10482 STRCPY(buf + len, path_end);
10483 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10484 {
10485 /* need to expand another component of the path */
10486 /* remove backslashes for the remaining components only */
10487 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10488 }
10489 else
10490 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010491 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010492
Bram Moolenaar231334e2005-07-25 20:46:57 +000010493 /* no more wildcards, check if there is a match */
10494 /* remove backslashes for the remaining components only */
10495 if (*path_end != NUL)
10496 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010497 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010498 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010499 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010500 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010501#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010502 size_t precomp_len = STRLEN(buf)+1;
10503 char_u *precomp_buf =
10504 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010505
Bram Moolenaar231334e2005-07-25 20:46:57 +000010506 if (precomp_buf)
10507 {
10508 mch_memmove(buf, precomp_buf, precomp_len);
10509 vim_free(precomp_buf);
10510 }
10511#endif
10512 addfile(gap, buf, flags);
10513 }
10514 }
10515 }
10516 }
10517
10518 closedir(dirp);
10519 }
10520
10521 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010522 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010523
10524 matches = gap->ga_len - start_len;
10525 if (matches > 0)
10526 qsort(((char_u **)gap->ga_data) + start_len, matches,
10527 sizeof(char_u *), pstrcmp);
10528 return matches;
10529}
10530#endif
10531
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010532#if defined(FEAT_SEARCHPATH)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010533/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010534 * Moves "*psep" back to the previous path separator in "path".
10535 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010536 */
10537 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010538find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010539{
10540 /* skip the current separator */
10541 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010542 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010543
10544 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010545 while (*psep > path)
10546 {
10547 if (vim_ispathsep(**psep))
10548 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010549 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010550 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010551
10552 return FAIL;
10553}
10554
10555/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010556 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10557 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010558 */
10559 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010560is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010561{
10562 int j;
10563 int candidate_len;
10564 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010565 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010566 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010567
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010568 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010569 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010570 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010571 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010572
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010573 candidate_len = (int)STRLEN(maybe_unique);
10574 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010575 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010576 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010577
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010578 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010579 if (fnamecmp(maybe_unique, rival) == 0
10580 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010581 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010582 }
10583
Bram Moolenaar162bd912010-07-28 22:29:10 +020010584 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010585}
10586
10587/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010588 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010589 * paths are expanded to their equivalent fullpath. This includes the "."
10590 * (relative to current buffer directory) and empty path (relative to current
10591 * directory) notations.
10592 *
10593 * TODO: handle upward search (;) and path limiter (**N) notations by
10594 * expanding each into their equivalent path(s).
10595 */
10596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010597expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010598{
10599 char_u *path_option = *curbuf->b_p_path == NUL
10600 ? p_path : curbuf->b_p_path;
10601 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010602 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010603 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010604
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010605 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010606 return;
10607
10608 while (*path_option != NUL)
10609 {
10610 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10611
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010612 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010613 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010614 /* Relative to current buffer:
10615 * "/path/file" + "." -> "/path/"
10616 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010617 if (curbuf->b_ffname == NULL)
10618 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010619 p = gettail(curbuf->b_ffname);
10620 len = (int)(p - curbuf->b_ffname);
10621 if (len + (int)STRLEN(buf) >= MAXPATHL)
10622 continue;
10623 if (buf[1] == NUL)
10624 buf[len] = NUL;
10625 else
10626 STRMOVE(buf + len, buf + 2);
10627 mch_memmove(buf, curbuf->b_ffname, len);
10628 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010629 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010630 else if (buf[0] == NUL)
10631 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010632 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010633 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010634 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010635 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010636 else if (!mch_isFullName(buf))
10637 {
10638 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010639 len = (int)STRLEN(curdir);
10640 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010641 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010642 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010643 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010644 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010645 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010646 }
10647
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010648 if (ga_grow(gap, 1) == FAIL)
10649 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010650
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010651# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010652 /* Avoid the path ending in a backslash, it fails when a comma is
10653 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010654 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010655 if (buf[len - 1] == '\\')
10656 buf[len - 1] = '/';
10657# endif
10658
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010659 p = vim_strsave(buf);
10660 if (p == NULL)
10661 break;
10662 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010663 }
10664
10665 vim_free(buf);
10666}
10667
10668/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010669 * Returns a pointer to the file or directory name in "fname" that matches the
10670 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010671 *
10672 * path: /foo/bar/baz
10673 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010674 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010675 */
10676 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010677get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010678{
10679 int i;
10680 int maxlen = 0;
10681 char_u **path_part = (char_u **)gap->ga_data;
10682 char_u *cutoff = NULL;
10683
10684 for (i = 0; i < gap->ga_len; i++)
10685 {
10686 int j = 0;
10687
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010688 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010689# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010690 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10691#endif
10692 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010693 j++;
10694 if (j > maxlen)
10695 {
10696 maxlen = j;
10697 cutoff = &fname[j];
10698 }
10699 }
10700
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010701 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010702 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010703 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010704 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010705
10706 return cutoff;
10707}
10708
10709/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010710 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10711 * that they are unique with respect to each other while conserving the part
10712 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010713 */
10714 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010715uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010716{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010717 int i;
10718 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010719 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010720 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010721 char_u *pat;
10722 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010723 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010724 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010725 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010726 char_u **in_curdir = NULL;
10727 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010728
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010729 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010730 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010731
10732 /*
10733 * We need to prepend a '*' at the beginning of file_pattern so that the
10734 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010735 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010736 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010737 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010738 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010739 if (file_pattern == NULL)
10740 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010741 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010742 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010743 STRCAT(file_pattern, pattern);
10744 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10745 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010746 if (pat == NULL)
10747 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010748
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010749 regmatch.rm_ic = TRUE; /* always ignore case */
10750 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10751 vim_free(pat);
10752 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010753 return;
10754
Bram Moolenaar162bd912010-07-28 22:29:10 +020010755 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010756 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010757 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010758 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010759
10760 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010761 if (in_curdir == NULL)
10762 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010763
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010764 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010765 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010766 char_u *path = fnames[i];
10767 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010768 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010769 char_u *pathsep_p;
10770 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010771
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010772 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010773 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010774 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010775 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010776 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010777
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010778 /* Shorten the filename while maintaining its uniqueness */
10779 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010780
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010781 /* Don't assume all files can be reached without path when search
10782 * pattern starts with star star slash, so only remove path_cutoff
10783 * when possible. */
10784 if (pattern[0] == '*' && pattern[1] == '*'
10785 && vim_ispathsep_nocolon(pattern[2])
10786 && path_cutoff != NULL
10787 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10788 && is_unique(path_cutoff, gap, i))
10789 {
10790 sort_again = TRUE;
10791 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10792 }
10793 else
10794 {
10795 /* Here all files can be reached without path, so get shortest
10796 * unique path. We start at the end of the path. */
10797 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010798
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010799 while (find_previous_pathsep(path, &pathsep_p))
10800 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10801 && is_unique(pathsep_p + 1, gap, i)
10802 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10803 {
10804 sort_again = TRUE;
10805 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10806 break;
10807 }
10808 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010809
10810 if (mch_isFullName(path))
10811 {
10812 /*
10813 * Last resort: shorten relative to curdir if possible.
10814 * 'possible' means:
10815 * 1. It is under the current directory.
10816 * 2. The result is actually shorter than the original.
10817 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010818 * Before curdir After
10819 * /foo/bar/file.txt /foo/bar ./file.txt
10820 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10821 * /file.txt / /file.txt
10822 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010823 */
10824 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010825 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010826#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010827 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010828 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010829 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010830 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010831 && !vim_ispathsep(*short_name)
10832#endif
10833 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010834 {
10835 STRCPY(path, ".");
10836 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010837 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010838 }
10839 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010840 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010841 }
10842
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010843 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010844 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010845 {
10846 char_u *rel_path;
10847 char_u *path = in_curdir[i];
10848
10849 if (path == NULL)
10850 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010851
10852 /* If the {filename} is not unique, change it to ./{filename}.
10853 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010854 short_name = shorten_fname(path, curdir);
10855 if (short_name == NULL)
10856 short_name = path;
10857 if (is_unique(short_name, gap, i))
10858 {
10859 STRCPY(fnames[i], short_name);
10860 continue;
10861 }
10862
10863 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10864 if (rel_path == NULL)
10865 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010866 STRCPY(rel_path, ".");
10867 add_pathsep(rel_path);
10868 STRCAT(rel_path, short_name);
10869
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010870 vim_free(fnames[i]);
10871 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010872 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010873 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010874 }
10875
Bram Moolenaar162bd912010-07-28 22:29:10 +020010876theend:
10877 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010878 if (in_curdir != NULL)
10879 {
10880 for (i = 0; i < gap->ga_len; i++)
10881 vim_free(in_curdir[i]);
10882 vim_free(in_curdir);
10883 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010884 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010885 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010886
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010887 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010888 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010889}
10890
10891/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010892 * Calls globpath() with 'path' values for the given pattern and stores the
10893 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010894 * Returns the total number of matches.
10895 */
10896 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010897expand_in_path(
10898 garray_T *gap,
10899 char_u *pattern,
10900 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010901{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010902 char_u *curdir;
10903 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010904 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010905 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010906
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010907 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010908 return 0;
10909 mch_dirname(curdir, MAXPATHL);
10910
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010911 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010912 expand_path_option(curdir, &path_ga);
10913 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010914 if (path_ga.ga_len == 0)
10915 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010916
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010917 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010918 ga_clear_strings(&path_ga);
10919 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010920 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010921
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010922 if (flags & EW_ICASE)
10923 glob_flags |= WILD_ICASE;
10924 if (flags & EW_ADDSLASH)
10925 glob_flags |= WILD_ADD_SLASH;
10926 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010927 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010928
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010929 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010930}
10931#endif
10932
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010933#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10934/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010935 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10936 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010937 */
10938 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010939remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010940{
10941 int i;
10942 int j;
10943 char_u **fnames = (char_u **)gap->ga_data;
10944
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010945 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010946 for (i = gap->ga_len - 1; i > 0; --i)
10947 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10948 {
10949 vim_free(fnames[i]);
10950 for (j = i + 1; j < gap->ga_len; ++j)
10951 fnames[j - 1] = fnames[j];
10952 --gap->ga_len;
10953 }
10954}
10955#endif
10956
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010957/*
10958 * Return TRUE if "p" contains what looks like an environment variable.
10959 * Allowing for escaping.
10960 */
10961 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010962has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010963{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010964 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010965 {
10966 if (*p == '\\' && p[1] != NUL)
10967 ++p;
10968 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010969#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010970 "$%"
10971#else
10972 "$"
10973#endif
10974 , *p) != NULL)
10975 return TRUE;
10976 }
10977 return FALSE;
10978}
10979
10980#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010981/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010982 * Return TRUE if "p" contains a special wildcard character, one that Vim
10983 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010984 */
10985 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010986has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010987{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010988 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010989 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010990 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010991 if (*p == '\\' && p[1] != NUL)
10992 ++p;
10993 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10994 return TRUE;
10995 }
10996 return FALSE;
10997}
10998#endif
10999
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000/*
11001 * Generic wildcard expansion code.
11002 *
11003 * Characters in "pat" that should not be expanded must be preceded with a
11004 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11005 *
11006 * Return FAIL when no single file was found. In this case "num_file" is not
11007 * set, and "file" may contain an error message.
11008 * Return OK when some files found. "num_file" is set to the number of
11009 * matches, "file" to the array of matches. Call FreeWild() later.
11010 */
11011 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011012gen_expand_wildcards(
11013 int num_pat, /* number of input patterns */
11014 char_u **pat, /* array of input patterns */
11015 int *num_file, /* resulting number of files */
11016 char_u ***file, /* array of resulting files */
11017 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018{
11019 int i;
11020 garray_T ga;
11021 char_u *p;
11022 static int recursive = FALSE;
11023 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011024 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011025#if defined(FEAT_SEARCHPATH)
11026 int did_expand_in_path = FALSE;
11027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028
11029 /*
11030 * expand_env() is called to expand things like "~user". If this fails,
11031 * it calls ExpandOne(), which brings us back here. In this case, always
11032 * call the machine specific expansion function, if possible. Otherwise,
11033 * return FAIL.
11034 */
11035 if (recursive)
11036#ifdef SPECIAL_WILDCHAR
11037 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11038#else
11039 return FAIL;
11040#endif
11041
11042#ifdef SPECIAL_WILDCHAR
11043 /*
11044 * If there are any special wildcard characters which we cannot handle
11045 * here, call machine specific function for all the expansion. This
11046 * avoids starting the shell for each argument separately.
11047 * For `=expr` do use the internal function.
11048 */
11049 for (i = 0; i < num_pat; i++)
11050 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011051 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052# ifdef VIM_BACKTICK
11053 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11054# endif
11055 )
11056 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11057 }
11058#endif
11059
11060 recursive = TRUE;
11061
11062 /*
11063 * The matching file names are stored in a growarray. Init it empty.
11064 */
11065 ga_init2(&ga, (int)sizeof(char_u *), 30);
11066
11067 for (i = 0; i < num_pat; ++i)
11068 {
11069 add_pat = -1;
11070 p = pat[i];
11071
11072#ifdef VIM_BACKTICK
11073 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011076 if (add_pat == -1)
11077 retval = FAIL;
11078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 else
11080#endif
11081 {
11082 /*
11083 * First expand environment variables, "~/" and "~user/".
11084 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011085 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011087 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 if (p == NULL)
11089 p = pat[i];
11090#ifdef UNIX
11091 /*
11092 * On Unix, if expand_env() can't expand an environment
11093 * variable, use the shell to do that. Discard previously
11094 * found file names and start all over again.
11095 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011096 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097 {
11098 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011099 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011101 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 recursive = FALSE;
11103 return i;
11104 }
11105#endif
11106 }
11107
11108 /*
11109 * If there are wildcards: Expand file names and add each match to
11110 * the list. If there is no match, and EW_NOTFOUND is given, add
11111 * the pattern.
11112 * If there are no wildcards: Add the file name if it exists or
11113 * when EW_NOTFOUND is given.
11114 */
11115 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011116 {
11117#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011118 if ((flags & EW_PATH)
11119 && !mch_isFullName(p)
11120 && !(p[0] == '.'
11121 && (vim_ispathsep(p[1])
11122 || (p[1] == '.' && vim_ispathsep(p[2]))))
11123 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011124 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011125 /* :find completion where 'path' is used.
11126 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011127 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011128 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011129 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011130 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011131 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011132 else
11133#endif
11134 add_pat = mch_expandpath(&ga, p, flags);
11135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 }
11137
11138 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11139 {
11140 char_u *t = backslash_halve_save(p);
11141
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11143 * "vim c:/" work. */
11144 if (flags & EW_NOTFOUND)
11145 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011146 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 addfile(&ga, t, flags);
11148 vim_free(t);
11149 }
11150
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011151#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011152 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011153 uniquefy_paths(&ga, p);
11154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 if (p != pat[i])
11156 vim_free(p);
11157 }
11158
11159 *num_file = ga.ga_len;
11160 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11161
11162 recursive = FALSE;
11163
Bram Moolenaar336bd622016-01-17 18:23:58 +010011164 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165}
11166
11167# ifdef VIM_BACKTICK
11168
11169/*
11170 * Return TRUE if we can expand this backtick thing here.
11171 */
11172 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011173vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174{
11175 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11176}
11177
11178/*
11179 * Expand an item in `backticks` by executing it as a command.
11180 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011181 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 */
11183 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011184expand_backtick(
11185 garray_T *gap,
11186 char_u *pat,
11187 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188{
11189 char_u *p;
11190 char_u *cmd;
11191 char_u *buffer;
11192 int cnt = 0;
11193 int i;
11194
11195 /* Create the command: lop off the backticks. */
11196 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11197 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011198 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199
11200#ifdef FEAT_EVAL
11201 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011202 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 else
11204#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011205 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011206 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 vim_free(cmd);
11208 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011209 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210
11211 cmd = buffer;
11212 while (*cmd != NUL)
11213 {
11214 cmd = skipwhite(cmd); /* skip over white space */
11215 p = cmd;
11216 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11217 ++p;
11218 /* add an entry if it is not empty */
11219 if (p > cmd)
11220 {
11221 i = *p;
11222 *p = NUL;
11223 addfile(gap, cmd, flags);
11224 *p = i;
11225 ++cnt;
11226 }
11227 cmd = p;
11228 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11229 ++cmd;
11230 }
11231
11232 vim_free(buffer);
11233 return cnt;
11234}
11235# endif /* VIM_BACKTICK */
11236
11237/*
11238 * Add a file to a file list. Accepted flags:
11239 * EW_DIR add directories
11240 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011241 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 * EW_NOTFOUND add even when it doesn't exist
11243 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011244 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245 */
11246 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011247addfile(
11248 garray_T *gap,
11249 char_u *f, /* filename */
11250 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251{
11252 char_u *p;
11253 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011254 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011256 /* if the file/dir/link doesn't exist, may not add it */
11257 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011258 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 return;
11260
11261#ifdef FNAME_ILLEGAL
11262 /* if the file/dir contains illegal characters, don't add it */
11263 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11264 return;
11265#endif
11266
11267 isdir = mch_isdir(f);
11268 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11269 return;
11270
Bram Moolenaarb5971142015-03-21 17:32:19 +010011271 /* If the file isn't executable, may not add it. Do accept directories.
11272 * When invoked from expand_shellcmd() do not use $PATH. */
11273 if (!isdir && (flags & EW_EXEC)
11274 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011275 return;
11276
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 /* Make room for another item in the file list. */
11278 if (ga_grow(gap, 1) == FAIL)
11279 return;
11280
11281 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11282 if (p == NULL)
11283 return;
11284
11285 STRCPY(p, f);
11286#ifdef BACKSLASH_IN_FILENAME
11287 slash_adjust(p);
11288#endif
11289 /*
11290 * Append a slash or backslash after directory names if none is present.
11291 */
11292#ifndef DONT_ADD_PATHSEP_TO_DIR
11293 if (isdir && (flags & EW_ADDSLASH))
11294 add_pathsep(p);
11295#endif
11296 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297}
11298#endif /* !NO_EXPANDPATH */
11299
11300#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11301
11302#ifndef SEEK_SET
11303# define SEEK_SET 0
11304#endif
11305#ifndef SEEK_END
11306# define SEEK_END 2
11307#endif
11308
11309/*
11310 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011311 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11312 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 * Returns an allocated string, or NULL for error.
11314 */
11315 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011316get_cmd_output(
11317 char_u *cmd,
11318 char_u *infile, /* optional input file name */
11319 int flags, /* can be SHELL_SILENT */
11320 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321{
11322 char_u *tempname;
11323 char_u *command;
11324 char_u *buffer = NULL;
11325 int len;
11326 int i = 0;
11327 FILE *fd;
11328
11329 if (check_restricted() || check_secure())
11330 return NULL;
11331
11332 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011333 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011335 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 return NULL;
11337 }
11338
11339 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011340 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 if (command == NULL)
11342 goto done;
11343
11344 /*
11345 * Call the shell to execute the command (errors are ignored).
11346 * Don't check timestamps here.
11347 */
11348 ++no_check_timestamps;
11349 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11350 --no_check_timestamps;
11351
11352 vim_free(command);
11353
11354 /*
11355 * read the names from the file into memory
11356 */
11357# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011358 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 fd = mch_fopen((char *)tempname, "r");
11360# else
11361 fd = mch_fopen((char *)tempname, READBIN);
11362# endif
11363
11364 if (fd == NULL)
11365 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011366 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 goto done;
11368 }
11369
11370 fseek(fd, 0L, SEEK_END);
11371 len = ftell(fd); /* get size of temp file */
11372 fseek(fd, 0L, SEEK_SET);
11373
11374 buffer = alloc(len + 1);
11375 if (buffer != NULL)
11376 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11377 fclose(fd);
11378 mch_remove(tempname);
11379 if (buffer == NULL)
11380 goto done;
11381#ifdef VMS
11382 len = i; /* VMS doesn't give us what we asked for... */
11383#endif
11384 if (i != len)
11385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011386 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011387 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011389 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011390 {
11391 /* Change NUL into SOH, otherwise the string is truncated. */
11392 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011393 if (buffer[i] == NUL)
11394 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011395
Bram Moolenaar162bd912010-07-28 22:29:10 +020011396 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011397 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011398 else
11399 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400
11401done:
11402 vim_free(tempname);
11403 return buffer;
11404}
11405#endif
11406
11407/*
11408 * Free the list of files returned by expand_wildcards() or other expansion
11409 * functions.
11410 */
11411 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011412FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011414 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416 while (count--)
11417 vim_free(files[count]);
11418 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419}
11420
11421/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011422 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 * Don't do this when still processing a command or a mapping.
11424 * Don't do this when inside a ":normal" command.
11425 */
11426 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011427goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428{
11429 return (p_im && stuff_empty() && typebuf_typed());
11430}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011431
11432/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011433 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011434 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11435 * - Remove any argument. E.g., "csh -f" -> "csh".
11436 * But don't allow a space in the path, so that this works:
11437 * "/usr/bin/csh --rcfile ~/.cshrc"
11438 * But don't do that for Windows, it's common to have a space in the path.
11439 */
11440 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011441get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011442{
11443 char_u *p;
11444
11445#ifdef WIN3264
11446 p = gettail(p_sh);
11447 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11448#else
11449 p = skiptowhite(p_sh);
11450 if (*p == NUL)
11451 {
11452 /* No white space, use the tail. */
11453 p = vim_strsave(gettail(p_sh));
11454 }
11455 else
11456 {
11457 char_u *p1, *p2;
11458
11459 /* Find the last path separator before the space. */
11460 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011461 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011462 if (vim_ispathsep(*p2))
11463 p1 = p2 + 1;
11464 p = vim_strnsave(p1, (int)(p - p1));
11465 }
11466#endif
11467 return p;
11468}