blob: 0c38c8af53725e2135e2a12637dffeb4965c1652 [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 Moolenaarbfe3bf82012-06-13 17:28:55 +0200625#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200626 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200627#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200628 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200629 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200630 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000631
632 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 getvcol(curwin, &pos, &col, NULL, NULL);
635 return (int)col;
636}
637
Bram Moolenaar597a4222014-06-25 14:39:50 +0200638#if defined(FEAT_LINEBREAK) || defined(PROTO)
639/*
640 * Return appropriate space number for breakindent, taking influencing
641 * parameters into account. Window must be specified, since it is not
642 * necessarily always the current one.
643 */
644 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100645get_breakindent_win(
646 win_T *wp,
647 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200648{
649 static int prev_indent = 0; /* cached indent value */
650 static long prev_ts = 0L; /* cached tabstop value */
651 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100652 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200653#ifdef FEAT_VARTABS
654 static int *prev_vts = NULL; /* cached vartabs values */
655#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200656 int bri = 0;
657 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200658 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200659 - ((wp->w_p_nu || wp->w_p_rnu)
660 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
661 ? number_width(wp) + 1 : 0);
662
663 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200664 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200665 || prev_tick != CHANGEDTICK(wp->w_buffer)
666#ifdef FEAT_VARTABS
667 || prev_vts != wp->w_buffer->b_p_vts_array
668#endif
669 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200670 {
671 prev_line = line;
672 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100673 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200674#ifdef FEAT_VARTABS
675 prev_vts = wp->w_buffer->b_p_vts_array;
676 prev_indent = get_indent_str_vtab(line,
677 (int)wp->w_buffer->b_p_ts,
678 wp->w_buffer->b_p_vts_array, wp->w_p_list);
679#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200680 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200681 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200682#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200683 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200684 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200685
686 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200687 if (wp->w_p_brisbr)
688 bri -= vim_strsize(p_sbr);
689
690 /* Add offset for number column, if 'n' is in 'cpoptions' */
691 bri += win_col_off2(wp);
692
693 /* never indent past left window margin */
694 if (bri < 0)
695 bri = 0;
696 /* always leave at least bri_min characters on the left,
697 * if text width is sufficient */
698 else if (bri > eff_wwidth - wp->w_p_brimin)
699 bri = (eff_wwidth - wp->w_p_brimin < 0)
700 ? 0 : eff_wwidth - wp->w_p_brimin;
701
702 return bri;
703}
704#endif
705
706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
708
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709/*
710 * Return TRUE if the string "line" starts with a word from 'cinwords'.
711 */
712 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100713cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
715 char_u *cinw;
716 char_u *cinw_buf;
717 int cinw_len;
718 int retval = FALSE;
719 int len;
720
721 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
722 cinw_buf = alloc((unsigned)cinw_len);
723 if (cinw_buf != NULL)
724 {
725 line = skipwhite(line);
726 for (cinw = curbuf->b_p_cinw; *cinw; )
727 {
728 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
729 if (STRNCMP(line, cinw_buf, len) == 0
730 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
731 {
732 retval = TRUE;
733 break;
734 }
735 }
736 vim_free(cinw_buf);
737 }
738 return retval;
739}
740#endif
741
742/*
743 * open_line: Add a new line below or above the current line.
744 *
745 * For VREPLACE mode, we only add a new line when we get to the end of the
746 * file, otherwise we just start replacing the next line.
747 *
748 * Caller must take care of undo. Since VREPLACE may affect any number of
749 * lines however, it may call u_save_cursor() again when starting to change a
750 * new line.
751 * "flags": OPENLINE_DELSPACES delete spaces after cursor
752 * OPENLINE_DO_COM format comments
753 * OPENLINE_KEEPTRAIL keep trailing spaces
754 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200755 * OPENLINE_COM_LIST format comments with list or 2nd line indent
756 *
757 * "second_line_indent": indent for after ^^D in Insert mode or if flag
758 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 *
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200760 * Return OK for success, FAIL for failure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 */
762 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100763open_line(
764 int dir, /* FORWARD or BACKWARD */
765 int flags,
766 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767{
768 char_u *saved_line; /* copy of the original line */
769 char_u *next_line = NULL; /* copy of the next line */
770 char_u *p_extra = NULL; /* what goes to next line */
771 int less_cols = 0; /* less columns for mark in new line */
772 int less_cols_off = 0; /* columns to skip for mark adjust */
773 pos_T old_cursor; /* old cursor position */
774 int newcol = 0; /* new cursor column */
775 int newindent = 0; /* auto-indent of the new line */
776 int n;
777 int trunc_line = FALSE; /* truncate current line afterwards */
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200778 int retval = FAIL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779#ifdef FEAT_COMMENTS
780 int extra_len = 0; /* length of p_extra string */
781 int lead_len; /* length of comment leader */
782 char_u *lead_flags; /* position in 'comments' for comment leader */
783 char_u *leader = NULL; /* copy of comment leader */
784#endif
785 char_u *allocated = NULL; /* allocated memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 int saved_char = NUL; /* init for GCC */
788#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
789 pos_T *pos;
790#endif
791#ifdef FEAT_SMARTINDENT
792 int do_si = (!p_paste && curbuf->b_p_si
793# ifdef FEAT_CINDENT
794 && !curbuf->b_p_cin
795# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200796# ifdef FEAT_EVAL
797 && *curbuf->b_p_inde == NUL
798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 );
800 int no_si = FALSE; /* reset did_si afterwards */
801 int first_char = NUL; /* init for GCC */
802#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200803#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 int vreplace_mode;
805#endif
806 int did_append; /* appended a new line */
807 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
808
809 /*
810 * make a copy of the current line so we can mess with it
811 */
812 saved_line = vim_strsave(ml_get_curline());
813 if (saved_line == NULL) /* out of memory! */
814 return FALSE;
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (State & VREPLACE_FLAG)
817 {
818 /*
819 * With VREPLACE we make a copy of the next line, which we will be
820 * starting to replace. First make the new line empty and let vim play
821 * with the indenting and comment leader to its heart's content. Then
822 * we grab what it ended up putting on the new line, put back the
823 * original line, and call ins_char() to put each new character onto
824 * the line, replacing what was there before and pushing the right
825 * stuff onto the replace stack. -- webb.
826 */
827 if (curwin->w_cursor.lnum < orig_line_count)
828 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
829 else
830 next_line = vim_strsave((char_u *)"");
831 if (next_line == NULL) /* out of memory! */
832 goto theend;
833
834 /*
835 * In VREPLACE mode, a NL replaces the rest of the line, and starts
836 * replacing the next line, so push all of the characters left on the
837 * line onto the replace stack. We'll push any other characters that
838 * might be replaced at the start of the next line (due to autoindent
839 * etc) a bit later.
840 */
841 replace_push(NUL); /* Call twice because BS over NL expects it */
842 replace_push(NUL);
843 p = saved_line + curwin->w_cursor.col;
844 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000845 {
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000846 if (has_mbyte)
847 p += replace_push_mb(p);
848 else
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000849 replace_push(*p++);
850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 saved_line[curwin->w_cursor.col] = NUL;
852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200854 if ((State & INSERT) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
856 p_extra = saved_line + curwin->w_cursor.col;
857#ifdef FEAT_SMARTINDENT
858 if (do_si) /* need first char after new line break */
859 {
860 p = skipwhite(p_extra);
861 first_char = *p;
862 }
863#endif
864#ifdef FEAT_COMMENTS
865 extra_len = (int)STRLEN(p_extra);
866#endif
867 saved_char = *p_extra;
868 *p_extra = NUL;
869 }
870
871 u_clearline(); /* cannot do "U" command when adding lines */
872#ifdef FEAT_SMARTINDENT
873 did_si = FALSE;
874#endif
875 ai_col = 0;
876
877 /*
878 * If we just did an auto-indent, then we didn't type anything on
879 * the prior line, and it should be truncated. Do this even if 'ai' is not
880 * set because automatically inserting a comment leader also sets did_ai.
881 */
882 if (dir == FORWARD && did_ai)
883 trunc_line = TRUE;
884
885 /*
886 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
887 * indent to use for the new line.
888 */
889 if (curbuf->b_p_ai
890#ifdef FEAT_SMARTINDENT
891 || do_si
892#endif
893 )
894 {
895 /*
896 * count white space on current line
897 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200898#ifdef FEAT_VARTABS
899 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
900 curbuf->b_p_vts_array, FALSE);
901#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200902 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200903#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200904 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
905 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
907#ifdef FEAT_SMARTINDENT
908 /*
909 * Do smart indenting.
910 * In insert/replace mode (only when dir == FORWARD)
911 * we may move some text to the next line. If it starts with '{'
912 * don't add an indent. Fixes inserting a NL before '{' in line
913 * "if (condition) {"
914 */
915 if (!trunc_line && do_si && *saved_line != NUL
916 && (p_extra == NULL || first_char != '{'))
917 {
918 char_u *ptr;
919 char_u last_char;
920
921 old_cursor = curwin->w_cursor;
922 ptr = saved_line;
923# ifdef FEAT_COMMENTS
924 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200925 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 else
927 lead_len = 0;
928# endif
929 if (dir == FORWARD)
930 {
931 /*
932 * Skip preprocessor directives, unless they are
933 * recognised as comments.
934 */
935 if (
936# ifdef FEAT_COMMENTS
937 lead_len == 0 &&
938# endif
939 ptr[0] == '#')
940 {
941 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
942 ptr = ml_get(--curwin->w_cursor.lnum);
943 newindent = get_indent();
944 }
945# ifdef FEAT_COMMENTS
946 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200947 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 else
949 lead_len = 0;
950 if (lead_len > 0)
951 {
952 /*
953 * This case gets the following right:
954 * \*
955 * * A comment (read '\' as '/').
956 * *\
957 * #define IN_THE_WAY
958 * This should line up here;
959 */
960 p = skipwhite(ptr);
961 if (p[0] == '/' && p[1] == '*')
962 p++;
963 if (p[0] == '*')
964 {
965 for (p++; *p; p++)
966 {
967 if (p[0] == '/' && p[-1] == '*')
968 {
969 /*
970 * End of C comment, indent should line up
971 * with the line containing the start of
972 * the comment
973 */
974 curwin->w_cursor.col = (colnr_T)(p - ptr);
975 if ((pos = findmatch(NULL, NUL)) != NULL)
976 {
977 curwin->w_cursor.lnum = pos->lnum;
978 newindent = get_indent();
979 }
980 }
981 }
982 }
983 }
984 else /* Not a comment line */
985# endif
986 {
987 /* Find last non-blank in line */
988 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100989 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 --p;
991 last_char = *p;
992
993 /*
994 * find the character just before the '{' or ';'
995 */
996 if (last_char == '{' || last_char == ';')
997 {
998 if (p > ptr)
999 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001000 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 --p;
1002 }
1003 /*
1004 * Try to catch lines that are split over multiple
1005 * lines. eg:
1006 * if (condition &&
1007 * condition) {
1008 * Should line up here!
1009 * }
1010 */
1011 if (*p == ')')
1012 {
1013 curwin->w_cursor.col = (colnr_T)(p - ptr);
1014 if ((pos = findmatch(NULL, '(')) != NULL)
1015 {
1016 curwin->w_cursor.lnum = pos->lnum;
1017 newindent = get_indent();
1018 ptr = ml_get_curline();
1019 }
1020 }
1021 /*
1022 * If last character is '{' do indent, without
1023 * checking for "if" and the like.
1024 */
1025 if (last_char == '{')
1026 {
1027 did_si = TRUE; /* do indent */
1028 no_si = TRUE; /* don't delete it when '{' typed */
1029 }
1030 /*
1031 * Look for "if" and the like, use 'cinwords'.
1032 * Don't do this if the previous line ended in ';' or
1033 * '}'.
1034 */
1035 else if (last_char != ';' && last_char != '}'
1036 && cin_is_cinword(ptr))
1037 did_si = TRUE;
1038 }
1039 }
1040 else /* dir == BACKWARD */
1041 {
1042 /*
1043 * Skip preprocessor directives, unless they are
1044 * recognised as comments.
1045 */
1046 if (
1047# ifdef FEAT_COMMENTS
1048 lead_len == 0 &&
1049# endif
1050 ptr[0] == '#')
1051 {
1052 int was_backslashed = FALSE;
1053
1054 while ((ptr[0] == '#' || was_backslashed) &&
1055 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1056 {
1057 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1058 was_backslashed = TRUE;
1059 else
1060 was_backslashed = FALSE;
1061 ptr = ml_get(++curwin->w_cursor.lnum);
1062 }
1063 if (was_backslashed)
1064 newindent = 0; /* Got to end of file */
1065 else
1066 newindent = get_indent();
1067 }
1068 p = skipwhite(ptr);
1069 if (*p == '}') /* if line starts with '}': do indent */
1070 did_si = TRUE;
1071 else /* can delete indent when '{' typed */
1072 can_si_back = TRUE;
1073 }
1074 curwin->w_cursor = old_cursor;
1075 }
1076 if (do_si)
1077 can_si = TRUE;
1078#endif /* FEAT_SMARTINDENT */
1079
1080 did_ai = TRUE;
1081 }
1082
1083#ifdef FEAT_COMMENTS
1084 /*
1085 * Find out if the current line starts with a comment leader.
1086 * This may then be inserted in front of the new line.
1087 */
1088 end_comment_pending = NUL;
1089 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +02001090 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 else
1092 lead_len = 0;
1093 if (lead_len > 0)
1094 {
1095 char_u *lead_repl = NULL; /* replaces comment leader */
1096 int lead_repl_len = 0; /* length of *lead_repl */
1097 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
1098 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
1099 char_u *comment_end = NULL; /* where lead_end has been found */
1100 int extra_space = FALSE; /* append extra space */
1101 int current_flag;
1102 int require_blank = FALSE; /* requires blank after middle */
1103 char_u *p2;
1104
1105 /*
1106 * If the comment leader has the start, middle or end flag, it may not
1107 * be used or may be replaced with the middle leader.
1108 */
1109 for (p = lead_flags; *p && *p != ':'; ++p)
1110 {
1111 if (*p == COM_BLANK)
1112 {
1113 require_blank = TRUE;
1114 continue;
1115 }
1116 if (*p == COM_START || *p == COM_MIDDLE)
1117 {
1118 current_flag = *p;
1119 if (*p == COM_START)
1120 {
1121 /*
1122 * Doing "O" on a start of comment does not insert leader.
1123 */
1124 if (dir == BACKWARD)
1125 {
1126 lead_len = 0;
1127 break;
1128 }
1129
1130 /* find start of middle part */
1131 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1132 require_blank = FALSE;
1133 }
1134
1135 /*
1136 * Isolate the strings of the middle and end leader.
1137 */
1138 while (*p && p[-1] != ':') /* find end of middle flags */
1139 {
1140 if (*p == COM_BLANK)
1141 require_blank = TRUE;
1142 ++p;
1143 }
1144 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1145
1146 while (*p && p[-1] != ':') /* find end of end flags */
1147 {
1148 /* Check whether we allow automatic ending of comments */
1149 if (*p == COM_AUTO_END)
1150 end_comment_pending = -1; /* means we want to set it */
1151 ++p;
1152 }
1153 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1154
1155 if (end_comment_pending == -1) /* we can set it now */
1156 end_comment_pending = lead_end[n - 1];
1157
1158 /*
1159 * If the end of the comment is in the same line, don't use
1160 * the comment leader.
1161 */
1162 if (dir == FORWARD)
1163 {
1164 for (p = saved_line + lead_len; *p; ++p)
1165 if (STRNCMP(p, lead_end, n) == 0)
1166 {
1167 comment_end = p;
1168 lead_len = 0;
1169 break;
1170 }
1171 }
1172
1173 /*
1174 * Doing "o" on a start of comment inserts the middle leader.
1175 */
1176 if (lead_len > 0)
1177 {
1178 if (current_flag == COM_START)
1179 {
1180 lead_repl = lead_middle;
1181 lead_repl_len = (int)STRLEN(lead_middle);
1182 }
1183
1184 /*
1185 * If we have hit RETURN immediately after the start
1186 * comment leader, then put a space after the middle
1187 * comment leader on the next line.
1188 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001189 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 && ((p_extra != NULL
1191 && (int)curwin->w_cursor.col == lead_len)
1192 || (p_extra == NULL
1193 && saved_line[lead_len] == NUL)
1194 || require_blank))
1195 extra_space = TRUE;
1196 }
1197 break;
1198 }
1199 if (*p == COM_END)
1200 {
1201 /*
1202 * Doing "o" on the end of a comment does not insert leader.
1203 * Remember where the end is, might want to use it to find the
1204 * start (for C-comments).
1205 */
1206 if (dir == FORWARD)
1207 {
1208 comment_end = skipwhite(saved_line);
1209 lead_len = 0;
1210 break;
1211 }
1212
1213 /*
1214 * Doing "O" on the end of a comment inserts the middle leader.
1215 * Find the string for the middle leader, searching backwards.
1216 */
1217 while (p > curbuf->b_p_com && *p != ',')
1218 --p;
1219 for (lead_repl = p; lead_repl > curbuf->b_p_com
1220 && lead_repl[-1] != ':'; --lead_repl)
1221 ;
1222 lead_repl_len = (int)(p - lead_repl);
1223
1224 /* We can probably always add an extra space when doing "O" on
1225 * the comment-end */
1226 extra_space = TRUE;
1227
1228 /* Check whether we allow automatic ending of comments */
1229 for (p2 = p; *p2 && *p2 != ':'; p2++)
1230 {
1231 if (*p2 == COM_AUTO_END)
1232 end_comment_pending = -1; /* means we want to set it */
1233 }
1234 if (end_comment_pending == -1)
1235 {
1236 /* Find last character in end-comment string */
1237 while (*p2 && *p2 != ',')
1238 p2++;
1239 end_comment_pending = p2[-1];
1240 }
1241 break;
1242 }
1243 if (*p == COM_FIRST)
1244 {
1245 /*
1246 * Comment leader for first line only: Don't repeat leader
1247 * when using "O", blank out leader when using "o".
1248 */
1249 if (dir == BACKWARD)
1250 lead_len = 0;
1251 else
1252 {
1253 lead_repl = (char_u *)"";
1254 lead_repl_len = 0;
1255 }
1256 break;
1257 }
1258 }
1259 if (lead_len)
1260 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001261 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001262 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001263 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 allocated = leader; /* remember to free it later */
1265
1266 if (leader == NULL)
1267 lead_len = 0;
1268 else
1269 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001270 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271
1272 /*
1273 * Replace leader with lead_repl, right or left adjusted
1274 */
1275 if (lead_repl != NULL)
1276 {
1277 int c = 0;
1278 int off = 0;
1279
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001280 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 {
1282 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001283 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 else if (VIM_ISDIGIT(*p) || *p == '-')
1285 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001286 else
1287 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289 if (c == COM_RIGHT) /* right adjusted leader */
1290 {
1291 /* find last non-white in the leader to line up with */
1292 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001293 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001296
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001297 /* Compute the length of the replaced characters in
1298 * screen characters, not bytes. */
1299 {
1300 int repl_size = vim_strnsize(lead_repl,
1301 lead_repl_len);
1302 int old_size = 0;
1303 char_u *endp = p;
1304 int l;
1305
1306 while (old_size < repl_size && p > leader)
1307 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001308 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001309 old_size += ptr2cells(p);
1310 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001311 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001312 if (l != 0)
1313 mch_memmove(endp + l, endp,
1314 (size_t)((leader + lead_len) - endp));
1315 lead_len += l;
1316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1318 if (p + lead_repl_len > leader + lead_len)
1319 p[lead_repl_len] = NUL;
1320
1321 /* blank-out any other chars from the old leader. */
1322 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001323 {
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001324 int l = mb_head_off(leader, p);
1325
1326 if (l > 1)
1327 {
1328 p -= l;
1329 if (ptr2cells(p) > 1)
1330 {
1331 p[1] = ' ';
1332 --l;
1333 }
1334 mch_memmove(p + 1, p + l + 1,
1335 (size_t)((leader + lead_len) - (p + l + 1)));
1336 lead_len -= l;
1337 *p = ' ';
1338 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001339 else if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343 else /* left adjusted leader */
1344 {
1345 p = skipwhite(leader);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001346
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001347 /* Compute the length of the replaced characters in
1348 * screen characters, not bytes. Move the part that is
1349 * not to be overwritten. */
1350 {
1351 int repl_size = vim_strnsize(lead_repl,
1352 lead_repl_len);
1353 int i;
1354 int l;
1355
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001356 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001357 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001358 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001359 if (vim_strnsize(p, i + l) > repl_size)
1360 break;
1361 }
1362 if (i != lead_repl_len)
1363 {
1364 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001365 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001366 lead_len += lead_repl_len - i;
1367 }
1368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1370
1371 /* Replace any remaining non-white chars in the old
1372 * leader by spaces. Keep Tabs, the indent must
1373 * remain the same. */
1374 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001375 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 {
1377 /* Don't put a space before a TAB. */
1378 if (p + 1 < leader + lead_len && p[1] == TAB)
1379 {
1380 --lead_len;
1381 mch_memmove(p, p + 1,
1382 (leader + lead_len) - p);
1383 }
1384 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001385 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001386 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001387
1388 if (l > 1)
1389 {
1390 if (ptr2cells(p) > 1)
1391 {
1392 /* Replace a double-wide char with
1393 * two spaces */
1394 --l;
1395 *p++ = ' ';
1396 }
1397 mch_memmove(p + 1, p + l,
1398 (leader + lead_len) - p);
1399 lead_len -= l - 1;
1400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404 *p = NUL;
1405 }
1406
1407 /* Recompute the indent, it may have changed. */
1408 if (curbuf->b_p_ai
1409#ifdef FEAT_SMARTINDENT
1410 || do_si
1411#endif
1412 )
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001413#ifdef FEAT_VARTABS
1414 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1415 curbuf->b_p_vts_array, FALSE);
1416#else
1417 newindent = get_indent_str(leader,
1418 (int)curbuf->b_p_ts, FALSE);
1419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421 /* Add the indent offset */
1422 if (newindent + off < 0)
1423 {
1424 off = -newindent;
1425 newindent = 0;
1426 }
1427 else
1428 newindent += off;
1429
1430 /* Correct trailing spaces for the shift, so that
1431 * alignment remains equal. */
1432 while (off > 0 && lead_len > 0
1433 && leader[lead_len - 1] == ' ')
1434 {
1435 /* Don't do it when there is a tab before the space */
1436 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1437 break;
1438 --lead_len;
1439 --off;
1440 }
1441
1442 /* If the leader ends in white space, don't add an
1443 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001444 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 extra_space = FALSE;
1446 leader[lead_len] = NUL;
1447 }
1448
1449 if (extra_space)
1450 {
1451 leader[lead_len++] = ' ';
1452 leader[lead_len] = NUL;
1453 }
1454
1455 newcol = lead_len;
1456
1457 /*
1458 * if a new indent will be set below, remove the indent that
1459 * is in the comment leader
1460 */
1461 if (newindent
1462#ifdef FEAT_SMARTINDENT
1463 || did_si
1464#endif
1465 )
1466 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001467 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 {
1469 --lead_len;
1470 --newcol;
1471 ++leader;
1472 }
1473 }
1474
1475 }
1476#ifdef FEAT_SMARTINDENT
1477 did_si = can_si = FALSE;
1478#endif
1479 }
1480 else if (comment_end != NULL)
1481 {
1482 /*
1483 * We have finished a comment, so we don't use the leader.
1484 * If this was a C-comment and 'ai' or 'si' is set do a normal
1485 * indent to align with the line containing the start of the
1486 * comment.
1487 */
1488 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1489 (curbuf->b_p_ai
1490#ifdef FEAT_SMARTINDENT
1491 || do_si
1492#endif
1493 ))
1494 {
1495 old_cursor = curwin->w_cursor;
1496 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1497 if ((pos = findmatch(NULL, NUL)) != NULL)
1498 {
1499 curwin->w_cursor.lnum = pos->lnum;
1500 newindent = get_indent();
1501 }
1502 curwin->w_cursor = old_cursor;
1503 }
1504 }
1505 }
1506#endif
1507
1508 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1509 if (p_extra != NULL)
1510 {
1511 *p_extra = saved_char; /* restore char that NUL replaced */
1512
1513 /*
1514 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1515 * non-blank.
1516 *
1517 * When in REPLACE mode, put the deleted blanks on the replace stack,
1518 * preceded by a NUL, so they can be put back when a BS is entered.
1519 */
1520 if (REPLACE_NORMAL(State))
1521 replace_push(NUL); /* end of extra blanks */
1522 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1523 {
1524 while ((*p_extra == ' ' || *p_extra == '\t')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 && (!enc_utf8
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001526 || !utf_iscomposing(utf_ptr2char(p_extra + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 {
1528 if (REPLACE_NORMAL(State))
1529 replace_push(*p_extra);
1530 ++p_extra;
1531 ++less_cols_off;
1532 }
1533 }
1534 if (*p_extra != NUL)
1535 did_ai = FALSE; /* append some text, don't truncate now */
1536
1537 /* columns for marks adjusted for removed columns */
1538 less_cols = (int)(p_extra - saved_line);
1539 }
1540
1541 if (p_extra == NULL)
1542 p_extra = (char_u *)""; /* append empty line */
1543
1544#ifdef FEAT_COMMENTS
1545 /* concatenate leader and p_extra, if there is a leader */
1546 if (lead_len)
1547 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001548 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1549 {
1550 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001551 int padding = second_line_indent
1552 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001553
1554 /* Here whitespace is inserted after the comment char.
1555 * Below, set_indent(newindent, SIN_INSERT) will insert the
1556 * whitespace needed before the comment char. */
1557 for (i = 0; i < padding; i++)
1558 {
1559 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001560 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001561 newcol++;
1562 }
1563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 STRCAT(leader, p_extra);
1565 p_extra = leader;
1566 did_ai = TRUE; /* So truncating blanks works with comments */
1567 less_cols -= lead_len;
1568 }
1569 else
1570 end_comment_pending = NUL; /* turns out there was no leader */
1571#endif
1572
1573 old_cursor = curwin->w_cursor;
1574 if (dir == BACKWARD)
1575 --curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 {
1578 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1579 == FAIL)
1580 goto theend;
1581 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001582 * with markers.
1583 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001584 * be marks there. But still needed in diff mode. */
1585 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1586#ifdef FEAT_DIFF
1587 || curwin->w_p_diff
1588#endif
1589 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001590 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 did_append = TRUE;
1592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 else
1594 {
1595 /*
1596 * In VREPLACE mode we are starting to replace the next line.
1597 */
1598 curwin->w_cursor.lnum++;
1599 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1600 {
1601 /* In case we NL to a new line, BS to the previous one, and NL
1602 * again, we don't want to save the new line for undo twice.
1603 */
1604 (void)u_save_cursor(); /* errors are ignored! */
1605 vr_lines_changed++;
1606 }
1607 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1608 changed_bytes(curwin->w_cursor.lnum, 0);
1609 curwin->w_cursor.lnum--;
1610 did_append = FALSE;
1611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
1613 if (newindent
1614#ifdef FEAT_SMARTINDENT
1615 || did_si
1616#endif
1617 )
1618 {
1619 ++curwin->w_cursor.lnum;
1620#ifdef FEAT_SMARTINDENT
1621 if (did_si)
1622 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001623 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001626 newindent -= newindent % sw;
1627 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 }
1629#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001630 /* Copy the indent */
1631 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 {
1633 (void)copy_indent(newindent, saved_line);
1634
1635 /*
1636 * Set the 'preserveindent' option so that any further screwing
1637 * with the line doesn't entirely destroy our efforts to preserve
1638 * it. It gets restored at the function end.
1639 */
1640 curbuf->b_p_pi = TRUE;
1641 }
1642 else
1643 (void)set_indent(newindent, SIN_INSERT);
1644 less_cols -= curwin->w_cursor.col;
1645
1646 ai_col = curwin->w_cursor.col;
1647
1648 /*
1649 * In REPLACE mode, for each character in the new indent, there must
1650 * be a NUL on the replace stack, for when it is deleted with BS
1651 */
1652 if (REPLACE_NORMAL(State))
1653 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1654 replace_push(NUL);
1655 newcol += curwin->w_cursor.col;
1656#ifdef FEAT_SMARTINDENT
1657 if (no_si)
1658 did_si = FALSE;
1659#endif
1660 }
1661
1662#ifdef FEAT_COMMENTS
1663 /*
1664 * In REPLACE mode, for each character in the extra leader, there must be
1665 * a NUL on the replace stack, for when it is deleted with BS.
1666 */
1667 if (REPLACE_NORMAL(State))
1668 while (lead_len-- > 0)
1669 replace_push(NUL);
1670#endif
1671
1672 curwin->w_cursor = old_cursor;
1673
1674 if (dir == FORWARD)
1675 {
1676 if (trunc_line || (State & INSERT))
1677 {
1678 /* truncate current line at cursor */
1679 saved_line[curwin->w_cursor.col] = NUL;
1680 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1681 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1682 truncate_spaces(saved_line);
1683 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1684 saved_line = NULL;
1685 if (did_append)
1686 {
1687 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1688 curwin->w_cursor.lnum + 1, 1L);
1689 did_append = FALSE;
1690
1691 /* Move marks after the line break to the new line. */
1692 if (flags & OPENLINE_MARKFIX)
1693 mark_col_adjust(curwin->w_cursor.lnum,
1694 curwin->w_cursor.col + less_cols_off,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01001695 1L, (long)-less_cols, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 }
1697 else
1698 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1699 }
1700
1701 /*
1702 * Put the cursor on the new line. Careful: the scrollup() above may
1703 * have moved w_cursor, we must use old_cursor.
1704 */
1705 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1706 }
1707 if (did_append)
1708 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1709
1710 curwin->w_cursor.col = newcol;
1711#ifdef FEAT_VIRTUALEDIT
1712 curwin->w_cursor.coladd = 0;
1713#endif
1714
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001715#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 /*
1717 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1718 * fixthisline() from doing it (via change_indent()) by telling it we're in
1719 * normal INSERT mode.
1720 */
1721 if (State & VREPLACE_FLAG)
1722 {
1723 vreplace_mode = State; /* So we know to put things right later */
1724 State = INSERT;
1725 }
1726 else
1727 vreplace_mode = 0;
1728#endif
1729#ifdef FEAT_LISP
1730 /*
1731 * May do lisp indenting.
1732 */
1733 if (!p_paste
1734# ifdef FEAT_COMMENTS
1735 && leader == NULL
1736# endif
1737 && curbuf->b_p_lisp
1738 && curbuf->b_p_ai)
1739 {
1740 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001741 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 }
1743#endif
1744#ifdef FEAT_CINDENT
1745 /*
1746 * May do indenting after opening a new line.
1747 */
1748 if (!p_paste
1749 && (curbuf->b_p_cin
1750# ifdef FEAT_EVAL
1751 || *curbuf->b_p_inde != NUL
1752# endif
1753 )
1754 && in_cinkeys(dir == FORWARD
1755 ? KEY_OPEN_FORW
1756 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1757 {
1758 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001759 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001762#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (vreplace_mode != 0)
1764 State = vreplace_mode;
1765#endif
1766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 /*
1768 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1769 * original line, and inserts the new stuff char by char, pushing old stuff
1770 * onto the replace stack (via ins_char()).
1771 */
1772 if (State & VREPLACE_FLAG)
1773 {
1774 /* Put new line in p_extra */
1775 p_extra = vim_strsave(ml_get_curline());
1776 if (p_extra == NULL)
1777 goto theend;
1778
1779 /* Put back original line */
1780 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1781
1782 /* Insert new stuff into line again */
1783 curwin->w_cursor.col = 0;
1784#ifdef FEAT_VIRTUALEDIT
1785 curwin->w_cursor.coladd = 0;
1786#endif
1787 ins_bytes(p_extra); /* will call changed_bytes() */
1788 vim_free(p_extra);
1789 next_line = NULL;
1790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001792 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793theend:
1794 curbuf->b_p_pi = saved_pi;
1795 vim_free(saved_line);
1796 vim_free(next_line);
1797 vim_free(allocated);
1798 return retval;
1799}
1800
1801#if defined(FEAT_COMMENTS) || defined(PROTO)
1802/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001803 * get_leader_len() returns the length in bytes of the prefix of the given
1804 * string which introduces a comment. If this string is not a comment then
1805 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 * When "flags" is not NULL, it is set to point to the flags of the recognized
1807 * comment leader.
1808 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001809 * If "include_space" is set, include trailing whitespace while calculating the
1810 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 */
1812 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001813get_leader_len(
1814 char_u *line,
1815 char_u **flags,
1816 int backward,
1817 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
1819 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001820 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 int got_com = FALSE;
1822 int found_one;
1823 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1824 char_u *string; /* pointer to comment string */
1825 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001826 int middle_match_len = 0;
1827 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001828 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaar81340392012-06-06 16:12:59 +02001830 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001831 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 ++i;
1833
1834 /*
1835 * Repeat to match several nested comment strings.
1836 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001837 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
1839 /*
1840 * scan through the 'comments' option for a match
1841 */
1842 found_one = FALSE;
1843 for (list = curbuf->b_p_com; *list; )
1844 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001845 /* Get one option part into part_buf[]. Advance "list" to next
1846 * one. Put "string" at start of string. */
1847 if (!got_com && flags != NULL)
1848 *flags = list; /* remember where flags started */
1849 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1851 string = vim_strchr(part_buf, ':');
1852 if (string == NULL) /* missing ':', ignore this part */
1853 continue;
1854 *string++ = NUL; /* isolate flags from string */
1855
Bram Moolenaara4271d52011-05-10 13:38:27 +02001856 /* If we found a middle match previously, use that match when this
1857 * is not a middle or end. */
1858 if (middle_match_len != 0
1859 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1860 && vim_strchr(part_buf, COM_END) == NULL)
1861 break;
1862
1863 /* When we already found a nested comment, only accept further
1864 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1866 continue;
1867
Bram Moolenaara4271d52011-05-10 13:38:27 +02001868 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1870 continue;
1871
Bram Moolenaara4271d52011-05-10 13:38:27 +02001872 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 * When string starts with white space, must have some white space
1874 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001875 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001876 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001878 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001879 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001880 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 ++string;
1882 }
1883 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1884 ;
1885 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001886 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
Bram Moolenaara4271d52011-05-10 13:38:27 +02001888 /* When 'b' flag used, there must be white space or an
1889 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001891 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 continue;
1893
Bram Moolenaara4271d52011-05-10 13:38:27 +02001894 /* We have found a match, stop searching unless this is a middle
1895 * comment. The middle comment can be a substring of the end
1896 * comment in which case it's better to return the length of the
1897 * end comment and its flags. Thus we keep searching with middle
1898 * and end matches and use an end match if it matches better. */
1899 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1900 {
1901 if (middle_match_len == 0)
1902 {
1903 middle_match_len = j;
1904 saved_flags = prev_list;
1905 }
1906 continue;
1907 }
1908 if (middle_match_len != 0 && j > middle_match_len)
1909 /* Use this match instead of the middle match, since it's a
1910 * longer thus better match. */
1911 middle_match_len = 0;
1912
1913 if (middle_match_len == 0)
1914 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 found_one = TRUE;
1916 break;
1917 }
1918
Bram Moolenaara4271d52011-05-10 13:38:27 +02001919 if (middle_match_len != 0)
1920 {
1921 /* Use the previously found middle match after failing to find a
1922 * match with an end. */
1923 if (!got_com && flags != NULL)
1924 *flags = saved_flags;
1925 i += middle_match_len;
1926 found_one = TRUE;
1927 }
1928
1929 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (!found_one)
1931 break;
1932
Bram Moolenaar81340392012-06-06 16:12:59 +02001933 result = i;
1934
Bram Moolenaara4271d52011-05-10 13:38:27 +02001935 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001936 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 ++i;
1938
Bram Moolenaar81340392012-06-06 16:12:59 +02001939 if (include_space)
1940 result = i;
1941
Bram Moolenaara4271d52011-05-10 13:38:27 +02001942 /* If this comment doesn't nest, stop here. */
1943 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (vim_strchr(part_buf, COM_NEST) == NULL)
1945 break;
1946 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001947 return result;
1948}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001949
Bram Moolenaar81340392012-06-06 16:12:59 +02001950/*
1951 * Return the offset at which the last comment in line starts. If there is no
1952 * comment in the whole line, -1 is returned.
1953 *
1954 * When "flags" is not null, it is set to point to the flags describing the
1955 * recognized comment leader.
1956 */
1957 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001958get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001959{
1960 int result = -1;
1961 int i, j;
1962 int lower_check_bound = 0;
1963 char_u *string;
1964 char_u *com_leader;
1965 char_u *com_flags;
1966 char_u *list;
1967 int found_one;
1968 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1969
1970 /*
1971 * Repeat to match several nested comment strings.
1972 */
1973 i = (int)STRLEN(line);
1974 while (--i >= lower_check_bound)
1975 {
1976 /*
1977 * scan through the 'comments' option for a match
1978 */
1979 found_one = FALSE;
1980 for (list = curbuf->b_p_com; *list; )
1981 {
1982 char_u *flags_save = list;
1983
1984 /*
1985 * Get one option part into part_buf[]. Advance list to next one.
1986 * put string at start of string.
1987 */
1988 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1989 string = vim_strchr(part_buf, ':');
1990 if (string == NULL) /* If everything is fine, this cannot actually
1991 * happen. */
1992 {
1993 continue;
1994 }
1995 *string++ = NUL; /* Isolate flags from string. */
1996 com_leader = string;
1997
1998 /*
1999 * Line contents and string must match.
2000 * When string starts with white space, must have some white space
2001 * (but the amount does not need to match, there might be a mix of
2002 * TABs and spaces).
2003 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002004 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002005 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002006 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002007 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002008 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002009 ++string;
2010 }
2011 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
2012 /* do nothing */;
2013 if (string[j] != NUL)
2014 continue;
2015
2016 /*
2017 * When 'b' flag used, there must be white space or an
2018 * end-of-line after the string in the line.
2019 */
2020 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002021 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02002022 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002023
Bram Moolenaar4af72592018-12-09 15:00:52 +01002024 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +01002025 {
Bram Moolenaar4af72592018-12-09 15:00:52 +01002026 // For a middlepart comment, only consider it to match if
2027 // everything before the current position in the line is
2028 // whitespace. Otherwise we would think we are inside a
2029 // comment if the middle part appears somewhere in the middle
2030 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +01002031 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
2032 ;
2033 if (j < i)
2034 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +02002035 }
2036
2037 /*
2038 * We have found a match, stop searching.
2039 */
2040 found_one = TRUE;
2041
2042 if (flags)
2043 *flags = flags_save;
2044 com_flags = flags_save;
2045
2046 break;
2047 }
2048
2049 if (found_one)
2050 {
2051 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2052 int len1, len2, off;
2053
2054 result = i;
2055 /*
2056 * If this comment nests, continue searching.
2057 */
2058 if (vim_strchr(part_buf, COM_NEST) != NULL)
2059 continue;
2060
2061 lower_check_bound = i;
2062
2063 /* Let's verify whether the comment leader found is a substring
2064 * of other comment leaders. If it is, let's adjust the
2065 * lower_check_bound so that we make sure that we have determined
2066 * the comment leader correctly.
2067 */
2068
Bram Moolenaar1c465442017-03-12 20:10:05 +01002069 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002070 ++com_leader;
2071 len1 = (int)STRLEN(com_leader);
2072
2073 for (list = curbuf->b_p_com; *list; )
2074 {
2075 char_u *flags_save = list;
2076
2077 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2078 if (flags_save == com_flags)
2079 continue;
2080 string = vim_strchr(part_buf2, ':');
2081 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002082 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002083 ++string;
2084 len2 = (int)STRLEN(string);
2085 if (len2 == 0)
2086 continue;
2087
2088 /* Now we have to verify whether string ends with a substring
2089 * beginning the com_leader. */
2090 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2091 {
2092 --off;
2093 if (!STRNCMP(string + off, com_leader, len2 - off))
2094 {
2095 if (i - off < lower_check_bound)
2096 lower_check_bound = i - off;
2097 }
2098 }
2099 }
2100 }
2101 }
2102 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103}
2104#endif
2105
2106/*
2107 * Return the number of window lines occupied by buffer line "lnum".
2108 */
2109 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002110plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111{
2112 return plines_win(curwin, lnum, TRUE);
2113}
2114
2115 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002116plines_win(
2117 win_T *wp,
2118 linenr_T lnum,
2119 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120{
2121#if defined(FEAT_DIFF) || defined(PROTO)
2122 /* Check for filler lines above this buffer line. When folded the result
2123 * is one line anyway. */
2124 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2125}
2126
2127 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002128plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129{
2130 return plines_win_nofill(curwin, lnum, TRUE);
2131}
2132
2133 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002134plines_win_nofill(
2135 win_T *wp,
2136 linenr_T lnum,
2137 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138{
2139#endif
2140 int lines;
2141
2142 if (!wp->w_p_wrap)
2143 return 1;
2144
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (wp->w_width == 0)
2146 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
2148#ifdef FEAT_FOLDING
2149 /* A folded lines is handled just like an empty line. */
2150 /* NOTE: Caller must handle lines that are MAYBE folded. */
2151 if (lineFolded(wp, lnum) == TRUE)
2152 return 1;
2153#endif
2154
2155 lines = plines_win_nofold(wp, lnum);
2156 if (winheight > 0 && lines > wp->w_height)
2157 return (int)wp->w_height;
2158 return lines;
2159}
2160
2161/*
2162 * Return number of window lines physical line "lnum" will occupy in window
2163 * "wp". Does not care about folding, 'wrap' or 'diff'.
2164 */
2165 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002166plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167{
2168 char_u *s;
2169 long col;
2170 int width;
2171
2172 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2173 if (*s == NUL) /* empty line */
2174 return 1;
2175 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2176
2177 /*
2178 * If list mode is on, then the '$' at the end of the line may take up one
2179 * extra column.
2180 */
2181 if (wp->w_p_list && lcs_eol != NUL)
2182 col += 1;
2183
2184 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002185 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002187 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 if (width <= 0)
2189 return 32000;
2190 if (col <= width)
2191 return 1;
2192 col -= width;
2193 width += win_col_off2(wp);
2194 return (col + (width - 1)) / width + 1;
2195}
2196
2197/*
2198 * Like plines_win(), but only reports the number of physical screen lines
2199 * used from the start of the line to the given column number.
2200 */
2201 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002202plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203{
2204 long col;
2205 char_u *s;
2206 int lines = 0;
2207 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002208 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210#ifdef FEAT_DIFF
2211 /* Check for filler lines above this buffer line. When folded the result
2212 * is one line anyway. */
2213 lines = diff_check_fill(wp, lnum);
2214#endif
2215
2216 if (!wp->w_p_wrap)
2217 return lines + 1;
2218
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (wp->w_width == 0)
2220 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
Bram Moolenaar597a4222014-06-25 14:39:50 +02002222 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
2224 col = 0;
2225 while (*s != NUL && --column >= 0)
2226 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002227 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002228 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
2230
2231 /*
2232 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2233 * INSERT mode, then col must be adjusted so that it represents the last
2234 * screen position of the TAB. This only fixes an error when the TAB wraps
2235 * from one screen line to the next (when 'columns' is not a multiple of
2236 * 'ts') -- webb.
2237 */
2238 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002239 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240
2241 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002242 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002244 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002245 if (width <= 0)
2246 return 9999;
2247
2248 lines += 1;
2249 if (col > width)
2250 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2251 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252}
2253
2254 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002255plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
2257 int count = 0;
2258
2259 while (first <= last)
2260 {
2261#ifdef FEAT_FOLDING
2262 int x;
2263
2264 /* Check if there are any really folded lines, but also included lines
2265 * that are maybe folded. */
2266 x = foldedCount(wp, first, NULL);
2267 if (x > 0)
2268 {
2269 ++count; /* count 1 for "+-- folded" line */
2270 first += x;
2271 }
2272 else
2273#endif
2274 {
2275#ifdef FEAT_DIFF
2276 if (first == wp->w_topline)
2277 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2278 else
2279#endif
2280 count += plines_win(wp, first, TRUE);
2281 ++first;
2282 }
2283 }
2284 return (count);
2285}
2286
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287/*
2288 * Insert string "p" at the cursor position. Stops at a NUL byte.
2289 * Handles Replace mode and multi-byte characters.
2290 */
2291 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002292ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
2294 ins_bytes_len(p, (int)STRLEN(p));
2295}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297/*
2298 * Insert string "p" with length "len" at the cursor position.
2299 * Handles Replace mode and multi-byte characters.
2300 */
2301 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002302ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303{
2304 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 int n;
2306
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002307 if (has_mbyte)
2308 for (i = 0; i < len; i += n)
2309 {
2310 if (enc_utf8)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002311 // avoid reading past p[len]
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002312 n = utfc_ptr2len_len(p + i, len - i);
2313 else
2314 n = (*mb_ptr2len)(p + i);
2315 ins_char_bytes(p + i, n);
2316 }
2317 else
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002318 for (i = 0; i < len; ++i)
2319 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
2322/*
2323 * Insert or replace a single character at the cursor position.
2324 * When in REPLACE or VREPLACE mode, replace any existing character.
2325 * Caller must have prepared for undo.
2326 * For multi-byte characters we get the whole character, the caller must
2327 * convert bytes to a character.
2328 */
2329 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002330ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002332 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002333 int n = (*mb_char2bytes)(c, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
2335 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2336 * Happens for CTRL-Vu9900. */
2337 if (buf[0] == 0)
2338 buf[0] = '\n';
2339
2340 ins_char_bytes(buf, n);
2341}
2342
2343 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002344ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
2346 int c = buf[0];
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002347 int newlen; // nr of bytes inserted
2348 int oldlen; // nr of bytes deleted (0 when not replacing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 char_u *p;
2350 char_u *newp;
2351 char_u *oldp;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002352 int linelen; // length of old line including NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 colnr_T col;
2354 linenr_T lnum = curwin->w_cursor.lnum;
2355 int i;
2356
2357#ifdef FEAT_VIRTUALEDIT
2358 /* Break tabs if needed. */
2359 if (virtual_active() && curwin->w_cursor.coladd > 0)
2360 coladvance_force(getviscol());
2361#endif
2362
2363 col = curwin->w_cursor.col;
2364 oldp = ml_get(lnum);
2365 linelen = (int)STRLEN(oldp) + 1;
2366
2367 /* The lengths default to the values for when not replacing. */
2368 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371 if (State & REPLACE_FLAG)
2372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (State & VREPLACE_FLAG)
2374 {
2375 colnr_T new_vcol = 0; /* init for GCC */
2376 colnr_T vcol;
2377 int old_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379 /*
2380 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2381 * Returns the old value of list, so when finished,
2382 * curwin->w_p_list should be set back to this.
2383 */
2384 old_list = curwin->w_p_list;
2385 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2386 curwin->w_p_list = FALSE;
2387
2388 /*
2389 * In virtual replace mode each character may replace one or more
2390 * characters (zero if it's a TAB). Count the number of bytes to
2391 * be deleted to make room for the new character, counting screen
2392 * cells. May result in adding spaces to fill a gap.
2393 */
2394 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 new_vcol = vcol + chartabsize(buf, vcol);
2396 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2397 {
2398 vcol += chartabsize(oldp + col + oldlen, vcol);
2399 /* Don't need to remove a TAB that takes us to the right
2400 * position. */
2401 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2402 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002403 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 /* Deleted a bit too much, insert spaces. */
2405 if (vcol > new_vcol)
2406 newlen += vcol - new_vcol;
2407 }
2408 curwin->w_p_list = old_list;
2409 }
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002410 else if (oldp[col] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 {
2412 /* normal replace */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002413 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415
2416
2417 /* Push the replaced bytes onto the replace stack, so that they can be
2418 * put back when BS is used. The bytes of a multi-byte character are
2419 * done the other way around, so that the first byte is popped off
2420 * first (it tells the byte length of the character). */
2421 replace_push(NUL);
2422 for (i = 0; i < oldlen; ++i)
2423 {
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002424 if (has_mbyte)
2425 i += replace_push_mb(oldp + col + i) - 1;
2426 else
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002427 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429 }
2430
2431 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2432 if (newp == NULL)
2433 return;
2434
2435 /* Copy bytes before the cursor. */
2436 if (col > 0)
2437 mch_memmove(newp, oldp, (size_t)col);
2438
2439 /* Copy bytes after the changed character(s). */
2440 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002441 if (linelen > col + oldlen)
2442 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 (size_t)(linelen - col - oldlen));
2444
2445 /* Insert or overwrite the new character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 mch_memmove(p, buf, charlen);
2447 i = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 /* Fill with spaces when necessary. */
2450 while (i < newlen)
2451 p[i++] = ' ';
2452
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002453 // Replace the line in the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 ml_replace(lnum, newp, FALSE);
2455
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002456 // mark the buffer as changed and prepare for displaying
2457 inserted_bytes(lnum, col, newlen - oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459 /*
2460 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2461 * show the match for right parens and braces.
2462 */
2463 if (p_sm && (State & INSERT)
2464 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002465#ifdef FEAT_INS_EXPAND
2466 && !ins_compl_active()
2467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002469 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002470 if (has_mbyte)
2471 showmatch(mb_ptr2char(buf));
2472 else
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002473 showmatch(c);
2474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476#ifdef FEAT_RIGHTLEFT
2477 if (!p_ri || (State & REPLACE_FLAG))
2478#endif
2479 {
2480 /* Normal insert: move cursor right */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 curwin->w_cursor.col += charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 }
2483 /*
2484 * TODO: should try to update w_row here, to avoid recomputing it later.
2485 */
2486}
2487
2488/*
2489 * Insert a string at the cursor position.
2490 * Note: Does NOT handle Replace mode.
2491 * Caller must have prepared for undo.
2492 */
2493 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002494ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495{
2496 char_u *oldp, *newp;
2497 int newlen = (int)STRLEN(s);
2498 int oldlen;
2499 colnr_T col;
2500 linenr_T lnum = curwin->w_cursor.lnum;
2501
2502#ifdef FEAT_VIRTUALEDIT
2503 if (virtual_active() && curwin->w_cursor.coladd > 0)
2504 coladvance_force(getviscol());
2505#endif
2506
2507 col = curwin->w_cursor.col;
2508 oldp = ml_get(lnum);
2509 oldlen = (int)STRLEN(oldp);
2510
2511 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2512 if (newp == NULL)
2513 return;
2514 if (col > 0)
2515 mch_memmove(newp, oldp, (size_t)col);
2516 mch_memmove(newp + col, s, (size_t)newlen);
2517 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2518 ml_replace(lnum, newp, FALSE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002519 inserted_bytes(lnum, col, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 curwin->w_cursor.col += newlen;
2521}
2522
2523/*
2524 * Delete one character under the cursor.
2525 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2526 * Caller must have prepared for undo.
2527 *
2528 * return FAIL for failure, OK otherwise
2529 */
2530 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002531del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (has_mbyte)
2534 {
2535 /* Make sure the cursor is at the start of a character. */
2536 mb_adjust_cursor();
2537 if (*ml_get_cursor() == NUL)
2538 return FAIL;
2539 return del_chars(1L, fixpos);
2540 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002541 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542}
2543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544/*
2545 * Like del_bytes(), but delete characters instead of bytes.
2546 */
2547 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002548del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549{
2550 long bytes = 0;
2551 long i;
2552 char_u *p;
2553 int l;
2554
2555 p = ml_get_cursor();
2556 for (i = 0; i < count && *p != NUL; ++i)
2557 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002558 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 bytes += l;
2560 p += l;
2561 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002562 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
2565/*
2566 * Delete "count" bytes under the cursor.
2567 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2568 * Caller must have prepared for undo.
2569 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002570 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 */
2572 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002573del_bytes(
2574 long count,
2575 int fixpos_arg,
2576 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
2578 char_u *oldp, *newp;
2579 colnr_T oldlen;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002580 colnr_T newlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 linenr_T lnum = curwin->w_cursor.lnum;
2582 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002583 int alloc_newp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002585 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
2587 oldp = ml_get(lnum);
2588 oldlen = (int)STRLEN(oldp);
2589
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002590 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (col >= oldlen)
2592 return FAIL;
2593
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002594 /* If "count" is zero there is nothing to do. */
2595 if (count == 0)
2596 return OK;
2597
2598 /* If "count" is negative the caller must be doing something wrong. */
2599 if (count < 1)
2600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002601 siemsg("E950: Invalid count for del_bytes(): %ld", count);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002602 return FAIL;
2603 }
2604
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 /* If 'delcombine' is set and deleting (less than) one character, only
2606 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002607 if (p_deco && use_delcombine && enc_utf8
2608 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002610 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 int n;
2612
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002613 (void)utfc_ptr2char(oldp + col, cc);
2614 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 /* Find the last composing char, there can be several. */
2617 n = col;
2618 do
2619 {
2620 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002621 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 n += count;
2623 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2624 fixpos = 0;
2625 }
2626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
2628 /*
2629 * When count is too big, reduce it.
2630 */
2631 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2632 if (movelen <= 1)
2633 {
2634 /*
2635 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002636 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2637 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002639 if (col > 0 && fixpos && restart_edit == 0
2640#ifdef FEAT_VIRTUALEDIT
2641 && (ve_flags & VE_ONEMORE) == 0
2642#endif
2643 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 {
2645 --curwin->w_cursor.col;
2646#ifdef FEAT_VIRTUALEDIT
2647 curwin->w_cursor.coladd = 0;
2648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (has_mbyte)
2650 curwin->w_cursor.col -=
2651 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653 count = oldlen - col;
2654 movelen = 1;
2655 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002656 newlen = oldlen - count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658 /*
2659 * If the old line has been allocated the deletion can be done in the
2660 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002661 * Can't do this when using Netbeans, because we would need to invoke
2662 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002663 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002666 if (netbeans_active())
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002667 alloc_newp = TRUE;
Bram Moolenaare21877a2008-02-13 09:58:14 +00002668 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002670 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
2671 if (!alloc_newp)
2672 newp = oldp; // use same allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 else
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002674 { // need to allocate a new line
2675 newp = alloc((unsigned)(newlen + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (newp == NULL)
2677 return FAIL;
2678 mch_memmove(newp, oldp, (size_t)col);
2679 }
2680 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002681 if (alloc_newp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 ml_replace(lnum, newp, FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002683#ifdef FEAT_TEXT_PROP
2684 else
2685 {
2686 // Also move any following text properties.
2687 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
2688 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
2689 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
2690 curbuf->b_ml.ml_line_len -= count;
2691 }
2692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002694 // mark the buffer as changed and prepare for displaying
Bram Moolenaar33c8ca92019-01-02 18:00:27 +01002695 inserted_bytes(lnum, curwin->w_cursor.col, -count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697 return OK;
2698}
2699
2700/*
2701 * Delete from cursor to end of line.
2702 * Caller must have prepared for undo.
2703 *
2704 * return FAIL for failure, OK otherwise
2705 */
2706 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002707truncate_line(
2708 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
2710 char_u *newp;
2711 linenr_T lnum = curwin->w_cursor.lnum;
2712 colnr_T col = curwin->w_cursor.col;
2713
2714 if (col == 0)
2715 newp = vim_strsave((char_u *)"");
2716 else
2717 newp = vim_strnsave(ml_get(lnum), col);
2718
2719 if (newp == NULL)
2720 return FAIL;
2721
2722 ml_replace(lnum, newp, FALSE);
2723
2724 /* mark the buffer as changed and prepare for displaying */
2725 changed_bytes(lnum, curwin->w_cursor.col);
2726
2727 /*
2728 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2729 */
2730 if (fixpos && curwin->w_cursor.col > 0)
2731 --curwin->w_cursor.col;
2732
2733 return OK;
2734}
2735
2736/*
2737 * Delete "nlines" lines at the cursor.
2738 * Saves the lines for undo first if "undo" is TRUE.
2739 */
2740 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002741del_lines(
2742 long nlines, /* number of lines to delete */
2743 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744{
2745 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002746 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
2748 if (nlines <= 0)
2749 return;
2750
2751 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002752 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 return;
2754
2755 for (n = 0; n < nlines; )
2756 {
2757 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2758 break;
2759
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002760 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 ++n;
2762
2763 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002764 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 break;
2766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002768 /* Correct the cursor position before calling deleted_lines_mark(), it may
2769 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 curwin->w_cursor.col = 0;
2771 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002772
2773 /* adjust marks, mark the buffer as changed and prepare for displaying */
2774 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775}
2776
2777 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002778gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002780 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002782 /* When searching columns is sometimes put at the end of a line. */
2783 if (pos->col == MAXCOL)
2784 return NUL;
2785 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (has_mbyte)
2787 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 return (int)*ptr;
2789}
2790
2791 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002792gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 if (has_mbyte)
2795 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 return (int)*ml_get_cursor();
2797}
2798
2799/*
2800 * Write a character at the current cursor position.
2801 * It is directly written into the block.
2802 */
2803 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002804pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805{
2806 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2807 + curwin->w_cursor.col) = c;
2808}
2809
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810/*
2811 * When extra == 0: Return TRUE if the cursor is before or on the first
2812 * non-blank in the line.
2813 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2814 * the line.
2815 */
2816 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002817inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
2819 char_u *ptr;
2820 colnr_T col;
2821
Bram Moolenaar1c465442017-03-12 20:10:05 +01002822 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 ++ptr;
2824 if (col >= curwin->w_cursor.col + extra)
2825 return TRUE;
2826 else
2827 return FALSE;
2828}
2829
2830/*
2831 * Skip to next part of an option argument: Skip space and comma.
2832 */
2833 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002834skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
2836 if (*p == ',')
2837 ++p;
2838 while (*p == ' ')
2839 ++p;
2840 return p;
2841}
2842
2843/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002844 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 *
2846 * Most often called through changed_bytes() and changed_lines(), which also
2847 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002848 *
2849 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 */
2851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002852changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853{
2854#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002855 if (p_imst == IM_ON_THE_SPOT)
2856 {
2857 /* The text of the preediting area is inserted, but this doesn't
2858 * mean a change of the buffer yet. That is delayed until the
2859 * text is committed. (this means preedit becomes empty) */
2860 if (im_is_preediting() && !xim_changed_while_preediting)
2861 return;
2862 xim_changed_while_preediting = FALSE;
2863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864#endif
2865
2866 if (!curbuf->b_changed)
2867 {
2868 int save_msg_scroll = msg_scroll;
2869
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002870 /* Give a warning about changing a read-only file. This may also
2871 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002873
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 /* Create a swap file if that is wanted.
2875 * Don't do this for "nofile" and "nowrite" buffer types. */
2876 if (curbuf->b_may_swap
2877#ifdef FEAT_QUICKFIX
2878 && !bt_dontwrite(curbuf)
2879#endif
2880 )
2881 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002882 int save_need_wait_return = need_wait_return;
2883
2884 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 ml_open_file(curbuf);
2886
2887 /* The ml_open_file() can cause an ATTENTION message.
2888 * Wait two seconds, to make sure the user reads this unexpected
2889 * message. Since we could be anywhere, call wait_return() now,
2890 * and don't let the emsg() set msg_scroll. */
2891 if (need_wait_return && emsg_silent == 0)
2892 {
2893 out_flush();
2894 ui_delay(2000L, TRUE);
2895 wait_return(TRUE);
2896 msg_scroll = save_msg_scroll;
2897 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002898 else
2899 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002901 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002903 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904}
2905
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002906/*
2907 * Internal part of changed(), no user interaction.
2908 */
2909 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002910changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002911{
2912 curbuf->b_changed = TRUE;
2913 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002914 check_status(curbuf);
2915 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002916#ifdef FEAT_TITLE
2917 need_maketitle = TRUE; /* set window title later */
2918#endif
2919}
2920
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002921static void changedOneline(buf_T *buf, linenr_T lnum);
2922static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2923static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
2925/*
2926 * Changed bytes within a single line for the current buffer.
2927 * - marks the windows on this buffer to be redisplayed
2928 * - marks the buffer changed by calling changed()
2929 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002930 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 */
2932 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002933changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002935 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002937
2938#ifdef FEAT_DIFF
2939 /* Diff highlighting in other diff windows may need to be updated too. */
2940 if (curwin->w_p_diff)
2941 {
2942 win_T *wp;
2943 linenr_T wlnum;
2944
Bram Moolenaar29323592016-07-24 22:04:11 +02002945 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002946 if (wp->w_p_diff && wp != curwin)
2947 {
2948 redraw_win_later(wp, VALID);
2949 wlnum = diff_lnum_win(lnum, wp);
2950 if (wlnum > 0)
2951 changedOneline(wp->w_buffer, wlnum);
2952 }
2953 }
2954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955}
2956
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002957/*
2958 * Like changed_bytes() but also adjust text properties for "added" bytes.
2959 * When "added" is negative text was deleted.
2960 */
2961 void
Bram Moolenaar402385a2019-01-11 14:10:03 +01002962inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002963{
2964 changed_bytes(lnum, col);
2965
2966#ifdef FEAT_TEXT_PROP
2967 if (curbuf->b_has_textprop && added != 0)
2968 adjust_prop_columns(lnum, col, added);
2969#endif
2970}
2971
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002973changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002975 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 {
2977 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002978 if (lnum < buf->b_mod_top)
2979 buf->b_mod_top = lnum;
2980 else if (lnum >= buf->b_mod_bot)
2981 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
2983 else
2984 {
2985 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002986 buf->b_mod_set = TRUE;
2987 buf->b_mod_top = lnum;
2988 buf->b_mod_bot = lnum + 1;
2989 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 }
2991}
2992
2993/*
2994 * Appended "count" lines below line "lnum" in the current buffer.
2995 * Must be called AFTER the change and after mark_adjust().
2996 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2997 */
2998 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002999appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 changed_lines(lnum + 1, 0, lnum + 1, count);
3002}
3003
3004/*
3005 * Like appended_lines(), but adjust marks first.
3006 */
3007 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003008appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
Bram Moolenaar82faa252016-06-04 20:14:07 +02003010 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003011 * be marks there. But it's still needed in diff mode. */
3012 if (lnum + count < curbuf->b_ml.ml_line_count
3013#ifdef FEAT_DIFF
3014 || curwin->w_p_diff
3015#endif
3016 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003017 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 changed_lines(lnum + 1, 0, lnum + 1, count);
3019}
3020
3021/*
3022 * Deleted "count" lines at line "lnum" in the current buffer.
3023 * Must be called AFTER the change and after mark_adjust().
3024 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3025 */
3026 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003027deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 changed_lines(lnum, 0, lnum + count, -count);
3030}
3031
3032/*
3033 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003034 * Make sure the cursor is on a valid line before calling, a GUI callback may
3035 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 */
3037 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003038deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
3040 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3041 changed_lines(lnum, 0, lnum + count, -count);
3042}
3043
3044/*
3045 * Changed lines for the current buffer.
3046 * Must be called AFTER the change and after mark_adjust().
3047 * - mark the buffer changed by calling changed()
3048 * - mark the windows on this buffer to be redisplayed
3049 * - invalidate cached values
3050 * "lnum" is the first line that needs displaying, "lnume" the first line
3051 * below the changed lines (BEFORE the change).
3052 * When only inserting lines, "lnum" and "lnume" are equal.
3053 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003054 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 */
3056 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003057changed_lines(
3058 linenr_T lnum, /* first line with change */
3059 colnr_T col, /* column in first line with change */
3060 linenr_T lnume, /* line below last changed line */
3061 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003063 changed_lines_buf(curbuf, lnum, lnume, xtra);
3064
3065#ifdef FEAT_DIFF
Bram Moolenaare3521d92018-09-16 14:10:31 +02003066 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
Bram Moolenaardba8a912005-04-24 22:08:39 +00003067 {
3068 /* When the number of lines doesn't change then mark_adjust() isn't
3069 * called and other diff buffers still need to be marked for
3070 * displaying. */
3071 win_T *wp;
3072 linenr_T wlnum;
3073
Bram Moolenaar29323592016-07-24 22:04:11 +02003074 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003075 if (wp->w_p_diff && wp != curwin)
3076 {
3077 redraw_win_later(wp, VALID);
3078 wlnum = diff_lnum_win(lnum, wp);
3079 if (wlnum > 0)
3080 changed_lines_buf(wp->w_buffer, wlnum,
3081 lnume - lnum + wlnum, 0L);
3082 }
3083 }
3084#endif
3085
3086 changed_common(lnum, col, lnume, xtra);
3087}
3088
3089 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003090changed_lines_buf(
3091 buf_T *buf,
3092 linenr_T lnum, /* first line with change */
3093 linenr_T lnume, /* line below last changed line */
3094 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003095{
3096 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
3098 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003099 if (lnum < buf->b_mod_top)
3100 buf->b_mod_top = lnum;
3101 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
3103 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003104 buf->b_mod_bot += xtra;
3105 if (buf->b_mod_bot < lnum)
3106 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003108 if (lnume + xtra > buf->b_mod_bot)
3109 buf->b_mod_bot = lnume + xtra;
3110 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
3112 else
3113 {
3114 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003115 buf->b_mod_set = TRUE;
3116 buf->b_mod_top = lnum;
3117 buf->b_mod_bot = lnume + xtra;
3118 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120}
3121
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003122/*
3123 * Common code for when a change is was made.
3124 * See changed_lines() for the arguments.
3125 * Careful: may trigger autocommands that reload the buffer.
3126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003128changed_common(
3129 linenr_T lnum,
3130 colnr_T col,
3131 linenr_T lnume,
3132 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003135 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 int i;
3137#ifdef FEAT_JUMPLIST
3138 int cols;
3139 pos_T *p;
3140 int add;
3141#endif
3142
3143 /* mark the buffer as modified */
3144 changed();
3145
Bram Moolenaare3521d92018-09-16 14:10:31 +02003146#ifdef FEAT_DIFF
3147 if (curwin->w_p_diff && diff_internal())
3148 curtab->tp_diff_update = TRUE;
3149#endif
3150
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 /* set the '. mark */
3152 if (!cmdmod.keepjumps)
3153 {
3154 curbuf->b_last_change.lnum = lnum;
3155 curbuf->b_last_change.col = col;
3156
3157#ifdef FEAT_JUMPLIST
3158 /* Create a new entry if a new undo-able change was started or we
3159 * don't have an entry yet. */
3160 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3161 {
3162 if (curbuf->b_changelistlen == 0)
3163 add = TRUE;
3164 else
3165 {
3166 /* Don't create a new entry when the line number is the same
3167 * as the last one and the column is not too far away. Avoids
3168 * creating many entries for typing "xxxxx". */
3169 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3170 if (p->lnum != lnum)
3171 add = TRUE;
3172 else
3173 {
3174 cols = comp_textwidth(FALSE);
3175 if (cols == 0)
3176 cols = 79;
3177 add = (p->col + cols < col || col + cols < p->col);
3178 }
3179 }
3180 if (add)
3181 {
3182 /* This is the first of a new sequence of undo-able changes
3183 * and it's at some distance of the last change. Use a new
3184 * position in the changelist. */
3185 curbuf->b_new_change = FALSE;
3186
3187 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3188 {
3189 /* changelist is full: remove oldest entry */
3190 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3191 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3192 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003193 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
3195 /* Correct position in changelist for other windows on
3196 * this buffer. */
3197 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3198 --wp->w_changelistidx;
3199 }
3200 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003201 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
3203 /* For other windows, if the position in the changelist is
3204 * at the end it stays at the end. */
3205 if (wp->w_buffer == curbuf
3206 && wp->w_changelistidx == curbuf->b_changelistlen)
3207 ++wp->w_changelistidx;
3208 }
3209 ++curbuf->b_changelistlen;
3210 }
3211 }
3212 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3213 curbuf->b_last_change;
3214 /* The current window is always after the last change, so that "g,"
3215 * takes you back to it. */
3216 curwin->w_changelistidx = curbuf->b_changelistlen;
3217#endif
3218 }
3219
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003220 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
3222 if (wp->w_buffer == curbuf)
3223 {
3224 /* Mark this window to be redrawn later. */
3225 if (wp->w_redr_type < VALID)
3226 wp->w_redr_type = VALID;
3227
3228 /* Check if a change in the buffer has invalidated the cached
3229 * values for the cursor. */
3230#ifdef FEAT_FOLDING
3231 /*
3232 * Update the folds for this window. Can't postpone this, because
3233 * a following operator might work on the whole fold: ">>dd".
3234 */
3235 foldUpdate(wp, lnum, lnume + xtra - 1);
3236
3237 /* The change may cause lines above or below the change to become
3238 * included in a fold. Set lnum/lnume to the first/last line that
3239 * might be displayed differently.
3240 * Set w_cline_folded here as an efficient way to update it when
3241 * inserting lines just above a closed fold. */
3242 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3243 if (wp->w_cursor.lnum == lnum)
3244 wp->w_cline_folded = i;
3245 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3246 if (wp->w_cursor.lnum == lnume)
3247 wp->w_cline_folded = i;
3248
3249 /* If the changed line is in a range of previously folded lines,
3250 * compare with the first line in that range. */
3251 if (wp->w_cursor.lnum <= lnum)
3252 {
3253 i = find_wl_entry(wp, lnum);
3254 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3255 changed_line_abv_curs_win(wp);
3256 }
3257#endif
3258
3259 if (wp->w_cursor.lnum > lnum)
3260 changed_line_abv_curs_win(wp);
3261 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3262 changed_cline_bef_curs_win(wp);
3263 if (wp->w_botline >= lnum)
3264 {
3265 /* Assume that botline doesn't change (inserted lines make
3266 * other lines scroll down below botline). */
3267 approximate_botline_win(wp);
3268 }
3269
3270 /* Check if any w_lines[] entries have become invalid.
3271 * For entries below the change: Correct the lnums for
3272 * inserted/deleted lines. Makes it possible to stop displaying
3273 * after the change. */
3274 for (i = 0; i < wp->w_lines_valid; ++i)
3275 if (wp->w_lines[i].wl_valid)
3276 {
3277 if (wp->w_lines[i].wl_lnum >= lnum)
3278 {
3279 if (wp->w_lines[i].wl_lnum < lnume)
3280 {
3281 /* line included in change */
3282 wp->w_lines[i].wl_valid = FALSE;
3283 }
3284 else if (xtra != 0)
3285 {
3286 /* line below change */
3287 wp->w_lines[i].wl_lnum += xtra;
3288#ifdef FEAT_FOLDING
3289 wp->w_lines[i].wl_lastlnum += xtra;
3290#endif
3291 }
3292 }
3293#ifdef FEAT_FOLDING
3294 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3295 {
3296 /* change somewhere inside this range of folded lines,
3297 * may need to be redrawn */
3298 wp->w_lines[i].wl_valid = FALSE;
3299 }
3300#endif
3301 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003302
3303#ifdef FEAT_FOLDING
3304 /* Take care of side effects for setting w_topline when folds have
3305 * changed. Esp. when the buffer was changed in another window. */
3306 if (hasAnyFolding(wp))
3307 set_topline(wp, wp->w_topline);
3308#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003309 /* relative numbering may require updating more */
3310 if (wp->w_p_rnu)
3311 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313 }
3314
3315 /* Call update_screen() later, which checks out what needs to be redrawn,
3316 * since it notices b_mod_set and then uses b_mod_*. */
3317 if (must_redraw < VALID)
3318 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003319
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003320 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003321 if (lnum <= curwin->w_cursor.lnum
3322 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003323 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324}
3325
3326/*
3327 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3328 */
3329 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003330unchanged(
3331 buf_T *buf,
3332 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003334 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
3336 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003337 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (ff)
3339 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003341 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342#ifdef FEAT_TITLE
3343 need_maketitle = TRUE; /* set window title later */
3344#endif
3345 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003346 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347#ifdef FEAT_NETBEANS_INTG
3348 netbeans_unmodified(buf);
3349#endif
3350}
3351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352/*
3353 * check_status: called when the status bars for the buffer 'buf'
3354 * need to be updated
3355 */
3356 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003357check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
3359 win_T *wp;
3360
Bram Moolenaar29323592016-07-24 22:04:11 +02003361 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (wp->w_buffer == buf && wp->w_status_height)
3363 {
3364 wp->w_redr_status = TRUE;
3365 if (must_redraw < VALID)
3366 must_redraw = VALID;
3367 }
3368}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369
3370/*
3371 * If the file is readonly, give a warning message with the first change.
3372 * Don't do this for autocommands.
3373 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003374 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003376 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 */
3378 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003379change_warning(
3380 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 mode and 'showmode' is on */
3382{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003383 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3384
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 if (curbuf->b_did_warn == FALSE
3386 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 && curbuf->b_p_ro)
3389 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003390 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003392 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 if (!curbuf->b_p_ro)
3394 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 /*
3396 * Do what msg() does, but with a column offset if the warning should
3397 * be after the mode message.
3398 */
3399 msg_start();
3400 if (msg_row == Rows - 1)
3401 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003402 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003403 msg_puts_attr(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003404#ifdef FEAT_EVAL
3405 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 msg_clr_eos();
3408 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003409 if (msg_silent == 0 && !silent_mode
3410#ifdef FEAT_EVAL
3411 && time_for_testing != 1
3412#endif
3413 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 {
3415 out_flush();
3416 ui_delay(1000L, TRUE); /* give the user time to think about it */
3417 }
3418 curbuf->b_did_warn = TRUE;
3419 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3420 if (msg_row < Rows - 1)
3421 showmode();
3422 }
3423}
3424
3425/*
3426 * Ask for a reply from the user, a 'y' or a 'n'.
3427 * No other characters are accepted, the message is repeated until a valid
3428 * reply is entered or CTRL-C is hit.
3429 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3430 * from any buffers but directly from the user.
3431 *
3432 * return the 'y' or 'n'
3433 */
3434 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003435ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 int r = ' ';
3438 int save_State = State;
3439
3440 if (exiting) /* put terminal in raw mode for this question */
3441 settmode(TMODE_RAW);
3442 ++no_wait_return;
3443#ifdef USE_ON_FLY_SCROLL
3444 dont_scroll = TRUE; /* disallow scrolling here */
3445#endif
3446 State = CONFIRM; /* mouse behaves like with :confirm */
3447#ifdef FEAT_MOUSE
3448 setmouse(); /* disables mouse for xterm */
3449#endif
3450 ++no_mapping;
3451 ++allow_keys; /* no mapping here, but recognize keys */
3452
3453 while (r != 'y' && r != 'n')
3454 {
3455 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003456 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (direct)
3458 r = get_keystroke();
3459 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003460 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (r == Ctrl_C || r == ESC)
3462 r = 'n';
3463 msg_putchar(r); /* show what you typed */
3464 out_flush();
3465 }
3466 --no_wait_return;
3467 State = save_State;
3468#ifdef FEAT_MOUSE
3469 setmouse();
3470#endif
3471 --no_mapping;
3472 --allow_keys;
3473
3474 return r;
3475}
3476
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003477#if defined(FEAT_MOUSE) || defined(PROTO)
3478/*
3479 * Return TRUE if "c" is a mouse key.
3480 */
3481 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003482is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003483{
3484 return c == K_LEFTMOUSE
3485 || c == K_LEFTMOUSE_NM
3486 || c == K_LEFTDRAG
3487 || c == K_LEFTRELEASE
3488 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003489 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003490 || c == K_MIDDLEMOUSE
3491 || c == K_MIDDLEDRAG
3492 || c == K_MIDDLERELEASE
3493 || c == K_RIGHTMOUSE
3494 || c == K_RIGHTDRAG
3495 || c == K_RIGHTRELEASE
3496 || c == K_MOUSEDOWN
3497 || c == K_MOUSEUP
3498 || c == K_MOUSELEFT
3499 || c == K_MOUSERIGHT
3500 || c == K_X1MOUSE
3501 || c == K_X1DRAG
3502 || c == K_X1RELEASE
3503 || c == K_X2MOUSE
3504 || c == K_X2DRAG
3505 || c == K_X2RELEASE;
3506}
3507#endif
3508
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509/*
3510 * Get a key stroke directly from the user.
3511 * Ignores mouse clicks and scrollbar events, except a click for the left
3512 * button (used at the more prompt).
3513 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3514 * Disadvantage: typeahead is ignored.
3515 * Translates the interrupt character for unix to ESC.
3516 */
3517 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003518get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003520 char_u *buf = NULL;
3521 int buflen = 150;
3522 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 int len = 0;
3524 int n;
3525 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003526 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
3528 mapped_ctrl_c = FALSE; /* mappings are not used here */
3529 for (;;)
3530 {
3531 cursor_on();
3532 out_flush();
3533
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003534 /* Leave some room for check_termcode() to insert a key code into (max
3535 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3536 * bytes. */
3537 maxlen = (buflen - 6 - len) / 3;
3538 if (buf == NULL)
3539 buf = alloc(buflen);
3540 else if (maxlen < 10)
3541 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003542 char_u *t_buf = buf;
3543
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003544 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003545 * escape sequence. */
3546 buflen += 100;
3547 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003548 if (buf == NULL)
3549 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003550 maxlen = (buflen - 6 - len) / 3;
3551 }
3552 if (buf == NULL)
3553 {
3554 do_outofmem_msg((long_u)buflen);
3555 return ESC; /* panic! */
3556 }
3557
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003559 * terminal code to complete. */
3560 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (n > 0)
3562 {
3563 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003564 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003566 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003568 else if (len > 0)
3569 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
Bram Moolenaar4395a712006-09-05 18:57:57 +00003571 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003572 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003573 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003575
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003576 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003577 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003578 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003579 {
3580 /* Redrawing was postponed, do it now. */
3581 update_screen(0);
3582 setcursor(); /* put cursor back where it belongs */
3583 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003584 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003585 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003586 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003588 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 continue;
3590
3591 /* Handle modifier and/or special key code. */
3592 n = buf[0];
3593 if (n == K_SPECIAL)
3594 {
3595 n = TO_SPECIAL(buf[1], buf[2]);
3596 if (buf[1] == KS_MODIFIER
3597 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003598#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003599 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003600#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003601#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 || n == K_VER_SCROLLBAR
3603 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604#endif
3605 )
3606 {
3607 if (buf[1] == KS_MODIFIER)
3608 mod_mask = buf[2];
3609 len -= 3;
3610 if (len > 0)
3611 mch_memmove(buf, buf + 3, (size_t)len);
3612 continue;
3613 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003614 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 if (has_mbyte)
3617 {
3618 if (MB_BYTE2LEN(n) > len)
3619 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003620 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 n = (*mb_ptr2char)(buf);
3622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623#ifdef UNIX
3624 if (n == intr_char)
3625 n = ESC;
3626#endif
3627 break;
3628 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003629 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
3631 mapped_ctrl_c = save_mapped_ctrl_c;
3632 return n;
3633}
3634
3635/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003636 * Get a number from the user.
3637 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 */
3639 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003640get_number(
3641 int colon, /* allow colon to abort */
3642 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
3644 int n = 0;
3645 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003646 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003648 if (mouse_used != NULL)
3649 *mouse_used = FALSE;
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 /* When not printing messages, the user won't know what to type, return a
3652 * zero (as if CR was hit). */
3653 if (msg_silent != 0)
3654 return 0;
3655
3656#ifdef USE_ON_FLY_SCROLL
3657 dont_scroll = TRUE; /* disallow scrolling here */
3658#endif
3659 ++no_mapping;
3660 ++allow_keys; /* no mapping here, but recognize keys */
3661 for (;;)
3662 {
3663 windgoto(msg_row, msg_col);
3664 c = safe_vgetc();
3665 if (VIM_ISDIGIT(c))
3666 {
3667 n = n * 10 + c - '0';
3668 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003669 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3672 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003673 if (typed > 0)
3674 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003675 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003676 --typed;
3677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003680#ifdef FEAT_MOUSE
3681 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3682 {
3683 *mouse_used = TRUE;
3684 n = mouse_row + 1;
3685 break;
3686 }
3687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 else if (n == 0 && c == ':' && colon)
3689 {
3690 stuffcharReadbuff(':');
3691 if (!exmode_active)
3692 cmdline_row = msg_row;
3693 skip_redraw = TRUE; /* skip redraw once */
3694 do_redraw = FALSE;
3695 break;
3696 }
3697 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3698 break;
3699 }
3700 --no_mapping;
3701 --allow_keys;
3702 return n;
3703}
3704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003705/*
3706 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003707 * When "mouse_used" is not NULL allow using the mouse and in that case return
3708 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003709 */
3710 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003711prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003712{
3713 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003714 int save_cmdline_row;
3715 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003716
3717 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003718 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003719 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003720 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003721 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003722
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003723 // Set the state such that text can be selected/copied/pasted and we still
3724 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
3725 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003726 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003727 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003728 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003729 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02003730#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003731 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003732 setmouse();
3733#endif
3734
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003735 i = get_number(TRUE, mouse_used);
3736 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003737 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003738 /* don't call wait_return() now */
3739 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003740 cmdline_row = msg_row - 1;
3741 need_wait_return = FALSE;
3742 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003743 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003744 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003745 else
3746 cmdline_row = save_cmdline_row;
3747 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003748#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003749 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003750 setmouse();
3751#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753 return i;
3754}
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003757msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
3759 long pn;
3760
3761 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3763 return;
3764
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003765 /* We don't want to overwrite another important message, but do overwrite
3766 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3767 * then "put" reports the last action. */
3768 if (keep_msg != NULL && !keep_msg_more)
3769 return;
3770
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 if (n > 0)
3772 pn = n;
3773 else
3774 pn = -n;
3775
3776 if (pn > p_report)
3777 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003778 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003779 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003780 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003782 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003783 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003785 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
3786 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (msg(msg_buf))
3788 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003789 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003790 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 }
3793}
3794
3795/*
3796 * flush map and typeahead buffers and give a warning for an error
3797 */
3798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003799beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800{
3801 if (emsg_silent == 0)
3802 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003803 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003804 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
3806}
3807
3808/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003809 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 */
3811 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003812vim_beep(
3813 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003815#ifdef FEAT_EVAL
3816 called_vim_beep = TRUE;
3817#endif
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 if (emsg_silent == 0)
3820 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003821 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3822 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003823#ifdef ELAPSED_FUNC
3824 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01003825 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003826
3827 /* Only beep once per half a second, otherwise a sequence of beeps
3828 * would freeze Vim. */
3829 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3830 {
3831 did_init = TRUE;
3832 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003834 if (p_vb
3835#ifdef FEAT_GUI
3836 /* While the GUI is starting up the termcap is set for
3837 * the GUI but the output still goes to a terminal. */
3838 && !(gui.in_use && gui.starting)
3839#endif
3840 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003841 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003842 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003843#ifdef FEAT_VTP
3844 /* No restore color information, refresh the screen. */
3845 if (has_vtp_working() != 0
3846# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003847 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003848# endif
3849 )
3850 {
3851 redraw_later(CLEAR);
3852 update_screen(0);
3853 redrawcmd();
3854 }
3855#endif
3856 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003857 else
3858 out_char(BELL);
3859#ifdef ELAPSED_FUNC
3860 }
3861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003863
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003864 /* When 'debug' contains "beep" produce a message. If we are sourcing
3865 * a script or executing a function give the user a hint where the beep
3866 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003867 if (vim_strchr(p_debug, 'e') != NULL)
3868 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003869 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003870 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873}
3874
3875/*
3876 * To get the "real" home directory:
3877 * - get value of $HOME
3878 * For Unix:
3879 * - go to that directory
3880 * - do mch_dirname() to get the real name of that directory.
3881 * This also works with mounts and links.
3882 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01003883 * For Windows:
3884 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 */
3886static char_u *homedir = NULL;
3887
3888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003889init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890{
3891 char_u *var;
3892
Bram Moolenaar05159a02005-02-26 23:04:13 +00003893 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003894 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003895
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896#ifdef VMS
3897 var = mch_getenv((char_u *)"SYS$LOGIN");
3898#else
3899 var = mch_getenv((char_u *)"HOME");
3900#endif
3901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902#ifdef WIN3264
3903 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003904 * Typically, $HOME is not defined on Windows, unless the user has
3905 * specifically defined it for Vim's sake. However, on Windows NT
3906 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3907 * each user. Try constructing $HOME from these.
3908 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003909 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003910 {
3911 char_u *homedrive, *homepath;
3912
3913 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3914 homepath = mch_getenv((char_u *)"HOMEPATH");
3915 if (homepath == NULL || *homepath == NUL)
3916 homepath = (char_u *)"\\";
3917 if (homedrive != NULL
3918 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3919 {
3920 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3921 if (NameBuff[0] != NUL)
3922 var = NameBuff;
3923 }
3924 }
3925
3926 if (var == NULL)
3927 var = mch_getenv((char_u *)"USERPROFILE");
3928
3929 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 * Weird but true: $HOME may contain an indirect reference to another
3931 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3932 * when $HOME is being set.
3933 */
3934 if (var != NULL && *var == '%')
3935 {
3936 char_u *p;
3937 char_u *exp;
3938
3939 p = vim_strchr(var + 1, '%');
3940 if (p != NULL)
3941 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003942 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 exp = mch_getenv(NameBuff);
3944 if (exp != NULL && *exp != NUL
3945 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3946 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003947 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 }
3951 }
3952
Bram Moolenaar48340b62017-08-29 22:08:53 +02003953 if (var != NULL && *var == NUL) /* empty is same as not set */
3954 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
Bram Moolenaar05159a02005-02-26 23:04:13 +00003956 if (enc_utf8 && var != NULL)
3957 {
3958 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003959 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003960
3961 /* Convert from active codepage to UTF-8. Other conversions are
3962 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003963 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003964 if (pp != NULL)
3965 {
3966 homedir = pp;
3967 return;
3968 }
3969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 /*
3972 * Default home dir is C:/
3973 * Best assumption we can make in such a situation.
3974 */
3975 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003976 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003978
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (var != NULL)
3980 {
3981#ifdef UNIX
3982 /*
3983 * Change to the directory and get the actual path. This resolves
3984 * links. Don't do it when we can't return.
3985 */
3986 if (mch_dirname(NameBuff, MAXPATHL) == OK
3987 && mch_chdir((char *)NameBuff) == 0)
3988 {
3989 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3990 var = IObuff;
3991 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003992 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994#endif
3995 homedir = vim_strsave(var);
3996 }
3997}
3998
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003999#if defined(EXITFREE) || defined(PROTO)
4000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004001free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004002{
4003 vim_free(homedir);
4004}
Bram Moolenaar24305862012-08-15 14:05:05 +02004005
4006# ifdef FEAT_CMDL_COMPL
4007 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004008free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004009{
4010 ga_clear_strings(&ga_users);
4011}
4012# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004013#endif
4014
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004016 * Call expand_env() and store the result in an allocated string.
4017 * This is not very memory efficient, this expects the result to be freed
4018 * again soon.
4019 */
4020 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004021expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004022{
4023 return expand_env_save_opt(src, FALSE);
4024}
4025
4026/*
4027 * Idem, but when "one" is TRUE handle the string as one file name, only
4028 * expand "~" at the start.
4029 */
4030 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004031expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004032{
4033 char_u *p;
4034
4035 p = alloc(MAXPATHL);
4036 if (p != NULL)
4037 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4038 return p;
4039}
4040
4041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 * Expand environment variable with path name.
4043 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004044 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 * If anything fails no expansion is done and dst equals src.
4046 */
4047 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004048expand_env(
4049 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4050 char_u *dst, /* where to put the result */
4051 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004053 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054}
4055
4056 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004057expand_env_esc(
4058 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4059 char_u *dst, /* where to put the result */
4060 int dstlen, /* maximum length of the result */
4061 int esc, /* escape spaces in expanded variables */
4062 int one, /* "srcp" is one file name */
4063 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004065 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 char_u *tail;
4067 int c;
4068 char_u *var;
4069 int copy_char;
4070 int mustfree; /* var was allocated, need to free it later */
4071 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004072 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004074 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004075 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004076
4077 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 --dstlen; /* leave one char space for "\," */
4079 while (*src && dstlen > 0)
4080 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004081#ifdef FEAT_EVAL
4082 /* Skip over `=expr`. */
4083 if (src[0] == '`' && src[1] == '=')
4084 {
4085 size_t len;
4086
4087 var = src;
4088 src += 2;
4089 (void)skip_expr(&src);
4090 if (*src == '`')
4091 ++src;
4092 len = src - var;
4093 if (len > (size_t)dstlen)
4094 len = dstlen;
4095 vim_strncpy(dst, var, len);
4096 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004097 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004098 continue;
4099 }
4100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004102 if ((*src == '$'
4103#ifdef VMS
4104 && at_start
4105#endif
4106 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004107#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 || *src == '%'
4109#endif
4110 || (*src == '~' && at_start))
4111 {
4112 mustfree = FALSE;
4113
4114 /*
4115 * The variable name is copied into dst temporarily, because it may
4116 * be a string in read-only memory and a NUL needs to be appended.
4117 */
4118 if (*src != '~') /* environment var */
4119 {
4120 tail = src + 1;
4121 var = dst;
4122 c = dstlen - 1;
4123
4124#ifdef UNIX
4125 /* Unix has ${var-name} type environment vars */
4126 if (*tail == '{' && !vim_isIDc('{'))
4127 {
4128 tail++; /* ignore '{' */
4129 while (c-- > 0 && *tail && *tail != '}')
4130 *var++ = *tail++;
4131 }
4132 else
4133#endif
4134 {
4135 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004136#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 || (*src == '%' && *tail != '%')
4138#endif
4139 ))
4140 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 }
4144
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004145#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146# ifdef UNIX
4147 if (src[1] == '{' && *tail != '}')
4148# else
4149 if (*src == '%' && *tail != '%')
4150# endif
4151 var = NULL;
4152 else
4153 {
4154# ifdef UNIX
4155 if (src[1] == '{')
4156# else
4157 if (*src == '%')
4158#endif
4159 ++tail;
4160#endif
4161 *var = NUL;
4162 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004163#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
4165#endif
4166 }
4167 /* home directory */
4168 else if ( src[1] == NUL
4169 || vim_ispathsep(src[1])
4170 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4171 {
4172 var = homedir;
4173 tail = src + 1;
4174 }
4175 else /* user directory */
4176 {
4177#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4178 /*
4179 * Copy ~user to dst[], so we can put a NUL after it.
4180 */
4181 tail = src;
4182 var = dst;
4183 c = dstlen - 1;
4184 while ( c-- > 0
4185 && *tail
4186 && vim_isfilec(*tail)
4187 && !vim_ispathsep(*tail))
4188 *var++ = *tail++;
4189 *var = NUL;
4190# ifdef UNIX
4191 /*
4192 * If the system supports getpwnam(), use it.
4193 * Otherwise, or if getpwnam() fails, the shell is used to
4194 * expand ~user. This is slower and may fail if the shell
4195 * does not support ~user (old versions of /bin/sh).
4196 */
4197# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4198 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004199 /* Note: memory allocated by getpwnam() is never freed.
4200 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004201 struct passwd *pw = (*dst == NUL)
4202 ? NULL : getpwnam((char *)dst + 1);
4203
4204 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206 if (var == NULL)
4207# endif
4208 {
4209 expand_T xpc;
4210
4211 ExpandInit(&xpc);
4212 xpc.xp_context = EXPAND_FILES;
4213 var = ExpandOne(&xpc, dst, NULL,
4214 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 mustfree = TRUE;
4216 }
4217
4218# else /* !UNIX, thus VMS */
4219 /*
4220 * USER_HOME is a comma-separated list of
4221 * directories to search for the user account in.
4222 */
4223 {
4224 char_u test[MAXPATHL], paths[MAXPATHL];
4225 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004226 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 STRCPY(paths, USER_HOME);
4229 next_path = paths;
4230 while (*next_path)
4231 {
4232 for (path = next_path; *next_path && *next_path != ',';
4233 next_path++);
4234 if (*next_path)
4235 *next_path++ = NUL;
4236 STRCPY(test, path);
4237 STRCAT(test, "/");
4238 STRCAT(test, dst + 1);
4239 if (mch_stat(test, &st) == 0)
4240 {
4241 var = alloc(STRLEN(test) + 1);
4242 STRCPY(var, test);
4243 mustfree = TRUE;
4244 break;
4245 }
4246 }
4247 }
4248# endif /* UNIX */
4249#else
4250 /* cannot expand user's home directory, so don't try */
4251 var = NULL;
4252 tail = (char_u *)""; /* for gcc */
4253#endif /* UNIX || VMS */
4254 }
4255
4256#ifdef BACKSLASH_IN_FILENAME
4257 /* If 'shellslash' is set change backslashes to forward slashes.
4258 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4259 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4260 {
4261 char_u *p = vim_strsave(var);
4262
4263 if (p != NULL)
4264 {
4265 if (mustfree)
4266 vim_free(var);
4267 var = p;
4268 mustfree = TRUE;
4269 forward_slash(var);
4270 }
4271 }
4272#endif
4273
4274 /* If "var" contains white space, escape it with a backslash.
4275 * Required for ":e ~/tt" when $HOME includes a space. */
4276 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4277 {
4278 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4279
4280 if (p != NULL)
4281 {
4282 if (mustfree)
4283 vim_free(var);
4284 var = p;
4285 mustfree = TRUE;
4286 }
4287 }
4288
4289 if (var != NULL && *var != NUL
4290 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4291 {
4292 STRCPY(dst, var);
4293 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004294 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 /* if var[] ends in a path separator and tail[] starts
4296 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004297 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4299 && dst[-1] != ':'
4300#endif
4301 && vim_ispathsep(*tail))
4302 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004303 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 src = tail;
4305 copy_char = FALSE;
4306 }
4307 if (mustfree)
4308 vim_free(var);
4309 }
4310
4311 if (copy_char) /* copy at least one char */
4312 {
4313 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004314 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004315 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4316 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 */
4318 at_start = FALSE;
4319 if (src[0] == '\\' && src[1] != NUL)
4320 {
4321 *dst++ = *src++;
4322 --dstlen;
4323 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004324 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004326 if (dstlen > 0)
4327 {
4328 *dst++ = *src++;
4329 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004330
Bram Moolenaar1c864092017-08-06 18:15:45 +02004331 if (startstr != NULL && src - startstr_len >= srcp
4332 && STRNCMP(src - startstr_len, startstr,
4333 startstr_len) == 0)
4334 at_start = TRUE;
4335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004337
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 *dst = NUL;
4340}
4341
4342/*
4343 * Vim's version of getenv().
4344 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004345 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004346 * "mustfree" is set to TRUE when returned is allocated, it must be
4347 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 */
4349 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004350vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351{
4352 char_u *p;
4353 char_u *pend;
4354 int vimruntime;
4355
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004356#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 /* use "C:/" when $HOME is not set */
4358 if (STRCMP(name, "HOME") == 0)
4359 return homedir;
4360#endif
4361
4362 p = mch_getenv(name);
4363 if (p != NULL && *p == NUL) /* empty is the same as not set */
4364 p = NULL;
4365
4366 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004367 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004368#if defined(WIN3264)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004369 if (enc_utf8)
4370 {
4371 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004372 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004373
4374 /* Convert from active codepage to UTF-8. Other conversions are
4375 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004376 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004377 if (pp != NULL)
4378 {
4379 p = pp;
4380 *mustfree = TRUE;
4381 }
4382 }
4383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4388 if (!vimruntime && STRCMP(name, "VIM") != 0)
4389 return NULL;
4390
4391 /*
4392 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4393 * Don't do this when default_vimruntime_dir is non-empty.
4394 */
4395 if (vimruntime
4396#ifdef HAVE_PATHDEF
4397 && *default_vimruntime_dir == NUL
4398#endif
4399 )
4400 {
4401 p = mch_getenv((char_u *)"VIM");
4402 if (p != NULL && *p == NUL) /* empty is the same as not set */
4403 p = NULL;
4404 if (p != NULL)
4405 {
4406 p = vim_version_dir(p);
4407 if (p != NULL)
4408 *mustfree = TRUE;
4409 else
4410 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004411
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004412#if defined(WIN3264)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004413 if (enc_utf8)
4414 {
4415 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004416 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004417
4418 /* Convert from active codepage to UTF-8. Other conversions
4419 * are not done, because they would fail for non-ASCII
4420 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004421 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004422 if (pp != NULL)
4423 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004424 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004425 vim_free(p);
4426 p = pp;
4427 *mustfree = TRUE;
4428 }
4429 }
4430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432 }
4433
4434 /*
4435 * When expanding $VIM or $VIMRUNTIME fails, try using:
4436 * - the directory name from 'helpfile' (unless it contains '$')
4437 * - the executable name from argv[0]
4438 */
4439 if (p == NULL)
4440 {
4441 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4442 p = p_hf;
4443#ifdef USE_EXE_NAME
4444 /*
4445 * Use the name of the executable, obtained from argv[0].
4446 */
4447 else
4448 p = exe_name;
4449#endif
4450 if (p != NULL)
4451 {
4452 /* remove the file name */
4453 pend = gettail(p);
4454
4455 /* remove "doc/" from 'helpfile', if present */
4456 if (p == p_hf)
4457 pend = remove_tail(p, pend, (char_u *)"doc");
4458
4459#ifdef USE_EXE_NAME
4460# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004461 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 if (p == exe_name)
4463 {
4464 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004465 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004467 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4468 if (pend1 != pend)
4469 {
4470 pnew = alloc((unsigned)(pend1 - p) + 15);
4471 if (pnew != NULL)
4472 {
4473 STRNCPY(pnew, p, (pend1 - p));
4474 STRCPY(pnew + (pend1 - p), "Resources/vim");
4475 p = pnew;
4476 pend = p + STRLEN(p);
4477 }
4478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
4480# endif
4481 /* remove "src/" from exe_name, if present */
4482 if (p == exe_name)
4483 pend = remove_tail(p, pend, (char_u *)"src");
4484#endif
4485
4486 /* for $VIM, remove "runtime/" or "vim54/", if present */
4487 if (!vimruntime)
4488 {
4489 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4490 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4491 }
4492
4493 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004494 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004497#ifdef MACOS_X
4498 if (p == exe_name || p == p_hf)
4499#endif
4500 /* check that the result is a directory name */
4501 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004504 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 else
4506 {
4507#ifdef USE_EXE_NAME
4508 /* may add "/vim54" or "/runtime" if it exists */
4509 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4510 {
4511 vim_free(p);
4512 p = pend;
4513 }
4514#endif
4515 *mustfree = TRUE;
4516 }
4517 }
4518 }
4519
4520#ifdef HAVE_PATHDEF
4521 /* When there is a pathdef.c file we can use default_vim_dir and
4522 * default_vimruntime_dir */
4523 if (p == NULL)
4524 {
4525 /* Only use default_vimruntime_dir when it is not empty */
4526 if (vimruntime && *default_vimruntime_dir != NUL)
4527 {
4528 p = default_vimruntime_dir;
4529 *mustfree = FALSE;
4530 }
4531 else if (*default_vim_dir != NUL)
4532 {
4533 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4534 *mustfree = TRUE;
4535 else
4536 {
4537 p = default_vim_dir;
4538 *mustfree = FALSE;
4539 }
4540 }
4541 }
4542#endif
4543
4544 /*
4545 * Set the environment variable, so that the new value can be found fast
4546 * next time, and others can also use it (e.g. Perl).
4547 */
4548 if (p != NULL)
4549 {
4550 if (vimruntime)
4551 {
4552 vim_setenv((char_u *)"VIMRUNTIME", p);
4553 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555 else
4556 {
4557 vim_setenv((char_u *)"VIM", p);
4558 didset_vim = TRUE;
4559 }
4560 }
4561 return p;
4562}
4563
4564/*
4565 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4566 * Return NULL if not, return its name in allocated memory otherwise.
4567 */
4568 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004569vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570{
4571 char_u *p;
4572
4573 if (vimdir == NULL || *vimdir == NUL)
4574 return NULL;
4575 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4576 if (p != NULL && mch_isdir(p))
4577 return p;
4578 vim_free(p);
4579 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4580 if (p != NULL && mch_isdir(p))
4581 return p;
4582 vim_free(p);
4583 return NULL;
4584}
4585
4586/*
4587 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4588 * the length of "name/". Otherwise return "pend".
4589 */
4590 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004591remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592{
4593 int len = (int)STRLEN(name) + 1;
4594 char_u *newend = pend - len;
4595
4596 if (newend >= p
4597 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004598 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 return newend;
4600 return pend;
4601}
4602
Bram Moolenaar113e1072019-01-20 15:30:40 +01004603#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02004604 void
4605vim_unsetenv(char_u *var)
4606{
4607#ifdef HAVE_UNSETENV
4608 unsetenv((char *)var);
4609#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004610 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004611#endif
4612}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004613#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02004614
4615
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 * Our portable version of setenv.
4618 */
4619 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004620vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621{
4622#ifdef HAVE_SETENV
4623 mch_setenv((char *)name, (char *)val, 1);
4624#else
4625 char_u *envbuf;
4626
4627 /*
4628 * Putenv does not copy the string, it has to remain
4629 * valid. The allocated memory will never be freed.
4630 */
4631 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4632 if (envbuf != NULL)
4633 {
4634 sprintf((char *)envbuf, "%s=%s", name, val);
4635 putenv((char *)envbuf);
4636 }
4637#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004638#ifdef FEAT_GETTEXT
4639 /*
4640 * When setting $VIMRUNTIME adjust the directory to find message
4641 * translations to $VIMRUNTIME/lang.
4642 */
4643 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4644 {
4645 char_u *buf = concat_str(val, (char_u *)"/lang");
4646
4647 if (buf != NULL)
4648 {
4649 bindtextdomain(VIMPACKAGE, (char *)buf);
4650 vim_free(buf);
4651 }
4652 }
4653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654}
4655
4656#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4657/*
4658 * Function given to ExpandGeneric() to obtain an environment variable name.
4659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004661get_env_name(
4662 expand_T *xp UNUSED,
4663 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
Bram Moolenaard0573012017-10-28 21:11:06 +02004665# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004667 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 */
4669 return NULL;
4670# else
4671# ifndef __WIN32__
4672 /* Borland C++ 5.2 has this in a header file. */
4673 extern char **environ;
4674# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004675# define ENVNAMELEN 100
4676 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 char_u *str;
4678 int n;
4679
4680 str = (char_u *)environ[idx];
4681 if (str == NULL)
4682 return NULL;
4683
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004684 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
4686 if (str[n] == '=' || str[n] == NUL)
4687 break;
4688 name[n] = str[n];
4689 }
4690 name[n] = NUL;
4691 return name;
4692# endif
4693}
Bram Moolenaar24305862012-08-15 14:05:05 +02004694
4695/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004696 * Add a user name to the list of users in ga_users.
4697 * Do nothing if user name is NULL or empty.
4698 */
4699 static void
4700add_user(char_u *user, int need_copy)
4701{
4702 char_u *user_copy = (user != NULL && need_copy)
4703 ? vim_strsave(user) : user;
4704
4705 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4706 {
4707 if (need_copy)
4708 vim_free(user);
4709 return;
4710 }
4711 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4712}
4713
4714/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004715 * Find all user names for user completion.
4716 * Done only once and then cached.
4717 */
4718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004719init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004720{
Bram Moolenaar24305862012-08-15 14:05:05 +02004721 static int lazy_init_done = FALSE;
4722
4723 if (lazy_init_done)
4724 return;
4725
4726 lazy_init_done = TRUE;
4727 ga_init2(&ga_users, sizeof(char_u *), 20);
4728
4729# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4730 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004731 struct passwd* pw;
4732
4733 setpwent();
4734 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004735 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004736 endpwent();
4737 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004738# elif defined(WIN3264)
4739 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004740 DWORD nusers = 0, ntotal = 0, i;
4741 PUSER_INFO_0 uinfo;
4742
4743 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4744 &nusers, &ntotal, NULL) == NERR_Success)
4745 {
4746 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004747 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004748
4749 NetApiBufferFree(uinfo);
4750 }
4751 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004752# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004753# if defined(HAVE_GETPWNAM)
4754 {
4755 char_u *user_env = mch_getenv((char_u *)"USER");
4756
4757 // The $USER environment variable may be a valid remote user name (NIS,
4758 // LDAP) not already listed by getpwent(), as getpwent() only lists
4759 // local user names. If $USER is not already listed, check whether it
4760 // is a valid remote user name using getpwnam() and if it is, add it to
4761 // the list of user names.
4762
4763 if (user_env != NULL && *user_env != NUL)
4764 {
4765 int i;
4766
4767 for (i = 0; i < ga_users.ga_len; i++)
4768 {
4769 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4770
4771 if (STRCMP(local_user, user_env) == 0)
4772 break;
4773 }
4774
4775 if (i == ga_users.ga_len)
4776 {
4777 struct passwd *pw = getpwnam((char *)user_env);
4778
4779 if (pw != NULL)
4780 add_user((char_u *)pw->pw_name, TRUE);
4781 }
4782 }
4783 }
4784# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004785}
4786
4787/*
4788 * Function given to ExpandGeneric() to obtain an user names.
4789 */
4790 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004791get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004792{
4793 init_users();
4794 if (idx < ga_users.ga_len)
4795 return ((char_u **)ga_users.ga_data)[idx];
4796 return NULL;
4797}
4798
4799/*
4800 * Check whether name matches a user name. Return:
4801 * 0 if name does not match any user name.
4802 * 1 if name partially matches the beginning of a user name.
4803 * 2 is name fully matches a user name.
4804 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004805 int
4806match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004807{
4808 int i;
4809 int n = (int)STRLEN(name);
4810 int result = 0;
4811
4812 init_users();
4813 for (i = 0; i < ga_users.ga_len; i++)
4814 {
4815 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4816 return 2; /* full match */
4817 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4818 result = 1; /* partial match */
4819 }
4820 return result;
4821}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822#endif
4823
4824/*
4825 * Replace home directory by "~" in each space or comma separated file name in
4826 * 'src'.
4827 * If anything fails (except when out of space) dst equals src.
4828 */
4829 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004830home_replace(
4831 buf_T *buf, /* when not NULL, check for help files */
4832 char_u *src, /* input file name */
4833 char_u *dst, /* where to put the result */
4834 int dstlen, /* maximum length of the result */
4835 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 spaces and commas in the file name. */
4837{
4838 size_t dirlen = 0, envlen = 0;
4839 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004840 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 char_u *p;
4842
4843 if (src == NULL)
4844 {
4845 *dst = NUL;
4846 return;
4847 }
4848
4849 /*
4850 * If the file is a help file, remove the path completely.
4851 */
4852 if (buf != NULL && buf->b_help)
4853 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004854 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 return;
4856 }
4857
4858 /*
4859 * We check both the value of the $HOME environment variable and the
4860 * "real" home directory.
4861 */
4862 if (homedir != NULL)
4863 dirlen = STRLEN(homedir);
4864
4865#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004866 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004868 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4869#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004870#ifdef WIN3264
4871 if (homedir_env == NULL)
4872 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4873#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004874 /* Empty is the same as not set. */
4875 if (homedir_env != NULL && *homedir_env == NUL)
4876 homedir_env = NULL;
4877
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004878#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaaref0a1d52018-12-30 11:38:57 +01004879 if (homedir_env != NULL && *homedir_env == '~')
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004880 {
4881 int usedlen = 0;
4882 int flen;
4883 char_u *fbuf = NULL;
4884
4885 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004886 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004887 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004888 flen = (int)STRLEN(homedir_env);
4889 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4890 /* Remove the trailing / that is added to a directory. */
4891 homedir_env[flen - 1] = NUL;
4892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893#endif
4894
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if (homedir_env != NULL)
4896 envlen = STRLEN(homedir_env);
4897
4898 if (!one)
4899 src = skipwhite(src);
4900 while (*src && dstlen > 0)
4901 {
4902 /*
4903 * Here we are at the beginning of a file name.
4904 * First, check to see if the beginning of the file name matches
4905 * $HOME or the "real" home directory. Check that there is a '/'
4906 * after the match (so that if e.g. the file is "/home/pieter/bla",
4907 * and the home directory is "/home/piet", the file does not end up
4908 * as "~er/bla" (which would seem to indicate the file "bla" in user
4909 * er's home directory)).
4910 */
4911 p = homedir;
4912 len = dirlen;
4913 for (;;)
4914 {
4915 if ( len
4916 && fnamencmp(src, p, len) == 0
4917 && (vim_ispathsep(src[len])
4918 || (!one && (src[len] == ',' || src[len] == ' '))
4919 || src[len] == NUL))
4920 {
4921 src += len;
4922 if (--dstlen > 0)
4923 *dst++ = '~';
4924
4925 /*
4926 * If it's just the home directory, add "/".
4927 */
4928 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4929 *dst++ = '/';
4930 break;
4931 }
4932 if (p == homedir_env)
4933 break;
4934 p = homedir_env;
4935 len = envlen;
4936 }
4937
4938 /* if (!one) skip to separator: space or comma */
4939 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4940 *dst++ = *src++;
4941 /* skip separator */
4942 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4943 *dst++ = *src++;
4944 }
4945 /* if (dstlen == 0) out of space, what to do??? */
4946
4947 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004948
4949 if (homedir_env != homedir_env_orig)
4950 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951}
4952
4953/*
4954 * Like home_replace, store the replaced string in allocated memory.
4955 * When something fails, NULL is returned.
4956 */
4957 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004958home_replace_save(
4959 buf_T *buf, /* when not NULL, check for help files */
4960 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
4962 char_u *dst;
4963 unsigned len;
4964
4965 len = 3; /* space for "~/" and trailing NUL */
4966 if (src != NULL) /* just in case */
4967 len += (unsigned)STRLEN(src);
4968 dst = alloc(len);
4969 if (dst != NULL)
4970 home_replace(buf, src, dst, len, TRUE);
4971 return dst;
4972}
4973
4974/*
4975 * Compare two file names and return:
4976 * FPC_SAME if they both exist and are the same file.
4977 * FPC_SAMEX if they both don't exist and have the same file name.
4978 * FPC_DIFF if they both exist and are different files.
4979 * FPC_NOTX if they both don't exist.
4980 * FPC_DIFFX if one of them doesn't exist.
4981 * For the first name environment variables are expanded
4982 */
4983 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004984fullpathcmp(
4985 char_u *s1,
4986 char_u *s2,
4987 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988{
4989#ifdef UNIX
4990 char_u exp1[MAXPATHL];
4991 char_u full1[MAXPATHL];
4992 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004993 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 int r1, r2;
4995
4996 expand_env(s1, exp1, MAXPATHL);
4997 r1 = mch_stat((char *)exp1, &st1);
4998 r2 = mch_stat((char *)s2, &st2);
4999 if (r1 != 0 && r2 != 0)
5000 {
5001 /* if mch_stat() doesn't work, may compare the names */
5002 if (checkname)
5003 {
5004 if (fnamecmp(exp1, s2) == 0)
5005 return FPC_SAMEX;
5006 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5007 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5008 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5009 return FPC_SAMEX;
5010 }
5011 return FPC_NOTX;
5012 }
5013 if (r1 != 0 || r2 != 0)
5014 return FPC_DIFFX;
5015 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5016 return FPC_SAME;
5017 return FPC_DIFF;
5018#else
5019 char_u *exp1; /* expanded s1 */
5020 char_u *full1; /* full path of s1 */
5021 char_u *full2; /* full path of s2 */
5022 int retval = FPC_DIFF;
5023 int r1, r2;
5024
5025 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5026 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5027 {
5028 full1 = exp1 + MAXPATHL;
5029 full2 = full1 + MAXPATHL;
5030
5031 expand_env(s1, exp1, MAXPATHL);
5032 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5033 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5034
5035 /* If vim_FullName() fails, the file probably doesn't exist. */
5036 if (r1 != OK && r2 != OK)
5037 {
5038 if (checkname && fnamecmp(exp1, s2) == 0)
5039 retval = FPC_SAMEX;
5040 else
5041 retval = FPC_NOTX;
5042 }
5043 else if (r1 != OK || r2 != OK)
5044 retval = FPC_DIFFX;
5045 else if (fnamecmp(full1, full2))
5046 retval = FPC_DIFF;
5047 else
5048 retval = FPC_SAME;
5049 vim_free(exp1);
5050 }
5051 return retval;
5052#endif
5053}
5054
5055/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005056 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005057 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005058 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 */
5060 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005061gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062{
5063 char_u *p1, *p2;
5064
5065 if (fname == NULL)
5066 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005067 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005069 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005071 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073 return p1;
5074}
5075
Bram Moolenaar31710262010-08-13 13:36:15 +02005076#if defined(FEAT_SEARCHPATH)
Bram Moolenaar31710262010-08-13 13:36:15 +02005077/*
5078 * Return the end of the directory name, on the first path
5079 * separator:
5080 * "/path/file", "/path/dir/", "/path//dir", "/file"
5081 * ^ ^ ^ ^
5082 */
5083 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005084gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005085{
5086 char_u *dir_end = fname;
5087 char_u *next_dir_end = fname;
5088 int look_for_sep = TRUE;
5089 char_u *p;
5090
5091 for (p = fname; *p != NUL; )
5092 {
5093 if (vim_ispathsep(*p))
5094 {
5095 if (look_for_sep)
5096 {
5097 next_dir_end = p;
5098 look_for_sep = FALSE;
5099 }
5100 }
5101 else
5102 {
5103 if (!look_for_sep)
5104 dir_end = next_dir_end;
5105 look_for_sep = TRUE;
5106 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005107 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005108 }
5109 return dir_end;
5110}
5111#endif
5112
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005114 * Get pointer to tail of "fname", including path separators. Putting a NUL
5115 * here leaves the directory name. Takes care of "c:/" and "//".
5116 * Always returns a valid pointer.
5117 */
5118 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005119gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005120{
5121 char_u *p;
5122 char_u *t;
5123
5124 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5125 t = gettail(fname);
5126 while (t > p && after_pathsep(fname, t))
5127 --t;
5128#ifdef VMS
5129 /* path separator is part of the path */
5130 ++t;
5131#endif
5132 return t;
5133}
5134
5135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 * get the next path component (just after the next path separator).
5137 */
5138 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005139getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140{
5141 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005142 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 if (*fname)
5144 ++fname;
5145 return fname;
5146}
5147
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148/*
5149 * Get a pointer to one character past the head of a path name.
5150 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5151 * If there is no head, path is returned.
5152 */
5153 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005154get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155{
5156 char_u *retval;
5157
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005158#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 /* may skip "c:" */
5160 if (isalpha(path[0]) && path[1] == ':')
5161 retval = path + 2;
5162 else
5163 retval = path;
5164#else
5165# if defined(AMIGA)
5166 /* may skip "label:" */
5167 retval = vim_strchr(path, ':');
5168 if (retval == NULL)
5169 retval = path;
5170# else /* Unix */
5171 retval = path;
5172# endif
5173#endif
5174
5175 while (vim_ispathsep(*retval))
5176 ++retval;
5177
5178 return retval;
5179}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
5181/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005182 * Return TRUE if 'c' is a path separator.
5183 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 */
5185 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005186vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005188#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005190#else
5191# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005193# else
5194# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5196 return (c == ':' || c == '[' || c == ']' || c == '/'
5197 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005198# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005200# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203}
5204
Bram Moolenaar69c35002013-11-04 02:54:12 +01005205/*
5206 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5207 */
5208 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005209vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005210{
5211 return vim_ispathsep(c)
5212#ifdef BACKSLASH_IN_FILENAME
5213 && c != ':'
5214#endif
5215 ;
5216}
5217
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5219/*
5220 * return TRUE if 'c' is a path list separator.
5221 */
5222 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005223vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
5225#ifdef UNIX
5226 return (c == ':');
5227#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005228 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229#endif
5230}
5231#endif
5232
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005233/*
5234 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5235 * It's done in-place.
5236 */
5237 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005238shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005239{
5240 char_u *tail, *s, *d;
5241 int skip = FALSE;
5242
5243 tail = gettail(str);
5244 d = str;
5245 for (s = str; ; ++s)
5246 {
5247 if (s >= tail) /* copy the whole tail */
5248 {
5249 *d++ = *s;
5250 if (*s == NUL)
5251 break;
5252 }
5253 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5254 {
5255 *d++ = *s;
5256 skip = FALSE;
5257 }
5258 else if (!skip)
5259 {
5260 *d++ = *s; /* copy next char */
5261 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5262 skip = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005263 if (has_mbyte)
5264 {
5265 int l = mb_ptr2len(s);
5266
5267 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005268 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005269 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005270 }
5271 }
5272}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005273
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005274/*
5275 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5276 * Also returns TRUE if there is no directory name.
5277 * "fname" must be writable!.
5278 */
5279 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005280dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005281{
5282 char_u *p;
5283 int c;
5284 int retval;
5285
5286 p = gettail_sep(fname);
5287 if (p == fname)
5288 return TRUE;
5289 c = *p;
5290 *p = NUL;
5291 retval = mch_isdir(fname);
5292 *p = c;
5293 return retval;
5294}
5295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005297 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5298 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 */
5300 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005301vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005303#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005305#else
5306 if (p_fic)
5307 return MB_STRICMP(x, y);
5308 return STRCMP(x, y);
5309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310}
5311
5312 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005313vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005315#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005316 char_u *px = x;
5317 char_u *py = y;
5318 int cx = NUL;
5319 int cy = NUL;
5320
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005321 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005323 cx = PTR2CHAR(px);
5324 cy = PTR2CHAR(py);
5325 if (cx == NUL || cy == NUL
5326 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5327 && !(cx == '/' && cy == '\\')
5328 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005330 len -= MB_PTR2LEN(px);
5331 px += MB_PTR2LEN(px);
5332 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 }
5334 if (len == 0)
5335 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005336 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005337#else
5338 if (p_fic)
5339 return MB_STRNICMP(x, y, len);
5340 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005342}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344/*
5345 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005346 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 */
5348 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005349concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350{
5351 char_u *dest;
5352
5353 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5354 if (dest != NULL)
5355 {
5356 STRCPY(dest, fname1);
5357 if (sep)
5358 add_pathsep(dest);
5359 STRCAT(dest, fname2);
5360 }
5361 return dest;
5362}
5363
Bram Moolenaard6754642005-01-17 22:18:45 +00005364/*
5365 * Concatenate two strings and return the result in allocated memory.
5366 * Returns NULL when out of memory.
5367 */
5368 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005369concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005370{
5371 char_u *dest;
5372 size_t l = STRLEN(str1);
5373
5374 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5375 if (dest != NULL)
5376 {
5377 STRCPY(dest, str1);
5378 STRCPY(dest + l, str2);
5379 }
5380 return dest;
5381}
Bram Moolenaard6754642005-01-17 22:18:45 +00005382
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383/*
5384 * Add a path separator to a file name, unless it already ends in a path
5385 * separator.
5386 */
5387 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005388add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005390 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 STRCAT(p, PATHSEPSTR);
5392}
5393
5394/*
5395 * FullName_save - Make an allocated copy of a full file name.
5396 * Returns NULL when out of memory.
5397 */
5398 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005399FullName_save(
5400 char_u *fname,
5401 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005402 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
5404 char_u *buf;
5405 char_u *new_fname = NULL;
5406
5407 if (fname == NULL)
5408 return NULL;
5409
5410 buf = alloc((unsigned)MAXPATHL);
5411 if (buf != NULL)
5412 {
5413 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5414 new_fname = vim_strsave(buf);
5415 else
5416 new_fname = vim_strsave(fname);
5417 vim_free(buf);
5418 }
5419 return new_fname;
5420}
5421
5422#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5423
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005424static char_u *skip_string(char_u *p);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005425static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
5427/*
5428 * Find the start of a comment, not knowing if we are in a comment right now.
5429 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005430 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005432 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005433ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005434{
5435 return find_start_comment(curbuf->b_ind_maxcomment);
5436}
5437
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005439find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
5441 pos_T *pos;
5442 char_u *line;
5443 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005444 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005446 for (;;)
5447 {
5448 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5449 if (pos == NULL)
5450 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005452 /*
5453 * Check if the comment start we found is inside a string.
5454 * If it is then restrict the search to below this line and try again.
5455 */
5456 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005457 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005458 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005459 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005460 break;
5461 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5462 if (cur_maxcomment <= 0)
5463 {
5464 pos = NULL;
5465 break;
5466 }
5467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 return pos;
5469}
5470
5471/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005472 * Find the start of a comment or raw string, not knowing if we are in a
5473 * comment or raw string right now.
5474 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005475 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005476 * Return NULL when not inside a comment or raw string.
5477 * "CORS" -> Comment Or Raw String
5478 */
5479 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005480ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005481{
Bram Moolenaar089af182015-10-07 11:41:49 +02005482 static pos_T comment_pos_copy;
5483 pos_T *comment_pos;
5484 pos_T *rs_pos;
5485
5486 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5487 if (comment_pos != NULL)
5488 {
5489 /* Need to make a copy of the static pos in findmatchlimit(),
5490 * calling find_start_rawstring() may change it. */
5491 comment_pos_copy = *comment_pos;
5492 comment_pos = &comment_pos_copy;
5493 }
5494 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005495
5496 /* If comment_pos is before rs_pos the raw string is inside the comment.
5497 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005498 if (comment_pos == NULL || (rs_pos != NULL
5499 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005500 {
5501 if (is_raw != NULL && rs_pos != NULL)
5502 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005503 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005504 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005505 return comment_pos;
5506}
5507
5508/*
5509 * Find the start of a raw string, not knowing if we are in one right now.
5510 * Search starts at w_cursor.lnum and goes backwards.
5511 * Return NULL when not inside a raw string.
5512 */
5513 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005514find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005515{
5516 pos_T *pos;
5517 char_u *line;
5518 char_u *p;
5519 int cur_maxcomment = ind_maxcomment;
5520
5521 for (;;)
5522 {
5523 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5524 if (pos == NULL)
5525 break;
5526
5527 /*
5528 * Check if the raw string start we found is inside a string.
5529 * If it is then restrict the search to below this line and try again.
5530 */
5531 line = ml_get(pos->lnum);
5532 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5533 p = skip_string(p);
5534 if ((colnr_T)(p - line) <= pos->col)
5535 break;
5536 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5537 if (cur_maxcomment <= 0)
5538 {
5539 pos = NULL;
5540 break;
5541 }
5542 }
5543 return pos;
5544}
5545
5546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 * Skip to the end of a "string" and a 'c' character.
5548 * If there is no string or character, return argument unmodified.
5549 */
5550 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005551skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553 int i;
5554
5555 /*
5556 * We loop, because strings may be concatenated: "date""time".
5557 */
5558 for ( ; ; ++p)
5559 {
5560 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5561 {
5562 if (!p[1]) /* ' at end of line */
5563 break;
5564 i = 2;
5565 if (p[1] == '\\') /* '\n' or '\000' */
5566 {
5567 ++i;
5568 while (vim_isdigit(p[i - 1])) /* '\000' */
5569 ++i;
5570 }
5571 if (p[i] == '\'') /* check for trailing ' */
5572 {
5573 p += i;
5574 continue;
5575 }
5576 }
5577 else if (p[0] == '"') /* start of string */
5578 {
5579 for (++p; p[0]; ++p)
5580 {
5581 if (p[0] == '\\' && p[1] != NUL)
5582 ++p;
5583 else if (p[0] == '"') /* end of string */
5584 break;
5585 }
5586 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005587 continue; /* continue for another string */
5588 }
5589 else if (p[0] == 'R' && p[1] == '"')
5590 {
5591 /* Raw string: R"[delim](...)[delim]" */
5592 char_u *delim = p + 2;
5593 char_u *paren = vim_strchr(delim, '(');
5594
5595 if (paren != NULL)
5596 {
5597 size_t delim_len = paren - delim;
5598
5599 for (p += 3; *p; ++p)
5600 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5601 && p[delim_len + 1] == '"')
5602 {
5603 p += delim_len + 1;
5604 break;
5605 }
5606 if (p[0] == '"')
5607 continue; /* continue for another string */
5608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 }
5610 break; /* no string found */
5611 }
5612 if (!*p)
5613 --p; /* backup from NUL */
5614 return p;
5615}
5616#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5617
5618#if defined(FEAT_CINDENT) || defined(PROTO)
5619
5620/*
5621 * Do C or expression indenting on the current line.
5622 */
5623 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005624do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626# ifdef FEAT_EVAL
5627 if (*curbuf->b_p_inde != NUL)
5628 fixthisline(get_expr_indent);
5629 else
5630# endif
5631 fixthisline(get_c_indent);
5632}
5633
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005634/* Find result cache for cpp_baseclass */
5635typedef struct {
5636 int found;
5637 lpos_T lpos;
5638} cpp_baseclass_cache_T;
5639
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640/*
5641 * Functions for C-indenting.
5642 * Most of this originally comes from Eric Fischer.
5643 */
5644/*
5645 * Below "XXX" means that this function may unlock the current line.
5646 */
5647
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005648static int cin_isdefault(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005649static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005650static int cin_iscomment(char_u *);
5651static int cin_islinecomment(char_u *);
5652static int cin_isterminated(char_u *, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005653static int cin_iselse(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005654static int cin_ends_in(char_u *, char_u *, char_u *);
5655static int cin_starts_with(char_u *s, char *word);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005656static pos_T *find_match_paren(int);
5657static pos_T *find_match_char(int c, int ind_maxparen);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005658static int find_last_paren(char_u *l, int start, int end);
5659static int find_match(int lookfor, linenr_T ourscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661/*
5662 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005663 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 */
5665 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005666cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
5668 while (*s)
5669 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005670 char_u *prev_s = s;
5671
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005673
5674 /* Perl/shell # comment comment continues until eol. Require a space
5675 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005676 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005677 {
5678 s += STRLEN(s);
5679 break;
5680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 if (*s != '/')
5682 break;
5683 ++s;
5684 if (*s == '/') /* slash-slash comment continues till eol */
5685 {
5686 s += STRLEN(s);
5687 break;
5688 }
5689 if (*s != '*')
5690 break;
5691 for (++s; *s; ++s) /* skip slash-star comment */
5692 if (s[0] == '*' && s[1] == '/')
5693 {
5694 s += 2;
5695 break;
5696 }
5697 }
5698 return s;
5699}
5700
5701/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005702 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 * not considered code.
5704 */
5705 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005706cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 return *cin_skipcomment(s) == NUL;
5709}
5710
5711/*
5712 * Check previous lines for a "//" line comment, skipping over blank lines.
5713 */
5714 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005715find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
5717 static pos_T pos;
5718 char_u *line;
5719 char_u *p;
5720
5721 pos = curwin->w_cursor;
5722 while (--pos.lnum > 0)
5723 {
5724 line = ml_get(pos.lnum);
5725 p = skipwhite(line);
5726 if (cin_islinecomment(p))
5727 {
5728 pos.col = (int)(p - line);
5729 return &pos;
5730 }
5731 if (*p != NUL)
5732 break;
5733 }
5734 return NULL;
5735}
5736
5737/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005738 * Return TRUE if "text" starts with "key:".
5739 */
5740 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005741cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005742{
5743 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005744 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005745
5746 if (*s == '\'' || *s == '"')
5747 {
5748 /* can be 'key': or "key": */
5749 quote = *s;
5750 ++s;
5751 }
5752 if (!vim_isIDc(*s)) /* need at least one ID character */
5753 return FALSE;
5754
5755 while (vim_isIDc(*s))
5756 ++s;
5757 if (*s == quote)
5758 ++s;
5759
5760 s = cin_skipcomment(s);
5761
5762 /* "::" is not a label, it's C++ */
5763 return (*s == ':' && s[1] != ':');
5764}
5765
5766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005768 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 */
5770 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005771cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772{
5773 if (!vim_isIDc(**s)) /* need at least one ID character */
5774 return FALSE;
5775
5776 while (vim_isIDc(**s))
5777 (*s)++;
5778
5779 *s = cin_skipcomment(*s);
5780
5781 /* "::" is not a label, it's C++ */
5782 return (**s == ':' && *++*s != ':');
5783}
5784
5785/*
5786 * Recognize a label: "label:".
5787 * Note: curwin->w_cursor must be where we are looking for the label.
5788 */
5789 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005790cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791{
5792 char_u *s;
5793
5794 s = cin_skipcomment(ml_get_curline());
5795
5796 /*
5797 * Exclude "default" from labels, since it should be indented
5798 * like a switch label. Same for C++ scope declarations.
5799 */
5800 if (cin_isdefault(s))
5801 return FALSE;
5802 if (cin_isscopedecl(s))
5803 return FALSE;
5804
5805 if (cin_islabel_skip(&s))
5806 {
5807 /*
5808 * Only accept a label if the previous line is terminated or is a case
5809 * label.
5810 */
5811 pos_T cursor_save;
5812 pos_T *trypos;
5813 char_u *line;
5814
5815 cursor_save = curwin->w_cursor;
5816 while (curwin->w_cursor.lnum > 1)
5817 {
5818 --curwin->w_cursor.lnum;
5819
5820 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005821 * If we're in a comment or raw string now, skip to the start of
5822 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 */
5824 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005825 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 curwin->w_cursor = *trypos;
5827
5828 line = ml_get_curline();
5829 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5830 continue;
5831 if (*(line = cin_skipcomment(line)) == NUL)
5832 continue;
5833
5834 curwin->w_cursor = cursor_save;
5835 if (cin_isterminated(line, TRUE, FALSE)
5836 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005837 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 || (cin_islabel_skip(&line) && cin_nocode(line)))
5839 return TRUE;
5840 return FALSE;
5841 }
5842 curwin->w_cursor = cursor_save;
5843 return TRUE; /* label at start of file??? */
5844 }
5845 return FALSE;
5846}
5847
5848/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005849 * Recognize structure initialization and enumerations:
5850 * "[typedef] [static|public|protected|private] enum"
5851 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 */
5853 static int
5854cin_isinit(void)
5855{
5856 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005857 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858
5859 s = cin_skipcomment(ml_get_curline());
5860
Bram Moolenaar75342212013-03-07 13:13:52 +01005861 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 s = cin_skipcomment(s + 7);
5863
Bram Moolenaar75342212013-03-07 13:13:52 +01005864 for (;;)
5865 {
5866 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005867
Bram Moolenaar75342212013-03-07 13:13:52 +01005868 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5869 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005870 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005871 if (cin_starts_with(s, skip[i]))
5872 {
5873 s = cin_skipcomment(s + l);
5874 l = 0;
5875 break;
5876 }
5877 }
5878 if (l != 0)
5879 break;
5880 }
5881
5882 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 return TRUE;
5884
5885 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5886 return TRUE;
5887
5888 return FALSE;
5889}
5890
5891/*
5892 * Recognize a switch label: "case .*:" or "default:".
5893 */
5894 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005895cin_iscase(
5896 char_u *s,
5897 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005900 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 {
5902 for (s += 4; *s; ++s)
5903 {
5904 s = cin_skipcomment(s);
5905 if (*s == ':')
5906 {
5907 if (s[1] == ':') /* skip over "::" for C++ */
5908 ++s;
5909 else
5910 return TRUE;
5911 }
5912 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005913 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5915 return FALSE; /* stop at comment */
5916 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005917 {
5918 /* JS etc. */
5919 if (strict)
5920 return FALSE; /* stop at string */
5921 else
5922 return TRUE;
5923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 }
5925 return FALSE;
5926 }
5927
5928 if (cin_isdefault(s))
5929 return TRUE;
5930 return FALSE;
5931}
5932
5933/*
5934 * Recognize a "default" switch label.
5935 */
5936 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005937cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
5939 return (STRNCMP(s, "default", 7) == 0
5940 && *(s = cin_skipcomment(s + 7)) == ':'
5941 && s[1] != ':');
5942}
5943
5944/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005945 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 */
5947 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005948cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949{
5950 int i;
5951
5952 s = cin_skipcomment(s);
5953 if (STRNCMP(s, "public", 6) == 0)
5954 i = 6;
5955 else if (STRNCMP(s, "protected", 9) == 0)
5956 i = 9;
5957 else if (STRNCMP(s, "private", 7) == 0)
5958 i = 7;
5959 else
5960 return FALSE;
5961 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5962}
5963
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005964/* Maximum number of lines to search back for a "namespace" line. */
5965#define FIND_NAMESPACE_LIM 20
5966
5967/*
5968 * Recognize a "namespace" scope declaration.
5969 */
5970 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005971cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005972{
5973 char_u *p;
5974 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005975 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005976
5977 s = cin_skipcomment(s);
5978 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5979 {
5980 p = cin_skipcomment(skipwhite(s + 9));
5981 while (*p != NUL)
5982 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005983 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005984 {
5985 has_name = TRUE; /* found end of a name */
5986 p = cin_skipcomment(skipwhite(p));
5987 }
5988 else if (*p == '{')
5989 {
5990 break;
5991 }
5992 else if (vim_iswordc(*p))
5993 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005994 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005995 if (has_name)
5996 return FALSE; /* word character after skipping past name */
5997 ++p;
5998 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005999 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6000 {
6001 if (!has_name_start || has_name)
6002 return FALSE;
6003 /* C++ 17 nested namespace */
6004 p += 3;
6005 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006006 else
6007 {
6008 return FALSE;
6009 }
6010 }
6011 return TRUE;
6012 }
6013 return FALSE;
6014}
6015
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006017 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6018 */
6019 static int
6020cin_is_cpp_extern_c(char_u *s)
6021{
6022 char_u *p;
6023 int has_string_literal = FALSE;
6024
6025 s = cin_skipcomment(s);
6026 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6027 {
6028 p = cin_skipcomment(skipwhite(s + 6));
6029 while (*p != NUL)
6030 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006031 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006032 {
6033 p = cin_skipcomment(skipwhite(p));
6034 }
6035 else if (*p == '{')
6036 {
6037 break;
6038 }
6039 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6040 {
6041 if (has_string_literal)
6042 return FALSE;
6043 has_string_literal = TRUE;
6044 p += 3;
6045 }
6046 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6047 && p[4] == '"')
6048 {
6049 if (has_string_literal)
6050 return FALSE;
6051 has_string_literal = TRUE;
6052 p += 5;
6053 }
6054 else
6055 {
6056 return FALSE;
6057 }
6058 }
6059 return has_string_literal ? TRUE : FALSE;
6060 }
6061 return FALSE;
6062}
6063
6064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 * Return a pointer to the first non-empty non-comment character after a ':'.
6066 * Return NULL if not found.
6067 * case 234: a = b;
6068 * ^
6069 */
6070 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006071after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
6073 for ( ; *l; ++l)
6074 {
6075 if (*l == ':')
6076 {
6077 if (l[1] == ':') /* skip over "::" for C++ */
6078 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006079 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 break;
6081 }
6082 else if (*l == '\'' && l[1] && l[2] == '\'')
6083 l += 2; /* skip over 'x' */
6084 }
6085 if (*l == NUL)
6086 return NULL;
6087 l = cin_skipcomment(l + 1);
6088 if (*l == NUL)
6089 return NULL;
6090 return l;
6091}
6092
6093/*
6094 * Get indent of line "lnum", skipping a label.
6095 * Return 0 if there is nothing after the label.
6096 */
6097 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006098get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099{
6100 char_u *l;
6101 pos_T fp;
6102 colnr_T col;
6103 char_u *p;
6104
6105 l = ml_get(lnum);
6106 p = after_label(l);
6107 if (p == NULL)
6108 return 0;
6109
6110 fp.col = (colnr_T)(p - l);
6111 fp.lnum = lnum;
6112 getvcol(curwin, &fp, &col, NULL, NULL);
6113 return (int)col;
6114}
6115
6116/*
6117 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006118 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 * label: if (asdf && asdfasdf)
6120 * ^
6121 */
6122 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006123skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 char_u *l;
6126 int amount;
6127 pos_T cursor_save;
6128
6129 cursor_save = curwin->w_cursor;
6130 curwin->w_cursor.lnum = lnum;
6131 l = ml_get_curline();
6132 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006133 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 {
6135 amount = get_indent_nolabel(lnum);
6136 l = after_label(ml_get_curline());
6137 if (l == NULL) /* just in case */
6138 l = ml_get_curline();
6139 }
6140 else
6141 {
6142 amount = get_indent();
6143 l = ml_get_curline();
6144 }
6145 *pp = l;
6146
6147 curwin->w_cursor = cursor_save;
6148 return amount;
6149}
6150
6151/*
6152 * Return the indent of the first variable name after a type in a declaration.
6153 * int a, indent of "a"
6154 * static struct foo b, indent of "b"
6155 * enum bla c, indent of "c"
6156 * Returns zero when it doesn't look like a declaration.
6157 */
6158 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006159cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
6161 char_u *line, *p, *s;
6162 int len;
6163 pos_T fp;
6164 colnr_T col;
6165
6166 line = ml_get_curline();
6167 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006168 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6170 {
6171 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006172 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 }
6174 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6175 p = skipwhite(p + 6);
6176 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6177 p = skipwhite(p + 4);
6178 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6179 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6180 {
6181 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006182 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6183 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6184 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6185 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 p = s;
6187 }
6188 for (len = 0; vim_isIDc(p[len]); ++len)
6189 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006190 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 return 0;
6192
6193 p = skipwhite(p + len);
6194 fp.lnum = curwin->w_cursor.lnum;
6195 fp.col = (colnr_T)(p - line);
6196 getvcol(curwin, &fp, &col, NULL, NULL);
6197 return (int)col;
6198}
6199
6200/*
6201 * Return the indent of the first non-blank after an equal sign.
6202 * char *foo = "here";
6203 * Return zero if no (useful) equal sign found.
6204 * Return -1 if the line above "lnum" ends in a backslash.
6205 * foo = "asdf\
6206 * asdf\
6207 * here";
6208 */
6209 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006210cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 char_u *line;
6213 char_u *s;
6214 colnr_T col;
6215 pos_T fp;
6216
6217 if (lnum > 1)
6218 {
6219 line = ml_get(lnum - 1);
6220 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6221 return -1;
6222 }
6223
6224 line = s = ml_get(lnum);
6225 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6226 {
6227 if (cin_iscomment(s)) /* ignore comments */
6228 s = cin_skipcomment(s);
6229 else
6230 ++s;
6231 }
6232 if (*s != '=')
6233 return 0;
6234
6235 s = skipwhite(s + 1);
6236 if (cin_nocode(s))
6237 return 0;
6238
6239 if (*s == '"') /* nice alignment for continued strings */
6240 ++s;
6241
6242 fp.lnum = lnum;
6243 fp.col = (colnr_T)(s - line);
6244 getvcol(curwin, &fp, &col, NULL, NULL);
6245 return (int)col;
6246}
6247
6248/*
6249 * Recognize a preprocessor statement: Any line that starts with '#'.
6250 */
6251 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006252cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006254 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 return TRUE;
6256 return FALSE;
6257}
6258
6259/*
6260 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6261 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6262 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006263 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 */
6265 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006266cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267{
6268 char_u *line = *pp;
6269 linenr_T lnum = *lnump;
6270 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006271 int candidate_amount = *amount;
6272
6273 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6274 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006276 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 {
6278 if (cin_ispreproc(line))
6279 {
6280 retval = TRUE;
6281 *lnump = lnum;
6282 break;
6283 }
6284 if (lnum == 1)
6285 break;
6286 line = ml_get(--lnum);
6287 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6288 break;
6289 }
6290
6291 if (lnum != *lnump)
6292 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006293 if (retval)
6294 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 return retval;
6296}
6297
6298/*
6299 * Recognize the start of a C or C++ comment.
6300 */
6301 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006302cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303{
6304 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6305}
6306
6307/*
6308 * Recognize the start of a "//" comment.
6309 */
6310 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006311cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
6313 return (p[0] == '/' && p[1] == '/');
6314}
6315
6316/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006317 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6318 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006320 * If a line begins with an "else", only consider it terminated if no unmatched
6321 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 * Return the character terminating the line (ending char's have precedence if
6323 * both apply in order to determine initializations).
6324 */
6325 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006326cin_isterminated(
6327 char_u *s,
6328 int incl_open, /* include '{' at the end as terminator */
6329 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006331 char_u found_start = 0;
6332 unsigned n_open = 0;
6333 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334
6335 s = cin_skipcomment(s);
6336
6337 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6338 found_start = *s;
6339
Bram Moolenaar496f9512011-05-19 16:35:09 +02006340 if (!found_start)
6341 is_else = cin_iselse(s);
6342
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 while (*s)
6344 {
6345 /* skip over comments, "" strings and 'c'haracters */
6346 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006347 if (*s == '}' && n_open > 0)
6348 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006349 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006350 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 && cin_nocode(s + 1))
6352 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006353 else if (*s == '{')
6354 {
6355 if (incl_open && cin_nocode(s + 1))
6356 return *s;
6357 else
6358 ++n_open;
6359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
6361 if (*s)
6362 s++;
6363 }
6364 return found_start;
6365}
6366
6367/*
6368 * Recognize the basic picture of a function declaration -- it needs to
6369 * have an open paren somewhere and a close paren at the end of the line and
6370 * no semicolons anywhere.
6371 * When a line ends in a comma we continue looking in the next line.
6372 * "sp" points to a string with the line. When looking at other lines it must
6373 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006374 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006375 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 */
6377 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006378cin_isfuncdecl(
6379 char_u **sp,
6380 linenr_T first_lnum,
6381 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 char_u *s;
6384 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006385 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006387 pos_T *trypos;
6388 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
6390 if (sp == NULL)
6391 s = ml_get(lnum);
6392 else
6393 s = *sp;
6394
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006395 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006396 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006397 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006398 {
6399 lnum = trypos->lnum;
6400 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006401 {
6402 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006403 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006404 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006405
6406 s = ml_get(lnum);
6407 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006408 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006409
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006410 /* Ignore line starting with #. */
6411 if (cin_ispreproc(s))
6412 return FALSE;
6413
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6415 {
6416 if (cin_iscomment(s)) /* ignore comments */
6417 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006418 else if (*s == ':')
6419 {
6420 if (*(s + 1) == ':')
6421 s += 2;
6422 else
6423 /* To avoid a mistake in the following situation:
6424 * A::A(int a, int b)
6425 * : a(0) // <--not a function decl
6426 * , b(0)
6427 * {...
6428 */
6429 return FALSE;
6430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 else
6432 ++s;
6433 }
6434 if (*s != '(')
6435 return FALSE; /* ';', ' or " before any () or no '(' */
6436
6437 while (*s && *s != ';' && *s != '\'' && *s != '"')
6438 {
6439 if (*s == ')' && cin_nocode(s + 1))
6440 {
6441 /* ')' at the end: may have found a match
6442 * Check for he previous line not to end in a backslash:
6443 * #if defined(x) && \
6444 * defined(y)
6445 */
6446 lnum = first_lnum - 1;
6447 s = ml_get(lnum);
6448 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6449 retval = TRUE;
6450 goto done;
6451 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006452 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006454 int comma = (*s == ',');
6455
6456 /* ',' at the end: continue looking in the next line.
6457 * At the end: check for ',' in the next line, for this style:
6458 * func(arg1
6459 * , arg2) */
6460 for (;;)
6461 {
6462 if (lnum >= curbuf->b_ml.ml_line_count)
6463 break;
6464 s = ml_get(++lnum);
6465 if (!cin_ispreproc(s))
6466 break;
6467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 if (lnum >= curbuf->b_ml.ml_line_count)
6469 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006470 /* Require a comma at end of the line or a comma or ')' at the
6471 * start of next line. */
6472 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006473 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006474 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006475 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 }
6477 else if (cin_iscomment(s)) /* ignore comments */
6478 s = cin_skipcomment(s);
6479 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006482 just_started = FALSE;
6483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 }
6485
6486done:
6487 if (lnum != first_lnum && sp != NULL)
6488 *sp = ml_get(first_lnum);
6489
6490 return retval;
6491}
6492
6493 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006494cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006496 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497}
6498
6499 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006500cin_iselse(
6501 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 if (*p == '}') /* accept "} else" */
6504 p = cin_skipcomment(p + 1);
6505 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6506}
6507
6508 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006509cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6512}
6513
6514/*
6515 * Check if this is a "while" that should have a matching "do".
6516 * We only accept a "while (condition) ;", with only white space between the
6517 * ')' and ';'. The condition may be spread over several lines.
6518 */
6519 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006520cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
6522 pos_T cursor_save;
6523 pos_T *trypos;
6524 int retval = FALSE;
6525
6526 p = cin_skipcomment(p);
6527 if (*p == '}') /* accept "} while (cond);" */
6528 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006529 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 {
6531 cursor_save = curwin->w_cursor;
6532 curwin->w_cursor.lnum = lnum;
6533 curwin->w_cursor.col = 0;
6534 p = ml_get_curline();
6535 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6536 {
6537 ++p;
6538 ++curwin->w_cursor.col;
6539 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006540 if ((trypos = findmatchlimit(NULL, 0, 0,
6541 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6543 retval = TRUE;
6544 curwin->w_cursor = cursor_save;
6545 }
6546 return retval;
6547}
6548
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006549/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006550 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006551 * Return 0 if there is none.
6552 * Otherwise return !0 and update "*poffset" to point to the place where the
6553 * string was found.
6554 */
6555 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006556cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006557{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006558 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006559
6560 if (offset-- < 2)
6561 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006562 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006563 --offset;
6564
6565 offset -= 1;
6566 if (!STRNCMP(line + offset, "if", 2))
6567 goto probablyFound;
6568
6569 if (offset >= 1)
6570 {
6571 offset -= 1;
6572 if (!STRNCMP(line + offset, "for", 3))
6573 goto probablyFound;
6574
6575 if (offset >= 2)
6576 {
6577 offset -= 2;
6578 if (!STRNCMP(line + offset, "while", 5))
6579 goto probablyFound;
6580 }
6581 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006582 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006583
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006584probablyFound:
6585 if (!offset || !vim_isIDc(line[offset - 1]))
6586 {
6587 *poffset = offset;
6588 return 1;
6589 }
6590 return 0;
6591}
6592
6593/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006594 * Return TRUE if we are at the end of a do-while.
6595 * do
6596 * nothing;
6597 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006598 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006599 * Adjust the cursor to the line with "while".
6600 */
6601 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006602cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006603{
6604 char_u *line;
6605 char_u *p;
6606 char_u *s;
6607 pos_T *trypos;
6608 int i;
6609
6610 if (terminated != ';') /* there must be a ';' at the end */
6611 return FALSE;
6612
6613 p = line = ml_get_curline();
6614 while (*p != NUL)
6615 {
6616 p = cin_skipcomment(p);
6617 if (*p == ')')
6618 {
6619 s = skipwhite(p + 1);
6620 if (*s == ';' && cin_nocode(s + 1))
6621 {
6622 /* Found ");" at end of the line, now check there is "while"
6623 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006624 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006625 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006626 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006627 if (trypos != NULL)
6628 {
6629 s = cin_skipcomment(ml_get(trypos->lnum));
6630 if (*s == '}') /* accept "} while (cond);" */
6631 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006632 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006633 {
6634 curwin->w_cursor.lnum = trypos->lnum;
6635 return TRUE;
6636 }
6637 }
6638
6639 /* Searching may have made "line" invalid, get it again. */
6640 line = ml_get_curline();
6641 p = line + i;
6642 }
6643 }
6644 if (*p != NUL)
6645 ++p;
6646 }
6647 return FALSE;
6648}
6649
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006651cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652{
6653 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6654}
6655
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006656/*
6657 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 * constructor-initialization. eg:
6659 *
6660 * class MyClass :
6661 * baseClass <-- here
6662 * class MyClass : public baseClass,
6663 * anotherBaseClass <-- here (should probably lineup ??)
6664 * MyClass::MyClass(...) :
6665 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006666 *
6667 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 */
6669 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006670cin_is_cpp_baseclass(
6671 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006673 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 char_u *s;
6675 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006676 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006677 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006679 if (pos->lnum <= lnum)
6680 return cached->found; /* Use the cached result */
6681
6682 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006684 s = skipwhite(line);
6685 if (*s == '#') /* skip #define FOO x ? (x) : x */
6686 return FALSE;
6687 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 if (*s == NUL)
6689 return FALSE;
6690
6691 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6692
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006693 /* Search for a line starting with '#', empty, ending in ';' or containing
6694 * '{' or '}' and start below it. This handles the following situations:
6695 * a = cond ?
6696 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006697 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006698 * func::foo()
6699 * : something
6700 * {}
6701 * Foo::Foo (int one, int two)
6702 * : something(4),
6703 * somethingelse(3)
6704 * {}
6705 */
6706 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006708 line = ml_get(lnum - 1);
6709 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006710 if (*s == '#' || *s == NUL)
6711 break;
6712 while (*s != NUL)
6713 {
6714 s = cin_skipcomment(s);
6715 if (*s == '{' || *s == '}'
6716 || (*s == ';' && cin_nocode(s + 1)))
6717 break;
6718 if (*s != NUL)
6719 ++s;
6720 }
6721 if (*s != NUL)
6722 break;
6723 --lnum;
6724 }
6725
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006726 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006727 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006728 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006729 for (;;)
6730 {
6731 if (*s == NUL)
6732 {
6733 if (lnum == curwin->w_cursor.lnum)
6734 break;
6735 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006736 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006737 s = line;
6738 }
6739 if (s == line)
6740 {
6741 /* don't recognize "case (foo):" as a baseclass */
6742 if (cin_iscase(s, FALSE))
6743 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006744 s = cin_skipcomment(line);
6745 if (*s == NUL)
6746 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006747 }
6748
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006749 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006750 s = skip_string(s) + 1;
6751 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 {
6753 if (s[1] == ':')
6754 {
6755 /* skip double colon. It can't be a constructor
6756 * initialization any more */
6757 lookfor_ctor_init = FALSE;
6758 s = cin_skipcomment(s + 2);
6759 }
6760 else if (lookfor_ctor_init || class_or_struct)
6761 {
6762 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006763 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 cpp_base_class = TRUE;
6765 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006766 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 s = cin_skipcomment(s + 1);
6768 }
6769 else
6770 s = cin_skipcomment(s + 1);
6771 }
6772 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6773 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6774 {
6775 class_or_struct = TRUE;
6776 lookfor_ctor_init = FALSE;
6777
6778 if (*s == 'c')
6779 s = cin_skipcomment(s + 5);
6780 else
6781 s = cin_skipcomment(s + 6);
6782 }
6783 else
6784 {
6785 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6786 {
6787 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6788 }
6789 else if (s[0] == ')')
6790 {
6791 /* Constructor-initialization is assumed if we come across
6792 * something like "):" */
6793 class_or_struct = FALSE;
6794 lookfor_ctor_init = TRUE;
6795 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006796 else if (s[0] == '?')
6797 {
6798 /* Avoid seeing '() :' after '?' as constructor init. */
6799 return FALSE;
6800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 else if (!vim_isIDc(s[0]))
6802 {
6803 /* if it is not an identifier, we are wrong */
6804 class_or_struct = FALSE;
6805 lookfor_ctor_init = FALSE;
6806 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006807 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {
6809 /* it can't be a constructor-initialization any more */
6810 lookfor_ctor_init = FALSE;
6811
6812 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006813 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006814 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006817 /* When the line ends in a comma don't align with it. */
6818 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006819 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006820
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 s = cin_skipcomment(s + 1);
6822 }
6823 }
6824
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006825 cached->found = cpp_base_class;
6826 if (cpp_base_class)
6827 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 return cpp_base_class;
6829}
6830
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006831 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006832get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006833{
6834 int amount;
6835 colnr_T vcol;
6836 pos_T *trypos;
6837
6838 if (col == 0)
6839 {
6840 amount = get_indent();
6841 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006842 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006843 amount = get_indent_lnum(trypos->lnum); /* XXX */
6844 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006845 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006846 }
6847 else
6848 {
6849 curwin->w_cursor.col = col;
6850 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6851 amount = (int)vcol;
6852 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006853 if (amount < curbuf->b_ind_cpp_baseclass)
6854 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006855 return amount;
6856}
6857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858/*
6859 * Return TRUE if string "s" ends with the string "find", possibly followed by
6860 * white space and comments. Skip strings and comments.
6861 * Ignore "ignore" after "find" if it's not NULL.
6862 */
6863 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006864cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 char_u *p = s;
6867 char_u *r;
6868 int len = (int)STRLEN(find);
6869
6870 while (*p != NUL)
6871 {
6872 p = cin_skipcomment(p);
6873 if (STRNCMP(p, find, len) == 0)
6874 {
6875 r = skipwhite(p + len);
6876 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6877 r = skipwhite(r + STRLEN(ignore));
6878 if (cin_nocode(r))
6879 return TRUE;
6880 }
6881 if (*p != NUL)
6882 ++p;
6883 }
6884 return FALSE;
6885}
6886
6887/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006888 * Return TRUE when "s" starts with "word" and then a non-ID character.
6889 */
6890 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006891cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006892{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006893 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006894
6895 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6896}
6897
6898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 * Skip strings, chars and comments until at or past "trypos".
6900 * Return the column found.
6901 */
6902 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006903cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
6905 char_u *line;
6906 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006907 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
6909 p = line = ml_get(trypos->lnum);
6910 while (*p && (colnr_T)(p - line) < trypos->col)
6911 {
6912 if (cin_iscomment(p))
6913 p = cin_skipcomment(p);
6914 else
6915 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006916 new_p = skip_string(p);
6917 if (new_p == p)
6918 ++p;
6919 else
6920 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 }
6922 }
6923 return (int)(p - line);
6924}
6925
6926/*
6927 * Find the '{' at the start of the block we are in.
6928 * Return NULL if no match found.
6929 * Ignore a '{' that is in a comment, makes indenting the next three lines
6930 * work. */
6931/* foo() */
6932/* { */
6933/* } */
6934
6935 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006936find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
6938 pos_T cursor_save;
6939 pos_T *trypos;
6940 pos_T *pos;
6941 static pos_T pos_copy;
6942
6943 cursor_save = curwin->w_cursor;
6944 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6945 {
6946 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6947 trypos = &pos_copy;
6948 curwin->w_cursor = *trypos;
6949 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006950 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006952 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 break;
6954 if (pos != NULL)
6955 curwin->w_cursor.lnum = pos->lnum;
6956 }
6957 curwin->w_cursor = cursor_save;
6958 return trypos;
6959}
6960
6961/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006962 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006963 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 */
6965 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006966find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006968 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006969}
6970
6971 static pos_T *
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02006972find_match_char(int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006973{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 pos_T cursor_save;
6975 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006976 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006977 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978
6979 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006980 ind_maxp_wk = ind_maxparen;
6981retry:
6982 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {
6984 /* check if the ( is in a // comment */
6985 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006986 {
6987 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6988 if (ind_maxp_wk > 0)
6989 {
6990 curwin->w_cursor = *trypos;
6991 curwin->w_cursor.col = 0; /* XXX */
6992 goto retry;
6993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 else
6997 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006998 pos_T *trypos_wk;
6999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7001 trypos = &pos_copy;
7002 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007003 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007004 {
7005 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7006 - trypos_wk->lnum);
7007 if (ind_maxp_wk > 0)
7008 {
7009 curwin->w_cursor = *trypos_wk;
7010 goto retry;
7011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 }
7015 }
7016 curwin->w_cursor = cursor_save;
7017 return trypos;
7018}
7019
7020/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007021 * Find the matching '(', ignoring it if it is in a comment or before an
7022 * unmatched {.
7023 * Return NULL if no match found.
7024 */
7025 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007026find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007027{
7028 pos_T *trypos = find_match_paren(ind_maxparen);
7029
7030 if (trypos != NULL)
7031 {
7032 pos_T *tryposBrace = find_start_brace();
7033
7034 /* If both an unmatched '(' and '{' is found. Ignore the '('
7035 * position if the '{' is further down. */
7036 if (tryposBrace != NULL
7037 && (trypos->lnum != tryposBrace->lnum
7038 ? trypos->lnum < tryposBrace->lnum
7039 : trypos->col < tryposBrace->col))
7040 trypos = NULL;
7041 }
7042 return trypos;
7043}
7044
7045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 * Return ind_maxparen corrected for the difference in line number between the
7047 * cursor position and "startpos". This makes sure that searching for a
7048 * matching paren above the cursor line doesn't find a match because of
7049 * looking a few lines further.
7050 */
7051 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007052corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053{
7054 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7055
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007056 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7057 return curbuf->b_ind_maxparen - (int)n;
7058 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059}
7060
7061/*
7062 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007063 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 */
7065 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007066find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067{
7068 int i;
7069 int retval = FALSE;
7070 int open_count = 0;
7071
7072 curwin->w_cursor.col = 0; /* default is start of line */
7073
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007074 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {
7076 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7077 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7078 if (l[i] == start)
7079 ++open_count;
7080 else if (l[i] == end)
7081 {
7082 if (open_count > 0)
7083 --open_count;
7084 else
7085 {
7086 curwin->w_cursor.col = i;
7087 retval = TRUE;
7088 }
7089 }
7090 }
7091 return retval;
7092}
7093
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007094/*
7095 * Parse 'cinoptions' and set the values in "curbuf".
7096 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7097 */
7098 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007099parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007100{
7101 char_u *p;
7102 char_u *l;
7103 char_u *digits;
7104 int n;
7105 int divider;
7106 int fraction = 0;
7107 int sw = (int)get_sw_value(buf);
7108
7109 /*
7110 * Set the default values.
7111 */
7112 /* Spaces from a block's opening brace the prevailing indent for that
7113 * block should be. */
7114 buf->b_ind_level = sw;
7115
7116 /* Spaces from the edge of the line an open brace that's at the end of a
7117 * line is imagined to be. */
7118 buf->b_ind_open_imag = 0;
7119
7120 /* Spaces from the prevailing indent for a line that is not preceded by
7121 * an opening brace. */
7122 buf->b_ind_no_brace = 0;
7123
7124 /* Column where the first { of a function should be located }. */
7125 buf->b_ind_first_open = 0;
7126
7127 /* Spaces from the prevailing indent a leftmost open brace should be
7128 * located. */
7129 buf->b_ind_open_extra = 0;
7130
7131 /* Spaces from the matching open brace (real location for one at the left
7132 * edge; imaginary location from one that ends a line) the matching close
7133 * brace should be located. */
7134 buf->b_ind_close_extra = 0;
7135
7136 /* Spaces from the edge of the line an open brace sitting in the leftmost
7137 * column is imagined to be. */
7138 buf->b_ind_open_left_imag = 0;
7139
7140 /* Spaces jump labels should be shifted to the left if N is non-negative,
7141 * otherwise the jump label will be put to column 1. */
7142 buf->b_ind_jump_label = -1;
7143
7144 /* Spaces from the switch() indent a "case xx" label should be located. */
7145 buf->b_ind_case = sw;
7146
7147 /* Spaces from the "case xx:" code after a switch() should be located. */
7148 buf->b_ind_case_code = sw;
7149
7150 /* Lineup break at end of case in switch() with case label. */
7151 buf->b_ind_case_break = 0;
7152
7153 /* Spaces from the class declaration indent a scope declaration label
7154 * should be located. */
7155 buf->b_ind_scopedecl = sw;
7156
7157 /* Spaces from the scope declaration label code should be located. */
7158 buf->b_ind_scopedecl_code = sw;
7159
7160 /* Amount K&R-style parameters should be indented. */
7161 buf->b_ind_param = sw;
7162
7163 /* Amount a function type spec should be indented. */
7164 buf->b_ind_func_type = sw;
7165
7166 /* Amount a cpp base class declaration or constructor initialization
7167 * should be indented. */
7168 buf->b_ind_cpp_baseclass = sw;
7169
7170 /* additional spaces beyond the prevailing indent a continuation line
7171 * should be located. */
7172 buf->b_ind_continuation = sw;
7173
7174 /* Spaces from the indent of the line with an unclosed parentheses. */
7175 buf->b_ind_unclosed = sw * 2;
7176
7177 /* Spaces from the indent of the line with an unclosed parentheses, which
7178 * itself is also unclosed. */
7179 buf->b_ind_unclosed2 = sw;
7180
7181 /* Suppress ignoring spaces from the indent of a line starting with an
7182 * unclosed parentheses. */
7183 buf->b_ind_unclosed_noignore = 0;
7184
7185 /* If the opening paren is the last nonwhite character on the line, and
7186 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7187 * context (for very long lines). */
7188 buf->b_ind_unclosed_wrapped = 0;
7189
7190 /* Suppress ignoring white space when lining up with the character after
7191 * an unclosed parentheses. */
7192 buf->b_ind_unclosed_whiteok = 0;
7193
7194 /* Indent a closing parentheses under the line start of the matching
7195 * opening parentheses. */
7196 buf->b_ind_matching_paren = 0;
7197
7198 /* Indent a closing parentheses under the previous line. */
7199 buf->b_ind_paren_prev = 0;
7200
7201 /* Extra indent for comments. */
7202 buf->b_ind_comment = 0;
7203
7204 /* Spaces from the comment opener when there is nothing after it. */
7205 buf->b_ind_in_comment = 3;
7206
7207 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7208 * after the comment opener. */
7209 buf->b_ind_in_comment2 = 0;
7210
7211 /* Max lines to search for an open paren. */
7212 buf->b_ind_maxparen = 20;
7213
7214 /* Max lines to search for an open comment. */
7215 buf->b_ind_maxcomment = 70;
7216
7217 /* Handle braces for java code. */
7218 buf->b_ind_java = 0;
7219
7220 /* Not to confuse JS object properties with labels. */
7221 buf->b_ind_js = 0;
7222
7223 /* Handle blocked cases correctly. */
7224 buf->b_ind_keep_case_label = 0;
7225
7226 /* Handle C++ namespace. */
7227 buf->b_ind_cpp_namespace = 0;
7228
7229 /* Handle continuation lines containing conditions of if(), for() and
7230 * while(). */
7231 buf->b_ind_if_for_while = 0;
7232
Bram Moolenaar6b643942017-03-05 19:44:06 +01007233 /* indentation for # comments */
7234 buf->b_ind_hash_comment = 0;
7235
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007236 /* Handle C++ extern "C" or "C++" */
7237 buf->b_ind_cpp_extern_c = 0;
7238
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007239 for (p = buf->b_p_cino; *p; )
7240 {
7241 l = p++;
7242 if (*p == '-')
7243 ++p;
7244 digits = p; /* remember where the digits start */
7245 n = getdigits(&p);
7246 divider = 0;
7247 if (*p == '.') /* ".5s" means a fraction */
7248 {
7249 fraction = atol((char *)++p);
7250 while (VIM_ISDIGIT(*p))
7251 {
7252 ++p;
7253 if (divider)
7254 divider *= 10;
7255 else
7256 divider = 10;
7257 }
7258 }
7259 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7260 {
7261 if (p == digits)
7262 n = sw; /* just "s" is one 'shiftwidth' */
7263 else
7264 {
7265 n *= sw;
7266 if (divider)
7267 n += (sw * fraction + divider / 2) / divider;
7268 }
7269 ++p;
7270 }
7271 if (l[1] == '-')
7272 n = -n;
7273
7274 /* When adding an entry here, also update the default 'cinoptions' in
7275 * doc/indent.txt, and add explanation for it! */
7276 switch (*l)
7277 {
7278 case '>': buf->b_ind_level = n; break;
7279 case 'e': buf->b_ind_open_imag = n; break;
7280 case 'n': buf->b_ind_no_brace = n; break;
7281 case 'f': buf->b_ind_first_open = n; break;
7282 case '{': buf->b_ind_open_extra = n; break;
7283 case '}': buf->b_ind_close_extra = n; break;
7284 case '^': buf->b_ind_open_left_imag = n; break;
7285 case 'L': buf->b_ind_jump_label = n; break;
7286 case ':': buf->b_ind_case = n; break;
7287 case '=': buf->b_ind_case_code = n; break;
7288 case 'b': buf->b_ind_case_break = n; break;
7289 case 'p': buf->b_ind_param = n; break;
7290 case 't': buf->b_ind_func_type = n; break;
7291 case '/': buf->b_ind_comment = n; break;
7292 case 'c': buf->b_ind_in_comment = n; break;
7293 case 'C': buf->b_ind_in_comment2 = n; break;
7294 case 'i': buf->b_ind_cpp_baseclass = n; break;
7295 case '+': buf->b_ind_continuation = n; break;
7296 case '(': buf->b_ind_unclosed = n; break;
7297 case 'u': buf->b_ind_unclosed2 = n; break;
7298 case 'U': buf->b_ind_unclosed_noignore = n; break;
7299 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7300 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7301 case 'm': buf->b_ind_matching_paren = n; break;
7302 case 'M': buf->b_ind_paren_prev = n; break;
7303 case ')': buf->b_ind_maxparen = n; break;
7304 case '*': buf->b_ind_maxcomment = n; break;
7305 case 'g': buf->b_ind_scopedecl = n; break;
7306 case 'h': buf->b_ind_scopedecl_code = n; break;
7307 case 'j': buf->b_ind_java = n; break;
7308 case 'J': buf->b_ind_js = n; break;
7309 case 'l': buf->b_ind_keep_case_label = n; break;
7310 case '#': buf->b_ind_hash_comment = n; break;
7311 case 'N': buf->b_ind_cpp_namespace = n; break;
7312 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007313 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007314 }
7315 if (*p == ',')
7316 ++p;
7317 }
7318}
7319
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007320/*
7321 * Return the desired indent for C code.
7322 * Return -1 if the indent should be left alone (inside a raw string).
7323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007325get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 pos_T cur_curpos;
7328 int amount;
7329 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007330 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 colnr_T col;
7332 char_u *theline;
7333 char_u *linecopy;
7334 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007335 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007337 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 pos_T our_paren_pos;
7339 char_u *start;
7340 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007341#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342#define BRACE_AT_START 2 /* '{' is at start of line */
7343#define BRACE_AT_END 3 /* '{' is at end of line */
7344 linenr_T ourscope;
7345 char_u *l;
7346 char_u *look;
7347 char_u terminated;
7348 int lookfor;
7349#define LOOKFOR_INITIAL 0
7350#define LOOKFOR_IF 1
7351#define LOOKFOR_DO 2
7352#define LOOKFOR_CASE 3
7353#define LOOKFOR_ANY 4
7354#define LOOKFOR_TERM 5
7355#define LOOKFOR_UNTERM 6
7356#define LOOKFOR_SCOPEDECL 7
7357#define LOOKFOR_NOBREAK 8
7358#define LOOKFOR_CPP_BASECLASS 9
7359#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007360#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007361#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362
7363 int whilelevel;
7364 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 int n;
7366 int iscase;
7367 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007368 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007370 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007371 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007372 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007373 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007374 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007376 /* make a copy, value is changed below */
7377 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378
7379 /* remember where the cursor was when we started */
7380 cur_curpos = curwin->w_cursor;
7381
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007382 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007383 if (cur_curpos.lnum == 1)
7384 return 0;
7385
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 /* Get a copy of the current contents of the line.
7387 * This is required, because only the most recent line obtained with
7388 * ml_get is valid! */
7389 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7390 if (linecopy == NULL)
7391 return 0;
7392
7393 /*
7394 * In insert mode and the cursor is on a ')' truncate the line at the
7395 * cursor position. We don't want to line up with the matching '(' when
7396 * inserting new stuff.
7397 * For unknown reasons the cursor might be past the end of the line, thus
7398 * check for that.
7399 */
7400 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007401 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 && linecopy[curwin->w_cursor.col] == ')')
7403 linecopy[curwin->w_cursor.col] = NUL;
7404
7405 theline = skipwhite(linecopy);
7406
7407 /* move the cursor to the start of the line */
7408
7409 curwin->w_cursor.col = 0;
7410
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007411 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007412
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007414 * If we are inside a raw string don't change the indent.
7415 * Ignore a raw string inside a comment.
7416 */
7417 comment_pos = ind_find_start_comment();
7418 if (comment_pos != NULL)
7419 {
7420 /* findmatchlimit() static pos is overwritten, make a copy */
7421 tryposCopy = *comment_pos;
7422 comment_pos = &tryposCopy;
7423 }
7424 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007425 if (trypos != NULL && (comment_pos == NULL
7426 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007427 {
7428 amount = -1;
7429 goto laterend;
7430 }
7431
7432 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 * #defines and so on always go at the left when included in 'cinkeys'.
7434 */
7435 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007436 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007437 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007438 goto theend;
7439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
7441 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007442 * Is it a non-case label? Then that goes at the left margin too unless:
7443 * - JS flag is set.
7444 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007446 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007447 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 {
7449 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007450 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 }
7452
7453 /*
7454 * If we're inside a "//" comment and there is a "//" comment in a
7455 * previous line, lineup with that one.
7456 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007457 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 && (trypos = find_line_comment()) != NULL) /* XXX */
7459 {
7460 /* find how indented the line beginning the comment is */
7461 getvcol(curwin, trypos, &col, NULL, NULL);
7462 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007463 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 }
7465
7466 /*
7467 * If we're inside a comment and not looking at the start of the
7468 * comment, try using the 'comments' option.
7469 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007470 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 {
7472 int lead_start_len = 2;
7473 int lead_middle_len = 1;
7474 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7475 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7476 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7477 char_u *p;
7478 int start_align = 0;
7479 int start_off = 0;
7480 int done = FALSE;
7481
7482 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007483 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007485 *lead_start = NUL;
7486 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
7488 p = curbuf->b_p_com;
7489 while (*p != NUL)
7490 {
7491 int align = 0;
7492 int off = 0;
7493 int what = 0;
7494
7495 while (*p != NUL && *p != ':')
7496 {
7497 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7498 what = *p++;
7499 else if (*p == COM_LEFT || *p == COM_RIGHT)
7500 align = *p++;
7501 else if (VIM_ISDIGIT(*p) || *p == '-')
7502 off = getdigits(&p);
7503 else
7504 ++p;
7505 }
7506
7507 if (*p == ':')
7508 ++p;
7509 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7510 if (what == COM_START)
7511 {
7512 STRCPY(lead_start, lead_end);
7513 lead_start_len = (int)STRLEN(lead_start);
7514 start_off = off;
7515 start_align = align;
7516 }
7517 else if (what == COM_MIDDLE)
7518 {
7519 STRCPY(lead_middle, lead_end);
7520 lead_middle_len = (int)STRLEN(lead_middle);
7521 }
7522 else if (what == COM_END)
7523 {
7524 /* If our line starts with the middle comment string, line it
7525 * up with the comment opener per the 'comments' option. */
7526 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7527 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7528 {
7529 done = TRUE;
7530 if (curwin->w_cursor.lnum > 1)
7531 {
7532 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007533 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 * the middle comment string matches in the previous
7535 * line, use the indent of that line. XXX */
7536 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7537 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7538 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7539 else if (STRNCMP(look, lead_middle,
7540 lead_middle_len) == 0)
7541 {
7542 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7543 break;
7544 }
7545 /* If the start comment string doesn't match with the
7546 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007547 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 lead_start, lead_start_len) != 0)
7549 continue;
7550 }
7551 if (start_off != 0)
7552 amount += start_off;
7553 else if (start_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 break;
7557 }
7558
7559 /* If our line starts with the end comment string, line it up
7560 * with the middle comment */
7561 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7562 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7563 {
7564 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7565 /* XXX */
7566 if (off != 0)
7567 amount += off;
7568 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007569 amount += vim_strsize(lead_start)
7570 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 done = TRUE;
7572 break;
7573 }
7574 }
7575 }
7576
7577 /* If our line starts with an asterisk, line up with the
7578 * asterisk in the comment opener; otherwise, line up
7579 * with the first character of the comment text.
7580 */
7581 if (done)
7582 ;
7583 else if (theline[0] == '*')
7584 amount += 1;
7585 else
7586 {
7587 /*
7588 * If we are more than one line away from the comment opener, take
7589 * the indent of the previous non-empty line. If 'cino' has "CO"
7590 * and we are just below the comment opener and there are any
7591 * white characters after it line up with the text after it;
7592 * otherwise, add the amount specified by "c" in 'cino'
7593 */
7594 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007595 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 {
7597 if (linewhite(lnum)) /* skip blank lines */
7598 continue;
7599 amount = get_indent_lnum(lnum); /* XXX */
7600 break;
7601 }
7602 if (amount == -1) /* use the comment opener */
7603 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007604 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007606 start = ml_get(comment_pos->lnum);
7607 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007609 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007611 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007613 if (curbuf->b_ind_in_comment2 || *look == NUL)
7614 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 }
7616 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007617 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 }
7619
7620 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007621 * Are we looking at a ']' that has a match?
7622 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007623 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007624 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7625 {
7626 /* align with the line containing the '['. */
7627 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007628 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007629 }
7630
7631 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 * Are we inside parentheses or braces?
7633 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007634 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007635 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007636 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 || trypos != NULL)
7638 {
7639 if (trypos != NULL && tryposBrace != NULL)
7640 {
7641 /* Both an unmatched '(' and '{' is found. Use the one which is
7642 * closer to the current cursor position, set the other to NULL. */
7643 if (trypos->lnum != tryposBrace->lnum
7644 ? trypos->lnum < tryposBrace->lnum
7645 : trypos->col < tryposBrace->col)
7646 trypos = NULL;
7647 else
7648 tryposBrace = NULL;
7649 }
7650
7651 if (trypos != NULL)
7652 {
7653 /*
7654 * If the matching paren is more than one line away, use the indent of
7655 * a previous non-empty line that matches the same paren.
7656 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007657 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007659 /* Line up with the start of the matching paren line. */
7660 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7661 }
7662 else
7663 {
7664 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007665 our_paren_pos = *trypos;
7666 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007668 l = skipwhite(ml_get(lnum));
7669 if (cin_nocode(l)) /* skip comment lines */
7670 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007671 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007672 continue; /* ignore #define, #if, etc. */
7673 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007675 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007676 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007677 {
7678 lnum = trypos->lnum + 1;
7679 continue;
7680 }
7681
7682 /* XXX */
7683 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007684 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007685 && trypos->lnum == our_paren_pos.lnum
7686 && trypos->col == our_paren_pos.col)
7687 {
7688 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007690 if (theline[0] == ')')
7691 {
7692 if (our_paren_pos.lnum != lnum
7693 && cur_amount > amount)
7694 cur_amount = amount;
7695 amount = -1;
7696 }
7697 break;
7698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 }
7700 }
7701
7702 /*
7703 * Line up with line where the matching paren is. XXX
7704 * If the line starts with a '(' or the indent for unclosed
7705 * parentheses is zero, line up with the unclosed parentheses.
7706 */
7707 if (amount == -1)
7708 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007709 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007710 int is_if_for_while = 0;
7711
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007712 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007713 {
7714 /* Look for the outermost opening parenthesis on this line
7715 * and check whether it belongs to an "if", "for" or "while". */
7716
7717 pos_T cursor_save = curwin->w_cursor;
7718 pos_T outermost;
7719 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007720
7721 trypos = &our_paren_pos;
7722 do {
7723 outermost = *trypos;
7724 curwin->w_cursor.lnum = outermost.lnum;
7725 curwin->w_cursor.col = outermost.col;
7726
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007727 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007728 } while (trypos && trypos->lnum == outermost.lnum);
7729
7730 curwin->w_cursor = cursor_save;
7731
7732 line = ml_get(outermost.lnum);
7733
7734 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007735 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007736 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007737
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007738 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007739 look = skipwhite(look);
7740 if (*look == '(')
7741 {
7742 linenr_T save_lnum = curwin->w_cursor.lnum;
7743 char_u *line;
7744 int look_col;
7745
7746 /* Ignore a '(' in front of the line that has a match before
7747 * our matching '('. */
7748 curwin->w_cursor.lnum = our_paren_pos.lnum;
7749 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007750 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007751 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007752 if ((trypos = findmatchlimit(NULL, ')', 0,
7753 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007754 != NULL
7755 && trypos->lnum == our_paren_pos.lnum
7756 && trypos->col < our_paren_pos.col)
7757 ignore_paren_col = trypos->col + 1;
7758
7759 curwin->w_cursor.lnum = save_lnum;
7760 look = ml_get(our_paren_pos.lnum) + look_col;
7761 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007762 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7763 && is_if_for_while == 0)
7764 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007765 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 {
7767 /*
7768 * If we're looking at a close paren, line up right there;
7769 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007770 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 * the last nonwhite character of the line, use either the
7772 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007773 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 * lines).
7775 */
7776 if (theline[0] != ')')
7777 {
7778 cur_amount = MAXCOL;
7779 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007780 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 && cin_ends_in(l, (char_u *)"(", NULL))
7782 {
7783 /* look for opening unmatched paren, indent one level
7784 * for each additional level */
7785 n = 1;
7786 for (col = 0; col < our_paren_pos.col; ++col)
7787 {
7788 switch (l[col])
7789 {
7790 case '(':
7791 case '{': ++n;
7792 break;
7793
7794 case ')':
7795 case '}': if (n > 1)
7796 --n;
7797 break;
7798 }
7799 }
7800
7801 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007802 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007804 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 our_paren_pos.col++;
7806 else
7807 {
7808 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007809 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 col++;
7811 if (l[col] != NUL) /* In case of trailing space */
7812 our_paren_pos.col = col;
7813 else
7814 our_paren_pos.col++;
7815 }
7816 }
7817
7818 /*
7819 * Find how indented the paren is, or the character after it
7820 * if we did the above "if".
7821 */
7822 if (our_paren_pos.col > 0)
7823 {
7824 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7825 if (cur_amount > (int)col)
7826 cur_amount = col;
7827 }
7828 }
7829
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007830 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {
7832 /* Line up with the start of the matching paren line. */
7833 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007834 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7835 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007836 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 {
7838 if (cur_amount != MAXCOL)
7839 amount = cur_amount;
7840 }
7841 else
7842 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007843 /* Add b_ind_unclosed2 for each '(' before our matching one,
7844 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007846 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
7848 --our_paren_pos.col;
7849 switch (*ml_get_pos(&our_paren_pos))
7850 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007851 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 col = our_paren_pos.col;
7853 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007854 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 col = MAXCOL;
7856 break;
7857 }
7858 }
7859
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007860 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 * braces */
7862 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007863 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 else
7865 {
7866 curwin->w_cursor.lnum = our_paren_pos.lnum;
7867 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007868 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7869 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007870 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007872 {
7873 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007874 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007875 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007876 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 }
7879 /*
7880 * For a line starting with ')' use the minimum of the two
7881 * positions, to avoid giving it more indent than the previous
7882 * lines:
7883 * func_long_name( if (x
7884 * arg && yy
7885 * ) ^ not here ) ^ not here
7886 */
7887 if (cur_amount < amount)
7888 amount = cur_amount;
7889 }
7890 }
7891
7892 /* add extra indent for a comment */
7893 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007894 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 else
7897 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007898 /*
7899 * We are inside braces, there is a { before this line at the position
7900 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007901 * Make a copy of tryposBrace, it may point to pos_copy inside
7902 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007903 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007904 tryposCopy = *tryposBrace;
7905 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 ourscope = trypos->lnum;
7908 start = ml_get(ourscope);
7909
7910 /*
7911 * Now figure out how indented the line is in general.
7912 * If the brace was at the start of the line, we use that;
7913 * otherwise, check out the indentation of the line as
7914 * a whole and then add the "imaginary indent" to that.
7915 */
7916 look = skipwhite(start);
7917 if (*look == '{')
7918 {
7919 getvcol(curwin, trypos, &col, NULL, NULL);
7920 amount = col;
7921 if (*start == '{')
7922 start_brace = BRACE_IN_COL0;
7923 else
7924 start_brace = BRACE_AT_START;
7925 }
7926 else
7927 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007928 /* That opening brace might have been on a continuation
7929 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 curwin->w_cursor.lnum = ourscope;
7931
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007932 /* Position the cursor over the rightmost paren, so that
7933 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 lnum = ourscope;
7935 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007936 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7937 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 lnum = trypos->lnum;
7939
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007940 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 * case 1: if (asdf &&
7942 * ldfd) {
7943 * }
7944 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007945 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7946 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007948 else if (curbuf->b_ind_js)
7949 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007951 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952
7953 start_brace = BRACE_AT_END;
7954 }
7955
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007956 /* For Javascript check if the line starts with "key:". */
7957 if (curbuf->b_ind_js)
7958 js_cur_has_key = cin_has_js_key(theline);
7959
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007961 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 * we want to be. otherwise, add the amount of room
7963 * that an indent is supposed to be.
7964 */
7965 if (theline[0] == '}')
7966 {
7967 /*
7968 * they may want closing braces to line up with something
7969 * other than the open brace. indulge them, if so.
7970 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007971 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 }
7973 else
7974 {
7975 /*
7976 * If we're looking at an "else", try to find an "if"
7977 * to match it with.
7978 * If we're looking at a "while", try to find a "do"
7979 * to match it with.
7980 */
7981 lookfor = LOOKFOR_INITIAL;
7982 if (cin_iselse(theline))
7983 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007984 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 lookfor = LOOKFOR_DO;
7986 if (lookfor != LOOKFOR_INITIAL)
7987 {
7988 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007989 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {
7991 amount = get_indent(); /* XXX */
7992 goto theend;
7993 }
7994 }
7995
7996 /*
7997 * We get here if we are not on an "while-of-do" or "else" (or
7998 * failed to find a matching "if").
7999 * Search backwards for something to line up with.
8000 * First set amount for when we don't find anything.
8001 */
8002
8003 /*
8004 * if the '{' is _really_ at the left margin, use the imaginary
8005 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008006 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 */
8008
8009 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8010 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008011 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008012 lookfor_cpp_namespace = TRUE;
8013 }
8014 else if (start_brace == BRACE_AT_START &&
8015 lookfor_cpp_namespace) /* '{' is at start */
8016 {
8017
8018 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 }
8020 else
8021 {
8022 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008023 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008024 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008025
8026 l = skipwhite(ml_get_curline());
8027 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008028 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008029 else if (cin_is_cpp_extern_c(l))
8030 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 else
8033 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008034 /* Compensate for adding b_ind_open_extra later. */
8035 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 if (amount < 0)
8037 amount = 0;
8038 }
8039 }
8040
8041 lookfor_break = FALSE;
8042
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008043 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {
8045 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008046 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 }
8048 else if (cin_isscopedecl(theline)) /* private:, ... */
8049 {
8050 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008051 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053 else
8054 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008055 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8056 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 lookfor_break = TRUE;
8058
8059 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008060 /* b_ind_level from start of block */
8061 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 }
8063 scope_amount = amount;
8064 whilelevel = 0;
8065
8066 /*
8067 * Search backwards. If we find something we recognize, line up
8068 * with that.
8069 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008070 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 * the usual amount relative to the conditional
8072 * that opens the block.
8073 */
8074 curwin->w_cursor = cur_curpos;
8075 for (;;)
8076 {
8077 curwin->w_cursor.lnum--;
8078 curwin->w_cursor.col = 0;
8079
8080 /*
8081 * If we went all the way back to the start of our scope, line
8082 * up with it.
8083 */
8084 if (curwin->w_cursor.lnum <= ourscope)
8085 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008086 /* We reached end of scope:
8087 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008089 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 * don't add ind_continuation, otherwise it is a variable
8091 * declaration:
8092 * int x,
8093 * here; <-- add ind_continuation
8094 */
8095 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8096 {
8097 if (curwin->w_cursor.lnum == 0
8098 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008099 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008101 /* nothing found (abuse curbuf->b_ind_maxparen as
8102 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 * initialization) */
8104 if (cont_amount > 0)
8105 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008106 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 amount += ind_continuation;
8108 break;
8109 }
8110
8111 l = ml_get_curline();
8112
8113 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008114 * If we're in a comment or raw string now, skip to
8115 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008117 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 if (trypos != NULL)
8119 {
8120 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008121 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 continue;
8123 }
8124
8125 /*
8126 * Skip preprocessor directives and blank lines.
8127 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008128 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8129 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 continue;
8131
8132 if (cin_nocode(l))
8133 continue;
8134
8135 terminated = cin_isterminated(l, FALSE, TRUE);
8136
8137 /*
8138 * If we are at top level and the line looks like a
8139 * function declaration, we are done
8140 * (it's a variable declaration).
8141 */
8142 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008143 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {
8145 /* if the line is terminated with another ','
8146 * it is a continued variable initialization.
8147 * don't add extra indent.
8148 * TODO: does not work, if a function
8149 * declaration is split over multiple lines:
8150 * cin_isfuncdecl returns FALSE then.
8151 */
8152 if (terminated == ',')
8153 break;
8154
8155 /* if it es a enum declaration or an assignment,
8156 * we are done.
8157 */
8158 if (terminated != ';' && cin_isinit())
8159 break;
8160
8161 /* nothing useful found */
8162 if (terminated == 0 || terminated == '{')
8163 continue;
8164 }
8165
8166 if (terminated != ';')
8167 {
8168 /* Skip parens and braces. Position the cursor
8169 * over the rightmost paren, so that matching it
8170 * will take us back to the start of the line.
8171 */ /* XXX */
8172 trypos = NULL;
8173 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008174 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008175 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176
8177 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008178 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179
8180 if (trypos != NULL)
8181 {
8182 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008183 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 continue;
8185 }
8186 }
8187
8188 /* it's a variable declaration, add indentation
8189 * like in
8190 * int a,
8191 * b;
8192 */
8193 if (cont_amount > 0)
8194 amount = cont_amount;
8195 else
8196 amount += ind_continuation;
8197 }
8198 else if (lookfor == LOOKFOR_UNTERM)
8199 {
8200 if (cont_amount > 0)
8201 amount = cont_amount;
8202 else
8203 amount += ind_continuation;
8204 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008205 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008206 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008207 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008208 && lookfor != LOOKFOR_CPP_BASECLASS
8209 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008210 {
8211 amount = scope_amount;
8212 if (theline[0] == '{')
8213 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008214 amount += curbuf->b_ind_open_extra;
8215 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008216 }
8217 }
8218
8219 if (lookfor_cpp_namespace)
8220 {
8221 /*
8222 * Looking for C++ namespace, need to look further
8223 * back.
8224 */
8225 if (curwin->w_cursor.lnum == ourscope)
8226 continue;
8227
8228 if (curwin->w_cursor.lnum == 0
8229 || curwin->w_cursor.lnum
8230 < ourscope - FIND_NAMESPACE_LIM)
8231 break;
8232
8233 l = ml_get_curline();
8234
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008235 /* If we're in a comment or raw string now, skip
8236 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008237 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008238 if (trypos != NULL)
8239 {
8240 curwin->w_cursor.lnum = trypos->lnum + 1;
8241 curwin->w_cursor.col = 0;
8242 continue;
8243 }
8244
8245 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008246 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8247 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008248 continue;
8249
8250 /* Finally the actual check for "namespace". */
8251 if (cin_is_cpp_namespace(l))
8252 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008253 amount += curbuf->b_ind_cpp_namespace
8254 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008255 break;
8256 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008257 else if (cin_is_cpp_extern_c(l))
8258 {
8259 amount += curbuf->b_ind_cpp_extern_c
8260 - added_to_amount;
8261 break;
8262 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008263
8264 if (cin_nocode(l))
8265 continue;
8266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 }
8268 break;
8269 }
8270
8271 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008272 * If we're in a comment or raw string now, skip to the start
8273 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008275 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {
8277 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008278 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 continue;
8280 }
8281
8282 l = ml_get_curline();
8283
8284 /*
8285 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008286 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008288 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 if (iscase || cin_isscopedecl(l))
8290 {
8291 /* we are only looking for cpp base class
8292 * declaration/initialization any longer */
8293 if (lookfor == LOOKFOR_CPP_BASECLASS)
8294 break;
8295
8296 /* When looking for a "do" we are not interested in
8297 * labels. */
8298 if (whilelevel > 0)
8299 continue;
8300
8301 /*
8302 * case xx:
8303 * c = 99 + <- this indent plus continuation
8304 *-> here;
8305 */
8306 if (lookfor == LOOKFOR_UNTERM
8307 || lookfor == LOOKFOR_ENUM_OR_INIT)
8308 {
8309 if (cont_amount > 0)
8310 amount = cont_amount;
8311 else
8312 amount += ind_continuation;
8313 break;
8314 }
8315
8316 /*
8317 * case xx: <- line up with this case
8318 * x = 333;
8319 * case yy:
8320 */
8321 if ( (iscase && lookfor == LOOKFOR_CASE)
8322 || (iscase && lookfor_break)
8323 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8324 {
8325 /*
8326 * Check that this case label is not for another
8327 * switch()
8328 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008329 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008330 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {
8332 amount = get_indent(); /* XXX */
8333 break;
8334 }
8335 continue;
8336 }
8337
8338 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8339
8340 /*
8341 * case xx: if (cond) <- line up with this if
8342 * y = y + 1;
8343 * -> s = 99;
8344 *
8345 * case xx:
8346 * if (cond) <- line up with this line
8347 * y = y + 1;
8348 * -> s = 99;
8349 */
8350 if (lookfor == LOOKFOR_TERM)
8351 {
8352 if (n)
8353 amount = n;
8354
8355 if (!lookfor_break)
8356 break;
8357 }
8358
8359 /*
8360 * case xx: x = x + 1; <- line up with this x
8361 * -> y = y + 1;
8362 *
8363 * case xx: if (cond) <- line up with this if
8364 * -> y = y + 1;
8365 */
8366 if (n)
8367 {
8368 amount = n;
8369 l = after_label(ml_get_curline());
8370 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008371 {
8372 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008373 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008374 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008375 amount += curbuf->b_ind_level
8376 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 break;
8379 }
8380
8381 /*
8382 * Try to get the indent of a statement before the switch
8383 * label. If nothing is found, line up relative to the
8384 * switch label.
8385 * break; <- may line up with this line
8386 * case xx:
8387 * -> y = 1;
8388 */
8389 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008390 ? curbuf->b_ind_case_code
8391 : curbuf->b_ind_scopedecl_code);
8392 lookfor = curbuf->b_ind_case_break
8393 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 continue;
8395 }
8396
8397 /*
8398 * Looking for a switch() label or C++ scope declaration,
8399 * ignore other lines, skip {}-blocks.
8400 */
8401 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8402 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008403 if (find_last_paren(l, '{', '}')
8404 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008407 curwin->w_cursor.col = 0;
8408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 continue;
8410 }
8411
8412 /*
8413 * Ignore jump labels with nothing after them.
8414 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008415 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {
8417 l = after_label(ml_get_curline());
8418 if (l == NULL || cin_nocode(l))
8419 continue;
8420 }
8421
8422 /*
8423 * Ignore #defines, #if, etc.
8424 * Ignore comment and empty lines.
8425 * (need to get the line again, cin_islabel() may have
8426 * unlocked it)
8427 */
8428 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008429 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 || cin_nocode(l))
8431 continue;
8432
8433 /*
8434 * Are we at the start of a cpp base class declaration or
8435 * constructor initialization?
8436 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008437 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008438 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008439 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008440 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008441 l = ml_get_curline();
8442 }
8443 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {
8445 if (lookfor == LOOKFOR_UNTERM)
8446 {
8447 if (cont_amount > 0)
8448 amount = cont_amount;
8449 else
8450 amount += ind_continuation;
8451 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008452 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008454 /* Need to find start of the declaration. */
8455 lookfor = LOOKFOR_UNTERM;
8456 ind_continuation = 0;
8457 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 }
8459 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008460 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008461 amount = get_baseclass_amount(
8462 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 break;
8464 }
8465 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8466 {
8467 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008468 * declaration or initialization before the opening brace.
8469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 if (cin_isterminated(l, TRUE, FALSE))
8471 break;
8472 else
8473 continue;
8474 }
8475
8476 /*
8477 * What happens next depends on the line being terminated.
8478 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008479 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 * 123,
8481 * sizeof
8482 * here
8483 * Otherwise check whether it is a enumeration or structure
8484 * initialisation (not indented) or a variable declaration
8485 * (indented).
8486 */
8487 terminated = cin_isterminated(l, FALSE, TRUE);
8488
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008489 if (js_cur_has_key)
8490 {
8491 js_cur_has_key = 0; /* only check the first line */
8492 if (curbuf->b_ind_js && terminated == ',')
8493 {
8494 /* For Javascript we might be inside an object:
8495 * key: something, <- align with this
8496 * key: something
8497 * or:
8498 * key: something + <- align with this
8499 * something,
8500 * key: something
8501 */
8502 lookfor = LOOKFOR_JS_KEY;
8503 }
8504 }
8505 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8506 {
8507 amount = get_indent();
8508 break;
8509 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008510 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008511 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008512 if (tryposBrace != NULL && tryposBrace->lnum
8513 >= curwin->w_cursor.lnum)
8514 break;
8515 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008516 /* line below current line is the one that starts a
8517 * (possibly broken) line ending in a comma. */
8518 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008519 else
8520 {
8521 amount = get_indent();
8522 if (curwin->w_cursor.lnum - 1 == ourscope)
8523 /* line above is start of the scope, thus current
8524 * line is the one that stars a (possibly broken)
8525 * line ending in a comma. */
8526 break;
8527 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008528 }
8529
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8531 && terminated == ','))
8532 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008533 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8534 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008535 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 /*
8537 * if we're in the middle of a paren thing,
8538 * go back to the line that starts it so
8539 * we can get the right prevailing indent
8540 * if ( foo &&
8541 * bar )
8542 */
8543 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008544 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008546 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 */
8548 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008549 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008550 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8551 || (trypos->lnum == tryposBrace->lnum
8552 && trypos->col < tryposBrace->col)))
8553 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554
8555 /*
8556 * If we are looking for ',', we also look for matching
8557 * braces.
8558 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008559 if (trypos == NULL && terminated == ','
8560 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008561 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562
8563 if (trypos != NULL)
8564 {
8565 /*
8566 * Check if we are on a case label now. This is
8567 * handled above.
8568 * case xx: if ( asdf &&
8569 * asdf)
8570 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008571 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008573 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 {
8575 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008576 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 continue;
8578 }
8579 }
8580
8581 /*
8582 * Skip over continuation lines to find the one to get the
8583 * indent from
8584 * char *usethis = "bla\
8585 * bla",
8586 * here;
8587 */
8588 if (terminated == ',')
8589 {
8590 while (curwin->w_cursor.lnum > 1)
8591 {
8592 l = ml_get(curwin->w_cursor.lnum - 1);
8593 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8594 break;
8595 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008596 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 }
8598 }
8599
8600 /*
8601 * Get indent and pointer to text for current line,
8602 * ignoring any jump label. XXX
8603 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008604 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008605 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008606 else
8607 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 /*
8609 * If this is just above the line we are indenting, and it
8610 * starts with a '{', line it up with this line.
8611 * while (not)
8612 * -> {
8613 * }
8614 */
8615 if (terminated != ',' && lookfor != LOOKFOR_TERM
8616 && theline[0] == '{')
8617 {
8618 amount = cur_amount;
8619 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008620 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 * doesn't start with a '{', which must have a match
8622 * in the same line (scope is the same). Probably:
8623 * { 1, 2 },
8624 * -> { 3, 4 }
8625 */
8626 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008627 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008629 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {
8631 /* have to look back, whether it is a cpp base
8632 * class declaration or initialization */
8633 lookfor = LOOKFOR_CPP_BASECLASS;
8634 continue;
8635 }
8636 break;
8637 }
8638
8639 /*
8640 * Check if we are after an "if", "while", etc.
8641 * Also allow " } else".
8642 */
8643 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8644 {
8645 /*
8646 * Found an unterminated line after an if (), line up
8647 * with the last one.
8648 * if (cond)
8649 * 100 +
8650 * -> here;
8651 */
8652 if (lookfor == LOOKFOR_UNTERM
8653 || lookfor == LOOKFOR_ENUM_OR_INIT)
8654 {
8655 if (cont_amount > 0)
8656 amount = cont_amount;
8657 else
8658 amount += ind_continuation;
8659 break;
8660 }
8661
8662 /*
8663 * If this is just above the line we are indenting, we
8664 * are finished.
8665 * while (not)
8666 * -> here;
8667 * Otherwise this indent can be used when the line
8668 * before this is terminated.
8669 * yyy;
8670 * if (stat)
8671 * while (not)
8672 * xxx;
8673 * -> here;
8674 */
8675 amount = cur_amount;
8676 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008677 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 if (lookfor != LOOKFOR_TERM)
8679 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008680 amount += curbuf->b_ind_level
8681 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 break;
8683 }
8684
8685 /*
8686 * Special trick: when expecting the while () after a
8687 * do, line up with the while()
8688 * do
8689 * x = 1;
8690 * -> here
8691 */
8692 l = skipwhite(ml_get_curline());
8693 if (cin_isdo(l))
8694 {
8695 if (whilelevel == 0)
8696 break;
8697 --whilelevel;
8698 }
8699
8700 /*
8701 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008702 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 * Need to use the scope of this "else". XXX
8704 * If whilelevel != 0 continue looking for a "do {".
8705 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008706 if (cin_iselse(l) && whilelevel == 0)
8707 {
8708 /* If we're looking at "} else", let's make sure we
8709 * find the opening brace of the enclosing scope,
8710 * not the one from "if () {". */
8711 if (*l == '}')
8712 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008713 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008714
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008715 if ((trypos = find_start_brace()) == NULL
8716 || find_match(LOOKFOR_IF, trypos->lnum)
8717 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008718 break;
8719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 }
8721
8722 /*
8723 * If we're below an unterminated line that is not an
8724 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008725 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 * the line before this one.
8727 */
8728 else
8729 {
8730 /*
8731 * Found two unterminated lines on a row, line up with
8732 * the last one.
8733 * c = 99 +
8734 * 100 +
8735 * -> here;
8736 */
8737 if (lookfor == LOOKFOR_UNTERM)
8738 {
8739 /* When line ends in a comma add extra indent */
8740 if (terminated == ',')
8741 amount += ind_continuation;
8742 break;
8743 }
8744
8745 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8746 {
8747 /* Found two lines ending in ',', lineup with the
8748 * lowest one, but check for cpp base class
8749 * declaration/initialization, if it is an
8750 * opening brace or we are looking just for
8751 * enumerations/initializations. */
8752 if (terminated == ',')
8753 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008754 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 break;
8756
8757 lookfor = LOOKFOR_CPP_BASECLASS;
8758 continue;
8759 }
8760
8761 /* Ignore unterminated lines in between, but
8762 * reduce indent. */
8763 if (amount > cur_amount)
8764 amount = cur_amount;
8765 }
8766 else
8767 {
8768 /*
8769 * Found first unterminated line on a row, may
8770 * line up with this line, remember its indent
8771 * 100 +
8772 * -> here;
8773 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008774 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008776
8777 n = (int)STRLEN(l);
8778 if (terminated == ',' && (*skipwhite(l) == ']'
8779 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008780 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781
8782 /*
8783 * If previous line ends in ',', check whether we
8784 * are in an initialization or enum
8785 * struct xxx =
8786 * {
8787 * sizeof a,
8788 * 124 };
8789 * or a normal possible continuation line.
8790 * but only, of no other statement has been found
8791 * yet.
8792 */
8793 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8794 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008795 if (curbuf->b_ind_js)
8796 {
8797 /* Search for a line ending in a comma
8798 * and line up with the line below it
8799 * (could be the current line).
8800 * some = [
8801 * 1, <- line up here
8802 * 2,
8803 * some = [
8804 * 3 + <- line up here
8805 * 4 *
8806 * 5,
8807 * 6,
8808 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008809 if (cin_iscomment(skipwhite(l)))
8810 break;
8811 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008812 trypos = find_match_char('[',
8813 curbuf->b_ind_maxparen);
8814 if (trypos != NULL)
8815 {
8816 if (trypos->lnum
8817 == curwin->w_cursor.lnum - 1)
8818 {
8819 /* Current line is first inside
8820 * [], line up with it. */
8821 break;
8822 }
8823 ourscope = trypos->lnum;
8824 }
8825 }
8826 else
8827 {
8828 lookfor = LOOKFOR_ENUM_OR_INIT;
8829 cont_amount = cin_first_id_amount();
8830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 }
8832 else
8833 {
8834 if (lookfor == LOOKFOR_INITIAL
8835 && *l != NUL
8836 && l[STRLEN(l) - 1] == '\\')
8837 /* XXX */
8838 cont_amount = cin_get_equal_amount(
8839 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008840 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008841 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008842 && lookfor != LOOKFOR_COMMA
8843 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 lookfor = LOOKFOR_UNTERM;
8845 }
8846 }
8847 }
8848 }
8849
8850 /*
8851 * Check if we are after a while (cond);
8852 * If so: Ignore until the matching "do".
8853 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008854 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 {
8856 /*
8857 * Found an unterminated line after a while ();, line up
8858 * with the last one.
8859 * while (cond);
8860 * 100 + <- line up with this one
8861 * -> here;
8862 */
8863 if (lookfor == LOOKFOR_UNTERM
8864 || lookfor == LOOKFOR_ENUM_OR_INIT)
8865 {
8866 if (cont_amount > 0)
8867 amount = cont_amount;
8868 else
8869 amount += ind_continuation;
8870 break;
8871 }
8872
8873 if (whilelevel == 0)
8874 {
8875 lookfor = LOOKFOR_TERM;
8876 amount = get_indent(); /* XXX */
8877 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008878 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 }
8880 ++whilelevel;
8881 }
8882
8883 /*
8884 * We are after a "normal" statement.
8885 * If we had another statement we can stop now and use the
8886 * indent of that other statement.
8887 * Otherwise the indent of the current statement may be used,
8888 * search backwards for the next "normal" statement.
8889 */
8890 else
8891 {
8892 /*
8893 * Skip single break line, if before a switch label. It
8894 * may be lined up with the case label.
8895 */
8896 if (lookfor == LOOKFOR_NOBREAK
8897 && cin_isbreak(skipwhite(ml_get_curline())))
8898 {
8899 lookfor = LOOKFOR_ANY;
8900 continue;
8901 }
8902
8903 /*
8904 * Handle "do {" line.
8905 */
8906 if (whilelevel > 0)
8907 {
8908 l = cin_skipcomment(ml_get_curline());
8909 if (cin_isdo(l))
8910 {
8911 amount = get_indent(); /* XXX */
8912 --whilelevel;
8913 continue;
8914 }
8915 }
8916
8917 /*
8918 * Found a terminated line above an unterminated line. Add
8919 * the amount for a continuation line.
8920 * x = 1;
8921 * y = foo +
8922 * -> here;
8923 * or
8924 * int x = 1;
8925 * int foo,
8926 * -> here;
8927 */
8928 if (lookfor == LOOKFOR_UNTERM
8929 || lookfor == LOOKFOR_ENUM_OR_INIT)
8930 {
8931 if (cont_amount > 0)
8932 amount = cont_amount;
8933 else
8934 amount += ind_continuation;
8935 break;
8936 }
8937
8938 /*
8939 * Found a terminated line above a terminated line or "if"
8940 * etc. line. Use the amount of the line below us.
8941 * x = 1; x = 1;
8942 * if (asdf) y = 2;
8943 * while (asdf) ->here;
8944 * here;
8945 * ->foo;
8946 */
8947 if (lookfor == LOOKFOR_TERM)
8948 {
8949 if (!lookfor_break && whilelevel == 0)
8950 break;
8951 }
8952
8953 /*
8954 * First line above the one we're indenting is terminated.
8955 * To know what needs to be done look further backward for
8956 * a terminated line.
8957 */
8958 else
8959 {
8960 /*
8961 * position the cursor over the rightmost paren, so
8962 * that matching it will take us back to the start of
8963 * the line. Helps for:
8964 * func(asdr,
8965 * asdfasdf);
8966 * here;
8967 */
8968term_again:
8969 l = ml_get_curline();
8970 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008971 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008972 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 {
8974 /*
8975 * Check if we are on a case label now. This is
8976 * handled above.
8977 * case xx: if ( asdf &&
8978 * asdf)
8979 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008980 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008982 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 {
8984 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008985 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 continue;
8987 }
8988 }
8989
8990 /* When aligning with the case statement, don't align
8991 * with a statement after it.
8992 * case 1: { <-- don't use this { position
8993 * stat;
8994 * }
8995 * case 2:
8996 * stat;
8997 * }
8998 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008999 iscase = (curbuf->b_ind_keep_case_label
9000 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001
9002 /*
9003 * Get indent and pointer to text for current line,
9004 * ignoring any jump label.
9005 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009006 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007
9008 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009009 amount += curbuf->b_ind_open_extra;
9010 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009011 l = skipwhite(l);
9012 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009013 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9015
9016 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009017 * When a terminated line starts with "else" skip to
9018 * the matching "if":
9019 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009020 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009021 * Need to use the scope of this "else". XXX
9022 * If whilelevel != 0 continue looking for a "do {".
9023 */
9024 if (lookfor == LOOKFOR_TERM
9025 && *l != '}'
9026 && cin_iselse(l)
9027 && whilelevel == 0)
9028 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009029 if ((trypos = find_start_brace()) == NULL
9030 || find_match(LOOKFOR_IF, trypos->lnum)
9031 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009032 break;
9033 continue;
9034 }
9035
9036 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 * If we're at the end of a block, skip to the start of
9038 * that block.
9039 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009040 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009041 if (find_last_paren(l, '{', '}') /* XXX */
9042 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009044 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 /* if not "else {" check for terminated again */
9046 /* but skip block for "} else {" */
9047 l = cin_skipcomment(ml_get_curline());
9048 if (*l == '}' || !cin_iselse(l))
9049 goto term_again;
9050 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009051 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 }
9053 }
9054 }
9055 }
9056 }
9057 }
9058
9059 /* add extra indent for a comment */
9060 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009061 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009062
9063 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009064 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9065 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009066
9067 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009069
9070 /*
9071 * ok -- we're not inside any sort of structure at all!
9072 *
9073 * This means we're at the top level, and everything should
9074 * basically just match where the previous line is, except
9075 * for the lines immediately following a function declaration,
9076 * which are K&R-style parameters and need to be indented.
9077 *
9078 * if our line starts with an open brace, forget about any
9079 * prevailing indent and make sure it looks like the start
9080 * of a function
9081 */
9082
9083 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009085 amount = curbuf->b_ind_first_open;
9086 goto theend;
9087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009089 /*
9090 * If the NEXT line is a function declaration, the current
9091 * line needs to be indented as a function type spec.
9092 * Don't do this if the current line looks like a comment or if the
9093 * current line is terminated, ie. ends in ';', or if the current line
9094 * contains { or }: "void f() {\n if (1)"
9095 */
9096 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9097 && !cin_nocode(theline)
9098 && vim_strchr(theline, '{') == NULL
9099 && vim_strchr(theline, '}') == NULL
9100 && !cin_ends_in(theline, (char_u *)":", NULL)
9101 && !cin_ends_in(theline, (char_u *)",", NULL)
9102 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9103 cur_curpos.lnum + 1)
9104 && !cin_isterminated(theline, FALSE, TRUE))
9105 {
9106 amount = curbuf->b_ind_func_type;
9107 goto theend;
9108 }
9109
9110 /* search backwards until we find something we recognize */
9111 amount = 0;
9112 curwin->w_cursor = cur_curpos;
9113 while (curwin->w_cursor.lnum > 1)
9114 {
9115 curwin->w_cursor.lnum--;
9116 curwin->w_cursor.col = 0;
9117
9118 l = ml_get_curline();
9119
9120 /*
9121 * If we're in a comment or raw string now, skip to the start
9122 * of it.
9123 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009124 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009126 curwin->w_cursor.lnum = trypos->lnum + 1;
9127 curwin->w_cursor.col = 0;
9128 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 }
9130
9131 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009132 * Are we at the start of a cpp base class declaration or
9133 * constructor initialization?
9134 */ /* XXX */
9135 n = FALSE;
9136 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009138 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9139 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009141 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009143 /* XXX */
9144 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9145 break;
9146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009148 /*
9149 * Skip preprocessor directives and blank lines.
9150 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009151 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009152 continue;
9153
9154 if (cin_nocode(l))
9155 continue;
9156
9157 /*
9158 * If the previous line ends in ',', use one level of
9159 * indentation:
9160 * int foo,
9161 * bar;
9162 * do this before checking for '}' in case of eg.
9163 * enum foobar
9164 * {
9165 * ...
9166 * } foo,
9167 * bar;
9168 */
9169 n = 0;
9170 if (cin_ends_in(l, (char_u *)",", NULL)
9171 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9172 {
9173 /* take us back to opening paren */
9174 if (find_last_paren(l, '(', ')')
9175 && (trypos = find_match_paren(
9176 curbuf->b_ind_maxparen)) != NULL)
9177 curwin->w_cursor = *trypos;
9178
9179 /* For a line ending in ',' that is a continuation line go
9180 * back to the first line with a backslash:
9181 * char *foo = "bla\
9182 * bla",
9183 * here;
9184 */
9185 while (n == 0 && curwin->w_cursor.lnum > 1)
9186 {
9187 l = ml_get(curwin->w_cursor.lnum - 1);
9188 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9189 break;
9190 --curwin->w_cursor.lnum;
9191 curwin->w_cursor.col = 0;
9192 }
9193
9194 amount = get_indent(); /* XXX */
9195
9196 if (amount == 0)
9197 amount = cin_first_id_amount();
9198 if (amount == 0)
9199 amount = ind_continuation;
9200 break;
9201 }
9202
9203 /*
9204 * If the line looks like a function declaration, and we're
9205 * not in a comment, put it the left margin.
9206 */
9207 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9208 break;
9209 l = ml_get_curline();
9210
9211 /*
9212 * Finding the closing '}' of a previous function. Put
9213 * current line at the left margin. For when 'cino' has "fs".
9214 */
9215 if (*skipwhite(l) == '}')
9216 break;
9217
9218 /* (matching {)
9219 * If the previous line ends on '};' (maybe followed by
9220 * comments) align at column 0. For example:
9221 * char *string_array[] = { "foo",
9222 * / * x * / "b};ar" }; / * foobar * /
9223 */
9224 if (cin_ends_in(l, (char_u *)"};", NULL))
9225 break;
9226
9227 /*
9228 * If the previous line ends on '[' we are probably in an
9229 * array constant:
9230 * something = [
9231 * 234, <- extra indent
9232 */
9233 if (cin_ends_in(l, (char_u *)"[", NULL))
9234 {
9235 amount = get_indent() + ind_continuation;
9236 break;
9237 }
9238
9239 /*
9240 * Find a line only has a semicolon that belongs to a previous
9241 * line ending in '}', e.g. before an #endif. Don't increase
9242 * indent then.
9243 */
9244 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9245 {
9246 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247
9248 while (curwin->w_cursor.lnum > 1)
9249 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009250 look = ml_get(--curwin->w_cursor.lnum);
9251 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009252 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009254 }
9255 if (curwin->w_cursor.lnum > 0
9256 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009259 curwin->w_cursor = curpos_save;
9260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009262 /*
9263 * If the PREVIOUS line is a function declaration, the current
9264 * line (and the ones that follow) needs to be indented as
9265 * parameters.
9266 */
9267 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9268 {
9269 amount = curbuf->b_ind_param;
9270 break;
9271 }
9272
9273 /*
9274 * If the previous line ends in ';' and the line before the
9275 * previous line ends in ',' or '\', ident to column zero:
9276 * int foo,
9277 * bar;
9278 * indent_to_0 here;
9279 */
9280 if (cin_ends_in(l, (char_u *)";", NULL))
9281 {
9282 l = ml_get(curwin->w_cursor.lnum - 1);
9283 if (cin_ends_in(l, (char_u *)",", NULL)
9284 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9285 break;
9286 l = ml_get_curline();
9287 }
9288
9289 /*
9290 * Doesn't look like anything interesting -- so just
9291 * use the indent of this line.
9292 *
9293 * Position the cursor over the rightmost paren, so that
9294 * matching it will take us back to the start of the line.
9295 */
9296 find_last_paren(l, '(', ')');
9297
9298 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9299 curwin->w_cursor = *trypos;
9300 amount = get_indent(); /* XXX */
9301 break;
9302 }
9303
9304 /* add extra indent for a comment */
9305 if (cin_iscomment(theline))
9306 amount += curbuf->b_ind_comment;
9307
9308 /* add extra indent if the previous line ended in a backslash:
9309 * "asdfasdf\
9310 * here";
9311 * char *foo = "asdf\
9312 * here";
9313 */
9314 if (cur_curpos.lnum > 1)
9315 {
9316 l = ml_get(cur_curpos.lnum - 1);
9317 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9318 {
9319 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9320 if (cur_amount > 0)
9321 amount = cur_amount;
9322 else if (cur_amount == 0)
9323 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 }
9325 }
9326
9327theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009328 if (amount < 0)
9329 amount = 0;
9330
9331laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 /* put the cursor back where it belongs */
9333 curwin->w_cursor = cur_curpos;
9334
9335 vim_free(linecopy);
9336
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 return amount;
9338}
9339
9340 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009341find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
9343 char_u *look;
9344 pos_T *theirscope;
9345 char_u *mightbeif;
9346 int elselevel;
9347 int whilelevel;
9348
9349 if (lookfor == LOOKFOR_IF)
9350 {
9351 elselevel = 1;
9352 whilelevel = 0;
9353 }
9354 else
9355 {
9356 elselevel = 0;
9357 whilelevel = 1;
9358 }
9359
9360 curwin->w_cursor.col = 0;
9361
9362 while (curwin->w_cursor.lnum > ourscope + 1)
9363 {
9364 curwin->w_cursor.lnum--;
9365 curwin->w_cursor.col = 0;
9366
9367 look = cin_skipcomment(ml_get_curline());
9368 if (cin_iselse(look)
9369 || cin_isif(look)
9370 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009371 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 {
9373 /*
9374 * if we've gone outside the braces entirely,
9375 * we must be out of scope...
9376 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009377 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 if (theirscope == NULL)
9379 break;
9380
9381 /*
9382 * and if the brace enclosing this is further
9383 * back than the one enclosing the else, we're
9384 * out of luck too.
9385 */
9386 if (theirscope->lnum < ourscope)
9387 break;
9388
9389 /*
9390 * and if they're enclosed in a *deeper* brace,
9391 * then we can ignore it because it's in a
9392 * different scope...
9393 */
9394 if (theirscope->lnum > ourscope)
9395 continue;
9396
9397 /*
9398 * if it was an "else" (that's not an "else if")
9399 * then we need to go back to another if, so
9400 * increment elselevel
9401 */
9402 look = cin_skipcomment(ml_get_curline());
9403 if (cin_iselse(look))
9404 {
9405 mightbeif = cin_skipcomment(look + 4);
9406 if (!cin_isif(mightbeif))
9407 ++elselevel;
9408 continue;
9409 }
9410
9411 /*
9412 * if it was a "while" then we need to go back to
9413 * another "do", so increment whilelevel. XXX
9414 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009415 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 {
9417 ++whilelevel;
9418 continue;
9419 }
9420
9421 /* If it's an "if" decrement elselevel */
9422 look = cin_skipcomment(ml_get_curline());
9423 if (cin_isif(look))
9424 {
9425 elselevel--;
9426 /*
9427 * When looking for an "if" ignore "while"s that
9428 * get in the way.
9429 */
9430 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9431 whilelevel = 0;
9432 }
9433
9434 /* If it's a "do" decrement whilelevel */
9435 if (cin_isdo(look))
9436 whilelevel--;
9437
9438 /*
9439 * if we've used up all the elses, then
9440 * this must be the if that we want!
9441 * match the indent level of that if.
9442 */
9443 if (elselevel <= 0 && whilelevel <= 0)
9444 {
9445 return OK;
9446 }
9447 }
9448 }
9449 return FAIL;
9450}
9451
9452# if defined(FEAT_EVAL) || defined(PROTO)
9453/*
9454 * Get indent level from 'indentexpr'.
9455 */
9456 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009457get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009459 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009460 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009461 pos_T save_pos;
9462 colnr_T save_curswant;
9463 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009465 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9466 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009468 /* Save and restore cursor position and curswant, in case it was changed
9469 * via :normal commands */
9470 save_pos = curwin->w_cursor;
9471 save_curswant = curwin->w_curswant;
9472 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009474 if (use_sandbox)
9475 ++sandbox;
9476 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009477
9478 /* Need to make a copy, the 'indentexpr' option could be changed while
9479 * evaluating it. */
9480 inde_copy = vim_strsave(curbuf->b_p_inde);
9481 if (inde_copy != NULL)
9482 {
9483 indent = (int)eval_to_number(inde_copy);
9484 vim_free(inde_copy);
9485 }
9486
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009487 if (use_sandbox)
9488 --sandbox;
9489 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490
9491 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9492 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9493 * command. */
9494 save_State = State;
9495 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009496 curwin->w_cursor = save_pos;
9497 curwin->w_curswant = save_curswant;
9498 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 check_cursor();
9500 State = save_State;
9501
9502 /* If there is an error, just keep the current indent. */
9503 if (indent < 0)
9504 indent = get_indent();
9505
9506 return indent;
9507}
9508# endif
9509
9510#endif /* FEAT_CINDENT */
9511
9512#if defined(FEAT_LISP) || defined(PROTO)
9513
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009515lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
9517 char_u buf[LSIZE];
9518 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009519 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520
9521 while (*word != NUL)
9522 {
9523 (void)copy_option_part(&word, buf, LSIZE, ",");
9524 len = (int)STRLEN(buf);
9525 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9526 return TRUE;
9527 }
9528 return FALSE;
9529}
9530
9531/*
9532 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9533 * The incompatible newer method is quite a bit better at indenting
9534 * code in lisp-like languages than the traditional one; it's still
9535 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9536 *
9537 * TODO:
9538 * Findmatch() should be adapted for lisp, also to make showmatch
9539 * work correctly: now (v5.3) it seems all C/C++ oriented:
9540 * - it does not recognize the #\( and #\) notations as character literals
9541 * - it doesn't know about comments starting with a semicolon
9542 * - it incorrectly interprets '(' as a character literal
9543 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009544 * Update from Sergey Khorev:
9545 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 */
9547 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009548get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009550 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 int amount;
9552 char_u *that;
9553 colnr_T col;
9554 colnr_T firsttry;
9555 int parencount, quotecount;
9556 int vi_lisp;
9557
9558 /* Set vi_lisp to use the vi-compatible method */
9559 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9560
9561 realpos = curwin->w_cursor;
9562 curwin->w_cursor.col = 0;
9563
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009564 if ((pos = findmatch(NULL, '(')) == NULL)
9565 pos = findmatch(NULL, '[');
9566 else
9567 {
9568 paren = *pos;
9569 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009570 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009571 pos = &paren;
9572 }
9573 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 {
9575 /* Extra trick: Take the indent of the first previous non-white
9576 * line that is at the same () level. */
9577 amount = -1;
9578 parencount = 0;
9579
9580 while (--curwin->w_cursor.lnum >= pos->lnum)
9581 {
9582 if (linewhite(curwin->w_cursor.lnum))
9583 continue;
9584 for (that = ml_get_curline(); *that != NUL; ++that)
9585 {
9586 if (*that == ';')
9587 {
9588 while (*(that + 1) != NUL)
9589 ++that;
9590 continue;
9591 }
9592 if (*that == '\\')
9593 {
9594 if (*(that + 1) != NUL)
9595 ++that;
9596 continue;
9597 }
9598 if (*that == '"' && *(that + 1) != NUL)
9599 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009600 while (*++that && *that != '"')
9601 {
9602 /* skipping escaped characters in the string */
9603 if (*that == '\\')
9604 {
9605 if (*++that == NUL)
9606 break;
9607 if (that[1] == NUL)
9608 {
9609 ++that;
9610 break;
9611 }
9612 }
9613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009615 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009617 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 --parencount;
9619 }
9620 if (parencount == 0)
9621 {
9622 amount = get_indent();
9623 break;
9624 }
9625 }
9626
9627 if (amount == -1)
9628 {
9629 curwin->w_cursor.lnum = pos->lnum;
9630 curwin->w_cursor.col = pos->col;
9631 col = pos->col;
9632
9633 that = ml_get_curline();
9634
9635 if (vi_lisp && get_indent() == 0)
9636 amount = 2;
9637 else
9638 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009639 char_u *line = that;
9640
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 amount = 0;
9642 while (*that && col)
9643 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009644 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 col--;
9646 }
9647
9648 /*
9649 * Some keywords require "body" indenting rules (the
9650 * non-standard-lisp ones are Scheme special forms):
9651 *
9652 * (let ((a 1)) instead (let ((a 1))
9653 * (...)) of (...))
9654 */
9655
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009656 if (!vi_lisp && (*that == '(' || *that == '[')
9657 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 amount += 2;
9659 else
9660 {
9661 that++;
9662 amount++;
9663 firsttry = amount;
9664
Bram Moolenaar1c465442017-03-12 20:10:05 +01009665 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009667 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 ++that;
9669 }
9670
9671 if (*that && *that != ';') /* not a comment line */
9672 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009673 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009675 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 firsttry++;
9677
9678 parencount = 0;
9679 quotecount = 0;
9680
9681 if (vi_lisp
9682 || (*that != '"'
9683 && *that != '\''
9684 && *that != '#'
9685 && (*that < '0' || *that > '9')))
9686 {
9687 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009688 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 || quotecount
9690 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009691 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 && !quotecount
9693 && !parencount
9694 && vi_lisp)))
9695 {
9696 if (*that == '"')
9697 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009698 if ((*that == '(' || *that == '[')
9699 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009701 if ((*that == ')' || *that == ']')
9702 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 --parencount;
9704 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009705 amount += lbr_chartabsize_adv(
9706 line, &that, (colnr_T)amount);
9707 amount += lbr_chartabsize_adv(
9708 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 }
9710 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009711 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009713 amount += lbr_chartabsize(
9714 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 that++;
9716 }
9717 if (!*that || *that == ';')
9718 amount = firsttry;
9719 }
9720 }
9721 }
9722 }
9723 }
9724 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009725 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726
9727 curwin->w_cursor = realpos;
9728
9729 return amount;
9730}
9731#endif /* FEAT_LISP */
9732
9733 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009734prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009736#if defined(SIGHUP) && defined(SIG_IGN)
9737 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9738 * makes Vim exit and then handling SIGHUP causes various reentrance
9739 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009740 signal(SIGHUP, SIG_IGN);
9741#endif
9742
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743#ifdef FEAT_GUI
9744 if (gui.in_use)
9745 {
9746 gui.dying = TRUE;
9747 out_trash(); /* trash any pending output */
9748 }
9749 else
9750#endif
9751 {
9752 windgoto((int)Rows - 1, 0);
9753
9754 /*
9755 * Switch terminal mode back now, so messages end up on the "normal"
9756 * screen (if there are two screens).
9757 */
9758 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009759 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 out_flush();
9761 }
9762}
9763
9764/*
9765 * Preserve files and exit.
9766 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009767 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9768 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 */
9770 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009771preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772{
9773 buf_T *buf;
9774
9775 prepare_to_exit();
9776
Bram Moolenaar4770d092006-01-12 23:22:24 +00009777 /* Setting this will prevent free() calls. That avoids calling free()
9778 * recursively when free() was invoked with a bad pointer. */
9779 really_exiting = TRUE;
9780
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 out_str(IObuff);
9782 screen_start(); /* don't know where cursor is now */
9783 out_flush();
9784
9785 ml_close_notmod(); /* close all not-modified buffers */
9786
Bram Moolenaar29323592016-07-24 22:04:11 +02009787 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 {
9789 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9790 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009791 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 screen_start(); /* don't know where cursor is now */
9793 out_flush();
9794 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9795 break;
9796 }
9797 }
9798
9799 ml_close_all(FALSE); /* close all memfiles, without deleting */
9800
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009801 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802
9803 getout(1);
9804}
9805
9806/*
9807 * return TRUE if "fname" exists.
9808 */
9809 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009810vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009812 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813
9814 if (mch_stat((char *)fname, &st))
9815 return FALSE;
9816 return TRUE;
9817}
9818
9819/*
9820 * Check for CTRL-C pressed, but only once in a while.
9821 * Should be used instead of ui_breakcheck() for functions that check for
9822 * each line in the file. Calling ui_breakcheck() each time takes too much
9823 * time, because it can be a system call.
9824 */
9825
9826#ifndef BREAKCHECK_SKIP
9827# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9828# define BREAKCHECK_SKIP 200
9829# else
9830# define BREAKCHECK_SKIP 32
9831# endif
9832#endif
9833
9834static int breakcheck_count = 0;
9835
9836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009837line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838{
9839 if (++breakcheck_count >= BREAKCHECK_SKIP)
9840 {
9841 breakcheck_count = 0;
9842 ui_breakcheck();
9843 }
9844}
9845
9846/*
9847 * Like line_breakcheck() but check 10 times less often.
9848 */
9849 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009850fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851{
9852 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9853 {
9854 breakcheck_count = 0;
9855 ui_breakcheck();
9856 }
9857}
9858
9859/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009860 * Invoke expand_wildcards() for one pattern.
9861 * Expand items like "%:h" before the expansion.
9862 * Returns OK or FAIL.
9863 */
9864 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009865expand_wildcards_eval(
9866 char_u **pat, /* pointer to input pattern */
9867 int *num_file, /* resulting number of files */
9868 char_u ***file, /* array of resulting files */
9869 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009870{
9871 int ret = FAIL;
9872 char_u *eval_pat = NULL;
9873 char_u *exp_pat = *pat;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009874 char *ignored_msg;
Bram Moolenaard7834d32009-12-02 16:14:36 +00009875 int usedlen;
9876
9877 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9878 {
9879 ++emsg_off;
9880 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9881 NULL, &ignored_msg, NULL);
9882 --emsg_off;
9883 if (eval_pat != NULL)
9884 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9885 }
9886
9887 if (exp_pat != NULL)
9888 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9889
9890 if (eval_pat != NULL)
9891 {
9892 vim_free(exp_pat);
9893 vim_free(eval_pat);
9894 }
9895
9896 return ret;
9897}
9898
9899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9901 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009902 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 */
9904 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009905expand_wildcards(
9906 int num_pat, /* number of input patterns */
9907 char_u **pat, /* array of input patterns */
9908 int *num_files, /* resulting number of files */
9909 char_u ***files, /* array of resulting files */
9910 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911{
9912 int retval;
9913 int i, j;
9914 char_u *p;
9915 int non_suf_match; /* number without matching suffix */
9916
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009917 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918
9919 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009920 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 return retval;
9922
9923#ifdef FEAT_WILDIGN
9924 /*
9925 * Remove names that match 'wildignore'.
9926 */
9927 if (*p_wig)
9928 {
9929 char_u *ffname;
9930
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009931 /* check all files in (*files)[] */
9932 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009934 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 if (ffname == NULL) /* out of memory */
9936 break;
9937# ifdef VMS
9938 vms_remove_version(ffname);
9939# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009940 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009942 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009943 vim_free((*files)[i]);
9944 for (j = i; j + 1 < *num_files; ++j)
9945 (*files)[j] = (*files)[j + 1];
9946 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 --i;
9948 }
9949 vim_free(ffname);
9950 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009951
9952 /* If the number of matches is now zero, we fail. */
9953 if (*num_files == 0)
9954 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01009955 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009956 return FAIL;
9957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 }
9959#endif
9960
9961 /*
9962 * Move the names where 'suffixes' match to the end.
9963 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009964 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 {
9966 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009967 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009969 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 {
9971 /*
9972 * Move the name without matching suffix to the front
9973 * of the list.
9974 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009975 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009977 (*files)[j] = (*files)[j - 1];
9978 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 }
9980 }
9981 }
9982
9983 return retval;
9984}
9985
9986/*
9987 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9988 */
9989 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009990match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991{
9992 int fnamelen, setsuflen;
9993 char_u *setsuf;
9994#define MAXSUFLEN 30 /* maximum length of a file suffix */
9995 char_u suf_buf[MAXSUFLEN];
9996
9997 fnamelen = (int)STRLEN(fname);
9998 setsuflen = 0;
9999 for (setsuf = p_su; *setsuf; )
10000 {
10001 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010002 if (setsuflen == 0)
10003 {
10004 char_u *tail = gettail(fname);
10005
10006 /* empty entry: match name without a '.' */
10007 if (vim_strchr(tail, '.') == NULL)
10008 {
10009 setsuflen = 1;
10010 break;
10011 }
10012 }
10013 else
10014 {
10015 if (fnamelen >= setsuflen
10016 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10017 (size_t)setsuflen) == 0)
10018 break;
10019 setsuflen = 0;
10020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 }
10022 return (setsuflen != 0);
10023}
10024
10025#if !defined(NO_EXPANDPATH) || defined(PROTO)
10026
10027# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010028static int vim_backtick(char_u *p);
10029static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030# endif
10031
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010032# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033/*
10034 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10035 * it's shared between these systems.
10036 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010037# if defined(PROTO)
10038# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039# else
10040# ifdef __BORLANDC__
10041# define _cdecl _RTLENTRYF
10042# endif
10043# endif
10044
10045/*
10046 * comparison function for qsort in dos_expandpath()
10047 */
10048 static int _cdecl
10049pstrcmp(const void *a, const void *b)
10050{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010051 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052}
10053
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010055 * Recursively expand one path component into all matching files and/or
10056 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 * Return the number of matches found.
10058 * "path" has backslashes before chars that are not to be expanded, starting
10059 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010060 * Return the number of matches found.
10061 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 */
10063 static int
10064dos_expandpath(
10065 garray_T *gap,
10066 char_u *path,
10067 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010068 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010069 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010071 char_u *buf;
10072 char_u *path_end;
10073 char_u *p, *s, *e;
10074 int start_len = gap->ga_len;
10075 char_u *pat;
10076 regmatch_T regmatch;
10077 int starts_with_dot;
10078 int matches;
10079 int len;
10080 int starstar = FALSE;
10081 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 WIN32_FIND_DATA fb;
10083 HANDLE hFind = (HANDLE)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 WIN32_FIND_DATAW wfb;
10085 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010087 int ok;
10088
10089 /* Expanding "**" may take a long time, check for CTRL-C. */
10090 if (stardepth > 0)
10091 {
10092 ui_breakcheck();
10093 if (got_int)
10094 return 0;
10095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096
Bram Moolenaarfc3abf42019-01-24 15:54:21 +010010097 // Make room for file name. When doing encoding conversion the actual
10098 // length may be quite a bit longer, thus use the maximum possible length.
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010099 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 if (buf == NULL)
10101 return 0;
10102
10103 /*
10104 * Find the first part in the path name that contains a wildcard or a ~1.
10105 * Copy it into buf, including the preceding characters.
10106 */
10107 p = buf;
10108 s = buf;
10109 e = NULL;
10110 path_end = path;
10111 while (*path_end != NUL)
10112 {
10113 /* May ignore a wildcard that has a backslash before it; it will
10114 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10115 if (path_end >= path + wildoff && rem_backslash(path_end))
10116 *p++ = *path_end++;
10117 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10118 {
10119 if (e != NULL)
10120 break;
10121 s = p + 1;
10122 }
10123 else if (path_end >= path + wildoff
10124 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10125 e = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 if (has_mbyte)
10127 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010128 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 STRNCPY(p, path_end, len);
10130 p += len;
10131 path_end += len;
10132 }
10133 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 *p++ = *path_end++;
10135 }
10136 e = p;
10137 *e = NUL;
10138
10139 /* now we have one wildcard component between s and e */
10140 /* Remove backslashes between "wildoff" and the start of the wildcard
10141 * component. */
10142 for (p = buf + wildoff; p < s; ++p)
10143 if (rem_backslash(p))
10144 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010145 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 --e;
10147 --s;
10148 }
10149
Bram Moolenaar231334e2005-07-25 20:46:57 +000010150 /* Check for "**" between "s" and "e". */
10151 for (p = s; p < e; ++p)
10152 if (p[0] == '*' && p[1] == '*')
10153 starstar = TRUE;
10154
Bram Moolenaard82103e2016-01-17 17:04:05 +010010155 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10157 if (pat == NULL)
10158 {
10159 vim_free(buf);
10160 return 0;
10161 }
10162
10163 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010164 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010165 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 regmatch.rm_ic = TRUE; /* Always ignore case */
10167 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010168 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010169 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 vim_free(pat);
10171
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010172 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 {
10174 vim_free(buf);
10175 return 0;
10176 }
10177
10178 /* remember the pattern or file name being looked for */
10179 matchname = vim_strsave(s);
10180
Bram Moolenaar231334e2005-07-25 20:46:57 +000010181 /* If "**" is by itself, this is the first time we encounter it and more
10182 * is following then find matches without any directory. */
10183 if (!didstar && stardepth < 100 && starstar && e - s == 2
10184 && *path_end == '/')
10185 {
10186 STRCPY(s, path_end + 1);
10187 ++stardepth;
10188 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10189 --stardepth;
10190 }
10191
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 /* Scan all files in the directory with "dir/ *.*" */
10193 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10195 {
10196 /* The active codepage differs from 'encoding'. Attempt using the
10197 * wide function. If it fails because it is not implemented fall back
10198 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010199 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 if (wn != NULL)
10201 {
10202 hFind = FindFirstFileW(wn, &wfb);
10203 if (hFind == INVALID_HANDLE_VALUE
10204 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010205 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 }
10207 }
10208
10209 if (wn == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010210 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212
10213 while (ok)
10214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010216 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 /* Ignore entries starting with a dot, unless when asked for. Accept
10220 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010221 if ((p[0] != '.' || starts_with_dot
10222 || ((flags & EW_DODOT)
10223 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010225 || (regmatch.regprog != NULL
10226 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010227 || ((flags & EW_NOTWILD)
10228 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010232
10233 if (starstar && stardepth < 100)
10234 {
10235 /* For "**" in the pattern first go deeper in the tree to
10236 * find matches. */
10237 STRCPY(buf + len, "/**");
10238 STRCPY(buf + len + 3, path_end);
10239 ++stardepth;
10240 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10241 --stardepth;
10242 }
10243
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 STRCPY(buf + len, path_end);
10245 if (mch_has_exp_wildcard(path_end))
10246 {
10247 /* need to expand another component of the path */
10248 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010249 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 }
10251 else
10252 {
10253 /* no more wildcards, check if there is a match */
10254 /* remove backslashes for the remaining components only */
10255 if (*path_end != 0)
10256 backslash_halve(buf + len + 1);
10257 if (mch_getperm(buf) >= 0) /* add existing file */
10258 addfile(gap, buf, flags);
10259 }
10260 }
10261
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 if (wn != NULL)
10263 {
10264 vim_free(p);
10265 ok = FindNextFileW(hFind, &wfb);
10266 }
10267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269
10270 /* If no more matches and no match was used, try expanding the name
10271 * itself. Finds the long name of a short filename. */
10272 if (!ok && matchname != NULL && gap->ga_len == start_len)
10273 {
10274 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 FindClose(hFind);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 if (wn != NULL)
10277 {
10278 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010279 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 if (wn != NULL)
10281 hFind = FindFirstFileW(wn, &wfb);
10282 }
10283 if (wn == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010284 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010286 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 }
10288 }
10289
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 FindClose(hFind);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010293 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 vim_free(matchname);
10295
10296 matches = gap->ga_len - start_len;
10297 if (matches > 0)
10298 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10299 sizeof(char_u *), pstrcmp);
10300 return matches;
10301}
10302
10303 int
10304mch_expandpath(
10305 garray_T *gap,
10306 char_u *path,
10307 int flags) /* EW_* flags */
10308{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010309 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010311# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312
Bram Moolenaar231334e2005-07-25 20:46:57 +000010313#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10314 || defined(PROTO)
10315/*
10316 * Unix style wildcard expansion code.
10317 * It's here because it's used both for Unix and Mac.
10318 */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010319 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010320pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010321{
10322 return (pathcmp(*(char **)a, *(char **)b, -1));
10323}
10324
10325/*
10326 * Recursively expand one path component into all matching files and/or
10327 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10328 * "path" has backslashes before chars that are not to be expanded, starting
10329 * at "path + wildoff".
10330 * Return the number of matches found.
10331 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10332 */
10333 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010334unix_expandpath(
10335 garray_T *gap,
10336 char_u *path,
10337 int wildoff,
10338 int flags, /* EW_* flags */
10339 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010340{
10341 char_u *buf;
10342 char_u *path_end;
10343 char_u *p, *s, *e;
10344 int start_len = gap->ga_len;
10345 char_u *pat;
10346 regmatch_T regmatch;
10347 int starts_with_dot;
10348 int matches;
10349 int len;
10350 int starstar = FALSE;
10351 static int stardepth = 0; /* depth for "**" expansion */
10352
10353 DIR *dirp;
10354 struct dirent *dp;
10355
10356 /* Expanding "**" may take a long time, check for CTRL-C. */
10357 if (stardepth > 0)
10358 {
10359 ui_breakcheck();
10360 if (got_int)
10361 return 0;
10362 }
10363
10364 /* make room for file name */
10365 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10366 if (buf == NULL)
10367 return 0;
10368
10369 /*
10370 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010371 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010372 * Copy it into "buf", including the preceding characters.
10373 */
10374 p = buf;
10375 s = buf;
10376 e = NULL;
10377 path_end = path;
10378 while (*path_end != NUL)
10379 {
10380 /* May ignore a wildcard that has a backslash before it; it will
10381 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10382 if (path_end >= path + wildoff && rem_backslash(path_end))
10383 *p++ = *path_end++;
10384 else if (*path_end == '/')
10385 {
10386 if (e != NULL)
10387 break;
10388 s = p + 1;
10389 }
10390 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010391 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010392 || (!p_fic && (flags & EW_ICASE)
10393 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010394 e = p;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010395 if (has_mbyte)
10396 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010397 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010398 STRNCPY(p, path_end, len);
10399 p += len;
10400 path_end += len;
10401 }
10402 else
Bram Moolenaar231334e2005-07-25 20:46:57 +000010403 *p++ = *path_end++;
10404 }
10405 e = p;
10406 *e = NUL;
10407
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010408 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010409 /* Remove backslashes between "wildoff" and the start of the wildcard
10410 * component. */
10411 for (p = buf + wildoff; p < s; ++p)
10412 if (rem_backslash(p))
10413 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010414 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010415 --e;
10416 --s;
10417 }
10418
10419 /* Check for "**" between "s" and "e". */
10420 for (p = s; p < e; ++p)
10421 if (p[0] == '*' && p[1] == '*')
10422 starstar = TRUE;
10423
10424 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010425 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010426 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10427 if (pat == NULL)
10428 {
10429 vim_free(buf);
10430 return 0;
10431 }
10432
10433 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010434 if (flags & EW_ICASE)
10435 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10436 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010437 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010438 if (flags & (EW_NOERROR | EW_NOTWILD))
10439 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010440 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010441 if (flags & (EW_NOERROR | EW_NOTWILD))
10442 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010443 vim_free(pat);
10444
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010445 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010446 {
10447 vim_free(buf);
10448 return 0;
10449 }
10450
10451 /* If "**" is by itself, this is the first time we encounter it and more
10452 * is following then find matches without any directory. */
10453 if (!didstar && stardepth < 100 && starstar && e - s == 2
10454 && *path_end == '/')
10455 {
10456 STRCPY(s, path_end + 1);
10457 ++stardepth;
10458 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10459 --stardepth;
10460 }
10461
10462 /* open the directory for scanning */
10463 *s = NUL;
10464 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10465
10466 /* Find all matching entries */
10467 if (dirp != NULL)
10468 {
10469 for (;;)
10470 {
10471 dp = readdir(dirp);
10472 if (dp == NULL)
10473 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010474 if ((dp->d_name[0] != '.' || starts_with_dot
10475 || ((flags & EW_DODOT)
10476 && dp->d_name[1] != NUL
10477 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010478 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10479 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010480 || ((flags & EW_NOTWILD)
10481 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010482 {
10483 STRCPY(s, dp->d_name);
10484 len = STRLEN(buf);
10485
10486 if (starstar && stardepth < 100)
10487 {
10488 /* For "**" in the pattern first go deeper in the tree to
10489 * find matches. */
10490 STRCPY(buf + len, "/**");
10491 STRCPY(buf + len + 3, path_end);
10492 ++stardepth;
10493 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10494 --stardepth;
10495 }
10496
10497 STRCPY(buf + len, path_end);
10498 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10499 {
10500 /* need to expand another component of the path */
10501 /* remove backslashes for the remaining components only */
10502 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10503 }
10504 else
10505 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010506 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010507
Bram Moolenaar231334e2005-07-25 20:46:57 +000010508 /* no more wildcards, check if there is a match */
10509 /* remove backslashes for the remaining components only */
10510 if (*path_end != NUL)
10511 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010512 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010513 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010514 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010515 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010516#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010517 size_t precomp_len = STRLEN(buf)+1;
10518 char_u *precomp_buf =
10519 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010520
Bram Moolenaar231334e2005-07-25 20:46:57 +000010521 if (precomp_buf)
10522 {
10523 mch_memmove(buf, precomp_buf, precomp_len);
10524 vim_free(precomp_buf);
10525 }
10526#endif
10527 addfile(gap, buf, flags);
10528 }
10529 }
10530 }
10531 }
10532
10533 closedir(dirp);
10534 }
10535
10536 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010537 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010538
10539 matches = gap->ga_len - start_len;
10540 if (matches > 0)
10541 qsort(((char_u **)gap->ga_data) + start_len, matches,
10542 sizeof(char_u *), pstrcmp);
10543 return matches;
10544}
10545#endif
10546
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010547#if defined(FEAT_SEARCHPATH)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010548/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010549 * Moves "*psep" back to the previous path separator in "path".
10550 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010551 */
10552 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010553find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010554{
10555 /* skip the current separator */
10556 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010557 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010558
10559 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010560 while (*psep > path)
10561 {
10562 if (vim_ispathsep(**psep))
10563 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010564 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010565 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010566
10567 return FAIL;
10568}
10569
10570/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010571 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10572 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010573 */
10574 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010575is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010576{
10577 int j;
10578 int candidate_len;
10579 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010580 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010581 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010582
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010583 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010584 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010585 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010586 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010587
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010588 candidate_len = (int)STRLEN(maybe_unique);
10589 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010590 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010591 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010592
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010593 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010594 if (fnamecmp(maybe_unique, rival) == 0
10595 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010596 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010597 }
10598
Bram Moolenaar162bd912010-07-28 22:29:10 +020010599 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010600}
10601
10602/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010603 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010604 * paths are expanded to their equivalent fullpath. This includes the "."
10605 * (relative to current buffer directory) and empty path (relative to current
10606 * directory) notations.
10607 *
10608 * TODO: handle upward search (;) and path limiter (**N) notations by
10609 * expanding each into their equivalent path(s).
10610 */
10611 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010612expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010613{
10614 char_u *path_option = *curbuf->b_p_path == NUL
10615 ? p_path : curbuf->b_p_path;
10616 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010617 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010618 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010619
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010620 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010621 return;
10622
10623 while (*path_option != NUL)
10624 {
10625 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10626
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010627 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010628 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010629 /* Relative to current buffer:
10630 * "/path/file" + "." -> "/path/"
10631 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010632 if (curbuf->b_ffname == NULL)
10633 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010634 p = gettail(curbuf->b_ffname);
10635 len = (int)(p - curbuf->b_ffname);
10636 if (len + (int)STRLEN(buf) >= MAXPATHL)
10637 continue;
10638 if (buf[1] == NUL)
10639 buf[len] = NUL;
10640 else
10641 STRMOVE(buf + len, buf + 2);
10642 mch_memmove(buf, curbuf->b_ffname, len);
10643 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010644 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010645 else if (buf[0] == NUL)
10646 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010647 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010648 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010649 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010650 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010651 else if (!mch_isFullName(buf))
10652 {
10653 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010654 len = (int)STRLEN(curdir);
10655 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010656 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010657 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010658 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010659 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010660 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010661 }
10662
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010663 if (ga_grow(gap, 1) == FAIL)
10664 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010665
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010666# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010667 /* Avoid the path ending in a backslash, it fails when a comma is
10668 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010669 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010670 if (buf[len - 1] == '\\')
10671 buf[len - 1] = '/';
10672# endif
10673
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010674 p = vim_strsave(buf);
10675 if (p == NULL)
10676 break;
10677 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010678 }
10679
10680 vim_free(buf);
10681}
10682
10683/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010684 * Returns a pointer to the file or directory name in "fname" that matches the
10685 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010686 *
10687 * path: /foo/bar/baz
10688 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010689 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010690 */
10691 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010692get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010693{
10694 int i;
10695 int maxlen = 0;
10696 char_u **path_part = (char_u **)gap->ga_data;
10697 char_u *cutoff = NULL;
10698
10699 for (i = 0; i < gap->ga_len; i++)
10700 {
10701 int j = 0;
10702
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010703 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010704# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010705 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10706#endif
10707 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010708 j++;
10709 if (j > maxlen)
10710 {
10711 maxlen = j;
10712 cutoff = &fname[j];
10713 }
10714 }
10715
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010716 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010717 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010718 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010719 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010720
10721 return cutoff;
10722}
10723
10724/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010725 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10726 * that they are unique with respect to each other while conserving the part
10727 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010728 */
10729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010730uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010731{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010732 int i;
10733 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010734 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010735 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010736 char_u *pat;
10737 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010738 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010739 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010740 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010741 char_u **in_curdir = NULL;
10742 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010743
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010744 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010745 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010746
10747 /*
10748 * We need to prepend a '*' at the beginning of file_pattern so that the
10749 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010750 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010751 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010752 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010753 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010754 if (file_pattern == NULL)
10755 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010756 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010757 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010758 STRCAT(file_pattern, pattern);
10759 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10760 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010761 if (pat == NULL)
10762 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010763
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010764 regmatch.rm_ic = TRUE; /* always ignore case */
10765 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10766 vim_free(pat);
10767 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010768 return;
10769
Bram Moolenaar162bd912010-07-28 22:29:10 +020010770 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010771 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010772 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010773 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010774
10775 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010776 if (in_curdir == NULL)
10777 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010778
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010779 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010780 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010781 char_u *path = fnames[i];
10782 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010783 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010784 char_u *pathsep_p;
10785 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010786
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010787 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010788 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010789 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010790 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010791 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010792
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010793 /* Shorten the filename while maintaining its uniqueness */
10794 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010795
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010796 /* Don't assume all files can be reached without path when search
10797 * pattern starts with star star slash, so only remove path_cutoff
10798 * when possible. */
10799 if (pattern[0] == '*' && pattern[1] == '*'
10800 && vim_ispathsep_nocolon(pattern[2])
10801 && path_cutoff != NULL
10802 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10803 && is_unique(path_cutoff, gap, i))
10804 {
10805 sort_again = TRUE;
10806 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10807 }
10808 else
10809 {
10810 /* Here all files can be reached without path, so get shortest
10811 * unique path. We start at the end of the path. */
10812 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010813
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010814 while (find_previous_pathsep(path, &pathsep_p))
10815 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10816 && is_unique(pathsep_p + 1, gap, i)
10817 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10818 {
10819 sort_again = TRUE;
10820 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10821 break;
10822 }
10823 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010824
10825 if (mch_isFullName(path))
10826 {
10827 /*
10828 * Last resort: shorten relative to curdir if possible.
10829 * 'possible' means:
10830 * 1. It is under the current directory.
10831 * 2. The result is actually shorter than the original.
10832 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010833 * Before curdir After
10834 * /foo/bar/file.txt /foo/bar ./file.txt
10835 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10836 * /file.txt / /file.txt
10837 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010838 */
10839 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010840 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010841#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010842 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010843 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010844 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010845 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010846 && !vim_ispathsep(*short_name)
10847#endif
10848 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010849 {
10850 STRCPY(path, ".");
10851 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010852 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010853 }
10854 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010855 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010856 }
10857
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010858 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010859 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010860 {
10861 char_u *rel_path;
10862 char_u *path = in_curdir[i];
10863
10864 if (path == NULL)
10865 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010866
10867 /* If the {filename} is not unique, change it to ./{filename}.
10868 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010869 short_name = shorten_fname(path, curdir);
10870 if (short_name == NULL)
10871 short_name = path;
10872 if (is_unique(short_name, gap, i))
10873 {
10874 STRCPY(fnames[i], short_name);
10875 continue;
10876 }
10877
10878 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10879 if (rel_path == NULL)
10880 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010881 STRCPY(rel_path, ".");
10882 add_pathsep(rel_path);
10883 STRCAT(rel_path, short_name);
10884
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010885 vim_free(fnames[i]);
10886 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010887 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010888 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010889 }
10890
Bram Moolenaar162bd912010-07-28 22:29:10 +020010891theend:
10892 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010893 if (in_curdir != NULL)
10894 {
10895 for (i = 0; i < gap->ga_len; i++)
10896 vim_free(in_curdir[i]);
10897 vim_free(in_curdir);
10898 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010899 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010900 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010901
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010902 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010903 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010904}
10905
10906/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010907 * Calls globpath() with 'path' values for the given pattern and stores the
10908 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010909 * Returns the total number of matches.
10910 */
10911 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010912expand_in_path(
10913 garray_T *gap,
10914 char_u *pattern,
10915 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010916{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010917 char_u *curdir;
10918 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010919 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010920 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010921
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010922 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010923 return 0;
10924 mch_dirname(curdir, MAXPATHL);
10925
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010926 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010927 expand_path_option(curdir, &path_ga);
10928 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010929 if (path_ga.ga_len == 0)
10930 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010931
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010932 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010933 ga_clear_strings(&path_ga);
10934 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010935 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010936
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010937 if (flags & EW_ICASE)
10938 glob_flags |= WILD_ICASE;
10939 if (flags & EW_ADDSLASH)
10940 glob_flags |= WILD_ADD_SLASH;
10941 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010942 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010943
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010944 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010945}
10946#endif
10947
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010948#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10949/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010950 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10951 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010952 */
10953 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010954remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010955{
10956 int i;
10957 int j;
10958 char_u **fnames = (char_u **)gap->ga_data;
10959
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010960 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010961 for (i = gap->ga_len - 1; i > 0; --i)
10962 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10963 {
10964 vim_free(fnames[i]);
10965 for (j = i + 1; j < gap->ga_len; ++j)
10966 fnames[j - 1] = fnames[j];
10967 --gap->ga_len;
10968 }
10969}
10970#endif
10971
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010972/*
10973 * Return TRUE if "p" contains what looks like an environment variable.
10974 * Allowing for escaping.
10975 */
10976 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010977has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010978{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010979 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010980 {
10981 if (*p == '\\' && p[1] != NUL)
10982 ++p;
10983 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010984#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010985 "$%"
10986#else
10987 "$"
10988#endif
10989 , *p) != NULL)
10990 return TRUE;
10991 }
10992 return FALSE;
10993}
10994
10995#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010996/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010997 * Return TRUE if "p" contains a special wildcard character, one that Vim
10998 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010999 */
11000 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011001has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011002{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011003 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011004 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011005 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011006 if (*p == '\\' && p[1] != NUL)
11007 ++p;
11008 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11009 return TRUE;
11010 }
11011 return FALSE;
11012}
11013#endif
11014
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015/*
11016 * Generic wildcard expansion code.
11017 *
11018 * Characters in "pat" that should not be expanded must be preceded with a
11019 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11020 *
11021 * Return FAIL when no single file was found. In this case "num_file" is not
11022 * set, and "file" may contain an error message.
11023 * Return OK when some files found. "num_file" is set to the number of
11024 * matches, "file" to the array of matches. Call FreeWild() later.
11025 */
11026 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011027gen_expand_wildcards(
11028 int num_pat, /* number of input patterns */
11029 char_u **pat, /* array of input patterns */
11030 int *num_file, /* resulting number of files */
11031 char_u ***file, /* array of resulting files */
11032 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033{
11034 int i;
11035 garray_T ga;
11036 char_u *p;
11037 static int recursive = FALSE;
11038 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011039 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011040#if defined(FEAT_SEARCHPATH)
11041 int did_expand_in_path = FALSE;
11042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043
11044 /*
11045 * expand_env() is called to expand things like "~user". If this fails,
11046 * it calls ExpandOne(), which brings us back here. In this case, always
11047 * call the machine specific expansion function, if possible. Otherwise,
11048 * return FAIL.
11049 */
11050 if (recursive)
11051#ifdef SPECIAL_WILDCHAR
11052 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11053#else
11054 return FAIL;
11055#endif
11056
11057#ifdef SPECIAL_WILDCHAR
11058 /*
11059 * If there are any special wildcard characters which we cannot handle
11060 * here, call machine specific function for all the expansion. This
11061 * avoids starting the shell for each argument separately.
11062 * For `=expr` do use the internal function.
11063 */
11064 for (i = 0; i < num_pat; i++)
11065 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011066 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067# ifdef VIM_BACKTICK
11068 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11069# endif
11070 )
11071 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11072 }
11073#endif
11074
11075 recursive = TRUE;
11076
11077 /*
11078 * The matching file names are stored in a growarray. Init it empty.
11079 */
11080 ga_init2(&ga, (int)sizeof(char_u *), 30);
11081
11082 for (i = 0; i < num_pat; ++i)
11083 {
11084 add_pat = -1;
11085 p = pat[i];
11086
11087#ifdef VIM_BACKTICK
11088 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011091 if (add_pat == -1)
11092 retval = FAIL;
11093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 else
11095#endif
11096 {
11097 /*
11098 * First expand environment variables, "~/" and "~user/".
11099 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011100 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011102 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 if (p == NULL)
11104 p = pat[i];
11105#ifdef UNIX
11106 /*
11107 * On Unix, if expand_env() can't expand an environment
11108 * variable, use the shell to do that. Discard previously
11109 * found file names and start all over again.
11110 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011111 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 {
11113 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011114 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011116 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 recursive = FALSE;
11118 return i;
11119 }
11120#endif
11121 }
11122
11123 /*
11124 * If there are wildcards: Expand file names and add each match to
11125 * the list. If there is no match, and EW_NOTFOUND is given, add
11126 * the pattern.
11127 * If there are no wildcards: Add the file name if it exists or
11128 * when EW_NOTFOUND is given.
11129 */
11130 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011131 {
11132#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011133 if ((flags & EW_PATH)
11134 && !mch_isFullName(p)
11135 && !(p[0] == '.'
11136 && (vim_ispathsep(p[1])
11137 || (p[1] == '.' && vim_ispathsep(p[2]))))
11138 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011139 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011140 /* :find completion where 'path' is used.
11141 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011142 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011143 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011144 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011145 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011146 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011147 else
11148#endif
11149 add_pat = mch_expandpath(&ga, p, flags);
11150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 }
11152
11153 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11154 {
11155 char_u *t = backslash_halve_save(p);
11156
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11158 * "vim c:/" work. */
11159 if (flags & EW_NOTFOUND)
11160 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011161 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 addfile(&ga, t, flags);
11163 vim_free(t);
11164 }
11165
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011166#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011167 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011168 uniquefy_paths(&ga, p);
11169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 if (p != pat[i])
11171 vim_free(p);
11172 }
11173
11174 *num_file = ga.ga_len;
11175 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11176
11177 recursive = FALSE;
11178
Bram Moolenaar336bd622016-01-17 18:23:58 +010011179 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180}
11181
11182# ifdef VIM_BACKTICK
11183
11184/*
11185 * Return TRUE if we can expand this backtick thing here.
11186 */
11187 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011188vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189{
11190 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11191}
11192
11193/*
11194 * Expand an item in `backticks` by executing it as a command.
11195 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011196 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 */
11198 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011199expand_backtick(
11200 garray_T *gap,
11201 char_u *pat,
11202 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203{
11204 char_u *p;
11205 char_u *cmd;
11206 char_u *buffer;
11207 int cnt = 0;
11208 int i;
11209
11210 /* Create the command: lop off the backticks. */
11211 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11212 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011213 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214
11215#ifdef FEAT_EVAL
11216 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011217 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 else
11219#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011220 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011221 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 vim_free(cmd);
11223 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011224 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225
11226 cmd = buffer;
11227 while (*cmd != NUL)
11228 {
11229 cmd = skipwhite(cmd); /* skip over white space */
11230 p = cmd;
11231 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11232 ++p;
11233 /* add an entry if it is not empty */
11234 if (p > cmd)
11235 {
11236 i = *p;
11237 *p = NUL;
11238 addfile(gap, cmd, flags);
11239 *p = i;
11240 ++cnt;
11241 }
11242 cmd = p;
11243 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11244 ++cmd;
11245 }
11246
11247 vim_free(buffer);
11248 return cnt;
11249}
11250# endif /* VIM_BACKTICK */
11251
11252/*
11253 * Add a file to a file list. Accepted flags:
11254 * EW_DIR add directories
11255 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011256 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 * EW_NOTFOUND add even when it doesn't exist
11258 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011259 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 */
11261 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011262addfile(
11263 garray_T *gap,
11264 char_u *f, /* filename */
11265 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266{
11267 char_u *p;
11268 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011269 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011271 /* if the file/dir/link doesn't exist, may not add it */
11272 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011273 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 return;
11275
11276#ifdef FNAME_ILLEGAL
11277 /* if the file/dir contains illegal characters, don't add it */
11278 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11279 return;
11280#endif
11281
11282 isdir = mch_isdir(f);
11283 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11284 return;
11285
Bram Moolenaarb5971142015-03-21 17:32:19 +010011286 /* If the file isn't executable, may not add it. Do accept directories.
11287 * When invoked from expand_shellcmd() do not use $PATH. */
11288 if (!isdir && (flags & EW_EXEC)
11289 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011290 return;
11291
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 /* Make room for another item in the file list. */
11293 if (ga_grow(gap, 1) == FAIL)
11294 return;
11295
11296 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11297 if (p == NULL)
11298 return;
11299
11300 STRCPY(p, f);
11301#ifdef BACKSLASH_IN_FILENAME
11302 slash_adjust(p);
11303#endif
11304 /*
11305 * Append a slash or backslash after directory names if none is present.
11306 */
11307#ifndef DONT_ADD_PATHSEP_TO_DIR
11308 if (isdir && (flags & EW_ADDSLASH))
11309 add_pathsep(p);
11310#endif
11311 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312}
11313#endif /* !NO_EXPANDPATH */
11314
11315#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11316
11317#ifndef SEEK_SET
11318# define SEEK_SET 0
11319#endif
11320#ifndef SEEK_END
11321# define SEEK_END 2
11322#endif
11323
11324/*
11325 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011326 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11327 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 * Returns an allocated string, or NULL for error.
11329 */
11330 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011331get_cmd_output(
11332 char_u *cmd,
11333 char_u *infile, /* optional input file name */
11334 int flags, /* can be SHELL_SILENT */
11335 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336{
11337 char_u *tempname;
11338 char_u *command;
11339 char_u *buffer = NULL;
11340 int len;
11341 int i = 0;
11342 FILE *fd;
11343
11344 if (check_restricted() || check_secure())
11345 return NULL;
11346
11347 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011348 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011350 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 return NULL;
11352 }
11353
11354 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011355 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 if (command == NULL)
11357 goto done;
11358
11359 /*
11360 * Call the shell to execute the command (errors are ignored).
11361 * Don't check timestamps here.
11362 */
11363 ++no_check_timestamps;
11364 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11365 --no_check_timestamps;
11366
11367 vim_free(command);
11368
11369 /*
11370 * read the names from the file into memory
11371 */
11372# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011373 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 fd = mch_fopen((char *)tempname, "r");
11375# else
11376 fd = mch_fopen((char *)tempname, READBIN);
11377# endif
11378
11379 if (fd == NULL)
11380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011381 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 goto done;
11383 }
11384
11385 fseek(fd, 0L, SEEK_END);
11386 len = ftell(fd); /* get size of temp file */
11387 fseek(fd, 0L, SEEK_SET);
11388
11389 buffer = alloc(len + 1);
11390 if (buffer != NULL)
11391 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11392 fclose(fd);
11393 mch_remove(tempname);
11394 if (buffer == NULL)
11395 goto done;
11396#ifdef VMS
11397 len = i; /* VMS doesn't give us what we asked for... */
11398#endif
11399 if (i != len)
11400 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011401 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011402 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011404 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011405 {
11406 /* Change NUL into SOH, otherwise the string is truncated. */
11407 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011408 if (buffer[i] == NUL)
11409 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011410
Bram Moolenaar162bd912010-07-28 22:29:10 +020011411 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011412 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011413 else
11414 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415
11416done:
11417 vim_free(tempname);
11418 return buffer;
11419}
11420#endif
11421
11422/*
11423 * Free the list of files returned by expand_wildcards() or other expansion
11424 * functions.
11425 */
11426 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011427FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011429 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 while (count--)
11432 vim_free(files[count]);
11433 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434}
11435
11436/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011437 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 * Don't do this when still processing a command or a mapping.
11439 * Don't do this when inside a ":normal" command.
11440 */
11441 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011442goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443{
11444 return (p_im && stuff_empty() && typebuf_typed());
11445}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011446
11447/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011448 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011449 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11450 * - Remove any argument. E.g., "csh -f" -> "csh".
11451 * But don't allow a space in the path, so that this works:
11452 * "/usr/bin/csh --rcfile ~/.cshrc"
11453 * But don't do that for Windows, it's common to have a space in the path.
11454 */
11455 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011456get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011457{
11458 char_u *p;
11459
11460#ifdef WIN3264
11461 p = gettail(p_sh);
11462 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11463#else
11464 p = skiptowhite(p_sh);
11465 if (*p == NUL)
11466 {
11467 /* No white space, use the tail. */
11468 p = vim_strsave(gettail(p_sh));
11469 }
11470 else
11471 {
11472 char_u *p1, *p2;
11473
11474 /* Find the last path separator before the space. */
11475 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011476 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011477 if (vim_ispathsep(*p2))
11478 p1 = p2 + 1;
11479 p = vim_strnsave(p1, (int)(p - p1));
11480 }
11481#endif
11482 return p;
11483}